[64/77] Add a scalar_mode class
[official-gcc.git] / gcc / expr.c
blob71a81ef1f3069bffb83209078c0b169dbe1a0a26
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "ssa.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "cgraph.h"
38 #include "diagnostic.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "attribs.h"
43 #include "varasm.h"
44 #include "except.h"
45 #include "insn-attr.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "stmt.h"
50 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
51 #include "expr.h"
52 #include "optabs-tree.h"
53 #include "libfuncs.h"
54 #include "reload.h"
55 #include "langhooks.h"
56 #include "common/common-target.h"
57 #include "tree-ssa-live.h"
58 #include "tree-outof-ssa.h"
59 #include "tree-ssa-address.h"
60 #include "builtins.h"
61 #include "tree-chkp.h"
62 #include "rtl-chkp.h"
63 #include "ccmp.h"
66 /* If this is nonzero, we do not bother generating VOLATILE
67 around volatile memory references, and we are willing to
68 output indirect addresses. If cse is to follow, we reject
69 indirect addresses so a useful potential cse is generated;
70 if it is used only once, instruction combination will produce
71 the same indirect address eventually. */
72 int cse_not_expected;
74 static bool block_move_libcall_safe_for_call_parm (void);
75 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
76 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT);
78 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
79 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
80 static rtx_insn *compress_float_constant (rtx, rtx);
81 static rtx get_subtarget (rtx);
82 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
83 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
84 unsigned HOST_WIDE_INT, machine_mode,
85 tree, int, alias_set_type, bool);
86 static void store_constructor (tree, rtx, int, HOST_WIDE_INT, bool);
87 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
88 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
89 machine_mode, tree, alias_set_type, bool, bool);
91 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
93 static int is_aligning_offset (const_tree, const_tree);
94 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
95 static rtx do_store_flag (sepops, rtx, machine_mode);
96 #ifdef PUSH_ROUNDING
97 static void emit_single_push_insn (machine_mode, rtx, tree);
98 #endif
99 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
100 profile_probability);
101 static rtx const_vector_from_tree (tree);
102 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
103 static tree tree_expr_size (const_tree);
104 static HOST_WIDE_INT int_expr_size (tree);
107 /* This is run to set up which modes can be used
108 directly in memory and to initialize the block move optab. It is run
109 at the beginning of compilation and when the target is reinitialized. */
111 void
112 init_expr_target (void)
114 rtx pat;
115 int num_clobbers;
116 rtx mem, mem1;
117 rtx reg;
119 /* Try indexing by frame ptr and try by stack ptr.
120 It is known that on the Convex the stack ptr isn't a valid index.
121 With luck, one or the other is valid on any machine. */
122 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
123 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
125 /* A scratch register we can modify in-place below to avoid
126 useless RTL allocations. */
127 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
129 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
130 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
131 PATTERN (insn) = pat;
133 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
134 mode = (machine_mode) ((int) mode + 1))
136 int regno;
138 direct_load[(int) mode] = direct_store[(int) mode] = 0;
139 PUT_MODE (mem, mode);
140 PUT_MODE (mem1, mode);
142 /* See if there is some register that can be used in this mode and
143 directly loaded or stored from memory. */
145 if (mode != VOIDmode && mode != BLKmode)
146 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
147 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
148 regno++)
150 if (! HARD_REGNO_MODE_OK (regno, mode))
151 continue;
153 set_mode_and_regno (reg, mode, regno);
155 SET_SRC (pat) = mem;
156 SET_DEST (pat) = reg;
157 if (recog (pat, insn, &num_clobbers) >= 0)
158 direct_load[(int) mode] = 1;
160 SET_SRC (pat) = mem1;
161 SET_DEST (pat) = reg;
162 if (recog (pat, insn, &num_clobbers) >= 0)
163 direct_load[(int) mode] = 1;
165 SET_SRC (pat) = reg;
166 SET_DEST (pat) = mem;
167 if (recog (pat, insn, &num_clobbers) >= 0)
168 direct_store[(int) mode] = 1;
170 SET_SRC (pat) = reg;
171 SET_DEST (pat) = mem1;
172 if (recog (pat, insn, &num_clobbers) >= 0)
173 direct_store[(int) mode] = 1;
177 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
179 opt_scalar_float_mode mode_iter;
180 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
182 scalar_float_mode mode = mode_iter.require ();
183 scalar_float_mode srcmode;
184 FOR_EACH_MODE_UNTIL (srcmode, mode)
186 enum insn_code ic;
188 ic = can_extend_p (mode, srcmode, 0);
189 if (ic == CODE_FOR_nothing)
190 continue;
192 PUT_MODE (mem, srcmode);
194 if (insn_operand_matches (ic, 1, mem))
195 float_extend_from_mem[mode][srcmode] = true;
200 /* This is run at the start of compiling a function. */
202 void
203 init_expr (void)
205 memset (&crtl->expr, 0, sizeof (crtl->expr));
208 /* Copy data from FROM to TO, where the machine modes are not the same.
209 Both modes may be integer, or both may be floating, or both may be
210 fixed-point.
211 UNSIGNEDP should be nonzero if FROM is an unsigned type.
212 This causes zero-extension instead of sign-extension. */
214 void
215 convert_move (rtx to, rtx from, int unsignedp)
217 machine_mode to_mode = GET_MODE (to);
218 machine_mode from_mode = GET_MODE (from);
219 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
220 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
221 enum insn_code code;
222 rtx libcall;
224 /* rtx code for making an equivalent value. */
225 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
226 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
229 gcc_assert (to_real == from_real);
230 gcc_assert (to_mode != BLKmode);
231 gcc_assert (from_mode != BLKmode);
233 /* If the source and destination are already the same, then there's
234 nothing to do. */
235 if (to == from)
236 return;
238 /* If FROM is a SUBREG that indicates that we have already done at least
239 the required extension, strip it. We don't handle such SUBREGs as
240 TO here. */
242 scalar_int_mode to_int_mode;
243 if (GET_CODE (from) == SUBREG
244 && SUBREG_PROMOTED_VAR_P (from)
245 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
246 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
247 >= GET_MODE_PRECISION (to_int_mode))
248 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
249 from = gen_lowpart (to_int_mode, from), from_mode = to_int_mode;
251 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
253 if (to_mode == from_mode
254 || (from_mode == VOIDmode && CONSTANT_P (from)))
256 emit_move_insn (to, from);
257 return;
260 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
262 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
264 if (VECTOR_MODE_P (to_mode))
265 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
266 else
267 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
269 emit_move_insn (to, from);
270 return;
273 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
275 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
276 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
277 return;
280 if (to_real)
282 rtx value;
283 rtx_insn *insns;
284 convert_optab tab;
286 gcc_assert ((GET_MODE_PRECISION (from_mode)
287 != GET_MODE_PRECISION (to_mode))
288 || (DECIMAL_FLOAT_MODE_P (from_mode)
289 != DECIMAL_FLOAT_MODE_P (to_mode)));
291 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
292 /* Conversion between decimal float and binary float, same size. */
293 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
294 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
295 tab = sext_optab;
296 else
297 tab = trunc_optab;
299 /* Try converting directly if the insn is supported. */
301 code = convert_optab_handler (tab, to_mode, from_mode);
302 if (code != CODE_FOR_nothing)
304 emit_unop_insn (code, to, from,
305 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
306 return;
309 /* Otherwise use a libcall. */
310 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
312 /* Is this conversion implemented yet? */
313 gcc_assert (libcall);
315 start_sequence ();
316 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
317 1, from, from_mode);
318 insns = get_insns ();
319 end_sequence ();
320 emit_libcall_block (insns, to, value,
321 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
322 from)
323 : gen_rtx_FLOAT_EXTEND (to_mode, from));
324 return;
327 /* Handle pointer conversion. */ /* SPEE 900220. */
328 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
330 convert_optab ctab;
332 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
333 ctab = trunc_optab;
334 else if (unsignedp)
335 ctab = zext_optab;
336 else
337 ctab = sext_optab;
339 if (convert_optab_handler (ctab, to_mode, from_mode)
340 != CODE_FOR_nothing)
342 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
343 to, from, UNKNOWN);
344 return;
348 /* Targets are expected to provide conversion insns between PxImode and
349 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
350 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
352 scalar_int_mode full_mode
353 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
355 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
356 != CODE_FOR_nothing);
358 if (full_mode != from_mode)
359 from = convert_to_mode (full_mode, from, unsignedp);
360 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
361 to, from, UNKNOWN);
362 return;
364 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
366 rtx new_from;
367 scalar_int_mode full_mode
368 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
369 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
370 enum insn_code icode;
372 icode = convert_optab_handler (ctab, full_mode, from_mode);
373 gcc_assert (icode != CODE_FOR_nothing);
375 if (to_mode == full_mode)
377 emit_unop_insn (icode, to, from, UNKNOWN);
378 return;
381 new_from = gen_reg_rtx (full_mode);
382 emit_unop_insn (icode, new_from, from, UNKNOWN);
384 /* else proceed to integer conversions below. */
385 from_mode = full_mode;
386 from = new_from;
389 /* Make sure both are fixed-point modes or both are not. */
390 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
391 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
392 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
394 /* If we widen from_mode to to_mode and they are in the same class,
395 we won't saturate the result.
396 Otherwise, always saturate the result to play safe. */
397 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
398 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
399 expand_fixed_convert (to, from, 0, 0);
400 else
401 expand_fixed_convert (to, from, 0, 1);
402 return;
405 /* Now both modes are integers. */
407 /* Handle expanding beyond a word. */
408 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
409 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
411 rtx_insn *insns;
412 rtx lowpart;
413 rtx fill_value;
414 rtx lowfrom;
415 int i;
416 machine_mode lowpart_mode;
417 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
419 /* Try converting directly if the insn is supported. */
420 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
421 != CODE_FOR_nothing)
423 /* If FROM is a SUBREG, put it into a register. Do this
424 so that we always generate the same set of insns for
425 better cse'ing; if an intermediate assignment occurred,
426 we won't be doing the operation directly on the SUBREG. */
427 if (optimize > 0 && GET_CODE (from) == SUBREG)
428 from = force_reg (from_mode, from);
429 emit_unop_insn (code, to, from, equiv_code);
430 return;
432 /* Next, try converting via full word. */
433 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
434 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
435 != CODE_FOR_nothing))
437 rtx word_to = gen_reg_rtx (word_mode);
438 if (REG_P (to))
440 if (reg_overlap_mentioned_p (to, from))
441 from = force_reg (from_mode, from);
442 emit_clobber (to);
444 convert_move (word_to, from, unsignedp);
445 emit_unop_insn (code, to, word_to, equiv_code);
446 return;
449 /* No special multiword conversion insn; do it by hand. */
450 start_sequence ();
452 /* Since we will turn this into a no conflict block, we must ensure
453 the source does not overlap the target so force it into an isolated
454 register when maybe so. Likewise for any MEM input, since the
455 conversion sequence might require several references to it and we
456 must ensure we're getting the same value every time. */
458 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
459 from = force_reg (from_mode, from);
461 /* Get a copy of FROM widened to a word, if necessary. */
462 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
463 lowpart_mode = word_mode;
464 else
465 lowpart_mode = from_mode;
467 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
469 lowpart = gen_lowpart (lowpart_mode, to);
470 emit_move_insn (lowpart, lowfrom);
472 /* Compute the value to put in each remaining word. */
473 if (unsignedp)
474 fill_value = const0_rtx;
475 else
476 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
477 LT, lowfrom, const0_rtx,
478 lowpart_mode, 0, -1);
480 /* Fill the remaining words. */
481 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
483 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
484 rtx subword = operand_subword (to, index, 1, to_mode);
486 gcc_assert (subword);
488 if (fill_value != subword)
489 emit_move_insn (subword, fill_value);
492 insns = get_insns ();
493 end_sequence ();
495 emit_insn (insns);
496 return;
499 /* Truncating multi-word to a word or less. */
500 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
501 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
503 if (!((MEM_P (from)
504 && ! MEM_VOLATILE_P (from)
505 && direct_load[(int) to_mode]
506 && ! mode_dependent_address_p (XEXP (from, 0),
507 MEM_ADDR_SPACE (from)))
508 || REG_P (from)
509 || GET_CODE (from) == SUBREG))
510 from = force_reg (from_mode, from);
511 convert_move (to, gen_lowpart (word_mode, from), 0);
512 return;
515 /* Now follow all the conversions between integers
516 no more than a word long. */
518 /* For truncation, usually we can just refer to FROM in a narrower mode. */
519 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
520 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
522 if (!((MEM_P (from)
523 && ! MEM_VOLATILE_P (from)
524 && direct_load[(int) to_mode]
525 && ! mode_dependent_address_p (XEXP (from, 0),
526 MEM_ADDR_SPACE (from)))
527 || REG_P (from)
528 || GET_CODE (from) == SUBREG))
529 from = force_reg (from_mode, from);
530 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
531 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
532 from = copy_to_reg (from);
533 emit_move_insn (to, gen_lowpart (to_mode, from));
534 return;
537 /* Handle extension. */
538 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
540 /* Convert directly if that works. */
541 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
542 != CODE_FOR_nothing)
544 emit_unop_insn (code, to, from, equiv_code);
545 return;
547 else
549 machine_mode intermediate;
550 rtx tmp;
551 int shift_amount;
553 /* Search for a mode to convert via. */
554 FOR_EACH_MODE_FROM (intermediate, from_mode)
555 if (((can_extend_p (to_mode, intermediate, unsignedp)
556 != CODE_FOR_nothing)
557 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
558 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
559 && (can_extend_p (intermediate, from_mode, unsignedp)
560 != CODE_FOR_nothing))
562 convert_move (to, convert_to_mode (intermediate, from,
563 unsignedp), unsignedp);
564 return;
567 /* No suitable intermediate mode.
568 Generate what we need with shifts. */
569 shift_amount = (GET_MODE_PRECISION (to_mode)
570 - GET_MODE_PRECISION (from_mode));
571 from = gen_lowpart (to_mode, force_reg (from_mode, from));
572 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
573 to, unsignedp);
574 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
575 to, unsignedp);
576 if (tmp != to)
577 emit_move_insn (to, tmp);
578 return;
582 /* Support special truncate insns for certain modes. */
583 if (convert_optab_handler (trunc_optab, to_mode,
584 from_mode) != CODE_FOR_nothing)
586 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
587 to, from, UNKNOWN);
588 return;
591 /* Handle truncation of volatile memrefs, and so on;
592 the things that couldn't be truncated directly,
593 and for which there was no special instruction.
595 ??? Code above formerly short-circuited this, for most integer
596 mode pairs, with a force_reg in from_mode followed by a recursive
597 call to this routine. Appears always to have been wrong. */
598 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
600 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
601 emit_move_insn (to, temp);
602 return;
605 /* Mode combination is not recognized. */
606 gcc_unreachable ();
609 /* Return an rtx for a value that would result
610 from converting X to mode MODE.
611 Both X and MODE may be floating, or both integer.
612 UNSIGNEDP is nonzero if X is an unsigned value.
613 This can be done by referring to a part of X in place
614 or by copying to a new temporary with conversion. */
617 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
619 return convert_modes (mode, VOIDmode, x, unsignedp);
622 /* Return an rtx for a value that would result
623 from converting X from mode OLDMODE to mode MODE.
624 Both modes may be floating, or both integer.
625 UNSIGNEDP is nonzero if X is an unsigned value.
627 This can be done by referring to a part of X in place
628 or by copying to a new temporary with conversion.
630 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
633 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
635 rtx temp;
636 scalar_int_mode int_mode;
638 /* If FROM is a SUBREG that indicates that we have already done at least
639 the required extension, strip it. */
641 if (GET_CODE (x) == SUBREG
642 && SUBREG_PROMOTED_VAR_P (x)
643 && is_a <scalar_int_mode> (mode, &int_mode)
644 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
645 >= GET_MODE_PRECISION (int_mode))
646 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
647 x = gen_lowpart (int_mode, SUBREG_REG (x));
649 if (GET_MODE (x) != VOIDmode)
650 oldmode = GET_MODE (x);
652 if (mode == oldmode)
653 return x;
655 if (CONST_SCALAR_INT_P (x)
656 && is_int_mode (mode, &int_mode))
658 /* If the caller did not tell us the old mode, then there is not
659 much to do with respect to canonicalization. We have to
660 assume that all the bits are significant. */
661 if (GET_MODE_CLASS (oldmode) != MODE_INT)
662 oldmode = MAX_MODE_INT;
663 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
664 GET_MODE_PRECISION (int_mode),
665 unsignedp ? UNSIGNED : SIGNED);
666 return immed_wide_int_const (w, int_mode);
669 /* We can do this with a gen_lowpart if both desired and current modes
670 are integer, and this is either a constant integer, a register, or a
671 non-volatile MEM. */
672 scalar_int_mode int_oldmode;
673 if (is_int_mode (mode, &int_mode)
674 && is_int_mode (oldmode, &int_oldmode)
675 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
676 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
677 || (REG_P (x)
678 && (!HARD_REGISTER_P (x)
679 || HARD_REGNO_MODE_OK (REGNO (x), int_mode))
680 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
681 return gen_lowpart (int_mode, x);
683 /* Converting from integer constant into mode is always equivalent to an
684 subreg operation. */
685 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
687 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
688 return simplify_gen_subreg (mode, x, oldmode, 0);
691 temp = gen_reg_rtx (mode);
692 convert_move (temp, x, unsignedp);
693 return temp;
696 /* Return the largest alignment we can use for doing a move (or store)
697 of MAX_PIECES. ALIGN is the largest alignment we could use. */
699 static unsigned int
700 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
702 scalar_int_mode tmode
703 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
705 if (align >= GET_MODE_ALIGNMENT (tmode))
706 align = GET_MODE_ALIGNMENT (tmode);
707 else
709 scalar_int_mode xmode = NARROWEST_INT_MODE;
710 opt_scalar_int_mode mode_iter;
711 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
713 tmode = mode_iter.require ();
714 if (GET_MODE_SIZE (tmode) > max_pieces
715 || SLOW_UNALIGNED_ACCESS (tmode, align))
716 break;
717 xmode = tmode;
720 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
723 return align;
726 /* Return the widest integer mode that is narrower than SIZE bytes. */
728 static scalar_int_mode
729 widest_int_mode_for_size (unsigned int size)
731 scalar_int_mode result = NARROWEST_INT_MODE;
733 gcc_checking_assert (size > 1);
735 opt_scalar_int_mode tmode;
736 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
737 if (GET_MODE_SIZE (tmode.require ()) < size)
738 result = tmode.require ();
740 return result;
743 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
744 and should be performed piecewise. */
746 static bool
747 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
748 enum by_pieces_operation op)
750 return targetm.use_by_pieces_infrastructure_p (len, align, op,
751 optimize_insn_for_speed_p ());
754 /* Determine whether the LEN bytes can be moved by using several move
755 instructions. Return nonzero if a call to move_by_pieces should
756 succeed. */
758 bool
759 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
761 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
764 /* Return number of insns required to perform operation OP by pieces
765 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
767 unsigned HOST_WIDE_INT
768 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
769 unsigned int max_size, by_pieces_operation op)
771 unsigned HOST_WIDE_INT n_insns = 0;
773 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
775 while (max_size > 1 && l > 0)
777 scalar_int_mode mode = widest_int_mode_for_size (max_size);
778 enum insn_code icode;
780 unsigned int modesize = GET_MODE_SIZE (mode);
782 icode = optab_handler (mov_optab, mode);
783 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
785 unsigned HOST_WIDE_INT n_pieces = l / modesize;
786 l %= modesize;
787 switch (op)
789 default:
790 n_insns += n_pieces;
791 break;
793 case COMPARE_BY_PIECES:
794 int batch = targetm.compare_by_pieces_branch_ratio (mode);
795 int batch_ops = 4 * batch - 1;
796 unsigned HOST_WIDE_INT full = n_pieces / batch;
797 n_insns += full * batch_ops;
798 if (n_pieces % batch != 0)
799 n_insns++;
800 break;
804 max_size = modesize;
807 gcc_assert (!l);
808 return n_insns;
811 /* Used when performing piecewise block operations, holds information
812 about one of the memory objects involved. The member functions
813 can be used to generate code for loading from the object and
814 updating the address when iterating. */
816 class pieces_addr
818 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
819 stack pushes. */
820 rtx m_obj;
821 /* The address of the object. Can differ from that seen in the
822 MEM rtx if we copied the address to a register. */
823 rtx m_addr;
824 /* Nonzero if the address on the object has an autoincrement already,
825 signifies whether that was an increment or decrement. */
826 signed char m_addr_inc;
827 /* Nonzero if we intend to use autoinc without the address already
828 having autoinc form. We will insert add insns around each memory
829 reference, expecting later passes to form autoinc addressing modes.
830 The only supported options are predecrement and postincrement. */
831 signed char m_explicit_inc;
832 /* True if we have either of the two possible cases of using
833 autoincrement. */
834 bool m_auto;
835 /* True if this is an address to be used for load operations rather
836 than stores. */
837 bool m_is_load;
839 /* Optionally, a function to obtain constants for any given offset into
840 the objects, and data associated with it. */
841 by_pieces_constfn m_constfn;
842 void *m_cfndata;
843 public:
844 pieces_addr (rtx, bool, by_pieces_constfn, void *);
845 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
846 void increment_address (HOST_WIDE_INT);
847 void maybe_predec (HOST_WIDE_INT);
848 void maybe_postinc (HOST_WIDE_INT);
849 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
850 int get_addr_inc ()
852 return m_addr_inc;
856 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
857 true if the operation to be performed on this object is a load
858 rather than a store. For stores, OBJ can be NULL, in which case we
859 assume the operation is a stack push. For loads, the optional
860 CONSTFN and its associated CFNDATA can be used in place of the
861 memory load. */
863 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
864 void *cfndata)
865 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
867 m_addr_inc = 0;
868 m_auto = false;
869 if (obj)
871 rtx addr = XEXP (obj, 0);
872 rtx_code code = GET_CODE (addr);
873 m_addr = addr;
874 bool dec = code == PRE_DEC || code == POST_DEC;
875 bool inc = code == PRE_INC || code == POST_INC;
876 m_auto = inc || dec;
877 if (m_auto)
878 m_addr_inc = dec ? -1 : 1;
880 /* While we have always looked for these codes here, the code
881 implementing the memory operation has never handled them.
882 Support could be added later if necessary or beneficial. */
883 gcc_assert (code != PRE_INC && code != POST_DEC);
885 else
887 m_addr = NULL_RTX;
888 if (!is_load)
890 m_auto = true;
891 if (STACK_GROWS_DOWNWARD)
892 m_addr_inc = -1;
893 else
894 m_addr_inc = 1;
896 else
897 gcc_assert (constfn != NULL);
899 m_explicit_inc = 0;
900 if (constfn)
901 gcc_assert (is_load);
904 /* Decide whether to use autoinc for an address involved in a memory op.
905 MODE is the mode of the accesses, REVERSE is true if we've decided to
906 perform the operation starting from the end, and LEN is the length of
907 the operation. Don't override an earlier decision to set m_auto. */
909 void
910 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
911 HOST_WIDE_INT len)
913 if (m_auto || m_obj == NULL_RTX)
914 return;
916 bool use_predec = (m_is_load
917 ? USE_LOAD_PRE_DECREMENT (mode)
918 : USE_STORE_PRE_DECREMENT (mode));
919 bool use_postinc = (m_is_load
920 ? USE_LOAD_POST_INCREMENT (mode)
921 : USE_STORE_POST_INCREMENT (mode));
922 machine_mode addr_mode = get_address_mode (m_obj);
924 if (use_predec && reverse)
926 m_addr = copy_to_mode_reg (addr_mode,
927 plus_constant (addr_mode,
928 m_addr, len));
929 m_auto = true;
930 m_explicit_inc = -1;
932 else if (use_postinc && !reverse)
934 m_addr = copy_to_mode_reg (addr_mode, m_addr);
935 m_auto = true;
936 m_explicit_inc = 1;
938 else if (CONSTANT_P (m_addr))
939 m_addr = copy_to_mode_reg (addr_mode, m_addr);
942 /* Adjust the address to refer to the data at OFFSET in MODE. If we
943 are using autoincrement for this address, we don't add the offset,
944 but we still modify the MEM's properties. */
947 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
949 if (m_constfn)
950 return m_constfn (m_cfndata, offset, mode);
951 if (m_obj == NULL_RTX)
952 return NULL_RTX;
953 if (m_auto)
954 return adjust_automodify_address (m_obj, mode, m_addr, offset);
955 else
956 return adjust_address (m_obj, mode, offset);
959 /* Emit an add instruction to increment the address by SIZE. */
961 void
962 pieces_addr::increment_address (HOST_WIDE_INT size)
964 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
965 emit_insn (gen_add2_insn (m_addr, amount));
968 /* If we are supposed to decrement the address after each access, emit code
969 to do so now. Increment by SIZE (which has should have the correct sign
970 already). */
972 void
973 pieces_addr::maybe_predec (HOST_WIDE_INT size)
975 if (m_explicit_inc >= 0)
976 return;
977 gcc_assert (HAVE_PRE_DECREMENT);
978 increment_address (size);
981 /* If we are supposed to decrement the address after each access, emit code
982 to do so now. Increment by SIZE. */
984 void
985 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
987 if (m_explicit_inc <= 0)
988 return;
989 gcc_assert (HAVE_POST_INCREMENT);
990 increment_address (size);
993 /* This structure is used by do_op_by_pieces to describe the operation
994 to be performed. */
996 class op_by_pieces_d
998 protected:
999 pieces_addr m_to, m_from;
1000 unsigned HOST_WIDE_INT m_len;
1001 HOST_WIDE_INT m_offset;
1002 unsigned int m_align;
1003 unsigned int m_max_size;
1004 bool m_reverse;
1006 /* Virtual functions, overriden by derived classes for the specific
1007 operation. */
1008 virtual void generate (rtx, rtx, machine_mode) = 0;
1009 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1010 virtual void finish_mode (machine_mode)
1014 public:
1015 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1016 unsigned HOST_WIDE_INT, unsigned int);
1017 void run ();
1020 /* The constructor for an op_by_pieces_d structure. We require two
1021 objects named TO and FROM, which are identified as loads or stores
1022 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1023 and its associated FROM_CFN_DATA can be used to replace loads with
1024 constant values. LEN describes the length of the operation. */
1026 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1027 rtx from, bool from_load,
1028 by_pieces_constfn from_cfn,
1029 void *from_cfn_data,
1030 unsigned HOST_WIDE_INT len,
1031 unsigned int align)
1032 : m_to (to, to_load, NULL, NULL),
1033 m_from (from, from_load, from_cfn, from_cfn_data),
1034 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1036 int toi = m_to.get_addr_inc ();
1037 int fromi = m_from.get_addr_inc ();
1038 if (toi >= 0 && fromi >= 0)
1039 m_reverse = false;
1040 else if (toi <= 0 && fromi <= 0)
1041 m_reverse = true;
1042 else
1043 gcc_unreachable ();
1045 m_offset = m_reverse ? len : 0;
1046 align = MIN (to ? MEM_ALIGN (to) : align,
1047 from ? MEM_ALIGN (from) : align);
1049 /* If copying requires more than two move insns,
1050 copy addresses to registers (to make displacements shorter)
1051 and use post-increment if available. */
1052 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1054 /* Find the mode of the largest comparison. */
1055 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1057 m_from.decide_autoinc (mode, m_reverse, len);
1058 m_to.decide_autoinc (mode, m_reverse, len);
1061 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1062 m_align = align;
1065 /* This function contains the main loop used for expanding a block
1066 operation. First move what we can in the largest integer mode,
1067 then go to successively smaller modes. For every access, call
1068 GENFUN with the two operands and the EXTRA_DATA. */
1070 void
1071 op_by_pieces_d::run ()
1073 while (m_max_size > 1 && m_len > 0)
1075 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1077 if (prepare_mode (mode, m_align))
1079 unsigned int size = GET_MODE_SIZE (mode);
1080 rtx to1 = NULL_RTX, from1;
1082 while (m_len >= size)
1084 if (m_reverse)
1085 m_offset -= size;
1087 to1 = m_to.adjust (mode, m_offset);
1088 from1 = m_from.adjust (mode, m_offset);
1090 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1091 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1093 generate (to1, from1, mode);
1095 m_to.maybe_postinc (size);
1096 m_from.maybe_postinc (size);
1098 if (!m_reverse)
1099 m_offset += size;
1101 m_len -= size;
1104 finish_mode (mode);
1107 m_max_size = GET_MODE_SIZE (mode);
1110 /* The code above should have handled everything. */
1111 gcc_assert (!m_len);
1114 /* Derived class from op_by_pieces_d, providing support for block move
1115 operations. */
1117 class move_by_pieces_d : public op_by_pieces_d
1119 insn_gen_fn m_gen_fun;
1120 void generate (rtx, rtx, machine_mode);
1121 bool prepare_mode (machine_mode, unsigned int);
1123 public:
1124 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1125 unsigned int align)
1126 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1129 rtx finish_endp (int);
1132 /* Return true if MODE can be used for a set of copies, given an
1133 alignment ALIGN. Prepare whatever data is necessary for later
1134 calls to generate. */
1136 bool
1137 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1139 insn_code icode = optab_handler (mov_optab, mode);
1140 m_gen_fun = GEN_FCN (icode);
1141 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1144 /* A callback used when iterating for a compare_by_pieces_operation.
1145 OP0 and OP1 are the values that have been loaded and should be
1146 compared in MODE. If OP0 is NULL, this means we should generate a
1147 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1148 gen function that should be used to generate the mode. */
1150 void
1151 move_by_pieces_d::generate (rtx op0, rtx op1,
1152 machine_mode mode ATTRIBUTE_UNUSED)
1154 #ifdef PUSH_ROUNDING
1155 if (op0 == NULL_RTX)
1157 emit_single_push_insn (mode, op1, NULL);
1158 return;
1160 #endif
1161 emit_insn (m_gen_fun (op0, op1));
1164 /* Perform the final adjustment at the end of a string to obtain the
1165 correct return value for the block operation. If ENDP is 1 return
1166 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1167 end minus one byte ala stpcpy. */
1170 move_by_pieces_d::finish_endp (int endp)
1172 gcc_assert (!m_reverse);
1173 if (endp == 2)
1175 m_to.maybe_postinc (-1);
1176 --m_offset;
1178 return m_to.adjust (QImode, m_offset);
1181 /* Generate several move instructions to copy LEN bytes from block FROM to
1182 block TO. (These are MEM rtx's with BLKmode).
1184 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1185 used to push FROM to the stack.
1187 ALIGN is maximum stack alignment we can assume.
1189 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1190 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1191 stpcpy. */
1194 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1195 unsigned int align, int endp)
1197 #ifndef PUSH_ROUNDING
1198 if (to == NULL)
1199 gcc_unreachable ();
1200 #endif
1202 move_by_pieces_d data (to, from, len, align);
1204 data.run ();
1206 if (endp)
1207 return data.finish_endp (endp);
1208 else
1209 return to;
1212 /* Derived class from op_by_pieces_d, providing support for block move
1213 operations. */
1215 class store_by_pieces_d : public op_by_pieces_d
1217 insn_gen_fn m_gen_fun;
1218 void generate (rtx, rtx, machine_mode);
1219 bool prepare_mode (machine_mode, unsigned int);
1221 public:
1222 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1223 unsigned HOST_WIDE_INT len, unsigned int align)
1224 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1227 rtx finish_endp (int);
1230 /* Return true if MODE can be used for a set of stores, given an
1231 alignment ALIGN. Prepare whatever data is necessary for later
1232 calls to generate. */
1234 bool
1235 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1237 insn_code icode = optab_handler (mov_optab, mode);
1238 m_gen_fun = GEN_FCN (icode);
1239 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1242 /* A callback used when iterating for a store_by_pieces_operation.
1243 OP0 and OP1 are the values that have been loaded and should be
1244 compared in MODE. If OP0 is NULL, this means we should generate a
1245 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1246 gen function that should be used to generate the mode. */
1248 void
1249 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1251 emit_insn (m_gen_fun (op0, op1));
1254 /* Perform the final adjustment at the end of a string to obtain the
1255 correct return value for the block operation. If ENDP is 1 return
1256 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1257 end minus one byte ala stpcpy. */
1260 store_by_pieces_d::finish_endp (int endp)
1262 gcc_assert (!m_reverse);
1263 if (endp == 2)
1265 m_to.maybe_postinc (-1);
1266 --m_offset;
1268 return m_to.adjust (QImode, m_offset);
1271 /* Determine whether the LEN bytes generated by CONSTFUN can be
1272 stored to memory using several move instructions. CONSTFUNDATA is
1273 a pointer which will be passed as argument in every CONSTFUN call.
1274 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1275 a memset operation and false if it's a copy of a constant string.
1276 Return nonzero if a call to store_by_pieces should succeed. */
1279 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1280 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1281 void *constfundata, unsigned int align, bool memsetp)
1283 unsigned HOST_WIDE_INT l;
1284 unsigned int max_size;
1285 HOST_WIDE_INT offset = 0;
1286 enum insn_code icode;
1287 int reverse;
1288 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1289 rtx cst ATTRIBUTE_UNUSED;
1291 if (len == 0)
1292 return 1;
1294 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1295 memsetp
1296 ? SET_BY_PIECES
1297 : STORE_BY_PIECES,
1298 optimize_insn_for_speed_p ()))
1299 return 0;
1301 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1303 /* We would first store what we can in the largest integer mode, then go to
1304 successively smaller modes. */
1306 for (reverse = 0;
1307 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1308 reverse++)
1310 l = len;
1311 max_size = STORE_MAX_PIECES + 1;
1312 while (max_size > 1 && l > 0)
1314 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1316 icode = optab_handler (mov_optab, mode);
1317 if (icode != CODE_FOR_nothing
1318 && align >= GET_MODE_ALIGNMENT (mode))
1320 unsigned int size = GET_MODE_SIZE (mode);
1322 while (l >= size)
1324 if (reverse)
1325 offset -= size;
1327 cst = (*constfun) (constfundata, offset, mode);
1328 if (!targetm.legitimate_constant_p (mode, cst))
1329 return 0;
1331 if (!reverse)
1332 offset += size;
1334 l -= size;
1338 max_size = GET_MODE_SIZE (mode);
1341 /* The code above should have handled everything. */
1342 gcc_assert (!l);
1345 return 1;
1348 /* Generate several move instructions to store LEN bytes generated by
1349 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1350 pointer which will be passed as argument in every CONSTFUN call.
1351 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1352 a memset operation and false if it's a copy of a constant string.
1353 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1354 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1355 stpcpy. */
1358 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1359 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1360 void *constfundata, unsigned int align, bool memsetp, int endp)
1362 if (len == 0)
1364 gcc_assert (endp != 2);
1365 return to;
1368 gcc_assert (targetm.use_by_pieces_infrastructure_p
1369 (len, align,
1370 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1371 optimize_insn_for_speed_p ()));
1373 store_by_pieces_d data (to, constfun, constfundata, len, align);
1374 data.run ();
1376 if (endp)
1377 return data.finish_endp (endp);
1378 else
1379 return to;
1382 /* Callback routine for clear_by_pieces.
1383 Return const0_rtx unconditionally. */
1385 static rtx
1386 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1388 return const0_rtx;
1391 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1392 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1394 static void
1395 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1397 if (len == 0)
1398 return;
1400 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1401 data.run ();
1404 /* Context used by compare_by_pieces_genfn. It stores the fail label
1405 to jump to in case of miscomparison, and for branch ratios greater than 1,
1406 it stores an accumulator and the current and maximum counts before
1407 emitting another branch. */
1409 class compare_by_pieces_d : public op_by_pieces_d
1411 rtx_code_label *m_fail_label;
1412 rtx m_accumulator;
1413 int m_count, m_batch;
1415 void generate (rtx, rtx, machine_mode);
1416 bool prepare_mode (machine_mode, unsigned int);
1417 void finish_mode (machine_mode);
1418 public:
1419 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1420 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1421 rtx_code_label *fail_label)
1422 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1424 m_fail_label = fail_label;
1428 /* A callback used when iterating for a compare_by_pieces_operation.
1429 OP0 and OP1 are the values that have been loaded and should be
1430 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1431 context structure. */
1433 void
1434 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1436 if (m_batch > 1)
1438 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1439 true, OPTAB_LIB_WIDEN);
1440 if (m_count != 0)
1441 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1442 true, OPTAB_LIB_WIDEN);
1443 m_accumulator = temp;
1445 if (++m_count < m_batch)
1446 return;
1448 m_count = 0;
1449 op0 = m_accumulator;
1450 op1 = const0_rtx;
1451 m_accumulator = NULL_RTX;
1453 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1454 m_fail_label, profile_probability::uninitialized ());
1457 /* Return true if MODE can be used for a set of moves and comparisons,
1458 given an alignment ALIGN. Prepare whatever data is necessary for
1459 later calls to generate. */
1461 bool
1462 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1464 insn_code icode = optab_handler (mov_optab, mode);
1465 if (icode == CODE_FOR_nothing
1466 || align < GET_MODE_ALIGNMENT (mode)
1467 || !can_compare_p (EQ, mode, ccp_jump))
1468 return false;
1469 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1470 if (m_batch < 0)
1471 return false;
1472 m_accumulator = NULL_RTX;
1473 m_count = 0;
1474 return true;
1477 /* Called after expanding a series of comparisons in MODE. If we have
1478 accumulated results for which we haven't emitted a branch yet, do
1479 so now. */
1481 void
1482 compare_by_pieces_d::finish_mode (machine_mode mode)
1484 if (m_accumulator != NULL_RTX)
1485 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1486 NULL_RTX, NULL, m_fail_label,
1487 profile_probability::uninitialized ());
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;
1711 if (expected_align < align)
1712 expected_align = align;
1713 if (expected_size != -1)
1715 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1716 expected_size = probable_max_size;
1717 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1718 expected_size = min_size;
1721 /* Since this is a move insn, we don't care about volatility. */
1722 volatile_ok = 1;
1724 /* Try the most limited insn first, because there's no point
1725 including more than one in the machine description unless
1726 the more limited one has some advantage. */
1728 opt_scalar_int_mode mode_iter;
1729 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1731 scalar_int_mode mode = mode_iter.require ();
1732 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1734 if (code != CODE_FOR_nothing
1735 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1736 here because if SIZE is less than the mode mask, as it is
1737 returned by the macro, it will definitely be less than the
1738 actual mode mask. Since SIZE is within the Pmode address
1739 space, we limit MODE to Pmode. */
1740 && ((CONST_INT_P (size)
1741 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1742 <= (GET_MODE_MASK (mode) >> 1)))
1743 || max_size <= (GET_MODE_MASK (mode) >> 1)
1744 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1746 struct expand_operand ops[9];
1747 unsigned int nops;
1749 /* ??? When called via emit_block_move_for_call, it'd be
1750 nice if there were some way to inform the backend, so
1751 that it doesn't fail the expansion because it thinks
1752 emitting the libcall would be more efficient. */
1753 nops = insn_data[(int) code].n_generator_args;
1754 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1756 create_fixed_operand (&ops[0], x);
1757 create_fixed_operand (&ops[1], y);
1758 /* The check above guarantees that this size conversion is valid. */
1759 create_convert_operand_to (&ops[2], size, mode, true);
1760 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1761 if (nops >= 6)
1763 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1764 create_integer_operand (&ops[5], expected_size);
1766 if (nops >= 8)
1768 create_integer_operand (&ops[6], min_size);
1769 /* If we can not represent the maximal size,
1770 make parameter NULL. */
1771 if ((HOST_WIDE_INT) max_size != -1)
1772 create_integer_operand (&ops[7], max_size);
1773 else
1774 create_fixed_operand (&ops[7], NULL);
1776 if (nops == 9)
1778 /* If we can not represent the maximal size,
1779 make parameter NULL. */
1780 if ((HOST_WIDE_INT) probable_max_size != -1)
1781 create_integer_operand (&ops[8], probable_max_size);
1782 else
1783 create_fixed_operand (&ops[8], NULL);
1785 if (maybe_expand_insn (code, nops, ops))
1787 volatile_ok = save_volatile_ok;
1788 return true;
1793 volatile_ok = save_volatile_ok;
1794 return false;
1797 /* A subroutine of emit_block_move. Copy the data via an explicit
1798 loop. This is used only when libcalls are forbidden. */
1799 /* ??? It'd be nice to copy in hunks larger than QImode. */
1801 static void
1802 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1803 unsigned int align ATTRIBUTE_UNUSED)
1805 rtx_code_label *cmp_label, *top_label;
1806 rtx iter, x_addr, y_addr, tmp;
1807 machine_mode x_addr_mode = get_address_mode (x);
1808 machine_mode y_addr_mode = get_address_mode (y);
1809 machine_mode iter_mode;
1811 iter_mode = GET_MODE (size);
1812 if (iter_mode == VOIDmode)
1813 iter_mode = word_mode;
1815 top_label = gen_label_rtx ();
1816 cmp_label = gen_label_rtx ();
1817 iter = gen_reg_rtx (iter_mode);
1819 emit_move_insn (iter, const0_rtx);
1821 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1822 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1823 do_pending_stack_adjust ();
1825 emit_jump (cmp_label);
1826 emit_label (top_label);
1828 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1829 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1831 if (x_addr_mode != y_addr_mode)
1832 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1833 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1835 x = change_address (x, QImode, x_addr);
1836 y = change_address (y, QImode, y_addr);
1838 emit_move_insn (x, y);
1840 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1841 true, OPTAB_LIB_WIDEN);
1842 if (tmp != iter)
1843 emit_move_insn (iter, tmp);
1845 emit_label (cmp_label);
1847 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1848 true, top_label,
1849 profile_probability::guessed_always ()
1850 .apply_scale (9, 10));
1853 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1854 TAILCALL is true if this is a tail call. */
1857 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1858 rtx size, bool tailcall)
1860 rtx dst_addr, src_addr;
1861 tree call_expr, dst_tree, src_tree, size_tree;
1862 machine_mode size_mode;
1864 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1865 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1866 dst_tree = make_tree (ptr_type_node, dst_addr);
1868 src_addr = copy_addr_to_reg (XEXP (src, 0));
1869 src_addr = convert_memory_address (ptr_mode, src_addr);
1870 src_tree = make_tree (ptr_type_node, src_addr);
1872 size_mode = TYPE_MODE (sizetype);
1873 size = convert_to_mode (size_mode, size, 1);
1874 size = copy_to_mode_reg (size_mode, size);
1875 size_tree = make_tree (sizetype, size);
1877 /* It is incorrect to use the libcall calling conventions for calls to
1878 memcpy/memmove/memcmp because they can be provided by the user. */
1879 tree fn = builtin_decl_implicit (fncode);
1880 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1881 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1883 return expand_call (call_expr, NULL_RTX, false);
1886 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1887 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1888 otherwise return null. */
1891 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1892 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1893 HOST_WIDE_INT align)
1895 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1897 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1898 target = NULL_RTX;
1900 struct expand_operand ops[5];
1901 create_output_operand (&ops[0], target, insn_mode);
1902 create_fixed_operand (&ops[1], arg1_rtx);
1903 create_fixed_operand (&ops[2], arg2_rtx);
1904 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1905 TYPE_UNSIGNED (arg3_type));
1906 create_integer_operand (&ops[4], align);
1907 if (maybe_expand_insn (icode, 5, ops))
1908 return ops[0].value;
1909 return NULL_RTX;
1912 /* Expand a block compare between X and Y with length LEN using the
1913 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1914 of the expression that was used to calculate the length. ALIGN
1915 gives the known minimum common alignment. */
1917 static rtx
1918 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1919 unsigned align)
1921 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1922 implementing memcmp because it will stop if it encounters two
1923 zero bytes. */
1924 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1926 if (icode == CODE_FOR_nothing)
1927 return NULL_RTX;
1929 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1932 /* Emit code to compare a block Y to a block X. This may be done with
1933 string-compare instructions, with multiple scalar instructions,
1934 or with a library call.
1936 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1937 they are. LEN_TYPE is the type of the expression that was used to
1938 calculate it.
1940 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1941 value of a normal memcmp call, instead we can just compare for equality.
1942 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1943 returning NULL_RTX.
1945 Optionally, the caller can pass a constfn and associated data in Y_CFN
1946 and Y_CFN_DATA. describing that the second operand being compared is a
1947 known constant and how to obtain its data.
1948 Return the result of the comparison, or NULL_RTX if we failed to
1949 perform the operation. */
1952 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1953 bool equality_only, by_pieces_constfn y_cfn,
1954 void *y_cfndata)
1956 rtx result = 0;
1958 if (CONST_INT_P (len) && INTVAL (len) == 0)
1959 return const0_rtx;
1961 gcc_assert (MEM_P (x) && MEM_P (y));
1962 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1963 gcc_assert (align >= BITS_PER_UNIT);
1965 x = adjust_address (x, BLKmode, 0);
1966 y = adjust_address (y, BLKmode, 0);
1968 if (equality_only
1969 && CONST_INT_P (len)
1970 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1971 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1972 y_cfn, y_cfndata);
1973 else
1974 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1976 return result;
1979 /* Copy all or part of a value X into registers starting at REGNO.
1980 The number of registers to be filled is NREGS. */
1982 void
1983 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1985 if (nregs == 0)
1986 return;
1988 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1989 x = validize_mem (force_const_mem (mode, x));
1991 /* See if the machine can do this with a load multiple insn. */
1992 if (targetm.have_load_multiple ())
1994 rtx_insn *last = get_last_insn ();
1995 rtx first = gen_rtx_REG (word_mode, regno);
1996 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1997 GEN_INT (nregs)))
1999 emit_insn (pat);
2000 return;
2002 else
2003 delete_insns_since (last);
2006 for (int i = 0; i < nregs; i++)
2007 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2008 operand_subword_force (x, i, mode));
2011 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2012 The number of registers to be filled is NREGS. */
2014 void
2015 move_block_from_reg (int regno, rtx x, int nregs)
2017 if (nregs == 0)
2018 return;
2020 /* See if the machine can do this with a store multiple insn. */
2021 if (targetm.have_store_multiple ())
2023 rtx_insn *last = get_last_insn ();
2024 rtx first = gen_rtx_REG (word_mode, regno);
2025 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2026 GEN_INT (nregs)))
2028 emit_insn (pat);
2029 return;
2031 else
2032 delete_insns_since (last);
2035 for (int i = 0; i < nregs; i++)
2037 rtx tem = operand_subword (x, i, 1, BLKmode);
2039 gcc_assert (tem);
2041 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2045 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2046 ORIG, where ORIG is a non-consecutive group of registers represented by
2047 a PARALLEL. The clone is identical to the original except in that the
2048 original set of registers is replaced by a new set of pseudo registers.
2049 The new set has the same modes as the original set. */
2052 gen_group_rtx (rtx orig)
2054 int i, length;
2055 rtx *tmps;
2057 gcc_assert (GET_CODE (orig) == PARALLEL);
2059 length = XVECLEN (orig, 0);
2060 tmps = XALLOCAVEC (rtx, length);
2062 /* Skip a NULL entry in first slot. */
2063 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2065 if (i)
2066 tmps[0] = 0;
2068 for (; i < length; i++)
2070 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2071 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2073 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2076 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2079 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2080 except that values are placed in TMPS[i], and must later be moved
2081 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2083 static void
2084 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2086 rtx src;
2087 int start, i;
2088 machine_mode m = GET_MODE (orig_src);
2090 gcc_assert (GET_CODE (dst) == PARALLEL);
2092 if (m != VOIDmode
2093 && !SCALAR_INT_MODE_P (m)
2094 && !MEM_P (orig_src)
2095 && GET_CODE (orig_src) != CONCAT)
2097 scalar_int_mode imode;
2098 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2100 src = gen_reg_rtx (imode);
2101 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2103 else
2105 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2106 emit_move_insn (src, orig_src);
2108 emit_group_load_1 (tmps, dst, src, type, ssize);
2109 return;
2112 /* Check for a NULL entry, used to indicate that the parameter goes
2113 both on the stack and in registers. */
2114 if (XEXP (XVECEXP (dst, 0, 0), 0))
2115 start = 0;
2116 else
2117 start = 1;
2119 /* Process the pieces. */
2120 for (i = start; i < XVECLEN (dst, 0); i++)
2122 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2123 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2124 unsigned int bytelen = GET_MODE_SIZE (mode);
2125 int shift = 0;
2127 /* Handle trailing fragments that run over the size of the struct. */
2128 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2130 /* Arrange to shift the fragment to where it belongs.
2131 extract_bit_field loads to the lsb of the reg. */
2132 if (
2133 #ifdef BLOCK_REG_PADDING
2134 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2135 == (BYTES_BIG_ENDIAN ? upward : downward)
2136 #else
2137 BYTES_BIG_ENDIAN
2138 #endif
2140 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2141 bytelen = ssize - bytepos;
2142 gcc_assert (bytelen > 0);
2145 /* If we won't be loading directly from memory, protect the real source
2146 from strange tricks we might play; but make sure that the source can
2147 be loaded directly into the destination. */
2148 src = orig_src;
2149 if (!MEM_P (orig_src)
2150 && (!CONSTANT_P (orig_src)
2151 || (GET_MODE (orig_src) != mode
2152 && GET_MODE (orig_src) != VOIDmode)))
2154 if (GET_MODE (orig_src) == VOIDmode)
2155 src = gen_reg_rtx (mode);
2156 else
2157 src = gen_reg_rtx (GET_MODE (orig_src));
2159 emit_move_insn (src, orig_src);
2162 /* Optimize the access just a bit. */
2163 if (MEM_P (src)
2164 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2165 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2166 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2167 && bytelen == GET_MODE_SIZE (mode))
2169 tmps[i] = gen_reg_rtx (mode);
2170 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2172 else if (COMPLEX_MODE_P (mode)
2173 && GET_MODE (src) == mode
2174 && bytelen == GET_MODE_SIZE (mode))
2175 /* Let emit_move_complex do the bulk of the work. */
2176 tmps[i] = src;
2177 else if (GET_CODE (src) == CONCAT)
2179 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2180 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2181 unsigned int elt = bytepos / slen0;
2182 unsigned int subpos = bytepos % slen0;
2184 if (subpos + bytelen <= slen0)
2186 /* The following assumes that the concatenated objects all
2187 have the same size. In this case, a simple calculation
2188 can be used to determine the object and the bit field
2189 to be extracted. */
2190 tmps[i] = XEXP (src, elt);
2191 if (subpos != 0
2192 || subpos + bytelen != slen0
2193 || (!CONSTANT_P (tmps[i])
2194 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2195 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2196 subpos * BITS_PER_UNIT,
2197 1, NULL_RTX, mode, mode, false,
2198 NULL);
2200 else
2202 rtx mem;
2204 gcc_assert (!bytepos);
2205 mem = assign_stack_temp (GET_MODE (src), slen);
2206 emit_move_insn (mem, src);
2207 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2208 0, 1, NULL_RTX, mode, mode, false,
2209 NULL);
2212 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2213 SIMD register, which is currently broken. While we get GCC
2214 to emit proper RTL for these cases, let's dump to memory. */
2215 else if (VECTOR_MODE_P (GET_MODE (dst))
2216 && REG_P (src))
2218 int slen = GET_MODE_SIZE (GET_MODE (src));
2219 rtx mem;
2221 mem = assign_stack_temp (GET_MODE (src), slen);
2222 emit_move_insn (mem, src);
2223 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2225 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2226 && XVECLEN (dst, 0) > 1)
2227 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2228 else if (CONSTANT_P (src))
2230 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2232 if (len == ssize)
2233 tmps[i] = src;
2234 else
2236 rtx first, second;
2238 /* TODO: const_wide_int can have sizes other than this... */
2239 gcc_assert (2 * len == ssize);
2240 split_double (src, &first, &second);
2241 if (i)
2242 tmps[i] = second;
2243 else
2244 tmps[i] = first;
2247 else if (REG_P (src) && GET_MODE (src) == mode)
2248 tmps[i] = src;
2249 else
2250 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2251 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2252 mode, mode, false, NULL);
2254 if (shift)
2255 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2256 shift, tmps[i], 0);
2260 /* Emit code to move a block SRC of type TYPE to a block DST,
2261 where DST is non-consecutive registers represented by a PARALLEL.
2262 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2263 if not known. */
2265 void
2266 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2268 rtx *tmps;
2269 int i;
2271 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2272 emit_group_load_1 (tmps, dst, src, type, ssize);
2274 /* Copy the extracted pieces into the proper (probable) hard regs. */
2275 for (i = 0; i < XVECLEN (dst, 0); i++)
2277 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2278 if (d == NULL)
2279 continue;
2280 emit_move_insn (d, tmps[i]);
2284 /* Similar, but load SRC into new pseudos in a format that looks like
2285 PARALLEL. This can later be fed to emit_group_move to get things
2286 in the right place. */
2289 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2291 rtvec vec;
2292 int i;
2294 vec = rtvec_alloc (XVECLEN (parallel, 0));
2295 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2297 /* Convert the vector to look just like the original PARALLEL, except
2298 with the computed values. */
2299 for (i = 0; i < XVECLEN (parallel, 0); i++)
2301 rtx e = XVECEXP (parallel, 0, i);
2302 rtx d = XEXP (e, 0);
2304 if (d)
2306 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2307 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2309 RTVEC_ELT (vec, i) = e;
2312 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2315 /* Emit code to move a block SRC to block DST, where SRC and DST are
2316 non-consecutive groups of registers, each represented by a PARALLEL. */
2318 void
2319 emit_group_move (rtx dst, rtx src)
2321 int i;
2323 gcc_assert (GET_CODE (src) == PARALLEL
2324 && GET_CODE (dst) == PARALLEL
2325 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2327 /* Skip first entry if NULL. */
2328 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2329 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2330 XEXP (XVECEXP (src, 0, i), 0));
2333 /* Move a group of registers represented by a PARALLEL into pseudos. */
2336 emit_group_move_into_temps (rtx src)
2338 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2339 int i;
2341 for (i = 0; i < XVECLEN (src, 0); i++)
2343 rtx e = XVECEXP (src, 0, i);
2344 rtx d = XEXP (e, 0);
2346 if (d)
2347 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2348 RTVEC_ELT (vec, i) = e;
2351 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2354 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2355 where SRC is non-consecutive registers represented by a PARALLEL.
2356 SSIZE represents the total size of block ORIG_DST, or -1 if not
2357 known. */
2359 void
2360 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2362 rtx *tmps, dst;
2363 int start, finish, i;
2364 machine_mode m = GET_MODE (orig_dst);
2366 gcc_assert (GET_CODE (src) == PARALLEL);
2368 if (!SCALAR_INT_MODE_P (m)
2369 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2371 scalar_int_mode imode;
2372 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2374 dst = gen_reg_rtx (imode);
2375 emit_group_store (dst, src, type, ssize);
2376 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2378 else
2380 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2381 emit_group_store (dst, src, type, ssize);
2383 emit_move_insn (orig_dst, dst);
2384 return;
2387 /* Check for a NULL entry, used to indicate that the parameter goes
2388 both on the stack and in registers. */
2389 if (XEXP (XVECEXP (src, 0, 0), 0))
2390 start = 0;
2391 else
2392 start = 1;
2393 finish = XVECLEN (src, 0);
2395 tmps = XALLOCAVEC (rtx, finish);
2397 /* Copy the (probable) hard regs into pseudos. */
2398 for (i = start; i < finish; i++)
2400 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2401 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2403 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2404 emit_move_insn (tmps[i], reg);
2406 else
2407 tmps[i] = reg;
2410 /* If we won't be storing directly into memory, protect the real destination
2411 from strange tricks we might play. */
2412 dst = orig_dst;
2413 if (GET_CODE (dst) == PARALLEL)
2415 rtx temp;
2417 /* We can get a PARALLEL dst if there is a conditional expression in
2418 a return statement. In that case, the dst and src are the same,
2419 so no action is necessary. */
2420 if (rtx_equal_p (dst, src))
2421 return;
2423 /* It is unclear if we can ever reach here, but we may as well handle
2424 it. Allocate a temporary, and split this into a store/load to/from
2425 the temporary. */
2426 temp = assign_stack_temp (GET_MODE (dst), ssize);
2427 emit_group_store (temp, src, type, ssize);
2428 emit_group_load (dst, temp, type, ssize);
2429 return;
2431 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2433 machine_mode outer = GET_MODE (dst);
2434 machine_mode inner;
2435 HOST_WIDE_INT bytepos;
2436 bool done = false;
2437 rtx temp;
2439 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2440 dst = gen_reg_rtx (outer);
2442 /* Make life a bit easier for combine. */
2443 /* If the first element of the vector is the low part
2444 of the destination mode, use a paradoxical subreg to
2445 initialize the destination. */
2446 if (start < finish)
2448 inner = GET_MODE (tmps[start]);
2449 bytepos = subreg_lowpart_offset (inner, outer);
2450 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2452 temp = simplify_gen_subreg (outer, tmps[start],
2453 inner, 0);
2454 if (temp)
2456 emit_move_insn (dst, temp);
2457 done = true;
2458 start++;
2463 /* If the first element wasn't the low part, try the last. */
2464 if (!done
2465 && start < finish - 1)
2467 inner = GET_MODE (tmps[finish - 1]);
2468 bytepos = subreg_lowpart_offset (inner, outer);
2469 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2471 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2472 inner, 0);
2473 if (temp)
2475 emit_move_insn (dst, temp);
2476 done = true;
2477 finish--;
2482 /* Otherwise, simply initialize the result to zero. */
2483 if (!done)
2484 emit_move_insn (dst, CONST0_RTX (outer));
2487 /* Process the pieces. */
2488 for (i = start; i < finish; i++)
2490 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2491 machine_mode mode = GET_MODE (tmps[i]);
2492 unsigned int bytelen = GET_MODE_SIZE (mode);
2493 unsigned int adj_bytelen;
2494 rtx dest = dst;
2496 /* Handle trailing fragments that run over the size of the struct. */
2497 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2498 adj_bytelen = ssize - bytepos;
2499 else
2500 adj_bytelen = bytelen;
2502 if (GET_CODE (dst) == CONCAT)
2504 if (bytepos + adj_bytelen
2505 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2506 dest = XEXP (dst, 0);
2507 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2509 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2510 dest = XEXP (dst, 1);
2512 else
2514 machine_mode dest_mode = GET_MODE (dest);
2515 machine_mode tmp_mode = GET_MODE (tmps[i]);
2517 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2519 if (GET_MODE_ALIGNMENT (dest_mode)
2520 >= GET_MODE_ALIGNMENT (tmp_mode))
2522 dest = assign_stack_temp (dest_mode,
2523 GET_MODE_SIZE (dest_mode));
2524 emit_move_insn (adjust_address (dest,
2525 tmp_mode,
2526 bytepos),
2527 tmps[i]);
2528 dst = dest;
2530 else
2532 dest = assign_stack_temp (tmp_mode,
2533 GET_MODE_SIZE (tmp_mode));
2534 emit_move_insn (dest, tmps[i]);
2535 dst = adjust_address (dest, dest_mode, bytepos);
2537 break;
2541 /* Handle trailing fragments that run over the size of the struct. */
2542 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2544 /* store_bit_field always takes its value from the lsb.
2545 Move the fragment to the lsb if it's not already there. */
2546 if (
2547 #ifdef BLOCK_REG_PADDING
2548 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2549 == (BYTES_BIG_ENDIAN ? upward : downward)
2550 #else
2551 BYTES_BIG_ENDIAN
2552 #endif
2555 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2556 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2557 shift, tmps[i], 0);
2560 /* Make sure not to write past the end of the struct. */
2561 store_bit_field (dest,
2562 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2563 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2564 VOIDmode, tmps[i], false);
2567 /* Optimize the access just a bit. */
2568 else if (MEM_P (dest)
2569 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2570 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2571 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2572 && bytelen == GET_MODE_SIZE (mode))
2573 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2575 else
2576 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2577 0, 0, mode, tmps[i], false);
2580 /* Copy from the pseudo into the (probable) hard reg. */
2581 if (orig_dst != dst)
2582 emit_move_insn (orig_dst, dst);
2585 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2586 of the value stored in X. */
2589 maybe_emit_group_store (rtx x, tree type)
2591 machine_mode mode = TYPE_MODE (type);
2592 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2593 if (GET_CODE (x) == PARALLEL)
2595 rtx result = gen_reg_rtx (mode);
2596 emit_group_store (result, x, type, int_size_in_bytes (type));
2597 return result;
2599 return x;
2602 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2604 This is used on targets that return BLKmode values in registers. */
2606 static void
2607 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2609 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2610 rtx src = NULL, dst = NULL;
2611 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2612 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2613 machine_mode mode = GET_MODE (srcreg);
2614 machine_mode tmode = GET_MODE (target);
2615 machine_mode copy_mode;
2617 /* BLKmode registers created in the back-end shouldn't have survived. */
2618 gcc_assert (mode != BLKmode);
2620 /* If the structure doesn't take up a whole number of words, see whether
2621 SRCREG is padded on the left or on the right. If it's on the left,
2622 set PADDING_CORRECTION to the number of bits to skip.
2624 In most ABIs, the structure will be returned at the least end of
2625 the register, which translates to right padding on little-endian
2626 targets and left padding on big-endian targets. The opposite
2627 holds if the structure is returned at the most significant
2628 end of the register. */
2629 if (bytes % UNITS_PER_WORD != 0
2630 && (targetm.calls.return_in_msb (type)
2631 ? !BYTES_BIG_ENDIAN
2632 : BYTES_BIG_ENDIAN))
2633 padding_correction
2634 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2636 /* We can use a single move if we have an exact mode for the size. */
2637 else if (MEM_P (target)
2638 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2639 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2640 && bytes == GET_MODE_SIZE (mode))
2642 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2643 return;
2646 /* And if we additionally have the same mode for a register. */
2647 else if (REG_P (target)
2648 && GET_MODE (target) == mode
2649 && bytes == GET_MODE_SIZE (mode))
2651 emit_move_insn (target, srcreg);
2652 return;
2655 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2656 into a new pseudo which is a full word. */
2657 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2659 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2660 mode = word_mode;
2663 /* Copy the structure BITSIZE bits at a time. If the target lives in
2664 memory, take care of not reading/writing past its end by selecting
2665 a copy mode suited to BITSIZE. This should always be possible given
2666 how it is computed.
2668 If the target lives in register, make sure not to select a copy mode
2669 larger than the mode of the register.
2671 We could probably emit more efficient code for machines which do not use
2672 strict alignment, but it doesn't seem worth the effort at the current
2673 time. */
2675 copy_mode = word_mode;
2676 if (MEM_P (target))
2678 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2679 if (mem_mode.exists ())
2680 copy_mode = mem_mode.require ();
2682 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2683 copy_mode = tmode;
2685 for (bitpos = 0, xbitpos = padding_correction;
2686 bitpos < bytes * BITS_PER_UNIT;
2687 bitpos += bitsize, xbitpos += bitsize)
2689 /* We need a new source operand each time xbitpos is on a
2690 word boundary and when xbitpos == padding_correction
2691 (the first time through). */
2692 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2693 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2695 /* We need a new destination operand each time bitpos is on
2696 a word boundary. */
2697 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2698 dst = target;
2699 else if (bitpos % BITS_PER_WORD == 0)
2700 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2702 /* Use xbitpos for the source extraction (right justified) and
2703 bitpos for the destination store (left justified). */
2704 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2705 extract_bit_field (src, bitsize,
2706 xbitpos % BITS_PER_WORD, 1,
2707 NULL_RTX, copy_mode, copy_mode,
2708 false, NULL),
2709 false);
2713 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2714 register if it contains any data, otherwise return null.
2716 This is used on targets that return BLKmode values in registers. */
2719 copy_blkmode_to_reg (machine_mode mode, tree src)
2721 int i, n_regs;
2722 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2723 unsigned int bitsize;
2724 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2725 machine_mode dst_mode;
2727 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2729 x = expand_normal (src);
2731 bytes = int_size_in_bytes (TREE_TYPE (src));
2732 if (bytes == 0)
2733 return NULL_RTX;
2735 /* If the structure doesn't take up a whole number of words, see
2736 whether the register value should be padded on the left or on
2737 the right. Set PADDING_CORRECTION to the number of padding
2738 bits needed on the left side.
2740 In most ABIs, the structure will be returned at the least end of
2741 the register, which translates to right padding on little-endian
2742 targets and left padding on big-endian targets. The opposite
2743 holds if the structure is returned at the most significant
2744 end of the register. */
2745 if (bytes % UNITS_PER_WORD != 0
2746 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2747 ? !BYTES_BIG_ENDIAN
2748 : BYTES_BIG_ENDIAN))
2749 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2750 * BITS_PER_UNIT));
2752 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2753 dst_words = XALLOCAVEC (rtx, n_regs);
2754 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2756 /* Copy the structure BITSIZE bits at a time. */
2757 for (bitpos = 0, xbitpos = padding_correction;
2758 bitpos < bytes * BITS_PER_UNIT;
2759 bitpos += bitsize, xbitpos += bitsize)
2761 /* We need a new destination pseudo each time xbitpos is
2762 on a word boundary and when xbitpos == padding_correction
2763 (the first time through). */
2764 if (xbitpos % BITS_PER_WORD == 0
2765 || xbitpos == padding_correction)
2767 /* Generate an appropriate register. */
2768 dst_word = gen_reg_rtx (word_mode);
2769 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2771 /* Clear the destination before we move anything into it. */
2772 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2775 /* We need a new source operand each time bitpos is on a word
2776 boundary. */
2777 if (bitpos % BITS_PER_WORD == 0)
2778 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2780 /* Use bitpos for the source extraction (left justified) and
2781 xbitpos for the destination store (right justified). */
2782 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2783 0, 0, word_mode,
2784 extract_bit_field (src_word, bitsize,
2785 bitpos % BITS_PER_WORD, 1,
2786 NULL_RTX, word_mode, word_mode,
2787 false, NULL),
2788 false);
2791 if (mode == BLKmode)
2793 /* Find the smallest integer mode large enough to hold the
2794 entire structure. */
2795 opt_scalar_int_mode mode_iter;
2796 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2797 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2798 break;
2800 /* A suitable mode should have been found. */
2801 mode = mode_iter.require ();
2804 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2805 dst_mode = word_mode;
2806 else
2807 dst_mode = mode;
2808 dst = gen_reg_rtx (dst_mode);
2810 for (i = 0; i < n_regs; i++)
2811 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2813 if (mode != dst_mode)
2814 dst = gen_lowpart (mode, dst);
2816 return dst;
2819 /* Add a USE expression for REG to the (possibly empty) list pointed
2820 to by CALL_FUSAGE. REG must denote a hard register. */
2822 void
2823 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2825 gcc_assert (REG_P (reg));
2827 if (!HARD_REGISTER_P (reg))
2828 return;
2830 *call_fusage
2831 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2834 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2835 to by CALL_FUSAGE. REG must denote a hard register. */
2837 void
2838 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2840 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2842 *call_fusage
2843 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2846 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2847 starting at REGNO. All of these registers must be hard registers. */
2849 void
2850 use_regs (rtx *call_fusage, int regno, int nregs)
2852 int i;
2854 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2856 for (i = 0; i < nregs; i++)
2857 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2860 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2861 PARALLEL REGS. This is for calls that pass values in multiple
2862 non-contiguous locations. The Irix 6 ABI has examples of this. */
2864 void
2865 use_group_regs (rtx *call_fusage, rtx regs)
2867 int i;
2869 for (i = 0; i < XVECLEN (regs, 0); i++)
2871 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2873 /* A NULL entry means the parameter goes both on the stack and in
2874 registers. This can also be a MEM for targets that pass values
2875 partially on the stack and partially in registers. */
2876 if (reg != 0 && REG_P (reg))
2877 use_reg (call_fusage, reg);
2881 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2882 assigment and the code of the expresion on the RHS is CODE. Return
2883 NULL otherwise. */
2885 static gimple *
2886 get_def_for_expr (tree name, enum tree_code code)
2888 gimple *def_stmt;
2890 if (TREE_CODE (name) != SSA_NAME)
2891 return NULL;
2893 def_stmt = get_gimple_for_ssa_name (name);
2894 if (!def_stmt
2895 || gimple_assign_rhs_code (def_stmt) != code)
2896 return NULL;
2898 return def_stmt;
2901 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2902 assigment and the class of the expresion on the RHS is CLASS. Return
2903 NULL otherwise. */
2905 static gimple *
2906 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2908 gimple *def_stmt;
2910 if (TREE_CODE (name) != SSA_NAME)
2911 return NULL;
2913 def_stmt = get_gimple_for_ssa_name (name);
2914 if (!def_stmt
2915 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2916 return NULL;
2918 return def_stmt;
2921 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2922 its length in bytes. */
2925 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2926 unsigned int expected_align, HOST_WIDE_INT expected_size,
2927 unsigned HOST_WIDE_INT min_size,
2928 unsigned HOST_WIDE_INT max_size,
2929 unsigned HOST_WIDE_INT probable_max_size)
2931 machine_mode mode = GET_MODE (object);
2932 unsigned int align;
2934 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2936 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2937 just move a zero. Otherwise, do this a piece at a time. */
2938 if (mode != BLKmode
2939 && CONST_INT_P (size)
2940 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2942 rtx zero = CONST0_RTX (mode);
2943 if (zero != NULL)
2945 emit_move_insn (object, zero);
2946 return NULL;
2949 if (COMPLEX_MODE_P (mode))
2951 zero = CONST0_RTX (GET_MODE_INNER (mode));
2952 if (zero != NULL)
2954 write_complex_part (object, zero, 0);
2955 write_complex_part (object, zero, 1);
2956 return NULL;
2961 if (size == const0_rtx)
2962 return NULL;
2964 align = MEM_ALIGN (object);
2966 if (CONST_INT_P (size)
2967 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2968 CLEAR_BY_PIECES,
2969 optimize_insn_for_speed_p ()))
2970 clear_by_pieces (object, INTVAL (size), align);
2971 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2972 expected_align, expected_size,
2973 min_size, max_size, probable_max_size))
2975 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2976 return set_storage_via_libcall (object, size, const0_rtx,
2977 method == BLOCK_OP_TAILCALL);
2978 else
2979 gcc_unreachable ();
2981 return NULL;
2985 clear_storage (rtx object, rtx size, enum block_op_methods method)
2987 unsigned HOST_WIDE_INT max, min = 0;
2988 if (GET_CODE (size) == CONST_INT)
2989 min = max = UINTVAL (size);
2990 else
2991 max = GET_MODE_MASK (GET_MODE (size));
2992 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2996 /* A subroutine of clear_storage. Expand a call to memset.
2997 Return the return value of memset, 0 otherwise. */
3000 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3002 tree call_expr, fn, object_tree, size_tree, val_tree;
3003 machine_mode size_mode;
3005 object = copy_addr_to_reg (XEXP (object, 0));
3006 object_tree = make_tree (ptr_type_node, object);
3008 if (!CONST_INT_P (val))
3009 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3010 val_tree = make_tree (integer_type_node, val);
3012 size_mode = TYPE_MODE (sizetype);
3013 size = convert_to_mode (size_mode, size, 1);
3014 size = copy_to_mode_reg (size_mode, size);
3015 size_tree = make_tree (sizetype, size);
3017 /* It is incorrect to use the libcall calling conventions for calls to
3018 memset because it can be provided by the user. */
3019 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3020 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3021 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3023 return expand_call (call_expr, NULL_RTX, false);
3026 /* Expand a setmem pattern; return true if successful. */
3028 bool
3029 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3030 unsigned int expected_align, HOST_WIDE_INT expected_size,
3031 unsigned HOST_WIDE_INT min_size,
3032 unsigned HOST_WIDE_INT max_size,
3033 unsigned HOST_WIDE_INT probable_max_size)
3035 /* Try the most limited insn first, because there's no point
3036 including more than one in the machine description unless
3037 the more limited one has some advantage. */
3039 if (expected_align < align)
3040 expected_align = align;
3041 if (expected_size != -1)
3043 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3044 expected_size = max_size;
3045 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3046 expected_size = min_size;
3049 opt_scalar_int_mode mode_iter;
3050 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3052 scalar_int_mode mode = mode_iter.require ();
3053 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3055 if (code != CODE_FOR_nothing
3056 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3057 here because if SIZE is less than the mode mask, as it is
3058 returned by the macro, it will definitely be less than the
3059 actual mode mask. Since SIZE is within the Pmode address
3060 space, we limit MODE to Pmode. */
3061 && ((CONST_INT_P (size)
3062 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3063 <= (GET_MODE_MASK (mode) >> 1)))
3064 || max_size <= (GET_MODE_MASK (mode) >> 1)
3065 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3067 struct expand_operand ops[9];
3068 unsigned int nops;
3070 nops = insn_data[(int) code].n_generator_args;
3071 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3073 create_fixed_operand (&ops[0], object);
3074 /* The check above guarantees that this size conversion is valid. */
3075 create_convert_operand_to (&ops[1], size, mode, true);
3076 create_convert_operand_from (&ops[2], val, byte_mode, true);
3077 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3078 if (nops >= 6)
3080 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3081 create_integer_operand (&ops[5], expected_size);
3083 if (nops >= 8)
3085 create_integer_operand (&ops[6], min_size);
3086 /* If we can not represent the maximal size,
3087 make parameter NULL. */
3088 if ((HOST_WIDE_INT) max_size != -1)
3089 create_integer_operand (&ops[7], max_size);
3090 else
3091 create_fixed_operand (&ops[7], NULL);
3093 if (nops == 9)
3095 /* If we can not represent the maximal size,
3096 make parameter NULL. */
3097 if ((HOST_WIDE_INT) probable_max_size != -1)
3098 create_integer_operand (&ops[8], probable_max_size);
3099 else
3100 create_fixed_operand (&ops[8], NULL);
3102 if (maybe_expand_insn (code, nops, ops))
3103 return true;
3107 return false;
3111 /* Write to one of the components of the complex value CPLX. Write VAL to
3112 the real part if IMAG_P is false, and the imaginary part if its true. */
3114 void
3115 write_complex_part (rtx cplx, rtx val, bool imag_p)
3117 machine_mode cmode;
3118 scalar_mode imode;
3119 unsigned ibitsize;
3121 if (GET_CODE (cplx) == CONCAT)
3123 emit_move_insn (XEXP (cplx, imag_p), val);
3124 return;
3127 cmode = GET_MODE (cplx);
3128 imode = GET_MODE_INNER (cmode);
3129 ibitsize = GET_MODE_BITSIZE (imode);
3131 /* For MEMs simplify_gen_subreg may generate an invalid new address
3132 because, e.g., the original address is considered mode-dependent
3133 by the target, which restricts simplify_subreg from invoking
3134 adjust_address_nv. Instead of preparing fallback support for an
3135 invalid address, we call adjust_address_nv directly. */
3136 if (MEM_P (cplx))
3138 emit_move_insn (adjust_address_nv (cplx, imode,
3139 imag_p ? GET_MODE_SIZE (imode) : 0),
3140 val);
3141 return;
3144 /* If the sub-object is at least word sized, then we know that subregging
3145 will work. This special case is important, since store_bit_field
3146 wants to operate on integer modes, and there's rarely an OImode to
3147 correspond to TCmode. */
3148 if (ibitsize >= BITS_PER_WORD
3149 /* For hard regs we have exact predicates. Assume we can split
3150 the original object if it spans an even number of hard regs.
3151 This special case is important for SCmode on 64-bit platforms
3152 where the natural size of floating-point regs is 32-bit. */
3153 || (REG_P (cplx)
3154 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3155 && REG_NREGS (cplx) % 2 == 0))
3157 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3158 imag_p ? GET_MODE_SIZE (imode) : 0);
3159 if (part)
3161 emit_move_insn (part, val);
3162 return;
3164 else
3165 /* simplify_gen_subreg may fail for sub-word MEMs. */
3166 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3169 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3170 false);
3173 /* Extract one of the components of the complex value CPLX. Extract the
3174 real part if IMAG_P is false, and the imaginary part if it's true. */
3177 read_complex_part (rtx cplx, bool imag_p)
3179 machine_mode cmode;
3180 scalar_mode imode;
3181 unsigned ibitsize;
3183 if (GET_CODE (cplx) == CONCAT)
3184 return XEXP (cplx, imag_p);
3186 cmode = GET_MODE (cplx);
3187 imode = GET_MODE_INNER (cmode);
3188 ibitsize = GET_MODE_BITSIZE (imode);
3190 /* Special case reads from complex constants that got spilled to memory. */
3191 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3193 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3194 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3196 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3197 if (CONSTANT_CLASS_P (part))
3198 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3202 /* For MEMs simplify_gen_subreg may generate an invalid new address
3203 because, e.g., the original address is considered mode-dependent
3204 by the target, which restricts simplify_subreg from invoking
3205 adjust_address_nv. Instead of preparing fallback support for an
3206 invalid address, we call adjust_address_nv directly. */
3207 if (MEM_P (cplx))
3208 return adjust_address_nv (cplx, imode,
3209 imag_p ? GET_MODE_SIZE (imode) : 0);
3211 /* If the sub-object is at least word sized, then we know that subregging
3212 will work. This special case is important, since extract_bit_field
3213 wants to operate on integer modes, and there's rarely an OImode to
3214 correspond to TCmode. */
3215 if (ibitsize >= BITS_PER_WORD
3216 /* For hard regs we have exact predicates. Assume we can split
3217 the original object if it spans an even number of hard regs.
3218 This special case is important for SCmode on 64-bit platforms
3219 where the natural size of floating-point regs is 32-bit. */
3220 || (REG_P (cplx)
3221 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3222 && REG_NREGS (cplx) % 2 == 0))
3224 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3225 imag_p ? GET_MODE_SIZE (imode) : 0);
3226 if (ret)
3227 return ret;
3228 else
3229 /* simplify_gen_subreg may fail for sub-word MEMs. */
3230 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3233 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3234 true, NULL_RTX, imode, imode, false, NULL);
3237 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3238 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3239 represented in NEW_MODE. If FORCE is true, this will never happen, as
3240 we'll force-create a SUBREG if needed. */
3242 static rtx
3243 emit_move_change_mode (machine_mode new_mode,
3244 machine_mode old_mode, rtx x, bool force)
3246 rtx ret;
3248 if (push_operand (x, GET_MODE (x)))
3250 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3251 MEM_COPY_ATTRIBUTES (ret, x);
3253 else if (MEM_P (x))
3255 /* We don't have to worry about changing the address since the
3256 size in bytes is supposed to be the same. */
3257 if (reload_in_progress)
3259 /* Copy the MEM to change the mode and move any
3260 substitutions from the old MEM to the new one. */
3261 ret = adjust_address_nv (x, new_mode, 0);
3262 copy_replacements (x, ret);
3264 else
3265 ret = adjust_address (x, new_mode, 0);
3267 else
3269 /* Note that we do want simplify_subreg's behavior of validating
3270 that the new mode is ok for a hard register. If we were to use
3271 simplify_gen_subreg, we would create the subreg, but would
3272 probably run into the target not being able to implement it. */
3273 /* Except, of course, when FORCE is true, when this is exactly what
3274 we want. Which is needed for CCmodes on some targets. */
3275 if (force)
3276 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3277 else
3278 ret = simplify_subreg (new_mode, x, old_mode, 0);
3281 return ret;
3284 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3285 an integer mode of the same size as MODE. Returns the instruction
3286 emitted, or NULL if such a move could not be generated. */
3288 static rtx_insn *
3289 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3291 scalar_int_mode imode;
3292 enum insn_code code;
3294 /* There must exist a mode of the exact size we require. */
3295 if (!int_mode_for_mode (mode).exists (&imode))
3296 return NULL;
3298 /* The target must support moves in this mode. */
3299 code = optab_handler (mov_optab, imode);
3300 if (code == CODE_FOR_nothing)
3301 return NULL;
3303 x = emit_move_change_mode (imode, mode, x, force);
3304 if (x == NULL_RTX)
3305 return NULL;
3306 y = emit_move_change_mode (imode, mode, y, force);
3307 if (y == NULL_RTX)
3308 return NULL;
3309 return emit_insn (GEN_FCN (code) (x, y));
3312 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3313 Return an equivalent MEM that does not use an auto-increment. */
3316 emit_move_resolve_push (machine_mode mode, rtx x)
3318 enum rtx_code code = GET_CODE (XEXP (x, 0));
3319 HOST_WIDE_INT adjust;
3320 rtx temp;
3322 adjust = GET_MODE_SIZE (mode);
3323 #ifdef PUSH_ROUNDING
3324 adjust = PUSH_ROUNDING (adjust);
3325 #endif
3326 if (code == PRE_DEC || code == POST_DEC)
3327 adjust = -adjust;
3328 else if (code == PRE_MODIFY || code == POST_MODIFY)
3330 rtx expr = XEXP (XEXP (x, 0), 1);
3331 HOST_WIDE_INT val;
3333 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3334 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3335 val = INTVAL (XEXP (expr, 1));
3336 if (GET_CODE (expr) == MINUS)
3337 val = -val;
3338 gcc_assert (adjust == val || adjust == -val);
3339 adjust = val;
3342 /* Do not use anti_adjust_stack, since we don't want to update
3343 stack_pointer_delta. */
3344 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3345 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3346 0, OPTAB_LIB_WIDEN);
3347 if (temp != stack_pointer_rtx)
3348 emit_move_insn (stack_pointer_rtx, temp);
3350 switch (code)
3352 case PRE_INC:
3353 case PRE_DEC:
3354 case PRE_MODIFY:
3355 temp = stack_pointer_rtx;
3356 break;
3357 case POST_INC:
3358 case POST_DEC:
3359 case POST_MODIFY:
3360 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3361 break;
3362 default:
3363 gcc_unreachable ();
3366 return replace_equiv_address (x, temp);
3369 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3370 X is known to satisfy push_operand, and MODE is known to be complex.
3371 Returns the last instruction emitted. */
3373 rtx_insn *
3374 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3376 scalar_mode submode = GET_MODE_INNER (mode);
3377 bool imag_first;
3379 #ifdef PUSH_ROUNDING
3380 unsigned int submodesize = GET_MODE_SIZE (submode);
3382 /* In case we output to the stack, but the size is smaller than the
3383 machine can push exactly, we need to use move instructions. */
3384 if (PUSH_ROUNDING (submodesize) != submodesize)
3386 x = emit_move_resolve_push (mode, x);
3387 return emit_move_insn (x, y);
3389 #endif
3391 /* Note that the real part always precedes the imag part in memory
3392 regardless of machine's endianness. */
3393 switch (GET_CODE (XEXP (x, 0)))
3395 case PRE_DEC:
3396 case POST_DEC:
3397 imag_first = true;
3398 break;
3399 case PRE_INC:
3400 case POST_INC:
3401 imag_first = false;
3402 break;
3403 default:
3404 gcc_unreachable ();
3407 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3408 read_complex_part (y, imag_first));
3409 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3410 read_complex_part (y, !imag_first));
3413 /* A subroutine of emit_move_complex. Perform the move from Y to X
3414 via two moves of the parts. Returns the last instruction emitted. */
3416 rtx_insn *
3417 emit_move_complex_parts (rtx x, rtx y)
3419 /* Show the output dies here. This is necessary for SUBREGs
3420 of pseudos since we cannot track their lifetimes correctly;
3421 hard regs shouldn't appear here except as return values. */
3422 if (!reload_completed && !reload_in_progress
3423 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3424 emit_clobber (x);
3426 write_complex_part (x, read_complex_part (y, false), false);
3427 write_complex_part (x, read_complex_part (y, true), true);
3429 return get_last_insn ();
3432 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3433 MODE is known to be complex. Returns the last instruction emitted. */
3435 static rtx_insn *
3436 emit_move_complex (machine_mode mode, rtx x, rtx y)
3438 bool try_int;
3440 /* Need to take special care for pushes, to maintain proper ordering
3441 of the data, and possibly extra padding. */
3442 if (push_operand (x, mode))
3443 return emit_move_complex_push (mode, x, y);
3445 /* See if we can coerce the target into moving both values at once, except
3446 for floating point where we favor moving as parts if this is easy. */
3447 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3448 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3449 && !(REG_P (x)
3450 && HARD_REGISTER_P (x)
3451 && REG_NREGS (x) == 1)
3452 && !(REG_P (y)
3453 && HARD_REGISTER_P (y)
3454 && REG_NREGS (y) == 1))
3455 try_int = false;
3456 /* Not possible if the values are inherently not adjacent. */
3457 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3458 try_int = false;
3459 /* Is possible if both are registers (or subregs of registers). */
3460 else if (register_operand (x, mode) && register_operand (y, mode))
3461 try_int = true;
3462 /* If one of the operands is a memory, and alignment constraints
3463 are friendly enough, we may be able to do combined memory operations.
3464 We do not attempt this if Y is a constant because that combination is
3465 usually better with the by-parts thing below. */
3466 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3467 && (!STRICT_ALIGNMENT
3468 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3469 try_int = true;
3470 else
3471 try_int = false;
3473 if (try_int)
3475 rtx_insn *ret;
3477 /* For memory to memory moves, optimal behavior can be had with the
3478 existing block move logic. */
3479 if (MEM_P (x) && MEM_P (y))
3481 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3482 BLOCK_OP_NO_LIBCALL);
3483 return get_last_insn ();
3486 ret = emit_move_via_integer (mode, x, y, true);
3487 if (ret)
3488 return ret;
3491 return emit_move_complex_parts (x, y);
3494 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3495 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3497 static rtx_insn *
3498 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3500 rtx_insn *ret;
3502 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3503 if (mode != CCmode)
3505 enum insn_code code = optab_handler (mov_optab, CCmode);
3506 if (code != CODE_FOR_nothing)
3508 x = emit_move_change_mode (CCmode, mode, x, true);
3509 y = emit_move_change_mode (CCmode, mode, y, true);
3510 return emit_insn (GEN_FCN (code) (x, y));
3514 /* Otherwise, find the MODE_INT mode of the same width. */
3515 ret = emit_move_via_integer (mode, x, y, false);
3516 gcc_assert (ret != NULL);
3517 return ret;
3520 /* Return true if word I of OP lies entirely in the
3521 undefined bits of a paradoxical subreg. */
3523 static bool
3524 undefined_operand_subword_p (const_rtx op, int i)
3526 machine_mode innermode, innermostmode;
3527 int offset;
3528 if (GET_CODE (op) != SUBREG)
3529 return false;
3530 innermode = GET_MODE (op);
3531 innermostmode = GET_MODE (SUBREG_REG (op));
3532 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3533 /* The SUBREG_BYTE represents offset, as if the value were stored in
3534 memory, except for a paradoxical subreg where we define
3535 SUBREG_BYTE to be 0; undo this exception as in
3536 simplify_subreg. */
3537 if (SUBREG_BYTE (op) == 0
3538 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3540 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3541 if (WORDS_BIG_ENDIAN)
3542 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3543 if (BYTES_BIG_ENDIAN)
3544 offset += difference % UNITS_PER_WORD;
3546 if (offset >= GET_MODE_SIZE (innermostmode)
3547 || offset <= -GET_MODE_SIZE (word_mode))
3548 return true;
3549 return false;
3552 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3553 MODE is any multi-word or full-word mode that lacks a move_insn
3554 pattern. Note that you will get better code if you define such
3555 patterns, even if they must turn into multiple assembler instructions. */
3557 static rtx_insn *
3558 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3560 rtx_insn *last_insn = 0;
3561 rtx_insn *seq;
3562 rtx inner;
3563 bool need_clobber;
3564 int i;
3566 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3568 /* If X is a push on the stack, do the push now and replace
3569 X with a reference to the stack pointer. */
3570 if (push_operand (x, mode))
3571 x = emit_move_resolve_push (mode, x);
3573 /* If we are in reload, see if either operand is a MEM whose address
3574 is scheduled for replacement. */
3575 if (reload_in_progress && MEM_P (x)
3576 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3577 x = replace_equiv_address_nv (x, inner);
3578 if (reload_in_progress && MEM_P (y)
3579 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3580 y = replace_equiv_address_nv (y, inner);
3582 start_sequence ();
3584 need_clobber = false;
3585 for (i = 0;
3586 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3587 i++)
3589 rtx xpart = operand_subword (x, i, 1, mode);
3590 rtx ypart;
3592 /* Do not generate code for a move if it would come entirely
3593 from the undefined bits of a paradoxical subreg. */
3594 if (undefined_operand_subword_p (y, i))
3595 continue;
3597 ypart = operand_subword (y, i, 1, mode);
3599 /* If we can't get a part of Y, put Y into memory if it is a
3600 constant. Otherwise, force it into a register. Then we must
3601 be able to get a part of Y. */
3602 if (ypart == 0 && CONSTANT_P (y))
3604 y = use_anchored_address (force_const_mem (mode, y));
3605 ypart = operand_subword (y, i, 1, mode);
3607 else if (ypart == 0)
3608 ypart = operand_subword_force (y, i, mode);
3610 gcc_assert (xpart && ypart);
3612 need_clobber |= (GET_CODE (xpart) == SUBREG);
3614 last_insn = emit_move_insn (xpart, ypart);
3617 seq = get_insns ();
3618 end_sequence ();
3620 /* Show the output dies here. This is necessary for SUBREGs
3621 of pseudos since we cannot track their lifetimes correctly;
3622 hard regs shouldn't appear here except as return values.
3623 We never want to emit such a clobber after reload. */
3624 if (x != y
3625 && ! (reload_in_progress || reload_completed)
3626 && need_clobber != 0)
3627 emit_clobber (x);
3629 emit_insn (seq);
3631 return last_insn;
3634 /* Low level part of emit_move_insn.
3635 Called just like emit_move_insn, but assumes X and Y
3636 are basically valid. */
3638 rtx_insn *
3639 emit_move_insn_1 (rtx x, rtx y)
3641 machine_mode mode = GET_MODE (x);
3642 enum insn_code code;
3644 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3646 code = optab_handler (mov_optab, mode);
3647 if (code != CODE_FOR_nothing)
3648 return emit_insn (GEN_FCN (code) (x, y));
3650 /* Expand complex moves by moving real part and imag part. */
3651 if (COMPLEX_MODE_P (mode))
3652 return emit_move_complex (mode, x, y);
3654 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3655 || ALL_FIXED_POINT_MODE_P (mode))
3657 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3659 /* If we can't find an integer mode, use multi words. */
3660 if (result)
3661 return result;
3662 else
3663 return emit_move_multi_word (mode, x, y);
3666 if (GET_MODE_CLASS (mode) == MODE_CC)
3667 return emit_move_ccmode (mode, x, y);
3669 /* Try using a move pattern for the corresponding integer mode. This is
3670 only safe when simplify_subreg can convert MODE constants into integer
3671 constants. At present, it can only do this reliably if the value
3672 fits within a HOST_WIDE_INT. */
3673 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3675 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3677 if (ret)
3679 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3680 return ret;
3684 return emit_move_multi_word (mode, x, y);
3687 /* Generate code to copy Y into X.
3688 Both Y and X must have the same mode, except that
3689 Y can be a constant with VOIDmode.
3690 This mode cannot be BLKmode; use emit_block_move for that.
3692 Return the last instruction emitted. */
3694 rtx_insn *
3695 emit_move_insn (rtx x, rtx y)
3697 machine_mode mode = GET_MODE (x);
3698 rtx y_cst = NULL_RTX;
3699 rtx_insn *last_insn;
3700 rtx set;
3702 gcc_assert (mode != BLKmode
3703 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3705 if (CONSTANT_P (y))
3707 if (optimize
3708 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3709 && (last_insn = compress_float_constant (x, y)))
3710 return last_insn;
3712 y_cst = y;
3714 if (!targetm.legitimate_constant_p (mode, y))
3716 y = force_const_mem (mode, y);
3718 /* If the target's cannot_force_const_mem prevented the spill,
3719 assume that the target's move expanders will also take care
3720 of the non-legitimate constant. */
3721 if (!y)
3722 y = y_cst;
3723 else
3724 y = use_anchored_address (y);
3728 /* If X or Y are memory references, verify that their addresses are valid
3729 for the machine. */
3730 if (MEM_P (x)
3731 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3732 MEM_ADDR_SPACE (x))
3733 && ! push_operand (x, GET_MODE (x))))
3734 x = validize_mem (x);
3736 if (MEM_P (y)
3737 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3738 MEM_ADDR_SPACE (y)))
3739 y = validize_mem (y);
3741 gcc_assert (mode != BLKmode);
3743 last_insn = emit_move_insn_1 (x, y);
3745 if (y_cst && REG_P (x)
3746 && (set = single_set (last_insn)) != NULL_RTX
3747 && SET_DEST (set) == x
3748 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3749 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3751 return last_insn;
3754 /* Generate the body of an instruction to copy Y into X.
3755 It may be a list of insns, if one insn isn't enough. */
3757 rtx_insn *
3758 gen_move_insn (rtx x, rtx y)
3760 rtx_insn *seq;
3762 start_sequence ();
3763 emit_move_insn_1 (x, y);
3764 seq = get_insns ();
3765 end_sequence ();
3766 return seq;
3769 /* If Y is representable exactly in a narrower mode, and the target can
3770 perform the extension directly from constant or memory, then emit the
3771 move as an extension. */
3773 static rtx_insn *
3774 compress_float_constant (rtx x, rtx y)
3776 machine_mode dstmode = GET_MODE (x);
3777 machine_mode orig_srcmode = GET_MODE (y);
3778 machine_mode srcmode;
3779 const REAL_VALUE_TYPE *r;
3780 int oldcost, newcost;
3781 bool speed = optimize_insn_for_speed_p ();
3783 r = CONST_DOUBLE_REAL_VALUE (y);
3785 if (targetm.legitimate_constant_p (dstmode, y))
3786 oldcost = set_src_cost (y, orig_srcmode, speed);
3787 else
3788 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3790 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3792 enum insn_code ic;
3793 rtx trunc_y;
3794 rtx_insn *last_insn;
3796 /* Skip if the target can't extend this way. */
3797 ic = can_extend_p (dstmode, srcmode, 0);
3798 if (ic == CODE_FOR_nothing)
3799 continue;
3801 /* Skip if the narrowed value isn't exact. */
3802 if (! exact_real_truncate (srcmode, r))
3803 continue;
3805 trunc_y = const_double_from_real_value (*r, srcmode);
3807 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3809 /* Skip if the target needs extra instructions to perform
3810 the extension. */
3811 if (!insn_operand_matches (ic, 1, trunc_y))
3812 continue;
3813 /* This is valid, but may not be cheaper than the original. */
3814 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3815 dstmode, speed);
3816 if (oldcost < newcost)
3817 continue;
3819 else if (float_extend_from_mem[dstmode][srcmode])
3821 trunc_y = force_const_mem (srcmode, trunc_y);
3822 /* This is valid, but may not be cheaper than the original. */
3823 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3824 dstmode, speed);
3825 if (oldcost < newcost)
3826 continue;
3827 trunc_y = validize_mem (trunc_y);
3829 else
3830 continue;
3832 /* For CSE's benefit, force the compressed constant pool entry
3833 into a new pseudo. This constant may be used in different modes,
3834 and if not, combine will put things back together for us. */
3835 trunc_y = force_reg (srcmode, trunc_y);
3837 /* If x is a hard register, perform the extension into a pseudo,
3838 so that e.g. stack realignment code is aware of it. */
3839 rtx target = x;
3840 if (REG_P (x) && HARD_REGISTER_P (x))
3841 target = gen_reg_rtx (dstmode);
3843 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3844 last_insn = get_last_insn ();
3846 if (REG_P (target))
3847 set_unique_reg_note (last_insn, REG_EQUAL, y);
3849 if (target != x)
3850 return emit_move_insn (x, target);
3851 return last_insn;
3854 return NULL;
3857 /* Pushing data onto the stack. */
3859 /* Push a block of length SIZE (perhaps variable)
3860 and return an rtx to address the beginning of the block.
3861 The value may be virtual_outgoing_args_rtx.
3863 EXTRA is the number of bytes of padding to push in addition to SIZE.
3864 BELOW nonzero means this padding comes at low addresses;
3865 otherwise, the padding comes at high addresses. */
3868 push_block (rtx size, int extra, int below)
3870 rtx temp;
3872 size = convert_modes (Pmode, ptr_mode, size, 1);
3873 if (CONSTANT_P (size))
3874 anti_adjust_stack (plus_constant (Pmode, size, extra));
3875 else if (REG_P (size) && extra == 0)
3876 anti_adjust_stack (size);
3877 else
3879 temp = copy_to_mode_reg (Pmode, size);
3880 if (extra != 0)
3881 temp = expand_binop (Pmode, add_optab, temp,
3882 gen_int_mode (extra, Pmode),
3883 temp, 0, OPTAB_LIB_WIDEN);
3884 anti_adjust_stack (temp);
3887 if (STACK_GROWS_DOWNWARD)
3889 temp = virtual_outgoing_args_rtx;
3890 if (extra != 0 && below)
3891 temp = plus_constant (Pmode, temp, extra);
3893 else
3895 if (CONST_INT_P (size))
3896 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3897 -INTVAL (size) - (below ? 0 : extra));
3898 else if (extra != 0 && !below)
3899 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3900 negate_rtx (Pmode, plus_constant (Pmode, size,
3901 extra)));
3902 else
3903 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3904 negate_rtx (Pmode, size));
3907 return memory_address (NARROWEST_INT_MODE, temp);
3910 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3912 static rtx
3913 mem_autoinc_base (rtx mem)
3915 if (MEM_P (mem))
3917 rtx addr = XEXP (mem, 0);
3918 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3919 return XEXP (addr, 0);
3921 return NULL;
3924 /* A utility routine used here, in reload, and in try_split. The insns
3925 after PREV up to and including LAST are known to adjust the stack,
3926 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3927 placing notes as appropriate. PREV may be NULL, indicating the
3928 entire insn sequence prior to LAST should be scanned.
3930 The set of allowed stack pointer modifications is small:
3931 (1) One or more auto-inc style memory references (aka pushes),
3932 (2) One or more addition/subtraction with the SP as destination,
3933 (3) A single move insn with the SP as destination,
3934 (4) A call_pop insn,
3935 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3937 Insns in the sequence that do not modify the SP are ignored,
3938 except for noreturn calls.
3940 The return value is the amount of adjustment that can be trivially
3941 verified, via immediate operand or auto-inc. If the adjustment
3942 cannot be trivially extracted, the return value is INT_MIN. */
3944 HOST_WIDE_INT
3945 find_args_size_adjust (rtx_insn *insn)
3947 rtx dest, set, pat;
3948 int i;
3950 pat = PATTERN (insn);
3951 set = NULL;
3953 /* Look for a call_pop pattern. */
3954 if (CALL_P (insn))
3956 /* We have to allow non-call_pop patterns for the case
3957 of emit_single_push_insn of a TLS address. */
3958 if (GET_CODE (pat) != PARALLEL)
3959 return 0;
3961 /* All call_pop have a stack pointer adjust in the parallel.
3962 The call itself is always first, and the stack adjust is
3963 usually last, so search from the end. */
3964 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3966 set = XVECEXP (pat, 0, i);
3967 if (GET_CODE (set) != SET)
3968 continue;
3969 dest = SET_DEST (set);
3970 if (dest == stack_pointer_rtx)
3971 break;
3973 /* We'd better have found the stack pointer adjust. */
3974 if (i == 0)
3975 return 0;
3976 /* Fall through to process the extracted SET and DEST
3977 as if it was a standalone insn. */
3979 else if (GET_CODE (pat) == SET)
3980 set = pat;
3981 else if ((set = single_set (insn)) != NULL)
3983 else if (GET_CODE (pat) == PARALLEL)
3985 /* ??? Some older ports use a parallel with a stack adjust
3986 and a store for a PUSH_ROUNDING pattern, rather than a
3987 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3988 /* ??? See h8300 and m68k, pushqi1. */
3989 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3991 set = XVECEXP (pat, 0, i);
3992 if (GET_CODE (set) != SET)
3993 continue;
3994 dest = SET_DEST (set);
3995 if (dest == stack_pointer_rtx)
3996 break;
3998 /* We do not expect an auto-inc of the sp in the parallel. */
3999 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4000 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4001 != stack_pointer_rtx);
4003 if (i < 0)
4004 return 0;
4006 else
4007 return 0;
4009 dest = SET_DEST (set);
4011 /* Look for direct modifications of the stack pointer. */
4012 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4014 /* Look for a trivial adjustment, otherwise assume nothing. */
4015 /* Note that the SPU restore_stack_block pattern refers to
4016 the stack pointer in V4SImode. Consider that non-trivial. */
4017 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4018 && GET_CODE (SET_SRC (set)) == PLUS
4019 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4020 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4021 return INTVAL (XEXP (SET_SRC (set), 1));
4022 /* ??? Reload can generate no-op moves, which will be cleaned
4023 up later. Recognize it and continue searching. */
4024 else if (rtx_equal_p (dest, SET_SRC (set)))
4025 return 0;
4026 else
4027 return HOST_WIDE_INT_MIN;
4029 else
4031 rtx mem, addr;
4033 /* Otherwise only think about autoinc patterns. */
4034 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4036 mem = dest;
4037 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4038 != stack_pointer_rtx);
4040 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4041 mem = SET_SRC (set);
4042 else
4043 return 0;
4045 addr = XEXP (mem, 0);
4046 switch (GET_CODE (addr))
4048 case PRE_INC:
4049 case POST_INC:
4050 return GET_MODE_SIZE (GET_MODE (mem));
4051 case PRE_DEC:
4052 case POST_DEC:
4053 return -GET_MODE_SIZE (GET_MODE (mem));
4054 case PRE_MODIFY:
4055 case POST_MODIFY:
4056 addr = XEXP (addr, 1);
4057 gcc_assert (GET_CODE (addr) == PLUS);
4058 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4059 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4060 return INTVAL (XEXP (addr, 1));
4061 default:
4062 gcc_unreachable ();
4068 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4070 int args_size = end_args_size;
4071 bool saw_unknown = false;
4072 rtx_insn *insn;
4074 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4076 HOST_WIDE_INT this_delta;
4078 if (!NONDEBUG_INSN_P (insn))
4079 continue;
4081 this_delta = find_args_size_adjust (insn);
4082 if (this_delta == 0)
4084 if (!CALL_P (insn)
4085 || ACCUMULATE_OUTGOING_ARGS
4086 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4087 continue;
4090 gcc_assert (!saw_unknown);
4091 if (this_delta == HOST_WIDE_INT_MIN)
4092 saw_unknown = true;
4094 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4095 if (STACK_GROWS_DOWNWARD)
4096 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4098 args_size -= this_delta;
4101 return saw_unknown ? INT_MIN : args_size;
4104 #ifdef PUSH_ROUNDING
4105 /* Emit single push insn. */
4107 static void
4108 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4110 rtx dest_addr;
4111 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4112 rtx dest;
4113 enum insn_code icode;
4115 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4116 /* If there is push pattern, use it. Otherwise try old way of throwing
4117 MEM representing push operation to move expander. */
4118 icode = optab_handler (push_optab, mode);
4119 if (icode != CODE_FOR_nothing)
4121 struct expand_operand ops[1];
4123 create_input_operand (&ops[0], x, mode);
4124 if (maybe_expand_insn (icode, 1, ops))
4125 return;
4127 if (GET_MODE_SIZE (mode) == rounded_size)
4128 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4129 /* If we are to pad downward, adjust the stack pointer first and
4130 then store X into the stack location using an offset. This is
4131 because emit_move_insn does not know how to pad; it does not have
4132 access to type. */
4133 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4135 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4136 HOST_WIDE_INT offset;
4138 emit_move_insn (stack_pointer_rtx,
4139 expand_binop (Pmode,
4140 STACK_GROWS_DOWNWARD ? sub_optab
4141 : add_optab,
4142 stack_pointer_rtx,
4143 gen_int_mode (rounded_size, Pmode),
4144 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4146 offset = (HOST_WIDE_INT) padding_size;
4147 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4148 /* We have already decremented the stack pointer, so get the
4149 previous value. */
4150 offset += (HOST_WIDE_INT) rounded_size;
4152 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4153 /* We have already incremented the stack pointer, so get the
4154 previous value. */
4155 offset -= (HOST_WIDE_INT) rounded_size;
4157 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4158 gen_int_mode (offset, Pmode));
4160 else
4162 if (STACK_GROWS_DOWNWARD)
4163 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4164 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4165 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4166 Pmode));
4167 else
4168 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4169 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4170 gen_int_mode (rounded_size, Pmode));
4172 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4175 dest = gen_rtx_MEM (mode, dest_addr);
4177 if (type != 0)
4179 set_mem_attributes (dest, type, 1);
4181 if (cfun->tail_call_marked)
4182 /* Function incoming arguments may overlap with sibling call
4183 outgoing arguments and we cannot allow reordering of reads
4184 from function arguments with stores to outgoing arguments
4185 of sibling calls. */
4186 set_mem_alias_set (dest, 0);
4188 emit_move_insn (dest, x);
4191 /* Emit and annotate a single push insn. */
4193 static void
4194 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4196 int delta, old_delta = stack_pointer_delta;
4197 rtx_insn *prev = get_last_insn ();
4198 rtx_insn *last;
4200 emit_single_push_insn_1 (mode, x, type);
4202 last = get_last_insn ();
4204 /* Notice the common case where we emitted exactly one insn. */
4205 if (PREV_INSN (last) == prev)
4207 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4208 return;
4211 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4212 gcc_assert (delta == INT_MIN || delta == old_delta);
4214 #endif
4216 /* If reading SIZE bytes from X will end up reading from
4217 Y return the number of bytes that overlap. Return -1
4218 if there is no overlap or -2 if we can't determine
4219 (for example when X and Y have different base registers). */
4221 static int
4222 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4224 rtx tmp = plus_constant (Pmode, x, size);
4225 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4227 if (!CONST_INT_P (sub))
4228 return -2;
4230 HOST_WIDE_INT val = INTVAL (sub);
4232 return IN_RANGE (val, 1, size) ? val : -1;
4235 /* Generate code to push X onto the stack, assuming it has mode MODE and
4236 type TYPE.
4237 MODE is redundant except when X is a CONST_INT (since they don't
4238 carry mode info).
4239 SIZE is an rtx for the size of data to be copied (in bytes),
4240 needed only if X is BLKmode.
4241 Return true if successful. May return false if asked to push a
4242 partial argument during a sibcall optimization (as specified by
4243 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4244 to not overlap.
4246 ALIGN (in bits) is maximum alignment we can assume.
4248 If PARTIAL and REG are both nonzero, then copy that many of the first
4249 bytes of X into registers starting with REG, and push the rest of X.
4250 The amount of space pushed is decreased by PARTIAL bytes.
4251 REG must be a hard register in this case.
4252 If REG is zero but PARTIAL is not, take any all others actions for an
4253 argument partially in registers, but do not actually load any
4254 registers.
4256 EXTRA is the amount in bytes of extra space to leave next to this arg.
4257 This is ignored if an argument block has already been allocated.
4259 On a machine that lacks real push insns, ARGS_ADDR is the address of
4260 the bottom of the argument block for this call. We use indexing off there
4261 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4262 argument block has not been preallocated.
4264 ARGS_SO_FAR is the size of args previously pushed for this call.
4266 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4267 for arguments passed in registers. If nonzero, it will be the number
4268 of bytes required. */
4270 bool
4271 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4272 unsigned int align, int partial, rtx reg, int extra,
4273 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4274 rtx alignment_pad, bool sibcall_p)
4276 rtx xinner;
4277 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4279 /* Decide where to pad the argument: `downward' for below,
4280 `upward' for above, or `none' for don't pad it.
4281 Default is below for small data on big-endian machines; else above. */
4282 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4284 /* Invert direction if stack is post-decrement.
4285 FIXME: why? */
4286 if (STACK_PUSH_CODE == POST_DEC)
4287 if (where_pad != none)
4288 where_pad = (where_pad == downward ? upward : downward);
4290 xinner = x;
4292 int nregs = partial / UNITS_PER_WORD;
4293 rtx *tmp_regs = NULL;
4294 int overlapping = 0;
4296 if (mode == BLKmode
4297 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4299 /* Copy a block into the stack, entirely or partially. */
4301 rtx temp;
4302 int used;
4303 int offset;
4304 int skip;
4306 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4307 used = partial - offset;
4309 if (mode != BLKmode)
4311 /* A value is to be stored in an insufficiently aligned
4312 stack slot; copy via a suitably aligned slot if
4313 necessary. */
4314 size = GEN_INT (GET_MODE_SIZE (mode));
4315 if (!MEM_P (xinner))
4317 temp = assign_temp (type, 1, 1);
4318 emit_move_insn (temp, xinner);
4319 xinner = temp;
4323 gcc_assert (size);
4325 /* USED is now the # of bytes we need not copy to the stack
4326 because registers will take care of them. */
4328 if (partial != 0)
4329 xinner = adjust_address (xinner, BLKmode, used);
4331 /* If the partial register-part of the arg counts in its stack size,
4332 skip the part of stack space corresponding to the registers.
4333 Otherwise, start copying to the beginning of the stack space,
4334 by setting SKIP to 0. */
4335 skip = (reg_parm_stack_space == 0) ? 0 : used;
4337 #ifdef PUSH_ROUNDING
4338 /* Do it with several push insns if that doesn't take lots of insns
4339 and if there is no difficulty with push insns that skip bytes
4340 on the stack for alignment purposes. */
4341 if (args_addr == 0
4342 && PUSH_ARGS
4343 && CONST_INT_P (size)
4344 && skip == 0
4345 && MEM_ALIGN (xinner) >= align
4346 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4347 /* Here we avoid the case of a structure whose weak alignment
4348 forces many pushes of a small amount of data,
4349 and such small pushes do rounding that causes trouble. */
4350 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4351 || align >= BIGGEST_ALIGNMENT
4352 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4353 == (align / BITS_PER_UNIT)))
4354 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4356 /* Push padding now if padding above and stack grows down,
4357 or if padding below and stack grows up.
4358 But if space already allocated, this has already been done. */
4359 if (extra && args_addr == 0
4360 && where_pad != none && where_pad != stack_direction)
4361 anti_adjust_stack (GEN_INT (extra));
4363 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4365 else
4366 #endif /* PUSH_ROUNDING */
4368 rtx target;
4370 /* Otherwise make space on the stack and copy the data
4371 to the address of that space. */
4373 /* Deduct words put into registers from the size we must copy. */
4374 if (partial != 0)
4376 if (CONST_INT_P (size))
4377 size = GEN_INT (INTVAL (size) - used);
4378 else
4379 size = expand_binop (GET_MODE (size), sub_optab, size,
4380 gen_int_mode (used, GET_MODE (size)),
4381 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4384 /* Get the address of the stack space.
4385 In this case, we do not deal with EXTRA separately.
4386 A single stack adjust will do. */
4387 if (! args_addr)
4389 temp = push_block (size, extra, where_pad == downward);
4390 extra = 0;
4392 else if (CONST_INT_P (args_so_far))
4393 temp = memory_address (BLKmode,
4394 plus_constant (Pmode, args_addr,
4395 skip + INTVAL (args_so_far)));
4396 else
4397 temp = memory_address (BLKmode,
4398 plus_constant (Pmode,
4399 gen_rtx_PLUS (Pmode,
4400 args_addr,
4401 args_so_far),
4402 skip));
4404 if (!ACCUMULATE_OUTGOING_ARGS)
4406 /* If the source is referenced relative to the stack pointer,
4407 copy it to another register to stabilize it. We do not need
4408 to do this if we know that we won't be changing sp. */
4410 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4411 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4412 temp = copy_to_reg (temp);
4415 target = gen_rtx_MEM (BLKmode, temp);
4417 /* We do *not* set_mem_attributes here, because incoming arguments
4418 may overlap with sibling call outgoing arguments and we cannot
4419 allow reordering of reads from function arguments with stores
4420 to outgoing arguments of sibling calls. We do, however, want
4421 to record the alignment of the stack slot. */
4422 /* ALIGN may well be better aligned than TYPE, e.g. due to
4423 PARM_BOUNDARY. Assume the caller isn't lying. */
4424 set_mem_align (target, align);
4426 /* If part should go in registers and pushing to that part would
4427 overwrite some of the values that need to go into regs, load the
4428 overlapping values into temporary pseudos to be moved into the hard
4429 regs at the end after the stack pushing has completed.
4430 We cannot load them directly into the hard regs here because
4431 they can be clobbered by the block move expansions.
4432 See PR 65358. */
4434 if (partial > 0 && reg != 0 && mode == BLKmode
4435 && GET_CODE (reg) != PARALLEL)
4437 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4438 if (overlapping > 0)
4440 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4441 overlapping /= UNITS_PER_WORD;
4443 tmp_regs = XALLOCAVEC (rtx, overlapping);
4445 for (int i = 0; i < overlapping; i++)
4446 tmp_regs[i] = gen_reg_rtx (word_mode);
4448 for (int i = 0; i < overlapping; i++)
4449 emit_move_insn (tmp_regs[i],
4450 operand_subword_force (target, i, mode));
4452 else if (overlapping == -1)
4453 overlapping = 0;
4454 /* Could not determine whether there is overlap.
4455 Fail the sibcall. */
4456 else
4458 overlapping = 0;
4459 if (sibcall_p)
4460 return false;
4463 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4466 else if (partial > 0)
4468 /* Scalar partly in registers. */
4470 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4471 int i;
4472 int not_stack;
4473 /* # bytes of start of argument
4474 that we must make space for but need not store. */
4475 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4476 int args_offset = INTVAL (args_so_far);
4477 int skip;
4479 /* Push padding now if padding above and stack grows down,
4480 or if padding below and stack grows up.
4481 But if space already allocated, this has already been done. */
4482 if (extra && args_addr == 0
4483 && where_pad != none && where_pad != stack_direction)
4484 anti_adjust_stack (GEN_INT (extra));
4486 /* If we make space by pushing it, we might as well push
4487 the real data. Otherwise, we can leave OFFSET nonzero
4488 and leave the space uninitialized. */
4489 if (args_addr == 0)
4490 offset = 0;
4492 /* Now NOT_STACK gets the number of words that we don't need to
4493 allocate on the stack. Convert OFFSET to words too. */
4494 not_stack = (partial - offset) / UNITS_PER_WORD;
4495 offset /= UNITS_PER_WORD;
4497 /* If the partial register-part of the arg counts in its stack size,
4498 skip the part of stack space corresponding to the registers.
4499 Otherwise, start copying to the beginning of the stack space,
4500 by setting SKIP to 0. */
4501 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4503 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4504 x = validize_mem (force_const_mem (mode, x));
4506 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4507 SUBREGs of such registers are not allowed. */
4508 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4509 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4510 x = copy_to_reg (x);
4512 /* Loop over all the words allocated on the stack for this arg. */
4513 /* We can do it by words, because any scalar bigger than a word
4514 has a size a multiple of a word. */
4515 for (i = size - 1; i >= not_stack; i--)
4516 if (i >= not_stack + offset)
4517 if (!emit_push_insn (operand_subword_force (x, i, mode),
4518 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4519 0, args_addr,
4520 GEN_INT (args_offset + ((i - not_stack + skip)
4521 * UNITS_PER_WORD)),
4522 reg_parm_stack_space, alignment_pad, sibcall_p))
4523 return false;
4525 else
4527 rtx addr;
4528 rtx dest;
4530 /* Push padding now if padding above and stack grows down,
4531 or if padding below and stack grows up.
4532 But if space already allocated, this has already been done. */
4533 if (extra && args_addr == 0
4534 && where_pad != none && where_pad != stack_direction)
4535 anti_adjust_stack (GEN_INT (extra));
4537 #ifdef PUSH_ROUNDING
4538 if (args_addr == 0 && PUSH_ARGS)
4539 emit_single_push_insn (mode, x, type);
4540 else
4541 #endif
4543 if (CONST_INT_P (args_so_far))
4544 addr
4545 = memory_address (mode,
4546 plus_constant (Pmode, args_addr,
4547 INTVAL (args_so_far)));
4548 else
4549 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4550 args_so_far));
4551 dest = gen_rtx_MEM (mode, addr);
4553 /* We do *not* set_mem_attributes here, because incoming arguments
4554 may overlap with sibling call outgoing arguments and we cannot
4555 allow reordering of reads from function arguments with stores
4556 to outgoing arguments of sibling calls. We do, however, want
4557 to record the alignment of the stack slot. */
4558 /* ALIGN may well be better aligned than TYPE, e.g. due to
4559 PARM_BOUNDARY. Assume the caller isn't lying. */
4560 set_mem_align (dest, align);
4562 emit_move_insn (dest, x);
4566 /* Move the partial arguments into the registers and any overlapping
4567 values that we moved into the pseudos in tmp_regs. */
4568 if (partial > 0 && reg != 0)
4570 /* Handle calls that pass values in multiple non-contiguous locations.
4571 The Irix 6 ABI has examples of this. */
4572 if (GET_CODE (reg) == PARALLEL)
4573 emit_group_load (reg, x, type, -1);
4574 else
4576 gcc_assert (partial % UNITS_PER_WORD == 0);
4577 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4579 for (int i = 0; i < overlapping; i++)
4580 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4581 + nregs - overlapping + i),
4582 tmp_regs[i]);
4587 if (extra && args_addr == 0 && where_pad == stack_direction)
4588 anti_adjust_stack (GEN_INT (extra));
4590 if (alignment_pad && args_addr == 0)
4591 anti_adjust_stack (alignment_pad);
4593 return true;
4596 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4597 operations. */
4599 static rtx
4600 get_subtarget (rtx x)
4602 return (optimize
4603 || x == 0
4604 /* Only registers can be subtargets. */
4605 || !REG_P (x)
4606 /* Don't use hard regs to avoid extending their life. */
4607 || REGNO (x) < FIRST_PSEUDO_REGISTER
4608 ? 0 : x);
4611 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4612 FIELD is a bitfield. Returns true if the optimization was successful,
4613 and there's nothing else to do. */
4615 static bool
4616 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4617 unsigned HOST_WIDE_INT bitpos,
4618 unsigned HOST_WIDE_INT bitregion_start,
4619 unsigned HOST_WIDE_INT bitregion_end,
4620 machine_mode mode1, rtx str_rtx,
4621 tree to, tree src, bool reverse)
4623 machine_mode str_mode = GET_MODE (str_rtx);
4624 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4625 tree op0, op1;
4626 rtx value, result;
4627 optab binop;
4628 gimple *srcstmt;
4629 enum tree_code code;
4631 if (mode1 != VOIDmode
4632 || bitsize >= BITS_PER_WORD
4633 || str_bitsize > BITS_PER_WORD
4634 || TREE_SIDE_EFFECTS (to)
4635 || TREE_THIS_VOLATILE (to))
4636 return false;
4638 STRIP_NOPS (src);
4639 if (TREE_CODE (src) != SSA_NAME)
4640 return false;
4641 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4642 return false;
4644 srcstmt = get_gimple_for_ssa_name (src);
4645 if (!srcstmt
4646 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4647 return false;
4649 code = gimple_assign_rhs_code (srcstmt);
4651 op0 = gimple_assign_rhs1 (srcstmt);
4653 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4654 to find its initialization. Hopefully the initialization will
4655 be from a bitfield load. */
4656 if (TREE_CODE (op0) == SSA_NAME)
4658 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4660 /* We want to eventually have OP0 be the same as TO, which
4661 should be a bitfield. */
4662 if (!op0stmt
4663 || !is_gimple_assign (op0stmt)
4664 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4665 return false;
4666 op0 = gimple_assign_rhs1 (op0stmt);
4669 op1 = gimple_assign_rhs2 (srcstmt);
4671 if (!operand_equal_p (to, op0, 0))
4672 return false;
4674 if (MEM_P (str_rtx))
4676 unsigned HOST_WIDE_INT offset1;
4678 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4679 str_bitsize = BITS_PER_WORD;
4681 scalar_int_mode best_mode;
4682 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4683 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4684 return false;
4685 str_mode = best_mode;
4686 str_bitsize = GET_MODE_BITSIZE (best_mode);
4688 offset1 = bitpos;
4689 bitpos %= str_bitsize;
4690 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4691 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4693 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4694 return false;
4695 else
4696 gcc_assert (!reverse);
4698 /* If the bit field covers the whole REG/MEM, store_field
4699 will likely generate better code. */
4700 if (bitsize >= str_bitsize)
4701 return false;
4703 /* We can't handle fields split across multiple entities. */
4704 if (bitpos + bitsize > str_bitsize)
4705 return false;
4707 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4708 bitpos = str_bitsize - bitpos - bitsize;
4710 switch (code)
4712 case PLUS_EXPR:
4713 case MINUS_EXPR:
4714 /* For now, just optimize the case of the topmost bitfield
4715 where we don't need to do any masking and also
4716 1 bit bitfields where xor can be used.
4717 We might win by one instruction for the other bitfields
4718 too if insv/extv instructions aren't used, so that
4719 can be added later. */
4720 if ((reverse || bitpos + bitsize != str_bitsize)
4721 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4722 break;
4724 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4725 value = convert_modes (str_mode,
4726 TYPE_MODE (TREE_TYPE (op1)), value,
4727 TYPE_UNSIGNED (TREE_TYPE (op1)));
4729 /* We may be accessing data outside the field, which means
4730 we can alias adjacent data. */
4731 if (MEM_P (str_rtx))
4733 str_rtx = shallow_copy_rtx (str_rtx);
4734 set_mem_alias_set (str_rtx, 0);
4735 set_mem_expr (str_rtx, 0);
4738 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4740 value = expand_and (str_mode, value, const1_rtx, NULL);
4741 binop = xor_optab;
4743 else
4744 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4746 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4747 if (reverse)
4748 value = flip_storage_order (str_mode, value);
4749 result = expand_binop (str_mode, binop, str_rtx,
4750 value, str_rtx, 1, OPTAB_WIDEN);
4751 if (result != str_rtx)
4752 emit_move_insn (str_rtx, result);
4753 return true;
4755 case BIT_IOR_EXPR:
4756 case BIT_XOR_EXPR:
4757 if (TREE_CODE (op1) != INTEGER_CST)
4758 break;
4759 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4760 value = convert_modes (str_mode,
4761 TYPE_MODE (TREE_TYPE (op1)), value,
4762 TYPE_UNSIGNED (TREE_TYPE (op1)));
4764 /* We may be accessing data outside the field, which means
4765 we can alias adjacent data. */
4766 if (MEM_P (str_rtx))
4768 str_rtx = shallow_copy_rtx (str_rtx);
4769 set_mem_alias_set (str_rtx, 0);
4770 set_mem_expr (str_rtx, 0);
4773 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4774 if (bitpos + bitsize != str_bitsize)
4776 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4777 str_mode);
4778 value = expand_and (str_mode, value, mask, NULL_RTX);
4780 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4781 if (reverse)
4782 value = flip_storage_order (str_mode, value);
4783 result = expand_binop (str_mode, binop, str_rtx,
4784 value, str_rtx, 1, OPTAB_WIDEN);
4785 if (result != str_rtx)
4786 emit_move_insn (str_rtx, result);
4787 return true;
4789 default:
4790 break;
4793 return false;
4796 /* In the C++ memory model, consecutive bit fields in a structure are
4797 considered one memory location.
4799 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4800 returns the bit range of consecutive bits in which this COMPONENT_REF
4801 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4802 and *OFFSET may be adjusted in the process.
4804 If the access does not need to be restricted, 0 is returned in both
4805 *BITSTART and *BITEND. */
4807 void
4808 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4809 unsigned HOST_WIDE_INT *bitend,
4810 tree exp,
4811 HOST_WIDE_INT *bitpos,
4812 tree *offset)
4814 HOST_WIDE_INT bitoffset;
4815 tree field, repr;
4817 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4819 field = TREE_OPERAND (exp, 1);
4820 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4821 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4822 need to limit the range we can access. */
4823 if (!repr)
4825 *bitstart = *bitend = 0;
4826 return;
4829 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4830 part of a larger bit field, then the representative does not serve any
4831 useful purpose. This can occur in Ada. */
4832 if (handled_component_p (TREE_OPERAND (exp, 0)))
4834 machine_mode rmode;
4835 HOST_WIDE_INT rbitsize, rbitpos;
4836 tree roffset;
4837 int unsignedp, reversep, volatilep = 0;
4838 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4839 &roffset, &rmode, &unsignedp, &reversep,
4840 &volatilep);
4841 if ((rbitpos % BITS_PER_UNIT) != 0)
4843 *bitstart = *bitend = 0;
4844 return;
4848 /* Compute the adjustment to bitpos from the offset of the field
4849 relative to the representative. DECL_FIELD_OFFSET of field and
4850 repr are the same by construction if they are not constants,
4851 see finish_bitfield_layout. */
4852 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4853 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4854 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4855 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4856 else
4857 bitoffset = 0;
4858 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4859 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4861 /* If the adjustment is larger than bitpos, we would have a negative bit
4862 position for the lower bound and this may wreak havoc later. Adjust
4863 offset and bitpos to make the lower bound non-negative in that case. */
4864 if (bitoffset > *bitpos)
4866 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4867 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4869 *bitpos += adjust;
4870 if (*offset == NULL_TREE)
4871 *offset = size_int (-adjust / BITS_PER_UNIT);
4872 else
4873 *offset
4874 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4875 *bitstart = 0;
4877 else
4878 *bitstart = *bitpos - bitoffset;
4880 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4883 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4884 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4885 DECL_RTL was not set yet, return NORTL. */
4887 static inline bool
4888 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4890 if (TREE_CODE (addr) != ADDR_EXPR)
4891 return false;
4893 tree base = TREE_OPERAND (addr, 0);
4895 if (!DECL_P (base)
4896 || TREE_ADDRESSABLE (base)
4897 || DECL_MODE (base) == BLKmode)
4898 return false;
4900 if (!DECL_RTL_SET_P (base))
4901 return nortl;
4903 return (!MEM_P (DECL_RTL (base)));
4906 /* Returns true if the MEM_REF REF refers to an object that does not
4907 reside in memory and has non-BLKmode. */
4909 static inline bool
4910 mem_ref_refers_to_non_mem_p (tree ref)
4912 tree base = TREE_OPERAND (ref, 0);
4913 return addr_expr_of_non_mem_decl_p_1 (base, false);
4916 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4917 is true, try generating a nontemporal store. */
4919 void
4920 expand_assignment (tree to, tree from, bool nontemporal)
4922 rtx to_rtx = 0;
4923 rtx result;
4924 machine_mode mode;
4925 unsigned int align;
4926 enum insn_code icode;
4928 /* Don't crash if the lhs of the assignment was erroneous. */
4929 if (TREE_CODE (to) == ERROR_MARK)
4931 expand_normal (from);
4932 return;
4935 /* Optimize away no-op moves without side-effects. */
4936 if (operand_equal_p (to, from, 0))
4937 return;
4939 /* Handle misaligned stores. */
4940 mode = TYPE_MODE (TREE_TYPE (to));
4941 if ((TREE_CODE (to) == MEM_REF
4942 || TREE_CODE (to) == TARGET_MEM_REF)
4943 && mode != BLKmode
4944 && !mem_ref_refers_to_non_mem_p (to)
4945 && ((align = get_object_alignment (to))
4946 < GET_MODE_ALIGNMENT (mode))
4947 && (((icode = optab_handler (movmisalign_optab, mode))
4948 != CODE_FOR_nothing)
4949 || SLOW_UNALIGNED_ACCESS (mode, align)))
4951 rtx reg, mem;
4953 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4954 reg = force_not_mem (reg);
4955 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4956 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4957 reg = flip_storage_order (mode, reg);
4959 if (icode != CODE_FOR_nothing)
4961 struct expand_operand ops[2];
4963 create_fixed_operand (&ops[0], mem);
4964 create_input_operand (&ops[1], reg, mode);
4965 /* The movmisalign<mode> pattern cannot fail, else the assignment
4966 would silently be omitted. */
4967 expand_insn (icode, 2, ops);
4969 else
4970 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4971 false);
4972 return;
4975 /* Assignment of a structure component needs special treatment
4976 if the structure component's rtx is not simply a MEM.
4977 Assignment of an array element at a constant index, and assignment of
4978 an array element in an unaligned packed structure field, has the same
4979 problem. Same for (partially) storing into a non-memory object. */
4980 if (handled_component_p (to)
4981 || (TREE_CODE (to) == MEM_REF
4982 && (REF_REVERSE_STORAGE_ORDER (to)
4983 || mem_ref_refers_to_non_mem_p (to)))
4984 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4986 machine_mode mode1;
4987 HOST_WIDE_INT bitsize, bitpos;
4988 unsigned HOST_WIDE_INT bitregion_start = 0;
4989 unsigned HOST_WIDE_INT bitregion_end = 0;
4990 tree offset;
4991 int unsignedp, reversep, volatilep = 0;
4992 tree tem;
4994 push_temp_slots ();
4995 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4996 &unsignedp, &reversep, &volatilep);
4998 /* Make sure bitpos is not negative, it can wreak havoc later. */
4999 if (bitpos < 0)
5001 gcc_assert (offset == NULL_TREE);
5002 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
5003 bitpos &= BITS_PER_UNIT - 1;
5006 if (TREE_CODE (to) == COMPONENT_REF
5007 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5008 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5009 /* The C++ memory model naturally applies to byte-aligned fields.
5010 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5011 BITSIZE are not byte-aligned, there is no need to limit the range
5012 we can access. This can occur with packed structures in Ada. */
5013 else if (bitsize > 0
5014 && bitsize % BITS_PER_UNIT == 0
5015 && bitpos % BITS_PER_UNIT == 0)
5017 bitregion_start = bitpos;
5018 bitregion_end = bitpos + bitsize - 1;
5021 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5023 /* If the field has a mode, we want to access it in the
5024 field's mode, not the computed mode.
5025 If a MEM has VOIDmode (external with incomplete type),
5026 use BLKmode for it instead. */
5027 if (MEM_P (to_rtx))
5029 if (mode1 != VOIDmode)
5030 to_rtx = adjust_address (to_rtx, mode1, 0);
5031 else if (GET_MODE (to_rtx) == VOIDmode)
5032 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5035 if (offset != 0)
5037 machine_mode address_mode;
5038 rtx offset_rtx;
5040 if (!MEM_P (to_rtx))
5042 /* We can get constant negative offsets into arrays with broken
5043 user code. Translate this to a trap instead of ICEing. */
5044 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5045 expand_builtin_trap ();
5046 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5049 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5050 address_mode = get_address_mode (to_rtx);
5051 if (GET_MODE (offset_rtx) != address_mode)
5053 /* We cannot be sure that the RTL in offset_rtx is valid outside
5054 of a memory address context, so force it into a register
5055 before attempting to convert it to the desired mode. */
5056 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5057 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5060 /* If we have an expression in OFFSET_RTX and a non-zero
5061 byte offset in BITPOS, adding the byte offset before the
5062 OFFSET_RTX results in better intermediate code, which makes
5063 later rtl optimization passes perform better.
5065 We prefer intermediate code like this:
5067 r124:DI=r123:DI+0x18
5068 [r124:DI]=r121:DI
5070 ... instead of ...
5072 r124:DI=r123:DI+0x10
5073 [r124:DI+0x8]=r121:DI
5075 This is only done for aligned data values, as these can
5076 be expected to result in single move instructions. */
5077 if (mode1 != VOIDmode
5078 && bitpos != 0
5079 && bitsize > 0
5080 && (bitpos % bitsize) == 0
5081 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5082 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5084 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5085 bitregion_start = 0;
5086 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5087 bitregion_end -= bitpos;
5088 bitpos = 0;
5091 to_rtx = offset_address (to_rtx, offset_rtx,
5092 highest_pow2_factor_for_target (to,
5093 offset));
5096 /* No action is needed if the target is not a memory and the field
5097 lies completely outside that target. This can occur if the source
5098 code contains an out-of-bounds access to a small array. */
5099 if (!MEM_P (to_rtx)
5100 && GET_MODE (to_rtx) != BLKmode
5101 && (unsigned HOST_WIDE_INT) bitpos
5102 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5104 expand_normal (from);
5105 result = NULL;
5107 /* Handle expand_expr of a complex value returning a CONCAT. */
5108 else if (GET_CODE (to_rtx) == CONCAT)
5110 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5111 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5112 && bitpos == 0
5113 && bitsize == mode_bitsize)
5114 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5115 else if (bitsize == mode_bitsize / 2
5116 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5117 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5118 nontemporal, reversep);
5119 else if (bitpos + bitsize <= mode_bitsize / 2)
5120 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5121 bitregion_start, bitregion_end,
5122 mode1, from, get_alias_set (to),
5123 nontemporal, reversep);
5124 else if (bitpos >= mode_bitsize / 2)
5125 result = store_field (XEXP (to_rtx, 1), bitsize,
5126 bitpos - mode_bitsize / 2,
5127 bitregion_start, bitregion_end,
5128 mode1, from, get_alias_set (to),
5129 nontemporal, reversep);
5130 else if (bitpos == 0 && bitsize == mode_bitsize)
5132 rtx from_rtx;
5133 result = expand_normal (from);
5134 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5135 TYPE_MODE (TREE_TYPE (from)), 0);
5136 emit_move_insn (XEXP (to_rtx, 0),
5137 read_complex_part (from_rtx, false));
5138 emit_move_insn (XEXP (to_rtx, 1),
5139 read_complex_part (from_rtx, true));
5141 else
5143 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5144 GET_MODE_SIZE (GET_MODE (to_rtx)));
5145 write_complex_part (temp, XEXP (to_rtx, 0), false);
5146 write_complex_part (temp, XEXP (to_rtx, 1), true);
5147 result = store_field (temp, bitsize, bitpos,
5148 bitregion_start, bitregion_end,
5149 mode1, from, get_alias_set (to),
5150 nontemporal, reversep);
5151 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5152 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5155 else
5157 if (MEM_P (to_rtx))
5159 /* If the field is at offset zero, we could have been given the
5160 DECL_RTX of the parent struct. Don't munge it. */
5161 to_rtx = shallow_copy_rtx (to_rtx);
5162 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5163 if (volatilep)
5164 MEM_VOLATILE_P (to_rtx) = 1;
5167 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5168 bitregion_start, bitregion_end,
5169 mode1, to_rtx, to, from,
5170 reversep))
5171 result = NULL;
5172 else
5173 result = store_field (to_rtx, bitsize, bitpos,
5174 bitregion_start, bitregion_end,
5175 mode1, from, get_alias_set (to),
5176 nontemporal, reversep);
5179 if (result)
5180 preserve_temp_slots (result);
5181 pop_temp_slots ();
5182 return;
5185 /* If the rhs is a function call and its value is not an aggregate,
5186 call the function before we start to compute the lhs.
5187 This is needed for correct code for cases such as
5188 val = setjmp (buf) on machines where reference to val
5189 requires loading up part of an address in a separate insn.
5191 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5192 since it might be a promoted variable where the zero- or sign- extension
5193 needs to be done. Handling this in the normal way is safe because no
5194 computation is done before the call. The same is true for SSA names. */
5195 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5196 && COMPLETE_TYPE_P (TREE_TYPE (from))
5197 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5198 && ! (((VAR_P (to)
5199 || TREE_CODE (to) == PARM_DECL
5200 || TREE_CODE (to) == RESULT_DECL)
5201 && REG_P (DECL_RTL (to)))
5202 || TREE_CODE (to) == SSA_NAME))
5204 rtx value;
5205 rtx bounds;
5207 push_temp_slots ();
5208 value = expand_normal (from);
5210 /* Split value and bounds to store them separately. */
5211 chkp_split_slot (value, &value, &bounds);
5213 if (to_rtx == 0)
5214 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5216 /* Handle calls that return values in multiple non-contiguous locations.
5217 The Irix 6 ABI has examples of this. */
5218 if (GET_CODE (to_rtx) == PARALLEL)
5220 if (GET_CODE (value) == PARALLEL)
5221 emit_group_move (to_rtx, value);
5222 else
5223 emit_group_load (to_rtx, value, TREE_TYPE (from),
5224 int_size_in_bytes (TREE_TYPE (from)));
5226 else if (GET_CODE (value) == PARALLEL)
5227 emit_group_store (to_rtx, value, TREE_TYPE (from),
5228 int_size_in_bytes (TREE_TYPE (from)));
5229 else if (GET_MODE (to_rtx) == BLKmode)
5231 /* Handle calls that return BLKmode values in registers. */
5232 if (REG_P (value))
5233 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5234 else
5235 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5237 else
5239 if (POINTER_TYPE_P (TREE_TYPE (to)))
5240 value = convert_memory_address_addr_space
5241 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5242 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5244 emit_move_insn (to_rtx, value);
5247 /* Store bounds if required. */
5248 if (bounds
5249 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5251 gcc_assert (MEM_P (to_rtx));
5252 chkp_emit_bounds_store (bounds, value, to_rtx);
5255 preserve_temp_slots (to_rtx);
5256 pop_temp_slots ();
5257 return;
5260 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5261 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5263 /* Don't move directly into a return register. */
5264 if (TREE_CODE (to) == RESULT_DECL
5265 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5267 rtx temp;
5269 push_temp_slots ();
5271 /* If the source is itself a return value, it still is in a pseudo at
5272 this point so we can move it back to the return register directly. */
5273 if (REG_P (to_rtx)
5274 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5275 && TREE_CODE (from) != CALL_EXPR)
5276 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5277 else
5278 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5280 /* Handle calls that return values in multiple non-contiguous locations.
5281 The Irix 6 ABI has examples of this. */
5282 if (GET_CODE (to_rtx) == PARALLEL)
5284 if (GET_CODE (temp) == PARALLEL)
5285 emit_group_move (to_rtx, temp);
5286 else
5287 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5288 int_size_in_bytes (TREE_TYPE (from)));
5290 else if (temp)
5291 emit_move_insn (to_rtx, temp);
5293 preserve_temp_slots (to_rtx);
5294 pop_temp_slots ();
5295 return;
5298 /* In case we are returning the contents of an object which overlaps
5299 the place the value is being stored, use a safe function when copying
5300 a value through a pointer into a structure value return block. */
5301 if (TREE_CODE (to) == RESULT_DECL
5302 && TREE_CODE (from) == INDIRECT_REF
5303 && ADDR_SPACE_GENERIC_P
5304 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5305 && refs_may_alias_p (to, from)
5306 && cfun->returns_struct
5307 && !cfun->returns_pcc_struct)
5309 rtx from_rtx, size;
5311 push_temp_slots ();
5312 size = expr_size (from);
5313 from_rtx = expand_normal (from);
5315 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5317 preserve_temp_slots (to_rtx);
5318 pop_temp_slots ();
5319 return;
5322 /* Compute FROM and store the value in the rtx we got. */
5324 push_temp_slots ();
5325 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5326 preserve_temp_slots (result);
5327 pop_temp_slots ();
5328 return;
5331 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5332 succeeded, false otherwise. */
5334 bool
5335 emit_storent_insn (rtx to, rtx from)
5337 struct expand_operand ops[2];
5338 machine_mode mode = GET_MODE (to);
5339 enum insn_code code = optab_handler (storent_optab, mode);
5341 if (code == CODE_FOR_nothing)
5342 return false;
5344 create_fixed_operand (&ops[0], to);
5345 create_input_operand (&ops[1], from, mode);
5346 return maybe_expand_insn (code, 2, ops);
5349 /* Generate code for computing expression EXP,
5350 and storing the value into TARGET.
5352 If the mode is BLKmode then we may return TARGET itself.
5353 It turns out that in BLKmode it doesn't cause a problem.
5354 because C has no operators that could combine two different
5355 assignments into the same BLKmode object with different values
5356 with no sequence point. Will other languages need this to
5357 be more thorough?
5359 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5360 stack, and block moves may need to be treated specially.
5362 If NONTEMPORAL is true, try using a nontemporal store instruction.
5364 If REVERSE is true, the store is to be done in reverse order.
5366 If BTARGET is not NULL then computed bounds of EXP are
5367 associated with BTARGET. */
5370 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5371 bool nontemporal, bool reverse, tree btarget)
5373 rtx temp;
5374 rtx alt_rtl = NULL_RTX;
5375 location_t loc = curr_insn_location ();
5377 if (VOID_TYPE_P (TREE_TYPE (exp)))
5379 /* C++ can generate ?: expressions with a throw expression in one
5380 branch and an rvalue in the other. Here, we resolve attempts to
5381 store the throw expression's nonexistent result. */
5382 gcc_assert (!call_param_p);
5383 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5384 return NULL_RTX;
5386 if (TREE_CODE (exp) == COMPOUND_EXPR)
5388 /* Perform first part of compound expression, then assign from second
5389 part. */
5390 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5391 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5392 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5393 call_param_p, nontemporal, reverse,
5394 btarget);
5396 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5398 /* For conditional expression, get safe form of the target. Then
5399 test the condition, doing the appropriate assignment on either
5400 side. This avoids the creation of unnecessary temporaries.
5401 For non-BLKmode, it is more efficient not to do this. */
5403 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5405 do_pending_stack_adjust ();
5406 NO_DEFER_POP;
5407 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5408 profile_probability::uninitialized ());
5409 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5410 nontemporal, reverse, btarget);
5411 emit_jump_insn (targetm.gen_jump (lab2));
5412 emit_barrier ();
5413 emit_label (lab1);
5414 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5415 nontemporal, reverse, btarget);
5416 emit_label (lab2);
5417 OK_DEFER_POP;
5419 return NULL_RTX;
5421 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5422 /* If this is a scalar in a register that is stored in a wider mode
5423 than the declared mode, compute the result into its declared mode
5424 and then convert to the wider mode. Our value is the computed
5425 expression. */
5427 rtx inner_target = 0;
5428 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5429 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5431 /* We can do the conversion inside EXP, which will often result
5432 in some optimizations. Do the conversion in two steps: first
5433 change the signedness, if needed, then the extend. But don't
5434 do this if the type of EXP is a subtype of something else
5435 since then the conversion might involve more than just
5436 converting modes. */
5437 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5438 && TREE_TYPE (TREE_TYPE (exp)) == 0
5439 && GET_MODE_PRECISION (outer_mode)
5440 == TYPE_PRECISION (TREE_TYPE (exp)))
5442 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5443 TYPE_UNSIGNED (TREE_TYPE (exp))))
5445 /* Some types, e.g. Fortran's logical*4, won't have a signed
5446 version, so use the mode instead. */
5447 tree ntype
5448 = (signed_or_unsigned_type_for
5449 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5450 if (ntype == NULL)
5451 ntype = lang_hooks.types.type_for_mode
5452 (TYPE_MODE (TREE_TYPE (exp)),
5453 SUBREG_PROMOTED_SIGN (target));
5455 exp = fold_convert_loc (loc, ntype, exp);
5458 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5459 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5460 exp);
5462 inner_target = SUBREG_REG (target);
5465 temp = expand_expr (exp, inner_target, VOIDmode,
5466 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5468 /* Handle bounds returned by call. */
5469 if (TREE_CODE (exp) == CALL_EXPR)
5471 rtx bounds;
5472 chkp_split_slot (temp, &temp, &bounds);
5473 if (bounds && btarget)
5475 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5476 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5477 chkp_set_rtl_bounds (btarget, tmp);
5481 /* If TEMP is a VOIDmode constant, use convert_modes to make
5482 sure that we properly convert it. */
5483 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5485 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5486 temp, SUBREG_PROMOTED_SIGN (target));
5487 temp = convert_modes (inner_mode, outer_mode, temp,
5488 SUBREG_PROMOTED_SIGN (target));
5491 convert_move (SUBREG_REG (target), temp,
5492 SUBREG_PROMOTED_SIGN (target));
5494 return NULL_RTX;
5496 else if ((TREE_CODE (exp) == STRING_CST
5497 || (TREE_CODE (exp) == MEM_REF
5498 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5499 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5500 == STRING_CST
5501 && integer_zerop (TREE_OPERAND (exp, 1))))
5502 && !nontemporal && !call_param_p
5503 && MEM_P (target))
5505 /* Optimize initialization of an array with a STRING_CST. */
5506 HOST_WIDE_INT exp_len, str_copy_len;
5507 rtx dest_mem;
5508 tree str = TREE_CODE (exp) == STRING_CST
5509 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5511 exp_len = int_expr_size (exp);
5512 if (exp_len <= 0)
5513 goto normal_expr;
5515 if (TREE_STRING_LENGTH (str) <= 0)
5516 goto normal_expr;
5518 str_copy_len = strlen (TREE_STRING_POINTER (str));
5519 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5520 goto normal_expr;
5522 str_copy_len = TREE_STRING_LENGTH (str);
5523 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5524 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5526 str_copy_len += STORE_MAX_PIECES - 1;
5527 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5529 str_copy_len = MIN (str_copy_len, exp_len);
5530 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5531 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5532 MEM_ALIGN (target), false))
5533 goto normal_expr;
5535 dest_mem = target;
5537 dest_mem = store_by_pieces (dest_mem,
5538 str_copy_len, builtin_strncpy_read_str,
5539 CONST_CAST (char *,
5540 TREE_STRING_POINTER (str)),
5541 MEM_ALIGN (target), false,
5542 exp_len > str_copy_len ? 1 : 0);
5543 if (exp_len > str_copy_len)
5544 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5545 GEN_INT (exp_len - str_copy_len),
5546 BLOCK_OP_NORMAL);
5547 return NULL_RTX;
5549 else
5551 rtx tmp_target;
5553 normal_expr:
5554 /* If we want to use a nontemporal or a reverse order store, force the
5555 value into a register first. */
5556 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5557 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5558 (call_param_p
5559 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5560 &alt_rtl, false);
5562 /* Handle bounds returned by call. */
5563 if (TREE_CODE (exp) == CALL_EXPR)
5565 rtx bounds;
5566 chkp_split_slot (temp, &temp, &bounds);
5567 if (bounds && btarget)
5569 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5570 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5571 chkp_set_rtl_bounds (btarget, tmp);
5576 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5577 the same as that of TARGET, adjust the constant. This is needed, for
5578 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5579 only a word-sized value. */
5580 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5581 && TREE_CODE (exp) != ERROR_MARK
5582 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5583 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5584 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5586 /* If value was not generated in the target, store it there.
5587 Convert the value to TARGET's type first if necessary and emit the
5588 pending incrementations that have been queued when expanding EXP.
5589 Note that we cannot emit the whole queue blindly because this will
5590 effectively disable the POST_INC optimization later.
5592 If TEMP and TARGET compare equal according to rtx_equal_p, but
5593 one or both of them are volatile memory refs, we have to distinguish
5594 two cases:
5595 - expand_expr has used TARGET. In this case, we must not generate
5596 another copy. This can be detected by TARGET being equal according
5597 to == .
5598 - expand_expr has not used TARGET - that means that the source just
5599 happens to have the same RTX form. Since temp will have been created
5600 by expand_expr, it will compare unequal according to == .
5601 We must generate a copy in this case, to reach the correct number
5602 of volatile memory references. */
5604 if ((! rtx_equal_p (temp, target)
5605 || (temp != target && (side_effects_p (temp)
5606 || side_effects_p (target))))
5607 && TREE_CODE (exp) != ERROR_MARK
5608 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5609 but TARGET is not valid memory reference, TEMP will differ
5610 from TARGET although it is really the same location. */
5611 && !(alt_rtl
5612 && rtx_equal_p (alt_rtl, target)
5613 && !side_effects_p (alt_rtl)
5614 && !side_effects_p (target))
5615 /* If there's nothing to copy, don't bother. Don't call
5616 expr_size unless necessary, because some front-ends (C++)
5617 expr_size-hook must not be given objects that are not
5618 supposed to be bit-copied or bit-initialized. */
5619 && expr_size (exp) != const0_rtx)
5621 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5623 if (GET_MODE (target) == BLKmode)
5625 /* Handle calls that return BLKmode values in registers. */
5626 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5627 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5628 else
5629 store_bit_field (target,
5630 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5631 0, 0, 0, GET_MODE (temp), temp, reverse);
5633 else
5634 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5637 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5639 /* Handle copying a string constant into an array. The string
5640 constant may be shorter than the array. So copy just the string's
5641 actual length, and clear the rest. First get the size of the data
5642 type of the string, which is actually the size of the target. */
5643 rtx size = expr_size (exp);
5645 if (CONST_INT_P (size)
5646 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5647 emit_block_move (target, temp, size,
5648 (call_param_p
5649 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5650 else
5652 machine_mode pointer_mode
5653 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5654 machine_mode address_mode = get_address_mode (target);
5656 /* Compute the size of the data to copy from the string. */
5657 tree copy_size
5658 = size_binop_loc (loc, MIN_EXPR,
5659 make_tree (sizetype, size),
5660 size_int (TREE_STRING_LENGTH (exp)));
5661 rtx copy_size_rtx
5662 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5663 (call_param_p
5664 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5665 rtx_code_label *label = 0;
5667 /* Copy that much. */
5668 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5669 TYPE_UNSIGNED (sizetype));
5670 emit_block_move (target, temp, copy_size_rtx,
5671 (call_param_p
5672 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5674 /* Figure out how much is left in TARGET that we have to clear.
5675 Do all calculations in pointer_mode. */
5676 if (CONST_INT_P (copy_size_rtx))
5678 size = plus_constant (address_mode, size,
5679 -INTVAL (copy_size_rtx));
5680 target = adjust_address (target, BLKmode,
5681 INTVAL (copy_size_rtx));
5683 else
5685 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5686 copy_size_rtx, NULL_RTX, 0,
5687 OPTAB_LIB_WIDEN);
5689 if (GET_MODE (copy_size_rtx) != address_mode)
5690 copy_size_rtx = convert_to_mode (address_mode,
5691 copy_size_rtx,
5692 TYPE_UNSIGNED (sizetype));
5694 target = offset_address (target, copy_size_rtx,
5695 highest_pow2_factor (copy_size));
5696 label = gen_label_rtx ();
5697 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5698 GET_MODE (size), 0, label);
5701 if (size != const0_rtx)
5702 clear_storage (target, size, BLOCK_OP_NORMAL);
5704 if (label)
5705 emit_label (label);
5708 /* Handle calls that return values in multiple non-contiguous locations.
5709 The Irix 6 ABI has examples of this. */
5710 else if (GET_CODE (target) == PARALLEL)
5712 if (GET_CODE (temp) == PARALLEL)
5713 emit_group_move (target, temp);
5714 else
5715 emit_group_load (target, temp, TREE_TYPE (exp),
5716 int_size_in_bytes (TREE_TYPE (exp)));
5718 else if (GET_CODE (temp) == PARALLEL)
5719 emit_group_store (target, temp, TREE_TYPE (exp),
5720 int_size_in_bytes (TREE_TYPE (exp)));
5721 else if (GET_MODE (temp) == BLKmode)
5722 emit_block_move (target, temp, expr_size (exp),
5723 (call_param_p
5724 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5725 /* If we emit a nontemporal store, there is nothing else to do. */
5726 else if (nontemporal && emit_storent_insn (target, temp))
5728 else
5730 if (reverse)
5731 temp = flip_storage_order (GET_MODE (target), temp);
5732 temp = force_operand (temp, target);
5733 if (temp != target)
5734 emit_move_insn (target, temp);
5738 return NULL_RTX;
5741 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5743 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5744 bool reverse)
5746 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5747 reverse, NULL);
5750 /* Return true if field F of structure TYPE is a flexible array. */
5752 static bool
5753 flexible_array_member_p (const_tree f, const_tree type)
5755 const_tree tf;
5757 tf = TREE_TYPE (f);
5758 return (DECL_CHAIN (f) == NULL
5759 && TREE_CODE (tf) == ARRAY_TYPE
5760 && TYPE_DOMAIN (tf)
5761 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5762 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5763 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5764 && int_size_in_bytes (type) >= 0);
5767 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5768 must have in order for it to completely initialize a value of type TYPE.
5769 Return -1 if the number isn't known.
5771 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5773 static HOST_WIDE_INT
5774 count_type_elements (const_tree type, bool for_ctor_p)
5776 switch (TREE_CODE (type))
5778 case ARRAY_TYPE:
5780 tree nelts;
5782 nelts = array_type_nelts (type);
5783 if (nelts && tree_fits_uhwi_p (nelts))
5785 unsigned HOST_WIDE_INT n;
5787 n = tree_to_uhwi (nelts) + 1;
5788 if (n == 0 || for_ctor_p)
5789 return n;
5790 else
5791 return n * count_type_elements (TREE_TYPE (type), false);
5793 return for_ctor_p ? -1 : 1;
5796 case RECORD_TYPE:
5798 unsigned HOST_WIDE_INT n;
5799 tree f;
5801 n = 0;
5802 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5803 if (TREE_CODE (f) == FIELD_DECL)
5805 if (!for_ctor_p)
5806 n += count_type_elements (TREE_TYPE (f), false);
5807 else if (!flexible_array_member_p (f, type))
5808 /* Don't count flexible arrays, which are not supposed
5809 to be initialized. */
5810 n += 1;
5813 return n;
5816 case UNION_TYPE:
5817 case QUAL_UNION_TYPE:
5819 tree f;
5820 HOST_WIDE_INT n, m;
5822 gcc_assert (!for_ctor_p);
5823 /* Estimate the number of scalars in each field and pick the
5824 maximum. Other estimates would do instead; the idea is simply
5825 to make sure that the estimate is not sensitive to the ordering
5826 of the fields. */
5827 n = 1;
5828 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5829 if (TREE_CODE (f) == FIELD_DECL)
5831 m = count_type_elements (TREE_TYPE (f), false);
5832 /* If the field doesn't span the whole union, add an extra
5833 scalar for the rest. */
5834 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5835 TYPE_SIZE (type)) != 1)
5836 m++;
5837 if (n < m)
5838 n = m;
5840 return n;
5843 case COMPLEX_TYPE:
5844 return 2;
5846 case VECTOR_TYPE:
5847 return TYPE_VECTOR_SUBPARTS (type);
5849 case INTEGER_TYPE:
5850 case REAL_TYPE:
5851 case FIXED_POINT_TYPE:
5852 case ENUMERAL_TYPE:
5853 case BOOLEAN_TYPE:
5854 case POINTER_TYPE:
5855 case OFFSET_TYPE:
5856 case REFERENCE_TYPE:
5857 case NULLPTR_TYPE:
5858 return 1;
5860 case ERROR_MARK:
5861 return 0;
5863 case VOID_TYPE:
5864 case METHOD_TYPE:
5865 case FUNCTION_TYPE:
5866 case LANG_TYPE:
5867 default:
5868 gcc_unreachable ();
5872 /* Helper for categorize_ctor_elements. Identical interface. */
5874 static bool
5875 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5876 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5878 unsigned HOST_WIDE_INT idx;
5879 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5880 tree value, purpose, elt_type;
5882 /* Whether CTOR is a valid constant initializer, in accordance with what
5883 initializer_constant_valid_p does. If inferred from the constructor
5884 elements, true until proven otherwise. */
5885 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5886 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5888 nz_elts = 0;
5889 init_elts = 0;
5890 num_fields = 0;
5891 elt_type = NULL_TREE;
5893 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5895 HOST_WIDE_INT mult = 1;
5897 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5899 tree lo_index = TREE_OPERAND (purpose, 0);
5900 tree hi_index = TREE_OPERAND (purpose, 1);
5902 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5903 mult = (tree_to_uhwi (hi_index)
5904 - tree_to_uhwi (lo_index) + 1);
5906 num_fields += mult;
5907 elt_type = TREE_TYPE (value);
5909 switch (TREE_CODE (value))
5911 case CONSTRUCTOR:
5913 HOST_WIDE_INT nz = 0, ic = 0;
5915 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5916 p_complete);
5918 nz_elts += mult * nz;
5919 init_elts += mult * ic;
5921 if (const_from_elts_p && const_p)
5922 const_p = const_elt_p;
5924 break;
5926 case INTEGER_CST:
5927 case REAL_CST:
5928 case FIXED_CST:
5929 if (!initializer_zerop (value))
5930 nz_elts += mult;
5931 init_elts += mult;
5932 break;
5934 case STRING_CST:
5935 nz_elts += mult * TREE_STRING_LENGTH (value);
5936 init_elts += mult * TREE_STRING_LENGTH (value);
5937 break;
5939 case COMPLEX_CST:
5940 if (!initializer_zerop (TREE_REALPART (value)))
5941 nz_elts += mult;
5942 if (!initializer_zerop (TREE_IMAGPART (value)))
5943 nz_elts += mult;
5944 init_elts += mult;
5945 break;
5947 case VECTOR_CST:
5949 unsigned i;
5950 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5952 tree v = VECTOR_CST_ELT (value, i);
5953 if (!initializer_zerop (v))
5954 nz_elts += mult;
5955 init_elts += mult;
5958 break;
5960 default:
5962 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5963 nz_elts += mult * tc;
5964 init_elts += mult * tc;
5966 if (const_from_elts_p && const_p)
5967 const_p
5968 = initializer_constant_valid_p (value,
5969 elt_type,
5970 TYPE_REVERSE_STORAGE_ORDER
5971 (TREE_TYPE (ctor)))
5972 != NULL_TREE;
5974 break;
5978 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5979 num_fields, elt_type))
5980 *p_complete = false;
5982 *p_nz_elts += nz_elts;
5983 *p_init_elts += init_elts;
5985 return const_p;
5988 /* Examine CTOR to discover:
5989 * how many scalar fields are set to nonzero values,
5990 and place it in *P_NZ_ELTS;
5991 * how many scalar fields in total are in CTOR,
5992 and place it in *P_ELT_COUNT.
5993 * whether the constructor is complete -- in the sense that every
5994 meaningful byte is explicitly given a value --
5995 and place it in *P_COMPLETE.
5997 Return whether or not CTOR is a valid static constant initializer, the same
5998 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6000 bool
6001 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6002 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6004 *p_nz_elts = 0;
6005 *p_init_elts = 0;
6006 *p_complete = true;
6008 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6011 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6012 of which had type LAST_TYPE. Each element was itself a complete
6013 initializer, in the sense that every meaningful byte was explicitly
6014 given a value. Return true if the same is true for the constructor
6015 as a whole. */
6017 bool
6018 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6019 const_tree last_type)
6021 if (TREE_CODE (type) == UNION_TYPE
6022 || TREE_CODE (type) == QUAL_UNION_TYPE)
6024 if (num_elts == 0)
6025 return false;
6027 gcc_assert (num_elts == 1 && last_type);
6029 /* ??? We could look at each element of the union, and find the
6030 largest element. Which would avoid comparing the size of the
6031 initialized element against any tail padding in the union.
6032 Doesn't seem worth the effort... */
6033 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6036 return count_type_elements (type, true) == num_elts;
6039 /* Return 1 if EXP contains mostly (3/4) zeros. */
6041 static int
6042 mostly_zeros_p (const_tree exp)
6044 if (TREE_CODE (exp) == CONSTRUCTOR)
6046 HOST_WIDE_INT nz_elts, init_elts;
6047 bool complete_p;
6049 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6050 return !complete_p || nz_elts < init_elts / 4;
6053 return initializer_zerop (exp);
6056 /* Return 1 if EXP contains all zeros. */
6058 static int
6059 all_zeros_p (const_tree exp)
6061 if (TREE_CODE (exp) == CONSTRUCTOR)
6063 HOST_WIDE_INT nz_elts, init_elts;
6064 bool complete_p;
6066 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6067 return nz_elts == 0;
6070 return initializer_zerop (exp);
6073 /* Helper function for store_constructor.
6074 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6075 CLEARED is as for store_constructor.
6076 ALIAS_SET is the alias set to use for any stores.
6077 If REVERSE is true, the store is to be done in reverse order.
6079 This provides a recursive shortcut back to store_constructor when it isn't
6080 necessary to go through store_field. This is so that we can pass through
6081 the cleared field to let store_constructor know that we may not have to
6082 clear a substructure if the outer structure has already been cleared. */
6084 static void
6085 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6086 HOST_WIDE_INT bitpos,
6087 unsigned HOST_WIDE_INT bitregion_start,
6088 unsigned HOST_WIDE_INT bitregion_end,
6089 machine_mode mode,
6090 tree exp, int cleared,
6091 alias_set_type alias_set, bool reverse)
6093 if (TREE_CODE (exp) == CONSTRUCTOR
6094 /* We can only call store_constructor recursively if the size and
6095 bit position are on a byte boundary. */
6096 && bitpos % BITS_PER_UNIT == 0
6097 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6098 /* If we have a nonzero bitpos for a register target, then we just
6099 let store_field do the bitfield handling. This is unlikely to
6100 generate unnecessary clear instructions anyways. */
6101 && (bitpos == 0 || MEM_P (target)))
6103 if (MEM_P (target))
6104 target
6105 = adjust_address (target,
6106 GET_MODE (target) == BLKmode
6107 || 0 != (bitpos
6108 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6109 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6112 /* Update the alias set, if required. */
6113 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6114 && MEM_ALIAS_SET (target) != 0)
6116 target = copy_rtx (target);
6117 set_mem_alias_set (target, alias_set);
6120 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6121 reverse);
6123 else
6124 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6125 exp, alias_set, false, reverse);
6129 /* Returns the number of FIELD_DECLs in TYPE. */
6131 static int
6132 fields_length (const_tree type)
6134 tree t = TYPE_FIELDS (type);
6135 int count = 0;
6137 for (; t; t = DECL_CHAIN (t))
6138 if (TREE_CODE (t) == FIELD_DECL)
6139 ++count;
6141 return count;
6145 /* Store the value of constructor EXP into the rtx TARGET.
6146 TARGET is either a REG or a MEM; we know it cannot conflict, since
6147 safe_from_p has been called.
6148 CLEARED is true if TARGET is known to have been zero'd.
6149 SIZE is the number of bytes of TARGET we are allowed to modify: this
6150 may not be the same as the size of EXP if we are assigning to a field
6151 which has been packed to exclude padding bits.
6152 If REVERSE is true, the store is to be done in reverse order. */
6154 static void
6155 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6156 bool reverse)
6158 tree type = TREE_TYPE (exp);
6159 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6160 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6162 switch (TREE_CODE (type))
6164 case RECORD_TYPE:
6165 case UNION_TYPE:
6166 case QUAL_UNION_TYPE:
6168 unsigned HOST_WIDE_INT idx;
6169 tree field, value;
6171 /* The storage order is specified for every aggregate type. */
6172 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6174 /* If size is zero or the target is already cleared, do nothing. */
6175 if (size == 0 || cleared)
6176 cleared = 1;
6177 /* We either clear the aggregate or indicate the value is dead. */
6178 else if ((TREE_CODE (type) == UNION_TYPE
6179 || TREE_CODE (type) == QUAL_UNION_TYPE)
6180 && ! CONSTRUCTOR_ELTS (exp))
6181 /* If the constructor is empty, clear the union. */
6183 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6184 cleared = 1;
6187 /* If we are building a static constructor into a register,
6188 set the initial value as zero so we can fold the value into
6189 a constant. But if more than one register is involved,
6190 this probably loses. */
6191 else if (REG_P (target) && TREE_STATIC (exp)
6192 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6194 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6195 cleared = 1;
6198 /* If the constructor has fewer fields than the structure or
6199 if we are initializing the structure to mostly zeros, clear
6200 the whole structure first. Don't do this if TARGET is a
6201 register whose mode size isn't equal to SIZE since
6202 clear_storage can't handle this case. */
6203 else if (size > 0
6204 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6205 || mostly_zeros_p (exp))
6206 && (!REG_P (target)
6207 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6208 == size)))
6210 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6211 cleared = 1;
6214 if (REG_P (target) && !cleared)
6215 emit_clobber (target);
6217 /* Store each element of the constructor into the
6218 corresponding field of TARGET. */
6219 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6221 machine_mode mode;
6222 HOST_WIDE_INT bitsize;
6223 HOST_WIDE_INT bitpos = 0;
6224 tree offset;
6225 rtx to_rtx = target;
6227 /* Just ignore missing fields. We cleared the whole
6228 structure, above, if any fields are missing. */
6229 if (field == 0)
6230 continue;
6232 if (cleared && initializer_zerop (value))
6233 continue;
6235 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6236 bitsize = tree_to_uhwi (DECL_SIZE (field));
6237 else
6238 gcc_unreachable ();
6240 mode = DECL_MODE (field);
6241 if (DECL_BIT_FIELD (field))
6242 mode = VOIDmode;
6244 offset = DECL_FIELD_OFFSET (field);
6245 if (tree_fits_shwi_p (offset)
6246 && tree_fits_shwi_p (bit_position (field)))
6248 bitpos = int_bit_position (field);
6249 offset = NULL_TREE;
6251 else
6252 gcc_unreachable ();
6254 /* If this initializes a field that is smaller than a
6255 word, at the start of a word, try to widen it to a full
6256 word. This special case allows us to output C++ member
6257 function initializations in a form that the optimizers
6258 can understand. */
6259 if (WORD_REGISTER_OPERATIONS
6260 && REG_P (target)
6261 && bitsize < BITS_PER_WORD
6262 && bitpos % BITS_PER_WORD == 0
6263 && GET_MODE_CLASS (mode) == MODE_INT
6264 && TREE_CODE (value) == INTEGER_CST
6265 && exp_size >= 0
6266 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6268 tree type = TREE_TYPE (value);
6270 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6272 type = lang_hooks.types.type_for_mode
6273 (word_mode, TYPE_UNSIGNED (type));
6274 value = fold_convert (type, value);
6275 /* Make sure the bits beyond the original bitsize are zero
6276 so that we can correctly avoid extra zeroing stores in
6277 later constructor elements. */
6278 tree bitsize_mask
6279 = wide_int_to_tree (type, wi::mask (bitsize, false,
6280 BITS_PER_WORD));
6281 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6284 if (BYTES_BIG_ENDIAN)
6285 value
6286 = fold_build2 (LSHIFT_EXPR, type, value,
6287 build_int_cst (type,
6288 BITS_PER_WORD - bitsize));
6289 bitsize = BITS_PER_WORD;
6290 mode = word_mode;
6293 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6294 && DECL_NONADDRESSABLE_P (field))
6296 to_rtx = copy_rtx (to_rtx);
6297 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6300 store_constructor_field (to_rtx, bitsize, bitpos,
6301 0, bitregion_end, mode,
6302 value, cleared,
6303 get_alias_set (TREE_TYPE (field)),
6304 reverse);
6306 break;
6308 case ARRAY_TYPE:
6310 tree value, index;
6311 unsigned HOST_WIDE_INT i;
6312 int need_to_clear;
6313 tree domain;
6314 tree elttype = TREE_TYPE (type);
6315 int const_bounds_p;
6316 HOST_WIDE_INT minelt = 0;
6317 HOST_WIDE_INT maxelt = 0;
6319 /* The storage order is specified for every aggregate type. */
6320 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6322 domain = TYPE_DOMAIN (type);
6323 const_bounds_p = (TYPE_MIN_VALUE (domain)
6324 && TYPE_MAX_VALUE (domain)
6325 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6326 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6328 /* If we have constant bounds for the range of the type, get them. */
6329 if (const_bounds_p)
6331 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6332 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6335 /* If the constructor has fewer elements than the array, clear
6336 the whole array first. Similarly if this is static
6337 constructor of a non-BLKmode object. */
6338 if (cleared)
6339 need_to_clear = 0;
6340 else if (REG_P (target) && TREE_STATIC (exp))
6341 need_to_clear = 1;
6342 else
6344 unsigned HOST_WIDE_INT idx;
6345 tree index, value;
6346 HOST_WIDE_INT count = 0, zero_count = 0;
6347 need_to_clear = ! const_bounds_p;
6349 /* This loop is a more accurate version of the loop in
6350 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6351 is also needed to check for missing elements. */
6352 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6354 HOST_WIDE_INT this_node_count;
6356 if (need_to_clear)
6357 break;
6359 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6361 tree lo_index = TREE_OPERAND (index, 0);
6362 tree hi_index = TREE_OPERAND (index, 1);
6364 if (! tree_fits_uhwi_p (lo_index)
6365 || ! tree_fits_uhwi_p (hi_index))
6367 need_to_clear = 1;
6368 break;
6371 this_node_count = (tree_to_uhwi (hi_index)
6372 - tree_to_uhwi (lo_index) + 1);
6374 else
6375 this_node_count = 1;
6377 count += this_node_count;
6378 if (mostly_zeros_p (value))
6379 zero_count += this_node_count;
6382 /* Clear the entire array first if there are any missing
6383 elements, or if the incidence of zero elements is >=
6384 75%. */
6385 if (! need_to_clear
6386 && (count < maxelt - minelt + 1
6387 || 4 * zero_count >= 3 * count))
6388 need_to_clear = 1;
6391 if (need_to_clear && size > 0)
6393 if (REG_P (target))
6394 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6395 else
6396 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6397 cleared = 1;
6400 if (!cleared && REG_P (target))
6401 /* Inform later passes that the old value is dead. */
6402 emit_clobber (target);
6404 /* Store each element of the constructor into the
6405 corresponding element of TARGET, determined by counting the
6406 elements. */
6407 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6409 machine_mode mode;
6410 HOST_WIDE_INT bitsize;
6411 HOST_WIDE_INT bitpos;
6412 rtx xtarget = target;
6414 if (cleared && initializer_zerop (value))
6415 continue;
6417 mode = TYPE_MODE (elttype);
6418 if (mode == BLKmode)
6419 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6420 ? tree_to_uhwi (TYPE_SIZE (elttype))
6421 : -1);
6422 else
6423 bitsize = GET_MODE_BITSIZE (mode);
6425 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6427 tree lo_index = TREE_OPERAND (index, 0);
6428 tree hi_index = TREE_OPERAND (index, 1);
6429 rtx index_r, pos_rtx;
6430 HOST_WIDE_INT lo, hi, count;
6431 tree position;
6433 /* If the range is constant and "small", unroll the loop. */
6434 if (const_bounds_p
6435 && tree_fits_shwi_p (lo_index)
6436 && tree_fits_shwi_p (hi_index)
6437 && (lo = tree_to_shwi (lo_index),
6438 hi = tree_to_shwi (hi_index),
6439 count = hi - lo + 1,
6440 (!MEM_P (target)
6441 || count <= 2
6442 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6443 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6444 <= 40 * 8)))))
6446 lo -= minelt; hi -= minelt;
6447 for (; lo <= hi; lo++)
6449 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6451 if (MEM_P (target)
6452 && !MEM_KEEP_ALIAS_SET_P (target)
6453 && TREE_CODE (type) == ARRAY_TYPE
6454 && TYPE_NONALIASED_COMPONENT (type))
6456 target = copy_rtx (target);
6457 MEM_KEEP_ALIAS_SET_P (target) = 1;
6460 store_constructor_field
6461 (target, bitsize, bitpos, 0, bitregion_end,
6462 mode, value, cleared,
6463 get_alias_set (elttype), reverse);
6466 else
6468 rtx_code_label *loop_start = gen_label_rtx ();
6469 rtx_code_label *loop_end = gen_label_rtx ();
6470 tree exit_cond;
6472 expand_normal (hi_index);
6474 index = build_decl (EXPR_LOCATION (exp),
6475 VAR_DECL, NULL_TREE, domain);
6476 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6477 SET_DECL_RTL (index, index_r);
6478 store_expr (lo_index, index_r, 0, false, reverse);
6480 /* Build the head of the loop. */
6481 do_pending_stack_adjust ();
6482 emit_label (loop_start);
6484 /* Assign value to element index. */
6485 position =
6486 fold_convert (ssizetype,
6487 fold_build2 (MINUS_EXPR,
6488 TREE_TYPE (index),
6489 index,
6490 TYPE_MIN_VALUE (domain)));
6492 position =
6493 size_binop (MULT_EXPR, position,
6494 fold_convert (ssizetype,
6495 TYPE_SIZE_UNIT (elttype)));
6497 pos_rtx = expand_normal (position);
6498 xtarget = offset_address (target, pos_rtx,
6499 highest_pow2_factor (position));
6500 xtarget = adjust_address (xtarget, mode, 0);
6501 if (TREE_CODE (value) == CONSTRUCTOR)
6502 store_constructor (value, xtarget, cleared,
6503 bitsize / BITS_PER_UNIT, reverse);
6504 else
6505 store_expr (value, xtarget, 0, false, reverse);
6507 /* Generate a conditional jump to exit the loop. */
6508 exit_cond = build2 (LT_EXPR, integer_type_node,
6509 index, hi_index);
6510 jumpif (exit_cond, loop_end,
6511 profile_probability::uninitialized ());
6513 /* Update the loop counter, and jump to the head of
6514 the loop. */
6515 expand_assignment (index,
6516 build2 (PLUS_EXPR, TREE_TYPE (index),
6517 index, integer_one_node),
6518 false);
6520 emit_jump (loop_start);
6522 /* Build the end of the loop. */
6523 emit_label (loop_end);
6526 else if ((index != 0 && ! tree_fits_shwi_p (index))
6527 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6529 tree position;
6531 if (index == 0)
6532 index = ssize_int (1);
6534 if (minelt)
6535 index = fold_convert (ssizetype,
6536 fold_build2 (MINUS_EXPR,
6537 TREE_TYPE (index),
6538 index,
6539 TYPE_MIN_VALUE (domain)));
6541 position =
6542 size_binop (MULT_EXPR, index,
6543 fold_convert (ssizetype,
6544 TYPE_SIZE_UNIT (elttype)));
6545 xtarget = offset_address (target,
6546 expand_normal (position),
6547 highest_pow2_factor (position));
6548 xtarget = adjust_address (xtarget, mode, 0);
6549 store_expr (value, xtarget, 0, false, reverse);
6551 else
6553 if (index != 0)
6554 bitpos = ((tree_to_shwi (index) - minelt)
6555 * tree_to_uhwi (TYPE_SIZE (elttype)));
6556 else
6557 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6559 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6560 && TREE_CODE (type) == ARRAY_TYPE
6561 && TYPE_NONALIASED_COMPONENT (type))
6563 target = copy_rtx (target);
6564 MEM_KEEP_ALIAS_SET_P (target) = 1;
6566 store_constructor_field (target, bitsize, bitpos, 0,
6567 bitregion_end, mode, value,
6568 cleared, get_alias_set (elttype),
6569 reverse);
6572 break;
6575 case VECTOR_TYPE:
6577 unsigned HOST_WIDE_INT idx;
6578 constructor_elt *ce;
6579 int i;
6580 int need_to_clear;
6581 int icode = CODE_FOR_nothing;
6582 tree elttype = TREE_TYPE (type);
6583 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6584 machine_mode eltmode = TYPE_MODE (elttype);
6585 HOST_WIDE_INT bitsize;
6586 HOST_WIDE_INT bitpos;
6587 rtvec vector = NULL;
6588 unsigned n_elts;
6589 alias_set_type alias;
6590 bool vec_vec_init_p = false;
6592 gcc_assert (eltmode != BLKmode);
6594 n_elts = TYPE_VECTOR_SUBPARTS (type);
6595 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6597 machine_mode mode = GET_MODE (target);
6598 machine_mode emode = eltmode;
6600 if (CONSTRUCTOR_NELTS (exp)
6601 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6602 == VECTOR_TYPE))
6604 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6605 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6606 == n_elts);
6607 emode = TYPE_MODE (etype);
6609 icode = (int) convert_optab_handler (vec_init_optab, mode, emode);
6610 if (icode != CODE_FOR_nothing)
6612 unsigned int i, n = n_elts;
6614 if (emode != eltmode)
6616 n = CONSTRUCTOR_NELTS (exp);
6617 vec_vec_init_p = true;
6619 vector = rtvec_alloc (n);
6620 for (i = 0; i < n; i++)
6621 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6625 /* If the constructor has fewer elements than the vector,
6626 clear the whole array first. Similarly if this is static
6627 constructor of a non-BLKmode object. */
6628 if (cleared)
6629 need_to_clear = 0;
6630 else if (REG_P (target) && TREE_STATIC (exp))
6631 need_to_clear = 1;
6632 else
6634 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6635 tree value;
6637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6639 tree sz = TYPE_SIZE (TREE_TYPE (value));
6640 int n_elts_here
6641 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6642 TYPE_SIZE (elttype)));
6644 count += n_elts_here;
6645 if (mostly_zeros_p (value))
6646 zero_count += n_elts_here;
6649 /* Clear the entire vector first if there are any missing elements,
6650 or if the incidence of zero elements is >= 75%. */
6651 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6654 if (need_to_clear && size > 0 && !vector)
6656 if (REG_P (target))
6657 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6658 else
6659 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6660 cleared = 1;
6663 /* Inform later passes that the old value is dead. */
6664 if (!cleared && !vector && REG_P (target))
6665 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6667 if (MEM_P (target))
6668 alias = MEM_ALIAS_SET (target);
6669 else
6670 alias = get_alias_set (elttype);
6672 /* Store each element of the constructor into the corresponding
6673 element of TARGET, determined by counting the elements. */
6674 for (idx = 0, i = 0;
6675 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6676 idx++, i += bitsize / elt_size)
6678 HOST_WIDE_INT eltpos;
6679 tree value = ce->value;
6681 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6682 if (cleared && initializer_zerop (value))
6683 continue;
6685 if (ce->index)
6686 eltpos = tree_to_uhwi (ce->index);
6687 else
6688 eltpos = i;
6690 if (vector)
6692 if (vec_vec_init_p)
6694 gcc_assert (ce->index == NULL_TREE);
6695 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6696 eltpos = idx;
6698 else
6699 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6700 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6702 else
6704 machine_mode value_mode
6705 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6706 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6707 bitpos = eltpos * elt_size;
6708 store_constructor_field (target, bitsize, bitpos, 0,
6709 bitregion_end, value_mode,
6710 value, cleared, alias, reverse);
6714 if (vector)
6715 emit_insn (GEN_FCN (icode) (target,
6716 gen_rtx_PARALLEL (GET_MODE (target),
6717 vector)));
6718 break;
6721 default:
6722 gcc_unreachable ();
6726 /* Store the value of EXP (an expression tree)
6727 into a subfield of TARGET which has mode MODE and occupies
6728 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6729 If MODE is VOIDmode, it means that we are storing into a bit-field.
6731 BITREGION_START is bitpos of the first bitfield in this region.
6732 BITREGION_END is the bitpos of the ending bitfield in this region.
6733 These two fields are 0, if the C++ memory model does not apply,
6734 or we are not interested in keeping track of bitfield regions.
6736 Always return const0_rtx unless we have something particular to
6737 return.
6739 ALIAS_SET is the alias set for the destination. This value will
6740 (in general) be different from that for TARGET, since TARGET is a
6741 reference to the containing structure.
6743 If NONTEMPORAL is true, try generating a nontemporal store.
6745 If REVERSE is true, the store is to be done in reverse order. */
6747 static rtx
6748 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6749 unsigned HOST_WIDE_INT bitregion_start,
6750 unsigned HOST_WIDE_INT bitregion_end,
6751 machine_mode mode, tree exp,
6752 alias_set_type alias_set, bool nontemporal, bool reverse)
6754 if (TREE_CODE (exp) == ERROR_MARK)
6755 return const0_rtx;
6757 /* If we have nothing to store, do nothing unless the expression has
6758 side-effects. */
6759 if (bitsize == 0)
6760 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6762 if (GET_CODE (target) == CONCAT)
6764 /* We're storing into a struct containing a single __complex. */
6766 gcc_assert (!bitpos);
6767 return store_expr (exp, target, 0, nontemporal, reverse);
6770 /* If the structure is in a register or if the component
6771 is a bit field, we cannot use addressing to access it.
6772 Use bit-field techniques or SUBREG to store in it. */
6774 if (mode == VOIDmode
6775 || (mode != BLKmode && ! direct_store[(int) mode]
6776 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6777 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6778 || REG_P (target)
6779 || GET_CODE (target) == SUBREG
6780 /* If the field isn't aligned enough to store as an ordinary memref,
6781 store it as a bit field. */
6782 || (mode != BLKmode
6783 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6784 || bitpos % GET_MODE_ALIGNMENT (mode))
6785 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6786 || (bitpos % BITS_PER_UNIT != 0)))
6787 || (bitsize >= 0 && mode != BLKmode
6788 && GET_MODE_BITSIZE (mode) > bitsize)
6789 /* If the RHS and field are a constant size and the size of the
6790 RHS isn't the same size as the bitfield, we must use bitfield
6791 operations. */
6792 || (bitsize >= 0
6793 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6794 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6795 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6796 we will handle specially below. */
6797 && !(TREE_CODE (exp) == CONSTRUCTOR
6798 && bitsize % BITS_PER_UNIT == 0)
6799 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6800 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6801 includes some extra padding. store_expr / expand_expr will in
6802 that case call get_inner_reference that will have the bitsize
6803 we check here and thus the block move will not clobber the
6804 padding that shouldn't be clobbered. In the future we could
6805 replace the TREE_ADDRESSABLE check with a check that
6806 get_base_address needs to live in memory. */
6807 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6808 || TREE_CODE (exp) != COMPONENT_REF
6809 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6810 || (bitsize % BITS_PER_UNIT != 0)
6811 || (bitpos % BITS_PER_UNIT != 0)
6812 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6813 != 0)))
6814 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6815 decl we must use bitfield operations. */
6816 || (bitsize >= 0
6817 && TREE_CODE (exp) == MEM_REF
6818 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6819 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6820 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6821 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6823 rtx temp;
6824 gimple *nop_def;
6826 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6827 implies a mask operation. If the precision is the same size as
6828 the field we're storing into, that mask is redundant. This is
6829 particularly common with bit field assignments generated by the
6830 C front end. */
6831 nop_def = get_def_for_expr (exp, NOP_EXPR);
6832 if (nop_def)
6834 tree type = TREE_TYPE (exp);
6835 if (INTEGRAL_TYPE_P (type)
6836 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6837 && bitsize == TYPE_PRECISION (type))
6839 tree op = gimple_assign_rhs1 (nop_def);
6840 type = TREE_TYPE (op);
6841 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6842 exp = op;
6846 temp = expand_normal (exp);
6848 /* Handle calls that return values in multiple non-contiguous locations.
6849 The Irix 6 ABI has examples of this. */
6850 if (GET_CODE (temp) == PARALLEL)
6852 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6853 scalar_int_mode temp_mode
6854 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6855 rtx temp_target = gen_reg_rtx (temp_mode);
6856 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6857 temp = temp_target;
6860 /* Handle calls that return BLKmode values in registers. */
6861 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6863 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6864 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6865 temp = temp_target;
6868 /* If the value has aggregate type and an integral mode then, if BITSIZE
6869 is narrower than this mode and this is for big-endian data, we first
6870 need to put the value into the low-order bits for store_bit_field,
6871 except when MODE is BLKmode and BITSIZE larger than the word size
6872 (see the handling of fields larger than a word in store_bit_field).
6873 Moreover, the field may be not aligned on a byte boundary; in this
6874 case, if it has reverse storage order, it needs to be accessed as a
6875 scalar field with reverse storage order and we must first put the
6876 value into target order. */
6877 scalar_int_mode temp_mode;
6878 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6879 && is_int_mode (GET_MODE (temp), &temp_mode))
6881 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6883 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6885 if (reverse)
6886 temp = flip_storage_order (temp_mode, temp);
6888 if (bitsize < size
6889 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6890 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6891 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6892 size - bitsize, NULL_RTX, 1);
6895 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6896 if (mode != VOIDmode && mode != BLKmode
6897 && mode != TYPE_MODE (TREE_TYPE (exp)))
6898 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6900 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6901 and BITPOS must be aligned on a byte boundary. If so, we simply do
6902 a block copy. Likewise for a BLKmode-like TARGET. */
6903 if (GET_MODE (temp) == BLKmode
6904 && (GET_MODE (target) == BLKmode
6905 || (MEM_P (target)
6906 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6907 && (bitpos % BITS_PER_UNIT) == 0
6908 && (bitsize % BITS_PER_UNIT) == 0)))
6910 gcc_assert (MEM_P (target) && MEM_P (temp)
6911 && (bitpos % BITS_PER_UNIT) == 0);
6913 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6914 emit_block_move (target, temp,
6915 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6916 / BITS_PER_UNIT),
6917 BLOCK_OP_NORMAL);
6919 return const0_rtx;
6922 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6923 word size, we need to load the value (see again store_bit_field). */
6924 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6926 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
6927 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6928 temp_mode, false, NULL);
6931 /* Store the value in the bitfield. */
6932 store_bit_field (target, bitsize, bitpos,
6933 bitregion_start, bitregion_end,
6934 mode, temp, reverse);
6936 return const0_rtx;
6938 else
6940 /* Now build a reference to just the desired component. */
6941 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6943 if (to_rtx == target)
6944 to_rtx = copy_rtx (to_rtx);
6946 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6947 set_mem_alias_set (to_rtx, alias_set);
6949 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6950 into a target smaller than its type; handle that case now. */
6951 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6953 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6954 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6955 return to_rtx;
6958 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6962 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6963 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6964 codes and find the ultimate containing object, which we return.
6966 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6967 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6968 storage order of the field.
6969 If the position of the field is variable, we store a tree
6970 giving the variable offset (in units) in *POFFSET.
6971 This offset is in addition to the bit position.
6972 If the position is not variable, we store 0 in *POFFSET.
6974 If any of the extraction expressions is volatile,
6975 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6977 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6978 Otherwise, it is a mode that can be used to access the field.
6980 If the field describes a variable-sized object, *PMODE is set to
6981 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6982 this case, but the address of the object can be found. */
6984 tree
6985 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6986 HOST_WIDE_INT *pbitpos, tree *poffset,
6987 machine_mode *pmode, int *punsignedp,
6988 int *preversep, int *pvolatilep)
6990 tree size_tree = 0;
6991 machine_mode mode = VOIDmode;
6992 bool blkmode_bitfield = false;
6993 tree offset = size_zero_node;
6994 offset_int bit_offset = 0;
6996 /* First get the mode, signedness, storage order and size. We do this from
6997 just the outermost expression. */
6998 *pbitsize = -1;
6999 if (TREE_CODE (exp) == COMPONENT_REF)
7001 tree field = TREE_OPERAND (exp, 1);
7002 size_tree = DECL_SIZE (field);
7003 if (flag_strict_volatile_bitfields > 0
7004 && TREE_THIS_VOLATILE (exp)
7005 && DECL_BIT_FIELD_TYPE (field)
7006 && DECL_MODE (field) != BLKmode)
7007 /* Volatile bitfields should be accessed in the mode of the
7008 field's type, not the mode computed based on the bit
7009 size. */
7010 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7011 else if (!DECL_BIT_FIELD (field))
7012 mode = DECL_MODE (field);
7013 else if (DECL_MODE (field) == BLKmode)
7014 blkmode_bitfield = true;
7016 *punsignedp = DECL_UNSIGNED (field);
7018 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7020 size_tree = TREE_OPERAND (exp, 1);
7021 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7022 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7024 /* For vector types, with the correct size of access, use the mode of
7025 inner type. */
7026 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7027 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7028 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7029 mode = TYPE_MODE (TREE_TYPE (exp));
7031 else
7033 mode = TYPE_MODE (TREE_TYPE (exp));
7034 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7036 if (mode == BLKmode)
7037 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7038 else
7039 *pbitsize = GET_MODE_BITSIZE (mode);
7042 if (size_tree != 0)
7044 if (! tree_fits_uhwi_p (size_tree))
7045 mode = BLKmode, *pbitsize = -1;
7046 else
7047 *pbitsize = tree_to_uhwi (size_tree);
7050 *preversep = reverse_storage_order_for_component_p (exp);
7052 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7053 and find the ultimate containing object. */
7054 while (1)
7056 switch (TREE_CODE (exp))
7058 case BIT_FIELD_REF:
7059 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7060 break;
7062 case COMPONENT_REF:
7064 tree field = TREE_OPERAND (exp, 1);
7065 tree this_offset = component_ref_field_offset (exp);
7067 /* If this field hasn't been filled in yet, don't go past it.
7068 This should only happen when folding expressions made during
7069 type construction. */
7070 if (this_offset == 0)
7071 break;
7073 offset = size_binop (PLUS_EXPR, offset, this_offset);
7074 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7076 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7078 break;
7080 case ARRAY_REF:
7081 case ARRAY_RANGE_REF:
7083 tree index = TREE_OPERAND (exp, 1);
7084 tree low_bound = array_ref_low_bound (exp);
7085 tree unit_size = array_ref_element_size (exp);
7087 /* We assume all arrays have sizes that are a multiple of a byte.
7088 First subtract the lower bound, if any, in the type of the
7089 index, then convert to sizetype and multiply by the size of
7090 the array element. */
7091 if (! integer_zerop (low_bound))
7092 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7093 index, low_bound);
7095 offset = size_binop (PLUS_EXPR, offset,
7096 size_binop (MULT_EXPR,
7097 fold_convert (sizetype, index),
7098 unit_size));
7100 break;
7102 case REALPART_EXPR:
7103 break;
7105 case IMAGPART_EXPR:
7106 bit_offset += *pbitsize;
7107 break;
7109 case VIEW_CONVERT_EXPR:
7110 break;
7112 case MEM_REF:
7113 /* Hand back the decl for MEM[&decl, off]. */
7114 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7116 tree off = TREE_OPERAND (exp, 1);
7117 if (!integer_zerop (off))
7119 offset_int boff, coff = mem_ref_offset (exp);
7120 boff = coff << LOG2_BITS_PER_UNIT;
7121 bit_offset += boff;
7123 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7125 goto done;
7127 default:
7128 goto done;
7131 /* If any reference in the chain is volatile, the effect is volatile. */
7132 if (TREE_THIS_VOLATILE (exp))
7133 *pvolatilep = 1;
7135 exp = TREE_OPERAND (exp, 0);
7137 done:
7139 /* If OFFSET is constant, see if we can return the whole thing as a
7140 constant bit position. Make sure to handle overflow during
7141 this conversion. */
7142 if (TREE_CODE (offset) == INTEGER_CST)
7144 offset_int tem = wi::sext (wi::to_offset (offset),
7145 TYPE_PRECISION (sizetype));
7146 tem <<= LOG2_BITS_PER_UNIT;
7147 tem += bit_offset;
7148 if (wi::fits_shwi_p (tem))
7150 *pbitpos = tem.to_shwi ();
7151 *poffset = offset = NULL_TREE;
7155 /* Otherwise, split it up. */
7156 if (offset)
7158 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7159 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7161 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7162 offset_int tem = bit_offset.and_not (mask);
7163 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7164 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7165 bit_offset -= tem;
7166 tem >>= LOG2_BITS_PER_UNIT;
7167 offset = size_binop (PLUS_EXPR, offset,
7168 wide_int_to_tree (sizetype, tem));
7171 *pbitpos = bit_offset.to_shwi ();
7172 *poffset = offset;
7175 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7176 if (mode == VOIDmode
7177 && blkmode_bitfield
7178 && (*pbitpos % BITS_PER_UNIT) == 0
7179 && (*pbitsize % BITS_PER_UNIT) == 0)
7180 *pmode = BLKmode;
7181 else
7182 *pmode = mode;
7184 return exp;
7187 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7189 static unsigned HOST_WIDE_INT
7190 target_align (const_tree target)
7192 /* We might have a chain of nested references with intermediate misaligning
7193 bitfields components, so need to recurse to find out. */
7195 unsigned HOST_WIDE_INT this_align, outer_align;
7197 switch (TREE_CODE (target))
7199 case BIT_FIELD_REF:
7200 return 1;
7202 case COMPONENT_REF:
7203 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7204 outer_align = target_align (TREE_OPERAND (target, 0));
7205 return MIN (this_align, outer_align);
7207 case ARRAY_REF:
7208 case ARRAY_RANGE_REF:
7209 this_align = TYPE_ALIGN (TREE_TYPE (target));
7210 outer_align = target_align (TREE_OPERAND (target, 0));
7211 return MIN (this_align, outer_align);
7213 CASE_CONVERT:
7214 case NON_LVALUE_EXPR:
7215 case VIEW_CONVERT_EXPR:
7216 this_align = TYPE_ALIGN (TREE_TYPE (target));
7217 outer_align = target_align (TREE_OPERAND (target, 0));
7218 return MAX (this_align, outer_align);
7220 default:
7221 return TYPE_ALIGN (TREE_TYPE (target));
7226 /* Given an rtx VALUE that may contain additions and multiplications, return
7227 an equivalent value that just refers to a register, memory, or constant.
7228 This is done by generating instructions to perform the arithmetic and
7229 returning a pseudo-register containing the value.
7231 The returned value may be a REG, SUBREG, MEM or constant. */
7234 force_operand (rtx value, rtx target)
7236 rtx op1, op2;
7237 /* Use subtarget as the target for operand 0 of a binary operation. */
7238 rtx subtarget = get_subtarget (target);
7239 enum rtx_code code = GET_CODE (value);
7241 /* Check for subreg applied to an expression produced by loop optimizer. */
7242 if (code == SUBREG
7243 && !REG_P (SUBREG_REG (value))
7244 && !MEM_P (SUBREG_REG (value)))
7246 value
7247 = simplify_gen_subreg (GET_MODE (value),
7248 force_reg (GET_MODE (SUBREG_REG (value)),
7249 force_operand (SUBREG_REG (value),
7250 NULL_RTX)),
7251 GET_MODE (SUBREG_REG (value)),
7252 SUBREG_BYTE (value));
7253 code = GET_CODE (value);
7256 /* Check for a PIC address load. */
7257 if ((code == PLUS || code == MINUS)
7258 && XEXP (value, 0) == pic_offset_table_rtx
7259 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7260 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7261 || GET_CODE (XEXP (value, 1)) == CONST))
7263 if (!subtarget)
7264 subtarget = gen_reg_rtx (GET_MODE (value));
7265 emit_move_insn (subtarget, value);
7266 return subtarget;
7269 if (ARITHMETIC_P (value))
7271 op2 = XEXP (value, 1);
7272 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7273 subtarget = 0;
7274 if (code == MINUS && CONST_INT_P (op2))
7276 code = PLUS;
7277 op2 = negate_rtx (GET_MODE (value), op2);
7280 /* Check for an addition with OP2 a constant integer and our first
7281 operand a PLUS of a virtual register and something else. In that
7282 case, we want to emit the sum of the virtual register and the
7283 constant first and then add the other value. This allows virtual
7284 register instantiation to simply modify the constant rather than
7285 creating another one around this addition. */
7286 if (code == PLUS && CONST_INT_P (op2)
7287 && GET_CODE (XEXP (value, 0)) == PLUS
7288 && REG_P (XEXP (XEXP (value, 0), 0))
7289 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7290 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7292 rtx temp = expand_simple_binop (GET_MODE (value), code,
7293 XEXP (XEXP (value, 0), 0), op2,
7294 subtarget, 0, OPTAB_LIB_WIDEN);
7295 return expand_simple_binop (GET_MODE (value), code, temp,
7296 force_operand (XEXP (XEXP (value,
7297 0), 1), 0),
7298 target, 0, OPTAB_LIB_WIDEN);
7301 op1 = force_operand (XEXP (value, 0), subtarget);
7302 op2 = force_operand (op2, NULL_RTX);
7303 switch (code)
7305 case MULT:
7306 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7307 case DIV:
7308 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7309 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7310 target, 1, OPTAB_LIB_WIDEN);
7311 else
7312 return expand_divmod (0,
7313 FLOAT_MODE_P (GET_MODE (value))
7314 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7315 GET_MODE (value), op1, op2, target, 0);
7316 case MOD:
7317 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7318 target, 0);
7319 case UDIV:
7320 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7321 target, 1);
7322 case UMOD:
7323 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7324 target, 1);
7325 case ASHIFTRT:
7326 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7327 target, 0, OPTAB_LIB_WIDEN);
7328 default:
7329 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7330 target, 1, OPTAB_LIB_WIDEN);
7333 if (UNARY_P (value))
7335 if (!target)
7336 target = gen_reg_rtx (GET_MODE (value));
7337 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7338 switch (code)
7340 case ZERO_EXTEND:
7341 case SIGN_EXTEND:
7342 case TRUNCATE:
7343 case FLOAT_EXTEND:
7344 case FLOAT_TRUNCATE:
7345 convert_move (target, op1, code == ZERO_EXTEND);
7346 return target;
7348 case FIX:
7349 case UNSIGNED_FIX:
7350 expand_fix (target, op1, code == UNSIGNED_FIX);
7351 return target;
7353 case FLOAT:
7354 case UNSIGNED_FLOAT:
7355 expand_float (target, op1, code == UNSIGNED_FLOAT);
7356 return target;
7358 default:
7359 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7363 #ifdef INSN_SCHEDULING
7364 /* On machines that have insn scheduling, we want all memory reference to be
7365 explicit, so we need to deal with such paradoxical SUBREGs. */
7366 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7367 value
7368 = simplify_gen_subreg (GET_MODE (value),
7369 force_reg (GET_MODE (SUBREG_REG (value)),
7370 force_operand (SUBREG_REG (value),
7371 NULL_RTX)),
7372 GET_MODE (SUBREG_REG (value)),
7373 SUBREG_BYTE (value));
7374 #endif
7376 return value;
7379 /* Subroutine of expand_expr: return nonzero iff there is no way that
7380 EXP can reference X, which is being modified. TOP_P is nonzero if this
7381 call is going to be used to determine whether we need a temporary
7382 for EXP, as opposed to a recursive call to this function.
7384 It is always safe for this routine to return zero since it merely
7385 searches for optimization opportunities. */
7388 safe_from_p (const_rtx x, tree exp, int top_p)
7390 rtx exp_rtl = 0;
7391 int i, nops;
7393 if (x == 0
7394 /* If EXP has varying size, we MUST use a target since we currently
7395 have no way of allocating temporaries of variable size
7396 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7397 So we assume here that something at a higher level has prevented a
7398 clash. This is somewhat bogus, but the best we can do. Only
7399 do this when X is BLKmode and when we are at the top level. */
7400 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7401 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7402 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7403 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7404 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7405 != INTEGER_CST)
7406 && GET_MODE (x) == BLKmode)
7407 /* If X is in the outgoing argument area, it is always safe. */
7408 || (MEM_P (x)
7409 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7410 || (GET_CODE (XEXP (x, 0)) == PLUS
7411 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7412 return 1;
7414 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7415 find the underlying pseudo. */
7416 if (GET_CODE (x) == SUBREG)
7418 x = SUBREG_REG (x);
7419 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7420 return 0;
7423 /* Now look at our tree code and possibly recurse. */
7424 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7426 case tcc_declaration:
7427 exp_rtl = DECL_RTL_IF_SET (exp);
7428 break;
7430 case tcc_constant:
7431 return 1;
7433 case tcc_exceptional:
7434 if (TREE_CODE (exp) == TREE_LIST)
7436 while (1)
7438 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7439 return 0;
7440 exp = TREE_CHAIN (exp);
7441 if (!exp)
7442 return 1;
7443 if (TREE_CODE (exp) != TREE_LIST)
7444 return safe_from_p (x, exp, 0);
7447 else if (TREE_CODE (exp) == CONSTRUCTOR)
7449 constructor_elt *ce;
7450 unsigned HOST_WIDE_INT idx;
7452 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7453 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7454 || !safe_from_p (x, ce->value, 0))
7455 return 0;
7456 return 1;
7458 else if (TREE_CODE (exp) == ERROR_MARK)
7459 return 1; /* An already-visited SAVE_EXPR? */
7460 else
7461 return 0;
7463 case tcc_statement:
7464 /* The only case we look at here is the DECL_INITIAL inside a
7465 DECL_EXPR. */
7466 return (TREE_CODE (exp) != DECL_EXPR
7467 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7468 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7469 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7471 case tcc_binary:
7472 case tcc_comparison:
7473 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7474 return 0;
7475 /* Fall through. */
7477 case tcc_unary:
7478 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7480 case tcc_expression:
7481 case tcc_reference:
7482 case tcc_vl_exp:
7483 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7484 the expression. If it is set, we conflict iff we are that rtx or
7485 both are in memory. Otherwise, we check all operands of the
7486 expression recursively. */
7488 switch (TREE_CODE (exp))
7490 case ADDR_EXPR:
7491 /* If the operand is static or we are static, we can't conflict.
7492 Likewise if we don't conflict with the operand at all. */
7493 if (staticp (TREE_OPERAND (exp, 0))
7494 || TREE_STATIC (exp)
7495 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7496 return 1;
7498 /* Otherwise, the only way this can conflict is if we are taking
7499 the address of a DECL a that address if part of X, which is
7500 very rare. */
7501 exp = TREE_OPERAND (exp, 0);
7502 if (DECL_P (exp))
7504 if (!DECL_RTL_SET_P (exp)
7505 || !MEM_P (DECL_RTL (exp)))
7506 return 0;
7507 else
7508 exp_rtl = XEXP (DECL_RTL (exp), 0);
7510 break;
7512 case MEM_REF:
7513 if (MEM_P (x)
7514 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7515 get_alias_set (exp)))
7516 return 0;
7517 break;
7519 case CALL_EXPR:
7520 /* Assume that the call will clobber all hard registers and
7521 all of memory. */
7522 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7523 || MEM_P (x))
7524 return 0;
7525 break;
7527 case WITH_CLEANUP_EXPR:
7528 case CLEANUP_POINT_EXPR:
7529 /* Lowered by gimplify.c. */
7530 gcc_unreachable ();
7532 case SAVE_EXPR:
7533 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7535 default:
7536 break;
7539 /* If we have an rtx, we do not need to scan our operands. */
7540 if (exp_rtl)
7541 break;
7543 nops = TREE_OPERAND_LENGTH (exp);
7544 for (i = 0; i < nops; i++)
7545 if (TREE_OPERAND (exp, i) != 0
7546 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7547 return 0;
7549 break;
7551 case tcc_type:
7552 /* Should never get a type here. */
7553 gcc_unreachable ();
7556 /* If we have an rtl, find any enclosed object. Then see if we conflict
7557 with it. */
7558 if (exp_rtl)
7560 if (GET_CODE (exp_rtl) == SUBREG)
7562 exp_rtl = SUBREG_REG (exp_rtl);
7563 if (REG_P (exp_rtl)
7564 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7565 return 0;
7568 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7569 are memory and they conflict. */
7570 return ! (rtx_equal_p (x, exp_rtl)
7571 || (MEM_P (x) && MEM_P (exp_rtl)
7572 && true_dependence (exp_rtl, VOIDmode, x)));
7575 /* If we reach here, it is safe. */
7576 return 1;
7580 /* Return the highest power of two that EXP is known to be a multiple of.
7581 This is used in updating alignment of MEMs in array references. */
7583 unsigned HOST_WIDE_INT
7584 highest_pow2_factor (const_tree exp)
7586 unsigned HOST_WIDE_INT ret;
7587 int trailing_zeros = tree_ctz (exp);
7588 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7589 return BIGGEST_ALIGNMENT;
7590 ret = HOST_WIDE_INT_1U << trailing_zeros;
7591 if (ret > BIGGEST_ALIGNMENT)
7592 return BIGGEST_ALIGNMENT;
7593 return ret;
7596 /* Similar, except that the alignment requirements of TARGET are
7597 taken into account. Assume it is at least as aligned as its
7598 type, unless it is a COMPONENT_REF in which case the layout of
7599 the structure gives the alignment. */
7601 static unsigned HOST_WIDE_INT
7602 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7604 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7605 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7607 return MAX (factor, talign);
7610 /* Convert the tree comparison code TCODE to the rtl one where the
7611 signedness is UNSIGNEDP. */
7613 static enum rtx_code
7614 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7616 enum rtx_code code;
7617 switch (tcode)
7619 case EQ_EXPR:
7620 code = EQ;
7621 break;
7622 case NE_EXPR:
7623 code = NE;
7624 break;
7625 case LT_EXPR:
7626 code = unsignedp ? LTU : LT;
7627 break;
7628 case LE_EXPR:
7629 code = unsignedp ? LEU : LE;
7630 break;
7631 case GT_EXPR:
7632 code = unsignedp ? GTU : GT;
7633 break;
7634 case GE_EXPR:
7635 code = unsignedp ? GEU : GE;
7636 break;
7637 case UNORDERED_EXPR:
7638 code = UNORDERED;
7639 break;
7640 case ORDERED_EXPR:
7641 code = ORDERED;
7642 break;
7643 case UNLT_EXPR:
7644 code = UNLT;
7645 break;
7646 case UNLE_EXPR:
7647 code = UNLE;
7648 break;
7649 case UNGT_EXPR:
7650 code = UNGT;
7651 break;
7652 case UNGE_EXPR:
7653 code = UNGE;
7654 break;
7655 case UNEQ_EXPR:
7656 code = UNEQ;
7657 break;
7658 case LTGT_EXPR:
7659 code = LTGT;
7660 break;
7662 default:
7663 gcc_unreachable ();
7665 return code;
7668 /* Subroutine of expand_expr. Expand the two operands of a binary
7669 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7670 The value may be stored in TARGET if TARGET is nonzero. The
7671 MODIFIER argument is as documented by expand_expr. */
7673 void
7674 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7675 enum expand_modifier modifier)
7677 if (! safe_from_p (target, exp1, 1))
7678 target = 0;
7679 if (operand_equal_p (exp0, exp1, 0))
7681 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7682 *op1 = copy_rtx (*op0);
7684 else
7686 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7687 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7692 /* Return a MEM that contains constant EXP. DEFER is as for
7693 output_constant_def and MODIFIER is as for expand_expr. */
7695 static rtx
7696 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7698 rtx mem;
7700 mem = output_constant_def (exp, defer);
7701 if (modifier != EXPAND_INITIALIZER)
7702 mem = use_anchored_address (mem);
7703 return mem;
7706 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7707 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7709 static rtx
7710 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7711 enum expand_modifier modifier, addr_space_t as)
7713 rtx result, subtarget;
7714 tree inner, offset;
7715 HOST_WIDE_INT bitsize, bitpos;
7716 int unsignedp, reversep, volatilep = 0;
7717 machine_mode mode1;
7719 /* If we are taking the address of a constant and are at the top level,
7720 we have to use output_constant_def since we can't call force_const_mem
7721 at top level. */
7722 /* ??? This should be considered a front-end bug. We should not be
7723 generating ADDR_EXPR of something that isn't an LVALUE. The only
7724 exception here is STRING_CST. */
7725 if (CONSTANT_CLASS_P (exp))
7727 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7728 if (modifier < EXPAND_SUM)
7729 result = force_operand (result, target);
7730 return result;
7733 /* Everything must be something allowed by is_gimple_addressable. */
7734 switch (TREE_CODE (exp))
7736 case INDIRECT_REF:
7737 /* This case will happen via recursion for &a->b. */
7738 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7740 case MEM_REF:
7742 tree tem = TREE_OPERAND (exp, 0);
7743 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7744 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7745 return expand_expr (tem, target, tmode, modifier);
7748 case CONST_DECL:
7749 /* Expand the initializer like constants above. */
7750 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7751 0, modifier), 0);
7752 if (modifier < EXPAND_SUM)
7753 result = force_operand (result, target);
7754 return result;
7756 case REALPART_EXPR:
7757 /* The real part of the complex number is always first, therefore
7758 the address is the same as the address of the parent object. */
7759 offset = 0;
7760 bitpos = 0;
7761 inner = TREE_OPERAND (exp, 0);
7762 break;
7764 case IMAGPART_EXPR:
7765 /* The imaginary part of the complex number is always second.
7766 The expression is therefore always offset by the size of the
7767 scalar type. */
7768 offset = 0;
7769 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7770 inner = TREE_OPERAND (exp, 0);
7771 break;
7773 case COMPOUND_LITERAL_EXPR:
7774 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7775 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7776 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7777 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7778 the initializers aren't gimplified. */
7779 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7780 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7781 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7782 target, tmode, modifier, as);
7783 /* FALLTHRU */
7784 default:
7785 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7786 expand_expr, as that can have various side effects; LABEL_DECLs for
7787 example, may not have their DECL_RTL set yet. Expand the rtl of
7788 CONSTRUCTORs too, which should yield a memory reference for the
7789 constructor's contents. Assume language specific tree nodes can
7790 be expanded in some interesting way. */
7791 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7792 if (DECL_P (exp)
7793 || TREE_CODE (exp) == CONSTRUCTOR
7794 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7796 result = expand_expr (exp, target, tmode,
7797 modifier == EXPAND_INITIALIZER
7798 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7800 /* If the DECL isn't in memory, then the DECL wasn't properly
7801 marked TREE_ADDRESSABLE, which will be either a front-end
7802 or a tree optimizer bug. */
7804 gcc_assert (MEM_P (result));
7805 result = XEXP (result, 0);
7807 /* ??? Is this needed anymore? */
7808 if (DECL_P (exp))
7809 TREE_USED (exp) = 1;
7811 if (modifier != EXPAND_INITIALIZER
7812 && modifier != EXPAND_CONST_ADDRESS
7813 && modifier != EXPAND_SUM)
7814 result = force_operand (result, target);
7815 return result;
7818 /* Pass FALSE as the last argument to get_inner_reference although
7819 we are expanding to RTL. The rationale is that we know how to
7820 handle "aligning nodes" here: we can just bypass them because
7821 they won't change the final object whose address will be returned
7822 (they actually exist only for that purpose). */
7823 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7824 &unsignedp, &reversep, &volatilep);
7825 break;
7828 /* We must have made progress. */
7829 gcc_assert (inner != exp);
7831 subtarget = offset || bitpos ? NULL_RTX : target;
7832 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7833 inner alignment, force the inner to be sufficiently aligned. */
7834 if (CONSTANT_CLASS_P (inner)
7835 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7837 inner = copy_node (inner);
7838 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7839 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7840 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7842 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7844 if (offset)
7846 rtx tmp;
7848 if (modifier != EXPAND_NORMAL)
7849 result = force_operand (result, NULL);
7850 tmp = expand_expr (offset, NULL_RTX, tmode,
7851 modifier == EXPAND_INITIALIZER
7852 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7854 /* expand_expr is allowed to return an object in a mode other
7855 than TMODE. If it did, we need to convert. */
7856 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7857 tmp = convert_modes (tmode, GET_MODE (tmp),
7858 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7859 result = convert_memory_address_addr_space (tmode, result, as);
7860 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7862 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7863 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7864 else
7866 subtarget = bitpos ? NULL_RTX : target;
7867 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7868 1, OPTAB_LIB_WIDEN);
7872 if (bitpos)
7874 /* Someone beforehand should have rejected taking the address
7875 of such an object. */
7876 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7878 result = convert_memory_address_addr_space (tmode, result, as);
7879 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7880 if (modifier < EXPAND_SUM)
7881 result = force_operand (result, target);
7884 return result;
7887 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7888 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7890 static rtx
7891 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7892 enum expand_modifier modifier)
7894 addr_space_t as = ADDR_SPACE_GENERIC;
7895 scalar_int_mode address_mode = Pmode;
7896 scalar_int_mode pointer_mode = ptr_mode;
7897 machine_mode rmode;
7898 rtx result;
7900 /* Target mode of VOIDmode says "whatever's natural". */
7901 if (tmode == VOIDmode)
7902 tmode = TYPE_MODE (TREE_TYPE (exp));
7904 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7906 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7907 address_mode = targetm.addr_space.address_mode (as);
7908 pointer_mode = targetm.addr_space.pointer_mode (as);
7911 /* We can get called with some Weird Things if the user does silliness
7912 like "(short) &a". In that case, convert_memory_address won't do
7913 the right thing, so ignore the given target mode. */
7914 scalar_int_mode new_tmode = (tmode == pointer_mode
7915 ? pointer_mode
7916 : address_mode);
7918 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7919 new_tmode, modifier, as);
7921 /* Despite expand_expr claims concerning ignoring TMODE when not
7922 strictly convenient, stuff breaks if we don't honor it. Note
7923 that combined with the above, we only do this for pointer modes. */
7924 rmode = GET_MODE (result);
7925 if (rmode == VOIDmode)
7926 rmode = new_tmode;
7927 if (rmode != new_tmode)
7928 result = convert_memory_address_addr_space (new_tmode, result, as);
7930 return result;
7933 /* Generate code for computing CONSTRUCTOR EXP.
7934 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7935 is TRUE, instead of creating a temporary variable in memory
7936 NULL is returned and the caller needs to handle it differently. */
7938 static rtx
7939 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7940 bool avoid_temp_mem)
7942 tree type = TREE_TYPE (exp);
7943 machine_mode mode = TYPE_MODE (type);
7945 /* Try to avoid creating a temporary at all. This is possible
7946 if all of the initializer is zero.
7947 FIXME: try to handle all [0..255] initializers we can handle
7948 with memset. */
7949 if (TREE_STATIC (exp)
7950 && !TREE_ADDRESSABLE (exp)
7951 && target != 0 && mode == BLKmode
7952 && all_zeros_p (exp))
7954 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7955 return target;
7958 /* All elts simple constants => refer to a constant in memory. But
7959 if this is a non-BLKmode mode, let it store a field at a time
7960 since that should make a CONST_INT, CONST_WIDE_INT or
7961 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7962 use, it is best to store directly into the target unless the type
7963 is large enough that memcpy will be used. If we are making an
7964 initializer and all operands are constant, put it in memory as
7965 well.
7967 FIXME: Avoid trying to fill vector constructors piece-meal.
7968 Output them with output_constant_def below unless we're sure
7969 they're zeros. This should go away when vector initializers
7970 are treated like VECTOR_CST instead of arrays. */
7971 if ((TREE_STATIC (exp)
7972 && ((mode == BLKmode
7973 && ! (target != 0 && safe_from_p (target, exp, 1)))
7974 || TREE_ADDRESSABLE (exp)
7975 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7976 && (! can_move_by_pieces
7977 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7978 TYPE_ALIGN (type)))
7979 && ! mostly_zeros_p (exp))))
7980 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7981 && TREE_CONSTANT (exp)))
7983 rtx constructor;
7985 if (avoid_temp_mem)
7986 return NULL_RTX;
7988 constructor = expand_expr_constant (exp, 1, modifier);
7990 if (modifier != EXPAND_CONST_ADDRESS
7991 && modifier != EXPAND_INITIALIZER
7992 && modifier != EXPAND_SUM)
7993 constructor = validize_mem (constructor);
7995 return constructor;
7998 /* Handle calls that pass values in multiple non-contiguous
7999 locations. The Irix 6 ABI has examples of this. */
8000 if (target == 0 || ! safe_from_p (target, exp, 1)
8001 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8003 if (avoid_temp_mem)
8004 return NULL_RTX;
8006 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8009 store_constructor (exp, target, 0, int_expr_size (exp), false);
8010 return target;
8014 /* expand_expr: generate code for computing expression EXP.
8015 An rtx for the computed value is returned. The value is never null.
8016 In the case of a void EXP, const0_rtx is returned.
8018 The value may be stored in TARGET if TARGET is nonzero.
8019 TARGET is just a suggestion; callers must assume that
8020 the rtx returned may not be the same as TARGET.
8022 If TARGET is CONST0_RTX, it means that the value will be ignored.
8024 If TMODE is not VOIDmode, it suggests generating the
8025 result in mode TMODE. But this is done only when convenient.
8026 Otherwise, TMODE is ignored and the value generated in its natural mode.
8027 TMODE is just a suggestion; callers must assume that
8028 the rtx returned may not have mode TMODE.
8030 Note that TARGET may have neither TMODE nor MODE. In that case, it
8031 probably will not be used.
8033 If MODIFIER is EXPAND_SUM then when EXP is an addition
8034 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8035 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8036 products as above, or REG or MEM, or constant.
8037 Ordinarily in such cases we would output mul or add instructions
8038 and then return a pseudo reg containing the sum.
8040 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8041 it also marks a label as absolutely required (it can't be dead).
8042 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8043 This is used for outputting expressions used in initializers.
8045 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8046 with a constant address even if that address is not normally legitimate.
8047 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8049 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8050 a call parameter. Such targets require special care as we haven't yet
8051 marked TARGET so that it's safe from being trashed by libcalls. We
8052 don't want to use TARGET for anything but the final result;
8053 Intermediate values must go elsewhere. Additionally, calls to
8054 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8056 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8057 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8058 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8059 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8060 recursively.
8062 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8063 In this case, we don't adjust a returned MEM rtx that wouldn't be
8064 sufficiently aligned for its mode; instead, it's up to the caller
8065 to deal with it afterwards. This is used to make sure that unaligned
8066 base objects for which out-of-bounds accesses are supported, for
8067 example record types with trailing arrays, aren't realigned behind
8068 the back of the caller.
8069 The normal operating mode is to pass FALSE for this parameter. */
8072 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8073 enum expand_modifier modifier, rtx *alt_rtl,
8074 bool inner_reference_p)
8076 rtx ret;
8078 /* Handle ERROR_MARK before anybody tries to access its type. */
8079 if (TREE_CODE (exp) == ERROR_MARK
8080 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8082 ret = CONST0_RTX (tmode);
8083 return ret ? ret : const0_rtx;
8086 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8087 inner_reference_p);
8088 return ret;
8091 /* Try to expand the conditional expression which is represented by
8092 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8093 return the rtl reg which represents the result. Otherwise return
8094 NULL_RTX. */
8096 static rtx
8097 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8098 tree treeop1 ATTRIBUTE_UNUSED,
8099 tree treeop2 ATTRIBUTE_UNUSED)
8101 rtx insn;
8102 rtx op00, op01, op1, op2;
8103 enum rtx_code comparison_code;
8104 machine_mode comparison_mode;
8105 gimple *srcstmt;
8106 rtx temp;
8107 tree type = TREE_TYPE (treeop1);
8108 int unsignedp = TYPE_UNSIGNED (type);
8109 machine_mode mode = TYPE_MODE (type);
8110 machine_mode orig_mode = mode;
8111 static bool expanding_cond_expr_using_cmove = false;
8113 /* Conditional move expansion can end up TERing two operands which,
8114 when recursively hitting conditional expressions can result in
8115 exponential behavior if the cmove expansion ultimatively fails.
8116 It's hardly profitable to TER a cmove into a cmove so avoid doing
8117 that by failing early if we end up recursing. */
8118 if (expanding_cond_expr_using_cmove)
8119 return NULL_RTX;
8121 /* If we cannot do a conditional move on the mode, try doing it
8122 with the promoted mode. */
8123 if (!can_conditionally_move_p (mode))
8125 mode = promote_mode (type, mode, &unsignedp);
8126 if (!can_conditionally_move_p (mode))
8127 return NULL_RTX;
8128 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8130 else
8131 temp = assign_temp (type, 0, 1);
8133 expanding_cond_expr_using_cmove = true;
8134 start_sequence ();
8135 expand_operands (treeop1, treeop2,
8136 temp, &op1, &op2, EXPAND_NORMAL);
8138 if (TREE_CODE (treeop0) == SSA_NAME
8139 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8141 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8142 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8143 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8144 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8145 comparison_mode = TYPE_MODE (type);
8146 unsignedp = TYPE_UNSIGNED (type);
8147 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8149 else if (COMPARISON_CLASS_P (treeop0))
8151 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8152 enum tree_code cmpcode = TREE_CODE (treeop0);
8153 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8154 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8155 unsignedp = TYPE_UNSIGNED (type);
8156 comparison_mode = TYPE_MODE (type);
8157 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8159 else
8161 op00 = expand_normal (treeop0);
8162 op01 = const0_rtx;
8163 comparison_code = NE;
8164 comparison_mode = GET_MODE (op00);
8165 if (comparison_mode == VOIDmode)
8166 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8168 expanding_cond_expr_using_cmove = false;
8170 if (GET_MODE (op1) != mode)
8171 op1 = gen_lowpart (mode, op1);
8173 if (GET_MODE (op2) != mode)
8174 op2 = gen_lowpart (mode, op2);
8176 /* Try to emit the conditional move. */
8177 insn = emit_conditional_move (temp, comparison_code,
8178 op00, op01, comparison_mode,
8179 op1, op2, mode,
8180 unsignedp);
8182 /* If we could do the conditional move, emit the sequence,
8183 and return. */
8184 if (insn)
8186 rtx_insn *seq = get_insns ();
8187 end_sequence ();
8188 emit_insn (seq);
8189 return convert_modes (orig_mode, mode, temp, 0);
8192 /* Otherwise discard the sequence and fall back to code with
8193 branches. */
8194 end_sequence ();
8195 return NULL_RTX;
8199 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8200 enum expand_modifier modifier)
8202 rtx op0, op1, op2, temp;
8203 rtx_code_label *lab;
8204 tree type;
8205 int unsignedp;
8206 machine_mode mode;
8207 scalar_int_mode int_mode;
8208 enum tree_code code = ops->code;
8209 optab this_optab;
8210 rtx subtarget, original_target;
8211 int ignore;
8212 bool reduce_bit_field;
8213 location_t loc = ops->location;
8214 tree treeop0, treeop1, treeop2;
8215 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8216 ? reduce_to_bit_field_precision ((expr), \
8217 target, \
8218 type) \
8219 : (expr))
8221 type = ops->type;
8222 mode = TYPE_MODE (type);
8223 unsignedp = TYPE_UNSIGNED (type);
8225 treeop0 = ops->op0;
8226 treeop1 = ops->op1;
8227 treeop2 = ops->op2;
8229 /* We should be called only on simple (binary or unary) expressions,
8230 exactly those that are valid in gimple expressions that aren't
8231 GIMPLE_SINGLE_RHS (or invalid). */
8232 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8233 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8234 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8236 ignore = (target == const0_rtx
8237 || ((CONVERT_EXPR_CODE_P (code)
8238 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8239 && TREE_CODE (type) == VOID_TYPE));
8241 /* We should be called only if we need the result. */
8242 gcc_assert (!ignore);
8244 /* An operation in what may be a bit-field type needs the
8245 result to be reduced to the precision of the bit-field type,
8246 which is narrower than that of the type's mode. */
8247 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8248 && !type_has_mode_precision_p (type));
8250 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8251 target = 0;
8253 /* Use subtarget as the target for operand 0 of a binary operation. */
8254 subtarget = get_subtarget (target);
8255 original_target = target;
8257 switch (code)
8259 case NON_LVALUE_EXPR:
8260 case PAREN_EXPR:
8261 CASE_CONVERT:
8262 if (treeop0 == error_mark_node)
8263 return const0_rtx;
8265 if (TREE_CODE (type) == UNION_TYPE)
8267 tree valtype = TREE_TYPE (treeop0);
8269 /* If both input and output are BLKmode, this conversion isn't doing
8270 anything except possibly changing memory attribute. */
8271 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8273 rtx result = expand_expr (treeop0, target, tmode,
8274 modifier);
8276 result = copy_rtx (result);
8277 set_mem_attributes (result, type, 0);
8278 return result;
8281 if (target == 0)
8283 if (TYPE_MODE (type) != BLKmode)
8284 target = gen_reg_rtx (TYPE_MODE (type));
8285 else
8286 target = assign_temp (type, 1, 1);
8289 if (MEM_P (target))
8290 /* Store data into beginning of memory target. */
8291 store_expr (treeop0,
8292 adjust_address (target, TYPE_MODE (valtype), 0),
8293 modifier == EXPAND_STACK_PARM,
8294 false, TYPE_REVERSE_STORAGE_ORDER (type));
8296 else
8298 gcc_assert (REG_P (target)
8299 && !TYPE_REVERSE_STORAGE_ORDER (type));
8301 /* Store this field into a union of the proper type. */
8302 store_field (target,
8303 MIN ((int_size_in_bytes (TREE_TYPE
8304 (treeop0))
8305 * BITS_PER_UNIT),
8306 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8307 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8308 false, false);
8311 /* Return the entire union. */
8312 return target;
8315 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8317 op0 = expand_expr (treeop0, target, VOIDmode,
8318 modifier);
8320 /* If the signedness of the conversion differs and OP0 is
8321 a promoted SUBREG, clear that indication since we now
8322 have to do the proper extension. */
8323 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8324 && GET_CODE (op0) == SUBREG)
8325 SUBREG_PROMOTED_VAR_P (op0) = 0;
8327 return REDUCE_BIT_FIELD (op0);
8330 op0 = expand_expr (treeop0, NULL_RTX, mode,
8331 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8332 if (GET_MODE (op0) == mode)
8335 /* If OP0 is a constant, just convert it into the proper mode. */
8336 else if (CONSTANT_P (op0))
8338 tree inner_type = TREE_TYPE (treeop0);
8339 machine_mode inner_mode = GET_MODE (op0);
8341 if (inner_mode == VOIDmode)
8342 inner_mode = TYPE_MODE (inner_type);
8344 if (modifier == EXPAND_INITIALIZER)
8345 op0 = lowpart_subreg (mode, op0, inner_mode);
8346 else
8347 op0= convert_modes (mode, inner_mode, op0,
8348 TYPE_UNSIGNED (inner_type));
8351 else if (modifier == EXPAND_INITIALIZER)
8352 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8353 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8355 else if (target == 0)
8356 op0 = convert_to_mode (mode, op0,
8357 TYPE_UNSIGNED (TREE_TYPE
8358 (treeop0)));
8359 else
8361 convert_move (target, op0,
8362 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8363 op0 = target;
8366 return REDUCE_BIT_FIELD (op0);
8368 case ADDR_SPACE_CONVERT_EXPR:
8370 tree treeop0_type = TREE_TYPE (treeop0);
8372 gcc_assert (POINTER_TYPE_P (type));
8373 gcc_assert (POINTER_TYPE_P (treeop0_type));
8375 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8376 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8378 /* Conversions between pointers to the same address space should
8379 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8380 gcc_assert (as_to != as_from);
8382 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8384 /* Ask target code to handle conversion between pointers
8385 to overlapping address spaces. */
8386 if (targetm.addr_space.subset_p (as_to, as_from)
8387 || targetm.addr_space.subset_p (as_from, as_to))
8389 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8391 else
8393 /* For disjoint address spaces, converting anything but a null
8394 pointer invokes undefined behavior. We truncate or extend the
8395 value as if we'd converted via integers, which handles 0 as
8396 required, and all others as the programmer likely expects. */
8397 #ifndef POINTERS_EXTEND_UNSIGNED
8398 const int POINTERS_EXTEND_UNSIGNED = 1;
8399 #endif
8400 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8401 op0, POINTERS_EXTEND_UNSIGNED);
8403 gcc_assert (op0);
8404 return op0;
8407 case POINTER_PLUS_EXPR:
8408 /* Even though the sizetype mode and the pointer's mode can be different
8409 expand is able to handle this correctly and get the correct result out
8410 of the PLUS_EXPR code. */
8411 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8412 if sizetype precision is smaller than pointer precision. */
8413 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8414 treeop1 = fold_convert_loc (loc, type,
8415 fold_convert_loc (loc, ssizetype,
8416 treeop1));
8417 /* If sizetype precision is larger than pointer precision, truncate the
8418 offset to have matching modes. */
8419 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8420 treeop1 = fold_convert_loc (loc, type, treeop1);
8421 /* FALLTHRU */
8423 case PLUS_EXPR:
8424 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8425 something else, make sure we add the register to the constant and
8426 then to the other thing. This case can occur during strength
8427 reduction and doing it this way will produce better code if the
8428 frame pointer or argument pointer is eliminated.
8430 fold-const.c will ensure that the constant is always in the inner
8431 PLUS_EXPR, so the only case we need to do anything about is if
8432 sp, ap, or fp is our second argument, in which case we must swap
8433 the innermost first argument and our second argument. */
8435 if (TREE_CODE (treeop0) == PLUS_EXPR
8436 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8437 && VAR_P (treeop1)
8438 && (DECL_RTL (treeop1) == frame_pointer_rtx
8439 || DECL_RTL (treeop1) == stack_pointer_rtx
8440 || DECL_RTL (treeop1) == arg_pointer_rtx))
8442 gcc_unreachable ();
8445 /* If the result is to be ptr_mode and we are adding an integer to
8446 something, we might be forming a constant. So try to use
8447 plus_constant. If it produces a sum and we can't accept it,
8448 use force_operand. This allows P = &ARR[const] to generate
8449 efficient code on machines where a SYMBOL_REF is not a valid
8450 address.
8452 If this is an EXPAND_SUM call, always return the sum. */
8453 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8454 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8456 if (modifier == EXPAND_STACK_PARM)
8457 target = 0;
8458 if (TREE_CODE (treeop0) == INTEGER_CST
8459 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8460 && TREE_CONSTANT (treeop1))
8462 rtx constant_part;
8463 HOST_WIDE_INT wc;
8464 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8466 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8467 EXPAND_SUM);
8468 /* Use wi::shwi to ensure that the constant is
8469 truncated according to the mode of OP1, then sign extended
8470 to a HOST_WIDE_INT. Using the constant directly can result
8471 in non-canonical RTL in a 64x32 cross compile. */
8472 wc = TREE_INT_CST_LOW (treeop0);
8473 constant_part =
8474 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8475 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8476 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8477 op1 = force_operand (op1, target);
8478 return REDUCE_BIT_FIELD (op1);
8481 else if (TREE_CODE (treeop1) == INTEGER_CST
8482 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8483 && TREE_CONSTANT (treeop0))
8485 rtx constant_part;
8486 HOST_WIDE_INT wc;
8487 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8489 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8490 (modifier == EXPAND_INITIALIZER
8491 ? EXPAND_INITIALIZER : EXPAND_SUM));
8492 if (! CONSTANT_P (op0))
8494 op1 = expand_expr (treeop1, NULL_RTX,
8495 VOIDmode, modifier);
8496 /* Return a PLUS if modifier says it's OK. */
8497 if (modifier == EXPAND_SUM
8498 || modifier == EXPAND_INITIALIZER)
8499 return simplify_gen_binary (PLUS, mode, op0, op1);
8500 goto binop2;
8502 /* Use wi::shwi to ensure that the constant is
8503 truncated according to the mode of OP1, then sign extended
8504 to a HOST_WIDE_INT. Using the constant directly can result
8505 in non-canonical RTL in a 64x32 cross compile. */
8506 wc = TREE_INT_CST_LOW (treeop1);
8507 constant_part
8508 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8509 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8510 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8511 op0 = force_operand (op0, target);
8512 return REDUCE_BIT_FIELD (op0);
8516 /* Use TER to expand pointer addition of a negated value
8517 as pointer subtraction. */
8518 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8519 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8520 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8521 && TREE_CODE (treeop1) == SSA_NAME
8522 && TYPE_MODE (TREE_TYPE (treeop0))
8523 == TYPE_MODE (TREE_TYPE (treeop1)))
8525 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8526 if (def)
8528 treeop1 = gimple_assign_rhs1 (def);
8529 code = MINUS_EXPR;
8530 goto do_minus;
8534 /* No sense saving up arithmetic to be done
8535 if it's all in the wrong mode to form part of an address.
8536 And force_operand won't know whether to sign-extend or
8537 zero-extend. */
8538 if (modifier != EXPAND_INITIALIZER
8539 && (modifier != EXPAND_SUM || mode != ptr_mode))
8541 expand_operands (treeop0, treeop1,
8542 subtarget, &op0, &op1, modifier);
8543 if (op0 == const0_rtx)
8544 return op1;
8545 if (op1 == const0_rtx)
8546 return op0;
8547 goto binop2;
8550 expand_operands (treeop0, treeop1,
8551 subtarget, &op0, &op1, modifier);
8552 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8554 case MINUS_EXPR:
8555 do_minus:
8556 /* For initializers, we are allowed to return a MINUS of two
8557 symbolic constants. Here we handle all cases when both operands
8558 are constant. */
8559 /* Handle difference of two symbolic constants,
8560 for the sake of an initializer. */
8561 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8562 && really_constant_p (treeop0)
8563 && really_constant_p (treeop1))
8565 expand_operands (treeop0, treeop1,
8566 NULL_RTX, &op0, &op1, modifier);
8568 /* If the last operand is a CONST_INT, use plus_constant of
8569 the negated constant. Else make the MINUS. */
8570 if (CONST_INT_P (op1))
8571 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8572 -INTVAL (op1)));
8573 else
8574 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8577 /* No sense saving up arithmetic to be done
8578 if it's all in the wrong mode to form part of an address.
8579 And force_operand won't know whether to sign-extend or
8580 zero-extend. */
8581 if (modifier != EXPAND_INITIALIZER
8582 && (modifier != EXPAND_SUM || mode != ptr_mode))
8583 goto binop;
8585 expand_operands (treeop0, treeop1,
8586 subtarget, &op0, &op1, modifier);
8588 /* Convert A - const to A + (-const). */
8589 if (CONST_INT_P (op1))
8591 op1 = negate_rtx (mode, op1);
8592 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8595 goto binop2;
8597 case WIDEN_MULT_PLUS_EXPR:
8598 case WIDEN_MULT_MINUS_EXPR:
8599 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8600 op2 = expand_normal (treeop2);
8601 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8602 target, unsignedp);
8603 return target;
8605 case WIDEN_MULT_EXPR:
8606 /* If first operand is constant, swap them.
8607 Thus the following special case checks need only
8608 check the second operand. */
8609 if (TREE_CODE (treeop0) == INTEGER_CST)
8610 std::swap (treeop0, treeop1);
8612 /* First, check if we have a multiplication of one signed and one
8613 unsigned operand. */
8614 if (TREE_CODE (treeop1) != INTEGER_CST
8615 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8616 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8618 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8619 this_optab = usmul_widen_optab;
8620 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8621 != CODE_FOR_nothing)
8623 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8624 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8625 EXPAND_NORMAL);
8626 else
8627 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8628 EXPAND_NORMAL);
8629 /* op0 and op1 might still be constant, despite the above
8630 != INTEGER_CST check. Handle it. */
8631 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8633 op0 = convert_modes (innermode, mode, op0, true);
8634 op1 = convert_modes (innermode, mode, op1, false);
8635 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8636 target, unsignedp));
8638 goto binop3;
8641 /* Check for a multiplication with matching signedness. */
8642 else if ((TREE_CODE (treeop1) == INTEGER_CST
8643 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8644 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8645 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8647 tree op0type = TREE_TYPE (treeop0);
8648 machine_mode innermode = TYPE_MODE (op0type);
8649 bool zextend_p = TYPE_UNSIGNED (op0type);
8650 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8651 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8653 if (TREE_CODE (treeop0) != INTEGER_CST)
8655 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8656 != CODE_FOR_nothing)
8658 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8659 EXPAND_NORMAL);
8660 /* op0 and op1 might still be constant, despite the above
8661 != INTEGER_CST check. Handle it. */
8662 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8664 widen_mult_const:
8665 op0 = convert_modes (innermode, mode, op0, zextend_p);
8667 = convert_modes (innermode, mode, op1,
8668 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8669 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8670 target,
8671 unsignedp));
8673 temp = expand_widening_mult (mode, op0, op1, target,
8674 unsignedp, this_optab);
8675 return REDUCE_BIT_FIELD (temp);
8677 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8678 != CODE_FOR_nothing
8679 && innermode == word_mode)
8681 rtx htem, hipart;
8682 op0 = expand_normal (treeop0);
8683 if (TREE_CODE (treeop1) == INTEGER_CST)
8684 op1 = convert_modes (word_mode, mode,
8685 expand_normal (treeop1),
8686 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8687 else
8688 op1 = expand_normal (treeop1);
8689 /* op0 and op1 might still be constant, despite the above
8690 != INTEGER_CST check. Handle it. */
8691 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8692 goto widen_mult_const;
8693 temp = expand_binop (mode, other_optab, op0, op1, target,
8694 unsignedp, OPTAB_LIB_WIDEN);
8695 hipart = gen_highpart (word_mode, temp);
8696 htem = expand_mult_highpart_adjust (word_mode, hipart,
8697 op0, op1, hipart,
8698 zextend_p);
8699 if (htem != hipart)
8700 emit_move_insn (hipart, htem);
8701 return REDUCE_BIT_FIELD (temp);
8705 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8706 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8707 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8708 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8710 case FMA_EXPR:
8712 optab opt = fma_optab;
8713 gimple *def0, *def2;
8715 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8716 call. */
8717 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8719 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8720 tree call_expr;
8722 gcc_assert (fn != NULL_TREE);
8723 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8724 return expand_builtin (call_expr, target, subtarget, mode, false);
8727 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8728 /* The multiplication is commutative - look at its 2nd operand
8729 if the first isn't fed by a negate. */
8730 if (!def0)
8732 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8733 /* Swap operands if the 2nd operand is fed by a negate. */
8734 if (def0)
8735 std::swap (treeop0, treeop1);
8737 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8739 op0 = op2 = NULL;
8741 if (def0 && def2
8742 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8744 opt = fnms_optab;
8745 op0 = expand_normal (gimple_assign_rhs1 (def0));
8746 op2 = expand_normal (gimple_assign_rhs1 (def2));
8748 else if (def0
8749 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8751 opt = fnma_optab;
8752 op0 = expand_normal (gimple_assign_rhs1 (def0));
8754 else if (def2
8755 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8757 opt = fms_optab;
8758 op2 = expand_normal (gimple_assign_rhs1 (def2));
8761 if (op0 == NULL)
8762 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8763 if (op2 == NULL)
8764 op2 = expand_normal (treeop2);
8765 op1 = expand_normal (treeop1);
8767 return expand_ternary_op (TYPE_MODE (type), opt,
8768 op0, op1, op2, target, 0);
8771 case MULT_EXPR:
8772 /* If this is a fixed-point operation, then we cannot use the code
8773 below because "expand_mult" doesn't support sat/no-sat fixed-point
8774 multiplications. */
8775 if (ALL_FIXED_POINT_MODE_P (mode))
8776 goto binop;
8778 /* If first operand is constant, swap them.
8779 Thus the following special case checks need only
8780 check the second operand. */
8781 if (TREE_CODE (treeop0) == INTEGER_CST)
8782 std::swap (treeop0, treeop1);
8784 /* Attempt to return something suitable for generating an
8785 indexed address, for machines that support that. */
8787 if (modifier == EXPAND_SUM && mode == ptr_mode
8788 && tree_fits_shwi_p (treeop1))
8790 tree exp1 = treeop1;
8792 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8793 EXPAND_SUM);
8795 if (!REG_P (op0))
8796 op0 = force_operand (op0, NULL_RTX);
8797 if (!REG_P (op0))
8798 op0 = copy_to_mode_reg (mode, op0);
8800 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8801 gen_int_mode (tree_to_shwi (exp1),
8802 TYPE_MODE (TREE_TYPE (exp1)))));
8805 if (modifier == EXPAND_STACK_PARM)
8806 target = 0;
8808 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8809 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8811 case TRUNC_MOD_EXPR:
8812 case FLOOR_MOD_EXPR:
8813 case CEIL_MOD_EXPR:
8814 case ROUND_MOD_EXPR:
8816 case TRUNC_DIV_EXPR:
8817 case FLOOR_DIV_EXPR:
8818 case CEIL_DIV_EXPR:
8819 case ROUND_DIV_EXPR:
8820 case EXACT_DIV_EXPR:
8822 /* If this is a fixed-point operation, then we cannot use the code
8823 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8824 divisions. */
8825 if (ALL_FIXED_POINT_MODE_P (mode))
8826 goto binop;
8828 if (modifier == EXPAND_STACK_PARM)
8829 target = 0;
8830 /* Possible optimization: compute the dividend with EXPAND_SUM
8831 then if the divisor is constant can optimize the case
8832 where some terms of the dividend have coeffs divisible by it. */
8833 expand_operands (treeop0, treeop1,
8834 subtarget, &op0, &op1, EXPAND_NORMAL);
8835 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8836 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8837 if (SCALAR_INT_MODE_P (mode)
8838 && optimize >= 2
8839 && get_range_pos_neg (treeop0) == 1
8840 && get_range_pos_neg (treeop1) == 1)
8842 /* If both arguments are known to be positive when interpreted
8843 as signed, we can expand it as both signed and unsigned
8844 division or modulo. Choose the cheaper sequence in that case. */
8845 bool speed_p = optimize_insn_for_speed_p ();
8846 do_pending_stack_adjust ();
8847 start_sequence ();
8848 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8849 rtx_insn *uns_insns = get_insns ();
8850 end_sequence ();
8851 start_sequence ();
8852 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8853 rtx_insn *sgn_insns = get_insns ();
8854 end_sequence ();
8855 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8856 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8858 /* If costs are the same then use as tie breaker the other
8859 other factor. */
8860 if (uns_cost == sgn_cost)
8862 uns_cost = seq_cost (uns_insns, !speed_p);
8863 sgn_cost = seq_cost (sgn_insns, !speed_p);
8866 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8868 emit_insn (uns_insns);
8869 return uns_ret;
8871 emit_insn (sgn_insns);
8872 return sgn_ret;
8874 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8876 case RDIV_EXPR:
8877 goto binop;
8879 case MULT_HIGHPART_EXPR:
8880 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8881 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8882 gcc_assert (temp);
8883 return temp;
8885 case FIXED_CONVERT_EXPR:
8886 op0 = expand_normal (treeop0);
8887 if (target == 0 || modifier == EXPAND_STACK_PARM)
8888 target = gen_reg_rtx (mode);
8890 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8891 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8892 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8893 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8894 else
8895 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8896 return target;
8898 case FIX_TRUNC_EXPR:
8899 op0 = expand_normal (treeop0);
8900 if (target == 0 || modifier == EXPAND_STACK_PARM)
8901 target = gen_reg_rtx (mode);
8902 expand_fix (target, op0, unsignedp);
8903 return target;
8905 case FLOAT_EXPR:
8906 op0 = expand_normal (treeop0);
8907 if (target == 0 || modifier == EXPAND_STACK_PARM)
8908 target = gen_reg_rtx (mode);
8909 /* expand_float can't figure out what to do if FROM has VOIDmode.
8910 So give it the correct mode. With -O, cse will optimize this. */
8911 if (GET_MODE (op0) == VOIDmode)
8912 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8913 op0);
8914 expand_float (target, op0,
8915 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8916 return target;
8918 case NEGATE_EXPR:
8919 op0 = expand_expr (treeop0, subtarget,
8920 VOIDmode, EXPAND_NORMAL);
8921 if (modifier == EXPAND_STACK_PARM)
8922 target = 0;
8923 temp = expand_unop (mode,
8924 optab_for_tree_code (NEGATE_EXPR, type,
8925 optab_default),
8926 op0, target, 0);
8927 gcc_assert (temp);
8928 return REDUCE_BIT_FIELD (temp);
8930 case ABS_EXPR:
8931 op0 = expand_expr (treeop0, subtarget,
8932 VOIDmode, EXPAND_NORMAL);
8933 if (modifier == EXPAND_STACK_PARM)
8934 target = 0;
8936 /* ABS_EXPR is not valid for complex arguments. */
8937 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8938 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8940 /* Unsigned abs is simply the operand. Testing here means we don't
8941 risk generating incorrect code below. */
8942 if (TYPE_UNSIGNED (type))
8943 return op0;
8945 return expand_abs (mode, op0, target, unsignedp,
8946 safe_from_p (target, treeop0, 1));
8948 case MAX_EXPR:
8949 case MIN_EXPR:
8950 target = original_target;
8951 if (target == 0
8952 || modifier == EXPAND_STACK_PARM
8953 || (MEM_P (target) && MEM_VOLATILE_P (target))
8954 || GET_MODE (target) != mode
8955 || (REG_P (target)
8956 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8957 target = gen_reg_rtx (mode);
8958 expand_operands (treeop0, treeop1,
8959 target, &op0, &op1, EXPAND_NORMAL);
8961 /* First try to do it with a special MIN or MAX instruction.
8962 If that does not win, use a conditional jump to select the proper
8963 value. */
8964 this_optab = optab_for_tree_code (code, type, optab_default);
8965 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8966 OPTAB_WIDEN);
8967 if (temp != 0)
8968 return temp;
8970 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8971 and similarly for MAX <x, y>. */
8972 if (VECTOR_TYPE_P (type))
8974 tree t0 = make_tree (type, op0);
8975 tree t1 = make_tree (type, op1);
8976 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8977 type, t0, t1);
8978 return expand_vec_cond_expr (type, comparison, t0, t1,
8979 original_target);
8982 /* At this point, a MEM target is no longer useful; we will get better
8983 code without it. */
8985 if (! REG_P (target))
8986 target = gen_reg_rtx (mode);
8988 /* If op1 was placed in target, swap op0 and op1. */
8989 if (target != op0 && target == op1)
8990 std::swap (op0, op1);
8992 /* We generate better code and avoid problems with op1 mentioning
8993 target by forcing op1 into a pseudo if it isn't a constant. */
8994 if (! CONSTANT_P (op1))
8995 op1 = force_reg (mode, op1);
8998 enum rtx_code comparison_code;
8999 rtx cmpop1 = op1;
9001 if (code == MAX_EXPR)
9002 comparison_code = unsignedp ? GEU : GE;
9003 else
9004 comparison_code = unsignedp ? LEU : LE;
9006 /* Canonicalize to comparisons against 0. */
9007 if (op1 == const1_rtx)
9009 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9010 or (a != 0 ? a : 1) for unsigned.
9011 For MIN we are safe converting (a <= 1 ? a : 1)
9012 into (a <= 0 ? a : 1) */
9013 cmpop1 = const0_rtx;
9014 if (code == MAX_EXPR)
9015 comparison_code = unsignedp ? NE : GT;
9017 if (op1 == constm1_rtx && !unsignedp)
9019 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9020 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9021 cmpop1 = const0_rtx;
9022 if (code == MIN_EXPR)
9023 comparison_code = LT;
9026 /* Use a conditional move if possible. */
9027 if (can_conditionally_move_p (mode))
9029 rtx insn;
9031 start_sequence ();
9033 /* Try to emit the conditional move. */
9034 insn = emit_conditional_move (target, comparison_code,
9035 op0, cmpop1, mode,
9036 op0, op1, mode,
9037 unsignedp);
9039 /* If we could do the conditional move, emit the sequence,
9040 and return. */
9041 if (insn)
9043 rtx_insn *seq = get_insns ();
9044 end_sequence ();
9045 emit_insn (seq);
9046 return target;
9049 /* Otherwise discard the sequence and fall back to code with
9050 branches. */
9051 end_sequence ();
9054 if (target != op0)
9055 emit_move_insn (target, op0);
9057 lab = gen_label_rtx ();
9058 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9059 unsignedp, mode, NULL_RTX, NULL, lab,
9060 profile_probability::uninitialized ());
9062 emit_move_insn (target, op1);
9063 emit_label (lab);
9064 return target;
9066 case BIT_NOT_EXPR:
9067 op0 = expand_expr (treeop0, subtarget,
9068 VOIDmode, EXPAND_NORMAL);
9069 if (modifier == EXPAND_STACK_PARM)
9070 target = 0;
9071 /* In case we have to reduce the result to bitfield precision
9072 for unsigned bitfield expand this as XOR with a proper constant
9073 instead. */
9074 if (reduce_bit_field && TYPE_UNSIGNED (type))
9076 int_mode = SCALAR_INT_TYPE_MODE (type);
9077 wide_int mask = wi::mask (TYPE_PRECISION (type),
9078 false, GET_MODE_PRECISION (int_mode));
9080 temp = expand_binop (int_mode, xor_optab, op0,
9081 immed_wide_int_const (mask, int_mode),
9082 target, 1, OPTAB_LIB_WIDEN);
9084 else
9085 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9086 gcc_assert (temp);
9087 return temp;
9089 /* ??? Can optimize bitwise operations with one arg constant.
9090 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9091 and (a bitwise1 b) bitwise2 b (etc)
9092 but that is probably not worth while. */
9094 case BIT_AND_EXPR:
9095 case BIT_IOR_EXPR:
9096 case BIT_XOR_EXPR:
9097 goto binop;
9099 case LROTATE_EXPR:
9100 case RROTATE_EXPR:
9101 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9102 || type_has_mode_precision_p (type));
9103 /* fall through */
9105 case LSHIFT_EXPR:
9106 case RSHIFT_EXPR:
9108 /* If this is a fixed-point operation, then we cannot use the code
9109 below because "expand_shift" doesn't support sat/no-sat fixed-point
9110 shifts. */
9111 if (ALL_FIXED_POINT_MODE_P (mode))
9112 goto binop;
9114 if (! safe_from_p (subtarget, treeop1, 1))
9115 subtarget = 0;
9116 if (modifier == EXPAND_STACK_PARM)
9117 target = 0;
9118 op0 = expand_expr (treeop0, subtarget,
9119 VOIDmode, EXPAND_NORMAL);
9121 /* Left shift optimization when shifting across word_size boundary.
9123 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9124 there isn't native instruction to support this wide mode
9125 left shift. Given below scenario:
9127 Type A = (Type) B << C
9129 |< T >|
9130 | dest_high | dest_low |
9132 | word_size |
9134 If the shift amount C caused we shift B to across the word
9135 size boundary, i.e part of B shifted into high half of
9136 destination register, and part of B remains in the low
9137 half, then GCC will use the following left shift expand
9138 logic:
9140 1. Initialize dest_low to B.
9141 2. Initialize every bit of dest_high to the sign bit of B.
9142 3. Logic left shift dest_low by C bit to finalize dest_low.
9143 The value of dest_low before this shift is kept in a temp D.
9144 4. Logic left shift dest_high by C.
9145 5. Logic right shift D by (word_size - C).
9146 6. Or the result of 4 and 5 to finalize dest_high.
9148 While, by checking gimple statements, if operand B is
9149 coming from signed extension, then we can simplify above
9150 expand logic into:
9152 1. dest_high = src_low >> (word_size - C).
9153 2. dest_low = src_low << C.
9155 We can use one arithmetic right shift to finish all the
9156 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9157 needed from 6 into 2.
9159 The case is similar for zero extension, except that we
9160 initialize dest_high to zero rather than copies of the sign
9161 bit from B. Furthermore, we need to use a logical right shift
9162 in this case.
9164 The choice of sign-extension versus zero-extension is
9165 determined entirely by whether or not B is signed and is
9166 independent of the current setting of unsignedp. */
9168 temp = NULL_RTX;
9169 if (code == LSHIFT_EXPR
9170 && target
9171 && REG_P (target)
9172 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9173 && mode == int_mode
9174 && TREE_CONSTANT (treeop1)
9175 && TREE_CODE (treeop0) == SSA_NAME)
9177 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9178 if (is_gimple_assign (def)
9179 && gimple_assign_rhs_code (def) == NOP_EXPR)
9181 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9182 (TREE_TYPE (gimple_assign_rhs1 (def)));
9184 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9185 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9186 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9187 >= GET_MODE_BITSIZE (word_mode)))
9189 rtx_insn *seq, *seq_old;
9190 unsigned int high_off = subreg_highpart_offset (word_mode,
9191 int_mode);
9192 bool extend_unsigned
9193 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9194 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9195 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9196 rtx dest_high = simplify_gen_subreg (word_mode, target,
9197 int_mode, high_off);
9198 HOST_WIDE_INT ramount = (BITS_PER_WORD
9199 - TREE_INT_CST_LOW (treeop1));
9200 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9202 start_sequence ();
9203 /* dest_high = src_low >> (word_size - C). */
9204 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9205 rshift, dest_high,
9206 extend_unsigned);
9207 if (temp != dest_high)
9208 emit_move_insn (dest_high, temp);
9210 /* dest_low = src_low << C. */
9211 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9212 treeop1, dest_low, unsignedp);
9213 if (temp != dest_low)
9214 emit_move_insn (dest_low, temp);
9216 seq = get_insns ();
9217 end_sequence ();
9218 temp = target ;
9220 if (have_insn_for (ASHIFT, int_mode))
9222 bool speed_p = optimize_insn_for_speed_p ();
9223 start_sequence ();
9224 rtx ret_old = expand_variable_shift (code, int_mode,
9225 op0, treeop1,
9226 target,
9227 unsignedp);
9229 seq_old = get_insns ();
9230 end_sequence ();
9231 if (seq_cost (seq, speed_p)
9232 >= seq_cost (seq_old, speed_p))
9234 seq = seq_old;
9235 temp = ret_old;
9238 emit_insn (seq);
9243 if (temp == NULL_RTX)
9244 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9245 unsignedp);
9246 if (code == LSHIFT_EXPR)
9247 temp = REDUCE_BIT_FIELD (temp);
9248 return temp;
9251 /* Could determine the answer when only additive constants differ. Also,
9252 the addition of one can be handled by changing the condition. */
9253 case LT_EXPR:
9254 case LE_EXPR:
9255 case GT_EXPR:
9256 case GE_EXPR:
9257 case EQ_EXPR:
9258 case NE_EXPR:
9259 case UNORDERED_EXPR:
9260 case ORDERED_EXPR:
9261 case UNLT_EXPR:
9262 case UNLE_EXPR:
9263 case UNGT_EXPR:
9264 case UNGE_EXPR:
9265 case UNEQ_EXPR:
9266 case LTGT_EXPR:
9268 temp = do_store_flag (ops,
9269 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9270 tmode != VOIDmode ? tmode : mode);
9271 if (temp)
9272 return temp;
9274 /* Use a compare and a jump for BLKmode comparisons, or for function
9275 type comparisons is have_canonicalize_funcptr_for_compare. */
9277 if ((target == 0
9278 || modifier == EXPAND_STACK_PARM
9279 || ! safe_from_p (target, treeop0, 1)
9280 || ! safe_from_p (target, treeop1, 1)
9281 /* Make sure we don't have a hard reg (such as function's return
9282 value) live across basic blocks, if not optimizing. */
9283 || (!optimize && REG_P (target)
9284 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9285 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9287 emit_move_insn (target, const0_rtx);
9289 rtx_code_label *lab1 = gen_label_rtx ();
9290 jumpifnot_1 (code, treeop0, treeop1, lab1,
9291 profile_probability::uninitialized ());
9293 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9294 emit_move_insn (target, constm1_rtx);
9295 else
9296 emit_move_insn (target, const1_rtx);
9298 emit_label (lab1);
9299 return target;
9301 case COMPLEX_EXPR:
9302 /* Get the rtx code of the operands. */
9303 op0 = expand_normal (treeop0);
9304 op1 = expand_normal (treeop1);
9306 if (!target)
9307 target = gen_reg_rtx (TYPE_MODE (type));
9308 else
9309 /* If target overlaps with op1, then either we need to force
9310 op1 into a pseudo (if target also overlaps with op0),
9311 or write the complex parts in reverse order. */
9312 switch (GET_CODE (target))
9314 case CONCAT:
9315 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9317 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9319 complex_expr_force_op1:
9320 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9321 emit_move_insn (temp, op1);
9322 op1 = temp;
9323 break;
9325 complex_expr_swap_order:
9326 /* Move the imaginary (op1) and real (op0) parts to their
9327 location. */
9328 write_complex_part (target, op1, true);
9329 write_complex_part (target, op0, false);
9331 return target;
9333 break;
9334 case MEM:
9335 temp = adjust_address_nv (target,
9336 GET_MODE_INNER (GET_MODE (target)), 0);
9337 if (reg_overlap_mentioned_p (temp, op1))
9339 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9340 temp = adjust_address_nv (target, imode,
9341 GET_MODE_SIZE (imode));
9342 if (reg_overlap_mentioned_p (temp, op0))
9343 goto complex_expr_force_op1;
9344 goto complex_expr_swap_order;
9346 break;
9347 default:
9348 if (reg_overlap_mentioned_p (target, op1))
9350 if (reg_overlap_mentioned_p (target, op0))
9351 goto complex_expr_force_op1;
9352 goto complex_expr_swap_order;
9354 break;
9357 /* Move the real (op0) and imaginary (op1) parts to their location. */
9358 write_complex_part (target, op0, false);
9359 write_complex_part (target, op1, true);
9361 return target;
9363 case WIDEN_SUM_EXPR:
9365 tree oprnd0 = treeop0;
9366 tree oprnd1 = treeop1;
9368 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9369 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9370 target, unsignedp);
9371 return target;
9374 case REDUC_MAX_EXPR:
9375 case REDUC_MIN_EXPR:
9376 case REDUC_PLUS_EXPR:
9378 op0 = expand_normal (treeop0);
9379 this_optab = optab_for_tree_code (code, type, optab_default);
9380 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9382 struct expand_operand ops[2];
9383 enum insn_code icode = optab_handler (this_optab, vec_mode);
9385 create_output_operand (&ops[0], target, mode);
9386 create_input_operand (&ops[1], op0, vec_mode);
9387 expand_insn (icode, 2, ops);
9388 target = ops[0].value;
9389 if (GET_MODE (target) != mode)
9390 return gen_lowpart (tmode, target);
9391 return target;
9394 case VEC_UNPACK_HI_EXPR:
9395 case VEC_UNPACK_LO_EXPR:
9397 op0 = expand_normal (treeop0);
9398 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9399 target, unsignedp);
9400 gcc_assert (temp);
9401 return temp;
9404 case VEC_UNPACK_FLOAT_HI_EXPR:
9405 case VEC_UNPACK_FLOAT_LO_EXPR:
9407 op0 = expand_normal (treeop0);
9408 /* The signedness is determined from input operand. */
9409 temp = expand_widen_pattern_expr
9410 (ops, op0, NULL_RTX, NULL_RTX,
9411 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9413 gcc_assert (temp);
9414 return temp;
9417 case VEC_WIDEN_MULT_HI_EXPR:
9418 case VEC_WIDEN_MULT_LO_EXPR:
9419 case VEC_WIDEN_MULT_EVEN_EXPR:
9420 case VEC_WIDEN_MULT_ODD_EXPR:
9421 case VEC_WIDEN_LSHIFT_HI_EXPR:
9422 case VEC_WIDEN_LSHIFT_LO_EXPR:
9423 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9424 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9425 target, unsignedp);
9426 gcc_assert (target);
9427 return target;
9429 case VEC_PACK_TRUNC_EXPR:
9430 case VEC_PACK_SAT_EXPR:
9431 case VEC_PACK_FIX_TRUNC_EXPR:
9432 mode = TYPE_MODE (TREE_TYPE (treeop0));
9433 goto binop;
9435 case VEC_PERM_EXPR:
9436 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9437 op2 = expand_normal (treeop2);
9439 /* Careful here: if the target doesn't support integral vector modes,
9440 a constant selection vector could wind up smooshed into a normal
9441 integral constant. */
9442 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9444 tree sel_type = TREE_TYPE (treeop2);
9445 machine_mode vmode
9446 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9447 TYPE_VECTOR_SUBPARTS (sel_type));
9448 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9449 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9450 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9452 else
9453 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9455 temp = expand_vec_perm (mode, op0, op1, op2, target);
9456 gcc_assert (temp);
9457 return temp;
9459 case DOT_PROD_EXPR:
9461 tree oprnd0 = treeop0;
9462 tree oprnd1 = treeop1;
9463 tree oprnd2 = treeop2;
9464 rtx op2;
9466 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9467 op2 = expand_normal (oprnd2);
9468 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9469 target, unsignedp);
9470 return target;
9473 case SAD_EXPR:
9475 tree oprnd0 = treeop0;
9476 tree oprnd1 = treeop1;
9477 tree oprnd2 = treeop2;
9478 rtx op2;
9480 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9481 op2 = expand_normal (oprnd2);
9482 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9483 target, unsignedp);
9484 return target;
9487 case REALIGN_LOAD_EXPR:
9489 tree oprnd0 = treeop0;
9490 tree oprnd1 = treeop1;
9491 tree oprnd2 = treeop2;
9492 rtx op2;
9494 this_optab = optab_for_tree_code (code, type, optab_default);
9495 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9496 op2 = expand_normal (oprnd2);
9497 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9498 target, unsignedp);
9499 gcc_assert (temp);
9500 return temp;
9503 case COND_EXPR:
9505 /* A COND_EXPR with its type being VOID_TYPE represents a
9506 conditional jump and is handled in
9507 expand_gimple_cond_expr. */
9508 gcc_assert (!VOID_TYPE_P (type));
9510 /* Note that COND_EXPRs whose type is a structure or union
9511 are required to be constructed to contain assignments of
9512 a temporary variable, so that we can evaluate them here
9513 for side effect only. If type is void, we must do likewise. */
9515 gcc_assert (!TREE_ADDRESSABLE (type)
9516 && !ignore
9517 && TREE_TYPE (treeop1) != void_type_node
9518 && TREE_TYPE (treeop2) != void_type_node);
9520 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9521 if (temp)
9522 return temp;
9524 /* If we are not to produce a result, we have no target. Otherwise,
9525 if a target was specified use it; it will not be used as an
9526 intermediate target unless it is safe. If no target, use a
9527 temporary. */
9529 if (modifier != EXPAND_STACK_PARM
9530 && original_target
9531 && safe_from_p (original_target, treeop0, 1)
9532 && GET_MODE (original_target) == mode
9533 && !MEM_P (original_target))
9534 temp = original_target;
9535 else
9536 temp = assign_temp (type, 0, 1);
9538 do_pending_stack_adjust ();
9539 NO_DEFER_POP;
9540 rtx_code_label *lab0 = gen_label_rtx ();
9541 rtx_code_label *lab1 = gen_label_rtx ();
9542 jumpifnot (treeop0, lab0,
9543 profile_probability::uninitialized ());
9544 store_expr (treeop1, temp,
9545 modifier == EXPAND_STACK_PARM,
9546 false, false);
9548 emit_jump_insn (targetm.gen_jump (lab1));
9549 emit_barrier ();
9550 emit_label (lab0);
9551 store_expr (treeop2, temp,
9552 modifier == EXPAND_STACK_PARM,
9553 false, false);
9555 emit_label (lab1);
9556 OK_DEFER_POP;
9557 return temp;
9560 case VEC_COND_EXPR:
9561 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9562 return target;
9564 case BIT_INSERT_EXPR:
9566 unsigned bitpos = tree_to_uhwi (treeop2);
9567 unsigned bitsize;
9568 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9569 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9570 else
9571 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9572 rtx op0 = expand_normal (treeop0);
9573 rtx op1 = expand_normal (treeop1);
9574 rtx dst = gen_reg_rtx (mode);
9575 emit_move_insn (dst, op0);
9576 store_bit_field (dst, bitsize, bitpos, 0, 0,
9577 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9578 return dst;
9581 default:
9582 gcc_unreachable ();
9585 /* Here to do an ordinary binary operator. */
9586 binop:
9587 expand_operands (treeop0, treeop1,
9588 subtarget, &op0, &op1, EXPAND_NORMAL);
9589 binop2:
9590 this_optab = optab_for_tree_code (code, type, optab_default);
9591 binop3:
9592 if (modifier == EXPAND_STACK_PARM)
9593 target = 0;
9594 temp = expand_binop (mode, this_optab, op0, op1, target,
9595 unsignedp, OPTAB_LIB_WIDEN);
9596 gcc_assert (temp);
9597 /* Bitwise operations do not need bitfield reduction as we expect their
9598 operands being properly truncated. */
9599 if (code == BIT_XOR_EXPR
9600 || code == BIT_AND_EXPR
9601 || code == BIT_IOR_EXPR)
9602 return temp;
9603 return REDUCE_BIT_FIELD (temp);
9605 #undef REDUCE_BIT_FIELD
9608 /* Return TRUE if expression STMT is suitable for replacement.
9609 Never consider memory loads as replaceable, because those don't ever lead
9610 into constant expressions. */
9612 static bool
9613 stmt_is_replaceable_p (gimple *stmt)
9615 if (ssa_is_replaceable_p (stmt))
9617 /* Don't move around loads. */
9618 if (!gimple_assign_single_p (stmt)
9619 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9620 return true;
9622 return false;
9626 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9627 enum expand_modifier modifier, rtx *alt_rtl,
9628 bool inner_reference_p)
9630 rtx op0, op1, temp, decl_rtl;
9631 tree type;
9632 int unsignedp;
9633 machine_mode mode, dmode;
9634 enum tree_code code = TREE_CODE (exp);
9635 rtx subtarget, original_target;
9636 int ignore;
9637 tree context;
9638 bool reduce_bit_field;
9639 location_t loc = EXPR_LOCATION (exp);
9640 struct separate_ops ops;
9641 tree treeop0, treeop1, treeop2;
9642 tree ssa_name = NULL_TREE;
9643 gimple *g;
9645 type = TREE_TYPE (exp);
9646 mode = TYPE_MODE (type);
9647 unsignedp = TYPE_UNSIGNED (type);
9649 treeop0 = treeop1 = treeop2 = NULL_TREE;
9650 if (!VL_EXP_CLASS_P (exp))
9651 switch (TREE_CODE_LENGTH (code))
9653 default:
9654 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9655 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9656 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9657 case 0: break;
9659 ops.code = code;
9660 ops.type = type;
9661 ops.op0 = treeop0;
9662 ops.op1 = treeop1;
9663 ops.op2 = treeop2;
9664 ops.location = loc;
9666 ignore = (target == const0_rtx
9667 || ((CONVERT_EXPR_CODE_P (code)
9668 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9669 && TREE_CODE (type) == VOID_TYPE));
9671 /* An operation in what may be a bit-field type needs the
9672 result to be reduced to the precision of the bit-field type,
9673 which is narrower than that of the type's mode. */
9674 reduce_bit_field = (!ignore
9675 && INTEGRAL_TYPE_P (type)
9676 && !type_has_mode_precision_p (type));
9678 /* If we are going to ignore this result, we need only do something
9679 if there is a side-effect somewhere in the expression. If there
9680 is, short-circuit the most common cases here. Note that we must
9681 not call expand_expr with anything but const0_rtx in case this
9682 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9684 if (ignore)
9686 if (! TREE_SIDE_EFFECTS (exp))
9687 return const0_rtx;
9689 /* Ensure we reference a volatile object even if value is ignored, but
9690 don't do this if all we are doing is taking its address. */
9691 if (TREE_THIS_VOLATILE (exp)
9692 && TREE_CODE (exp) != FUNCTION_DECL
9693 && mode != VOIDmode && mode != BLKmode
9694 && modifier != EXPAND_CONST_ADDRESS)
9696 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9697 if (MEM_P (temp))
9698 copy_to_reg (temp);
9699 return const0_rtx;
9702 if (TREE_CODE_CLASS (code) == tcc_unary
9703 || code == BIT_FIELD_REF
9704 || code == COMPONENT_REF
9705 || code == INDIRECT_REF)
9706 return expand_expr (treeop0, const0_rtx, VOIDmode,
9707 modifier);
9709 else if (TREE_CODE_CLASS (code) == tcc_binary
9710 || TREE_CODE_CLASS (code) == tcc_comparison
9711 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9713 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9714 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9715 return const0_rtx;
9718 target = 0;
9721 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9722 target = 0;
9724 /* Use subtarget as the target for operand 0 of a binary operation. */
9725 subtarget = get_subtarget (target);
9726 original_target = target;
9728 switch (code)
9730 case LABEL_DECL:
9732 tree function = decl_function_context (exp);
9734 temp = label_rtx (exp);
9735 temp = gen_rtx_LABEL_REF (Pmode, temp);
9737 if (function != current_function_decl
9738 && function != 0)
9739 LABEL_REF_NONLOCAL_P (temp) = 1;
9741 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9742 return temp;
9745 case SSA_NAME:
9746 /* ??? ivopts calls expander, without any preparation from
9747 out-of-ssa. So fake instructions as if this was an access to the
9748 base variable. This unnecessarily allocates a pseudo, see how we can
9749 reuse it, if partition base vars have it set already. */
9750 if (!currently_expanding_to_rtl)
9752 tree var = SSA_NAME_VAR (exp);
9753 if (var && DECL_RTL_SET_P (var))
9754 return DECL_RTL (var);
9755 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9756 LAST_VIRTUAL_REGISTER + 1);
9759 g = get_gimple_for_ssa_name (exp);
9760 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9761 if (g == NULL
9762 && modifier == EXPAND_INITIALIZER
9763 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9764 && (optimize || !SSA_NAME_VAR (exp)
9765 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9766 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9767 g = SSA_NAME_DEF_STMT (exp);
9768 if (g)
9770 rtx r;
9771 location_t saved_loc = curr_insn_location ();
9772 location_t loc = gimple_location (g);
9773 if (loc != UNKNOWN_LOCATION)
9774 set_curr_insn_location (loc);
9775 ops.code = gimple_assign_rhs_code (g);
9776 switch (get_gimple_rhs_class (ops.code))
9778 case GIMPLE_TERNARY_RHS:
9779 ops.op2 = gimple_assign_rhs3 (g);
9780 /* Fallthru */
9781 case GIMPLE_BINARY_RHS:
9782 ops.op1 = gimple_assign_rhs2 (g);
9784 /* Try to expand conditonal compare. */
9785 if (targetm.gen_ccmp_first)
9787 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9788 r = expand_ccmp_expr (g, mode);
9789 if (r)
9790 break;
9792 /* Fallthru */
9793 case GIMPLE_UNARY_RHS:
9794 ops.op0 = gimple_assign_rhs1 (g);
9795 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9796 ops.location = loc;
9797 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9798 break;
9799 case GIMPLE_SINGLE_RHS:
9801 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9802 tmode, modifier, alt_rtl,
9803 inner_reference_p);
9804 break;
9806 default:
9807 gcc_unreachable ();
9809 set_curr_insn_location (saved_loc);
9810 if (REG_P (r) && !REG_EXPR (r))
9811 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9812 return r;
9815 ssa_name = exp;
9816 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9817 exp = SSA_NAME_VAR (ssa_name);
9818 goto expand_decl_rtl;
9820 case PARM_DECL:
9821 case VAR_DECL:
9822 /* If a static var's type was incomplete when the decl was written,
9823 but the type is complete now, lay out the decl now. */
9824 if (DECL_SIZE (exp) == 0
9825 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9826 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9827 layout_decl (exp, 0);
9829 /* fall through */
9831 case FUNCTION_DECL:
9832 case RESULT_DECL:
9833 decl_rtl = DECL_RTL (exp);
9834 expand_decl_rtl:
9835 gcc_assert (decl_rtl);
9837 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9838 settings for VECTOR_TYPE_P that might switch for the function. */
9839 if (currently_expanding_to_rtl
9840 && code == VAR_DECL && MEM_P (decl_rtl)
9841 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9842 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9843 else
9844 decl_rtl = copy_rtx (decl_rtl);
9846 /* Record writes to register variables. */
9847 if (modifier == EXPAND_WRITE
9848 && REG_P (decl_rtl)
9849 && HARD_REGISTER_P (decl_rtl))
9850 add_to_hard_reg_set (&crtl->asm_clobbers,
9851 GET_MODE (decl_rtl), REGNO (decl_rtl));
9853 /* Ensure variable marked as used even if it doesn't go through
9854 a parser. If it hasn't be used yet, write out an external
9855 definition. */
9856 if (exp)
9857 TREE_USED (exp) = 1;
9859 /* Show we haven't gotten RTL for this yet. */
9860 temp = 0;
9862 /* Variables inherited from containing functions should have
9863 been lowered by this point. */
9864 if (exp)
9865 context = decl_function_context (exp);
9866 gcc_assert (!exp
9867 || SCOPE_FILE_SCOPE_P (context)
9868 || context == current_function_decl
9869 || TREE_STATIC (exp)
9870 || DECL_EXTERNAL (exp)
9871 /* ??? C++ creates functions that are not TREE_STATIC. */
9872 || TREE_CODE (exp) == FUNCTION_DECL);
9874 /* This is the case of an array whose size is to be determined
9875 from its initializer, while the initializer is still being parsed.
9876 ??? We aren't parsing while expanding anymore. */
9878 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9879 temp = validize_mem (decl_rtl);
9881 /* If DECL_RTL is memory, we are in the normal case and the
9882 address is not valid, get the address into a register. */
9884 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9886 if (alt_rtl)
9887 *alt_rtl = decl_rtl;
9888 decl_rtl = use_anchored_address (decl_rtl);
9889 if (modifier != EXPAND_CONST_ADDRESS
9890 && modifier != EXPAND_SUM
9891 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9892 : GET_MODE (decl_rtl),
9893 XEXP (decl_rtl, 0),
9894 MEM_ADDR_SPACE (decl_rtl)))
9895 temp = replace_equiv_address (decl_rtl,
9896 copy_rtx (XEXP (decl_rtl, 0)));
9899 /* If we got something, return it. But first, set the alignment
9900 if the address is a register. */
9901 if (temp != 0)
9903 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9904 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9906 return temp;
9909 if (exp)
9910 dmode = DECL_MODE (exp);
9911 else
9912 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9914 /* If the mode of DECL_RTL does not match that of the decl,
9915 there are two cases: we are dealing with a BLKmode value
9916 that is returned in a register, or we are dealing with
9917 a promoted value. In the latter case, return a SUBREG
9918 of the wanted mode, but mark it so that we know that it
9919 was already extended. */
9920 if (REG_P (decl_rtl)
9921 && dmode != BLKmode
9922 && GET_MODE (decl_rtl) != dmode)
9924 machine_mode pmode;
9926 /* Get the signedness to be used for this variable. Ensure we get
9927 the same mode we got when the variable was declared. */
9928 if (code != SSA_NAME)
9929 pmode = promote_decl_mode (exp, &unsignedp);
9930 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9931 && gimple_code (g) == GIMPLE_CALL
9932 && !gimple_call_internal_p (g))
9933 pmode = promote_function_mode (type, mode, &unsignedp,
9934 gimple_call_fntype (g),
9936 else
9937 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9938 gcc_assert (GET_MODE (decl_rtl) == pmode);
9940 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9941 SUBREG_PROMOTED_VAR_P (temp) = 1;
9942 SUBREG_PROMOTED_SET (temp, unsignedp);
9943 return temp;
9946 return decl_rtl;
9948 case INTEGER_CST:
9950 /* Given that TYPE_PRECISION (type) is not always equal to
9951 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9952 the former to the latter according to the signedness of the
9953 type. */
9954 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
9955 temp = immed_wide_int_const
9956 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
9957 return temp;
9960 case VECTOR_CST:
9962 tree tmp = NULL_TREE;
9963 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9964 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9965 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9966 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9967 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9968 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9969 return const_vector_from_tree (exp);
9970 scalar_int_mode int_mode;
9971 if (is_int_mode (mode, &int_mode))
9973 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9974 return const_scalar_mask_from_tree (int_mode, exp);
9975 else
9977 tree type_for_mode
9978 = lang_hooks.types.type_for_mode (int_mode, 1);
9979 if (type_for_mode)
9980 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9981 type_for_mode, exp);
9984 if (!tmp)
9986 vec<constructor_elt, va_gc> *v;
9987 unsigned i;
9988 vec_alloc (v, VECTOR_CST_NELTS (exp));
9989 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9990 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9991 tmp = build_constructor (type, v);
9993 return expand_expr (tmp, ignore ? const0_rtx : target,
9994 tmode, modifier);
9997 case CONST_DECL:
9998 if (modifier == EXPAND_WRITE)
10000 /* Writing into CONST_DECL is always invalid, but handle it
10001 gracefully. */
10002 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10003 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10004 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10005 EXPAND_NORMAL, as);
10006 op0 = memory_address_addr_space (mode, op0, as);
10007 temp = gen_rtx_MEM (mode, op0);
10008 set_mem_addr_space (temp, as);
10009 return temp;
10011 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10013 case REAL_CST:
10014 /* If optimized, generate immediate CONST_DOUBLE
10015 which will be turned into memory by reload if necessary.
10017 We used to force a register so that loop.c could see it. But
10018 this does not allow gen_* patterns to perform optimizations with
10019 the constants. It also produces two insns in cases like "x = 1.0;".
10020 On most machines, floating-point constants are not permitted in
10021 many insns, so we'd end up copying it to a register in any case.
10023 Now, we do the copying in expand_binop, if appropriate. */
10024 return const_double_from_real_value (TREE_REAL_CST (exp),
10025 TYPE_MODE (TREE_TYPE (exp)));
10027 case FIXED_CST:
10028 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10029 TYPE_MODE (TREE_TYPE (exp)));
10031 case COMPLEX_CST:
10032 /* Handle evaluating a complex constant in a CONCAT target. */
10033 if (original_target && GET_CODE (original_target) == CONCAT)
10035 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10036 rtx rtarg, itarg;
10038 rtarg = XEXP (original_target, 0);
10039 itarg = XEXP (original_target, 1);
10041 /* Move the real and imaginary parts separately. */
10042 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10043 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10045 if (op0 != rtarg)
10046 emit_move_insn (rtarg, op0);
10047 if (op1 != itarg)
10048 emit_move_insn (itarg, op1);
10050 return original_target;
10053 /* fall through */
10055 case STRING_CST:
10056 temp = expand_expr_constant (exp, 1, modifier);
10058 /* temp contains a constant address.
10059 On RISC machines where a constant address isn't valid,
10060 make some insns to get that address into a register. */
10061 if (modifier != EXPAND_CONST_ADDRESS
10062 && modifier != EXPAND_INITIALIZER
10063 && modifier != EXPAND_SUM
10064 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10065 MEM_ADDR_SPACE (temp)))
10066 return replace_equiv_address (temp,
10067 copy_rtx (XEXP (temp, 0)));
10068 return temp;
10070 case SAVE_EXPR:
10072 tree val = treeop0;
10073 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10074 inner_reference_p);
10076 if (!SAVE_EXPR_RESOLVED_P (exp))
10078 /* We can indeed still hit this case, typically via builtin
10079 expanders calling save_expr immediately before expanding
10080 something. Assume this means that we only have to deal
10081 with non-BLKmode values. */
10082 gcc_assert (GET_MODE (ret) != BLKmode);
10084 val = build_decl (curr_insn_location (),
10085 VAR_DECL, NULL, TREE_TYPE (exp));
10086 DECL_ARTIFICIAL (val) = 1;
10087 DECL_IGNORED_P (val) = 1;
10088 treeop0 = val;
10089 TREE_OPERAND (exp, 0) = treeop0;
10090 SAVE_EXPR_RESOLVED_P (exp) = 1;
10092 if (!CONSTANT_P (ret))
10093 ret = copy_to_reg (ret);
10094 SET_DECL_RTL (val, ret);
10097 return ret;
10101 case CONSTRUCTOR:
10102 /* If we don't need the result, just ensure we evaluate any
10103 subexpressions. */
10104 if (ignore)
10106 unsigned HOST_WIDE_INT idx;
10107 tree value;
10109 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10110 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10112 return const0_rtx;
10115 return expand_constructor (exp, target, modifier, false);
10117 case TARGET_MEM_REF:
10119 addr_space_t as
10120 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10121 enum insn_code icode;
10122 unsigned int align;
10124 op0 = addr_for_mem_ref (exp, as, true);
10125 op0 = memory_address_addr_space (mode, op0, as);
10126 temp = gen_rtx_MEM (mode, op0);
10127 set_mem_attributes (temp, exp, 0);
10128 set_mem_addr_space (temp, as);
10129 align = get_object_alignment (exp);
10130 if (modifier != EXPAND_WRITE
10131 && modifier != EXPAND_MEMORY
10132 && mode != BLKmode
10133 && align < GET_MODE_ALIGNMENT (mode)
10134 /* If the target does not have special handling for unaligned
10135 loads of mode then it can use regular moves for them. */
10136 && ((icode = optab_handler (movmisalign_optab, mode))
10137 != CODE_FOR_nothing))
10139 struct expand_operand ops[2];
10141 /* We've already validated the memory, and we're creating a
10142 new pseudo destination. The predicates really can't fail,
10143 nor can the generator. */
10144 create_output_operand (&ops[0], NULL_RTX, mode);
10145 create_fixed_operand (&ops[1], temp);
10146 expand_insn (icode, 2, ops);
10147 temp = ops[0].value;
10149 return temp;
10152 case MEM_REF:
10154 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10155 addr_space_t as
10156 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10157 machine_mode address_mode;
10158 tree base = TREE_OPERAND (exp, 0);
10159 gimple *def_stmt;
10160 enum insn_code icode;
10161 unsigned align;
10162 /* Handle expansion of non-aliased memory with non-BLKmode. That
10163 might end up in a register. */
10164 if (mem_ref_refers_to_non_mem_p (exp))
10166 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10167 base = TREE_OPERAND (base, 0);
10168 if (offset == 0
10169 && !reverse
10170 && tree_fits_uhwi_p (TYPE_SIZE (type))
10171 && (GET_MODE_BITSIZE (DECL_MODE (base))
10172 == tree_to_uhwi (TYPE_SIZE (type))))
10173 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10174 target, tmode, modifier);
10175 if (TYPE_MODE (type) == BLKmode)
10177 temp = assign_stack_temp (DECL_MODE (base),
10178 GET_MODE_SIZE (DECL_MODE (base)));
10179 store_expr (base, temp, 0, false, false);
10180 temp = adjust_address (temp, BLKmode, offset);
10181 set_mem_size (temp, int_size_in_bytes (type));
10182 return temp;
10184 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10185 bitsize_int (offset * BITS_PER_UNIT));
10186 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10187 return expand_expr (exp, target, tmode, modifier);
10189 address_mode = targetm.addr_space.address_mode (as);
10190 base = TREE_OPERAND (exp, 0);
10191 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10193 tree mask = gimple_assign_rhs2 (def_stmt);
10194 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10195 gimple_assign_rhs1 (def_stmt), mask);
10196 TREE_OPERAND (exp, 0) = base;
10198 align = get_object_alignment (exp);
10199 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10200 op0 = memory_address_addr_space (mode, op0, as);
10201 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10203 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10204 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10205 op0 = memory_address_addr_space (mode, op0, as);
10207 temp = gen_rtx_MEM (mode, op0);
10208 set_mem_attributes (temp, exp, 0);
10209 set_mem_addr_space (temp, as);
10210 if (TREE_THIS_VOLATILE (exp))
10211 MEM_VOLATILE_P (temp) = 1;
10212 if (modifier != EXPAND_WRITE
10213 && modifier != EXPAND_MEMORY
10214 && !inner_reference_p
10215 && mode != BLKmode
10216 && align < GET_MODE_ALIGNMENT (mode))
10218 if ((icode = optab_handler (movmisalign_optab, mode))
10219 != CODE_FOR_nothing)
10221 struct expand_operand ops[2];
10223 /* We've already validated the memory, and we're creating a
10224 new pseudo destination. The predicates really can't fail,
10225 nor can the generator. */
10226 create_output_operand (&ops[0], NULL_RTX, mode);
10227 create_fixed_operand (&ops[1], temp);
10228 expand_insn (icode, 2, ops);
10229 temp = ops[0].value;
10231 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10232 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10233 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10234 (modifier == EXPAND_STACK_PARM
10235 ? NULL_RTX : target),
10236 mode, mode, false, alt_rtl);
10238 if (reverse
10239 && modifier != EXPAND_MEMORY
10240 && modifier != EXPAND_WRITE)
10241 temp = flip_storage_order (mode, temp);
10242 return temp;
10245 case ARRAY_REF:
10248 tree array = treeop0;
10249 tree index = treeop1;
10250 tree init;
10252 /* Fold an expression like: "foo"[2].
10253 This is not done in fold so it won't happen inside &.
10254 Don't fold if this is for wide characters since it's too
10255 difficult to do correctly and this is a very rare case. */
10257 if (modifier != EXPAND_CONST_ADDRESS
10258 && modifier != EXPAND_INITIALIZER
10259 && modifier != EXPAND_MEMORY)
10261 tree t = fold_read_from_constant_string (exp);
10263 if (t)
10264 return expand_expr (t, target, tmode, modifier);
10267 /* If this is a constant index into a constant array,
10268 just get the value from the array. Handle both the cases when
10269 we have an explicit constructor and when our operand is a variable
10270 that was declared const. */
10272 if (modifier != EXPAND_CONST_ADDRESS
10273 && modifier != EXPAND_INITIALIZER
10274 && modifier != EXPAND_MEMORY
10275 && TREE_CODE (array) == CONSTRUCTOR
10276 && ! TREE_SIDE_EFFECTS (array)
10277 && TREE_CODE (index) == INTEGER_CST)
10279 unsigned HOST_WIDE_INT ix;
10280 tree field, value;
10282 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10283 field, value)
10284 if (tree_int_cst_equal (field, index))
10286 if (!TREE_SIDE_EFFECTS (value))
10287 return expand_expr (fold (value), target, tmode, modifier);
10288 break;
10292 else if (optimize >= 1
10293 && modifier != EXPAND_CONST_ADDRESS
10294 && modifier != EXPAND_INITIALIZER
10295 && modifier != EXPAND_MEMORY
10296 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10297 && TREE_CODE (index) == INTEGER_CST
10298 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10299 && (init = ctor_for_folding (array)) != error_mark_node)
10301 if (init == NULL_TREE)
10303 tree value = build_zero_cst (type);
10304 if (TREE_CODE (value) == CONSTRUCTOR)
10306 /* If VALUE is a CONSTRUCTOR, this optimization is only
10307 useful if this doesn't store the CONSTRUCTOR into
10308 memory. If it does, it is more efficient to just
10309 load the data from the array directly. */
10310 rtx ret = expand_constructor (value, target,
10311 modifier, true);
10312 if (ret == NULL_RTX)
10313 value = NULL_TREE;
10316 if (value)
10317 return expand_expr (value, target, tmode, modifier);
10319 else if (TREE_CODE (init) == CONSTRUCTOR)
10321 unsigned HOST_WIDE_INT ix;
10322 tree field, value;
10324 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10325 field, value)
10326 if (tree_int_cst_equal (field, index))
10328 if (TREE_SIDE_EFFECTS (value))
10329 break;
10331 if (TREE_CODE (value) == CONSTRUCTOR)
10333 /* If VALUE is a CONSTRUCTOR, this
10334 optimization is only useful if
10335 this doesn't store the CONSTRUCTOR
10336 into memory. If it does, it is more
10337 efficient to just load the data from
10338 the array directly. */
10339 rtx ret = expand_constructor (value, target,
10340 modifier, true);
10341 if (ret == NULL_RTX)
10342 break;
10345 return
10346 expand_expr (fold (value), target, tmode, modifier);
10349 else if (TREE_CODE (init) == STRING_CST)
10351 tree low_bound = array_ref_low_bound (exp);
10352 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10354 /* Optimize the special case of a zero lower bound.
10356 We convert the lower bound to sizetype to avoid problems
10357 with constant folding. E.g. suppose the lower bound is
10358 1 and its mode is QI. Without the conversion
10359 (ARRAY + (INDEX - (unsigned char)1))
10360 becomes
10361 (ARRAY + (-(unsigned char)1) + INDEX)
10362 which becomes
10363 (ARRAY + 255 + INDEX). Oops! */
10364 if (!integer_zerop (low_bound))
10365 index1 = size_diffop_loc (loc, index1,
10366 fold_convert_loc (loc, sizetype,
10367 low_bound));
10369 if (tree_fits_uhwi_p (index1)
10370 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10372 tree type = TREE_TYPE (TREE_TYPE (init));
10373 scalar_int_mode mode;
10375 if (is_int_mode (TYPE_MODE (type), &mode)
10376 && GET_MODE_SIZE (mode) == 1)
10377 return gen_int_mode (TREE_STRING_POINTER (init)
10378 [TREE_INT_CST_LOW (index1)],
10379 mode);
10384 goto normal_inner_ref;
10386 case COMPONENT_REF:
10387 /* If the operand is a CONSTRUCTOR, we can just extract the
10388 appropriate field if it is present. */
10389 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10391 unsigned HOST_WIDE_INT idx;
10392 tree field, value;
10393 scalar_int_mode field_mode;
10395 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10396 idx, field, value)
10397 if (field == treeop1
10398 /* We can normally use the value of the field in the
10399 CONSTRUCTOR. However, if this is a bitfield in
10400 an integral mode that we can fit in a HOST_WIDE_INT,
10401 we must mask only the number of bits in the bitfield,
10402 since this is done implicitly by the constructor. If
10403 the bitfield does not meet either of those conditions,
10404 we can't do this optimization. */
10405 && (! DECL_BIT_FIELD (field)
10406 || (is_int_mode (DECL_MODE (field), &field_mode)
10407 && (GET_MODE_PRECISION (field_mode)
10408 <= HOST_BITS_PER_WIDE_INT))))
10410 if (DECL_BIT_FIELD (field)
10411 && modifier == EXPAND_STACK_PARM)
10412 target = 0;
10413 op0 = expand_expr (value, target, tmode, modifier);
10414 if (DECL_BIT_FIELD (field))
10416 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10417 scalar_int_mode imode
10418 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10420 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10422 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10423 imode);
10424 op0 = expand_and (imode, op0, op1, target);
10426 else
10428 int count = GET_MODE_PRECISION (imode) - bitsize;
10430 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10431 target, 0);
10432 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10433 target, 0);
10437 return op0;
10440 goto normal_inner_ref;
10442 case BIT_FIELD_REF:
10443 case ARRAY_RANGE_REF:
10444 normal_inner_ref:
10446 machine_mode mode1, mode2;
10447 HOST_WIDE_INT bitsize, bitpos;
10448 tree offset;
10449 int reversep, volatilep = 0, must_force_mem;
10450 tree tem
10451 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10452 &unsignedp, &reversep, &volatilep);
10453 rtx orig_op0, memloc;
10454 bool clear_mem_expr = false;
10456 /* If we got back the original object, something is wrong. Perhaps
10457 we are evaluating an expression too early. In any event, don't
10458 infinitely recurse. */
10459 gcc_assert (tem != exp);
10461 /* If TEM's type is a union of variable size, pass TARGET to the inner
10462 computation, since it will need a temporary and TARGET is known
10463 to have to do. This occurs in unchecked conversion in Ada. */
10464 orig_op0 = op0
10465 = expand_expr_real (tem,
10466 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10467 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10468 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10469 != INTEGER_CST)
10470 && modifier != EXPAND_STACK_PARM
10471 ? target : NULL_RTX),
10472 VOIDmode,
10473 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10474 NULL, true);
10476 /* If the field has a mode, we want to access it in the
10477 field's mode, not the computed mode.
10478 If a MEM has VOIDmode (external with incomplete type),
10479 use BLKmode for it instead. */
10480 if (MEM_P (op0))
10482 if (mode1 != VOIDmode)
10483 op0 = adjust_address (op0, mode1, 0);
10484 else if (GET_MODE (op0) == VOIDmode)
10485 op0 = adjust_address (op0, BLKmode, 0);
10488 mode2
10489 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10491 /* If we have either an offset, a BLKmode result, or a reference
10492 outside the underlying object, we must force it to memory.
10493 Such a case can occur in Ada if we have unchecked conversion
10494 of an expression from a scalar type to an aggregate type or
10495 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10496 passed a partially uninitialized object or a view-conversion
10497 to a larger size. */
10498 must_force_mem = (offset
10499 || mode1 == BLKmode
10500 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10502 /* Handle CONCAT first. */
10503 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10505 if (bitpos == 0
10506 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10507 && COMPLEX_MODE_P (mode1)
10508 && COMPLEX_MODE_P (GET_MODE (op0))
10509 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10510 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10512 if (reversep)
10513 op0 = flip_storage_order (GET_MODE (op0), op0);
10514 if (mode1 != GET_MODE (op0))
10516 rtx parts[2];
10517 for (int i = 0; i < 2; i++)
10519 rtx op = read_complex_part (op0, i != 0);
10520 if (GET_CODE (op) == SUBREG)
10521 op = force_reg (GET_MODE (op), op);
10522 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10523 op);
10524 if (temp)
10525 op = temp;
10526 else
10528 if (!REG_P (op) && !MEM_P (op))
10529 op = force_reg (GET_MODE (op), op);
10530 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10532 parts[i] = op;
10534 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10536 return op0;
10538 if (bitpos == 0
10539 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10540 && bitsize)
10542 op0 = XEXP (op0, 0);
10543 mode2 = GET_MODE (op0);
10545 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10546 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10547 && bitpos
10548 && bitsize)
10550 op0 = XEXP (op0, 1);
10551 bitpos = 0;
10552 mode2 = GET_MODE (op0);
10554 else
10555 /* Otherwise force into memory. */
10556 must_force_mem = 1;
10559 /* If this is a constant, put it in a register if it is a legitimate
10560 constant and we don't need a memory reference. */
10561 if (CONSTANT_P (op0)
10562 && mode2 != BLKmode
10563 && targetm.legitimate_constant_p (mode2, op0)
10564 && !must_force_mem)
10565 op0 = force_reg (mode2, op0);
10567 /* Otherwise, if this is a constant, try to force it to the constant
10568 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10569 is a legitimate constant. */
10570 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10571 op0 = validize_mem (memloc);
10573 /* Otherwise, if this is a constant or the object is not in memory
10574 and need be, put it there. */
10575 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10577 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10578 emit_move_insn (memloc, op0);
10579 op0 = memloc;
10580 clear_mem_expr = true;
10583 if (offset)
10585 machine_mode address_mode;
10586 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10587 EXPAND_SUM);
10589 gcc_assert (MEM_P (op0));
10591 address_mode = get_address_mode (op0);
10592 if (GET_MODE (offset_rtx) != address_mode)
10594 /* We cannot be sure that the RTL in offset_rtx is valid outside
10595 of a memory address context, so force it into a register
10596 before attempting to convert it to the desired mode. */
10597 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10598 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10601 /* See the comment in expand_assignment for the rationale. */
10602 if (mode1 != VOIDmode
10603 && bitpos != 0
10604 && bitsize > 0
10605 && (bitpos % bitsize) == 0
10606 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10607 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10609 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10610 bitpos = 0;
10613 op0 = offset_address (op0, offset_rtx,
10614 highest_pow2_factor (offset));
10617 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10618 record its alignment as BIGGEST_ALIGNMENT. */
10619 if (MEM_P (op0) && bitpos == 0 && offset != 0
10620 && is_aligning_offset (offset, tem))
10621 set_mem_align (op0, BIGGEST_ALIGNMENT);
10623 /* Don't forget about volatility even if this is a bitfield. */
10624 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10626 if (op0 == orig_op0)
10627 op0 = copy_rtx (op0);
10629 MEM_VOLATILE_P (op0) = 1;
10632 /* In cases where an aligned union has an unaligned object
10633 as a field, we might be extracting a BLKmode value from
10634 an integer-mode (e.g., SImode) object. Handle this case
10635 by doing the extract into an object as wide as the field
10636 (which we know to be the width of a basic mode), then
10637 storing into memory, and changing the mode to BLKmode. */
10638 if (mode1 == VOIDmode
10639 || REG_P (op0) || GET_CODE (op0) == SUBREG
10640 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10641 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10642 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10643 && modifier != EXPAND_CONST_ADDRESS
10644 && modifier != EXPAND_INITIALIZER
10645 && modifier != EXPAND_MEMORY)
10646 /* If the bitfield is volatile and the bitsize
10647 is narrower than the access size of the bitfield,
10648 we need to extract bitfields from the access. */
10649 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10650 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10651 && mode1 != BLKmode
10652 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10653 /* If the field isn't aligned enough to fetch as a memref,
10654 fetch it as a bit field. */
10655 || (mode1 != BLKmode
10656 && (((MEM_P (op0)
10657 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10658 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10659 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10660 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10661 && modifier != EXPAND_MEMORY
10662 && ((modifier == EXPAND_CONST_ADDRESS
10663 || modifier == EXPAND_INITIALIZER)
10664 ? STRICT_ALIGNMENT
10665 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10666 || (bitpos % BITS_PER_UNIT != 0)))
10667 /* If the type and the field are a constant size and the
10668 size of the type isn't the same size as the bitfield,
10669 we must use bitfield operations. */
10670 || (bitsize >= 0
10671 && TYPE_SIZE (TREE_TYPE (exp))
10672 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10673 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10674 bitsize)))
10676 machine_mode ext_mode = mode;
10678 if (ext_mode == BLKmode
10679 && ! (target != 0 && MEM_P (op0)
10680 && MEM_P (target)
10681 && bitpos % BITS_PER_UNIT == 0))
10682 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10684 if (ext_mode == BLKmode)
10686 if (target == 0)
10687 target = assign_temp (type, 1, 1);
10689 /* ??? Unlike the similar test a few lines below, this one is
10690 very likely obsolete. */
10691 if (bitsize == 0)
10692 return target;
10694 /* In this case, BITPOS must start at a byte boundary and
10695 TARGET, if specified, must be a MEM. */
10696 gcc_assert (MEM_P (op0)
10697 && (!target || MEM_P (target))
10698 && !(bitpos % BITS_PER_UNIT));
10700 emit_block_move (target,
10701 adjust_address (op0, VOIDmode,
10702 bitpos / BITS_PER_UNIT),
10703 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10704 / BITS_PER_UNIT),
10705 (modifier == EXPAND_STACK_PARM
10706 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10708 return target;
10711 /* If we have nothing to extract, the result will be 0 for targets
10712 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10713 return 0 for the sake of consistency, as reading a zero-sized
10714 bitfield is valid in Ada and the value is fully specified. */
10715 if (bitsize == 0)
10716 return const0_rtx;
10718 op0 = validize_mem (op0);
10720 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10721 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10723 /* If the result has a record type and the extraction is done in
10724 an integral mode, then the field may be not aligned on a byte
10725 boundary; in this case, if it has reverse storage order, it
10726 needs to be extracted as a scalar field with reverse storage
10727 order and put back into memory order afterwards. */
10728 if (TREE_CODE (type) == RECORD_TYPE
10729 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10730 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10732 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10733 (modifier == EXPAND_STACK_PARM
10734 ? NULL_RTX : target),
10735 ext_mode, ext_mode, reversep, alt_rtl);
10737 /* If the result has a record type and the mode of OP0 is an
10738 integral mode then, if BITSIZE is narrower than this mode
10739 and this is for big-endian data, we must put the field
10740 into the high-order bits. And we must also put it back
10741 into memory order if it has been previously reversed. */
10742 scalar_int_mode op0_mode;
10743 if (TREE_CODE (type) == RECORD_TYPE
10744 && is_int_mode (GET_MODE (op0), &op0_mode))
10746 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10748 if (bitsize < size
10749 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10750 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10751 size - bitsize, op0, 1);
10753 if (reversep)
10754 op0 = flip_storage_order (op0_mode, op0);
10757 /* If the result type is BLKmode, store the data into a temporary
10758 of the appropriate type, but with the mode corresponding to the
10759 mode for the data we have (op0's mode). */
10760 if (mode == BLKmode)
10762 rtx new_rtx
10763 = assign_stack_temp_for_type (ext_mode,
10764 GET_MODE_BITSIZE (ext_mode),
10765 type);
10766 emit_move_insn (new_rtx, op0);
10767 op0 = copy_rtx (new_rtx);
10768 PUT_MODE (op0, BLKmode);
10771 return op0;
10774 /* If the result is BLKmode, use that to access the object
10775 now as well. */
10776 if (mode == BLKmode)
10777 mode1 = BLKmode;
10779 /* Get a reference to just this component. */
10780 if (modifier == EXPAND_CONST_ADDRESS
10781 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10782 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10783 else
10784 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10786 if (op0 == orig_op0)
10787 op0 = copy_rtx (op0);
10789 /* Don't set memory attributes if the base expression is
10790 SSA_NAME that got expanded as a MEM. In that case, we should
10791 just honor its original memory attributes. */
10792 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10793 set_mem_attributes (op0, exp, 0);
10795 if (REG_P (XEXP (op0, 0)))
10796 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10798 /* If op0 is a temporary because the original expressions was forced
10799 to memory, clear MEM_EXPR so that the original expression cannot
10800 be marked as addressable through MEM_EXPR of the temporary. */
10801 if (clear_mem_expr)
10802 set_mem_expr (op0, NULL_TREE);
10804 MEM_VOLATILE_P (op0) |= volatilep;
10806 if (reversep
10807 && modifier != EXPAND_MEMORY
10808 && modifier != EXPAND_WRITE)
10809 op0 = flip_storage_order (mode1, op0);
10811 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10812 || modifier == EXPAND_CONST_ADDRESS
10813 || modifier == EXPAND_INITIALIZER)
10814 return op0;
10816 if (target == 0)
10817 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10819 convert_move (target, op0, unsignedp);
10820 return target;
10823 case OBJ_TYPE_REF:
10824 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10826 case CALL_EXPR:
10827 /* All valid uses of __builtin_va_arg_pack () are removed during
10828 inlining. */
10829 if (CALL_EXPR_VA_ARG_PACK (exp))
10830 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10832 tree fndecl = get_callee_fndecl (exp), attr;
10834 if (fndecl
10835 && (attr = lookup_attribute ("error",
10836 DECL_ATTRIBUTES (fndecl))) != NULL)
10837 error ("%Kcall to %qs declared with attribute error: %s",
10838 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10839 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10840 if (fndecl
10841 && (attr = lookup_attribute ("warning",
10842 DECL_ATTRIBUTES (fndecl))) != NULL)
10843 warning_at (tree_nonartificial_location (exp),
10844 0, "%Kcall to %qs declared with attribute warning: %s",
10845 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10846 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10848 /* Check for a built-in function. */
10849 if (fndecl && DECL_BUILT_IN (fndecl))
10851 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10852 if (CALL_WITH_BOUNDS_P (exp))
10853 return expand_builtin_with_bounds (exp, target, subtarget,
10854 tmode, ignore);
10855 else
10856 return expand_builtin (exp, target, subtarget, tmode, ignore);
10859 return expand_call (exp, target, ignore);
10861 case VIEW_CONVERT_EXPR:
10862 op0 = NULL_RTX;
10864 /* If we are converting to BLKmode, try to avoid an intermediate
10865 temporary by fetching an inner memory reference. */
10866 if (mode == BLKmode
10867 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10868 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10869 && handled_component_p (treeop0))
10871 machine_mode mode1;
10872 HOST_WIDE_INT bitsize, bitpos;
10873 tree offset;
10874 int unsignedp, reversep, volatilep = 0;
10875 tree tem
10876 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10877 &unsignedp, &reversep, &volatilep);
10878 rtx orig_op0;
10880 /* ??? We should work harder and deal with non-zero offsets. */
10881 if (!offset
10882 && (bitpos % BITS_PER_UNIT) == 0
10883 && !reversep
10884 && bitsize >= 0
10885 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10887 /* See the normal_inner_ref case for the rationale. */
10888 orig_op0
10889 = expand_expr_real (tem,
10890 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10891 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10892 != INTEGER_CST)
10893 && modifier != EXPAND_STACK_PARM
10894 ? target : NULL_RTX),
10895 VOIDmode,
10896 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10897 NULL, true);
10899 if (MEM_P (orig_op0))
10901 op0 = orig_op0;
10903 /* Get a reference to just this component. */
10904 if (modifier == EXPAND_CONST_ADDRESS
10905 || modifier == EXPAND_SUM
10906 || modifier == EXPAND_INITIALIZER)
10907 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10908 else
10909 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10911 if (op0 == orig_op0)
10912 op0 = copy_rtx (op0);
10914 set_mem_attributes (op0, treeop0, 0);
10915 if (REG_P (XEXP (op0, 0)))
10916 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10918 MEM_VOLATILE_P (op0) |= volatilep;
10923 if (!op0)
10924 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10925 NULL, inner_reference_p);
10927 /* If the input and output modes are both the same, we are done. */
10928 if (mode == GET_MODE (op0))
10930 /* If neither mode is BLKmode, and both modes are the same size
10931 then we can use gen_lowpart. */
10932 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10933 && (GET_MODE_PRECISION (mode)
10934 == GET_MODE_PRECISION (GET_MODE (op0)))
10935 && !COMPLEX_MODE_P (GET_MODE (op0)))
10937 if (GET_CODE (op0) == SUBREG)
10938 op0 = force_reg (GET_MODE (op0), op0);
10939 temp = gen_lowpart_common (mode, op0);
10940 if (temp)
10941 op0 = temp;
10942 else
10944 if (!REG_P (op0) && !MEM_P (op0))
10945 op0 = force_reg (GET_MODE (op0), op0);
10946 op0 = gen_lowpart (mode, op0);
10949 /* If both types are integral, convert from one mode to the other. */
10950 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10951 op0 = convert_modes (mode, GET_MODE (op0), op0,
10952 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10953 /* If the output type is a bit-field type, do an extraction. */
10954 else if (reduce_bit_field)
10955 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10956 TYPE_UNSIGNED (type), NULL_RTX,
10957 mode, mode, false, NULL);
10958 /* As a last resort, spill op0 to memory, and reload it in a
10959 different mode. */
10960 else if (!MEM_P (op0))
10962 /* If the operand is not a MEM, force it into memory. Since we
10963 are going to be changing the mode of the MEM, don't call
10964 force_const_mem for constants because we don't allow pool
10965 constants to change mode. */
10966 tree inner_type = TREE_TYPE (treeop0);
10968 gcc_assert (!TREE_ADDRESSABLE (exp));
10970 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10971 target
10972 = assign_stack_temp_for_type
10973 (TYPE_MODE (inner_type),
10974 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10976 emit_move_insn (target, op0);
10977 op0 = target;
10980 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10981 output type is such that the operand is known to be aligned, indicate
10982 that it is. Otherwise, we need only be concerned about alignment for
10983 non-BLKmode results. */
10984 if (MEM_P (op0))
10986 enum insn_code icode;
10988 if (modifier != EXPAND_WRITE
10989 && modifier != EXPAND_MEMORY
10990 && !inner_reference_p
10991 && mode != BLKmode
10992 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10994 /* If the target does have special handling for unaligned
10995 loads of mode then use them. */
10996 if ((icode = optab_handler (movmisalign_optab, mode))
10997 != CODE_FOR_nothing)
10999 rtx reg;
11001 op0 = adjust_address (op0, mode, 0);
11002 /* We've already validated the memory, and we're creating a
11003 new pseudo destination. The predicates really can't
11004 fail. */
11005 reg = gen_reg_rtx (mode);
11007 /* Nor can the insn generator. */
11008 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11009 emit_insn (insn);
11010 return reg;
11012 else if (STRICT_ALIGNMENT)
11014 tree inner_type = TREE_TYPE (treeop0);
11015 HOST_WIDE_INT temp_size
11016 = MAX (int_size_in_bytes (inner_type),
11017 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11018 rtx new_rtx
11019 = assign_stack_temp_for_type (mode, temp_size, type);
11020 rtx new_with_op0_mode
11021 = adjust_address (new_rtx, GET_MODE (op0), 0);
11023 gcc_assert (!TREE_ADDRESSABLE (exp));
11025 if (GET_MODE (op0) == BLKmode)
11026 emit_block_move (new_with_op0_mode, op0,
11027 GEN_INT (GET_MODE_SIZE (mode)),
11028 (modifier == EXPAND_STACK_PARM
11029 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11030 else
11031 emit_move_insn (new_with_op0_mode, op0);
11033 op0 = new_rtx;
11037 op0 = adjust_address (op0, mode, 0);
11040 return op0;
11042 case MODIFY_EXPR:
11044 tree lhs = treeop0;
11045 tree rhs = treeop1;
11046 gcc_assert (ignore);
11048 /* Check for |= or &= of a bitfield of size one into another bitfield
11049 of size 1. In this case, (unless we need the result of the
11050 assignment) we can do this more efficiently with a
11051 test followed by an assignment, if necessary.
11053 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11054 things change so we do, this code should be enhanced to
11055 support it. */
11056 if (TREE_CODE (lhs) == COMPONENT_REF
11057 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11058 || TREE_CODE (rhs) == BIT_AND_EXPR)
11059 && TREE_OPERAND (rhs, 0) == lhs
11060 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11061 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11062 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11064 rtx_code_label *label = gen_label_rtx ();
11065 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11066 do_jump (TREE_OPERAND (rhs, 1),
11067 value ? label : 0,
11068 value ? 0 : label,
11069 profile_probability::uninitialized ());
11070 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11071 false);
11072 do_pending_stack_adjust ();
11073 emit_label (label);
11074 return const0_rtx;
11077 expand_assignment (lhs, rhs, false);
11078 return const0_rtx;
11081 case ADDR_EXPR:
11082 return expand_expr_addr_expr (exp, target, tmode, modifier);
11084 case REALPART_EXPR:
11085 op0 = expand_normal (treeop0);
11086 return read_complex_part (op0, false);
11088 case IMAGPART_EXPR:
11089 op0 = expand_normal (treeop0);
11090 return read_complex_part (op0, true);
11092 case RETURN_EXPR:
11093 case LABEL_EXPR:
11094 case GOTO_EXPR:
11095 case SWITCH_EXPR:
11096 case ASM_EXPR:
11097 /* Expanded in cfgexpand.c. */
11098 gcc_unreachable ();
11100 case TRY_CATCH_EXPR:
11101 case CATCH_EXPR:
11102 case EH_FILTER_EXPR:
11103 case TRY_FINALLY_EXPR:
11104 /* Lowered by tree-eh.c. */
11105 gcc_unreachable ();
11107 case WITH_CLEANUP_EXPR:
11108 case CLEANUP_POINT_EXPR:
11109 case TARGET_EXPR:
11110 case CASE_LABEL_EXPR:
11111 case VA_ARG_EXPR:
11112 case BIND_EXPR:
11113 case INIT_EXPR:
11114 case CONJ_EXPR:
11115 case COMPOUND_EXPR:
11116 case PREINCREMENT_EXPR:
11117 case PREDECREMENT_EXPR:
11118 case POSTINCREMENT_EXPR:
11119 case POSTDECREMENT_EXPR:
11120 case LOOP_EXPR:
11121 case EXIT_EXPR:
11122 case COMPOUND_LITERAL_EXPR:
11123 /* Lowered by gimplify.c. */
11124 gcc_unreachable ();
11126 case FDESC_EXPR:
11127 /* Function descriptors are not valid except for as
11128 initialization constants, and should not be expanded. */
11129 gcc_unreachable ();
11131 case WITH_SIZE_EXPR:
11132 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11133 have pulled out the size to use in whatever context it needed. */
11134 return expand_expr_real (treeop0, original_target, tmode,
11135 modifier, alt_rtl, inner_reference_p);
11137 default:
11138 return expand_expr_real_2 (&ops, target, tmode, modifier);
11142 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11143 signedness of TYPE), possibly returning the result in TARGET.
11144 TYPE is known to be a partial integer type. */
11145 static rtx
11146 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11148 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11149 if (target && GET_MODE (target) != GET_MODE (exp))
11150 target = 0;
11151 /* For constant values, reduce using build_int_cst_type. */
11152 if (CONST_INT_P (exp))
11154 HOST_WIDE_INT value = INTVAL (exp);
11155 tree t = build_int_cst_type (type, value);
11156 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11158 else if (TYPE_UNSIGNED (type))
11160 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11161 rtx mask = immed_wide_int_const
11162 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11163 return expand_and (mode, exp, mask, target);
11165 else
11167 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11168 int count = GET_MODE_PRECISION (mode) - prec;
11169 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11170 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11174 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11175 when applied to the address of EXP produces an address known to be
11176 aligned more than BIGGEST_ALIGNMENT. */
11178 static int
11179 is_aligning_offset (const_tree offset, const_tree exp)
11181 /* Strip off any conversions. */
11182 while (CONVERT_EXPR_P (offset))
11183 offset = TREE_OPERAND (offset, 0);
11185 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11186 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11187 if (TREE_CODE (offset) != BIT_AND_EXPR
11188 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11189 || compare_tree_int (TREE_OPERAND (offset, 1),
11190 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11191 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11192 return 0;
11194 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11195 It must be NEGATE_EXPR. Then strip any more conversions. */
11196 offset = TREE_OPERAND (offset, 0);
11197 while (CONVERT_EXPR_P (offset))
11198 offset = TREE_OPERAND (offset, 0);
11200 if (TREE_CODE (offset) != NEGATE_EXPR)
11201 return 0;
11203 offset = TREE_OPERAND (offset, 0);
11204 while (CONVERT_EXPR_P (offset))
11205 offset = TREE_OPERAND (offset, 0);
11207 /* This must now be the address of EXP. */
11208 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11211 /* Return the tree node if an ARG corresponds to a string constant or zero
11212 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11213 in bytes within the string that ARG is accessing. The type of the
11214 offset will be `sizetype'. */
11216 tree
11217 string_constant (tree arg, tree *ptr_offset)
11219 tree array, offset, lower_bound;
11220 STRIP_NOPS (arg);
11222 if (TREE_CODE (arg) == ADDR_EXPR)
11224 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11226 *ptr_offset = size_zero_node;
11227 return TREE_OPERAND (arg, 0);
11229 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11231 array = TREE_OPERAND (arg, 0);
11232 offset = size_zero_node;
11234 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11236 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11237 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11238 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11239 return 0;
11241 /* Check if the array has a nonzero lower bound. */
11242 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11243 if (!integer_zerop (lower_bound))
11245 /* If the offset and base aren't both constants, return 0. */
11246 if (TREE_CODE (lower_bound) != INTEGER_CST)
11247 return 0;
11248 if (TREE_CODE (offset) != INTEGER_CST)
11249 return 0;
11250 /* Adjust offset by the lower bound. */
11251 offset = size_diffop (fold_convert (sizetype, offset),
11252 fold_convert (sizetype, lower_bound));
11255 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11257 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11258 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11259 if (TREE_CODE (array) != ADDR_EXPR)
11260 return 0;
11261 array = TREE_OPERAND (array, 0);
11262 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11263 return 0;
11265 else
11266 return 0;
11268 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11270 tree arg0 = TREE_OPERAND (arg, 0);
11271 tree arg1 = TREE_OPERAND (arg, 1);
11273 STRIP_NOPS (arg0);
11274 STRIP_NOPS (arg1);
11276 if (TREE_CODE (arg0) == ADDR_EXPR
11277 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11278 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11280 array = TREE_OPERAND (arg0, 0);
11281 offset = arg1;
11283 else if (TREE_CODE (arg1) == ADDR_EXPR
11284 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11285 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11287 array = TREE_OPERAND (arg1, 0);
11288 offset = arg0;
11290 else
11291 return 0;
11293 else
11294 return 0;
11296 if (TREE_CODE (array) == STRING_CST)
11298 *ptr_offset = fold_convert (sizetype, offset);
11299 return array;
11301 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11303 int length;
11304 tree init = ctor_for_folding (array);
11306 /* Variables initialized to string literals can be handled too. */
11307 if (init == error_mark_node
11308 || !init
11309 || TREE_CODE (init) != STRING_CST)
11310 return 0;
11312 /* Avoid const char foo[4] = "abcde"; */
11313 if (DECL_SIZE_UNIT (array) == NULL_TREE
11314 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11315 || (length = TREE_STRING_LENGTH (init)) <= 0
11316 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11317 return 0;
11319 /* If variable is bigger than the string literal, OFFSET must be constant
11320 and inside of the bounds of the string literal. */
11321 offset = fold_convert (sizetype, offset);
11322 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11323 && (! tree_fits_uhwi_p (offset)
11324 || compare_tree_int (offset, length) >= 0))
11325 return 0;
11327 *ptr_offset = offset;
11328 return init;
11331 return 0;
11334 /* Generate code to calculate OPS, and exploded expression
11335 using a store-flag instruction and return an rtx for the result.
11336 OPS reflects a comparison.
11338 If TARGET is nonzero, store the result there if convenient.
11340 Return zero if there is no suitable set-flag instruction
11341 available on this machine.
11343 Once expand_expr has been called on the arguments of the comparison,
11344 we are committed to doing the store flag, since it is not safe to
11345 re-evaluate the expression. We emit the store-flag insn by calling
11346 emit_store_flag, but only expand the arguments if we have a reason
11347 to believe that emit_store_flag will be successful. If we think that
11348 it will, but it isn't, we have to simulate the store-flag with a
11349 set/jump/set sequence. */
11351 static rtx
11352 do_store_flag (sepops ops, rtx target, machine_mode mode)
11354 enum rtx_code code;
11355 tree arg0, arg1, type;
11356 machine_mode operand_mode;
11357 int unsignedp;
11358 rtx op0, op1;
11359 rtx subtarget = target;
11360 location_t loc = ops->location;
11362 arg0 = ops->op0;
11363 arg1 = ops->op1;
11365 /* Don't crash if the comparison was erroneous. */
11366 if (arg0 == error_mark_node || arg1 == error_mark_node)
11367 return const0_rtx;
11369 type = TREE_TYPE (arg0);
11370 operand_mode = TYPE_MODE (type);
11371 unsignedp = TYPE_UNSIGNED (type);
11373 /* We won't bother with BLKmode store-flag operations because it would mean
11374 passing a lot of information to emit_store_flag. */
11375 if (operand_mode == BLKmode)
11376 return 0;
11378 /* We won't bother with store-flag operations involving function pointers
11379 when function pointers must be canonicalized before comparisons. */
11380 if (targetm.have_canonicalize_funcptr_for_compare ()
11381 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11382 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11383 == FUNCTION_TYPE))
11384 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11385 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11386 == FUNCTION_TYPE))))
11387 return 0;
11389 STRIP_NOPS (arg0);
11390 STRIP_NOPS (arg1);
11392 /* For vector typed comparisons emit code to generate the desired
11393 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11394 expander for this. */
11395 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11397 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11398 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11399 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11400 return expand_vec_cmp_expr (ops->type, ifexp, target);
11401 else
11403 tree if_true = constant_boolean_node (true, ops->type);
11404 tree if_false = constant_boolean_node (false, ops->type);
11405 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11406 if_false, target);
11410 /* Get the rtx comparison code to use. We know that EXP is a comparison
11411 operation of some type. Some comparisons against 1 and -1 can be
11412 converted to comparisons with zero. Do so here so that the tests
11413 below will be aware that we have a comparison with zero. These
11414 tests will not catch constants in the first operand, but constants
11415 are rarely passed as the first operand. */
11417 switch (ops->code)
11419 case EQ_EXPR:
11420 code = EQ;
11421 break;
11422 case NE_EXPR:
11423 code = NE;
11424 break;
11425 case LT_EXPR:
11426 if (integer_onep (arg1))
11427 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11428 else
11429 code = unsignedp ? LTU : LT;
11430 break;
11431 case LE_EXPR:
11432 if (! unsignedp && integer_all_onesp (arg1))
11433 arg1 = integer_zero_node, code = LT;
11434 else
11435 code = unsignedp ? LEU : LE;
11436 break;
11437 case GT_EXPR:
11438 if (! unsignedp && integer_all_onesp (arg1))
11439 arg1 = integer_zero_node, code = GE;
11440 else
11441 code = unsignedp ? GTU : GT;
11442 break;
11443 case GE_EXPR:
11444 if (integer_onep (arg1))
11445 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11446 else
11447 code = unsignedp ? GEU : GE;
11448 break;
11450 case UNORDERED_EXPR:
11451 code = UNORDERED;
11452 break;
11453 case ORDERED_EXPR:
11454 code = ORDERED;
11455 break;
11456 case UNLT_EXPR:
11457 code = UNLT;
11458 break;
11459 case UNLE_EXPR:
11460 code = UNLE;
11461 break;
11462 case UNGT_EXPR:
11463 code = UNGT;
11464 break;
11465 case UNGE_EXPR:
11466 code = UNGE;
11467 break;
11468 case UNEQ_EXPR:
11469 code = UNEQ;
11470 break;
11471 case LTGT_EXPR:
11472 code = LTGT;
11473 break;
11475 default:
11476 gcc_unreachable ();
11479 /* Put a constant second. */
11480 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11481 || TREE_CODE (arg0) == FIXED_CST)
11483 std::swap (arg0, arg1);
11484 code = swap_condition (code);
11487 /* If this is an equality or inequality test of a single bit, we can
11488 do this by shifting the bit being tested to the low-order bit and
11489 masking the result with the constant 1. If the condition was EQ,
11490 we xor it with 1. This does not require an scc insn and is faster
11491 than an scc insn even if we have it.
11493 The code to make this transformation was moved into fold_single_bit_test,
11494 so we just call into the folder and expand its result. */
11496 if ((code == NE || code == EQ)
11497 && integer_zerop (arg1)
11498 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11500 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11501 if (srcstmt
11502 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11504 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11505 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11506 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11507 gimple_assign_rhs1 (srcstmt),
11508 gimple_assign_rhs2 (srcstmt));
11509 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11510 if (temp)
11511 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11515 if (! get_subtarget (target)
11516 || GET_MODE (subtarget) != operand_mode)
11517 subtarget = 0;
11519 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11521 if (target == 0)
11522 target = gen_reg_rtx (mode);
11524 /* Try a cstore if possible. */
11525 return emit_store_flag_force (target, code, op0, op1,
11526 operand_mode, unsignedp,
11527 (TYPE_PRECISION (ops->type) == 1
11528 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11531 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11532 0 otherwise (i.e. if there is no casesi instruction).
11534 DEFAULT_PROBABILITY is the probability of jumping to the default
11535 label. */
11537 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11538 rtx table_label, rtx default_label, rtx fallback_label,
11539 profile_probability default_probability)
11541 struct expand_operand ops[5];
11542 scalar_int_mode index_mode = SImode;
11543 rtx op1, op2, index;
11545 if (! targetm.have_casesi ())
11546 return 0;
11548 /* The index must be some form of integer. Convert it to SImode. */
11549 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11550 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11552 rtx rangertx = expand_normal (range);
11554 /* We must handle the endpoints in the original mode. */
11555 index_expr = build2 (MINUS_EXPR, index_type,
11556 index_expr, minval);
11557 minval = integer_zero_node;
11558 index = expand_normal (index_expr);
11559 if (default_label)
11560 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11561 omode, 1, default_label,
11562 default_probability);
11563 /* Now we can safely truncate. */
11564 index = convert_to_mode (index_mode, index, 0);
11566 else
11568 if (omode != index_mode)
11570 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11571 index_expr = fold_convert (index_type, index_expr);
11574 index = expand_normal (index_expr);
11577 do_pending_stack_adjust ();
11579 op1 = expand_normal (minval);
11580 op2 = expand_normal (range);
11582 create_input_operand (&ops[0], index, index_mode);
11583 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11584 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11585 create_fixed_operand (&ops[3], table_label);
11586 create_fixed_operand (&ops[4], (default_label
11587 ? default_label
11588 : fallback_label));
11589 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11590 return 1;
11593 /* Attempt to generate a tablejump instruction; same concept. */
11594 /* Subroutine of the next function.
11596 INDEX is the value being switched on, with the lowest value
11597 in the table already subtracted.
11598 MODE is its expected mode (needed if INDEX is constant).
11599 RANGE is the length of the jump table.
11600 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11602 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11603 index value is out of range.
11604 DEFAULT_PROBABILITY is the probability of jumping to
11605 the default label. */
11607 static void
11608 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11609 rtx default_label, profile_probability default_probability)
11611 rtx temp, vector;
11613 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11614 cfun->cfg->max_jumptable_ents = INTVAL (range);
11616 /* Do an unsigned comparison (in the proper mode) between the index
11617 expression and the value which represents the length of the range.
11618 Since we just finished subtracting the lower bound of the range
11619 from the index expression, this comparison allows us to simultaneously
11620 check that the original index expression value is both greater than
11621 or equal to the minimum value of the range and less than or equal to
11622 the maximum value of the range. */
11624 if (default_label)
11625 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11626 default_label, default_probability);
11629 /* If index is in range, it must fit in Pmode.
11630 Convert to Pmode so we can index with it. */
11631 if (mode != Pmode)
11632 index = convert_to_mode (Pmode, index, 1);
11634 /* Don't let a MEM slip through, because then INDEX that comes
11635 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11636 and break_out_memory_refs will go to work on it and mess it up. */
11637 #ifdef PIC_CASE_VECTOR_ADDRESS
11638 if (flag_pic && !REG_P (index))
11639 index = copy_to_mode_reg (Pmode, index);
11640 #endif
11642 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11643 GET_MODE_SIZE, because this indicates how large insns are. The other
11644 uses should all be Pmode, because they are addresses. This code
11645 could fail if addresses and insns are not the same size. */
11646 index = simplify_gen_binary (MULT, Pmode, index,
11647 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11648 Pmode));
11649 index = simplify_gen_binary (PLUS, Pmode, index,
11650 gen_rtx_LABEL_REF (Pmode, table_label));
11652 #ifdef PIC_CASE_VECTOR_ADDRESS
11653 if (flag_pic)
11654 index = PIC_CASE_VECTOR_ADDRESS (index);
11655 else
11656 #endif
11657 index = memory_address (CASE_VECTOR_MODE, index);
11658 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11659 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11660 convert_move (temp, vector, 0);
11662 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11664 /* If we are generating PIC code or if the table is PC-relative, the
11665 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11666 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11667 emit_barrier ();
11671 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11672 rtx table_label, rtx default_label,
11673 profile_probability default_probability)
11675 rtx index;
11677 if (! targetm.have_tablejump ())
11678 return 0;
11680 index_expr = fold_build2 (MINUS_EXPR, index_type,
11681 fold_convert (index_type, index_expr),
11682 fold_convert (index_type, minval));
11683 index = expand_normal (index_expr);
11684 do_pending_stack_adjust ();
11686 do_tablejump (index, TYPE_MODE (index_type),
11687 convert_modes (TYPE_MODE (index_type),
11688 TYPE_MODE (TREE_TYPE (range)),
11689 expand_normal (range),
11690 TYPE_UNSIGNED (TREE_TYPE (range))),
11691 table_label, default_label, default_probability);
11692 return 1;
11695 /* Return a CONST_VECTOR rtx representing vector mask for
11696 a VECTOR_CST of booleans. */
11697 static rtx
11698 const_vector_mask_from_tree (tree exp)
11700 rtvec v;
11701 unsigned i;
11702 int units;
11703 tree elt;
11704 machine_mode inner, mode;
11706 mode = TYPE_MODE (TREE_TYPE (exp));
11707 units = GET_MODE_NUNITS (mode);
11708 inner = GET_MODE_INNER (mode);
11710 v = rtvec_alloc (units);
11712 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11714 elt = VECTOR_CST_ELT (exp, i);
11716 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11717 if (integer_zerop (elt))
11718 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11719 else if (integer_onep (elt)
11720 || integer_minus_onep (elt))
11721 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11722 else
11723 gcc_unreachable ();
11726 return gen_rtx_CONST_VECTOR (mode, v);
11729 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11730 Return a constant scalar rtx of mode MODE in which bit X is set if element
11731 X of EXP is nonzero. */
11732 static rtx
11733 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11735 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11736 tree elt;
11737 unsigned i;
11739 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11741 elt = VECTOR_CST_ELT (exp, i);
11742 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11743 if (integer_all_onesp (elt))
11744 res = wi::set_bit (res, i);
11745 else
11746 gcc_assert (integer_zerop (elt));
11749 return immed_wide_int_const (res, mode);
11752 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11753 static rtx
11754 const_vector_from_tree (tree exp)
11756 rtvec v;
11757 unsigned i;
11758 int units;
11759 tree elt;
11760 machine_mode inner, mode;
11762 mode = TYPE_MODE (TREE_TYPE (exp));
11764 if (initializer_zerop (exp))
11765 return CONST0_RTX (mode);
11767 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11768 return const_vector_mask_from_tree (exp);
11770 units = GET_MODE_NUNITS (mode);
11771 inner = GET_MODE_INNER (mode);
11773 v = rtvec_alloc (units);
11775 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11777 elt = VECTOR_CST_ELT (exp, i);
11779 if (TREE_CODE (elt) == REAL_CST)
11780 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11781 inner);
11782 else if (TREE_CODE (elt) == FIXED_CST)
11783 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11784 inner);
11785 else
11786 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11789 return gen_rtx_CONST_VECTOR (mode, v);
11792 /* Build a decl for a personality function given a language prefix. */
11794 tree
11795 build_personality_function (const char *lang)
11797 const char *unwind_and_version;
11798 tree decl, type;
11799 char *name;
11801 switch (targetm_common.except_unwind_info (&global_options))
11803 case UI_NONE:
11804 return NULL;
11805 case UI_SJLJ:
11806 unwind_and_version = "_sj0";
11807 break;
11808 case UI_DWARF2:
11809 case UI_TARGET:
11810 unwind_and_version = "_v0";
11811 break;
11812 case UI_SEH:
11813 unwind_and_version = "_seh0";
11814 break;
11815 default:
11816 gcc_unreachable ();
11819 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11821 type = build_function_type_list (integer_type_node, integer_type_node,
11822 long_long_unsigned_type_node,
11823 ptr_type_node, ptr_type_node, NULL_TREE);
11824 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11825 get_identifier (name), type);
11826 DECL_ARTIFICIAL (decl) = 1;
11827 DECL_EXTERNAL (decl) = 1;
11828 TREE_PUBLIC (decl) = 1;
11830 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11831 are the flags assigned by targetm.encode_section_info. */
11832 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11834 return decl;
11837 /* Extracts the personality function of DECL and returns the corresponding
11838 libfunc. */
11841 get_personality_function (tree decl)
11843 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11844 enum eh_personality_kind pk;
11846 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11847 if (pk == eh_personality_none)
11848 return NULL;
11850 if (!personality
11851 && pk == eh_personality_any)
11852 personality = lang_hooks.eh_personality ();
11854 if (pk == eh_personality_lang)
11855 gcc_assert (personality != NULL_TREE);
11857 return XEXP (DECL_RTL (personality), 0);
11860 /* Returns a tree for the size of EXP in bytes. */
11862 static tree
11863 tree_expr_size (const_tree exp)
11865 if (DECL_P (exp)
11866 && DECL_SIZE_UNIT (exp) != 0)
11867 return DECL_SIZE_UNIT (exp);
11868 else
11869 return size_in_bytes (TREE_TYPE (exp));
11872 /* Return an rtx for the size in bytes of the value of EXP. */
11875 expr_size (tree exp)
11877 tree size;
11879 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11880 size = TREE_OPERAND (exp, 1);
11881 else
11883 size = tree_expr_size (exp);
11884 gcc_assert (size);
11885 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11888 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11891 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11892 if the size can vary or is larger than an integer. */
11894 static HOST_WIDE_INT
11895 int_expr_size (tree exp)
11897 tree size;
11899 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11900 size = TREE_OPERAND (exp, 1);
11901 else
11903 size = tree_expr_size (exp);
11904 gcc_assert (size);
11907 if (size == 0 || !tree_fits_shwi_p (size))
11908 return -1;
11910 return tree_to_shwi (size);