poly_int: PUSH_ROUNDING
[official-gcc.git] / gcc / expr.c
blob580ec4868c04da262534301f55e55f5f83d757fa
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"
64 #include "rtx-vector-builder.h"
67 /* If this is nonzero, we do not bother generating VOLATILE
68 around volatile memory references, and we are willing to
69 output indirect addresses. If cse is to follow, we reject
70 indirect addresses so a useful potential cse is generated;
71 if it is used only once, instruction combination will produce
72 the same indirect address eventually. */
73 int cse_not_expected;
75 static bool block_move_libcall_safe_for_call_parm (void);
76 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
78 unsigned HOST_WIDE_INT);
79 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
80 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
81 static rtx_insn *compress_float_constant (rtx, rtx);
82 static rtx get_subtarget (rtx);
83 static void store_constructor (tree, rtx, int, poly_int64, bool);
84 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
85 machine_mode, tree, alias_set_type, bool, bool);
87 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
89 static int is_aligning_offset (const_tree, const_tree);
90 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
91 static rtx do_store_flag (sepops, rtx, machine_mode);
92 #ifdef PUSH_ROUNDING
93 static void emit_single_push_insn (machine_mode, rtx, tree);
94 #endif
95 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
96 profile_probability);
97 static rtx const_vector_from_tree (tree);
98 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
99 static tree tree_expr_size (const_tree);
100 static HOST_WIDE_INT int_expr_size (tree);
101 static void convert_mode_scalar (rtx, rtx, int);
104 /* This is run to set up which modes can be used
105 directly in memory and to initialize the block move optab. It is run
106 at the beginning of compilation and when the target is reinitialized. */
108 void
109 init_expr_target (void)
111 rtx pat;
112 int num_clobbers;
113 rtx mem, mem1;
114 rtx reg;
116 /* Try indexing by frame ptr and try by stack ptr.
117 It is known that on the Convex the stack ptr isn't a valid index.
118 With luck, one or the other is valid on any machine. */
119 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
120 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
122 /* A scratch register we can modify in-place below to avoid
123 useless RTL allocations. */
124 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
126 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
127 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
128 PATTERN (insn) = pat;
130 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
131 mode = (machine_mode) ((int) mode + 1))
133 int regno;
135 direct_load[(int) mode] = direct_store[(int) mode] = 0;
136 PUT_MODE (mem, mode);
137 PUT_MODE (mem1, mode);
139 /* See if there is some register that can be used in this mode and
140 directly loaded or stored from memory. */
142 if (mode != VOIDmode && mode != BLKmode)
143 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
144 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
145 regno++)
147 if (!targetm.hard_regno_mode_ok (regno, mode))
148 continue;
150 set_mode_and_regno (reg, mode, regno);
152 SET_SRC (pat) = mem;
153 SET_DEST (pat) = reg;
154 if (recog (pat, insn, &num_clobbers) >= 0)
155 direct_load[(int) mode] = 1;
157 SET_SRC (pat) = mem1;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
162 SET_SRC (pat) = reg;
163 SET_DEST (pat) = mem;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_store[(int) mode] = 1;
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem1;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
174 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
176 opt_scalar_float_mode mode_iter;
177 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
179 scalar_float_mode mode = mode_iter.require ();
180 scalar_float_mode srcmode;
181 FOR_EACH_MODE_UNTIL (srcmode, mode)
183 enum insn_code ic;
185 ic = can_extend_p (mode, srcmode, 0);
186 if (ic == CODE_FOR_nothing)
187 continue;
189 PUT_MODE (mem, srcmode);
191 if (insn_operand_matches (ic, 1, mem))
192 float_extend_from_mem[mode][srcmode] = true;
197 /* This is run at the start of compiling a function. */
199 void
200 init_expr (void)
202 memset (&crtl->expr, 0, sizeof (crtl->expr));
205 /* Copy data from FROM to TO, where the machine modes are not the same.
206 Both modes may be integer, or both may be floating, or both may be
207 fixed-point.
208 UNSIGNEDP should be nonzero if FROM is an unsigned type.
209 This causes zero-extension instead of sign-extension. */
211 void
212 convert_move (rtx to, rtx from, int unsignedp)
214 machine_mode to_mode = GET_MODE (to);
215 machine_mode from_mode = GET_MODE (from);
217 gcc_assert (to_mode != BLKmode);
218 gcc_assert (from_mode != BLKmode);
220 /* If the source and destination are already the same, then there's
221 nothing to do. */
222 if (to == from)
223 return;
225 /* If FROM is a SUBREG that indicates that we have already done at least
226 the required extension, strip it. We don't handle such SUBREGs as
227 TO here. */
229 scalar_int_mode to_int_mode;
230 if (GET_CODE (from) == SUBREG
231 && SUBREG_PROMOTED_VAR_P (from)
232 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
233 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
234 >= GET_MODE_PRECISION (to_int_mode))
235 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
236 from = gen_lowpart (to_int_mode, from), from_mode = to_int_mode;
238 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
240 if (to_mode == from_mode
241 || (from_mode == VOIDmode && CONSTANT_P (from)))
243 emit_move_insn (to, from);
244 return;
247 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
249 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
251 if (VECTOR_MODE_P (to_mode))
252 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
253 else
254 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
256 emit_move_insn (to, from);
257 return;
260 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
262 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
263 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
264 return;
267 convert_mode_scalar (to, from, unsignedp);
270 /* Like convert_move, but deals only with scalar modes. */
272 static void
273 convert_mode_scalar (rtx to, rtx from, int unsignedp)
275 /* Both modes should be scalar types. */
276 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
277 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
278 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
279 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
280 enum insn_code code;
281 rtx libcall;
283 gcc_assert (to_real == from_real);
285 /* rtx code for making an equivalent value. */
286 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
287 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
289 if (to_real)
291 rtx value;
292 rtx_insn *insns;
293 convert_optab tab;
295 gcc_assert ((GET_MODE_PRECISION (from_mode)
296 != GET_MODE_PRECISION (to_mode))
297 || (DECIMAL_FLOAT_MODE_P (from_mode)
298 != DECIMAL_FLOAT_MODE_P (to_mode)));
300 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
301 /* Conversion between decimal float and binary float, same size. */
302 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
303 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
304 tab = sext_optab;
305 else
306 tab = trunc_optab;
308 /* Try converting directly if the insn is supported. */
310 code = convert_optab_handler (tab, to_mode, from_mode);
311 if (code != CODE_FOR_nothing)
313 emit_unop_insn (code, to, from,
314 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
315 return;
318 /* Otherwise use a libcall. */
319 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
321 /* Is this conversion implemented yet? */
322 gcc_assert (libcall);
324 start_sequence ();
325 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
326 from, from_mode);
327 insns = get_insns ();
328 end_sequence ();
329 emit_libcall_block (insns, to, value,
330 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
331 from)
332 : gen_rtx_FLOAT_EXTEND (to_mode, from));
333 return;
336 /* Handle pointer conversion. */ /* SPEE 900220. */
337 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
339 convert_optab ctab;
341 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
342 ctab = trunc_optab;
343 else if (unsignedp)
344 ctab = zext_optab;
345 else
346 ctab = sext_optab;
348 if (convert_optab_handler (ctab, to_mode, from_mode)
349 != CODE_FOR_nothing)
351 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
352 to, from, UNKNOWN);
353 return;
357 /* Targets are expected to provide conversion insns between PxImode and
358 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
359 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
361 scalar_int_mode full_mode
362 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
364 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
365 != CODE_FOR_nothing);
367 if (full_mode != from_mode)
368 from = convert_to_mode (full_mode, from, unsignedp);
369 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
370 to, from, UNKNOWN);
371 return;
373 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
375 rtx new_from;
376 scalar_int_mode full_mode
377 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
378 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
379 enum insn_code icode;
381 icode = convert_optab_handler (ctab, full_mode, from_mode);
382 gcc_assert (icode != CODE_FOR_nothing);
384 if (to_mode == full_mode)
386 emit_unop_insn (icode, to, from, UNKNOWN);
387 return;
390 new_from = gen_reg_rtx (full_mode);
391 emit_unop_insn (icode, new_from, from, UNKNOWN);
393 /* else proceed to integer conversions below. */
394 from_mode = full_mode;
395 from = new_from;
398 /* Make sure both are fixed-point modes or both are not. */
399 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
400 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
401 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
403 /* If we widen from_mode to to_mode and they are in the same class,
404 we won't saturate the result.
405 Otherwise, always saturate the result to play safe. */
406 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
407 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
408 expand_fixed_convert (to, from, 0, 0);
409 else
410 expand_fixed_convert (to, from, 0, 1);
411 return;
414 /* Now both modes are integers. */
416 /* Handle expanding beyond a word. */
417 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
418 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
420 rtx_insn *insns;
421 rtx lowpart;
422 rtx fill_value;
423 rtx lowfrom;
424 int i;
425 scalar_mode lowpart_mode;
426 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
428 /* Try converting directly if the insn is supported. */
429 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
430 != CODE_FOR_nothing)
432 /* If FROM is a SUBREG, put it into a register. Do this
433 so that we always generate the same set of insns for
434 better cse'ing; if an intermediate assignment occurred,
435 we won't be doing the operation directly on the SUBREG. */
436 if (optimize > 0 && GET_CODE (from) == SUBREG)
437 from = force_reg (from_mode, from);
438 emit_unop_insn (code, to, from, equiv_code);
439 return;
441 /* Next, try converting via full word. */
442 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
443 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
444 != CODE_FOR_nothing))
446 rtx word_to = gen_reg_rtx (word_mode);
447 if (REG_P (to))
449 if (reg_overlap_mentioned_p (to, from))
450 from = force_reg (from_mode, from);
451 emit_clobber (to);
453 convert_move (word_to, from, unsignedp);
454 emit_unop_insn (code, to, word_to, equiv_code);
455 return;
458 /* No special multiword conversion insn; do it by hand. */
459 start_sequence ();
461 /* Since we will turn this into a no conflict block, we must ensure
462 the source does not overlap the target so force it into an isolated
463 register when maybe so. Likewise for any MEM input, since the
464 conversion sequence might require several references to it and we
465 must ensure we're getting the same value every time. */
467 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
468 from = force_reg (from_mode, from);
470 /* Get a copy of FROM widened to a word, if necessary. */
471 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
472 lowpart_mode = word_mode;
473 else
474 lowpart_mode = from_mode;
476 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
478 lowpart = gen_lowpart (lowpart_mode, to);
479 emit_move_insn (lowpart, lowfrom);
481 /* Compute the value to put in each remaining word. */
482 if (unsignedp)
483 fill_value = const0_rtx;
484 else
485 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
486 LT, lowfrom, const0_rtx,
487 lowpart_mode, 0, -1);
489 /* Fill the remaining words. */
490 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
492 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
493 rtx subword = operand_subword (to, index, 1, to_mode);
495 gcc_assert (subword);
497 if (fill_value != subword)
498 emit_move_insn (subword, fill_value);
501 insns = get_insns ();
502 end_sequence ();
504 emit_insn (insns);
505 return;
508 /* Truncating multi-word to a word or less. */
509 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
510 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
512 if (!((MEM_P (from)
513 && ! MEM_VOLATILE_P (from)
514 && direct_load[(int) to_mode]
515 && ! mode_dependent_address_p (XEXP (from, 0),
516 MEM_ADDR_SPACE (from)))
517 || REG_P (from)
518 || GET_CODE (from) == SUBREG))
519 from = force_reg (from_mode, from);
520 convert_move (to, gen_lowpart (word_mode, from), 0);
521 return;
524 /* Now follow all the conversions between integers
525 no more than a word long. */
527 /* For truncation, usually we can just refer to FROM in a narrower mode. */
528 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
529 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
531 if (!((MEM_P (from)
532 && ! MEM_VOLATILE_P (from)
533 && direct_load[(int) to_mode]
534 && ! mode_dependent_address_p (XEXP (from, 0),
535 MEM_ADDR_SPACE (from)))
536 || REG_P (from)
537 || GET_CODE (from) == SUBREG))
538 from = force_reg (from_mode, from);
539 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
540 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
541 from = copy_to_reg (from);
542 emit_move_insn (to, gen_lowpart (to_mode, from));
543 return;
546 /* Handle extension. */
547 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
549 /* Convert directly if that works. */
550 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
551 != CODE_FOR_nothing)
553 emit_unop_insn (code, to, from, equiv_code);
554 return;
556 else
558 scalar_mode intermediate;
559 rtx tmp;
560 int shift_amount;
562 /* Search for a mode to convert via. */
563 opt_scalar_mode intermediate_iter;
564 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
566 scalar_mode intermediate = intermediate_iter.require ();
567 if (((can_extend_p (to_mode, intermediate, unsignedp)
568 != CODE_FOR_nothing)
569 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
570 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
571 intermediate)))
572 && (can_extend_p (intermediate, from_mode, unsignedp)
573 != CODE_FOR_nothing))
575 convert_move (to, convert_to_mode (intermediate, from,
576 unsignedp), unsignedp);
577 return;
581 /* No suitable intermediate mode.
582 Generate what we need with shifts. */
583 shift_amount = (GET_MODE_PRECISION (to_mode)
584 - GET_MODE_PRECISION (from_mode));
585 from = gen_lowpart (to_mode, force_reg (from_mode, from));
586 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
587 to, unsignedp);
588 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
589 to, unsignedp);
590 if (tmp != to)
591 emit_move_insn (to, tmp);
592 return;
596 /* Support special truncate insns for certain modes. */
597 if (convert_optab_handler (trunc_optab, to_mode,
598 from_mode) != CODE_FOR_nothing)
600 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
601 to, from, UNKNOWN);
602 return;
605 /* Handle truncation of volatile memrefs, and so on;
606 the things that couldn't be truncated directly,
607 and for which there was no special instruction.
609 ??? Code above formerly short-circuited this, for most integer
610 mode pairs, with a force_reg in from_mode followed by a recursive
611 call to this routine. Appears always to have been wrong. */
612 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
614 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
615 emit_move_insn (to, temp);
616 return;
619 /* Mode combination is not recognized. */
620 gcc_unreachable ();
623 /* Return an rtx for a value that would result
624 from converting X to mode MODE.
625 Both X and MODE may be floating, or both integer.
626 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. */
631 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
633 return convert_modes (mode, VOIDmode, x, unsignedp);
636 /* Return an rtx for a value that would result
637 from converting X from mode OLDMODE to mode MODE.
638 Both modes may be floating, or both integer.
639 UNSIGNEDP is nonzero if X is an unsigned value.
641 This can be done by referring to a part of X in place
642 or by copying to a new temporary with conversion.
644 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
647 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
649 rtx temp;
650 scalar_int_mode int_mode;
652 /* If FROM is a SUBREG that indicates that we have already done at least
653 the required extension, strip it. */
655 if (GET_CODE (x) == SUBREG
656 && SUBREG_PROMOTED_VAR_P (x)
657 && is_a <scalar_int_mode> (mode, &int_mode)
658 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
659 >= GET_MODE_PRECISION (int_mode))
660 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
661 x = gen_lowpart (int_mode, SUBREG_REG (x));
663 if (GET_MODE (x) != VOIDmode)
664 oldmode = GET_MODE (x);
666 if (mode == oldmode)
667 return x;
669 if (CONST_SCALAR_INT_P (x)
670 && is_int_mode (mode, &int_mode))
672 /* If the caller did not tell us the old mode, then there is not
673 much to do with respect to canonicalization. We have to
674 assume that all the bits are significant. */
675 if (GET_MODE_CLASS (oldmode) != MODE_INT)
676 oldmode = MAX_MODE_INT;
677 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
678 GET_MODE_PRECISION (int_mode),
679 unsignedp ? UNSIGNED : SIGNED);
680 return immed_wide_int_const (w, int_mode);
683 /* We can do this with a gen_lowpart if both desired and current modes
684 are integer, and this is either a constant integer, a register, or a
685 non-volatile MEM. */
686 scalar_int_mode int_oldmode;
687 if (is_int_mode (mode, &int_mode)
688 && is_int_mode (oldmode, &int_oldmode)
689 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
690 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
691 || CONST_POLY_INT_P (x)
692 || (REG_P (x)
693 && (!HARD_REGISTER_P (x)
694 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
695 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
696 return gen_lowpart (int_mode, x);
698 /* Converting from integer constant into mode is always equivalent to an
699 subreg operation. */
700 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
702 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
703 return simplify_gen_subreg (mode, x, oldmode, 0);
706 temp = gen_reg_rtx (mode);
707 convert_move (temp, x, unsignedp);
708 return temp;
711 /* Return the largest alignment we can use for doing a move (or store)
712 of MAX_PIECES. ALIGN is the largest alignment we could use. */
714 static unsigned int
715 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
717 scalar_int_mode tmode
718 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
720 if (align >= GET_MODE_ALIGNMENT (tmode))
721 align = GET_MODE_ALIGNMENT (tmode);
722 else
724 scalar_int_mode xmode = NARROWEST_INT_MODE;
725 opt_scalar_int_mode mode_iter;
726 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
728 tmode = mode_iter.require ();
729 if (GET_MODE_SIZE (tmode) > max_pieces
730 || targetm.slow_unaligned_access (tmode, align))
731 break;
732 xmode = tmode;
735 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
738 return align;
741 /* Return the widest integer mode that is narrower than SIZE bytes. */
743 static scalar_int_mode
744 widest_int_mode_for_size (unsigned int size)
746 scalar_int_mode result = NARROWEST_INT_MODE;
748 gcc_checking_assert (size > 1);
750 opt_scalar_int_mode tmode;
751 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
752 if (GET_MODE_SIZE (tmode.require ()) < size)
753 result = tmode.require ();
755 return result;
758 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
759 and should be performed piecewise. */
761 static bool
762 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
763 enum by_pieces_operation op)
765 return targetm.use_by_pieces_infrastructure_p (len, align, op,
766 optimize_insn_for_speed_p ());
769 /* Determine whether the LEN bytes can be moved by using several move
770 instructions. Return nonzero if a call to move_by_pieces should
771 succeed. */
773 bool
774 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
776 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
779 /* Return number of insns required to perform operation OP by pieces
780 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
782 unsigned HOST_WIDE_INT
783 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
784 unsigned int max_size, by_pieces_operation op)
786 unsigned HOST_WIDE_INT n_insns = 0;
788 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
790 while (max_size > 1 && l > 0)
792 scalar_int_mode mode = widest_int_mode_for_size (max_size);
793 enum insn_code icode;
795 unsigned int modesize = GET_MODE_SIZE (mode);
797 icode = optab_handler (mov_optab, mode);
798 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
800 unsigned HOST_WIDE_INT n_pieces = l / modesize;
801 l %= modesize;
802 switch (op)
804 default:
805 n_insns += n_pieces;
806 break;
808 case COMPARE_BY_PIECES:
809 int batch = targetm.compare_by_pieces_branch_ratio (mode);
810 int batch_ops = 4 * batch - 1;
811 unsigned HOST_WIDE_INT full = n_pieces / batch;
812 n_insns += full * batch_ops;
813 if (n_pieces % batch != 0)
814 n_insns++;
815 break;
819 max_size = modesize;
822 gcc_assert (!l);
823 return n_insns;
826 /* Used when performing piecewise block operations, holds information
827 about one of the memory objects involved. The member functions
828 can be used to generate code for loading from the object and
829 updating the address when iterating. */
831 class pieces_addr
833 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
834 stack pushes. */
835 rtx m_obj;
836 /* The address of the object. Can differ from that seen in the
837 MEM rtx if we copied the address to a register. */
838 rtx m_addr;
839 /* Nonzero if the address on the object has an autoincrement already,
840 signifies whether that was an increment or decrement. */
841 signed char m_addr_inc;
842 /* Nonzero if we intend to use autoinc without the address already
843 having autoinc form. We will insert add insns around each memory
844 reference, expecting later passes to form autoinc addressing modes.
845 The only supported options are predecrement and postincrement. */
846 signed char m_explicit_inc;
847 /* True if we have either of the two possible cases of using
848 autoincrement. */
849 bool m_auto;
850 /* True if this is an address to be used for load operations rather
851 than stores. */
852 bool m_is_load;
854 /* Optionally, a function to obtain constants for any given offset into
855 the objects, and data associated with it. */
856 by_pieces_constfn m_constfn;
857 void *m_cfndata;
858 public:
859 pieces_addr (rtx, bool, by_pieces_constfn, void *);
860 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
861 void increment_address (HOST_WIDE_INT);
862 void maybe_predec (HOST_WIDE_INT);
863 void maybe_postinc (HOST_WIDE_INT);
864 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
865 int get_addr_inc ()
867 return m_addr_inc;
871 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
872 true if the operation to be performed on this object is a load
873 rather than a store. For stores, OBJ can be NULL, in which case we
874 assume the operation is a stack push. For loads, the optional
875 CONSTFN and its associated CFNDATA can be used in place of the
876 memory load. */
878 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
879 void *cfndata)
880 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
882 m_addr_inc = 0;
883 m_auto = false;
884 if (obj)
886 rtx addr = XEXP (obj, 0);
887 rtx_code code = GET_CODE (addr);
888 m_addr = addr;
889 bool dec = code == PRE_DEC || code == POST_DEC;
890 bool inc = code == PRE_INC || code == POST_INC;
891 m_auto = inc || dec;
892 if (m_auto)
893 m_addr_inc = dec ? -1 : 1;
895 /* While we have always looked for these codes here, the code
896 implementing the memory operation has never handled them.
897 Support could be added later if necessary or beneficial. */
898 gcc_assert (code != PRE_INC && code != POST_DEC);
900 else
902 m_addr = NULL_RTX;
903 if (!is_load)
905 m_auto = true;
906 if (STACK_GROWS_DOWNWARD)
907 m_addr_inc = -1;
908 else
909 m_addr_inc = 1;
911 else
912 gcc_assert (constfn != NULL);
914 m_explicit_inc = 0;
915 if (constfn)
916 gcc_assert (is_load);
919 /* Decide whether to use autoinc for an address involved in a memory op.
920 MODE is the mode of the accesses, REVERSE is true if we've decided to
921 perform the operation starting from the end, and LEN is the length of
922 the operation. Don't override an earlier decision to set m_auto. */
924 void
925 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
926 HOST_WIDE_INT len)
928 if (m_auto || m_obj == NULL_RTX)
929 return;
931 bool use_predec = (m_is_load
932 ? USE_LOAD_PRE_DECREMENT (mode)
933 : USE_STORE_PRE_DECREMENT (mode));
934 bool use_postinc = (m_is_load
935 ? USE_LOAD_POST_INCREMENT (mode)
936 : USE_STORE_POST_INCREMENT (mode));
937 machine_mode addr_mode = get_address_mode (m_obj);
939 if (use_predec && reverse)
941 m_addr = copy_to_mode_reg (addr_mode,
942 plus_constant (addr_mode,
943 m_addr, len));
944 m_auto = true;
945 m_explicit_inc = -1;
947 else if (use_postinc && !reverse)
949 m_addr = copy_to_mode_reg (addr_mode, m_addr);
950 m_auto = true;
951 m_explicit_inc = 1;
953 else if (CONSTANT_P (m_addr))
954 m_addr = copy_to_mode_reg (addr_mode, m_addr);
957 /* Adjust the address to refer to the data at OFFSET in MODE. If we
958 are using autoincrement for this address, we don't add the offset,
959 but we still modify the MEM's properties. */
962 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
964 if (m_constfn)
965 return m_constfn (m_cfndata, offset, mode);
966 if (m_obj == NULL_RTX)
967 return NULL_RTX;
968 if (m_auto)
969 return adjust_automodify_address (m_obj, mode, m_addr, offset);
970 else
971 return adjust_address (m_obj, mode, offset);
974 /* Emit an add instruction to increment the address by SIZE. */
976 void
977 pieces_addr::increment_address (HOST_WIDE_INT size)
979 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
980 emit_insn (gen_add2_insn (m_addr, amount));
983 /* If we are supposed to decrement the address after each access, emit code
984 to do so now. Increment by SIZE (which has should have the correct sign
985 already). */
987 void
988 pieces_addr::maybe_predec (HOST_WIDE_INT size)
990 if (m_explicit_inc >= 0)
991 return;
992 gcc_assert (HAVE_PRE_DECREMENT);
993 increment_address (size);
996 /* If we are supposed to decrement the address after each access, emit code
997 to do so now. Increment by SIZE. */
999 void
1000 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1002 if (m_explicit_inc <= 0)
1003 return;
1004 gcc_assert (HAVE_POST_INCREMENT);
1005 increment_address (size);
1008 /* This structure is used by do_op_by_pieces to describe the operation
1009 to be performed. */
1011 class op_by_pieces_d
1013 protected:
1014 pieces_addr m_to, m_from;
1015 unsigned HOST_WIDE_INT m_len;
1016 HOST_WIDE_INT m_offset;
1017 unsigned int m_align;
1018 unsigned int m_max_size;
1019 bool m_reverse;
1021 /* Virtual functions, overriden by derived classes for the specific
1022 operation. */
1023 virtual void generate (rtx, rtx, machine_mode) = 0;
1024 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1025 virtual void finish_mode (machine_mode)
1029 public:
1030 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1031 unsigned HOST_WIDE_INT, unsigned int);
1032 void run ();
1035 /* The constructor for an op_by_pieces_d structure. We require two
1036 objects named TO and FROM, which are identified as loads or stores
1037 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1038 and its associated FROM_CFN_DATA can be used to replace loads with
1039 constant values. LEN describes the length of the operation. */
1041 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1042 rtx from, bool from_load,
1043 by_pieces_constfn from_cfn,
1044 void *from_cfn_data,
1045 unsigned HOST_WIDE_INT len,
1046 unsigned int align)
1047 : m_to (to, to_load, NULL, NULL),
1048 m_from (from, from_load, from_cfn, from_cfn_data),
1049 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1051 int toi = m_to.get_addr_inc ();
1052 int fromi = m_from.get_addr_inc ();
1053 if (toi >= 0 && fromi >= 0)
1054 m_reverse = false;
1055 else if (toi <= 0 && fromi <= 0)
1056 m_reverse = true;
1057 else
1058 gcc_unreachable ();
1060 m_offset = m_reverse ? len : 0;
1061 align = MIN (to ? MEM_ALIGN (to) : align,
1062 from ? MEM_ALIGN (from) : align);
1064 /* If copying requires more than two move insns,
1065 copy addresses to registers (to make displacements shorter)
1066 and use post-increment if available. */
1067 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1069 /* Find the mode of the largest comparison. */
1070 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1072 m_from.decide_autoinc (mode, m_reverse, len);
1073 m_to.decide_autoinc (mode, m_reverse, len);
1076 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1077 m_align = align;
1080 /* This function contains the main loop used for expanding a block
1081 operation. First move what we can in the largest integer mode,
1082 then go to successively smaller modes. For every access, call
1083 GENFUN with the two operands and the EXTRA_DATA. */
1085 void
1086 op_by_pieces_d::run ()
1088 while (m_max_size > 1 && m_len > 0)
1090 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1092 if (prepare_mode (mode, m_align))
1094 unsigned int size = GET_MODE_SIZE (mode);
1095 rtx to1 = NULL_RTX, from1;
1097 while (m_len >= size)
1099 if (m_reverse)
1100 m_offset -= size;
1102 to1 = m_to.adjust (mode, m_offset);
1103 from1 = m_from.adjust (mode, m_offset);
1105 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1106 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1108 generate (to1, from1, mode);
1110 m_to.maybe_postinc (size);
1111 m_from.maybe_postinc (size);
1113 if (!m_reverse)
1114 m_offset += size;
1116 m_len -= size;
1119 finish_mode (mode);
1122 m_max_size = GET_MODE_SIZE (mode);
1125 /* The code above should have handled everything. */
1126 gcc_assert (!m_len);
1129 /* Derived class from op_by_pieces_d, providing support for block move
1130 operations. */
1132 class move_by_pieces_d : public op_by_pieces_d
1134 insn_gen_fn m_gen_fun;
1135 void generate (rtx, rtx, machine_mode);
1136 bool prepare_mode (machine_mode, unsigned int);
1138 public:
1139 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1140 unsigned int align)
1141 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1144 rtx finish_endp (int);
1147 /* Return true if MODE can be used for a set of copies, given an
1148 alignment ALIGN. Prepare whatever data is necessary for later
1149 calls to generate. */
1151 bool
1152 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1154 insn_code icode = optab_handler (mov_optab, mode);
1155 m_gen_fun = GEN_FCN (icode);
1156 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1159 /* A callback used when iterating for a compare_by_pieces_operation.
1160 OP0 and OP1 are the values that have been loaded and should be
1161 compared in MODE. If OP0 is NULL, this means we should generate a
1162 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1163 gen function that should be used to generate the mode. */
1165 void
1166 move_by_pieces_d::generate (rtx op0, rtx op1,
1167 machine_mode mode ATTRIBUTE_UNUSED)
1169 #ifdef PUSH_ROUNDING
1170 if (op0 == NULL_RTX)
1172 emit_single_push_insn (mode, op1, NULL);
1173 return;
1175 #endif
1176 emit_insn (m_gen_fun (op0, op1));
1179 /* Perform the final adjustment at the end of a string to obtain the
1180 correct return value for the block operation. If ENDP is 1 return
1181 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1182 end minus one byte ala stpcpy. */
1185 move_by_pieces_d::finish_endp (int endp)
1187 gcc_assert (!m_reverse);
1188 if (endp == 2)
1190 m_to.maybe_postinc (-1);
1191 --m_offset;
1193 return m_to.adjust (QImode, m_offset);
1196 /* Generate several move instructions to copy LEN bytes from block FROM to
1197 block TO. (These are MEM rtx's with BLKmode).
1199 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1200 used to push FROM to the stack.
1202 ALIGN is maximum stack alignment we can assume.
1204 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1205 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1206 stpcpy. */
1209 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1210 unsigned int align, int endp)
1212 #ifndef PUSH_ROUNDING
1213 if (to == NULL)
1214 gcc_unreachable ();
1215 #endif
1217 move_by_pieces_d data (to, from, len, align);
1219 data.run ();
1221 if (endp)
1222 return data.finish_endp (endp);
1223 else
1224 return to;
1227 /* Derived class from op_by_pieces_d, providing support for block move
1228 operations. */
1230 class store_by_pieces_d : public op_by_pieces_d
1232 insn_gen_fn m_gen_fun;
1233 void generate (rtx, rtx, machine_mode);
1234 bool prepare_mode (machine_mode, unsigned int);
1236 public:
1237 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1238 unsigned HOST_WIDE_INT len, unsigned int align)
1239 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1242 rtx finish_endp (int);
1245 /* Return true if MODE can be used for a set of stores, given an
1246 alignment ALIGN. Prepare whatever data is necessary for later
1247 calls to generate. */
1249 bool
1250 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1252 insn_code icode = optab_handler (mov_optab, mode);
1253 m_gen_fun = GEN_FCN (icode);
1254 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1257 /* A callback used when iterating for a store_by_pieces_operation.
1258 OP0 and OP1 are the values that have been loaded and should be
1259 compared in MODE. If OP0 is NULL, this means we should generate a
1260 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1261 gen function that should be used to generate the mode. */
1263 void
1264 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1266 emit_insn (m_gen_fun (op0, op1));
1269 /* Perform the final adjustment at the end of a string to obtain the
1270 correct return value for the block operation. If ENDP is 1 return
1271 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1272 end minus one byte ala stpcpy. */
1275 store_by_pieces_d::finish_endp (int endp)
1277 gcc_assert (!m_reverse);
1278 if (endp == 2)
1280 m_to.maybe_postinc (-1);
1281 --m_offset;
1283 return m_to.adjust (QImode, m_offset);
1286 /* Determine whether the LEN bytes generated by CONSTFUN can be
1287 stored to memory using several move instructions. CONSTFUNDATA is
1288 a pointer which will be passed as argument in every CONSTFUN call.
1289 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1290 a memset operation and false if it's a copy of a constant string.
1291 Return nonzero if a call to store_by_pieces should succeed. */
1294 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1295 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1296 void *constfundata, unsigned int align, bool memsetp)
1298 unsigned HOST_WIDE_INT l;
1299 unsigned int max_size;
1300 HOST_WIDE_INT offset = 0;
1301 enum insn_code icode;
1302 int reverse;
1303 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1304 rtx cst ATTRIBUTE_UNUSED;
1306 if (len == 0)
1307 return 1;
1309 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1310 memsetp
1311 ? SET_BY_PIECES
1312 : STORE_BY_PIECES,
1313 optimize_insn_for_speed_p ()))
1314 return 0;
1316 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1318 /* We would first store what we can in the largest integer mode, then go to
1319 successively smaller modes. */
1321 for (reverse = 0;
1322 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1323 reverse++)
1325 l = len;
1326 max_size = STORE_MAX_PIECES + 1;
1327 while (max_size > 1 && l > 0)
1329 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1331 icode = optab_handler (mov_optab, mode);
1332 if (icode != CODE_FOR_nothing
1333 && align >= GET_MODE_ALIGNMENT (mode))
1335 unsigned int size = GET_MODE_SIZE (mode);
1337 while (l >= size)
1339 if (reverse)
1340 offset -= size;
1342 cst = (*constfun) (constfundata, offset, mode);
1343 if (!targetm.legitimate_constant_p (mode, cst))
1344 return 0;
1346 if (!reverse)
1347 offset += size;
1349 l -= size;
1353 max_size = GET_MODE_SIZE (mode);
1356 /* The code above should have handled everything. */
1357 gcc_assert (!l);
1360 return 1;
1363 /* Generate several move instructions to store LEN bytes generated by
1364 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1365 pointer which will be passed as argument in every CONSTFUN call.
1366 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1367 a memset operation and false if it's a copy of a constant string.
1368 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1369 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1370 stpcpy. */
1373 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1374 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1375 void *constfundata, unsigned int align, bool memsetp, int endp)
1377 if (len == 0)
1379 gcc_assert (endp != 2);
1380 return to;
1383 gcc_assert (targetm.use_by_pieces_infrastructure_p
1384 (len, align,
1385 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1386 optimize_insn_for_speed_p ()));
1388 store_by_pieces_d data (to, constfun, constfundata, len, align);
1389 data.run ();
1391 if (endp)
1392 return data.finish_endp (endp);
1393 else
1394 return to;
1397 /* Callback routine for clear_by_pieces.
1398 Return const0_rtx unconditionally. */
1400 static rtx
1401 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1403 return const0_rtx;
1406 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1407 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1409 static void
1410 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1412 if (len == 0)
1413 return;
1415 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1416 data.run ();
1419 /* Context used by compare_by_pieces_genfn. It stores the fail label
1420 to jump to in case of miscomparison, and for branch ratios greater than 1,
1421 it stores an accumulator and the current and maximum counts before
1422 emitting another branch. */
1424 class compare_by_pieces_d : public op_by_pieces_d
1426 rtx_code_label *m_fail_label;
1427 rtx m_accumulator;
1428 int m_count, m_batch;
1430 void generate (rtx, rtx, machine_mode);
1431 bool prepare_mode (machine_mode, unsigned int);
1432 void finish_mode (machine_mode);
1433 public:
1434 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1435 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1436 rtx_code_label *fail_label)
1437 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1439 m_fail_label = fail_label;
1443 /* A callback used when iterating for a compare_by_pieces_operation.
1444 OP0 and OP1 are the values that have been loaded and should be
1445 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1446 context structure. */
1448 void
1449 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1451 if (m_batch > 1)
1453 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1454 true, OPTAB_LIB_WIDEN);
1455 if (m_count != 0)
1456 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1457 true, OPTAB_LIB_WIDEN);
1458 m_accumulator = temp;
1460 if (++m_count < m_batch)
1461 return;
1463 m_count = 0;
1464 op0 = m_accumulator;
1465 op1 = const0_rtx;
1466 m_accumulator = NULL_RTX;
1468 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1469 m_fail_label, profile_probability::uninitialized ());
1472 /* Return true if MODE can be used for a set of moves and comparisons,
1473 given an alignment ALIGN. Prepare whatever data is necessary for
1474 later calls to generate. */
1476 bool
1477 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1479 insn_code icode = optab_handler (mov_optab, mode);
1480 if (icode == CODE_FOR_nothing
1481 || align < GET_MODE_ALIGNMENT (mode)
1482 || !can_compare_p (EQ, mode, ccp_jump))
1483 return false;
1484 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1485 if (m_batch < 0)
1486 return false;
1487 m_accumulator = NULL_RTX;
1488 m_count = 0;
1489 return true;
1492 /* Called after expanding a series of comparisons in MODE. If we have
1493 accumulated results for which we haven't emitted a branch yet, do
1494 so now. */
1496 void
1497 compare_by_pieces_d::finish_mode (machine_mode mode)
1499 if (m_accumulator != NULL_RTX)
1500 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1501 NULL_RTX, NULL, m_fail_label,
1502 profile_probability::uninitialized ());
1505 /* Generate several move instructions to compare LEN bytes from blocks
1506 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1508 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1509 used to push FROM to the stack.
1511 ALIGN is maximum stack alignment we can assume.
1513 Optionally, the caller can pass a constfn and associated data in A1_CFN
1514 and A1_CFN_DATA. describing that the second operand being compared is a
1515 known constant and how to obtain its data. */
1517 static rtx
1518 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1519 rtx target, unsigned int align,
1520 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1522 rtx_code_label *fail_label = gen_label_rtx ();
1523 rtx_code_label *end_label = gen_label_rtx ();
1525 if (target == NULL_RTX
1526 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1527 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1529 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1530 fail_label);
1532 data.run ();
1534 emit_move_insn (target, const0_rtx);
1535 emit_jump (end_label);
1536 emit_barrier ();
1537 emit_label (fail_label);
1538 emit_move_insn (target, const1_rtx);
1539 emit_label (end_label);
1541 return target;
1544 /* Emit code to move a block Y to a block X. This may be done with
1545 string-move instructions, with multiple scalar move instructions,
1546 or with a library call.
1548 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1549 SIZE is an rtx that says how long they are.
1550 ALIGN is the maximum alignment we can assume they have.
1551 METHOD describes what kind of copy this is, and what mechanisms may be used.
1552 MIN_SIZE is the minimal size of block to move
1553 MAX_SIZE is the maximal size of block to move, if it can not be represented
1554 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1556 Return the address of the new block, if memcpy is called and returns it,
1557 0 otherwise. */
1560 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1561 unsigned int expected_align, HOST_WIDE_INT expected_size,
1562 unsigned HOST_WIDE_INT min_size,
1563 unsigned HOST_WIDE_INT max_size,
1564 unsigned HOST_WIDE_INT probable_max_size)
1566 bool may_use_call;
1567 rtx retval = 0;
1568 unsigned int align;
1570 gcc_assert (size);
1571 if (CONST_INT_P (size) && INTVAL (size) == 0)
1572 return 0;
1574 switch (method)
1576 case BLOCK_OP_NORMAL:
1577 case BLOCK_OP_TAILCALL:
1578 may_use_call = true;
1579 break;
1581 case BLOCK_OP_CALL_PARM:
1582 may_use_call = block_move_libcall_safe_for_call_parm ();
1584 /* Make inhibit_defer_pop nonzero around the library call
1585 to force it to pop the arguments right away. */
1586 NO_DEFER_POP;
1587 break;
1589 case BLOCK_OP_NO_LIBCALL:
1590 may_use_call = false;
1591 break;
1593 default:
1594 gcc_unreachable ();
1597 gcc_assert (MEM_P (x) && MEM_P (y));
1598 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1599 gcc_assert (align >= BITS_PER_UNIT);
1601 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1602 block copy is more efficient for other large modes, e.g. DCmode. */
1603 x = adjust_address (x, BLKmode, 0);
1604 y = adjust_address (y, BLKmode, 0);
1606 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1607 can be incorrect is coming from __builtin_memcpy. */
1608 if (CONST_INT_P (size))
1610 x = shallow_copy_rtx (x);
1611 y = shallow_copy_rtx (y);
1612 set_mem_size (x, INTVAL (size));
1613 set_mem_size (y, INTVAL (size));
1616 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1617 move_by_pieces (x, y, INTVAL (size), align, 0);
1618 else if (emit_block_move_via_movmem (x, y, size, align,
1619 expected_align, expected_size,
1620 min_size, max_size, probable_max_size))
1622 else if (may_use_call
1623 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1624 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1626 /* Since x and y are passed to a libcall, mark the corresponding
1627 tree EXPR as addressable. */
1628 tree y_expr = MEM_EXPR (y);
1629 tree x_expr = MEM_EXPR (x);
1630 if (y_expr)
1631 mark_addressable (y_expr);
1632 if (x_expr)
1633 mark_addressable (x_expr);
1634 retval = emit_block_copy_via_libcall (x, y, size,
1635 method == BLOCK_OP_TAILCALL);
1638 else
1639 emit_block_move_via_loop (x, y, size, align);
1641 if (method == BLOCK_OP_CALL_PARM)
1642 OK_DEFER_POP;
1644 return retval;
1648 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1650 unsigned HOST_WIDE_INT max, min = 0;
1651 if (GET_CODE (size) == CONST_INT)
1652 min = max = UINTVAL (size);
1653 else
1654 max = GET_MODE_MASK (GET_MODE (size));
1655 return emit_block_move_hints (x, y, size, method, 0, -1,
1656 min, max, max);
1659 /* A subroutine of emit_block_move. Returns true if calling the
1660 block move libcall will not clobber any parameters which may have
1661 already been placed on the stack. */
1663 static bool
1664 block_move_libcall_safe_for_call_parm (void)
1666 #if defined (REG_PARM_STACK_SPACE)
1667 tree fn;
1668 #endif
1670 /* If arguments are pushed on the stack, then they're safe. */
1671 if (PUSH_ARGS)
1672 return true;
1674 /* If registers go on the stack anyway, any argument is sure to clobber
1675 an outgoing argument. */
1676 #if defined (REG_PARM_STACK_SPACE)
1677 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1678 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1679 depend on its argument. */
1680 (void) fn;
1681 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1682 && REG_PARM_STACK_SPACE (fn) != 0)
1683 return false;
1684 #endif
1686 /* If any argument goes in memory, then it might clobber an outgoing
1687 argument. */
1689 CUMULATIVE_ARGS args_so_far_v;
1690 cumulative_args_t args_so_far;
1691 tree fn, arg;
1693 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1694 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1695 args_so_far = pack_cumulative_args (&args_so_far_v);
1697 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1698 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1700 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1701 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1702 NULL_TREE, true);
1703 if (!tmp || !REG_P (tmp))
1704 return false;
1705 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1706 return false;
1707 targetm.calls.function_arg_advance (args_so_far, mode,
1708 NULL_TREE, true);
1711 return true;
1714 /* A subroutine of emit_block_move. Expand a movmem pattern;
1715 return true if successful. */
1717 static bool
1718 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1719 unsigned int expected_align, HOST_WIDE_INT expected_size,
1720 unsigned HOST_WIDE_INT min_size,
1721 unsigned HOST_WIDE_INT max_size,
1722 unsigned HOST_WIDE_INT probable_max_size)
1724 int save_volatile_ok = volatile_ok;
1726 if (expected_align < align)
1727 expected_align = align;
1728 if (expected_size != -1)
1730 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1731 expected_size = probable_max_size;
1732 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1733 expected_size = min_size;
1736 /* Since this is a move insn, we don't care about volatility. */
1737 volatile_ok = 1;
1739 /* Try the most limited insn first, because there's no point
1740 including more than one in the machine description unless
1741 the more limited one has some advantage. */
1743 opt_scalar_int_mode mode_iter;
1744 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1746 scalar_int_mode mode = mode_iter.require ();
1747 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1749 if (code != CODE_FOR_nothing
1750 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1751 here because if SIZE is less than the mode mask, as it is
1752 returned by the macro, it will definitely be less than the
1753 actual mode mask. Since SIZE is within the Pmode address
1754 space, we limit MODE to Pmode. */
1755 && ((CONST_INT_P (size)
1756 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1757 <= (GET_MODE_MASK (mode) >> 1)))
1758 || max_size <= (GET_MODE_MASK (mode) >> 1)
1759 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1761 struct expand_operand ops[9];
1762 unsigned int nops;
1764 /* ??? When called via emit_block_move_for_call, it'd be
1765 nice if there were some way to inform the backend, so
1766 that it doesn't fail the expansion because it thinks
1767 emitting the libcall would be more efficient. */
1768 nops = insn_data[(int) code].n_generator_args;
1769 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1771 create_fixed_operand (&ops[0], x);
1772 create_fixed_operand (&ops[1], y);
1773 /* The check above guarantees that this size conversion is valid. */
1774 create_convert_operand_to (&ops[2], size, mode, true);
1775 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1776 if (nops >= 6)
1778 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1779 create_integer_operand (&ops[5], expected_size);
1781 if (nops >= 8)
1783 create_integer_operand (&ops[6], min_size);
1784 /* If we can not represent the maximal size,
1785 make parameter NULL. */
1786 if ((HOST_WIDE_INT) max_size != -1)
1787 create_integer_operand (&ops[7], max_size);
1788 else
1789 create_fixed_operand (&ops[7], NULL);
1791 if (nops == 9)
1793 /* If we can not represent the maximal size,
1794 make parameter NULL. */
1795 if ((HOST_WIDE_INT) probable_max_size != -1)
1796 create_integer_operand (&ops[8], probable_max_size);
1797 else
1798 create_fixed_operand (&ops[8], NULL);
1800 if (maybe_expand_insn (code, nops, ops))
1802 volatile_ok = save_volatile_ok;
1803 return true;
1808 volatile_ok = save_volatile_ok;
1809 return false;
1812 /* A subroutine of emit_block_move. Copy the data via an explicit
1813 loop. This is used only when libcalls are forbidden. */
1814 /* ??? It'd be nice to copy in hunks larger than QImode. */
1816 static void
1817 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1818 unsigned int align ATTRIBUTE_UNUSED)
1820 rtx_code_label *cmp_label, *top_label;
1821 rtx iter, x_addr, y_addr, tmp;
1822 machine_mode x_addr_mode = get_address_mode (x);
1823 machine_mode y_addr_mode = get_address_mode (y);
1824 machine_mode iter_mode;
1826 iter_mode = GET_MODE (size);
1827 if (iter_mode == VOIDmode)
1828 iter_mode = word_mode;
1830 top_label = gen_label_rtx ();
1831 cmp_label = gen_label_rtx ();
1832 iter = gen_reg_rtx (iter_mode);
1834 emit_move_insn (iter, const0_rtx);
1836 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1837 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1838 do_pending_stack_adjust ();
1840 emit_jump (cmp_label);
1841 emit_label (top_label);
1843 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1844 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1846 if (x_addr_mode != y_addr_mode)
1847 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1848 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1850 x = change_address (x, QImode, x_addr);
1851 y = change_address (y, QImode, y_addr);
1853 emit_move_insn (x, y);
1855 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1856 true, OPTAB_LIB_WIDEN);
1857 if (tmp != iter)
1858 emit_move_insn (iter, tmp);
1860 emit_label (cmp_label);
1862 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1863 true, top_label,
1864 profile_probability::guessed_always ()
1865 .apply_scale (9, 10));
1868 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1869 TAILCALL is true if this is a tail call. */
1872 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1873 rtx size, bool tailcall)
1875 rtx dst_addr, src_addr;
1876 tree call_expr, dst_tree, src_tree, size_tree;
1877 machine_mode size_mode;
1879 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1880 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1881 dst_tree = make_tree (ptr_type_node, dst_addr);
1883 src_addr = copy_addr_to_reg (XEXP (src, 0));
1884 src_addr = convert_memory_address (ptr_mode, src_addr);
1885 src_tree = make_tree (ptr_type_node, src_addr);
1887 size_mode = TYPE_MODE (sizetype);
1888 size = convert_to_mode (size_mode, size, 1);
1889 size = copy_to_mode_reg (size_mode, size);
1890 size_tree = make_tree (sizetype, size);
1892 /* It is incorrect to use the libcall calling conventions for calls to
1893 memcpy/memmove/memcmp because they can be provided by the user. */
1894 tree fn = builtin_decl_implicit (fncode);
1895 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1896 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1898 return expand_call (call_expr, NULL_RTX, false);
1901 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1902 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1903 otherwise return null. */
1906 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1907 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1908 HOST_WIDE_INT align)
1910 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1912 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1913 target = NULL_RTX;
1915 struct expand_operand ops[5];
1916 create_output_operand (&ops[0], target, insn_mode);
1917 create_fixed_operand (&ops[1], arg1_rtx);
1918 create_fixed_operand (&ops[2], arg2_rtx);
1919 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1920 TYPE_UNSIGNED (arg3_type));
1921 create_integer_operand (&ops[4], align);
1922 if (maybe_expand_insn (icode, 5, ops))
1923 return ops[0].value;
1924 return NULL_RTX;
1927 /* Expand a block compare between X and Y with length LEN using the
1928 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1929 of the expression that was used to calculate the length. ALIGN
1930 gives the known minimum common alignment. */
1932 static rtx
1933 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1934 unsigned align)
1936 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1937 implementing memcmp because it will stop if it encounters two
1938 zero bytes. */
1939 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1941 if (icode == CODE_FOR_nothing)
1942 return NULL_RTX;
1944 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1947 /* Emit code to compare a block Y to a block X. This may be done with
1948 string-compare instructions, with multiple scalar instructions,
1949 or with a library call.
1951 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1952 they are. LEN_TYPE is the type of the expression that was used to
1953 calculate it.
1955 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1956 value of a normal memcmp call, instead we can just compare for equality.
1957 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1958 returning NULL_RTX.
1960 Optionally, the caller can pass a constfn and associated data in Y_CFN
1961 and Y_CFN_DATA. describing that the second operand being compared is a
1962 known constant and how to obtain its data.
1963 Return the result of the comparison, or NULL_RTX if we failed to
1964 perform the operation. */
1967 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1968 bool equality_only, by_pieces_constfn y_cfn,
1969 void *y_cfndata)
1971 rtx result = 0;
1973 if (CONST_INT_P (len) && INTVAL (len) == 0)
1974 return const0_rtx;
1976 gcc_assert (MEM_P (x) && MEM_P (y));
1977 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1978 gcc_assert (align >= BITS_PER_UNIT);
1980 x = adjust_address (x, BLKmode, 0);
1981 y = adjust_address (y, BLKmode, 0);
1983 if (equality_only
1984 && CONST_INT_P (len)
1985 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1986 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1987 y_cfn, y_cfndata);
1988 else
1989 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1991 return result;
1994 /* Copy all or part of a value X into registers starting at REGNO.
1995 The number of registers to be filled is NREGS. */
1997 void
1998 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2000 if (nregs == 0)
2001 return;
2003 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2004 x = validize_mem (force_const_mem (mode, x));
2006 /* See if the machine can do this with a load multiple insn. */
2007 if (targetm.have_load_multiple ())
2009 rtx_insn *last = get_last_insn ();
2010 rtx first = gen_rtx_REG (word_mode, regno);
2011 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2012 GEN_INT (nregs)))
2014 emit_insn (pat);
2015 return;
2017 else
2018 delete_insns_since (last);
2021 for (int i = 0; i < nregs; i++)
2022 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2023 operand_subword_force (x, i, mode));
2026 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2027 The number of registers to be filled is NREGS. */
2029 void
2030 move_block_from_reg (int regno, rtx x, int nregs)
2032 if (nregs == 0)
2033 return;
2035 /* See if the machine can do this with a store multiple insn. */
2036 if (targetm.have_store_multiple ())
2038 rtx_insn *last = get_last_insn ();
2039 rtx first = gen_rtx_REG (word_mode, regno);
2040 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2041 GEN_INT (nregs)))
2043 emit_insn (pat);
2044 return;
2046 else
2047 delete_insns_since (last);
2050 for (int i = 0; i < nregs; i++)
2052 rtx tem = operand_subword (x, i, 1, BLKmode);
2054 gcc_assert (tem);
2056 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2060 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2061 ORIG, where ORIG is a non-consecutive group of registers represented by
2062 a PARALLEL. The clone is identical to the original except in that the
2063 original set of registers is replaced by a new set of pseudo registers.
2064 The new set has the same modes as the original set. */
2067 gen_group_rtx (rtx orig)
2069 int i, length;
2070 rtx *tmps;
2072 gcc_assert (GET_CODE (orig) == PARALLEL);
2074 length = XVECLEN (orig, 0);
2075 tmps = XALLOCAVEC (rtx, length);
2077 /* Skip a NULL entry in first slot. */
2078 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2080 if (i)
2081 tmps[0] = 0;
2083 for (; i < length; i++)
2085 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2086 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2088 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2091 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2094 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2095 except that values are placed in TMPS[i], and must later be moved
2096 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2098 static void
2099 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2100 poly_int64 ssize)
2102 rtx src;
2103 int start, i;
2104 machine_mode m = GET_MODE (orig_src);
2106 gcc_assert (GET_CODE (dst) == PARALLEL);
2108 if (m != VOIDmode
2109 && !SCALAR_INT_MODE_P (m)
2110 && !MEM_P (orig_src)
2111 && GET_CODE (orig_src) != CONCAT)
2113 scalar_int_mode imode;
2114 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2116 src = gen_reg_rtx (imode);
2117 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2119 else
2121 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2122 emit_move_insn (src, orig_src);
2124 emit_group_load_1 (tmps, dst, src, type, ssize);
2125 return;
2128 /* Check for a NULL entry, used to indicate that the parameter goes
2129 both on the stack and in registers. */
2130 if (XEXP (XVECEXP (dst, 0, 0), 0))
2131 start = 0;
2132 else
2133 start = 1;
2135 /* Process the pieces. */
2136 for (i = start; i < XVECLEN (dst, 0); i++)
2138 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2139 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2140 poly_int64 bytelen = GET_MODE_SIZE (mode);
2141 poly_int64 shift = 0;
2143 /* Handle trailing fragments that run over the size of the struct.
2144 It's the target's responsibility to make sure that the fragment
2145 cannot be strictly smaller in some cases and strictly larger
2146 in others. */
2147 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2148 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2150 /* Arrange to shift the fragment to where it belongs.
2151 extract_bit_field loads to the lsb of the reg. */
2152 if (
2153 #ifdef BLOCK_REG_PADDING
2154 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2155 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2156 #else
2157 BYTES_BIG_ENDIAN
2158 #endif
2160 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2161 bytelen = ssize - bytepos;
2162 gcc_assert (maybe_gt (bytelen, 0));
2165 /* If we won't be loading directly from memory, protect the real source
2166 from strange tricks we might play; but make sure that the source can
2167 be loaded directly into the destination. */
2168 src = orig_src;
2169 if (!MEM_P (orig_src)
2170 && (!CONSTANT_P (orig_src)
2171 || (GET_MODE (orig_src) != mode
2172 && GET_MODE (orig_src) != VOIDmode)))
2174 if (GET_MODE (orig_src) == VOIDmode)
2175 src = gen_reg_rtx (mode);
2176 else
2177 src = gen_reg_rtx (GET_MODE (orig_src));
2179 emit_move_insn (src, orig_src);
2182 /* Optimize the access just a bit. */
2183 if (MEM_P (src)
2184 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2185 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2186 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2187 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2189 tmps[i] = gen_reg_rtx (mode);
2190 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2192 else if (COMPLEX_MODE_P (mode)
2193 && GET_MODE (src) == mode
2194 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2195 /* Let emit_move_complex do the bulk of the work. */
2196 tmps[i] = src;
2197 else if (GET_CODE (src) == CONCAT)
2199 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2200 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2201 unsigned int elt;
2202 poly_int64 subpos;
2204 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2205 && known_le (subpos + bytelen, slen0))
2207 /* The following assumes that the concatenated objects all
2208 have the same size. In this case, a simple calculation
2209 can be used to determine the object and the bit field
2210 to be extracted. */
2211 tmps[i] = XEXP (src, elt);
2212 if (maybe_ne (subpos, 0)
2213 || maybe_ne (subpos + bytelen, slen0)
2214 || (!CONSTANT_P (tmps[i])
2215 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2216 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2217 subpos * BITS_PER_UNIT,
2218 1, NULL_RTX, mode, mode, false,
2219 NULL);
2221 else
2223 rtx mem;
2225 gcc_assert (known_eq (bytepos, 0));
2226 mem = assign_stack_temp (GET_MODE (src), slen);
2227 emit_move_insn (mem, src);
2228 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2229 0, 1, NULL_RTX, mode, mode, false,
2230 NULL);
2233 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2234 SIMD register, which is currently broken. While we get GCC
2235 to emit proper RTL for these cases, let's dump to memory. */
2236 else if (VECTOR_MODE_P (GET_MODE (dst))
2237 && REG_P (src))
2239 int slen = GET_MODE_SIZE (GET_MODE (src));
2240 rtx mem;
2242 mem = assign_stack_temp (GET_MODE (src), slen);
2243 emit_move_insn (mem, src);
2244 tmps[i] = adjust_address (mem, mode, bytepos);
2246 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2247 && XVECLEN (dst, 0) > 1)
2248 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2249 else if (CONSTANT_P (src))
2251 if (known_eq (bytelen, ssize))
2252 tmps[i] = src;
2253 else
2255 rtx first, second;
2257 /* TODO: const_wide_int can have sizes other than this... */
2258 gcc_assert (known_eq (2 * bytelen, ssize));
2259 split_double (src, &first, &second);
2260 if (i)
2261 tmps[i] = second;
2262 else
2263 tmps[i] = first;
2266 else if (REG_P (src) && GET_MODE (src) == mode)
2267 tmps[i] = src;
2268 else
2269 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2270 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2271 mode, mode, false, NULL);
2273 if (maybe_ne (shift, 0))
2274 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2275 shift, tmps[i], 0);
2279 /* Emit code to move a block SRC of type TYPE to a block DST,
2280 where DST is non-consecutive registers represented by a PARALLEL.
2281 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2282 if not known. */
2284 void
2285 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2287 rtx *tmps;
2288 int i;
2290 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2291 emit_group_load_1 (tmps, dst, src, type, ssize);
2293 /* Copy the extracted pieces into the proper (probable) hard regs. */
2294 for (i = 0; i < XVECLEN (dst, 0); i++)
2296 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2297 if (d == NULL)
2298 continue;
2299 emit_move_insn (d, tmps[i]);
2303 /* Similar, but load SRC into new pseudos in a format that looks like
2304 PARALLEL. This can later be fed to emit_group_move to get things
2305 in the right place. */
2308 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2310 rtvec vec;
2311 int i;
2313 vec = rtvec_alloc (XVECLEN (parallel, 0));
2314 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2316 /* Convert the vector to look just like the original PARALLEL, except
2317 with the computed values. */
2318 for (i = 0; i < XVECLEN (parallel, 0); i++)
2320 rtx e = XVECEXP (parallel, 0, i);
2321 rtx d = XEXP (e, 0);
2323 if (d)
2325 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2326 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2328 RTVEC_ELT (vec, i) = e;
2331 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2334 /* Emit code to move a block SRC to block DST, where SRC and DST are
2335 non-consecutive groups of registers, each represented by a PARALLEL. */
2337 void
2338 emit_group_move (rtx dst, rtx src)
2340 int i;
2342 gcc_assert (GET_CODE (src) == PARALLEL
2343 && GET_CODE (dst) == PARALLEL
2344 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2346 /* Skip first entry if NULL. */
2347 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2348 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2349 XEXP (XVECEXP (src, 0, i), 0));
2352 /* Move a group of registers represented by a PARALLEL into pseudos. */
2355 emit_group_move_into_temps (rtx src)
2357 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2358 int i;
2360 for (i = 0; i < XVECLEN (src, 0); i++)
2362 rtx e = XVECEXP (src, 0, i);
2363 rtx d = XEXP (e, 0);
2365 if (d)
2366 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2367 RTVEC_ELT (vec, i) = e;
2370 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2373 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2374 where SRC is non-consecutive registers represented by a PARALLEL.
2375 SSIZE represents the total size of block ORIG_DST, or -1 if not
2376 known. */
2378 void
2379 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2380 poly_int64 ssize)
2382 rtx *tmps, dst;
2383 int start, finish, i;
2384 machine_mode m = GET_MODE (orig_dst);
2386 gcc_assert (GET_CODE (src) == PARALLEL);
2388 if (!SCALAR_INT_MODE_P (m)
2389 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2391 scalar_int_mode imode;
2392 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2394 dst = gen_reg_rtx (imode);
2395 emit_group_store (dst, src, type, ssize);
2396 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2398 else
2400 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2401 emit_group_store (dst, src, type, ssize);
2403 emit_move_insn (orig_dst, dst);
2404 return;
2407 /* Check for a NULL entry, used to indicate that the parameter goes
2408 both on the stack and in registers. */
2409 if (XEXP (XVECEXP (src, 0, 0), 0))
2410 start = 0;
2411 else
2412 start = 1;
2413 finish = XVECLEN (src, 0);
2415 tmps = XALLOCAVEC (rtx, finish);
2417 /* Copy the (probable) hard regs into pseudos. */
2418 for (i = start; i < finish; i++)
2420 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2421 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2423 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2424 emit_move_insn (tmps[i], reg);
2426 else
2427 tmps[i] = reg;
2430 /* If we won't be storing directly into memory, protect the real destination
2431 from strange tricks we might play. */
2432 dst = orig_dst;
2433 if (GET_CODE (dst) == PARALLEL)
2435 rtx temp;
2437 /* We can get a PARALLEL dst if there is a conditional expression in
2438 a return statement. In that case, the dst and src are the same,
2439 so no action is necessary. */
2440 if (rtx_equal_p (dst, src))
2441 return;
2443 /* It is unclear if we can ever reach here, but we may as well handle
2444 it. Allocate a temporary, and split this into a store/load to/from
2445 the temporary. */
2446 temp = assign_stack_temp (GET_MODE (dst), ssize);
2447 emit_group_store (temp, src, type, ssize);
2448 emit_group_load (dst, temp, type, ssize);
2449 return;
2451 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2453 machine_mode outer = GET_MODE (dst);
2454 machine_mode inner;
2455 poly_int64 bytepos;
2456 bool done = false;
2457 rtx temp;
2459 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2460 dst = gen_reg_rtx (outer);
2462 /* Make life a bit easier for combine. */
2463 /* If the first element of the vector is the low part
2464 of the destination mode, use a paradoxical subreg to
2465 initialize the destination. */
2466 if (start < finish)
2468 inner = GET_MODE (tmps[start]);
2469 bytepos = subreg_lowpart_offset (inner, outer);
2470 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, start), 1)), bytepos))
2472 temp = simplify_gen_subreg (outer, tmps[start],
2473 inner, 0);
2474 if (temp)
2476 emit_move_insn (dst, temp);
2477 done = true;
2478 start++;
2483 /* If the first element wasn't the low part, try the last. */
2484 if (!done
2485 && start < finish - 1)
2487 inner = GET_MODE (tmps[finish - 1]);
2488 bytepos = subreg_lowpart_offset (inner, outer);
2489 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)),
2490 bytepos))
2492 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2493 inner, 0);
2494 if (temp)
2496 emit_move_insn (dst, temp);
2497 done = true;
2498 finish--;
2503 /* Otherwise, simply initialize the result to zero. */
2504 if (!done)
2505 emit_move_insn (dst, CONST0_RTX (outer));
2508 /* Process the pieces. */
2509 for (i = start; i < finish; i++)
2511 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2512 machine_mode mode = GET_MODE (tmps[i]);
2513 poly_int64 bytelen = GET_MODE_SIZE (mode);
2514 poly_uint64 adj_bytelen;
2515 rtx dest = dst;
2517 /* Handle trailing fragments that run over the size of the struct.
2518 It's the target's responsibility to make sure that the fragment
2519 cannot be strictly smaller in some cases and strictly larger
2520 in others. */
2521 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2522 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2523 adj_bytelen = ssize - bytepos;
2524 else
2525 adj_bytelen = bytelen;
2527 if (GET_CODE (dst) == CONCAT)
2529 if (known_le (bytepos + adj_bytelen,
2530 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2531 dest = XEXP (dst, 0);
2532 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2534 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2535 dest = XEXP (dst, 1);
2537 else
2539 machine_mode dest_mode = GET_MODE (dest);
2540 machine_mode tmp_mode = GET_MODE (tmps[i]);
2542 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2544 if (GET_MODE_ALIGNMENT (dest_mode)
2545 >= GET_MODE_ALIGNMENT (tmp_mode))
2547 dest = assign_stack_temp (dest_mode,
2548 GET_MODE_SIZE (dest_mode));
2549 emit_move_insn (adjust_address (dest,
2550 tmp_mode,
2551 bytepos),
2552 tmps[i]);
2553 dst = dest;
2555 else
2557 dest = assign_stack_temp (tmp_mode,
2558 GET_MODE_SIZE (tmp_mode));
2559 emit_move_insn (dest, tmps[i]);
2560 dst = adjust_address (dest, dest_mode, bytepos);
2562 break;
2566 /* Handle trailing fragments that run over the size of the struct. */
2567 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2569 /* store_bit_field always takes its value from the lsb.
2570 Move the fragment to the lsb if it's not already there. */
2571 if (
2572 #ifdef BLOCK_REG_PADDING
2573 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2574 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2575 #else
2576 BYTES_BIG_ENDIAN
2577 #endif
2580 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2581 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2582 shift, tmps[i], 0);
2585 /* Make sure not to write past the end of the struct. */
2586 store_bit_field (dest,
2587 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2588 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2589 VOIDmode, tmps[i], false);
2592 /* Optimize the access just a bit. */
2593 else if (MEM_P (dest)
2594 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2595 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2596 && multiple_p (bytepos * BITS_PER_UNIT,
2597 GET_MODE_ALIGNMENT (mode))
2598 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2599 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2601 else
2602 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2603 0, 0, mode, tmps[i], false);
2606 /* Copy from the pseudo into the (probable) hard reg. */
2607 if (orig_dst != dst)
2608 emit_move_insn (orig_dst, dst);
2611 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2612 of the value stored in X. */
2615 maybe_emit_group_store (rtx x, tree type)
2617 machine_mode mode = TYPE_MODE (type);
2618 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2619 if (GET_CODE (x) == PARALLEL)
2621 rtx result = gen_reg_rtx (mode);
2622 emit_group_store (result, x, type, int_size_in_bytes (type));
2623 return result;
2625 return x;
2628 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2630 This is used on targets that return BLKmode values in registers. */
2632 static void
2633 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2635 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2636 rtx src = NULL, dst = NULL;
2637 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2638 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2639 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2640 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2641 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2642 fixed_size_mode copy_mode;
2644 /* BLKmode registers created in the back-end shouldn't have survived. */
2645 gcc_assert (mode != BLKmode);
2647 /* If the structure doesn't take up a whole number of words, see whether
2648 SRCREG is padded on the left or on the right. If it's on the left,
2649 set PADDING_CORRECTION to the number of bits to skip.
2651 In most ABIs, the structure will be returned at the least end of
2652 the register, which translates to right padding on little-endian
2653 targets and left padding on big-endian targets. The opposite
2654 holds if the structure is returned at the most significant
2655 end of the register. */
2656 if (bytes % UNITS_PER_WORD != 0
2657 && (targetm.calls.return_in_msb (type)
2658 ? !BYTES_BIG_ENDIAN
2659 : BYTES_BIG_ENDIAN))
2660 padding_correction
2661 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2663 /* We can use a single move if we have an exact mode for the size. */
2664 else if (MEM_P (target)
2665 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2666 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2667 && bytes == GET_MODE_SIZE (mode))
2669 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2670 return;
2673 /* And if we additionally have the same mode for a register. */
2674 else if (REG_P (target)
2675 && GET_MODE (target) == mode
2676 && bytes == GET_MODE_SIZE (mode))
2678 emit_move_insn (target, srcreg);
2679 return;
2682 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2683 into a new pseudo which is a full word. */
2684 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2686 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2687 mode = word_mode;
2690 /* Copy the structure BITSIZE bits at a time. If the target lives in
2691 memory, take care of not reading/writing past its end by selecting
2692 a copy mode suited to BITSIZE. This should always be possible given
2693 how it is computed.
2695 If the target lives in register, make sure not to select a copy mode
2696 larger than the mode of the register.
2698 We could probably emit more efficient code for machines which do not use
2699 strict alignment, but it doesn't seem worth the effort at the current
2700 time. */
2702 copy_mode = word_mode;
2703 if (MEM_P (target))
2705 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2706 if (mem_mode.exists ())
2707 copy_mode = mem_mode.require ();
2709 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2710 copy_mode = tmode;
2712 for (bitpos = 0, xbitpos = padding_correction;
2713 bitpos < bytes * BITS_PER_UNIT;
2714 bitpos += bitsize, xbitpos += bitsize)
2716 /* We need a new source operand each time xbitpos is on a
2717 word boundary and when xbitpos == padding_correction
2718 (the first time through). */
2719 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2720 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2722 /* We need a new destination operand each time bitpos is on
2723 a word boundary. */
2724 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2725 dst = target;
2726 else if (bitpos % BITS_PER_WORD == 0)
2727 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2729 /* Use xbitpos for the source extraction (right justified) and
2730 bitpos for the destination store (left justified). */
2731 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2732 extract_bit_field (src, bitsize,
2733 xbitpos % BITS_PER_WORD, 1,
2734 NULL_RTX, copy_mode, copy_mode,
2735 false, NULL),
2736 false);
2740 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2741 register if it contains any data, otherwise return null.
2743 This is used on targets that return BLKmode values in registers. */
2746 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2748 int i, n_regs;
2749 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2750 unsigned int bitsize;
2751 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2752 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2753 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2754 fixed_size_mode dst_mode;
2756 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2758 x = expand_normal (src);
2760 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2761 if (bytes == 0)
2762 return NULL_RTX;
2764 /* If the structure doesn't take up a whole number of words, see
2765 whether the register value should be padded on the left or on
2766 the right. Set PADDING_CORRECTION to the number of padding
2767 bits needed on the left side.
2769 In most ABIs, the structure will be returned at the least end of
2770 the register, which translates to right padding on little-endian
2771 targets and left padding on big-endian targets. The opposite
2772 holds if the structure is returned at the most significant
2773 end of the register. */
2774 if (bytes % UNITS_PER_WORD != 0
2775 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2776 ? !BYTES_BIG_ENDIAN
2777 : BYTES_BIG_ENDIAN))
2778 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2779 * BITS_PER_UNIT));
2781 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2782 dst_words = XALLOCAVEC (rtx, n_regs);
2783 bitsize = BITS_PER_WORD;
2784 if (targetm.slow_unaligned_access (word_mode, TYPE_ALIGN (TREE_TYPE (src))))
2785 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2787 /* Copy the structure BITSIZE bits at a time. */
2788 for (bitpos = 0, xbitpos = padding_correction;
2789 bitpos < bytes * BITS_PER_UNIT;
2790 bitpos += bitsize, xbitpos += bitsize)
2792 /* We need a new destination pseudo each time xbitpos is
2793 on a word boundary and when xbitpos == padding_correction
2794 (the first time through). */
2795 if (xbitpos % BITS_PER_WORD == 0
2796 || xbitpos == padding_correction)
2798 /* Generate an appropriate register. */
2799 dst_word = gen_reg_rtx (word_mode);
2800 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2802 /* Clear the destination before we move anything into it. */
2803 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2806 /* We need a new source operand each time bitpos is on a word
2807 boundary. */
2808 if (bitpos % BITS_PER_WORD == 0)
2809 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2811 /* Use bitpos for the source extraction (left justified) and
2812 xbitpos for the destination store (right justified). */
2813 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2814 0, 0, word_mode,
2815 extract_bit_field (src_word, bitsize,
2816 bitpos % BITS_PER_WORD, 1,
2817 NULL_RTX, word_mode, word_mode,
2818 false, NULL),
2819 false);
2822 if (mode == BLKmode)
2824 /* Find the smallest integer mode large enough to hold the
2825 entire structure. */
2826 opt_scalar_int_mode mode_iter;
2827 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2828 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2829 break;
2831 /* A suitable mode should have been found. */
2832 mode = mode_iter.require ();
2835 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2836 dst_mode = word_mode;
2837 else
2838 dst_mode = mode;
2839 dst = gen_reg_rtx (dst_mode);
2841 for (i = 0; i < n_regs; i++)
2842 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2844 if (mode != dst_mode)
2845 dst = gen_lowpart (mode, dst);
2847 return dst;
2850 /* Add a USE expression for REG to the (possibly empty) list pointed
2851 to by CALL_FUSAGE. REG must denote a hard register. */
2853 void
2854 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2856 gcc_assert (REG_P (reg));
2858 if (!HARD_REGISTER_P (reg))
2859 return;
2861 *call_fusage
2862 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2865 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2866 to by CALL_FUSAGE. REG must denote a hard register. */
2868 void
2869 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2871 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2873 *call_fusage
2874 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2877 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2878 starting at REGNO. All of these registers must be hard registers. */
2880 void
2881 use_regs (rtx *call_fusage, int regno, int nregs)
2883 int i;
2885 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2887 for (i = 0; i < nregs; i++)
2888 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2891 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2892 PARALLEL REGS. This is for calls that pass values in multiple
2893 non-contiguous locations. The Irix 6 ABI has examples of this. */
2895 void
2896 use_group_regs (rtx *call_fusage, rtx regs)
2898 int i;
2900 for (i = 0; i < XVECLEN (regs, 0); i++)
2902 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2904 /* A NULL entry means the parameter goes both on the stack and in
2905 registers. This can also be a MEM for targets that pass values
2906 partially on the stack and partially in registers. */
2907 if (reg != 0 && REG_P (reg))
2908 use_reg (call_fusage, reg);
2912 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2913 assigment and the code of the expresion on the RHS is CODE. Return
2914 NULL otherwise. */
2916 static gimple *
2917 get_def_for_expr (tree name, enum tree_code code)
2919 gimple *def_stmt;
2921 if (TREE_CODE (name) != SSA_NAME)
2922 return NULL;
2924 def_stmt = get_gimple_for_ssa_name (name);
2925 if (!def_stmt
2926 || gimple_assign_rhs_code (def_stmt) != code)
2927 return NULL;
2929 return def_stmt;
2932 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2933 assigment and the class of the expresion on the RHS is CLASS. Return
2934 NULL otherwise. */
2936 static gimple *
2937 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2939 gimple *def_stmt;
2941 if (TREE_CODE (name) != SSA_NAME)
2942 return NULL;
2944 def_stmt = get_gimple_for_ssa_name (name);
2945 if (!def_stmt
2946 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2947 return NULL;
2949 return def_stmt;
2952 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2953 its length in bytes. */
2956 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2957 unsigned int expected_align, HOST_WIDE_INT expected_size,
2958 unsigned HOST_WIDE_INT min_size,
2959 unsigned HOST_WIDE_INT max_size,
2960 unsigned HOST_WIDE_INT probable_max_size)
2962 machine_mode mode = GET_MODE (object);
2963 unsigned int align;
2965 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2967 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2968 just move a zero. Otherwise, do this a piece at a time. */
2969 if (mode != BLKmode
2970 && CONST_INT_P (size)
2971 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2973 rtx zero = CONST0_RTX (mode);
2974 if (zero != NULL)
2976 emit_move_insn (object, zero);
2977 return NULL;
2980 if (COMPLEX_MODE_P (mode))
2982 zero = CONST0_RTX (GET_MODE_INNER (mode));
2983 if (zero != NULL)
2985 write_complex_part (object, zero, 0);
2986 write_complex_part (object, zero, 1);
2987 return NULL;
2992 if (size == const0_rtx)
2993 return NULL;
2995 align = MEM_ALIGN (object);
2997 if (CONST_INT_P (size)
2998 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2999 CLEAR_BY_PIECES,
3000 optimize_insn_for_speed_p ()))
3001 clear_by_pieces (object, INTVAL (size), align);
3002 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3003 expected_align, expected_size,
3004 min_size, max_size, probable_max_size))
3006 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3007 return set_storage_via_libcall (object, size, const0_rtx,
3008 method == BLOCK_OP_TAILCALL);
3009 else
3010 gcc_unreachable ();
3012 return NULL;
3016 clear_storage (rtx object, rtx size, enum block_op_methods method)
3018 unsigned HOST_WIDE_INT max, min = 0;
3019 if (GET_CODE (size) == CONST_INT)
3020 min = max = UINTVAL (size);
3021 else
3022 max = GET_MODE_MASK (GET_MODE (size));
3023 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3027 /* A subroutine of clear_storage. Expand a call to memset.
3028 Return the return value of memset, 0 otherwise. */
3031 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3033 tree call_expr, fn, object_tree, size_tree, val_tree;
3034 machine_mode size_mode;
3036 object = copy_addr_to_reg (XEXP (object, 0));
3037 object_tree = make_tree (ptr_type_node, object);
3039 if (!CONST_INT_P (val))
3040 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3041 val_tree = make_tree (integer_type_node, val);
3043 size_mode = TYPE_MODE (sizetype);
3044 size = convert_to_mode (size_mode, size, 1);
3045 size = copy_to_mode_reg (size_mode, size);
3046 size_tree = make_tree (sizetype, size);
3048 /* It is incorrect to use the libcall calling conventions for calls to
3049 memset because it can be provided by the user. */
3050 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3051 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3052 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3054 return expand_call (call_expr, NULL_RTX, false);
3057 /* Expand a setmem pattern; return true if successful. */
3059 bool
3060 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3061 unsigned int expected_align, HOST_WIDE_INT expected_size,
3062 unsigned HOST_WIDE_INT min_size,
3063 unsigned HOST_WIDE_INT max_size,
3064 unsigned HOST_WIDE_INT probable_max_size)
3066 /* Try the most limited insn first, because there's no point
3067 including more than one in the machine description unless
3068 the more limited one has some advantage. */
3070 if (expected_align < align)
3071 expected_align = align;
3072 if (expected_size != -1)
3074 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3075 expected_size = max_size;
3076 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3077 expected_size = min_size;
3080 opt_scalar_int_mode mode_iter;
3081 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3083 scalar_int_mode mode = mode_iter.require ();
3084 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3086 if (code != CODE_FOR_nothing
3087 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3088 here because if SIZE is less than the mode mask, as it is
3089 returned by the macro, it will definitely be less than the
3090 actual mode mask. Since SIZE is within the Pmode address
3091 space, we limit MODE to Pmode. */
3092 && ((CONST_INT_P (size)
3093 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3094 <= (GET_MODE_MASK (mode) >> 1)))
3095 || max_size <= (GET_MODE_MASK (mode) >> 1)
3096 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3098 struct expand_operand ops[9];
3099 unsigned int nops;
3101 nops = insn_data[(int) code].n_generator_args;
3102 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3104 create_fixed_operand (&ops[0], object);
3105 /* The check above guarantees that this size conversion is valid. */
3106 create_convert_operand_to (&ops[1], size, mode, true);
3107 create_convert_operand_from (&ops[2], val, byte_mode, true);
3108 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3109 if (nops >= 6)
3111 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3112 create_integer_operand (&ops[5], expected_size);
3114 if (nops >= 8)
3116 create_integer_operand (&ops[6], min_size);
3117 /* If we can not represent the maximal size,
3118 make parameter NULL. */
3119 if ((HOST_WIDE_INT) max_size != -1)
3120 create_integer_operand (&ops[7], max_size);
3121 else
3122 create_fixed_operand (&ops[7], NULL);
3124 if (nops == 9)
3126 /* If we can not represent the maximal size,
3127 make parameter NULL. */
3128 if ((HOST_WIDE_INT) probable_max_size != -1)
3129 create_integer_operand (&ops[8], probable_max_size);
3130 else
3131 create_fixed_operand (&ops[8], NULL);
3133 if (maybe_expand_insn (code, nops, ops))
3134 return true;
3138 return false;
3142 /* Write to one of the components of the complex value CPLX. Write VAL to
3143 the real part if IMAG_P is false, and the imaginary part if its true. */
3145 void
3146 write_complex_part (rtx cplx, rtx val, bool imag_p)
3148 machine_mode cmode;
3149 scalar_mode imode;
3150 unsigned ibitsize;
3152 if (GET_CODE (cplx) == CONCAT)
3154 emit_move_insn (XEXP (cplx, imag_p), val);
3155 return;
3158 cmode = GET_MODE (cplx);
3159 imode = GET_MODE_INNER (cmode);
3160 ibitsize = GET_MODE_BITSIZE (imode);
3162 /* For MEMs simplify_gen_subreg may generate an invalid new address
3163 because, e.g., the original address is considered mode-dependent
3164 by the target, which restricts simplify_subreg from invoking
3165 adjust_address_nv. Instead of preparing fallback support for an
3166 invalid address, we call adjust_address_nv directly. */
3167 if (MEM_P (cplx))
3169 emit_move_insn (adjust_address_nv (cplx, imode,
3170 imag_p ? GET_MODE_SIZE (imode) : 0),
3171 val);
3172 return;
3175 /* If the sub-object is at least word sized, then we know that subregging
3176 will work. This special case is important, since store_bit_field
3177 wants to operate on integer modes, and there's rarely an OImode to
3178 correspond to TCmode. */
3179 if (ibitsize >= BITS_PER_WORD
3180 /* For hard regs we have exact predicates. Assume we can split
3181 the original object if it spans an even number of hard regs.
3182 This special case is important for SCmode on 64-bit platforms
3183 where the natural size of floating-point regs is 32-bit. */
3184 || (REG_P (cplx)
3185 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3186 && REG_NREGS (cplx) % 2 == 0))
3188 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3189 imag_p ? GET_MODE_SIZE (imode) : 0);
3190 if (part)
3192 emit_move_insn (part, val);
3193 return;
3195 else
3196 /* simplify_gen_subreg may fail for sub-word MEMs. */
3197 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3200 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3201 false);
3204 /* Extract one of the components of the complex value CPLX. Extract the
3205 real part if IMAG_P is false, and the imaginary part if it's true. */
3208 read_complex_part (rtx cplx, bool imag_p)
3210 machine_mode cmode;
3211 scalar_mode imode;
3212 unsigned ibitsize;
3214 if (GET_CODE (cplx) == CONCAT)
3215 return XEXP (cplx, imag_p);
3217 cmode = GET_MODE (cplx);
3218 imode = GET_MODE_INNER (cmode);
3219 ibitsize = GET_MODE_BITSIZE (imode);
3221 /* Special case reads from complex constants that got spilled to memory. */
3222 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3224 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3225 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3227 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3228 if (CONSTANT_CLASS_P (part))
3229 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3233 /* For MEMs simplify_gen_subreg may generate an invalid new address
3234 because, e.g., the original address is considered mode-dependent
3235 by the target, which restricts simplify_subreg from invoking
3236 adjust_address_nv. Instead of preparing fallback support for an
3237 invalid address, we call adjust_address_nv directly. */
3238 if (MEM_P (cplx))
3239 return adjust_address_nv (cplx, imode,
3240 imag_p ? GET_MODE_SIZE (imode) : 0);
3242 /* If the sub-object is at least word sized, then we know that subregging
3243 will work. This special case is important, since extract_bit_field
3244 wants to operate on integer modes, and there's rarely an OImode to
3245 correspond to TCmode. */
3246 if (ibitsize >= BITS_PER_WORD
3247 /* For hard regs we have exact predicates. Assume we can split
3248 the original object if it spans an even number of hard regs.
3249 This special case is important for SCmode on 64-bit platforms
3250 where the natural size of floating-point regs is 32-bit. */
3251 || (REG_P (cplx)
3252 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3253 && REG_NREGS (cplx) % 2 == 0))
3255 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3256 imag_p ? GET_MODE_SIZE (imode) : 0);
3257 if (ret)
3258 return ret;
3259 else
3260 /* simplify_gen_subreg may fail for sub-word MEMs. */
3261 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3264 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3265 true, NULL_RTX, imode, imode, false, NULL);
3268 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3269 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3270 represented in NEW_MODE. If FORCE is true, this will never happen, as
3271 we'll force-create a SUBREG if needed. */
3273 static rtx
3274 emit_move_change_mode (machine_mode new_mode,
3275 machine_mode old_mode, rtx x, bool force)
3277 rtx ret;
3279 if (push_operand (x, GET_MODE (x)))
3281 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3282 MEM_COPY_ATTRIBUTES (ret, x);
3284 else if (MEM_P (x))
3286 /* We don't have to worry about changing the address since the
3287 size in bytes is supposed to be the same. */
3288 if (reload_in_progress)
3290 /* Copy the MEM to change the mode and move any
3291 substitutions from the old MEM to the new one. */
3292 ret = adjust_address_nv (x, new_mode, 0);
3293 copy_replacements (x, ret);
3295 else
3296 ret = adjust_address (x, new_mode, 0);
3298 else
3300 /* Note that we do want simplify_subreg's behavior of validating
3301 that the new mode is ok for a hard register. If we were to use
3302 simplify_gen_subreg, we would create the subreg, but would
3303 probably run into the target not being able to implement it. */
3304 /* Except, of course, when FORCE is true, when this is exactly what
3305 we want. Which is needed for CCmodes on some targets. */
3306 if (force)
3307 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3308 else
3309 ret = simplify_subreg (new_mode, x, old_mode, 0);
3312 return ret;
3315 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3316 an integer mode of the same size as MODE. Returns the instruction
3317 emitted, or NULL if such a move could not be generated. */
3319 static rtx_insn *
3320 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3322 scalar_int_mode imode;
3323 enum insn_code code;
3325 /* There must exist a mode of the exact size we require. */
3326 if (!int_mode_for_mode (mode).exists (&imode))
3327 return NULL;
3329 /* The target must support moves in this mode. */
3330 code = optab_handler (mov_optab, imode);
3331 if (code == CODE_FOR_nothing)
3332 return NULL;
3334 x = emit_move_change_mode (imode, mode, x, force);
3335 if (x == NULL_RTX)
3336 return NULL;
3337 y = emit_move_change_mode (imode, mode, y, force);
3338 if (y == NULL_RTX)
3339 return NULL;
3340 return emit_insn (GEN_FCN (code) (x, y));
3343 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3344 Return an equivalent MEM that does not use an auto-increment. */
3347 emit_move_resolve_push (machine_mode mode, rtx x)
3349 enum rtx_code code = GET_CODE (XEXP (x, 0));
3350 rtx temp;
3352 poly_int64 adjust = GET_MODE_SIZE (mode);
3353 #ifdef PUSH_ROUNDING
3354 adjust = PUSH_ROUNDING (adjust);
3355 #endif
3356 if (code == PRE_DEC || code == POST_DEC)
3357 adjust = -adjust;
3358 else if (code == PRE_MODIFY || code == POST_MODIFY)
3360 rtx expr = XEXP (XEXP (x, 0), 1);
3362 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3363 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3364 if (GET_CODE (expr) == MINUS)
3365 val = -val;
3366 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3367 adjust = val;
3370 /* Do not use anti_adjust_stack, since we don't want to update
3371 stack_pointer_delta. */
3372 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3373 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3374 0, OPTAB_LIB_WIDEN);
3375 if (temp != stack_pointer_rtx)
3376 emit_move_insn (stack_pointer_rtx, temp);
3378 switch (code)
3380 case PRE_INC:
3381 case PRE_DEC:
3382 case PRE_MODIFY:
3383 temp = stack_pointer_rtx;
3384 break;
3385 case POST_INC:
3386 case POST_DEC:
3387 case POST_MODIFY:
3388 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3389 break;
3390 default:
3391 gcc_unreachable ();
3394 return replace_equiv_address (x, temp);
3397 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3398 X is known to satisfy push_operand, and MODE is known to be complex.
3399 Returns the last instruction emitted. */
3401 rtx_insn *
3402 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3404 scalar_mode submode = GET_MODE_INNER (mode);
3405 bool imag_first;
3407 #ifdef PUSH_ROUNDING
3408 poly_int64 submodesize = GET_MODE_SIZE (submode);
3410 /* In case we output to the stack, but the size is smaller than the
3411 machine can push exactly, we need to use move instructions. */
3412 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3414 x = emit_move_resolve_push (mode, x);
3415 return emit_move_insn (x, y);
3417 #endif
3419 /* Note that the real part always precedes the imag part in memory
3420 regardless of machine's endianness. */
3421 switch (GET_CODE (XEXP (x, 0)))
3423 case PRE_DEC:
3424 case POST_DEC:
3425 imag_first = true;
3426 break;
3427 case PRE_INC:
3428 case POST_INC:
3429 imag_first = false;
3430 break;
3431 default:
3432 gcc_unreachable ();
3435 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3436 read_complex_part (y, imag_first));
3437 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3438 read_complex_part (y, !imag_first));
3441 /* A subroutine of emit_move_complex. Perform the move from Y to X
3442 via two moves of the parts. Returns the last instruction emitted. */
3444 rtx_insn *
3445 emit_move_complex_parts (rtx x, rtx y)
3447 /* Show the output dies here. This is necessary for SUBREGs
3448 of pseudos since we cannot track their lifetimes correctly;
3449 hard regs shouldn't appear here except as return values. */
3450 if (!reload_completed && !reload_in_progress
3451 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3452 emit_clobber (x);
3454 write_complex_part (x, read_complex_part (y, false), false);
3455 write_complex_part (x, read_complex_part (y, true), true);
3457 return get_last_insn ();
3460 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3461 MODE is known to be complex. Returns the last instruction emitted. */
3463 static rtx_insn *
3464 emit_move_complex (machine_mode mode, rtx x, rtx y)
3466 bool try_int;
3468 /* Need to take special care for pushes, to maintain proper ordering
3469 of the data, and possibly extra padding. */
3470 if (push_operand (x, mode))
3471 return emit_move_complex_push (mode, x, y);
3473 /* See if we can coerce the target into moving both values at once, except
3474 for floating point where we favor moving as parts if this is easy. */
3475 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3476 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3477 && !(REG_P (x)
3478 && HARD_REGISTER_P (x)
3479 && REG_NREGS (x) == 1)
3480 && !(REG_P (y)
3481 && HARD_REGISTER_P (y)
3482 && REG_NREGS (y) == 1))
3483 try_int = false;
3484 /* Not possible if the values are inherently not adjacent. */
3485 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3486 try_int = false;
3487 /* Is possible if both are registers (or subregs of registers). */
3488 else if (register_operand (x, mode) && register_operand (y, mode))
3489 try_int = true;
3490 /* If one of the operands is a memory, and alignment constraints
3491 are friendly enough, we may be able to do combined memory operations.
3492 We do not attempt this if Y is a constant because that combination is
3493 usually better with the by-parts thing below. */
3494 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3495 && (!STRICT_ALIGNMENT
3496 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3497 try_int = true;
3498 else
3499 try_int = false;
3501 if (try_int)
3503 rtx_insn *ret;
3505 /* For memory to memory moves, optimal behavior can be had with the
3506 existing block move logic. */
3507 if (MEM_P (x) && MEM_P (y))
3509 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3510 BLOCK_OP_NO_LIBCALL);
3511 return get_last_insn ();
3514 ret = emit_move_via_integer (mode, x, y, true);
3515 if (ret)
3516 return ret;
3519 return emit_move_complex_parts (x, y);
3522 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3523 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3525 static rtx_insn *
3526 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3528 rtx_insn *ret;
3530 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3531 if (mode != CCmode)
3533 enum insn_code code = optab_handler (mov_optab, CCmode);
3534 if (code != CODE_FOR_nothing)
3536 x = emit_move_change_mode (CCmode, mode, x, true);
3537 y = emit_move_change_mode (CCmode, mode, y, true);
3538 return emit_insn (GEN_FCN (code) (x, y));
3542 /* Otherwise, find the MODE_INT mode of the same width. */
3543 ret = emit_move_via_integer (mode, x, y, false);
3544 gcc_assert (ret != NULL);
3545 return ret;
3548 /* Return true if word I of OP lies entirely in the
3549 undefined bits of a paradoxical subreg. */
3551 static bool
3552 undefined_operand_subword_p (const_rtx op, int i)
3554 if (GET_CODE (op) != SUBREG)
3555 return false;
3556 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3557 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3558 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3559 || known_le (offset, -UNITS_PER_WORD));
3562 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3563 MODE is any multi-word or full-word mode that lacks a move_insn
3564 pattern. Note that you will get better code if you define such
3565 patterns, even if they must turn into multiple assembler instructions. */
3567 static rtx_insn *
3568 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3570 rtx_insn *last_insn = 0;
3571 rtx_insn *seq;
3572 rtx inner;
3573 bool need_clobber;
3574 int i;
3576 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3578 /* If X is a push on the stack, do the push now and replace
3579 X with a reference to the stack pointer. */
3580 if (push_operand (x, mode))
3581 x = emit_move_resolve_push (mode, x);
3583 /* If we are in reload, see if either operand is a MEM whose address
3584 is scheduled for replacement. */
3585 if (reload_in_progress && MEM_P (x)
3586 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3587 x = replace_equiv_address_nv (x, inner);
3588 if (reload_in_progress && MEM_P (y)
3589 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3590 y = replace_equiv_address_nv (y, inner);
3592 start_sequence ();
3594 need_clobber = false;
3595 for (i = 0;
3596 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3597 i++)
3599 rtx xpart = operand_subword (x, i, 1, mode);
3600 rtx ypart;
3602 /* Do not generate code for a move if it would come entirely
3603 from the undefined bits of a paradoxical subreg. */
3604 if (undefined_operand_subword_p (y, i))
3605 continue;
3607 ypart = operand_subword (y, i, 1, mode);
3609 /* If we can't get a part of Y, put Y into memory if it is a
3610 constant. Otherwise, force it into a register. Then we must
3611 be able to get a part of Y. */
3612 if (ypart == 0 && CONSTANT_P (y))
3614 y = use_anchored_address (force_const_mem (mode, y));
3615 ypart = operand_subword (y, i, 1, mode);
3617 else if (ypart == 0)
3618 ypart = operand_subword_force (y, i, mode);
3620 gcc_assert (xpart && ypart);
3622 need_clobber |= (GET_CODE (xpart) == SUBREG);
3624 last_insn = emit_move_insn (xpart, ypart);
3627 seq = get_insns ();
3628 end_sequence ();
3630 /* Show the output dies here. This is necessary for SUBREGs
3631 of pseudos since we cannot track their lifetimes correctly;
3632 hard regs shouldn't appear here except as return values.
3633 We never want to emit such a clobber after reload. */
3634 if (x != y
3635 && ! (reload_in_progress || reload_completed)
3636 && need_clobber != 0)
3637 emit_clobber (x);
3639 emit_insn (seq);
3641 return last_insn;
3644 /* Low level part of emit_move_insn.
3645 Called just like emit_move_insn, but assumes X and Y
3646 are basically valid. */
3648 rtx_insn *
3649 emit_move_insn_1 (rtx x, rtx y)
3651 machine_mode mode = GET_MODE (x);
3652 enum insn_code code;
3654 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3656 code = optab_handler (mov_optab, mode);
3657 if (code != CODE_FOR_nothing)
3658 return emit_insn (GEN_FCN (code) (x, y));
3660 /* Expand complex moves by moving real part and imag part. */
3661 if (COMPLEX_MODE_P (mode))
3662 return emit_move_complex (mode, x, y);
3664 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3665 || ALL_FIXED_POINT_MODE_P (mode))
3667 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3669 /* If we can't find an integer mode, use multi words. */
3670 if (result)
3671 return result;
3672 else
3673 return emit_move_multi_word (mode, x, y);
3676 if (GET_MODE_CLASS (mode) == MODE_CC)
3677 return emit_move_ccmode (mode, x, y);
3679 /* Try using a move pattern for the corresponding integer mode. This is
3680 only safe when simplify_subreg can convert MODE constants into integer
3681 constants. At present, it can only do this reliably if the value
3682 fits within a HOST_WIDE_INT. */
3683 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3685 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3687 if (ret)
3689 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3690 return ret;
3694 return emit_move_multi_word (mode, x, y);
3697 /* Generate code to copy Y into X.
3698 Both Y and X must have the same mode, except that
3699 Y can be a constant with VOIDmode.
3700 This mode cannot be BLKmode; use emit_block_move for that.
3702 Return the last instruction emitted. */
3704 rtx_insn *
3705 emit_move_insn (rtx x, rtx y)
3707 machine_mode mode = GET_MODE (x);
3708 rtx y_cst = NULL_RTX;
3709 rtx_insn *last_insn;
3710 rtx set;
3712 gcc_assert (mode != BLKmode
3713 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3715 if (CONSTANT_P (y))
3717 if (optimize
3718 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3719 && (last_insn = compress_float_constant (x, y)))
3720 return last_insn;
3722 y_cst = y;
3724 if (!targetm.legitimate_constant_p (mode, y))
3726 y = force_const_mem (mode, y);
3728 /* If the target's cannot_force_const_mem prevented the spill,
3729 assume that the target's move expanders will also take care
3730 of the non-legitimate constant. */
3731 if (!y)
3732 y = y_cst;
3733 else
3734 y = use_anchored_address (y);
3738 /* If X or Y are memory references, verify that their addresses are valid
3739 for the machine. */
3740 if (MEM_P (x)
3741 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3742 MEM_ADDR_SPACE (x))
3743 && ! push_operand (x, GET_MODE (x))))
3744 x = validize_mem (x);
3746 if (MEM_P (y)
3747 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3748 MEM_ADDR_SPACE (y)))
3749 y = validize_mem (y);
3751 gcc_assert (mode != BLKmode);
3753 last_insn = emit_move_insn_1 (x, y);
3755 if (y_cst && REG_P (x)
3756 && (set = single_set (last_insn)) != NULL_RTX
3757 && SET_DEST (set) == x
3758 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3759 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3761 return last_insn;
3764 /* Generate the body of an instruction to copy Y into X.
3765 It may be a list of insns, if one insn isn't enough. */
3767 rtx_insn *
3768 gen_move_insn (rtx x, rtx y)
3770 rtx_insn *seq;
3772 start_sequence ();
3773 emit_move_insn_1 (x, y);
3774 seq = get_insns ();
3775 end_sequence ();
3776 return seq;
3779 /* If Y is representable exactly in a narrower mode, and the target can
3780 perform the extension directly from constant or memory, then emit the
3781 move as an extension. */
3783 static rtx_insn *
3784 compress_float_constant (rtx x, rtx y)
3786 machine_mode dstmode = GET_MODE (x);
3787 machine_mode orig_srcmode = GET_MODE (y);
3788 machine_mode srcmode;
3789 const REAL_VALUE_TYPE *r;
3790 int oldcost, newcost;
3791 bool speed = optimize_insn_for_speed_p ();
3793 r = CONST_DOUBLE_REAL_VALUE (y);
3795 if (targetm.legitimate_constant_p (dstmode, y))
3796 oldcost = set_src_cost (y, orig_srcmode, speed);
3797 else
3798 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3800 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3802 enum insn_code ic;
3803 rtx trunc_y;
3804 rtx_insn *last_insn;
3806 /* Skip if the target can't extend this way. */
3807 ic = can_extend_p (dstmode, srcmode, 0);
3808 if (ic == CODE_FOR_nothing)
3809 continue;
3811 /* Skip if the narrowed value isn't exact. */
3812 if (! exact_real_truncate (srcmode, r))
3813 continue;
3815 trunc_y = const_double_from_real_value (*r, srcmode);
3817 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3819 /* Skip if the target needs extra instructions to perform
3820 the extension. */
3821 if (!insn_operand_matches (ic, 1, trunc_y))
3822 continue;
3823 /* This is valid, but may not be cheaper than the original. */
3824 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3825 dstmode, speed);
3826 if (oldcost < newcost)
3827 continue;
3829 else if (float_extend_from_mem[dstmode][srcmode])
3831 trunc_y = force_const_mem (srcmode, trunc_y);
3832 /* This is valid, but may not be cheaper than the original. */
3833 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3834 dstmode, speed);
3835 if (oldcost < newcost)
3836 continue;
3837 trunc_y = validize_mem (trunc_y);
3839 else
3840 continue;
3842 /* For CSE's benefit, force the compressed constant pool entry
3843 into a new pseudo. This constant may be used in different modes,
3844 and if not, combine will put things back together for us. */
3845 trunc_y = force_reg (srcmode, trunc_y);
3847 /* If x is a hard register, perform the extension into a pseudo,
3848 so that e.g. stack realignment code is aware of it. */
3849 rtx target = x;
3850 if (REG_P (x) && HARD_REGISTER_P (x))
3851 target = gen_reg_rtx (dstmode);
3853 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3854 last_insn = get_last_insn ();
3856 if (REG_P (target))
3857 set_unique_reg_note (last_insn, REG_EQUAL, y);
3859 if (target != x)
3860 return emit_move_insn (x, target);
3861 return last_insn;
3864 return NULL;
3867 /* Pushing data onto the stack. */
3869 /* Push a block of length SIZE (perhaps variable)
3870 and return an rtx to address the beginning of the block.
3871 The value may be virtual_outgoing_args_rtx.
3873 EXTRA is the number of bytes of padding to push in addition to SIZE.
3874 BELOW nonzero means this padding comes at low addresses;
3875 otherwise, the padding comes at high addresses. */
3878 push_block (rtx size, poly_int64 extra, int below)
3880 rtx temp;
3882 size = convert_modes (Pmode, ptr_mode, size, 1);
3883 if (CONSTANT_P (size))
3884 anti_adjust_stack (plus_constant (Pmode, size, extra));
3885 else if (REG_P (size) && known_eq (extra, 0))
3886 anti_adjust_stack (size);
3887 else
3889 temp = copy_to_mode_reg (Pmode, size);
3890 if (maybe_ne (extra, 0))
3891 temp = expand_binop (Pmode, add_optab, temp,
3892 gen_int_mode (extra, Pmode),
3893 temp, 0, OPTAB_LIB_WIDEN);
3894 anti_adjust_stack (temp);
3897 if (STACK_GROWS_DOWNWARD)
3899 temp = virtual_outgoing_args_rtx;
3900 if (maybe_ne (extra, 0) && below)
3901 temp = plus_constant (Pmode, temp, extra);
3903 else
3905 if (CONST_INT_P (size))
3906 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3907 -INTVAL (size) - (below ? 0 : extra));
3908 else if (maybe_ne (extra, 0) && !below)
3909 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3910 negate_rtx (Pmode, plus_constant (Pmode, size,
3911 extra)));
3912 else
3913 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3914 negate_rtx (Pmode, size));
3917 return memory_address (NARROWEST_INT_MODE, temp);
3920 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3922 static rtx
3923 mem_autoinc_base (rtx mem)
3925 if (MEM_P (mem))
3927 rtx addr = XEXP (mem, 0);
3928 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3929 return XEXP (addr, 0);
3931 return NULL;
3934 /* A utility routine used here, in reload, and in try_split. The insns
3935 after PREV up to and including LAST are known to adjust the stack,
3936 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3937 placing notes as appropriate. PREV may be NULL, indicating the
3938 entire insn sequence prior to LAST should be scanned.
3940 The set of allowed stack pointer modifications is small:
3941 (1) One or more auto-inc style memory references (aka pushes),
3942 (2) One or more addition/subtraction with the SP as destination,
3943 (3) A single move insn with the SP as destination,
3944 (4) A call_pop insn,
3945 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3947 Insns in the sequence that do not modify the SP are ignored,
3948 except for noreturn calls.
3950 The return value is the amount of adjustment that can be trivially
3951 verified, via immediate operand or auto-inc. If the adjustment
3952 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
3954 poly_int64
3955 find_args_size_adjust (rtx_insn *insn)
3957 rtx dest, set, pat;
3958 int i;
3960 pat = PATTERN (insn);
3961 set = NULL;
3963 /* Look for a call_pop pattern. */
3964 if (CALL_P (insn))
3966 /* We have to allow non-call_pop patterns for the case
3967 of emit_single_push_insn of a TLS address. */
3968 if (GET_CODE (pat) != PARALLEL)
3969 return 0;
3971 /* All call_pop have a stack pointer adjust in the parallel.
3972 The call itself is always first, and the stack adjust is
3973 usually last, so search from the end. */
3974 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3976 set = XVECEXP (pat, 0, i);
3977 if (GET_CODE (set) != SET)
3978 continue;
3979 dest = SET_DEST (set);
3980 if (dest == stack_pointer_rtx)
3981 break;
3983 /* We'd better have found the stack pointer adjust. */
3984 if (i == 0)
3985 return 0;
3986 /* Fall through to process the extracted SET and DEST
3987 as if it was a standalone insn. */
3989 else if (GET_CODE (pat) == SET)
3990 set = pat;
3991 else if ((set = single_set (insn)) != NULL)
3993 else if (GET_CODE (pat) == PARALLEL)
3995 /* ??? Some older ports use a parallel with a stack adjust
3996 and a store for a PUSH_ROUNDING pattern, rather than a
3997 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3998 /* ??? See h8300 and m68k, pushqi1. */
3999 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4001 set = XVECEXP (pat, 0, i);
4002 if (GET_CODE (set) != SET)
4003 continue;
4004 dest = SET_DEST (set);
4005 if (dest == stack_pointer_rtx)
4006 break;
4008 /* We do not expect an auto-inc of the sp in the parallel. */
4009 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4010 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4011 != stack_pointer_rtx);
4013 if (i < 0)
4014 return 0;
4016 else
4017 return 0;
4019 dest = SET_DEST (set);
4021 /* Look for direct modifications of the stack pointer. */
4022 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4024 /* Look for a trivial adjustment, otherwise assume nothing. */
4025 /* Note that the SPU restore_stack_block pattern refers to
4026 the stack pointer in V4SImode. Consider that non-trivial. */
4027 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4028 && GET_CODE (SET_SRC (set)) == PLUS
4029 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4030 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4031 return INTVAL (XEXP (SET_SRC (set), 1));
4032 /* ??? Reload can generate no-op moves, which will be cleaned
4033 up later. Recognize it and continue searching. */
4034 else if (rtx_equal_p (dest, SET_SRC (set)))
4035 return 0;
4036 else
4037 return HOST_WIDE_INT_MIN;
4039 else
4041 rtx mem, addr;
4043 /* Otherwise only think about autoinc patterns. */
4044 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4046 mem = dest;
4047 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4048 != stack_pointer_rtx);
4050 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4051 mem = SET_SRC (set);
4052 else
4053 return 0;
4055 addr = XEXP (mem, 0);
4056 switch (GET_CODE (addr))
4058 case PRE_INC:
4059 case POST_INC:
4060 return GET_MODE_SIZE (GET_MODE (mem));
4061 case PRE_DEC:
4062 case POST_DEC:
4063 return -GET_MODE_SIZE (GET_MODE (mem));
4064 case PRE_MODIFY:
4065 case POST_MODIFY:
4066 addr = XEXP (addr, 1);
4067 gcc_assert (GET_CODE (addr) == PLUS);
4068 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4069 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4070 return INTVAL (XEXP (addr, 1));
4071 default:
4072 gcc_unreachable ();
4077 poly_int64
4078 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4079 poly_int64 end_args_size)
4081 poly_int64 args_size = end_args_size;
4082 bool saw_unknown = false;
4083 rtx_insn *insn;
4085 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4087 if (!NONDEBUG_INSN_P (insn))
4088 continue;
4090 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4091 a call argument containing a TLS address that itself requires
4092 a call to __tls_get_addr. The handling of stack_pointer_delta
4093 in emit_single_push_insn is supposed to ensure that any such
4094 notes are already correct. */
4095 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4096 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4098 poly_int64 this_delta = find_args_size_adjust (insn);
4099 if (known_eq (this_delta, 0))
4101 if (!CALL_P (insn)
4102 || ACCUMULATE_OUTGOING_ARGS
4103 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4104 continue;
4107 gcc_assert (!saw_unknown);
4108 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4109 saw_unknown = true;
4111 if (!note)
4112 add_args_size_note (insn, args_size);
4113 if (STACK_GROWS_DOWNWARD)
4114 this_delta = -poly_uint64 (this_delta);
4116 if (saw_unknown)
4117 args_size = HOST_WIDE_INT_MIN;
4118 else
4119 args_size -= this_delta;
4122 return args_size;
4125 #ifdef PUSH_ROUNDING
4126 /* Emit single push insn. */
4128 static void
4129 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4131 rtx dest_addr;
4132 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4133 rtx dest;
4134 enum insn_code icode;
4136 /* If there is push pattern, use it. Otherwise try old way of throwing
4137 MEM representing push operation to move expander. */
4138 icode = optab_handler (push_optab, mode);
4139 if (icode != CODE_FOR_nothing)
4141 struct expand_operand ops[1];
4143 create_input_operand (&ops[0], x, mode);
4144 if (maybe_expand_insn (icode, 1, ops))
4145 return;
4147 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4148 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4149 /* If we are to pad downward, adjust the stack pointer first and
4150 then store X into the stack location using an offset. This is
4151 because emit_move_insn does not know how to pad; it does not have
4152 access to type. */
4153 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4155 emit_move_insn (stack_pointer_rtx,
4156 expand_binop (Pmode,
4157 STACK_GROWS_DOWNWARD ? sub_optab
4158 : add_optab,
4159 stack_pointer_rtx,
4160 gen_int_mode (rounded_size, Pmode),
4161 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4163 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4164 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4165 /* We have already decremented the stack pointer, so get the
4166 previous value. */
4167 offset += rounded_size;
4169 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4170 /* We have already incremented the stack pointer, so get the
4171 previous value. */
4172 offset -= rounded_size;
4174 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4176 else
4178 if (STACK_GROWS_DOWNWARD)
4179 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4180 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4181 else
4182 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4183 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4185 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4188 dest = gen_rtx_MEM (mode, dest_addr);
4190 if (type != 0)
4192 set_mem_attributes (dest, type, 1);
4194 if (cfun->tail_call_marked)
4195 /* Function incoming arguments may overlap with sibling call
4196 outgoing arguments and we cannot allow reordering of reads
4197 from function arguments with stores to outgoing arguments
4198 of sibling calls. */
4199 set_mem_alias_set (dest, 0);
4201 emit_move_insn (dest, x);
4204 /* Emit and annotate a single push insn. */
4206 static void
4207 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4209 poly_int64 delta, old_delta = stack_pointer_delta;
4210 rtx_insn *prev = get_last_insn ();
4211 rtx_insn *last;
4213 emit_single_push_insn_1 (mode, x, type);
4215 /* Adjust stack_pointer_delta to describe the situation after the push
4216 we just performed. Note that we must do this after the push rather
4217 than before the push in case calculating X needs pushes and pops of
4218 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4219 for such pushes and pops must not include the effect of the future
4220 push of X. */
4221 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4223 last = get_last_insn ();
4225 /* Notice the common case where we emitted exactly one insn. */
4226 if (PREV_INSN (last) == prev)
4228 add_args_size_note (last, stack_pointer_delta);
4229 return;
4232 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4233 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4234 || known_eq (delta, old_delta));
4236 #endif
4238 /* If reading SIZE bytes from X will end up reading from
4239 Y return the number of bytes that overlap. Return -1
4240 if there is no overlap or -2 if we can't determine
4241 (for example when X and Y have different base registers). */
4243 static int
4244 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4246 rtx tmp = plus_constant (Pmode, x, size);
4247 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4249 if (!CONST_INT_P (sub))
4250 return -2;
4252 HOST_WIDE_INT val = INTVAL (sub);
4254 return IN_RANGE (val, 1, size) ? val : -1;
4257 /* Generate code to push X onto the stack, assuming it has mode MODE and
4258 type TYPE.
4259 MODE is redundant except when X is a CONST_INT (since they don't
4260 carry mode info).
4261 SIZE is an rtx for the size of data to be copied (in bytes),
4262 needed only if X is BLKmode.
4263 Return true if successful. May return false if asked to push a
4264 partial argument during a sibcall optimization (as specified by
4265 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4266 to not overlap.
4268 ALIGN (in bits) is maximum alignment we can assume.
4270 If PARTIAL and REG are both nonzero, then copy that many of the first
4271 bytes of X into registers starting with REG, and push the rest of X.
4272 The amount of space pushed is decreased by PARTIAL bytes.
4273 REG must be a hard register in this case.
4274 If REG is zero but PARTIAL is not, take any all others actions for an
4275 argument partially in registers, but do not actually load any
4276 registers.
4278 EXTRA is the amount in bytes of extra space to leave next to this arg.
4279 This is ignored if an argument block has already been allocated.
4281 On a machine that lacks real push insns, ARGS_ADDR is the address of
4282 the bottom of the argument block for this call. We use indexing off there
4283 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4284 argument block has not been preallocated.
4286 ARGS_SO_FAR is the size of args previously pushed for this call.
4288 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4289 for arguments passed in registers. If nonzero, it will be the number
4290 of bytes required. */
4292 bool
4293 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4294 unsigned int align, int partial, rtx reg, poly_int64 extra,
4295 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4296 rtx alignment_pad, bool sibcall_p)
4298 rtx xinner;
4299 pad_direction stack_direction
4300 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4302 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4303 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4304 Default is below for small data on big-endian machines; else above. */
4305 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4307 /* Invert direction if stack is post-decrement.
4308 FIXME: why? */
4309 if (STACK_PUSH_CODE == POST_DEC)
4310 if (where_pad != PAD_NONE)
4311 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4313 xinner = x;
4315 int nregs = partial / UNITS_PER_WORD;
4316 rtx *tmp_regs = NULL;
4317 int overlapping = 0;
4319 if (mode == BLKmode
4320 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4322 /* Copy a block into the stack, entirely or partially. */
4324 rtx temp;
4325 int used;
4326 int offset;
4327 int skip;
4329 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4330 used = partial - offset;
4332 if (mode != BLKmode)
4334 /* A value is to be stored in an insufficiently aligned
4335 stack slot; copy via a suitably aligned slot if
4336 necessary. */
4337 size = GEN_INT (GET_MODE_SIZE (mode));
4338 if (!MEM_P (xinner))
4340 temp = assign_temp (type, 1, 1);
4341 emit_move_insn (temp, xinner);
4342 xinner = temp;
4346 gcc_assert (size);
4348 /* USED is now the # of bytes we need not copy to the stack
4349 because registers will take care of them. */
4351 if (partial != 0)
4352 xinner = adjust_address (xinner, BLKmode, used);
4354 /* If the partial register-part of the arg counts in its stack size,
4355 skip the part of stack space corresponding to the registers.
4356 Otherwise, start copying to the beginning of the stack space,
4357 by setting SKIP to 0. */
4358 skip = (reg_parm_stack_space == 0) ? 0 : used;
4360 #ifdef PUSH_ROUNDING
4361 /* Do it with several push insns if that doesn't take lots of insns
4362 and if there is no difficulty with push insns that skip bytes
4363 on the stack for alignment purposes. */
4364 if (args_addr == 0
4365 && PUSH_ARGS
4366 && CONST_INT_P (size)
4367 && skip == 0
4368 && MEM_ALIGN (xinner) >= align
4369 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4370 /* Here we avoid the case of a structure whose weak alignment
4371 forces many pushes of a small amount of data,
4372 and such small pushes do rounding that causes trouble. */
4373 && ((!targetm.slow_unaligned_access (word_mode, align))
4374 || align >= BIGGEST_ALIGNMENT
4375 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4376 align / BITS_PER_UNIT))
4377 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4379 /* Push padding now if padding above and stack grows down,
4380 or if padding below and stack grows up.
4381 But if space already allocated, this has already been done. */
4382 if (maybe_ne (extra, 0)
4383 && args_addr == 0
4384 && where_pad != PAD_NONE
4385 && where_pad != stack_direction)
4386 anti_adjust_stack (gen_int_mode (extra, Pmode));
4388 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4390 else
4391 #endif /* PUSH_ROUNDING */
4393 rtx target;
4395 /* Otherwise make space on the stack and copy the data
4396 to the address of that space. */
4398 /* Deduct words put into registers from the size we must copy. */
4399 if (partial != 0)
4401 if (CONST_INT_P (size))
4402 size = GEN_INT (INTVAL (size) - used);
4403 else
4404 size = expand_binop (GET_MODE (size), sub_optab, size,
4405 gen_int_mode (used, GET_MODE (size)),
4406 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4409 /* Get the address of the stack space.
4410 In this case, we do not deal with EXTRA separately.
4411 A single stack adjust will do. */
4412 if (! args_addr)
4414 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4415 extra = 0;
4417 else if (CONST_INT_P (args_so_far))
4418 temp = memory_address (BLKmode,
4419 plus_constant (Pmode, args_addr,
4420 skip + INTVAL (args_so_far)));
4421 else
4422 temp = memory_address (BLKmode,
4423 plus_constant (Pmode,
4424 gen_rtx_PLUS (Pmode,
4425 args_addr,
4426 args_so_far),
4427 skip));
4429 if (!ACCUMULATE_OUTGOING_ARGS)
4431 /* If the source is referenced relative to the stack pointer,
4432 copy it to another register to stabilize it. We do not need
4433 to do this if we know that we won't be changing sp. */
4435 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4436 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4437 temp = copy_to_reg (temp);
4440 target = gen_rtx_MEM (BLKmode, temp);
4442 /* We do *not* set_mem_attributes here, because incoming arguments
4443 may overlap with sibling call outgoing arguments and we cannot
4444 allow reordering of reads from function arguments with stores
4445 to outgoing arguments of sibling calls. We do, however, want
4446 to record the alignment of the stack slot. */
4447 /* ALIGN may well be better aligned than TYPE, e.g. due to
4448 PARM_BOUNDARY. Assume the caller isn't lying. */
4449 set_mem_align (target, align);
4451 /* If part should go in registers and pushing to that part would
4452 overwrite some of the values that need to go into regs, load the
4453 overlapping values into temporary pseudos to be moved into the hard
4454 regs at the end after the stack pushing has completed.
4455 We cannot load them directly into the hard regs here because
4456 they can be clobbered by the block move expansions.
4457 See PR 65358. */
4459 if (partial > 0 && reg != 0 && mode == BLKmode
4460 && GET_CODE (reg) != PARALLEL)
4462 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4463 if (overlapping > 0)
4465 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4466 overlapping /= UNITS_PER_WORD;
4468 tmp_regs = XALLOCAVEC (rtx, overlapping);
4470 for (int i = 0; i < overlapping; i++)
4471 tmp_regs[i] = gen_reg_rtx (word_mode);
4473 for (int i = 0; i < overlapping; i++)
4474 emit_move_insn (tmp_regs[i],
4475 operand_subword_force (target, i, mode));
4477 else if (overlapping == -1)
4478 overlapping = 0;
4479 /* Could not determine whether there is overlap.
4480 Fail the sibcall. */
4481 else
4483 overlapping = 0;
4484 if (sibcall_p)
4485 return false;
4488 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4491 else if (partial > 0)
4493 /* Scalar partly in registers. */
4495 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4496 int i;
4497 int not_stack;
4498 /* # bytes of start of argument
4499 that we must make space for but need not store. */
4500 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4501 int args_offset = INTVAL (args_so_far);
4502 int skip;
4504 /* Push padding now if padding above and stack grows down,
4505 or if padding below and stack grows up.
4506 But if space already allocated, this has already been done. */
4507 if (maybe_ne (extra, 0)
4508 && args_addr == 0
4509 && where_pad != PAD_NONE
4510 && where_pad != stack_direction)
4511 anti_adjust_stack (gen_int_mode (extra, Pmode));
4513 /* If we make space by pushing it, we might as well push
4514 the real data. Otherwise, we can leave OFFSET nonzero
4515 and leave the space uninitialized. */
4516 if (args_addr == 0)
4517 offset = 0;
4519 /* Now NOT_STACK gets the number of words that we don't need to
4520 allocate on the stack. Convert OFFSET to words too. */
4521 not_stack = (partial - offset) / UNITS_PER_WORD;
4522 offset /= UNITS_PER_WORD;
4524 /* If the partial register-part of the arg counts in its stack size,
4525 skip the part of stack space corresponding to the registers.
4526 Otherwise, start copying to the beginning of the stack space,
4527 by setting SKIP to 0. */
4528 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4530 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4531 x = validize_mem (force_const_mem (mode, x));
4533 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4534 SUBREGs of such registers are not allowed. */
4535 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4536 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4537 x = copy_to_reg (x);
4539 /* Loop over all the words allocated on the stack for this arg. */
4540 /* We can do it by words, because any scalar bigger than a word
4541 has a size a multiple of a word. */
4542 for (i = size - 1; i >= not_stack; i--)
4543 if (i >= not_stack + offset)
4544 if (!emit_push_insn (operand_subword_force (x, i, mode),
4545 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4546 0, args_addr,
4547 GEN_INT (args_offset + ((i - not_stack + skip)
4548 * UNITS_PER_WORD)),
4549 reg_parm_stack_space, alignment_pad, sibcall_p))
4550 return false;
4552 else
4554 rtx addr;
4555 rtx dest;
4557 /* Push padding now if padding above and stack grows down,
4558 or if padding below and stack grows up.
4559 But if space already allocated, this has already been done. */
4560 if (maybe_ne (extra, 0)
4561 && args_addr == 0
4562 && where_pad != PAD_NONE
4563 && where_pad != stack_direction)
4564 anti_adjust_stack (gen_int_mode (extra, Pmode));
4566 #ifdef PUSH_ROUNDING
4567 if (args_addr == 0 && PUSH_ARGS)
4568 emit_single_push_insn (mode, x, type);
4569 else
4570 #endif
4572 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4573 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4575 /* We do *not* set_mem_attributes here, because incoming arguments
4576 may overlap with sibling call outgoing arguments and we cannot
4577 allow reordering of reads from function arguments with stores
4578 to outgoing arguments of sibling calls. We do, however, want
4579 to record the alignment of the stack slot. */
4580 /* ALIGN may well be better aligned than TYPE, e.g. due to
4581 PARM_BOUNDARY. Assume the caller isn't lying. */
4582 set_mem_align (dest, align);
4584 emit_move_insn (dest, x);
4588 /* Move the partial arguments into the registers and any overlapping
4589 values that we moved into the pseudos in tmp_regs. */
4590 if (partial > 0 && reg != 0)
4592 /* Handle calls that pass values in multiple non-contiguous locations.
4593 The Irix 6 ABI has examples of this. */
4594 if (GET_CODE (reg) == PARALLEL)
4595 emit_group_load (reg, x, type, -1);
4596 else
4598 gcc_assert (partial % UNITS_PER_WORD == 0);
4599 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4601 for (int i = 0; i < overlapping; i++)
4602 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4603 + nregs - overlapping + i),
4604 tmp_regs[i]);
4609 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4610 anti_adjust_stack (gen_int_mode (extra, Pmode));
4612 if (alignment_pad && args_addr == 0)
4613 anti_adjust_stack (alignment_pad);
4615 return true;
4618 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4619 operations. */
4621 static rtx
4622 get_subtarget (rtx x)
4624 return (optimize
4625 || x == 0
4626 /* Only registers can be subtargets. */
4627 || !REG_P (x)
4628 /* Don't use hard regs to avoid extending their life. */
4629 || REGNO (x) < FIRST_PSEUDO_REGISTER
4630 ? 0 : x);
4633 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4634 FIELD is a bitfield. Returns true if the optimization was successful,
4635 and there's nothing else to do. */
4637 static bool
4638 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4639 poly_uint64 pbitpos,
4640 poly_uint64 pbitregion_start,
4641 poly_uint64 pbitregion_end,
4642 machine_mode mode1, rtx str_rtx,
4643 tree to, tree src, bool reverse)
4645 machine_mode str_mode = GET_MODE (str_rtx);
4646 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4647 tree op0, op1;
4648 rtx value, result;
4649 optab binop;
4650 gimple *srcstmt;
4651 enum tree_code code;
4653 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4654 if (mode1 != VOIDmode
4655 || !pbitsize.is_constant (&bitsize)
4656 || !pbitpos.is_constant (&bitpos)
4657 || !pbitregion_start.is_constant (&bitregion_start)
4658 || !pbitregion_end.is_constant (&bitregion_end)
4659 || bitsize >= BITS_PER_WORD
4660 || str_bitsize > BITS_PER_WORD
4661 || TREE_SIDE_EFFECTS (to)
4662 || TREE_THIS_VOLATILE (to))
4663 return false;
4665 STRIP_NOPS (src);
4666 if (TREE_CODE (src) != SSA_NAME)
4667 return false;
4668 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4669 return false;
4671 srcstmt = get_gimple_for_ssa_name (src);
4672 if (!srcstmt
4673 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4674 return false;
4676 code = gimple_assign_rhs_code (srcstmt);
4678 op0 = gimple_assign_rhs1 (srcstmt);
4680 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4681 to find its initialization. Hopefully the initialization will
4682 be from a bitfield load. */
4683 if (TREE_CODE (op0) == SSA_NAME)
4685 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4687 /* We want to eventually have OP0 be the same as TO, which
4688 should be a bitfield. */
4689 if (!op0stmt
4690 || !is_gimple_assign (op0stmt)
4691 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4692 return false;
4693 op0 = gimple_assign_rhs1 (op0stmt);
4696 op1 = gimple_assign_rhs2 (srcstmt);
4698 if (!operand_equal_p (to, op0, 0))
4699 return false;
4701 if (MEM_P (str_rtx))
4703 unsigned HOST_WIDE_INT offset1;
4705 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4706 str_bitsize = BITS_PER_WORD;
4708 scalar_int_mode best_mode;
4709 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4710 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4711 return false;
4712 str_mode = best_mode;
4713 str_bitsize = GET_MODE_BITSIZE (best_mode);
4715 offset1 = bitpos;
4716 bitpos %= str_bitsize;
4717 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4718 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4720 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4721 return false;
4722 else
4723 gcc_assert (!reverse);
4725 /* If the bit field covers the whole REG/MEM, store_field
4726 will likely generate better code. */
4727 if (bitsize >= str_bitsize)
4728 return false;
4730 /* We can't handle fields split across multiple entities. */
4731 if (bitpos + bitsize > str_bitsize)
4732 return false;
4734 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4735 bitpos = str_bitsize - bitpos - bitsize;
4737 switch (code)
4739 case PLUS_EXPR:
4740 case MINUS_EXPR:
4741 /* For now, just optimize the case of the topmost bitfield
4742 where we don't need to do any masking and also
4743 1 bit bitfields where xor can be used.
4744 We might win by one instruction for the other bitfields
4745 too if insv/extv instructions aren't used, so that
4746 can be added later. */
4747 if ((reverse || bitpos + bitsize != str_bitsize)
4748 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4749 break;
4751 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4752 value = convert_modes (str_mode,
4753 TYPE_MODE (TREE_TYPE (op1)), value,
4754 TYPE_UNSIGNED (TREE_TYPE (op1)));
4756 /* We may be accessing data outside the field, which means
4757 we can alias adjacent data. */
4758 if (MEM_P (str_rtx))
4760 str_rtx = shallow_copy_rtx (str_rtx);
4761 set_mem_alias_set (str_rtx, 0);
4762 set_mem_expr (str_rtx, 0);
4765 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4767 value = expand_and (str_mode, value, const1_rtx, NULL);
4768 binop = xor_optab;
4770 else
4771 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4773 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4774 if (reverse)
4775 value = flip_storage_order (str_mode, value);
4776 result = expand_binop (str_mode, binop, str_rtx,
4777 value, str_rtx, 1, OPTAB_WIDEN);
4778 if (result != str_rtx)
4779 emit_move_insn (str_rtx, result);
4780 return true;
4782 case BIT_IOR_EXPR:
4783 case BIT_XOR_EXPR:
4784 if (TREE_CODE (op1) != INTEGER_CST)
4785 break;
4786 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4787 value = convert_modes (str_mode,
4788 TYPE_MODE (TREE_TYPE (op1)), value,
4789 TYPE_UNSIGNED (TREE_TYPE (op1)));
4791 /* We may be accessing data outside the field, which means
4792 we can alias adjacent data. */
4793 if (MEM_P (str_rtx))
4795 str_rtx = shallow_copy_rtx (str_rtx);
4796 set_mem_alias_set (str_rtx, 0);
4797 set_mem_expr (str_rtx, 0);
4800 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4801 if (bitpos + bitsize != str_bitsize)
4803 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4804 str_mode);
4805 value = expand_and (str_mode, value, mask, NULL_RTX);
4807 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4808 if (reverse)
4809 value = flip_storage_order (str_mode, value);
4810 result = expand_binop (str_mode, binop, str_rtx,
4811 value, str_rtx, 1, OPTAB_WIDEN);
4812 if (result != str_rtx)
4813 emit_move_insn (str_rtx, result);
4814 return true;
4816 default:
4817 break;
4820 return false;
4823 /* In the C++ memory model, consecutive bit fields in a structure are
4824 considered one memory location.
4826 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4827 returns the bit range of consecutive bits in which this COMPONENT_REF
4828 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4829 and *OFFSET may be adjusted in the process.
4831 If the access does not need to be restricted, 0 is returned in both
4832 *BITSTART and *BITEND. */
4834 void
4835 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
4836 poly_int64_pod *bitpos, tree *offset)
4838 poly_int64 bitoffset;
4839 tree field, repr;
4841 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4843 field = TREE_OPERAND (exp, 1);
4844 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4845 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4846 need to limit the range we can access. */
4847 if (!repr)
4849 *bitstart = *bitend = 0;
4850 return;
4853 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4854 part of a larger bit field, then the representative does not serve any
4855 useful purpose. This can occur in Ada. */
4856 if (handled_component_p (TREE_OPERAND (exp, 0)))
4858 machine_mode rmode;
4859 poly_int64 rbitsize, rbitpos;
4860 tree roffset;
4861 int unsignedp, reversep, volatilep = 0;
4862 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4863 &roffset, &rmode, &unsignedp, &reversep,
4864 &volatilep);
4865 if (!multiple_p (rbitpos, BITS_PER_UNIT))
4867 *bitstart = *bitend = 0;
4868 return;
4872 /* Compute the adjustment to bitpos from the offset of the field
4873 relative to the representative. DECL_FIELD_OFFSET of field and
4874 repr are the same by construction if they are not constants,
4875 see finish_bitfield_layout. */
4876 poly_uint64 field_offset, repr_offset;
4877 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
4878 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
4879 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
4880 else
4881 bitoffset = 0;
4882 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4883 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4885 /* If the adjustment is larger than bitpos, we would have a negative bit
4886 position for the lower bound and this may wreak havoc later. Adjust
4887 offset and bitpos to make the lower bound non-negative in that case. */
4888 if (maybe_gt (bitoffset, *bitpos))
4890 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
4891 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
4893 *bitpos += adjust_bits;
4894 if (*offset == NULL_TREE)
4895 *offset = size_int (-adjust_bytes);
4896 else
4897 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
4898 *bitstart = 0;
4900 else
4901 *bitstart = *bitpos - bitoffset;
4903 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4906 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4907 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4908 DECL_RTL was not set yet, return NORTL. */
4910 static inline bool
4911 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4913 if (TREE_CODE (addr) != ADDR_EXPR)
4914 return false;
4916 tree base = TREE_OPERAND (addr, 0);
4918 if (!DECL_P (base)
4919 || TREE_ADDRESSABLE (base)
4920 || DECL_MODE (base) == BLKmode)
4921 return false;
4923 if (!DECL_RTL_SET_P (base))
4924 return nortl;
4926 return (!MEM_P (DECL_RTL (base)));
4929 /* Returns true if the MEM_REF REF refers to an object that does not
4930 reside in memory and has non-BLKmode. */
4932 static inline bool
4933 mem_ref_refers_to_non_mem_p (tree ref)
4935 tree base = TREE_OPERAND (ref, 0);
4936 return addr_expr_of_non_mem_decl_p_1 (base, false);
4939 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4940 is true, try generating a nontemporal store. */
4942 void
4943 expand_assignment (tree to, tree from, bool nontemporal)
4945 rtx to_rtx = 0;
4946 rtx result;
4947 machine_mode mode;
4948 unsigned int align;
4949 enum insn_code icode;
4951 /* Don't crash if the lhs of the assignment was erroneous. */
4952 if (TREE_CODE (to) == ERROR_MARK)
4954 expand_normal (from);
4955 return;
4958 /* Optimize away no-op moves without side-effects. */
4959 if (operand_equal_p (to, from, 0))
4960 return;
4962 /* Handle misaligned stores. */
4963 mode = TYPE_MODE (TREE_TYPE (to));
4964 if ((TREE_CODE (to) == MEM_REF
4965 || TREE_CODE (to) == TARGET_MEM_REF)
4966 && mode != BLKmode
4967 && !mem_ref_refers_to_non_mem_p (to)
4968 && ((align = get_object_alignment (to))
4969 < GET_MODE_ALIGNMENT (mode))
4970 && (((icode = optab_handler (movmisalign_optab, mode))
4971 != CODE_FOR_nothing)
4972 || targetm.slow_unaligned_access (mode, align)))
4974 rtx reg, mem;
4976 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4977 reg = force_not_mem (reg);
4978 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4979 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4980 reg = flip_storage_order (mode, reg);
4982 if (icode != CODE_FOR_nothing)
4984 struct expand_operand ops[2];
4986 create_fixed_operand (&ops[0], mem);
4987 create_input_operand (&ops[1], reg, mode);
4988 /* The movmisalign<mode> pattern cannot fail, else the assignment
4989 would silently be omitted. */
4990 expand_insn (icode, 2, ops);
4992 else
4993 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4994 false);
4995 return;
4998 /* Assignment of a structure component needs special treatment
4999 if the structure component's rtx is not simply a MEM.
5000 Assignment of an array element at a constant index, and assignment of
5001 an array element in an unaligned packed structure field, has the same
5002 problem. Same for (partially) storing into a non-memory object. */
5003 if (handled_component_p (to)
5004 || (TREE_CODE (to) == MEM_REF
5005 && (REF_REVERSE_STORAGE_ORDER (to)
5006 || mem_ref_refers_to_non_mem_p (to)))
5007 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5009 machine_mode mode1;
5010 poly_int64 bitsize, bitpos;
5011 poly_uint64 bitregion_start = 0;
5012 poly_uint64 bitregion_end = 0;
5013 tree offset;
5014 int unsignedp, reversep, volatilep = 0;
5015 tree tem;
5017 push_temp_slots ();
5018 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5019 &unsignedp, &reversep, &volatilep);
5021 /* Make sure bitpos is not negative, it can wreak havoc later. */
5022 if (maybe_lt (bitpos, 0))
5024 gcc_assert (offset == NULL_TREE);
5025 offset = size_int (bits_to_bytes_round_down (bitpos));
5026 bitpos = num_trailing_bits (bitpos);
5029 if (TREE_CODE (to) == COMPONENT_REF
5030 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5031 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5032 /* The C++ memory model naturally applies to byte-aligned fields.
5033 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5034 BITSIZE are not byte-aligned, there is no need to limit the range
5035 we can access. This can occur with packed structures in Ada. */
5036 else if (maybe_gt (bitsize, 0)
5037 && multiple_p (bitsize, BITS_PER_UNIT)
5038 && multiple_p (bitpos, BITS_PER_UNIT))
5040 bitregion_start = bitpos;
5041 bitregion_end = bitpos + bitsize - 1;
5044 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5046 /* If the field has a mode, we want to access it in the
5047 field's mode, not the computed mode.
5048 If a MEM has VOIDmode (external with incomplete type),
5049 use BLKmode for it instead. */
5050 if (MEM_P (to_rtx))
5052 if (mode1 != VOIDmode)
5053 to_rtx = adjust_address (to_rtx, mode1, 0);
5054 else if (GET_MODE (to_rtx) == VOIDmode)
5055 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5058 if (offset != 0)
5060 machine_mode address_mode;
5061 rtx offset_rtx;
5063 if (!MEM_P (to_rtx))
5065 /* We can get constant negative offsets into arrays with broken
5066 user code. Translate this to a trap instead of ICEing. */
5067 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5068 expand_builtin_trap ();
5069 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5072 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5073 address_mode = get_address_mode (to_rtx);
5074 if (GET_MODE (offset_rtx) != address_mode)
5076 /* We cannot be sure that the RTL in offset_rtx is valid outside
5077 of a memory address context, so force it into a register
5078 before attempting to convert it to the desired mode. */
5079 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5080 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5083 /* If we have an expression in OFFSET_RTX and a non-zero
5084 byte offset in BITPOS, adding the byte offset before the
5085 OFFSET_RTX results in better intermediate code, which makes
5086 later rtl optimization passes perform better.
5088 We prefer intermediate code like this:
5090 r124:DI=r123:DI+0x18
5091 [r124:DI]=r121:DI
5093 ... instead of ...
5095 r124:DI=r123:DI+0x10
5096 [r124:DI+0x8]=r121:DI
5098 This is only done for aligned data values, as these can
5099 be expected to result in single move instructions. */
5100 poly_int64 bytepos;
5101 if (mode1 != VOIDmode
5102 && maybe_ne (bitpos, 0)
5103 && maybe_gt (bitsize, 0)
5104 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5105 && multiple_p (bitpos, bitsize)
5106 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5107 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5109 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5110 bitregion_start = 0;
5111 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5112 bitregion_end -= bitpos;
5113 bitpos = 0;
5116 to_rtx = offset_address (to_rtx, offset_rtx,
5117 highest_pow2_factor_for_target (to,
5118 offset));
5121 /* No action is needed if the target is not a memory and the field
5122 lies completely outside that target. This can occur if the source
5123 code contains an out-of-bounds access to a small array. */
5124 if (!MEM_P (to_rtx)
5125 && GET_MODE (to_rtx) != BLKmode
5126 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5128 expand_normal (from);
5129 result = NULL;
5131 /* Handle expand_expr of a complex value returning a CONCAT. */
5132 else if (GET_CODE (to_rtx) == CONCAT)
5134 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5135 if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE (to_rtx)
5136 && COMPLEX_MODE_P (GET_MODE (to_rtx))
5137 && known_eq (bitpos, 0)
5138 && known_eq (bitsize, mode_bitsize))
5139 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5140 else if (known_eq (bitsize, mode_bitsize / 2)
5141 && (known_eq (bitpos, 0)
5142 || known_eq (bitpos, mode_bitsize / 2)))
5143 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5144 false, nontemporal, reversep);
5145 else if (known_le (bitpos + bitsize, mode_bitsize / 2))
5146 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5147 bitregion_start, bitregion_end,
5148 mode1, from, get_alias_set (to),
5149 nontemporal, reversep);
5150 else if (known_ge (bitpos, mode_bitsize / 2))
5151 result = store_field (XEXP (to_rtx, 1), bitsize,
5152 bitpos - mode_bitsize / 2,
5153 bitregion_start, bitregion_end,
5154 mode1, from, get_alias_set (to),
5155 nontemporal, reversep);
5156 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5158 result = expand_normal (from);
5159 if (GET_CODE (result) == CONCAT)
5161 machine_mode to_mode = GET_MODE_INNER (GET_MODE (to_rtx));
5162 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5163 rtx from_real
5164 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5165 from_mode, 0);
5166 rtx from_imag
5167 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5168 from_mode, 0);
5169 if (!from_real || !from_imag)
5170 goto concat_store_slow;
5171 emit_move_insn (XEXP (to_rtx, 0), from_real);
5172 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5174 else
5176 rtx from_rtx
5177 = simplify_gen_subreg (GET_MODE (to_rtx), result,
5178 TYPE_MODE (TREE_TYPE (from)), 0);
5179 if (from_rtx)
5181 emit_move_insn (XEXP (to_rtx, 0),
5182 read_complex_part (from_rtx, false));
5183 emit_move_insn (XEXP (to_rtx, 1),
5184 read_complex_part (from_rtx, true));
5186 else
5188 machine_mode to_mode
5189 = GET_MODE_INNER (GET_MODE (to_rtx));
5190 rtx from_real
5191 = simplify_gen_subreg (to_mode, result,
5192 TYPE_MODE (TREE_TYPE (from)),
5194 rtx from_imag
5195 = simplify_gen_subreg (to_mode, result,
5196 TYPE_MODE (TREE_TYPE (from)),
5197 GET_MODE_SIZE (to_mode));
5198 if (!from_real || !from_imag)
5199 goto concat_store_slow;
5200 emit_move_insn (XEXP (to_rtx, 0), from_real);
5201 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5205 else
5207 concat_store_slow:;
5208 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5209 GET_MODE_SIZE (GET_MODE (to_rtx)));
5210 write_complex_part (temp, XEXP (to_rtx, 0), false);
5211 write_complex_part (temp, XEXP (to_rtx, 1), true);
5212 result = store_field (temp, bitsize, bitpos,
5213 bitregion_start, bitregion_end,
5214 mode1, from, get_alias_set (to),
5215 nontemporal, reversep);
5216 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5217 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5220 else
5222 if (MEM_P (to_rtx))
5224 /* If the field is at offset zero, we could have been given the
5225 DECL_RTX of the parent struct. Don't munge it. */
5226 to_rtx = shallow_copy_rtx (to_rtx);
5227 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5228 if (volatilep)
5229 MEM_VOLATILE_P (to_rtx) = 1;
5232 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5233 bitregion_start, bitregion_end,
5234 mode1, to_rtx, to, from,
5235 reversep))
5236 result = NULL;
5237 else
5238 result = store_field (to_rtx, bitsize, bitpos,
5239 bitregion_start, bitregion_end,
5240 mode1, from, get_alias_set (to),
5241 nontemporal, reversep);
5244 if (result)
5245 preserve_temp_slots (result);
5246 pop_temp_slots ();
5247 return;
5250 /* If the rhs is a function call and its value is not an aggregate,
5251 call the function before we start to compute the lhs.
5252 This is needed for correct code for cases such as
5253 val = setjmp (buf) on machines where reference to val
5254 requires loading up part of an address in a separate insn.
5256 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5257 since it might be a promoted variable where the zero- or sign- extension
5258 needs to be done. Handling this in the normal way is safe because no
5259 computation is done before the call. The same is true for SSA names. */
5260 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5261 && COMPLETE_TYPE_P (TREE_TYPE (from))
5262 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5263 && ! (((VAR_P (to)
5264 || TREE_CODE (to) == PARM_DECL
5265 || TREE_CODE (to) == RESULT_DECL)
5266 && REG_P (DECL_RTL (to)))
5267 || TREE_CODE (to) == SSA_NAME))
5269 rtx value;
5270 rtx bounds;
5272 push_temp_slots ();
5273 value = expand_normal (from);
5275 /* Split value and bounds to store them separately. */
5276 chkp_split_slot (value, &value, &bounds);
5278 if (to_rtx == 0)
5279 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5281 /* Handle calls that return values in multiple non-contiguous locations.
5282 The Irix 6 ABI has examples of this. */
5283 if (GET_CODE (to_rtx) == PARALLEL)
5285 if (GET_CODE (value) == PARALLEL)
5286 emit_group_move (to_rtx, value);
5287 else
5288 emit_group_load (to_rtx, value, TREE_TYPE (from),
5289 int_size_in_bytes (TREE_TYPE (from)));
5291 else if (GET_CODE (value) == PARALLEL)
5292 emit_group_store (to_rtx, value, TREE_TYPE (from),
5293 int_size_in_bytes (TREE_TYPE (from)));
5294 else if (GET_MODE (to_rtx) == BLKmode)
5296 /* Handle calls that return BLKmode values in registers. */
5297 if (REG_P (value))
5298 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5299 else
5300 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5302 else
5304 if (POINTER_TYPE_P (TREE_TYPE (to)))
5305 value = convert_memory_address_addr_space
5306 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5307 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5309 emit_move_insn (to_rtx, value);
5312 /* Store bounds if required. */
5313 if (bounds
5314 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5316 gcc_assert (MEM_P (to_rtx));
5317 chkp_emit_bounds_store (bounds, value, to_rtx);
5320 preserve_temp_slots (to_rtx);
5321 pop_temp_slots ();
5322 return;
5325 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5326 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5328 /* Don't move directly into a return register. */
5329 if (TREE_CODE (to) == RESULT_DECL
5330 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5332 rtx temp;
5334 push_temp_slots ();
5336 /* If the source is itself a return value, it still is in a pseudo at
5337 this point so we can move it back to the return register directly. */
5338 if (REG_P (to_rtx)
5339 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5340 && TREE_CODE (from) != CALL_EXPR)
5341 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5342 else
5343 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5345 /* Handle calls that return values in multiple non-contiguous locations.
5346 The Irix 6 ABI has examples of this. */
5347 if (GET_CODE (to_rtx) == PARALLEL)
5349 if (GET_CODE (temp) == PARALLEL)
5350 emit_group_move (to_rtx, temp);
5351 else
5352 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5353 int_size_in_bytes (TREE_TYPE (from)));
5355 else if (temp)
5356 emit_move_insn (to_rtx, temp);
5358 preserve_temp_slots (to_rtx);
5359 pop_temp_slots ();
5360 return;
5363 /* In case we are returning the contents of an object which overlaps
5364 the place the value is being stored, use a safe function when copying
5365 a value through a pointer into a structure value return block. */
5366 if (TREE_CODE (to) == RESULT_DECL
5367 && TREE_CODE (from) == INDIRECT_REF
5368 && ADDR_SPACE_GENERIC_P
5369 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5370 && refs_may_alias_p (to, from)
5371 && cfun->returns_struct
5372 && !cfun->returns_pcc_struct)
5374 rtx from_rtx, size;
5376 push_temp_slots ();
5377 size = expr_size (from);
5378 from_rtx = expand_normal (from);
5380 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5382 preserve_temp_slots (to_rtx);
5383 pop_temp_slots ();
5384 return;
5387 /* Compute FROM and store the value in the rtx we got. */
5389 push_temp_slots ();
5390 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5391 preserve_temp_slots (result);
5392 pop_temp_slots ();
5393 return;
5396 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5397 succeeded, false otherwise. */
5399 bool
5400 emit_storent_insn (rtx to, rtx from)
5402 struct expand_operand ops[2];
5403 machine_mode mode = GET_MODE (to);
5404 enum insn_code code = optab_handler (storent_optab, mode);
5406 if (code == CODE_FOR_nothing)
5407 return false;
5409 create_fixed_operand (&ops[0], to);
5410 create_input_operand (&ops[1], from, mode);
5411 return maybe_expand_insn (code, 2, ops);
5414 /* Generate code for computing expression EXP,
5415 and storing the value into TARGET.
5417 If the mode is BLKmode then we may return TARGET itself.
5418 It turns out that in BLKmode it doesn't cause a problem.
5419 because C has no operators that could combine two different
5420 assignments into the same BLKmode object with different values
5421 with no sequence point. Will other languages need this to
5422 be more thorough?
5424 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5425 stack, and block moves may need to be treated specially.
5427 If NONTEMPORAL is true, try using a nontemporal store instruction.
5429 If REVERSE is true, the store is to be done in reverse order.
5431 If BTARGET is not NULL then computed bounds of EXP are
5432 associated with BTARGET. */
5435 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5436 bool nontemporal, bool reverse, tree btarget)
5438 rtx temp;
5439 rtx alt_rtl = NULL_RTX;
5440 location_t loc = curr_insn_location ();
5442 if (VOID_TYPE_P (TREE_TYPE (exp)))
5444 /* C++ can generate ?: expressions with a throw expression in one
5445 branch and an rvalue in the other. Here, we resolve attempts to
5446 store the throw expression's nonexistent result. */
5447 gcc_assert (!call_param_p);
5448 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5449 return NULL_RTX;
5451 if (TREE_CODE (exp) == COMPOUND_EXPR)
5453 /* Perform first part of compound expression, then assign from second
5454 part. */
5455 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5456 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5457 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5458 call_param_p, nontemporal, reverse,
5459 btarget);
5461 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5463 /* For conditional expression, get safe form of the target. Then
5464 test the condition, doing the appropriate assignment on either
5465 side. This avoids the creation of unnecessary temporaries.
5466 For non-BLKmode, it is more efficient not to do this. */
5468 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5470 do_pending_stack_adjust ();
5471 NO_DEFER_POP;
5472 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5473 profile_probability::uninitialized ());
5474 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5475 nontemporal, reverse, btarget);
5476 emit_jump_insn (targetm.gen_jump (lab2));
5477 emit_barrier ();
5478 emit_label (lab1);
5479 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5480 nontemporal, reverse, btarget);
5481 emit_label (lab2);
5482 OK_DEFER_POP;
5484 return NULL_RTX;
5486 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5487 /* If this is a scalar in a register that is stored in a wider mode
5488 than the declared mode, compute the result into its declared mode
5489 and then convert to the wider mode. Our value is the computed
5490 expression. */
5492 rtx inner_target = 0;
5493 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5494 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5496 /* We can do the conversion inside EXP, which will often result
5497 in some optimizations. Do the conversion in two steps: first
5498 change the signedness, if needed, then the extend. But don't
5499 do this if the type of EXP is a subtype of something else
5500 since then the conversion might involve more than just
5501 converting modes. */
5502 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5503 && TREE_TYPE (TREE_TYPE (exp)) == 0
5504 && GET_MODE_PRECISION (outer_mode)
5505 == TYPE_PRECISION (TREE_TYPE (exp)))
5507 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5508 TYPE_UNSIGNED (TREE_TYPE (exp))))
5510 /* Some types, e.g. Fortran's logical*4, won't have a signed
5511 version, so use the mode instead. */
5512 tree ntype
5513 = (signed_or_unsigned_type_for
5514 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5515 if (ntype == NULL)
5516 ntype = lang_hooks.types.type_for_mode
5517 (TYPE_MODE (TREE_TYPE (exp)),
5518 SUBREG_PROMOTED_SIGN (target));
5520 exp = fold_convert_loc (loc, ntype, exp);
5523 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5524 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5525 exp);
5527 inner_target = SUBREG_REG (target);
5530 temp = expand_expr (exp, inner_target, VOIDmode,
5531 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5533 /* Handle bounds returned by call. */
5534 if (TREE_CODE (exp) == CALL_EXPR)
5536 rtx bounds;
5537 chkp_split_slot (temp, &temp, &bounds);
5538 if (bounds && btarget)
5540 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5541 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5542 chkp_set_rtl_bounds (btarget, tmp);
5546 /* If TEMP is a VOIDmode constant, use convert_modes to make
5547 sure that we properly convert it. */
5548 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5550 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5551 temp, SUBREG_PROMOTED_SIGN (target));
5552 temp = convert_modes (inner_mode, outer_mode, temp,
5553 SUBREG_PROMOTED_SIGN (target));
5556 convert_move (SUBREG_REG (target), temp,
5557 SUBREG_PROMOTED_SIGN (target));
5559 return NULL_RTX;
5561 else if ((TREE_CODE (exp) == STRING_CST
5562 || (TREE_CODE (exp) == MEM_REF
5563 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5564 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5565 == STRING_CST
5566 && integer_zerop (TREE_OPERAND (exp, 1))))
5567 && !nontemporal && !call_param_p
5568 && MEM_P (target))
5570 /* Optimize initialization of an array with a STRING_CST. */
5571 HOST_WIDE_INT exp_len, str_copy_len;
5572 rtx dest_mem;
5573 tree str = TREE_CODE (exp) == STRING_CST
5574 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5576 exp_len = int_expr_size (exp);
5577 if (exp_len <= 0)
5578 goto normal_expr;
5580 if (TREE_STRING_LENGTH (str) <= 0)
5581 goto normal_expr;
5583 str_copy_len = strlen (TREE_STRING_POINTER (str));
5584 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5585 goto normal_expr;
5587 str_copy_len = TREE_STRING_LENGTH (str);
5588 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5589 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5591 str_copy_len += STORE_MAX_PIECES - 1;
5592 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5594 str_copy_len = MIN (str_copy_len, exp_len);
5595 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5596 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5597 MEM_ALIGN (target), false))
5598 goto normal_expr;
5600 dest_mem = target;
5602 dest_mem = store_by_pieces (dest_mem,
5603 str_copy_len, builtin_strncpy_read_str,
5604 CONST_CAST (char *,
5605 TREE_STRING_POINTER (str)),
5606 MEM_ALIGN (target), false,
5607 exp_len > str_copy_len ? 1 : 0);
5608 if (exp_len > str_copy_len)
5609 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5610 GEN_INT (exp_len - str_copy_len),
5611 BLOCK_OP_NORMAL);
5612 return NULL_RTX;
5614 else
5616 rtx tmp_target;
5618 normal_expr:
5619 /* If we want to use a nontemporal or a reverse order store, force the
5620 value into a register first. */
5621 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5622 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5623 (call_param_p
5624 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5625 &alt_rtl, false);
5627 /* Handle bounds returned by call. */
5628 if (TREE_CODE (exp) == CALL_EXPR)
5630 rtx bounds;
5631 chkp_split_slot (temp, &temp, &bounds);
5632 if (bounds && btarget)
5634 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5635 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5636 chkp_set_rtl_bounds (btarget, tmp);
5641 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5642 the same as that of TARGET, adjust the constant. This is needed, for
5643 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5644 only a word-sized value. */
5645 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5646 && TREE_CODE (exp) != ERROR_MARK
5647 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5649 if (GET_MODE_CLASS (GET_MODE (target))
5650 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
5651 && GET_MODE_BITSIZE (GET_MODE (target))
5652 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp))))
5654 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
5655 TYPE_MODE (TREE_TYPE (exp)), 0);
5656 if (t)
5657 temp = t;
5659 if (GET_MODE (temp) == VOIDmode)
5660 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5661 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5664 /* If value was not generated in the target, store it there.
5665 Convert the value to TARGET's type first if necessary and emit the
5666 pending incrementations that have been queued when expanding EXP.
5667 Note that we cannot emit the whole queue blindly because this will
5668 effectively disable the POST_INC optimization later.
5670 If TEMP and TARGET compare equal according to rtx_equal_p, but
5671 one or both of them are volatile memory refs, we have to distinguish
5672 two cases:
5673 - expand_expr has used TARGET. In this case, we must not generate
5674 another copy. This can be detected by TARGET being equal according
5675 to == .
5676 - expand_expr has not used TARGET - that means that the source just
5677 happens to have the same RTX form. Since temp will have been created
5678 by expand_expr, it will compare unequal according to == .
5679 We must generate a copy in this case, to reach the correct number
5680 of volatile memory references. */
5682 if ((! rtx_equal_p (temp, target)
5683 || (temp != target && (side_effects_p (temp)
5684 || side_effects_p (target))))
5685 && TREE_CODE (exp) != ERROR_MARK
5686 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5687 but TARGET is not valid memory reference, TEMP will differ
5688 from TARGET although it is really the same location. */
5689 && !(alt_rtl
5690 && rtx_equal_p (alt_rtl, target)
5691 && !side_effects_p (alt_rtl)
5692 && !side_effects_p (target))
5693 /* If there's nothing to copy, don't bother. Don't call
5694 expr_size unless necessary, because some front-ends (C++)
5695 expr_size-hook must not be given objects that are not
5696 supposed to be bit-copied or bit-initialized. */
5697 && expr_size (exp) != const0_rtx)
5699 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5701 if (GET_MODE (target) == BLKmode)
5703 /* Handle calls that return BLKmode values in registers. */
5704 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5705 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5706 else
5707 store_bit_field (target,
5708 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5709 0, 0, 0, GET_MODE (temp), temp, reverse);
5711 else
5712 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5715 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5717 /* Handle copying a string constant into an array. The string
5718 constant may be shorter than the array. So copy just the string's
5719 actual length, and clear the rest. First get the size of the data
5720 type of the string, which is actually the size of the target. */
5721 rtx size = expr_size (exp);
5723 if (CONST_INT_P (size)
5724 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5725 emit_block_move (target, temp, size,
5726 (call_param_p
5727 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5728 else
5730 machine_mode pointer_mode
5731 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5732 machine_mode address_mode = get_address_mode (target);
5734 /* Compute the size of the data to copy from the string. */
5735 tree copy_size
5736 = size_binop_loc (loc, MIN_EXPR,
5737 make_tree (sizetype, size),
5738 size_int (TREE_STRING_LENGTH (exp)));
5739 rtx copy_size_rtx
5740 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5741 (call_param_p
5742 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5743 rtx_code_label *label = 0;
5745 /* Copy that much. */
5746 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5747 TYPE_UNSIGNED (sizetype));
5748 emit_block_move (target, temp, copy_size_rtx,
5749 (call_param_p
5750 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5752 /* Figure out how much is left in TARGET that we have to clear.
5753 Do all calculations in pointer_mode. */
5754 if (CONST_INT_P (copy_size_rtx))
5756 size = plus_constant (address_mode, size,
5757 -INTVAL (copy_size_rtx));
5758 target = adjust_address (target, BLKmode,
5759 INTVAL (copy_size_rtx));
5761 else
5763 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5764 copy_size_rtx, NULL_RTX, 0,
5765 OPTAB_LIB_WIDEN);
5767 if (GET_MODE (copy_size_rtx) != address_mode)
5768 copy_size_rtx = convert_to_mode (address_mode,
5769 copy_size_rtx,
5770 TYPE_UNSIGNED (sizetype));
5772 target = offset_address (target, copy_size_rtx,
5773 highest_pow2_factor (copy_size));
5774 label = gen_label_rtx ();
5775 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5776 GET_MODE (size), 0, label);
5779 if (size != const0_rtx)
5780 clear_storage (target, size, BLOCK_OP_NORMAL);
5782 if (label)
5783 emit_label (label);
5786 /* Handle calls that return values in multiple non-contiguous locations.
5787 The Irix 6 ABI has examples of this. */
5788 else if (GET_CODE (target) == PARALLEL)
5790 if (GET_CODE (temp) == PARALLEL)
5791 emit_group_move (target, temp);
5792 else
5793 emit_group_load (target, temp, TREE_TYPE (exp),
5794 int_size_in_bytes (TREE_TYPE (exp)));
5796 else if (GET_CODE (temp) == PARALLEL)
5797 emit_group_store (target, temp, TREE_TYPE (exp),
5798 int_size_in_bytes (TREE_TYPE (exp)));
5799 else if (GET_MODE (temp) == BLKmode)
5800 emit_block_move (target, temp, expr_size (exp),
5801 (call_param_p
5802 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5803 /* If we emit a nontemporal store, there is nothing else to do. */
5804 else if (nontemporal && emit_storent_insn (target, temp))
5806 else
5808 if (reverse)
5809 temp = flip_storage_order (GET_MODE (target), temp);
5810 temp = force_operand (temp, target);
5811 if (temp != target)
5812 emit_move_insn (target, temp);
5816 return NULL_RTX;
5819 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5821 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5822 bool reverse)
5824 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5825 reverse, NULL);
5828 /* Return true if field F of structure TYPE is a flexible array. */
5830 static bool
5831 flexible_array_member_p (const_tree f, const_tree type)
5833 const_tree tf;
5835 tf = TREE_TYPE (f);
5836 return (DECL_CHAIN (f) == NULL
5837 && TREE_CODE (tf) == ARRAY_TYPE
5838 && TYPE_DOMAIN (tf)
5839 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5840 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5841 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5842 && int_size_in_bytes (type) >= 0);
5845 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5846 must have in order for it to completely initialize a value of type TYPE.
5847 Return -1 if the number isn't known.
5849 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5851 static HOST_WIDE_INT
5852 count_type_elements (const_tree type, bool for_ctor_p)
5854 switch (TREE_CODE (type))
5856 case ARRAY_TYPE:
5858 tree nelts;
5860 nelts = array_type_nelts (type);
5861 if (nelts && tree_fits_uhwi_p (nelts))
5863 unsigned HOST_WIDE_INT n;
5865 n = tree_to_uhwi (nelts) + 1;
5866 if (n == 0 || for_ctor_p)
5867 return n;
5868 else
5869 return n * count_type_elements (TREE_TYPE (type), false);
5871 return for_ctor_p ? -1 : 1;
5874 case RECORD_TYPE:
5876 unsigned HOST_WIDE_INT n;
5877 tree f;
5879 n = 0;
5880 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5881 if (TREE_CODE (f) == FIELD_DECL)
5883 if (!for_ctor_p)
5884 n += count_type_elements (TREE_TYPE (f), false);
5885 else if (!flexible_array_member_p (f, type))
5886 /* Don't count flexible arrays, which are not supposed
5887 to be initialized. */
5888 n += 1;
5891 return n;
5894 case UNION_TYPE:
5895 case QUAL_UNION_TYPE:
5897 tree f;
5898 HOST_WIDE_INT n, m;
5900 gcc_assert (!for_ctor_p);
5901 /* Estimate the number of scalars in each field and pick the
5902 maximum. Other estimates would do instead; the idea is simply
5903 to make sure that the estimate is not sensitive to the ordering
5904 of the fields. */
5905 n = 1;
5906 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5907 if (TREE_CODE (f) == FIELD_DECL)
5909 m = count_type_elements (TREE_TYPE (f), false);
5910 /* If the field doesn't span the whole union, add an extra
5911 scalar for the rest. */
5912 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5913 TYPE_SIZE (type)) != 1)
5914 m++;
5915 if (n < m)
5916 n = m;
5918 return n;
5921 case COMPLEX_TYPE:
5922 return 2;
5924 case VECTOR_TYPE:
5925 return TYPE_VECTOR_SUBPARTS (type);
5927 case INTEGER_TYPE:
5928 case REAL_TYPE:
5929 case FIXED_POINT_TYPE:
5930 case ENUMERAL_TYPE:
5931 case BOOLEAN_TYPE:
5932 case POINTER_TYPE:
5933 case OFFSET_TYPE:
5934 case REFERENCE_TYPE:
5935 case NULLPTR_TYPE:
5936 return 1;
5938 case ERROR_MARK:
5939 return 0;
5941 case VOID_TYPE:
5942 case METHOD_TYPE:
5943 case FUNCTION_TYPE:
5944 case LANG_TYPE:
5945 default:
5946 gcc_unreachable ();
5950 /* Helper for categorize_ctor_elements. Identical interface. */
5952 static bool
5953 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5954 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5956 unsigned HOST_WIDE_INT idx;
5957 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5958 tree value, purpose, elt_type;
5960 /* Whether CTOR is a valid constant initializer, in accordance with what
5961 initializer_constant_valid_p does. If inferred from the constructor
5962 elements, true until proven otherwise. */
5963 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5964 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5966 nz_elts = 0;
5967 init_elts = 0;
5968 num_fields = 0;
5969 elt_type = NULL_TREE;
5971 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5973 HOST_WIDE_INT mult = 1;
5975 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5977 tree lo_index = TREE_OPERAND (purpose, 0);
5978 tree hi_index = TREE_OPERAND (purpose, 1);
5980 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5981 mult = (tree_to_uhwi (hi_index)
5982 - tree_to_uhwi (lo_index) + 1);
5984 num_fields += mult;
5985 elt_type = TREE_TYPE (value);
5987 switch (TREE_CODE (value))
5989 case CONSTRUCTOR:
5991 HOST_WIDE_INT nz = 0, ic = 0;
5993 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5994 p_complete);
5996 nz_elts += mult * nz;
5997 init_elts += mult * ic;
5999 if (const_from_elts_p && const_p)
6000 const_p = const_elt_p;
6002 break;
6004 case INTEGER_CST:
6005 case REAL_CST:
6006 case FIXED_CST:
6007 if (!initializer_zerop (value))
6008 nz_elts += mult;
6009 init_elts += mult;
6010 break;
6012 case STRING_CST:
6013 nz_elts += mult * TREE_STRING_LENGTH (value);
6014 init_elts += mult * TREE_STRING_LENGTH (value);
6015 break;
6017 case COMPLEX_CST:
6018 if (!initializer_zerop (TREE_REALPART (value)))
6019 nz_elts += mult;
6020 if (!initializer_zerop (TREE_IMAGPART (value)))
6021 nz_elts += mult;
6022 init_elts += mult;
6023 break;
6025 case VECTOR_CST:
6027 unsigned i;
6028 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
6030 tree v = VECTOR_CST_ELT (value, i);
6031 if (!initializer_zerop (v))
6032 nz_elts += mult;
6033 init_elts += mult;
6036 break;
6038 default:
6040 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6041 nz_elts += mult * tc;
6042 init_elts += mult * tc;
6044 if (const_from_elts_p && const_p)
6045 const_p
6046 = initializer_constant_valid_p (value,
6047 elt_type,
6048 TYPE_REVERSE_STORAGE_ORDER
6049 (TREE_TYPE (ctor)))
6050 != NULL_TREE;
6052 break;
6056 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6057 num_fields, elt_type))
6058 *p_complete = false;
6060 *p_nz_elts += nz_elts;
6061 *p_init_elts += init_elts;
6063 return const_p;
6066 /* Examine CTOR to discover:
6067 * how many scalar fields are set to nonzero values,
6068 and place it in *P_NZ_ELTS;
6069 * how many scalar fields in total are in CTOR,
6070 and place it in *P_ELT_COUNT.
6071 * whether the constructor is complete -- in the sense that every
6072 meaningful byte is explicitly given a value --
6073 and place it in *P_COMPLETE.
6075 Return whether or not CTOR is a valid static constant initializer, the same
6076 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6078 bool
6079 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6080 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6082 *p_nz_elts = 0;
6083 *p_init_elts = 0;
6084 *p_complete = true;
6086 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6089 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6090 of which had type LAST_TYPE. Each element was itself a complete
6091 initializer, in the sense that every meaningful byte was explicitly
6092 given a value. Return true if the same is true for the constructor
6093 as a whole. */
6095 bool
6096 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6097 const_tree last_type)
6099 if (TREE_CODE (type) == UNION_TYPE
6100 || TREE_CODE (type) == QUAL_UNION_TYPE)
6102 if (num_elts == 0)
6103 return false;
6105 gcc_assert (num_elts == 1 && last_type);
6107 /* ??? We could look at each element of the union, and find the
6108 largest element. Which would avoid comparing the size of the
6109 initialized element against any tail padding in the union.
6110 Doesn't seem worth the effort... */
6111 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6114 return count_type_elements (type, true) == num_elts;
6117 /* Return 1 if EXP contains mostly (3/4) zeros. */
6119 static int
6120 mostly_zeros_p (const_tree exp)
6122 if (TREE_CODE (exp) == CONSTRUCTOR)
6124 HOST_WIDE_INT nz_elts, init_elts;
6125 bool complete_p;
6127 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6128 return !complete_p || nz_elts < init_elts / 4;
6131 return initializer_zerop (exp);
6134 /* Return 1 if EXP contains all zeros. */
6136 static int
6137 all_zeros_p (const_tree exp)
6139 if (TREE_CODE (exp) == CONSTRUCTOR)
6141 HOST_WIDE_INT nz_elts, init_elts;
6142 bool complete_p;
6144 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6145 return nz_elts == 0;
6148 return initializer_zerop (exp);
6151 /* Helper function for store_constructor.
6152 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6153 CLEARED is as for store_constructor.
6154 ALIAS_SET is the alias set to use for any stores.
6155 If REVERSE is true, the store is to be done in reverse order.
6157 This provides a recursive shortcut back to store_constructor when it isn't
6158 necessary to go through store_field. This is so that we can pass through
6159 the cleared field to let store_constructor know that we may not have to
6160 clear a substructure if the outer structure has already been cleared. */
6162 static void
6163 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6164 poly_uint64 bitregion_start,
6165 poly_uint64 bitregion_end,
6166 machine_mode mode,
6167 tree exp, int cleared,
6168 alias_set_type alias_set, bool reverse)
6170 poly_int64 bytepos;
6171 poly_uint64 bytesize;
6172 if (TREE_CODE (exp) == CONSTRUCTOR
6173 /* We can only call store_constructor recursively if the size and
6174 bit position are on a byte boundary. */
6175 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6176 && maybe_ne (bitsize, 0U)
6177 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6178 /* If we have a nonzero bitpos for a register target, then we just
6179 let store_field do the bitfield handling. This is unlikely to
6180 generate unnecessary clear instructions anyways. */
6181 && (known_eq (bitpos, 0) || MEM_P (target)))
6183 if (MEM_P (target))
6185 machine_mode target_mode = GET_MODE (target);
6186 if (target_mode != BLKmode
6187 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6188 target_mode = BLKmode;
6189 target = adjust_address (target, target_mode, bytepos);
6193 /* Update the alias set, if required. */
6194 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6195 && MEM_ALIAS_SET (target) != 0)
6197 target = copy_rtx (target);
6198 set_mem_alias_set (target, alias_set);
6201 store_constructor (exp, target, cleared, bytesize, reverse);
6203 else
6204 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6205 exp, alias_set, false, reverse);
6209 /* Returns the number of FIELD_DECLs in TYPE. */
6211 static int
6212 fields_length (const_tree type)
6214 tree t = TYPE_FIELDS (type);
6215 int count = 0;
6217 for (; t; t = DECL_CHAIN (t))
6218 if (TREE_CODE (t) == FIELD_DECL)
6219 ++count;
6221 return count;
6225 /* Store the value of constructor EXP into the rtx TARGET.
6226 TARGET is either a REG or a MEM; we know it cannot conflict, since
6227 safe_from_p has been called.
6228 CLEARED is true if TARGET is known to have been zero'd.
6229 SIZE is the number of bytes of TARGET we are allowed to modify: this
6230 may not be the same as the size of EXP if we are assigning to a field
6231 which has been packed to exclude padding bits.
6232 If REVERSE is true, the store is to be done in reverse order. */
6234 static void
6235 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6236 bool reverse)
6238 tree type = TREE_TYPE (exp);
6239 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6240 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6242 switch (TREE_CODE (type))
6244 case RECORD_TYPE:
6245 case UNION_TYPE:
6246 case QUAL_UNION_TYPE:
6248 unsigned HOST_WIDE_INT idx;
6249 tree field, value;
6251 /* The storage order is specified for every aggregate type. */
6252 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6254 /* If size is zero or the target is already cleared, do nothing. */
6255 if (known_eq (size, 0) || cleared)
6256 cleared = 1;
6257 /* We either clear the aggregate or indicate the value is dead. */
6258 else if ((TREE_CODE (type) == UNION_TYPE
6259 || TREE_CODE (type) == QUAL_UNION_TYPE)
6260 && ! CONSTRUCTOR_ELTS (exp))
6261 /* If the constructor is empty, clear the union. */
6263 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6264 cleared = 1;
6267 /* If we are building a static constructor into a register,
6268 set the initial value as zero so we can fold the value into
6269 a constant. But if more than one register is involved,
6270 this probably loses. */
6271 else if (REG_P (target) && TREE_STATIC (exp)
6272 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6273 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6275 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6276 cleared = 1;
6279 /* If the constructor has fewer fields than the structure or
6280 if we are initializing the structure to mostly zeros, clear
6281 the whole structure first. Don't do this if TARGET is a
6282 register whose mode size isn't equal to SIZE since
6283 clear_storage can't handle this case. */
6284 else if (known_size_p (size)
6285 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6286 || mostly_zeros_p (exp))
6287 && (!REG_P (target)
6288 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6290 clear_storage (target, gen_int_mode (size, Pmode),
6291 BLOCK_OP_NORMAL);
6292 cleared = 1;
6295 if (REG_P (target) && !cleared)
6296 emit_clobber (target);
6298 /* Store each element of the constructor into the
6299 corresponding field of TARGET. */
6300 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6302 machine_mode mode;
6303 HOST_WIDE_INT bitsize;
6304 HOST_WIDE_INT bitpos = 0;
6305 tree offset;
6306 rtx to_rtx = target;
6308 /* Just ignore missing fields. We cleared the whole
6309 structure, above, if any fields are missing. */
6310 if (field == 0)
6311 continue;
6313 if (cleared && initializer_zerop (value))
6314 continue;
6316 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6317 bitsize = tree_to_uhwi (DECL_SIZE (field));
6318 else
6319 gcc_unreachable ();
6321 mode = DECL_MODE (field);
6322 if (DECL_BIT_FIELD (field))
6323 mode = VOIDmode;
6325 offset = DECL_FIELD_OFFSET (field);
6326 if (tree_fits_shwi_p (offset)
6327 && tree_fits_shwi_p (bit_position (field)))
6329 bitpos = int_bit_position (field);
6330 offset = NULL_TREE;
6332 else
6333 gcc_unreachable ();
6335 /* If this initializes a field that is smaller than a
6336 word, at the start of a word, try to widen it to a full
6337 word. This special case allows us to output C++ member
6338 function initializations in a form that the optimizers
6339 can understand. */
6340 if (WORD_REGISTER_OPERATIONS
6341 && REG_P (target)
6342 && bitsize < BITS_PER_WORD
6343 && bitpos % BITS_PER_WORD == 0
6344 && GET_MODE_CLASS (mode) == MODE_INT
6345 && TREE_CODE (value) == INTEGER_CST
6346 && exp_size >= 0
6347 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6349 tree type = TREE_TYPE (value);
6351 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6353 type = lang_hooks.types.type_for_mode
6354 (word_mode, TYPE_UNSIGNED (type));
6355 value = fold_convert (type, value);
6356 /* Make sure the bits beyond the original bitsize are zero
6357 so that we can correctly avoid extra zeroing stores in
6358 later constructor elements. */
6359 tree bitsize_mask
6360 = wide_int_to_tree (type, wi::mask (bitsize, false,
6361 BITS_PER_WORD));
6362 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6365 if (BYTES_BIG_ENDIAN)
6366 value
6367 = fold_build2 (LSHIFT_EXPR, type, value,
6368 build_int_cst (type,
6369 BITS_PER_WORD - bitsize));
6370 bitsize = BITS_PER_WORD;
6371 mode = word_mode;
6374 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6375 && DECL_NONADDRESSABLE_P (field))
6377 to_rtx = copy_rtx (to_rtx);
6378 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6381 store_constructor_field (to_rtx, bitsize, bitpos,
6382 0, bitregion_end, mode,
6383 value, cleared,
6384 get_alias_set (TREE_TYPE (field)),
6385 reverse);
6387 break;
6389 case ARRAY_TYPE:
6391 tree value, index;
6392 unsigned HOST_WIDE_INT i;
6393 int need_to_clear;
6394 tree domain;
6395 tree elttype = TREE_TYPE (type);
6396 int const_bounds_p;
6397 HOST_WIDE_INT minelt = 0;
6398 HOST_WIDE_INT maxelt = 0;
6400 /* The storage order is specified for every aggregate type. */
6401 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6403 domain = TYPE_DOMAIN (type);
6404 const_bounds_p = (TYPE_MIN_VALUE (domain)
6405 && TYPE_MAX_VALUE (domain)
6406 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6407 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6409 /* If we have constant bounds for the range of the type, get them. */
6410 if (const_bounds_p)
6412 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6413 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6416 /* If the constructor has fewer elements than the array, clear
6417 the whole array first. Similarly if this is static
6418 constructor of a non-BLKmode object. */
6419 if (cleared)
6420 need_to_clear = 0;
6421 else if (REG_P (target) && TREE_STATIC (exp))
6422 need_to_clear = 1;
6423 else
6425 unsigned HOST_WIDE_INT idx;
6426 tree index, value;
6427 HOST_WIDE_INT count = 0, zero_count = 0;
6428 need_to_clear = ! const_bounds_p;
6430 /* This loop is a more accurate version of the loop in
6431 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6432 is also needed to check for missing elements. */
6433 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6435 HOST_WIDE_INT this_node_count;
6437 if (need_to_clear)
6438 break;
6440 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6442 tree lo_index = TREE_OPERAND (index, 0);
6443 tree hi_index = TREE_OPERAND (index, 1);
6445 if (! tree_fits_uhwi_p (lo_index)
6446 || ! tree_fits_uhwi_p (hi_index))
6448 need_to_clear = 1;
6449 break;
6452 this_node_count = (tree_to_uhwi (hi_index)
6453 - tree_to_uhwi (lo_index) + 1);
6455 else
6456 this_node_count = 1;
6458 count += this_node_count;
6459 if (mostly_zeros_p (value))
6460 zero_count += this_node_count;
6463 /* Clear the entire array first if there are any missing
6464 elements, or if the incidence of zero elements is >=
6465 75%. */
6466 if (! need_to_clear
6467 && (count < maxelt - minelt + 1
6468 || 4 * zero_count >= 3 * count))
6469 need_to_clear = 1;
6472 if (need_to_clear && maybe_gt (size, 0))
6474 if (REG_P (target))
6475 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6476 else
6477 clear_storage (target, gen_int_mode (size, Pmode),
6478 BLOCK_OP_NORMAL);
6479 cleared = 1;
6482 if (!cleared && REG_P (target))
6483 /* Inform later passes that the old value is dead. */
6484 emit_clobber (target);
6486 /* Store each element of the constructor into the
6487 corresponding element of TARGET, determined by counting the
6488 elements. */
6489 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6491 machine_mode mode;
6492 poly_int64 bitsize;
6493 HOST_WIDE_INT bitpos;
6494 rtx xtarget = target;
6496 if (cleared && initializer_zerop (value))
6497 continue;
6499 mode = TYPE_MODE (elttype);
6500 if (mode == BLKmode)
6501 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6502 ? tree_to_uhwi (TYPE_SIZE (elttype))
6503 : -1);
6504 else
6505 bitsize = GET_MODE_BITSIZE (mode);
6507 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6509 tree lo_index = TREE_OPERAND (index, 0);
6510 tree hi_index = TREE_OPERAND (index, 1);
6511 rtx index_r, pos_rtx;
6512 HOST_WIDE_INT lo, hi, count;
6513 tree position;
6515 /* If the range is constant and "small", unroll the loop. */
6516 if (const_bounds_p
6517 && tree_fits_shwi_p (lo_index)
6518 && tree_fits_shwi_p (hi_index)
6519 && (lo = tree_to_shwi (lo_index),
6520 hi = tree_to_shwi (hi_index),
6521 count = hi - lo + 1,
6522 (!MEM_P (target)
6523 || count <= 2
6524 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6525 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6526 <= 40 * 8)))))
6528 lo -= minelt; hi -= minelt;
6529 for (; lo <= hi; lo++)
6531 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6533 if (MEM_P (target)
6534 && !MEM_KEEP_ALIAS_SET_P (target)
6535 && TREE_CODE (type) == ARRAY_TYPE
6536 && TYPE_NONALIASED_COMPONENT (type))
6538 target = copy_rtx (target);
6539 MEM_KEEP_ALIAS_SET_P (target) = 1;
6542 store_constructor_field
6543 (target, bitsize, bitpos, 0, bitregion_end,
6544 mode, value, cleared,
6545 get_alias_set (elttype), reverse);
6548 else
6550 rtx_code_label *loop_start = gen_label_rtx ();
6551 rtx_code_label *loop_end = gen_label_rtx ();
6552 tree exit_cond;
6554 expand_normal (hi_index);
6556 index = build_decl (EXPR_LOCATION (exp),
6557 VAR_DECL, NULL_TREE, domain);
6558 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6559 SET_DECL_RTL (index, index_r);
6560 store_expr (lo_index, index_r, 0, false, reverse);
6562 /* Build the head of the loop. */
6563 do_pending_stack_adjust ();
6564 emit_label (loop_start);
6566 /* Assign value to element index. */
6567 position =
6568 fold_convert (ssizetype,
6569 fold_build2 (MINUS_EXPR,
6570 TREE_TYPE (index),
6571 index,
6572 TYPE_MIN_VALUE (domain)));
6574 position =
6575 size_binop (MULT_EXPR, position,
6576 fold_convert (ssizetype,
6577 TYPE_SIZE_UNIT (elttype)));
6579 pos_rtx = expand_normal (position);
6580 xtarget = offset_address (target, pos_rtx,
6581 highest_pow2_factor (position));
6582 xtarget = adjust_address (xtarget, mode, 0);
6583 if (TREE_CODE (value) == CONSTRUCTOR)
6584 store_constructor (value, xtarget, cleared,
6585 exact_div (bitsize, BITS_PER_UNIT),
6586 reverse);
6587 else
6588 store_expr (value, xtarget, 0, false, reverse);
6590 /* Generate a conditional jump to exit the loop. */
6591 exit_cond = build2 (LT_EXPR, integer_type_node,
6592 index, hi_index);
6593 jumpif (exit_cond, loop_end,
6594 profile_probability::uninitialized ());
6596 /* Update the loop counter, and jump to the head of
6597 the loop. */
6598 expand_assignment (index,
6599 build2 (PLUS_EXPR, TREE_TYPE (index),
6600 index, integer_one_node),
6601 false);
6603 emit_jump (loop_start);
6605 /* Build the end of the loop. */
6606 emit_label (loop_end);
6609 else if ((index != 0 && ! tree_fits_shwi_p (index))
6610 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6612 tree position;
6614 if (index == 0)
6615 index = ssize_int (1);
6617 if (minelt)
6618 index = fold_convert (ssizetype,
6619 fold_build2 (MINUS_EXPR,
6620 TREE_TYPE (index),
6621 index,
6622 TYPE_MIN_VALUE (domain)));
6624 position =
6625 size_binop (MULT_EXPR, index,
6626 fold_convert (ssizetype,
6627 TYPE_SIZE_UNIT (elttype)));
6628 xtarget = offset_address (target,
6629 expand_normal (position),
6630 highest_pow2_factor (position));
6631 xtarget = adjust_address (xtarget, mode, 0);
6632 store_expr (value, xtarget, 0, false, reverse);
6634 else
6636 if (index != 0)
6637 bitpos = ((tree_to_shwi (index) - minelt)
6638 * tree_to_uhwi (TYPE_SIZE (elttype)));
6639 else
6640 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6642 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6643 && TREE_CODE (type) == ARRAY_TYPE
6644 && TYPE_NONALIASED_COMPONENT (type))
6646 target = copy_rtx (target);
6647 MEM_KEEP_ALIAS_SET_P (target) = 1;
6649 store_constructor_field (target, bitsize, bitpos, 0,
6650 bitregion_end, mode, value,
6651 cleared, get_alias_set (elttype),
6652 reverse);
6655 break;
6658 case VECTOR_TYPE:
6660 unsigned HOST_WIDE_INT idx;
6661 constructor_elt *ce;
6662 int i;
6663 int need_to_clear;
6664 insn_code icode = CODE_FOR_nothing;
6665 tree elt;
6666 tree elttype = TREE_TYPE (type);
6667 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6668 machine_mode eltmode = TYPE_MODE (elttype);
6669 HOST_WIDE_INT bitsize;
6670 HOST_WIDE_INT bitpos;
6671 rtvec vector = NULL;
6672 unsigned n_elts;
6673 alias_set_type alias;
6674 bool vec_vec_init_p = false;
6675 machine_mode mode = GET_MODE (target);
6677 gcc_assert (eltmode != BLKmode);
6679 /* Try using vec_duplicate_optab for uniform vectors. */
6680 if (!TREE_SIDE_EFFECTS (exp)
6681 && VECTOR_MODE_P (mode)
6682 && eltmode == GET_MODE_INNER (mode)
6683 && ((icode = optab_handler (vec_duplicate_optab, mode))
6684 != CODE_FOR_nothing)
6685 && (elt = uniform_vector_p (exp)))
6687 struct expand_operand ops[2];
6688 create_output_operand (&ops[0], target, mode);
6689 create_input_operand (&ops[1], expand_normal (elt), eltmode);
6690 expand_insn (icode, 2, ops);
6691 if (!rtx_equal_p (target, ops[0].value))
6692 emit_move_insn (target, ops[0].value);
6693 break;
6696 n_elts = TYPE_VECTOR_SUBPARTS (type);
6697 if (REG_P (target) && VECTOR_MODE_P (mode))
6699 machine_mode emode = eltmode;
6701 if (CONSTRUCTOR_NELTS (exp)
6702 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6703 == VECTOR_TYPE))
6705 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6706 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6707 == n_elts);
6708 emode = TYPE_MODE (etype);
6710 icode = convert_optab_handler (vec_init_optab, mode, emode);
6711 if (icode != CODE_FOR_nothing)
6713 unsigned int i, n = n_elts;
6715 if (emode != eltmode)
6717 n = CONSTRUCTOR_NELTS (exp);
6718 vec_vec_init_p = true;
6720 vector = rtvec_alloc (n);
6721 for (i = 0; i < n; i++)
6722 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6726 /* If the constructor has fewer elements than the vector,
6727 clear the whole array first. Similarly if this is static
6728 constructor of a non-BLKmode object. */
6729 if (cleared)
6730 need_to_clear = 0;
6731 else if (REG_P (target) && TREE_STATIC (exp))
6732 need_to_clear = 1;
6733 else
6735 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6736 tree value;
6738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6740 tree sz = TYPE_SIZE (TREE_TYPE (value));
6741 int n_elts_here
6742 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6743 TYPE_SIZE (elttype)));
6745 count += n_elts_here;
6746 if (mostly_zeros_p (value))
6747 zero_count += n_elts_here;
6750 /* Clear the entire vector first if there are any missing elements,
6751 or if the incidence of zero elements is >= 75%. */
6752 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6755 if (need_to_clear && maybe_gt (size, 0) && !vector)
6757 if (REG_P (target))
6758 emit_move_insn (target, CONST0_RTX (mode));
6759 else
6760 clear_storage (target, gen_int_mode (size, Pmode),
6761 BLOCK_OP_NORMAL);
6762 cleared = 1;
6765 /* Inform later passes that the old value is dead. */
6766 if (!cleared && !vector && REG_P (target))
6767 emit_move_insn (target, CONST0_RTX (mode));
6769 if (MEM_P (target))
6770 alias = MEM_ALIAS_SET (target);
6771 else
6772 alias = get_alias_set (elttype);
6774 /* Store each element of the constructor into the corresponding
6775 element of TARGET, determined by counting the elements. */
6776 for (idx = 0, i = 0;
6777 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6778 idx++, i += bitsize / elt_size)
6780 HOST_WIDE_INT eltpos;
6781 tree value = ce->value;
6783 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6784 if (cleared && initializer_zerop (value))
6785 continue;
6787 if (ce->index)
6788 eltpos = tree_to_uhwi (ce->index);
6789 else
6790 eltpos = i;
6792 if (vector)
6794 if (vec_vec_init_p)
6796 gcc_assert (ce->index == NULL_TREE);
6797 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6798 eltpos = idx;
6800 else
6801 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6802 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6804 else
6806 machine_mode value_mode
6807 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6808 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6809 bitpos = eltpos * elt_size;
6810 store_constructor_field (target, bitsize, bitpos, 0,
6811 bitregion_end, value_mode,
6812 value, cleared, alias, reverse);
6816 if (vector)
6817 emit_insn (GEN_FCN (icode) (target,
6818 gen_rtx_PARALLEL (mode, vector)));
6819 break;
6822 default:
6823 gcc_unreachable ();
6827 /* Store the value of EXP (an expression tree)
6828 into a subfield of TARGET which has mode MODE and occupies
6829 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6830 If MODE is VOIDmode, it means that we are storing into a bit-field.
6832 BITREGION_START is bitpos of the first bitfield in this region.
6833 BITREGION_END is the bitpos of the ending bitfield in this region.
6834 These two fields are 0, if the C++ memory model does not apply,
6835 or we are not interested in keeping track of bitfield regions.
6837 Always return const0_rtx unless we have something particular to
6838 return.
6840 ALIAS_SET is the alias set for the destination. This value will
6841 (in general) be different from that for TARGET, since TARGET is a
6842 reference to the containing structure.
6844 If NONTEMPORAL is true, try generating a nontemporal store.
6846 If REVERSE is true, the store is to be done in reverse order. */
6848 static rtx
6849 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
6850 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
6851 machine_mode mode, tree exp,
6852 alias_set_type alias_set, bool nontemporal, bool reverse)
6854 if (TREE_CODE (exp) == ERROR_MARK)
6855 return const0_rtx;
6857 /* If we have nothing to store, do nothing unless the expression has
6858 side-effects. Don't do that for zero sized addressable lhs of
6859 calls. */
6860 if (known_eq (bitsize, 0)
6861 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6862 || TREE_CODE (exp) != CALL_EXPR))
6863 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6865 if (GET_CODE (target) == CONCAT)
6867 /* We're storing into a struct containing a single __complex. */
6869 gcc_assert (known_eq (bitpos, 0));
6870 return store_expr (exp, target, 0, nontemporal, reverse);
6873 /* If the structure is in a register or if the component
6874 is a bit field, we cannot use addressing to access it.
6875 Use bit-field techniques or SUBREG to store in it. */
6877 poly_int64 decl_bitsize;
6878 if (mode == VOIDmode
6879 || (mode != BLKmode && ! direct_store[(int) mode]
6880 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6881 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6882 || REG_P (target)
6883 || GET_CODE (target) == SUBREG
6884 /* If the field isn't aligned enough to store as an ordinary memref,
6885 store it as a bit field. */
6886 || (mode != BLKmode
6887 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6888 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
6889 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6890 || !multiple_p (bitpos, BITS_PER_UNIT)))
6891 || (known_size_p (bitsize)
6892 && mode != BLKmode
6893 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
6894 /* If the RHS and field are a constant size and the size of the
6895 RHS isn't the same size as the bitfield, we must use bitfield
6896 operations. */
6897 || (known_size_p (bitsize)
6898 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
6899 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
6900 bitsize)
6901 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6902 we will handle specially below. */
6903 && !(TREE_CODE (exp) == CONSTRUCTOR
6904 && multiple_p (bitsize, BITS_PER_UNIT))
6905 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6906 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6907 includes some extra padding. store_expr / expand_expr will in
6908 that case call get_inner_reference that will have the bitsize
6909 we check here and thus the block move will not clobber the
6910 padding that shouldn't be clobbered. In the future we could
6911 replace the TREE_ADDRESSABLE check with a check that
6912 get_base_address needs to live in memory. */
6913 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6914 || TREE_CODE (exp) != COMPONENT_REF
6915 || !multiple_p (bitsize, BITS_PER_UNIT)
6916 || !multiple_p (bitpos, BITS_PER_UNIT)
6917 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
6918 &decl_bitsize)
6919 || maybe_ne (decl_bitsize, bitsize)))
6920 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6921 decl we must use bitfield operations. */
6922 || (known_size_p (bitsize)
6923 && TREE_CODE (exp) == MEM_REF
6924 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6925 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6926 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6927 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6929 rtx temp;
6930 gimple *nop_def;
6932 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6933 implies a mask operation. If the precision is the same size as
6934 the field we're storing into, that mask is redundant. This is
6935 particularly common with bit field assignments generated by the
6936 C front end. */
6937 nop_def = get_def_for_expr (exp, NOP_EXPR);
6938 if (nop_def)
6940 tree type = TREE_TYPE (exp);
6941 if (INTEGRAL_TYPE_P (type)
6942 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6943 && known_eq (bitsize, TYPE_PRECISION (type)))
6945 tree op = gimple_assign_rhs1 (nop_def);
6946 type = TREE_TYPE (op);
6947 if (INTEGRAL_TYPE_P (type)
6948 && known_ge (TYPE_PRECISION (type), bitsize))
6949 exp = op;
6953 temp = expand_normal (exp);
6955 /* We don't support variable-sized BLKmode bitfields, since our
6956 handling of BLKmode is bound up with the ability to break
6957 things into words. */
6958 gcc_assert (mode != BLKmode || bitsize.is_constant ());
6960 /* Handle calls that return values in multiple non-contiguous locations.
6961 The Irix 6 ABI has examples of this. */
6962 if (GET_CODE (temp) == PARALLEL)
6964 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6965 scalar_int_mode temp_mode
6966 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6967 rtx temp_target = gen_reg_rtx (temp_mode);
6968 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6969 temp = temp_target;
6972 /* Handle calls that return BLKmode values in registers. */
6973 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6975 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6976 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6977 temp = temp_target;
6980 /* If the value has aggregate type and an integral mode then, if BITSIZE
6981 is narrower than this mode and this is for big-endian data, we first
6982 need to put the value into the low-order bits for store_bit_field,
6983 except when MODE is BLKmode and BITSIZE larger than the word size
6984 (see the handling of fields larger than a word in store_bit_field).
6985 Moreover, the field may be not aligned on a byte boundary; in this
6986 case, if it has reverse storage order, it needs to be accessed as a
6987 scalar field with reverse storage order and we must first put the
6988 value into target order. */
6989 scalar_int_mode temp_mode;
6990 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6991 && is_int_mode (GET_MODE (temp), &temp_mode))
6993 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6995 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6997 if (reverse)
6998 temp = flip_storage_order (temp_mode, temp);
7000 gcc_checking_assert (known_le (bitsize, size));
7001 if (maybe_lt (bitsize, size)
7002 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7003 /* Use of to_constant for BLKmode was checked above. */
7004 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7005 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7006 size - bitsize, NULL_RTX, 1);
7009 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7010 if (mode != VOIDmode && mode != BLKmode
7011 && mode != TYPE_MODE (TREE_TYPE (exp)))
7012 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7014 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7015 and BITPOS must be aligned on a byte boundary. If so, we simply do
7016 a block copy. Likewise for a BLKmode-like TARGET. */
7017 if (GET_MODE (temp) == BLKmode
7018 && (GET_MODE (target) == BLKmode
7019 || (MEM_P (target)
7020 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7021 && multiple_p (bitpos, BITS_PER_UNIT)
7022 && multiple_p (bitsize, BITS_PER_UNIT))))
7024 gcc_assert (MEM_P (target) && MEM_P (temp));
7025 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7026 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7028 target = adjust_address (target, VOIDmode, bytepos);
7029 emit_block_move (target, temp,
7030 gen_int_mode (bytesize, Pmode),
7031 BLOCK_OP_NORMAL);
7033 return const0_rtx;
7036 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7037 word size, we need to load the value (see again store_bit_field). */
7038 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7040 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
7041 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7042 temp_mode, false, NULL);
7045 /* Store the value in the bitfield. */
7046 store_bit_field (target, bitsize, bitpos,
7047 bitregion_start, bitregion_end,
7048 mode, temp, reverse);
7050 return const0_rtx;
7052 else
7054 /* Now build a reference to just the desired component. */
7055 rtx to_rtx = adjust_address (target, mode,
7056 exact_div (bitpos, BITS_PER_UNIT));
7058 if (to_rtx == target)
7059 to_rtx = copy_rtx (to_rtx);
7061 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7062 set_mem_alias_set (to_rtx, alias_set);
7064 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7065 into a target smaller than its type; handle that case now. */
7066 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7068 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7069 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7070 return to_rtx;
7073 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7077 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7078 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7079 codes and find the ultimate containing object, which we return.
7081 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7082 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7083 storage order of the field.
7084 If the position of the field is variable, we store a tree
7085 giving the variable offset (in units) in *POFFSET.
7086 This offset is in addition to the bit position.
7087 If the position is not variable, we store 0 in *POFFSET.
7089 If any of the extraction expressions is volatile,
7090 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7092 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7093 Otherwise, it is a mode that can be used to access the field.
7095 If the field describes a variable-sized object, *PMODE is set to
7096 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7097 this case, but the address of the object can be found. */
7099 tree
7100 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7101 poly_int64_pod *pbitpos, tree *poffset,
7102 machine_mode *pmode, int *punsignedp,
7103 int *preversep, int *pvolatilep)
7105 tree size_tree = 0;
7106 machine_mode mode = VOIDmode;
7107 bool blkmode_bitfield = false;
7108 tree offset = size_zero_node;
7109 poly_offset_int bit_offset = 0;
7111 /* First get the mode, signedness, storage order and size. We do this from
7112 just the outermost expression. */
7113 *pbitsize = -1;
7114 if (TREE_CODE (exp) == COMPONENT_REF)
7116 tree field = TREE_OPERAND (exp, 1);
7117 size_tree = DECL_SIZE (field);
7118 if (flag_strict_volatile_bitfields > 0
7119 && TREE_THIS_VOLATILE (exp)
7120 && DECL_BIT_FIELD_TYPE (field)
7121 && DECL_MODE (field) != BLKmode)
7122 /* Volatile bitfields should be accessed in the mode of the
7123 field's type, not the mode computed based on the bit
7124 size. */
7125 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7126 else if (!DECL_BIT_FIELD (field))
7128 mode = DECL_MODE (field);
7129 /* For vector fields re-check the target flags, as DECL_MODE
7130 could have been set with different target flags than
7131 the current function has. */
7132 if (mode == BLKmode
7133 && VECTOR_TYPE_P (TREE_TYPE (field))
7134 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7135 mode = TYPE_MODE (TREE_TYPE (field));
7137 else if (DECL_MODE (field) == BLKmode)
7138 blkmode_bitfield = true;
7140 *punsignedp = DECL_UNSIGNED (field);
7142 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7144 size_tree = TREE_OPERAND (exp, 1);
7145 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7146 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7148 /* For vector types, with the correct size of access, use the mode of
7149 inner type. */
7150 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7151 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7152 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7153 mode = TYPE_MODE (TREE_TYPE (exp));
7155 else
7157 mode = TYPE_MODE (TREE_TYPE (exp));
7158 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7160 if (mode == BLKmode)
7161 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7162 else
7163 *pbitsize = GET_MODE_BITSIZE (mode);
7166 if (size_tree != 0)
7168 if (! tree_fits_uhwi_p (size_tree))
7169 mode = BLKmode, *pbitsize = -1;
7170 else
7171 *pbitsize = tree_to_uhwi (size_tree);
7174 *preversep = reverse_storage_order_for_component_p (exp);
7176 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7177 and find the ultimate containing object. */
7178 while (1)
7180 switch (TREE_CODE (exp))
7182 case BIT_FIELD_REF:
7183 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7184 break;
7186 case COMPONENT_REF:
7188 tree field = TREE_OPERAND (exp, 1);
7189 tree this_offset = component_ref_field_offset (exp);
7191 /* If this field hasn't been filled in yet, don't go past it.
7192 This should only happen when folding expressions made during
7193 type construction. */
7194 if (this_offset == 0)
7195 break;
7197 offset = size_binop (PLUS_EXPR, offset, this_offset);
7198 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7200 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7202 break;
7204 case ARRAY_REF:
7205 case ARRAY_RANGE_REF:
7207 tree index = TREE_OPERAND (exp, 1);
7208 tree low_bound = array_ref_low_bound (exp);
7209 tree unit_size = array_ref_element_size (exp);
7211 /* We assume all arrays have sizes that are a multiple of a byte.
7212 First subtract the lower bound, if any, in the type of the
7213 index, then convert to sizetype and multiply by the size of
7214 the array element. */
7215 if (! integer_zerop (low_bound))
7216 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7217 index, low_bound);
7219 offset = size_binop (PLUS_EXPR, offset,
7220 size_binop (MULT_EXPR,
7221 fold_convert (sizetype, index),
7222 unit_size));
7224 break;
7226 case REALPART_EXPR:
7227 break;
7229 case IMAGPART_EXPR:
7230 bit_offset += *pbitsize;
7231 break;
7233 case VIEW_CONVERT_EXPR:
7234 break;
7236 case MEM_REF:
7237 /* Hand back the decl for MEM[&decl, off]. */
7238 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7240 tree off = TREE_OPERAND (exp, 1);
7241 if (!integer_zerop (off))
7243 poly_offset_int boff = mem_ref_offset (exp);
7244 boff <<= LOG2_BITS_PER_UNIT;
7245 bit_offset += boff;
7247 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7249 goto done;
7251 default:
7252 goto done;
7255 /* If any reference in the chain is volatile, the effect is volatile. */
7256 if (TREE_THIS_VOLATILE (exp))
7257 *pvolatilep = 1;
7259 exp = TREE_OPERAND (exp, 0);
7261 done:
7263 /* If OFFSET is constant, see if we can return the whole thing as a
7264 constant bit position. Make sure to handle overflow during
7265 this conversion. */
7266 if (poly_int_tree_p (offset))
7268 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7269 TYPE_PRECISION (sizetype));
7270 tem <<= LOG2_BITS_PER_UNIT;
7271 tem += bit_offset;
7272 if (tem.to_shwi (pbitpos))
7273 *poffset = offset = NULL_TREE;
7276 /* Otherwise, split it up. */
7277 if (offset)
7279 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7280 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7282 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7283 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7284 offset = size_binop (PLUS_EXPR, offset,
7285 build_int_cst (sizetype, bytes.force_shwi ()));
7288 *poffset = offset;
7291 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7292 if (mode == VOIDmode
7293 && blkmode_bitfield
7294 && multiple_p (*pbitpos, BITS_PER_UNIT)
7295 && multiple_p (*pbitsize, BITS_PER_UNIT))
7296 *pmode = BLKmode;
7297 else
7298 *pmode = mode;
7300 return exp;
7303 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7305 static unsigned HOST_WIDE_INT
7306 target_align (const_tree target)
7308 /* We might have a chain of nested references with intermediate misaligning
7309 bitfields components, so need to recurse to find out. */
7311 unsigned HOST_WIDE_INT this_align, outer_align;
7313 switch (TREE_CODE (target))
7315 case BIT_FIELD_REF:
7316 return 1;
7318 case COMPONENT_REF:
7319 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7320 outer_align = target_align (TREE_OPERAND (target, 0));
7321 return MIN (this_align, outer_align);
7323 case ARRAY_REF:
7324 case ARRAY_RANGE_REF:
7325 this_align = TYPE_ALIGN (TREE_TYPE (target));
7326 outer_align = target_align (TREE_OPERAND (target, 0));
7327 return MIN (this_align, outer_align);
7329 CASE_CONVERT:
7330 case NON_LVALUE_EXPR:
7331 case VIEW_CONVERT_EXPR:
7332 this_align = TYPE_ALIGN (TREE_TYPE (target));
7333 outer_align = target_align (TREE_OPERAND (target, 0));
7334 return MAX (this_align, outer_align);
7336 default:
7337 return TYPE_ALIGN (TREE_TYPE (target));
7342 /* Given an rtx VALUE that may contain additions and multiplications, return
7343 an equivalent value that just refers to a register, memory, or constant.
7344 This is done by generating instructions to perform the arithmetic and
7345 returning a pseudo-register containing the value.
7347 The returned value may be a REG, SUBREG, MEM or constant. */
7350 force_operand (rtx value, rtx target)
7352 rtx op1, op2;
7353 /* Use subtarget as the target for operand 0 of a binary operation. */
7354 rtx subtarget = get_subtarget (target);
7355 enum rtx_code code = GET_CODE (value);
7357 /* Check for subreg applied to an expression produced by loop optimizer. */
7358 if (code == SUBREG
7359 && !REG_P (SUBREG_REG (value))
7360 && !MEM_P (SUBREG_REG (value)))
7362 value
7363 = simplify_gen_subreg (GET_MODE (value),
7364 force_reg (GET_MODE (SUBREG_REG (value)),
7365 force_operand (SUBREG_REG (value),
7366 NULL_RTX)),
7367 GET_MODE (SUBREG_REG (value)),
7368 SUBREG_BYTE (value));
7369 code = GET_CODE (value);
7372 /* Check for a PIC address load. */
7373 if ((code == PLUS || code == MINUS)
7374 && XEXP (value, 0) == pic_offset_table_rtx
7375 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7376 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7377 || GET_CODE (XEXP (value, 1)) == CONST))
7379 if (!subtarget)
7380 subtarget = gen_reg_rtx (GET_MODE (value));
7381 emit_move_insn (subtarget, value);
7382 return subtarget;
7385 if (ARITHMETIC_P (value))
7387 op2 = XEXP (value, 1);
7388 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7389 subtarget = 0;
7390 if (code == MINUS && CONST_INT_P (op2))
7392 code = PLUS;
7393 op2 = negate_rtx (GET_MODE (value), op2);
7396 /* Check for an addition with OP2 a constant integer and our first
7397 operand a PLUS of a virtual register and something else. In that
7398 case, we want to emit the sum of the virtual register and the
7399 constant first and then add the other value. This allows virtual
7400 register instantiation to simply modify the constant rather than
7401 creating another one around this addition. */
7402 if (code == PLUS && CONST_INT_P (op2)
7403 && GET_CODE (XEXP (value, 0)) == PLUS
7404 && REG_P (XEXP (XEXP (value, 0), 0))
7405 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7406 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7408 rtx temp = expand_simple_binop (GET_MODE (value), code,
7409 XEXP (XEXP (value, 0), 0), op2,
7410 subtarget, 0, OPTAB_LIB_WIDEN);
7411 return expand_simple_binop (GET_MODE (value), code, temp,
7412 force_operand (XEXP (XEXP (value,
7413 0), 1), 0),
7414 target, 0, OPTAB_LIB_WIDEN);
7417 op1 = force_operand (XEXP (value, 0), subtarget);
7418 op2 = force_operand (op2, NULL_RTX);
7419 switch (code)
7421 case MULT:
7422 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7423 case DIV:
7424 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7425 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7426 target, 1, OPTAB_LIB_WIDEN);
7427 else
7428 return expand_divmod (0,
7429 FLOAT_MODE_P (GET_MODE (value))
7430 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7431 GET_MODE (value), op1, op2, target, 0);
7432 case MOD:
7433 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7434 target, 0);
7435 case UDIV:
7436 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7437 target, 1);
7438 case UMOD:
7439 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7440 target, 1);
7441 case ASHIFTRT:
7442 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7443 target, 0, OPTAB_LIB_WIDEN);
7444 default:
7445 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7446 target, 1, OPTAB_LIB_WIDEN);
7449 if (UNARY_P (value))
7451 if (!target)
7452 target = gen_reg_rtx (GET_MODE (value));
7453 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7454 switch (code)
7456 case ZERO_EXTEND:
7457 case SIGN_EXTEND:
7458 case TRUNCATE:
7459 case FLOAT_EXTEND:
7460 case FLOAT_TRUNCATE:
7461 convert_move (target, op1, code == ZERO_EXTEND);
7462 return target;
7464 case FIX:
7465 case UNSIGNED_FIX:
7466 expand_fix (target, op1, code == UNSIGNED_FIX);
7467 return target;
7469 case FLOAT:
7470 case UNSIGNED_FLOAT:
7471 expand_float (target, op1, code == UNSIGNED_FLOAT);
7472 return target;
7474 default:
7475 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7479 #ifdef INSN_SCHEDULING
7480 /* On machines that have insn scheduling, we want all memory reference to be
7481 explicit, so we need to deal with such paradoxical SUBREGs. */
7482 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7483 value
7484 = simplify_gen_subreg (GET_MODE (value),
7485 force_reg (GET_MODE (SUBREG_REG (value)),
7486 force_operand (SUBREG_REG (value),
7487 NULL_RTX)),
7488 GET_MODE (SUBREG_REG (value)),
7489 SUBREG_BYTE (value));
7490 #endif
7492 return value;
7495 /* Subroutine of expand_expr: return nonzero iff there is no way that
7496 EXP can reference X, which is being modified. TOP_P is nonzero if this
7497 call is going to be used to determine whether we need a temporary
7498 for EXP, as opposed to a recursive call to this function.
7500 It is always safe for this routine to return zero since it merely
7501 searches for optimization opportunities. */
7504 safe_from_p (const_rtx x, tree exp, int top_p)
7506 rtx exp_rtl = 0;
7507 int i, nops;
7509 if (x == 0
7510 /* If EXP has varying size, we MUST use a target since we currently
7511 have no way of allocating temporaries of variable size
7512 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7513 So we assume here that something at a higher level has prevented a
7514 clash. This is somewhat bogus, but the best we can do. Only
7515 do this when X is BLKmode and when we are at the top level. */
7516 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7517 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7518 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7519 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7520 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7521 != INTEGER_CST)
7522 && GET_MODE (x) == BLKmode)
7523 /* If X is in the outgoing argument area, it is always safe. */
7524 || (MEM_P (x)
7525 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7526 || (GET_CODE (XEXP (x, 0)) == PLUS
7527 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7528 return 1;
7530 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7531 find the underlying pseudo. */
7532 if (GET_CODE (x) == SUBREG)
7534 x = SUBREG_REG (x);
7535 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7536 return 0;
7539 /* Now look at our tree code and possibly recurse. */
7540 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7542 case tcc_declaration:
7543 exp_rtl = DECL_RTL_IF_SET (exp);
7544 break;
7546 case tcc_constant:
7547 return 1;
7549 case tcc_exceptional:
7550 if (TREE_CODE (exp) == TREE_LIST)
7552 while (1)
7554 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7555 return 0;
7556 exp = TREE_CHAIN (exp);
7557 if (!exp)
7558 return 1;
7559 if (TREE_CODE (exp) != TREE_LIST)
7560 return safe_from_p (x, exp, 0);
7563 else if (TREE_CODE (exp) == CONSTRUCTOR)
7565 constructor_elt *ce;
7566 unsigned HOST_WIDE_INT idx;
7568 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7569 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7570 || !safe_from_p (x, ce->value, 0))
7571 return 0;
7572 return 1;
7574 else if (TREE_CODE (exp) == ERROR_MARK)
7575 return 1; /* An already-visited SAVE_EXPR? */
7576 else
7577 return 0;
7579 case tcc_statement:
7580 /* The only case we look at here is the DECL_INITIAL inside a
7581 DECL_EXPR. */
7582 return (TREE_CODE (exp) != DECL_EXPR
7583 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7584 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7585 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7587 case tcc_binary:
7588 case tcc_comparison:
7589 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7590 return 0;
7591 /* Fall through. */
7593 case tcc_unary:
7594 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7596 case tcc_expression:
7597 case tcc_reference:
7598 case tcc_vl_exp:
7599 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7600 the expression. If it is set, we conflict iff we are that rtx or
7601 both are in memory. Otherwise, we check all operands of the
7602 expression recursively. */
7604 switch (TREE_CODE (exp))
7606 case ADDR_EXPR:
7607 /* If the operand is static or we are static, we can't conflict.
7608 Likewise if we don't conflict with the operand at all. */
7609 if (staticp (TREE_OPERAND (exp, 0))
7610 || TREE_STATIC (exp)
7611 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7612 return 1;
7614 /* Otherwise, the only way this can conflict is if we are taking
7615 the address of a DECL a that address if part of X, which is
7616 very rare. */
7617 exp = TREE_OPERAND (exp, 0);
7618 if (DECL_P (exp))
7620 if (!DECL_RTL_SET_P (exp)
7621 || !MEM_P (DECL_RTL (exp)))
7622 return 0;
7623 else
7624 exp_rtl = XEXP (DECL_RTL (exp), 0);
7626 break;
7628 case MEM_REF:
7629 if (MEM_P (x)
7630 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7631 get_alias_set (exp)))
7632 return 0;
7633 break;
7635 case CALL_EXPR:
7636 /* Assume that the call will clobber all hard registers and
7637 all of memory. */
7638 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7639 || MEM_P (x))
7640 return 0;
7641 break;
7643 case WITH_CLEANUP_EXPR:
7644 case CLEANUP_POINT_EXPR:
7645 /* Lowered by gimplify.c. */
7646 gcc_unreachable ();
7648 case SAVE_EXPR:
7649 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7651 default:
7652 break;
7655 /* If we have an rtx, we do not need to scan our operands. */
7656 if (exp_rtl)
7657 break;
7659 nops = TREE_OPERAND_LENGTH (exp);
7660 for (i = 0; i < nops; i++)
7661 if (TREE_OPERAND (exp, i) != 0
7662 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7663 return 0;
7665 break;
7667 case tcc_type:
7668 /* Should never get a type here. */
7669 gcc_unreachable ();
7672 /* If we have an rtl, find any enclosed object. Then see if we conflict
7673 with it. */
7674 if (exp_rtl)
7676 if (GET_CODE (exp_rtl) == SUBREG)
7678 exp_rtl = SUBREG_REG (exp_rtl);
7679 if (REG_P (exp_rtl)
7680 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7681 return 0;
7684 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7685 are memory and they conflict. */
7686 return ! (rtx_equal_p (x, exp_rtl)
7687 || (MEM_P (x) && MEM_P (exp_rtl)
7688 && true_dependence (exp_rtl, VOIDmode, x)));
7691 /* If we reach here, it is safe. */
7692 return 1;
7696 /* Return the highest power of two that EXP is known to be a multiple of.
7697 This is used in updating alignment of MEMs in array references. */
7699 unsigned HOST_WIDE_INT
7700 highest_pow2_factor (const_tree exp)
7702 unsigned HOST_WIDE_INT ret;
7703 int trailing_zeros = tree_ctz (exp);
7704 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7705 return BIGGEST_ALIGNMENT;
7706 ret = HOST_WIDE_INT_1U << trailing_zeros;
7707 if (ret > BIGGEST_ALIGNMENT)
7708 return BIGGEST_ALIGNMENT;
7709 return ret;
7712 /* Similar, except that the alignment requirements of TARGET are
7713 taken into account. Assume it is at least as aligned as its
7714 type, unless it is a COMPONENT_REF in which case the layout of
7715 the structure gives the alignment. */
7717 static unsigned HOST_WIDE_INT
7718 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7720 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7721 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7723 return MAX (factor, talign);
7726 /* Convert the tree comparison code TCODE to the rtl one where the
7727 signedness is UNSIGNEDP. */
7729 static enum rtx_code
7730 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7732 enum rtx_code code;
7733 switch (tcode)
7735 case EQ_EXPR:
7736 code = EQ;
7737 break;
7738 case NE_EXPR:
7739 code = NE;
7740 break;
7741 case LT_EXPR:
7742 code = unsignedp ? LTU : LT;
7743 break;
7744 case LE_EXPR:
7745 code = unsignedp ? LEU : LE;
7746 break;
7747 case GT_EXPR:
7748 code = unsignedp ? GTU : GT;
7749 break;
7750 case GE_EXPR:
7751 code = unsignedp ? GEU : GE;
7752 break;
7753 case UNORDERED_EXPR:
7754 code = UNORDERED;
7755 break;
7756 case ORDERED_EXPR:
7757 code = ORDERED;
7758 break;
7759 case UNLT_EXPR:
7760 code = UNLT;
7761 break;
7762 case UNLE_EXPR:
7763 code = UNLE;
7764 break;
7765 case UNGT_EXPR:
7766 code = UNGT;
7767 break;
7768 case UNGE_EXPR:
7769 code = UNGE;
7770 break;
7771 case UNEQ_EXPR:
7772 code = UNEQ;
7773 break;
7774 case LTGT_EXPR:
7775 code = LTGT;
7776 break;
7778 default:
7779 gcc_unreachable ();
7781 return code;
7784 /* Subroutine of expand_expr. Expand the two operands of a binary
7785 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7786 The value may be stored in TARGET if TARGET is nonzero. The
7787 MODIFIER argument is as documented by expand_expr. */
7789 void
7790 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7791 enum expand_modifier modifier)
7793 if (! safe_from_p (target, exp1, 1))
7794 target = 0;
7795 if (operand_equal_p (exp0, exp1, 0))
7797 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7798 *op1 = copy_rtx (*op0);
7800 else
7802 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7803 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7808 /* Return a MEM that contains constant EXP. DEFER is as for
7809 output_constant_def and MODIFIER is as for expand_expr. */
7811 static rtx
7812 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7814 rtx mem;
7816 mem = output_constant_def (exp, defer);
7817 if (modifier != EXPAND_INITIALIZER)
7818 mem = use_anchored_address (mem);
7819 return mem;
7822 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7823 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7825 static rtx
7826 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7827 enum expand_modifier modifier, addr_space_t as)
7829 rtx result, subtarget;
7830 tree inner, offset;
7831 poly_int64 bitsize, bitpos;
7832 int unsignedp, reversep, volatilep = 0;
7833 machine_mode mode1;
7835 /* If we are taking the address of a constant and are at the top level,
7836 we have to use output_constant_def since we can't call force_const_mem
7837 at top level. */
7838 /* ??? This should be considered a front-end bug. We should not be
7839 generating ADDR_EXPR of something that isn't an LVALUE. The only
7840 exception here is STRING_CST. */
7841 if (CONSTANT_CLASS_P (exp))
7843 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7844 if (modifier < EXPAND_SUM)
7845 result = force_operand (result, target);
7846 return result;
7849 /* Everything must be something allowed by is_gimple_addressable. */
7850 switch (TREE_CODE (exp))
7852 case INDIRECT_REF:
7853 /* This case will happen via recursion for &a->b. */
7854 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7856 case MEM_REF:
7858 tree tem = TREE_OPERAND (exp, 0);
7859 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7860 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7861 return expand_expr (tem, target, tmode, modifier);
7864 case CONST_DECL:
7865 /* Expand the initializer like constants above. */
7866 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7867 0, modifier), 0);
7868 if (modifier < EXPAND_SUM)
7869 result = force_operand (result, target);
7870 return result;
7872 case REALPART_EXPR:
7873 /* The real part of the complex number is always first, therefore
7874 the address is the same as the address of the parent object. */
7875 offset = 0;
7876 bitpos = 0;
7877 inner = TREE_OPERAND (exp, 0);
7878 break;
7880 case IMAGPART_EXPR:
7881 /* The imaginary part of the complex number is always second.
7882 The expression is therefore always offset by the size of the
7883 scalar type. */
7884 offset = 0;
7885 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7886 inner = TREE_OPERAND (exp, 0);
7887 break;
7889 case COMPOUND_LITERAL_EXPR:
7890 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7891 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7892 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7893 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7894 the initializers aren't gimplified. */
7895 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7896 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7897 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7898 target, tmode, modifier, as);
7899 /* FALLTHRU */
7900 default:
7901 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7902 expand_expr, as that can have various side effects; LABEL_DECLs for
7903 example, may not have their DECL_RTL set yet. Expand the rtl of
7904 CONSTRUCTORs too, which should yield a memory reference for the
7905 constructor's contents. Assume language specific tree nodes can
7906 be expanded in some interesting way. */
7907 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7908 if (DECL_P (exp)
7909 || TREE_CODE (exp) == CONSTRUCTOR
7910 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7912 result = expand_expr (exp, target, tmode,
7913 modifier == EXPAND_INITIALIZER
7914 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7916 /* If the DECL isn't in memory, then the DECL wasn't properly
7917 marked TREE_ADDRESSABLE, which will be either a front-end
7918 or a tree optimizer bug. */
7920 gcc_assert (MEM_P (result));
7921 result = XEXP (result, 0);
7923 /* ??? Is this needed anymore? */
7924 if (DECL_P (exp))
7925 TREE_USED (exp) = 1;
7927 if (modifier != EXPAND_INITIALIZER
7928 && modifier != EXPAND_CONST_ADDRESS
7929 && modifier != EXPAND_SUM)
7930 result = force_operand (result, target);
7931 return result;
7934 /* Pass FALSE as the last argument to get_inner_reference although
7935 we are expanding to RTL. The rationale is that we know how to
7936 handle "aligning nodes" here: we can just bypass them because
7937 they won't change the final object whose address will be returned
7938 (they actually exist only for that purpose). */
7939 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7940 &unsignedp, &reversep, &volatilep);
7941 break;
7944 /* We must have made progress. */
7945 gcc_assert (inner != exp);
7947 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
7948 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7949 inner alignment, force the inner to be sufficiently aligned. */
7950 if (CONSTANT_CLASS_P (inner)
7951 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7953 inner = copy_node (inner);
7954 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7955 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7956 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7958 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7960 if (offset)
7962 rtx tmp;
7964 if (modifier != EXPAND_NORMAL)
7965 result = force_operand (result, NULL);
7966 tmp = expand_expr (offset, NULL_RTX, tmode,
7967 modifier == EXPAND_INITIALIZER
7968 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7970 /* expand_expr is allowed to return an object in a mode other
7971 than TMODE. If it did, we need to convert. */
7972 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7973 tmp = convert_modes (tmode, GET_MODE (tmp),
7974 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7975 result = convert_memory_address_addr_space (tmode, result, as);
7976 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7978 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7979 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7980 else
7982 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
7983 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7984 1, OPTAB_LIB_WIDEN);
7988 if (maybe_ne (bitpos, 0))
7990 /* Someone beforehand should have rejected taking the address
7991 of an object that isn't byte-aligned. */
7992 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7993 result = convert_memory_address_addr_space (tmode, result, as);
7994 result = plus_constant (tmode, result, bytepos);
7995 if (modifier < EXPAND_SUM)
7996 result = force_operand (result, target);
7999 return result;
8002 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8003 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8005 static rtx
8006 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8007 enum expand_modifier modifier)
8009 addr_space_t as = ADDR_SPACE_GENERIC;
8010 scalar_int_mode address_mode = Pmode;
8011 scalar_int_mode pointer_mode = ptr_mode;
8012 machine_mode rmode;
8013 rtx result;
8015 /* Target mode of VOIDmode says "whatever's natural". */
8016 if (tmode == VOIDmode)
8017 tmode = TYPE_MODE (TREE_TYPE (exp));
8019 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8021 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8022 address_mode = targetm.addr_space.address_mode (as);
8023 pointer_mode = targetm.addr_space.pointer_mode (as);
8026 /* We can get called with some Weird Things if the user does silliness
8027 like "(short) &a". In that case, convert_memory_address won't do
8028 the right thing, so ignore the given target mode. */
8029 scalar_int_mode new_tmode = (tmode == pointer_mode
8030 ? pointer_mode
8031 : address_mode);
8033 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8034 new_tmode, modifier, as);
8036 /* Despite expand_expr claims concerning ignoring TMODE when not
8037 strictly convenient, stuff breaks if we don't honor it. Note
8038 that combined with the above, we only do this for pointer modes. */
8039 rmode = GET_MODE (result);
8040 if (rmode == VOIDmode)
8041 rmode = new_tmode;
8042 if (rmode != new_tmode)
8043 result = convert_memory_address_addr_space (new_tmode, result, as);
8045 return result;
8048 /* Generate code for computing CONSTRUCTOR EXP.
8049 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8050 is TRUE, instead of creating a temporary variable in memory
8051 NULL is returned and the caller needs to handle it differently. */
8053 static rtx
8054 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8055 bool avoid_temp_mem)
8057 tree type = TREE_TYPE (exp);
8058 machine_mode mode = TYPE_MODE (type);
8060 /* Try to avoid creating a temporary at all. This is possible
8061 if all of the initializer is zero.
8062 FIXME: try to handle all [0..255] initializers we can handle
8063 with memset. */
8064 if (TREE_STATIC (exp)
8065 && !TREE_ADDRESSABLE (exp)
8066 && target != 0 && mode == BLKmode
8067 && all_zeros_p (exp))
8069 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8070 return target;
8073 /* All elts simple constants => refer to a constant in memory. But
8074 if this is a non-BLKmode mode, let it store a field at a time
8075 since that should make a CONST_INT, CONST_WIDE_INT or
8076 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8077 use, it is best to store directly into the target unless the type
8078 is large enough that memcpy will be used. If we are making an
8079 initializer and all operands are constant, put it in memory as
8080 well.
8082 FIXME: Avoid trying to fill vector constructors piece-meal.
8083 Output them with output_constant_def below unless we're sure
8084 they're zeros. This should go away when vector initializers
8085 are treated like VECTOR_CST instead of arrays. */
8086 if ((TREE_STATIC (exp)
8087 && ((mode == BLKmode
8088 && ! (target != 0 && safe_from_p (target, exp, 1)))
8089 || TREE_ADDRESSABLE (exp)
8090 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8091 && (! can_move_by_pieces
8092 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8093 TYPE_ALIGN (type)))
8094 && ! mostly_zeros_p (exp))))
8095 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8096 && TREE_CONSTANT (exp)))
8098 rtx constructor;
8100 if (avoid_temp_mem)
8101 return NULL_RTX;
8103 constructor = expand_expr_constant (exp, 1, modifier);
8105 if (modifier != EXPAND_CONST_ADDRESS
8106 && modifier != EXPAND_INITIALIZER
8107 && modifier != EXPAND_SUM)
8108 constructor = validize_mem (constructor);
8110 return constructor;
8113 /* Handle calls that pass values in multiple non-contiguous
8114 locations. The Irix 6 ABI has examples of this. */
8115 if (target == 0 || ! safe_from_p (target, exp, 1)
8116 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8118 if (avoid_temp_mem)
8119 return NULL_RTX;
8121 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8124 store_constructor (exp, target, 0, int_expr_size (exp), false);
8125 return target;
8129 /* expand_expr: generate code for computing expression EXP.
8130 An rtx for the computed value is returned. The value is never null.
8131 In the case of a void EXP, const0_rtx is returned.
8133 The value may be stored in TARGET if TARGET is nonzero.
8134 TARGET is just a suggestion; callers must assume that
8135 the rtx returned may not be the same as TARGET.
8137 If TARGET is CONST0_RTX, it means that the value will be ignored.
8139 If TMODE is not VOIDmode, it suggests generating the
8140 result in mode TMODE. But this is done only when convenient.
8141 Otherwise, TMODE is ignored and the value generated in its natural mode.
8142 TMODE is just a suggestion; callers must assume that
8143 the rtx returned may not have mode TMODE.
8145 Note that TARGET may have neither TMODE nor MODE. In that case, it
8146 probably will not be used.
8148 If MODIFIER is EXPAND_SUM then when EXP is an addition
8149 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8150 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8151 products as above, or REG or MEM, or constant.
8152 Ordinarily in such cases we would output mul or add instructions
8153 and then return a pseudo reg containing the sum.
8155 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8156 it also marks a label as absolutely required (it can't be dead).
8157 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8158 This is used for outputting expressions used in initializers.
8160 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8161 with a constant address even if that address is not normally legitimate.
8162 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8164 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8165 a call parameter. Such targets require special care as we haven't yet
8166 marked TARGET so that it's safe from being trashed by libcalls. We
8167 don't want to use TARGET for anything but the final result;
8168 Intermediate values must go elsewhere. Additionally, calls to
8169 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8171 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8172 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8173 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8174 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8175 recursively.
8177 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8178 In this case, we don't adjust a returned MEM rtx that wouldn't be
8179 sufficiently aligned for its mode; instead, it's up to the caller
8180 to deal with it afterwards. This is used to make sure that unaligned
8181 base objects for which out-of-bounds accesses are supported, for
8182 example record types with trailing arrays, aren't realigned behind
8183 the back of the caller.
8184 The normal operating mode is to pass FALSE for this parameter. */
8187 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8188 enum expand_modifier modifier, rtx *alt_rtl,
8189 bool inner_reference_p)
8191 rtx ret;
8193 /* Handle ERROR_MARK before anybody tries to access its type. */
8194 if (TREE_CODE (exp) == ERROR_MARK
8195 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8197 ret = CONST0_RTX (tmode);
8198 return ret ? ret : const0_rtx;
8201 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8202 inner_reference_p);
8203 return ret;
8206 /* Try to expand the conditional expression which is represented by
8207 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8208 return the rtl reg which represents the result. Otherwise return
8209 NULL_RTX. */
8211 static rtx
8212 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8213 tree treeop1 ATTRIBUTE_UNUSED,
8214 tree treeop2 ATTRIBUTE_UNUSED)
8216 rtx insn;
8217 rtx op00, op01, op1, op2;
8218 enum rtx_code comparison_code;
8219 machine_mode comparison_mode;
8220 gimple *srcstmt;
8221 rtx temp;
8222 tree type = TREE_TYPE (treeop1);
8223 int unsignedp = TYPE_UNSIGNED (type);
8224 machine_mode mode = TYPE_MODE (type);
8225 machine_mode orig_mode = mode;
8226 static bool expanding_cond_expr_using_cmove = false;
8228 /* Conditional move expansion can end up TERing two operands which,
8229 when recursively hitting conditional expressions can result in
8230 exponential behavior if the cmove expansion ultimatively fails.
8231 It's hardly profitable to TER a cmove into a cmove so avoid doing
8232 that by failing early if we end up recursing. */
8233 if (expanding_cond_expr_using_cmove)
8234 return NULL_RTX;
8236 /* If we cannot do a conditional move on the mode, try doing it
8237 with the promoted mode. */
8238 if (!can_conditionally_move_p (mode))
8240 mode = promote_mode (type, mode, &unsignedp);
8241 if (!can_conditionally_move_p (mode))
8242 return NULL_RTX;
8243 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8245 else
8246 temp = assign_temp (type, 0, 1);
8248 expanding_cond_expr_using_cmove = true;
8249 start_sequence ();
8250 expand_operands (treeop1, treeop2,
8251 temp, &op1, &op2, EXPAND_NORMAL);
8253 if (TREE_CODE (treeop0) == SSA_NAME
8254 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8256 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8257 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8258 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8259 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8260 comparison_mode = TYPE_MODE (type);
8261 unsignedp = TYPE_UNSIGNED (type);
8262 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8264 else if (COMPARISON_CLASS_P (treeop0))
8266 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8267 enum tree_code cmpcode = TREE_CODE (treeop0);
8268 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8269 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8270 unsignedp = TYPE_UNSIGNED (type);
8271 comparison_mode = TYPE_MODE (type);
8272 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8274 else
8276 op00 = expand_normal (treeop0);
8277 op01 = const0_rtx;
8278 comparison_code = NE;
8279 comparison_mode = GET_MODE (op00);
8280 if (comparison_mode == VOIDmode)
8281 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8283 expanding_cond_expr_using_cmove = false;
8285 if (GET_MODE (op1) != mode)
8286 op1 = gen_lowpart (mode, op1);
8288 if (GET_MODE (op2) != mode)
8289 op2 = gen_lowpart (mode, op2);
8291 /* Try to emit the conditional move. */
8292 insn = emit_conditional_move (temp, comparison_code,
8293 op00, op01, comparison_mode,
8294 op1, op2, mode,
8295 unsignedp);
8297 /* If we could do the conditional move, emit the sequence,
8298 and return. */
8299 if (insn)
8301 rtx_insn *seq = get_insns ();
8302 end_sequence ();
8303 emit_insn (seq);
8304 return convert_modes (orig_mode, mode, temp, 0);
8307 /* Otherwise discard the sequence and fall back to code with
8308 branches. */
8309 end_sequence ();
8310 return NULL_RTX;
8314 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8315 enum expand_modifier modifier)
8317 rtx op0, op1, op2, temp;
8318 rtx_code_label *lab;
8319 tree type;
8320 int unsignedp;
8321 machine_mode mode;
8322 scalar_int_mode int_mode;
8323 enum tree_code code = ops->code;
8324 optab this_optab;
8325 rtx subtarget, original_target;
8326 int ignore;
8327 bool reduce_bit_field;
8328 location_t loc = ops->location;
8329 tree treeop0, treeop1, treeop2;
8330 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8331 ? reduce_to_bit_field_precision ((expr), \
8332 target, \
8333 type) \
8334 : (expr))
8336 type = ops->type;
8337 mode = TYPE_MODE (type);
8338 unsignedp = TYPE_UNSIGNED (type);
8340 treeop0 = ops->op0;
8341 treeop1 = ops->op1;
8342 treeop2 = ops->op2;
8344 /* We should be called only on simple (binary or unary) expressions,
8345 exactly those that are valid in gimple expressions that aren't
8346 GIMPLE_SINGLE_RHS (or invalid). */
8347 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8348 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8349 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8351 ignore = (target == const0_rtx
8352 || ((CONVERT_EXPR_CODE_P (code)
8353 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8354 && TREE_CODE (type) == VOID_TYPE));
8356 /* We should be called only if we need the result. */
8357 gcc_assert (!ignore);
8359 /* An operation in what may be a bit-field type needs the
8360 result to be reduced to the precision of the bit-field type,
8361 which is narrower than that of the type's mode. */
8362 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8363 && !type_has_mode_precision_p (type));
8365 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8366 target = 0;
8368 /* Use subtarget as the target for operand 0 of a binary operation. */
8369 subtarget = get_subtarget (target);
8370 original_target = target;
8372 switch (code)
8374 case NON_LVALUE_EXPR:
8375 case PAREN_EXPR:
8376 CASE_CONVERT:
8377 if (treeop0 == error_mark_node)
8378 return const0_rtx;
8380 if (TREE_CODE (type) == UNION_TYPE)
8382 tree valtype = TREE_TYPE (treeop0);
8384 /* If both input and output are BLKmode, this conversion isn't doing
8385 anything except possibly changing memory attribute. */
8386 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8388 rtx result = expand_expr (treeop0, target, tmode,
8389 modifier);
8391 result = copy_rtx (result);
8392 set_mem_attributes (result, type, 0);
8393 return result;
8396 if (target == 0)
8398 if (TYPE_MODE (type) != BLKmode)
8399 target = gen_reg_rtx (TYPE_MODE (type));
8400 else
8401 target = assign_temp (type, 1, 1);
8404 if (MEM_P (target))
8405 /* Store data into beginning of memory target. */
8406 store_expr (treeop0,
8407 adjust_address (target, TYPE_MODE (valtype), 0),
8408 modifier == EXPAND_STACK_PARM,
8409 false, TYPE_REVERSE_STORAGE_ORDER (type));
8411 else
8413 gcc_assert (REG_P (target)
8414 && !TYPE_REVERSE_STORAGE_ORDER (type));
8416 /* Store this field into a union of the proper type. */
8417 poly_uint64 op0_size
8418 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
8419 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
8420 store_field (target,
8421 /* The conversion must be constructed so that
8422 we know at compile time how many bits
8423 to preserve. */
8424 ordered_min (op0_size, union_size),
8425 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8426 false, false);
8429 /* Return the entire union. */
8430 return target;
8433 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8435 op0 = expand_expr (treeop0, target, VOIDmode,
8436 modifier);
8438 /* If the signedness of the conversion differs and OP0 is
8439 a promoted SUBREG, clear that indication since we now
8440 have to do the proper extension. */
8441 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8442 && GET_CODE (op0) == SUBREG)
8443 SUBREG_PROMOTED_VAR_P (op0) = 0;
8445 return REDUCE_BIT_FIELD (op0);
8448 op0 = expand_expr (treeop0, NULL_RTX, mode,
8449 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8450 if (GET_MODE (op0) == mode)
8453 /* If OP0 is a constant, just convert it into the proper mode. */
8454 else if (CONSTANT_P (op0))
8456 tree inner_type = TREE_TYPE (treeop0);
8457 machine_mode inner_mode = GET_MODE (op0);
8459 if (inner_mode == VOIDmode)
8460 inner_mode = TYPE_MODE (inner_type);
8462 if (modifier == EXPAND_INITIALIZER)
8463 op0 = lowpart_subreg (mode, op0, inner_mode);
8464 else
8465 op0= convert_modes (mode, inner_mode, op0,
8466 TYPE_UNSIGNED (inner_type));
8469 else if (modifier == EXPAND_INITIALIZER)
8470 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8471 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8473 else if (target == 0)
8474 op0 = convert_to_mode (mode, op0,
8475 TYPE_UNSIGNED (TREE_TYPE
8476 (treeop0)));
8477 else
8479 convert_move (target, op0,
8480 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8481 op0 = target;
8484 return REDUCE_BIT_FIELD (op0);
8486 case ADDR_SPACE_CONVERT_EXPR:
8488 tree treeop0_type = TREE_TYPE (treeop0);
8490 gcc_assert (POINTER_TYPE_P (type));
8491 gcc_assert (POINTER_TYPE_P (treeop0_type));
8493 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8494 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8496 /* Conversions between pointers to the same address space should
8497 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8498 gcc_assert (as_to != as_from);
8500 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8502 /* Ask target code to handle conversion between pointers
8503 to overlapping address spaces. */
8504 if (targetm.addr_space.subset_p (as_to, as_from)
8505 || targetm.addr_space.subset_p (as_from, as_to))
8507 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8509 else
8511 /* For disjoint address spaces, converting anything but a null
8512 pointer invokes undefined behavior. We truncate or extend the
8513 value as if we'd converted via integers, which handles 0 as
8514 required, and all others as the programmer likely expects. */
8515 #ifndef POINTERS_EXTEND_UNSIGNED
8516 const int POINTERS_EXTEND_UNSIGNED = 1;
8517 #endif
8518 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8519 op0, POINTERS_EXTEND_UNSIGNED);
8521 gcc_assert (op0);
8522 return op0;
8525 case POINTER_PLUS_EXPR:
8526 /* Even though the sizetype mode and the pointer's mode can be different
8527 expand is able to handle this correctly and get the correct result out
8528 of the PLUS_EXPR code. */
8529 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8530 if sizetype precision is smaller than pointer precision. */
8531 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8532 treeop1 = fold_convert_loc (loc, type,
8533 fold_convert_loc (loc, ssizetype,
8534 treeop1));
8535 /* If sizetype precision is larger than pointer precision, truncate the
8536 offset to have matching modes. */
8537 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8538 treeop1 = fold_convert_loc (loc, type, treeop1);
8539 /* FALLTHRU */
8541 case PLUS_EXPR:
8542 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8543 something else, make sure we add the register to the constant and
8544 then to the other thing. This case can occur during strength
8545 reduction and doing it this way will produce better code if the
8546 frame pointer or argument pointer is eliminated.
8548 fold-const.c will ensure that the constant is always in the inner
8549 PLUS_EXPR, so the only case we need to do anything about is if
8550 sp, ap, or fp is our second argument, in which case we must swap
8551 the innermost first argument and our second argument. */
8553 if (TREE_CODE (treeop0) == PLUS_EXPR
8554 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8555 && VAR_P (treeop1)
8556 && (DECL_RTL (treeop1) == frame_pointer_rtx
8557 || DECL_RTL (treeop1) == stack_pointer_rtx
8558 || DECL_RTL (treeop1) == arg_pointer_rtx))
8560 gcc_unreachable ();
8563 /* If the result is to be ptr_mode and we are adding an integer to
8564 something, we might be forming a constant. So try to use
8565 plus_constant. If it produces a sum and we can't accept it,
8566 use force_operand. This allows P = &ARR[const] to generate
8567 efficient code on machines where a SYMBOL_REF is not a valid
8568 address.
8570 If this is an EXPAND_SUM call, always return the sum. */
8571 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8572 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8574 if (modifier == EXPAND_STACK_PARM)
8575 target = 0;
8576 if (TREE_CODE (treeop0) == INTEGER_CST
8577 && HWI_COMPUTABLE_MODE_P (mode)
8578 && TREE_CONSTANT (treeop1))
8580 rtx constant_part;
8581 HOST_WIDE_INT wc;
8582 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8584 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8585 EXPAND_SUM);
8586 /* Use wi::shwi to ensure that the constant is
8587 truncated according to the mode of OP1, then sign extended
8588 to a HOST_WIDE_INT. Using the constant directly can result
8589 in non-canonical RTL in a 64x32 cross compile. */
8590 wc = TREE_INT_CST_LOW (treeop0);
8591 constant_part =
8592 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8593 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8594 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8595 op1 = force_operand (op1, target);
8596 return REDUCE_BIT_FIELD (op1);
8599 else if (TREE_CODE (treeop1) == INTEGER_CST
8600 && HWI_COMPUTABLE_MODE_P (mode)
8601 && TREE_CONSTANT (treeop0))
8603 rtx constant_part;
8604 HOST_WIDE_INT wc;
8605 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8607 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8608 (modifier == EXPAND_INITIALIZER
8609 ? EXPAND_INITIALIZER : EXPAND_SUM));
8610 if (! CONSTANT_P (op0))
8612 op1 = expand_expr (treeop1, NULL_RTX,
8613 VOIDmode, modifier);
8614 /* Return a PLUS if modifier says it's OK. */
8615 if (modifier == EXPAND_SUM
8616 || modifier == EXPAND_INITIALIZER)
8617 return simplify_gen_binary (PLUS, mode, op0, op1);
8618 goto binop2;
8620 /* Use wi::shwi to ensure that the constant is
8621 truncated according to the mode of OP1, then sign extended
8622 to a HOST_WIDE_INT. Using the constant directly can result
8623 in non-canonical RTL in a 64x32 cross compile. */
8624 wc = TREE_INT_CST_LOW (treeop1);
8625 constant_part
8626 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8627 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8628 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8629 op0 = force_operand (op0, target);
8630 return REDUCE_BIT_FIELD (op0);
8634 /* Use TER to expand pointer addition of a negated value
8635 as pointer subtraction. */
8636 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8637 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8638 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8639 && TREE_CODE (treeop1) == SSA_NAME
8640 && TYPE_MODE (TREE_TYPE (treeop0))
8641 == TYPE_MODE (TREE_TYPE (treeop1)))
8643 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8644 if (def)
8646 treeop1 = gimple_assign_rhs1 (def);
8647 code = MINUS_EXPR;
8648 goto do_minus;
8652 /* No sense saving up arithmetic to be done
8653 if it's all in the wrong mode to form part of an address.
8654 And force_operand won't know whether to sign-extend or
8655 zero-extend. */
8656 if (modifier != EXPAND_INITIALIZER
8657 && (modifier != EXPAND_SUM || mode != ptr_mode))
8659 expand_operands (treeop0, treeop1,
8660 subtarget, &op0, &op1, modifier);
8661 if (op0 == const0_rtx)
8662 return op1;
8663 if (op1 == const0_rtx)
8664 return op0;
8665 goto binop2;
8668 expand_operands (treeop0, treeop1,
8669 subtarget, &op0, &op1, modifier);
8670 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8672 case MINUS_EXPR:
8673 case POINTER_DIFF_EXPR:
8674 do_minus:
8675 /* For initializers, we are allowed to return a MINUS of two
8676 symbolic constants. Here we handle all cases when both operands
8677 are constant. */
8678 /* Handle difference of two symbolic constants,
8679 for the sake of an initializer. */
8680 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8681 && really_constant_p (treeop0)
8682 && really_constant_p (treeop1))
8684 expand_operands (treeop0, treeop1,
8685 NULL_RTX, &op0, &op1, modifier);
8686 return simplify_gen_binary (MINUS, mode, op0, op1);
8689 /* No sense saving up arithmetic to be done
8690 if it's all in the wrong mode to form part of an address.
8691 And force_operand won't know whether to sign-extend or
8692 zero-extend. */
8693 if (modifier != EXPAND_INITIALIZER
8694 && (modifier != EXPAND_SUM || mode != ptr_mode))
8695 goto binop;
8697 expand_operands (treeop0, treeop1,
8698 subtarget, &op0, &op1, modifier);
8700 /* Convert A - const to A + (-const). */
8701 if (CONST_INT_P (op1))
8703 op1 = negate_rtx (mode, op1);
8704 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8707 goto binop2;
8709 case WIDEN_MULT_PLUS_EXPR:
8710 case WIDEN_MULT_MINUS_EXPR:
8711 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8712 op2 = expand_normal (treeop2);
8713 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8714 target, unsignedp);
8715 return target;
8717 case WIDEN_MULT_EXPR:
8718 /* If first operand is constant, swap them.
8719 Thus the following special case checks need only
8720 check the second operand. */
8721 if (TREE_CODE (treeop0) == INTEGER_CST)
8722 std::swap (treeop0, treeop1);
8724 /* First, check if we have a multiplication of one signed and one
8725 unsigned operand. */
8726 if (TREE_CODE (treeop1) != INTEGER_CST
8727 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8728 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8730 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8731 this_optab = usmul_widen_optab;
8732 if (find_widening_optab_handler (this_optab, mode, innermode)
8733 != CODE_FOR_nothing)
8735 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8736 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8737 EXPAND_NORMAL);
8738 else
8739 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8740 EXPAND_NORMAL);
8741 /* op0 and op1 might still be constant, despite the above
8742 != INTEGER_CST check. Handle it. */
8743 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8745 op0 = convert_modes (innermode, mode, op0, true);
8746 op1 = convert_modes (innermode, mode, op1, false);
8747 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8748 target, unsignedp));
8750 goto binop3;
8753 /* Check for a multiplication with matching signedness. */
8754 else if ((TREE_CODE (treeop1) == INTEGER_CST
8755 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8756 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8757 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8759 tree op0type = TREE_TYPE (treeop0);
8760 machine_mode innermode = TYPE_MODE (op0type);
8761 bool zextend_p = TYPE_UNSIGNED (op0type);
8762 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8763 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8765 if (TREE_CODE (treeop0) != INTEGER_CST)
8767 if (find_widening_optab_handler (this_optab, mode, innermode)
8768 != CODE_FOR_nothing)
8770 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8771 EXPAND_NORMAL);
8772 /* op0 and op1 might still be constant, despite the above
8773 != INTEGER_CST check. Handle it. */
8774 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8776 widen_mult_const:
8777 op0 = convert_modes (innermode, mode, op0, zextend_p);
8779 = convert_modes (innermode, mode, op1,
8780 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8781 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8782 target,
8783 unsignedp));
8785 temp = expand_widening_mult (mode, op0, op1, target,
8786 unsignedp, this_optab);
8787 return REDUCE_BIT_FIELD (temp);
8789 if (find_widening_optab_handler (other_optab, mode, innermode)
8790 != CODE_FOR_nothing
8791 && innermode == word_mode)
8793 rtx htem, hipart;
8794 op0 = expand_normal (treeop0);
8795 if (TREE_CODE (treeop1) == INTEGER_CST)
8796 op1 = convert_modes (word_mode, mode,
8797 expand_normal (treeop1),
8798 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8799 else
8800 op1 = expand_normal (treeop1);
8801 /* op0 and op1 might still be constant, despite the above
8802 != INTEGER_CST check. Handle it. */
8803 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8804 goto widen_mult_const;
8805 temp = expand_binop (mode, other_optab, op0, op1, target,
8806 unsignedp, OPTAB_LIB_WIDEN);
8807 hipart = gen_highpart (word_mode, temp);
8808 htem = expand_mult_highpart_adjust (word_mode, hipart,
8809 op0, op1, hipart,
8810 zextend_p);
8811 if (htem != hipart)
8812 emit_move_insn (hipart, htem);
8813 return REDUCE_BIT_FIELD (temp);
8817 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8818 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8819 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8820 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8822 case FMA_EXPR:
8824 optab opt = fma_optab;
8825 gimple *def0, *def2;
8827 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8828 call. */
8829 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8831 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8832 tree call_expr;
8834 gcc_assert (fn != NULL_TREE);
8835 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8836 return expand_builtin (call_expr, target, subtarget, mode, false);
8839 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8840 /* The multiplication is commutative - look at its 2nd operand
8841 if the first isn't fed by a negate. */
8842 if (!def0)
8844 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8845 /* Swap operands if the 2nd operand is fed by a negate. */
8846 if (def0)
8847 std::swap (treeop0, treeop1);
8849 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8851 op0 = op2 = NULL;
8853 if (def0 && def2
8854 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8856 opt = fnms_optab;
8857 op0 = expand_normal (gimple_assign_rhs1 (def0));
8858 op2 = expand_normal (gimple_assign_rhs1 (def2));
8860 else if (def0
8861 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8863 opt = fnma_optab;
8864 op0 = expand_normal (gimple_assign_rhs1 (def0));
8866 else if (def2
8867 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8869 opt = fms_optab;
8870 op2 = expand_normal (gimple_assign_rhs1 (def2));
8873 if (op0 == NULL)
8874 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8875 if (op2 == NULL)
8876 op2 = expand_normal (treeop2);
8877 op1 = expand_normal (treeop1);
8879 return expand_ternary_op (TYPE_MODE (type), opt,
8880 op0, op1, op2, target, 0);
8883 case MULT_EXPR:
8884 /* If this is a fixed-point operation, then we cannot use the code
8885 below because "expand_mult" doesn't support sat/no-sat fixed-point
8886 multiplications. */
8887 if (ALL_FIXED_POINT_MODE_P (mode))
8888 goto binop;
8890 /* If first operand is constant, swap them.
8891 Thus the following special case checks need only
8892 check the second operand. */
8893 if (TREE_CODE (treeop0) == INTEGER_CST)
8894 std::swap (treeop0, treeop1);
8896 /* Attempt to return something suitable for generating an
8897 indexed address, for machines that support that. */
8899 if (modifier == EXPAND_SUM && mode == ptr_mode
8900 && tree_fits_shwi_p (treeop1))
8902 tree exp1 = treeop1;
8904 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8905 EXPAND_SUM);
8907 if (!REG_P (op0))
8908 op0 = force_operand (op0, NULL_RTX);
8909 if (!REG_P (op0))
8910 op0 = copy_to_mode_reg (mode, op0);
8912 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8913 gen_int_mode (tree_to_shwi (exp1),
8914 TYPE_MODE (TREE_TYPE (exp1)))));
8917 if (modifier == EXPAND_STACK_PARM)
8918 target = 0;
8920 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8921 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8923 case TRUNC_MOD_EXPR:
8924 case FLOOR_MOD_EXPR:
8925 case CEIL_MOD_EXPR:
8926 case ROUND_MOD_EXPR:
8928 case TRUNC_DIV_EXPR:
8929 case FLOOR_DIV_EXPR:
8930 case CEIL_DIV_EXPR:
8931 case ROUND_DIV_EXPR:
8932 case EXACT_DIV_EXPR:
8934 /* If this is a fixed-point operation, then we cannot use the code
8935 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8936 divisions. */
8937 if (ALL_FIXED_POINT_MODE_P (mode))
8938 goto binop;
8940 if (modifier == EXPAND_STACK_PARM)
8941 target = 0;
8942 /* Possible optimization: compute the dividend with EXPAND_SUM
8943 then if the divisor is constant can optimize the case
8944 where some terms of the dividend have coeffs divisible by it. */
8945 expand_operands (treeop0, treeop1,
8946 subtarget, &op0, &op1, EXPAND_NORMAL);
8947 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8948 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8949 if (SCALAR_INT_MODE_P (mode)
8950 && optimize >= 2
8951 && get_range_pos_neg (treeop0) == 1
8952 && get_range_pos_neg (treeop1) == 1)
8954 /* If both arguments are known to be positive when interpreted
8955 as signed, we can expand it as both signed and unsigned
8956 division or modulo. Choose the cheaper sequence in that case. */
8957 bool speed_p = optimize_insn_for_speed_p ();
8958 do_pending_stack_adjust ();
8959 start_sequence ();
8960 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8961 rtx_insn *uns_insns = get_insns ();
8962 end_sequence ();
8963 start_sequence ();
8964 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8965 rtx_insn *sgn_insns = get_insns ();
8966 end_sequence ();
8967 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8968 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8970 /* If costs are the same then use as tie breaker the other
8971 other factor. */
8972 if (uns_cost == sgn_cost)
8974 uns_cost = seq_cost (uns_insns, !speed_p);
8975 sgn_cost = seq_cost (sgn_insns, !speed_p);
8978 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8980 emit_insn (uns_insns);
8981 return uns_ret;
8983 emit_insn (sgn_insns);
8984 return sgn_ret;
8986 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8988 case RDIV_EXPR:
8989 goto binop;
8991 case MULT_HIGHPART_EXPR:
8992 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8993 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8994 gcc_assert (temp);
8995 return temp;
8997 case FIXED_CONVERT_EXPR:
8998 op0 = expand_normal (treeop0);
8999 if (target == 0 || modifier == EXPAND_STACK_PARM)
9000 target = gen_reg_rtx (mode);
9002 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9003 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9004 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9005 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9006 else
9007 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9008 return target;
9010 case FIX_TRUNC_EXPR:
9011 op0 = expand_normal (treeop0);
9012 if (target == 0 || modifier == EXPAND_STACK_PARM)
9013 target = gen_reg_rtx (mode);
9014 expand_fix (target, op0, unsignedp);
9015 return target;
9017 case FLOAT_EXPR:
9018 op0 = expand_normal (treeop0);
9019 if (target == 0 || modifier == EXPAND_STACK_PARM)
9020 target = gen_reg_rtx (mode);
9021 /* expand_float can't figure out what to do if FROM has VOIDmode.
9022 So give it the correct mode. With -O, cse will optimize this. */
9023 if (GET_MODE (op0) == VOIDmode)
9024 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9025 op0);
9026 expand_float (target, op0,
9027 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9028 return target;
9030 case NEGATE_EXPR:
9031 op0 = expand_expr (treeop0, subtarget,
9032 VOIDmode, EXPAND_NORMAL);
9033 if (modifier == EXPAND_STACK_PARM)
9034 target = 0;
9035 temp = expand_unop (mode,
9036 optab_for_tree_code (NEGATE_EXPR, type,
9037 optab_default),
9038 op0, target, 0);
9039 gcc_assert (temp);
9040 return REDUCE_BIT_FIELD (temp);
9042 case ABS_EXPR:
9043 op0 = expand_expr (treeop0, subtarget,
9044 VOIDmode, EXPAND_NORMAL);
9045 if (modifier == EXPAND_STACK_PARM)
9046 target = 0;
9048 /* ABS_EXPR is not valid for complex arguments. */
9049 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9050 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9052 /* Unsigned abs is simply the operand. Testing here means we don't
9053 risk generating incorrect code below. */
9054 if (TYPE_UNSIGNED (type))
9055 return op0;
9057 return expand_abs (mode, op0, target, unsignedp,
9058 safe_from_p (target, treeop0, 1));
9060 case MAX_EXPR:
9061 case MIN_EXPR:
9062 target = original_target;
9063 if (target == 0
9064 || modifier == EXPAND_STACK_PARM
9065 || (MEM_P (target) && MEM_VOLATILE_P (target))
9066 || GET_MODE (target) != mode
9067 || (REG_P (target)
9068 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9069 target = gen_reg_rtx (mode);
9070 expand_operands (treeop0, treeop1,
9071 target, &op0, &op1, EXPAND_NORMAL);
9073 /* First try to do it with a special MIN or MAX instruction.
9074 If that does not win, use a conditional jump to select the proper
9075 value. */
9076 this_optab = optab_for_tree_code (code, type, optab_default);
9077 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9078 OPTAB_WIDEN);
9079 if (temp != 0)
9080 return temp;
9082 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
9083 and similarly for MAX <x, y>. */
9084 if (VECTOR_TYPE_P (type))
9086 tree t0 = make_tree (type, op0);
9087 tree t1 = make_tree (type, op1);
9088 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
9089 type, t0, t1);
9090 return expand_vec_cond_expr (type, comparison, t0, t1,
9091 original_target);
9094 /* At this point, a MEM target is no longer useful; we will get better
9095 code without it. */
9097 if (! REG_P (target))
9098 target = gen_reg_rtx (mode);
9100 /* If op1 was placed in target, swap op0 and op1. */
9101 if (target != op0 && target == op1)
9102 std::swap (op0, op1);
9104 /* We generate better code and avoid problems with op1 mentioning
9105 target by forcing op1 into a pseudo if it isn't a constant. */
9106 if (! CONSTANT_P (op1))
9107 op1 = force_reg (mode, op1);
9110 enum rtx_code comparison_code;
9111 rtx cmpop1 = op1;
9113 if (code == MAX_EXPR)
9114 comparison_code = unsignedp ? GEU : GE;
9115 else
9116 comparison_code = unsignedp ? LEU : LE;
9118 /* Canonicalize to comparisons against 0. */
9119 if (op1 == const1_rtx)
9121 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9122 or (a != 0 ? a : 1) for unsigned.
9123 For MIN we are safe converting (a <= 1 ? a : 1)
9124 into (a <= 0 ? a : 1) */
9125 cmpop1 = const0_rtx;
9126 if (code == MAX_EXPR)
9127 comparison_code = unsignedp ? NE : GT;
9129 if (op1 == constm1_rtx && !unsignedp)
9131 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9132 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9133 cmpop1 = const0_rtx;
9134 if (code == MIN_EXPR)
9135 comparison_code = LT;
9138 /* Use a conditional move if possible. */
9139 if (can_conditionally_move_p (mode))
9141 rtx insn;
9143 start_sequence ();
9145 /* Try to emit the conditional move. */
9146 insn = emit_conditional_move (target, comparison_code,
9147 op0, cmpop1, mode,
9148 op0, op1, mode,
9149 unsignedp);
9151 /* If we could do the conditional move, emit the sequence,
9152 and return. */
9153 if (insn)
9155 rtx_insn *seq = get_insns ();
9156 end_sequence ();
9157 emit_insn (seq);
9158 return target;
9161 /* Otherwise discard the sequence and fall back to code with
9162 branches. */
9163 end_sequence ();
9166 if (target != op0)
9167 emit_move_insn (target, op0);
9169 lab = gen_label_rtx ();
9170 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9171 unsignedp, mode, NULL_RTX, NULL, lab,
9172 profile_probability::uninitialized ());
9174 emit_move_insn (target, op1);
9175 emit_label (lab);
9176 return target;
9178 case BIT_NOT_EXPR:
9179 op0 = expand_expr (treeop0, subtarget,
9180 VOIDmode, EXPAND_NORMAL);
9181 if (modifier == EXPAND_STACK_PARM)
9182 target = 0;
9183 /* In case we have to reduce the result to bitfield precision
9184 for unsigned bitfield expand this as XOR with a proper constant
9185 instead. */
9186 if (reduce_bit_field && TYPE_UNSIGNED (type))
9188 int_mode = SCALAR_INT_TYPE_MODE (type);
9189 wide_int mask = wi::mask (TYPE_PRECISION (type),
9190 false, GET_MODE_PRECISION (int_mode));
9192 temp = expand_binop (int_mode, xor_optab, op0,
9193 immed_wide_int_const (mask, int_mode),
9194 target, 1, OPTAB_LIB_WIDEN);
9196 else
9197 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9198 gcc_assert (temp);
9199 return temp;
9201 /* ??? Can optimize bitwise operations with one arg constant.
9202 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9203 and (a bitwise1 b) bitwise2 b (etc)
9204 but that is probably not worth while. */
9206 case BIT_AND_EXPR:
9207 case BIT_IOR_EXPR:
9208 case BIT_XOR_EXPR:
9209 goto binop;
9211 case LROTATE_EXPR:
9212 case RROTATE_EXPR:
9213 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9214 || type_has_mode_precision_p (type));
9215 /* fall through */
9217 case LSHIFT_EXPR:
9218 case RSHIFT_EXPR:
9220 /* If this is a fixed-point operation, then we cannot use the code
9221 below because "expand_shift" doesn't support sat/no-sat fixed-point
9222 shifts. */
9223 if (ALL_FIXED_POINT_MODE_P (mode))
9224 goto binop;
9226 if (! safe_from_p (subtarget, treeop1, 1))
9227 subtarget = 0;
9228 if (modifier == EXPAND_STACK_PARM)
9229 target = 0;
9230 op0 = expand_expr (treeop0, subtarget,
9231 VOIDmode, EXPAND_NORMAL);
9233 /* Left shift optimization when shifting across word_size boundary.
9235 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9236 there isn't native instruction to support this wide mode
9237 left shift. Given below scenario:
9239 Type A = (Type) B << C
9241 |< T >|
9242 | dest_high | dest_low |
9244 | word_size |
9246 If the shift amount C caused we shift B to across the word
9247 size boundary, i.e part of B shifted into high half of
9248 destination register, and part of B remains in the low
9249 half, then GCC will use the following left shift expand
9250 logic:
9252 1. Initialize dest_low to B.
9253 2. Initialize every bit of dest_high to the sign bit of B.
9254 3. Logic left shift dest_low by C bit to finalize dest_low.
9255 The value of dest_low before this shift is kept in a temp D.
9256 4. Logic left shift dest_high by C.
9257 5. Logic right shift D by (word_size - C).
9258 6. Or the result of 4 and 5 to finalize dest_high.
9260 While, by checking gimple statements, if operand B is
9261 coming from signed extension, then we can simplify above
9262 expand logic into:
9264 1. dest_high = src_low >> (word_size - C).
9265 2. dest_low = src_low << C.
9267 We can use one arithmetic right shift to finish all the
9268 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9269 needed from 6 into 2.
9271 The case is similar for zero extension, except that we
9272 initialize dest_high to zero rather than copies of the sign
9273 bit from B. Furthermore, we need to use a logical right shift
9274 in this case.
9276 The choice of sign-extension versus zero-extension is
9277 determined entirely by whether or not B is signed and is
9278 independent of the current setting of unsignedp. */
9280 temp = NULL_RTX;
9281 if (code == LSHIFT_EXPR
9282 && target
9283 && REG_P (target)
9284 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9285 && mode == int_mode
9286 && TREE_CONSTANT (treeop1)
9287 && TREE_CODE (treeop0) == SSA_NAME)
9289 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9290 if (is_gimple_assign (def)
9291 && gimple_assign_rhs_code (def) == NOP_EXPR)
9293 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9294 (TREE_TYPE (gimple_assign_rhs1 (def)));
9296 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9297 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9298 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9299 >= GET_MODE_BITSIZE (word_mode)))
9301 rtx_insn *seq, *seq_old;
9302 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9303 int_mode);
9304 bool extend_unsigned
9305 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9306 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9307 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9308 rtx dest_high = simplify_gen_subreg (word_mode, target,
9309 int_mode, high_off);
9310 HOST_WIDE_INT ramount = (BITS_PER_WORD
9311 - TREE_INT_CST_LOW (treeop1));
9312 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9314 start_sequence ();
9315 /* dest_high = src_low >> (word_size - C). */
9316 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9317 rshift, dest_high,
9318 extend_unsigned);
9319 if (temp != dest_high)
9320 emit_move_insn (dest_high, temp);
9322 /* dest_low = src_low << C. */
9323 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9324 treeop1, dest_low, unsignedp);
9325 if (temp != dest_low)
9326 emit_move_insn (dest_low, temp);
9328 seq = get_insns ();
9329 end_sequence ();
9330 temp = target ;
9332 if (have_insn_for (ASHIFT, int_mode))
9334 bool speed_p = optimize_insn_for_speed_p ();
9335 start_sequence ();
9336 rtx ret_old = expand_variable_shift (code, int_mode,
9337 op0, treeop1,
9338 target,
9339 unsignedp);
9341 seq_old = get_insns ();
9342 end_sequence ();
9343 if (seq_cost (seq, speed_p)
9344 >= seq_cost (seq_old, speed_p))
9346 seq = seq_old;
9347 temp = ret_old;
9350 emit_insn (seq);
9355 if (temp == NULL_RTX)
9356 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9357 unsignedp);
9358 if (code == LSHIFT_EXPR)
9359 temp = REDUCE_BIT_FIELD (temp);
9360 return temp;
9363 /* Could determine the answer when only additive constants differ. Also,
9364 the addition of one can be handled by changing the condition. */
9365 case LT_EXPR:
9366 case LE_EXPR:
9367 case GT_EXPR:
9368 case GE_EXPR:
9369 case EQ_EXPR:
9370 case NE_EXPR:
9371 case UNORDERED_EXPR:
9372 case ORDERED_EXPR:
9373 case UNLT_EXPR:
9374 case UNLE_EXPR:
9375 case UNGT_EXPR:
9376 case UNGE_EXPR:
9377 case UNEQ_EXPR:
9378 case LTGT_EXPR:
9380 temp = do_store_flag (ops,
9381 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9382 tmode != VOIDmode ? tmode : mode);
9383 if (temp)
9384 return temp;
9386 /* Use a compare and a jump for BLKmode comparisons, or for function
9387 type comparisons is have_canonicalize_funcptr_for_compare. */
9389 if ((target == 0
9390 || modifier == EXPAND_STACK_PARM
9391 || ! safe_from_p (target, treeop0, 1)
9392 || ! safe_from_p (target, treeop1, 1)
9393 /* Make sure we don't have a hard reg (such as function's return
9394 value) live across basic blocks, if not optimizing. */
9395 || (!optimize && REG_P (target)
9396 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9397 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9399 emit_move_insn (target, const0_rtx);
9401 rtx_code_label *lab1 = gen_label_rtx ();
9402 jumpifnot_1 (code, treeop0, treeop1, lab1,
9403 profile_probability::uninitialized ());
9405 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9406 emit_move_insn (target, constm1_rtx);
9407 else
9408 emit_move_insn (target, const1_rtx);
9410 emit_label (lab1);
9411 return target;
9413 case COMPLEX_EXPR:
9414 /* Get the rtx code of the operands. */
9415 op0 = expand_normal (treeop0);
9416 op1 = expand_normal (treeop1);
9418 if (!target)
9419 target = gen_reg_rtx (TYPE_MODE (type));
9420 else
9421 /* If target overlaps with op1, then either we need to force
9422 op1 into a pseudo (if target also overlaps with op0),
9423 or write the complex parts in reverse order. */
9424 switch (GET_CODE (target))
9426 case CONCAT:
9427 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9429 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9431 complex_expr_force_op1:
9432 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9433 emit_move_insn (temp, op1);
9434 op1 = temp;
9435 break;
9437 complex_expr_swap_order:
9438 /* Move the imaginary (op1) and real (op0) parts to their
9439 location. */
9440 write_complex_part (target, op1, true);
9441 write_complex_part (target, op0, false);
9443 return target;
9445 break;
9446 case MEM:
9447 temp = adjust_address_nv (target,
9448 GET_MODE_INNER (GET_MODE (target)), 0);
9449 if (reg_overlap_mentioned_p (temp, op1))
9451 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9452 temp = adjust_address_nv (target, imode,
9453 GET_MODE_SIZE (imode));
9454 if (reg_overlap_mentioned_p (temp, op0))
9455 goto complex_expr_force_op1;
9456 goto complex_expr_swap_order;
9458 break;
9459 default:
9460 if (reg_overlap_mentioned_p (target, op1))
9462 if (reg_overlap_mentioned_p (target, op0))
9463 goto complex_expr_force_op1;
9464 goto complex_expr_swap_order;
9466 break;
9469 /* Move the real (op0) and imaginary (op1) parts to their location. */
9470 write_complex_part (target, op0, false);
9471 write_complex_part (target, op1, true);
9473 return target;
9475 case WIDEN_SUM_EXPR:
9477 tree oprnd0 = treeop0;
9478 tree oprnd1 = treeop1;
9480 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9481 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9482 target, unsignedp);
9483 return target;
9486 case VEC_UNPACK_HI_EXPR:
9487 case VEC_UNPACK_LO_EXPR:
9489 op0 = expand_normal (treeop0);
9490 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9491 target, unsignedp);
9492 gcc_assert (temp);
9493 return temp;
9496 case VEC_UNPACK_FLOAT_HI_EXPR:
9497 case VEC_UNPACK_FLOAT_LO_EXPR:
9499 op0 = expand_normal (treeop0);
9500 /* The signedness is determined from input operand. */
9501 temp = expand_widen_pattern_expr
9502 (ops, op0, NULL_RTX, NULL_RTX,
9503 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9505 gcc_assert (temp);
9506 return temp;
9509 case VEC_WIDEN_MULT_HI_EXPR:
9510 case VEC_WIDEN_MULT_LO_EXPR:
9511 case VEC_WIDEN_MULT_EVEN_EXPR:
9512 case VEC_WIDEN_MULT_ODD_EXPR:
9513 case VEC_WIDEN_LSHIFT_HI_EXPR:
9514 case VEC_WIDEN_LSHIFT_LO_EXPR:
9515 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9516 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9517 target, unsignedp);
9518 gcc_assert (target);
9519 return target;
9521 case VEC_PACK_TRUNC_EXPR:
9522 case VEC_PACK_SAT_EXPR:
9523 case VEC_PACK_FIX_TRUNC_EXPR:
9524 mode = TYPE_MODE (TREE_TYPE (treeop0));
9525 goto binop;
9527 case VEC_PERM_EXPR:
9529 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9530 vec_perm_builder sel;
9531 if (TREE_CODE (treeop2) == VECTOR_CST
9532 && tree_to_vec_perm_builder (&sel, treeop2))
9534 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
9535 temp = expand_vec_perm_const (mode, op0, op1, sel,
9536 sel_mode, target);
9538 else
9540 op2 = expand_normal (treeop2);
9541 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
9543 gcc_assert (temp);
9544 return temp;
9547 case DOT_PROD_EXPR:
9549 tree oprnd0 = treeop0;
9550 tree oprnd1 = treeop1;
9551 tree oprnd2 = treeop2;
9552 rtx op2;
9554 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9555 op2 = expand_normal (oprnd2);
9556 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9557 target, unsignedp);
9558 return target;
9561 case SAD_EXPR:
9563 tree oprnd0 = treeop0;
9564 tree oprnd1 = treeop1;
9565 tree oprnd2 = treeop2;
9566 rtx op2;
9568 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9569 op2 = expand_normal (oprnd2);
9570 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9571 target, unsignedp);
9572 return target;
9575 case REALIGN_LOAD_EXPR:
9577 tree oprnd0 = treeop0;
9578 tree oprnd1 = treeop1;
9579 tree oprnd2 = treeop2;
9580 rtx op2;
9582 this_optab = optab_for_tree_code (code, type, optab_default);
9583 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9584 op2 = expand_normal (oprnd2);
9585 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9586 target, unsignedp);
9587 gcc_assert (temp);
9588 return temp;
9591 case COND_EXPR:
9593 /* A COND_EXPR with its type being VOID_TYPE represents a
9594 conditional jump and is handled in
9595 expand_gimple_cond_expr. */
9596 gcc_assert (!VOID_TYPE_P (type));
9598 /* Note that COND_EXPRs whose type is a structure or union
9599 are required to be constructed to contain assignments of
9600 a temporary variable, so that we can evaluate them here
9601 for side effect only. If type is void, we must do likewise. */
9603 gcc_assert (!TREE_ADDRESSABLE (type)
9604 && !ignore
9605 && TREE_TYPE (treeop1) != void_type_node
9606 && TREE_TYPE (treeop2) != void_type_node);
9608 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9609 if (temp)
9610 return temp;
9612 /* If we are not to produce a result, we have no target. Otherwise,
9613 if a target was specified use it; it will not be used as an
9614 intermediate target unless it is safe. If no target, use a
9615 temporary. */
9617 if (modifier != EXPAND_STACK_PARM
9618 && original_target
9619 && safe_from_p (original_target, treeop0, 1)
9620 && GET_MODE (original_target) == mode
9621 && !MEM_P (original_target))
9622 temp = original_target;
9623 else
9624 temp = assign_temp (type, 0, 1);
9626 do_pending_stack_adjust ();
9627 NO_DEFER_POP;
9628 rtx_code_label *lab0 = gen_label_rtx ();
9629 rtx_code_label *lab1 = gen_label_rtx ();
9630 jumpifnot (treeop0, lab0,
9631 profile_probability::uninitialized ());
9632 store_expr (treeop1, temp,
9633 modifier == EXPAND_STACK_PARM,
9634 false, false);
9636 emit_jump_insn (targetm.gen_jump (lab1));
9637 emit_barrier ();
9638 emit_label (lab0);
9639 store_expr (treeop2, temp,
9640 modifier == EXPAND_STACK_PARM,
9641 false, false);
9643 emit_label (lab1);
9644 OK_DEFER_POP;
9645 return temp;
9648 case VEC_COND_EXPR:
9649 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9650 return target;
9652 case VEC_DUPLICATE_EXPR:
9653 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9654 target = expand_vector_broadcast (mode, op0);
9655 gcc_assert (target);
9656 return target;
9658 case VEC_SERIES_EXPR:
9659 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
9660 return expand_vec_series_expr (mode, op0, op1, target);
9662 case BIT_INSERT_EXPR:
9664 unsigned bitpos = tree_to_uhwi (treeop2);
9665 unsigned bitsize;
9666 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9667 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9668 else
9669 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9670 rtx op0 = expand_normal (treeop0);
9671 rtx op1 = expand_normal (treeop1);
9672 rtx dst = gen_reg_rtx (mode);
9673 emit_move_insn (dst, op0);
9674 store_bit_field (dst, bitsize, bitpos, 0, 0,
9675 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9676 return dst;
9679 default:
9680 gcc_unreachable ();
9683 /* Here to do an ordinary binary operator. */
9684 binop:
9685 expand_operands (treeop0, treeop1,
9686 subtarget, &op0, &op1, EXPAND_NORMAL);
9687 binop2:
9688 this_optab = optab_for_tree_code (code, type, optab_default);
9689 binop3:
9690 if (modifier == EXPAND_STACK_PARM)
9691 target = 0;
9692 temp = expand_binop (mode, this_optab, op0, op1, target,
9693 unsignedp, OPTAB_LIB_WIDEN);
9694 gcc_assert (temp);
9695 /* Bitwise operations do not need bitfield reduction as we expect their
9696 operands being properly truncated. */
9697 if (code == BIT_XOR_EXPR
9698 || code == BIT_AND_EXPR
9699 || code == BIT_IOR_EXPR)
9700 return temp;
9701 return REDUCE_BIT_FIELD (temp);
9703 #undef REDUCE_BIT_FIELD
9706 /* Return TRUE if expression STMT is suitable for replacement.
9707 Never consider memory loads as replaceable, because those don't ever lead
9708 into constant expressions. */
9710 static bool
9711 stmt_is_replaceable_p (gimple *stmt)
9713 if (ssa_is_replaceable_p (stmt))
9715 /* Don't move around loads. */
9716 if (!gimple_assign_single_p (stmt)
9717 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9718 return true;
9720 return false;
9724 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9725 enum expand_modifier modifier, rtx *alt_rtl,
9726 bool inner_reference_p)
9728 rtx op0, op1, temp, decl_rtl;
9729 tree type;
9730 int unsignedp;
9731 machine_mode mode, dmode;
9732 enum tree_code code = TREE_CODE (exp);
9733 rtx subtarget, original_target;
9734 int ignore;
9735 tree context;
9736 bool reduce_bit_field;
9737 location_t loc = EXPR_LOCATION (exp);
9738 struct separate_ops ops;
9739 tree treeop0, treeop1, treeop2;
9740 tree ssa_name = NULL_TREE;
9741 gimple *g;
9743 type = TREE_TYPE (exp);
9744 mode = TYPE_MODE (type);
9745 unsignedp = TYPE_UNSIGNED (type);
9747 treeop0 = treeop1 = treeop2 = NULL_TREE;
9748 if (!VL_EXP_CLASS_P (exp))
9749 switch (TREE_CODE_LENGTH (code))
9751 default:
9752 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9753 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9754 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9755 case 0: break;
9757 ops.code = code;
9758 ops.type = type;
9759 ops.op0 = treeop0;
9760 ops.op1 = treeop1;
9761 ops.op2 = treeop2;
9762 ops.location = loc;
9764 ignore = (target == const0_rtx
9765 || ((CONVERT_EXPR_CODE_P (code)
9766 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9767 && TREE_CODE (type) == VOID_TYPE));
9769 /* An operation in what may be a bit-field type needs the
9770 result to be reduced to the precision of the bit-field type,
9771 which is narrower than that of the type's mode. */
9772 reduce_bit_field = (!ignore
9773 && INTEGRAL_TYPE_P (type)
9774 && !type_has_mode_precision_p (type));
9776 /* If we are going to ignore this result, we need only do something
9777 if there is a side-effect somewhere in the expression. If there
9778 is, short-circuit the most common cases here. Note that we must
9779 not call expand_expr with anything but const0_rtx in case this
9780 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9782 if (ignore)
9784 if (! TREE_SIDE_EFFECTS (exp))
9785 return const0_rtx;
9787 /* Ensure we reference a volatile object even if value is ignored, but
9788 don't do this if all we are doing is taking its address. */
9789 if (TREE_THIS_VOLATILE (exp)
9790 && TREE_CODE (exp) != FUNCTION_DECL
9791 && mode != VOIDmode && mode != BLKmode
9792 && modifier != EXPAND_CONST_ADDRESS)
9794 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9795 if (MEM_P (temp))
9796 copy_to_reg (temp);
9797 return const0_rtx;
9800 if (TREE_CODE_CLASS (code) == tcc_unary
9801 || code == BIT_FIELD_REF
9802 || code == COMPONENT_REF
9803 || code == INDIRECT_REF)
9804 return expand_expr (treeop0, const0_rtx, VOIDmode,
9805 modifier);
9807 else if (TREE_CODE_CLASS (code) == tcc_binary
9808 || TREE_CODE_CLASS (code) == tcc_comparison
9809 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9811 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9812 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9813 return const0_rtx;
9816 target = 0;
9819 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9820 target = 0;
9822 /* Use subtarget as the target for operand 0 of a binary operation. */
9823 subtarget = get_subtarget (target);
9824 original_target = target;
9826 switch (code)
9828 case LABEL_DECL:
9830 tree function = decl_function_context (exp);
9832 temp = label_rtx (exp);
9833 temp = gen_rtx_LABEL_REF (Pmode, temp);
9835 if (function != current_function_decl
9836 && function != 0)
9837 LABEL_REF_NONLOCAL_P (temp) = 1;
9839 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9840 return temp;
9843 case SSA_NAME:
9844 /* ??? ivopts calls expander, without any preparation from
9845 out-of-ssa. So fake instructions as if this was an access to the
9846 base variable. This unnecessarily allocates a pseudo, see how we can
9847 reuse it, if partition base vars have it set already. */
9848 if (!currently_expanding_to_rtl)
9850 tree var = SSA_NAME_VAR (exp);
9851 if (var && DECL_RTL_SET_P (var))
9852 return DECL_RTL (var);
9853 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9854 LAST_VIRTUAL_REGISTER + 1);
9857 g = get_gimple_for_ssa_name (exp);
9858 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9859 if (g == NULL
9860 && modifier == EXPAND_INITIALIZER
9861 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9862 && (optimize || !SSA_NAME_VAR (exp)
9863 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9864 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9865 g = SSA_NAME_DEF_STMT (exp);
9866 if (g)
9868 rtx r;
9869 location_t saved_loc = curr_insn_location ();
9870 location_t loc = gimple_location (g);
9871 if (loc != UNKNOWN_LOCATION)
9872 set_curr_insn_location (loc);
9873 ops.code = gimple_assign_rhs_code (g);
9874 switch (get_gimple_rhs_class (ops.code))
9876 case GIMPLE_TERNARY_RHS:
9877 ops.op2 = gimple_assign_rhs3 (g);
9878 /* Fallthru */
9879 case GIMPLE_BINARY_RHS:
9880 ops.op1 = gimple_assign_rhs2 (g);
9882 /* Try to expand conditonal compare. */
9883 if (targetm.gen_ccmp_first)
9885 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9886 r = expand_ccmp_expr (g, mode);
9887 if (r)
9888 break;
9890 /* Fallthru */
9891 case GIMPLE_UNARY_RHS:
9892 ops.op0 = gimple_assign_rhs1 (g);
9893 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9894 ops.location = loc;
9895 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9896 break;
9897 case GIMPLE_SINGLE_RHS:
9899 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9900 tmode, modifier, alt_rtl,
9901 inner_reference_p);
9902 break;
9904 default:
9905 gcc_unreachable ();
9907 set_curr_insn_location (saved_loc);
9908 if (REG_P (r) && !REG_EXPR (r))
9909 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9910 return r;
9913 ssa_name = exp;
9914 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9915 exp = SSA_NAME_VAR (ssa_name);
9916 goto expand_decl_rtl;
9918 case PARM_DECL:
9919 case VAR_DECL:
9920 /* If a static var's type was incomplete when the decl was written,
9921 but the type is complete now, lay out the decl now. */
9922 if (DECL_SIZE (exp) == 0
9923 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9924 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9925 layout_decl (exp, 0);
9927 /* fall through */
9929 case FUNCTION_DECL:
9930 case RESULT_DECL:
9931 decl_rtl = DECL_RTL (exp);
9932 expand_decl_rtl:
9933 gcc_assert (decl_rtl);
9935 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9936 settings for VECTOR_TYPE_P that might switch for the function. */
9937 if (currently_expanding_to_rtl
9938 && code == VAR_DECL && MEM_P (decl_rtl)
9939 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9940 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9941 else
9942 decl_rtl = copy_rtx (decl_rtl);
9944 /* Record writes to register variables. */
9945 if (modifier == EXPAND_WRITE
9946 && REG_P (decl_rtl)
9947 && HARD_REGISTER_P (decl_rtl))
9948 add_to_hard_reg_set (&crtl->asm_clobbers,
9949 GET_MODE (decl_rtl), REGNO (decl_rtl));
9951 /* Ensure variable marked as used even if it doesn't go through
9952 a parser. If it hasn't be used yet, write out an external
9953 definition. */
9954 if (exp)
9955 TREE_USED (exp) = 1;
9957 /* Show we haven't gotten RTL for this yet. */
9958 temp = 0;
9960 /* Variables inherited from containing functions should have
9961 been lowered by this point. */
9962 if (exp)
9963 context = decl_function_context (exp);
9964 gcc_assert (!exp
9965 || SCOPE_FILE_SCOPE_P (context)
9966 || context == current_function_decl
9967 || TREE_STATIC (exp)
9968 || DECL_EXTERNAL (exp)
9969 /* ??? C++ creates functions that are not TREE_STATIC. */
9970 || TREE_CODE (exp) == FUNCTION_DECL);
9972 /* This is the case of an array whose size is to be determined
9973 from its initializer, while the initializer is still being parsed.
9974 ??? We aren't parsing while expanding anymore. */
9976 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9977 temp = validize_mem (decl_rtl);
9979 /* If DECL_RTL is memory, we are in the normal case and the
9980 address is not valid, get the address into a register. */
9982 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9984 if (alt_rtl)
9985 *alt_rtl = decl_rtl;
9986 decl_rtl = use_anchored_address (decl_rtl);
9987 if (modifier != EXPAND_CONST_ADDRESS
9988 && modifier != EXPAND_SUM
9989 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9990 : GET_MODE (decl_rtl),
9991 XEXP (decl_rtl, 0),
9992 MEM_ADDR_SPACE (decl_rtl)))
9993 temp = replace_equiv_address (decl_rtl,
9994 copy_rtx (XEXP (decl_rtl, 0)));
9997 /* If we got something, return it. But first, set the alignment
9998 if the address is a register. */
9999 if (temp != 0)
10001 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10002 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10004 return temp;
10007 if (exp)
10008 dmode = DECL_MODE (exp);
10009 else
10010 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10012 /* If the mode of DECL_RTL does not match that of the decl,
10013 there are two cases: we are dealing with a BLKmode value
10014 that is returned in a register, or we are dealing with
10015 a promoted value. In the latter case, return a SUBREG
10016 of the wanted mode, but mark it so that we know that it
10017 was already extended. */
10018 if (REG_P (decl_rtl)
10019 && dmode != BLKmode
10020 && GET_MODE (decl_rtl) != dmode)
10022 machine_mode pmode;
10024 /* Get the signedness to be used for this variable. Ensure we get
10025 the same mode we got when the variable was declared. */
10026 if (code != SSA_NAME)
10027 pmode = promote_decl_mode (exp, &unsignedp);
10028 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10029 && gimple_code (g) == GIMPLE_CALL
10030 && !gimple_call_internal_p (g))
10031 pmode = promote_function_mode (type, mode, &unsignedp,
10032 gimple_call_fntype (g),
10034 else
10035 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10036 gcc_assert (GET_MODE (decl_rtl) == pmode);
10038 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10039 SUBREG_PROMOTED_VAR_P (temp) = 1;
10040 SUBREG_PROMOTED_SET (temp, unsignedp);
10041 return temp;
10044 return decl_rtl;
10046 case INTEGER_CST:
10048 /* Given that TYPE_PRECISION (type) is not always equal to
10049 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10050 the former to the latter according to the signedness of the
10051 type. */
10052 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
10053 temp = immed_wide_int_const
10054 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
10055 return temp;
10058 case VECTOR_CST:
10060 tree tmp = NULL_TREE;
10061 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
10062 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
10063 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
10064 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
10065 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
10066 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
10067 return const_vector_from_tree (exp);
10068 scalar_int_mode int_mode;
10069 if (is_int_mode (mode, &int_mode))
10071 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
10072 return const_scalar_mask_from_tree (int_mode, exp);
10073 else
10075 tree type_for_mode
10076 = lang_hooks.types.type_for_mode (int_mode, 1);
10077 if (type_for_mode)
10078 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10079 type_for_mode, exp);
10082 if (!tmp)
10084 vec<constructor_elt, va_gc> *v;
10085 unsigned i;
10086 vec_alloc (v, VECTOR_CST_NELTS (exp));
10087 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
10088 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10089 tmp = build_constructor (type, v);
10091 return expand_expr (tmp, ignore ? const0_rtx : target,
10092 tmode, modifier);
10095 case CONST_DECL:
10096 if (modifier == EXPAND_WRITE)
10098 /* Writing into CONST_DECL is always invalid, but handle it
10099 gracefully. */
10100 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10101 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10102 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10103 EXPAND_NORMAL, as);
10104 op0 = memory_address_addr_space (mode, op0, as);
10105 temp = gen_rtx_MEM (mode, op0);
10106 set_mem_addr_space (temp, as);
10107 return temp;
10109 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10111 case REAL_CST:
10112 /* If optimized, generate immediate CONST_DOUBLE
10113 which will be turned into memory by reload if necessary.
10115 We used to force a register so that loop.c could see it. But
10116 this does not allow gen_* patterns to perform optimizations with
10117 the constants. It also produces two insns in cases like "x = 1.0;".
10118 On most machines, floating-point constants are not permitted in
10119 many insns, so we'd end up copying it to a register in any case.
10121 Now, we do the copying in expand_binop, if appropriate. */
10122 return const_double_from_real_value (TREE_REAL_CST (exp),
10123 TYPE_MODE (TREE_TYPE (exp)));
10125 case FIXED_CST:
10126 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10127 TYPE_MODE (TREE_TYPE (exp)));
10129 case COMPLEX_CST:
10130 /* Handle evaluating a complex constant in a CONCAT target. */
10131 if (original_target && GET_CODE (original_target) == CONCAT)
10133 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10134 rtx rtarg, itarg;
10136 rtarg = XEXP (original_target, 0);
10137 itarg = XEXP (original_target, 1);
10139 /* Move the real and imaginary parts separately. */
10140 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10141 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10143 if (op0 != rtarg)
10144 emit_move_insn (rtarg, op0);
10145 if (op1 != itarg)
10146 emit_move_insn (itarg, op1);
10148 return original_target;
10151 /* fall through */
10153 case STRING_CST:
10154 temp = expand_expr_constant (exp, 1, modifier);
10156 /* temp contains a constant address.
10157 On RISC machines where a constant address isn't valid,
10158 make some insns to get that address into a register. */
10159 if (modifier != EXPAND_CONST_ADDRESS
10160 && modifier != EXPAND_INITIALIZER
10161 && modifier != EXPAND_SUM
10162 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10163 MEM_ADDR_SPACE (temp)))
10164 return replace_equiv_address (temp,
10165 copy_rtx (XEXP (temp, 0)));
10166 return temp;
10168 case POLY_INT_CST:
10169 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10171 case SAVE_EXPR:
10173 tree val = treeop0;
10174 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10175 inner_reference_p);
10177 if (!SAVE_EXPR_RESOLVED_P (exp))
10179 /* We can indeed still hit this case, typically via builtin
10180 expanders calling save_expr immediately before expanding
10181 something. Assume this means that we only have to deal
10182 with non-BLKmode values. */
10183 gcc_assert (GET_MODE (ret) != BLKmode);
10185 val = build_decl (curr_insn_location (),
10186 VAR_DECL, NULL, TREE_TYPE (exp));
10187 DECL_ARTIFICIAL (val) = 1;
10188 DECL_IGNORED_P (val) = 1;
10189 treeop0 = val;
10190 TREE_OPERAND (exp, 0) = treeop0;
10191 SAVE_EXPR_RESOLVED_P (exp) = 1;
10193 if (!CONSTANT_P (ret))
10194 ret = copy_to_reg (ret);
10195 SET_DECL_RTL (val, ret);
10198 return ret;
10202 case CONSTRUCTOR:
10203 /* If we don't need the result, just ensure we evaluate any
10204 subexpressions. */
10205 if (ignore)
10207 unsigned HOST_WIDE_INT idx;
10208 tree value;
10210 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10211 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10213 return const0_rtx;
10216 return expand_constructor (exp, target, modifier, false);
10218 case TARGET_MEM_REF:
10220 addr_space_t as
10221 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10222 enum insn_code icode;
10223 unsigned int align;
10225 op0 = addr_for_mem_ref (exp, as, true);
10226 op0 = memory_address_addr_space (mode, op0, as);
10227 temp = gen_rtx_MEM (mode, op0);
10228 set_mem_attributes (temp, exp, 0);
10229 set_mem_addr_space (temp, as);
10230 align = get_object_alignment (exp);
10231 if (modifier != EXPAND_WRITE
10232 && modifier != EXPAND_MEMORY
10233 && mode != BLKmode
10234 && align < GET_MODE_ALIGNMENT (mode)
10235 /* If the target does not have special handling for unaligned
10236 loads of mode then it can use regular moves for them. */
10237 && ((icode = optab_handler (movmisalign_optab, mode))
10238 != CODE_FOR_nothing))
10240 struct expand_operand ops[2];
10242 /* We've already validated the memory, and we're creating a
10243 new pseudo destination. The predicates really can't fail,
10244 nor can the generator. */
10245 create_output_operand (&ops[0], NULL_RTX, mode);
10246 create_fixed_operand (&ops[1], temp);
10247 expand_insn (icode, 2, ops);
10248 temp = ops[0].value;
10250 return temp;
10253 case MEM_REF:
10255 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10256 addr_space_t as
10257 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10258 machine_mode address_mode;
10259 tree base = TREE_OPERAND (exp, 0);
10260 gimple *def_stmt;
10261 enum insn_code icode;
10262 unsigned align;
10263 /* Handle expansion of non-aliased memory with non-BLKmode. That
10264 might end up in a register. */
10265 if (mem_ref_refers_to_non_mem_p (exp))
10267 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10268 base = TREE_OPERAND (base, 0);
10269 if (known_eq (offset, 0)
10270 && !reverse
10271 && tree_fits_uhwi_p (TYPE_SIZE (type))
10272 && (GET_MODE_BITSIZE (DECL_MODE (base))
10273 == tree_to_uhwi (TYPE_SIZE (type))))
10274 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10275 target, tmode, modifier);
10276 if (TYPE_MODE (type) == BLKmode)
10278 temp = assign_stack_temp (DECL_MODE (base),
10279 GET_MODE_SIZE (DECL_MODE (base)));
10280 store_expr (base, temp, 0, false, false);
10281 temp = adjust_address (temp, BLKmode, offset);
10282 set_mem_size (temp, int_size_in_bytes (type));
10283 return temp;
10285 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10286 bitsize_int (offset * BITS_PER_UNIT));
10287 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10288 return expand_expr (exp, target, tmode, modifier);
10290 address_mode = targetm.addr_space.address_mode (as);
10291 base = TREE_OPERAND (exp, 0);
10292 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10294 tree mask = gimple_assign_rhs2 (def_stmt);
10295 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10296 gimple_assign_rhs1 (def_stmt), mask);
10297 TREE_OPERAND (exp, 0) = base;
10299 align = get_object_alignment (exp);
10300 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10301 op0 = memory_address_addr_space (mode, op0, as);
10302 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10304 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10305 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10306 op0 = memory_address_addr_space (mode, op0, as);
10308 temp = gen_rtx_MEM (mode, op0);
10309 set_mem_attributes (temp, exp, 0);
10310 set_mem_addr_space (temp, as);
10311 if (TREE_THIS_VOLATILE (exp))
10312 MEM_VOLATILE_P (temp) = 1;
10313 if (modifier != EXPAND_WRITE
10314 && modifier != EXPAND_MEMORY
10315 && !inner_reference_p
10316 && mode != BLKmode
10317 && align < GET_MODE_ALIGNMENT (mode))
10319 if ((icode = optab_handler (movmisalign_optab, mode))
10320 != CODE_FOR_nothing)
10322 struct expand_operand ops[2];
10324 /* We've already validated the memory, and we're creating a
10325 new pseudo destination. The predicates really can't fail,
10326 nor can the generator. */
10327 create_output_operand (&ops[0], NULL_RTX, mode);
10328 create_fixed_operand (&ops[1], temp);
10329 expand_insn (icode, 2, ops);
10330 temp = ops[0].value;
10332 else if (targetm.slow_unaligned_access (mode, align))
10333 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10334 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10335 (modifier == EXPAND_STACK_PARM
10336 ? NULL_RTX : target),
10337 mode, mode, false, alt_rtl);
10339 if (reverse
10340 && modifier != EXPAND_MEMORY
10341 && modifier != EXPAND_WRITE)
10342 temp = flip_storage_order (mode, temp);
10343 return temp;
10346 case ARRAY_REF:
10349 tree array = treeop0;
10350 tree index = treeop1;
10351 tree init;
10353 /* Fold an expression like: "foo"[2].
10354 This is not done in fold so it won't happen inside &.
10355 Don't fold if this is for wide characters since it's too
10356 difficult to do correctly and this is a very rare case. */
10358 if (modifier != EXPAND_CONST_ADDRESS
10359 && modifier != EXPAND_INITIALIZER
10360 && modifier != EXPAND_MEMORY)
10362 tree t = fold_read_from_constant_string (exp);
10364 if (t)
10365 return expand_expr (t, target, tmode, modifier);
10368 /* If this is a constant index into a constant array,
10369 just get the value from the array. Handle both the cases when
10370 we have an explicit constructor and when our operand is a variable
10371 that was declared const. */
10373 if (modifier != EXPAND_CONST_ADDRESS
10374 && modifier != EXPAND_INITIALIZER
10375 && modifier != EXPAND_MEMORY
10376 && TREE_CODE (array) == CONSTRUCTOR
10377 && ! TREE_SIDE_EFFECTS (array)
10378 && TREE_CODE (index) == INTEGER_CST)
10380 unsigned HOST_WIDE_INT ix;
10381 tree field, value;
10383 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10384 field, value)
10385 if (tree_int_cst_equal (field, index))
10387 if (!TREE_SIDE_EFFECTS (value))
10388 return expand_expr (fold (value), target, tmode, modifier);
10389 break;
10393 else if (optimize >= 1
10394 && modifier != EXPAND_CONST_ADDRESS
10395 && modifier != EXPAND_INITIALIZER
10396 && modifier != EXPAND_MEMORY
10397 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10398 && TREE_CODE (index) == INTEGER_CST
10399 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10400 && (init = ctor_for_folding (array)) != error_mark_node)
10402 if (init == NULL_TREE)
10404 tree value = build_zero_cst (type);
10405 if (TREE_CODE (value) == CONSTRUCTOR)
10407 /* If VALUE is a CONSTRUCTOR, this optimization is only
10408 useful if this doesn't store the CONSTRUCTOR into
10409 memory. If it does, it is more efficient to just
10410 load the data from the array directly. */
10411 rtx ret = expand_constructor (value, target,
10412 modifier, true);
10413 if (ret == NULL_RTX)
10414 value = NULL_TREE;
10417 if (value)
10418 return expand_expr (value, target, tmode, modifier);
10420 else if (TREE_CODE (init) == CONSTRUCTOR)
10422 unsigned HOST_WIDE_INT ix;
10423 tree field, value;
10425 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10426 field, value)
10427 if (tree_int_cst_equal (field, index))
10429 if (TREE_SIDE_EFFECTS (value))
10430 break;
10432 if (TREE_CODE (value) == CONSTRUCTOR)
10434 /* If VALUE is a CONSTRUCTOR, this
10435 optimization is only useful if
10436 this doesn't store the CONSTRUCTOR
10437 into memory. If it does, it is more
10438 efficient to just load the data from
10439 the array directly. */
10440 rtx ret = expand_constructor (value, target,
10441 modifier, true);
10442 if (ret == NULL_RTX)
10443 break;
10446 return
10447 expand_expr (fold (value), target, tmode, modifier);
10450 else if (TREE_CODE (init) == STRING_CST)
10452 tree low_bound = array_ref_low_bound (exp);
10453 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10455 /* Optimize the special case of a zero lower bound.
10457 We convert the lower bound to sizetype to avoid problems
10458 with constant folding. E.g. suppose the lower bound is
10459 1 and its mode is QI. Without the conversion
10460 (ARRAY + (INDEX - (unsigned char)1))
10461 becomes
10462 (ARRAY + (-(unsigned char)1) + INDEX)
10463 which becomes
10464 (ARRAY + 255 + INDEX). Oops! */
10465 if (!integer_zerop (low_bound))
10466 index1 = size_diffop_loc (loc, index1,
10467 fold_convert_loc (loc, sizetype,
10468 low_bound));
10470 if (tree_fits_uhwi_p (index1)
10471 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10473 tree type = TREE_TYPE (TREE_TYPE (init));
10474 scalar_int_mode mode;
10476 if (is_int_mode (TYPE_MODE (type), &mode)
10477 && GET_MODE_SIZE (mode) == 1)
10478 return gen_int_mode (TREE_STRING_POINTER (init)
10479 [TREE_INT_CST_LOW (index1)],
10480 mode);
10485 goto normal_inner_ref;
10487 case COMPONENT_REF:
10488 /* If the operand is a CONSTRUCTOR, we can just extract the
10489 appropriate field if it is present. */
10490 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10492 unsigned HOST_WIDE_INT idx;
10493 tree field, value;
10494 scalar_int_mode field_mode;
10496 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10497 idx, field, value)
10498 if (field == treeop1
10499 /* We can normally use the value of the field in the
10500 CONSTRUCTOR. However, if this is a bitfield in
10501 an integral mode that we can fit in a HOST_WIDE_INT,
10502 we must mask only the number of bits in the bitfield,
10503 since this is done implicitly by the constructor. If
10504 the bitfield does not meet either of those conditions,
10505 we can't do this optimization. */
10506 && (! DECL_BIT_FIELD (field)
10507 || (is_int_mode (DECL_MODE (field), &field_mode)
10508 && (GET_MODE_PRECISION (field_mode)
10509 <= HOST_BITS_PER_WIDE_INT))))
10511 if (DECL_BIT_FIELD (field)
10512 && modifier == EXPAND_STACK_PARM)
10513 target = 0;
10514 op0 = expand_expr (value, target, tmode, modifier);
10515 if (DECL_BIT_FIELD (field))
10517 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10518 scalar_int_mode imode
10519 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10521 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10523 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10524 imode);
10525 op0 = expand_and (imode, op0, op1, target);
10527 else
10529 int count = GET_MODE_PRECISION (imode) - bitsize;
10531 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10532 target, 0);
10533 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10534 target, 0);
10538 return op0;
10541 goto normal_inner_ref;
10543 case BIT_FIELD_REF:
10544 case ARRAY_RANGE_REF:
10545 normal_inner_ref:
10547 machine_mode mode1, mode2;
10548 poly_int64 bitsize, bitpos, bytepos;
10549 tree offset;
10550 int reversep, volatilep = 0, must_force_mem;
10551 tree tem
10552 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10553 &unsignedp, &reversep, &volatilep);
10554 rtx orig_op0, memloc;
10555 bool clear_mem_expr = false;
10557 /* If we got back the original object, something is wrong. Perhaps
10558 we are evaluating an expression too early. In any event, don't
10559 infinitely recurse. */
10560 gcc_assert (tem != exp);
10562 /* If TEM's type is a union of variable size, pass TARGET to the inner
10563 computation, since it will need a temporary and TARGET is known
10564 to have to do. This occurs in unchecked conversion in Ada. */
10565 orig_op0 = op0
10566 = expand_expr_real (tem,
10567 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10568 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10569 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10570 != INTEGER_CST)
10571 && modifier != EXPAND_STACK_PARM
10572 ? target : NULL_RTX),
10573 VOIDmode,
10574 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10575 NULL, true);
10577 /* If the field has a mode, we want to access it in the
10578 field's mode, not the computed mode.
10579 If a MEM has VOIDmode (external with incomplete type),
10580 use BLKmode for it instead. */
10581 if (MEM_P (op0))
10583 if (mode1 != VOIDmode)
10584 op0 = adjust_address (op0, mode1, 0);
10585 else if (GET_MODE (op0) == VOIDmode)
10586 op0 = adjust_address (op0, BLKmode, 0);
10589 mode2
10590 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10592 /* If we have either an offset, a BLKmode result, or a reference
10593 outside the underlying object, we must force it to memory.
10594 Such a case can occur in Ada if we have unchecked conversion
10595 of an expression from a scalar type to an aggregate type or
10596 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10597 passed a partially uninitialized object or a view-conversion
10598 to a larger size. */
10599 must_force_mem = (offset
10600 || mode1 == BLKmode
10601 || maybe_gt (bitpos + bitsize,
10602 GET_MODE_BITSIZE (mode2)));
10604 /* Handle CONCAT first. */
10605 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10607 if (known_eq (bitpos, 0)
10608 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
10609 && COMPLEX_MODE_P (mode1)
10610 && COMPLEX_MODE_P (GET_MODE (op0))
10611 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10612 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10614 if (reversep)
10615 op0 = flip_storage_order (GET_MODE (op0), op0);
10616 if (mode1 != GET_MODE (op0))
10618 rtx parts[2];
10619 for (int i = 0; i < 2; i++)
10621 rtx op = read_complex_part (op0, i != 0);
10622 if (GET_CODE (op) == SUBREG)
10623 op = force_reg (GET_MODE (op), op);
10624 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10625 op);
10626 if (temp)
10627 op = temp;
10628 else
10630 if (!REG_P (op) && !MEM_P (op))
10631 op = force_reg (GET_MODE (op), op);
10632 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10634 parts[i] = op;
10636 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10638 return op0;
10640 if (known_eq (bitpos, 0)
10641 && known_eq (bitsize,
10642 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10643 && maybe_ne (bitsize, 0))
10645 op0 = XEXP (op0, 0);
10646 mode2 = GET_MODE (op0);
10648 else if (known_eq (bitpos,
10649 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10650 && known_eq (bitsize,
10651 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
10652 && maybe_ne (bitpos, 0)
10653 && maybe_ne (bitsize, 0))
10655 op0 = XEXP (op0, 1);
10656 bitpos = 0;
10657 mode2 = GET_MODE (op0);
10659 else
10660 /* Otherwise force into memory. */
10661 must_force_mem = 1;
10664 /* If this is a constant, put it in a register if it is a legitimate
10665 constant and we don't need a memory reference. */
10666 if (CONSTANT_P (op0)
10667 && mode2 != BLKmode
10668 && targetm.legitimate_constant_p (mode2, op0)
10669 && !must_force_mem)
10670 op0 = force_reg (mode2, op0);
10672 /* Otherwise, if this is a constant, try to force it to the constant
10673 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10674 is a legitimate constant. */
10675 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10676 op0 = validize_mem (memloc);
10678 /* Otherwise, if this is a constant or the object is not in memory
10679 and need be, put it there. */
10680 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10682 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10683 emit_move_insn (memloc, op0);
10684 op0 = memloc;
10685 clear_mem_expr = true;
10688 if (offset)
10690 machine_mode address_mode;
10691 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10692 EXPAND_SUM);
10694 gcc_assert (MEM_P (op0));
10696 address_mode = get_address_mode (op0);
10697 if (GET_MODE (offset_rtx) != address_mode)
10699 /* We cannot be sure that the RTL in offset_rtx is valid outside
10700 of a memory address context, so force it into a register
10701 before attempting to convert it to the desired mode. */
10702 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10703 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10706 /* See the comment in expand_assignment for the rationale. */
10707 if (mode1 != VOIDmode
10708 && maybe_ne (bitpos, 0)
10709 && maybe_gt (bitsize, 0)
10710 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10711 && multiple_p (bitpos, bitsize)
10712 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
10713 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10715 op0 = adjust_address (op0, mode1, bytepos);
10716 bitpos = 0;
10719 op0 = offset_address (op0, offset_rtx,
10720 highest_pow2_factor (offset));
10723 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10724 record its alignment as BIGGEST_ALIGNMENT. */
10725 if (MEM_P (op0)
10726 && known_eq (bitpos, 0)
10727 && offset != 0
10728 && is_aligning_offset (offset, tem))
10729 set_mem_align (op0, BIGGEST_ALIGNMENT);
10731 /* Don't forget about volatility even if this is a bitfield. */
10732 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10734 if (op0 == orig_op0)
10735 op0 = copy_rtx (op0);
10737 MEM_VOLATILE_P (op0) = 1;
10740 /* In cases where an aligned union has an unaligned object
10741 as a field, we might be extracting a BLKmode value from
10742 an integer-mode (e.g., SImode) object. Handle this case
10743 by doing the extract into an object as wide as the field
10744 (which we know to be the width of a basic mode), then
10745 storing into memory, and changing the mode to BLKmode. */
10746 if (mode1 == VOIDmode
10747 || REG_P (op0) || GET_CODE (op0) == SUBREG
10748 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10749 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10750 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10751 && modifier != EXPAND_CONST_ADDRESS
10752 && modifier != EXPAND_INITIALIZER
10753 && modifier != EXPAND_MEMORY)
10754 /* If the bitfield is volatile and the bitsize
10755 is narrower than the access size of the bitfield,
10756 we need to extract bitfields from the access. */
10757 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10758 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10759 && mode1 != BLKmode
10760 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
10761 /* If the field isn't aligned enough to fetch as a memref,
10762 fetch it as a bit field. */
10763 || (mode1 != BLKmode
10764 && (((MEM_P (op0)
10765 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10766 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
10767 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10768 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
10769 && modifier != EXPAND_MEMORY
10770 && ((modifier == EXPAND_CONST_ADDRESS
10771 || modifier == EXPAND_INITIALIZER)
10772 ? STRICT_ALIGNMENT
10773 : targetm.slow_unaligned_access (mode1,
10774 MEM_ALIGN (op0))))
10775 || !multiple_p (bitpos, BITS_PER_UNIT)))
10776 /* If the type and the field are a constant size and the
10777 size of the type isn't the same size as the bitfield,
10778 we must use bitfield operations. */
10779 || (known_size_p (bitsize)
10780 && TYPE_SIZE (TREE_TYPE (exp))
10781 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
10782 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
10783 bitsize)))
10785 machine_mode ext_mode = mode;
10787 if (ext_mode == BLKmode
10788 && ! (target != 0 && MEM_P (op0)
10789 && MEM_P (target)
10790 && multiple_p (bitpos, BITS_PER_UNIT)))
10791 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10793 if (ext_mode == BLKmode)
10795 if (target == 0)
10796 target = assign_temp (type, 1, 1);
10798 /* ??? Unlike the similar test a few lines below, this one is
10799 very likely obsolete. */
10800 if (known_eq (bitsize, 0))
10801 return target;
10803 /* In this case, BITPOS must start at a byte boundary and
10804 TARGET, if specified, must be a MEM. */
10805 gcc_assert (MEM_P (op0)
10806 && (!target || MEM_P (target)));
10808 bytepos = exact_div (bitpos, BITS_PER_UNIT);
10809 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
10810 emit_block_move (target,
10811 adjust_address (op0, VOIDmode, bytepos),
10812 gen_int_mode (bytesize, Pmode),
10813 (modifier == EXPAND_STACK_PARM
10814 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10816 return target;
10819 /* If we have nothing to extract, the result will be 0 for targets
10820 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10821 return 0 for the sake of consistency, as reading a zero-sized
10822 bitfield is valid in Ada and the value is fully specified. */
10823 if (known_eq (bitsize, 0))
10824 return const0_rtx;
10826 op0 = validize_mem (op0);
10828 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10829 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10831 /* If the result has a record type and the extraction is done in
10832 an integral mode, then the field may be not aligned on a byte
10833 boundary; in this case, if it has reverse storage order, it
10834 needs to be extracted as a scalar field with reverse storage
10835 order and put back into memory order afterwards. */
10836 if (TREE_CODE (type) == RECORD_TYPE
10837 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10838 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10840 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10841 (modifier == EXPAND_STACK_PARM
10842 ? NULL_RTX : target),
10843 ext_mode, ext_mode, reversep, alt_rtl);
10845 /* If the result has a record type and the mode of OP0 is an
10846 integral mode then, if BITSIZE is narrower than this mode
10847 and this is for big-endian data, we must put the field
10848 into the high-order bits. And we must also put it back
10849 into memory order if it has been previously reversed. */
10850 scalar_int_mode op0_mode;
10851 if (TREE_CODE (type) == RECORD_TYPE
10852 && is_int_mode (GET_MODE (op0), &op0_mode))
10854 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10856 gcc_checking_assert (known_le (bitsize, size));
10857 if (maybe_lt (bitsize, size)
10858 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10859 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10860 size - bitsize, op0, 1);
10862 if (reversep)
10863 op0 = flip_storage_order (op0_mode, op0);
10866 /* If the result type is BLKmode, store the data into a temporary
10867 of the appropriate type, but with the mode corresponding to the
10868 mode for the data we have (op0's mode). */
10869 if (mode == BLKmode)
10871 rtx new_rtx
10872 = assign_stack_temp_for_type (ext_mode,
10873 GET_MODE_BITSIZE (ext_mode),
10874 type);
10875 emit_move_insn (new_rtx, op0);
10876 op0 = copy_rtx (new_rtx);
10877 PUT_MODE (op0, BLKmode);
10880 return op0;
10883 /* If the result is BLKmode, use that to access the object
10884 now as well. */
10885 if (mode == BLKmode)
10886 mode1 = BLKmode;
10888 /* Get a reference to just this component. */
10889 bytepos = bits_to_bytes_round_down (bitpos);
10890 if (modifier == EXPAND_CONST_ADDRESS
10891 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10892 op0 = adjust_address_nv (op0, mode1, bytepos);
10893 else
10894 op0 = adjust_address (op0, mode1, bytepos);
10896 if (op0 == orig_op0)
10897 op0 = copy_rtx (op0);
10899 /* Don't set memory attributes if the base expression is
10900 SSA_NAME that got expanded as a MEM. In that case, we should
10901 just honor its original memory attributes. */
10902 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10903 set_mem_attributes (op0, exp, 0);
10905 if (REG_P (XEXP (op0, 0)))
10906 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10908 /* If op0 is a temporary because the original expressions was forced
10909 to memory, clear MEM_EXPR so that the original expression cannot
10910 be marked as addressable through MEM_EXPR of the temporary. */
10911 if (clear_mem_expr)
10912 set_mem_expr (op0, NULL_TREE);
10914 MEM_VOLATILE_P (op0) |= volatilep;
10916 if (reversep
10917 && modifier != EXPAND_MEMORY
10918 && modifier != EXPAND_WRITE)
10919 op0 = flip_storage_order (mode1, op0);
10921 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10922 || modifier == EXPAND_CONST_ADDRESS
10923 || modifier == EXPAND_INITIALIZER)
10924 return op0;
10926 if (target == 0)
10927 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10929 convert_move (target, op0, unsignedp);
10930 return target;
10933 case OBJ_TYPE_REF:
10934 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10936 case CALL_EXPR:
10937 /* All valid uses of __builtin_va_arg_pack () are removed during
10938 inlining. */
10939 if (CALL_EXPR_VA_ARG_PACK (exp))
10940 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10942 tree fndecl = get_callee_fndecl (exp), attr;
10944 if (fndecl
10945 && (attr = lookup_attribute ("error",
10946 DECL_ATTRIBUTES (fndecl))) != NULL)
10947 error ("%Kcall to %qs declared with attribute error: %s",
10948 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10949 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10950 if (fndecl
10951 && (attr = lookup_attribute ("warning",
10952 DECL_ATTRIBUTES (fndecl))) != NULL)
10953 warning_at (tree_nonartificial_location (exp),
10954 0, "%Kcall to %qs declared with attribute warning: %s",
10955 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10956 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10958 /* Check for a built-in function. */
10959 if (fndecl && DECL_BUILT_IN (fndecl))
10961 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10962 if (CALL_WITH_BOUNDS_P (exp))
10963 return expand_builtin_with_bounds (exp, target, subtarget,
10964 tmode, ignore);
10965 else
10966 return expand_builtin (exp, target, subtarget, tmode, ignore);
10969 return expand_call (exp, target, ignore);
10971 case VIEW_CONVERT_EXPR:
10972 op0 = NULL_RTX;
10974 /* If we are converting to BLKmode, try to avoid an intermediate
10975 temporary by fetching an inner memory reference. */
10976 if (mode == BLKmode
10977 && poly_int_tree_p (TYPE_SIZE (type))
10978 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10979 && handled_component_p (treeop0))
10981 machine_mode mode1;
10982 poly_int64 bitsize, bitpos, bytepos;
10983 tree offset;
10984 int unsignedp, reversep, volatilep = 0;
10985 tree tem
10986 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10987 &unsignedp, &reversep, &volatilep);
10988 rtx orig_op0;
10990 /* ??? We should work harder and deal with non-zero offsets. */
10991 if (!offset
10992 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10993 && !reversep
10994 && known_size_p (bitsize)
10995 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
10997 /* See the normal_inner_ref case for the rationale. */
10998 orig_op0
10999 = expand_expr_real (tem,
11000 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11001 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11002 != INTEGER_CST)
11003 && modifier != EXPAND_STACK_PARM
11004 ? target : NULL_RTX),
11005 VOIDmode,
11006 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11007 NULL, true);
11009 if (MEM_P (orig_op0))
11011 op0 = orig_op0;
11013 /* Get a reference to just this component. */
11014 if (modifier == EXPAND_CONST_ADDRESS
11015 || modifier == EXPAND_SUM
11016 || modifier == EXPAND_INITIALIZER)
11017 op0 = adjust_address_nv (op0, mode, bytepos);
11018 else
11019 op0 = adjust_address (op0, mode, bytepos);
11021 if (op0 == orig_op0)
11022 op0 = copy_rtx (op0);
11024 set_mem_attributes (op0, treeop0, 0);
11025 if (REG_P (XEXP (op0, 0)))
11026 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11028 MEM_VOLATILE_P (op0) |= volatilep;
11033 if (!op0)
11034 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11035 NULL, inner_reference_p);
11037 /* If the input and output modes are both the same, we are done. */
11038 if (mode == GET_MODE (op0))
11040 /* If neither mode is BLKmode, and both modes are the same size
11041 then we can use gen_lowpart. */
11042 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
11043 && (GET_MODE_PRECISION (mode)
11044 == GET_MODE_PRECISION (GET_MODE (op0)))
11045 && !COMPLEX_MODE_P (GET_MODE (op0)))
11047 if (GET_CODE (op0) == SUBREG)
11048 op0 = force_reg (GET_MODE (op0), op0);
11049 temp = gen_lowpart_common (mode, op0);
11050 if (temp)
11051 op0 = temp;
11052 else
11054 if (!REG_P (op0) && !MEM_P (op0))
11055 op0 = force_reg (GET_MODE (op0), op0);
11056 op0 = gen_lowpart (mode, op0);
11059 /* If both types are integral, convert from one mode to the other. */
11060 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11061 op0 = convert_modes (mode, GET_MODE (op0), op0,
11062 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11063 /* If the output type is a bit-field type, do an extraction. */
11064 else if (reduce_bit_field)
11065 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11066 TYPE_UNSIGNED (type), NULL_RTX,
11067 mode, mode, false, NULL);
11068 /* As a last resort, spill op0 to memory, and reload it in a
11069 different mode. */
11070 else if (!MEM_P (op0))
11072 /* If the operand is not a MEM, force it into memory. Since we
11073 are going to be changing the mode of the MEM, don't call
11074 force_const_mem for constants because we don't allow pool
11075 constants to change mode. */
11076 tree inner_type = TREE_TYPE (treeop0);
11078 gcc_assert (!TREE_ADDRESSABLE (exp));
11080 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11081 target
11082 = assign_stack_temp_for_type
11083 (TYPE_MODE (inner_type),
11084 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11086 emit_move_insn (target, op0);
11087 op0 = target;
11090 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11091 output type is such that the operand is known to be aligned, indicate
11092 that it is. Otherwise, we need only be concerned about alignment for
11093 non-BLKmode results. */
11094 if (MEM_P (op0))
11096 enum insn_code icode;
11098 if (modifier != EXPAND_WRITE
11099 && modifier != EXPAND_MEMORY
11100 && !inner_reference_p
11101 && mode != BLKmode
11102 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11104 /* If the target does have special handling for unaligned
11105 loads of mode then use them. */
11106 if ((icode = optab_handler (movmisalign_optab, mode))
11107 != CODE_FOR_nothing)
11109 rtx reg;
11111 op0 = adjust_address (op0, mode, 0);
11112 /* We've already validated the memory, and we're creating a
11113 new pseudo destination. The predicates really can't
11114 fail. */
11115 reg = gen_reg_rtx (mode);
11117 /* Nor can the insn generator. */
11118 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11119 emit_insn (insn);
11120 return reg;
11122 else if (STRICT_ALIGNMENT)
11124 tree inner_type = TREE_TYPE (treeop0);
11125 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11126 poly_uint64 op0_size
11127 = tree_to_poly_uint64 (TYPE_SIZE_UNIT (inner_type));
11128 poly_int64 temp_size = upper_bound (op0_size, mode_size);
11129 rtx new_rtx
11130 = assign_stack_temp_for_type (mode, temp_size, type);
11131 rtx new_with_op0_mode
11132 = adjust_address (new_rtx, GET_MODE (op0), 0);
11134 gcc_assert (!TREE_ADDRESSABLE (exp));
11136 if (GET_MODE (op0) == BLKmode)
11137 emit_block_move (new_with_op0_mode, op0,
11138 GEN_INT (GET_MODE_SIZE (mode)),
11139 (modifier == EXPAND_STACK_PARM
11140 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11141 else
11142 emit_move_insn (new_with_op0_mode, op0);
11144 op0 = new_rtx;
11148 op0 = adjust_address (op0, mode, 0);
11151 return op0;
11153 case MODIFY_EXPR:
11155 tree lhs = treeop0;
11156 tree rhs = treeop1;
11157 gcc_assert (ignore);
11159 /* Check for |= or &= of a bitfield of size one into another bitfield
11160 of size 1. In this case, (unless we need the result of the
11161 assignment) we can do this more efficiently with a
11162 test followed by an assignment, if necessary.
11164 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11165 things change so we do, this code should be enhanced to
11166 support it. */
11167 if (TREE_CODE (lhs) == COMPONENT_REF
11168 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11169 || TREE_CODE (rhs) == BIT_AND_EXPR)
11170 && TREE_OPERAND (rhs, 0) == lhs
11171 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11172 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11173 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11175 rtx_code_label *label = gen_label_rtx ();
11176 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11177 do_jump (TREE_OPERAND (rhs, 1),
11178 value ? label : 0,
11179 value ? 0 : label,
11180 profile_probability::uninitialized ());
11181 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11182 false);
11183 do_pending_stack_adjust ();
11184 emit_label (label);
11185 return const0_rtx;
11188 expand_assignment (lhs, rhs, false);
11189 return const0_rtx;
11192 case ADDR_EXPR:
11193 return expand_expr_addr_expr (exp, target, tmode, modifier);
11195 case REALPART_EXPR:
11196 op0 = expand_normal (treeop0);
11197 return read_complex_part (op0, false);
11199 case IMAGPART_EXPR:
11200 op0 = expand_normal (treeop0);
11201 return read_complex_part (op0, true);
11203 case RETURN_EXPR:
11204 case LABEL_EXPR:
11205 case GOTO_EXPR:
11206 case SWITCH_EXPR:
11207 case ASM_EXPR:
11208 /* Expanded in cfgexpand.c. */
11209 gcc_unreachable ();
11211 case TRY_CATCH_EXPR:
11212 case CATCH_EXPR:
11213 case EH_FILTER_EXPR:
11214 case TRY_FINALLY_EXPR:
11215 /* Lowered by tree-eh.c. */
11216 gcc_unreachable ();
11218 case WITH_CLEANUP_EXPR:
11219 case CLEANUP_POINT_EXPR:
11220 case TARGET_EXPR:
11221 case CASE_LABEL_EXPR:
11222 case VA_ARG_EXPR:
11223 case BIND_EXPR:
11224 case INIT_EXPR:
11225 case CONJ_EXPR:
11226 case COMPOUND_EXPR:
11227 case PREINCREMENT_EXPR:
11228 case PREDECREMENT_EXPR:
11229 case POSTINCREMENT_EXPR:
11230 case POSTDECREMENT_EXPR:
11231 case LOOP_EXPR:
11232 case EXIT_EXPR:
11233 case COMPOUND_LITERAL_EXPR:
11234 /* Lowered by gimplify.c. */
11235 gcc_unreachable ();
11237 case FDESC_EXPR:
11238 /* Function descriptors are not valid except for as
11239 initialization constants, and should not be expanded. */
11240 gcc_unreachable ();
11242 case WITH_SIZE_EXPR:
11243 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11244 have pulled out the size to use in whatever context it needed. */
11245 return expand_expr_real (treeop0, original_target, tmode,
11246 modifier, alt_rtl, inner_reference_p);
11248 default:
11249 return expand_expr_real_2 (&ops, target, tmode, modifier);
11253 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11254 signedness of TYPE), possibly returning the result in TARGET.
11255 TYPE is known to be a partial integer type. */
11256 static rtx
11257 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11259 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11260 if (target && GET_MODE (target) != GET_MODE (exp))
11261 target = 0;
11262 /* For constant values, reduce using build_int_cst_type. */
11263 if (CONST_INT_P (exp))
11265 HOST_WIDE_INT value = INTVAL (exp);
11266 tree t = build_int_cst_type (type, value);
11267 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11269 else if (TYPE_UNSIGNED (type))
11271 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11272 rtx mask = immed_wide_int_const
11273 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11274 return expand_and (mode, exp, mask, target);
11276 else
11278 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11279 int count = GET_MODE_PRECISION (mode) - prec;
11280 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11281 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11285 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11286 when applied to the address of EXP produces an address known to be
11287 aligned more than BIGGEST_ALIGNMENT. */
11289 static int
11290 is_aligning_offset (const_tree offset, const_tree exp)
11292 /* Strip off any conversions. */
11293 while (CONVERT_EXPR_P (offset))
11294 offset = TREE_OPERAND (offset, 0);
11296 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11297 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11298 if (TREE_CODE (offset) != BIT_AND_EXPR
11299 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11300 || compare_tree_int (TREE_OPERAND (offset, 1),
11301 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11302 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11303 return 0;
11305 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11306 It must be NEGATE_EXPR. Then strip any more conversions. */
11307 offset = TREE_OPERAND (offset, 0);
11308 while (CONVERT_EXPR_P (offset))
11309 offset = TREE_OPERAND (offset, 0);
11311 if (TREE_CODE (offset) != NEGATE_EXPR)
11312 return 0;
11314 offset = TREE_OPERAND (offset, 0);
11315 while (CONVERT_EXPR_P (offset))
11316 offset = TREE_OPERAND (offset, 0);
11318 /* This must now be the address of EXP. */
11319 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11322 /* Return the tree node if an ARG corresponds to a string constant or zero
11323 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11324 in bytes within the string that ARG is accessing. The type of the
11325 offset will be `sizetype'. */
11327 tree
11328 string_constant (tree arg, tree *ptr_offset)
11330 tree array, offset, lower_bound;
11331 STRIP_NOPS (arg);
11333 if (TREE_CODE (arg) == ADDR_EXPR)
11335 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11337 *ptr_offset = size_zero_node;
11338 return TREE_OPERAND (arg, 0);
11340 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11342 array = TREE_OPERAND (arg, 0);
11343 offset = size_zero_node;
11345 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11347 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11348 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11349 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11350 return 0;
11352 /* Check if the array has a nonzero lower bound. */
11353 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11354 if (!integer_zerop (lower_bound))
11356 /* If the offset and base aren't both constants, return 0. */
11357 if (TREE_CODE (lower_bound) != INTEGER_CST)
11358 return 0;
11359 if (TREE_CODE (offset) != INTEGER_CST)
11360 return 0;
11361 /* Adjust offset by the lower bound. */
11362 offset = size_diffop (fold_convert (sizetype, offset),
11363 fold_convert (sizetype, lower_bound));
11366 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11368 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11369 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11370 if (TREE_CODE (array) != ADDR_EXPR)
11371 return 0;
11372 array = TREE_OPERAND (array, 0);
11373 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11374 return 0;
11376 else
11377 return 0;
11379 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11381 tree arg0 = TREE_OPERAND (arg, 0);
11382 tree arg1 = TREE_OPERAND (arg, 1);
11384 STRIP_NOPS (arg0);
11385 STRIP_NOPS (arg1);
11387 if (TREE_CODE (arg0) == ADDR_EXPR
11388 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11389 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11391 array = TREE_OPERAND (arg0, 0);
11392 offset = arg1;
11394 else if (TREE_CODE (arg1) == ADDR_EXPR
11395 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11396 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11398 array = TREE_OPERAND (arg1, 0);
11399 offset = arg0;
11401 else
11402 return 0;
11404 else
11405 return 0;
11407 if (TREE_CODE (array) == STRING_CST)
11409 *ptr_offset = fold_convert (sizetype, offset);
11410 return array;
11412 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11414 int length;
11415 tree init = ctor_for_folding (array);
11417 /* Variables initialized to string literals can be handled too. */
11418 if (init == error_mark_node
11419 || !init
11420 || TREE_CODE (init) != STRING_CST)
11421 return 0;
11423 /* Avoid const char foo[4] = "abcde"; */
11424 if (DECL_SIZE_UNIT (array) == NULL_TREE
11425 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11426 || (length = TREE_STRING_LENGTH (init)) <= 0
11427 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11428 return 0;
11430 /* If variable is bigger than the string literal, OFFSET must be constant
11431 and inside of the bounds of the string literal. */
11432 offset = fold_convert (sizetype, offset);
11433 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11434 && (! tree_fits_uhwi_p (offset)
11435 || compare_tree_int (offset, length) >= 0))
11436 return 0;
11438 *ptr_offset = offset;
11439 return init;
11442 return 0;
11445 /* Generate code to calculate OPS, and exploded expression
11446 using a store-flag instruction and return an rtx for the result.
11447 OPS reflects a comparison.
11449 If TARGET is nonzero, store the result there if convenient.
11451 Return zero if there is no suitable set-flag instruction
11452 available on this machine.
11454 Once expand_expr has been called on the arguments of the comparison,
11455 we are committed to doing the store flag, since it is not safe to
11456 re-evaluate the expression. We emit the store-flag insn by calling
11457 emit_store_flag, but only expand the arguments if we have a reason
11458 to believe that emit_store_flag will be successful. If we think that
11459 it will, but it isn't, we have to simulate the store-flag with a
11460 set/jump/set sequence. */
11462 static rtx
11463 do_store_flag (sepops ops, rtx target, machine_mode mode)
11465 enum rtx_code code;
11466 tree arg0, arg1, type;
11467 machine_mode operand_mode;
11468 int unsignedp;
11469 rtx op0, op1;
11470 rtx subtarget = target;
11471 location_t loc = ops->location;
11473 arg0 = ops->op0;
11474 arg1 = ops->op1;
11476 /* Don't crash if the comparison was erroneous. */
11477 if (arg0 == error_mark_node || arg1 == error_mark_node)
11478 return const0_rtx;
11480 type = TREE_TYPE (arg0);
11481 operand_mode = TYPE_MODE (type);
11482 unsignedp = TYPE_UNSIGNED (type);
11484 /* We won't bother with BLKmode store-flag operations because it would mean
11485 passing a lot of information to emit_store_flag. */
11486 if (operand_mode == BLKmode)
11487 return 0;
11489 /* We won't bother with store-flag operations involving function pointers
11490 when function pointers must be canonicalized before comparisons. */
11491 if (targetm.have_canonicalize_funcptr_for_compare ()
11492 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11493 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11494 == FUNCTION_TYPE))
11495 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11496 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11497 == FUNCTION_TYPE))))
11498 return 0;
11500 STRIP_NOPS (arg0);
11501 STRIP_NOPS (arg1);
11503 /* For vector typed comparisons emit code to generate the desired
11504 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11505 expander for this. */
11506 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11508 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11509 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11510 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11511 return expand_vec_cmp_expr (ops->type, ifexp, target);
11512 else
11514 tree if_true = constant_boolean_node (true, ops->type);
11515 tree if_false = constant_boolean_node (false, ops->type);
11516 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11517 if_false, target);
11521 /* Get the rtx comparison code to use. We know that EXP is a comparison
11522 operation of some type. Some comparisons against 1 and -1 can be
11523 converted to comparisons with zero. Do so here so that the tests
11524 below will be aware that we have a comparison with zero. These
11525 tests will not catch constants in the first operand, but constants
11526 are rarely passed as the first operand. */
11528 switch (ops->code)
11530 case EQ_EXPR:
11531 code = EQ;
11532 break;
11533 case NE_EXPR:
11534 code = NE;
11535 break;
11536 case LT_EXPR:
11537 if (integer_onep (arg1))
11538 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11539 else
11540 code = unsignedp ? LTU : LT;
11541 break;
11542 case LE_EXPR:
11543 if (! unsignedp && integer_all_onesp (arg1))
11544 arg1 = integer_zero_node, code = LT;
11545 else
11546 code = unsignedp ? LEU : LE;
11547 break;
11548 case GT_EXPR:
11549 if (! unsignedp && integer_all_onesp (arg1))
11550 arg1 = integer_zero_node, code = GE;
11551 else
11552 code = unsignedp ? GTU : GT;
11553 break;
11554 case GE_EXPR:
11555 if (integer_onep (arg1))
11556 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11557 else
11558 code = unsignedp ? GEU : GE;
11559 break;
11561 case UNORDERED_EXPR:
11562 code = UNORDERED;
11563 break;
11564 case ORDERED_EXPR:
11565 code = ORDERED;
11566 break;
11567 case UNLT_EXPR:
11568 code = UNLT;
11569 break;
11570 case UNLE_EXPR:
11571 code = UNLE;
11572 break;
11573 case UNGT_EXPR:
11574 code = UNGT;
11575 break;
11576 case UNGE_EXPR:
11577 code = UNGE;
11578 break;
11579 case UNEQ_EXPR:
11580 code = UNEQ;
11581 break;
11582 case LTGT_EXPR:
11583 code = LTGT;
11584 break;
11586 default:
11587 gcc_unreachable ();
11590 /* Put a constant second. */
11591 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11592 || TREE_CODE (arg0) == FIXED_CST)
11594 std::swap (arg0, arg1);
11595 code = swap_condition (code);
11598 /* If this is an equality or inequality test of a single bit, we can
11599 do this by shifting the bit being tested to the low-order bit and
11600 masking the result with the constant 1. If the condition was EQ,
11601 we xor it with 1. This does not require an scc insn and is faster
11602 than an scc insn even if we have it.
11604 The code to make this transformation was moved into fold_single_bit_test,
11605 so we just call into the folder and expand its result. */
11607 if ((code == NE || code == EQ)
11608 && integer_zerop (arg1)
11609 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11611 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11612 if (srcstmt
11613 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11615 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11616 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11617 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11618 gimple_assign_rhs1 (srcstmt),
11619 gimple_assign_rhs2 (srcstmt));
11620 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11621 if (temp)
11622 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11626 if (! get_subtarget (target)
11627 || GET_MODE (subtarget) != operand_mode)
11628 subtarget = 0;
11630 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11632 if (target == 0)
11633 target = gen_reg_rtx (mode);
11635 /* Try a cstore if possible. */
11636 return emit_store_flag_force (target, code, op0, op1,
11637 operand_mode, unsignedp,
11638 (TYPE_PRECISION (ops->type) == 1
11639 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11642 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11643 0 otherwise (i.e. if there is no casesi instruction).
11645 DEFAULT_PROBABILITY is the probability of jumping to the default
11646 label. */
11648 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11649 rtx table_label, rtx default_label, rtx fallback_label,
11650 profile_probability default_probability)
11652 struct expand_operand ops[5];
11653 scalar_int_mode index_mode = SImode;
11654 rtx op1, op2, index;
11656 if (! targetm.have_casesi ())
11657 return 0;
11659 /* The index must be some form of integer. Convert it to SImode. */
11660 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11661 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11663 rtx rangertx = expand_normal (range);
11665 /* We must handle the endpoints in the original mode. */
11666 index_expr = build2 (MINUS_EXPR, index_type,
11667 index_expr, minval);
11668 minval = integer_zero_node;
11669 index = expand_normal (index_expr);
11670 if (default_label)
11671 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11672 omode, 1, default_label,
11673 default_probability);
11674 /* Now we can safely truncate. */
11675 index = convert_to_mode (index_mode, index, 0);
11677 else
11679 if (omode != index_mode)
11681 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11682 index_expr = fold_convert (index_type, index_expr);
11685 index = expand_normal (index_expr);
11688 do_pending_stack_adjust ();
11690 op1 = expand_normal (minval);
11691 op2 = expand_normal (range);
11693 create_input_operand (&ops[0], index, index_mode);
11694 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11695 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11696 create_fixed_operand (&ops[3], table_label);
11697 create_fixed_operand (&ops[4], (default_label
11698 ? default_label
11699 : fallback_label));
11700 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11701 return 1;
11704 /* Attempt to generate a tablejump instruction; same concept. */
11705 /* Subroutine of the next function.
11707 INDEX is the value being switched on, with the lowest value
11708 in the table already subtracted.
11709 MODE is its expected mode (needed if INDEX is constant).
11710 RANGE is the length of the jump table.
11711 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11713 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11714 index value is out of range.
11715 DEFAULT_PROBABILITY is the probability of jumping to
11716 the default label. */
11718 static void
11719 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11720 rtx default_label, profile_probability default_probability)
11722 rtx temp, vector;
11724 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11725 cfun->cfg->max_jumptable_ents = INTVAL (range);
11727 /* Do an unsigned comparison (in the proper mode) between the index
11728 expression and the value which represents the length of the range.
11729 Since we just finished subtracting the lower bound of the range
11730 from the index expression, this comparison allows us to simultaneously
11731 check that the original index expression value is both greater than
11732 or equal to the minimum value of the range and less than or equal to
11733 the maximum value of the range. */
11735 if (default_label)
11736 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11737 default_label, default_probability);
11740 /* If index is in range, it must fit in Pmode.
11741 Convert to Pmode so we can index with it. */
11742 if (mode != Pmode)
11743 index = convert_to_mode (Pmode, index, 1);
11745 /* Don't let a MEM slip through, because then INDEX that comes
11746 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11747 and break_out_memory_refs will go to work on it and mess it up. */
11748 #ifdef PIC_CASE_VECTOR_ADDRESS
11749 if (flag_pic && !REG_P (index))
11750 index = copy_to_mode_reg (Pmode, index);
11751 #endif
11753 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11754 GET_MODE_SIZE, because this indicates how large insns are. The other
11755 uses should all be Pmode, because they are addresses. This code
11756 could fail if addresses and insns are not the same size. */
11757 index = simplify_gen_binary (MULT, Pmode, index,
11758 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11759 Pmode));
11760 index = simplify_gen_binary (PLUS, Pmode, index,
11761 gen_rtx_LABEL_REF (Pmode, table_label));
11763 #ifdef PIC_CASE_VECTOR_ADDRESS
11764 if (flag_pic)
11765 index = PIC_CASE_VECTOR_ADDRESS (index);
11766 else
11767 #endif
11768 index = memory_address (CASE_VECTOR_MODE, index);
11769 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11770 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11771 convert_move (temp, vector, 0);
11773 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11775 /* If we are generating PIC code or if the table is PC-relative, the
11776 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11777 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11778 emit_barrier ();
11782 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11783 rtx table_label, rtx default_label,
11784 profile_probability default_probability)
11786 rtx index;
11788 if (! targetm.have_tablejump ())
11789 return 0;
11791 index_expr = fold_build2 (MINUS_EXPR, index_type,
11792 fold_convert (index_type, index_expr),
11793 fold_convert (index_type, minval));
11794 index = expand_normal (index_expr);
11795 do_pending_stack_adjust ();
11797 do_tablejump (index, TYPE_MODE (index_type),
11798 convert_modes (TYPE_MODE (index_type),
11799 TYPE_MODE (TREE_TYPE (range)),
11800 expand_normal (range),
11801 TYPE_UNSIGNED (TREE_TYPE (range))),
11802 table_label, default_label, default_probability);
11803 return 1;
11806 /* Return a CONST_VECTOR rtx representing vector mask for
11807 a VECTOR_CST of booleans. */
11808 static rtx
11809 const_vector_mask_from_tree (tree exp)
11811 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11812 machine_mode inner = GET_MODE_INNER (mode);
11814 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11815 VECTOR_CST_NELTS_PER_PATTERN (exp));
11816 unsigned int count = builder.encoded_nelts ();
11817 for (unsigned int i = 0; i < count; ++i)
11819 tree elt = VECTOR_CST_ELT (exp, i);
11820 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11821 if (integer_zerop (elt))
11822 builder.quick_push (CONST0_RTX (inner));
11823 else if (integer_onep (elt)
11824 || integer_minus_onep (elt))
11825 builder.quick_push (CONSTM1_RTX (inner));
11826 else
11827 gcc_unreachable ();
11829 return builder.build ();
11832 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11833 Return a constant scalar rtx of mode MODE in which bit X is set if element
11834 X of EXP is nonzero. */
11835 static rtx
11836 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11838 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11839 tree elt;
11840 unsigned i;
11842 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11844 elt = VECTOR_CST_ELT (exp, i);
11845 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11846 if (integer_all_onesp (elt))
11847 res = wi::set_bit (res, i);
11848 else
11849 gcc_assert (integer_zerop (elt));
11852 return immed_wide_int_const (res, mode);
11855 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11856 static rtx
11857 const_vector_from_tree (tree exp)
11859 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11861 if (initializer_zerop (exp))
11862 return CONST0_RTX (mode);
11864 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11865 return const_vector_mask_from_tree (exp);
11867 machine_mode inner = GET_MODE_INNER (mode);
11869 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11870 VECTOR_CST_NELTS_PER_PATTERN (exp));
11871 unsigned int count = builder.encoded_nelts ();
11872 for (unsigned int i = 0; i < count; ++i)
11874 tree elt = VECTOR_CST_ELT (exp, i);
11875 if (TREE_CODE (elt) == REAL_CST)
11876 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
11877 inner));
11878 else if (TREE_CODE (elt) == FIXED_CST)
11879 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11880 inner));
11881 else
11882 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
11883 inner));
11885 return builder.build ();
11888 /* Build a decl for a personality function given a language prefix. */
11890 tree
11891 build_personality_function (const char *lang)
11893 const char *unwind_and_version;
11894 tree decl, type;
11895 char *name;
11897 switch (targetm_common.except_unwind_info (&global_options))
11899 case UI_NONE:
11900 return NULL;
11901 case UI_SJLJ:
11902 unwind_and_version = "_sj0";
11903 break;
11904 case UI_DWARF2:
11905 case UI_TARGET:
11906 unwind_and_version = "_v0";
11907 break;
11908 case UI_SEH:
11909 unwind_and_version = "_seh0";
11910 break;
11911 default:
11912 gcc_unreachable ();
11915 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11917 type = build_function_type_list (integer_type_node, integer_type_node,
11918 long_long_unsigned_type_node,
11919 ptr_type_node, ptr_type_node, NULL_TREE);
11920 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11921 get_identifier (name), type);
11922 DECL_ARTIFICIAL (decl) = 1;
11923 DECL_EXTERNAL (decl) = 1;
11924 TREE_PUBLIC (decl) = 1;
11926 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11927 are the flags assigned by targetm.encode_section_info. */
11928 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11930 return decl;
11933 /* Extracts the personality function of DECL and returns the corresponding
11934 libfunc. */
11937 get_personality_function (tree decl)
11939 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11940 enum eh_personality_kind pk;
11942 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11943 if (pk == eh_personality_none)
11944 return NULL;
11946 if (!personality
11947 && pk == eh_personality_any)
11948 personality = lang_hooks.eh_personality ();
11950 if (pk == eh_personality_lang)
11951 gcc_assert (personality != NULL_TREE);
11953 return XEXP (DECL_RTL (personality), 0);
11956 /* Returns a tree for the size of EXP in bytes. */
11958 static tree
11959 tree_expr_size (const_tree exp)
11961 if (DECL_P (exp)
11962 && DECL_SIZE_UNIT (exp) != 0)
11963 return DECL_SIZE_UNIT (exp);
11964 else
11965 return size_in_bytes (TREE_TYPE (exp));
11968 /* Return an rtx for the size in bytes of the value of EXP. */
11971 expr_size (tree exp)
11973 tree size;
11975 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11976 size = TREE_OPERAND (exp, 1);
11977 else
11979 size = tree_expr_size (exp);
11980 gcc_assert (size);
11981 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11984 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11987 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11988 if the size can vary or is larger than an integer. */
11990 static HOST_WIDE_INT
11991 int_expr_size (tree exp)
11993 tree size;
11995 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11996 size = TREE_OPERAND (exp, 1);
11997 else
11999 size = tree_expr_size (exp);
12000 gcc_assert (size);
12003 if (size == 0 || !tree_fits_shwi_p (size))
12004 return -1;
12006 return tree_to_shwi (size);