compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / expr.cc
blobba627f176a784faf7caca94a61f49f5c6cbd315c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2022 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 "optabs.h"
33 #include "expmed.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-dfa.h"
58 #include "tree-ssa-live.h"
59 #include "tree-outof-ssa.h"
60 #include "tree-ssa-address.h"
61 #include "builtins.h"
62 #include "ccmp.h"
63 #include "gimple-iterator.h"
64 #include "gimple-fold.h"
65 #include "rtx-vector-builder.h"
66 #include "tree-pretty-print.h"
67 #include "flags.h"
70 /* If this is nonzero, we do not bother generating VOLATILE
71 around volatile memory references, and we are willing to
72 output indirect addresses. If cse is to follow, we reject
73 indirect addresses so a useful potential cse is generated;
74 if it is used only once, instruction combination will produce
75 the same indirect address eventually. */
76 int cse_not_expected;
78 static bool block_move_libcall_safe_for_call_parm (void);
79 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
80 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
81 unsigned HOST_WIDE_INT,
82 unsigned HOST_WIDE_INT, bool);
83 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
84 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
85 static rtx_insn *compress_float_constant (rtx, rtx);
86 static rtx get_subtarget (rtx);
87 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
88 machine_mode, tree, alias_set_type, bool, bool);
90 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
92 static int is_aligning_offset (const_tree, const_tree);
93 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
94 static rtx do_store_flag (sepops, rtx, machine_mode);
95 #ifdef PUSH_ROUNDING
96 static void emit_single_push_insn (machine_mode, rtx, tree);
97 #endif
98 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
99 profile_probability);
100 static rtx const_vector_from_tree (tree);
101 static tree tree_expr_size (const_tree);
102 static void convert_mode_scalar (rtx, rtx, int);
105 /* This is run to set up which modes can be used
106 directly in memory and to initialize the block move optab. It is run
107 at the beginning of compilation and when the target is reinitialized. */
109 void
110 init_expr_target (void)
112 rtx pat;
113 int num_clobbers;
114 rtx mem, mem1;
115 rtx reg;
117 /* Try indexing by frame ptr and try by stack ptr.
118 It is known that on the Convex the stack ptr isn't a valid index.
119 With luck, one or the other is valid on any machine. */
120 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
121 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
123 /* A scratch register we can modify in-place below to avoid
124 useless RTL allocations. */
125 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
127 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
128 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
129 PATTERN (insn) = pat;
131 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
132 mode = (machine_mode) ((int) mode + 1))
134 int regno;
136 direct_load[(int) mode] = direct_store[(int) mode] = 0;
137 PUT_MODE (mem, mode);
138 PUT_MODE (mem1, mode);
140 /* See if there is some register that can be used in this mode and
141 directly loaded or stored from memory. */
143 if (mode != VOIDmode && mode != BLKmode)
144 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
145 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
146 regno++)
148 if (!targetm.hard_regno_mode_ok (regno, mode))
149 continue;
151 set_mode_and_regno (reg, mode, regno);
153 SET_SRC (pat) = mem;
154 SET_DEST (pat) = reg;
155 if (recog (pat, insn, &num_clobbers) >= 0)
156 direct_load[(int) mode] = 1;
158 SET_SRC (pat) = mem1;
159 SET_DEST (pat) = reg;
160 if (recog (pat, insn, &num_clobbers) >= 0)
161 direct_load[(int) mode] = 1;
163 SET_SRC (pat) = reg;
164 SET_DEST (pat) = mem;
165 if (recog (pat, insn, &num_clobbers) >= 0)
166 direct_store[(int) mode] = 1;
168 SET_SRC (pat) = reg;
169 SET_DEST (pat) = mem1;
170 if (recog (pat, insn, &num_clobbers) >= 0)
171 direct_store[(int) mode] = 1;
175 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
177 opt_scalar_float_mode mode_iter;
178 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
180 scalar_float_mode mode = mode_iter.require ();
181 scalar_float_mode srcmode;
182 FOR_EACH_MODE_UNTIL (srcmode, mode)
184 enum insn_code ic;
186 ic = can_extend_p (mode, srcmode, 0);
187 if (ic == CODE_FOR_nothing)
188 continue;
190 PUT_MODE (mem, srcmode);
192 if (insn_operand_matches (ic, 1, mem))
193 float_extend_from_mem[mode][srcmode] = true;
198 /* This is run at the start of compiling a function. */
200 void
201 init_expr (void)
203 memset (&crtl->expr, 0, sizeof (crtl->expr));
206 /* Copy data from FROM to TO, where the machine modes are not the same.
207 Both modes may be integer, or both may be floating, or both may be
208 fixed-point.
209 UNSIGNEDP should be nonzero if FROM is an unsigned type.
210 This causes zero-extension instead of sign-extension. */
212 void
213 convert_move (rtx to, rtx from, int unsignedp)
215 machine_mode to_mode = GET_MODE (to);
216 machine_mode from_mode = GET_MODE (from);
218 gcc_assert (to_mode != BLKmode);
219 gcc_assert (from_mode != BLKmode);
221 /* If the source and destination are already the same, then there's
222 nothing to do. */
223 if (to == from)
224 return;
226 /* If FROM is a SUBREG that indicates that we have already done at least
227 the required extension, strip it. We don't handle such SUBREGs as
228 TO here. */
230 scalar_int_mode to_int_mode;
231 if (GET_CODE (from) == SUBREG
232 && SUBREG_PROMOTED_VAR_P (from)
233 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
234 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
235 >= GET_MODE_PRECISION (to_int_mode))
236 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
238 scalar_int_mode int_orig_mode;
239 scalar_int_mode int_inner_mode;
240 machine_mode orig_mode = GET_MODE (from);
242 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
243 from_mode = to_int_mode;
245 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
246 the original mode, but narrower than the inner mode. */
247 if (GET_CODE (from) == SUBREG
248 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
249 && GET_MODE_PRECISION (to_int_mode)
250 > GET_MODE_PRECISION (int_orig_mode)
251 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)),
252 &int_inner_mode)
253 && GET_MODE_PRECISION (int_inner_mode)
254 > GET_MODE_PRECISION (to_int_mode))
256 SUBREG_PROMOTED_VAR_P (from) = 1;
257 SUBREG_PROMOTED_SET (from, unsignedp);
261 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
263 if (to_mode == from_mode
264 || (from_mode == VOIDmode && CONSTANT_P (from)))
266 emit_move_insn (to, from);
267 return;
270 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
272 if (GET_MODE_UNIT_PRECISION (to_mode)
273 > GET_MODE_UNIT_PRECISION (from_mode))
275 optab op = unsignedp ? zext_optab : sext_optab;
276 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
277 if (icode != CODE_FOR_nothing)
279 emit_unop_insn (icode, to, from,
280 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
281 return;
285 if (GET_MODE_UNIT_PRECISION (to_mode)
286 < GET_MODE_UNIT_PRECISION (from_mode))
288 insn_code icode = convert_optab_handler (trunc_optab,
289 to_mode, from_mode);
290 if (icode != CODE_FOR_nothing)
292 emit_unop_insn (icode, to, from, TRUNCATE);
293 return;
297 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
298 GET_MODE_BITSIZE (to_mode)));
300 if (VECTOR_MODE_P (to_mode))
301 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
302 else
303 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
305 emit_move_insn (to, from);
306 return;
309 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
311 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
312 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
313 return;
316 convert_mode_scalar (to, from, unsignedp);
319 /* Like convert_move, but deals only with scalar modes. */
321 static void
322 convert_mode_scalar (rtx to, rtx from, int unsignedp)
324 /* Both modes should be scalar types. */
325 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
326 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
327 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
328 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
329 enum insn_code code;
330 rtx libcall;
332 gcc_assert (to_real == from_real);
334 /* rtx code for making an equivalent value. */
335 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
336 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
338 if (to_real)
340 rtx value;
341 rtx_insn *insns;
342 convert_optab tab;
344 gcc_assert ((GET_MODE_PRECISION (from_mode)
345 != GET_MODE_PRECISION (to_mode))
346 || (DECIMAL_FLOAT_MODE_P (from_mode)
347 != DECIMAL_FLOAT_MODE_P (to_mode)));
349 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
350 /* Conversion between decimal float and binary float, same size. */
351 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
352 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
353 tab = sext_optab;
354 else
355 tab = trunc_optab;
357 /* Try converting directly if the insn is supported. */
359 code = convert_optab_handler (tab, to_mode, from_mode);
360 if (code != CODE_FOR_nothing)
362 emit_unop_insn (code, to, from,
363 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
364 return;
367 /* Otherwise use a libcall. */
368 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
370 /* Is this conversion implemented yet? */
371 gcc_assert (libcall);
373 start_sequence ();
374 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
375 from, from_mode);
376 insns = get_insns ();
377 end_sequence ();
378 emit_libcall_block (insns, to, value,
379 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
380 from)
381 : gen_rtx_FLOAT_EXTEND (to_mode, from));
382 return;
385 /* Handle pointer conversion. */ /* SPEE 900220. */
386 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
388 convert_optab ctab;
390 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
391 ctab = trunc_optab;
392 else if (unsignedp)
393 ctab = zext_optab;
394 else
395 ctab = sext_optab;
397 if (convert_optab_handler (ctab, to_mode, from_mode)
398 != CODE_FOR_nothing)
400 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
401 to, from, UNKNOWN);
402 return;
406 /* Targets are expected to provide conversion insns between PxImode and
407 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
408 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
410 scalar_int_mode full_mode
411 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
413 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
414 != CODE_FOR_nothing);
416 if (full_mode != from_mode)
417 from = convert_to_mode (full_mode, from, unsignedp);
418 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
419 to, from, UNKNOWN);
420 return;
422 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
424 rtx new_from;
425 scalar_int_mode full_mode
426 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
427 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
428 enum insn_code icode;
430 icode = convert_optab_handler (ctab, full_mode, from_mode);
431 gcc_assert (icode != CODE_FOR_nothing);
433 if (to_mode == full_mode)
435 emit_unop_insn (icode, to, from, UNKNOWN);
436 return;
439 new_from = gen_reg_rtx (full_mode);
440 emit_unop_insn (icode, new_from, from, UNKNOWN);
442 /* else proceed to integer conversions below. */
443 from_mode = full_mode;
444 from = new_from;
447 /* Make sure both are fixed-point modes or both are not. */
448 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
449 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
450 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
452 /* If we widen from_mode to to_mode and they are in the same class,
453 we won't saturate the result.
454 Otherwise, always saturate the result to play safe. */
455 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
456 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
457 expand_fixed_convert (to, from, 0, 0);
458 else
459 expand_fixed_convert (to, from, 0, 1);
460 return;
463 /* Now both modes are integers. */
465 /* Handle expanding beyond a word. */
466 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
467 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
469 rtx_insn *insns;
470 rtx lowpart;
471 rtx fill_value;
472 rtx lowfrom;
473 int i;
474 scalar_mode lowpart_mode;
475 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
477 /* Try converting directly if the insn is supported. */
478 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
479 != CODE_FOR_nothing)
481 /* If FROM is a SUBREG, put it into a register. Do this
482 so that we always generate the same set of insns for
483 better cse'ing; if an intermediate assignment occurred,
484 we won't be doing the operation directly on the SUBREG. */
485 if (optimize > 0 && GET_CODE (from) == SUBREG)
486 from = force_reg (from_mode, from);
487 emit_unop_insn (code, to, from, equiv_code);
488 return;
490 /* Next, try converting via full word. */
491 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
492 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
493 != CODE_FOR_nothing))
495 rtx word_to = gen_reg_rtx (word_mode);
496 if (REG_P (to))
498 if (reg_overlap_mentioned_p (to, from))
499 from = force_reg (from_mode, from);
500 emit_clobber (to);
502 convert_move (word_to, from, unsignedp);
503 emit_unop_insn (code, to, word_to, equiv_code);
504 return;
507 /* No special multiword conversion insn; do it by hand. */
508 start_sequence ();
510 /* Since we will turn this into a no conflict block, we must ensure
511 the source does not overlap the target so force it into an isolated
512 register when maybe so. Likewise for any MEM input, since the
513 conversion sequence might require several references to it and we
514 must ensure we're getting the same value every time. */
516 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
517 from = force_reg (from_mode, from);
519 /* Get a copy of FROM widened to a word, if necessary. */
520 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
521 lowpart_mode = word_mode;
522 else
523 lowpart_mode = from_mode;
525 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
527 lowpart = gen_lowpart (lowpart_mode, to);
528 emit_move_insn (lowpart, lowfrom);
530 /* Compute the value to put in each remaining word. */
531 if (unsignedp)
532 fill_value = const0_rtx;
533 else
534 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
535 LT, lowfrom, const0_rtx,
536 lowpart_mode, 0, -1);
538 /* Fill the remaining words. */
539 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
541 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
542 rtx subword = operand_subword (to, index, 1, to_mode);
544 gcc_assert (subword);
546 if (fill_value != subword)
547 emit_move_insn (subword, fill_value);
550 insns = get_insns ();
551 end_sequence ();
553 emit_insn (insns);
554 return;
557 /* Truncating multi-word to a word or less. */
558 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
559 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
561 if (!((MEM_P (from)
562 && ! MEM_VOLATILE_P (from)
563 && direct_load[(int) to_mode]
564 && ! mode_dependent_address_p (XEXP (from, 0),
565 MEM_ADDR_SPACE (from)))
566 || REG_P (from)
567 || GET_CODE (from) == SUBREG))
568 from = force_reg (from_mode, from);
569 convert_move (to, gen_lowpart (word_mode, from), 0);
570 return;
573 /* Now follow all the conversions between integers
574 no more than a word long. */
576 /* For truncation, usually we can just refer to FROM in a narrower mode. */
577 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
578 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
580 if (!((MEM_P (from)
581 && ! MEM_VOLATILE_P (from)
582 && direct_load[(int) to_mode]
583 && ! mode_dependent_address_p (XEXP (from, 0),
584 MEM_ADDR_SPACE (from)))
585 || REG_P (from)
586 || GET_CODE (from) == SUBREG))
587 from = force_reg (from_mode, from);
588 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
589 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
590 from = copy_to_reg (from);
591 emit_move_insn (to, gen_lowpart (to_mode, from));
592 return;
595 /* Handle extension. */
596 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
598 /* Convert directly if that works. */
599 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
600 != CODE_FOR_nothing)
602 emit_unop_insn (code, to, from, equiv_code);
603 return;
605 else
607 rtx tmp;
608 int shift_amount;
610 /* Search for a mode to convert via. */
611 opt_scalar_mode intermediate_iter;
612 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
614 scalar_mode intermediate = intermediate_iter.require ();
615 if (((can_extend_p (to_mode, intermediate, unsignedp)
616 != CODE_FOR_nothing)
617 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
618 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
619 intermediate)))
620 && (can_extend_p (intermediate, from_mode, unsignedp)
621 != CODE_FOR_nothing))
623 convert_move (to, convert_to_mode (intermediate, from,
624 unsignedp), unsignedp);
625 return;
629 /* No suitable intermediate mode.
630 Generate what we need with shifts. */
631 shift_amount = (GET_MODE_PRECISION (to_mode)
632 - GET_MODE_PRECISION (from_mode));
633 from = gen_lowpart (to_mode, force_reg (from_mode, from));
634 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
635 to, unsignedp);
636 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
637 to, unsignedp);
638 if (tmp != to)
639 emit_move_insn (to, tmp);
640 return;
644 /* Support special truncate insns for certain modes. */
645 if (convert_optab_handler (trunc_optab, to_mode,
646 from_mode) != CODE_FOR_nothing)
648 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
649 to, from, UNKNOWN);
650 return;
653 /* Handle truncation of volatile memrefs, and so on;
654 the things that couldn't be truncated directly,
655 and for which there was no special instruction.
657 ??? Code above formerly short-circuited this, for most integer
658 mode pairs, with a force_reg in from_mode followed by a recursive
659 call to this routine. Appears always to have been wrong. */
660 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
662 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
663 emit_move_insn (to, temp);
664 return;
667 /* Mode combination is not recognized. */
668 gcc_unreachable ();
671 /* Return an rtx for a value that would result
672 from converting X to mode MODE.
673 Both X and MODE may be floating, or both integer.
674 UNSIGNEDP is nonzero if X is an unsigned value.
675 This can be done by referring to a part of X in place
676 or by copying to a new temporary with conversion. */
679 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
681 return convert_modes (mode, VOIDmode, x, unsignedp);
684 /* Return an rtx for a value that would result
685 from converting X from mode OLDMODE to mode MODE.
686 Both modes may be floating, or both integer.
687 UNSIGNEDP is nonzero if X is an unsigned value.
689 This can be done by referring to a part of X in place
690 or by copying to a new temporary with conversion.
692 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
695 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
697 rtx temp;
698 scalar_int_mode int_mode;
700 /* If FROM is a SUBREG that indicates that we have already done at least
701 the required extension, strip it. */
703 if (GET_CODE (x) == SUBREG
704 && SUBREG_PROMOTED_VAR_P (x)
705 && is_a <scalar_int_mode> (mode, &int_mode)
706 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
707 >= GET_MODE_PRECISION (int_mode))
708 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
710 scalar_int_mode int_orig_mode;
711 scalar_int_mode int_inner_mode;
712 machine_mode orig_mode = GET_MODE (x);
713 x = gen_lowpart (int_mode, SUBREG_REG (x));
715 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
716 the original mode, but narrower than the inner mode. */
717 if (GET_CODE (x) == SUBREG
718 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
719 && GET_MODE_PRECISION (int_mode)
720 > GET_MODE_PRECISION (int_orig_mode)
721 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
722 &int_inner_mode)
723 && GET_MODE_PRECISION (int_inner_mode)
724 > GET_MODE_PRECISION (int_mode))
726 SUBREG_PROMOTED_VAR_P (x) = 1;
727 SUBREG_PROMOTED_SET (x, unsignedp);
731 if (GET_MODE (x) != VOIDmode)
732 oldmode = GET_MODE (x);
734 if (mode == oldmode)
735 return x;
737 if (CONST_SCALAR_INT_P (x)
738 && is_a <scalar_int_mode> (mode, &int_mode))
740 /* If the caller did not tell us the old mode, then there is not
741 much to do with respect to canonicalization. We have to
742 assume that all the bits are significant. */
743 if (!is_a <scalar_int_mode> (oldmode))
744 oldmode = MAX_MODE_INT;
745 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
746 GET_MODE_PRECISION (int_mode),
747 unsignedp ? UNSIGNED : SIGNED);
748 return immed_wide_int_const (w, int_mode);
751 /* We can do this with a gen_lowpart if both desired and current modes
752 are integer, and this is either a constant integer, a register, or a
753 non-volatile MEM. */
754 scalar_int_mode int_oldmode;
755 if (is_int_mode (mode, &int_mode)
756 && is_int_mode (oldmode, &int_oldmode)
757 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
758 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
759 || CONST_POLY_INT_P (x)
760 || (REG_P (x)
761 && (!HARD_REGISTER_P (x)
762 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
763 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
764 return gen_lowpart (int_mode, x);
766 /* Converting from integer constant into mode is always equivalent to an
767 subreg operation. */
768 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
770 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
771 GET_MODE_BITSIZE (oldmode)));
772 return simplify_gen_subreg (mode, x, oldmode, 0);
775 temp = gen_reg_rtx (mode);
776 convert_move (temp, x, unsignedp);
777 return temp;
780 /* Variant of convert_modes for ABI parameter passing/return.
781 Return an rtx for a value that would result from converting X from
782 a floating point mode FMODE to wider integer mode MODE. */
785 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x)
787 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode));
788 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require ();
789 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
790 return convert_modes (mode, tmp_mode, tmp, 1);
793 /* Variant of convert_modes for ABI parameter passing/return.
794 Return an rtx for a value that would result from converting X from
795 an integer mode IMODE to a narrower floating point mode MODE. */
798 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x)
800 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode));
801 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require ();
802 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
803 return gen_lowpart_SUBREG (mode, tmp);
806 /* Return the largest alignment we can use for doing a move (or store)
807 of MAX_PIECES. ALIGN is the largest alignment we could use. */
809 static unsigned int
810 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
812 scalar_int_mode tmode
813 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
815 if (align >= GET_MODE_ALIGNMENT (tmode))
816 align = GET_MODE_ALIGNMENT (tmode);
817 else
819 scalar_int_mode xmode = NARROWEST_INT_MODE;
820 opt_scalar_int_mode mode_iter;
821 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
823 tmode = mode_iter.require ();
824 if (GET_MODE_SIZE (tmode) > max_pieces
825 || targetm.slow_unaligned_access (tmode, align))
826 break;
827 xmode = tmode;
830 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
833 return align;
836 /* Return the widest QI vector, if QI_MODE is true, or integer mode
837 that is narrower than SIZE bytes. */
839 static fixed_size_mode
840 widest_fixed_size_mode_for_size (unsigned int size, bool qi_vector)
842 fixed_size_mode result = NARROWEST_INT_MODE;
844 gcc_checking_assert (size > 1);
846 /* Use QI vector only if size is wider than a WORD. */
847 if (qi_vector && size > UNITS_PER_WORD)
849 machine_mode mode;
850 fixed_size_mode candidate;
851 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
852 if (is_a<fixed_size_mode> (mode, &candidate)
853 && GET_MODE_INNER (candidate) == QImode)
855 if (GET_MODE_SIZE (candidate) >= size)
856 break;
857 if (optab_handler (vec_duplicate_optab, candidate)
858 != CODE_FOR_nothing)
859 result = candidate;
862 if (result != NARROWEST_INT_MODE)
863 return result;
866 opt_scalar_int_mode tmode;
867 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
868 if (GET_MODE_SIZE (tmode.require ()) < size)
869 result = tmode.require ();
871 return result;
874 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
875 and should be performed piecewise. */
877 static bool
878 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
879 enum by_pieces_operation op)
881 return targetm.use_by_pieces_infrastructure_p (len, align, op,
882 optimize_insn_for_speed_p ());
885 /* Determine whether the LEN bytes can be moved by using several move
886 instructions. Return nonzero if a call to move_by_pieces should
887 succeed. */
889 bool
890 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
892 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
895 /* Return number of insns required to perform operation OP by pieces
896 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
898 unsigned HOST_WIDE_INT
899 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
900 unsigned int max_size, by_pieces_operation op)
902 unsigned HOST_WIDE_INT n_insns = 0;
903 fixed_size_mode mode;
905 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
907 /* NB: Round up L and ALIGN to the widest integer mode for
908 MAX_SIZE. */
909 mode = widest_fixed_size_mode_for_size (max_size,
910 op == SET_BY_PIECES);
911 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
913 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
914 if (up > l)
915 l = up;
916 align = GET_MODE_ALIGNMENT (mode);
920 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
922 while (max_size > 1 && l > 0)
924 mode = widest_fixed_size_mode_for_size (max_size,
925 op == SET_BY_PIECES);
926 enum insn_code icode;
928 unsigned int modesize = GET_MODE_SIZE (mode);
930 icode = optab_handler (mov_optab, mode);
931 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
933 unsigned HOST_WIDE_INT n_pieces = l / modesize;
934 l %= modesize;
935 switch (op)
937 default:
938 n_insns += n_pieces;
939 break;
941 case COMPARE_BY_PIECES:
942 int batch = targetm.compare_by_pieces_branch_ratio (mode);
943 int batch_ops = 4 * batch - 1;
944 unsigned HOST_WIDE_INT full = n_pieces / batch;
945 n_insns += full * batch_ops;
946 if (n_pieces % batch != 0)
947 n_insns++;
948 break;
952 max_size = modesize;
955 gcc_assert (!l);
956 return n_insns;
959 /* Used when performing piecewise block operations, holds information
960 about one of the memory objects involved. The member functions
961 can be used to generate code for loading from the object and
962 updating the address when iterating. */
964 class pieces_addr
966 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
967 stack pushes. */
968 rtx m_obj;
969 /* The address of the object. Can differ from that seen in the
970 MEM rtx if we copied the address to a register. */
971 rtx m_addr;
972 /* Nonzero if the address on the object has an autoincrement already,
973 signifies whether that was an increment or decrement. */
974 signed char m_addr_inc;
975 /* Nonzero if we intend to use autoinc without the address already
976 having autoinc form. We will insert add insns around each memory
977 reference, expecting later passes to form autoinc addressing modes.
978 The only supported options are predecrement and postincrement. */
979 signed char m_explicit_inc;
980 /* True if we have either of the two possible cases of using
981 autoincrement. */
982 bool m_auto;
983 /* True if this is an address to be used for load operations rather
984 than stores. */
985 bool m_is_load;
987 /* Optionally, a function to obtain constants for any given offset into
988 the objects, and data associated with it. */
989 by_pieces_constfn m_constfn;
990 void *m_cfndata;
991 public:
992 pieces_addr (rtx, bool, by_pieces_constfn, void *);
993 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
994 void increment_address (HOST_WIDE_INT);
995 void maybe_predec (HOST_WIDE_INT);
996 void maybe_postinc (HOST_WIDE_INT);
997 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
998 int get_addr_inc ()
1000 return m_addr_inc;
1004 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1005 true if the operation to be performed on this object is a load
1006 rather than a store. For stores, OBJ can be NULL, in which case we
1007 assume the operation is a stack push. For loads, the optional
1008 CONSTFN and its associated CFNDATA can be used in place of the
1009 memory load. */
1011 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1012 void *cfndata)
1013 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1015 m_addr_inc = 0;
1016 m_auto = false;
1017 if (obj)
1019 rtx addr = XEXP (obj, 0);
1020 rtx_code code = GET_CODE (addr);
1021 m_addr = addr;
1022 bool dec = code == PRE_DEC || code == POST_DEC;
1023 bool inc = code == PRE_INC || code == POST_INC;
1024 m_auto = inc || dec;
1025 if (m_auto)
1026 m_addr_inc = dec ? -1 : 1;
1028 /* While we have always looked for these codes here, the code
1029 implementing the memory operation has never handled them.
1030 Support could be added later if necessary or beneficial. */
1031 gcc_assert (code != PRE_INC && code != POST_DEC);
1033 else
1035 m_addr = NULL_RTX;
1036 if (!is_load)
1038 m_auto = true;
1039 if (STACK_GROWS_DOWNWARD)
1040 m_addr_inc = -1;
1041 else
1042 m_addr_inc = 1;
1044 else
1045 gcc_assert (constfn != NULL);
1047 m_explicit_inc = 0;
1048 if (constfn)
1049 gcc_assert (is_load);
1052 /* Decide whether to use autoinc for an address involved in a memory op.
1053 MODE is the mode of the accesses, REVERSE is true if we've decided to
1054 perform the operation starting from the end, and LEN is the length of
1055 the operation. Don't override an earlier decision to set m_auto. */
1057 void
1058 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1059 HOST_WIDE_INT len)
1061 if (m_auto || m_obj == NULL_RTX)
1062 return;
1064 bool use_predec = (m_is_load
1065 ? USE_LOAD_PRE_DECREMENT (mode)
1066 : USE_STORE_PRE_DECREMENT (mode));
1067 bool use_postinc = (m_is_load
1068 ? USE_LOAD_POST_INCREMENT (mode)
1069 : USE_STORE_POST_INCREMENT (mode));
1070 machine_mode addr_mode = get_address_mode (m_obj);
1072 if (use_predec && reverse)
1074 m_addr = copy_to_mode_reg (addr_mode,
1075 plus_constant (addr_mode,
1076 m_addr, len));
1077 m_auto = true;
1078 m_explicit_inc = -1;
1080 else if (use_postinc && !reverse)
1082 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1083 m_auto = true;
1084 m_explicit_inc = 1;
1086 else if (CONSTANT_P (m_addr))
1087 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1090 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1091 are using autoincrement for this address, we don't add the offset,
1092 but we still modify the MEM's properties. */
1095 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1096 by_pieces_prev *prev)
1098 if (m_constfn)
1099 /* Pass the previous data to m_constfn. */
1100 return m_constfn (m_cfndata, prev, offset, mode);
1101 if (m_obj == NULL_RTX)
1102 return NULL_RTX;
1103 if (m_auto)
1104 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1105 else
1106 return adjust_address (m_obj, mode, offset);
1109 /* Emit an add instruction to increment the address by SIZE. */
1111 void
1112 pieces_addr::increment_address (HOST_WIDE_INT size)
1114 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1115 emit_insn (gen_add2_insn (m_addr, amount));
1118 /* If we are supposed to decrement the address after each access, emit code
1119 to do so now. Increment by SIZE (which has should have the correct sign
1120 already). */
1122 void
1123 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1125 if (m_explicit_inc >= 0)
1126 return;
1127 gcc_assert (HAVE_PRE_DECREMENT);
1128 increment_address (size);
1131 /* If we are supposed to decrement the address after each access, emit code
1132 to do so now. Increment by SIZE. */
1134 void
1135 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1137 if (m_explicit_inc <= 0)
1138 return;
1139 gcc_assert (HAVE_POST_INCREMENT);
1140 increment_address (size);
1143 /* This structure is used by do_op_by_pieces to describe the operation
1144 to be performed. */
1146 class op_by_pieces_d
1148 private:
1149 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1150 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1152 protected:
1153 pieces_addr m_to, m_from;
1154 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1155 use it to check the valid mode size. */
1156 const unsigned HOST_WIDE_INT m_len;
1157 HOST_WIDE_INT m_offset;
1158 unsigned int m_align;
1159 unsigned int m_max_size;
1160 bool m_reverse;
1161 /* True if this is a stack push. */
1162 bool m_push;
1163 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1164 bool m_overlap_op_by_pieces;
1165 /* True if QI vector mode can be used. */
1166 bool m_qi_vector_mode;
1168 /* Virtual functions, overriden by derived classes for the specific
1169 operation. */
1170 virtual void generate (rtx, rtx, machine_mode) = 0;
1171 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1172 virtual void finish_mode (machine_mode)
1176 public:
1177 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1178 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1179 bool = false);
1180 void run ();
1183 /* The constructor for an op_by_pieces_d structure. We require two
1184 objects named TO and FROM, which are identified as loads or stores
1185 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1186 and its associated FROM_CFN_DATA can be used to replace loads with
1187 constant values. MAX_PIECES describes the maximum number of bytes
1188 at a time which can be moved efficiently. LEN describes the length
1189 of the operation. */
1191 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1192 bool to_load, rtx from, bool from_load,
1193 by_pieces_constfn from_cfn,
1194 void *from_cfn_data,
1195 unsigned HOST_WIDE_INT len,
1196 unsigned int align, bool push,
1197 bool qi_vector_mode)
1198 : m_to (to, to_load, NULL, NULL),
1199 m_from (from, from_load, from_cfn, from_cfn_data),
1200 m_len (len), m_max_size (max_pieces + 1),
1201 m_push (push), m_qi_vector_mode (qi_vector_mode)
1203 int toi = m_to.get_addr_inc ();
1204 int fromi = m_from.get_addr_inc ();
1205 if (toi >= 0 && fromi >= 0)
1206 m_reverse = false;
1207 else if (toi <= 0 && fromi <= 0)
1208 m_reverse = true;
1209 else
1210 gcc_unreachable ();
1212 m_offset = m_reverse ? len : 0;
1213 align = MIN (to ? MEM_ALIGN (to) : align,
1214 from ? MEM_ALIGN (from) : align);
1216 /* If copying requires more than two move insns,
1217 copy addresses to registers (to make displacements shorter)
1218 and use post-increment if available. */
1219 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1221 /* Find the mode of the largest comparison. */
1222 fixed_size_mode mode
1223 = widest_fixed_size_mode_for_size (m_max_size,
1224 m_qi_vector_mode);
1226 m_from.decide_autoinc (mode, m_reverse, len);
1227 m_to.decide_autoinc (mode, m_reverse, len);
1230 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1231 m_align = align;
1233 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1236 /* This function returns the largest usable integer mode for LEN bytes
1237 whose size is no bigger than size of MODE. */
1239 fixed_size_mode
1240 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1242 unsigned int size;
1245 size = GET_MODE_SIZE (mode);
1246 if (len >= size && prepare_mode (mode, m_align))
1247 break;
1248 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1249 mode = widest_fixed_size_mode_for_size (size, m_qi_vector_mode);
1251 while (1);
1252 return mode;
1255 /* Return the smallest integer or QI vector mode that is not narrower
1256 than SIZE bytes. */
1258 fixed_size_mode
1259 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1261 /* Use QI vector only for > size of WORD. */
1262 if (m_qi_vector_mode && size > UNITS_PER_WORD)
1264 machine_mode mode;
1265 fixed_size_mode candidate;
1266 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1267 if (is_a<fixed_size_mode> (mode, &candidate)
1268 && GET_MODE_INNER (candidate) == QImode)
1270 /* Don't return a mode wider than M_LEN. */
1271 if (GET_MODE_SIZE (candidate) > m_len)
1272 break;
1274 if (GET_MODE_SIZE (candidate) >= size
1275 && (optab_handler (vec_duplicate_optab, candidate)
1276 != CODE_FOR_nothing))
1277 return candidate;
1281 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1284 /* This function contains the main loop used for expanding a block
1285 operation. First move what we can in the largest integer mode,
1286 then go to successively smaller modes. For every access, call
1287 GENFUN with the two operands and the EXTRA_DATA. */
1289 void
1290 op_by_pieces_d::run ()
1292 if (m_len == 0)
1293 return;
1295 unsigned HOST_WIDE_INT length = m_len;
1297 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1298 fixed_size_mode mode
1299 = widest_fixed_size_mode_for_size (m_max_size, m_qi_vector_mode);
1300 mode = get_usable_mode (mode, length);
1302 by_pieces_prev to_prev = { nullptr, mode };
1303 by_pieces_prev from_prev = { nullptr, mode };
1307 unsigned int size = GET_MODE_SIZE (mode);
1308 rtx to1 = NULL_RTX, from1;
1310 while (length >= size)
1312 if (m_reverse)
1313 m_offset -= size;
1315 to1 = m_to.adjust (mode, m_offset, &to_prev);
1316 to_prev.data = to1;
1317 to_prev.mode = mode;
1318 from1 = m_from.adjust (mode, m_offset, &from_prev);
1319 from_prev.data = from1;
1320 from_prev.mode = mode;
1322 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1323 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1325 generate (to1, from1, mode);
1327 m_to.maybe_postinc (size);
1328 m_from.maybe_postinc (size);
1330 if (!m_reverse)
1331 m_offset += size;
1333 length -= size;
1336 finish_mode (mode);
1338 if (length == 0)
1339 return;
1341 if (!m_push && m_overlap_op_by_pieces)
1343 /* NB: Generate overlapping operations if it is not a stack
1344 push since stack push must not overlap. Get the smallest
1345 fixed size mode for M_LEN bytes. */
1346 mode = smallest_fixed_size_mode_for_size (length);
1347 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1348 int gap = GET_MODE_SIZE (mode) - length;
1349 if (gap > 0)
1351 /* If size of MODE > M_LEN, generate the last operation
1352 in MODE for the remaining bytes with ovelapping memory
1353 from the previois operation. */
1354 if (m_reverse)
1355 m_offset += gap;
1356 else
1357 m_offset -= gap;
1358 length += gap;
1361 else
1363 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1364 mode = widest_fixed_size_mode_for_size (size,
1365 m_qi_vector_mode);
1366 mode = get_usable_mode (mode, length);
1369 while (1);
1372 /* Derived class from op_by_pieces_d, providing support for block move
1373 operations. */
1375 #ifdef PUSH_ROUNDING
1376 #define PUSHG_P(to) ((to) == nullptr)
1377 #else
1378 #define PUSHG_P(to) false
1379 #endif
1381 class move_by_pieces_d : public op_by_pieces_d
1383 insn_gen_fn m_gen_fun;
1384 void generate (rtx, rtx, machine_mode) final override;
1385 bool prepare_mode (machine_mode, unsigned int) final override;
1387 public:
1388 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1389 unsigned int align)
1390 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1391 NULL, len, align, PUSHG_P (to))
1394 rtx finish_retmode (memop_ret);
1397 /* Return true if MODE can be used for a set of copies, given an
1398 alignment ALIGN. Prepare whatever data is necessary for later
1399 calls to generate. */
1401 bool
1402 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1404 insn_code icode = optab_handler (mov_optab, mode);
1405 m_gen_fun = GEN_FCN (icode);
1406 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1409 /* A callback used when iterating for a compare_by_pieces_operation.
1410 OP0 and OP1 are the values that have been loaded and should be
1411 compared in MODE. If OP0 is NULL, this means we should generate a
1412 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1413 gen function that should be used to generate the mode. */
1415 void
1416 move_by_pieces_d::generate (rtx op0, rtx op1,
1417 machine_mode mode ATTRIBUTE_UNUSED)
1419 #ifdef PUSH_ROUNDING
1420 if (op0 == NULL_RTX)
1422 emit_single_push_insn (mode, op1, NULL);
1423 return;
1425 #endif
1426 emit_insn (m_gen_fun (op0, op1));
1429 /* Perform the final adjustment at the end of a string to obtain the
1430 correct return value for the block operation.
1431 Return value is based on RETMODE argument. */
1434 move_by_pieces_d::finish_retmode (memop_ret retmode)
1436 gcc_assert (!m_reverse);
1437 if (retmode == RETURN_END_MINUS_ONE)
1439 m_to.maybe_postinc (-1);
1440 --m_offset;
1442 return m_to.adjust (QImode, m_offset);
1445 /* Generate several move instructions to copy LEN bytes from block FROM to
1446 block TO. (These are MEM rtx's with BLKmode).
1448 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1449 used to push FROM to the stack.
1451 ALIGN is maximum stack alignment we can assume.
1453 Return value is based on RETMODE argument. */
1456 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1457 unsigned int align, memop_ret retmode)
1459 #ifndef PUSH_ROUNDING
1460 if (to == NULL)
1461 gcc_unreachable ();
1462 #endif
1464 move_by_pieces_d data (to, from, len, align);
1466 data.run ();
1468 if (retmode != RETURN_BEGIN)
1469 return data.finish_retmode (retmode);
1470 else
1471 return to;
1474 /* Derived class from op_by_pieces_d, providing support for block move
1475 operations. */
1477 class store_by_pieces_d : public op_by_pieces_d
1479 insn_gen_fn m_gen_fun;
1480 void generate (rtx, rtx, machine_mode) final override;
1481 bool prepare_mode (machine_mode, unsigned int) final override;
1483 public:
1484 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1485 unsigned HOST_WIDE_INT len, unsigned int align,
1486 bool qi_vector_mode)
1487 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1488 cfn_data, len, align, false, qi_vector_mode)
1491 rtx finish_retmode (memop_ret);
1494 /* Return true if MODE can be used for a set of stores, given an
1495 alignment ALIGN. Prepare whatever data is necessary for later
1496 calls to generate. */
1498 bool
1499 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1501 insn_code icode = optab_handler (mov_optab, mode);
1502 m_gen_fun = GEN_FCN (icode);
1503 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1506 /* A callback used when iterating for a store_by_pieces_operation.
1507 OP0 and OP1 are the values that have been loaded and should be
1508 compared in MODE. If OP0 is NULL, this means we should generate a
1509 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1510 gen function that should be used to generate the mode. */
1512 void
1513 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1515 emit_insn (m_gen_fun (op0, op1));
1518 /* Perform the final adjustment at the end of a string to obtain the
1519 correct return value for the block operation.
1520 Return value is based on RETMODE argument. */
1523 store_by_pieces_d::finish_retmode (memop_ret retmode)
1525 gcc_assert (!m_reverse);
1526 if (retmode == RETURN_END_MINUS_ONE)
1528 m_to.maybe_postinc (-1);
1529 --m_offset;
1531 return m_to.adjust (QImode, m_offset);
1534 /* Determine whether the LEN bytes generated by CONSTFUN can be
1535 stored to memory using several move instructions. CONSTFUNDATA is
1536 a pointer which will be passed as argument in every CONSTFUN call.
1537 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1538 a memset operation and false if it's a copy of a constant string.
1539 Return nonzero if a call to store_by_pieces should succeed. */
1542 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1543 by_pieces_constfn constfun,
1544 void *constfundata, unsigned int align, bool memsetp)
1546 unsigned HOST_WIDE_INT l;
1547 unsigned int max_size;
1548 HOST_WIDE_INT offset = 0;
1549 enum insn_code icode;
1550 int reverse;
1551 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1552 rtx cst ATTRIBUTE_UNUSED;
1554 if (len == 0)
1555 return 1;
1557 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1558 memsetp
1559 ? SET_BY_PIECES
1560 : STORE_BY_PIECES,
1561 optimize_insn_for_speed_p ()))
1562 return 0;
1564 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1566 /* We would first store what we can in the largest integer mode, then go to
1567 successively smaller modes. */
1569 for (reverse = 0;
1570 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1571 reverse++)
1573 l = len;
1574 max_size = STORE_MAX_PIECES + 1;
1575 while (max_size > 1 && l > 0)
1577 fixed_size_mode mode
1578 = widest_fixed_size_mode_for_size (max_size, memsetp);
1580 icode = optab_handler (mov_optab, mode);
1581 if (icode != CODE_FOR_nothing
1582 && align >= GET_MODE_ALIGNMENT (mode))
1584 unsigned int size = GET_MODE_SIZE (mode);
1586 while (l >= size)
1588 if (reverse)
1589 offset -= size;
1591 cst = (*constfun) (constfundata, nullptr, offset, mode);
1592 /* All CONST_VECTORs can be loaded for memset since
1593 vec_duplicate_optab is a precondition to pick a
1594 vector mode for the memset expander. */
1595 if (!((memsetp && VECTOR_MODE_P (mode))
1596 || targetm.legitimate_constant_p (mode, cst)))
1597 return 0;
1599 if (!reverse)
1600 offset += size;
1602 l -= size;
1606 max_size = GET_MODE_SIZE (mode);
1609 /* The code above should have handled everything. */
1610 gcc_assert (!l);
1613 return 1;
1616 /* Generate several move instructions to store LEN bytes generated by
1617 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1618 pointer which will be passed as argument in every CONSTFUN call.
1619 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1620 a memset operation and false if it's a copy of a constant string.
1621 Return value is based on RETMODE argument. */
1624 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1625 by_pieces_constfn constfun,
1626 void *constfundata, unsigned int align, bool memsetp,
1627 memop_ret retmode)
1629 if (len == 0)
1631 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1632 return to;
1635 gcc_assert (targetm.use_by_pieces_infrastructure_p
1636 (len, align,
1637 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1638 optimize_insn_for_speed_p ()));
1640 store_by_pieces_d data (to, constfun, constfundata, len, align,
1641 memsetp);
1642 data.run ();
1644 if (retmode != RETURN_BEGIN)
1645 return data.finish_retmode (retmode);
1646 else
1647 return to;
1650 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1651 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1653 static void
1654 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1656 if (len == 0)
1657 return;
1659 /* Use builtin_memset_read_str to support vector mode broadcast. */
1660 char c = 0;
1661 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1662 true);
1663 data.run ();
1666 /* Context used by compare_by_pieces_genfn. It stores the fail label
1667 to jump to in case of miscomparison, and for branch ratios greater than 1,
1668 it stores an accumulator and the current and maximum counts before
1669 emitting another branch. */
1671 class compare_by_pieces_d : public op_by_pieces_d
1673 rtx_code_label *m_fail_label;
1674 rtx m_accumulator;
1675 int m_count, m_batch;
1677 void generate (rtx, rtx, machine_mode) final override;
1678 bool prepare_mode (machine_mode, unsigned int) final override;
1679 void finish_mode (machine_mode) final override;
1680 public:
1681 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1682 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1683 rtx_code_label *fail_label)
1684 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1685 op1_cfn_data, len, align, false)
1687 m_fail_label = fail_label;
1691 /* A callback used when iterating for a compare_by_pieces_operation.
1692 OP0 and OP1 are the values that have been loaded and should be
1693 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1694 context structure. */
1696 void
1697 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1699 if (m_batch > 1)
1701 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1702 true, OPTAB_LIB_WIDEN);
1703 if (m_count != 0)
1704 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1705 true, OPTAB_LIB_WIDEN);
1706 m_accumulator = temp;
1708 if (++m_count < m_batch)
1709 return;
1711 m_count = 0;
1712 op0 = m_accumulator;
1713 op1 = const0_rtx;
1714 m_accumulator = NULL_RTX;
1716 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1717 m_fail_label, profile_probability::uninitialized ());
1720 /* Return true if MODE can be used for a set of moves and comparisons,
1721 given an alignment ALIGN. Prepare whatever data is necessary for
1722 later calls to generate. */
1724 bool
1725 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1727 insn_code icode = optab_handler (mov_optab, mode);
1728 if (icode == CODE_FOR_nothing
1729 || align < GET_MODE_ALIGNMENT (mode)
1730 || !can_compare_p (EQ, mode, ccp_jump))
1731 return false;
1732 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1733 if (m_batch < 0)
1734 return false;
1735 m_accumulator = NULL_RTX;
1736 m_count = 0;
1737 return true;
1740 /* Called after expanding a series of comparisons in MODE. If we have
1741 accumulated results for which we haven't emitted a branch yet, do
1742 so now. */
1744 void
1745 compare_by_pieces_d::finish_mode (machine_mode mode)
1747 if (m_accumulator != NULL_RTX)
1748 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1749 NULL_RTX, NULL, m_fail_label,
1750 profile_probability::uninitialized ());
1753 /* Generate several move instructions to compare LEN bytes from blocks
1754 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1756 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1757 used to push FROM to the stack.
1759 ALIGN is maximum stack alignment we can assume.
1761 Optionally, the caller can pass a constfn and associated data in A1_CFN
1762 and A1_CFN_DATA. describing that the second operand being compared is a
1763 known constant and how to obtain its data. */
1765 static rtx
1766 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1767 rtx target, unsigned int align,
1768 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1770 rtx_code_label *fail_label = gen_label_rtx ();
1771 rtx_code_label *end_label = gen_label_rtx ();
1773 if (target == NULL_RTX
1774 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1775 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1777 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1778 fail_label);
1780 data.run ();
1782 emit_move_insn (target, const0_rtx);
1783 emit_jump (end_label);
1784 emit_barrier ();
1785 emit_label (fail_label);
1786 emit_move_insn (target, const1_rtx);
1787 emit_label (end_label);
1789 return target;
1792 /* Emit code to move a block Y to a block X. This may be done with
1793 string-move instructions, with multiple scalar move instructions,
1794 or with a library call.
1796 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1797 SIZE is an rtx that says how long they are.
1798 ALIGN is the maximum alignment we can assume they have.
1799 METHOD describes what kind of copy this is, and what mechanisms may be used.
1800 MIN_SIZE is the minimal size of block to move
1801 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1802 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1804 Return the address of the new block, if memcpy is called and returns it,
1805 0 otherwise. */
1808 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1809 unsigned int expected_align, HOST_WIDE_INT expected_size,
1810 unsigned HOST_WIDE_INT min_size,
1811 unsigned HOST_WIDE_INT max_size,
1812 unsigned HOST_WIDE_INT probable_max_size,
1813 bool bail_out_libcall, bool *is_move_done,
1814 bool might_overlap)
1816 int may_use_call;
1817 rtx retval = 0;
1818 unsigned int align;
1820 if (is_move_done)
1821 *is_move_done = true;
1823 gcc_assert (size);
1824 if (CONST_INT_P (size) && INTVAL (size) == 0)
1825 return 0;
1827 switch (method)
1829 case BLOCK_OP_NORMAL:
1830 case BLOCK_OP_TAILCALL:
1831 may_use_call = 1;
1832 break;
1834 case BLOCK_OP_CALL_PARM:
1835 may_use_call = block_move_libcall_safe_for_call_parm ();
1837 /* Make inhibit_defer_pop nonzero around the library call
1838 to force it to pop the arguments right away. */
1839 NO_DEFER_POP;
1840 break;
1842 case BLOCK_OP_NO_LIBCALL:
1843 may_use_call = 0;
1844 break;
1846 case BLOCK_OP_NO_LIBCALL_RET:
1847 may_use_call = -1;
1848 break;
1850 default:
1851 gcc_unreachable ();
1854 gcc_assert (MEM_P (x) && MEM_P (y));
1855 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1856 gcc_assert (align >= BITS_PER_UNIT);
1858 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1859 block copy is more efficient for other large modes, e.g. DCmode. */
1860 x = adjust_address (x, BLKmode, 0);
1861 y = adjust_address (y, BLKmode, 0);
1863 /* If source and destination are the same, no need to copy anything. */
1864 if (rtx_equal_p (x, y)
1865 && !MEM_VOLATILE_P (x)
1866 && !MEM_VOLATILE_P (y))
1867 return 0;
1869 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1870 can be incorrect is coming from __builtin_memcpy. */
1871 poly_int64 const_size;
1872 if (poly_int_rtx_p (size, &const_size))
1874 x = shallow_copy_rtx (x);
1875 y = shallow_copy_rtx (y);
1876 set_mem_size (x, const_size);
1877 set_mem_size (y, const_size);
1880 bool pieces_ok = CONST_INT_P (size)
1881 && can_move_by_pieces (INTVAL (size), align);
1882 bool pattern_ok = false;
1884 if (!pieces_ok || might_overlap)
1886 pattern_ok
1887 = emit_block_move_via_pattern (x, y, size, align,
1888 expected_align, expected_size,
1889 min_size, max_size, probable_max_size,
1890 might_overlap);
1891 if (!pattern_ok && might_overlap)
1893 /* Do not try any of the other methods below as they are not safe
1894 for overlapping moves. */
1895 *is_move_done = false;
1896 return retval;
1900 if (pattern_ok)
1902 else if (pieces_ok)
1903 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1904 else if (may_use_call && !might_overlap
1905 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1906 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1908 if (bail_out_libcall)
1910 if (is_move_done)
1911 *is_move_done = false;
1912 return retval;
1915 if (may_use_call < 0)
1916 return pc_rtx;
1918 retval = emit_block_copy_via_libcall (x, y, size,
1919 method == BLOCK_OP_TAILCALL);
1921 else if (might_overlap)
1922 *is_move_done = false;
1923 else
1924 emit_block_move_via_loop (x, y, size, align);
1926 if (method == BLOCK_OP_CALL_PARM)
1927 OK_DEFER_POP;
1929 return retval;
1933 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1935 unsigned HOST_WIDE_INT max, min = 0;
1936 if (GET_CODE (size) == CONST_INT)
1937 min = max = UINTVAL (size);
1938 else
1939 max = GET_MODE_MASK (GET_MODE (size));
1940 return emit_block_move_hints (x, y, size, method, 0, -1,
1941 min, max, max);
1944 /* A subroutine of emit_block_move. Returns true if calling the
1945 block move libcall will not clobber any parameters which may have
1946 already been placed on the stack. */
1948 static bool
1949 block_move_libcall_safe_for_call_parm (void)
1951 tree fn;
1953 /* If arguments are pushed on the stack, then they're safe. */
1954 if (targetm.calls.push_argument (0))
1955 return true;
1957 /* If registers go on the stack anyway, any argument is sure to clobber
1958 an outgoing argument. */
1959 #if defined (REG_PARM_STACK_SPACE)
1960 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1961 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1962 depend on its argument. */
1963 (void) fn;
1964 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1965 && REG_PARM_STACK_SPACE (fn) != 0)
1966 return false;
1967 #endif
1969 /* If any argument goes in memory, then it might clobber an outgoing
1970 argument. */
1972 CUMULATIVE_ARGS args_so_far_v;
1973 cumulative_args_t args_so_far;
1974 tree arg;
1976 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1977 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1978 args_so_far = pack_cumulative_args (&args_so_far_v);
1980 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1981 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1983 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1984 function_arg_info arg_info (mode, /*named=*/true);
1985 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1986 if (!tmp || !REG_P (tmp))
1987 return false;
1988 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1989 return false;
1990 targetm.calls.function_arg_advance (args_so_far, arg_info);
1993 return true;
1996 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1997 return true if successful.
1999 X is the destination of the copy or move.
2000 Y is the source of the copy or move.
2001 SIZE is the size of the block to be moved.
2003 MIGHT_OVERLAP indicates this originated with expansion of a
2004 builtin_memmove() and the source and destination blocks may
2005 overlap.
2008 static bool
2009 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2010 unsigned int expected_align,
2011 HOST_WIDE_INT expected_size,
2012 unsigned HOST_WIDE_INT min_size,
2013 unsigned HOST_WIDE_INT max_size,
2014 unsigned HOST_WIDE_INT probable_max_size,
2015 bool might_overlap)
2017 if (expected_align < align)
2018 expected_align = align;
2019 if (expected_size != -1)
2021 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2022 expected_size = probable_max_size;
2023 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2024 expected_size = min_size;
2027 /* Since this is a move insn, we don't care about volatility. */
2028 temporary_volatile_ok v (true);
2030 /* Try the most limited insn first, because there's no point
2031 including more than one in the machine description unless
2032 the more limited one has some advantage. */
2034 opt_scalar_int_mode mode_iter;
2035 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2037 scalar_int_mode mode = mode_iter.require ();
2038 enum insn_code code;
2039 if (might_overlap)
2040 code = direct_optab_handler (movmem_optab, mode);
2041 else
2042 code = direct_optab_handler (cpymem_optab, mode);
2044 if (code != CODE_FOR_nothing
2045 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2046 here because if SIZE is less than the mode mask, as it is
2047 returned by the macro, it will definitely be less than the
2048 actual mode mask. Since SIZE is within the Pmode address
2049 space, we limit MODE to Pmode. */
2050 && ((CONST_INT_P (size)
2051 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2052 <= (GET_MODE_MASK (mode) >> 1)))
2053 || max_size <= (GET_MODE_MASK (mode) >> 1)
2054 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2056 class expand_operand ops[9];
2057 unsigned int nops;
2059 /* ??? When called via emit_block_move_for_call, it'd be
2060 nice if there were some way to inform the backend, so
2061 that it doesn't fail the expansion because it thinks
2062 emitting the libcall would be more efficient. */
2063 nops = insn_data[(int) code].n_generator_args;
2064 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2066 create_fixed_operand (&ops[0], x);
2067 create_fixed_operand (&ops[1], y);
2068 /* The check above guarantees that this size conversion is valid. */
2069 create_convert_operand_to (&ops[2], size, mode, true);
2070 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2071 if (nops >= 6)
2073 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2074 create_integer_operand (&ops[5], expected_size);
2076 if (nops >= 8)
2078 create_integer_operand (&ops[6], min_size);
2079 /* If we cannot represent the maximal size,
2080 make parameter NULL. */
2081 if ((HOST_WIDE_INT) max_size != -1)
2082 create_integer_operand (&ops[7], max_size);
2083 else
2084 create_fixed_operand (&ops[7], NULL);
2086 if (nops == 9)
2088 /* If we cannot represent the maximal size,
2089 make parameter NULL. */
2090 if ((HOST_WIDE_INT) probable_max_size != -1)
2091 create_integer_operand (&ops[8], probable_max_size);
2092 else
2093 create_fixed_operand (&ops[8], NULL);
2095 if (maybe_expand_insn (code, nops, ops))
2096 return true;
2100 return false;
2103 /* A subroutine of emit_block_move. Copy the data via an explicit
2104 loop. This is used only when libcalls are forbidden. */
2105 /* ??? It'd be nice to copy in hunks larger than QImode. */
2107 static void
2108 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2109 unsigned int align ATTRIBUTE_UNUSED)
2111 rtx_code_label *cmp_label, *top_label;
2112 rtx iter, x_addr, y_addr, tmp;
2113 machine_mode x_addr_mode = get_address_mode (x);
2114 machine_mode y_addr_mode = get_address_mode (y);
2115 machine_mode iter_mode;
2117 iter_mode = GET_MODE (size);
2118 if (iter_mode == VOIDmode)
2119 iter_mode = word_mode;
2121 top_label = gen_label_rtx ();
2122 cmp_label = gen_label_rtx ();
2123 iter = gen_reg_rtx (iter_mode);
2125 emit_move_insn (iter, const0_rtx);
2127 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2128 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2129 do_pending_stack_adjust ();
2131 emit_jump (cmp_label);
2132 emit_label (top_label);
2134 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2135 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2137 if (x_addr_mode != y_addr_mode)
2138 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2139 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2141 x = change_address (x, QImode, x_addr);
2142 y = change_address (y, QImode, y_addr);
2144 emit_move_insn (x, y);
2146 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2147 true, OPTAB_LIB_WIDEN);
2148 if (tmp != iter)
2149 emit_move_insn (iter, tmp);
2151 emit_label (cmp_label);
2153 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2154 true, top_label,
2155 profile_probability::guessed_always ()
2156 .apply_scale (9, 10));
2159 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2160 TAILCALL is true if this is a tail call. */
2163 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2164 rtx size, bool tailcall)
2166 rtx dst_addr, src_addr;
2167 tree call_expr, dst_tree, src_tree, size_tree;
2168 machine_mode size_mode;
2170 /* Since dst and src are passed to a libcall, mark the corresponding
2171 tree EXPR as addressable. */
2172 tree dst_expr = MEM_EXPR (dst);
2173 tree src_expr = MEM_EXPR (src);
2174 if (dst_expr)
2175 mark_addressable (dst_expr);
2176 if (src_expr)
2177 mark_addressable (src_expr);
2179 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2180 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2181 dst_tree = make_tree (ptr_type_node, dst_addr);
2183 src_addr = copy_addr_to_reg (XEXP (src, 0));
2184 src_addr = convert_memory_address (ptr_mode, src_addr);
2185 src_tree = make_tree (ptr_type_node, src_addr);
2187 size_mode = TYPE_MODE (sizetype);
2188 size = convert_to_mode (size_mode, size, 1);
2189 size = copy_to_mode_reg (size_mode, size);
2190 size_tree = make_tree (sizetype, size);
2192 /* It is incorrect to use the libcall calling conventions for calls to
2193 memcpy/memmove/memcmp because they can be provided by the user. */
2194 tree fn = builtin_decl_implicit (fncode);
2195 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2196 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2198 return expand_call (call_expr, NULL_RTX, false);
2201 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2202 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2203 otherwise return null. */
2206 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2207 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2208 HOST_WIDE_INT align)
2210 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2212 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2213 target = NULL_RTX;
2215 class expand_operand ops[5];
2216 create_output_operand (&ops[0], target, insn_mode);
2217 create_fixed_operand (&ops[1], arg1_rtx);
2218 create_fixed_operand (&ops[2], arg2_rtx);
2219 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2220 TYPE_UNSIGNED (arg3_type));
2221 create_integer_operand (&ops[4], align);
2222 if (maybe_expand_insn (icode, 5, ops))
2223 return ops[0].value;
2224 return NULL_RTX;
2227 /* Expand a block compare between X and Y with length LEN using the
2228 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2229 of the expression that was used to calculate the length. ALIGN
2230 gives the known minimum common alignment. */
2232 static rtx
2233 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2234 unsigned align)
2236 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2237 implementing memcmp because it will stop if it encounters two
2238 zero bytes. */
2239 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2241 if (icode == CODE_FOR_nothing)
2242 return NULL_RTX;
2244 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2247 /* Emit code to compare a block Y to a block X. This may be done with
2248 string-compare instructions, with multiple scalar instructions,
2249 or with a library call.
2251 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2252 they are. LEN_TYPE is the type of the expression that was used to
2253 calculate it.
2255 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2256 value of a normal memcmp call, instead we can just compare for equality.
2257 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2258 returning NULL_RTX.
2260 Optionally, the caller can pass a constfn and associated data in Y_CFN
2261 and Y_CFN_DATA. describing that the second operand being compared is a
2262 known constant and how to obtain its data.
2263 Return the result of the comparison, or NULL_RTX if we failed to
2264 perform the operation. */
2267 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2268 bool equality_only, by_pieces_constfn y_cfn,
2269 void *y_cfndata)
2271 rtx result = 0;
2273 if (CONST_INT_P (len) && INTVAL (len) == 0)
2274 return const0_rtx;
2276 gcc_assert (MEM_P (x) && MEM_P (y));
2277 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2278 gcc_assert (align >= BITS_PER_UNIT);
2280 x = adjust_address (x, BLKmode, 0);
2281 y = adjust_address (y, BLKmode, 0);
2283 if (equality_only
2284 && CONST_INT_P (len)
2285 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2286 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2287 y_cfn, y_cfndata);
2288 else
2289 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2291 return result;
2294 /* Copy all or part of a value X into registers starting at REGNO.
2295 The number of registers to be filled is NREGS. */
2297 void
2298 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2300 if (nregs == 0)
2301 return;
2303 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2304 x = validize_mem (force_const_mem (mode, x));
2306 /* See if the machine can do this with a load multiple insn. */
2307 if (targetm.have_load_multiple ())
2309 rtx_insn *last = get_last_insn ();
2310 rtx first = gen_rtx_REG (word_mode, regno);
2311 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2312 GEN_INT (nregs)))
2314 emit_insn (pat);
2315 return;
2317 else
2318 delete_insns_since (last);
2321 for (int i = 0; i < nregs; i++)
2322 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2323 operand_subword_force (x, i, mode));
2326 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2327 The number of registers to be filled is NREGS. */
2329 void
2330 move_block_from_reg (int regno, rtx x, int nregs)
2332 if (nregs == 0)
2333 return;
2335 /* See if the machine can do this with a store multiple insn. */
2336 if (targetm.have_store_multiple ())
2338 rtx_insn *last = get_last_insn ();
2339 rtx first = gen_rtx_REG (word_mode, regno);
2340 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2341 GEN_INT (nregs)))
2343 emit_insn (pat);
2344 return;
2346 else
2347 delete_insns_since (last);
2350 for (int i = 0; i < nregs; i++)
2352 rtx tem = operand_subword (x, i, 1, BLKmode);
2354 gcc_assert (tem);
2356 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2360 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2361 ORIG, where ORIG is a non-consecutive group of registers represented by
2362 a PARALLEL. The clone is identical to the original except in that the
2363 original set of registers is replaced by a new set of pseudo registers.
2364 The new set has the same modes as the original set. */
2367 gen_group_rtx (rtx orig)
2369 int i, length;
2370 rtx *tmps;
2372 gcc_assert (GET_CODE (orig) == PARALLEL);
2374 length = XVECLEN (orig, 0);
2375 tmps = XALLOCAVEC (rtx, length);
2377 /* Skip a NULL entry in first slot. */
2378 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2380 if (i)
2381 tmps[0] = 0;
2383 for (; i < length; i++)
2385 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2386 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2388 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2391 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2394 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2395 except that values are placed in TMPS[i], and must later be moved
2396 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2398 static void
2399 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2400 poly_int64 ssize)
2402 rtx src;
2403 int start, i;
2404 machine_mode m = GET_MODE (orig_src);
2406 gcc_assert (GET_CODE (dst) == PARALLEL);
2408 if (m != VOIDmode
2409 && !SCALAR_INT_MODE_P (m)
2410 && !MEM_P (orig_src)
2411 && GET_CODE (orig_src) != CONCAT)
2413 scalar_int_mode imode;
2414 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2416 src = gen_reg_rtx (imode);
2417 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2419 else
2421 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2422 emit_move_insn (src, orig_src);
2424 emit_group_load_1 (tmps, dst, src, type, ssize);
2425 return;
2428 /* Check for a NULL entry, used to indicate that the parameter goes
2429 both on the stack and in registers. */
2430 if (XEXP (XVECEXP (dst, 0, 0), 0))
2431 start = 0;
2432 else
2433 start = 1;
2435 /* Process the pieces. */
2436 for (i = start; i < XVECLEN (dst, 0); i++)
2438 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2439 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2440 poly_int64 bytelen = GET_MODE_SIZE (mode);
2441 poly_int64 shift = 0;
2443 /* Handle trailing fragments that run over the size of the struct.
2444 It's the target's responsibility to make sure that the fragment
2445 cannot be strictly smaller in some cases and strictly larger
2446 in others. */
2447 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2448 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2450 /* Arrange to shift the fragment to where it belongs.
2451 extract_bit_field loads to the lsb of the reg. */
2452 if (
2453 #ifdef BLOCK_REG_PADDING
2454 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2455 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2456 #else
2457 BYTES_BIG_ENDIAN
2458 #endif
2460 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2461 bytelen = ssize - bytepos;
2462 gcc_assert (maybe_gt (bytelen, 0));
2465 /* If we won't be loading directly from memory, protect the real source
2466 from strange tricks we might play; but make sure that the source can
2467 be loaded directly into the destination. */
2468 src = orig_src;
2469 if (!MEM_P (orig_src)
2470 && (!CONSTANT_P (orig_src)
2471 || (GET_MODE (orig_src) != mode
2472 && GET_MODE (orig_src) != VOIDmode)))
2474 if (GET_MODE (orig_src) == VOIDmode)
2475 src = gen_reg_rtx (mode);
2476 else
2477 src = gen_reg_rtx (GET_MODE (orig_src));
2479 emit_move_insn (src, orig_src);
2482 /* Optimize the access just a bit. */
2483 if (MEM_P (src)
2484 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2485 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2486 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2487 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2489 tmps[i] = gen_reg_rtx (mode);
2490 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2492 else if (COMPLEX_MODE_P (mode)
2493 && GET_MODE (src) == mode
2494 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2495 /* Let emit_move_complex do the bulk of the work. */
2496 tmps[i] = src;
2497 else if (GET_CODE (src) == CONCAT)
2499 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2500 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2501 unsigned int elt;
2502 poly_int64 subpos;
2504 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2505 && known_le (subpos + bytelen, slen0))
2507 /* The following assumes that the concatenated objects all
2508 have the same size. In this case, a simple calculation
2509 can be used to determine the object and the bit field
2510 to be extracted. */
2511 tmps[i] = XEXP (src, elt);
2512 if (maybe_ne (subpos, 0)
2513 || maybe_ne (subpos + bytelen, slen0)
2514 || (!CONSTANT_P (tmps[i])
2515 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2516 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2517 subpos * BITS_PER_UNIT,
2518 1, NULL_RTX, mode, mode, false,
2519 NULL);
2521 else
2523 rtx mem;
2525 gcc_assert (known_eq (bytepos, 0));
2526 mem = assign_stack_temp (GET_MODE (src), slen);
2527 emit_move_insn (mem, src);
2528 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2529 0, 1, NULL_RTX, mode, mode, false,
2530 NULL);
2533 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2534 && XVECLEN (dst, 0) > 1)
2535 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2536 else if (CONSTANT_P (src))
2538 if (known_eq (bytelen, ssize))
2539 tmps[i] = src;
2540 else
2542 rtx first, second;
2544 /* TODO: const_wide_int can have sizes other than this... */
2545 gcc_assert (known_eq (2 * bytelen, ssize));
2546 split_double (src, &first, &second);
2547 if (i)
2548 tmps[i] = second;
2549 else
2550 tmps[i] = first;
2553 else if (REG_P (src) && GET_MODE (src) == mode)
2554 tmps[i] = src;
2555 else
2556 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2557 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2558 mode, mode, false, NULL);
2560 if (maybe_ne (shift, 0))
2561 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2562 shift, tmps[i], 0);
2566 /* Emit code to move a block SRC of type TYPE to a block DST,
2567 where DST is non-consecutive registers represented by a PARALLEL.
2568 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2569 if not known. */
2571 void
2572 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2574 rtx *tmps;
2575 int i;
2577 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2578 emit_group_load_1 (tmps, dst, src, type, ssize);
2580 /* Copy the extracted pieces into the proper (probable) hard regs. */
2581 for (i = 0; i < XVECLEN (dst, 0); i++)
2583 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2584 if (d == NULL)
2585 continue;
2586 emit_move_insn (d, tmps[i]);
2590 /* Similar, but load SRC into new pseudos in a format that looks like
2591 PARALLEL. This can later be fed to emit_group_move to get things
2592 in the right place. */
2595 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2597 rtvec vec;
2598 int i;
2600 vec = rtvec_alloc (XVECLEN (parallel, 0));
2601 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2603 /* Convert the vector to look just like the original PARALLEL, except
2604 with the computed values. */
2605 for (i = 0; i < XVECLEN (parallel, 0); i++)
2607 rtx e = XVECEXP (parallel, 0, i);
2608 rtx d = XEXP (e, 0);
2610 if (d)
2612 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2613 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2615 RTVEC_ELT (vec, i) = e;
2618 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2621 /* Emit code to move a block SRC to block DST, where SRC and DST are
2622 non-consecutive groups of registers, each represented by a PARALLEL. */
2624 void
2625 emit_group_move (rtx dst, rtx src)
2627 int i;
2629 gcc_assert (GET_CODE (src) == PARALLEL
2630 && GET_CODE (dst) == PARALLEL
2631 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2633 /* Skip first entry if NULL. */
2634 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2635 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2636 XEXP (XVECEXP (src, 0, i), 0));
2639 /* Move a group of registers represented by a PARALLEL into pseudos. */
2642 emit_group_move_into_temps (rtx src)
2644 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2645 int i;
2647 for (i = 0; i < XVECLEN (src, 0); i++)
2649 rtx e = XVECEXP (src, 0, i);
2650 rtx d = XEXP (e, 0);
2652 if (d)
2653 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2654 RTVEC_ELT (vec, i) = e;
2657 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2660 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2661 where SRC is non-consecutive registers represented by a PARALLEL.
2662 SSIZE represents the total size of block ORIG_DST, or -1 if not
2663 known. */
2665 void
2666 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2667 poly_int64 ssize)
2669 rtx *tmps, dst;
2670 int start, finish, i;
2671 machine_mode m = GET_MODE (orig_dst);
2673 gcc_assert (GET_CODE (src) == PARALLEL);
2675 if (!SCALAR_INT_MODE_P (m)
2676 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2678 scalar_int_mode imode;
2679 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2681 dst = gen_reg_rtx (imode);
2682 emit_group_store (dst, src, type, ssize);
2683 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2685 else
2687 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2688 emit_group_store (dst, src, type, ssize);
2690 emit_move_insn (orig_dst, dst);
2691 return;
2694 /* Check for a NULL entry, used to indicate that the parameter goes
2695 both on the stack and in registers. */
2696 if (XEXP (XVECEXP (src, 0, 0), 0))
2697 start = 0;
2698 else
2699 start = 1;
2700 finish = XVECLEN (src, 0);
2702 tmps = XALLOCAVEC (rtx, finish);
2704 /* Copy the (probable) hard regs into pseudos. */
2705 for (i = start; i < finish; i++)
2707 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2708 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2710 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2711 emit_move_insn (tmps[i], reg);
2713 else
2714 tmps[i] = reg;
2717 /* If we won't be storing directly into memory, protect the real destination
2718 from strange tricks we might play. */
2719 dst = orig_dst;
2720 if (GET_CODE (dst) == PARALLEL)
2722 rtx temp;
2724 /* We can get a PARALLEL dst if there is a conditional expression in
2725 a return statement. In that case, the dst and src are the same,
2726 so no action is necessary. */
2727 if (rtx_equal_p (dst, src))
2728 return;
2730 /* It is unclear if we can ever reach here, but we may as well handle
2731 it. Allocate a temporary, and split this into a store/load to/from
2732 the temporary. */
2733 temp = assign_stack_temp (GET_MODE (dst), ssize);
2734 emit_group_store (temp, src, type, ssize);
2735 emit_group_load (dst, temp, type, ssize);
2736 return;
2738 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2740 machine_mode outer = GET_MODE (dst);
2741 machine_mode inner;
2742 poly_int64 bytepos;
2743 bool done = false;
2744 rtx temp;
2746 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2747 dst = gen_reg_rtx (outer);
2749 /* Make life a bit easier for combine. */
2750 /* If the first element of the vector is the low part
2751 of the destination mode, use a paradoxical subreg to
2752 initialize the destination. */
2753 if (start < finish)
2755 inner = GET_MODE (tmps[start]);
2756 bytepos = subreg_lowpart_offset (inner, outer);
2757 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2758 bytepos))
2760 temp = simplify_gen_subreg (outer, tmps[start],
2761 inner, 0);
2762 if (temp)
2764 emit_move_insn (dst, temp);
2765 done = true;
2766 start++;
2771 /* If the first element wasn't the low part, try the last. */
2772 if (!done
2773 && start < finish - 1)
2775 inner = GET_MODE (tmps[finish - 1]);
2776 bytepos = subreg_lowpart_offset (inner, outer);
2777 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2778 finish - 1), 1)),
2779 bytepos))
2781 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2782 inner, 0);
2783 if (temp)
2785 emit_move_insn (dst, temp);
2786 done = true;
2787 finish--;
2792 /* Otherwise, simply initialize the result to zero. */
2793 if (!done)
2794 emit_move_insn (dst, CONST0_RTX (outer));
2797 /* Process the pieces. */
2798 for (i = start; i < finish; i++)
2800 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2801 machine_mode mode = GET_MODE (tmps[i]);
2802 poly_int64 bytelen = GET_MODE_SIZE (mode);
2803 poly_uint64 adj_bytelen;
2804 rtx dest = dst;
2806 /* Handle trailing fragments that run over the size of the struct.
2807 It's the target's responsibility to make sure that the fragment
2808 cannot be strictly smaller in some cases and strictly larger
2809 in others. */
2810 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2811 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2812 adj_bytelen = ssize - bytepos;
2813 else
2814 adj_bytelen = bytelen;
2816 if (GET_CODE (dst) == CONCAT)
2818 if (known_le (bytepos + adj_bytelen,
2819 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2820 dest = XEXP (dst, 0);
2821 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2823 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2824 dest = XEXP (dst, 1);
2826 else
2828 machine_mode dest_mode = GET_MODE (dest);
2829 machine_mode tmp_mode = GET_MODE (tmps[i]);
2830 scalar_int_mode imode;
2832 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2834 if (finish == 1
2835 && REG_P (tmps[i])
2836 && COMPLEX_MODE_P (dest_mode)
2837 && SCALAR_INT_MODE_P (tmp_mode)
2838 && int_mode_for_mode (dest_mode).exists (&imode))
2840 if (tmp_mode != imode)
2842 rtx tmp = gen_reg_rtx (imode);
2843 emit_move_insn (tmp, gen_lowpart (imode, tmps[i]));
2844 dst = gen_lowpart (dest_mode, tmp);
2846 else
2847 dst = gen_lowpart (dest_mode, tmps[i]);
2849 else if (GET_MODE_ALIGNMENT (dest_mode)
2850 >= GET_MODE_ALIGNMENT (tmp_mode))
2852 dest = assign_stack_temp (dest_mode,
2853 GET_MODE_SIZE (dest_mode));
2854 emit_move_insn (adjust_address (dest,
2855 tmp_mode,
2856 bytepos),
2857 tmps[i]);
2858 dst = dest;
2860 else
2862 dest = assign_stack_temp (tmp_mode,
2863 GET_MODE_SIZE (tmp_mode));
2864 emit_move_insn (dest, tmps[i]);
2865 dst = adjust_address (dest, dest_mode, bytepos);
2867 break;
2871 /* Handle trailing fragments that run over the size of the struct. */
2872 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2874 /* store_bit_field always takes its value from the lsb.
2875 Move the fragment to the lsb if it's not already there. */
2876 if (
2877 #ifdef BLOCK_REG_PADDING
2878 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2879 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2880 #else
2881 BYTES_BIG_ENDIAN
2882 #endif
2885 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2886 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2887 shift, tmps[i], 0);
2890 /* Make sure not to write past the end of the struct. */
2891 store_bit_field (dest,
2892 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2893 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2894 VOIDmode, tmps[i], false, false);
2897 /* Optimize the access just a bit. */
2898 else if (MEM_P (dest)
2899 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2900 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2901 && multiple_p (bytepos * BITS_PER_UNIT,
2902 GET_MODE_ALIGNMENT (mode))
2903 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2904 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2906 else
2907 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2908 0, 0, mode, tmps[i], false, false);
2911 /* Copy from the pseudo into the (probable) hard reg. */
2912 if (orig_dst != dst)
2913 emit_move_insn (orig_dst, dst);
2916 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2917 of the value stored in X. */
2920 maybe_emit_group_store (rtx x, tree type)
2922 machine_mode mode = TYPE_MODE (type);
2923 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2924 if (GET_CODE (x) == PARALLEL)
2926 rtx result = gen_reg_rtx (mode);
2927 emit_group_store (result, x, type, int_size_in_bytes (type));
2928 return result;
2930 return x;
2933 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2935 This is used on targets that return BLKmode values in registers. */
2937 static void
2938 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2940 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2941 rtx src = NULL, dst = NULL;
2942 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2943 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2944 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2945 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2946 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2947 fixed_size_mode copy_mode;
2949 /* BLKmode registers created in the back-end shouldn't have survived. */
2950 gcc_assert (mode != BLKmode);
2952 /* If the structure doesn't take up a whole number of words, see whether
2953 SRCREG is padded on the left or on the right. If it's on the left,
2954 set PADDING_CORRECTION to the number of bits to skip.
2956 In most ABIs, the structure will be returned at the least end of
2957 the register, which translates to right padding on little-endian
2958 targets and left padding on big-endian targets. The opposite
2959 holds if the structure is returned at the most significant
2960 end of the register. */
2961 if (bytes % UNITS_PER_WORD != 0
2962 && (targetm.calls.return_in_msb (type)
2963 ? !BYTES_BIG_ENDIAN
2964 : BYTES_BIG_ENDIAN))
2965 padding_correction
2966 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2968 /* We can use a single move if we have an exact mode for the size. */
2969 else if (MEM_P (target)
2970 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2971 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2972 && bytes == GET_MODE_SIZE (mode))
2974 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2975 return;
2978 /* And if we additionally have the same mode for a register. */
2979 else if (REG_P (target)
2980 && GET_MODE (target) == mode
2981 && bytes == GET_MODE_SIZE (mode))
2983 emit_move_insn (target, srcreg);
2984 return;
2987 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2988 into a new pseudo which is a full word. */
2989 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2991 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2992 mode = word_mode;
2995 /* Copy the structure BITSIZE bits at a time. If the target lives in
2996 memory, take care of not reading/writing past its end by selecting
2997 a copy mode suited to BITSIZE. This should always be possible given
2998 how it is computed.
3000 If the target lives in register, make sure not to select a copy mode
3001 larger than the mode of the register.
3003 We could probably emit more efficient code for machines which do not use
3004 strict alignment, but it doesn't seem worth the effort at the current
3005 time. */
3007 copy_mode = word_mode;
3008 if (MEM_P (target))
3010 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3011 if (mem_mode.exists ())
3012 copy_mode = mem_mode.require ();
3014 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3015 copy_mode = tmode;
3017 for (bitpos = 0, xbitpos = padding_correction;
3018 bitpos < bytes * BITS_PER_UNIT;
3019 bitpos += bitsize, xbitpos += bitsize)
3021 /* We need a new source operand each time xbitpos is on a
3022 word boundary and when xbitpos == padding_correction
3023 (the first time through). */
3024 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3025 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3027 /* We need a new destination operand each time bitpos is on
3028 a word boundary. */
3029 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3030 dst = target;
3031 else if (bitpos % BITS_PER_WORD == 0)
3032 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3034 /* Use xbitpos for the source extraction (right justified) and
3035 bitpos for the destination store (left justified). */
3036 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3037 extract_bit_field (src, bitsize,
3038 xbitpos % BITS_PER_WORD, 1,
3039 NULL_RTX, copy_mode, copy_mode,
3040 false, NULL),
3041 false, false);
3045 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3046 register if it contains any data, otherwise return null.
3048 This is used on targets that return BLKmode values in registers. */
3051 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3053 int i, n_regs;
3054 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3055 unsigned int bitsize;
3056 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3057 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3058 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3059 fixed_size_mode dst_mode;
3060 scalar_int_mode min_mode;
3062 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3064 x = expand_normal (src);
3066 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3067 if (bytes == 0)
3068 return NULL_RTX;
3070 /* If the structure doesn't take up a whole number of words, see
3071 whether the register value should be padded on the left or on
3072 the right. Set PADDING_CORRECTION to the number of padding
3073 bits needed on the left side.
3075 In most ABIs, the structure will be returned at the least end of
3076 the register, which translates to right padding on little-endian
3077 targets and left padding on big-endian targets. The opposite
3078 holds if the structure is returned at the most significant
3079 end of the register. */
3080 if (bytes % UNITS_PER_WORD != 0
3081 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3082 ? !BYTES_BIG_ENDIAN
3083 : BYTES_BIG_ENDIAN))
3084 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3085 * BITS_PER_UNIT));
3087 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3088 dst_words = XALLOCAVEC (rtx, n_regs);
3089 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3090 min_mode = smallest_int_mode_for_size (bitsize);
3092 /* Copy the structure BITSIZE bits at a time. */
3093 for (bitpos = 0, xbitpos = padding_correction;
3094 bitpos < bytes * BITS_PER_UNIT;
3095 bitpos += bitsize, xbitpos += bitsize)
3097 /* We need a new destination pseudo each time xbitpos is
3098 on a word boundary and when xbitpos == padding_correction
3099 (the first time through). */
3100 if (xbitpos % BITS_PER_WORD == 0
3101 || xbitpos == padding_correction)
3103 /* Generate an appropriate register. */
3104 dst_word = gen_reg_rtx (word_mode);
3105 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3107 /* Clear the destination before we move anything into it. */
3108 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3111 /* Find the largest integer mode that can be used to copy all or as
3112 many bits as possible of the structure if the target supports larger
3113 copies. There are too many corner cases here w.r.t to alignments on
3114 the read/writes. So if there is any padding just use single byte
3115 operations. */
3116 opt_scalar_int_mode mode_iter;
3117 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3119 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3121 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3122 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3123 && msize <= BITS_PER_WORD)
3124 bitsize = msize;
3125 else
3126 break;
3130 /* We need a new source operand each time bitpos is on a word
3131 boundary. */
3132 if (bitpos % BITS_PER_WORD == 0)
3133 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3135 /* Use bitpos for the source extraction (left justified) and
3136 xbitpos for the destination store (right justified). */
3137 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3138 0, 0, word_mode,
3139 extract_bit_field (src_word, bitsize,
3140 bitpos % BITS_PER_WORD, 1,
3141 NULL_RTX, word_mode, word_mode,
3142 false, NULL),
3143 false, false);
3146 if (mode == BLKmode)
3148 /* Find the smallest integer mode large enough to hold the
3149 entire structure. */
3150 opt_scalar_int_mode mode_iter;
3151 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3152 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3153 break;
3155 /* A suitable mode should have been found. */
3156 mode = mode_iter.require ();
3159 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3160 dst_mode = word_mode;
3161 else
3162 dst_mode = mode;
3163 dst = gen_reg_rtx (dst_mode);
3165 for (i = 0; i < n_regs; i++)
3166 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3168 if (mode != dst_mode)
3169 dst = gen_lowpart (mode, dst);
3171 return dst;
3174 /* Add a USE expression for REG to the (possibly empty) list pointed
3175 to by CALL_FUSAGE. REG must denote a hard register. */
3177 void
3178 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3180 gcc_assert (REG_P (reg));
3182 if (!HARD_REGISTER_P (reg))
3183 return;
3185 *call_fusage
3186 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3189 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3190 to by CALL_FUSAGE. REG must denote a hard register. */
3192 void
3193 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3195 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3197 *call_fusage
3198 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3201 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3202 starting at REGNO. All of these registers must be hard registers. */
3204 void
3205 use_regs (rtx *call_fusage, int regno, int nregs)
3207 int i;
3209 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3211 for (i = 0; i < nregs; i++)
3212 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3215 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3216 PARALLEL REGS. This is for calls that pass values in multiple
3217 non-contiguous locations. The Irix 6 ABI has examples of this. */
3219 void
3220 use_group_regs (rtx *call_fusage, rtx regs)
3222 int i;
3224 for (i = 0; i < XVECLEN (regs, 0); i++)
3226 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3228 /* A NULL entry means the parameter goes both on the stack and in
3229 registers. This can also be a MEM for targets that pass values
3230 partially on the stack and partially in registers. */
3231 if (reg != 0 && REG_P (reg))
3232 use_reg (call_fusage, reg);
3236 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3237 assigment and the code of the expresion on the RHS is CODE. Return
3238 NULL otherwise. */
3240 static gimple *
3241 get_def_for_expr (tree name, enum tree_code code)
3243 gimple *def_stmt;
3245 if (TREE_CODE (name) != SSA_NAME)
3246 return NULL;
3248 def_stmt = get_gimple_for_ssa_name (name);
3249 if (!def_stmt
3250 || gimple_assign_rhs_code (def_stmt) != code)
3251 return NULL;
3253 return def_stmt;
3256 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3257 assigment and the class of the expresion on the RHS is CLASS. Return
3258 NULL otherwise. */
3260 static gimple *
3261 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3263 gimple *def_stmt;
3265 if (TREE_CODE (name) != SSA_NAME)
3266 return NULL;
3268 def_stmt = get_gimple_for_ssa_name (name);
3269 if (!def_stmt
3270 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3271 return NULL;
3273 return def_stmt;
3276 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3277 its length in bytes. */
3280 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3281 unsigned int expected_align, HOST_WIDE_INT expected_size,
3282 unsigned HOST_WIDE_INT min_size,
3283 unsigned HOST_WIDE_INT max_size,
3284 unsigned HOST_WIDE_INT probable_max_size,
3285 unsigned ctz_size)
3287 machine_mode mode = GET_MODE (object);
3288 unsigned int align;
3290 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3292 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3293 just move a zero. Otherwise, do this a piece at a time. */
3294 poly_int64 size_val;
3295 if (mode != BLKmode
3296 && poly_int_rtx_p (size, &size_val)
3297 && known_eq (size_val, GET_MODE_SIZE (mode)))
3299 rtx zero = CONST0_RTX (mode);
3300 if (zero != NULL)
3302 emit_move_insn (object, zero);
3303 return NULL;
3306 if (COMPLEX_MODE_P (mode))
3308 zero = CONST0_RTX (GET_MODE_INNER (mode));
3309 if (zero != NULL)
3311 write_complex_part (object, zero, 0, true);
3312 write_complex_part (object, zero, 1, false);
3313 return NULL;
3318 if (size == const0_rtx)
3319 return NULL;
3321 align = MEM_ALIGN (object);
3323 if (CONST_INT_P (size)
3324 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3325 CLEAR_BY_PIECES,
3326 optimize_insn_for_speed_p ()))
3327 clear_by_pieces (object, INTVAL (size), align);
3328 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3329 expected_align, expected_size,
3330 min_size, max_size, probable_max_size))
3332 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3333 min_size, max_size,
3334 NULL_RTX, 0, align))
3336 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3337 return set_storage_via_libcall (object, size, const0_rtx,
3338 method == BLOCK_OP_TAILCALL);
3339 else
3340 gcc_unreachable ();
3342 return NULL;
3346 clear_storage (rtx object, rtx size, enum block_op_methods method)
3348 unsigned HOST_WIDE_INT max, min = 0;
3349 if (GET_CODE (size) == CONST_INT)
3350 min = max = UINTVAL (size);
3351 else
3352 max = GET_MODE_MASK (GET_MODE (size));
3353 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3357 /* A subroutine of clear_storage. Expand a call to memset.
3358 Return the return value of memset, 0 otherwise. */
3361 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3363 tree call_expr, fn, object_tree, size_tree, val_tree;
3364 machine_mode size_mode;
3366 object = copy_addr_to_reg (XEXP (object, 0));
3367 object_tree = make_tree (ptr_type_node, object);
3369 if (!CONST_INT_P (val))
3370 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3371 val_tree = make_tree (integer_type_node, val);
3373 size_mode = TYPE_MODE (sizetype);
3374 size = convert_to_mode (size_mode, size, 1);
3375 size = copy_to_mode_reg (size_mode, size);
3376 size_tree = make_tree (sizetype, size);
3378 /* It is incorrect to use the libcall calling conventions for calls to
3379 memset because it can be provided by the user. */
3380 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3381 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3382 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3384 return expand_call (call_expr, NULL_RTX, false);
3387 /* Expand a setmem pattern; return true if successful. */
3389 bool
3390 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3391 unsigned int expected_align, HOST_WIDE_INT expected_size,
3392 unsigned HOST_WIDE_INT min_size,
3393 unsigned HOST_WIDE_INT max_size,
3394 unsigned HOST_WIDE_INT probable_max_size)
3396 /* Try the most limited insn first, because there's no point
3397 including more than one in the machine description unless
3398 the more limited one has some advantage. */
3400 if (expected_align < align)
3401 expected_align = align;
3402 if (expected_size != -1)
3404 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3405 expected_size = max_size;
3406 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3407 expected_size = min_size;
3410 opt_scalar_int_mode mode_iter;
3411 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3413 scalar_int_mode mode = mode_iter.require ();
3414 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3416 if (code != CODE_FOR_nothing
3417 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3418 here because if SIZE is less than the mode mask, as it is
3419 returned by the macro, it will definitely be less than the
3420 actual mode mask. Since SIZE is within the Pmode address
3421 space, we limit MODE to Pmode. */
3422 && ((CONST_INT_P (size)
3423 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3424 <= (GET_MODE_MASK (mode) >> 1)))
3425 || max_size <= (GET_MODE_MASK (mode) >> 1)
3426 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3428 class expand_operand ops[9];
3429 unsigned int nops;
3431 nops = insn_data[(int) code].n_generator_args;
3432 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3434 create_fixed_operand (&ops[0], object);
3435 /* The check above guarantees that this size conversion is valid. */
3436 create_convert_operand_to (&ops[1], size, mode, true);
3437 create_convert_operand_from (&ops[2], val, byte_mode, true);
3438 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3439 if (nops >= 6)
3441 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3442 create_integer_operand (&ops[5], expected_size);
3444 if (nops >= 8)
3446 create_integer_operand (&ops[6], min_size);
3447 /* If we cannot represent the maximal size,
3448 make parameter NULL. */
3449 if ((HOST_WIDE_INT) max_size != -1)
3450 create_integer_operand (&ops[7], max_size);
3451 else
3452 create_fixed_operand (&ops[7], NULL);
3454 if (nops == 9)
3456 /* If we cannot represent the maximal size,
3457 make parameter NULL. */
3458 if ((HOST_WIDE_INT) probable_max_size != -1)
3459 create_integer_operand (&ops[8], probable_max_size);
3460 else
3461 create_fixed_operand (&ops[8], NULL);
3463 if (maybe_expand_insn (code, nops, ops))
3464 return true;
3468 return false;
3472 /* Write to one of the components of the complex value CPLX. Write VAL to
3473 the real part if IMAG_P is false, and the imaginary part if its true.
3474 If UNDEFINED_P then the value in CPLX is currently undefined. */
3476 void
3477 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
3479 machine_mode cmode;
3480 scalar_mode imode;
3481 unsigned ibitsize;
3483 if (GET_CODE (cplx) == CONCAT)
3485 emit_move_insn (XEXP (cplx, imag_p), val);
3486 return;
3489 cmode = GET_MODE (cplx);
3490 imode = GET_MODE_INNER (cmode);
3491 ibitsize = GET_MODE_BITSIZE (imode);
3493 /* For MEMs simplify_gen_subreg may generate an invalid new address
3494 because, e.g., the original address is considered mode-dependent
3495 by the target, which restricts simplify_subreg from invoking
3496 adjust_address_nv. Instead of preparing fallback support for an
3497 invalid address, we call adjust_address_nv directly. */
3498 if (MEM_P (cplx))
3500 emit_move_insn (adjust_address_nv (cplx, imode,
3501 imag_p ? GET_MODE_SIZE (imode) : 0),
3502 val);
3503 return;
3506 /* If the sub-object is at least word sized, then we know that subregging
3507 will work. This special case is important, since store_bit_field
3508 wants to operate on integer modes, and there's rarely an OImode to
3509 correspond to TCmode. */
3510 if (ibitsize >= BITS_PER_WORD
3511 /* For hard regs we have exact predicates. Assume we can split
3512 the original object if it spans an even number of hard regs.
3513 This special case is important for SCmode on 64-bit platforms
3514 where the natural size of floating-point regs is 32-bit. */
3515 || (REG_P (cplx)
3516 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3517 && REG_NREGS (cplx) % 2 == 0))
3519 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3520 imag_p ? GET_MODE_SIZE (imode) : 0);
3521 if (part)
3523 emit_move_insn (part, val);
3524 return;
3526 else
3527 /* simplify_gen_subreg may fail for sub-word MEMs. */
3528 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3531 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3532 false, undefined_p);
3535 /* Extract one of the components of the complex value CPLX. Extract the
3536 real part if IMAG_P is false, and the imaginary part if it's true. */
3539 read_complex_part (rtx cplx, bool imag_p)
3541 machine_mode cmode;
3542 scalar_mode imode;
3543 unsigned ibitsize;
3545 if (GET_CODE (cplx) == CONCAT)
3546 return XEXP (cplx, imag_p);
3548 cmode = GET_MODE (cplx);
3549 imode = GET_MODE_INNER (cmode);
3550 ibitsize = GET_MODE_BITSIZE (imode);
3552 /* Special case reads from complex constants that got spilled to memory. */
3553 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3555 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3556 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3558 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3559 if (CONSTANT_CLASS_P (part))
3560 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3564 /* For MEMs simplify_gen_subreg may generate an invalid new address
3565 because, e.g., the original address is considered mode-dependent
3566 by the target, which restricts simplify_subreg from invoking
3567 adjust_address_nv. Instead of preparing fallback support for an
3568 invalid address, we call adjust_address_nv directly. */
3569 if (MEM_P (cplx))
3570 return adjust_address_nv (cplx, imode,
3571 imag_p ? GET_MODE_SIZE (imode) : 0);
3573 /* If the sub-object is at least word sized, then we know that subregging
3574 will work. This special case is important, since extract_bit_field
3575 wants to operate on integer modes, and there's rarely an OImode to
3576 correspond to TCmode. */
3577 if (ibitsize >= BITS_PER_WORD
3578 /* For hard regs we have exact predicates. Assume we can split
3579 the original object if it spans an even number of hard regs.
3580 This special case is important for SCmode on 64-bit platforms
3581 where the natural size of floating-point regs is 32-bit. */
3582 || (REG_P (cplx)
3583 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3584 && REG_NREGS (cplx) % 2 == 0))
3586 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3587 imag_p ? GET_MODE_SIZE (imode) : 0);
3588 if (ret)
3589 return ret;
3590 else
3591 /* simplify_gen_subreg may fail for sub-word MEMs. */
3592 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3595 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3596 true, NULL_RTX, imode, imode, false, NULL);
3599 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3600 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3601 represented in NEW_MODE. If FORCE is true, this will never happen, as
3602 we'll force-create a SUBREG if needed. */
3604 static rtx
3605 emit_move_change_mode (machine_mode new_mode,
3606 machine_mode old_mode, rtx x, bool force)
3608 rtx ret;
3610 if (push_operand (x, GET_MODE (x)))
3612 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3613 MEM_COPY_ATTRIBUTES (ret, x);
3615 else if (MEM_P (x))
3617 /* We don't have to worry about changing the address since the
3618 size in bytes is supposed to be the same. */
3619 if (reload_in_progress)
3621 /* Copy the MEM to change the mode and move any
3622 substitutions from the old MEM to the new one. */
3623 ret = adjust_address_nv (x, new_mode, 0);
3624 copy_replacements (x, ret);
3626 else
3627 ret = adjust_address (x, new_mode, 0);
3629 else
3631 /* Note that we do want simplify_subreg's behavior of validating
3632 that the new mode is ok for a hard register. If we were to use
3633 simplify_gen_subreg, we would create the subreg, but would
3634 probably run into the target not being able to implement it. */
3635 /* Except, of course, when FORCE is true, when this is exactly what
3636 we want. Which is needed for CCmodes on some targets. */
3637 if (force)
3638 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3639 else
3640 ret = simplify_subreg (new_mode, x, old_mode, 0);
3643 return ret;
3646 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3647 an integer mode of the same size as MODE. Returns the instruction
3648 emitted, or NULL if such a move could not be generated. */
3650 static rtx_insn *
3651 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3653 scalar_int_mode imode;
3654 enum insn_code code;
3656 /* There must exist a mode of the exact size we require. */
3657 if (!int_mode_for_mode (mode).exists (&imode))
3658 return NULL;
3660 /* The target must support moves in this mode. */
3661 code = optab_handler (mov_optab, imode);
3662 if (code == CODE_FOR_nothing)
3663 return NULL;
3665 x = emit_move_change_mode (imode, mode, x, force);
3666 if (x == NULL_RTX)
3667 return NULL;
3668 y = emit_move_change_mode (imode, mode, y, force);
3669 if (y == NULL_RTX)
3670 return NULL;
3671 return emit_insn (GEN_FCN (code) (x, y));
3674 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3675 Return an equivalent MEM that does not use an auto-increment. */
3678 emit_move_resolve_push (machine_mode mode, rtx x)
3680 enum rtx_code code = GET_CODE (XEXP (x, 0));
3681 rtx temp;
3683 poly_int64 adjust = GET_MODE_SIZE (mode);
3684 #ifdef PUSH_ROUNDING
3685 adjust = PUSH_ROUNDING (adjust);
3686 #endif
3687 if (code == PRE_DEC || code == POST_DEC)
3688 adjust = -adjust;
3689 else if (code == PRE_MODIFY || code == POST_MODIFY)
3691 rtx expr = XEXP (XEXP (x, 0), 1);
3693 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3694 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3695 if (GET_CODE (expr) == MINUS)
3696 val = -val;
3697 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3698 adjust = val;
3701 /* Do not use anti_adjust_stack, since we don't want to update
3702 stack_pointer_delta. */
3703 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3704 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3705 0, OPTAB_LIB_WIDEN);
3706 if (temp != stack_pointer_rtx)
3707 emit_move_insn (stack_pointer_rtx, temp);
3709 switch (code)
3711 case PRE_INC:
3712 case PRE_DEC:
3713 case PRE_MODIFY:
3714 temp = stack_pointer_rtx;
3715 break;
3716 case POST_INC:
3717 case POST_DEC:
3718 case POST_MODIFY:
3719 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3720 break;
3721 default:
3722 gcc_unreachable ();
3725 return replace_equiv_address (x, temp);
3728 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3729 X is known to satisfy push_operand, and MODE is known to be complex.
3730 Returns the last instruction emitted. */
3732 rtx_insn *
3733 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3735 scalar_mode submode = GET_MODE_INNER (mode);
3736 bool imag_first;
3738 #ifdef PUSH_ROUNDING
3739 poly_int64 submodesize = GET_MODE_SIZE (submode);
3741 /* In case we output to the stack, but the size is smaller than the
3742 machine can push exactly, we need to use move instructions. */
3743 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3745 x = emit_move_resolve_push (mode, x);
3746 return emit_move_insn (x, y);
3748 #endif
3750 /* Note that the real part always precedes the imag part in memory
3751 regardless of machine's endianness. */
3752 switch (GET_CODE (XEXP (x, 0)))
3754 case PRE_DEC:
3755 case POST_DEC:
3756 imag_first = true;
3757 break;
3758 case PRE_INC:
3759 case POST_INC:
3760 imag_first = false;
3761 break;
3762 default:
3763 gcc_unreachable ();
3766 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3767 read_complex_part (y, imag_first));
3768 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3769 read_complex_part (y, !imag_first));
3772 /* A subroutine of emit_move_complex. Perform the move from Y to X
3773 via two moves of the parts. Returns the last instruction emitted. */
3775 rtx_insn *
3776 emit_move_complex_parts (rtx x, rtx y)
3778 /* Show the output dies here. This is necessary for SUBREGs
3779 of pseudos since we cannot track their lifetimes correctly;
3780 hard regs shouldn't appear here except as return values. */
3781 if (!reload_completed && !reload_in_progress
3782 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3783 emit_clobber (x);
3785 write_complex_part (x, read_complex_part (y, false), false, true);
3786 write_complex_part (x, read_complex_part (y, true), true, false);
3788 return get_last_insn ();
3791 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3792 MODE is known to be complex. Returns the last instruction emitted. */
3794 static rtx_insn *
3795 emit_move_complex (machine_mode mode, rtx x, rtx y)
3797 bool try_int;
3799 /* Need to take special care for pushes, to maintain proper ordering
3800 of the data, and possibly extra padding. */
3801 if (push_operand (x, mode))
3802 return emit_move_complex_push (mode, x, y);
3804 /* See if we can coerce the target into moving both values at once, except
3805 for floating point where we favor moving as parts if this is easy. */
3806 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3807 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3808 && !(REG_P (x)
3809 && HARD_REGISTER_P (x)
3810 && REG_NREGS (x) == 1)
3811 && !(REG_P (y)
3812 && HARD_REGISTER_P (y)
3813 && REG_NREGS (y) == 1))
3814 try_int = false;
3815 /* Not possible if the values are inherently not adjacent. */
3816 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3817 try_int = false;
3818 /* Is possible if both are registers (or subregs of registers). */
3819 else if (register_operand (x, mode) && register_operand (y, mode))
3820 try_int = true;
3821 /* If one of the operands is a memory, and alignment constraints
3822 are friendly enough, we may be able to do combined memory operations.
3823 We do not attempt this if Y is a constant because that combination is
3824 usually better with the by-parts thing below. */
3825 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3826 && (!STRICT_ALIGNMENT
3827 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3828 try_int = true;
3829 else
3830 try_int = false;
3832 if (try_int)
3834 rtx_insn *ret;
3836 /* For memory to memory moves, optimal behavior can be had with the
3837 existing block move logic. But use normal expansion if optimizing
3838 for size. */
3839 if (MEM_P (x) && MEM_P (y))
3841 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3842 (optimize_insn_for_speed_p()
3843 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3844 return get_last_insn ();
3847 ret = emit_move_via_integer (mode, x, y, true);
3848 if (ret)
3849 return ret;
3852 return emit_move_complex_parts (x, y);
3855 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3856 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3858 static rtx_insn *
3859 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3861 rtx_insn *ret;
3863 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3864 if (mode != CCmode)
3866 enum insn_code code = optab_handler (mov_optab, CCmode);
3867 if (code != CODE_FOR_nothing)
3869 x = emit_move_change_mode (CCmode, mode, x, true);
3870 y = emit_move_change_mode (CCmode, mode, y, true);
3871 return emit_insn (GEN_FCN (code) (x, y));
3875 /* Otherwise, find the MODE_INT mode of the same width. */
3876 ret = emit_move_via_integer (mode, x, y, false);
3877 gcc_assert (ret != NULL);
3878 return ret;
3881 /* Return true if word I of OP lies entirely in the
3882 undefined bits of a paradoxical subreg. */
3884 static bool
3885 undefined_operand_subword_p (const_rtx op, int i)
3887 if (GET_CODE (op) != SUBREG)
3888 return false;
3889 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3890 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3891 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3892 || known_le (offset, -UNITS_PER_WORD));
3895 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3896 MODE is any multi-word or full-word mode that lacks a move_insn
3897 pattern. Note that you will get better code if you define such
3898 patterns, even if they must turn into multiple assembler instructions. */
3900 static rtx_insn *
3901 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3903 rtx_insn *last_insn = 0;
3904 rtx_insn *seq;
3905 rtx inner;
3906 bool need_clobber;
3907 int i, mode_size;
3909 /* This function can only handle cases where the number of words is
3910 known at compile time. */
3911 mode_size = GET_MODE_SIZE (mode).to_constant ();
3912 gcc_assert (mode_size >= UNITS_PER_WORD);
3914 /* If X is a push on the stack, do the push now and replace
3915 X with a reference to the stack pointer. */
3916 if (push_operand (x, mode))
3917 x = emit_move_resolve_push (mode, x);
3919 /* If we are in reload, see if either operand is a MEM whose address
3920 is scheduled for replacement. */
3921 if (reload_in_progress && MEM_P (x)
3922 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3923 x = replace_equiv_address_nv (x, inner);
3924 if (reload_in_progress && MEM_P (y)
3925 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3926 y = replace_equiv_address_nv (y, inner);
3928 start_sequence ();
3930 need_clobber = false;
3931 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3933 /* Do not generate code for a move if it would go entirely
3934 to the non-existing bits of a paradoxical subreg. */
3935 if (undefined_operand_subword_p (x, i))
3936 continue;
3938 rtx xpart = operand_subword (x, i, 1, mode);
3939 rtx ypart;
3941 /* Do not generate code for a move if it would come entirely
3942 from the undefined bits of a paradoxical subreg. */
3943 if (undefined_operand_subword_p (y, i))
3944 continue;
3946 ypart = operand_subword (y, i, 1, mode);
3948 /* If we can't get a part of Y, put Y into memory if it is a
3949 constant. Otherwise, force it into a register. Then we must
3950 be able to get a part of Y. */
3951 if (ypart == 0 && CONSTANT_P (y))
3953 y = use_anchored_address (force_const_mem (mode, y));
3954 ypart = operand_subword (y, i, 1, mode);
3956 else if (ypart == 0)
3957 ypart = operand_subword_force (y, i, mode);
3959 gcc_assert (xpart && ypart);
3961 need_clobber |= (GET_CODE (xpart) == SUBREG);
3963 last_insn = emit_move_insn (xpart, ypart);
3966 seq = get_insns ();
3967 end_sequence ();
3969 /* Show the output dies here. This is necessary for SUBREGs
3970 of pseudos since we cannot track their lifetimes correctly;
3971 hard regs shouldn't appear here except as return values.
3972 We never want to emit such a clobber after reload. */
3973 if (x != y
3974 && ! (reload_in_progress || reload_completed)
3975 && need_clobber != 0)
3976 emit_clobber (x);
3978 emit_insn (seq);
3980 return last_insn;
3983 /* Low level part of emit_move_insn.
3984 Called just like emit_move_insn, but assumes X and Y
3985 are basically valid. */
3987 rtx_insn *
3988 emit_move_insn_1 (rtx x, rtx y)
3990 machine_mode mode = GET_MODE (x);
3991 enum insn_code code;
3993 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3995 code = optab_handler (mov_optab, mode);
3996 if (code != CODE_FOR_nothing)
3997 return emit_insn (GEN_FCN (code) (x, y));
3999 /* Expand complex moves by moving real part and imag part. */
4000 if (COMPLEX_MODE_P (mode))
4001 return emit_move_complex (mode, x, y);
4003 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4004 || ALL_FIXED_POINT_MODE_P (mode))
4006 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4008 /* If we can't find an integer mode, use multi words. */
4009 if (result)
4010 return result;
4011 else
4012 return emit_move_multi_word (mode, x, y);
4015 if (GET_MODE_CLASS (mode) == MODE_CC)
4016 return emit_move_ccmode (mode, x, y);
4018 /* Try using a move pattern for the corresponding integer mode. This is
4019 only safe when simplify_subreg can convert MODE constants into integer
4020 constants. At present, it can only do this reliably if the value
4021 fits within a HOST_WIDE_INT. */
4022 if (!CONSTANT_P (y)
4023 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4025 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4027 if (ret)
4029 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4030 return ret;
4034 return emit_move_multi_word (mode, x, y);
4037 /* Generate code to copy Y into X.
4038 Both Y and X must have the same mode, except that
4039 Y can be a constant with VOIDmode.
4040 This mode cannot be BLKmode; use emit_block_move for that.
4042 Return the last instruction emitted. */
4044 rtx_insn *
4045 emit_move_insn (rtx x, rtx y)
4047 machine_mode mode = GET_MODE (x);
4048 rtx y_cst = NULL_RTX;
4049 rtx_insn *last_insn;
4050 rtx set;
4052 gcc_assert (mode != BLKmode
4053 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4055 /* If we have a copy that looks like one of the following patterns:
4056 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4057 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4058 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4059 (set (subreg:M1 (reg:M2 ...)) (constant C))
4060 where mode M1 is equal in size to M2, try to detect whether the
4061 mode change involves an implicit round trip through memory.
4062 If so, see if we can avoid that by removing the subregs and
4063 doing the move in mode M2 instead. */
4065 rtx x_inner = NULL_RTX;
4066 rtx y_inner = NULL_RTX;
4068 auto candidate_subreg_p = [&](rtx subreg) {
4069 return (REG_P (SUBREG_REG (subreg))
4070 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4071 GET_MODE_SIZE (GET_MODE (subreg)))
4072 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4073 != CODE_FOR_nothing);
4076 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4077 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4078 && !push_operand (mem, GET_MODE (mem))
4079 /* Not a candiate if innermode requires too much alignment. */
4080 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4081 || targetm.slow_unaligned_access (GET_MODE (mem),
4082 MEM_ALIGN (mem))
4083 || !targetm.slow_unaligned_access (innermode,
4084 MEM_ALIGN (mem))));
4087 if (SUBREG_P (x) && candidate_subreg_p (x))
4088 x_inner = SUBREG_REG (x);
4090 if (SUBREG_P (y) && candidate_subreg_p (y))
4091 y_inner = SUBREG_REG (y);
4093 if (x_inner != NULL_RTX
4094 && y_inner != NULL_RTX
4095 && GET_MODE (x_inner) == GET_MODE (y_inner)
4096 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4098 x = x_inner;
4099 y = y_inner;
4100 mode = GET_MODE (x_inner);
4102 else if (x_inner != NULL_RTX
4103 && MEM_P (y)
4104 && candidate_mem_p (GET_MODE (x_inner), y))
4106 x = x_inner;
4107 y = adjust_address (y, GET_MODE (x_inner), 0);
4108 mode = GET_MODE (x_inner);
4110 else if (y_inner != NULL_RTX
4111 && MEM_P (x)
4112 && candidate_mem_p (GET_MODE (y_inner), x))
4114 x = adjust_address (x, GET_MODE (y_inner), 0);
4115 y = y_inner;
4116 mode = GET_MODE (y_inner);
4118 else if (x_inner != NULL_RTX
4119 && CONSTANT_P (y)
4120 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4121 mode, ALL_REGS)
4122 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4124 x = x_inner;
4125 y = y_inner;
4126 mode = GET_MODE (x_inner);
4129 if (CONSTANT_P (y))
4131 if (optimize
4132 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4133 && (last_insn = compress_float_constant (x, y)))
4134 return last_insn;
4136 y_cst = y;
4138 if (!targetm.legitimate_constant_p (mode, y))
4140 y = force_const_mem (mode, y);
4142 /* If the target's cannot_force_const_mem prevented the spill,
4143 assume that the target's move expanders will also take care
4144 of the non-legitimate constant. */
4145 if (!y)
4146 y = y_cst;
4147 else
4148 y = use_anchored_address (y);
4152 /* If X or Y are memory references, verify that their addresses are valid
4153 for the machine. */
4154 if (MEM_P (x)
4155 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4156 MEM_ADDR_SPACE (x))
4157 && ! push_operand (x, GET_MODE (x))))
4158 x = validize_mem (x);
4160 if (MEM_P (y)
4161 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4162 MEM_ADDR_SPACE (y)))
4163 y = validize_mem (y);
4165 gcc_assert (mode != BLKmode);
4167 last_insn = emit_move_insn_1 (x, y);
4169 if (y_cst && REG_P (x)
4170 && (set = single_set (last_insn)) != NULL_RTX
4171 && SET_DEST (set) == x
4172 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4173 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4175 return last_insn;
4178 /* Generate the body of an instruction to copy Y into X.
4179 It may be a list of insns, if one insn isn't enough. */
4181 rtx_insn *
4182 gen_move_insn (rtx x, rtx y)
4184 rtx_insn *seq;
4186 start_sequence ();
4187 emit_move_insn_1 (x, y);
4188 seq = get_insns ();
4189 end_sequence ();
4190 return seq;
4193 /* If Y is representable exactly in a narrower mode, and the target can
4194 perform the extension directly from constant or memory, then emit the
4195 move as an extension. */
4197 static rtx_insn *
4198 compress_float_constant (rtx x, rtx y)
4200 machine_mode dstmode = GET_MODE (x);
4201 machine_mode orig_srcmode = GET_MODE (y);
4202 machine_mode srcmode;
4203 const REAL_VALUE_TYPE *r;
4204 int oldcost, newcost;
4205 bool speed = optimize_insn_for_speed_p ();
4207 r = CONST_DOUBLE_REAL_VALUE (y);
4209 if (targetm.legitimate_constant_p (dstmode, y))
4210 oldcost = set_src_cost (y, orig_srcmode, speed);
4211 else
4212 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4214 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4216 enum insn_code ic;
4217 rtx trunc_y;
4218 rtx_insn *last_insn;
4220 /* Skip if the target can't extend this way. */
4221 ic = can_extend_p (dstmode, srcmode, 0);
4222 if (ic == CODE_FOR_nothing)
4223 continue;
4225 /* Skip if the narrowed value isn't exact. */
4226 if (! exact_real_truncate (srcmode, r))
4227 continue;
4229 trunc_y = const_double_from_real_value (*r, srcmode);
4231 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4233 /* Skip if the target needs extra instructions to perform
4234 the extension. */
4235 if (!insn_operand_matches (ic, 1, trunc_y))
4236 continue;
4237 /* This is valid, but may not be cheaper than the original. */
4238 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4239 dstmode, speed);
4240 if (oldcost < newcost)
4241 continue;
4243 else if (float_extend_from_mem[dstmode][srcmode])
4245 trunc_y = force_const_mem (srcmode, trunc_y);
4246 /* This is valid, but may not be cheaper than the original. */
4247 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4248 dstmode, speed);
4249 if (oldcost < newcost)
4250 continue;
4251 trunc_y = validize_mem (trunc_y);
4253 else
4254 continue;
4256 /* For CSE's benefit, force the compressed constant pool entry
4257 into a new pseudo. This constant may be used in different modes,
4258 and if not, combine will put things back together for us. */
4259 trunc_y = force_reg (srcmode, trunc_y);
4261 /* If x is a hard register, perform the extension into a pseudo,
4262 so that e.g. stack realignment code is aware of it. */
4263 rtx target = x;
4264 if (REG_P (x) && HARD_REGISTER_P (x))
4265 target = gen_reg_rtx (dstmode);
4267 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4268 last_insn = get_last_insn ();
4270 if (REG_P (target))
4271 set_unique_reg_note (last_insn, REG_EQUAL, y);
4273 if (target != x)
4274 return emit_move_insn (x, target);
4275 return last_insn;
4278 return NULL;
4281 /* Pushing data onto the stack. */
4283 /* Push a block of length SIZE (perhaps variable)
4284 and return an rtx to address the beginning of the block.
4285 The value may be virtual_outgoing_args_rtx.
4287 EXTRA is the number of bytes of padding to push in addition to SIZE.
4288 BELOW nonzero means this padding comes at low addresses;
4289 otherwise, the padding comes at high addresses. */
4292 push_block (rtx size, poly_int64 extra, int below)
4294 rtx temp;
4296 size = convert_modes (Pmode, ptr_mode, size, 1);
4297 if (CONSTANT_P (size))
4298 anti_adjust_stack (plus_constant (Pmode, size, extra));
4299 else if (REG_P (size) && known_eq (extra, 0))
4300 anti_adjust_stack (size);
4301 else
4303 temp = copy_to_mode_reg (Pmode, size);
4304 if (maybe_ne (extra, 0))
4305 temp = expand_binop (Pmode, add_optab, temp,
4306 gen_int_mode (extra, Pmode),
4307 temp, 0, OPTAB_LIB_WIDEN);
4308 anti_adjust_stack (temp);
4311 if (STACK_GROWS_DOWNWARD)
4313 temp = virtual_outgoing_args_rtx;
4314 if (maybe_ne (extra, 0) && below)
4315 temp = plus_constant (Pmode, temp, extra);
4317 else
4319 poly_int64 csize;
4320 if (poly_int_rtx_p (size, &csize))
4321 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4322 -csize - (below ? 0 : extra));
4323 else if (maybe_ne (extra, 0) && !below)
4324 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4325 negate_rtx (Pmode, plus_constant (Pmode, size,
4326 extra)));
4327 else
4328 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4329 negate_rtx (Pmode, size));
4332 return memory_address (NARROWEST_INT_MODE, temp);
4335 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4337 static rtx
4338 mem_autoinc_base (rtx mem)
4340 if (MEM_P (mem))
4342 rtx addr = XEXP (mem, 0);
4343 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4344 return XEXP (addr, 0);
4346 return NULL;
4349 /* A utility routine used here, in reload, and in try_split. The insns
4350 after PREV up to and including LAST are known to adjust the stack,
4351 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4352 placing notes as appropriate. PREV may be NULL, indicating the
4353 entire insn sequence prior to LAST should be scanned.
4355 The set of allowed stack pointer modifications is small:
4356 (1) One or more auto-inc style memory references (aka pushes),
4357 (2) One or more addition/subtraction with the SP as destination,
4358 (3) A single move insn with the SP as destination,
4359 (4) A call_pop insn,
4360 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4362 Insns in the sequence that do not modify the SP are ignored,
4363 except for noreturn calls.
4365 The return value is the amount of adjustment that can be trivially
4366 verified, via immediate operand or auto-inc. If the adjustment
4367 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4369 poly_int64
4370 find_args_size_adjust (rtx_insn *insn)
4372 rtx dest, set, pat;
4373 int i;
4375 pat = PATTERN (insn);
4376 set = NULL;
4378 /* Look for a call_pop pattern. */
4379 if (CALL_P (insn))
4381 /* We have to allow non-call_pop patterns for the case
4382 of emit_single_push_insn of a TLS address. */
4383 if (GET_CODE (pat) != PARALLEL)
4384 return 0;
4386 /* All call_pop have a stack pointer adjust in the parallel.
4387 The call itself is always first, and the stack adjust is
4388 usually last, so search from the end. */
4389 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4391 set = XVECEXP (pat, 0, i);
4392 if (GET_CODE (set) != SET)
4393 continue;
4394 dest = SET_DEST (set);
4395 if (dest == stack_pointer_rtx)
4396 break;
4398 /* We'd better have found the stack pointer adjust. */
4399 if (i == 0)
4400 return 0;
4401 /* Fall through to process the extracted SET and DEST
4402 as if it was a standalone insn. */
4404 else if (GET_CODE (pat) == SET)
4405 set = pat;
4406 else if ((set = single_set (insn)) != NULL)
4408 else if (GET_CODE (pat) == PARALLEL)
4410 /* ??? Some older ports use a parallel with a stack adjust
4411 and a store for a PUSH_ROUNDING pattern, rather than a
4412 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4413 /* ??? See h8300 and m68k, pushqi1. */
4414 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4416 set = XVECEXP (pat, 0, i);
4417 if (GET_CODE (set) != SET)
4418 continue;
4419 dest = SET_DEST (set);
4420 if (dest == stack_pointer_rtx)
4421 break;
4423 /* We do not expect an auto-inc of the sp in the parallel. */
4424 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4425 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4426 != stack_pointer_rtx);
4428 if (i < 0)
4429 return 0;
4431 else
4432 return 0;
4434 dest = SET_DEST (set);
4436 /* Look for direct modifications of the stack pointer. */
4437 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4439 /* Look for a trivial adjustment, otherwise assume nothing. */
4440 /* Note that the SPU restore_stack_block pattern refers to
4441 the stack pointer in V4SImode. Consider that non-trivial. */
4442 poly_int64 offset;
4443 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4444 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4445 return offset;
4446 /* ??? Reload can generate no-op moves, which will be cleaned
4447 up later. Recognize it and continue searching. */
4448 else if (rtx_equal_p (dest, SET_SRC (set)))
4449 return 0;
4450 else
4451 return HOST_WIDE_INT_MIN;
4453 else
4455 rtx mem, addr;
4457 /* Otherwise only think about autoinc patterns. */
4458 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4460 mem = dest;
4461 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4462 != stack_pointer_rtx);
4464 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4465 mem = SET_SRC (set);
4466 else
4467 return 0;
4469 addr = XEXP (mem, 0);
4470 switch (GET_CODE (addr))
4472 case PRE_INC:
4473 case POST_INC:
4474 return GET_MODE_SIZE (GET_MODE (mem));
4475 case PRE_DEC:
4476 case POST_DEC:
4477 return -GET_MODE_SIZE (GET_MODE (mem));
4478 case PRE_MODIFY:
4479 case POST_MODIFY:
4480 addr = XEXP (addr, 1);
4481 gcc_assert (GET_CODE (addr) == PLUS);
4482 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4483 return rtx_to_poly_int64 (XEXP (addr, 1));
4484 default:
4485 gcc_unreachable ();
4490 poly_int64
4491 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4492 poly_int64 end_args_size)
4494 poly_int64 args_size = end_args_size;
4495 bool saw_unknown = false;
4496 rtx_insn *insn;
4498 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4500 if (!NONDEBUG_INSN_P (insn))
4501 continue;
4503 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4504 a call argument containing a TLS address that itself requires
4505 a call to __tls_get_addr. The handling of stack_pointer_delta
4506 in emit_single_push_insn is supposed to ensure that any such
4507 notes are already correct. */
4508 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4509 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4511 poly_int64 this_delta = find_args_size_adjust (insn);
4512 if (known_eq (this_delta, 0))
4514 if (!CALL_P (insn)
4515 || ACCUMULATE_OUTGOING_ARGS
4516 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4517 continue;
4520 gcc_assert (!saw_unknown);
4521 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4522 saw_unknown = true;
4524 if (!note)
4525 add_args_size_note (insn, args_size);
4526 if (STACK_GROWS_DOWNWARD)
4527 this_delta = -poly_uint64 (this_delta);
4529 if (saw_unknown)
4530 args_size = HOST_WIDE_INT_MIN;
4531 else
4532 args_size -= this_delta;
4535 return args_size;
4538 #ifdef PUSH_ROUNDING
4539 /* Emit single push insn. */
4541 static void
4542 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4544 rtx dest_addr;
4545 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4546 rtx dest;
4547 enum insn_code icode;
4549 /* If there is push pattern, use it. Otherwise try old way of throwing
4550 MEM representing push operation to move expander. */
4551 icode = optab_handler (push_optab, mode);
4552 if (icode != CODE_FOR_nothing)
4554 class expand_operand ops[1];
4556 create_input_operand (&ops[0], x, mode);
4557 if (maybe_expand_insn (icode, 1, ops))
4558 return;
4560 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4561 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4562 /* If we are to pad downward, adjust the stack pointer first and
4563 then store X into the stack location using an offset. This is
4564 because emit_move_insn does not know how to pad; it does not have
4565 access to type. */
4566 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4568 emit_move_insn (stack_pointer_rtx,
4569 expand_binop (Pmode,
4570 STACK_GROWS_DOWNWARD ? sub_optab
4571 : add_optab,
4572 stack_pointer_rtx,
4573 gen_int_mode (rounded_size, Pmode),
4574 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4576 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4577 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4578 /* We have already decremented the stack pointer, so get the
4579 previous value. */
4580 offset += rounded_size;
4582 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4583 /* We have already incremented the stack pointer, so get the
4584 previous value. */
4585 offset -= rounded_size;
4587 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4589 else
4591 if (STACK_GROWS_DOWNWARD)
4592 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4593 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4594 else
4595 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4596 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4598 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4601 dest = gen_rtx_MEM (mode, dest_addr);
4603 if (type != 0)
4605 set_mem_attributes (dest, type, 1);
4607 if (cfun->tail_call_marked)
4608 /* Function incoming arguments may overlap with sibling call
4609 outgoing arguments and we cannot allow reordering of reads
4610 from function arguments with stores to outgoing arguments
4611 of sibling calls. */
4612 set_mem_alias_set (dest, 0);
4614 emit_move_insn (dest, x);
4617 /* Emit and annotate a single push insn. */
4619 static void
4620 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4622 poly_int64 delta, old_delta = stack_pointer_delta;
4623 rtx_insn *prev = get_last_insn ();
4624 rtx_insn *last;
4626 emit_single_push_insn_1 (mode, x, type);
4628 /* Adjust stack_pointer_delta to describe the situation after the push
4629 we just performed. Note that we must do this after the push rather
4630 than before the push in case calculating X needs pushes and pops of
4631 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4632 for such pushes and pops must not include the effect of the future
4633 push of X. */
4634 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4636 last = get_last_insn ();
4638 /* Notice the common case where we emitted exactly one insn. */
4639 if (PREV_INSN (last) == prev)
4641 add_args_size_note (last, stack_pointer_delta);
4642 return;
4645 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4646 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4647 || known_eq (delta, old_delta));
4649 #endif
4651 /* If reading SIZE bytes from X will end up reading from
4652 Y return the number of bytes that overlap. Return -1
4653 if there is no overlap or -2 if we can't determine
4654 (for example when X and Y have different base registers). */
4656 static int
4657 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4659 rtx tmp = plus_constant (Pmode, x, size);
4660 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4662 if (!CONST_INT_P (sub))
4663 return -2;
4665 HOST_WIDE_INT val = INTVAL (sub);
4667 return IN_RANGE (val, 1, size) ? val : -1;
4670 /* Generate code to push X onto the stack, assuming it has mode MODE and
4671 type TYPE.
4672 MODE is redundant except when X is a CONST_INT (since they don't
4673 carry mode info).
4674 SIZE is an rtx for the size of data to be copied (in bytes),
4675 needed only if X is BLKmode.
4676 Return true if successful. May return false if asked to push a
4677 partial argument during a sibcall optimization (as specified by
4678 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4679 to not overlap.
4681 ALIGN (in bits) is maximum alignment we can assume.
4683 If PARTIAL and REG are both nonzero, then copy that many of the first
4684 bytes of X into registers starting with REG, and push the rest of X.
4685 The amount of space pushed is decreased by PARTIAL bytes.
4686 REG must be a hard register in this case.
4687 If REG is zero but PARTIAL is not, take any all others actions for an
4688 argument partially in registers, but do not actually load any
4689 registers.
4691 EXTRA is the amount in bytes of extra space to leave next to this arg.
4692 This is ignored if an argument block has already been allocated.
4694 On a machine that lacks real push insns, ARGS_ADDR is the address of
4695 the bottom of the argument block for this call. We use indexing off there
4696 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4697 argument block has not been preallocated.
4699 ARGS_SO_FAR is the size of args previously pushed for this call.
4701 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4702 for arguments passed in registers. If nonzero, it will be the number
4703 of bytes required. */
4705 bool
4706 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4707 unsigned int align, int partial, rtx reg, poly_int64 extra,
4708 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4709 rtx alignment_pad, bool sibcall_p)
4711 rtx xinner;
4712 pad_direction stack_direction
4713 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4715 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4716 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4717 Default is below for small data on big-endian machines; else above. */
4718 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4720 /* Invert direction if stack is post-decrement.
4721 FIXME: why? */
4722 if (STACK_PUSH_CODE == POST_DEC)
4723 if (where_pad != PAD_NONE)
4724 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4726 xinner = x;
4728 int nregs = partial / UNITS_PER_WORD;
4729 rtx *tmp_regs = NULL;
4730 int overlapping = 0;
4732 if (mode == BLKmode
4733 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4735 /* Copy a block into the stack, entirely or partially. */
4737 rtx temp;
4738 int used;
4739 int offset;
4740 int skip;
4742 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4743 used = partial - offset;
4745 if (mode != BLKmode)
4747 /* A value is to be stored in an insufficiently aligned
4748 stack slot; copy via a suitably aligned slot if
4749 necessary. */
4750 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4751 if (!MEM_P (xinner))
4753 temp = assign_temp (type, 1, 1);
4754 emit_move_insn (temp, xinner);
4755 xinner = temp;
4759 gcc_assert (size);
4761 /* USED is now the # of bytes we need not copy to the stack
4762 because registers will take care of them. */
4764 if (partial != 0)
4765 xinner = adjust_address (xinner, BLKmode, used);
4767 /* If the partial register-part of the arg counts in its stack size,
4768 skip the part of stack space corresponding to the registers.
4769 Otherwise, start copying to the beginning of the stack space,
4770 by setting SKIP to 0. */
4771 skip = (reg_parm_stack_space == 0) ? 0 : used;
4773 #ifdef PUSH_ROUNDING
4774 /* NB: Let the backend known the number of bytes to push and
4775 decide if push insns should be generated. */
4776 unsigned int push_size;
4777 if (CONST_INT_P (size))
4778 push_size = INTVAL (size);
4779 else
4780 push_size = 0;
4782 /* Do it with several push insns if that doesn't take lots of insns
4783 and if there is no difficulty with push insns that skip bytes
4784 on the stack for alignment purposes. */
4785 if (args_addr == 0
4786 && targetm.calls.push_argument (push_size)
4787 && CONST_INT_P (size)
4788 && skip == 0
4789 && MEM_ALIGN (xinner) >= align
4790 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4791 /* Here we avoid the case of a structure whose weak alignment
4792 forces many pushes of a small amount of data,
4793 and such small pushes do rounding that causes trouble. */
4794 && ((!targetm.slow_unaligned_access (word_mode, align))
4795 || align >= BIGGEST_ALIGNMENT
4796 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4797 align / BITS_PER_UNIT))
4798 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4800 /* Push padding now if padding above and stack grows down,
4801 or if padding below and stack grows up.
4802 But if space already allocated, this has already been done. */
4803 if (maybe_ne (extra, 0)
4804 && args_addr == 0
4805 && where_pad != PAD_NONE
4806 && where_pad != stack_direction)
4807 anti_adjust_stack (gen_int_mode (extra, Pmode));
4809 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4810 RETURN_BEGIN);
4812 else
4813 #endif /* PUSH_ROUNDING */
4815 rtx target;
4817 /* Otherwise make space on the stack and copy the data
4818 to the address of that space. */
4820 /* Deduct words put into registers from the size we must copy. */
4821 if (partial != 0)
4823 if (CONST_INT_P (size))
4824 size = GEN_INT (INTVAL (size) - used);
4825 else
4826 size = expand_binop (GET_MODE (size), sub_optab, size,
4827 gen_int_mode (used, GET_MODE (size)),
4828 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4831 /* Get the address of the stack space.
4832 In this case, we do not deal with EXTRA separately.
4833 A single stack adjust will do. */
4834 poly_int64 const_args_so_far;
4835 if (! args_addr)
4837 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4838 extra = 0;
4840 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4841 temp = memory_address (BLKmode,
4842 plus_constant (Pmode, args_addr,
4843 skip + const_args_so_far));
4844 else
4845 temp = memory_address (BLKmode,
4846 plus_constant (Pmode,
4847 gen_rtx_PLUS (Pmode,
4848 args_addr,
4849 args_so_far),
4850 skip));
4852 if (!ACCUMULATE_OUTGOING_ARGS)
4854 /* If the source is referenced relative to the stack pointer,
4855 copy it to another register to stabilize it. We do not need
4856 to do this if we know that we won't be changing sp. */
4858 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4859 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4860 temp = copy_to_reg (temp);
4863 target = gen_rtx_MEM (BLKmode, temp);
4865 /* We do *not* set_mem_attributes here, because incoming arguments
4866 may overlap with sibling call outgoing arguments and we cannot
4867 allow reordering of reads from function arguments with stores
4868 to outgoing arguments of sibling calls. We do, however, want
4869 to record the alignment of the stack slot. */
4870 /* ALIGN may well be better aligned than TYPE, e.g. due to
4871 PARM_BOUNDARY. Assume the caller isn't lying. */
4872 set_mem_align (target, align);
4874 /* If part should go in registers and pushing to that part would
4875 overwrite some of the values that need to go into regs, load the
4876 overlapping values into temporary pseudos to be moved into the hard
4877 regs at the end after the stack pushing has completed.
4878 We cannot load them directly into the hard regs here because
4879 they can be clobbered by the block move expansions.
4880 See PR 65358. */
4882 if (partial > 0 && reg != 0 && mode == BLKmode
4883 && GET_CODE (reg) != PARALLEL)
4885 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4886 if (overlapping > 0)
4888 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4889 overlapping /= UNITS_PER_WORD;
4891 tmp_regs = XALLOCAVEC (rtx, overlapping);
4893 for (int i = 0; i < overlapping; i++)
4894 tmp_regs[i] = gen_reg_rtx (word_mode);
4896 for (int i = 0; i < overlapping; i++)
4897 emit_move_insn (tmp_regs[i],
4898 operand_subword_force (target, i, mode));
4900 else if (overlapping == -1)
4901 overlapping = 0;
4902 /* Could not determine whether there is overlap.
4903 Fail the sibcall. */
4904 else
4906 overlapping = 0;
4907 if (sibcall_p)
4908 return false;
4912 /* If source is a constant VAR_DECL with a simple constructor,
4913 store the constructor to the stack instead of moving it. */
4914 const_tree decl;
4915 if (partial == 0
4916 && MEM_P (xinner)
4917 && SYMBOL_REF_P (XEXP (xinner, 0))
4918 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
4919 && VAR_P (decl)
4920 && TREE_READONLY (decl)
4921 && !TREE_SIDE_EFFECTS (decl)
4922 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
4923 store_constructor (DECL_INITIAL (decl), target, 0,
4924 int_expr_size (DECL_INITIAL (decl)), false);
4925 else
4926 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4929 else if (partial > 0)
4931 /* Scalar partly in registers. This case is only supported
4932 for fixed-wdth modes. */
4933 int num_words = GET_MODE_SIZE (mode).to_constant ();
4934 num_words /= UNITS_PER_WORD;
4935 int i;
4936 int not_stack;
4937 /* # bytes of start of argument
4938 that we must make space for but need not store. */
4939 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4940 int args_offset = INTVAL (args_so_far);
4941 int skip;
4943 /* Push padding now if padding above and stack grows down,
4944 or if padding below and stack grows up.
4945 But if space already allocated, this has already been done. */
4946 if (maybe_ne (extra, 0)
4947 && args_addr == 0
4948 && where_pad != PAD_NONE
4949 && where_pad != stack_direction)
4950 anti_adjust_stack (gen_int_mode (extra, Pmode));
4952 /* If we make space by pushing it, we might as well push
4953 the real data. Otherwise, we can leave OFFSET nonzero
4954 and leave the space uninitialized. */
4955 if (args_addr == 0)
4956 offset = 0;
4958 /* Now NOT_STACK gets the number of words that we don't need to
4959 allocate on the stack. Convert OFFSET to words too. */
4960 not_stack = (partial - offset) / UNITS_PER_WORD;
4961 offset /= UNITS_PER_WORD;
4963 /* If the partial register-part of the arg counts in its stack size,
4964 skip the part of stack space corresponding to the registers.
4965 Otherwise, start copying to the beginning of the stack space,
4966 by setting SKIP to 0. */
4967 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4969 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4970 x = validize_mem (force_const_mem (mode, x));
4972 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4973 SUBREGs of such registers are not allowed. */
4974 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4975 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4976 x = copy_to_reg (x);
4978 /* Loop over all the words allocated on the stack for this arg. */
4979 /* We can do it by words, because any scalar bigger than a word
4980 has a size a multiple of a word. */
4981 for (i = num_words - 1; i >= not_stack; i--)
4982 if (i >= not_stack + offset)
4983 if (!emit_push_insn (operand_subword_force (x, i, mode),
4984 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4985 0, args_addr,
4986 GEN_INT (args_offset + ((i - not_stack + skip)
4987 * UNITS_PER_WORD)),
4988 reg_parm_stack_space, alignment_pad, sibcall_p))
4989 return false;
4991 else
4993 rtx addr;
4994 rtx dest;
4996 /* Push padding now if padding above and stack grows down,
4997 or if padding below and stack grows up.
4998 But if space already allocated, this has already been done. */
4999 if (maybe_ne (extra, 0)
5000 && args_addr == 0
5001 && where_pad != PAD_NONE
5002 && where_pad != stack_direction)
5003 anti_adjust_stack (gen_int_mode (extra, Pmode));
5005 #ifdef PUSH_ROUNDING
5006 if (args_addr == 0 && targetm.calls.push_argument (0))
5007 emit_single_push_insn (mode, x, type);
5008 else
5009 #endif
5011 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5012 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5014 /* We do *not* set_mem_attributes here, because incoming arguments
5015 may overlap with sibling call outgoing arguments and we cannot
5016 allow reordering of reads from function arguments with stores
5017 to outgoing arguments of sibling calls. We do, however, want
5018 to record the alignment of the stack slot. */
5019 /* ALIGN may well be better aligned than TYPE, e.g. due to
5020 PARM_BOUNDARY. Assume the caller isn't lying. */
5021 set_mem_align (dest, align);
5023 emit_move_insn (dest, x);
5027 /* Move the partial arguments into the registers and any overlapping
5028 values that we moved into the pseudos in tmp_regs. */
5029 if (partial > 0 && reg != 0)
5031 /* Handle calls that pass values in multiple non-contiguous locations.
5032 The Irix 6 ABI has examples of this. */
5033 if (GET_CODE (reg) == PARALLEL)
5034 emit_group_load (reg, x, type, -1);
5035 else
5037 gcc_assert (partial % UNITS_PER_WORD == 0);
5038 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5040 for (int i = 0; i < overlapping; i++)
5041 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5042 + nregs - overlapping + i),
5043 tmp_regs[i]);
5048 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5049 anti_adjust_stack (gen_int_mode (extra, Pmode));
5051 if (alignment_pad && args_addr == 0)
5052 anti_adjust_stack (alignment_pad);
5054 return true;
5057 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5058 operations. */
5060 static rtx
5061 get_subtarget (rtx x)
5063 return (optimize
5064 || x == 0
5065 /* Only registers can be subtargets. */
5066 || !REG_P (x)
5067 /* Don't use hard regs to avoid extending their life. */
5068 || REGNO (x) < FIRST_PSEUDO_REGISTER
5069 ? 0 : x);
5072 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5073 FIELD is a bitfield. Returns true if the optimization was successful,
5074 and there's nothing else to do. */
5076 static bool
5077 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5078 poly_uint64 pbitpos,
5079 poly_uint64 pbitregion_start,
5080 poly_uint64 pbitregion_end,
5081 machine_mode mode1, rtx str_rtx,
5082 tree to, tree src, bool reverse)
5084 /* str_mode is not guaranteed to be a scalar type. */
5085 machine_mode str_mode = GET_MODE (str_rtx);
5086 unsigned int str_bitsize;
5087 tree op0, op1;
5088 rtx value, result;
5089 optab binop;
5090 gimple *srcstmt;
5091 enum tree_code code;
5093 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5094 if (mode1 != VOIDmode
5095 || !pbitsize.is_constant (&bitsize)
5096 || !pbitpos.is_constant (&bitpos)
5097 || !pbitregion_start.is_constant (&bitregion_start)
5098 || !pbitregion_end.is_constant (&bitregion_end)
5099 || bitsize >= BITS_PER_WORD
5100 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5101 || str_bitsize > BITS_PER_WORD
5102 || TREE_SIDE_EFFECTS (to)
5103 || TREE_THIS_VOLATILE (to))
5104 return false;
5106 STRIP_NOPS (src);
5107 if (TREE_CODE (src) != SSA_NAME)
5108 return false;
5109 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5110 return false;
5112 srcstmt = get_gimple_for_ssa_name (src);
5113 if (!srcstmt
5114 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5115 return false;
5117 code = gimple_assign_rhs_code (srcstmt);
5119 op0 = gimple_assign_rhs1 (srcstmt);
5121 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5122 to find its initialization. Hopefully the initialization will
5123 be from a bitfield load. */
5124 if (TREE_CODE (op0) == SSA_NAME)
5126 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5128 /* We want to eventually have OP0 be the same as TO, which
5129 should be a bitfield. */
5130 if (!op0stmt
5131 || !is_gimple_assign (op0stmt)
5132 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5133 return false;
5134 op0 = gimple_assign_rhs1 (op0stmt);
5137 op1 = gimple_assign_rhs2 (srcstmt);
5139 if (!operand_equal_p (to, op0, 0))
5140 return false;
5142 if (MEM_P (str_rtx))
5144 unsigned HOST_WIDE_INT offset1;
5146 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5147 str_bitsize = BITS_PER_WORD;
5149 scalar_int_mode best_mode;
5150 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5151 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5152 return false;
5153 str_mode = best_mode;
5154 str_bitsize = GET_MODE_BITSIZE (best_mode);
5156 offset1 = bitpos;
5157 bitpos %= str_bitsize;
5158 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5159 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5161 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5162 return false;
5164 /* If the bit field covers the whole REG/MEM, store_field
5165 will likely generate better code. */
5166 if (bitsize >= str_bitsize)
5167 return false;
5169 /* We can't handle fields split across multiple entities. */
5170 if (bitpos + bitsize > str_bitsize)
5171 return false;
5173 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5174 bitpos = str_bitsize - bitpos - bitsize;
5176 switch (code)
5178 case PLUS_EXPR:
5179 case MINUS_EXPR:
5180 /* For now, just optimize the case of the topmost bitfield
5181 where we don't need to do any masking and also
5182 1 bit bitfields where xor can be used.
5183 We might win by one instruction for the other bitfields
5184 too if insv/extv instructions aren't used, so that
5185 can be added later. */
5186 if ((reverse || bitpos + bitsize != str_bitsize)
5187 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5188 break;
5190 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5191 value = convert_modes (str_mode,
5192 TYPE_MODE (TREE_TYPE (op1)), value,
5193 TYPE_UNSIGNED (TREE_TYPE (op1)));
5195 /* We may be accessing data outside the field, which means
5196 we can alias adjacent data. */
5197 if (MEM_P (str_rtx))
5199 str_rtx = shallow_copy_rtx (str_rtx);
5200 set_mem_alias_set (str_rtx, 0);
5201 set_mem_expr (str_rtx, 0);
5204 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5206 value = expand_and (str_mode, value, const1_rtx, NULL);
5207 binop = xor_optab;
5209 else
5210 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5212 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5213 if (reverse)
5214 value = flip_storage_order (str_mode, value);
5215 result = expand_binop (str_mode, binop, str_rtx,
5216 value, str_rtx, 1, OPTAB_WIDEN);
5217 if (result != str_rtx)
5218 emit_move_insn (str_rtx, result);
5219 return true;
5221 case BIT_IOR_EXPR:
5222 case BIT_XOR_EXPR:
5223 if (TREE_CODE (op1) != INTEGER_CST)
5224 break;
5225 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5226 value = convert_modes (str_mode,
5227 TYPE_MODE (TREE_TYPE (op1)), value,
5228 TYPE_UNSIGNED (TREE_TYPE (op1)));
5230 /* We may be accessing data outside the field, which means
5231 we can alias adjacent data. */
5232 if (MEM_P (str_rtx))
5234 str_rtx = shallow_copy_rtx (str_rtx);
5235 set_mem_alias_set (str_rtx, 0);
5236 set_mem_expr (str_rtx, 0);
5239 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5240 if (bitpos + bitsize != str_bitsize)
5242 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5243 str_mode);
5244 value = expand_and (str_mode, value, mask, NULL_RTX);
5246 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5247 if (reverse)
5248 value = flip_storage_order (str_mode, value);
5249 result = expand_binop (str_mode, binop, str_rtx,
5250 value, str_rtx, 1, OPTAB_WIDEN);
5251 if (result != str_rtx)
5252 emit_move_insn (str_rtx, result);
5253 return true;
5255 default:
5256 break;
5259 return false;
5262 /* In the C++ memory model, consecutive bit fields in a structure are
5263 considered one memory location.
5265 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5266 returns the bit range of consecutive bits in which this COMPONENT_REF
5267 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5268 and *OFFSET may be adjusted in the process.
5270 If the access does not need to be restricted, 0 is returned in both
5271 *BITSTART and *BITEND. */
5273 void
5274 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5275 poly_int64_pod *bitpos, tree *offset)
5277 poly_int64 bitoffset;
5278 tree field, repr;
5280 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5282 field = TREE_OPERAND (exp, 1);
5283 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5284 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5285 need to limit the range we can access. */
5286 if (!repr)
5288 *bitstart = *bitend = 0;
5289 return;
5292 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5293 part of a larger bit field, then the representative does not serve any
5294 useful purpose. This can occur in Ada. */
5295 if (handled_component_p (TREE_OPERAND (exp, 0)))
5297 machine_mode rmode;
5298 poly_int64 rbitsize, rbitpos;
5299 tree roffset;
5300 int unsignedp, reversep, volatilep = 0;
5301 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5302 &roffset, &rmode, &unsignedp, &reversep,
5303 &volatilep);
5304 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5306 *bitstart = *bitend = 0;
5307 return;
5311 /* Compute the adjustment to bitpos from the offset of the field
5312 relative to the representative. DECL_FIELD_OFFSET of field and
5313 repr are the same by construction if they are not constants,
5314 see finish_bitfield_layout. */
5315 poly_uint64 field_offset, repr_offset;
5316 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5317 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5318 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5319 else
5320 bitoffset = 0;
5321 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5322 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5324 /* If the adjustment is larger than bitpos, we would have a negative bit
5325 position for the lower bound and this may wreak havoc later. Adjust
5326 offset and bitpos to make the lower bound non-negative in that case. */
5327 if (maybe_gt (bitoffset, *bitpos))
5329 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5330 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5332 *bitpos += adjust_bits;
5333 if (*offset == NULL_TREE)
5334 *offset = size_int (-adjust_bytes);
5335 else
5336 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5337 *bitstart = 0;
5339 else
5340 *bitstart = *bitpos - bitoffset;
5342 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5345 /* Returns true if BASE is a DECL that does not reside in memory and
5346 has non-BLKmode. DECL_RTL must not be a MEM; if
5347 DECL_RTL was not set yet, return false. */
5349 bool
5350 non_mem_decl_p (tree base)
5352 if (!DECL_P (base)
5353 || TREE_ADDRESSABLE (base)
5354 || DECL_MODE (base) == BLKmode)
5355 return false;
5357 if (!DECL_RTL_SET_P (base))
5358 return false;
5360 return (!MEM_P (DECL_RTL (base)));
5363 /* Returns true if REF refers to an object that does not
5364 reside in memory and has non-BLKmode. */
5366 bool
5367 mem_ref_refers_to_non_mem_p (tree ref)
5369 tree base;
5371 if (TREE_CODE (ref) == MEM_REF
5372 || TREE_CODE (ref) == TARGET_MEM_REF)
5374 tree addr = TREE_OPERAND (ref, 0);
5376 if (TREE_CODE (addr) != ADDR_EXPR)
5377 return false;
5379 base = TREE_OPERAND (addr, 0);
5381 else
5382 base = ref;
5384 return non_mem_decl_p (base);
5387 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5388 is true, try generating a nontemporal store. */
5390 void
5391 expand_assignment (tree to, tree from, bool nontemporal)
5393 rtx to_rtx = 0;
5394 rtx result;
5395 machine_mode mode;
5396 unsigned int align;
5397 enum insn_code icode;
5399 /* Don't crash if the lhs of the assignment was erroneous. */
5400 if (TREE_CODE (to) == ERROR_MARK)
5402 expand_normal (from);
5403 return;
5406 /* Optimize away no-op moves without side-effects. */
5407 if (operand_equal_p (to, from, 0))
5408 return;
5410 /* Handle misaligned stores. */
5411 mode = TYPE_MODE (TREE_TYPE (to));
5412 if ((TREE_CODE (to) == MEM_REF
5413 || TREE_CODE (to) == TARGET_MEM_REF
5414 || DECL_P (to))
5415 && mode != BLKmode
5416 && !mem_ref_refers_to_non_mem_p (to)
5417 && ((align = get_object_alignment (to))
5418 < GET_MODE_ALIGNMENT (mode))
5419 && (((icode = optab_handler (movmisalign_optab, mode))
5420 != CODE_FOR_nothing)
5421 || targetm.slow_unaligned_access (mode, align)))
5423 rtx reg, mem;
5425 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5426 /* Handle PARALLEL. */
5427 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5428 reg = force_not_mem (reg);
5429 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5430 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5431 reg = flip_storage_order (mode, reg);
5433 if (icode != CODE_FOR_nothing)
5435 class expand_operand ops[2];
5437 create_fixed_operand (&ops[0], mem);
5438 create_input_operand (&ops[1], reg, mode);
5439 /* The movmisalign<mode> pattern cannot fail, else the assignment
5440 would silently be omitted. */
5441 expand_insn (icode, 2, ops);
5443 else
5444 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5445 false, false);
5446 return;
5449 /* Assignment of a structure component needs special treatment
5450 if the structure component's rtx is not simply a MEM.
5451 Assignment of an array element at a constant index, and assignment of
5452 an array element in an unaligned packed structure field, has the same
5453 problem. Same for (partially) storing into a non-memory object. */
5454 if (handled_component_p (to)
5455 || (TREE_CODE (to) == MEM_REF
5456 && (REF_REVERSE_STORAGE_ORDER (to)
5457 || mem_ref_refers_to_non_mem_p (to)))
5458 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5460 machine_mode mode1;
5461 poly_int64 bitsize, bitpos;
5462 poly_uint64 bitregion_start = 0;
5463 poly_uint64 bitregion_end = 0;
5464 tree offset;
5465 int unsignedp, reversep, volatilep = 0;
5466 tree tem;
5468 push_temp_slots ();
5469 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5470 &unsignedp, &reversep, &volatilep);
5472 /* Make sure bitpos is not negative, it can wreak havoc later. */
5473 if (maybe_lt (bitpos, 0))
5475 gcc_assert (offset == NULL_TREE);
5476 offset = size_int (bits_to_bytes_round_down (bitpos));
5477 bitpos = num_trailing_bits (bitpos);
5480 if (TREE_CODE (to) == COMPONENT_REF
5481 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5482 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5483 /* The C++ memory model naturally applies to byte-aligned fields.
5484 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5485 BITSIZE are not byte-aligned, there is no need to limit the range
5486 we can access. This can occur with packed structures in Ada. */
5487 else if (maybe_gt (bitsize, 0)
5488 && multiple_p (bitsize, BITS_PER_UNIT)
5489 && multiple_p (bitpos, BITS_PER_UNIT))
5491 bitregion_start = bitpos;
5492 bitregion_end = bitpos + bitsize - 1;
5495 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5497 /* If the field has a mode, we want to access it in the
5498 field's mode, not the computed mode.
5499 If a MEM has VOIDmode (external with incomplete type),
5500 use BLKmode for it instead. */
5501 if (MEM_P (to_rtx))
5503 if (mode1 != VOIDmode)
5504 to_rtx = adjust_address (to_rtx, mode1, 0);
5505 else if (GET_MODE (to_rtx) == VOIDmode)
5506 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5509 if (offset != 0)
5511 machine_mode address_mode;
5512 rtx offset_rtx;
5514 if (!MEM_P (to_rtx))
5516 /* We can get constant negative offsets into arrays with broken
5517 user code. Translate this to a trap instead of ICEing. */
5518 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5519 expand_builtin_trap ();
5520 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5523 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5524 address_mode = get_address_mode (to_rtx);
5525 if (GET_MODE (offset_rtx) != address_mode)
5527 /* We cannot be sure that the RTL in offset_rtx is valid outside
5528 of a memory address context, so force it into a register
5529 before attempting to convert it to the desired mode. */
5530 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5531 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5534 /* If we have an expression in OFFSET_RTX and a non-zero
5535 byte offset in BITPOS, adding the byte offset before the
5536 OFFSET_RTX results in better intermediate code, which makes
5537 later rtl optimization passes perform better.
5539 We prefer intermediate code like this:
5541 r124:DI=r123:DI+0x18
5542 [r124:DI]=r121:DI
5544 ... instead of ...
5546 r124:DI=r123:DI+0x10
5547 [r124:DI+0x8]=r121:DI
5549 This is only done for aligned data values, as these can
5550 be expected to result in single move instructions. */
5551 poly_int64 bytepos;
5552 if (mode1 != VOIDmode
5553 && maybe_ne (bitpos, 0)
5554 && maybe_gt (bitsize, 0)
5555 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5556 && multiple_p (bitpos, bitsize)
5557 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5558 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5560 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5561 bitregion_start = 0;
5562 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5563 bitregion_end -= bitpos;
5564 bitpos = 0;
5567 to_rtx = offset_address (to_rtx, offset_rtx,
5568 highest_pow2_factor_for_target (to,
5569 offset));
5572 /* No action is needed if the target is not a memory and the field
5573 lies completely outside that target. This can occur if the source
5574 code contains an out-of-bounds access to a small array. */
5575 if (!MEM_P (to_rtx)
5576 && GET_MODE (to_rtx) != BLKmode
5577 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5579 expand_normal (from);
5580 result = NULL;
5582 /* Handle expand_expr of a complex value returning a CONCAT. */
5583 else if (GET_CODE (to_rtx) == CONCAT)
5585 machine_mode to_mode = GET_MODE (to_rtx);
5586 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5587 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5588 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5589 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5590 && known_eq (bitpos, 0)
5591 && known_eq (bitsize, mode_bitsize))
5592 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5593 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5594 && known_eq (bitsize, inner_bitsize)
5595 && (known_eq (bitpos, 0)
5596 || known_eq (bitpos, inner_bitsize)))
5597 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5598 false, nontemporal, reversep);
5599 else if (known_le (bitpos + bitsize, inner_bitsize))
5600 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5601 bitregion_start, bitregion_end,
5602 mode1, from, get_alias_set (to),
5603 nontemporal, reversep);
5604 else if (known_ge (bitpos, inner_bitsize))
5605 result = store_field (XEXP (to_rtx, 1), bitsize,
5606 bitpos - inner_bitsize,
5607 bitregion_start, bitregion_end,
5608 mode1, from, get_alias_set (to),
5609 nontemporal, reversep);
5610 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5612 result = expand_normal (from);
5613 if (GET_CODE (result) == CONCAT)
5615 to_mode = GET_MODE_INNER (to_mode);
5616 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5617 rtx from_real
5618 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5619 from_mode, 0);
5620 rtx from_imag
5621 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5622 from_mode, 0);
5623 if (!from_real || !from_imag)
5624 goto concat_store_slow;
5625 emit_move_insn (XEXP (to_rtx, 0), from_real);
5626 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5628 else
5630 machine_mode from_mode
5631 = GET_MODE (result) == VOIDmode
5632 ? TYPE_MODE (TREE_TYPE (from))
5633 : GET_MODE (result);
5634 rtx from_rtx;
5635 if (MEM_P (result))
5636 from_rtx = change_address (result, to_mode, NULL_RTX);
5637 else
5638 from_rtx
5639 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5640 if (from_rtx)
5642 emit_move_insn (XEXP (to_rtx, 0),
5643 read_complex_part (from_rtx, false));
5644 emit_move_insn (XEXP (to_rtx, 1),
5645 read_complex_part (from_rtx, true));
5647 else
5649 to_mode = GET_MODE_INNER (to_mode);
5650 rtx from_real
5651 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5652 rtx from_imag
5653 = simplify_gen_subreg (to_mode, result, from_mode,
5654 GET_MODE_SIZE (to_mode));
5655 if (!from_real || !from_imag)
5656 goto concat_store_slow;
5657 emit_move_insn (XEXP (to_rtx, 0), from_real);
5658 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5662 else
5664 concat_store_slow:;
5665 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5666 GET_MODE_SIZE (GET_MODE (to_rtx)));
5667 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
5668 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
5669 result = store_field (temp, bitsize, bitpos,
5670 bitregion_start, bitregion_end,
5671 mode1, from, get_alias_set (to),
5672 nontemporal, reversep);
5673 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5674 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5677 /* For calls to functions returning variable length structures, if TO_RTX
5678 is not a MEM, go through a MEM because we must not create temporaries
5679 of the VLA type. */
5680 else if (!MEM_P (to_rtx)
5681 && TREE_CODE (from) == CALL_EXPR
5682 && COMPLETE_TYPE_P (TREE_TYPE (from))
5683 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5685 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5686 GET_MODE_SIZE (GET_MODE (to_rtx)));
5687 result = store_field (temp, bitsize, bitpos, bitregion_start,
5688 bitregion_end, mode1, from, get_alias_set (to),
5689 nontemporal, reversep);
5690 emit_move_insn (to_rtx, temp);
5692 else
5694 if (MEM_P (to_rtx))
5696 /* If the field is at offset zero, we could have been given the
5697 DECL_RTX of the parent struct. Don't munge it. */
5698 to_rtx = shallow_copy_rtx (to_rtx);
5699 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5700 if (volatilep)
5701 MEM_VOLATILE_P (to_rtx) = 1;
5704 gcc_checking_assert (known_ge (bitpos, 0));
5705 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5706 bitregion_start, bitregion_end,
5707 mode1, to_rtx, to, from,
5708 reversep))
5709 result = NULL;
5710 else if (SUBREG_P (to_rtx)
5711 && SUBREG_PROMOTED_VAR_P (to_rtx))
5713 /* If to_rtx is a promoted subreg, we need to zero or sign
5714 extend the value afterwards. */
5715 if (TREE_CODE (to) == MEM_REF
5716 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5717 && !REF_REVERSE_STORAGE_ORDER (to)
5718 && known_eq (bitpos, 0)
5719 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5720 result = store_expr (from, to_rtx, 0, nontemporal, false);
5721 else
5723 rtx to_rtx1
5724 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5725 SUBREG_REG (to_rtx),
5726 subreg_promoted_mode (to_rtx));
5727 result = store_field (to_rtx1, bitsize, bitpos,
5728 bitregion_start, bitregion_end,
5729 mode1, from, get_alias_set (to),
5730 nontemporal, reversep);
5731 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5732 SUBREG_PROMOTED_SIGN (to_rtx));
5735 else
5736 result = store_field (to_rtx, bitsize, bitpos,
5737 bitregion_start, bitregion_end,
5738 mode1, from, get_alias_set (to),
5739 nontemporal, reversep);
5742 if (result)
5743 preserve_temp_slots (result);
5744 pop_temp_slots ();
5745 return;
5748 /* If the rhs is a function call and its value is not an aggregate,
5749 call the function before we start to compute the lhs.
5750 This is needed for correct code for cases such as
5751 val = setjmp (buf) on machines where reference to val
5752 requires loading up part of an address in a separate insn.
5754 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5755 since it might be a promoted variable where the zero- or sign- extension
5756 needs to be done. Handling this in the normal way is safe because no
5757 computation is done before the call. The same is true for SSA names. */
5758 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5759 && COMPLETE_TYPE_P (TREE_TYPE (from))
5760 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5761 && ! (((VAR_P (to)
5762 || TREE_CODE (to) == PARM_DECL
5763 || TREE_CODE (to) == RESULT_DECL)
5764 && REG_P (DECL_RTL (to)))
5765 || TREE_CODE (to) == SSA_NAME))
5767 rtx value;
5769 push_temp_slots ();
5770 value = expand_normal (from);
5772 if (to_rtx == 0)
5773 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5775 /* Handle calls that return values in multiple non-contiguous locations.
5776 The Irix 6 ABI has examples of this. */
5777 if (GET_CODE (to_rtx) == PARALLEL)
5779 if (GET_CODE (value) == PARALLEL)
5780 emit_group_move (to_rtx, value);
5781 else
5782 emit_group_load (to_rtx, value, TREE_TYPE (from),
5783 int_size_in_bytes (TREE_TYPE (from)));
5785 else if (GET_CODE (value) == PARALLEL)
5786 emit_group_store (to_rtx, value, TREE_TYPE (from),
5787 int_size_in_bytes (TREE_TYPE (from)));
5788 else if (GET_MODE (to_rtx) == BLKmode)
5790 /* Handle calls that return BLKmode values in registers. */
5791 if (REG_P (value))
5792 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5793 else
5794 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5796 else
5798 if (POINTER_TYPE_P (TREE_TYPE (to)))
5799 value = convert_memory_address_addr_space
5800 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5801 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5803 emit_move_insn (to_rtx, value);
5806 preserve_temp_slots (to_rtx);
5807 pop_temp_slots ();
5808 return;
5811 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5812 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5814 /* Don't move directly into a return register. */
5815 if (TREE_CODE (to) == RESULT_DECL
5816 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5818 rtx temp;
5820 push_temp_slots ();
5822 /* If the source is itself a return value, it still is in a pseudo at
5823 this point so we can move it back to the return register directly. */
5824 if (REG_P (to_rtx)
5825 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5826 && TREE_CODE (from) != CALL_EXPR)
5827 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5828 else
5829 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5831 /* Handle calls that return values in multiple non-contiguous locations.
5832 The Irix 6 ABI has examples of this. */
5833 if (GET_CODE (to_rtx) == PARALLEL)
5835 if (GET_CODE (temp) == PARALLEL)
5836 emit_group_move (to_rtx, temp);
5837 else
5838 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5839 int_size_in_bytes (TREE_TYPE (from)));
5841 else if (temp)
5842 emit_move_insn (to_rtx, temp);
5844 preserve_temp_slots (to_rtx);
5845 pop_temp_slots ();
5846 return;
5849 /* In case we are returning the contents of an object which overlaps
5850 the place the value is being stored, use a safe function when copying
5851 a value through a pointer into a structure value return block. */
5852 if (TREE_CODE (to) == RESULT_DECL
5853 && TREE_CODE (from) == INDIRECT_REF
5854 && ADDR_SPACE_GENERIC_P
5855 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5856 && refs_may_alias_p (to, from)
5857 && cfun->returns_struct
5858 && !cfun->returns_pcc_struct)
5860 rtx from_rtx, size;
5862 push_temp_slots ();
5863 size = expr_size (from);
5864 from_rtx = expand_normal (from);
5866 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5868 preserve_temp_slots (to_rtx);
5869 pop_temp_slots ();
5870 return;
5873 /* Compute FROM and store the value in the rtx we got. */
5875 push_temp_slots ();
5876 result = store_expr (from, to_rtx, 0, nontemporal, false);
5877 preserve_temp_slots (result);
5878 pop_temp_slots ();
5879 return;
5882 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5883 succeeded, false otherwise. */
5885 bool
5886 emit_storent_insn (rtx to, rtx from)
5888 class expand_operand ops[2];
5889 machine_mode mode = GET_MODE (to);
5890 enum insn_code code = optab_handler (storent_optab, mode);
5892 if (code == CODE_FOR_nothing)
5893 return false;
5895 create_fixed_operand (&ops[0], to);
5896 create_input_operand (&ops[1], from, mode);
5897 return maybe_expand_insn (code, 2, ops);
5900 /* Helper function for store_expr storing of STRING_CST. */
5902 static rtx
5903 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5904 fixed_size_mode mode)
5906 tree str = (tree) data;
5908 gcc_assert (offset >= 0);
5909 if (offset >= TREE_STRING_LENGTH (str))
5910 return const0_rtx;
5912 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5913 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5915 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5916 size_t l = TREE_STRING_LENGTH (str) - offset;
5917 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5918 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5919 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
5922 /* The by-pieces infrastructure does not try to pick a vector mode
5923 for storing STRING_CST. */
5924 return c_readstr (TREE_STRING_POINTER (str) + offset,
5925 as_a <scalar_int_mode> (mode), false);
5928 /* Generate code for computing expression EXP,
5929 and storing the value into TARGET.
5931 If the mode is BLKmode then we may return TARGET itself.
5932 It turns out that in BLKmode it doesn't cause a problem.
5933 because C has no operators that could combine two different
5934 assignments into the same BLKmode object with different values
5935 with no sequence point. Will other languages need this to
5936 be more thorough?
5938 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5939 stack, and block moves may need to be treated specially.
5941 If NONTEMPORAL is true, try using a nontemporal store instruction.
5943 If REVERSE is true, the store is to be done in reverse order. */
5946 store_expr (tree exp, rtx target, int call_param_p,
5947 bool nontemporal, bool reverse)
5949 rtx temp;
5950 rtx alt_rtl = NULL_RTX;
5951 location_t loc = curr_insn_location ();
5952 bool shortened_string_cst = false;
5954 if (VOID_TYPE_P (TREE_TYPE (exp)))
5956 /* C++ can generate ?: expressions with a throw expression in one
5957 branch and an rvalue in the other. Here, we resolve attempts to
5958 store the throw expression's nonexistent result. */
5959 gcc_assert (!call_param_p);
5960 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5961 return NULL_RTX;
5963 if (TREE_CODE (exp) == COMPOUND_EXPR)
5965 /* Perform first part of compound expression, then assign from second
5966 part. */
5967 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5968 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5969 return store_expr (TREE_OPERAND (exp, 1), target,
5970 call_param_p, nontemporal, reverse);
5972 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5974 /* For conditional expression, get safe form of the target. Then
5975 test the condition, doing the appropriate assignment on either
5976 side. This avoids the creation of unnecessary temporaries.
5977 For non-BLKmode, it is more efficient not to do this. */
5979 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5981 do_pending_stack_adjust ();
5982 NO_DEFER_POP;
5983 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5984 profile_probability::uninitialized ());
5985 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5986 nontemporal, reverse);
5987 emit_jump_insn (targetm.gen_jump (lab2));
5988 emit_barrier ();
5989 emit_label (lab1);
5990 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5991 nontemporal, reverse);
5992 emit_label (lab2);
5993 OK_DEFER_POP;
5995 return NULL_RTX;
5997 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5998 /* If this is a scalar in a register that is stored in a wider mode
5999 than the declared mode, compute the result into its declared mode
6000 and then convert to the wider mode. Our value is the computed
6001 expression. */
6003 rtx inner_target = 0;
6004 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6005 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6007 /* We can do the conversion inside EXP, which will often result
6008 in some optimizations. Do the conversion in two steps: first
6009 change the signedness, if needed, then the extend. But don't
6010 do this if the type of EXP is a subtype of something else
6011 since then the conversion might involve more than just
6012 converting modes. */
6013 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6014 && TREE_TYPE (TREE_TYPE (exp)) == 0
6015 && GET_MODE_PRECISION (outer_mode)
6016 == TYPE_PRECISION (TREE_TYPE (exp)))
6018 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6019 TYPE_UNSIGNED (TREE_TYPE (exp))))
6021 /* Some types, e.g. Fortran's logical*4, won't have a signed
6022 version, so use the mode instead. */
6023 tree ntype
6024 = (signed_or_unsigned_type_for
6025 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6026 if (ntype == NULL)
6027 ntype = lang_hooks.types.type_for_mode
6028 (TYPE_MODE (TREE_TYPE (exp)),
6029 SUBREG_PROMOTED_SIGN (target));
6031 exp = fold_convert_loc (loc, ntype, exp);
6034 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6035 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6036 exp);
6038 inner_target = SUBREG_REG (target);
6041 temp = expand_expr (exp, inner_target, VOIDmode,
6042 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6045 /* If TEMP is a VOIDmode constant, use convert_modes to make
6046 sure that we properly convert it. */
6047 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6049 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6050 temp, SUBREG_PROMOTED_SIGN (target));
6051 temp = convert_modes (inner_mode, outer_mode, temp,
6052 SUBREG_PROMOTED_SIGN (target));
6055 convert_move (SUBREG_REG (target), temp,
6056 SUBREG_PROMOTED_SIGN (target));
6058 return NULL_RTX;
6060 else if ((TREE_CODE (exp) == STRING_CST
6061 || (TREE_CODE (exp) == MEM_REF
6062 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6063 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6064 == STRING_CST
6065 && integer_zerop (TREE_OPERAND (exp, 1))))
6066 && !nontemporal && !call_param_p
6067 && MEM_P (target))
6069 /* Optimize initialization of an array with a STRING_CST. */
6070 HOST_WIDE_INT exp_len, str_copy_len;
6071 rtx dest_mem;
6072 tree str = TREE_CODE (exp) == STRING_CST
6073 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6075 exp_len = int_expr_size (exp);
6076 if (exp_len <= 0)
6077 goto normal_expr;
6079 if (TREE_STRING_LENGTH (str) <= 0)
6080 goto normal_expr;
6082 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6083 MEM_ALIGN (target), false))
6085 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6086 MEM_ALIGN (target), false, RETURN_BEGIN);
6087 return NULL_RTX;
6090 str_copy_len = TREE_STRING_LENGTH (str);
6092 /* Trailing NUL bytes in EXP will be handled by the call to
6093 clear_storage, which is more efficient than copying them from
6094 the STRING_CST, so trim those from STR_COPY_LEN. */
6095 while (str_copy_len)
6097 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6098 break;
6099 str_copy_len--;
6102 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6104 str_copy_len += STORE_MAX_PIECES - 1;
6105 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6107 if (str_copy_len >= exp_len)
6108 goto normal_expr;
6110 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6111 (void *) str, MEM_ALIGN (target), false))
6112 goto normal_expr;
6114 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6115 (void *) str, MEM_ALIGN (target), false,
6116 RETURN_END);
6117 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6118 exp_len - str_copy_len),
6119 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6120 return NULL_RTX;
6122 else
6124 rtx tmp_target;
6126 normal_expr:
6127 /* If we want to use a nontemporal or a reverse order store, force the
6128 value into a register first. */
6129 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6130 tree rexp = exp;
6131 if (TREE_CODE (exp) == STRING_CST
6132 && tmp_target == target
6133 && GET_MODE (target) == BLKmode
6134 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6136 rtx size = expr_size (exp);
6137 if (CONST_INT_P (size)
6138 && size != const0_rtx
6139 && (UINTVAL (size)
6140 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6142 /* If the STRING_CST has much larger array type than
6143 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6144 it into the rodata section as the code later on will use
6145 memset zero for the remainder anyway. See PR95052. */
6146 tmp_target = NULL_RTX;
6147 rexp = copy_node (exp);
6148 tree index
6149 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6150 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6151 index);
6152 shortened_string_cst = true;
6155 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6156 (call_param_p
6157 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6158 &alt_rtl, false);
6159 if (shortened_string_cst)
6161 gcc_assert (MEM_P (temp));
6162 temp = change_address (temp, BLKmode, NULL_RTX);
6166 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6167 the same as that of TARGET, adjust the constant. This is needed, for
6168 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6169 only a word-sized value. */
6170 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6171 && TREE_CODE (exp) != ERROR_MARK
6172 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6174 gcc_assert (!shortened_string_cst);
6175 if (GET_MODE_CLASS (GET_MODE (target))
6176 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6177 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6178 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6180 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6181 TYPE_MODE (TREE_TYPE (exp)), 0);
6182 if (t)
6183 temp = t;
6185 if (GET_MODE (temp) == VOIDmode)
6186 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6187 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6190 /* If value was not generated in the target, store it there.
6191 Convert the value to TARGET's type first if necessary and emit the
6192 pending incrementations that have been queued when expanding EXP.
6193 Note that we cannot emit the whole queue blindly because this will
6194 effectively disable the POST_INC optimization later.
6196 If TEMP and TARGET compare equal according to rtx_equal_p, but
6197 one or both of them are volatile memory refs, we have to distinguish
6198 two cases:
6199 - expand_expr has used TARGET. In this case, we must not generate
6200 another copy. This can be detected by TARGET being equal according
6201 to == .
6202 - expand_expr has not used TARGET - that means that the source just
6203 happens to have the same RTX form. Since temp will have been created
6204 by expand_expr, it will compare unequal according to == .
6205 We must generate a copy in this case, to reach the correct number
6206 of volatile memory references. */
6208 if ((! rtx_equal_p (temp, target)
6209 || (temp != target && (side_effects_p (temp)
6210 || side_effects_p (target)
6211 || (MEM_P (temp)
6212 && !mems_same_for_tbaa_p (temp, target)))))
6213 && TREE_CODE (exp) != ERROR_MARK
6214 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6215 but TARGET is not valid memory reference, TEMP will differ
6216 from TARGET although it is really the same location. */
6217 && !(alt_rtl
6218 && rtx_equal_p (alt_rtl, target)
6219 && !side_effects_p (alt_rtl)
6220 && !side_effects_p (target))
6221 /* If there's nothing to copy, don't bother. Don't call
6222 expr_size unless necessary, because some front-ends (C++)
6223 expr_size-hook must not be given objects that are not
6224 supposed to be bit-copied or bit-initialized. */
6225 && expr_size (exp) != const0_rtx)
6227 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6229 gcc_assert (!shortened_string_cst);
6230 if (GET_MODE (target) == BLKmode)
6232 /* Handle calls that return BLKmode values in registers. */
6233 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6234 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6235 else
6236 store_bit_field (target,
6237 rtx_to_poly_int64 (expr_size (exp))
6238 * BITS_PER_UNIT,
6239 0, 0, 0, GET_MODE (temp), temp, reverse,
6240 false);
6242 else
6243 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6246 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6248 /* Handle copying a string constant into an array. The string
6249 constant may be shorter than the array. So copy just the string's
6250 actual length, and clear the rest. First get the size of the data
6251 type of the string, which is actually the size of the target. */
6252 rtx size = expr_size (exp);
6254 if (CONST_INT_P (size)
6255 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6256 emit_block_move (target, temp, size,
6257 (call_param_p
6258 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6259 else
6261 machine_mode pointer_mode
6262 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6263 machine_mode address_mode = get_address_mode (target);
6265 /* Compute the size of the data to copy from the string. */
6266 tree copy_size
6267 = size_binop_loc (loc, MIN_EXPR,
6268 make_tree (sizetype, size),
6269 size_int (TREE_STRING_LENGTH (exp)));
6270 rtx copy_size_rtx
6271 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6272 (call_param_p
6273 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6274 rtx_code_label *label = 0;
6276 /* Copy that much. */
6277 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6278 TYPE_UNSIGNED (sizetype));
6279 emit_block_move (target, temp, copy_size_rtx,
6280 (call_param_p
6281 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6283 /* Figure out how much is left in TARGET that we have to clear.
6284 Do all calculations in pointer_mode. */
6285 poly_int64 const_copy_size;
6286 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6288 size = plus_constant (address_mode, size, -const_copy_size);
6289 target = adjust_address (target, BLKmode, const_copy_size);
6291 else
6293 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6294 copy_size_rtx, NULL_RTX, 0,
6295 OPTAB_LIB_WIDEN);
6297 if (GET_MODE (copy_size_rtx) != address_mode)
6298 copy_size_rtx = convert_to_mode (address_mode,
6299 copy_size_rtx,
6300 TYPE_UNSIGNED (sizetype));
6302 target = offset_address (target, copy_size_rtx,
6303 highest_pow2_factor (copy_size));
6304 label = gen_label_rtx ();
6305 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6306 GET_MODE (size), 0, label);
6309 if (size != const0_rtx)
6310 clear_storage (target, size, BLOCK_OP_NORMAL);
6312 if (label)
6313 emit_label (label);
6316 else if (shortened_string_cst)
6317 gcc_unreachable ();
6318 /* Handle calls that return values in multiple non-contiguous locations.
6319 The Irix 6 ABI has examples of this. */
6320 else if (GET_CODE (target) == PARALLEL)
6322 if (GET_CODE (temp) == PARALLEL)
6323 emit_group_move (target, temp);
6324 else
6325 emit_group_load (target, temp, TREE_TYPE (exp),
6326 int_size_in_bytes (TREE_TYPE (exp)));
6328 else if (GET_CODE (temp) == PARALLEL)
6329 emit_group_store (target, temp, TREE_TYPE (exp),
6330 int_size_in_bytes (TREE_TYPE (exp)));
6331 else if (GET_MODE (temp) == BLKmode)
6332 emit_block_move (target, temp, expr_size (exp),
6333 (call_param_p
6334 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6335 /* If we emit a nontemporal store, there is nothing else to do. */
6336 else if (nontemporal && emit_storent_insn (target, temp))
6338 else
6340 if (reverse)
6341 temp = flip_storage_order (GET_MODE (target), temp);
6342 temp = force_operand (temp, target);
6343 if (temp != target)
6344 emit_move_insn (target, temp);
6347 else
6348 gcc_assert (!shortened_string_cst);
6350 return NULL_RTX;
6353 /* Return true if field F of structure TYPE is a flexible array. */
6355 static bool
6356 flexible_array_member_p (const_tree f, const_tree type)
6358 const_tree tf;
6360 tf = TREE_TYPE (f);
6361 return (DECL_CHAIN (f) == NULL
6362 && TREE_CODE (tf) == ARRAY_TYPE
6363 && TYPE_DOMAIN (tf)
6364 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6365 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6366 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6367 && int_size_in_bytes (type) >= 0);
6370 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6371 must have in order for it to completely initialize a value of type TYPE.
6372 Return -1 if the number isn't known.
6374 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6376 static HOST_WIDE_INT
6377 count_type_elements (const_tree type, bool for_ctor_p)
6379 switch (TREE_CODE (type))
6381 case ARRAY_TYPE:
6383 tree nelts;
6385 nelts = array_type_nelts (type);
6386 if (nelts && tree_fits_uhwi_p (nelts))
6388 unsigned HOST_WIDE_INT n;
6390 n = tree_to_uhwi (nelts) + 1;
6391 if (n == 0 || for_ctor_p)
6392 return n;
6393 else
6394 return n * count_type_elements (TREE_TYPE (type), false);
6396 return for_ctor_p ? -1 : 1;
6399 case RECORD_TYPE:
6401 unsigned HOST_WIDE_INT n;
6402 tree f;
6404 n = 0;
6405 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6406 if (TREE_CODE (f) == FIELD_DECL)
6408 if (!for_ctor_p)
6409 n += count_type_elements (TREE_TYPE (f), false);
6410 else if (!flexible_array_member_p (f, type))
6411 /* Don't count flexible arrays, which are not supposed
6412 to be initialized. */
6413 n += 1;
6416 return n;
6419 case UNION_TYPE:
6420 case QUAL_UNION_TYPE:
6422 tree f;
6423 HOST_WIDE_INT n, m;
6425 gcc_assert (!for_ctor_p);
6426 /* Estimate the number of scalars in each field and pick the
6427 maximum. Other estimates would do instead; the idea is simply
6428 to make sure that the estimate is not sensitive to the ordering
6429 of the fields. */
6430 n = 1;
6431 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6432 if (TREE_CODE (f) == FIELD_DECL)
6434 m = count_type_elements (TREE_TYPE (f), false);
6435 /* If the field doesn't span the whole union, add an extra
6436 scalar for the rest. */
6437 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6438 TYPE_SIZE (type)) != 1)
6439 m++;
6440 if (n < m)
6441 n = m;
6443 return n;
6446 case COMPLEX_TYPE:
6447 return 2;
6449 case VECTOR_TYPE:
6451 unsigned HOST_WIDE_INT nelts;
6452 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6453 return nelts;
6454 else
6455 return -1;
6458 case INTEGER_TYPE:
6459 case REAL_TYPE:
6460 case FIXED_POINT_TYPE:
6461 case ENUMERAL_TYPE:
6462 case BOOLEAN_TYPE:
6463 case POINTER_TYPE:
6464 case OFFSET_TYPE:
6465 case REFERENCE_TYPE:
6466 case NULLPTR_TYPE:
6467 case OPAQUE_TYPE:
6468 return 1;
6470 case ERROR_MARK:
6471 return 0;
6473 case VOID_TYPE:
6474 case METHOD_TYPE:
6475 case FUNCTION_TYPE:
6476 case LANG_TYPE:
6477 default:
6478 gcc_unreachable ();
6482 /* Helper for categorize_ctor_elements. Identical interface. */
6484 static bool
6485 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6486 HOST_WIDE_INT *p_unique_nz_elts,
6487 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6489 unsigned HOST_WIDE_INT idx;
6490 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6491 tree value, purpose, elt_type;
6493 /* Whether CTOR is a valid constant initializer, in accordance with what
6494 initializer_constant_valid_p does. If inferred from the constructor
6495 elements, true until proven otherwise. */
6496 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6497 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6499 nz_elts = 0;
6500 unique_nz_elts = 0;
6501 init_elts = 0;
6502 num_fields = 0;
6503 elt_type = NULL_TREE;
6505 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6507 HOST_WIDE_INT mult = 1;
6509 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6511 tree lo_index = TREE_OPERAND (purpose, 0);
6512 tree hi_index = TREE_OPERAND (purpose, 1);
6514 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6515 mult = (tree_to_uhwi (hi_index)
6516 - tree_to_uhwi (lo_index) + 1);
6518 num_fields += mult;
6519 elt_type = TREE_TYPE (value);
6521 switch (TREE_CODE (value))
6523 case CONSTRUCTOR:
6525 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6527 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6528 &ic, p_complete);
6530 nz_elts += mult * nz;
6531 unique_nz_elts += unz;
6532 init_elts += mult * ic;
6534 if (const_from_elts_p && const_p)
6535 const_p = const_elt_p;
6537 break;
6539 case INTEGER_CST:
6540 case REAL_CST:
6541 case FIXED_CST:
6542 if (!initializer_zerop (value))
6544 nz_elts += mult;
6545 unique_nz_elts++;
6547 init_elts += mult;
6548 break;
6550 case STRING_CST:
6551 nz_elts += mult * TREE_STRING_LENGTH (value);
6552 unique_nz_elts += TREE_STRING_LENGTH (value);
6553 init_elts += mult * TREE_STRING_LENGTH (value);
6554 break;
6556 case COMPLEX_CST:
6557 if (!initializer_zerop (TREE_REALPART (value)))
6559 nz_elts += mult;
6560 unique_nz_elts++;
6562 if (!initializer_zerop (TREE_IMAGPART (value)))
6564 nz_elts += mult;
6565 unique_nz_elts++;
6567 init_elts += 2 * mult;
6568 break;
6570 case VECTOR_CST:
6572 /* We can only construct constant-length vectors using
6573 CONSTRUCTOR. */
6574 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6575 for (unsigned int i = 0; i < nunits; ++i)
6577 tree v = VECTOR_CST_ELT (value, i);
6578 if (!initializer_zerop (v))
6580 nz_elts += mult;
6581 unique_nz_elts++;
6583 init_elts += mult;
6586 break;
6588 default:
6590 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6591 nz_elts += mult * tc;
6592 unique_nz_elts += tc;
6593 init_elts += mult * tc;
6595 if (const_from_elts_p && const_p)
6596 const_p
6597 = initializer_constant_valid_p (value,
6598 elt_type,
6599 TYPE_REVERSE_STORAGE_ORDER
6600 (TREE_TYPE (ctor)))
6601 != NULL_TREE;
6603 break;
6607 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6608 num_fields, elt_type))
6609 *p_complete = false;
6611 *p_nz_elts += nz_elts;
6612 *p_unique_nz_elts += unique_nz_elts;
6613 *p_init_elts += init_elts;
6615 return const_p;
6618 /* Examine CTOR to discover:
6619 * how many scalar fields are set to nonzero values,
6620 and place it in *P_NZ_ELTS;
6621 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6622 high - low + 1 (this can be useful for callers to determine ctors
6623 that could be cheaply initialized with - perhaps nested - loops
6624 compared to copied from huge read-only data),
6625 and place it in *P_UNIQUE_NZ_ELTS;
6626 * how many scalar fields in total are in CTOR,
6627 and place it in *P_ELT_COUNT.
6628 * whether the constructor is complete -- in the sense that every
6629 meaningful byte is explicitly given a value --
6630 and place it in *P_COMPLETE.
6632 Return whether or not CTOR is a valid static constant initializer, the same
6633 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6635 bool
6636 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6637 HOST_WIDE_INT *p_unique_nz_elts,
6638 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6640 *p_nz_elts = 0;
6641 *p_unique_nz_elts = 0;
6642 *p_init_elts = 0;
6643 *p_complete = true;
6645 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6646 p_init_elts, p_complete);
6649 /* Return true if constructor CTOR is simple enough to be materialized
6650 in an integer mode register. Limit the size to WORDS words, which
6651 is 1 by default. */
6653 bool
6654 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6656 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6657 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6658 return false;
6660 return TREE_CONSTANT (ctor)
6661 && !TREE_ADDRESSABLE (ctor)
6662 && CONSTRUCTOR_NELTS (ctor)
6663 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6664 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6665 && initializer_constant_valid_for_bitfield_p (ctor);
6668 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6669 of which had type LAST_TYPE. Each element was itself a complete
6670 initializer, in the sense that every meaningful byte was explicitly
6671 given a value. Return true if the same is true for the constructor
6672 as a whole. */
6674 bool
6675 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6676 const_tree last_type)
6678 if (TREE_CODE (type) == UNION_TYPE
6679 || TREE_CODE (type) == QUAL_UNION_TYPE)
6681 if (num_elts == 0)
6682 return false;
6684 gcc_assert (num_elts == 1 && last_type);
6686 /* ??? We could look at each element of the union, and find the
6687 largest element. Which would avoid comparing the size of the
6688 initialized element against any tail padding in the union.
6689 Doesn't seem worth the effort... */
6690 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6693 return count_type_elements (type, true) == num_elts;
6696 /* Return 1 if EXP contains mostly (3/4) zeros. */
6698 static int
6699 mostly_zeros_p (const_tree exp)
6701 if (TREE_CODE (exp) == CONSTRUCTOR)
6703 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6704 bool complete_p;
6706 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6707 &complete_p);
6708 return !complete_p || nz_elts < init_elts / 4;
6711 return initializer_zerop (exp);
6714 /* Return 1 if EXP contains all zeros. */
6716 static int
6717 all_zeros_p (const_tree exp)
6719 if (TREE_CODE (exp) == CONSTRUCTOR)
6721 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6722 bool complete_p;
6724 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6725 &complete_p);
6726 return nz_elts == 0;
6729 return initializer_zerop (exp);
6732 /* Helper function for store_constructor.
6733 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6734 CLEARED is as for store_constructor.
6735 ALIAS_SET is the alias set to use for any stores.
6736 If REVERSE is true, the store is to be done in reverse order.
6738 This provides a recursive shortcut back to store_constructor when it isn't
6739 necessary to go through store_field. This is so that we can pass through
6740 the cleared field to let store_constructor know that we may not have to
6741 clear a substructure if the outer structure has already been cleared. */
6743 static void
6744 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6745 poly_uint64 bitregion_start,
6746 poly_uint64 bitregion_end,
6747 machine_mode mode,
6748 tree exp, int cleared,
6749 alias_set_type alias_set, bool reverse)
6751 poly_int64 bytepos;
6752 poly_uint64 bytesize;
6753 if (TREE_CODE (exp) == CONSTRUCTOR
6754 /* We can only call store_constructor recursively if the size and
6755 bit position are on a byte boundary. */
6756 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6757 && maybe_ne (bitsize, 0U)
6758 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6759 /* If we have a nonzero bitpos for a register target, then we just
6760 let store_field do the bitfield handling. This is unlikely to
6761 generate unnecessary clear instructions anyways. */
6762 && (known_eq (bitpos, 0) || MEM_P (target)))
6764 if (MEM_P (target))
6766 machine_mode target_mode = GET_MODE (target);
6767 if (target_mode != BLKmode
6768 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6769 target_mode = BLKmode;
6770 target = adjust_address (target, target_mode, bytepos);
6774 /* Update the alias set, if required. */
6775 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6776 && MEM_ALIAS_SET (target) != 0)
6778 target = copy_rtx (target);
6779 set_mem_alias_set (target, alias_set);
6782 store_constructor (exp, target, cleared, bytesize, reverse);
6784 else
6785 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6786 exp, alias_set, false, reverse);
6790 /* Returns the number of FIELD_DECLs in TYPE. */
6792 static int
6793 fields_length (const_tree type)
6795 tree t = TYPE_FIELDS (type);
6796 int count = 0;
6798 for (; t; t = DECL_CHAIN (t))
6799 if (TREE_CODE (t) == FIELD_DECL)
6800 ++count;
6802 return count;
6806 /* Store the value of constructor EXP into the rtx TARGET.
6807 TARGET is either a REG or a MEM; we know it cannot conflict, since
6808 safe_from_p has been called.
6809 CLEARED is true if TARGET is known to have been zero'd.
6810 SIZE is the number of bytes of TARGET we are allowed to modify: this
6811 may not be the same as the size of EXP if we are assigning to a field
6812 which has been packed to exclude padding bits.
6813 If REVERSE is true, the store is to be done in reverse order. */
6815 void
6816 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6817 bool reverse)
6819 tree type = TREE_TYPE (exp);
6820 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6821 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6823 switch (TREE_CODE (type))
6825 case RECORD_TYPE:
6826 case UNION_TYPE:
6827 case QUAL_UNION_TYPE:
6829 unsigned HOST_WIDE_INT idx;
6830 tree field, value;
6832 /* The storage order is specified for every aggregate type. */
6833 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6835 /* If size is zero or the target is already cleared, do nothing. */
6836 if (known_eq (size, 0) || cleared)
6837 cleared = 1;
6838 /* We either clear the aggregate or indicate the value is dead. */
6839 else if ((TREE_CODE (type) == UNION_TYPE
6840 || TREE_CODE (type) == QUAL_UNION_TYPE)
6841 && ! CONSTRUCTOR_ELTS (exp))
6842 /* If the constructor is empty, clear the union. */
6844 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6845 cleared = 1;
6848 /* If we are building a static constructor into a register,
6849 set the initial value as zero so we can fold the value into
6850 a constant. But if more than one register is involved,
6851 this probably loses. */
6852 else if (REG_P (target) && TREE_STATIC (exp)
6853 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6854 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6856 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6857 cleared = 1;
6860 /* If the constructor has fewer fields than the structure or
6861 if we are initializing the structure to mostly zeros, clear
6862 the whole structure first. Don't do this if TARGET is a
6863 register whose mode size isn't equal to SIZE since
6864 clear_storage can't handle this case. */
6865 else if (known_size_p (size)
6866 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6867 || mostly_zeros_p (exp))
6868 && (!REG_P (target)
6869 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6871 clear_storage (target, gen_int_mode (size, Pmode),
6872 BLOCK_OP_NORMAL);
6873 cleared = 1;
6876 if (REG_P (target) && !cleared)
6877 emit_clobber (target);
6879 /* Store each element of the constructor into the
6880 corresponding field of TARGET. */
6881 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6883 machine_mode mode;
6884 HOST_WIDE_INT bitsize;
6885 HOST_WIDE_INT bitpos = 0;
6886 tree offset;
6887 rtx to_rtx = target;
6889 /* Just ignore missing fields. We cleared the whole
6890 structure, above, if any fields are missing. */
6891 if (field == 0)
6892 continue;
6894 if (cleared && initializer_zerop (value))
6895 continue;
6897 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6898 bitsize = tree_to_uhwi (DECL_SIZE (field));
6899 else
6900 gcc_unreachable ();
6902 mode = DECL_MODE (field);
6903 if (DECL_BIT_FIELD (field))
6904 mode = VOIDmode;
6906 offset = DECL_FIELD_OFFSET (field);
6907 if (tree_fits_shwi_p (offset)
6908 && tree_fits_shwi_p (bit_position (field)))
6910 bitpos = int_bit_position (field);
6911 offset = NULL_TREE;
6913 else
6914 gcc_unreachable ();
6916 /* If this initializes a field that is smaller than a
6917 word, at the start of a word, try to widen it to a full
6918 word. This special case allows us to output C++ member
6919 function initializations in a form that the optimizers
6920 can understand. */
6921 if (WORD_REGISTER_OPERATIONS
6922 && REG_P (target)
6923 && bitsize < BITS_PER_WORD
6924 && bitpos % BITS_PER_WORD == 0
6925 && GET_MODE_CLASS (mode) == MODE_INT
6926 && TREE_CODE (value) == INTEGER_CST
6927 && exp_size >= 0
6928 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6930 type = TREE_TYPE (value);
6932 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6934 type = lang_hooks.types.type_for_mode
6935 (word_mode, TYPE_UNSIGNED (type));
6936 value = fold_convert (type, value);
6937 /* Make sure the bits beyond the original bitsize are zero
6938 so that we can correctly avoid extra zeroing stores in
6939 later constructor elements. */
6940 tree bitsize_mask
6941 = wide_int_to_tree (type, wi::mask (bitsize, false,
6942 BITS_PER_WORD));
6943 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6946 if (BYTES_BIG_ENDIAN)
6947 value
6948 = fold_build2 (LSHIFT_EXPR, type, value,
6949 build_int_cst (type,
6950 BITS_PER_WORD - bitsize));
6951 bitsize = BITS_PER_WORD;
6952 mode = word_mode;
6955 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6956 && DECL_NONADDRESSABLE_P (field))
6958 to_rtx = copy_rtx (to_rtx);
6959 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6962 store_constructor_field (to_rtx, bitsize, bitpos,
6963 0, bitregion_end, mode,
6964 value, cleared,
6965 get_alias_set (TREE_TYPE (field)),
6966 reverse);
6968 break;
6970 case ARRAY_TYPE:
6972 tree value, index;
6973 unsigned HOST_WIDE_INT i;
6974 int need_to_clear;
6975 tree domain;
6976 tree elttype = TREE_TYPE (type);
6977 int const_bounds_p;
6978 HOST_WIDE_INT minelt = 0;
6979 HOST_WIDE_INT maxelt = 0;
6981 /* The storage order is specified for every aggregate type. */
6982 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6984 domain = TYPE_DOMAIN (type);
6985 const_bounds_p = (TYPE_MIN_VALUE (domain)
6986 && TYPE_MAX_VALUE (domain)
6987 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6988 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6990 /* If we have constant bounds for the range of the type, get them. */
6991 if (const_bounds_p)
6993 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6994 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6997 /* If the constructor has fewer elements than the array, clear
6998 the whole array first. Similarly if this is static
6999 constructor of a non-BLKmode object. */
7000 if (cleared)
7001 need_to_clear = 0;
7002 else if (REG_P (target) && TREE_STATIC (exp))
7003 need_to_clear = 1;
7004 else
7006 unsigned HOST_WIDE_INT idx;
7007 HOST_WIDE_INT count = 0, zero_count = 0;
7008 need_to_clear = ! const_bounds_p;
7010 /* This loop is a more accurate version of the loop in
7011 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7012 is also needed to check for missing elements. */
7013 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7015 HOST_WIDE_INT this_node_count;
7017 if (need_to_clear)
7018 break;
7020 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7022 tree lo_index = TREE_OPERAND (index, 0);
7023 tree hi_index = TREE_OPERAND (index, 1);
7025 if (! tree_fits_uhwi_p (lo_index)
7026 || ! tree_fits_uhwi_p (hi_index))
7028 need_to_clear = 1;
7029 break;
7032 this_node_count = (tree_to_uhwi (hi_index)
7033 - tree_to_uhwi (lo_index) + 1);
7035 else
7036 this_node_count = 1;
7038 count += this_node_count;
7039 if (mostly_zeros_p (value))
7040 zero_count += this_node_count;
7043 /* Clear the entire array first if there are any missing
7044 elements, or if the incidence of zero elements is >=
7045 75%. */
7046 if (! need_to_clear
7047 && (count < maxelt - minelt + 1
7048 || 4 * zero_count >= 3 * count))
7049 need_to_clear = 1;
7052 if (need_to_clear && maybe_gt (size, 0))
7054 if (REG_P (target))
7055 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7056 else
7057 clear_storage (target, gen_int_mode (size, Pmode),
7058 BLOCK_OP_NORMAL);
7059 cleared = 1;
7062 if (!cleared && REG_P (target))
7063 /* Inform later passes that the old value is dead. */
7064 emit_clobber (target);
7066 /* Store each element of the constructor into the
7067 corresponding element of TARGET, determined by counting the
7068 elements. */
7069 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7071 machine_mode mode;
7072 poly_int64 bitsize;
7073 HOST_WIDE_INT bitpos;
7074 rtx xtarget = target;
7076 if (cleared && initializer_zerop (value))
7077 continue;
7079 mode = TYPE_MODE (elttype);
7080 if (mode != BLKmode)
7081 bitsize = GET_MODE_BITSIZE (mode);
7082 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7083 bitsize = -1;
7085 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7087 tree lo_index = TREE_OPERAND (index, 0);
7088 tree hi_index = TREE_OPERAND (index, 1);
7089 rtx index_r, pos_rtx;
7090 HOST_WIDE_INT lo, hi, count;
7091 tree position;
7093 /* If the range is constant and "small", unroll the loop. */
7094 if (const_bounds_p
7095 && tree_fits_shwi_p (lo_index)
7096 && tree_fits_shwi_p (hi_index)
7097 && (lo = tree_to_shwi (lo_index),
7098 hi = tree_to_shwi (hi_index),
7099 count = hi - lo + 1,
7100 (!MEM_P (target)
7101 || count <= 2
7102 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7103 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7104 <= 40 * 8)))))
7106 lo -= minelt; hi -= minelt;
7107 for (; lo <= hi; lo++)
7109 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7111 if (MEM_P (target)
7112 && !MEM_KEEP_ALIAS_SET_P (target)
7113 && TREE_CODE (type) == ARRAY_TYPE
7114 && TYPE_NONALIASED_COMPONENT (type))
7116 target = copy_rtx (target);
7117 MEM_KEEP_ALIAS_SET_P (target) = 1;
7120 store_constructor_field
7121 (target, bitsize, bitpos, 0, bitregion_end,
7122 mode, value, cleared,
7123 get_alias_set (elttype), reverse);
7126 else
7128 rtx_code_label *loop_start = gen_label_rtx ();
7129 rtx_code_label *loop_end = gen_label_rtx ();
7130 tree exit_cond;
7132 expand_normal (hi_index);
7134 index = build_decl (EXPR_LOCATION (exp),
7135 VAR_DECL, NULL_TREE, domain);
7136 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7137 SET_DECL_RTL (index, index_r);
7138 store_expr (lo_index, index_r, 0, false, reverse);
7140 /* Build the head of the loop. */
7141 do_pending_stack_adjust ();
7142 emit_label (loop_start);
7144 /* Assign value to element index. */
7145 position =
7146 fold_convert (ssizetype,
7147 fold_build2 (MINUS_EXPR,
7148 TREE_TYPE (index),
7149 index,
7150 TYPE_MIN_VALUE (domain)));
7152 position =
7153 size_binop (MULT_EXPR, position,
7154 fold_convert (ssizetype,
7155 TYPE_SIZE_UNIT (elttype)));
7157 pos_rtx = expand_normal (position);
7158 xtarget = offset_address (target, pos_rtx,
7159 highest_pow2_factor (position));
7160 xtarget = adjust_address (xtarget, mode, 0);
7161 if (TREE_CODE (value) == CONSTRUCTOR)
7162 store_constructor (value, xtarget, cleared,
7163 exact_div (bitsize, BITS_PER_UNIT),
7164 reverse);
7165 else
7166 store_expr (value, xtarget, 0, false, reverse);
7168 /* Generate a conditional jump to exit the loop. */
7169 exit_cond = build2 (LT_EXPR, integer_type_node,
7170 index, hi_index);
7171 jumpif (exit_cond, loop_end,
7172 profile_probability::uninitialized ());
7174 /* Update the loop counter, and jump to the head of
7175 the loop. */
7176 expand_assignment (index,
7177 build2 (PLUS_EXPR, TREE_TYPE (index),
7178 index, integer_one_node),
7179 false);
7181 emit_jump (loop_start);
7183 /* Build the end of the loop. */
7184 emit_label (loop_end);
7187 else if ((index != 0 && ! tree_fits_shwi_p (index))
7188 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7190 tree position;
7192 if (index == 0)
7193 index = ssize_int (1);
7195 if (minelt)
7196 index = fold_convert (ssizetype,
7197 fold_build2 (MINUS_EXPR,
7198 TREE_TYPE (index),
7199 index,
7200 TYPE_MIN_VALUE (domain)));
7202 position =
7203 size_binop (MULT_EXPR, index,
7204 fold_convert (ssizetype,
7205 TYPE_SIZE_UNIT (elttype)));
7206 xtarget = offset_address (target,
7207 expand_normal (position),
7208 highest_pow2_factor (position));
7209 xtarget = adjust_address (xtarget, mode, 0);
7210 store_expr (value, xtarget, 0, false, reverse);
7212 else
7214 if (index != 0)
7215 bitpos = ((tree_to_shwi (index) - minelt)
7216 * tree_to_uhwi (TYPE_SIZE (elttype)));
7217 else
7218 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7220 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7221 && TREE_CODE (type) == ARRAY_TYPE
7222 && TYPE_NONALIASED_COMPONENT (type))
7224 target = copy_rtx (target);
7225 MEM_KEEP_ALIAS_SET_P (target) = 1;
7227 store_constructor_field (target, bitsize, bitpos, 0,
7228 bitregion_end, mode, value,
7229 cleared, get_alias_set (elttype),
7230 reverse);
7233 break;
7236 case VECTOR_TYPE:
7238 unsigned HOST_WIDE_INT idx;
7239 constructor_elt *ce;
7240 int i;
7241 int need_to_clear;
7242 insn_code icode = CODE_FOR_nothing;
7243 tree elt;
7244 tree elttype = TREE_TYPE (type);
7245 int elt_size = vector_element_bits (type);
7246 machine_mode eltmode = TYPE_MODE (elttype);
7247 HOST_WIDE_INT bitsize;
7248 HOST_WIDE_INT bitpos;
7249 rtvec vector = NULL;
7250 poly_uint64 n_elts;
7251 unsigned HOST_WIDE_INT const_n_elts;
7252 alias_set_type alias;
7253 bool vec_vec_init_p = false;
7254 machine_mode mode = GET_MODE (target);
7256 gcc_assert (eltmode != BLKmode);
7258 /* Try using vec_duplicate_optab for uniform vectors. */
7259 if (!TREE_SIDE_EFFECTS (exp)
7260 && VECTOR_MODE_P (mode)
7261 && eltmode == GET_MODE_INNER (mode)
7262 && ((icode = optab_handler (vec_duplicate_optab, mode))
7263 != CODE_FOR_nothing)
7264 && (elt = uniform_vector_p (exp))
7265 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7267 class expand_operand ops[2];
7268 create_output_operand (&ops[0], target, mode);
7269 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7270 expand_insn (icode, 2, ops);
7271 if (!rtx_equal_p (target, ops[0].value))
7272 emit_move_insn (target, ops[0].value);
7273 break;
7276 n_elts = TYPE_VECTOR_SUBPARTS (type);
7277 if (REG_P (target)
7278 && VECTOR_MODE_P (mode)
7279 && n_elts.is_constant (&const_n_elts))
7281 machine_mode emode = eltmode;
7282 bool vector_typed_elts_p = false;
7284 if (CONSTRUCTOR_NELTS (exp)
7285 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7286 == VECTOR_TYPE))
7288 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7289 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7290 * TYPE_VECTOR_SUBPARTS (etype),
7291 n_elts));
7292 emode = TYPE_MODE (etype);
7293 vector_typed_elts_p = true;
7295 icode = convert_optab_handler (vec_init_optab, mode, emode);
7296 if (icode != CODE_FOR_nothing)
7298 unsigned int n = const_n_elts;
7300 if (vector_typed_elts_p)
7302 n = CONSTRUCTOR_NELTS (exp);
7303 vec_vec_init_p = true;
7305 vector = rtvec_alloc (n);
7306 for (unsigned int k = 0; k < n; k++)
7307 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7311 /* Compute the size of the elements in the CTOR. It differs
7312 from the size of the vector type elements only when the
7313 CTOR elements are vectors themselves. */
7314 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7315 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7316 : elttype);
7317 if (VECTOR_TYPE_P (val_type))
7318 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7319 else
7320 bitsize = elt_size;
7322 /* If the constructor has fewer elements than the vector,
7323 clear the whole array first. Similarly if this is static
7324 constructor of a non-BLKmode object. */
7325 if (cleared)
7326 need_to_clear = 0;
7327 else if (REG_P (target) && TREE_STATIC (exp))
7328 need_to_clear = 1;
7329 else
7331 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7332 tree value;
7334 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7336 int n_elts_here = bitsize / elt_size;
7337 count += n_elts_here;
7338 if (mostly_zeros_p (value))
7339 zero_count += n_elts_here;
7342 /* Clear the entire vector first if there are any missing elements,
7343 or if the incidence of zero elements is >= 75%. */
7344 need_to_clear = (maybe_lt (count, n_elts)
7345 || 4 * zero_count >= 3 * count);
7348 if (need_to_clear && maybe_gt (size, 0) && !vector)
7350 if (REG_P (target))
7351 emit_move_insn (target, CONST0_RTX (mode));
7352 else
7353 clear_storage (target, gen_int_mode (size, Pmode),
7354 BLOCK_OP_NORMAL);
7355 cleared = 1;
7358 /* Inform later passes that the old value is dead. */
7359 if (!cleared && !vector && REG_P (target))
7360 emit_move_insn (target, CONST0_RTX (mode));
7362 if (MEM_P (target))
7363 alias = MEM_ALIAS_SET (target);
7364 else
7365 alias = get_alias_set (elttype);
7367 /* Store each element of the constructor into the corresponding
7368 element of TARGET, determined by counting the elements. */
7369 for (idx = 0, i = 0;
7370 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7371 idx++, i += bitsize / elt_size)
7373 HOST_WIDE_INT eltpos;
7374 tree value = ce->value;
7376 if (cleared && initializer_zerop (value))
7377 continue;
7379 if (ce->index)
7380 eltpos = tree_to_uhwi (ce->index);
7381 else
7382 eltpos = i;
7384 if (vector)
7386 if (vec_vec_init_p)
7388 gcc_assert (ce->index == NULL_TREE);
7389 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7390 eltpos = idx;
7392 else
7393 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7394 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7396 else
7398 machine_mode value_mode
7399 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7400 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7401 bitpos = eltpos * elt_size;
7402 store_constructor_field (target, bitsize, bitpos, 0,
7403 bitregion_end, value_mode,
7404 value, cleared, alias, reverse);
7408 if (vector)
7409 emit_insn (GEN_FCN (icode) (target,
7410 gen_rtx_PARALLEL (mode, vector)));
7411 break;
7414 default:
7415 gcc_unreachable ();
7419 /* Store the value of EXP (an expression tree)
7420 into a subfield of TARGET which has mode MODE and occupies
7421 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7422 If MODE is VOIDmode, it means that we are storing into a bit-field.
7424 BITREGION_START is bitpos of the first bitfield in this region.
7425 BITREGION_END is the bitpos of the ending bitfield in this region.
7426 These two fields are 0, if the C++ memory model does not apply,
7427 or we are not interested in keeping track of bitfield regions.
7429 Always return const0_rtx unless we have something particular to
7430 return.
7432 ALIAS_SET is the alias set for the destination. This value will
7433 (in general) be different from that for TARGET, since TARGET is a
7434 reference to the containing structure.
7436 If NONTEMPORAL is true, try generating a nontemporal store.
7438 If REVERSE is true, the store is to be done in reverse order. */
7440 static rtx
7441 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7442 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7443 machine_mode mode, tree exp,
7444 alias_set_type alias_set, bool nontemporal, bool reverse)
7446 if (TREE_CODE (exp) == ERROR_MARK)
7447 return const0_rtx;
7449 /* If we have nothing to store, do nothing unless the expression has
7450 side-effects. Don't do that for zero sized addressable lhs of
7451 calls. */
7452 if (known_eq (bitsize, 0)
7453 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7454 || TREE_CODE (exp) != CALL_EXPR))
7455 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7457 if (GET_CODE (target) == CONCAT)
7459 /* We're storing into a struct containing a single __complex. */
7461 gcc_assert (known_eq (bitpos, 0));
7462 return store_expr (exp, target, 0, nontemporal, reverse);
7465 /* If the structure is in a register or if the component
7466 is a bit field, we cannot use addressing to access it.
7467 Use bit-field techniques or SUBREG to store in it. */
7469 poly_int64 decl_bitsize;
7470 if (mode == VOIDmode
7471 || (mode != BLKmode && ! direct_store[(int) mode]
7472 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7473 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7474 || REG_P (target)
7475 || GET_CODE (target) == SUBREG
7476 /* If the field isn't aligned enough to store as an ordinary memref,
7477 store it as a bit field. */
7478 || (mode != BLKmode
7479 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7480 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7481 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7482 || !multiple_p (bitpos, BITS_PER_UNIT)))
7483 || (known_size_p (bitsize)
7484 && mode != BLKmode
7485 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7486 /* If the RHS and field are a constant size and the size of the
7487 RHS isn't the same size as the bitfield, we must use bitfield
7488 operations. */
7489 || (known_size_p (bitsize)
7490 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7491 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7492 bitsize)
7493 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7494 we will handle specially below. */
7495 && !(TREE_CODE (exp) == CONSTRUCTOR
7496 && multiple_p (bitsize, BITS_PER_UNIT))
7497 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7498 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7499 includes some extra padding. store_expr / expand_expr will in
7500 that case call get_inner_reference that will have the bitsize
7501 we check here and thus the block move will not clobber the
7502 padding that shouldn't be clobbered. In the future we could
7503 replace the TREE_ADDRESSABLE check with a check that
7504 get_base_address needs to live in memory. */
7505 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7506 || TREE_CODE (exp) != COMPONENT_REF
7507 || !multiple_p (bitsize, BITS_PER_UNIT)
7508 || !multiple_p (bitpos, BITS_PER_UNIT)
7509 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7510 &decl_bitsize)
7511 || maybe_ne (decl_bitsize, bitsize))
7512 /* A call with an addressable return type and return-slot
7513 optimization must not need bitfield operations but we must
7514 pass down the original target. */
7515 && (TREE_CODE (exp) != CALL_EXPR
7516 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7517 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7518 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7519 decl we must use bitfield operations. */
7520 || (known_size_p (bitsize)
7521 && TREE_CODE (exp) == MEM_REF
7522 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7523 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7524 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7525 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7527 rtx temp;
7528 gimple *nop_def;
7530 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7531 implies a mask operation. If the precision is the same size as
7532 the field we're storing into, that mask is redundant. This is
7533 particularly common with bit field assignments generated by the
7534 C front end. */
7535 nop_def = get_def_for_expr (exp, NOP_EXPR);
7536 if (nop_def)
7538 tree type = TREE_TYPE (exp);
7539 if (INTEGRAL_TYPE_P (type)
7540 && maybe_ne (TYPE_PRECISION (type),
7541 GET_MODE_BITSIZE (TYPE_MODE (type)))
7542 && known_eq (bitsize, TYPE_PRECISION (type)))
7544 tree op = gimple_assign_rhs1 (nop_def);
7545 type = TREE_TYPE (op);
7546 if (INTEGRAL_TYPE_P (type)
7547 && known_ge (TYPE_PRECISION (type), bitsize))
7548 exp = op;
7552 temp = expand_normal (exp);
7554 /* We don't support variable-sized BLKmode bitfields, since our
7555 handling of BLKmode is bound up with the ability to break
7556 things into words. */
7557 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7559 /* Handle calls that return values in multiple non-contiguous locations.
7560 The Irix 6 ABI has examples of this. */
7561 if (GET_CODE (temp) == PARALLEL)
7563 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7564 machine_mode temp_mode = GET_MODE (temp);
7565 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7566 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7567 rtx temp_target = gen_reg_rtx (temp_mode);
7568 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7569 temp = temp_target;
7572 /* Handle calls that return BLKmode values in registers. */
7573 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7575 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7576 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7577 temp = temp_target;
7580 /* If the value has aggregate type and an integral mode then, if BITSIZE
7581 is narrower than this mode and this is for big-endian data, we first
7582 need to put the value into the low-order bits for store_bit_field,
7583 except when MODE is BLKmode and BITSIZE larger than the word size
7584 (see the handling of fields larger than a word in store_bit_field).
7585 Moreover, the field may be not aligned on a byte boundary; in this
7586 case, if it has reverse storage order, it needs to be accessed as a
7587 scalar field with reverse storage order and we must first put the
7588 value into target order. */
7589 scalar_int_mode temp_mode;
7590 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7591 && is_int_mode (GET_MODE (temp), &temp_mode))
7593 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7595 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7597 if (reverse)
7598 temp = flip_storage_order (temp_mode, temp);
7600 gcc_checking_assert (known_le (bitsize, size));
7601 if (maybe_lt (bitsize, size)
7602 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7603 /* Use of to_constant for BLKmode was checked above. */
7604 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7605 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7606 size - bitsize, NULL_RTX, 1);
7609 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7610 if (mode != VOIDmode && mode != BLKmode
7611 && mode != TYPE_MODE (TREE_TYPE (exp)))
7612 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7614 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7615 and BITPOS must be aligned on a byte boundary. If so, we simply do
7616 a block copy. Likewise for a BLKmode-like TARGET. */
7617 if (GET_MODE (temp) == BLKmode
7618 && (GET_MODE (target) == BLKmode
7619 || (MEM_P (target)
7620 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7621 && multiple_p (bitpos, BITS_PER_UNIT)
7622 && multiple_p (bitsize, BITS_PER_UNIT))))
7624 gcc_assert (MEM_P (target) && MEM_P (temp));
7625 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7626 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7628 target = adjust_address (target, VOIDmode, bytepos);
7629 emit_block_move (target, temp,
7630 gen_int_mode (bytesize, Pmode),
7631 BLOCK_OP_NORMAL);
7633 return const0_rtx;
7636 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7637 word size, we need to load the value (see again store_bit_field). */
7638 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7640 temp_mode = smallest_int_mode_for_size (bitsize);
7641 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7642 temp_mode, false, NULL);
7645 /* Store the value in the bitfield. */
7646 gcc_checking_assert (known_ge (bitpos, 0));
7647 store_bit_field (target, bitsize, bitpos,
7648 bitregion_start, bitregion_end,
7649 mode, temp, reverse, false);
7651 return const0_rtx;
7653 else
7655 /* Now build a reference to just the desired component. */
7656 rtx to_rtx = adjust_address (target, mode,
7657 exact_div (bitpos, BITS_PER_UNIT));
7659 if (to_rtx == target)
7660 to_rtx = copy_rtx (to_rtx);
7662 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7663 set_mem_alias_set (to_rtx, alias_set);
7665 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7666 into a target smaller than its type; handle that case now. */
7667 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7669 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7670 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7671 return to_rtx;
7674 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7678 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7679 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7680 codes and find the ultimate containing object, which we return.
7682 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7683 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7684 storage order of the field.
7685 If the position of the field is variable, we store a tree
7686 giving the variable offset (in units) in *POFFSET.
7687 This offset is in addition to the bit position.
7688 If the position is not variable, we store 0 in *POFFSET.
7690 If any of the extraction expressions is volatile,
7691 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7693 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7694 Otherwise, it is a mode that can be used to access the field.
7696 If the field describes a variable-sized object, *PMODE is set to
7697 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7698 this case, but the address of the object can be found. */
7700 tree
7701 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7702 poly_int64_pod *pbitpos, tree *poffset,
7703 machine_mode *pmode, int *punsignedp,
7704 int *preversep, int *pvolatilep)
7706 tree size_tree = 0;
7707 machine_mode mode = VOIDmode;
7708 bool blkmode_bitfield = false;
7709 tree offset = size_zero_node;
7710 poly_offset_int bit_offset = 0;
7712 /* First get the mode, signedness, storage order and size. We do this from
7713 just the outermost expression. */
7714 *pbitsize = -1;
7715 if (TREE_CODE (exp) == COMPONENT_REF)
7717 tree field = TREE_OPERAND (exp, 1);
7718 size_tree = DECL_SIZE (field);
7719 if (flag_strict_volatile_bitfields > 0
7720 && TREE_THIS_VOLATILE (exp)
7721 && DECL_BIT_FIELD_TYPE (field)
7722 && DECL_MODE (field) != BLKmode)
7723 /* Volatile bitfields should be accessed in the mode of the
7724 field's type, not the mode computed based on the bit
7725 size. */
7726 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7727 else if (!DECL_BIT_FIELD (field))
7729 mode = DECL_MODE (field);
7730 /* For vector fields re-check the target flags, as DECL_MODE
7731 could have been set with different target flags than
7732 the current function has. */
7733 if (mode == BLKmode
7734 && VECTOR_TYPE_P (TREE_TYPE (field))
7735 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7736 mode = TYPE_MODE (TREE_TYPE (field));
7738 else if (DECL_MODE (field) == BLKmode)
7739 blkmode_bitfield = true;
7741 *punsignedp = DECL_UNSIGNED (field);
7743 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7745 size_tree = TREE_OPERAND (exp, 1);
7746 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7747 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7749 /* For vector element types with the correct size of access or for
7750 vector typed accesses use the mode of the access type. */
7751 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7752 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7753 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7754 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7755 mode = TYPE_MODE (TREE_TYPE (exp));
7757 else
7759 mode = TYPE_MODE (TREE_TYPE (exp));
7760 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7762 if (mode == BLKmode)
7763 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7764 else
7765 *pbitsize = GET_MODE_BITSIZE (mode);
7768 if (size_tree != 0)
7770 if (! tree_fits_uhwi_p (size_tree))
7771 mode = BLKmode, *pbitsize = -1;
7772 else
7773 *pbitsize = tree_to_uhwi (size_tree);
7776 *preversep = reverse_storage_order_for_component_p (exp);
7778 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7779 and find the ultimate containing object. */
7780 while (1)
7782 switch (TREE_CODE (exp))
7784 case BIT_FIELD_REF:
7785 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7786 break;
7788 case COMPONENT_REF:
7790 tree field = TREE_OPERAND (exp, 1);
7791 tree this_offset = component_ref_field_offset (exp);
7793 /* If this field hasn't been filled in yet, don't go past it.
7794 This should only happen when folding expressions made during
7795 type construction. */
7796 if (this_offset == 0)
7797 break;
7799 offset = size_binop (PLUS_EXPR, offset, this_offset);
7800 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7802 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7804 break;
7806 case ARRAY_REF:
7807 case ARRAY_RANGE_REF:
7809 tree index = TREE_OPERAND (exp, 1);
7810 tree low_bound = array_ref_low_bound (exp);
7811 tree unit_size = array_ref_element_size (exp);
7813 /* We assume all arrays have sizes that are a multiple of a byte.
7814 First subtract the lower bound, if any, in the type of the
7815 index, then convert to sizetype and multiply by the size of
7816 the array element. */
7817 if (! integer_zerop (low_bound))
7818 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7819 index, low_bound);
7821 offset = size_binop (PLUS_EXPR, offset,
7822 size_binop (MULT_EXPR,
7823 fold_convert (sizetype, index),
7824 unit_size));
7826 break;
7828 case REALPART_EXPR:
7829 break;
7831 case IMAGPART_EXPR:
7832 bit_offset += *pbitsize;
7833 break;
7835 case VIEW_CONVERT_EXPR:
7836 break;
7838 case MEM_REF:
7839 /* Hand back the decl for MEM[&decl, off]. */
7840 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7842 tree off = TREE_OPERAND (exp, 1);
7843 if (!integer_zerop (off))
7845 poly_offset_int boff = mem_ref_offset (exp);
7846 boff <<= LOG2_BITS_PER_UNIT;
7847 bit_offset += boff;
7849 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7851 goto done;
7853 default:
7854 goto done;
7857 /* If any reference in the chain is volatile, the effect is volatile. */
7858 if (TREE_THIS_VOLATILE (exp))
7859 *pvolatilep = 1;
7861 exp = TREE_OPERAND (exp, 0);
7863 done:
7865 /* If OFFSET is constant, see if we can return the whole thing as a
7866 constant bit position. Make sure to handle overflow during
7867 this conversion. */
7868 if (poly_int_tree_p (offset))
7870 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7871 TYPE_PRECISION (sizetype));
7872 tem <<= LOG2_BITS_PER_UNIT;
7873 tem += bit_offset;
7874 if (tem.to_shwi (pbitpos))
7875 *poffset = offset = NULL_TREE;
7878 /* Otherwise, split it up. */
7879 if (offset)
7881 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7882 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7884 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7885 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7886 offset = size_binop (PLUS_EXPR, offset,
7887 build_int_cst (sizetype, bytes.force_shwi ()));
7890 *poffset = offset;
7893 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7894 if (mode == VOIDmode
7895 && blkmode_bitfield
7896 && multiple_p (*pbitpos, BITS_PER_UNIT)
7897 && multiple_p (*pbitsize, BITS_PER_UNIT))
7898 *pmode = BLKmode;
7899 else
7900 *pmode = mode;
7902 return exp;
7905 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7907 static unsigned HOST_WIDE_INT
7908 target_align (const_tree target)
7910 /* We might have a chain of nested references with intermediate misaligning
7911 bitfields components, so need to recurse to find out. */
7913 unsigned HOST_WIDE_INT this_align, outer_align;
7915 switch (TREE_CODE (target))
7917 case BIT_FIELD_REF:
7918 return 1;
7920 case COMPONENT_REF:
7921 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7922 outer_align = target_align (TREE_OPERAND (target, 0));
7923 return MIN (this_align, outer_align);
7925 case ARRAY_REF:
7926 case ARRAY_RANGE_REF:
7927 this_align = TYPE_ALIGN (TREE_TYPE (target));
7928 outer_align = target_align (TREE_OPERAND (target, 0));
7929 return MIN (this_align, outer_align);
7931 CASE_CONVERT:
7932 case NON_LVALUE_EXPR:
7933 case VIEW_CONVERT_EXPR:
7934 this_align = TYPE_ALIGN (TREE_TYPE (target));
7935 outer_align = target_align (TREE_OPERAND (target, 0));
7936 return MAX (this_align, outer_align);
7938 default:
7939 return TYPE_ALIGN (TREE_TYPE (target));
7944 /* Given an rtx VALUE that may contain additions and multiplications, return
7945 an equivalent value that just refers to a register, memory, or constant.
7946 This is done by generating instructions to perform the arithmetic and
7947 returning a pseudo-register containing the value.
7949 The returned value may be a REG, SUBREG, MEM or constant. */
7952 force_operand (rtx value, rtx target)
7954 rtx op1, op2;
7955 /* Use subtarget as the target for operand 0 of a binary operation. */
7956 rtx subtarget = get_subtarget (target);
7957 enum rtx_code code = GET_CODE (value);
7959 /* Check for subreg applied to an expression produced by loop optimizer. */
7960 if (code == SUBREG
7961 && !REG_P (SUBREG_REG (value))
7962 && !MEM_P (SUBREG_REG (value)))
7964 value
7965 = simplify_gen_subreg (GET_MODE (value),
7966 force_reg (GET_MODE (SUBREG_REG (value)),
7967 force_operand (SUBREG_REG (value),
7968 NULL_RTX)),
7969 GET_MODE (SUBREG_REG (value)),
7970 SUBREG_BYTE (value));
7971 code = GET_CODE (value);
7974 /* Check for a PIC address load. */
7975 if ((code == PLUS || code == MINUS)
7976 && XEXP (value, 0) == pic_offset_table_rtx
7977 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7978 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7979 || GET_CODE (XEXP (value, 1)) == CONST))
7981 if (!subtarget)
7982 subtarget = gen_reg_rtx (GET_MODE (value));
7983 emit_move_insn (subtarget, value);
7984 return subtarget;
7987 if (ARITHMETIC_P (value))
7989 op2 = XEXP (value, 1);
7990 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7991 subtarget = 0;
7992 if (code == MINUS && CONST_INT_P (op2))
7994 code = PLUS;
7995 op2 = negate_rtx (GET_MODE (value), op2);
7998 /* Check for an addition with OP2 a constant integer and our first
7999 operand a PLUS of a virtual register and something else. In that
8000 case, we want to emit the sum of the virtual register and the
8001 constant first and then add the other value. This allows virtual
8002 register instantiation to simply modify the constant rather than
8003 creating another one around this addition. */
8004 if (code == PLUS && CONST_INT_P (op2)
8005 && GET_CODE (XEXP (value, 0)) == PLUS
8006 && REG_P (XEXP (XEXP (value, 0), 0))
8007 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
8008 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
8010 rtx temp = expand_simple_binop (GET_MODE (value), code,
8011 XEXP (XEXP (value, 0), 0), op2,
8012 subtarget, 0, OPTAB_LIB_WIDEN);
8013 return expand_simple_binop (GET_MODE (value), code, temp,
8014 force_operand (XEXP (XEXP (value,
8015 0), 1), 0),
8016 target, 0, OPTAB_LIB_WIDEN);
8019 op1 = force_operand (XEXP (value, 0), subtarget);
8020 op2 = force_operand (op2, NULL_RTX);
8021 switch (code)
8023 case MULT:
8024 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8025 case DIV:
8026 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8027 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8028 target, 1, OPTAB_LIB_WIDEN);
8029 else
8030 return expand_divmod (0,
8031 FLOAT_MODE_P (GET_MODE (value))
8032 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8033 GET_MODE (value), op1, op2, target, 0);
8034 case MOD:
8035 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8036 target, 0);
8037 case UDIV:
8038 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8039 target, 1);
8040 case UMOD:
8041 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8042 target, 1);
8043 case ASHIFTRT:
8044 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8045 target, 0, OPTAB_LIB_WIDEN);
8046 default:
8047 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8048 target, 1, OPTAB_LIB_WIDEN);
8051 if (UNARY_P (value))
8053 if (!target)
8054 target = gen_reg_rtx (GET_MODE (value));
8055 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8056 switch (code)
8058 case ZERO_EXTEND:
8059 case SIGN_EXTEND:
8060 case TRUNCATE:
8061 case FLOAT_EXTEND:
8062 case FLOAT_TRUNCATE:
8063 convert_move (target, op1, code == ZERO_EXTEND);
8064 return target;
8066 case FIX:
8067 case UNSIGNED_FIX:
8068 expand_fix (target, op1, code == UNSIGNED_FIX);
8069 return target;
8071 case FLOAT:
8072 case UNSIGNED_FLOAT:
8073 expand_float (target, op1, code == UNSIGNED_FLOAT);
8074 return target;
8076 default:
8077 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8081 #ifdef INSN_SCHEDULING
8082 /* On machines that have insn scheduling, we want all memory reference to be
8083 explicit, so we need to deal with such paradoxical SUBREGs. */
8084 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8085 value
8086 = simplify_gen_subreg (GET_MODE (value),
8087 force_reg (GET_MODE (SUBREG_REG (value)),
8088 force_operand (SUBREG_REG (value),
8089 NULL_RTX)),
8090 GET_MODE (SUBREG_REG (value)),
8091 SUBREG_BYTE (value));
8092 #endif
8094 return value;
8097 /* Subroutine of expand_expr: return nonzero iff there is no way that
8098 EXP can reference X, which is being modified. TOP_P is nonzero if this
8099 call is going to be used to determine whether we need a temporary
8100 for EXP, as opposed to a recursive call to this function.
8102 It is always safe for this routine to return zero since it merely
8103 searches for optimization opportunities. */
8106 safe_from_p (const_rtx x, tree exp, int top_p)
8108 rtx exp_rtl = 0;
8109 int i, nops;
8111 if (x == 0
8112 /* If EXP has varying size, we MUST use a target since we currently
8113 have no way of allocating temporaries of variable size
8114 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8115 So we assume here that something at a higher level has prevented a
8116 clash. This is somewhat bogus, but the best we can do. Only
8117 do this when X is BLKmode and when we are at the top level. */
8118 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8119 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8120 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8121 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8122 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8123 != INTEGER_CST)
8124 && GET_MODE (x) == BLKmode)
8125 /* If X is in the outgoing argument area, it is always safe. */
8126 || (MEM_P (x)
8127 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8128 || (GET_CODE (XEXP (x, 0)) == PLUS
8129 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8130 return 1;
8132 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8133 find the underlying pseudo. */
8134 if (GET_CODE (x) == SUBREG)
8136 x = SUBREG_REG (x);
8137 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8138 return 0;
8141 /* Now look at our tree code and possibly recurse. */
8142 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8144 case tcc_declaration:
8145 exp_rtl = DECL_RTL_IF_SET (exp);
8146 break;
8148 case tcc_constant:
8149 return 1;
8151 case tcc_exceptional:
8152 if (TREE_CODE (exp) == TREE_LIST)
8154 while (1)
8156 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8157 return 0;
8158 exp = TREE_CHAIN (exp);
8159 if (!exp)
8160 return 1;
8161 if (TREE_CODE (exp) != TREE_LIST)
8162 return safe_from_p (x, exp, 0);
8165 else if (TREE_CODE (exp) == CONSTRUCTOR)
8167 constructor_elt *ce;
8168 unsigned HOST_WIDE_INT idx;
8170 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8171 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8172 || !safe_from_p (x, ce->value, 0))
8173 return 0;
8174 return 1;
8176 else if (TREE_CODE (exp) == ERROR_MARK)
8177 return 1; /* An already-visited SAVE_EXPR? */
8178 else
8179 return 0;
8181 case tcc_statement:
8182 /* The only case we look at here is the DECL_INITIAL inside a
8183 DECL_EXPR. */
8184 return (TREE_CODE (exp) != DECL_EXPR
8185 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8186 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8187 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8189 case tcc_binary:
8190 case tcc_comparison:
8191 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8192 return 0;
8193 /* Fall through. */
8195 case tcc_unary:
8196 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8198 case tcc_expression:
8199 case tcc_reference:
8200 case tcc_vl_exp:
8201 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8202 the expression. If it is set, we conflict iff we are that rtx or
8203 both are in memory. Otherwise, we check all operands of the
8204 expression recursively. */
8206 switch (TREE_CODE (exp))
8208 case ADDR_EXPR:
8209 /* If the operand is static or we are static, we can't conflict.
8210 Likewise if we don't conflict with the operand at all. */
8211 if (staticp (TREE_OPERAND (exp, 0))
8212 || TREE_STATIC (exp)
8213 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8214 return 1;
8216 /* Otherwise, the only way this can conflict is if we are taking
8217 the address of a DECL a that address if part of X, which is
8218 very rare. */
8219 exp = TREE_OPERAND (exp, 0);
8220 if (DECL_P (exp))
8222 if (!DECL_RTL_SET_P (exp)
8223 || !MEM_P (DECL_RTL (exp)))
8224 return 0;
8225 else
8226 exp_rtl = XEXP (DECL_RTL (exp), 0);
8228 break;
8230 case MEM_REF:
8231 if (MEM_P (x)
8232 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8233 get_alias_set (exp)))
8234 return 0;
8235 break;
8237 case CALL_EXPR:
8238 /* Assume that the call will clobber all hard registers and
8239 all of memory. */
8240 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8241 || MEM_P (x))
8242 return 0;
8243 break;
8245 case WITH_CLEANUP_EXPR:
8246 case CLEANUP_POINT_EXPR:
8247 /* Lowered by gimplify.cc. */
8248 gcc_unreachable ();
8250 case SAVE_EXPR:
8251 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8253 default:
8254 break;
8257 /* If we have an rtx, we do not need to scan our operands. */
8258 if (exp_rtl)
8259 break;
8261 nops = TREE_OPERAND_LENGTH (exp);
8262 for (i = 0; i < nops; i++)
8263 if (TREE_OPERAND (exp, i) != 0
8264 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8265 return 0;
8267 break;
8269 case tcc_type:
8270 /* Should never get a type here. */
8271 gcc_unreachable ();
8274 /* If we have an rtl, find any enclosed object. Then see if we conflict
8275 with it. */
8276 if (exp_rtl)
8278 if (GET_CODE (exp_rtl) == SUBREG)
8280 exp_rtl = SUBREG_REG (exp_rtl);
8281 if (REG_P (exp_rtl)
8282 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8283 return 0;
8286 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8287 are memory and they conflict. */
8288 return ! (rtx_equal_p (x, exp_rtl)
8289 || (MEM_P (x) && MEM_P (exp_rtl)
8290 && true_dependence (exp_rtl, VOIDmode, x)));
8293 /* If we reach here, it is safe. */
8294 return 1;
8298 /* Return the highest power of two that EXP is known to be a multiple of.
8299 This is used in updating alignment of MEMs in array references. */
8301 unsigned HOST_WIDE_INT
8302 highest_pow2_factor (const_tree exp)
8304 unsigned HOST_WIDE_INT ret;
8305 int trailing_zeros = tree_ctz (exp);
8306 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8307 return BIGGEST_ALIGNMENT;
8308 ret = HOST_WIDE_INT_1U << trailing_zeros;
8309 if (ret > BIGGEST_ALIGNMENT)
8310 return BIGGEST_ALIGNMENT;
8311 return ret;
8314 /* Similar, except that the alignment requirements of TARGET are
8315 taken into account. Assume it is at least as aligned as its
8316 type, unless it is a COMPONENT_REF in which case the layout of
8317 the structure gives the alignment. */
8319 static unsigned HOST_WIDE_INT
8320 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8322 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8323 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8325 return MAX (factor, talign);
8328 /* Convert the tree comparison code TCODE to the rtl one where the
8329 signedness is UNSIGNEDP. */
8331 static enum rtx_code
8332 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8334 enum rtx_code code;
8335 switch (tcode)
8337 case EQ_EXPR:
8338 code = EQ;
8339 break;
8340 case NE_EXPR:
8341 code = NE;
8342 break;
8343 case LT_EXPR:
8344 code = unsignedp ? LTU : LT;
8345 break;
8346 case LE_EXPR:
8347 code = unsignedp ? LEU : LE;
8348 break;
8349 case GT_EXPR:
8350 code = unsignedp ? GTU : GT;
8351 break;
8352 case GE_EXPR:
8353 code = unsignedp ? GEU : GE;
8354 break;
8355 case UNORDERED_EXPR:
8356 code = UNORDERED;
8357 break;
8358 case ORDERED_EXPR:
8359 code = ORDERED;
8360 break;
8361 case UNLT_EXPR:
8362 code = UNLT;
8363 break;
8364 case UNLE_EXPR:
8365 code = UNLE;
8366 break;
8367 case UNGT_EXPR:
8368 code = UNGT;
8369 break;
8370 case UNGE_EXPR:
8371 code = UNGE;
8372 break;
8373 case UNEQ_EXPR:
8374 code = UNEQ;
8375 break;
8376 case LTGT_EXPR:
8377 code = LTGT;
8378 break;
8380 default:
8381 gcc_unreachable ();
8383 return code;
8386 /* Subroutine of expand_expr. Expand the two operands of a binary
8387 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8388 The value may be stored in TARGET if TARGET is nonzero. The
8389 MODIFIER argument is as documented by expand_expr. */
8391 void
8392 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8393 enum expand_modifier modifier)
8395 if (! safe_from_p (target, exp1, 1))
8396 target = 0;
8397 if (operand_equal_p (exp0, exp1, 0))
8399 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8400 *op1 = copy_rtx (*op0);
8402 else
8404 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8405 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8410 /* Return a MEM that contains constant EXP. DEFER is as for
8411 output_constant_def and MODIFIER is as for expand_expr. */
8413 static rtx
8414 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8416 rtx mem;
8418 mem = output_constant_def (exp, defer);
8419 if (modifier != EXPAND_INITIALIZER)
8420 mem = use_anchored_address (mem);
8421 return mem;
8424 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8425 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8427 static rtx
8428 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8429 enum expand_modifier modifier, addr_space_t as)
8431 rtx result, subtarget;
8432 tree inner, offset;
8433 poly_int64 bitsize, bitpos;
8434 int unsignedp, reversep, volatilep = 0;
8435 machine_mode mode1;
8437 /* If we are taking the address of a constant and are at the top level,
8438 we have to use output_constant_def since we can't call force_const_mem
8439 at top level. */
8440 /* ??? This should be considered a front-end bug. We should not be
8441 generating ADDR_EXPR of something that isn't an LVALUE. The only
8442 exception here is STRING_CST. */
8443 if (CONSTANT_CLASS_P (exp))
8445 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8446 if (modifier < EXPAND_SUM)
8447 result = force_operand (result, target);
8448 return result;
8451 /* Everything must be something allowed by is_gimple_addressable. */
8452 switch (TREE_CODE (exp))
8454 case INDIRECT_REF:
8455 /* This case will happen via recursion for &a->b. */
8456 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8458 case MEM_REF:
8460 tree tem = TREE_OPERAND (exp, 0);
8461 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8462 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8463 return expand_expr (tem, target, tmode, modifier);
8466 case TARGET_MEM_REF:
8467 return addr_for_mem_ref (exp, as, true);
8469 case CONST_DECL:
8470 /* Expand the initializer like constants above. */
8471 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8472 0, modifier), 0);
8473 if (modifier < EXPAND_SUM)
8474 result = force_operand (result, target);
8475 return result;
8477 case REALPART_EXPR:
8478 /* The real part of the complex number is always first, therefore
8479 the address is the same as the address of the parent object. */
8480 offset = 0;
8481 bitpos = 0;
8482 inner = TREE_OPERAND (exp, 0);
8483 break;
8485 case IMAGPART_EXPR:
8486 /* The imaginary part of the complex number is always second.
8487 The expression is therefore always offset by the size of the
8488 scalar type. */
8489 offset = 0;
8490 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8491 inner = TREE_OPERAND (exp, 0);
8492 break;
8494 case COMPOUND_LITERAL_EXPR:
8495 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8496 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8497 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8498 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8499 the initializers aren't gimplified. */
8500 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8501 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8502 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8503 target, tmode, modifier, as);
8504 /* FALLTHRU */
8505 default:
8506 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8507 expand_expr, as that can have various side effects; LABEL_DECLs for
8508 example, may not have their DECL_RTL set yet. Expand the rtl of
8509 CONSTRUCTORs too, which should yield a memory reference for the
8510 constructor's contents. Assume language specific tree nodes can
8511 be expanded in some interesting way. */
8512 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8513 if (DECL_P (exp)
8514 || TREE_CODE (exp) == CONSTRUCTOR
8515 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8517 result = expand_expr (exp, target, tmode,
8518 modifier == EXPAND_INITIALIZER
8519 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8521 /* If the DECL isn't in memory, then the DECL wasn't properly
8522 marked TREE_ADDRESSABLE, which will be either a front-end
8523 or a tree optimizer bug. */
8525 gcc_assert (MEM_P (result));
8526 result = XEXP (result, 0);
8528 /* ??? Is this needed anymore? */
8529 if (DECL_P (exp))
8530 TREE_USED (exp) = 1;
8532 if (modifier != EXPAND_INITIALIZER
8533 && modifier != EXPAND_CONST_ADDRESS
8534 && modifier != EXPAND_SUM)
8535 result = force_operand (result, target);
8536 return result;
8539 /* Pass FALSE as the last argument to get_inner_reference although
8540 we are expanding to RTL. The rationale is that we know how to
8541 handle "aligning nodes" here: we can just bypass them because
8542 they won't change the final object whose address will be returned
8543 (they actually exist only for that purpose). */
8544 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8545 &unsignedp, &reversep, &volatilep);
8546 break;
8549 /* We must have made progress. */
8550 gcc_assert (inner != exp);
8552 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8553 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8554 inner alignment, force the inner to be sufficiently aligned. */
8555 if (CONSTANT_CLASS_P (inner)
8556 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8558 inner = copy_node (inner);
8559 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8560 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8561 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8563 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8565 if (offset)
8567 rtx tmp;
8569 if (modifier != EXPAND_NORMAL)
8570 result = force_operand (result, NULL);
8571 tmp = expand_expr (offset, NULL_RTX, tmode,
8572 modifier == EXPAND_INITIALIZER
8573 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8575 /* expand_expr is allowed to return an object in a mode other
8576 than TMODE. If it did, we need to convert. */
8577 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8578 tmp = convert_modes (tmode, GET_MODE (tmp),
8579 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8580 result = convert_memory_address_addr_space (tmode, result, as);
8581 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8583 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8584 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8585 else
8587 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8588 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8589 1, OPTAB_LIB_WIDEN);
8593 if (maybe_ne (bitpos, 0))
8595 /* Someone beforehand should have rejected taking the address
8596 of an object that isn't byte-aligned. */
8597 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8598 result = convert_memory_address_addr_space (tmode, result, as);
8599 result = plus_constant (tmode, result, bytepos);
8600 if (modifier < EXPAND_SUM)
8601 result = force_operand (result, target);
8604 return result;
8607 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8608 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8610 static rtx
8611 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8612 enum expand_modifier modifier)
8614 addr_space_t as = ADDR_SPACE_GENERIC;
8615 scalar_int_mode address_mode = Pmode;
8616 scalar_int_mode pointer_mode = ptr_mode;
8617 machine_mode rmode;
8618 rtx result;
8620 /* Target mode of VOIDmode says "whatever's natural". */
8621 if (tmode == VOIDmode)
8622 tmode = TYPE_MODE (TREE_TYPE (exp));
8624 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8626 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8627 address_mode = targetm.addr_space.address_mode (as);
8628 pointer_mode = targetm.addr_space.pointer_mode (as);
8631 /* We can get called with some Weird Things if the user does silliness
8632 like "(short) &a". In that case, convert_memory_address won't do
8633 the right thing, so ignore the given target mode. */
8634 scalar_int_mode new_tmode = (tmode == pointer_mode
8635 ? pointer_mode
8636 : address_mode);
8638 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8639 new_tmode, modifier, as);
8641 /* Despite expand_expr claims concerning ignoring TMODE when not
8642 strictly convenient, stuff breaks if we don't honor it. Note
8643 that combined with the above, we only do this for pointer modes. */
8644 rmode = GET_MODE (result);
8645 if (rmode == VOIDmode)
8646 rmode = new_tmode;
8647 if (rmode != new_tmode)
8648 result = convert_memory_address_addr_space (new_tmode, result, as);
8650 return result;
8653 /* Generate code for computing CONSTRUCTOR EXP.
8654 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8655 is TRUE, instead of creating a temporary variable in memory
8656 NULL is returned and the caller needs to handle it differently. */
8658 static rtx
8659 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8660 bool avoid_temp_mem)
8662 tree type = TREE_TYPE (exp);
8663 machine_mode mode = TYPE_MODE (type);
8665 /* Try to avoid creating a temporary at all. This is possible
8666 if all of the initializer is zero.
8667 FIXME: try to handle all [0..255] initializers we can handle
8668 with memset. */
8669 if (TREE_STATIC (exp)
8670 && !TREE_ADDRESSABLE (exp)
8671 && target != 0 && mode == BLKmode
8672 && all_zeros_p (exp))
8674 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8675 return target;
8678 /* All elts simple constants => refer to a constant in memory. But
8679 if this is a non-BLKmode mode, let it store a field at a time
8680 since that should make a CONST_INT, CONST_WIDE_INT or
8681 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8682 use, it is best to store directly into the target unless the type
8683 is large enough that memcpy will be used. If we are making an
8684 initializer and all operands are constant, put it in memory as
8685 well.
8687 FIXME: Avoid trying to fill vector constructors piece-meal.
8688 Output them with output_constant_def below unless we're sure
8689 they're zeros. This should go away when vector initializers
8690 are treated like VECTOR_CST instead of arrays. */
8691 if ((TREE_STATIC (exp)
8692 && ((mode == BLKmode
8693 && ! (target != 0 && safe_from_p (target, exp, 1)))
8694 || TREE_ADDRESSABLE (exp)
8695 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8696 && (! can_move_by_pieces
8697 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8698 TYPE_ALIGN (type)))
8699 && ! mostly_zeros_p (exp))))
8700 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8701 && TREE_CONSTANT (exp)))
8703 rtx constructor;
8705 if (avoid_temp_mem)
8706 return NULL_RTX;
8708 constructor = expand_expr_constant (exp, 1, modifier);
8710 if (modifier != EXPAND_CONST_ADDRESS
8711 && modifier != EXPAND_INITIALIZER
8712 && modifier != EXPAND_SUM)
8713 constructor = validize_mem (constructor);
8715 return constructor;
8718 /* If the CTOR is available in static storage and not mostly
8719 zeros and we can move it by pieces prefer to do so since
8720 that's usually more efficient than performing a series of
8721 stores from immediates. */
8722 if (avoid_temp_mem
8723 && TREE_STATIC (exp)
8724 && TREE_CONSTANT (exp)
8725 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8726 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8727 TYPE_ALIGN (type))
8728 && ! mostly_zeros_p (exp))
8729 return NULL_RTX;
8731 /* Handle calls that pass values in multiple non-contiguous
8732 locations. The Irix 6 ABI has examples of this. */
8733 if (target == 0 || ! safe_from_p (target, exp, 1)
8734 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8735 /* Also make a temporary if the store is to volatile memory, to
8736 avoid individual accesses to aggregate members. */
8737 || (GET_CODE (target) == MEM
8738 && MEM_VOLATILE_P (target)
8739 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8741 if (avoid_temp_mem)
8742 return NULL_RTX;
8744 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8747 store_constructor (exp, target, 0, int_expr_size (exp), false);
8748 return target;
8752 /* expand_expr: generate code for computing expression EXP.
8753 An rtx for the computed value is returned. The value is never null.
8754 In the case of a void EXP, const0_rtx is returned.
8756 The value may be stored in TARGET if TARGET is nonzero.
8757 TARGET is just a suggestion; callers must assume that
8758 the rtx returned may not be the same as TARGET.
8760 If TARGET is CONST0_RTX, it means that the value will be ignored.
8762 If TMODE is not VOIDmode, it suggests generating the
8763 result in mode TMODE. But this is done only when convenient.
8764 Otherwise, TMODE is ignored and the value generated in its natural mode.
8765 TMODE is just a suggestion; callers must assume that
8766 the rtx returned may not have mode TMODE.
8768 Note that TARGET may have neither TMODE nor MODE. In that case, it
8769 probably will not be used.
8771 If MODIFIER is EXPAND_SUM then when EXP is an addition
8772 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8773 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8774 products as above, or REG or MEM, or constant.
8775 Ordinarily in such cases we would output mul or add instructions
8776 and then return a pseudo reg containing the sum.
8778 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8779 it also marks a label as absolutely required (it can't be dead).
8780 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8781 This is used for outputting expressions used in initializers.
8783 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8784 with a constant address even if that address is not normally legitimate.
8785 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8787 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8788 a call parameter. Such targets require special care as we haven't yet
8789 marked TARGET so that it's safe from being trashed by libcalls. We
8790 don't want to use TARGET for anything but the final result;
8791 Intermediate values must go elsewhere. Additionally, calls to
8792 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8794 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8795 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8796 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8797 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8798 recursively.
8799 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8800 then *ALT_RTL is set to TARGET (before legitimziation).
8802 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8803 In this case, we don't adjust a returned MEM rtx that wouldn't be
8804 sufficiently aligned for its mode; instead, it's up to the caller
8805 to deal with it afterwards. This is used to make sure that unaligned
8806 base objects for which out-of-bounds accesses are supported, for
8807 example record types with trailing arrays, aren't realigned behind
8808 the back of the caller.
8809 The normal operating mode is to pass FALSE for this parameter. */
8812 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8813 enum expand_modifier modifier, rtx *alt_rtl,
8814 bool inner_reference_p)
8816 rtx ret;
8818 /* Handle ERROR_MARK before anybody tries to access its type. */
8819 if (TREE_CODE (exp) == ERROR_MARK
8820 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8822 ret = CONST0_RTX (tmode);
8823 return ret ? ret : const0_rtx;
8826 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8827 inner_reference_p);
8828 return ret;
8831 /* Try to expand the conditional expression which is represented by
8832 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8833 return the rtl reg which represents the result. Otherwise return
8834 NULL_RTX. */
8836 static rtx
8837 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8838 tree treeop1 ATTRIBUTE_UNUSED,
8839 tree treeop2 ATTRIBUTE_UNUSED)
8841 rtx insn;
8842 rtx op00, op01, op1, op2;
8843 enum rtx_code comparison_code;
8844 machine_mode comparison_mode;
8845 gimple *srcstmt;
8846 rtx temp;
8847 tree type = TREE_TYPE (treeop1);
8848 int unsignedp = TYPE_UNSIGNED (type);
8849 machine_mode mode = TYPE_MODE (type);
8850 machine_mode orig_mode = mode;
8851 static bool expanding_cond_expr_using_cmove = false;
8853 /* Conditional move expansion can end up TERing two operands which,
8854 when recursively hitting conditional expressions can result in
8855 exponential behavior if the cmove expansion ultimatively fails.
8856 It's hardly profitable to TER a cmove into a cmove so avoid doing
8857 that by failing early if we end up recursing. */
8858 if (expanding_cond_expr_using_cmove)
8859 return NULL_RTX;
8861 /* If we cannot do a conditional move on the mode, try doing it
8862 with the promoted mode. */
8863 if (!can_conditionally_move_p (mode))
8865 mode = promote_mode (type, mode, &unsignedp);
8866 if (!can_conditionally_move_p (mode))
8867 return NULL_RTX;
8868 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8870 else
8871 temp = assign_temp (type, 0, 1);
8873 expanding_cond_expr_using_cmove = true;
8874 start_sequence ();
8875 expand_operands (treeop1, treeop2,
8876 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
8877 EXPAND_NORMAL);
8879 if (TREE_CODE (treeop0) == SSA_NAME
8880 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8882 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8883 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8884 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8885 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8886 comparison_mode = TYPE_MODE (type);
8887 unsignedp = TYPE_UNSIGNED (type);
8888 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8890 else if (COMPARISON_CLASS_P (treeop0))
8892 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8893 enum tree_code cmpcode = TREE_CODE (treeop0);
8894 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8895 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8896 unsignedp = TYPE_UNSIGNED (type);
8897 comparison_mode = TYPE_MODE (type);
8898 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8900 else
8902 op00 = expand_normal (treeop0);
8903 op01 = const0_rtx;
8904 comparison_code = NE;
8905 comparison_mode = GET_MODE (op00);
8906 if (comparison_mode == VOIDmode)
8907 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8909 expanding_cond_expr_using_cmove = false;
8911 if (GET_MODE (op1) != mode)
8912 op1 = gen_lowpart (mode, op1);
8914 if (GET_MODE (op2) != mode)
8915 op2 = gen_lowpart (mode, op2);
8917 /* Try to emit the conditional move. */
8918 insn = emit_conditional_move (temp,
8919 { comparison_code, op00, op01,
8920 comparison_mode },
8921 op1, op2, mode,
8922 unsignedp);
8924 /* If we could do the conditional move, emit the sequence,
8925 and return. */
8926 if (insn)
8928 rtx_insn *seq = get_insns ();
8929 end_sequence ();
8930 emit_insn (seq);
8931 return convert_modes (orig_mode, mode, temp, 0);
8934 /* Otherwise discard the sequence and fall back to code with
8935 branches. */
8936 end_sequence ();
8937 return NULL_RTX;
8940 /* A helper function for expand_expr_real_2 to be used with a
8941 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8942 is nonzero, with alignment ALIGN in bits.
8943 Store the value at TARGET if possible (if TARGET is nonzero).
8944 Regardless of TARGET, we return the rtx for where the value is placed.
8945 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8946 then *ALT_RTL is set to TARGET (before legitimziation). */
8948 static rtx
8949 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8950 unsigned int align, rtx target, rtx *alt_rtl)
8952 enum insn_code icode;
8954 if ((icode = optab_handler (movmisalign_optab, mode))
8955 != CODE_FOR_nothing)
8957 class expand_operand ops[2];
8959 /* We've already validated the memory, and we're creating a
8960 new pseudo destination. The predicates really can't fail,
8961 nor can the generator. */
8962 create_output_operand (&ops[0], NULL_RTX, mode);
8963 create_fixed_operand (&ops[1], temp);
8964 expand_insn (icode, 2, ops);
8965 temp = ops[0].value;
8967 else if (targetm.slow_unaligned_access (mode, align))
8968 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8969 0, unsignedp, target,
8970 mode, mode, false, alt_rtl);
8971 return temp;
8974 /* Helper function of expand_expr_2, expand a division or modulo.
8975 op0 and op1 should be already expanded treeop0 and treeop1, using
8976 expand_operands. */
8978 static rtx
8979 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8980 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8982 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8983 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8984 if (SCALAR_INT_MODE_P (mode)
8985 && optimize >= 2
8986 && get_range_pos_neg (treeop0) == 1
8987 && get_range_pos_neg (treeop1) == 1)
8989 /* If both arguments are known to be positive when interpreted
8990 as signed, we can expand it as both signed and unsigned
8991 division or modulo. Choose the cheaper sequence in that case. */
8992 bool speed_p = optimize_insn_for_speed_p ();
8993 do_pending_stack_adjust ();
8994 start_sequence ();
8995 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8996 rtx_insn *uns_insns = get_insns ();
8997 end_sequence ();
8998 start_sequence ();
8999 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9000 rtx_insn *sgn_insns = get_insns ();
9001 end_sequence ();
9002 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9003 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9005 /* If costs are the same then use as tie breaker the other other
9006 factor. */
9007 if (uns_cost == sgn_cost)
9009 uns_cost = seq_cost (uns_insns, !speed_p);
9010 sgn_cost = seq_cost (sgn_insns, !speed_p);
9013 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9015 emit_insn (uns_insns);
9016 return uns_ret;
9018 emit_insn (sgn_insns);
9019 return sgn_ret;
9021 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9025 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9026 enum expand_modifier modifier)
9028 rtx op0, op1, op2, temp;
9029 rtx_code_label *lab;
9030 tree type;
9031 int unsignedp;
9032 machine_mode mode;
9033 scalar_int_mode int_mode;
9034 enum tree_code code = ops->code;
9035 optab this_optab;
9036 rtx subtarget, original_target;
9037 int ignore;
9038 bool reduce_bit_field;
9039 location_t loc = ops->location;
9040 tree treeop0, treeop1, treeop2;
9041 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9042 ? reduce_to_bit_field_precision ((expr), \
9043 target, \
9044 type) \
9045 : (expr))
9047 type = ops->type;
9048 mode = TYPE_MODE (type);
9049 unsignedp = TYPE_UNSIGNED (type);
9051 treeop0 = ops->op0;
9052 treeop1 = ops->op1;
9053 treeop2 = ops->op2;
9055 /* We should be called only on simple (binary or unary) expressions,
9056 exactly those that are valid in gimple expressions that aren't
9057 GIMPLE_SINGLE_RHS (or invalid). */
9058 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9059 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9060 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9062 ignore = (target == const0_rtx
9063 || ((CONVERT_EXPR_CODE_P (code)
9064 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9065 && TREE_CODE (type) == VOID_TYPE));
9067 /* We should be called only if we need the result. */
9068 gcc_assert (!ignore);
9070 /* An operation in what may be a bit-field type needs the
9071 result to be reduced to the precision of the bit-field type,
9072 which is narrower than that of the type's mode. */
9073 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9074 && !type_has_mode_precision_p (type));
9076 if (reduce_bit_field
9077 && (modifier == EXPAND_STACK_PARM
9078 || (target && GET_MODE (target) != mode)))
9079 target = 0;
9081 /* Use subtarget as the target for operand 0 of a binary operation. */
9082 subtarget = get_subtarget (target);
9083 original_target = target;
9085 switch (code)
9087 case NON_LVALUE_EXPR:
9088 case PAREN_EXPR:
9089 CASE_CONVERT:
9090 if (treeop0 == error_mark_node)
9091 return const0_rtx;
9093 if (TREE_CODE (type) == UNION_TYPE)
9095 tree valtype = TREE_TYPE (treeop0);
9097 /* If both input and output are BLKmode, this conversion isn't doing
9098 anything except possibly changing memory attribute. */
9099 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9101 rtx result = expand_expr (treeop0, target, tmode,
9102 modifier);
9104 result = copy_rtx (result);
9105 set_mem_attributes (result, type, 0);
9106 return result;
9109 if (target == 0)
9111 if (TYPE_MODE (type) != BLKmode)
9112 target = gen_reg_rtx (TYPE_MODE (type));
9113 else
9114 target = assign_temp (type, 1, 1);
9117 if (MEM_P (target))
9118 /* Store data into beginning of memory target. */
9119 store_expr (treeop0,
9120 adjust_address (target, TYPE_MODE (valtype), 0),
9121 modifier == EXPAND_STACK_PARM,
9122 false, TYPE_REVERSE_STORAGE_ORDER (type));
9124 else
9126 gcc_assert (REG_P (target)
9127 && !TYPE_REVERSE_STORAGE_ORDER (type));
9129 /* Store this field into a union of the proper type. */
9130 poly_uint64 op0_size
9131 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9132 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9133 store_field (target,
9134 /* The conversion must be constructed so that
9135 we know at compile time how many bits
9136 to preserve. */
9137 ordered_min (op0_size, union_size),
9138 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9139 false, false);
9142 /* Return the entire union. */
9143 return target;
9146 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9148 op0 = expand_expr (treeop0, target, VOIDmode,
9149 modifier);
9151 /* If the signedness of the conversion differs and OP0 is
9152 a promoted SUBREG, clear that indication since we now
9153 have to do the proper extension. */
9154 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9155 && GET_CODE (op0) == SUBREG)
9156 SUBREG_PROMOTED_VAR_P (op0) = 0;
9158 return REDUCE_BIT_FIELD (op0);
9161 op0 = expand_expr (treeop0, NULL_RTX, mode,
9162 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9163 if (GET_MODE (op0) == mode)
9166 /* If OP0 is a constant, just convert it into the proper mode. */
9167 else if (CONSTANT_P (op0))
9169 tree inner_type = TREE_TYPE (treeop0);
9170 machine_mode inner_mode = GET_MODE (op0);
9172 if (inner_mode == VOIDmode)
9173 inner_mode = TYPE_MODE (inner_type);
9175 if (modifier == EXPAND_INITIALIZER)
9176 op0 = lowpart_subreg (mode, op0, inner_mode);
9177 else
9178 op0= convert_modes (mode, inner_mode, op0,
9179 TYPE_UNSIGNED (inner_type));
9182 else if (modifier == EXPAND_INITIALIZER)
9183 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9184 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9186 else if (target == 0)
9187 op0 = convert_to_mode (mode, op0,
9188 TYPE_UNSIGNED (TREE_TYPE
9189 (treeop0)));
9190 else
9192 convert_move (target, op0,
9193 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9194 op0 = target;
9197 return REDUCE_BIT_FIELD (op0);
9199 case ADDR_SPACE_CONVERT_EXPR:
9201 tree treeop0_type = TREE_TYPE (treeop0);
9203 gcc_assert (POINTER_TYPE_P (type));
9204 gcc_assert (POINTER_TYPE_P (treeop0_type));
9206 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9207 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9209 /* Conversions between pointers to the same address space should
9210 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9211 gcc_assert (as_to != as_from);
9213 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9215 /* Ask target code to handle conversion between pointers
9216 to overlapping address spaces. */
9217 if (targetm.addr_space.subset_p (as_to, as_from)
9218 || targetm.addr_space.subset_p (as_from, as_to))
9220 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9222 else
9224 /* For disjoint address spaces, converting anything but a null
9225 pointer invokes undefined behavior. We truncate or extend the
9226 value as if we'd converted via integers, which handles 0 as
9227 required, and all others as the programmer likely expects. */
9228 #ifndef POINTERS_EXTEND_UNSIGNED
9229 const int POINTERS_EXTEND_UNSIGNED = 1;
9230 #endif
9231 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9232 op0, POINTERS_EXTEND_UNSIGNED);
9234 gcc_assert (op0);
9235 return op0;
9238 case POINTER_PLUS_EXPR:
9239 /* Even though the sizetype mode and the pointer's mode can be different
9240 expand is able to handle this correctly and get the correct result out
9241 of the PLUS_EXPR code. */
9242 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9243 if sizetype precision is smaller than pointer precision. */
9244 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9245 treeop1 = fold_convert_loc (loc, type,
9246 fold_convert_loc (loc, ssizetype,
9247 treeop1));
9248 /* If sizetype precision is larger than pointer precision, truncate the
9249 offset to have matching modes. */
9250 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9251 treeop1 = fold_convert_loc (loc, type, treeop1);
9252 /* FALLTHRU */
9254 case PLUS_EXPR:
9255 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9256 something else, make sure we add the register to the constant and
9257 then to the other thing. This case can occur during strength
9258 reduction and doing it this way will produce better code if the
9259 frame pointer or argument pointer is eliminated.
9261 fold-const.cc will ensure that the constant is always in the inner
9262 PLUS_EXPR, so the only case we need to do anything about is if
9263 sp, ap, or fp is our second argument, in which case we must swap
9264 the innermost first argument and our second argument. */
9266 if (TREE_CODE (treeop0) == PLUS_EXPR
9267 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9268 && VAR_P (treeop1)
9269 && (DECL_RTL (treeop1) == frame_pointer_rtx
9270 || DECL_RTL (treeop1) == stack_pointer_rtx
9271 || DECL_RTL (treeop1) == arg_pointer_rtx))
9273 gcc_unreachable ();
9276 /* If the result is to be ptr_mode and we are adding an integer to
9277 something, we might be forming a constant. So try to use
9278 plus_constant. If it produces a sum and we can't accept it,
9279 use force_operand. This allows P = &ARR[const] to generate
9280 efficient code on machines where a SYMBOL_REF is not a valid
9281 address.
9283 If this is an EXPAND_SUM call, always return the sum. */
9284 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9285 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9287 if (modifier == EXPAND_STACK_PARM)
9288 target = 0;
9289 if (TREE_CODE (treeop0) == INTEGER_CST
9290 && HWI_COMPUTABLE_MODE_P (mode)
9291 && TREE_CONSTANT (treeop1))
9293 rtx constant_part;
9294 HOST_WIDE_INT wc;
9295 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9297 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9298 EXPAND_SUM);
9299 /* Use wi::shwi to ensure that the constant is
9300 truncated according to the mode of OP1, then sign extended
9301 to a HOST_WIDE_INT. Using the constant directly can result
9302 in non-canonical RTL in a 64x32 cross compile. */
9303 wc = TREE_INT_CST_LOW (treeop0);
9304 constant_part =
9305 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9306 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9307 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9308 op1 = force_operand (op1, target);
9309 return REDUCE_BIT_FIELD (op1);
9312 else if (TREE_CODE (treeop1) == INTEGER_CST
9313 && HWI_COMPUTABLE_MODE_P (mode)
9314 && TREE_CONSTANT (treeop0))
9316 rtx constant_part;
9317 HOST_WIDE_INT wc;
9318 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9320 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9321 (modifier == EXPAND_INITIALIZER
9322 ? EXPAND_INITIALIZER : EXPAND_SUM));
9323 if (! CONSTANT_P (op0))
9325 op1 = expand_expr (treeop1, NULL_RTX,
9326 VOIDmode, modifier);
9327 /* Return a PLUS if modifier says it's OK. */
9328 if (modifier == EXPAND_SUM
9329 || modifier == EXPAND_INITIALIZER)
9330 return simplify_gen_binary (PLUS, mode, op0, op1);
9331 goto binop2;
9333 /* Use wi::shwi to ensure that the constant is
9334 truncated according to the mode of OP1, then sign extended
9335 to a HOST_WIDE_INT. Using the constant directly can result
9336 in non-canonical RTL in a 64x32 cross compile. */
9337 wc = TREE_INT_CST_LOW (treeop1);
9338 constant_part
9339 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9340 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9341 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9342 op0 = force_operand (op0, target);
9343 return REDUCE_BIT_FIELD (op0);
9347 /* Use TER to expand pointer addition of a negated value
9348 as pointer subtraction. */
9349 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9350 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9351 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9352 && TREE_CODE (treeop1) == SSA_NAME
9353 && TYPE_MODE (TREE_TYPE (treeop0))
9354 == TYPE_MODE (TREE_TYPE (treeop1)))
9356 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9357 if (def)
9359 treeop1 = gimple_assign_rhs1 (def);
9360 code = MINUS_EXPR;
9361 goto do_minus;
9365 /* No sense saving up arithmetic to be done
9366 if it's all in the wrong mode to form part of an address.
9367 And force_operand won't know whether to sign-extend or
9368 zero-extend. */
9369 if (modifier != EXPAND_INITIALIZER
9370 && (modifier != EXPAND_SUM || mode != ptr_mode))
9372 expand_operands (treeop0, treeop1,
9373 subtarget, &op0, &op1, modifier);
9374 if (op0 == const0_rtx)
9375 return op1;
9376 if (op1 == const0_rtx)
9377 return op0;
9378 goto binop2;
9381 expand_operands (treeop0, treeop1,
9382 subtarget, &op0, &op1, modifier);
9383 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9385 case MINUS_EXPR:
9386 case POINTER_DIFF_EXPR:
9387 do_minus:
9388 /* For initializers, we are allowed to return a MINUS of two
9389 symbolic constants. Here we handle all cases when both operands
9390 are constant. */
9391 /* Handle difference of two symbolic constants,
9392 for the sake of an initializer. */
9393 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9394 && really_constant_p (treeop0)
9395 && really_constant_p (treeop1))
9397 expand_operands (treeop0, treeop1,
9398 NULL_RTX, &op0, &op1, modifier);
9399 return simplify_gen_binary (MINUS, mode, op0, op1);
9402 /* No sense saving up arithmetic to be done
9403 if it's all in the wrong mode to form part of an address.
9404 And force_operand won't know whether to sign-extend or
9405 zero-extend. */
9406 if (modifier != EXPAND_INITIALIZER
9407 && (modifier != EXPAND_SUM || mode != ptr_mode))
9408 goto binop;
9410 expand_operands (treeop0, treeop1,
9411 subtarget, &op0, &op1, modifier);
9413 /* Convert A - const to A + (-const). */
9414 if (CONST_INT_P (op1))
9416 op1 = negate_rtx (mode, op1);
9417 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9420 goto binop2;
9422 case WIDEN_MULT_PLUS_EXPR:
9423 case WIDEN_MULT_MINUS_EXPR:
9424 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9425 op2 = expand_normal (treeop2);
9426 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9427 target, unsignedp);
9428 return target;
9430 case WIDEN_PLUS_EXPR:
9431 case WIDEN_MINUS_EXPR:
9432 case WIDEN_MULT_EXPR:
9433 /* If first operand is constant, swap them.
9434 Thus the following special case checks need only
9435 check the second operand. */
9436 if (TREE_CODE (treeop0) == INTEGER_CST)
9437 std::swap (treeop0, treeop1);
9439 /* First, check if we have a multiplication of one signed and one
9440 unsigned operand. */
9441 if (TREE_CODE (treeop1) != INTEGER_CST
9442 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9443 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9445 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9446 this_optab = usmul_widen_optab;
9447 if (find_widening_optab_handler (this_optab, mode, innermode)
9448 != CODE_FOR_nothing)
9450 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9451 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9452 EXPAND_NORMAL);
9453 else
9454 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9455 EXPAND_NORMAL);
9456 /* op0 and op1 might still be constant, despite the above
9457 != INTEGER_CST check. Handle it. */
9458 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9460 op0 = convert_modes (mode, innermode, op0, true);
9461 op1 = convert_modes (mode, innermode, op1, false);
9462 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9463 target, unsignedp));
9465 goto binop3;
9468 /* Check for a multiplication with matching signedness. */
9469 else if ((TREE_CODE (treeop1) == INTEGER_CST
9470 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9471 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9472 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9474 tree op0type = TREE_TYPE (treeop0);
9475 machine_mode innermode = TYPE_MODE (op0type);
9476 bool zextend_p = TYPE_UNSIGNED (op0type);
9477 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9478 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9480 if (TREE_CODE (treeop0) != INTEGER_CST)
9482 if (find_widening_optab_handler (this_optab, mode, innermode)
9483 != CODE_FOR_nothing)
9485 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9486 EXPAND_NORMAL);
9487 /* op0 and op1 might still be constant, despite the above
9488 != INTEGER_CST check. Handle it. */
9489 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9491 widen_mult_const:
9492 op0 = convert_modes (mode, innermode, op0, zextend_p);
9494 = convert_modes (mode, innermode, op1,
9495 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9496 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9497 target,
9498 unsignedp));
9500 temp = expand_widening_mult (mode, op0, op1, target,
9501 unsignedp, this_optab);
9502 return REDUCE_BIT_FIELD (temp);
9504 if (find_widening_optab_handler (other_optab, mode, innermode)
9505 != CODE_FOR_nothing
9506 && innermode == word_mode)
9508 rtx htem, hipart;
9509 op0 = expand_normal (treeop0);
9510 op1 = expand_normal (treeop1);
9511 /* op0 and op1 might be constants, despite the above
9512 != INTEGER_CST check. Handle it. */
9513 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9514 goto widen_mult_const;
9515 temp = expand_binop (mode, other_optab, op0, op1, target,
9516 unsignedp, OPTAB_LIB_WIDEN);
9517 hipart = gen_highpart (word_mode, temp);
9518 htem = expand_mult_highpart_adjust (word_mode, hipart,
9519 op0, op1, hipart,
9520 zextend_p);
9521 if (htem != hipart)
9522 emit_move_insn (hipart, htem);
9523 return REDUCE_BIT_FIELD (temp);
9527 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9528 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9529 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9530 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9532 case MULT_EXPR:
9533 /* If this is a fixed-point operation, then we cannot use the code
9534 below because "expand_mult" doesn't support sat/no-sat fixed-point
9535 multiplications. */
9536 if (ALL_FIXED_POINT_MODE_P (mode))
9537 goto binop;
9539 /* If first operand is constant, swap them.
9540 Thus the following special case checks need only
9541 check the second operand. */
9542 if (TREE_CODE (treeop0) == INTEGER_CST)
9543 std::swap (treeop0, treeop1);
9545 /* Attempt to return something suitable for generating an
9546 indexed address, for machines that support that. */
9548 if (modifier == EXPAND_SUM && mode == ptr_mode
9549 && tree_fits_shwi_p (treeop1))
9551 tree exp1 = treeop1;
9553 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9554 EXPAND_SUM);
9556 if (!REG_P (op0))
9557 op0 = force_operand (op0, NULL_RTX);
9558 if (!REG_P (op0))
9559 op0 = copy_to_mode_reg (mode, op0);
9561 op1 = gen_int_mode (tree_to_shwi (exp1),
9562 TYPE_MODE (TREE_TYPE (exp1)));
9563 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9566 if (modifier == EXPAND_STACK_PARM)
9567 target = 0;
9569 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9571 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9572 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9573 if (def_stmt0
9574 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9575 def_stmt0 = NULL;
9576 if (def_stmt1
9577 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9578 def_stmt1 = NULL;
9580 if (def_stmt0 || def_stmt1)
9582 /* X / Y * Y can be expanded as X - X % Y too.
9583 Choose the cheaper sequence of those two. */
9584 if (def_stmt0)
9585 treeop0 = gimple_assign_rhs1 (def_stmt0);
9586 else
9588 treeop1 = treeop0;
9589 treeop0 = gimple_assign_rhs1 (def_stmt1);
9591 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9592 EXPAND_NORMAL);
9593 bool speed_p = optimize_insn_for_speed_p ();
9594 do_pending_stack_adjust ();
9595 start_sequence ();
9596 rtx divmul_ret
9597 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9598 op0, op1, NULL_RTX, unsignedp);
9599 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9600 unsignedp);
9601 rtx_insn *divmul_insns = get_insns ();
9602 end_sequence ();
9603 start_sequence ();
9604 rtx modsub_ret
9605 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9606 op0, op1, NULL_RTX, unsignedp);
9607 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9608 optab_default);
9609 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9610 target, unsignedp, OPTAB_LIB_WIDEN);
9611 rtx_insn *modsub_insns = get_insns ();
9612 end_sequence ();
9613 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9614 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9615 /* If costs are the same then use as tie breaker the other other
9616 factor. */
9617 if (divmul_cost == modsub_cost)
9619 divmul_cost = seq_cost (divmul_insns, !speed_p);
9620 modsub_cost = seq_cost (modsub_insns, !speed_p);
9623 if (divmul_cost <= modsub_cost)
9625 emit_insn (divmul_insns);
9626 return REDUCE_BIT_FIELD (divmul_ret);
9628 emit_insn (modsub_insns);
9629 return REDUCE_BIT_FIELD (modsub_ret);
9633 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9635 /* Expand X*Y as X&-Y when Y must be zero or one. */
9636 if (SCALAR_INT_MODE_P (mode))
9638 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9639 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9641 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9642 if (bit0_p && bit1_p)
9643 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9645 if (bit0_p || bit1_p)
9647 bool speed = optimize_insn_for_speed_p ();
9648 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9649 struct algorithm algorithm;
9650 enum mult_variant variant;
9651 if (CONST_INT_P (op1)
9652 ? !choose_mult_variant (mode, INTVAL (op1),
9653 &algorithm, &variant, cost)
9654 : cost < mul_cost (speed, mode))
9656 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9657 op1, target)
9658 : expand_and (mode, op0,
9659 negate_rtx (mode, op1),
9660 target);
9661 return REDUCE_BIT_FIELD (target);
9666 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9668 case TRUNC_MOD_EXPR:
9669 case FLOOR_MOD_EXPR:
9670 case CEIL_MOD_EXPR:
9671 case ROUND_MOD_EXPR:
9673 case TRUNC_DIV_EXPR:
9674 case FLOOR_DIV_EXPR:
9675 case CEIL_DIV_EXPR:
9676 case ROUND_DIV_EXPR:
9677 case EXACT_DIV_EXPR:
9678 /* If this is a fixed-point operation, then we cannot use the code
9679 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9680 divisions. */
9681 if (ALL_FIXED_POINT_MODE_P (mode))
9682 goto binop;
9684 if (modifier == EXPAND_STACK_PARM)
9685 target = 0;
9686 /* Possible optimization: compute the dividend with EXPAND_SUM
9687 then if the divisor is constant can optimize the case
9688 where some terms of the dividend have coeffs divisible by it. */
9689 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9690 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9691 target, unsignedp);
9693 case RDIV_EXPR:
9694 goto binop;
9696 case MULT_HIGHPART_EXPR:
9697 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9698 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9699 gcc_assert (temp);
9700 return temp;
9702 case FIXED_CONVERT_EXPR:
9703 op0 = expand_normal (treeop0);
9704 if (target == 0 || modifier == EXPAND_STACK_PARM)
9705 target = gen_reg_rtx (mode);
9707 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9708 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9709 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9710 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9711 else
9712 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9713 return target;
9715 case FIX_TRUNC_EXPR:
9716 op0 = expand_normal (treeop0);
9717 if (target == 0 || modifier == EXPAND_STACK_PARM)
9718 target = gen_reg_rtx (mode);
9719 expand_fix (target, op0, unsignedp);
9720 return target;
9722 case FLOAT_EXPR:
9723 op0 = expand_normal (treeop0);
9724 if (target == 0 || modifier == EXPAND_STACK_PARM)
9725 target = gen_reg_rtx (mode);
9726 /* expand_float can't figure out what to do if FROM has VOIDmode.
9727 So give it the correct mode. With -O, cse will optimize this. */
9728 if (GET_MODE (op0) == VOIDmode)
9729 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9730 op0);
9731 expand_float (target, op0,
9732 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9733 return target;
9735 case NEGATE_EXPR:
9736 op0 = expand_expr (treeop0, subtarget,
9737 VOIDmode, EXPAND_NORMAL);
9738 if (modifier == EXPAND_STACK_PARM)
9739 target = 0;
9740 temp = expand_unop (mode,
9741 optab_for_tree_code (NEGATE_EXPR, type,
9742 optab_default),
9743 op0, target, 0);
9744 gcc_assert (temp);
9745 return REDUCE_BIT_FIELD (temp);
9747 case ABS_EXPR:
9748 case ABSU_EXPR:
9749 op0 = expand_expr (treeop0, subtarget,
9750 VOIDmode, EXPAND_NORMAL);
9751 if (modifier == EXPAND_STACK_PARM)
9752 target = 0;
9754 /* ABS_EXPR is not valid for complex arguments. */
9755 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9756 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9758 /* Unsigned abs is simply the operand. Testing here means we don't
9759 risk generating incorrect code below. */
9760 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9761 return op0;
9763 return expand_abs (mode, op0, target, unsignedp,
9764 safe_from_p (target, treeop0, 1));
9766 case MAX_EXPR:
9767 case MIN_EXPR:
9768 target = original_target;
9769 if (target == 0
9770 || modifier == EXPAND_STACK_PARM
9771 || (MEM_P (target) && MEM_VOLATILE_P (target))
9772 || GET_MODE (target) != mode
9773 || (REG_P (target)
9774 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9775 target = gen_reg_rtx (mode);
9776 expand_operands (treeop0, treeop1,
9777 target, &op0, &op1, EXPAND_NORMAL);
9779 /* First try to do it with a special MIN or MAX instruction.
9780 If that does not win, use a conditional jump to select the proper
9781 value. */
9782 this_optab = optab_for_tree_code (code, type, optab_default);
9783 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9784 OPTAB_WIDEN);
9785 if (temp != 0)
9786 return temp;
9788 if (VECTOR_TYPE_P (type))
9789 gcc_unreachable ();
9791 /* At this point, a MEM target is no longer useful; we will get better
9792 code without it. */
9794 if (! REG_P (target))
9795 target = gen_reg_rtx (mode);
9797 /* If op1 was placed in target, swap op0 and op1. */
9798 if (target != op0 && target == op1)
9799 std::swap (op0, op1);
9801 /* We generate better code and avoid problems with op1 mentioning
9802 target by forcing op1 into a pseudo if it isn't a constant. */
9803 if (! CONSTANT_P (op1))
9804 op1 = force_reg (mode, op1);
9807 enum rtx_code comparison_code;
9808 rtx cmpop1 = op1;
9810 if (code == MAX_EXPR)
9811 comparison_code = unsignedp ? GEU : GE;
9812 else
9813 comparison_code = unsignedp ? LEU : LE;
9815 /* Canonicalize to comparisons against 0. */
9816 if (op1 == const1_rtx)
9818 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9819 or (a != 0 ? a : 1) for unsigned.
9820 For MIN we are safe converting (a <= 1 ? a : 1)
9821 into (a <= 0 ? a : 1) */
9822 cmpop1 = const0_rtx;
9823 if (code == MAX_EXPR)
9824 comparison_code = unsignedp ? NE : GT;
9826 if (op1 == constm1_rtx && !unsignedp)
9828 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9829 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9830 cmpop1 = const0_rtx;
9831 if (code == MIN_EXPR)
9832 comparison_code = LT;
9835 /* Use a conditional move if possible. */
9836 if (can_conditionally_move_p (mode))
9838 rtx insn;
9840 start_sequence ();
9842 /* Try to emit the conditional move. */
9843 insn = emit_conditional_move (target,
9844 { comparison_code,
9845 op0, cmpop1, mode },
9846 op0, op1, mode,
9847 unsignedp);
9849 /* If we could do the conditional move, emit the sequence,
9850 and return. */
9851 if (insn)
9853 rtx_insn *seq = get_insns ();
9854 end_sequence ();
9855 emit_insn (seq);
9856 return target;
9859 /* Otherwise discard the sequence and fall back to code with
9860 branches. */
9861 end_sequence ();
9864 if (target != op0)
9865 emit_move_insn (target, op0);
9867 lab = gen_label_rtx ();
9868 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9869 unsignedp, mode, NULL_RTX, NULL, lab,
9870 profile_probability::uninitialized ());
9872 emit_move_insn (target, op1);
9873 emit_label (lab);
9874 return target;
9876 case BIT_NOT_EXPR:
9877 op0 = expand_expr (treeop0, subtarget,
9878 VOIDmode, EXPAND_NORMAL);
9879 if (modifier == EXPAND_STACK_PARM)
9880 target = 0;
9881 /* In case we have to reduce the result to bitfield precision
9882 for unsigned bitfield expand this as XOR with a proper constant
9883 instead. */
9884 if (reduce_bit_field && TYPE_UNSIGNED (type))
9886 int_mode = SCALAR_INT_TYPE_MODE (type);
9887 wide_int mask = wi::mask (TYPE_PRECISION (type),
9888 false, GET_MODE_PRECISION (int_mode));
9890 temp = expand_binop (int_mode, xor_optab, op0,
9891 immed_wide_int_const (mask, int_mode),
9892 target, 1, OPTAB_LIB_WIDEN);
9894 else
9895 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9896 gcc_assert (temp);
9897 return temp;
9899 /* ??? Can optimize bitwise operations with one arg constant.
9900 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9901 and (a bitwise1 b) bitwise2 b (etc)
9902 but that is probably not worth while. */
9904 case BIT_AND_EXPR:
9905 case BIT_IOR_EXPR:
9906 case BIT_XOR_EXPR:
9907 goto binop;
9909 case LROTATE_EXPR:
9910 case RROTATE_EXPR:
9911 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9912 || type_has_mode_precision_p (type));
9913 /* fall through */
9915 case LSHIFT_EXPR:
9916 case RSHIFT_EXPR:
9918 /* If this is a fixed-point operation, then we cannot use the code
9919 below because "expand_shift" doesn't support sat/no-sat fixed-point
9920 shifts. */
9921 if (ALL_FIXED_POINT_MODE_P (mode))
9922 goto binop;
9924 if (! safe_from_p (subtarget, treeop1, 1))
9925 subtarget = 0;
9926 if (modifier == EXPAND_STACK_PARM)
9927 target = 0;
9928 op0 = expand_expr (treeop0, subtarget,
9929 VOIDmode, EXPAND_NORMAL);
9931 /* Left shift optimization when shifting across word_size boundary.
9933 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9934 there isn't native instruction to support this wide mode
9935 left shift. Given below scenario:
9937 Type A = (Type) B << C
9939 |< T >|
9940 | dest_high | dest_low |
9942 | word_size |
9944 If the shift amount C caused we shift B to across the word
9945 size boundary, i.e part of B shifted into high half of
9946 destination register, and part of B remains in the low
9947 half, then GCC will use the following left shift expand
9948 logic:
9950 1. Initialize dest_low to B.
9951 2. Initialize every bit of dest_high to the sign bit of B.
9952 3. Logic left shift dest_low by C bit to finalize dest_low.
9953 The value of dest_low before this shift is kept in a temp D.
9954 4. Logic left shift dest_high by C.
9955 5. Logic right shift D by (word_size - C).
9956 6. Or the result of 4 and 5 to finalize dest_high.
9958 While, by checking gimple statements, if operand B is
9959 coming from signed extension, then we can simplify above
9960 expand logic into:
9962 1. dest_high = src_low >> (word_size - C).
9963 2. dest_low = src_low << C.
9965 We can use one arithmetic right shift to finish all the
9966 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9967 needed from 6 into 2.
9969 The case is similar for zero extension, except that we
9970 initialize dest_high to zero rather than copies of the sign
9971 bit from B. Furthermore, we need to use a logical right shift
9972 in this case.
9974 The choice of sign-extension versus zero-extension is
9975 determined entirely by whether or not B is signed and is
9976 independent of the current setting of unsignedp. */
9978 temp = NULL_RTX;
9979 if (code == LSHIFT_EXPR
9980 && target
9981 && REG_P (target)
9982 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9983 && mode == int_mode
9984 && TREE_CONSTANT (treeop1)
9985 && TREE_CODE (treeop0) == SSA_NAME)
9987 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9988 if (is_gimple_assign (def)
9989 && gimple_assign_rhs_code (def) == NOP_EXPR)
9991 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9992 (TREE_TYPE (gimple_assign_rhs1 (def)));
9994 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9995 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9996 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9997 >= GET_MODE_BITSIZE (word_mode)))
9999 rtx_insn *seq, *seq_old;
10000 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10001 int_mode);
10002 bool extend_unsigned
10003 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10004 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10005 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10006 rtx dest_high = simplify_gen_subreg (word_mode, target,
10007 int_mode, high_off);
10008 HOST_WIDE_INT ramount = (BITS_PER_WORD
10009 - TREE_INT_CST_LOW (treeop1));
10010 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10012 start_sequence ();
10013 /* dest_high = src_low >> (word_size - C). */
10014 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10015 rshift, dest_high,
10016 extend_unsigned);
10017 if (temp != dest_high)
10018 emit_move_insn (dest_high, temp);
10020 /* dest_low = src_low << C. */
10021 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10022 treeop1, dest_low, unsignedp);
10023 if (temp != dest_low)
10024 emit_move_insn (dest_low, temp);
10026 seq = get_insns ();
10027 end_sequence ();
10028 temp = target ;
10030 if (have_insn_for (ASHIFT, int_mode))
10032 bool speed_p = optimize_insn_for_speed_p ();
10033 start_sequence ();
10034 rtx ret_old = expand_variable_shift (code, int_mode,
10035 op0, treeop1,
10036 target,
10037 unsignedp);
10039 seq_old = get_insns ();
10040 end_sequence ();
10041 if (seq_cost (seq, speed_p)
10042 >= seq_cost (seq_old, speed_p))
10044 seq = seq_old;
10045 temp = ret_old;
10048 emit_insn (seq);
10053 if (temp == NULL_RTX)
10054 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10055 unsignedp);
10056 if (code == LSHIFT_EXPR)
10057 temp = REDUCE_BIT_FIELD (temp);
10058 return temp;
10061 /* Could determine the answer when only additive constants differ. Also,
10062 the addition of one can be handled by changing the condition. */
10063 case LT_EXPR:
10064 case LE_EXPR:
10065 case GT_EXPR:
10066 case GE_EXPR:
10067 case EQ_EXPR:
10068 case NE_EXPR:
10069 case UNORDERED_EXPR:
10070 case ORDERED_EXPR:
10071 case UNLT_EXPR:
10072 case UNLE_EXPR:
10073 case UNGT_EXPR:
10074 case UNGE_EXPR:
10075 case UNEQ_EXPR:
10076 case LTGT_EXPR:
10078 temp = do_store_flag (ops,
10079 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10080 tmode != VOIDmode ? tmode : mode);
10081 if (temp)
10082 return temp;
10084 /* Use a compare and a jump for BLKmode comparisons, or for function
10085 type comparisons is have_canonicalize_funcptr_for_compare. */
10087 if ((target == 0
10088 || modifier == EXPAND_STACK_PARM
10089 || ! safe_from_p (target, treeop0, 1)
10090 || ! safe_from_p (target, treeop1, 1)
10091 /* Make sure we don't have a hard reg (such as function's return
10092 value) live across basic blocks, if not optimizing. */
10093 || (!optimize && REG_P (target)
10094 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10095 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10097 emit_move_insn (target, const0_rtx);
10099 rtx_code_label *lab1 = gen_label_rtx ();
10100 jumpifnot_1 (code, treeop0, treeop1, lab1,
10101 profile_probability::uninitialized ());
10103 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10104 emit_move_insn (target, constm1_rtx);
10105 else
10106 emit_move_insn (target, const1_rtx);
10108 emit_label (lab1);
10109 return target;
10111 case COMPLEX_EXPR:
10112 /* Get the rtx code of the operands. */
10113 op0 = expand_normal (treeop0);
10114 op1 = expand_normal (treeop1);
10116 if (!target)
10117 target = gen_reg_rtx (TYPE_MODE (type));
10118 else
10119 /* If target overlaps with op1, then either we need to force
10120 op1 into a pseudo (if target also overlaps with op0),
10121 or write the complex parts in reverse order. */
10122 switch (GET_CODE (target))
10124 case CONCAT:
10125 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10127 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10129 complex_expr_force_op1:
10130 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10131 emit_move_insn (temp, op1);
10132 op1 = temp;
10133 break;
10135 complex_expr_swap_order:
10136 /* Move the imaginary (op1) and real (op0) parts to their
10137 location. */
10138 write_complex_part (target, op1, true, true);
10139 write_complex_part (target, op0, false, false);
10141 return target;
10143 break;
10144 case MEM:
10145 temp = adjust_address_nv (target,
10146 GET_MODE_INNER (GET_MODE (target)), 0);
10147 if (reg_overlap_mentioned_p (temp, op1))
10149 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10150 temp = adjust_address_nv (target, imode,
10151 GET_MODE_SIZE (imode));
10152 if (reg_overlap_mentioned_p (temp, op0))
10153 goto complex_expr_force_op1;
10154 goto complex_expr_swap_order;
10156 break;
10157 default:
10158 if (reg_overlap_mentioned_p (target, op1))
10160 if (reg_overlap_mentioned_p (target, op0))
10161 goto complex_expr_force_op1;
10162 goto complex_expr_swap_order;
10164 break;
10167 /* Move the real (op0) and imaginary (op1) parts to their location. */
10168 write_complex_part (target, op0, false, true);
10169 write_complex_part (target, op1, true, false);
10171 return target;
10173 case WIDEN_SUM_EXPR:
10175 tree oprnd0 = treeop0;
10176 tree oprnd1 = treeop1;
10178 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10179 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10180 target, unsignedp);
10181 return target;
10184 case VEC_UNPACK_HI_EXPR:
10185 case VEC_UNPACK_LO_EXPR:
10186 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10187 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10189 op0 = expand_normal (treeop0);
10190 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10191 target, unsignedp);
10192 gcc_assert (temp);
10193 return temp;
10196 case VEC_UNPACK_FLOAT_HI_EXPR:
10197 case VEC_UNPACK_FLOAT_LO_EXPR:
10199 op0 = expand_normal (treeop0);
10200 /* The signedness is determined from input operand. */
10201 temp = expand_widen_pattern_expr
10202 (ops, op0, NULL_RTX, NULL_RTX,
10203 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10205 gcc_assert (temp);
10206 return temp;
10209 case VEC_WIDEN_PLUS_HI_EXPR:
10210 case VEC_WIDEN_PLUS_LO_EXPR:
10211 case VEC_WIDEN_MINUS_HI_EXPR:
10212 case VEC_WIDEN_MINUS_LO_EXPR:
10213 case VEC_WIDEN_MULT_HI_EXPR:
10214 case VEC_WIDEN_MULT_LO_EXPR:
10215 case VEC_WIDEN_MULT_EVEN_EXPR:
10216 case VEC_WIDEN_MULT_ODD_EXPR:
10217 case VEC_WIDEN_LSHIFT_HI_EXPR:
10218 case VEC_WIDEN_LSHIFT_LO_EXPR:
10219 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10220 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10221 target, unsignedp);
10222 gcc_assert (target);
10223 return target;
10225 case VEC_PACK_SAT_EXPR:
10226 case VEC_PACK_FIX_TRUNC_EXPR:
10227 mode = TYPE_MODE (TREE_TYPE (treeop0));
10228 subtarget = NULL_RTX;
10229 goto binop;
10231 case VEC_PACK_TRUNC_EXPR:
10232 if (VECTOR_BOOLEAN_TYPE_P (type)
10233 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10234 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10235 && SCALAR_INT_MODE_P (mode))
10237 class expand_operand eops[4];
10238 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10239 expand_operands (treeop0, treeop1,
10240 subtarget, &op0, &op1, EXPAND_NORMAL);
10241 this_optab = vec_pack_sbool_trunc_optab;
10242 enum insn_code icode = optab_handler (this_optab, imode);
10243 create_output_operand (&eops[0], target, mode);
10244 create_convert_operand_from (&eops[1], op0, imode, false);
10245 create_convert_operand_from (&eops[2], op1, imode, false);
10246 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10247 create_input_operand (&eops[3], temp, imode);
10248 expand_insn (icode, 4, eops);
10249 return eops[0].value;
10251 mode = TYPE_MODE (TREE_TYPE (treeop0));
10252 subtarget = NULL_RTX;
10253 goto binop;
10255 case VEC_PACK_FLOAT_EXPR:
10256 mode = TYPE_MODE (TREE_TYPE (treeop0));
10257 expand_operands (treeop0, treeop1,
10258 subtarget, &op0, &op1, EXPAND_NORMAL);
10259 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10260 optab_default);
10261 target = expand_binop (mode, this_optab, op0, op1, target,
10262 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10263 OPTAB_LIB_WIDEN);
10264 gcc_assert (target);
10265 return target;
10267 case VEC_PERM_EXPR:
10269 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10270 vec_perm_builder sel;
10271 if (TREE_CODE (treeop2) == VECTOR_CST
10272 && tree_to_vec_perm_builder (&sel, treeop2))
10274 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10275 temp = expand_vec_perm_const (mode, op0, op1, sel,
10276 sel_mode, target);
10278 else
10280 op2 = expand_normal (treeop2);
10281 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10283 gcc_assert (temp);
10284 return temp;
10287 case DOT_PROD_EXPR:
10289 tree oprnd0 = treeop0;
10290 tree oprnd1 = treeop1;
10291 tree oprnd2 = treeop2;
10293 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10294 op2 = expand_normal (oprnd2);
10295 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10296 target, unsignedp);
10297 return target;
10300 case SAD_EXPR:
10302 tree oprnd0 = treeop0;
10303 tree oprnd1 = treeop1;
10304 tree oprnd2 = treeop2;
10306 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10307 op2 = expand_normal (oprnd2);
10308 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10309 target, unsignedp);
10310 return target;
10313 case REALIGN_LOAD_EXPR:
10315 tree oprnd0 = treeop0;
10316 tree oprnd1 = treeop1;
10317 tree oprnd2 = treeop2;
10319 this_optab = optab_for_tree_code (code, type, optab_default);
10320 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10321 op2 = expand_normal (oprnd2);
10322 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10323 target, unsignedp);
10324 gcc_assert (temp);
10325 return temp;
10328 case COND_EXPR:
10330 /* A COND_EXPR with its type being VOID_TYPE represents a
10331 conditional jump and is handled in
10332 expand_gimple_cond_expr. */
10333 gcc_assert (!VOID_TYPE_P (type));
10335 /* Note that COND_EXPRs whose type is a structure or union
10336 are required to be constructed to contain assignments of
10337 a temporary variable, so that we can evaluate them here
10338 for side effect only. If type is void, we must do likewise. */
10340 gcc_assert (!TREE_ADDRESSABLE (type)
10341 && !ignore
10342 && TREE_TYPE (treeop1) != void_type_node
10343 && TREE_TYPE (treeop2) != void_type_node);
10345 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10346 if (temp)
10347 return temp;
10349 /* If we are not to produce a result, we have no target. Otherwise,
10350 if a target was specified use it; it will not be used as an
10351 intermediate target unless it is safe. If no target, use a
10352 temporary. */
10354 if (modifier != EXPAND_STACK_PARM
10355 && original_target
10356 && safe_from_p (original_target, treeop0, 1)
10357 && GET_MODE (original_target) == mode
10358 && !MEM_P (original_target))
10359 temp = original_target;
10360 else
10361 temp = assign_temp (type, 0, 1);
10363 do_pending_stack_adjust ();
10364 NO_DEFER_POP;
10365 rtx_code_label *lab0 = gen_label_rtx ();
10366 rtx_code_label *lab1 = gen_label_rtx ();
10367 jumpifnot (treeop0, lab0,
10368 profile_probability::uninitialized ());
10369 store_expr (treeop1, temp,
10370 modifier == EXPAND_STACK_PARM,
10371 false, false);
10373 emit_jump_insn (targetm.gen_jump (lab1));
10374 emit_barrier ();
10375 emit_label (lab0);
10376 store_expr (treeop2, temp,
10377 modifier == EXPAND_STACK_PARM,
10378 false, false);
10380 emit_label (lab1);
10381 OK_DEFER_POP;
10382 return temp;
10385 case VEC_DUPLICATE_EXPR:
10386 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10387 target = expand_vector_broadcast (mode, op0);
10388 gcc_assert (target);
10389 return target;
10391 case VEC_SERIES_EXPR:
10392 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10393 return expand_vec_series_expr (mode, op0, op1, target);
10395 case BIT_INSERT_EXPR:
10397 unsigned bitpos = tree_to_uhwi (treeop2);
10398 unsigned bitsize;
10399 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10400 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10401 else
10402 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10403 op0 = expand_normal (treeop0);
10404 op1 = expand_normal (treeop1);
10405 rtx dst = gen_reg_rtx (mode);
10406 emit_move_insn (dst, op0);
10407 store_bit_field (dst, bitsize, bitpos, 0, 0,
10408 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10409 return dst;
10412 default:
10413 gcc_unreachable ();
10416 /* Here to do an ordinary binary operator. */
10417 binop:
10418 expand_operands (treeop0, treeop1,
10419 subtarget, &op0, &op1, EXPAND_NORMAL);
10420 binop2:
10421 this_optab = optab_for_tree_code (code, type, optab_default);
10422 binop3:
10423 if (modifier == EXPAND_STACK_PARM)
10424 target = 0;
10425 temp = expand_binop (mode, this_optab, op0, op1, target,
10426 unsignedp, OPTAB_LIB_WIDEN);
10427 gcc_assert (temp);
10428 /* Bitwise operations do not need bitfield reduction as we expect their
10429 operands being properly truncated. */
10430 if (code == BIT_XOR_EXPR
10431 || code == BIT_AND_EXPR
10432 || code == BIT_IOR_EXPR)
10433 return temp;
10434 return REDUCE_BIT_FIELD (temp);
10436 #undef REDUCE_BIT_FIELD
10439 /* Return TRUE if expression STMT is suitable for replacement.
10440 Never consider memory loads as replaceable, because those don't ever lead
10441 into constant expressions. */
10443 static bool
10444 stmt_is_replaceable_p (gimple *stmt)
10446 if (ssa_is_replaceable_p (stmt))
10448 /* Don't move around loads. */
10449 if (!gimple_assign_single_p (stmt)
10450 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10451 return true;
10453 return false;
10457 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10458 enum expand_modifier modifier, rtx *alt_rtl,
10459 bool inner_reference_p)
10461 rtx op0, op1, temp, decl_rtl;
10462 tree type;
10463 int unsignedp;
10464 machine_mode mode, dmode;
10465 enum tree_code code = TREE_CODE (exp);
10466 rtx subtarget, original_target;
10467 int ignore;
10468 bool reduce_bit_field;
10469 location_t loc = EXPR_LOCATION (exp);
10470 struct separate_ops ops;
10471 tree treeop0, treeop1, treeop2;
10472 tree ssa_name = NULL_TREE;
10473 gimple *g;
10475 type = TREE_TYPE (exp);
10476 mode = TYPE_MODE (type);
10477 unsignedp = TYPE_UNSIGNED (type);
10479 treeop0 = treeop1 = treeop2 = NULL_TREE;
10480 if (!VL_EXP_CLASS_P (exp))
10481 switch (TREE_CODE_LENGTH (code))
10483 default:
10484 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10485 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10486 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10487 case 0: break;
10489 ops.code = code;
10490 ops.type = type;
10491 ops.op0 = treeop0;
10492 ops.op1 = treeop1;
10493 ops.op2 = treeop2;
10494 ops.location = loc;
10496 ignore = (target == const0_rtx
10497 || ((CONVERT_EXPR_CODE_P (code)
10498 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10499 && TREE_CODE (type) == VOID_TYPE));
10501 /* An operation in what may be a bit-field type needs the
10502 result to be reduced to the precision of the bit-field type,
10503 which is narrower than that of the type's mode. */
10504 reduce_bit_field = (!ignore
10505 && INTEGRAL_TYPE_P (type)
10506 && !type_has_mode_precision_p (type));
10508 /* If we are going to ignore this result, we need only do something
10509 if there is a side-effect somewhere in the expression. If there
10510 is, short-circuit the most common cases here. Note that we must
10511 not call expand_expr with anything but const0_rtx in case this
10512 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10514 if (ignore)
10516 if (! TREE_SIDE_EFFECTS (exp))
10517 return const0_rtx;
10519 /* Ensure we reference a volatile object even if value is ignored, but
10520 don't do this if all we are doing is taking its address. */
10521 if (TREE_THIS_VOLATILE (exp)
10522 && TREE_CODE (exp) != FUNCTION_DECL
10523 && mode != VOIDmode && mode != BLKmode
10524 && modifier != EXPAND_CONST_ADDRESS)
10526 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10527 if (MEM_P (temp))
10528 copy_to_reg (temp);
10529 return const0_rtx;
10532 if (TREE_CODE_CLASS (code) == tcc_unary
10533 || code == BIT_FIELD_REF
10534 || code == COMPONENT_REF
10535 || code == INDIRECT_REF)
10536 return expand_expr (treeop0, const0_rtx, VOIDmode,
10537 modifier);
10539 else if (TREE_CODE_CLASS (code) == tcc_binary
10540 || TREE_CODE_CLASS (code) == tcc_comparison
10541 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10543 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10544 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10545 return const0_rtx;
10548 target = 0;
10551 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10552 target = 0;
10554 /* Use subtarget as the target for operand 0 of a binary operation. */
10555 subtarget = get_subtarget (target);
10556 original_target = target;
10558 switch (code)
10560 case LABEL_DECL:
10562 tree function = decl_function_context (exp);
10564 temp = label_rtx (exp);
10565 temp = gen_rtx_LABEL_REF (Pmode, temp);
10567 if (function != current_function_decl
10568 && function != 0)
10569 LABEL_REF_NONLOCAL_P (temp) = 1;
10571 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10572 return temp;
10575 case SSA_NAME:
10576 /* ??? ivopts calls expander, without any preparation from
10577 out-of-ssa. So fake instructions as if this was an access to the
10578 base variable. This unnecessarily allocates a pseudo, see how we can
10579 reuse it, if partition base vars have it set already. */
10580 if (!currently_expanding_to_rtl)
10582 tree var = SSA_NAME_VAR (exp);
10583 if (var && DECL_RTL_SET_P (var))
10584 return DECL_RTL (var);
10585 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10586 LAST_VIRTUAL_REGISTER + 1);
10589 g = get_gimple_for_ssa_name (exp);
10590 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10591 if (g == NULL
10592 && modifier == EXPAND_INITIALIZER
10593 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10594 && (optimize || !SSA_NAME_VAR (exp)
10595 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10596 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10597 g = SSA_NAME_DEF_STMT (exp);
10598 if (g)
10600 rtx r;
10601 location_t saved_loc = curr_insn_location ();
10602 loc = gimple_location (g);
10603 if (loc != UNKNOWN_LOCATION)
10604 set_curr_insn_location (loc);
10605 ops.code = gimple_assign_rhs_code (g);
10606 switch (get_gimple_rhs_class (ops.code))
10608 case GIMPLE_TERNARY_RHS:
10609 ops.op2 = gimple_assign_rhs3 (g);
10610 /* Fallthru */
10611 case GIMPLE_BINARY_RHS:
10612 ops.op1 = gimple_assign_rhs2 (g);
10614 /* Try to expand conditonal compare. */
10615 if (targetm.gen_ccmp_first)
10617 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10618 r = expand_ccmp_expr (g, mode);
10619 if (r)
10620 break;
10622 /* Fallthru */
10623 case GIMPLE_UNARY_RHS:
10624 ops.op0 = gimple_assign_rhs1 (g);
10625 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10626 ops.location = loc;
10627 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10628 break;
10629 case GIMPLE_SINGLE_RHS:
10631 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10632 tmode, modifier, alt_rtl,
10633 inner_reference_p);
10634 break;
10636 default:
10637 gcc_unreachable ();
10639 set_curr_insn_location (saved_loc);
10640 if (REG_P (r) && !REG_EXPR (r))
10641 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10642 return r;
10645 ssa_name = exp;
10646 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10647 exp = SSA_NAME_VAR (ssa_name);
10648 goto expand_decl_rtl;
10650 case VAR_DECL:
10651 /* Allow accel compiler to handle variables that require special
10652 treatment, e.g. if they have been modified in some way earlier in
10653 compilation by the adjust_private_decl OpenACC hook. */
10654 if (flag_openacc && targetm.goacc.expand_var_decl)
10656 temp = targetm.goacc.expand_var_decl (exp);
10657 if (temp)
10658 return temp;
10660 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10661 have scalar integer modes to a reg via store_constructor. */
10662 if (TREE_READONLY (exp)
10663 && !TREE_SIDE_EFFECTS (exp)
10664 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10665 && immediate_const_ctor_p (DECL_INITIAL (exp))
10666 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10667 && crtl->emit.regno_pointer_align_length
10668 && !target)
10670 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10671 store_constructor (DECL_INITIAL (exp), target, 0,
10672 int_expr_size (DECL_INITIAL (exp)), false);
10673 return target;
10675 /* ... fall through ... */
10677 case PARM_DECL:
10678 /* If a static var's type was incomplete when the decl was written,
10679 but the type is complete now, lay out the decl now. */
10680 if (DECL_SIZE (exp) == 0
10681 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10682 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10683 layout_decl (exp, 0);
10685 /* fall through */
10687 case FUNCTION_DECL:
10688 case RESULT_DECL:
10689 decl_rtl = DECL_RTL (exp);
10690 expand_decl_rtl:
10691 gcc_assert (decl_rtl);
10693 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10694 settings for VECTOR_TYPE_P that might switch for the function. */
10695 if (currently_expanding_to_rtl
10696 && code == VAR_DECL && MEM_P (decl_rtl)
10697 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10698 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10699 else
10700 decl_rtl = copy_rtx (decl_rtl);
10702 /* Record writes to register variables. */
10703 if (modifier == EXPAND_WRITE
10704 && REG_P (decl_rtl)
10705 && HARD_REGISTER_P (decl_rtl))
10706 add_to_hard_reg_set (&crtl->asm_clobbers,
10707 GET_MODE (decl_rtl), REGNO (decl_rtl));
10709 /* Ensure variable marked as used even if it doesn't go through
10710 a parser. If it hasn't be used yet, write out an external
10711 definition. */
10712 if (exp)
10713 TREE_USED (exp) = 1;
10715 /* Show we haven't gotten RTL for this yet. */
10716 temp = 0;
10718 /* Variables inherited from containing functions should have
10719 been lowered by this point. */
10720 if (exp)
10722 tree context = decl_function_context (exp);
10723 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10724 || context == current_function_decl
10725 || TREE_STATIC (exp)
10726 || DECL_EXTERNAL (exp)
10727 /* ??? C++ creates functions that are not
10728 TREE_STATIC. */
10729 || TREE_CODE (exp) == FUNCTION_DECL);
10732 /* This is the case of an array whose size is to be determined
10733 from its initializer, while the initializer is still being parsed.
10734 ??? We aren't parsing while expanding anymore. */
10736 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10737 temp = validize_mem (decl_rtl);
10739 /* If DECL_RTL is memory, we are in the normal case and the
10740 address is not valid, get the address into a register. */
10742 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10744 if (alt_rtl)
10745 *alt_rtl = decl_rtl;
10746 decl_rtl = use_anchored_address (decl_rtl);
10747 if (modifier != EXPAND_CONST_ADDRESS
10748 && modifier != EXPAND_SUM
10749 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10750 : GET_MODE (decl_rtl),
10751 XEXP (decl_rtl, 0),
10752 MEM_ADDR_SPACE (decl_rtl)))
10753 temp = replace_equiv_address (decl_rtl,
10754 copy_rtx (XEXP (decl_rtl, 0)));
10757 /* If we got something, return it. But first, set the alignment
10758 if the address is a register. */
10759 if (temp != 0)
10761 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10762 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10764 else if (MEM_P (decl_rtl))
10765 temp = decl_rtl;
10767 if (temp != 0)
10769 if (MEM_P (temp)
10770 && modifier != EXPAND_WRITE
10771 && modifier != EXPAND_MEMORY
10772 && modifier != EXPAND_INITIALIZER
10773 && modifier != EXPAND_CONST_ADDRESS
10774 && modifier != EXPAND_SUM
10775 && !inner_reference_p
10776 && mode != BLKmode
10777 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10778 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10779 MEM_ALIGN (temp), NULL_RTX, NULL);
10781 return temp;
10784 if (exp)
10785 dmode = DECL_MODE (exp);
10786 else
10787 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10789 /* If the mode of DECL_RTL does not match that of the decl,
10790 there are two cases: we are dealing with a BLKmode value
10791 that is returned in a register, or we are dealing with
10792 a promoted value. In the latter case, return a SUBREG
10793 of the wanted mode, but mark it so that we know that it
10794 was already extended. */
10795 if (REG_P (decl_rtl)
10796 && dmode != BLKmode
10797 && GET_MODE (decl_rtl) != dmode)
10799 machine_mode pmode;
10801 /* Get the signedness to be used for this variable. Ensure we get
10802 the same mode we got when the variable was declared. */
10803 if (code != SSA_NAME)
10804 pmode = promote_decl_mode (exp, &unsignedp);
10805 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10806 && gimple_code (g) == GIMPLE_CALL
10807 && !gimple_call_internal_p (g))
10808 pmode = promote_function_mode (type, mode, &unsignedp,
10809 gimple_call_fntype (g),
10811 else
10812 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10813 gcc_assert (GET_MODE (decl_rtl) == pmode);
10815 /* Some ABIs require scalar floating point modes to be passed
10816 in a wider scalar integer mode. We need to explicitly
10817 truncate to an integer mode of the correct precision before
10818 using a SUBREG to reinterpret as a floating point value. */
10819 if (SCALAR_FLOAT_MODE_P (mode)
10820 && SCALAR_INT_MODE_P (pmode)
10821 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
10822 return convert_wider_int_to_float (mode, pmode, decl_rtl);
10824 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10825 SUBREG_PROMOTED_VAR_P (temp) = 1;
10826 SUBREG_PROMOTED_SET (temp, unsignedp);
10827 return temp;
10830 return decl_rtl;
10832 case INTEGER_CST:
10834 /* Given that TYPE_PRECISION (type) is not always equal to
10835 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10836 the former to the latter according to the signedness of the
10837 type. */
10838 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10839 temp = immed_wide_int_const
10840 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10841 return temp;
10844 case VECTOR_CST:
10846 tree tmp = NULL_TREE;
10847 if (VECTOR_MODE_P (mode))
10848 return const_vector_from_tree (exp);
10849 scalar_int_mode int_mode;
10850 if (is_int_mode (mode, &int_mode))
10852 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10853 if (type_for_mode)
10854 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10855 type_for_mode, exp);
10857 if (!tmp)
10859 vec<constructor_elt, va_gc> *v;
10860 /* Constructors need to be fixed-length. FIXME. */
10861 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10862 vec_alloc (v, nunits);
10863 for (unsigned int i = 0; i < nunits; ++i)
10864 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10865 tmp = build_constructor (type, v);
10867 return expand_expr (tmp, ignore ? const0_rtx : target,
10868 tmode, modifier);
10871 case CONST_DECL:
10872 if (modifier == EXPAND_WRITE)
10874 /* Writing into CONST_DECL is always invalid, but handle it
10875 gracefully. */
10876 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10877 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10878 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10879 EXPAND_NORMAL, as);
10880 op0 = memory_address_addr_space (mode, op0, as);
10881 temp = gen_rtx_MEM (mode, op0);
10882 set_mem_addr_space (temp, as);
10883 return temp;
10885 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10887 case REAL_CST:
10888 /* If optimized, generate immediate CONST_DOUBLE
10889 which will be turned into memory by reload if necessary.
10891 We used to force a register so that loop.c could see it. But
10892 this does not allow gen_* patterns to perform optimizations with
10893 the constants. It also produces two insns in cases like "x = 1.0;".
10894 On most machines, floating-point constants are not permitted in
10895 many insns, so we'd end up copying it to a register in any case.
10897 Now, we do the copying in expand_binop, if appropriate. */
10898 return const_double_from_real_value (TREE_REAL_CST (exp),
10899 TYPE_MODE (TREE_TYPE (exp)));
10901 case FIXED_CST:
10902 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10903 TYPE_MODE (TREE_TYPE (exp)));
10905 case COMPLEX_CST:
10906 /* Handle evaluating a complex constant in a CONCAT target. */
10907 if (original_target && GET_CODE (original_target) == CONCAT)
10909 rtx rtarg, itarg;
10911 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10912 rtarg = XEXP (original_target, 0);
10913 itarg = XEXP (original_target, 1);
10915 /* Move the real and imaginary parts separately. */
10916 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10917 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10919 if (op0 != rtarg)
10920 emit_move_insn (rtarg, op0);
10921 if (op1 != itarg)
10922 emit_move_insn (itarg, op1);
10924 return original_target;
10927 /* fall through */
10929 case STRING_CST:
10930 temp = expand_expr_constant (exp, 1, modifier);
10932 /* temp contains a constant address.
10933 On RISC machines where a constant address isn't valid,
10934 make some insns to get that address into a register. */
10935 if (modifier != EXPAND_CONST_ADDRESS
10936 && modifier != EXPAND_INITIALIZER
10937 && modifier != EXPAND_SUM
10938 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10939 MEM_ADDR_SPACE (temp)))
10940 return replace_equiv_address (temp,
10941 copy_rtx (XEXP (temp, 0)));
10942 return temp;
10944 case POLY_INT_CST:
10945 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10947 case SAVE_EXPR:
10949 tree val = treeop0;
10950 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10951 inner_reference_p);
10953 if (!SAVE_EXPR_RESOLVED_P (exp))
10955 /* We can indeed still hit this case, typically via builtin
10956 expanders calling save_expr immediately before expanding
10957 something. Assume this means that we only have to deal
10958 with non-BLKmode values. */
10959 gcc_assert (GET_MODE (ret) != BLKmode);
10961 val = build_decl (curr_insn_location (),
10962 VAR_DECL, NULL, TREE_TYPE (exp));
10963 DECL_ARTIFICIAL (val) = 1;
10964 DECL_IGNORED_P (val) = 1;
10965 treeop0 = val;
10966 TREE_OPERAND (exp, 0) = treeop0;
10967 SAVE_EXPR_RESOLVED_P (exp) = 1;
10969 if (!CONSTANT_P (ret))
10970 ret = copy_to_reg (ret);
10971 SET_DECL_RTL (val, ret);
10974 return ret;
10978 case CONSTRUCTOR:
10979 /* If we don't need the result, just ensure we evaluate any
10980 subexpressions. */
10981 if (ignore)
10983 unsigned HOST_WIDE_INT idx;
10984 tree value;
10986 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10987 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10989 return const0_rtx;
10992 return expand_constructor (exp, target, modifier, false);
10994 case TARGET_MEM_REF:
10996 addr_space_t as
10997 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10998 unsigned int align;
11000 op0 = addr_for_mem_ref (exp, as, true);
11001 op0 = memory_address_addr_space (mode, op0, as);
11002 temp = gen_rtx_MEM (mode, op0);
11003 set_mem_attributes (temp, exp, 0);
11004 set_mem_addr_space (temp, as);
11005 align = get_object_alignment (exp);
11006 if (modifier != EXPAND_WRITE
11007 && modifier != EXPAND_MEMORY
11008 && mode != BLKmode
11009 && align < GET_MODE_ALIGNMENT (mode))
11010 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11011 align, NULL_RTX, NULL);
11012 return temp;
11015 case MEM_REF:
11017 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11018 addr_space_t as
11019 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11020 machine_mode address_mode;
11021 tree base = TREE_OPERAND (exp, 0);
11022 gimple *def_stmt;
11023 unsigned align;
11024 /* Handle expansion of non-aliased memory with non-BLKmode. That
11025 might end up in a register. */
11026 if (mem_ref_refers_to_non_mem_p (exp))
11028 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11029 base = TREE_OPERAND (base, 0);
11030 poly_uint64 type_size;
11031 if (known_eq (offset, 0)
11032 && !reverse
11033 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11034 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11035 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11036 target, tmode, modifier);
11037 if (TYPE_MODE (type) == BLKmode)
11039 temp = assign_stack_temp (DECL_MODE (base),
11040 GET_MODE_SIZE (DECL_MODE (base)));
11041 store_expr (base, temp, 0, false, false);
11042 temp = adjust_address (temp, BLKmode, offset);
11043 set_mem_size (temp, int_size_in_bytes (type));
11044 return temp;
11046 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11047 bitsize_int (offset * BITS_PER_UNIT));
11048 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11049 return expand_expr (exp, target, tmode, modifier);
11051 address_mode = targetm.addr_space.address_mode (as);
11052 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11054 tree mask = gimple_assign_rhs2 (def_stmt);
11055 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11056 gimple_assign_rhs1 (def_stmt), mask);
11057 TREE_OPERAND (exp, 0) = base;
11059 align = get_object_alignment (exp);
11060 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11061 op0 = memory_address_addr_space (mode, op0, as);
11062 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11064 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11065 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11066 op0 = memory_address_addr_space (mode, op0, as);
11068 temp = gen_rtx_MEM (mode, op0);
11069 set_mem_attributes (temp, exp, 0);
11070 set_mem_addr_space (temp, as);
11071 if (TREE_THIS_VOLATILE (exp))
11072 MEM_VOLATILE_P (temp) = 1;
11073 if (modifier != EXPAND_WRITE
11074 && modifier != EXPAND_MEMORY
11075 && !inner_reference_p
11076 && mode != BLKmode
11077 && align < GET_MODE_ALIGNMENT (mode))
11078 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11079 modifier == EXPAND_STACK_PARM
11080 ? NULL_RTX : target, alt_rtl);
11081 if (reverse
11082 && modifier != EXPAND_MEMORY
11083 && modifier != EXPAND_WRITE)
11084 temp = flip_storage_order (mode, temp);
11085 return temp;
11088 case ARRAY_REF:
11091 tree array = treeop0;
11092 tree index = treeop1;
11093 tree init;
11095 /* Fold an expression like: "foo"[2].
11096 This is not done in fold so it won't happen inside &.
11097 Don't fold if this is for wide characters since it's too
11098 difficult to do correctly and this is a very rare case. */
11100 if (modifier != EXPAND_CONST_ADDRESS
11101 && modifier != EXPAND_INITIALIZER
11102 && modifier != EXPAND_MEMORY)
11104 tree t = fold_read_from_constant_string (exp);
11106 if (t)
11107 return expand_expr (t, target, tmode, modifier);
11110 /* If this is a constant index into a constant array,
11111 just get the value from the array. Handle both the cases when
11112 we have an explicit constructor and when our operand is a variable
11113 that was declared const. */
11115 if (modifier != EXPAND_CONST_ADDRESS
11116 && modifier != EXPAND_INITIALIZER
11117 && modifier != EXPAND_MEMORY
11118 && TREE_CODE (array) == CONSTRUCTOR
11119 && ! TREE_SIDE_EFFECTS (array)
11120 && TREE_CODE (index) == INTEGER_CST)
11122 unsigned HOST_WIDE_INT ix;
11123 tree field, value;
11125 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11126 field, value)
11127 if (tree_int_cst_equal (field, index))
11129 if (!TREE_SIDE_EFFECTS (value))
11130 return expand_expr (fold (value), target, tmode, modifier);
11131 break;
11135 else if (optimize >= 1
11136 && modifier != EXPAND_CONST_ADDRESS
11137 && modifier != EXPAND_INITIALIZER
11138 && modifier != EXPAND_MEMORY
11139 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11140 && TREE_CODE (index) == INTEGER_CST
11141 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11142 && (init = ctor_for_folding (array)) != error_mark_node)
11144 if (init == NULL_TREE)
11146 tree value = build_zero_cst (type);
11147 if (TREE_CODE (value) == CONSTRUCTOR)
11149 /* If VALUE is a CONSTRUCTOR, this optimization is only
11150 useful if this doesn't store the CONSTRUCTOR into
11151 memory. If it does, it is more efficient to just
11152 load the data from the array directly. */
11153 rtx ret = expand_constructor (value, target,
11154 modifier, true);
11155 if (ret == NULL_RTX)
11156 value = NULL_TREE;
11159 if (value)
11160 return expand_expr (value, target, tmode, modifier);
11162 else if (TREE_CODE (init) == CONSTRUCTOR)
11164 unsigned HOST_WIDE_INT ix;
11165 tree field, value;
11167 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11168 field, value)
11169 if (tree_int_cst_equal (field, index))
11171 if (TREE_SIDE_EFFECTS (value))
11172 break;
11174 if (TREE_CODE (value) == CONSTRUCTOR)
11176 /* If VALUE is a CONSTRUCTOR, this
11177 optimization is only useful if
11178 this doesn't store the CONSTRUCTOR
11179 into memory. If it does, it is more
11180 efficient to just load the data from
11181 the array directly. */
11182 rtx ret = expand_constructor (value, target,
11183 modifier, true);
11184 if (ret == NULL_RTX)
11185 break;
11188 return
11189 expand_expr (fold (value), target, tmode, modifier);
11192 else if (TREE_CODE (init) == STRING_CST)
11194 tree low_bound = array_ref_low_bound (exp);
11195 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11197 /* Optimize the special case of a zero lower bound.
11199 We convert the lower bound to sizetype to avoid problems
11200 with constant folding. E.g. suppose the lower bound is
11201 1 and its mode is QI. Without the conversion
11202 (ARRAY + (INDEX - (unsigned char)1))
11203 becomes
11204 (ARRAY + (-(unsigned char)1) + INDEX)
11205 which becomes
11206 (ARRAY + 255 + INDEX). Oops! */
11207 if (!integer_zerop (low_bound))
11208 index1 = size_diffop_loc (loc, index1,
11209 fold_convert_loc (loc, sizetype,
11210 low_bound));
11212 if (tree_fits_uhwi_p (index1)
11213 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11215 tree char_type = TREE_TYPE (TREE_TYPE (init));
11216 scalar_int_mode char_mode;
11218 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11219 && GET_MODE_SIZE (char_mode) == 1)
11220 return gen_int_mode (TREE_STRING_POINTER (init)
11221 [TREE_INT_CST_LOW (index1)],
11222 char_mode);
11227 goto normal_inner_ref;
11229 case COMPONENT_REF:
11230 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11231 /* Fall through. */
11232 case BIT_FIELD_REF:
11233 case ARRAY_RANGE_REF:
11234 normal_inner_ref:
11236 machine_mode mode1, mode2;
11237 poly_int64 bitsize, bitpos, bytepos;
11238 tree offset;
11239 int reversep, volatilep = 0;
11240 tree tem
11241 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11242 &unsignedp, &reversep, &volatilep);
11243 rtx orig_op0, memloc;
11244 bool clear_mem_expr = false;
11245 bool must_force_mem;
11247 /* If we got back the original object, something is wrong. Perhaps
11248 we are evaluating an expression too early. In any event, don't
11249 infinitely recurse. */
11250 gcc_assert (tem != exp);
11252 /* Make sure bitpos is not negative, this can wreak havoc later. */
11253 if (maybe_lt (bitpos, 0))
11255 gcc_checking_assert (offset == NULL_TREE);
11256 offset = size_int (bits_to_bytes_round_down (bitpos));
11257 bitpos = num_trailing_bits (bitpos);
11260 /* If we have either an offset, a BLKmode result, or a reference
11261 outside the underlying object, we must force it to memory.
11262 Such a case can occur in Ada if we have unchecked conversion
11263 of an expression from a scalar type to an aggregate type or
11264 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11265 passed a partially uninitialized object or a view-conversion
11266 to a larger size. */
11267 must_force_mem = offset != NULL_TREE
11268 || mode1 == BLKmode
11269 || (mode == BLKmode
11270 && !int_mode_for_size (bitsize, 1).exists ());
11272 const enum expand_modifier tem_modifier
11273 = must_force_mem
11274 ? EXPAND_MEMORY
11275 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11277 /* If TEM's type is a union of variable size, pass TARGET to the inner
11278 computation, since it will need a temporary and TARGET is known
11279 to have to do. This occurs in unchecked conversion in Ada. */
11280 const rtx tem_target
11281 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11282 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11283 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11284 && modifier != EXPAND_STACK_PARM
11285 ? target
11286 : NULL_RTX;
11288 orig_op0 = op0
11289 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11290 true);
11292 /* If the field has a mode, we want to access it in the
11293 field's mode, not the computed mode.
11294 If a MEM has VOIDmode (external with incomplete type),
11295 use BLKmode for it instead. */
11296 if (MEM_P (op0))
11298 if (mode1 != VOIDmode)
11299 op0 = adjust_address (op0, mode1, 0);
11300 else if (GET_MODE (op0) == VOIDmode)
11301 op0 = adjust_address (op0, BLKmode, 0);
11304 mode2
11305 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11307 /* See above for the rationale. */
11308 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11309 must_force_mem = true;
11311 /* Handle CONCAT first. */
11312 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11314 if (known_eq (bitpos, 0)
11315 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11316 && COMPLEX_MODE_P (mode1)
11317 && COMPLEX_MODE_P (GET_MODE (op0))
11318 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11319 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11321 if (reversep)
11322 op0 = flip_storage_order (GET_MODE (op0), op0);
11323 if (mode1 != GET_MODE (op0))
11325 rtx parts[2];
11326 for (int i = 0; i < 2; i++)
11328 rtx op = read_complex_part (op0, i != 0);
11329 if (GET_CODE (op) == SUBREG)
11330 op = force_reg (GET_MODE (op), op);
11331 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11332 if (temp)
11333 op = temp;
11334 else
11336 if (!REG_P (op) && !MEM_P (op))
11337 op = force_reg (GET_MODE (op), op);
11338 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11340 parts[i] = op;
11342 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11344 return op0;
11346 if (known_eq (bitpos, 0)
11347 && known_eq (bitsize,
11348 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11349 && maybe_ne (bitsize, 0))
11351 op0 = XEXP (op0, 0);
11352 mode2 = GET_MODE (op0);
11354 else if (known_eq (bitpos,
11355 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11356 && known_eq (bitsize,
11357 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11358 && maybe_ne (bitpos, 0)
11359 && maybe_ne (bitsize, 0))
11361 op0 = XEXP (op0, 1);
11362 bitpos = 0;
11363 mode2 = GET_MODE (op0);
11365 else
11366 /* Otherwise force into memory. */
11367 must_force_mem = true;
11370 /* If this is a constant, put it in a register if it is a legitimate
11371 constant and we don't need a memory reference. */
11372 if (CONSTANT_P (op0)
11373 && mode2 != BLKmode
11374 && targetm.legitimate_constant_p (mode2, op0)
11375 && !must_force_mem)
11376 op0 = force_reg (mode2, op0);
11378 /* Otherwise, if this is a constant, try to force it to the constant
11379 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11380 is a legitimate constant. */
11381 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11382 op0 = validize_mem (memloc);
11384 /* Otherwise, if this is a constant or the object is not in memory
11385 and need be, put it there. */
11386 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11388 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11389 emit_move_insn (memloc, op0);
11390 op0 = memloc;
11391 clear_mem_expr = true;
11394 if (offset)
11396 machine_mode address_mode;
11397 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11398 EXPAND_SUM);
11400 gcc_assert (MEM_P (op0));
11402 address_mode = get_address_mode (op0);
11403 if (GET_MODE (offset_rtx) != address_mode)
11405 /* We cannot be sure that the RTL in offset_rtx is valid outside
11406 of a memory address context, so force it into a register
11407 before attempting to convert it to the desired mode. */
11408 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11409 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11412 /* See the comment in expand_assignment for the rationale. */
11413 if (mode1 != VOIDmode
11414 && maybe_ne (bitpos, 0)
11415 && maybe_gt (bitsize, 0)
11416 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11417 && multiple_p (bitpos, bitsize)
11418 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11419 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11421 op0 = adjust_address (op0, mode1, bytepos);
11422 bitpos = 0;
11425 op0 = offset_address (op0, offset_rtx,
11426 highest_pow2_factor (offset));
11429 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11430 record its alignment as BIGGEST_ALIGNMENT. */
11431 if (MEM_P (op0)
11432 && known_eq (bitpos, 0)
11433 && offset != 0
11434 && is_aligning_offset (offset, tem))
11435 set_mem_align (op0, BIGGEST_ALIGNMENT);
11437 /* Don't forget about volatility even if this is a bitfield. */
11438 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11440 if (op0 == orig_op0)
11441 op0 = copy_rtx (op0);
11443 MEM_VOLATILE_P (op0) = 1;
11446 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11448 if (op0 == orig_op0)
11449 op0 = copy_rtx (op0);
11451 set_mem_align (op0, BITS_PER_UNIT);
11454 /* In cases where an aligned union has an unaligned object
11455 as a field, we might be extracting a BLKmode value from
11456 an integer-mode (e.g., SImode) object. Handle this case
11457 by doing the extract into an object as wide as the field
11458 (which we know to be the width of a basic mode), then
11459 storing into memory, and changing the mode to BLKmode. */
11460 if (mode1 == VOIDmode
11461 || REG_P (op0) || GET_CODE (op0) == SUBREG
11462 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11463 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11464 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11465 && modifier != EXPAND_CONST_ADDRESS
11466 && modifier != EXPAND_INITIALIZER
11467 && modifier != EXPAND_MEMORY)
11468 /* If the bitfield is volatile and the bitsize
11469 is narrower than the access size of the bitfield,
11470 we need to extract bitfields from the access. */
11471 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11472 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11473 && mode1 != BLKmode
11474 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11475 /* If the field isn't aligned enough to fetch as a memref,
11476 fetch it as a bit field. */
11477 || (mode1 != BLKmode
11478 && (((MEM_P (op0)
11479 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11480 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11481 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11482 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11483 && modifier != EXPAND_MEMORY
11484 && ((modifier == EXPAND_CONST_ADDRESS
11485 || modifier == EXPAND_INITIALIZER)
11486 ? STRICT_ALIGNMENT
11487 : targetm.slow_unaligned_access (mode1,
11488 MEM_ALIGN (op0))))
11489 || !multiple_p (bitpos, BITS_PER_UNIT)))
11490 /* If the type and the field are a constant size and the
11491 size of the type isn't the same size as the bitfield,
11492 we must use bitfield operations. */
11493 || (known_size_p (bitsize)
11494 && TYPE_SIZE (TREE_TYPE (exp))
11495 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11496 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11497 bitsize)))
11499 machine_mode ext_mode = mode;
11501 if (ext_mode == BLKmode
11502 && ! (target != 0 && MEM_P (op0)
11503 && MEM_P (target)
11504 && multiple_p (bitpos, BITS_PER_UNIT)))
11505 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11507 if (ext_mode == BLKmode)
11509 if (target == 0)
11510 target = assign_temp (type, 1, 1);
11512 /* ??? Unlike the similar test a few lines below, this one is
11513 very likely obsolete. */
11514 if (known_eq (bitsize, 0))
11515 return target;
11517 /* In this case, BITPOS must start at a byte boundary and
11518 TARGET, if specified, must be a MEM. */
11519 gcc_assert (MEM_P (op0)
11520 && (!target || MEM_P (target)));
11522 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11523 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11524 emit_block_move (target,
11525 adjust_address (op0, VOIDmode, bytepos),
11526 gen_int_mode (bytesize, Pmode),
11527 (modifier == EXPAND_STACK_PARM
11528 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11530 return target;
11533 /* If we have nothing to extract, the result will be 0 for targets
11534 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11535 return 0 for the sake of consistency, as reading a zero-sized
11536 bitfield is valid in Ada and the value is fully specified. */
11537 if (known_eq (bitsize, 0))
11538 return const0_rtx;
11540 op0 = validize_mem (op0);
11542 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11543 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11545 /* If the result has aggregate type and the extraction is done in
11546 an integral mode, then the field may be not aligned on a byte
11547 boundary; in this case, if it has reverse storage order, it
11548 needs to be extracted as a scalar field with reverse storage
11549 order and put back into memory order afterwards. */
11550 if (AGGREGATE_TYPE_P (type)
11551 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11552 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11554 gcc_checking_assert (known_ge (bitpos, 0));
11555 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11556 (modifier == EXPAND_STACK_PARM
11557 ? NULL_RTX : target),
11558 ext_mode, ext_mode, reversep, alt_rtl);
11560 /* If the result has aggregate type and the mode of OP0 is an
11561 integral mode then, if BITSIZE is narrower than this mode
11562 and this is for big-endian data, we must put the field
11563 into the high-order bits. And we must also put it back
11564 into memory order if it has been previously reversed. */
11565 scalar_int_mode op0_mode;
11566 if (AGGREGATE_TYPE_P (type)
11567 && is_int_mode (GET_MODE (op0), &op0_mode))
11569 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11571 gcc_checking_assert (known_le (bitsize, size));
11572 if (maybe_lt (bitsize, size)
11573 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11574 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11575 size - bitsize, op0, 1);
11577 if (reversep)
11578 op0 = flip_storage_order (op0_mode, op0);
11581 /* If the result type is BLKmode, store the data into a temporary
11582 of the appropriate type, but with the mode corresponding to the
11583 mode for the data we have (op0's mode). */
11584 if (mode == BLKmode)
11586 rtx new_rtx
11587 = assign_stack_temp_for_type (ext_mode,
11588 GET_MODE_BITSIZE (ext_mode),
11589 type);
11590 emit_move_insn (new_rtx, op0);
11591 op0 = copy_rtx (new_rtx);
11592 PUT_MODE (op0, BLKmode);
11595 return op0;
11598 /* If the result is BLKmode, use that to access the object
11599 now as well. */
11600 if (mode == BLKmode)
11601 mode1 = BLKmode;
11603 /* Get a reference to just this component. */
11604 bytepos = bits_to_bytes_round_down (bitpos);
11605 if (modifier == EXPAND_CONST_ADDRESS
11606 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11607 op0 = adjust_address_nv (op0, mode1, bytepos);
11608 else
11609 op0 = adjust_address (op0, mode1, bytepos);
11611 if (op0 == orig_op0)
11612 op0 = copy_rtx (op0);
11614 /* Don't set memory attributes if the base expression is
11615 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11616 we should just honor its original memory attributes. */
11617 if (!(TREE_CODE (tem) == SSA_NAME
11618 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11619 set_mem_attributes (op0, exp, 0);
11621 if (REG_P (XEXP (op0, 0)))
11622 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11624 /* If op0 is a temporary because the original expressions was forced
11625 to memory, clear MEM_EXPR so that the original expression cannot
11626 be marked as addressable through MEM_EXPR of the temporary. */
11627 if (clear_mem_expr)
11628 set_mem_expr (op0, NULL_TREE);
11630 MEM_VOLATILE_P (op0) |= volatilep;
11632 if (reversep
11633 && modifier != EXPAND_MEMORY
11634 && modifier != EXPAND_WRITE)
11635 op0 = flip_storage_order (mode1, op0);
11637 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11638 || modifier == EXPAND_CONST_ADDRESS
11639 || modifier == EXPAND_INITIALIZER)
11640 return op0;
11642 if (target == 0)
11643 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11645 convert_move (target, op0, unsignedp);
11646 return target;
11649 case OBJ_TYPE_REF:
11650 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11652 case CALL_EXPR:
11653 /* All valid uses of __builtin_va_arg_pack () are removed during
11654 inlining. */
11655 if (CALL_EXPR_VA_ARG_PACK (exp))
11656 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11658 tree fndecl = get_callee_fndecl (exp), attr;
11660 if (fndecl
11661 /* Don't diagnose the error attribute in thunks, those are
11662 artificially created. */
11663 && !CALL_FROM_THUNK_P (exp)
11664 && (attr = lookup_attribute ("error",
11665 DECL_ATTRIBUTES (fndecl))) != NULL)
11667 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11668 error ("call to %qs declared with attribute error: %s",
11669 identifier_to_locale (ident),
11670 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11672 if (fndecl
11673 /* Don't diagnose the warning attribute in thunks, those are
11674 artificially created. */
11675 && !CALL_FROM_THUNK_P (exp)
11676 && (attr = lookup_attribute ("warning",
11677 DECL_ATTRIBUTES (fndecl))) != NULL)
11679 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11680 warning_at (EXPR_LOCATION (exp),
11681 OPT_Wattribute_warning,
11682 "call to %qs declared with attribute warning: %s",
11683 identifier_to_locale (ident),
11684 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11687 /* Check for a built-in function. */
11688 if (fndecl && fndecl_built_in_p (fndecl))
11690 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11691 return expand_builtin (exp, target, subtarget, tmode, ignore);
11694 return expand_call (exp, target, ignore);
11696 case VIEW_CONVERT_EXPR:
11697 op0 = NULL_RTX;
11699 /* If we are converting to BLKmode, try to avoid an intermediate
11700 temporary by fetching an inner memory reference. */
11701 if (mode == BLKmode
11702 && poly_int_tree_p (TYPE_SIZE (type))
11703 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11704 && handled_component_p (treeop0))
11706 machine_mode mode1;
11707 poly_int64 bitsize, bitpos, bytepos;
11708 tree offset;
11709 int reversep, volatilep = 0;
11710 tree tem
11711 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11712 &unsignedp, &reversep, &volatilep);
11714 /* ??? We should work harder and deal with non-zero offsets. */
11715 if (!offset
11716 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11717 && !reversep
11718 && known_size_p (bitsize)
11719 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11721 /* See the normal_inner_ref case for the rationale. */
11722 rtx orig_op0
11723 = expand_expr_real (tem,
11724 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11725 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11726 != INTEGER_CST)
11727 && modifier != EXPAND_STACK_PARM
11728 ? target : NULL_RTX),
11729 VOIDmode,
11730 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11731 NULL, true);
11733 if (MEM_P (orig_op0))
11735 op0 = orig_op0;
11737 /* Get a reference to just this component. */
11738 if (modifier == EXPAND_CONST_ADDRESS
11739 || modifier == EXPAND_SUM
11740 || modifier == EXPAND_INITIALIZER)
11741 op0 = adjust_address_nv (op0, mode, bytepos);
11742 else
11743 op0 = adjust_address (op0, mode, bytepos);
11745 if (op0 == orig_op0)
11746 op0 = copy_rtx (op0);
11748 set_mem_attributes (op0, treeop0, 0);
11749 if (REG_P (XEXP (op0, 0)))
11750 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11752 MEM_VOLATILE_P (op0) |= volatilep;
11757 if (!op0)
11758 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11759 NULL, inner_reference_p);
11761 /* If the input and output modes are both the same, we are done. */
11762 if (mode == GET_MODE (op0))
11764 /* If neither mode is BLKmode, and both modes are the same size
11765 then we can use gen_lowpart. */
11766 else if (mode != BLKmode
11767 && GET_MODE (op0) != BLKmode
11768 && known_eq (GET_MODE_PRECISION (mode),
11769 GET_MODE_PRECISION (GET_MODE (op0)))
11770 && !COMPLEX_MODE_P (GET_MODE (op0)))
11772 if (GET_CODE (op0) == SUBREG)
11773 op0 = force_reg (GET_MODE (op0), op0);
11774 temp = gen_lowpart_common (mode, op0);
11775 if (temp)
11776 op0 = temp;
11777 else
11779 if (!REG_P (op0) && !MEM_P (op0))
11780 op0 = force_reg (GET_MODE (op0), op0);
11781 op0 = gen_lowpart (mode, op0);
11784 /* If both types are integral, convert from one mode to the other. */
11785 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11786 op0 = convert_modes (mode, GET_MODE (op0), op0,
11787 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11788 /* If the output type is a bit-field type, do an extraction. */
11789 else if (reduce_bit_field)
11790 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11791 TYPE_UNSIGNED (type), NULL_RTX,
11792 mode, mode, false, NULL);
11793 /* As a last resort, spill op0 to memory, and reload it in a
11794 different mode. */
11795 else if (!MEM_P (op0))
11797 /* If the operand is not a MEM, force it into memory. Since we
11798 are going to be changing the mode of the MEM, don't call
11799 force_const_mem for constants because we don't allow pool
11800 constants to change mode. */
11801 tree inner_type = TREE_TYPE (treeop0);
11803 gcc_assert (!TREE_ADDRESSABLE (exp));
11805 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11806 target
11807 = assign_stack_temp_for_type
11808 (TYPE_MODE (inner_type),
11809 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11811 emit_move_insn (target, op0);
11812 op0 = target;
11815 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11816 output type is such that the operand is known to be aligned, indicate
11817 that it is. Otherwise, we need only be concerned about alignment for
11818 non-BLKmode results. */
11819 if (MEM_P (op0))
11821 enum insn_code icode;
11823 if (modifier != EXPAND_WRITE
11824 && modifier != EXPAND_MEMORY
11825 && !inner_reference_p
11826 && mode != BLKmode
11827 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11829 /* If the target does have special handling for unaligned
11830 loads of mode then use them. */
11831 if ((icode = optab_handler (movmisalign_optab, mode))
11832 != CODE_FOR_nothing)
11834 rtx reg;
11836 op0 = adjust_address (op0, mode, 0);
11837 /* We've already validated the memory, and we're creating a
11838 new pseudo destination. The predicates really can't
11839 fail. */
11840 reg = gen_reg_rtx (mode);
11842 /* Nor can the insn generator. */
11843 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11844 emit_insn (insn);
11845 return reg;
11847 else if (STRICT_ALIGNMENT)
11849 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11850 poly_uint64 temp_size = mode_size;
11851 if (GET_MODE (op0) != BLKmode)
11852 temp_size = upper_bound (temp_size,
11853 GET_MODE_SIZE (GET_MODE (op0)));
11854 rtx new_rtx
11855 = assign_stack_temp_for_type (mode, temp_size, type);
11856 rtx new_with_op0_mode
11857 = adjust_address (new_rtx, GET_MODE (op0), 0);
11859 gcc_assert (!TREE_ADDRESSABLE (exp));
11861 if (GET_MODE (op0) == BLKmode)
11863 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11864 emit_block_move (new_with_op0_mode, op0, size_rtx,
11865 (modifier == EXPAND_STACK_PARM
11866 ? BLOCK_OP_CALL_PARM
11867 : BLOCK_OP_NORMAL));
11869 else
11870 emit_move_insn (new_with_op0_mode, op0);
11872 op0 = new_rtx;
11876 op0 = adjust_address (op0, mode, 0);
11879 return op0;
11881 case MODIFY_EXPR:
11883 tree lhs = treeop0;
11884 tree rhs = treeop1;
11885 gcc_assert (ignore);
11887 /* Check for |= or &= of a bitfield of size one into another bitfield
11888 of size 1. In this case, (unless we need the result of the
11889 assignment) we can do this more efficiently with a
11890 test followed by an assignment, if necessary.
11892 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11893 things change so we do, this code should be enhanced to
11894 support it. */
11895 if (TREE_CODE (lhs) == COMPONENT_REF
11896 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11897 || TREE_CODE (rhs) == BIT_AND_EXPR)
11898 && TREE_OPERAND (rhs, 0) == lhs
11899 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11900 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11901 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11903 rtx_code_label *label = gen_label_rtx ();
11904 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11905 profile_probability prob = profile_probability::uninitialized ();
11906 if (value)
11907 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11908 else
11909 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11910 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11911 false);
11912 do_pending_stack_adjust ();
11913 emit_label (label);
11914 return const0_rtx;
11917 expand_assignment (lhs, rhs, false);
11918 return const0_rtx;
11921 case ADDR_EXPR:
11922 return expand_expr_addr_expr (exp, target, tmode, modifier);
11924 case REALPART_EXPR:
11925 op0 = expand_normal (treeop0);
11926 return read_complex_part (op0, false);
11928 case IMAGPART_EXPR:
11929 op0 = expand_normal (treeop0);
11930 return read_complex_part (op0, true);
11932 case RETURN_EXPR:
11933 case LABEL_EXPR:
11934 case GOTO_EXPR:
11935 case SWITCH_EXPR:
11936 case ASM_EXPR:
11937 /* Expanded in cfgexpand.cc. */
11938 gcc_unreachable ();
11940 case TRY_CATCH_EXPR:
11941 case CATCH_EXPR:
11942 case EH_FILTER_EXPR:
11943 case TRY_FINALLY_EXPR:
11944 case EH_ELSE_EXPR:
11945 /* Lowered by tree-eh.cc. */
11946 gcc_unreachable ();
11948 case WITH_CLEANUP_EXPR:
11949 case CLEANUP_POINT_EXPR:
11950 case TARGET_EXPR:
11951 case CASE_LABEL_EXPR:
11952 case VA_ARG_EXPR:
11953 case BIND_EXPR:
11954 case INIT_EXPR:
11955 case CONJ_EXPR:
11956 case COMPOUND_EXPR:
11957 case PREINCREMENT_EXPR:
11958 case PREDECREMENT_EXPR:
11959 case POSTINCREMENT_EXPR:
11960 case POSTDECREMENT_EXPR:
11961 case LOOP_EXPR:
11962 case EXIT_EXPR:
11963 case COMPOUND_LITERAL_EXPR:
11964 /* Lowered by gimplify.cc. */
11965 gcc_unreachable ();
11967 case FDESC_EXPR:
11968 /* Function descriptors are not valid except for as
11969 initialization constants, and should not be expanded. */
11970 gcc_unreachable ();
11972 case WITH_SIZE_EXPR:
11973 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11974 have pulled out the size to use in whatever context it needed. */
11975 return expand_expr_real (treeop0, original_target, tmode,
11976 modifier, alt_rtl, inner_reference_p);
11978 default:
11979 return expand_expr_real_2 (&ops, target, tmode, modifier);
11983 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11984 signedness of TYPE), possibly returning the result in TARGET.
11985 TYPE is known to be a partial integer type. */
11986 static rtx
11987 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11989 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11990 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11991 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11992 && (!target || GET_MODE (target) == mode));
11994 /* For constant values, reduce using wide_int_to_tree. */
11995 if (poly_int_rtx_p (exp))
11997 auto value = wi::to_poly_wide (exp, mode);
11998 tree t = wide_int_to_tree (type, value);
11999 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12001 else if (TYPE_UNSIGNED (type))
12003 rtx mask = immed_wide_int_const
12004 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12005 return expand_and (mode, exp, mask, target);
12007 else
12009 int count = GET_MODE_PRECISION (mode) - prec;
12010 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12011 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12015 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
12016 when applied to the address of EXP produces an address known to be
12017 aligned more than BIGGEST_ALIGNMENT. */
12019 static int
12020 is_aligning_offset (const_tree offset, const_tree exp)
12022 /* Strip off any conversions. */
12023 while (CONVERT_EXPR_P (offset))
12024 offset = TREE_OPERAND (offset, 0);
12026 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12027 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12028 if (TREE_CODE (offset) != BIT_AND_EXPR
12029 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12030 || compare_tree_int (TREE_OPERAND (offset, 1),
12031 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12032 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12033 return 0;
12035 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12036 It must be NEGATE_EXPR. Then strip any more conversions. */
12037 offset = TREE_OPERAND (offset, 0);
12038 while (CONVERT_EXPR_P (offset))
12039 offset = TREE_OPERAND (offset, 0);
12041 if (TREE_CODE (offset) != NEGATE_EXPR)
12042 return 0;
12044 offset = TREE_OPERAND (offset, 0);
12045 while (CONVERT_EXPR_P (offset))
12046 offset = TREE_OPERAND (offset, 0);
12048 /* This must now be the address of EXP. */
12049 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12052 /* Return a STRING_CST corresponding to ARG's constant initializer either
12053 if it's a string constant, or, when VALREP is set, any other constant,
12054 or null otherwise.
12055 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12056 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12057 to the size of the byte string. If nonnull, set *DECL to the constant
12058 declaration ARG refers to. */
12060 static tree
12061 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12062 bool valrep = false)
12064 tree dummy = NULL_TREE;
12065 if (!mem_size)
12066 mem_size = &dummy;
12068 /* Store the type of the original expression before conversions
12069 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12070 removed. */
12071 tree argtype = TREE_TYPE (arg);
12073 tree array;
12074 STRIP_NOPS (arg);
12076 /* Non-constant index into the character array in an ARRAY_REF
12077 expression or null. */
12078 tree varidx = NULL_TREE;
12080 poly_int64 base_off = 0;
12082 if (TREE_CODE (arg) == ADDR_EXPR)
12084 arg = TREE_OPERAND (arg, 0);
12085 tree ref = arg;
12086 if (TREE_CODE (arg) == ARRAY_REF)
12088 tree idx = TREE_OPERAND (arg, 1);
12089 if (TREE_CODE (idx) != INTEGER_CST)
12091 /* From a pointer (but not array) argument extract the variable
12092 index to prevent get_addr_base_and_unit_offset() from failing
12093 due to it. Use it later to compute the non-constant offset
12094 into the string and return it to the caller. */
12095 varidx = idx;
12096 ref = TREE_OPERAND (arg, 0);
12098 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12099 return NULL_TREE;
12101 if (!integer_zerop (array_ref_low_bound (arg)))
12102 return NULL_TREE;
12104 if (!integer_onep (array_ref_element_size (arg)))
12105 return NULL_TREE;
12108 array = get_addr_base_and_unit_offset (ref, &base_off);
12109 if (!array
12110 || (TREE_CODE (array) != VAR_DECL
12111 && TREE_CODE (array) != CONST_DECL
12112 && TREE_CODE (array) != STRING_CST))
12113 return NULL_TREE;
12115 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12117 tree arg0 = TREE_OPERAND (arg, 0);
12118 tree arg1 = TREE_OPERAND (arg, 1);
12120 tree offset;
12121 tree str = string_constant (arg0, &offset, mem_size, decl);
12122 if (!str)
12124 str = string_constant (arg1, &offset, mem_size, decl);
12125 arg1 = arg0;
12128 if (str)
12130 /* Avoid pointers to arrays (see bug 86622). */
12131 if (POINTER_TYPE_P (TREE_TYPE (arg))
12132 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12133 && !(decl && !*decl)
12134 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12135 && tree_fits_uhwi_p (*mem_size)
12136 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12137 return NULL_TREE;
12139 tree type = TREE_TYPE (offset);
12140 arg1 = fold_convert (type, arg1);
12141 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12142 return str;
12144 return NULL_TREE;
12146 else if (TREE_CODE (arg) == SSA_NAME)
12148 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12149 if (!is_gimple_assign (stmt))
12150 return NULL_TREE;
12152 tree rhs1 = gimple_assign_rhs1 (stmt);
12153 tree_code code = gimple_assign_rhs_code (stmt);
12154 if (code == ADDR_EXPR)
12155 return string_constant (rhs1, ptr_offset, mem_size, decl);
12156 else if (code != POINTER_PLUS_EXPR)
12157 return NULL_TREE;
12159 tree offset;
12160 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12162 /* Avoid pointers to arrays (see bug 86622). */
12163 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12164 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12165 && !(decl && !*decl)
12166 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12167 && tree_fits_uhwi_p (*mem_size)
12168 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12169 return NULL_TREE;
12171 tree rhs2 = gimple_assign_rhs2 (stmt);
12172 tree type = TREE_TYPE (offset);
12173 rhs2 = fold_convert (type, rhs2);
12174 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12175 return str;
12177 return NULL_TREE;
12179 else if (DECL_P (arg))
12180 array = arg;
12181 else
12182 return NULL_TREE;
12184 tree offset = wide_int_to_tree (sizetype, base_off);
12185 if (varidx)
12187 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12188 return NULL_TREE;
12190 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12191 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12192 if (TREE_CODE (chartype) != INTEGER_TYPE)
12193 return NULL;
12195 offset = fold_convert (sizetype, varidx);
12198 if (TREE_CODE (array) == STRING_CST)
12200 *ptr_offset = fold_convert (sizetype, offset);
12201 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12202 if (decl)
12203 *decl = NULL_TREE;
12204 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12205 >= TREE_STRING_LENGTH (array));
12206 return array;
12209 tree init = ctor_for_folding (array);
12210 if (!init || init == error_mark_node)
12211 return NULL_TREE;
12213 if (valrep)
12215 HOST_WIDE_INT cstoff;
12216 if (!base_off.is_constant (&cstoff))
12217 return NULL_TREE;
12219 /* Check that the host and target are sane. */
12220 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12221 return NULL_TREE;
12223 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12224 if (typesz <= 0 || (int) typesz != typesz)
12225 return NULL_TREE;
12227 HOST_WIDE_INT size = typesz;
12228 if (VAR_P (array)
12229 && DECL_SIZE_UNIT (array)
12230 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12232 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12233 gcc_checking_assert (size >= typesz);
12236 /* If value representation was requested convert the initializer
12237 for the whole array or object into a string of bytes forming
12238 its value representation and return it. */
12239 unsigned char *bytes = XNEWVEC (unsigned char, size);
12240 int r = native_encode_initializer (init, bytes, size);
12241 if (r < typesz)
12243 XDELETEVEC (bytes);
12244 return NULL_TREE;
12247 if (r < size)
12248 memset (bytes + r, '\0', size - r);
12250 const char *p = reinterpret_cast<const char *>(bytes);
12251 init = build_string_literal (size, p, char_type_node);
12252 init = TREE_OPERAND (init, 0);
12253 init = TREE_OPERAND (init, 0);
12254 XDELETE (bytes);
12256 *mem_size = size_int (TREE_STRING_LENGTH (init));
12257 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12259 if (decl)
12260 *decl = array;
12262 return init;
12265 if (TREE_CODE (init) == CONSTRUCTOR)
12267 /* Convert the 64-bit constant offset to a wider type to avoid
12268 overflow and use it to obtain the initializer for the subobject
12269 it points into. */
12270 offset_int wioff;
12271 if (!base_off.is_constant (&wioff))
12272 return NULL_TREE;
12274 wioff *= BITS_PER_UNIT;
12275 if (!wi::fits_uhwi_p (wioff))
12276 return NULL_TREE;
12278 base_off = wioff.to_uhwi ();
12279 unsigned HOST_WIDE_INT fieldoff = 0;
12280 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12281 &fieldoff);
12282 if (!init || init == error_mark_node)
12283 return NULL_TREE;
12285 HOST_WIDE_INT cstoff;
12286 if (!base_off.is_constant (&cstoff))
12287 return NULL_TREE;
12289 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12290 tree off = build_int_cst (sizetype, cstoff);
12291 if (varidx)
12292 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12293 else
12294 offset = off;
12297 *ptr_offset = offset;
12299 tree inittype = TREE_TYPE (init);
12301 if (TREE_CODE (init) == INTEGER_CST
12302 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12303 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12305 /* Check that the host and target are sane. */
12306 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12307 return NULL_TREE;
12309 /* For a reference to (address of) a single constant character,
12310 store the native representation of the character in CHARBUF.
12311 If the reference is to an element of an array or a member
12312 of a struct, only consider narrow characters until ctors
12313 for wide character arrays are transformed to STRING_CSTs
12314 like those for narrow arrays. */
12315 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12316 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12317 if (len > 0)
12319 /* Construct a string literal with elements of INITTYPE and
12320 the representation above. Then strip
12321 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12322 init = build_string_literal (len, (char *)charbuf, inittype);
12323 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12327 tree initsize = TYPE_SIZE_UNIT (inittype);
12329 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12331 /* Fold an empty/zero constructor for an implicitly initialized
12332 object or subobject into the empty string. */
12334 /* Determine the character type from that of the original
12335 expression. */
12336 tree chartype = argtype;
12337 if (POINTER_TYPE_P (chartype))
12338 chartype = TREE_TYPE (chartype);
12339 while (TREE_CODE (chartype) == ARRAY_TYPE)
12340 chartype = TREE_TYPE (chartype);
12342 if (INTEGRAL_TYPE_P (chartype)
12343 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12345 /* Convert a char array to an empty STRING_CST having an array
12346 of the expected type and size. */
12347 if (!initsize)
12348 initsize = integer_zero_node;
12350 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12351 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12352 return NULL_TREE;
12354 init = build_string_literal (size, NULL, chartype, size);
12355 init = TREE_OPERAND (init, 0);
12356 init = TREE_OPERAND (init, 0);
12358 *ptr_offset = integer_zero_node;
12362 if (decl)
12363 *decl = array;
12365 if (TREE_CODE (init) != STRING_CST)
12366 return NULL_TREE;
12368 *mem_size = initsize;
12370 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12372 return init;
12375 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12376 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12377 non-constant) offset in bytes within the string that ARG is accessing.
12378 If MEM_SIZE is non-zero the storage size of the memory is returned.
12379 If DECL is non-zero the constant declaration is returned if available. */
12381 tree
12382 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12384 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12387 /* Similar to string_constant, return a STRING_CST corresponding
12388 to the value representation of the first argument if it's
12389 a constant. */
12391 tree
12392 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12394 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12397 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12398 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12399 for C2 > 0 to x & C3 == C2
12400 for C2 < 0 to x & C3 == (C2 & C3). */
12401 enum tree_code
12402 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12404 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12405 tree treeop0 = gimple_assign_rhs1 (stmt);
12406 tree treeop1 = gimple_assign_rhs2 (stmt);
12407 tree type = TREE_TYPE (*arg0);
12408 scalar_int_mode mode;
12409 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12410 return code;
12411 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12412 || TYPE_PRECISION (type) <= 1
12413 || TYPE_UNSIGNED (type)
12414 /* Signed x % c == 0 should have been optimized into unsigned modulo
12415 earlier. */
12416 || integer_zerop (*arg1)
12417 /* If c is known to be non-negative, modulo will be expanded as unsigned
12418 modulo. */
12419 || get_range_pos_neg (treeop0) == 1)
12420 return code;
12422 /* x % c == d where d < 0 && d <= -c should be always false. */
12423 if (tree_int_cst_sgn (*arg1) == -1
12424 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12425 return code;
12427 int prec = TYPE_PRECISION (type);
12428 wide_int w = wi::to_wide (treeop1) - 1;
12429 w |= wi::shifted_mask (0, prec - 1, true, prec);
12430 tree c3 = wide_int_to_tree (type, w);
12431 tree c4 = *arg1;
12432 if (tree_int_cst_sgn (*arg1) == -1)
12433 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12435 rtx op0 = expand_normal (treeop0);
12436 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12438 bool speed_p = optimize_insn_for_speed_p ();
12440 do_pending_stack_adjust ();
12442 location_t loc = gimple_location (stmt);
12443 struct separate_ops ops;
12444 ops.code = TRUNC_MOD_EXPR;
12445 ops.location = loc;
12446 ops.type = TREE_TYPE (treeop0);
12447 ops.op0 = treeop0;
12448 ops.op1 = treeop1;
12449 ops.op2 = NULL_TREE;
12450 start_sequence ();
12451 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12452 EXPAND_NORMAL);
12453 rtx_insn *moinsns = get_insns ();
12454 end_sequence ();
12456 unsigned mocost = seq_cost (moinsns, speed_p);
12457 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12458 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12460 ops.code = BIT_AND_EXPR;
12461 ops.location = loc;
12462 ops.type = TREE_TYPE (treeop0);
12463 ops.op0 = treeop0;
12464 ops.op1 = c3;
12465 ops.op2 = NULL_TREE;
12466 start_sequence ();
12467 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12468 EXPAND_NORMAL);
12469 rtx_insn *muinsns = get_insns ();
12470 end_sequence ();
12472 unsigned mucost = seq_cost (muinsns, speed_p);
12473 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12474 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12476 if (mocost <= mucost)
12478 emit_insn (moinsns);
12479 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12480 return code;
12483 emit_insn (muinsns);
12484 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12485 *arg1 = c4;
12486 return code;
12489 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12490 If C1 is odd to:
12491 (X - C2) * C3 <= C4 (or >), where
12492 C3 is modular multiplicative inverse of C1 and 1<<prec and
12493 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12494 if C2 > ((1<<prec) - 1) % C1).
12495 If C1 is even, S = ctz (C1) and C2 is 0, use
12496 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12497 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12499 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12500 unsigned):
12501 (X * C3) + C4 <= 2 * C4, where
12502 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12503 C4 is ((1<<(prec - 1) - 1) / C1).
12504 If C1 is even, S = ctz(C1), use
12505 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12506 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12507 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12509 See the Hacker's Delight book, section 10-17. */
12510 enum tree_code
12511 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12513 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12514 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12516 if (optimize < 2)
12517 return code;
12519 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12520 if (stmt == NULL)
12521 return code;
12523 tree treeop0 = gimple_assign_rhs1 (stmt);
12524 tree treeop1 = gimple_assign_rhs2 (stmt);
12525 if (TREE_CODE (treeop0) != SSA_NAME
12526 || TREE_CODE (treeop1) != INTEGER_CST
12527 /* Don't optimize the undefined behavior case x % 0;
12528 x % 1 should have been optimized into zero, punt if
12529 it makes it here for whatever reason;
12530 x % -c should have been optimized into x % c. */
12531 || compare_tree_int (treeop1, 2) <= 0
12532 /* Likewise x % c == d where d >= c should be always false. */
12533 || tree_int_cst_le (treeop1, *arg1))
12534 return code;
12536 /* Unsigned x % pow2 is handled right already, for signed
12537 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12538 if (integer_pow2p (treeop1))
12539 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12541 tree type = TREE_TYPE (*arg0);
12542 scalar_int_mode mode;
12543 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12544 return code;
12545 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12546 || TYPE_PRECISION (type) <= 1)
12547 return code;
12549 signop sgn = UNSIGNED;
12550 /* If both operands are known to have the sign bit clear, handle
12551 even the signed modulo case as unsigned. treeop1 is always
12552 positive >= 2, checked above. */
12553 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12554 sgn = SIGNED;
12556 if (!TYPE_UNSIGNED (type))
12558 if (tree_int_cst_sgn (*arg1) == -1)
12559 return code;
12560 type = unsigned_type_for (type);
12561 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12562 return code;
12565 int prec = TYPE_PRECISION (type);
12566 wide_int w = wi::to_wide (treeop1);
12567 int shift = wi::ctz (w);
12568 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12569 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12570 If C1 is odd, we can handle all cases by subtracting
12571 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12572 e.g. by testing for overflow on the subtraction, punt on that for now
12573 though. */
12574 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12576 if (sgn == SIGNED)
12577 return code;
12578 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12579 if (wi::gtu_p (wi::to_wide (*arg1), x))
12580 return code;
12583 imm_use_iterator imm_iter;
12584 use_operand_p use_p;
12585 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12587 gimple *use_stmt = USE_STMT (use_p);
12588 /* Punt if treeop0 is used in the same bb in a division
12589 or another modulo with the same divisor. We should expect
12590 the division and modulo combined together. */
12591 if (use_stmt == stmt
12592 || gimple_bb (use_stmt) != gimple_bb (stmt))
12593 continue;
12594 if (!is_gimple_assign (use_stmt)
12595 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12596 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12597 continue;
12598 if (gimple_assign_rhs1 (use_stmt) != treeop0
12599 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12600 continue;
12601 return code;
12604 w = wi::lrshift (w, shift);
12605 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12606 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12607 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12608 tree c3 = wide_int_to_tree (type, m);
12609 tree c5 = NULL_TREE;
12610 wide_int d, e;
12611 if (sgn == UNSIGNED)
12613 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12614 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12615 otherwise use < or subtract one from C4. E.g. for
12616 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12617 x % 3U == 1 already needs to be
12618 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12619 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12620 d -= 1;
12621 if (shift)
12622 d = wi::lrshift (d, shift);
12624 else
12626 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12627 if (!shift)
12628 d = wi::lshift (e, 1);
12629 else
12631 e = wi::bit_and (e, wi::mask (shift, true, prec));
12632 d = wi::lrshift (e, shift - 1);
12634 c5 = wide_int_to_tree (type, e);
12636 tree c4 = wide_int_to_tree (type, d);
12638 rtx op0 = expand_normal (treeop0);
12639 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12641 bool speed_p = optimize_insn_for_speed_p ();
12643 do_pending_stack_adjust ();
12645 location_t loc = gimple_location (stmt);
12646 struct separate_ops ops;
12647 ops.code = TRUNC_MOD_EXPR;
12648 ops.location = loc;
12649 ops.type = TREE_TYPE (treeop0);
12650 ops.op0 = treeop0;
12651 ops.op1 = treeop1;
12652 ops.op2 = NULL_TREE;
12653 start_sequence ();
12654 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12655 EXPAND_NORMAL);
12656 rtx_insn *moinsns = get_insns ();
12657 end_sequence ();
12659 unsigned mocost = seq_cost (moinsns, speed_p);
12660 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12661 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12663 tree t = fold_convert_loc (loc, type, treeop0);
12664 if (!integer_zerop (*arg1))
12665 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12666 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12667 if (sgn == SIGNED)
12668 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12669 if (shift)
12671 tree s = build_int_cst (NULL_TREE, shift);
12672 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12675 start_sequence ();
12676 rtx mur = expand_normal (t);
12677 rtx_insn *muinsns = get_insns ();
12678 end_sequence ();
12680 unsigned mucost = seq_cost (muinsns, speed_p);
12681 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12682 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12684 if (mocost <= mucost)
12686 emit_insn (moinsns);
12687 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12688 return code;
12691 emit_insn (muinsns);
12692 *arg0 = make_tree (type, mur);
12693 *arg1 = c4;
12694 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12697 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12699 void
12700 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12702 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12703 || code == LT_EXPR || code == LE_EXPR);
12704 gcc_checking_assert (integer_zerop (*arg1));
12706 if (!optimize)
12707 return;
12709 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12710 if (stmt == NULL)
12711 return;
12713 tree treeop0 = gimple_assign_rhs1 (stmt);
12714 tree treeop1 = gimple_assign_rhs2 (stmt);
12715 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12716 return;
12718 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12719 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12720 "assuming signed overflow does not occur when "
12721 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12722 op_symbol_code (code), op_symbol_code (code));
12724 *arg0 = treeop0;
12725 *arg1 = treeop1;
12728 /* Generate code to calculate OPS, and exploded expression
12729 using a store-flag instruction and return an rtx for the result.
12730 OPS reflects a comparison.
12732 If TARGET is nonzero, store the result there if convenient.
12734 Return zero if there is no suitable set-flag instruction
12735 available on this machine.
12737 Once expand_expr has been called on the arguments of the comparison,
12738 we are committed to doing the store flag, since it is not safe to
12739 re-evaluate the expression. We emit the store-flag insn by calling
12740 emit_store_flag, but only expand the arguments if we have a reason
12741 to believe that emit_store_flag will be successful. If we think that
12742 it will, but it isn't, we have to simulate the store-flag with a
12743 set/jump/set sequence. */
12745 static rtx
12746 do_store_flag (sepops ops, rtx target, machine_mode mode)
12748 enum rtx_code code;
12749 tree arg0, arg1, type;
12750 machine_mode operand_mode;
12751 int unsignedp;
12752 rtx op0, op1;
12753 rtx subtarget = target;
12754 location_t loc = ops->location;
12756 arg0 = ops->op0;
12757 arg1 = ops->op1;
12759 /* Don't crash if the comparison was erroneous. */
12760 if (arg0 == error_mark_node || arg1 == error_mark_node)
12761 return const0_rtx;
12763 type = TREE_TYPE (arg0);
12764 operand_mode = TYPE_MODE (type);
12765 unsignedp = TYPE_UNSIGNED (type);
12767 /* We won't bother with BLKmode store-flag operations because it would mean
12768 passing a lot of information to emit_store_flag. */
12769 if (operand_mode == BLKmode)
12770 return 0;
12772 /* We won't bother with store-flag operations involving function pointers
12773 when function pointers must be canonicalized before comparisons. */
12774 if (targetm.have_canonicalize_funcptr_for_compare ()
12775 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12776 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12777 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12778 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12779 return 0;
12781 STRIP_NOPS (arg0);
12782 STRIP_NOPS (arg1);
12784 /* For vector typed comparisons emit code to generate the desired
12785 all-ones or all-zeros mask. */
12786 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12788 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12789 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12790 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12791 return expand_vec_cmp_expr (ops->type, ifexp, target);
12792 else
12793 gcc_unreachable ();
12796 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12797 into (x - C2) * C3 < C4. */
12798 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12799 && TREE_CODE (arg0) == SSA_NAME
12800 && TREE_CODE (arg1) == INTEGER_CST)
12802 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12803 &arg0, &arg1);
12804 if (new_code != ops->code)
12806 struct separate_ops nops = *ops;
12807 nops.code = ops->code = new_code;
12808 nops.op0 = arg0;
12809 nops.op1 = arg1;
12810 nops.type = TREE_TYPE (arg0);
12811 return do_store_flag (&nops, target, mode);
12815 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12816 if (!unsignedp
12817 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12818 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12819 && integer_zerop (arg1)
12820 && TREE_CODE (arg0) == SSA_NAME)
12821 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12823 /* Get the rtx comparison code to use. We know that EXP is a comparison
12824 operation of some type. Some comparisons against 1 and -1 can be
12825 converted to comparisons with zero. Do so here so that the tests
12826 below will be aware that we have a comparison with zero. These
12827 tests will not catch constants in the first operand, but constants
12828 are rarely passed as the first operand. */
12830 switch (ops->code)
12832 case EQ_EXPR:
12833 code = EQ;
12834 break;
12835 case NE_EXPR:
12836 code = NE;
12837 break;
12838 case LT_EXPR:
12839 if (integer_onep (arg1))
12840 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12841 else
12842 code = unsignedp ? LTU : LT;
12843 break;
12844 case LE_EXPR:
12845 if (! unsignedp && integer_all_onesp (arg1))
12846 arg1 = integer_zero_node, code = LT;
12847 else
12848 code = unsignedp ? LEU : LE;
12849 break;
12850 case GT_EXPR:
12851 if (! unsignedp && integer_all_onesp (arg1))
12852 arg1 = integer_zero_node, code = GE;
12853 else
12854 code = unsignedp ? GTU : GT;
12855 break;
12856 case GE_EXPR:
12857 if (integer_onep (arg1))
12858 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12859 else
12860 code = unsignedp ? GEU : GE;
12861 break;
12863 case UNORDERED_EXPR:
12864 code = UNORDERED;
12865 break;
12866 case ORDERED_EXPR:
12867 code = ORDERED;
12868 break;
12869 case UNLT_EXPR:
12870 code = UNLT;
12871 break;
12872 case UNLE_EXPR:
12873 code = UNLE;
12874 break;
12875 case UNGT_EXPR:
12876 code = UNGT;
12877 break;
12878 case UNGE_EXPR:
12879 code = UNGE;
12880 break;
12881 case UNEQ_EXPR:
12882 code = UNEQ;
12883 break;
12884 case LTGT_EXPR:
12885 code = LTGT;
12886 break;
12888 default:
12889 gcc_unreachable ();
12892 /* Put a constant second. */
12893 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12894 || TREE_CODE (arg0) == FIXED_CST)
12896 std::swap (arg0, arg1);
12897 code = swap_condition (code);
12900 /* If this is an equality or inequality test of a single bit, we can
12901 do this by shifting the bit being tested to the low-order bit and
12902 masking the result with the constant 1. If the condition was EQ,
12903 we xor it with 1. This does not require an scc insn and is faster
12904 than an scc insn even if we have it.
12906 The code to make this transformation was moved into fold_single_bit_test,
12907 so we just call into the folder and expand its result. */
12909 if ((code == NE || code == EQ)
12910 && integer_zerop (arg1)
12911 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12913 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12914 if (srcstmt
12915 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12917 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12918 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12919 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12920 gimple_assign_rhs1 (srcstmt),
12921 gimple_assign_rhs2 (srcstmt));
12922 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12923 if (temp)
12924 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12928 if (! get_subtarget (target)
12929 || GET_MODE (subtarget) != operand_mode)
12930 subtarget = 0;
12932 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12934 if (target == 0)
12935 target = gen_reg_rtx (mode);
12937 /* Try a cstore if possible. */
12938 return emit_store_flag_force (target, code, op0, op1,
12939 operand_mode, unsignedp,
12940 (TYPE_PRECISION (ops->type) == 1
12941 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12944 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12945 0 otherwise (i.e. if there is no casesi instruction).
12947 DEFAULT_PROBABILITY is the probability of jumping to the default
12948 label. */
12950 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12951 rtx table_label, rtx default_label, rtx fallback_label,
12952 profile_probability default_probability)
12954 class expand_operand ops[5];
12955 scalar_int_mode index_mode = SImode;
12956 rtx op1, op2, index;
12958 if (! targetm.have_casesi ())
12959 return 0;
12961 /* The index must be some form of integer. Convert it to SImode. */
12962 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12963 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12965 rtx rangertx = expand_normal (range);
12967 /* We must handle the endpoints in the original mode. */
12968 index_expr = build2 (MINUS_EXPR, index_type,
12969 index_expr, minval);
12970 minval = integer_zero_node;
12971 index = expand_normal (index_expr);
12972 if (default_label)
12973 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12974 omode, 1, default_label,
12975 default_probability);
12976 /* Now we can safely truncate. */
12977 index = convert_to_mode (index_mode, index, 0);
12979 else
12981 if (omode != index_mode)
12983 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12984 index_expr = fold_convert (index_type, index_expr);
12987 index = expand_normal (index_expr);
12990 do_pending_stack_adjust ();
12992 op1 = expand_normal (minval);
12993 op2 = expand_normal (range);
12995 create_input_operand (&ops[0], index, index_mode);
12996 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12997 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12998 create_fixed_operand (&ops[3], table_label);
12999 create_fixed_operand (&ops[4], (default_label
13000 ? default_label
13001 : fallback_label));
13002 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13003 return 1;
13006 /* Attempt to generate a tablejump instruction; same concept. */
13007 /* Subroutine of the next function.
13009 INDEX is the value being switched on, with the lowest value
13010 in the table already subtracted.
13011 MODE is its expected mode (needed if INDEX is constant).
13012 RANGE is the length of the jump table.
13013 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13015 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13016 index value is out of range.
13017 DEFAULT_PROBABILITY is the probability of jumping to
13018 the default label. */
13020 static void
13021 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13022 rtx default_label, profile_probability default_probability)
13024 rtx temp, vector;
13026 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13027 cfun->cfg->max_jumptable_ents = INTVAL (range);
13029 /* Do an unsigned comparison (in the proper mode) between the index
13030 expression and the value which represents the length of the range.
13031 Since we just finished subtracting the lower bound of the range
13032 from the index expression, this comparison allows us to simultaneously
13033 check that the original index expression value is both greater than
13034 or equal to the minimum value of the range and less than or equal to
13035 the maximum value of the range. */
13037 if (default_label)
13038 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13039 default_label, default_probability);
13041 /* If index is in range, it must fit in Pmode.
13042 Convert to Pmode so we can index with it. */
13043 if (mode != Pmode)
13045 unsigned int width;
13047 /* We know the value of INDEX is between 0 and RANGE. If we have a
13048 sign-extended subreg, and RANGE does not have the sign bit set, then
13049 we have a value that is valid for both sign and zero extension. In
13050 this case, we get better code if we sign extend. */
13051 if (GET_CODE (index) == SUBREG
13052 && SUBREG_PROMOTED_VAR_P (index)
13053 && SUBREG_PROMOTED_SIGNED_P (index)
13054 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13055 <= HOST_BITS_PER_WIDE_INT)
13056 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13057 index = convert_to_mode (Pmode, index, 0);
13058 else
13059 index = convert_to_mode (Pmode, index, 1);
13062 /* Don't let a MEM slip through, because then INDEX that comes
13063 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13064 and break_out_memory_refs will go to work on it and mess it up. */
13065 #ifdef PIC_CASE_VECTOR_ADDRESS
13066 if (flag_pic && !REG_P (index))
13067 index = copy_to_mode_reg (Pmode, index);
13068 #endif
13070 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13071 GET_MODE_SIZE, because this indicates how large insns are. The other
13072 uses should all be Pmode, because they are addresses. This code
13073 could fail if addresses and insns are not the same size. */
13074 index = simplify_gen_binary (MULT, Pmode, index,
13075 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13076 Pmode));
13077 index = simplify_gen_binary (PLUS, Pmode, index,
13078 gen_rtx_LABEL_REF (Pmode, table_label));
13080 #ifdef PIC_CASE_VECTOR_ADDRESS
13081 if (flag_pic)
13082 index = PIC_CASE_VECTOR_ADDRESS (index);
13083 else
13084 #endif
13085 index = memory_address (CASE_VECTOR_MODE, index);
13086 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13087 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13088 convert_move (temp, vector, 0);
13090 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13092 /* If we are generating PIC code or if the table is PC-relative, the
13093 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13094 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13095 emit_barrier ();
13099 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13100 rtx table_label, rtx default_label,
13101 profile_probability default_probability)
13103 rtx index;
13105 if (! targetm.have_tablejump ())
13106 return 0;
13108 index_expr = fold_build2 (MINUS_EXPR, index_type,
13109 fold_convert (index_type, index_expr),
13110 fold_convert (index_type, minval));
13111 index = expand_normal (index_expr);
13112 do_pending_stack_adjust ();
13114 do_tablejump (index, TYPE_MODE (index_type),
13115 convert_modes (TYPE_MODE (index_type),
13116 TYPE_MODE (TREE_TYPE (range)),
13117 expand_normal (range),
13118 TYPE_UNSIGNED (TREE_TYPE (range))),
13119 table_label, default_label, default_probability);
13120 return 1;
13123 /* Return a CONST_VECTOR rtx representing vector mask for
13124 a VECTOR_CST of booleans. */
13125 static rtx
13126 const_vector_mask_from_tree (tree exp)
13128 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13129 machine_mode inner = GET_MODE_INNER (mode);
13131 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13132 VECTOR_CST_NELTS_PER_PATTERN (exp));
13133 unsigned int count = builder.encoded_nelts ();
13134 for (unsigned int i = 0; i < count; ++i)
13136 tree elt = VECTOR_CST_ELT (exp, i);
13137 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13138 if (integer_zerop (elt))
13139 builder.quick_push (CONST0_RTX (inner));
13140 else if (integer_onep (elt)
13141 || integer_minus_onep (elt))
13142 builder.quick_push (CONSTM1_RTX (inner));
13143 else
13144 gcc_unreachable ();
13146 return builder.build ();
13149 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13150 static rtx
13151 const_vector_from_tree (tree exp)
13153 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13155 if (initializer_zerop (exp))
13156 return CONST0_RTX (mode);
13158 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13159 return const_vector_mask_from_tree (exp);
13161 machine_mode inner = GET_MODE_INNER (mode);
13163 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13164 VECTOR_CST_NELTS_PER_PATTERN (exp));
13165 unsigned int count = builder.encoded_nelts ();
13166 for (unsigned int i = 0; i < count; ++i)
13168 tree elt = VECTOR_CST_ELT (exp, i);
13169 if (TREE_CODE (elt) == REAL_CST)
13170 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13171 inner));
13172 else if (TREE_CODE (elt) == FIXED_CST)
13173 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13174 inner));
13175 else
13176 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13177 inner));
13179 return builder.build ();
13182 /* Build a decl for a personality function given a language prefix. */
13184 tree
13185 build_personality_function (const char *lang)
13187 const char *unwind_and_version;
13188 tree decl, type;
13189 char *name;
13191 switch (targetm_common.except_unwind_info (&global_options))
13193 case UI_NONE:
13194 return NULL;
13195 case UI_SJLJ:
13196 unwind_and_version = "_sj0";
13197 break;
13198 case UI_DWARF2:
13199 case UI_TARGET:
13200 unwind_and_version = "_v0";
13201 break;
13202 case UI_SEH:
13203 unwind_and_version = "_seh0";
13204 break;
13205 default:
13206 gcc_unreachable ();
13209 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13211 type = build_function_type_list (unsigned_type_node,
13212 integer_type_node, integer_type_node,
13213 long_long_unsigned_type_node,
13214 ptr_type_node, ptr_type_node, NULL_TREE);
13215 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13216 get_identifier (name), type);
13217 DECL_ARTIFICIAL (decl) = 1;
13218 DECL_EXTERNAL (decl) = 1;
13219 TREE_PUBLIC (decl) = 1;
13221 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13222 are the flags assigned by targetm.encode_section_info. */
13223 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13225 return decl;
13228 /* Extracts the personality function of DECL and returns the corresponding
13229 libfunc. */
13232 get_personality_function (tree decl)
13234 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13235 enum eh_personality_kind pk;
13237 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13238 if (pk == eh_personality_none)
13239 return NULL;
13241 if (!personality
13242 && pk == eh_personality_any)
13243 personality = lang_hooks.eh_personality ();
13245 if (pk == eh_personality_lang)
13246 gcc_assert (personality != NULL_TREE);
13248 return XEXP (DECL_RTL (personality), 0);
13251 /* Returns a tree for the size of EXP in bytes. */
13253 static tree
13254 tree_expr_size (const_tree exp)
13256 if (DECL_P (exp)
13257 && DECL_SIZE_UNIT (exp) != 0)
13258 return DECL_SIZE_UNIT (exp);
13259 else
13260 return size_in_bytes (TREE_TYPE (exp));
13263 /* Return an rtx for the size in bytes of the value of EXP. */
13266 expr_size (tree exp)
13268 tree size;
13270 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13271 size = TREE_OPERAND (exp, 1);
13272 else
13274 size = tree_expr_size (exp);
13275 gcc_assert (size);
13276 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13279 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13282 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13283 if the size can vary or is larger than an integer. */
13285 HOST_WIDE_INT
13286 int_expr_size (const_tree exp)
13288 tree size;
13290 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13291 size = TREE_OPERAND (exp, 1);
13292 else
13294 size = tree_expr_size (exp);
13295 gcc_assert (size);
13298 if (size == 0 || !tree_fits_shwi_p (size))
13299 return -1;
13301 return tree_to_shwi (size);