For obj-c stage-final re-use the checksum from the previous stage
[official-gcc.git] / gcc / expr.c
blobe4660f0e90a8676b58ac085e9ce4e5a91e9aa174
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2021 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-fold.h"
64 #include "rtx-vector-builder.h"
65 #include "tree-pretty-print.h"
66 #include "flags.h"
69 /* If this is nonzero, we do not bother generating VOLATILE
70 around volatile memory references, and we are willing to
71 output indirect addresses. If cse is to follow, we reject
72 indirect addresses so a useful potential cse is generated;
73 if it is used only once, instruction combination will produce
74 the same indirect address eventually. */
75 int cse_not_expected;
77 static bool block_move_libcall_safe_for_call_parm (void);
78 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
79 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
80 unsigned HOST_WIDE_INT,
81 unsigned HOST_WIDE_INT, bool);
82 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
83 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
84 static rtx_insn *compress_float_constant (rtx, rtx);
85 static rtx get_subtarget (rtx);
86 static void store_constructor (tree, rtx, int, poly_int64, bool);
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 HOST_WIDE_INT int_expr_size (tree);
103 static void convert_mode_scalar (rtx, rtx, int);
106 /* This is run to set up which modes can be used
107 directly in memory and to initialize the block move optab. It is run
108 at the beginning of compilation and when the target is reinitialized. */
110 void
111 init_expr_target (void)
113 rtx pat;
114 int num_clobbers;
115 rtx mem, mem1;
116 rtx reg;
118 /* Try indexing by frame ptr and try by stack ptr.
119 It is known that on the Convex the stack ptr isn't a valid index.
120 With luck, one or the other is valid on any machine. */
121 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
122 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
124 /* A scratch register we can modify in-place below to avoid
125 useless RTL allocations. */
126 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
128 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
129 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
130 PATTERN (insn) = pat;
132 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
133 mode = (machine_mode) ((int) mode + 1))
135 int regno;
137 direct_load[(int) mode] = direct_store[(int) mode] = 0;
138 PUT_MODE (mem, mode);
139 PUT_MODE (mem1, mode);
141 /* See if there is some register that can be used in this mode and
142 directly loaded or stored from memory. */
144 if (mode != VOIDmode && mode != BLKmode)
145 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
146 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
147 regno++)
149 if (!targetm.hard_regno_mode_ok (regno, mode))
150 continue;
152 set_mode_and_regno (reg, mode, regno);
154 SET_SRC (pat) = mem;
155 SET_DEST (pat) = reg;
156 if (recog (pat, insn, &num_clobbers) >= 0)
157 direct_load[(int) mode] = 1;
159 SET_SRC (pat) = mem1;
160 SET_DEST (pat) = reg;
161 if (recog (pat, insn, &num_clobbers) >= 0)
162 direct_load[(int) mode] = 1;
164 SET_SRC (pat) = reg;
165 SET_DEST (pat) = mem;
166 if (recog (pat, insn, &num_clobbers) >= 0)
167 direct_store[(int) mode] = 1;
169 SET_SRC (pat) = reg;
170 SET_DEST (pat) = mem1;
171 if (recog (pat, insn, &num_clobbers) >= 0)
172 direct_store[(int) mode] = 1;
176 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
178 opt_scalar_float_mode mode_iter;
179 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
181 scalar_float_mode mode = mode_iter.require ();
182 scalar_float_mode srcmode;
183 FOR_EACH_MODE_UNTIL (srcmode, mode)
185 enum insn_code ic;
187 ic = can_extend_p (mode, srcmode, 0);
188 if (ic == CODE_FOR_nothing)
189 continue;
191 PUT_MODE (mem, srcmode);
193 if (insn_operand_matches (ic, 1, mem))
194 float_extend_from_mem[mode][srcmode] = true;
199 /* This is run at the start of compiling a function. */
201 void
202 init_expr (void)
204 memset (&crtl->expr, 0, sizeof (crtl->expr));
207 /* Copy data from FROM to TO, where the machine modes are not the same.
208 Both modes may be integer, or both may be floating, or both may be
209 fixed-point.
210 UNSIGNEDP should be nonzero if FROM is an unsigned type.
211 This causes zero-extension instead of sign-extension. */
213 void
214 convert_move (rtx to, rtx from, int unsignedp)
216 machine_mode to_mode = GET_MODE (to);
217 machine_mode from_mode = GET_MODE (from);
219 gcc_assert (to_mode != BLKmode);
220 gcc_assert (from_mode != BLKmode);
222 /* If the source and destination are already the same, then there's
223 nothing to do. */
224 if (to == from)
225 return;
227 /* If FROM is a SUBREG that indicates that we have already done at least
228 the required extension, strip it. We don't handle such SUBREGs as
229 TO here. */
231 scalar_int_mode to_int_mode;
232 if (GET_CODE (from) == SUBREG
233 && SUBREG_PROMOTED_VAR_P (from)
234 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
235 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
236 >= GET_MODE_PRECISION (to_int_mode))
237 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
239 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
240 from_mode = to_int_mode;
243 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
245 if (to_mode == from_mode
246 || (from_mode == VOIDmode && CONSTANT_P (from)))
248 emit_move_insn (to, from);
249 return;
252 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
254 if (GET_MODE_UNIT_PRECISION (to_mode)
255 > GET_MODE_UNIT_PRECISION (from_mode))
257 optab op = unsignedp ? zext_optab : sext_optab;
258 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
259 if (icode != CODE_FOR_nothing)
261 emit_unop_insn (icode, to, from,
262 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
263 return;
267 if (GET_MODE_UNIT_PRECISION (to_mode)
268 < GET_MODE_UNIT_PRECISION (from_mode))
270 insn_code icode = convert_optab_handler (trunc_optab,
271 to_mode, from_mode);
272 if (icode != CODE_FOR_nothing)
274 emit_unop_insn (icode, to, from, TRUNCATE);
275 return;
279 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
280 GET_MODE_BITSIZE (to_mode)));
282 if (VECTOR_MODE_P (to_mode))
283 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
284 else
285 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
287 emit_move_insn (to, from);
288 return;
291 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
293 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
294 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
295 return;
298 convert_mode_scalar (to, from, unsignedp);
301 /* Like convert_move, but deals only with scalar modes. */
303 static void
304 convert_mode_scalar (rtx to, rtx from, int unsignedp)
306 /* Both modes should be scalar types. */
307 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
308 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
309 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
310 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
311 enum insn_code code;
312 rtx libcall;
314 gcc_assert (to_real == from_real);
316 /* rtx code for making an equivalent value. */
317 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
318 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
320 if (to_real)
322 rtx value;
323 rtx_insn *insns;
324 convert_optab tab;
326 gcc_assert ((GET_MODE_PRECISION (from_mode)
327 != GET_MODE_PRECISION (to_mode))
328 || (DECIMAL_FLOAT_MODE_P (from_mode)
329 != DECIMAL_FLOAT_MODE_P (to_mode)));
331 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
332 /* Conversion between decimal float and binary float, same size. */
333 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
334 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
335 tab = sext_optab;
336 else
337 tab = trunc_optab;
339 /* Try converting directly if the insn is supported. */
341 code = convert_optab_handler (tab, to_mode, from_mode);
342 if (code != CODE_FOR_nothing)
344 emit_unop_insn (code, to, from,
345 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
346 return;
349 /* Otherwise use a libcall. */
350 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
352 /* Is this conversion implemented yet? */
353 gcc_assert (libcall);
355 start_sequence ();
356 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
357 from, from_mode);
358 insns = get_insns ();
359 end_sequence ();
360 emit_libcall_block (insns, to, value,
361 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
362 from)
363 : gen_rtx_FLOAT_EXTEND (to_mode, from));
364 return;
367 /* Handle pointer conversion. */ /* SPEE 900220. */
368 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
370 convert_optab ctab;
372 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
373 ctab = trunc_optab;
374 else if (unsignedp)
375 ctab = zext_optab;
376 else
377 ctab = sext_optab;
379 if (convert_optab_handler (ctab, to_mode, from_mode)
380 != CODE_FOR_nothing)
382 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
383 to, from, UNKNOWN);
384 return;
388 /* Targets are expected to provide conversion insns between PxImode and
389 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
390 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
392 scalar_int_mode full_mode
393 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
395 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
396 != CODE_FOR_nothing);
398 if (full_mode != from_mode)
399 from = convert_to_mode (full_mode, from, unsignedp);
400 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
401 to, from, UNKNOWN);
402 return;
404 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
406 rtx new_from;
407 scalar_int_mode full_mode
408 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
409 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
410 enum insn_code icode;
412 icode = convert_optab_handler (ctab, full_mode, from_mode);
413 gcc_assert (icode != CODE_FOR_nothing);
415 if (to_mode == full_mode)
417 emit_unop_insn (icode, to, from, UNKNOWN);
418 return;
421 new_from = gen_reg_rtx (full_mode);
422 emit_unop_insn (icode, new_from, from, UNKNOWN);
424 /* else proceed to integer conversions below. */
425 from_mode = full_mode;
426 from = new_from;
429 /* Make sure both are fixed-point modes or both are not. */
430 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
431 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
432 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
434 /* If we widen from_mode to to_mode and they are in the same class,
435 we won't saturate the result.
436 Otherwise, always saturate the result to play safe. */
437 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
438 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
439 expand_fixed_convert (to, from, 0, 0);
440 else
441 expand_fixed_convert (to, from, 0, 1);
442 return;
445 /* Now both modes are integers. */
447 /* Handle expanding beyond a word. */
448 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
449 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
451 rtx_insn *insns;
452 rtx lowpart;
453 rtx fill_value;
454 rtx lowfrom;
455 int i;
456 scalar_mode lowpart_mode;
457 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
459 /* Try converting directly if the insn is supported. */
460 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
461 != CODE_FOR_nothing)
463 /* If FROM is a SUBREG, put it into a register. Do this
464 so that we always generate the same set of insns for
465 better cse'ing; if an intermediate assignment occurred,
466 we won't be doing the operation directly on the SUBREG. */
467 if (optimize > 0 && GET_CODE (from) == SUBREG)
468 from = force_reg (from_mode, from);
469 emit_unop_insn (code, to, from, equiv_code);
470 return;
472 /* Next, try converting via full word. */
473 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
474 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
475 != CODE_FOR_nothing))
477 rtx word_to = gen_reg_rtx (word_mode);
478 if (REG_P (to))
480 if (reg_overlap_mentioned_p (to, from))
481 from = force_reg (from_mode, from);
482 emit_clobber (to);
484 convert_move (word_to, from, unsignedp);
485 emit_unop_insn (code, to, word_to, equiv_code);
486 return;
489 /* No special multiword conversion insn; do it by hand. */
490 start_sequence ();
492 /* Since we will turn this into a no conflict block, we must ensure
493 the source does not overlap the target so force it into an isolated
494 register when maybe so. Likewise for any MEM input, since the
495 conversion sequence might require several references to it and we
496 must ensure we're getting the same value every time. */
498 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
499 from = force_reg (from_mode, from);
501 /* Get a copy of FROM widened to a word, if necessary. */
502 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
503 lowpart_mode = word_mode;
504 else
505 lowpart_mode = from_mode;
507 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
509 lowpart = gen_lowpart (lowpart_mode, to);
510 emit_move_insn (lowpart, lowfrom);
512 /* Compute the value to put in each remaining word. */
513 if (unsignedp)
514 fill_value = const0_rtx;
515 else
516 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
517 LT, lowfrom, const0_rtx,
518 lowpart_mode, 0, -1);
520 /* Fill the remaining words. */
521 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
523 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
524 rtx subword = operand_subword (to, index, 1, to_mode);
526 gcc_assert (subword);
528 if (fill_value != subword)
529 emit_move_insn (subword, fill_value);
532 insns = get_insns ();
533 end_sequence ();
535 emit_insn (insns);
536 return;
539 /* Truncating multi-word to a word or less. */
540 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
541 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
543 if (!((MEM_P (from)
544 && ! MEM_VOLATILE_P (from)
545 && direct_load[(int) to_mode]
546 && ! mode_dependent_address_p (XEXP (from, 0),
547 MEM_ADDR_SPACE (from)))
548 || REG_P (from)
549 || GET_CODE (from) == SUBREG))
550 from = force_reg (from_mode, from);
551 convert_move (to, gen_lowpart (word_mode, from), 0);
552 return;
555 /* Now follow all the conversions between integers
556 no more than a word long. */
558 /* For truncation, usually we can just refer to FROM in a narrower mode. */
559 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
560 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
562 if (!((MEM_P (from)
563 && ! MEM_VOLATILE_P (from)
564 && direct_load[(int) to_mode]
565 && ! mode_dependent_address_p (XEXP (from, 0),
566 MEM_ADDR_SPACE (from)))
567 || REG_P (from)
568 || GET_CODE (from) == SUBREG))
569 from = force_reg (from_mode, from);
570 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
571 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
572 from = copy_to_reg (from);
573 emit_move_insn (to, gen_lowpart (to_mode, from));
574 return;
577 /* Handle extension. */
578 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
580 /* Convert directly if that works. */
581 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
582 != CODE_FOR_nothing)
584 emit_unop_insn (code, to, from, equiv_code);
585 return;
587 else
589 rtx tmp;
590 int shift_amount;
592 /* Search for a mode to convert via. */
593 opt_scalar_mode intermediate_iter;
594 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
596 scalar_mode intermediate = intermediate_iter.require ();
597 if (((can_extend_p (to_mode, intermediate, unsignedp)
598 != CODE_FOR_nothing)
599 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
600 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
601 intermediate)))
602 && (can_extend_p (intermediate, from_mode, unsignedp)
603 != CODE_FOR_nothing))
605 convert_move (to, convert_to_mode (intermediate, from,
606 unsignedp), unsignedp);
607 return;
611 /* No suitable intermediate mode.
612 Generate what we need with shifts. */
613 shift_amount = (GET_MODE_PRECISION (to_mode)
614 - GET_MODE_PRECISION (from_mode));
615 from = gen_lowpart (to_mode, force_reg (from_mode, from));
616 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
617 to, unsignedp);
618 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
619 to, unsignedp);
620 if (tmp != to)
621 emit_move_insn (to, tmp);
622 return;
626 /* Support special truncate insns for certain modes. */
627 if (convert_optab_handler (trunc_optab, to_mode,
628 from_mode) != CODE_FOR_nothing)
630 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
631 to, from, UNKNOWN);
632 return;
635 /* Handle truncation of volatile memrefs, and so on;
636 the things that couldn't be truncated directly,
637 and for which there was no special instruction.
639 ??? Code above formerly short-circuited this, for most integer
640 mode pairs, with a force_reg in from_mode followed by a recursive
641 call to this routine. Appears always to have been wrong. */
642 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
644 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
645 emit_move_insn (to, temp);
646 return;
649 /* Mode combination is not recognized. */
650 gcc_unreachable ();
653 /* Return an rtx for a value that would result
654 from converting X to mode MODE.
655 Both X and MODE may be floating, or both integer.
656 UNSIGNEDP is nonzero if X is an unsigned value.
657 This can be done by referring to a part of X in place
658 or by copying to a new temporary with conversion. */
661 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
663 return convert_modes (mode, VOIDmode, x, unsignedp);
666 /* Return an rtx for a value that would result
667 from converting X from mode OLDMODE to mode MODE.
668 Both modes may be floating, or both integer.
669 UNSIGNEDP is nonzero if X is an unsigned value.
671 This can be done by referring to a part of X in place
672 or by copying to a new temporary with conversion.
674 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
677 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
679 rtx temp;
680 scalar_int_mode int_mode;
682 /* If FROM is a SUBREG that indicates that we have already done at least
683 the required extension, strip it. */
685 if (GET_CODE (x) == SUBREG
686 && SUBREG_PROMOTED_VAR_P (x)
687 && is_a <scalar_int_mode> (mode, &int_mode)
688 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
689 >= GET_MODE_PRECISION (int_mode))
690 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
691 x = gen_lowpart (int_mode, SUBREG_REG (x));
693 if (GET_MODE (x) != VOIDmode)
694 oldmode = GET_MODE (x);
696 if (mode == oldmode)
697 return x;
699 if (CONST_SCALAR_INT_P (x)
700 && is_a <scalar_int_mode> (mode, &int_mode))
702 /* If the caller did not tell us the old mode, then there is not
703 much to do with respect to canonicalization. We have to
704 assume that all the bits are significant. */
705 if (!is_a <scalar_int_mode> (oldmode))
706 oldmode = MAX_MODE_INT;
707 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
708 GET_MODE_PRECISION (int_mode),
709 unsignedp ? UNSIGNED : SIGNED);
710 return immed_wide_int_const (w, int_mode);
713 /* We can do this with a gen_lowpart if both desired and current modes
714 are integer, and this is either a constant integer, a register, or a
715 non-volatile MEM. */
716 scalar_int_mode int_oldmode;
717 if (is_int_mode (mode, &int_mode)
718 && is_int_mode (oldmode, &int_oldmode)
719 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
720 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
721 || CONST_POLY_INT_P (x)
722 || (REG_P (x)
723 && (!HARD_REGISTER_P (x)
724 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
725 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
726 return gen_lowpart (int_mode, x);
728 /* Converting from integer constant into mode is always equivalent to an
729 subreg operation. */
730 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
732 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
733 GET_MODE_BITSIZE (oldmode)));
734 return simplify_gen_subreg (mode, x, oldmode, 0);
737 temp = gen_reg_rtx (mode);
738 convert_move (temp, x, unsignedp);
739 return temp;
742 /* Return the largest alignment we can use for doing a move (or store)
743 of MAX_PIECES. ALIGN is the largest alignment we could use. */
745 static unsigned int
746 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
748 scalar_int_mode tmode
749 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
751 if (align >= GET_MODE_ALIGNMENT (tmode))
752 align = GET_MODE_ALIGNMENT (tmode);
753 else
755 scalar_int_mode xmode = NARROWEST_INT_MODE;
756 opt_scalar_int_mode mode_iter;
757 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
759 tmode = mode_iter.require ();
760 if (GET_MODE_SIZE (tmode) > max_pieces
761 || targetm.slow_unaligned_access (tmode, align))
762 break;
763 xmode = tmode;
766 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
769 return align;
772 /* Return the widest integer mode that is narrower than SIZE bytes. */
774 static scalar_int_mode
775 widest_int_mode_for_size (unsigned int size)
777 scalar_int_mode result = NARROWEST_INT_MODE;
779 gcc_checking_assert (size > 1);
781 opt_scalar_int_mode tmode;
782 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
783 if (GET_MODE_SIZE (tmode.require ()) < size)
784 result = tmode.require ();
786 return result;
789 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
790 and should be performed piecewise. */
792 static bool
793 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
794 enum by_pieces_operation op)
796 return targetm.use_by_pieces_infrastructure_p (len, align, op,
797 optimize_insn_for_speed_p ());
800 /* Determine whether the LEN bytes can be moved by using several move
801 instructions. Return nonzero if a call to move_by_pieces should
802 succeed. */
804 bool
805 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
807 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
810 /* Return number of insns required to perform operation OP by pieces
811 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
813 unsigned HOST_WIDE_INT
814 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
815 unsigned int max_size, by_pieces_operation op)
817 unsigned HOST_WIDE_INT n_insns = 0;
818 scalar_int_mode mode;
820 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
822 /* NB: Round up L and ALIGN to the widest integer mode for
823 MAX_SIZE. */
824 mode = widest_int_mode_for_size (max_size);
825 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
827 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
828 if (up > l)
829 l = up;
830 align = GET_MODE_ALIGNMENT (mode);
834 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
836 while (max_size > 1 && l > 0)
838 mode = widest_int_mode_for_size (max_size);
839 enum insn_code icode;
841 unsigned int modesize = GET_MODE_SIZE (mode);
843 icode = optab_handler (mov_optab, mode);
844 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
846 unsigned HOST_WIDE_INT n_pieces = l / modesize;
847 l %= modesize;
848 switch (op)
850 default:
851 n_insns += n_pieces;
852 break;
854 case COMPARE_BY_PIECES:
855 int batch = targetm.compare_by_pieces_branch_ratio (mode);
856 int batch_ops = 4 * batch - 1;
857 unsigned HOST_WIDE_INT full = n_pieces / batch;
858 n_insns += full * batch_ops;
859 if (n_pieces % batch != 0)
860 n_insns++;
861 break;
865 max_size = modesize;
868 gcc_assert (!l);
869 return n_insns;
872 /* Used when performing piecewise block operations, holds information
873 about one of the memory objects involved. The member functions
874 can be used to generate code for loading from the object and
875 updating the address when iterating. */
877 class pieces_addr
879 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
880 stack pushes. */
881 rtx m_obj;
882 /* The address of the object. Can differ from that seen in the
883 MEM rtx if we copied the address to a register. */
884 rtx m_addr;
885 /* Nonzero if the address on the object has an autoincrement already,
886 signifies whether that was an increment or decrement. */
887 signed char m_addr_inc;
888 /* Nonzero if we intend to use autoinc without the address already
889 having autoinc form. We will insert add insns around each memory
890 reference, expecting later passes to form autoinc addressing modes.
891 The only supported options are predecrement and postincrement. */
892 signed char m_explicit_inc;
893 /* True if we have either of the two possible cases of using
894 autoincrement. */
895 bool m_auto;
896 /* True if this is an address to be used for load operations rather
897 than stores. */
898 bool m_is_load;
900 /* Optionally, a function to obtain constants for any given offset into
901 the objects, and data associated with it. */
902 by_pieces_constfn m_constfn;
903 void *m_cfndata;
904 public:
905 pieces_addr (rtx, bool, by_pieces_constfn, void *);
906 rtx adjust (scalar_int_mode, HOST_WIDE_INT,
907 by_pieces_prev * = nullptr);
908 void increment_address (HOST_WIDE_INT);
909 void maybe_predec (HOST_WIDE_INT);
910 void maybe_postinc (HOST_WIDE_INT);
911 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
912 int get_addr_inc ()
914 return m_addr_inc;
918 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
919 true if the operation to be performed on this object is a load
920 rather than a store. For stores, OBJ can be NULL, in which case we
921 assume the operation is a stack push. For loads, the optional
922 CONSTFN and its associated CFNDATA can be used in place of the
923 memory load. */
925 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
926 void *cfndata)
927 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
929 m_addr_inc = 0;
930 m_auto = false;
931 if (obj)
933 rtx addr = XEXP (obj, 0);
934 rtx_code code = GET_CODE (addr);
935 m_addr = addr;
936 bool dec = code == PRE_DEC || code == POST_DEC;
937 bool inc = code == PRE_INC || code == POST_INC;
938 m_auto = inc || dec;
939 if (m_auto)
940 m_addr_inc = dec ? -1 : 1;
942 /* While we have always looked for these codes here, the code
943 implementing the memory operation has never handled them.
944 Support could be added later if necessary or beneficial. */
945 gcc_assert (code != PRE_INC && code != POST_DEC);
947 else
949 m_addr = NULL_RTX;
950 if (!is_load)
952 m_auto = true;
953 if (STACK_GROWS_DOWNWARD)
954 m_addr_inc = -1;
955 else
956 m_addr_inc = 1;
958 else
959 gcc_assert (constfn != NULL);
961 m_explicit_inc = 0;
962 if (constfn)
963 gcc_assert (is_load);
966 /* Decide whether to use autoinc for an address involved in a memory op.
967 MODE is the mode of the accesses, REVERSE is true if we've decided to
968 perform the operation starting from the end, and LEN is the length of
969 the operation. Don't override an earlier decision to set m_auto. */
971 void
972 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
973 HOST_WIDE_INT len)
975 if (m_auto || m_obj == NULL_RTX)
976 return;
978 bool use_predec = (m_is_load
979 ? USE_LOAD_PRE_DECREMENT (mode)
980 : USE_STORE_PRE_DECREMENT (mode));
981 bool use_postinc = (m_is_load
982 ? USE_LOAD_POST_INCREMENT (mode)
983 : USE_STORE_POST_INCREMENT (mode));
984 machine_mode addr_mode = get_address_mode (m_obj);
986 if (use_predec && reverse)
988 m_addr = copy_to_mode_reg (addr_mode,
989 plus_constant (addr_mode,
990 m_addr, len));
991 m_auto = true;
992 m_explicit_inc = -1;
994 else if (use_postinc && !reverse)
996 m_addr = copy_to_mode_reg (addr_mode, m_addr);
997 m_auto = true;
998 m_explicit_inc = 1;
1000 else if (CONSTANT_P (m_addr))
1001 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1004 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1005 are using autoincrement for this address, we don't add the offset,
1006 but we still modify the MEM's properties. */
1009 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset,
1010 by_pieces_prev *prev)
1012 if (m_constfn)
1013 /* Pass the previous data to m_constfn. */
1014 return m_constfn (m_cfndata, prev, offset, mode);
1015 if (m_obj == NULL_RTX)
1016 return NULL_RTX;
1017 if (m_auto)
1018 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1019 else
1020 return adjust_address (m_obj, mode, offset);
1023 /* Emit an add instruction to increment the address by SIZE. */
1025 void
1026 pieces_addr::increment_address (HOST_WIDE_INT size)
1028 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1029 emit_insn (gen_add2_insn (m_addr, amount));
1032 /* If we are supposed to decrement the address after each access, emit code
1033 to do so now. Increment by SIZE (which has should have the correct sign
1034 already). */
1036 void
1037 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1039 if (m_explicit_inc >= 0)
1040 return;
1041 gcc_assert (HAVE_PRE_DECREMENT);
1042 increment_address (size);
1045 /* If we are supposed to decrement the address after each access, emit code
1046 to do so now. Increment by SIZE. */
1048 void
1049 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1051 if (m_explicit_inc <= 0)
1052 return;
1053 gcc_assert (HAVE_POST_INCREMENT);
1054 increment_address (size);
1057 /* This structure is used by do_op_by_pieces to describe the operation
1058 to be performed. */
1060 class op_by_pieces_d
1062 private:
1063 scalar_int_mode get_usable_mode (scalar_int_mode mode, unsigned int);
1065 protected:
1066 pieces_addr m_to, m_from;
1067 unsigned HOST_WIDE_INT m_len;
1068 HOST_WIDE_INT m_offset;
1069 unsigned int m_align;
1070 unsigned int m_max_size;
1071 bool m_reverse;
1072 /* True if this is a stack push. */
1073 bool m_push;
1074 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1075 bool m_overlap_op_by_pieces;
1077 /* Virtual functions, overriden by derived classes for the specific
1078 operation. */
1079 virtual void generate (rtx, rtx, machine_mode) = 0;
1080 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1081 virtual void finish_mode (machine_mode)
1085 public:
1086 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1087 unsigned HOST_WIDE_INT, unsigned int, bool);
1088 void run ();
1091 /* The constructor for an op_by_pieces_d structure. We require two
1092 objects named TO and FROM, which are identified as loads or stores
1093 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1094 and its associated FROM_CFN_DATA can be used to replace loads with
1095 constant values. LEN describes the length of the operation. */
1097 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1098 rtx from, bool from_load,
1099 by_pieces_constfn from_cfn,
1100 void *from_cfn_data,
1101 unsigned HOST_WIDE_INT len,
1102 unsigned int align, bool push)
1103 : m_to (to, to_load, NULL, NULL),
1104 m_from (from, from_load, from_cfn, from_cfn_data),
1105 m_len (len), m_max_size (MOVE_MAX_PIECES + 1),
1106 m_push (push)
1108 int toi = m_to.get_addr_inc ();
1109 int fromi = m_from.get_addr_inc ();
1110 if (toi >= 0 && fromi >= 0)
1111 m_reverse = false;
1112 else if (toi <= 0 && fromi <= 0)
1113 m_reverse = true;
1114 else
1115 gcc_unreachable ();
1117 m_offset = m_reverse ? len : 0;
1118 align = MIN (to ? MEM_ALIGN (to) : align,
1119 from ? MEM_ALIGN (from) : align);
1121 /* If copying requires more than two move insns,
1122 copy addresses to registers (to make displacements shorter)
1123 and use post-increment if available. */
1124 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1126 /* Find the mode of the largest comparison. */
1127 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1129 m_from.decide_autoinc (mode, m_reverse, len);
1130 m_to.decide_autoinc (mode, m_reverse, len);
1133 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1134 m_align = align;
1136 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1139 /* This function returns the largest usable integer mode for LEN bytes
1140 whose size is no bigger than size of MODE. */
1142 scalar_int_mode
1143 op_by_pieces_d::get_usable_mode (scalar_int_mode mode, unsigned int len)
1145 unsigned int size;
1148 size = GET_MODE_SIZE (mode);
1149 if (len >= size && prepare_mode (mode, m_align))
1150 break;
1151 /* NB: widest_int_mode_for_size checks SIZE > 1. */
1152 mode = widest_int_mode_for_size (size);
1154 while (1);
1155 return mode;
1158 /* This function contains the main loop used for expanding a block
1159 operation. First move what we can in the largest integer mode,
1160 then go to successively smaller modes. For every access, call
1161 GENFUN with the two operands and the EXTRA_DATA. */
1163 void
1164 op_by_pieces_d::run ()
1166 if (m_len == 0)
1167 return;
1169 /* NB: widest_int_mode_for_size checks M_MAX_SIZE > 1. */
1170 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1171 mode = get_usable_mode (mode, m_len);
1173 by_pieces_prev to_prev = { nullptr, mode };
1174 by_pieces_prev from_prev = { nullptr, mode };
1178 unsigned int size = GET_MODE_SIZE (mode);
1179 rtx to1 = NULL_RTX, from1;
1181 while (m_len >= size)
1183 if (m_reverse)
1184 m_offset -= size;
1186 to1 = m_to.adjust (mode, m_offset, &to_prev);
1187 to_prev.data = to1;
1188 to_prev.mode = mode;
1189 from1 = m_from.adjust (mode, m_offset, &from_prev);
1190 from_prev.data = from1;
1191 from_prev.mode = mode;
1193 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1194 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1196 generate (to1, from1, mode);
1198 m_to.maybe_postinc (size);
1199 m_from.maybe_postinc (size);
1201 if (!m_reverse)
1202 m_offset += size;
1204 m_len -= size;
1207 finish_mode (mode);
1209 if (m_len == 0)
1210 return;
1212 if (!m_push && m_overlap_op_by_pieces)
1214 /* NB: Generate overlapping operations if it is not a stack
1215 push since stack push must not overlap. Get the smallest
1216 integer mode for M_LEN bytes. */
1217 mode = smallest_int_mode_for_size (m_len * BITS_PER_UNIT);
1218 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1219 int gap = GET_MODE_SIZE (mode) - m_len;
1220 if (gap > 0)
1222 /* If size of MODE > M_LEN, generate the last operation
1223 in MODE for the remaining bytes with ovelapping memory
1224 from the previois operation. */
1225 if (m_reverse)
1226 m_offset += gap;
1227 else
1228 m_offset -= gap;
1229 m_len += gap;
1232 else
1234 /* NB: widest_int_mode_for_size checks SIZE > 1. */
1235 mode = widest_int_mode_for_size (size);
1236 mode = get_usable_mode (mode, m_len);
1239 while (1);
1241 /* The code above should have handled everything. */
1242 gcc_assert (!m_len);
1245 /* Derived class from op_by_pieces_d, providing support for block move
1246 operations. */
1248 #ifdef PUSH_ROUNDING
1249 #define PUSHG_P(to) ((to) == nullptr)
1250 #else
1251 #define PUSHG_P(to) false
1252 #endif
1254 class move_by_pieces_d : public op_by_pieces_d
1256 insn_gen_fn m_gen_fun;
1257 void generate (rtx, rtx, machine_mode);
1258 bool prepare_mode (machine_mode, unsigned int);
1260 public:
1261 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1262 unsigned int align)
1263 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align,
1264 PUSHG_P (to))
1267 rtx finish_retmode (memop_ret);
1270 /* Return true if MODE can be used for a set of copies, given an
1271 alignment ALIGN. Prepare whatever data is necessary for later
1272 calls to generate. */
1274 bool
1275 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1277 insn_code icode = optab_handler (mov_optab, mode);
1278 m_gen_fun = GEN_FCN (icode);
1279 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1282 /* A callback used when iterating for a compare_by_pieces_operation.
1283 OP0 and OP1 are the values that have been loaded and should be
1284 compared in MODE. If OP0 is NULL, this means we should generate a
1285 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1286 gen function that should be used to generate the mode. */
1288 void
1289 move_by_pieces_d::generate (rtx op0, rtx op1,
1290 machine_mode mode ATTRIBUTE_UNUSED)
1292 #ifdef PUSH_ROUNDING
1293 if (op0 == NULL_RTX)
1295 emit_single_push_insn (mode, op1, NULL);
1296 return;
1298 #endif
1299 emit_insn (m_gen_fun (op0, op1));
1302 /* Perform the final adjustment at the end of a string to obtain the
1303 correct return value for the block operation.
1304 Return value is based on RETMODE argument. */
1307 move_by_pieces_d::finish_retmode (memop_ret retmode)
1309 gcc_assert (!m_reverse);
1310 if (retmode == RETURN_END_MINUS_ONE)
1312 m_to.maybe_postinc (-1);
1313 --m_offset;
1315 return m_to.adjust (QImode, m_offset);
1318 /* Generate several move instructions to copy LEN bytes from block FROM to
1319 block TO. (These are MEM rtx's with BLKmode).
1321 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1322 used to push FROM to the stack.
1324 ALIGN is maximum stack alignment we can assume.
1326 Return value is based on RETMODE argument. */
1329 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1330 unsigned int align, memop_ret retmode)
1332 #ifndef PUSH_ROUNDING
1333 if (to == NULL)
1334 gcc_unreachable ();
1335 #endif
1337 move_by_pieces_d data (to, from, len, align);
1339 data.run ();
1341 if (retmode != RETURN_BEGIN)
1342 return data.finish_retmode (retmode);
1343 else
1344 return to;
1347 /* Derived class from op_by_pieces_d, providing support for block move
1348 operations. */
1350 class store_by_pieces_d : public op_by_pieces_d
1352 insn_gen_fn m_gen_fun;
1353 void generate (rtx, rtx, machine_mode);
1354 bool prepare_mode (machine_mode, unsigned int);
1356 public:
1357 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1358 unsigned HOST_WIDE_INT len, unsigned int align)
1359 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len,
1360 align, false)
1363 rtx finish_retmode (memop_ret);
1366 /* Return true if MODE can be used for a set of stores, given an
1367 alignment ALIGN. Prepare whatever data is necessary for later
1368 calls to generate. */
1370 bool
1371 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1373 insn_code icode = optab_handler (mov_optab, mode);
1374 m_gen_fun = GEN_FCN (icode);
1375 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1378 /* A callback used when iterating for a store_by_pieces_operation.
1379 OP0 and OP1 are the values that have been loaded and should be
1380 compared in MODE. If OP0 is NULL, this means we should generate a
1381 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1382 gen function that should be used to generate the mode. */
1384 void
1385 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1387 emit_insn (m_gen_fun (op0, op1));
1390 /* Perform the final adjustment at the end of a string to obtain the
1391 correct return value for the block operation.
1392 Return value is based on RETMODE argument. */
1395 store_by_pieces_d::finish_retmode (memop_ret retmode)
1397 gcc_assert (!m_reverse);
1398 if (retmode == RETURN_END_MINUS_ONE)
1400 m_to.maybe_postinc (-1);
1401 --m_offset;
1403 return m_to.adjust (QImode, m_offset);
1406 /* Determine whether the LEN bytes generated by CONSTFUN can be
1407 stored to memory using several move instructions. CONSTFUNDATA is
1408 a pointer which will be passed as argument in every CONSTFUN call.
1409 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1410 a memset operation and false if it's a copy of a constant string.
1411 Return nonzero if a call to store_by_pieces should succeed. */
1414 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1415 by_pieces_constfn constfun,
1416 void *constfundata, unsigned int align, bool memsetp)
1418 unsigned HOST_WIDE_INT l;
1419 unsigned int max_size;
1420 HOST_WIDE_INT offset = 0;
1421 enum insn_code icode;
1422 int reverse;
1423 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1424 rtx cst ATTRIBUTE_UNUSED;
1426 if (len == 0)
1427 return 1;
1429 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1430 memsetp
1431 ? SET_BY_PIECES
1432 : STORE_BY_PIECES,
1433 optimize_insn_for_speed_p ()))
1434 return 0;
1436 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1438 /* We would first store what we can in the largest integer mode, then go to
1439 successively smaller modes. */
1441 for (reverse = 0;
1442 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1443 reverse++)
1445 l = len;
1446 max_size = STORE_MAX_PIECES + 1;
1447 while (max_size > 1 && l > 0)
1449 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1451 icode = optab_handler (mov_optab, mode);
1452 if (icode != CODE_FOR_nothing
1453 && align >= GET_MODE_ALIGNMENT (mode))
1455 unsigned int size = GET_MODE_SIZE (mode);
1457 while (l >= size)
1459 if (reverse)
1460 offset -= size;
1462 cst = (*constfun) (constfundata, nullptr, offset, mode);
1463 if (!targetm.legitimate_constant_p (mode, cst))
1464 return 0;
1466 if (!reverse)
1467 offset += size;
1469 l -= size;
1473 max_size = GET_MODE_SIZE (mode);
1476 /* The code above should have handled everything. */
1477 gcc_assert (!l);
1480 return 1;
1483 /* Generate several move instructions to store LEN bytes generated by
1484 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1485 pointer which will be passed as argument in every CONSTFUN call.
1486 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1487 a memset operation and false if it's a copy of a constant string.
1488 Return value is based on RETMODE argument. */
1491 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1492 by_pieces_constfn constfun,
1493 void *constfundata, unsigned int align, bool memsetp,
1494 memop_ret retmode)
1496 if (len == 0)
1498 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1499 return to;
1502 gcc_assert (targetm.use_by_pieces_infrastructure_p
1503 (len, align,
1504 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1505 optimize_insn_for_speed_p ()));
1507 store_by_pieces_d data (to, constfun, constfundata, len, align);
1508 data.run ();
1510 if (retmode != RETURN_BEGIN)
1511 return data.finish_retmode (retmode);
1512 else
1513 return to;
1516 /* Callback routine for clear_by_pieces.
1517 Return const0_rtx unconditionally. */
1519 static rtx
1520 clear_by_pieces_1 (void *, void *, HOST_WIDE_INT, scalar_int_mode)
1522 return const0_rtx;
1525 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1526 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1528 static void
1529 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1531 if (len == 0)
1532 return;
1534 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1535 data.run ();
1538 /* Context used by compare_by_pieces_genfn. It stores the fail label
1539 to jump to in case of miscomparison, and for branch ratios greater than 1,
1540 it stores an accumulator and the current and maximum counts before
1541 emitting another branch. */
1543 class compare_by_pieces_d : public op_by_pieces_d
1545 rtx_code_label *m_fail_label;
1546 rtx m_accumulator;
1547 int m_count, m_batch;
1549 void generate (rtx, rtx, machine_mode);
1550 bool prepare_mode (machine_mode, unsigned int);
1551 void finish_mode (machine_mode);
1552 public:
1553 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1554 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1555 rtx_code_label *fail_label)
1556 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len,
1557 align, false)
1559 m_fail_label = fail_label;
1563 /* A callback used when iterating for a compare_by_pieces_operation.
1564 OP0 and OP1 are the values that have been loaded and should be
1565 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1566 context structure. */
1568 void
1569 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1571 if (m_batch > 1)
1573 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1574 true, OPTAB_LIB_WIDEN);
1575 if (m_count != 0)
1576 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1577 true, OPTAB_LIB_WIDEN);
1578 m_accumulator = temp;
1580 if (++m_count < m_batch)
1581 return;
1583 m_count = 0;
1584 op0 = m_accumulator;
1585 op1 = const0_rtx;
1586 m_accumulator = NULL_RTX;
1588 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1589 m_fail_label, profile_probability::uninitialized ());
1592 /* Return true if MODE can be used for a set of moves and comparisons,
1593 given an alignment ALIGN. Prepare whatever data is necessary for
1594 later calls to generate. */
1596 bool
1597 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1599 insn_code icode = optab_handler (mov_optab, mode);
1600 if (icode == CODE_FOR_nothing
1601 || align < GET_MODE_ALIGNMENT (mode)
1602 || !can_compare_p (EQ, mode, ccp_jump))
1603 return false;
1604 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1605 if (m_batch < 0)
1606 return false;
1607 m_accumulator = NULL_RTX;
1608 m_count = 0;
1609 return true;
1612 /* Called after expanding a series of comparisons in MODE. If we have
1613 accumulated results for which we haven't emitted a branch yet, do
1614 so now. */
1616 void
1617 compare_by_pieces_d::finish_mode (machine_mode mode)
1619 if (m_accumulator != NULL_RTX)
1620 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1621 NULL_RTX, NULL, m_fail_label,
1622 profile_probability::uninitialized ());
1625 /* Generate several move instructions to compare LEN bytes from blocks
1626 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1628 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1629 used to push FROM to the stack.
1631 ALIGN is maximum stack alignment we can assume.
1633 Optionally, the caller can pass a constfn and associated data in A1_CFN
1634 and A1_CFN_DATA. describing that the second operand being compared is a
1635 known constant and how to obtain its data. */
1637 static rtx
1638 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1639 rtx target, unsigned int align,
1640 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1642 rtx_code_label *fail_label = gen_label_rtx ();
1643 rtx_code_label *end_label = gen_label_rtx ();
1645 if (target == NULL_RTX
1646 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1647 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1649 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1650 fail_label);
1652 data.run ();
1654 emit_move_insn (target, const0_rtx);
1655 emit_jump (end_label);
1656 emit_barrier ();
1657 emit_label (fail_label);
1658 emit_move_insn (target, const1_rtx);
1659 emit_label (end_label);
1661 return target;
1664 /* Emit code to move a block Y to a block X. This may be done with
1665 string-move instructions, with multiple scalar move instructions,
1666 or with a library call.
1668 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1669 SIZE is an rtx that says how long they are.
1670 ALIGN is the maximum alignment we can assume they have.
1671 METHOD describes what kind of copy this is, and what mechanisms may be used.
1672 MIN_SIZE is the minimal size of block to move
1673 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1674 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1676 Return the address of the new block, if memcpy is called and returns it,
1677 0 otherwise. */
1680 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1681 unsigned int expected_align, HOST_WIDE_INT expected_size,
1682 unsigned HOST_WIDE_INT min_size,
1683 unsigned HOST_WIDE_INT max_size,
1684 unsigned HOST_WIDE_INT probable_max_size,
1685 bool bail_out_libcall, bool *is_move_done,
1686 bool might_overlap)
1688 int may_use_call;
1689 rtx retval = 0;
1690 unsigned int align;
1692 if (is_move_done)
1693 *is_move_done = true;
1695 gcc_assert (size);
1696 if (CONST_INT_P (size) && INTVAL (size) == 0)
1697 return 0;
1699 switch (method)
1701 case BLOCK_OP_NORMAL:
1702 case BLOCK_OP_TAILCALL:
1703 may_use_call = 1;
1704 break;
1706 case BLOCK_OP_CALL_PARM:
1707 may_use_call = block_move_libcall_safe_for_call_parm ();
1709 /* Make inhibit_defer_pop nonzero around the library call
1710 to force it to pop the arguments right away. */
1711 NO_DEFER_POP;
1712 break;
1714 case BLOCK_OP_NO_LIBCALL:
1715 may_use_call = 0;
1716 break;
1718 case BLOCK_OP_NO_LIBCALL_RET:
1719 may_use_call = -1;
1720 break;
1722 default:
1723 gcc_unreachable ();
1726 gcc_assert (MEM_P (x) && MEM_P (y));
1727 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1728 gcc_assert (align >= BITS_PER_UNIT);
1730 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1731 block copy is more efficient for other large modes, e.g. DCmode. */
1732 x = adjust_address (x, BLKmode, 0);
1733 y = adjust_address (y, BLKmode, 0);
1735 /* If source and destination are the same, no need to copy anything. */
1736 if (rtx_equal_p (x, y)
1737 && !MEM_VOLATILE_P (x)
1738 && !MEM_VOLATILE_P (y))
1739 return 0;
1741 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1742 can be incorrect is coming from __builtin_memcpy. */
1743 poly_int64 const_size;
1744 if (poly_int_rtx_p (size, &const_size))
1746 x = shallow_copy_rtx (x);
1747 y = shallow_copy_rtx (y);
1748 set_mem_size (x, const_size);
1749 set_mem_size (y, const_size);
1752 bool pieces_ok = CONST_INT_P (size)
1753 && can_move_by_pieces (INTVAL (size), align);
1754 bool pattern_ok = false;
1756 if (!pieces_ok || might_overlap)
1758 pattern_ok
1759 = emit_block_move_via_pattern (x, y, size, align,
1760 expected_align, expected_size,
1761 min_size, max_size, probable_max_size,
1762 might_overlap);
1763 if (!pattern_ok && might_overlap)
1765 /* Do not try any of the other methods below as they are not safe
1766 for overlapping moves. */
1767 *is_move_done = false;
1768 return retval;
1772 if (pattern_ok)
1774 else if (pieces_ok)
1775 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1776 else if (may_use_call && !might_overlap
1777 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1778 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1780 if (bail_out_libcall)
1782 if (is_move_done)
1783 *is_move_done = false;
1784 return retval;
1787 if (may_use_call < 0)
1788 return pc_rtx;
1790 retval = emit_block_copy_via_libcall (x, y, size,
1791 method == BLOCK_OP_TAILCALL);
1793 else if (might_overlap)
1794 *is_move_done = false;
1795 else
1796 emit_block_move_via_loop (x, y, size, align);
1798 if (method == BLOCK_OP_CALL_PARM)
1799 OK_DEFER_POP;
1801 return retval;
1805 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1807 unsigned HOST_WIDE_INT max, min = 0;
1808 if (GET_CODE (size) == CONST_INT)
1809 min = max = UINTVAL (size);
1810 else
1811 max = GET_MODE_MASK (GET_MODE (size));
1812 return emit_block_move_hints (x, y, size, method, 0, -1,
1813 min, max, max);
1816 /* A subroutine of emit_block_move. Returns true if calling the
1817 block move libcall will not clobber any parameters which may have
1818 already been placed on the stack. */
1820 static bool
1821 block_move_libcall_safe_for_call_parm (void)
1823 tree fn;
1825 /* If arguments are pushed on the stack, then they're safe. */
1826 if (PUSH_ARGS)
1827 return true;
1829 /* If registers go on the stack anyway, any argument is sure to clobber
1830 an outgoing argument. */
1831 #if defined (REG_PARM_STACK_SPACE)
1832 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1833 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1834 depend on its argument. */
1835 (void) fn;
1836 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1837 && REG_PARM_STACK_SPACE (fn) != 0)
1838 return false;
1839 #endif
1841 /* If any argument goes in memory, then it might clobber an outgoing
1842 argument. */
1844 CUMULATIVE_ARGS args_so_far_v;
1845 cumulative_args_t args_so_far;
1846 tree arg;
1848 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1849 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1850 args_so_far = pack_cumulative_args (&args_so_far_v);
1852 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1853 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1855 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1856 function_arg_info arg_info (mode, /*named=*/true);
1857 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1858 if (!tmp || !REG_P (tmp))
1859 return false;
1860 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1861 return false;
1862 targetm.calls.function_arg_advance (args_so_far, arg_info);
1865 return true;
1868 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1869 return true if successful.
1871 X is the destination of the copy or move.
1872 Y is the source of the copy or move.
1873 SIZE is the size of the block to be moved.
1875 MIGHT_OVERLAP indicates this originated with expansion of a
1876 builtin_memmove() and the source and destination blocks may
1877 overlap.
1880 static bool
1881 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
1882 unsigned int expected_align,
1883 HOST_WIDE_INT expected_size,
1884 unsigned HOST_WIDE_INT min_size,
1885 unsigned HOST_WIDE_INT max_size,
1886 unsigned HOST_WIDE_INT probable_max_size,
1887 bool might_overlap)
1889 if (expected_align < align)
1890 expected_align = align;
1891 if (expected_size != -1)
1893 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1894 expected_size = probable_max_size;
1895 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1896 expected_size = min_size;
1899 /* Since this is a move insn, we don't care about volatility. */
1900 temporary_volatile_ok v (true);
1902 /* Try the most limited insn first, because there's no point
1903 including more than one in the machine description unless
1904 the more limited one has some advantage. */
1906 opt_scalar_int_mode mode_iter;
1907 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1909 scalar_int_mode mode = mode_iter.require ();
1910 enum insn_code code;
1911 if (might_overlap)
1912 code = direct_optab_handler (movmem_optab, mode);
1913 else
1914 code = direct_optab_handler (cpymem_optab, mode);
1916 if (code != CODE_FOR_nothing
1917 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1918 here because if SIZE is less than the mode mask, as it is
1919 returned by the macro, it will definitely be less than the
1920 actual mode mask. Since SIZE is within the Pmode address
1921 space, we limit MODE to Pmode. */
1922 && ((CONST_INT_P (size)
1923 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1924 <= (GET_MODE_MASK (mode) >> 1)))
1925 || max_size <= (GET_MODE_MASK (mode) >> 1)
1926 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1928 class expand_operand ops[9];
1929 unsigned int nops;
1931 /* ??? When called via emit_block_move_for_call, it'd be
1932 nice if there were some way to inform the backend, so
1933 that it doesn't fail the expansion because it thinks
1934 emitting the libcall would be more efficient. */
1935 nops = insn_data[(int) code].n_generator_args;
1936 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1938 create_fixed_operand (&ops[0], x);
1939 create_fixed_operand (&ops[1], y);
1940 /* The check above guarantees that this size conversion is valid. */
1941 create_convert_operand_to (&ops[2], size, mode, true);
1942 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1943 if (nops >= 6)
1945 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1946 create_integer_operand (&ops[5], expected_size);
1948 if (nops >= 8)
1950 create_integer_operand (&ops[6], min_size);
1951 /* If we cannot represent the maximal size,
1952 make parameter NULL. */
1953 if ((HOST_WIDE_INT) max_size != -1)
1954 create_integer_operand (&ops[7], max_size);
1955 else
1956 create_fixed_operand (&ops[7], NULL);
1958 if (nops == 9)
1960 /* If we cannot represent the maximal size,
1961 make parameter NULL. */
1962 if ((HOST_WIDE_INT) probable_max_size != -1)
1963 create_integer_operand (&ops[8], probable_max_size);
1964 else
1965 create_fixed_operand (&ops[8], NULL);
1967 if (maybe_expand_insn (code, nops, ops))
1968 return true;
1972 return false;
1975 /* A subroutine of emit_block_move. Copy the data via an explicit
1976 loop. This is used only when libcalls are forbidden. */
1977 /* ??? It'd be nice to copy in hunks larger than QImode. */
1979 static void
1980 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1981 unsigned int align ATTRIBUTE_UNUSED)
1983 rtx_code_label *cmp_label, *top_label;
1984 rtx iter, x_addr, y_addr, tmp;
1985 machine_mode x_addr_mode = get_address_mode (x);
1986 machine_mode y_addr_mode = get_address_mode (y);
1987 machine_mode iter_mode;
1989 iter_mode = GET_MODE (size);
1990 if (iter_mode == VOIDmode)
1991 iter_mode = word_mode;
1993 top_label = gen_label_rtx ();
1994 cmp_label = gen_label_rtx ();
1995 iter = gen_reg_rtx (iter_mode);
1997 emit_move_insn (iter, const0_rtx);
1999 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2000 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2001 do_pending_stack_adjust ();
2003 emit_jump (cmp_label);
2004 emit_label (top_label);
2006 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2007 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2009 if (x_addr_mode != y_addr_mode)
2010 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2011 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2013 x = change_address (x, QImode, x_addr);
2014 y = change_address (y, QImode, y_addr);
2016 emit_move_insn (x, y);
2018 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2019 true, OPTAB_LIB_WIDEN);
2020 if (tmp != iter)
2021 emit_move_insn (iter, tmp);
2023 emit_label (cmp_label);
2025 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2026 true, top_label,
2027 profile_probability::guessed_always ()
2028 .apply_scale (9, 10));
2031 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2032 TAILCALL is true if this is a tail call. */
2035 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2036 rtx size, bool tailcall)
2038 rtx dst_addr, src_addr;
2039 tree call_expr, dst_tree, src_tree, size_tree;
2040 machine_mode size_mode;
2042 /* Since dst and src are passed to a libcall, mark the corresponding
2043 tree EXPR as addressable. */
2044 tree dst_expr = MEM_EXPR (dst);
2045 tree src_expr = MEM_EXPR (src);
2046 if (dst_expr)
2047 mark_addressable (dst_expr);
2048 if (src_expr)
2049 mark_addressable (src_expr);
2051 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2052 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2053 dst_tree = make_tree (ptr_type_node, dst_addr);
2055 src_addr = copy_addr_to_reg (XEXP (src, 0));
2056 src_addr = convert_memory_address (ptr_mode, src_addr);
2057 src_tree = make_tree (ptr_type_node, src_addr);
2059 size_mode = TYPE_MODE (sizetype);
2060 size = convert_to_mode (size_mode, size, 1);
2061 size = copy_to_mode_reg (size_mode, size);
2062 size_tree = make_tree (sizetype, size);
2064 /* It is incorrect to use the libcall calling conventions for calls to
2065 memcpy/memmove/memcmp because they can be provided by the user. */
2066 tree fn = builtin_decl_implicit (fncode);
2067 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2068 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2070 return expand_call (call_expr, NULL_RTX, false);
2073 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2074 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2075 otherwise return null. */
2078 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2079 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2080 HOST_WIDE_INT align)
2082 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2084 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2085 target = NULL_RTX;
2087 class expand_operand ops[5];
2088 create_output_operand (&ops[0], target, insn_mode);
2089 create_fixed_operand (&ops[1], arg1_rtx);
2090 create_fixed_operand (&ops[2], arg2_rtx);
2091 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2092 TYPE_UNSIGNED (arg3_type));
2093 create_integer_operand (&ops[4], align);
2094 if (maybe_expand_insn (icode, 5, ops))
2095 return ops[0].value;
2096 return NULL_RTX;
2099 /* Expand a block compare between X and Y with length LEN using the
2100 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2101 of the expression that was used to calculate the length. ALIGN
2102 gives the known minimum common alignment. */
2104 static rtx
2105 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2106 unsigned align)
2108 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2109 implementing memcmp because it will stop if it encounters two
2110 zero bytes. */
2111 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2113 if (icode == CODE_FOR_nothing)
2114 return NULL_RTX;
2116 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2119 /* Emit code to compare a block Y to a block X. This may be done with
2120 string-compare instructions, with multiple scalar instructions,
2121 or with a library call.
2123 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2124 they are. LEN_TYPE is the type of the expression that was used to
2125 calculate it.
2127 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2128 value of a normal memcmp call, instead we can just compare for equality.
2129 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2130 returning NULL_RTX.
2132 Optionally, the caller can pass a constfn and associated data in Y_CFN
2133 and Y_CFN_DATA. describing that the second operand being compared is a
2134 known constant and how to obtain its data.
2135 Return the result of the comparison, or NULL_RTX if we failed to
2136 perform the operation. */
2139 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2140 bool equality_only, by_pieces_constfn y_cfn,
2141 void *y_cfndata)
2143 rtx result = 0;
2145 if (CONST_INT_P (len) && INTVAL (len) == 0)
2146 return const0_rtx;
2148 gcc_assert (MEM_P (x) && MEM_P (y));
2149 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2150 gcc_assert (align >= BITS_PER_UNIT);
2152 x = adjust_address (x, BLKmode, 0);
2153 y = adjust_address (y, BLKmode, 0);
2155 if (equality_only
2156 && CONST_INT_P (len)
2157 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2158 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2159 y_cfn, y_cfndata);
2160 else
2161 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2163 return result;
2166 /* Copy all or part of a value X into registers starting at REGNO.
2167 The number of registers to be filled is NREGS. */
2169 void
2170 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2172 if (nregs == 0)
2173 return;
2175 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2176 x = validize_mem (force_const_mem (mode, x));
2178 /* See if the machine can do this with a load multiple insn. */
2179 if (targetm.have_load_multiple ())
2181 rtx_insn *last = get_last_insn ();
2182 rtx first = gen_rtx_REG (word_mode, regno);
2183 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2184 GEN_INT (nregs)))
2186 emit_insn (pat);
2187 return;
2189 else
2190 delete_insns_since (last);
2193 for (int i = 0; i < nregs; i++)
2194 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2195 operand_subword_force (x, i, mode));
2198 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2199 The number of registers to be filled is NREGS. */
2201 void
2202 move_block_from_reg (int regno, rtx x, int nregs)
2204 if (nregs == 0)
2205 return;
2207 /* See if the machine can do this with a store multiple insn. */
2208 if (targetm.have_store_multiple ())
2210 rtx_insn *last = get_last_insn ();
2211 rtx first = gen_rtx_REG (word_mode, regno);
2212 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2213 GEN_INT (nregs)))
2215 emit_insn (pat);
2216 return;
2218 else
2219 delete_insns_since (last);
2222 for (int i = 0; i < nregs; i++)
2224 rtx tem = operand_subword (x, i, 1, BLKmode);
2226 gcc_assert (tem);
2228 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2232 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2233 ORIG, where ORIG is a non-consecutive group of registers represented by
2234 a PARALLEL. The clone is identical to the original except in that the
2235 original set of registers is replaced by a new set of pseudo registers.
2236 The new set has the same modes as the original set. */
2239 gen_group_rtx (rtx orig)
2241 int i, length;
2242 rtx *tmps;
2244 gcc_assert (GET_CODE (orig) == PARALLEL);
2246 length = XVECLEN (orig, 0);
2247 tmps = XALLOCAVEC (rtx, length);
2249 /* Skip a NULL entry in first slot. */
2250 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2252 if (i)
2253 tmps[0] = 0;
2255 for (; i < length; i++)
2257 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2258 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2260 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2263 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2266 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2267 except that values are placed in TMPS[i], and must later be moved
2268 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2270 static void
2271 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2272 poly_int64 ssize)
2274 rtx src;
2275 int start, i;
2276 machine_mode m = GET_MODE (orig_src);
2278 gcc_assert (GET_CODE (dst) == PARALLEL);
2280 if (m != VOIDmode
2281 && !SCALAR_INT_MODE_P (m)
2282 && !MEM_P (orig_src)
2283 && GET_CODE (orig_src) != CONCAT)
2285 scalar_int_mode imode;
2286 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2288 src = gen_reg_rtx (imode);
2289 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2291 else
2293 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2294 emit_move_insn (src, orig_src);
2296 emit_group_load_1 (tmps, dst, src, type, ssize);
2297 return;
2300 /* Check for a NULL entry, used to indicate that the parameter goes
2301 both on the stack and in registers. */
2302 if (XEXP (XVECEXP (dst, 0, 0), 0))
2303 start = 0;
2304 else
2305 start = 1;
2307 /* Process the pieces. */
2308 for (i = start; i < XVECLEN (dst, 0); i++)
2310 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2311 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2312 poly_int64 bytelen = GET_MODE_SIZE (mode);
2313 poly_int64 shift = 0;
2315 /* Handle trailing fragments that run over the size of the struct.
2316 It's the target's responsibility to make sure that the fragment
2317 cannot be strictly smaller in some cases and strictly larger
2318 in others. */
2319 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2320 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2322 /* Arrange to shift the fragment to where it belongs.
2323 extract_bit_field loads to the lsb of the reg. */
2324 if (
2325 #ifdef BLOCK_REG_PADDING
2326 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2327 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2328 #else
2329 BYTES_BIG_ENDIAN
2330 #endif
2332 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2333 bytelen = ssize - bytepos;
2334 gcc_assert (maybe_gt (bytelen, 0));
2337 /* If we won't be loading directly from memory, protect the real source
2338 from strange tricks we might play; but make sure that the source can
2339 be loaded directly into the destination. */
2340 src = orig_src;
2341 if (!MEM_P (orig_src)
2342 && (!CONSTANT_P (orig_src)
2343 || (GET_MODE (orig_src) != mode
2344 && GET_MODE (orig_src) != VOIDmode)))
2346 if (GET_MODE (orig_src) == VOIDmode)
2347 src = gen_reg_rtx (mode);
2348 else
2349 src = gen_reg_rtx (GET_MODE (orig_src));
2351 emit_move_insn (src, orig_src);
2354 /* Optimize the access just a bit. */
2355 if (MEM_P (src)
2356 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2357 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2358 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2359 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2361 tmps[i] = gen_reg_rtx (mode);
2362 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2364 else if (COMPLEX_MODE_P (mode)
2365 && GET_MODE (src) == mode
2366 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2367 /* Let emit_move_complex do the bulk of the work. */
2368 tmps[i] = src;
2369 else if (GET_CODE (src) == CONCAT)
2371 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2372 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2373 unsigned int elt;
2374 poly_int64 subpos;
2376 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2377 && known_le (subpos + bytelen, slen0))
2379 /* The following assumes that the concatenated objects all
2380 have the same size. In this case, a simple calculation
2381 can be used to determine the object and the bit field
2382 to be extracted. */
2383 tmps[i] = XEXP (src, elt);
2384 if (maybe_ne (subpos, 0)
2385 || maybe_ne (subpos + bytelen, slen0)
2386 || (!CONSTANT_P (tmps[i])
2387 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2388 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2389 subpos * BITS_PER_UNIT,
2390 1, NULL_RTX, mode, mode, false,
2391 NULL);
2393 else
2395 rtx mem;
2397 gcc_assert (known_eq (bytepos, 0));
2398 mem = assign_stack_temp (GET_MODE (src), slen);
2399 emit_move_insn (mem, src);
2400 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2401 0, 1, NULL_RTX, mode, mode, false,
2402 NULL);
2405 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2406 SIMD register, which is currently broken. While we get GCC
2407 to emit proper RTL for these cases, let's dump to memory. */
2408 else if (VECTOR_MODE_P (GET_MODE (dst))
2409 && REG_P (src))
2411 poly_uint64 slen = GET_MODE_SIZE (GET_MODE (src));
2412 rtx mem;
2414 mem = assign_stack_temp (GET_MODE (src), slen);
2415 emit_move_insn (mem, src);
2416 tmps[i] = adjust_address (mem, mode, bytepos);
2418 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2419 && XVECLEN (dst, 0) > 1)
2420 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2421 else if (CONSTANT_P (src))
2423 if (known_eq (bytelen, ssize))
2424 tmps[i] = src;
2425 else
2427 rtx first, second;
2429 /* TODO: const_wide_int can have sizes other than this... */
2430 gcc_assert (known_eq (2 * bytelen, ssize));
2431 split_double (src, &first, &second);
2432 if (i)
2433 tmps[i] = second;
2434 else
2435 tmps[i] = first;
2438 else if (REG_P (src) && GET_MODE (src) == mode)
2439 tmps[i] = src;
2440 else
2441 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2442 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2443 mode, mode, false, NULL);
2445 if (maybe_ne (shift, 0))
2446 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2447 shift, tmps[i], 0);
2451 /* Emit code to move a block SRC of type TYPE to a block DST,
2452 where DST is non-consecutive registers represented by a PARALLEL.
2453 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2454 if not known. */
2456 void
2457 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2459 rtx *tmps;
2460 int i;
2462 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2463 emit_group_load_1 (tmps, dst, src, type, ssize);
2465 /* Copy the extracted pieces into the proper (probable) hard regs. */
2466 for (i = 0; i < XVECLEN (dst, 0); i++)
2468 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2469 if (d == NULL)
2470 continue;
2471 emit_move_insn (d, tmps[i]);
2475 /* Similar, but load SRC into new pseudos in a format that looks like
2476 PARALLEL. This can later be fed to emit_group_move to get things
2477 in the right place. */
2480 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2482 rtvec vec;
2483 int i;
2485 vec = rtvec_alloc (XVECLEN (parallel, 0));
2486 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2488 /* Convert the vector to look just like the original PARALLEL, except
2489 with the computed values. */
2490 for (i = 0; i < XVECLEN (parallel, 0); i++)
2492 rtx e = XVECEXP (parallel, 0, i);
2493 rtx d = XEXP (e, 0);
2495 if (d)
2497 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2498 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2500 RTVEC_ELT (vec, i) = e;
2503 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2506 /* Emit code to move a block SRC to block DST, where SRC and DST are
2507 non-consecutive groups of registers, each represented by a PARALLEL. */
2509 void
2510 emit_group_move (rtx dst, rtx src)
2512 int i;
2514 gcc_assert (GET_CODE (src) == PARALLEL
2515 && GET_CODE (dst) == PARALLEL
2516 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2518 /* Skip first entry if NULL. */
2519 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2520 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2521 XEXP (XVECEXP (src, 0, i), 0));
2524 /* Move a group of registers represented by a PARALLEL into pseudos. */
2527 emit_group_move_into_temps (rtx src)
2529 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2530 int i;
2532 for (i = 0; i < XVECLEN (src, 0); i++)
2534 rtx e = XVECEXP (src, 0, i);
2535 rtx d = XEXP (e, 0);
2537 if (d)
2538 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2539 RTVEC_ELT (vec, i) = e;
2542 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2545 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2546 where SRC is non-consecutive registers represented by a PARALLEL.
2547 SSIZE represents the total size of block ORIG_DST, or -1 if not
2548 known. */
2550 void
2551 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2552 poly_int64 ssize)
2554 rtx *tmps, dst;
2555 int start, finish, i;
2556 machine_mode m = GET_MODE (orig_dst);
2558 gcc_assert (GET_CODE (src) == PARALLEL);
2560 if (!SCALAR_INT_MODE_P (m)
2561 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2563 scalar_int_mode imode;
2564 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2566 dst = gen_reg_rtx (imode);
2567 emit_group_store (dst, src, type, ssize);
2568 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2570 else
2572 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2573 emit_group_store (dst, src, type, ssize);
2575 emit_move_insn (orig_dst, dst);
2576 return;
2579 /* Check for a NULL entry, used to indicate that the parameter goes
2580 both on the stack and in registers. */
2581 if (XEXP (XVECEXP (src, 0, 0), 0))
2582 start = 0;
2583 else
2584 start = 1;
2585 finish = XVECLEN (src, 0);
2587 tmps = XALLOCAVEC (rtx, finish);
2589 /* Copy the (probable) hard regs into pseudos. */
2590 for (i = start; i < finish; i++)
2592 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2593 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2595 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2596 emit_move_insn (tmps[i], reg);
2598 else
2599 tmps[i] = reg;
2602 /* If we won't be storing directly into memory, protect the real destination
2603 from strange tricks we might play. */
2604 dst = orig_dst;
2605 if (GET_CODE (dst) == PARALLEL)
2607 rtx temp;
2609 /* We can get a PARALLEL dst if there is a conditional expression in
2610 a return statement. In that case, the dst and src are the same,
2611 so no action is necessary. */
2612 if (rtx_equal_p (dst, src))
2613 return;
2615 /* It is unclear if we can ever reach here, but we may as well handle
2616 it. Allocate a temporary, and split this into a store/load to/from
2617 the temporary. */
2618 temp = assign_stack_temp (GET_MODE (dst), ssize);
2619 emit_group_store (temp, src, type, ssize);
2620 emit_group_load (dst, temp, type, ssize);
2621 return;
2623 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2625 machine_mode outer = GET_MODE (dst);
2626 machine_mode inner;
2627 poly_int64 bytepos;
2628 bool done = false;
2629 rtx temp;
2631 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2632 dst = gen_reg_rtx (outer);
2634 /* Make life a bit easier for combine. */
2635 /* If the first element of the vector is the low part
2636 of the destination mode, use a paradoxical subreg to
2637 initialize the destination. */
2638 if (start < finish)
2640 inner = GET_MODE (tmps[start]);
2641 bytepos = subreg_lowpart_offset (inner, outer);
2642 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2643 bytepos))
2645 temp = simplify_gen_subreg (outer, tmps[start],
2646 inner, 0);
2647 if (temp)
2649 emit_move_insn (dst, temp);
2650 done = true;
2651 start++;
2656 /* If the first element wasn't the low part, try the last. */
2657 if (!done
2658 && start < finish - 1)
2660 inner = GET_MODE (tmps[finish - 1]);
2661 bytepos = subreg_lowpart_offset (inner, outer);
2662 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2663 finish - 1), 1)),
2664 bytepos))
2666 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2667 inner, 0);
2668 if (temp)
2670 emit_move_insn (dst, temp);
2671 done = true;
2672 finish--;
2677 /* Otherwise, simply initialize the result to zero. */
2678 if (!done)
2679 emit_move_insn (dst, CONST0_RTX (outer));
2682 /* Process the pieces. */
2683 for (i = start; i < finish; i++)
2685 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2686 machine_mode mode = GET_MODE (tmps[i]);
2687 poly_int64 bytelen = GET_MODE_SIZE (mode);
2688 poly_uint64 adj_bytelen;
2689 rtx dest = dst;
2691 /* Handle trailing fragments that run over the size of the struct.
2692 It's the target's responsibility to make sure that the fragment
2693 cannot be strictly smaller in some cases and strictly larger
2694 in others. */
2695 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2696 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2697 adj_bytelen = ssize - bytepos;
2698 else
2699 adj_bytelen = bytelen;
2701 if (GET_CODE (dst) == CONCAT)
2703 if (known_le (bytepos + adj_bytelen,
2704 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2705 dest = XEXP (dst, 0);
2706 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2708 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2709 dest = XEXP (dst, 1);
2711 else
2713 machine_mode dest_mode = GET_MODE (dest);
2714 machine_mode tmp_mode = GET_MODE (tmps[i]);
2716 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2718 if (GET_MODE_ALIGNMENT (dest_mode)
2719 >= GET_MODE_ALIGNMENT (tmp_mode))
2721 dest = assign_stack_temp (dest_mode,
2722 GET_MODE_SIZE (dest_mode));
2723 emit_move_insn (adjust_address (dest,
2724 tmp_mode,
2725 bytepos),
2726 tmps[i]);
2727 dst = dest;
2729 else
2731 dest = assign_stack_temp (tmp_mode,
2732 GET_MODE_SIZE (tmp_mode));
2733 emit_move_insn (dest, tmps[i]);
2734 dst = adjust_address (dest, dest_mode, bytepos);
2736 break;
2740 /* Handle trailing fragments that run over the size of the struct. */
2741 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2743 /* store_bit_field always takes its value from the lsb.
2744 Move the fragment to the lsb if it's not already there. */
2745 if (
2746 #ifdef BLOCK_REG_PADDING
2747 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2748 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2749 #else
2750 BYTES_BIG_ENDIAN
2751 #endif
2754 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2755 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2756 shift, tmps[i], 0);
2759 /* Make sure not to write past the end of the struct. */
2760 store_bit_field (dest,
2761 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2762 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2763 VOIDmode, tmps[i], false);
2766 /* Optimize the access just a bit. */
2767 else if (MEM_P (dest)
2768 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2769 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2770 && multiple_p (bytepos * BITS_PER_UNIT,
2771 GET_MODE_ALIGNMENT (mode))
2772 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2773 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2775 else
2776 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2777 0, 0, mode, tmps[i], false);
2780 /* Copy from the pseudo into the (probable) hard reg. */
2781 if (orig_dst != dst)
2782 emit_move_insn (orig_dst, dst);
2785 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2786 of the value stored in X. */
2789 maybe_emit_group_store (rtx x, tree type)
2791 machine_mode mode = TYPE_MODE (type);
2792 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2793 if (GET_CODE (x) == PARALLEL)
2795 rtx result = gen_reg_rtx (mode);
2796 emit_group_store (result, x, type, int_size_in_bytes (type));
2797 return result;
2799 return x;
2802 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2804 This is used on targets that return BLKmode values in registers. */
2806 static void
2807 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2809 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2810 rtx src = NULL, dst = NULL;
2811 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2812 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2813 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2814 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2815 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2816 fixed_size_mode copy_mode;
2818 /* BLKmode registers created in the back-end shouldn't have survived. */
2819 gcc_assert (mode != BLKmode);
2821 /* If the structure doesn't take up a whole number of words, see whether
2822 SRCREG is padded on the left or on the right. If it's on the left,
2823 set PADDING_CORRECTION to the number of bits to skip.
2825 In most ABIs, the structure will be returned at the least end of
2826 the register, which translates to right padding on little-endian
2827 targets and left padding on big-endian targets. The opposite
2828 holds if the structure is returned at the most significant
2829 end of the register. */
2830 if (bytes % UNITS_PER_WORD != 0
2831 && (targetm.calls.return_in_msb (type)
2832 ? !BYTES_BIG_ENDIAN
2833 : BYTES_BIG_ENDIAN))
2834 padding_correction
2835 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2837 /* We can use a single move if we have an exact mode for the size. */
2838 else if (MEM_P (target)
2839 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2840 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2841 && bytes == GET_MODE_SIZE (mode))
2843 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2844 return;
2847 /* And if we additionally have the same mode for a register. */
2848 else if (REG_P (target)
2849 && GET_MODE (target) == mode
2850 && bytes == GET_MODE_SIZE (mode))
2852 emit_move_insn (target, srcreg);
2853 return;
2856 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2857 into a new pseudo which is a full word. */
2858 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2860 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2861 mode = word_mode;
2864 /* Copy the structure BITSIZE bits at a time. If the target lives in
2865 memory, take care of not reading/writing past its end by selecting
2866 a copy mode suited to BITSIZE. This should always be possible given
2867 how it is computed.
2869 If the target lives in register, make sure not to select a copy mode
2870 larger than the mode of the register.
2872 We could probably emit more efficient code for machines which do not use
2873 strict alignment, but it doesn't seem worth the effort at the current
2874 time. */
2876 copy_mode = word_mode;
2877 if (MEM_P (target))
2879 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2880 if (mem_mode.exists ())
2881 copy_mode = mem_mode.require ();
2883 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2884 copy_mode = tmode;
2886 for (bitpos = 0, xbitpos = padding_correction;
2887 bitpos < bytes * BITS_PER_UNIT;
2888 bitpos += bitsize, xbitpos += bitsize)
2890 /* We need a new source operand each time xbitpos is on a
2891 word boundary and when xbitpos == padding_correction
2892 (the first time through). */
2893 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2894 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2896 /* We need a new destination operand each time bitpos is on
2897 a word boundary. */
2898 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2899 dst = target;
2900 else if (bitpos % BITS_PER_WORD == 0)
2901 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2903 /* Use xbitpos for the source extraction (right justified) and
2904 bitpos for the destination store (left justified). */
2905 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2906 extract_bit_field (src, bitsize,
2907 xbitpos % BITS_PER_WORD, 1,
2908 NULL_RTX, copy_mode, copy_mode,
2909 false, NULL),
2910 false);
2914 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2915 register if it contains any data, otherwise return null.
2917 This is used on targets that return BLKmode values in registers. */
2920 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2922 int i, n_regs;
2923 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2924 unsigned int bitsize;
2925 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2926 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2927 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2928 fixed_size_mode dst_mode;
2929 scalar_int_mode min_mode;
2931 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2933 x = expand_normal (src);
2935 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2936 if (bytes == 0)
2937 return NULL_RTX;
2939 /* If the structure doesn't take up a whole number of words, see
2940 whether the register value should be padded on the left or on
2941 the right. Set PADDING_CORRECTION to the number of padding
2942 bits needed on the left side.
2944 In most ABIs, the structure will be returned at the least end of
2945 the register, which translates to right padding on little-endian
2946 targets and left padding on big-endian targets. The opposite
2947 holds if the structure is returned at the most significant
2948 end of the register. */
2949 if (bytes % UNITS_PER_WORD != 0
2950 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2951 ? !BYTES_BIG_ENDIAN
2952 : BYTES_BIG_ENDIAN))
2953 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2954 * BITS_PER_UNIT));
2956 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2957 dst_words = XALLOCAVEC (rtx, n_regs);
2958 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2959 min_mode = smallest_int_mode_for_size (bitsize);
2961 /* Copy the structure BITSIZE bits at a time. */
2962 for (bitpos = 0, xbitpos = padding_correction;
2963 bitpos < bytes * BITS_PER_UNIT;
2964 bitpos += bitsize, xbitpos += bitsize)
2966 /* We need a new destination pseudo each time xbitpos is
2967 on a word boundary and when xbitpos == padding_correction
2968 (the first time through). */
2969 if (xbitpos % BITS_PER_WORD == 0
2970 || xbitpos == padding_correction)
2972 /* Generate an appropriate register. */
2973 dst_word = gen_reg_rtx (word_mode);
2974 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2976 /* Clear the destination before we move anything into it. */
2977 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2980 /* Find the largest integer mode that can be used to copy all or as
2981 many bits as possible of the structure if the target supports larger
2982 copies. There are too many corner cases here w.r.t to alignments on
2983 the read/writes. So if there is any padding just use single byte
2984 operations. */
2985 opt_scalar_int_mode mode_iter;
2986 if (padding_correction == 0 && !STRICT_ALIGNMENT)
2988 FOR_EACH_MODE_FROM (mode_iter, min_mode)
2990 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
2991 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
2992 && msize <= BITS_PER_WORD)
2993 bitsize = msize;
2994 else
2995 break;
2999 /* We need a new source operand each time bitpos is on a word
3000 boundary. */
3001 if (bitpos % BITS_PER_WORD == 0)
3002 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3004 /* Use bitpos for the source extraction (left justified) and
3005 xbitpos for the destination store (right justified). */
3006 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3007 0, 0, word_mode,
3008 extract_bit_field (src_word, bitsize,
3009 bitpos % BITS_PER_WORD, 1,
3010 NULL_RTX, word_mode, word_mode,
3011 false, NULL),
3012 false);
3015 if (mode == BLKmode)
3017 /* Find the smallest integer mode large enough to hold the
3018 entire structure. */
3019 opt_scalar_int_mode mode_iter;
3020 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3021 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3022 break;
3024 /* A suitable mode should have been found. */
3025 mode = mode_iter.require ();
3028 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3029 dst_mode = word_mode;
3030 else
3031 dst_mode = mode;
3032 dst = gen_reg_rtx (dst_mode);
3034 for (i = 0; i < n_regs; i++)
3035 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3037 if (mode != dst_mode)
3038 dst = gen_lowpart (mode, dst);
3040 return dst;
3043 /* Add a USE expression for REG to the (possibly empty) list pointed
3044 to by CALL_FUSAGE. REG must denote a hard register. */
3046 void
3047 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3049 gcc_assert (REG_P (reg));
3051 if (!HARD_REGISTER_P (reg))
3052 return;
3054 *call_fusage
3055 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3058 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3059 to by CALL_FUSAGE. REG must denote a hard register. */
3061 void
3062 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3064 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3066 *call_fusage
3067 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3070 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3071 starting at REGNO. All of these registers must be hard registers. */
3073 void
3074 use_regs (rtx *call_fusage, int regno, int nregs)
3076 int i;
3078 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3080 for (i = 0; i < nregs; i++)
3081 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3084 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3085 PARALLEL REGS. This is for calls that pass values in multiple
3086 non-contiguous locations. The Irix 6 ABI has examples of this. */
3088 void
3089 use_group_regs (rtx *call_fusage, rtx regs)
3091 int i;
3093 for (i = 0; i < XVECLEN (regs, 0); i++)
3095 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3097 /* A NULL entry means the parameter goes both on the stack and in
3098 registers. This can also be a MEM for targets that pass values
3099 partially on the stack and partially in registers. */
3100 if (reg != 0 && REG_P (reg))
3101 use_reg (call_fusage, reg);
3105 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3106 assigment and the code of the expresion on the RHS is CODE. Return
3107 NULL otherwise. */
3109 static gimple *
3110 get_def_for_expr (tree name, enum tree_code code)
3112 gimple *def_stmt;
3114 if (TREE_CODE (name) != SSA_NAME)
3115 return NULL;
3117 def_stmt = get_gimple_for_ssa_name (name);
3118 if (!def_stmt
3119 || gimple_assign_rhs_code (def_stmt) != code)
3120 return NULL;
3122 return def_stmt;
3125 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3126 assigment and the class of the expresion on the RHS is CLASS. Return
3127 NULL otherwise. */
3129 static gimple *
3130 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3132 gimple *def_stmt;
3134 if (TREE_CODE (name) != SSA_NAME)
3135 return NULL;
3137 def_stmt = get_gimple_for_ssa_name (name);
3138 if (!def_stmt
3139 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3140 return NULL;
3142 return def_stmt;
3145 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3146 its length in bytes. */
3149 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3150 unsigned int expected_align, HOST_WIDE_INT expected_size,
3151 unsigned HOST_WIDE_INT min_size,
3152 unsigned HOST_WIDE_INT max_size,
3153 unsigned HOST_WIDE_INT probable_max_size,
3154 unsigned ctz_size)
3156 machine_mode mode = GET_MODE (object);
3157 unsigned int align;
3159 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3161 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3162 just move a zero. Otherwise, do this a piece at a time. */
3163 poly_int64 size_val;
3164 if (mode != BLKmode
3165 && poly_int_rtx_p (size, &size_val)
3166 && known_eq (size_val, GET_MODE_SIZE (mode)))
3168 rtx zero = CONST0_RTX (mode);
3169 if (zero != NULL)
3171 emit_move_insn (object, zero);
3172 return NULL;
3175 if (COMPLEX_MODE_P (mode))
3177 zero = CONST0_RTX (GET_MODE_INNER (mode));
3178 if (zero != NULL)
3180 write_complex_part (object, zero, 0);
3181 write_complex_part (object, zero, 1);
3182 return NULL;
3187 if (size == const0_rtx)
3188 return NULL;
3190 align = MEM_ALIGN (object);
3192 if (CONST_INT_P (size)
3193 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3194 CLEAR_BY_PIECES,
3195 optimize_insn_for_speed_p ()))
3196 clear_by_pieces (object, INTVAL (size), align);
3197 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3198 expected_align, expected_size,
3199 min_size, max_size, probable_max_size))
3201 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3202 min_size, max_size,
3203 NULL_RTX, 0, align))
3205 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3206 return set_storage_via_libcall (object, size, const0_rtx,
3207 method == BLOCK_OP_TAILCALL);
3208 else
3209 gcc_unreachable ();
3211 return NULL;
3215 clear_storage (rtx object, rtx size, enum block_op_methods method)
3217 unsigned HOST_WIDE_INT max, min = 0;
3218 if (GET_CODE (size) == CONST_INT)
3219 min = max = UINTVAL (size);
3220 else
3221 max = GET_MODE_MASK (GET_MODE (size));
3222 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3226 /* A subroutine of clear_storage. Expand a call to memset.
3227 Return the return value of memset, 0 otherwise. */
3230 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3232 tree call_expr, fn, object_tree, size_tree, val_tree;
3233 machine_mode size_mode;
3235 object = copy_addr_to_reg (XEXP (object, 0));
3236 object_tree = make_tree (ptr_type_node, object);
3238 if (!CONST_INT_P (val))
3239 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3240 val_tree = make_tree (integer_type_node, val);
3242 size_mode = TYPE_MODE (sizetype);
3243 size = convert_to_mode (size_mode, size, 1);
3244 size = copy_to_mode_reg (size_mode, size);
3245 size_tree = make_tree (sizetype, size);
3247 /* It is incorrect to use the libcall calling conventions for calls to
3248 memset because it can be provided by the user. */
3249 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3250 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3251 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3253 return expand_call (call_expr, NULL_RTX, false);
3256 /* Expand a setmem pattern; return true if successful. */
3258 bool
3259 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3260 unsigned int expected_align, HOST_WIDE_INT expected_size,
3261 unsigned HOST_WIDE_INT min_size,
3262 unsigned HOST_WIDE_INT max_size,
3263 unsigned HOST_WIDE_INT probable_max_size)
3265 /* Try the most limited insn first, because there's no point
3266 including more than one in the machine description unless
3267 the more limited one has some advantage. */
3269 if (expected_align < align)
3270 expected_align = align;
3271 if (expected_size != -1)
3273 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3274 expected_size = max_size;
3275 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3276 expected_size = min_size;
3279 opt_scalar_int_mode mode_iter;
3280 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3282 scalar_int_mode mode = mode_iter.require ();
3283 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3285 if (code != CODE_FOR_nothing
3286 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3287 here because if SIZE is less than the mode mask, as it is
3288 returned by the macro, it will definitely be less than the
3289 actual mode mask. Since SIZE is within the Pmode address
3290 space, we limit MODE to Pmode. */
3291 && ((CONST_INT_P (size)
3292 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3293 <= (GET_MODE_MASK (mode) >> 1)))
3294 || max_size <= (GET_MODE_MASK (mode) >> 1)
3295 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3297 class expand_operand ops[9];
3298 unsigned int nops;
3300 nops = insn_data[(int) code].n_generator_args;
3301 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3303 create_fixed_operand (&ops[0], object);
3304 /* The check above guarantees that this size conversion is valid. */
3305 create_convert_operand_to (&ops[1], size, mode, true);
3306 create_convert_operand_from (&ops[2], val, byte_mode, true);
3307 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3308 if (nops >= 6)
3310 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3311 create_integer_operand (&ops[5], expected_size);
3313 if (nops >= 8)
3315 create_integer_operand (&ops[6], min_size);
3316 /* If we cannot represent the maximal size,
3317 make parameter NULL. */
3318 if ((HOST_WIDE_INT) max_size != -1)
3319 create_integer_operand (&ops[7], max_size);
3320 else
3321 create_fixed_operand (&ops[7], NULL);
3323 if (nops == 9)
3325 /* If we cannot represent the maximal size,
3326 make parameter NULL. */
3327 if ((HOST_WIDE_INT) probable_max_size != -1)
3328 create_integer_operand (&ops[8], probable_max_size);
3329 else
3330 create_fixed_operand (&ops[8], NULL);
3332 if (maybe_expand_insn (code, nops, ops))
3333 return true;
3337 return false;
3341 /* Write to one of the components of the complex value CPLX. Write VAL to
3342 the real part if IMAG_P is false, and the imaginary part if its true. */
3344 void
3345 write_complex_part (rtx cplx, rtx val, bool imag_p)
3347 machine_mode cmode;
3348 scalar_mode imode;
3349 unsigned ibitsize;
3351 if (GET_CODE (cplx) == CONCAT)
3353 emit_move_insn (XEXP (cplx, imag_p), val);
3354 return;
3357 cmode = GET_MODE (cplx);
3358 imode = GET_MODE_INNER (cmode);
3359 ibitsize = GET_MODE_BITSIZE (imode);
3361 /* For MEMs simplify_gen_subreg may generate an invalid new address
3362 because, e.g., the original address is considered mode-dependent
3363 by the target, which restricts simplify_subreg from invoking
3364 adjust_address_nv. Instead of preparing fallback support for an
3365 invalid address, we call adjust_address_nv directly. */
3366 if (MEM_P (cplx))
3368 emit_move_insn (adjust_address_nv (cplx, imode,
3369 imag_p ? GET_MODE_SIZE (imode) : 0),
3370 val);
3371 return;
3374 /* If the sub-object is at least word sized, then we know that subregging
3375 will work. This special case is important, since store_bit_field
3376 wants to operate on integer modes, and there's rarely an OImode to
3377 correspond to TCmode. */
3378 if (ibitsize >= BITS_PER_WORD
3379 /* For hard regs we have exact predicates. Assume we can split
3380 the original object if it spans an even number of hard regs.
3381 This special case is important for SCmode on 64-bit platforms
3382 where the natural size of floating-point regs is 32-bit. */
3383 || (REG_P (cplx)
3384 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3385 && REG_NREGS (cplx) % 2 == 0))
3387 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3388 imag_p ? GET_MODE_SIZE (imode) : 0);
3389 if (part)
3391 emit_move_insn (part, val);
3392 return;
3394 else
3395 /* simplify_gen_subreg may fail for sub-word MEMs. */
3396 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3399 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3400 false);
3403 /* Extract one of the components of the complex value CPLX. Extract the
3404 real part if IMAG_P is false, and the imaginary part if it's true. */
3407 read_complex_part (rtx cplx, bool imag_p)
3409 machine_mode cmode;
3410 scalar_mode imode;
3411 unsigned ibitsize;
3413 if (GET_CODE (cplx) == CONCAT)
3414 return XEXP (cplx, imag_p);
3416 cmode = GET_MODE (cplx);
3417 imode = GET_MODE_INNER (cmode);
3418 ibitsize = GET_MODE_BITSIZE (imode);
3420 /* Special case reads from complex constants that got spilled to memory. */
3421 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3423 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3424 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3426 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3427 if (CONSTANT_CLASS_P (part))
3428 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3432 /* For MEMs simplify_gen_subreg may generate an invalid new address
3433 because, e.g., the original address is considered mode-dependent
3434 by the target, which restricts simplify_subreg from invoking
3435 adjust_address_nv. Instead of preparing fallback support for an
3436 invalid address, we call adjust_address_nv directly. */
3437 if (MEM_P (cplx))
3438 return adjust_address_nv (cplx, imode,
3439 imag_p ? GET_MODE_SIZE (imode) : 0);
3441 /* If the sub-object is at least word sized, then we know that subregging
3442 will work. This special case is important, since extract_bit_field
3443 wants to operate on integer modes, and there's rarely an OImode to
3444 correspond to TCmode. */
3445 if (ibitsize >= BITS_PER_WORD
3446 /* For hard regs we have exact predicates. Assume we can split
3447 the original object if it spans an even number of hard regs.
3448 This special case is important for SCmode on 64-bit platforms
3449 where the natural size of floating-point regs is 32-bit. */
3450 || (REG_P (cplx)
3451 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3452 && REG_NREGS (cplx) % 2 == 0))
3454 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3455 imag_p ? GET_MODE_SIZE (imode) : 0);
3456 if (ret)
3457 return ret;
3458 else
3459 /* simplify_gen_subreg may fail for sub-word MEMs. */
3460 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3463 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3464 true, NULL_RTX, imode, imode, false, NULL);
3467 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3468 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3469 represented in NEW_MODE. If FORCE is true, this will never happen, as
3470 we'll force-create a SUBREG if needed. */
3472 static rtx
3473 emit_move_change_mode (machine_mode new_mode,
3474 machine_mode old_mode, rtx x, bool force)
3476 rtx ret;
3478 if (push_operand (x, GET_MODE (x)))
3480 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3481 MEM_COPY_ATTRIBUTES (ret, x);
3483 else if (MEM_P (x))
3485 /* We don't have to worry about changing the address since the
3486 size in bytes is supposed to be the same. */
3487 if (reload_in_progress)
3489 /* Copy the MEM to change the mode and move any
3490 substitutions from the old MEM to the new one. */
3491 ret = adjust_address_nv (x, new_mode, 0);
3492 copy_replacements (x, ret);
3494 else
3495 ret = adjust_address (x, new_mode, 0);
3497 else
3499 /* Note that we do want simplify_subreg's behavior of validating
3500 that the new mode is ok for a hard register. If we were to use
3501 simplify_gen_subreg, we would create the subreg, but would
3502 probably run into the target not being able to implement it. */
3503 /* Except, of course, when FORCE is true, when this is exactly what
3504 we want. Which is needed for CCmodes on some targets. */
3505 if (force)
3506 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3507 else
3508 ret = simplify_subreg (new_mode, x, old_mode, 0);
3511 return ret;
3514 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3515 an integer mode of the same size as MODE. Returns the instruction
3516 emitted, or NULL if such a move could not be generated. */
3518 static rtx_insn *
3519 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3521 scalar_int_mode imode;
3522 enum insn_code code;
3524 /* There must exist a mode of the exact size we require. */
3525 if (!int_mode_for_mode (mode).exists (&imode))
3526 return NULL;
3528 /* The target must support moves in this mode. */
3529 code = optab_handler (mov_optab, imode);
3530 if (code == CODE_FOR_nothing)
3531 return NULL;
3533 x = emit_move_change_mode (imode, mode, x, force);
3534 if (x == NULL_RTX)
3535 return NULL;
3536 y = emit_move_change_mode (imode, mode, y, force);
3537 if (y == NULL_RTX)
3538 return NULL;
3539 return emit_insn (GEN_FCN (code) (x, y));
3542 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3543 Return an equivalent MEM that does not use an auto-increment. */
3546 emit_move_resolve_push (machine_mode mode, rtx x)
3548 enum rtx_code code = GET_CODE (XEXP (x, 0));
3549 rtx temp;
3551 poly_int64 adjust = GET_MODE_SIZE (mode);
3552 #ifdef PUSH_ROUNDING
3553 adjust = PUSH_ROUNDING (adjust);
3554 #endif
3555 if (code == PRE_DEC || code == POST_DEC)
3556 adjust = -adjust;
3557 else if (code == PRE_MODIFY || code == POST_MODIFY)
3559 rtx expr = XEXP (XEXP (x, 0), 1);
3561 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3562 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3563 if (GET_CODE (expr) == MINUS)
3564 val = -val;
3565 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3566 adjust = val;
3569 /* Do not use anti_adjust_stack, since we don't want to update
3570 stack_pointer_delta. */
3571 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3572 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3573 0, OPTAB_LIB_WIDEN);
3574 if (temp != stack_pointer_rtx)
3575 emit_move_insn (stack_pointer_rtx, temp);
3577 switch (code)
3579 case PRE_INC:
3580 case PRE_DEC:
3581 case PRE_MODIFY:
3582 temp = stack_pointer_rtx;
3583 break;
3584 case POST_INC:
3585 case POST_DEC:
3586 case POST_MODIFY:
3587 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3588 break;
3589 default:
3590 gcc_unreachable ();
3593 return replace_equiv_address (x, temp);
3596 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3597 X is known to satisfy push_operand, and MODE is known to be complex.
3598 Returns the last instruction emitted. */
3600 rtx_insn *
3601 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3603 scalar_mode submode = GET_MODE_INNER (mode);
3604 bool imag_first;
3606 #ifdef PUSH_ROUNDING
3607 poly_int64 submodesize = GET_MODE_SIZE (submode);
3609 /* In case we output to the stack, but the size is smaller than the
3610 machine can push exactly, we need to use move instructions. */
3611 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3613 x = emit_move_resolve_push (mode, x);
3614 return emit_move_insn (x, y);
3616 #endif
3618 /* Note that the real part always precedes the imag part in memory
3619 regardless of machine's endianness. */
3620 switch (GET_CODE (XEXP (x, 0)))
3622 case PRE_DEC:
3623 case POST_DEC:
3624 imag_first = true;
3625 break;
3626 case PRE_INC:
3627 case POST_INC:
3628 imag_first = false;
3629 break;
3630 default:
3631 gcc_unreachable ();
3634 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3635 read_complex_part (y, imag_first));
3636 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3637 read_complex_part (y, !imag_first));
3640 /* A subroutine of emit_move_complex. Perform the move from Y to X
3641 via two moves of the parts. Returns the last instruction emitted. */
3643 rtx_insn *
3644 emit_move_complex_parts (rtx x, rtx y)
3646 /* Show the output dies here. This is necessary for SUBREGs
3647 of pseudos since we cannot track their lifetimes correctly;
3648 hard regs shouldn't appear here except as return values. */
3649 if (!reload_completed && !reload_in_progress
3650 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3651 emit_clobber (x);
3653 write_complex_part (x, read_complex_part (y, false), false);
3654 write_complex_part (x, read_complex_part (y, true), true);
3656 return get_last_insn ();
3659 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3660 MODE is known to be complex. Returns the last instruction emitted. */
3662 static rtx_insn *
3663 emit_move_complex (machine_mode mode, rtx x, rtx y)
3665 bool try_int;
3667 /* Need to take special care for pushes, to maintain proper ordering
3668 of the data, and possibly extra padding. */
3669 if (push_operand (x, mode))
3670 return emit_move_complex_push (mode, x, y);
3672 /* See if we can coerce the target into moving both values at once, except
3673 for floating point where we favor moving as parts if this is easy. */
3674 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3675 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3676 && !(REG_P (x)
3677 && HARD_REGISTER_P (x)
3678 && REG_NREGS (x) == 1)
3679 && !(REG_P (y)
3680 && HARD_REGISTER_P (y)
3681 && REG_NREGS (y) == 1))
3682 try_int = false;
3683 /* Not possible if the values are inherently not adjacent. */
3684 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3685 try_int = false;
3686 /* Is possible if both are registers (or subregs of registers). */
3687 else if (register_operand (x, mode) && register_operand (y, mode))
3688 try_int = true;
3689 /* If one of the operands is a memory, and alignment constraints
3690 are friendly enough, we may be able to do combined memory operations.
3691 We do not attempt this if Y is a constant because that combination is
3692 usually better with the by-parts thing below. */
3693 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3694 && (!STRICT_ALIGNMENT
3695 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3696 try_int = true;
3697 else
3698 try_int = false;
3700 if (try_int)
3702 rtx_insn *ret;
3704 /* For memory to memory moves, optimal behavior can be had with the
3705 existing block move logic. But use normal expansion if optimizing
3706 for size. */
3707 if (MEM_P (x) && MEM_P (y))
3709 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3710 (optimize_insn_for_speed_p()
3711 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3712 return get_last_insn ();
3715 ret = emit_move_via_integer (mode, x, y, true);
3716 if (ret)
3717 return ret;
3720 return emit_move_complex_parts (x, y);
3723 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3724 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3726 static rtx_insn *
3727 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3729 rtx_insn *ret;
3731 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3732 if (mode != CCmode)
3734 enum insn_code code = optab_handler (mov_optab, CCmode);
3735 if (code != CODE_FOR_nothing)
3737 x = emit_move_change_mode (CCmode, mode, x, true);
3738 y = emit_move_change_mode (CCmode, mode, y, true);
3739 return emit_insn (GEN_FCN (code) (x, y));
3743 /* Otherwise, find the MODE_INT mode of the same width. */
3744 ret = emit_move_via_integer (mode, x, y, false);
3745 gcc_assert (ret != NULL);
3746 return ret;
3749 /* Return true if word I of OP lies entirely in the
3750 undefined bits of a paradoxical subreg. */
3752 static bool
3753 undefined_operand_subword_p (const_rtx op, int i)
3755 if (GET_CODE (op) != SUBREG)
3756 return false;
3757 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3758 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3759 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3760 || known_le (offset, -UNITS_PER_WORD));
3763 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3764 MODE is any multi-word or full-word mode that lacks a move_insn
3765 pattern. Note that you will get better code if you define such
3766 patterns, even if they must turn into multiple assembler instructions. */
3768 static rtx_insn *
3769 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3771 rtx_insn *last_insn = 0;
3772 rtx_insn *seq;
3773 rtx inner;
3774 bool need_clobber;
3775 int i, mode_size;
3777 /* This function can only handle cases where the number of words is
3778 known at compile time. */
3779 mode_size = GET_MODE_SIZE (mode).to_constant ();
3780 gcc_assert (mode_size >= UNITS_PER_WORD);
3782 /* If X is a push on the stack, do the push now and replace
3783 X with a reference to the stack pointer. */
3784 if (push_operand (x, mode))
3785 x = emit_move_resolve_push (mode, x);
3787 /* If we are in reload, see if either operand is a MEM whose address
3788 is scheduled for replacement. */
3789 if (reload_in_progress && MEM_P (x)
3790 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3791 x = replace_equiv_address_nv (x, inner);
3792 if (reload_in_progress && MEM_P (y)
3793 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3794 y = replace_equiv_address_nv (y, inner);
3796 start_sequence ();
3798 need_clobber = false;
3799 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3801 /* Do not generate code for a move if it would go entirely
3802 to the non-existing bits of a paradoxical subreg. */
3803 if (undefined_operand_subword_p (x, i))
3804 continue;
3806 rtx xpart = operand_subword (x, i, 1, mode);
3807 rtx ypart;
3809 /* Do not generate code for a move if it would come entirely
3810 from the undefined bits of a paradoxical subreg. */
3811 if (undefined_operand_subword_p (y, i))
3812 continue;
3814 ypart = operand_subword (y, i, 1, mode);
3816 /* If we can't get a part of Y, put Y into memory if it is a
3817 constant. Otherwise, force it into a register. Then we must
3818 be able to get a part of Y. */
3819 if (ypart == 0 && CONSTANT_P (y))
3821 y = use_anchored_address (force_const_mem (mode, y));
3822 ypart = operand_subword (y, i, 1, mode);
3824 else if (ypart == 0)
3825 ypart = operand_subword_force (y, i, mode);
3827 gcc_assert (xpart && ypart);
3829 need_clobber |= (GET_CODE (xpart) == SUBREG);
3831 last_insn = emit_move_insn (xpart, ypart);
3834 seq = get_insns ();
3835 end_sequence ();
3837 /* Show the output dies here. This is necessary for SUBREGs
3838 of pseudos since we cannot track their lifetimes correctly;
3839 hard regs shouldn't appear here except as return values.
3840 We never want to emit such a clobber after reload. */
3841 if (x != y
3842 && ! (reload_in_progress || reload_completed)
3843 && need_clobber != 0)
3844 emit_clobber (x);
3846 emit_insn (seq);
3848 return last_insn;
3851 /* Low level part of emit_move_insn.
3852 Called just like emit_move_insn, but assumes X and Y
3853 are basically valid. */
3855 rtx_insn *
3856 emit_move_insn_1 (rtx x, rtx y)
3858 machine_mode mode = GET_MODE (x);
3859 enum insn_code code;
3861 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3863 code = optab_handler (mov_optab, mode);
3864 if (code != CODE_FOR_nothing)
3865 return emit_insn (GEN_FCN (code) (x, y));
3867 /* Expand complex moves by moving real part and imag part. */
3868 if (COMPLEX_MODE_P (mode))
3869 return emit_move_complex (mode, x, y);
3871 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3872 || ALL_FIXED_POINT_MODE_P (mode))
3874 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3876 /* If we can't find an integer mode, use multi words. */
3877 if (result)
3878 return result;
3879 else
3880 return emit_move_multi_word (mode, x, y);
3883 if (GET_MODE_CLASS (mode) == MODE_CC)
3884 return emit_move_ccmode (mode, x, y);
3886 /* Try using a move pattern for the corresponding integer mode. This is
3887 only safe when simplify_subreg can convert MODE constants into integer
3888 constants. At present, it can only do this reliably if the value
3889 fits within a HOST_WIDE_INT. */
3890 if (!CONSTANT_P (y)
3891 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3893 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3895 if (ret)
3897 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3898 return ret;
3902 return emit_move_multi_word (mode, x, y);
3905 /* Generate code to copy Y into X.
3906 Both Y and X must have the same mode, except that
3907 Y can be a constant with VOIDmode.
3908 This mode cannot be BLKmode; use emit_block_move for that.
3910 Return the last instruction emitted. */
3912 rtx_insn *
3913 emit_move_insn (rtx x, rtx y)
3915 machine_mode mode = GET_MODE (x);
3916 rtx y_cst = NULL_RTX;
3917 rtx_insn *last_insn;
3918 rtx set;
3920 gcc_assert (mode != BLKmode
3921 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3923 /* If we have a copy that looks like one of the following patterns:
3924 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
3925 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
3926 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
3927 (set (subreg:M1 (reg:M2 ...)) (constant C))
3928 where mode M1 is equal in size to M2, try to detect whether the
3929 mode change involves an implicit round trip through memory.
3930 If so, see if we can avoid that by removing the subregs and
3931 doing the move in mode M2 instead. */
3933 rtx x_inner = NULL_RTX;
3934 rtx y_inner = NULL_RTX;
3936 auto candidate_subreg_p = [&](rtx subreg) {
3937 return (REG_P (SUBREG_REG (subreg))
3938 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
3939 GET_MODE_SIZE (GET_MODE (subreg)))
3940 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
3941 != CODE_FOR_nothing);
3944 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
3945 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
3946 && !push_operand (mem, GET_MODE (mem))
3947 /* Not a candiate if innermode requires too much alignment. */
3948 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
3949 || targetm.slow_unaligned_access (GET_MODE (mem),
3950 MEM_ALIGN (mem))
3951 || !targetm.slow_unaligned_access (innermode,
3952 MEM_ALIGN (mem))));
3955 if (SUBREG_P (x) && candidate_subreg_p (x))
3956 x_inner = SUBREG_REG (x);
3958 if (SUBREG_P (y) && candidate_subreg_p (y))
3959 y_inner = SUBREG_REG (y);
3961 if (x_inner != NULL_RTX
3962 && y_inner != NULL_RTX
3963 && GET_MODE (x_inner) == GET_MODE (y_inner)
3964 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
3966 x = x_inner;
3967 y = y_inner;
3968 mode = GET_MODE (x_inner);
3970 else if (x_inner != NULL_RTX
3971 && MEM_P (y)
3972 && candidate_mem_p (GET_MODE (x_inner), y))
3974 x = x_inner;
3975 y = adjust_address (y, GET_MODE (x_inner), 0);
3976 mode = GET_MODE (x_inner);
3978 else if (y_inner != NULL_RTX
3979 && MEM_P (x)
3980 && candidate_mem_p (GET_MODE (y_inner), x))
3982 x = adjust_address (x, GET_MODE (y_inner), 0);
3983 y = y_inner;
3984 mode = GET_MODE (y_inner);
3986 else if (x_inner != NULL_RTX
3987 && CONSTANT_P (y)
3988 && !targetm.can_change_mode_class (GET_MODE (x_inner),
3989 mode, ALL_REGS)
3990 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
3992 x = x_inner;
3993 y = y_inner;
3994 mode = GET_MODE (x_inner);
3997 if (CONSTANT_P (y))
3999 if (optimize
4000 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4001 && (last_insn = compress_float_constant (x, y)))
4002 return last_insn;
4004 y_cst = y;
4006 if (!targetm.legitimate_constant_p (mode, y))
4008 y = force_const_mem (mode, y);
4010 /* If the target's cannot_force_const_mem prevented the spill,
4011 assume that the target's move expanders will also take care
4012 of the non-legitimate constant. */
4013 if (!y)
4014 y = y_cst;
4015 else
4016 y = use_anchored_address (y);
4020 /* If X or Y are memory references, verify that their addresses are valid
4021 for the machine. */
4022 if (MEM_P (x)
4023 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4024 MEM_ADDR_SPACE (x))
4025 && ! push_operand (x, GET_MODE (x))))
4026 x = validize_mem (x);
4028 if (MEM_P (y)
4029 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4030 MEM_ADDR_SPACE (y)))
4031 y = validize_mem (y);
4033 gcc_assert (mode != BLKmode);
4035 last_insn = emit_move_insn_1 (x, y);
4037 if (y_cst && REG_P (x)
4038 && (set = single_set (last_insn)) != NULL_RTX
4039 && SET_DEST (set) == x
4040 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4041 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4043 return last_insn;
4046 /* Generate the body of an instruction to copy Y into X.
4047 It may be a list of insns, if one insn isn't enough. */
4049 rtx_insn *
4050 gen_move_insn (rtx x, rtx y)
4052 rtx_insn *seq;
4054 start_sequence ();
4055 emit_move_insn_1 (x, y);
4056 seq = get_insns ();
4057 end_sequence ();
4058 return seq;
4061 /* If Y is representable exactly in a narrower mode, and the target can
4062 perform the extension directly from constant or memory, then emit the
4063 move as an extension. */
4065 static rtx_insn *
4066 compress_float_constant (rtx x, rtx y)
4068 machine_mode dstmode = GET_MODE (x);
4069 machine_mode orig_srcmode = GET_MODE (y);
4070 machine_mode srcmode;
4071 const REAL_VALUE_TYPE *r;
4072 int oldcost, newcost;
4073 bool speed = optimize_insn_for_speed_p ();
4075 r = CONST_DOUBLE_REAL_VALUE (y);
4077 if (targetm.legitimate_constant_p (dstmode, y))
4078 oldcost = set_src_cost (y, orig_srcmode, speed);
4079 else
4080 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4082 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4084 enum insn_code ic;
4085 rtx trunc_y;
4086 rtx_insn *last_insn;
4088 /* Skip if the target can't extend this way. */
4089 ic = can_extend_p (dstmode, srcmode, 0);
4090 if (ic == CODE_FOR_nothing)
4091 continue;
4093 /* Skip if the narrowed value isn't exact. */
4094 if (! exact_real_truncate (srcmode, r))
4095 continue;
4097 trunc_y = const_double_from_real_value (*r, srcmode);
4099 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4101 /* Skip if the target needs extra instructions to perform
4102 the extension. */
4103 if (!insn_operand_matches (ic, 1, trunc_y))
4104 continue;
4105 /* This is valid, but may not be cheaper than the original. */
4106 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4107 dstmode, speed);
4108 if (oldcost < newcost)
4109 continue;
4111 else if (float_extend_from_mem[dstmode][srcmode])
4113 trunc_y = force_const_mem (srcmode, trunc_y);
4114 /* This is valid, but may not be cheaper than the original. */
4115 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4116 dstmode, speed);
4117 if (oldcost < newcost)
4118 continue;
4119 trunc_y = validize_mem (trunc_y);
4121 else
4122 continue;
4124 /* For CSE's benefit, force the compressed constant pool entry
4125 into a new pseudo. This constant may be used in different modes,
4126 and if not, combine will put things back together for us. */
4127 trunc_y = force_reg (srcmode, trunc_y);
4129 /* If x is a hard register, perform the extension into a pseudo,
4130 so that e.g. stack realignment code is aware of it. */
4131 rtx target = x;
4132 if (REG_P (x) && HARD_REGISTER_P (x))
4133 target = gen_reg_rtx (dstmode);
4135 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4136 last_insn = get_last_insn ();
4138 if (REG_P (target))
4139 set_unique_reg_note (last_insn, REG_EQUAL, y);
4141 if (target != x)
4142 return emit_move_insn (x, target);
4143 return last_insn;
4146 return NULL;
4149 /* Pushing data onto the stack. */
4151 /* Push a block of length SIZE (perhaps variable)
4152 and return an rtx to address the beginning of the block.
4153 The value may be virtual_outgoing_args_rtx.
4155 EXTRA is the number of bytes of padding to push in addition to SIZE.
4156 BELOW nonzero means this padding comes at low addresses;
4157 otherwise, the padding comes at high addresses. */
4160 push_block (rtx size, poly_int64 extra, int below)
4162 rtx temp;
4164 size = convert_modes (Pmode, ptr_mode, size, 1);
4165 if (CONSTANT_P (size))
4166 anti_adjust_stack (plus_constant (Pmode, size, extra));
4167 else if (REG_P (size) && known_eq (extra, 0))
4168 anti_adjust_stack (size);
4169 else
4171 temp = copy_to_mode_reg (Pmode, size);
4172 if (maybe_ne (extra, 0))
4173 temp = expand_binop (Pmode, add_optab, temp,
4174 gen_int_mode (extra, Pmode),
4175 temp, 0, OPTAB_LIB_WIDEN);
4176 anti_adjust_stack (temp);
4179 if (STACK_GROWS_DOWNWARD)
4181 temp = virtual_outgoing_args_rtx;
4182 if (maybe_ne (extra, 0) && below)
4183 temp = plus_constant (Pmode, temp, extra);
4185 else
4187 poly_int64 csize;
4188 if (poly_int_rtx_p (size, &csize))
4189 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4190 -csize - (below ? 0 : extra));
4191 else if (maybe_ne (extra, 0) && !below)
4192 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4193 negate_rtx (Pmode, plus_constant (Pmode, size,
4194 extra)));
4195 else
4196 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4197 negate_rtx (Pmode, size));
4200 return memory_address (NARROWEST_INT_MODE, temp);
4203 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4205 static rtx
4206 mem_autoinc_base (rtx mem)
4208 if (MEM_P (mem))
4210 rtx addr = XEXP (mem, 0);
4211 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4212 return XEXP (addr, 0);
4214 return NULL;
4217 /* A utility routine used here, in reload, and in try_split. The insns
4218 after PREV up to and including LAST are known to adjust the stack,
4219 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4220 placing notes as appropriate. PREV may be NULL, indicating the
4221 entire insn sequence prior to LAST should be scanned.
4223 The set of allowed stack pointer modifications is small:
4224 (1) One or more auto-inc style memory references (aka pushes),
4225 (2) One or more addition/subtraction with the SP as destination,
4226 (3) A single move insn with the SP as destination,
4227 (4) A call_pop insn,
4228 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4230 Insns in the sequence that do not modify the SP are ignored,
4231 except for noreturn calls.
4233 The return value is the amount of adjustment that can be trivially
4234 verified, via immediate operand or auto-inc. If the adjustment
4235 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4237 poly_int64
4238 find_args_size_adjust (rtx_insn *insn)
4240 rtx dest, set, pat;
4241 int i;
4243 pat = PATTERN (insn);
4244 set = NULL;
4246 /* Look for a call_pop pattern. */
4247 if (CALL_P (insn))
4249 /* We have to allow non-call_pop patterns for the case
4250 of emit_single_push_insn of a TLS address. */
4251 if (GET_CODE (pat) != PARALLEL)
4252 return 0;
4254 /* All call_pop have a stack pointer adjust in the parallel.
4255 The call itself is always first, and the stack adjust is
4256 usually last, so search from the end. */
4257 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4259 set = XVECEXP (pat, 0, i);
4260 if (GET_CODE (set) != SET)
4261 continue;
4262 dest = SET_DEST (set);
4263 if (dest == stack_pointer_rtx)
4264 break;
4266 /* We'd better have found the stack pointer adjust. */
4267 if (i == 0)
4268 return 0;
4269 /* Fall through to process the extracted SET and DEST
4270 as if it was a standalone insn. */
4272 else if (GET_CODE (pat) == SET)
4273 set = pat;
4274 else if ((set = single_set (insn)) != NULL)
4276 else if (GET_CODE (pat) == PARALLEL)
4278 /* ??? Some older ports use a parallel with a stack adjust
4279 and a store for a PUSH_ROUNDING pattern, rather than a
4280 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4281 /* ??? See h8300 and m68k, pushqi1. */
4282 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4284 set = XVECEXP (pat, 0, i);
4285 if (GET_CODE (set) != SET)
4286 continue;
4287 dest = SET_DEST (set);
4288 if (dest == stack_pointer_rtx)
4289 break;
4291 /* We do not expect an auto-inc of the sp in the parallel. */
4292 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4293 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4294 != stack_pointer_rtx);
4296 if (i < 0)
4297 return 0;
4299 else
4300 return 0;
4302 dest = SET_DEST (set);
4304 /* Look for direct modifications of the stack pointer. */
4305 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4307 /* Look for a trivial adjustment, otherwise assume nothing. */
4308 /* Note that the SPU restore_stack_block pattern refers to
4309 the stack pointer in V4SImode. Consider that non-trivial. */
4310 poly_int64 offset;
4311 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4312 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4313 return offset;
4314 /* ??? Reload can generate no-op moves, which will be cleaned
4315 up later. Recognize it and continue searching. */
4316 else if (rtx_equal_p (dest, SET_SRC (set)))
4317 return 0;
4318 else
4319 return HOST_WIDE_INT_MIN;
4321 else
4323 rtx mem, addr;
4325 /* Otherwise only think about autoinc patterns. */
4326 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4328 mem = dest;
4329 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4330 != stack_pointer_rtx);
4332 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4333 mem = SET_SRC (set);
4334 else
4335 return 0;
4337 addr = XEXP (mem, 0);
4338 switch (GET_CODE (addr))
4340 case PRE_INC:
4341 case POST_INC:
4342 return GET_MODE_SIZE (GET_MODE (mem));
4343 case PRE_DEC:
4344 case POST_DEC:
4345 return -GET_MODE_SIZE (GET_MODE (mem));
4346 case PRE_MODIFY:
4347 case POST_MODIFY:
4348 addr = XEXP (addr, 1);
4349 gcc_assert (GET_CODE (addr) == PLUS);
4350 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4351 return rtx_to_poly_int64 (XEXP (addr, 1));
4352 default:
4353 gcc_unreachable ();
4358 poly_int64
4359 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4360 poly_int64 end_args_size)
4362 poly_int64 args_size = end_args_size;
4363 bool saw_unknown = false;
4364 rtx_insn *insn;
4366 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4368 if (!NONDEBUG_INSN_P (insn))
4369 continue;
4371 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4372 a call argument containing a TLS address that itself requires
4373 a call to __tls_get_addr. The handling of stack_pointer_delta
4374 in emit_single_push_insn is supposed to ensure that any such
4375 notes are already correct. */
4376 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4377 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4379 poly_int64 this_delta = find_args_size_adjust (insn);
4380 if (known_eq (this_delta, 0))
4382 if (!CALL_P (insn)
4383 || ACCUMULATE_OUTGOING_ARGS
4384 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4385 continue;
4388 gcc_assert (!saw_unknown);
4389 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4390 saw_unknown = true;
4392 if (!note)
4393 add_args_size_note (insn, args_size);
4394 if (STACK_GROWS_DOWNWARD)
4395 this_delta = -poly_uint64 (this_delta);
4397 if (saw_unknown)
4398 args_size = HOST_WIDE_INT_MIN;
4399 else
4400 args_size -= this_delta;
4403 return args_size;
4406 #ifdef PUSH_ROUNDING
4407 /* Emit single push insn. */
4409 static void
4410 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4412 rtx dest_addr;
4413 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4414 rtx dest;
4415 enum insn_code icode;
4417 /* If there is push pattern, use it. Otherwise try old way of throwing
4418 MEM representing push operation to move expander. */
4419 icode = optab_handler (push_optab, mode);
4420 if (icode != CODE_FOR_nothing)
4422 class expand_operand ops[1];
4424 create_input_operand (&ops[0], x, mode);
4425 if (maybe_expand_insn (icode, 1, ops))
4426 return;
4428 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4429 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4430 /* If we are to pad downward, adjust the stack pointer first and
4431 then store X into the stack location using an offset. This is
4432 because emit_move_insn does not know how to pad; it does not have
4433 access to type. */
4434 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4436 emit_move_insn (stack_pointer_rtx,
4437 expand_binop (Pmode,
4438 STACK_GROWS_DOWNWARD ? sub_optab
4439 : add_optab,
4440 stack_pointer_rtx,
4441 gen_int_mode (rounded_size, Pmode),
4442 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4444 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4445 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4446 /* We have already decremented the stack pointer, so get the
4447 previous value. */
4448 offset += rounded_size;
4450 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4451 /* We have already incremented the stack pointer, so get the
4452 previous value. */
4453 offset -= rounded_size;
4455 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4457 else
4459 if (STACK_GROWS_DOWNWARD)
4460 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4461 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4462 else
4463 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4464 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4466 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4469 dest = gen_rtx_MEM (mode, dest_addr);
4471 if (type != 0)
4473 set_mem_attributes (dest, type, 1);
4475 if (cfun->tail_call_marked)
4476 /* Function incoming arguments may overlap with sibling call
4477 outgoing arguments and we cannot allow reordering of reads
4478 from function arguments with stores to outgoing arguments
4479 of sibling calls. */
4480 set_mem_alias_set (dest, 0);
4482 emit_move_insn (dest, x);
4485 /* Emit and annotate a single push insn. */
4487 static void
4488 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4490 poly_int64 delta, old_delta = stack_pointer_delta;
4491 rtx_insn *prev = get_last_insn ();
4492 rtx_insn *last;
4494 emit_single_push_insn_1 (mode, x, type);
4496 /* Adjust stack_pointer_delta to describe the situation after the push
4497 we just performed. Note that we must do this after the push rather
4498 than before the push in case calculating X needs pushes and pops of
4499 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4500 for such pushes and pops must not include the effect of the future
4501 push of X. */
4502 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4504 last = get_last_insn ();
4506 /* Notice the common case where we emitted exactly one insn. */
4507 if (PREV_INSN (last) == prev)
4509 add_args_size_note (last, stack_pointer_delta);
4510 return;
4513 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4514 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4515 || known_eq (delta, old_delta));
4517 #endif
4519 /* If reading SIZE bytes from X will end up reading from
4520 Y return the number of bytes that overlap. Return -1
4521 if there is no overlap or -2 if we can't determine
4522 (for example when X and Y have different base registers). */
4524 static int
4525 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4527 rtx tmp = plus_constant (Pmode, x, size);
4528 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4530 if (!CONST_INT_P (sub))
4531 return -2;
4533 HOST_WIDE_INT val = INTVAL (sub);
4535 return IN_RANGE (val, 1, size) ? val : -1;
4538 /* Generate code to push X onto the stack, assuming it has mode MODE and
4539 type TYPE.
4540 MODE is redundant except when X is a CONST_INT (since they don't
4541 carry mode info).
4542 SIZE is an rtx for the size of data to be copied (in bytes),
4543 needed only if X is BLKmode.
4544 Return true if successful. May return false if asked to push a
4545 partial argument during a sibcall optimization (as specified by
4546 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4547 to not overlap.
4549 ALIGN (in bits) is maximum alignment we can assume.
4551 If PARTIAL and REG are both nonzero, then copy that many of the first
4552 bytes of X into registers starting with REG, and push the rest of X.
4553 The amount of space pushed is decreased by PARTIAL bytes.
4554 REG must be a hard register in this case.
4555 If REG is zero but PARTIAL is not, take any all others actions for an
4556 argument partially in registers, but do not actually load any
4557 registers.
4559 EXTRA is the amount in bytes of extra space to leave next to this arg.
4560 This is ignored if an argument block has already been allocated.
4562 On a machine that lacks real push insns, ARGS_ADDR is the address of
4563 the bottom of the argument block for this call. We use indexing off there
4564 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4565 argument block has not been preallocated.
4567 ARGS_SO_FAR is the size of args previously pushed for this call.
4569 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4570 for arguments passed in registers. If nonzero, it will be the number
4571 of bytes required. */
4573 bool
4574 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4575 unsigned int align, int partial, rtx reg, poly_int64 extra,
4576 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4577 rtx alignment_pad, bool sibcall_p)
4579 rtx xinner;
4580 pad_direction stack_direction
4581 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4583 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4584 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4585 Default is below for small data on big-endian machines; else above. */
4586 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4588 /* Invert direction if stack is post-decrement.
4589 FIXME: why? */
4590 if (STACK_PUSH_CODE == POST_DEC)
4591 if (where_pad != PAD_NONE)
4592 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4594 xinner = x;
4596 int nregs = partial / UNITS_PER_WORD;
4597 rtx *tmp_regs = NULL;
4598 int overlapping = 0;
4600 if (mode == BLKmode
4601 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4603 /* Copy a block into the stack, entirely or partially. */
4605 rtx temp;
4606 int used;
4607 int offset;
4608 int skip;
4610 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4611 used = partial - offset;
4613 if (mode != BLKmode)
4615 /* A value is to be stored in an insufficiently aligned
4616 stack slot; copy via a suitably aligned slot if
4617 necessary. */
4618 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4619 if (!MEM_P (xinner))
4621 temp = assign_temp (type, 1, 1);
4622 emit_move_insn (temp, xinner);
4623 xinner = temp;
4627 gcc_assert (size);
4629 /* USED is now the # of bytes we need not copy to the stack
4630 because registers will take care of them. */
4632 if (partial != 0)
4633 xinner = adjust_address (xinner, BLKmode, used);
4635 /* If the partial register-part of the arg counts in its stack size,
4636 skip the part of stack space corresponding to the registers.
4637 Otherwise, start copying to the beginning of the stack space,
4638 by setting SKIP to 0. */
4639 skip = (reg_parm_stack_space == 0) ? 0 : used;
4641 #ifdef PUSH_ROUNDING
4642 /* Do it with several push insns if that doesn't take lots of insns
4643 and if there is no difficulty with push insns that skip bytes
4644 on the stack for alignment purposes. */
4645 if (args_addr == 0
4646 && PUSH_ARGS
4647 && CONST_INT_P (size)
4648 && skip == 0
4649 && MEM_ALIGN (xinner) >= align
4650 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4651 /* Here we avoid the case of a structure whose weak alignment
4652 forces many pushes of a small amount of data,
4653 and such small pushes do rounding that causes trouble. */
4654 && ((!targetm.slow_unaligned_access (word_mode, align))
4655 || align >= BIGGEST_ALIGNMENT
4656 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4657 align / BITS_PER_UNIT))
4658 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4660 /* Push padding now if padding above and stack grows down,
4661 or if padding below and stack grows up.
4662 But if space already allocated, this has already been done. */
4663 if (maybe_ne (extra, 0)
4664 && args_addr == 0
4665 && where_pad != PAD_NONE
4666 && where_pad != stack_direction)
4667 anti_adjust_stack (gen_int_mode (extra, Pmode));
4669 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4670 RETURN_BEGIN);
4672 else
4673 #endif /* PUSH_ROUNDING */
4675 rtx target;
4677 /* Otherwise make space on the stack and copy the data
4678 to the address of that space. */
4680 /* Deduct words put into registers from the size we must copy. */
4681 if (partial != 0)
4683 if (CONST_INT_P (size))
4684 size = GEN_INT (INTVAL (size) - used);
4685 else
4686 size = expand_binop (GET_MODE (size), sub_optab, size,
4687 gen_int_mode (used, GET_MODE (size)),
4688 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4691 /* Get the address of the stack space.
4692 In this case, we do not deal with EXTRA separately.
4693 A single stack adjust will do. */
4694 poly_int64 const_args_so_far;
4695 if (! args_addr)
4697 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4698 extra = 0;
4700 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4701 temp = memory_address (BLKmode,
4702 plus_constant (Pmode, args_addr,
4703 skip + const_args_so_far));
4704 else
4705 temp = memory_address (BLKmode,
4706 plus_constant (Pmode,
4707 gen_rtx_PLUS (Pmode,
4708 args_addr,
4709 args_so_far),
4710 skip));
4712 if (!ACCUMULATE_OUTGOING_ARGS)
4714 /* If the source is referenced relative to the stack pointer,
4715 copy it to another register to stabilize it. We do not need
4716 to do this if we know that we won't be changing sp. */
4718 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4719 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4720 temp = copy_to_reg (temp);
4723 target = gen_rtx_MEM (BLKmode, temp);
4725 /* We do *not* set_mem_attributes here, because incoming arguments
4726 may overlap with sibling call outgoing arguments and we cannot
4727 allow reordering of reads from function arguments with stores
4728 to outgoing arguments of sibling calls. We do, however, want
4729 to record the alignment of the stack slot. */
4730 /* ALIGN may well be better aligned than TYPE, e.g. due to
4731 PARM_BOUNDARY. Assume the caller isn't lying. */
4732 set_mem_align (target, align);
4734 /* If part should go in registers and pushing to that part would
4735 overwrite some of the values that need to go into regs, load the
4736 overlapping values into temporary pseudos to be moved into the hard
4737 regs at the end after the stack pushing has completed.
4738 We cannot load them directly into the hard regs here because
4739 they can be clobbered by the block move expansions.
4740 See PR 65358. */
4742 if (partial > 0 && reg != 0 && mode == BLKmode
4743 && GET_CODE (reg) != PARALLEL)
4745 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4746 if (overlapping > 0)
4748 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4749 overlapping /= UNITS_PER_WORD;
4751 tmp_regs = XALLOCAVEC (rtx, overlapping);
4753 for (int i = 0; i < overlapping; i++)
4754 tmp_regs[i] = gen_reg_rtx (word_mode);
4756 for (int i = 0; i < overlapping; i++)
4757 emit_move_insn (tmp_regs[i],
4758 operand_subword_force (target, i, mode));
4760 else if (overlapping == -1)
4761 overlapping = 0;
4762 /* Could not determine whether there is overlap.
4763 Fail the sibcall. */
4764 else
4766 overlapping = 0;
4767 if (sibcall_p)
4768 return false;
4771 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4774 else if (partial > 0)
4776 /* Scalar partly in registers. This case is only supported
4777 for fixed-wdth modes. */
4778 int num_words = GET_MODE_SIZE (mode).to_constant ();
4779 num_words /= UNITS_PER_WORD;
4780 int i;
4781 int not_stack;
4782 /* # bytes of start of argument
4783 that we must make space for but need not store. */
4784 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4785 int args_offset = INTVAL (args_so_far);
4786 int skip;
4788 /* Push padding now if padding above and stack grows down,
4789 or if padding below and stack grows up.
4790 But if space already allocated, this has already been done. */
4791 if (maybe_ne (extra, 0)
4792 && args_addr == 0
4793 && where_pad != PAD_NONE
4794 && where_pad != stack_direction)
4795 anti_adjust_stack (gen_int_mode (extra, Pmode));
4797 /* If we make space by pushing it, we might as well push
4798 the real data. Otherwise, we can leave OFFSET nonzero
4799 and leave the space uninitialized. */
4800 if (args_addr == 0)
4801 offset = 0;
4803 /* Now NOT_STACK gets the number of words that we don't need to
4804 allocate on the stack. Convert OFFSET to words too. */
4805 not_stack = (partial - offset) / UNITS_PER_WORD;
4806 offset /= UNITS_PER_WORD;
4808 /* If the partial register-part of the arg counts in its stack size,
4809 skip the part of stack space corresponding to the registers.
4810 Otherwise, start copying to the beginning of the stack space,
4811 by setting SKIP to 0. */
4812 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4814 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4815 x = validize_mem (force_const_mem (mode, x));
4817 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4818 SUBREGs of such registers are not allowed. */
4819 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4820 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4821 x = copy_to_reg (x);
4823 /* Loop over all the words allocated on the stack for this arg. */
4824 /* We can do it by words, because any scalar bigger than a word
4825 has a size a multiple of a word. */
4826 for (i = num_words - 1; i >= not_stack; i--)
4827 if (i >= not_stack + offset)
4828 if (!emit_push_insn (operand_subword_force (x, i, mode),
4829 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4830 0, args_addr,
4831 GEN_INT (args_offset + ((i - not_stack + skip)
4832 * UNITS_PER_WORD)),
4833 reg_parm_stack_space, alignment_pad, sibcall_p))
4834 return false;
4836 else
4838 rtx addr;
4839 rtx dest;
4841 /* Push padding now if padding above and stack grows down,
4842 or if padding below and stack grows up.
4843 But if space already allocated, this has already been done. */
4844 if (maybe_ne (extra, 0)
4845 && args_addr == 0
4846 && where_pad != PAD_NONE
4847 && where_pad != stack_direction)
4848 anti_adjust_stack (gen_int_mode (extra, Pmode));
4850 #ifdef PUSH_ROUNDING
4851 if (args_addr == 0 && PUSH_ARGS)
4852 emit_single_push_insn (mode, x, type);
4853 else
4854 #endif
4856 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4857 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4859 /* We do *not* set_mem_attributes here, because incoming arguments
4860 may overlap with sibling call outgoing arguments and we cannot
4861 allow reordering of reads from function arguments with stores
4862 to outgoing arguments of sibling calls. We do, however, want
4863 to record the alignment of the stack slot. */
4864 /* ALIGN may well be better aligned than TYPE, e.g. due to
4865 PARM_BOUNDARY. Assume the caller isn't lying. */
4866 set_mem_align (dest, align);
4868 emit_move_insn (dest, x);
4872 /* Move the partial arguments into the registers and any overlapping
4873 values that we moved into the pseudos in tmp_regs. */
4874 if (partial > 0 && reg != 0)
4876 /* Handle calls that pass values in multiple non-contiguous locations.
4877 The Irix 6 ABI has examples of this. */
4878 if (GET_CODE (reg) == PARALLEL)
4879 emit_group_load (reg, x, type, -1);
4880 else
4882 gcc_assert (partial % UNITS_PER_WORD == 0);
4883 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4885 for (int i = 0; i < overlapping; i++)
4886 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4887 + nregs - overlapping + i),
4888 tmp_regs[i]);
4893 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4894 anti_adjust_stack (gen_int_mode (extra, Pmode));
4896 if (alignment_pad && args_addr == 0)
4897 anti_adjust_stack (alignment_pad);
4899 return true;
4902 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4903 operations. */
4905 static rtx
4906 get_subtarget (rtx x)
4908 return (optimize
4909 || x == 0
4910 /* Only registers can be subtargets. */
4911 || !REG_P (x)
4912 /* Don't use hard regs to avoid extending their life. */
4913 || REGNO (x) < FIRST_PSEUDO_REGISTER
4914 ? 0 : x);
4917 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4918 FIELD is a bitfield. Returns true if the optimization was successful,
4919 and there's nothing else to do. */
4921 static bool
4922 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4923 poly_uint64 pbitpos,
4924 poly_uint64 pbitregion_start,
4925 poly_uint64 pbitregion_end,
4926 machine_mode mode1, rtx str_rtx,
4927 tree to, tree src, bool reverse)
4929 /* str_mode is not guaranteed to be a scalar type. */
4930 machine_mode str_mode = GET_MODE (str_rtx);
4931 unsigned int str_bitsize;
4932 tree op0, op1;
4933 rtx value, result;
4934 optab binop;
4935 gimple *srcstmt;
4936 enum tree_code code;
4938 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4939 if (mode1 != VOIDmode
4940 || !pbitsize.is_constant (&bitsize)
4941 || !pbitpos.is_constant (&bitpos)
4942 || !pbitregion_start.is_constant (&bitregion_start)
4943 || !pbitregion_end.is_constant (&bitregion_end)
4944 || bitsize >= BITS_PER_WORD
4945 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
4946 || str_bitsize > BITS_PER_WORD
4947 || TREE_SIDE_EFFECTS (to)
4948 || TREE_THIS_VOLATILE (to))
4949 return false;
4951 STRIP_NOPS (src);
4952 if (TREE_CODE (src) != SSA_NAME)
4953 return false;
4954 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4955 return false;
4957 srcstmt = get_gimple_for_ssa_name (src);
4958 if (!srcstmt
4959 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4960 return false;
4962 code = gimple_assign_rhs_code (srcstmt);
4964 op0 = gimple_assign_rhs1 (srcstmt);
4966 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4967 to find its initialization. Hopefully the initialization will
4968 be from a bitfield load. */
4969 if (TREE_CODE (op0) == SSA_NAME)
4971 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4973 /* We want to eventually have OP0 be the same as TO, which
4974 should be a bitfield. */
4975 if (!op0stmt
4976 || !is_gimple_assign (op0stmt)
4977 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4978 return false;
4979 op0 = gimple_assign_rhs1 (op0stmt);
4982 op1 = gimple_assign_rhs2 (srcstmt);
4984 if (!operand_equal_p (to, op0, 0))
4985 return false;
4987 if (MEM_P (str_rtx))
4989 unsigned HOST_WIDE_INT offset1;
4991 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4992 str_bitsize = BITS_PER_WORD;
4994 scalar_int_mode best_mode;
4995 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4996 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4997 return false;
4998 str_mode = best_mode;
4999 str_bitsize = GET_MODE_BITSIZE (best_mode);
5001 offset1 = bitpos;
5002 bitpos %= str_bitsize;
5003 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5004 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5006 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5007 return false;
5009 /* If the bit field covers the whole REG/MEM, store_field
5010 will likely generate better code. */
5011 if (bitsize >= str_bitsize)
5012 return false;
5014 /* We can't handle fields split across multiple entities. */
5015 if (bitpos + bitsize > str_bitsize)
5016 return false;
5018 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5019 bitpos = str_bitsize - bitpos - bitsize;
5021 switch (code)
5023 case PLUS_EXPR:
5024 case MINUS_EXPR:
5025 /* For now, just optimize the case of the topmost bitfield
5026 where we don't need to do any masking and also
5027 1 bit bitfields where xor can be used.
5028 We might win by one instruction for the other bitfields
5029 too if insv/extv instructions aren't used, so that
5030 can be added later. */
5031 if ((reverse || bitpos + bitsize != str_bitsize)
5032 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5033 break;
5035 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5036 value = convert_modes (str_mode,
5037 TYPE_MODE (TREE_TYPE (op1)), value,
5038 TYPE_UNSIGNED (TREE_TYPE (op1)));
5040 /* We may be accessing data outside the field, which means
5041 we can alias adjacent data. */
5042 if (MEM_P (str_rtx))
5044 str_rtx = shallow_copy_rtx (str_rtx);
5045 set_mem_alias_set (str_rtx, 0);
5046 set_mem_expr (str_rtx, 0);
5049 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5051 value = expand_and (str_mode, value, const1_rtx, NULL);
5052 binop = xor_optab;
5054 else
5055 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5057 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5058 if (reverse)
5059 value = flip_storage_order (str_mode, value);
5060 result = expand_binop (str_mode, binop, str_rtx,
5061 value, str_rtx, 1, OPTAB_WIDEN);
5062 if (result != str_rtx)
5063 emit_move_insn (str_rtx, result);
5064 return true;
5066 case BIT_IOR_EXPR:
5067 case BIT_XOR_EXPR:
5068 if (TREE_CODE (op1) != INTEGER_CST)
5069 break;
5070 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5071 value = convert_modes (str_mode,
5072 TYPE_MODE (TREE_TYPE (op1)), value,
5073 TYPE_UNSIGNED (TREE_TYPE (op1)));
5075 /* We may be accessing data outside the field, which means
5076 we can alias adjacent data. */
5077 if (MEM_P (str_rtx))
5079 str_rtx = shallow_copy_rtx (str_rtx);
5080 set_mem_alias_set (str_rtx, 0);
5081 set_mem_expr (str_rtx, 0);
5084 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5085 if (bitpos + bitsize != str_bitsize)
5087 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5088 str_mode);
5089 value = expand_and (str_mode, value, mask, NULL_RTX);
5091 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5092 if (reverse)
5093 value = flip_storage_order (str_mode, value);
5094 result = expand_binop (str_mode, binop, str_rtx,
5095 value, str_rtx, 1, OPTAB_WIDEN);
5096 if (result != str_rtx)
5097 emit_move_insn (str_rtx, result);
5098 return true;
5100 default:
5101 break;
5104 return false;
5107 /* In the C++ memory model, consecutive bit fields in a structure are
5108 considered one memory location.
5110 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5111 returns the bit range of consecutive bits in which this COMPONENT_REF
5112 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5113 and *OFFSET may be adjusted in the process.
5115 If the access does not need to be restricted, 0 is returned in both
5116 *BITSTART and *BITEND. */
5118 void
5119 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5120 poly_int64_pod *bitpos, tree *offset)
5122 poly_int64 bitoffset;
5123 tree field, repr;
5125 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5127 field = TREE_OPERAND (exp, 1);
5128 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5129 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5130 need to limit the range we can access. */
5131 if (!repr)
5133 *bitstart = *bitend = 0;
5134 return;
5137 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5138 part of a larger bit field, then the representative does not serve any
5139 useful purpose. This can occur in Ada. */
5140 if (handled_component_p (TREE_OPERAND (exp, 0)))
5142 machine_mode rmode;
5143 poly_int64 rbitsize, rbitpos;
5144 tree roffset;
5145 int unsignedp, reversep, volatilep = 0;
5146 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5147 &roffset, &rmode, &unsignedp, &reversep,
5148 &volatilep);
5149 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5151 *bitstart = *bitend = 0;
5152 return;
5156 /* Compute the adjustment to bitpos from the offset of the field
5157 relative to the representative. DECL_FIELD_OFFSET of field and
5158 repr are the same by construction if they are not constants,
5159 see finish_bitfield_layout. */
5160 poly_uint64 field_offset, repr_offset;
5161 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5162 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5163 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5164 else
5165 bitoffset = 0;
5166 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5167 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5169 /* If the adjustment is larger than bitpos, we would have a negative bit
5170 position for the lower bound and this may wreak havoc later. Adjust
5171 offset and bitpos to make the lower bound non-negative in that case. */
5172 if (maybe_gt (bitoffset, *bitpos))
5174 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5175 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5177 *bitpos += adjust_bits;
5178 if (*offset == NULL_TREE)
5179 *offset = size_int (-adjust_bytes);
5180 else
5181 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5182 *bitstart = 0;
5184 else
5185 *bitstart = *bitpos - bitoffset;
5187 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5190 /* Returns true if BASE is a DECL that does not reside in memory and
5191 has non-BLKmode. DECL_RTL must not be a MEM; if
5192 DECL_RTL was not set yet, return false. */
5194 static inline bool
5195 non_mem_decl_p (tree base)
5197 if (!DECL_P (base)
5198 || TREE_ADDRESSABLE (base)
5199 || DECL_MODE (base) == BLKmode)
5200 return false;
5202 if (!DECL_RTL_SET_P (base))
5203 return false;
5205 return (!MEM_P (DECL_RTL (base)));
5208 /* Returns true if REF refers to an object that does not
5209 reside in memory and has non-BLKmode. */
5211 static inline bool
5212 mem_ref_refers_to_non_mem_p (tree ref)
5214 tree base;
5216 if (TREE_CODE (ref) == MEM_REF
5217 || TREE_CODE (ref) == TARGET_MEM_REF)
5219 tree addr = TREE_OPERAND (ref, 0);
5221 if (TREE_CODE (addr) != ADDR_EXPR)
5222 return false;
5224 base = TREE_OPERAND (addr, 0);
5226 else
5227 base = ref;
5229 return non_mem_decl_p (base);
5232 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5233 is true, try generating a nontemporal store. */
5235 void
5236 expand_assignment (tree to, tree from, bool nontemporal)
5238 rtx to_rtx = 0;
5239 rtx result;
5240 machine_mode mode;
5241 unsigned int align;
5242 enum insn_code icode;
5244 /* Don't crash if the lhs of the assignment was erroneous. */
5245 if (TREE_CODE (to) == ERROR_MARK)
5247 expand_normal (from);
5248 return;
5251 /* Optimize away no-op moves without side-effects. */
5252 if (operand_equal_p (to, from, 0))
5253 return;
5255 /* Handle misaligned stores. */
5256 mode = TYPE_MODE (TREE_TYPE (to));
5257 if ((TREE_CODE (to) == MEM_REF
5258 || TREE_CODE (to) == TARGET_MEM_REF
5259 || DECL_P (to))
5260 && mode != BLKmode
5261 && !mem_ref_refers_to_non_mem_p (to)
5262 && ((align = get_object_alignment (to))
5263 < GET_MODE_ALIGNMENT (mode))
5264 && (((icode = optab_handler (movmisalign_optab, mode))
5265 != CODE_FOR_nothing)
5266 || targetm.slow_unaligned_access (mode, align)))
5268 rtx reg, mem;
5270 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5271 /* Handle PARALLEL. */
5272 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5273 reg = force_not_mem (reg);
5274 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5275 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5276 reg = flip_storage_order (mode, reg);
5278 if (icode != CODE_FOR_nothing)
5280 class expand_operand ops[2];
5282 create_fixed_operand (&ops[0], mem);
5283 create_input_operand (&ops[1], reg, mode);
5284 /* The movmisalign<mode> pattern cannot fail, else the assignment
5285 would silently be omitted. */
5286 expand_insn (icode, 2, ops);
5288 else
5289 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5290 false);
5291 return;
5294 /* Assignment of a structure component needs special treatment
5295 if the structure component's rtx is not simply a MEM.
5296 Assignment of an array element at a constant index, and assignment of
5297 an array element in an unaligned packed structure field, has the same
5298 problem. Same for (partially) storing into a non-memory object. */
5299 if (handled_component_p (to)
5300 || (TREE_CODE (to) == MEM_REF
5301 && (REF_REVERSE_STORAGE_ORDER (to)
5302 || mem_ref_refers_to_non_mem_p (to)))
5303 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5305 machine_mode mode1;
5306 poly_int64 bitsize, bitpos;
5307 poly_uint64 bitregion_start = 0;
5308 poly_uint64 bitregion_end = 0;
5309 tree offset;
5310 int unsignedp, reversep, volatilep = 0;
5311 tree tem;
5313 push_temp_slots ();
5314 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5315 &unsignedp, &reversep, &volatilep);
5317 /* Make sure bitpos is not negative, it can wreak havoc later. */
5318 if (maybe_lt (bitpos, 0))
5320 gcc_assert (offset == NULL_TREE);
5321 offset = size_int (bits_to_bytes_round_down (bitpos));
5322 bitpos = num_trailing_bits (bitpos);
5325 if (TREE_CODE (to) == COMPONENT_REF
5326 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5327 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5328 /* The C++ memory model naturally applies to byte-aligned fields.
5329 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5330 BITSIZE are not byte-aligned, there is no need to limit the range
5331 we can access. This can occur with packed structures in Ada. */
5332 else if (maybe_gt (bitsize, 0)
5333 && multiple_p (bitsize, BITS_PER_UNIT)
5334 && multiple_p (bitpos, BITS_PER_UNIT))
5336 bitregion_start = bitpos;
5337 bitregion_end = bitpos + bitsize - 1;
5340 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5342 /* If the field has a mode, we want to access it in the
5343 field's mode, not the computed mode.
5344 If a MEM has VOIDmode (external with incomplete type),
5345 use BLKmode for it instead. */
5346 if (MEM_P (to_rtx))
5348 if (mode1 != VOIDmode)
5349 to_rtx = adjust_address (to_rtx, mode1, 0);
5350 else if (GET_MODE (to_rtx) == VOIDmode)
5351 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5354 if (offset != 0)
5356 machine_mode address_mode;
5357 rtx offset_rtx;
5359 if (!MEM_P (to_rtx))
5361 /* We can get constant negative offsets into arrays with broken
5362 user code. Translate this to a trap instead of ICEing. */
5363 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5364 expand_builtin_trap ();
5365 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5368 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5369 address_mode = get_address_mode (to_rtx);
5370 if (GET_MODE (offset_rtx) != address_mode)
5372 /* We cannot be sure that the RTL in offset_rtx is valid outside
5373 of a memory address context, so force it into a register
5374 before attempting to convert it to the desired mode. */
5375 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5376 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5379 /* If we have an expression in OFFSET_RTX and a non-zero
5380 byte offset in BITPOS, adding the byte offset before the
5381 OFFSET_RTX results in better intermediate code, which makes
5382 later rtl optimization passes perform better.
5384 We prefer intermediate code like this:
5386 r124:DI=r123:DI+0x18
5387 [r124:DI]=r121:DI
5389 ... instead of ...
5391 r124:DI=r123:DI+0x10
5392 [r124:DI+0x8]=r121:DI
5394 This is only done for aligned data values, as these can
5395 be expected to result in single move instructions. */
5396 poly_int64 bytepos;
5397 if (mode1 != VOIDmode
5398 && maybe_ne (bitpos, 0)
5399 && maybe_gt (bitsize, 0)
5400 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5401 && multiple_p (bitpos, bitsize)
5402 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5403 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5405 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5406 bitregion_start = 0;
5407 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5408 bitregion_end -= bitpos;
5409 bitpos = 0;
5412 to_rtx = offset_address (to_rtx, offset_rtx,
5413 highest_pow2_factor_for_target (to,
5414 offset));
5417 /* No action is needed if the target is not a memory and the field
5418 lies completely outside that target. This can occur if the source
5419 code contains an out-of-bounds access to a small array. */
5420 if (!MEM_P (to_rtx)
5421 && GET_MODE (to_rtx) != BLKmode
5422 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5424 expand_normal (from);
5425 result = NULL;
5427 /* Handle expand_expr of a complex value returning a CONCAT. */
5428 else if (GET_CODE (to_rtx) == CONCAT)
5430 machine_mode to_mode = GET_MODE (to_rtx);
5431 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5432 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5433 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5434 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5435 && known_eq (bitpos, 0)
5436 && known_eq (bitsize, mode_bitsize))
5437 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5438 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5439 && known_eq (bitsize, inner_bitsize)
5440 && (known_eq (bitpos, 0)
5441 || known_eq (bitpos, inner_bitsize)))
5442 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5443 false, nontemporal, reversep);
5444 else if (known_le (bitpos + bitsize, inner_bitsize))
5445 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5446 bitregion_start, bitregion_end,
5447 mode1, from, get_alias_set (to),
5448 nontemporal, reversep);
5449 else if (known_ge (bitpos, inner_bitsize))
5450 result = store_field (XEXP (to_rtx, 1), bitsize,
5451 bitpos - inner_bitsize,
5452 bitregion_start, bitregion_end,
5453 mode1, from, get_alias_set (to),
5454 nontemporal, reversep);
5455 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5457 result = expand_normal (from);
5458 if (GET_CODE (result) == CONCAT)
5460 to_mode = GET_MODE_INNER (to_mode);
5461 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5462 rtx from_real
5463 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5464 from_mode, 0);
5465 rtx from_imag
5466 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5467 from_mode, 0);
5468 if (!from_real || !from_imag)
5469 goto concat_store_slow;
5470 emit_move_insn (XEXP (to_rtx, 0), from_real);
5471 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5473 else
5475 machine_mode from_mode
5476 = GET_MODE (result) == VOIDmode
5477 ? TYPE_MODE (TREE_TYPE (from))
5478 : GET_MODE (result);
5479 rtx from_rtx;
5480 if (MEM_P (result))
5481 from_rtx = change_address (result, to_mode, NULL_RTX);
5482 else
5483 from_rtx
5484 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5485 if (from_rtx)
5487 emit_move_insn (XEXP (to_rtx, 0),
5488 read_complex_part (from_rtx, false));
5489 emit_move_insn (XEXP (to_rtx, 1),
5490 read_complex_part (from_rtx, true));
5492 else
5494 to_mode = GET_MODE_INNER (to_mode);
5495 rtx from_real
5496 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5497 rtx from_imag
5498 = simplify_gen_subreg (to_mode, result, from_mode,
5499 GET_MODE_SIZE (to_mode));
5500 if (!from_real || !from_imag)
5501 goto concat_store_slow;
5502 emit_move_insn (XEXP (to_rtx, 0), from_real);
5503 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5507 else
5509 concat_store_slow:;
5510 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5511 GET_MODE_SIZE (GET_MODE (to_rtx)));
5512 write_complex_part (temp, XEXP (to_rtx, 0), false);
5513 write_complex_part (temp, XEXP (to_rtx, 1), true);
5514 result = store_field (temp, bitsize, bitpos,
5515 bitregion_start, bitregion_end,
5516 mode1, from, get_alias_set (to),
5517 nontemporal, reversep);
5518 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5519 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5522 /* For calls to functions returning variable length structures, if TO_RTX
5523 is not a MEM, go through a MEM because we must not create temporaries
5524 of the VLA type. */
5525 else if (!MEM_P (to_rtx)
5526 && TREE_CODE (from) == CALL_EXPR
5527 && COMPLETE_TYPE_P (TREE_TYPE (from))
5528 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5530 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5531 GET_MODE_SIZE (GET_MODE (to_rtx)));
5532 result = store_field (temp, bitsize, bitpos, bitregion_start,
5533 bitregion_end, mode1, from, get_alias_set (to),
5534 nontemporal, reversep);
5535 emit_move_insn (to_rtx, temp);
5537 else
5539 if (MEM_P (to_rtx))
5541 /* If the field is at offset zero, we could have been given the
5542 DECL_RTX of the parent struct. Don't munge it. */
5543 to_rtx = shallow_copy_rtx (to_rtx);
5544 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5545 if (volatilep)
5546 MEM_VOLATILE_P (to_rtx) = 1;
5549 gcc_checking_assert (known_ge (bitpos, 0));
5550 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5551 bitregion_start, bitregion_end,
5552 mode1, to_rtx, to, from,
5553 reversep))
5554 result = NULL;
5555 else if (SUBREG_P (to_rtx)
5556 && SUBREG_PROMOTED_VAR_P (to_rtx))
5558 /* If to_rtx is a promoted subreg, we need to zero or sign
5559 extend the value afterwards. */
5560 if (TREE_CODE (to) == MEM_REF
5561 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5562 && !REF_REVERSE_STORAGE_ORDER (to)
5563 && known_eq (bitpos, 0)
5564 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5565 result = store_expr (from, to_rtx, 0, nontemporal, false);
5566 else
5568 rtx to_rtx1
5569 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5570 SUBREG_REG (to_rtx),
5571 subreg_promoted_mode (to_rtx));
5572 result = store_field (to_rtx1, bitsize, bitpos,
5573 bitregion_start, bitregion_end,
5574 mode1, from, get_alias_set (to),
5575 nontemporal, reversep);
5576 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5577 SUBREG_PROMOTED_SIGN (to_rtx));
5580 else
5581 result = store_field (to_rtx, bitsize, bitpos,
5582 bitregion_start, bitregion_end,
5583 mode1, from, get_alias_set (to),
5584 nontemporal, reversep);
5587 if (result)
5588 preserve_temp_slots (result);
5589 pop_temp_slots ();
5590 return;
5593 /* If the rhs is a function call and its value is not an aggregate,
5594 call the function before we start to compute the lhs.
5595 This is needed for correct code for cases such as
5596 val = setjmp (buf) on machines where reference to val
5597 requires loading up part of an address in a separate insn.
5599 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5600 since it might be a promoted variable where the zero- or sign- extension
5601 needs to be done. Handling this in the normal way is safe because no
5602 computation is done before the call. The same is true for SSA names. */
5603 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5604 && COMPLETE_TYPE_P (TREE_TYPE (from))
5605 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5606 && ! (((VAR_P (to)
5607 || TREE_CODE (to) == PARM_DECL
5608 || TREE_CODE (to) == RESULT_DECL)
5609 && REG_P (DECL_RTL (to)))
5610 || TREE_CODE (to) == SSA_NAME))
5612 rtx value;
5614 push_temp_slots ();
5615 value = expand_normal (from);
5617 if (to_rtx == 0)
5618 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5620 /* Handle calls that return values in multiple non-contiguous locations.
5621 The Irix 6 ABI has examples of this. */
5622 if (GET_CODE (to_rtx) == PARALLEL)
5624 if (GET_CODE (value) == PARALLEL)
5625 emit_group_move (to_rtx, value);
5626 else
5627 emit_group_load (to_rtx, value, TREE_TYPE (from),
5628 int_size_in_bytes (TREE_TYPE (from)));
5630 else if (GET_CODE (value) == PARALLEL)
5631 emit_group_store (to_rtx, value, TREE_TYPE (from),
5632 int_size_in_bytes (TREE_TYPE (from)));
5633 else if (GET_MODE (to_rtx) == BLKmode)
5635 /* Handle calls that return BLKmode values in registers. */
5636 if (REG_P (value))
5637 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5638 else
5639 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5641 else
5643 if (POINTER_TYPE_P (TREE_TYPE (to)))
5644 value = convert_memory_address_addr_space
5645 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5646 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5648 emit_move_insn (to_rtx, value);
5651 preserve_temp_slots (to_rtx);
5652 pop_temp_slots ();
5653 return;
5656 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5657 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5659 /* Don't move directly into a return register. */
5660 if (TREE_CODE (to) == RESULT_DECL
5661 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5663 rtx temp;
5665 push_temp_slots ();
5667 /* If the source is itself a return value, it still is in a pseudo at
5668 this point so we can move it back to the return register directly. */
5669 if (REG_P (to_rtx)
5670 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5671 && TREE_CODE (from) != CALL_EXPR)
5672 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5673 else
5674 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5676 /* Handle calls that return values in multiple non-contiguous locations.
5677 The Irix 6 ABI has examples of this. */
5678 if (GET_CODE (to_rtx) == PARALLEL)
5680 if (GET_CODE (temp) == PARALLEL)
5681 emit_group_move (to_rtx, temp);
5682 else
5683 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5684 int_size_in_bytes (TREE_TYPE (from)));
5686 else if (temp)
5687 emit_move_insn (to_rtx, temp);
5689 preserve_temp_slots (to_rtx);
5690 pop_temp_slots ();
5691 return;
5694 /* In case we are returning the contents of an object which overlaps
5695 the place the value is being stored, use a safe function when copying
5696 a value through a pointer into a structure value return block. */
5697 if (TREE_CODE (to) == RESULT_DECL
5698 && TREE_CODE (from) == INDIRECT_REF
5699 && ADDR_SPACE_GENERIC_P
5700 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5701 && refs_may_alias_p (to, from)
5702 && cfun->returns_struct
5703 && !cfun->returns_pcc_struct)
5705 rtx from_rtx, size;
5707 push_temp_slots ();
5708 size = expr_size (from);
5709 from_rtx = expand_normal (from);
5711 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5713 preserve_temp_slots (to_rtx);
5714 pop_temp_slots ();
5715 return;
5718 /* Compute FROM and store the value in the rtx we got. */
5720 push_temp_slots ();
5721 result = store_expr (from, to_rtx, 0, nontemporal, false);
5722 preserve_temp_slots (result);
5723 pop_temp_slots ();
5724 return;
5727 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5728 succeeded, false otherwise. */
5730 bool
5731 emit_storent_insn (rtx to, rtx from)
5733 class expand_operand ops[2];
5734 machine_mode mode = GET_MODE (to);
5735 enum insn_code code = optab_handler (storent_optab, mode);
5737 if (code == CODE_FOR_nothing)
5738 return false;
5740 create_fixed_operand (&ops[0], to);
5741 create_input_operand (&ops[1], from, mode);
5742 return maybe_expand_insn (code, 2, ops);
5745 /* Helper function for store_expr storing of STRING_CST. */
5747 static rtx
5748 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5749 scalar_int_mode mode)
5751 tree str = (tree) data;
5753 gcc_assert (offset >= 0);
5754 if (offset >= TREE_STRING_LENGTH (str))
5755 return const0_rtx;
5757 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5758 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5760 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5761 size_t l = TREE_STRING_LENGTH (str) - offset;
5762 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5763 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5764 return c_readstr (p, mode, false);
5767 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
5770 /* Generate code for computing expression EXP,
5771 and storing the value into TARGET.
5773 If the mode is BLKmode then we may return TARGET itself.
5774 It turns out that in BLKmode it doesn't cause a problem.
5775 because C has no operators that could combine two different
5776 assignments into the same BLKmode object with different values
5777 with no sequence point. Will other languages need this to
5778 be more thorough?
5780 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5781 stack, and block moves may need to be treated specially.
5783 If NONTEMPORAL is true, try using a nontemporal store instruction.
5785 If REVERSE is true, the store is to be done in reverse order. */
5788 store_expr (tree exp, rtx target, int call_param_p,
5789 bool nontemporal, bool reverse)
5791 rtx temp;
5792 rtx alt_rtl = NULL_RTX;
5793 location_t loc = curr_insn_location ();
5794 bool shortened_string_cst = false;
5796 if (VOID_TYPE_P (TREE_TYPE (exp)))
5798 /* C++ can generate ?: expressions with a throw expression in one
5799 branch and an rvalue in the other. Here, we resolve attempts to
5800 store the throw expression's nonexistent result. */
5801 gcc_assert (!call_param_p);
5802 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5803 return NULL_RTX;
5805 if (TREE_CODE (exp) == COMPOUND_EXPR)
5807 /* Perform first part of compound expression, then assign from second
5808 part. */
5809 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5810 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5811 return store_expr (TREE_OPERAND (exp, 1), target,
5812 call_param_p, nontemporal, reverse);
5814 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5816 /* For conditional expression, get safe form of the target. Then
5817 test the condition, doing the appropriate assignment on either
5818 side. This avoids the creation of unnecessary temporaries.
5819 For non-BLKmode, it is more efficient not to do this. */
5821 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5823 do_pending_stack_adjust ();
5824 NO_DEFER_POP;
5825 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5826 profile_probability::uninitialized ());
5827 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5828 nontemporal, reverse);
5829 emit_jump_insn (targetm.gen_jump (lab2));
5830 emit_barrier ();
5831 emit_label (lab1);
5832 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5833 nontemporal, reverse);
5834 emit_label (lab2);
5835 OK_DEFER_POP;
5837 return NULL_RTX;
5839 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5840 /* If this is a scalar in a register that is stored in a wider mode
5841 than the declared mode, compute the result into its declared mode
5842 and then convert to the wider mode. Our value is the computed
5843 expression. */
5845 rtx inner_target = 0;
5846 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5847 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5849 /* We can do the conversion inside EXP, which will often result
5850 in some optimizations. Do the conversion in two steps: first
5851 change the signedness, if needed, then the extend. But don't
5852 do this if the type of EXP is a subtype of something else
5853 since then the conversion might involve more than just
5854 converting modes. */
5855 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5856 && TREE_TYPE (TREE_TYPE (exp)) == 0
5857 && GET_MODE_PRECISION (outer_mode)
5858 == TYPE_PRECISION (TREE_TYPE (exp)))
5860 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5861 TYPE_UNSIGNED (TREE_TYPE (exp))))
5863 /* Some types, e.g. Fortran's logical*4, won't have a signed
5864 version, so use the mode instead. */
5865 tree ntype
5866 = (signed_or_unsigned_type_for
5867 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5868 if (ntype == NULL)
5869 ntype = lang_hooks.types.type_for_mode
5870 (TYPE_MODE (TREE_TYPE (exp)),
5871 SUBREG_PROMOTED_SIGN (target));
5873 exp = fold_convert_loc (loc, ntype, exp);
5876 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5877 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5878 exp);
5880 inner_target = SUBREG_REG (target);
5883 temp = expand_expr (exp, inner_target, VOIDmode,
5884 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5887 /* If TEMP is a VOIDmode constant, use convert_modes to make
5888 sure that we properly convert it. */
5889 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5891 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5892 temp, SUBREG_PROMOTED_SIGN (target));
5893 temp = convert_modes (inner_mode, outer_mode, temp,
5894 SUBREG_PROMOTED_SIGN (target));
5897 convert_move (SUBREG_REG (target), temp,
5898 SUBREG_PROMOTED_SIGN (target));
5900 return NULL_RTX;
5902 else if ((TREE_CODE (exp) == STRING_CST
5903 || (TREE_CODE (exp) == MEM_REF
5904 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5905 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5906 == STRING_CST
5907 && integer_zerop (TREE_OPERAND (exp, 1))))
5908 && !nontemporal && !call_param_p
5909 && MEM_P (target))
5911 /* Optimize initialization of an array with a STRING_CST. */
5912 HOST_WIDE_INT exp_len, str_copy_len;
5913 rtx dest_mem;
5914 tree str = TREE_CODE (exp) == STRING_CST
5915 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5917 exp_len = int_expr_size (exp);
5918 if (exp_len <= 0)
5919 goto normal_expr;
5921 if (TREE_STRING_LENGTH (str) <= 0)
5922 goto normal_expr;
5924 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
5925 MEM_ALIGN (target), false))
5927 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
5928 MEM_ALIGN (target), false, RETURN_BEGIN);
5929 return NULL_RTX;
5932 str_copy_len = TREE_STRING_LENGTH (str);
5933 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
5935 str_copy_len += STORE_MAX_PIECES - 1;
5936 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5938 if (str_copy_len >= exp_len)
5939 goto normal_expr;
5941 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
5942 (void *) str, MEM_ALIGN (target), false))
5943 goto normal_expr;
5945 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
5946 (void *) str, MEM_ALIGN (target), false,
5947 RETURN_END);
5948 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
5949 exp_len - str_copy_len),
5950 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
5951 return NULL_RTX;
5953 else
5955 rtx tmp_target;
5957 normal_expr:
5958 /* If we want to use a nontemporal or a reverse order store, force the
5959 value into a register first. */
5960 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5961 tree rexp = exp;
5962 if (TREE_CODE (exp) == STRING_CST
5963 && tmp_target == target
5964 && GET_MODE (target) == BLKmode
5965 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
5967 rtx size = expr_size (exp);
5968 if (CONST_INT_P (size)
5969 && size != const0_rtx
5970 && (UINTVAL (size)
5971 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
5973 /* If the STRING_CST has much larger array type than
5974 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
5975 it into the rodata section as the code later on will use
5976 memset zero for the remainder anyway. See PR95052. */
5977 tmp_target = NULL_RTX;
5978 rexp = copy_node (exp);
5979 tree index
5980 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
5981 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
5982 index);
5983 shortened_string_cst = true;
5986 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
5987 (call_param_p
5988 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5989 &alt_rtl, false);
5990 if (shortened_string_cst)
5992 gcc_assert (MEM_P (temp));
5993 temp = change_address (temp, BLKmode, NULL_RTX);
5997 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5998 the same as that of TARGET, adjust the constant. This is needed, for
5999 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6000 only a word-sized value. */
6001 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6002 && TREE_CODE (exp) != ERROR_MARK
6003 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6005 gcc_assert (!shortened_string_cst);
6006 if (GET_MODE_CLASS (GET_MODE (target))
6007 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6008 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6009 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6011 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6012 TYPE_MODE (TREE_TYPE (exp)), 0);
6013 if (t)
6014 temp = t;
6016 if (GET_MODE (temp) == VOIDmode)
6017 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6018 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6021 /* If value was not generated in the target, store it there.
6022 Convert the value to TARGET's type first if necessary and emit the
6023 pending incrementations that have been queued when expanding EXP.
6024 Note that we cannot emit the whole queue blindly because this will
6025 effectively disable the POST_INC optimization later.
6027 If TEMP and TARGET compare equal according to rtx_equal_p, but
6028 one or both of them are volatile memory refs, we have to distinguish
6029 two cases:
6030 - expand_expr has used TARGET. In this case, we must not generate
6031 another copy. This can be detected by TARGET being equal according
6032 to == .
6033 - expand_expr has not used TARGET - that means that the source just
6034 happens to have the same RTX form. Since temp will have been created
6035 by expand_expr, it will compare unequal according to == .
6036 We must generate a copy in this case, to reach the correct number
6037 of volatile memory references. */
6039 if ((! rtx_equal_p (temp, target)
6040 || (temp != target && (side_effects_p (temp)
6041 || side_effects_p (target))))
6042 && TREE_CODE (exp) != ERROR_MARK
6043 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6044 but TARGET is not valid memory reference, TEMP will differ
6045 from TARGET although it is really the same location. */
6046 && !(alt_rtl
6047 && rtx_equal_p (alt_rtl, target)
6048 && !side_effects_p (alt_rtl)
6049 && !side_effects_p (target))
6050 /* If there's nothing to copy, don't bother. Don't call
6051 expr_size unless necessary, because some front-ends (C++)
6052 expr_size-hook must not be given objects that are not
6053 supposed to be bit-copied or bit-initialized. */
6054 && expr_size (exp) != const0_rtx)
6056 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6058 gcc_assert (!shortened_string_cst);
6059 if (GET_MODE (target) == BLKmode)
6061 /* Handle calls that return BLKmode values in registers. */
6062 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6063 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6064 else
6065 store_bit_field (target,
6066 rtx_to_poly_int64 (expr_size (exp))
6067 * BITS_PER_UNIT,
6068 0, 0, 0, GET_MODE (temp), temp, reverse);
6070 else
6071 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6074 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6076 /* Handle copying a string constant into an array. The string
6077 constant may be shorter than the array. So copy just the string's
6078 actual length, and clear the rest. First get the size of the data
6079 type of the string, which is actually the size of the target. */
6080 rtx size = expr_size (exp);
6082 if (CONST_INT_P (size)
6083 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6084 emit_block_move (target, temp, size,
6085 (call_param_p
6086 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6087 else
6089 machine_mode pointer_mode
6090 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6091 machine_mode address_mode = get_address_mode (target);
6093 /* Compute the size of the data to copy from the string. */
6094 tree copy_size
6095 = size_binop_loc (loc, MIN_EXPR,
6096 make_tree (sizetype, size),
6097 size_int (TREE_STRING_LENGTH (exp)));
6098 rtx copy_size_rtx
6099 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6100 (call_param_p
6101 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6102 rtx_code_label *label = 0;
6104 /* Copy that much. */
6105 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6106 TYPE_UNSIGNED (sizetype));
6107 emit_block_move (target, temp, copy_size_rtx,
6108 (call_param_p
6109 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6111 /* Figure out how much is left in TARGET that we have to clear.
6112 Do all calculations in pointer_mode. */
6113 poly_int64 const_copy_size;
6114 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6116 size = plus_constant (address_mode, size, -const_copy_size);
6117 target = adjust_address (target, BLKmode, const_copy_size);
6119 else
6121 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6122 copy_size_rtx, NULL_RTX, 0,
6123 OPTAB_LIB_WIDEN);
6125 if (GET_MODE (copy_size_rtx) != address_mode)
6126 copy_size_rtx = convert_to_mode (address_mode,
6127 copy_size_rtx,
6128 TYPE_UNSIGNED (sizetype));
6130 target = offset_address (target, copy_size_rtx,
6131 highest_pow2_factor (copy_size));
6132 label = gen_label_rtx ();
6133 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6134 GET_MODE (size), 0, label);
6137 if (size != const0_rtx)
6138 clear_storage (target, size, BLOCK_OP_NORMAL);
6140 if (label)
6141 emit_label (label);
6144 else if (shortened_string_cst)
6145 gcc_unreachable ();
6146 /* Handle calls that return values in multiple non-contiguous locations.
6147 The Irix 6 ABI has examples of this. */
6148 else if (GET_CODE (target) == PARALLEL)
6150 if (GET_CODE (temp) == PARALLEL)
6151 emit_group_move (target, temp);
6152 else
6153 emit_group_load (target, temp, TREE_TYPE (exp),
6154 int_size_in_bytes (TREE_TYPE (exp)));
6156 else if (GET_CODE (temp) == PARALLEL)
6157 emit_group_store (target, temp, TREE_TYPE (exp),
6158 int_size_in_bytes (TREE_TYPE (exp)));
6159 else if (GET_MODE (temp) == BLKmode)
6160 emit_block_move (target, temp, expr_size (exp),
6161 (call_param_p
6162 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6163 /* If we emit a nontemporal store, there is nothing else to do. */
6164 else if (nontemporal && emit_storent_insn (target, temp))
6166 else
6168 if (reverse)
6169 temp = flip_storage_order (GET_MODE (target), temp);
6170 temp = force_operand (temp, target);
6171 if (temp != target)
6172 emit_move_insn (target, temp);
6175 else
6176 gcc_assert (!shortened_string_cst);
6178 return NULL_RTX;
6181 /* Return true if field F of structure TYPE is a flexible array. */
6183 static bool
6184 flexible_array_member_p (const_tree f, const_tree type)
6186 const_tree tf;
6188 tf = TREE_TYPE (f);
6189 return (DECL_CHAIN (f) == NULL
6190 && TREE_CODE (tf) == ARRAY_TYPE
6191 && TYPE_DOMAIN (tf)
6192 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6193 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6194 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6195 && int_size_in_bytes (type) >= 0);
6198 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6199 must have in order for it to completely initialize a value of type TYPE.
6200 Return -1 if the number isn't known.
6202 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6204 static HOST_WIDE_INT
6205 count_type_elements (const_tree type, bool for_ctor_p)
6207 switch (TREE_CODE (type))
6209 case ARRAY_TYPE:
6211 tree nelts;
6213 nelts = array_type_nelts (type);
6214 if (nelts && tree_fits_uhwi_p (nelts))
6216 unsigned HOST_WIDE_INT n;
6218 n = tree_to_uhwi (nelts) + 1;
6219 if (n == 0 || for_ctor_p)
6220 return n;
6221 else
6222 return n * count_type_elements (TREE_TYPE (type), false);
6224 return for_ctor_p ? -1 : 1;
6227 case RECORD_TYPE:
6229 unsigned HOST_WIDE_INT n;
6230 tree f;
6232 n = 0;
6233 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6234 if (TREE_CODE (f) == FIELD_DECL)
6236 if (!for_ctor_p)
6237 n += count_type_elements (TREE_TYPE (f), false);
6238 else if (!flexible_array_member_p (f, type))
6239 /* Don't count flexible arrays, which are not supposed
6240 to be initialized. */
6241 n += 1;
6244 return n;
6247 case UNION_TYPE:
6248 case QUAL_UNION_TYPE:
6250 tree f;
6251 HOST_WIDE_INT n, m;
6253 gcc_assert (!for_ctor_p);
6254 /* Estimate the number of scalars in each field and pick the
6255 maximum. Other estimates would do instead; the idea is simply
6256 to make sure that the estimate is not sensitive to the ordering
6257 of the fields. */
6258 n = 1;
6259 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6260 if (TREE_CODE (f) == FIELD_DECL)
6262 m = count_type_elements (TREE_TYPE (f), false);
6263 /* If the field doesn't span the whole union, add an extra
6264 scalar for the rest. */
6265 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6266 TYPE_SIZE (type)) != 1)
6267 m++;
6268 if (n < m)
6269 n = m;
6271 return n;
6274 case COMPLEX_TYPE:
6275 return 2;
6277 case VECTOR_TYPE:
6279 unsigned HOST_WIDE_INT nelts;
6280 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6281 return nelts;
6282 else
6283 return -1;
6286 case INTEGER_TYPE:
6287 case REAL_TYPE:
6288 case FIXED_POINT_TYPE:
6289 case ENUMERAL_TYPE:
6290 case BOOLEAN_TYPE:
6291 case POINTER_TYPE:
6292 case OFFSET_TYPE:
6293 case REFERENCE_TYPE:
6294 case NULLPTR_TYPE:
6295 return 1;
6297 case ERROR_MARK:
6298 return 0;
6300 case VOID_TYPE:
6301 case OPAQUE_TYPE:
6302 case METHOD_TYPE:
6303 case FUNCTION_TYPE:
6304 case LANG_TYPE:
6305 default:
6306 gcc_unreachable ();
6310 /* Helper for categorize_ctor_elements. Identical interface. */
6312 static bool
6313 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6314 HOST_WIDE_INT *p_unique_nz_elts,
6315 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6317 unsigned HOST_WIDE_INT idx;
6318 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6319 tree value, purpose, elt_type;
6321 /* Whether CTOR is a valid constant initializer, in accordance with what
6322 initializer_constant_valid_p does. If inferred from the constructor
6323 elements, true until proven otherwise. */
6324 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6325 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6327 nz_elts = 0;
6328 unique_nz_elts = 0;
6329 init_elts = 0;
6330 num_fields = 0;
6331 elt_type = NULL_TREE;
6333 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6335 HOST_WIDE_INT mult = 1;
6337 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6339 tree lo_index = TREE_OPERAND (purpose, 0);
6340 tree hi_index = TREE_OPERAND (purpose, 1);
6342 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6343 mult = (tree_to_uhwi (hi_index)
6344 - tree_to_uhwi (lo_index) + 1);
6346 num_fields += mult;
6347 elt_type = TREE_TYPE (value);
6349 switch (TREE_CODE (value))
6351 case CONSTRUCTOR:
6353 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6355 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6356 &ic, p_complete);
6358 nz_elts += mult * nz;
6359 unique_nz_elts += unz;
6360 init_elts += mult * ic;
6362 if (const_from_elts_p && const_p)
6363 const_p = const_elt_p;
6365 break;
6367 case INTEGER_CST:
6368 case REAL_CST:
6369 case FIXED_CST:
6370 if (!initializer_zerop (value))
6372 nz_elts += mult;
6373 unique_nz_elts++;
6375 init_elts += mult;
6376 break;
6378 case STRING_CST:
6379 nz_elts += mult * TREE_STRING_LENGTH (value);
6380 unique_nz_elts += TREE_STRING_LENGTH (value);
6381 init_elts += mult * TREE_STRING_LENGTH (value);
6382 break;
6384 case COMPLEX_CST:
6385 if (!initializer_zerop (TREE_REALPART (value)))
6387 nz_elts += mult;
6388 unique_nz_elts++;
6390 if (!initializer_zerop (TREE_IMAGPART (value)))
6392 nz_elts += mult;
6393 unique_nz_elts++;
6395 init_elts += 2 * mult;
6396 break;
6398 case VECTOR_CST:
6400 /* We can only construct constant-length vectors using
6401 CONSTRUCTOR. */
6402 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6403 for (unsigned int i = 0; i < nunits; ++i)
6405 tree v = VECTOR_CST_ELT (value, i);
6406 if (!initializer_zerop (v))
6408 nz_elts += mult;
6409 unique_nz_elts++;
6411 init_elts += mult;
6414 break;
6416 default:
6418 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6419 nz_elts += mult * tc;
6420 unique_nz_elts += tc;
6421 init_elts += mult * tc;
6423 if (const_from_elts_p && const_p)
6424 const_p
6425 = initializer_constant_valid_p (value,
6426 elt_type,
6427 TYPE_REVERSE_STORAGE_ORDER
6428 (TREE_TYPE (ctor)))
6429 != NULL_TREE;
6431 break;
6435 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6436 num_fields, elt_type))
6437 *p_complete = false;
6439 *p_nz_elts += nz_elts;
6440 *p_unique_nz_elts += unique_nz_elts;
6441 *p_init_elts += init_elts;
6443 return const_p;
6446 /* Examine CTOR to discover:
6447 * how many scalar fields are set to nonzero values,
6448 and place it in *P_NZ_ELTS;
6449 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6450 high - low + 1 (this can be useful for callers to determine ctors
6451 that could be cheaply initialized with - perhaps nested - loops
6452 compared to copied from huge read-only data),
6453 and place it in *P_UNIQUE_NZ_ELTS;
6454 * how many scalar fields in total are in CTOR,
6455 and place it in *P_ELT_COUNT.
6456 * whether the constructor is complete -- in the sense that every
6457 meaningful byte is explicitly given a value --
6458 and place it in *P_COMPLETE.
6460 Return whether or not CTOR is a valid static constant initializer, the same
6461 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6463 bool
6464 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6465 HOST_WIDE_INT *p_unique_nz_elts,
6466 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6468 *p_nz_elts = 0;
6469 *p_unique_nz_elts = 0;
6470 *p_init_elts = 0;
6471 *p_complete = true;
6473 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6474 p_init_elts, p_complete);
6477 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6478 of which had type LAST_TYPE. Each element was itself a complete
6479 initializer, in the sense that every meaningful byte was explicitly
6480 given a value. Return true if the same is true for the constructor
6481 as a whole. */
6483 bool
6484 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6485 const_tree last_type)
6487 if (TREE_CODE (type) == UNION_TYPE
6488 || TREE_CODE (type) == QUAL_UNION_TYPE)
6490 if (num_elts == 0)
6491 return false;
6493 gcc_assert (num_elts == 1 && last_type);
6495 /* ??? We could look at each element of the union, and find the
6496 largest element. Which would avoid comparing the size of the
6497 initialized element against any tail padding in the union.
6498 Doesn't seem worth the effort... */
6499 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6502 return count_type_elements (type, true) == num_elts;
6505 /* Return 1 if EXP contains mostly (3/4) zeros. */
6507 static int
6508 mostly_zeros_p (const_tree exp)
6510 if (TREE_CODE (exp) == CONSTRUCTOR)
6512 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6513 bool complete_p;
6515 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6516 &complete_p);
6517 return !complete_p || nz_elts < init_elts / 4;
6520 return initializer_zerop (exp);
6523 /* Return 1 if EXP contains all zeros. */
6525 static int
6526 all_zeros_p (const_tree exp)
6528 if (TREE_CODE (exp) == CONSTRUCTOR)
6530 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6531 bool complete_p;
6533 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6534 &complete_p);
6535 return nz_elts == 0;
6538 return initializer_zerop (exp);
6541 /* Helper function for store_constructor.
6542 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6543 CLEARED is as for store_constructor.
6544 ALIAS_SET is the alias set to use for any stores.
6545 If REVERSE is true, the store is to be done in reverse order.
6547 This provides a recursive shortcut back to store_constructor when it isn't
6548 necessary to go through store_field. This is so that we can pass through
6549 the cleared field to let store_constructor know that we may not have to
6550 clear a substructure if the outer structure has already been cleared. */
6552 static void
6553 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6554 poly_uint64 bitregion_start,
6555 poly_uint64 bitregion_end,
6556 machine_mode mode,
6557 tree exp, int cleared,
6558 alias_set_type alias_set, bool reverse)
6560 poly_int64 bytepos;
6561 poly_uint64 bytesize;
6562 if (TREE_CODE (exp) == CONSTRUCTOR
6563 /* We can only call store_constructor recursively if the size and
6564 bit position are on a byte boundary. */
6565 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6566 && maybe_ne (bitsize, 0U)
6567 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6568 /* If we have a nonzero bitpos for a register target, then we just
6569 let store_field do the bitfield handling. This is unlikely to
6570 generate unnecessary clear instructions anyways. */
6571 && (known_eq (bitpos, 0) || MEM_P (target)))
6573 if (MEM_P (target))
6575 machine_mode target_mode = GET_MODE (target);
6576 if (target_mode != BLKmode
6577 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6578 target_mode = BLKmode;
6579 target = adjust_address (target, target_mode, bytepos);
6583 /* Update the alias set, if required. */
6584 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6585 && MEM_ALIAS_SET (target) != 0)
6587 target = copy_rtx (target);
6588 set_mem_alias_set (target, alias_set);
6591 store_constructor (exp, target, cleared, bytesize, reverse);
6593 else
6594 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6595 exp, alias_set, false, reverse);
6599 /* Returns the number of FIELD_DECLs in TYPE. */
6601 static int
6602 fields_length (const_tree type)
6604 tree t = TYPE_FIELDS (type);
6605 int count = 0;
6607 for (; t; t = DECL_CHAIN (t))
6608 if (TREE_CODE (t) == FIELD_DECL)
6609 ++count;
6611 return count;
6615 /* Store the value of constructor EXP into the rtx TARGET.
6616 TARGET is either a REG or a MEM; we know it cannot conflict, since
6617 safe_from_p has been called.
6618 CLEARED is true if TARGET is known to have been zero'd.
6619 SIZE is the number of bytes of TARGET we are allowed to modify: this
6620 may not be the same as the size of EXP if we are assigning to a field
6621 which has been packed to exclude padding bits.
6622 If REVERSE is true, the store is to be done in reverse order. */
6624 static void
6625 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6626 bool reverse)
6628 tree type = TREE_TYPE (exp);
6629 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6630 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6632 switch (TREE_CODE (type))
6634 case RECORD_TYPE:
6635 case UNION_TYPE:
6636 case QUAL_UNION_TYPE:
6638 unsigned HOST_WIDE_INT idx;
6639 tree field, value;
6641 /* The storage order is specified for every aggregate type. */
6642 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6644 /* If size is zero or the target is already cleared, do nothing. */
6645 if (known_eq (size, 0) || cleared)
6646 cleared = 1;
6647 /* We either clear the aggregate or indicate the value is dead. */
6648 else if ((TREE_CODE (type) == UNION_TYPE
6649 || TREE_CODE (type) == QUAL_UNION_TYPE)
6650 && ! CONSTRUCTOR_ELTS (exp))
6651 /* If the constructor is empty, clear the union. */
6653 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6654 cleared = 1;
6657 /* If we are building a static constructor into a register,
6658 set the initial value as zero so we can fold the value into
6659 a constant. But if more than one register is involved,
6660 this probably loses. */
6661 else if (REG_P (target) && TREE_STATIC (exp)
6662 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6663 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6665 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6666 cleared = 1;
6669 /* If the constructor has fewer fields than the structure or
6670 if we are initializing the structure to mostly zeros, clear
6671 the whole structure first. Don't do this if TARGET is a
6672 register whose mode size isn't equal to SIZE since
6673 clear_storage can't handle this case. */
6674 else if (known_size_p (size)
6675 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6676 || mostly_zeros_p (exp))
6677 && (!REG_P (target)
6678 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6680 clear_storage (target, gen_int_mode (size, Pmode),
6681 BLOCK_OP_NORMAL);
6682 cleared = 1;
6685 if (REG_P (target) && !cleared)
6686 emit_clobber (target);
6688 /* Store each element of the constructor into the
6689 corresponding field of TARGET. */
6690 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6692 machine_mode mode;
6693 HOST_WIDE_INT bitsize;
6694 HOST_WIDE_INT bitpos = 0;
6695 tree offset;
6696 rtx to_rtx = target;
6698 /* Just ignore missing fields. We cleared the whole
6699 structure, above, if any fields are missing. */
6700 if (field == 0)
6701 continue;
6703 if (cleared && initializer_zerop (value))
6704 continue;
6706 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6707 bitsize = tree_to_uhwi (DECL_SIZE (field));
6708 else
6709 gcc_unreachable ();
6711 mode = DECL_MODE (field);
6712 if (DECL_BIT_FIELD (field))
6713 mode = VOIDmode;
6715 offset = DECL_FIELD_OFFSET (field);
6716 if (tree_fits_shwi_p (offset)
6717 && tree_fits_shwi_p (bit_position (field)))
6719 bitpos = int_bit_position (field);
6720 offset = NULL_TREE;
6722 else
6723 gcc_unreachable ();
6725 /* If this initializes a field that is smaller than a
6726 word, at the start of a word, try to widen it to a full
6727 word. This special case allows us to output C++ member
6728 function initializations in a form that the optimizers
6729 can understand. */
6730 if (WORD_REGISTER_OPERATIONS
6731 && REG_P (target)
6732 && bitsize < BITS_PER_WORD
6733 && bitpos % BITS_PER_WORD == 0
6734 && GET_MODE_CLASS (mode) == MODE_INT
6735 && TREE_CODE (value) == INTEGER_CST
6736 && exp_size >= 0
6737 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6739 type = TREE_TYPE (value);
6741 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6743 type = lang_hooks.types.type_for_mode
6744 (word_mode, TYPE_UNSIGNED (type));
6745 value = fold_convert (type, value);
6746 /* Make sure the bits beyond the original bitsize are zero
6747 so that we can correctly avoid extra zeroing stores in
6748 later constructor elements. */
6749 tree bitsize_mask
6750 = wide_int_to_tree (type, wi::mask (bitsize, false,
6751 BITS_PER_WORD));
6752 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6755 if (BYTES_BIG_ENDIAN)
6756 value
6757 = fold_build2 (LSHIFT_EXPR, type, value,
6758 build_int_cst (type,
6759 BITS_PER_WORD - bitsize));
6760 bitsize = BITS_PER_WORD;
6761 mode = word_mode;
6764 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6765 && DECL_NONADDRESSABLE_P (field))
6767 to_rtx = copy_rtx (to_rtx);
6768 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6771 store_constructor_field (to_rtx, bitsize, bitpos,
6772 0, bitregion_end, mode,
6773 value, cleared,
6774 get_alias_set (TREE_TYPE (field)),
6775 reverse);
6777 break;
6779 case ARRAY_TYPE:
6781 tree value, index;
6782 unsigned HOST_WIDE_INT i;
6783 int need_to_clear;
6784 tree domain;
6785 tree elttype = TREE_TYPE (type);
6786 int const_bounds_p;
6787 HOST_WIDE_INT minelt = 0;
6788 HOST_WIDE_INT maxelt = 0;
6790 /* The storage order is specified for every aggregate type. */
6791 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6793 domain = TYPE_DOMAIN (type);
6794 const_bounds_p = (TYPE_MIN_VALUE (domain)
6795 && TYPE_MAX_VALUE (domain)
6796 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6797 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6799 /* If we have constant bounds for the range of the type, get them. */
6800 if (const_bounds_p)
6802 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6803 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6806 /* If the constructor has fewer elements than the array, clear
6807 the whole array first. Similarly if this is static
6808 constructor of a non-BLKmode object. */
6809 if (cleared)
6810 need_to_clear = 0;
6811 else if (REG_P (target) && TREE_STATIC (exp))
6812 need_to_clear = 1;
6813 else
6815 unsigned HOST_WIDE_INT idx;
6816 HOST_WIDE_INT count = 0, zero_count = 0;
6817 need_to_clear = ! const_bounds_p;
6819 /* This loop is a more accurate version of the loop in
6820 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6821 is also needed to check for missing elements. */
6822 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6824 HOST_WIDE_INT this_node_count;
6826 if (need_to_clear)
6827 break;
6829 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6831 tree lo_index = TREE_OPERAND (index, 0);
6832 tree hi_index = TREE_OPERAND (index, 1);
6834 if (! tree_fits_uhwi_p (lo_index)
6835 || ! tree_fits_uhwi_p (hi_index))
6837 need_to_clear = 1;
6838 break;
6841 this_node_count = (tree_to_uhwi (hi_index)
6842 - tree_to_uhwi (lo_index) + 1);
6844 else
6845 this_node_count = 1;
6847 count += this_node_count;
6848 if (mostly_zeros_p (value))
6849 zero_count += this_node_count;
6852 /* Clear the entire array first if there are any missing
6853 elements, or if the incidence of zero elements is >=
6854 75%. */
6855 if (! need_to_clear
6856 && (count < maxelt - minelt + 1
6857 || 4 * zero_count >= 3 * count))
6858 need_to_clear = 1;
6861 if (need_to_clear && maybe_gt (size, 0))
6863 if (REG_P (target))
6864 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6865 else
6866 clear_storage (target, gen_int_mode (size, Pmode),
6867 BLOCK_OP_NORMAL);
6868 cleared = 1;
6871 if (!cleared && REG_P (target))
6872 /* Inform later passes that the old value is dead. */
6873 emit_clobber (target);
6875 /* Store each element of the constructor into the
6876 corresponding element of TARGET, determined by counting the
6877 elements. */
6878 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6880 machine_mode mode;
6881 poly_int64 bitsize;
6882 HOST_WIDE_INT bitpos;
6883 rtx xtarget = target;
6885 if (cleared && initializer_zerop (value))
6886 continue;
6888 mode = TYPE_MODE (elttype);
6889 if (mode != BLKmode)
6890 bitsize = GET_MODE_BITSIZE (mode);
6891 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
6892 bitsize = -1;
6894 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6896 tree lo_index = TREE_OPERAND (index, 0);
6897 tree hi_index = TREE_OPERAND (index, 1);
6898 rtx index_r, pos_rtx;
6899 HOST_WIDE_INT lo, hi, count;
6900 tree position;
6902 /* If the range is constant and "small", unroll the loop. */
6903 if (const_bounds_p
6904 && tree_fits_shwi_p (lo_index)
6905 && tree_fits_shwi_p (hi_index)
6906 && (lo = tree_to_shwi (lo_index),
6907 hi = tree_to_shwi (hi_index),
6908 count = hi - lo + 1,
6909 (!MEM_P (target)
6910 || count <= 2
6911 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6912 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6913 <= 40 * 8)))))
6915 lo -= minelt; hi -= minelt;
6916 for (; lo <= hi; lo++)
6918 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6920 if (MEM_P (target)
6921 && !MEM_KEEP_ALIAS_SET_P (target)
6922 && TREE_CODE (type) == ARRAY_TYPE
6923 && TYPE_NONALIASED_COMPONENT (type))
6925 target = copy_rtx (target);
6926 MEM_KEEP_ALIAS_SET_P (target) = 1;
6929 store_constructor_field
6930 (target, bitsize, bitpos, 0, bitregion_end,
6931 mode, value, cleared,
6932 get_alias_set (elttype), reverse);
6935 else
6937 rtx_code_label *loop_start = gen_label_rtx ();
6938 rtx_code_label *loop_end = gen_label_rtx ();
6939 tree exit_cond;
6941 expand_normal (hi_index);
6943 index = build_decl (EXPR_LOCATION (exp),
6944 VAR_DECL, NULL_TREE, domain);
6945 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6946 SET_DECL_RTL (index, index_r);
6947 store_expr (lo_index, index_r, 0, false, reverse);
6949 /* Build the head of the loop. */
6950 do_pending_stack_adjust ();
6951 emit_label (loop_start);
6953 /* Assign value to element index. */
6954 position =
6955 fold_convert (ssizetype,
6956 fold_build2 (MINUS_EXPR,
6957 TREE_TYPE (index),
6958 index,
6959 TYPE_MIN_VALUE (domain)));
6961 position =
6962 size_binop (MULT_EXPR, position,
6963 fold_convert (ssizetype,
6964 TYPE_SIZE_UNIT (elttype)));
6966 pos_rtx = expand_normal (position);
6967 xtarget = offset_address (target, pos_rtx,
6968 highest_pow2_factor (position));
6969 xtarget = adjust_address (xtarget, mode, 0);
6970 if (TREE_CODE (value) == CONSTRUCTOR)
6971 store_constructor (value, xtarget, cleared,
6972 exact_div (bitsize, BITS_PER_UNIT),
6973 reverse);
6974 else
6975 store_expr (value, xtarget, 0, false, reverse);
6977 /* Generate a conditional jump to exit the loop. */
6978 exit_cond = build2 (LT_EXPR, integer_type_node,
6979 index, hi_index);
6980 jumpif (exit_cond, loop_end,
6981 profile_probability::uninitialized ());
6983 /* Update the loop counter, and jump to the head of
6984 the loop. */
6985 expand_assignment (index,
6986 build2 (PLUS_EXPR, TREE_TYPE (index),
6987 index, integer_one_node),
6988 false);
6990 emit_jump (loop_start);
6992 /* Build the end of the loop. */
6993 emit_label (loop_end);
6996 else if ((index != 0 && ! tree_fits_shwi_p (index))
6997 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6999 tree position;
7001 if (index == 0)
7002 index = ssize_int (1);
7004 if (minelt)
7005 index = fold_convert (ssizetype,
7006 fold_build2 (MINUS_EXPR,
7007 TREE_TYPE (index),
7008 index,
7009 TYPE_MIN_VALUE (domain)));
7011 position =
7012 size_binop (MULT_EXPR, index,
7013 fold_convert (ssizetype,
7014 TYPE_SIZE_UNIT (elttype)));
7015 xtarget = offset_address (target,
7016 expand_normal (position),
7017 highest_pow2_factor (position));
7018 xtarget = adjust_address (xtarget, mode, 0);
7019 store_expr (value, xtarget, 0, false, reverse);
7021 else
7023 if (index != 0)
7024 bitpos = ((tree_to_shwi (index) - minelt)
7025 * tree_to_uhwi (TYPE_SIZE (elttype)));
7026 else
7027 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7029 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7030 && TREE_CODE (type) == ARRAY_TYPE
7031 && TYPE_NONALIASED_COMPONENT (type))
7033 target = copy_rtx (target);
7034 MEM_KEEP_ALIAS_SET_P (target) = 1;
7036 store_constructor_field (target, bitsize, bitpos, 0,
7037 bitregion_end, mode, value,
7038 cleared, get_alias_set (elttype),
7039 reverse);
7042 break;
7045 case VECTOR_TYPE:
7047 unsigned HOST_WIDE_INT idx;
7048 constructor_elt *ce;
7049 int i;
7050 int need_to_clear;
7051 insn_code icode = CODE_FOR_nothing;
7052 tree elt;
7053 tree elttype = TREE_TYPE (type);
7054 int elt_size = vector_element_bits (type);
7055 machine_mode eltmode = TYPE_MODE (elttype);
7056 HOST_WIDE_INT bitsize;
7057 HOST_WIDE_INT bitpos;
7058 rtvec vector = NULL;
7059 poly_uint64 n_elts;
7060 unsigned HOST_WIDE_INT const_n_elts;
7061 alias_set_type alias;
7062 bool vec_vec_init_p = false;
7063 machine_mode mode = GET_MODE (target);
7065 gcc_assert (eltmode != BLKmode);
7067 /* Try using vec_duplicate_optab for uniform vectors. */
7068 if (!TREE_SIDE_EFFECTS (exp)
7069 && VECTOR_MODE_P (mode)
7070 && eltmode == GET_MODE_INNER (mode)
7071 && ((icode = optab_handler (vec_duplicate_optab, mode))
7072 != CODE_FOR_nothing)
7073 && (elt = uniform_vector_p (exp)))
7075 class expand_operand ops[2];
7076 create_output_operand (&ops[0], target, mode);
7077 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7078 expand_insn (icode, 2, ops);
7079 if (!rtx_equal_p (target, ops[0].value))
7080 emit_move_insn (target, ops[0].value);
7081 break;
7084 n_elts = TYPE_VECTOR_SUBPARTS (type);
7085 if (REG_P (target)
7086 && VECTOR_MODE_P (mode)
7087 && n_elts.is_constant (&const_n_elts))
7089 machine_mode emode = eltmode;
7090 bool vector_typed_elts_p = false;
7092 if (CONSTRUCTOR_NELTS (exp)
7093 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7094 == VECTOR_TYPE))
7096 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7097 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7098 * TYPE_VECTOR_SUBPARTS (etype),
7099 n_elts));
7100 emode = TYPE_MODE (etype);
7101 vector_typed_elts_p = true;
7103 icode = convert_optab_handler (vec_init_optab, mode, emode);
7104 if (icode != CODE_FOR_nothing)
7106 unsigned int n = const_n_elts;
7108 if (vector_typed_elts_p)
7110 n = CONSTRUCTOR_NELTS (exp);
7111 vec_vec_init_p = true;
7113 vector = rtvec_alloc (n);
7114 for (unsigned int k = 0; k < n; k++)
7115 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7119 /* Compute the size of the elements in the CTOR. It differs
7120 from the size of the vector type elements only when the
7121 CTOR elements are vectors themselves. */
7122 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7123 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7124 : elttype);
7125 if (VECTOR_TYPE_P (val_type))
7126 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7127 else
7128 bitsize = elt_size;
7130 /* If the constructor has fewer elements than the vector,
7131 clear the whole array first. Similarly if this is static
7132 constructor of a non-BLKmode object. */
7133 if (cleared)
7134 need_to_clear = 0;
7135 else if (REG_P (target) && TREE_STATIC (exp))
7136 need_to_clear = 1;
7137 else
7139 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7140 tree value;
7142 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7144 int n_elts_here = bitsize / elt_size;
7145 count += n_elts_here;
7146 if (mostly_zeros_p (value))
7147 zero_count += n_elts_here;
7150 /* Clear the entire vector first if there are any missing elements,
7151 or if the incidence of zero elements is >= 75%. */
7152 need_to_clear = (maybe_lt (count, n_elts)
7153 || 4 * zero_count >= 3 * count);
7156 if (need_to_clear && maybe_gt (size, 0) && !vector)
7158 if (REG_P (target))
7159 emit_move_insn (target, CONST0_RTX (mode));
7160 else
7161 clear_storage (target, gen_int_mode (size, Pmode),
7162 BLOCK_OP_NORMAL);
7163 cleared = 1;
7166 /* Inform later passes that the old value is dead. */
7167 if (!cleared && !vector && REG_P (target))
7168 emit_move_insn (target, CONST0_RTX (mode));
7170 if (MEM_P (target))
7171 alias = MEM_ALIAS_SET (target);
7172 else
7173 alias = get_alias_set (elttype);
7175 /* Store each element of the constructor into the corresponding
7176 element of TARGET, determined by counting the elements. */
7177 for (idx = 0, i = 0;
7178 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7179 idx++, i += bitsize / elt_size)
7181 HOST_WIDE_INT eltpos;
7182 tree value = ce->value;
7184 if (cleared && initializer_zerop (value))
7185 continue;
7187 if (ce->index)
7188 eltpos = tree_to_uhwi (ce->index);
7189 else
7190 eltpos = i;
7192 if (vector)
7194 if (vec_vec_init_p)
7196 gcc_assert (ce->index == NULL_TREE);
7197 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7198 eltpos = idx;
7200 else
7201 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7202 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7204 else
7206 machine_mode value_mode
7207 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7208 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7209 bitpos = eltpos * elt_size;
7210 store_constructor_field (target, bitsize, bitpos, 0,
7211 bitregion_end, value_mode,
7212 value, cleared, alias, reverse);
7216 if (vector)
7217 emit_insn (GEN_FCN (icode) (target,
7218 gen_rtx_PARALLEL (mode, vector)));
7219 break;
7222 default:
7223 gcc_unreachable ();
7227 /* Store the value of EXP (an expression tree)
7228 into a subfield of TARGET which has mode MODE and occupies
7229 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7230 If MODE is VOIDmode, it means that we are storing into a bit-field.
7232 BITREGION_START is bitpos of the first bitfield in this region.
7233 BITREGION_END is the bitpos of the ending bitfield in this region.
7234 These two fields are 0, if the C++ memory model does not apply,
7235 or we are not interested in keeping track of bitfield regions.
7237 Always return const0_rtx unless we have something particular to
7238 return.
7240 ALIAS_SET is the alias set for the destination. This value will
7241 (in general) be different from that for TARGET, since TARGET is a
7242 reference to the containing structure.
7244 If NONTEMPORAL is true, try generating a nontemporal store.
7246 If REVERSE is true, the store is to be done in reverse order. */
7248 static rtx
7249 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7250 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7251 machine_mode mode, tree exp,
7252 alias_set_type alias_set, bool nontemporal, bool reverse)
7254 if (TREE_CODE (exp) == ERROR_MARK)
7255 return const0_rtx;
7257 /* If we have nothing to store, do nothing unless the expression has
7258 side-effects. Don't do that for zero sized addressable lhs of
7259 calls. */
7260 if (known_eq (bitsize, 0)
7261 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7262 || TREE_CODE (exp) != CALL_EXPR))
7263 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7265 if (GET_CODE (target) == CONCAT)
7267 /* We're storing into a struct containing a single __complex. */
7269 gcc_assert (known_eq (bitpos, 0));
7270 return store_expr (exp, target, 0, nontemporal, reverse);
7273 /* If the structure is in a register or if the component
7274 is a bit field, we cannot use addressing to access it.
7275 Use bit-field techniques or SUBREG to store in it. */
7277 poly_int64 decl_bitsize;
7278 if (mode == VOIDmode
7279 || (mode != BLKmode && ! direct_store[(int) mode]
7280 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7281 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7282 || REG_P (target)
7283 || GET_CODE (target) == SUBREG
7284 /* If the field isn't aligned enough to store as an ordinary memref,
7285 store it as a bit field. */
7286 || (mode != BLKmode
7287 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7288 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7289 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7290 || !multiple_p (bitpos, BITS_PER_UNIT)))
7291 || (known_size_p (bitsize)
7292 && mode != BLKmode
7293 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7294 /* If the RHS and field are a constant size and the size of the
7295 RHS isn't the same size as the bitfield, we must use bitfield
7296 operations. */
7297 || (known_size_p (bitsize)
7298 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7299 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7300 bitsize)
7301 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7302 we will handle specially below. */
7303 && !(TREE_CODE (exp) == CONSTRUCTOR
7304 && multiple_p (bitsize, BITS_PER_UNIT))
7305 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7306 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7307 includes some extra padding. store_expr / expand_expr will in
7308 that case call get_inner_reference that will have the bitsize
7309 we check here and thus the block move will not clobber the
7310 padding that shouldn't be clobbered. In the future we could
7311 replace the TREE_ADDRESSABLE check with a check that
7312 get_base_address needs to live in memory. */
7313 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7314 || TREE_CODE (exp) != COMPONENT_REF
7315 || !multiple_p (bitsize, BITS_PER_UNIT)
7316 || !multiple_p (bitpos, BITS_PER_UNIT)
7317 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7318 &decl_bitsize)
7319 || maybe_ne (decl_bitsize, bitsize))
7320 /* A call with an addressable return type and return-slot
7321 optimization must not need bitfield operations but we must
7322 pass down the original target. */
7323 && (TREE_CODE (exp) != CALL_EXPR
7324 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7325 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7326 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7327 decl we must use bitfield operations. */
7328 || (known_size_p (bitsize)
7329 && TREE_CODE (exp) == MEM_REF
7330 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7331 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7332 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7333 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7335 rtx temp;
7336 gimple *nop_def;
7338 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7339 implies a mask operation. If the precision is the same size as
7340 the field we're storing into, that mask is redundant. This is
7341 particularly common with bit field assignments generated by the
7342 C front end. */
7343 nop_def = get_def_for_expr (exp, NOP_EXPR);
7344 if (nop_def)
7346 tree type = TREE_TYPE (exp);
7347 if (INTEGRAL_TYPE_P (type)
7348 && maybe_ne (TYPE_PRECISION (type),
7349 GET_MODE_BITSIZE (TYPE_MODE (type)))
7350 && known_eq (bitsize, TYPE_PRECISION (type)))
7352 tree op = gimple_assign_rhs1 (nop_def);
7353 type = TREE_TYPE (op);
7354 if (INTEGRAL_TYPE_P (type)
7355 && known_ge (TYPE_PRECISION (type), bitsize))
7356 exp = op;
7360 temp = expand_normal (exp);
7362 /* We don't support variable-sized BLKmode bitfields, since our
7363 handling of BLKmode is bound up with the ability to break
7364 things into words. */
7365 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7367 /* Handle calls that return values in multiple non-contiguous locations.
7368 The Irix 6 ABI has examples of this. */
7369 if (GET_CODE (temp) == PARALLEL)
7371 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7372 machine_mode temp_mode = GET_MODE (temp);
7373 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7374 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7375 rtx temp_target = gen_reg_rtx (temp_mode);
7376 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7377 temp = temp_target;
7380 /* Handle calls that return BLKmode values in registers. */
7381 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7383 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7384 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7385 temp = temp_target;
7388 /* If the value has aggregate type and an integral mode then, if BITSIZE
7389 is narrower than this mode and this is for big-endian data, we first
7390 need to put the value into the low-order bits for store_bit_field,
7391 except when MODE is BLKmode and BITSIZE larger than the word size
7392 (see the handling of fields larger than a word in store_bit_field).
7393 Moreover, the field may be not aligned on a byte boundary; in this
7394 case, if it has reverse storage order, it needs to be accessed as a
7395 scalar field with reverse storage order and we must first put the
7396 value into target order. */
7397 scalar_int_mode temp_mode;
7398 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7399 && is_int_mode (GET_MODE (temp), &temp_mode))
7401 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7403 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7405 if (reverse)
7406 temp = flip_storage_order (temp_mode, temp);
7408 gcc_checking_assert (known_le (bitsize, size));
7409 if (maybe_lt (bitsize, size)
7410 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7411 /* Use of to_constant for BLKmode was checked above. */
7412 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7413 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7414 size - bitsize, NULL_RTX, 1);
7417 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7418 if (mode != VOIDmode && mode != BLKmode
7419 && mode != TYPE_MODE (TREE_TYPE (exp)))
7420 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7422 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7423 and BITPOS must be aligned on a byte boundary. If so, we simply do
7424 a block copy. Likewise for a BLKmode-like TARGET. */
7425 if (GET_MODE (temp) == BLKmode
7426 && (GET_MODE (target) == BLKmode
7427 || (MEM_P (target)
7428 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7429 && multiple_p (bitpos, BITS_PER_UNIT)
7430 && multiple_p (bitsize, BITS_PER_UNIT))))
7432 gcc_assert (MEM_P (target) && MEM_P (temp));
7433 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7434 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7436 target = adjust_address (target, VOIDmode, bytepos);
7437 emit_block_move (target, temp,
7438 gen_int_mode (bytesize, Pmode),
7439 BLOCK_OP_NORMAL);
7441 return const0_rtx;
7444 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7445 word size, we need to load the value (see again store_bit_field). */
7446 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7448 temp_mode = smallest_int_mode_for_size (bitsize);
7449 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7450 temp_mode, false, NULL);
7453 /* Store the value in the bitfield. */
7454 gcc_checking_assert (known_ge (bitpos, 0));
7455 store_bit_field (target, bitsize, bitpos,
7456 bitregion_start, bitregion_end,
7457 mode, temp, reverse);
7459 return const0_rtx;
7461 else
7463 /* Now build a reference to just the desired component. */
7464 rtx to_rtx = adjust_address (target, mode,
7465 exact_div (bitpos, BITS_PER_UNIT));
7467 if (to_rtx == target)
7468 to_rtx = copy_rtx (to_rtx);
7470 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7471 set_mem_alias_set (to_rtx, alias_set);
7473 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7474 into a target smaller than its type; handle that case now. */
7475 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7477 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7478 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7479 return to_rtx;
7482 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7486 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7487 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7488 codes and find the ultimate containing object, which we return.
7490 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7491 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7492 storage order of the field.
7493 If the position of the field is variable, we store a tree
7494 giving the variable offset (in units) in *POFFSET.
7495 This offset is in addition to the bit position.
7496 If the position is not variable, we store 0 in *POFFSET.
7498 If any of the extraction expressions is volatile,
7499 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7501 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7502 Otherwise, it is a mode that can be used to access the field.
7504 If the field describes a variable-sized object, *PMODE is set to
7505 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7506 this case, but the address of the object can be found. */
7508 tree
7509 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7510 poly_int64_pod *pbitpos, tree *poffset,
7511 machine_mode *pmode, int *punsignedp,
7512 int *preversep, int *pvolatilep)
7514 tree size_tree = 0;
7515 machine_mode mode = VOIDmode;
7516 bool blkmode_bitfield = false;
7517 tree offset = size_zero_node;
7518 poly_offset_int bit_offset = 0;
7520 /* First get the mode, signedness, storage order and size. We do this from
7521 just the outermost expression. */
7522 *pbitsize = -1;
7523 if (TREE_CODE (exp) == COMPONENT_REF)
7525 tree field = TREE_OPERAND (exp, 1);
7526 size_tree = DECL_SIZE (field);
7527 if (flag_strict_volatile_bitfields > 0
7528 && TREE_THIS_VOLATILE (exp)
7529 && DECL_BIT_FIELD_TYPE (field)
7530 && DECL_MODE (field) != BLKmode)
7531 /* Volatile bitfields should be accessed in the mode of the
7532 field's type, not the mode computed based on the bit
7533 size. */
7534 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7535 else if (!DECL_BIT_FIELD (field))
7537 mode = DECL_MODE (field);
7538 /* For vector fields re-check the target flags, as DECL_MODE
7539 could have been set with different target flags than
7540 the current function has. */
7541 if (mode == BLKmode
7542 && VECTOR_TYPE_P (TREE_TYPE (field))
7543 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7544 mode = TYPE_MODE (TREE_TYPE (field));
7546 else if (DECL_MODE (field) == BLKmode)
7547 blkmode_bitfield = true;
7549 *punsignedp = DECL_UNSIGNED (field);
7551 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7553 size_tree = TREE_OPERAND (exp, 1);
7554 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7555 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7557 /* For vector element types with the correct size of access or for
7558 vector typed accesses use the mode of the access type. */
7559 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7560 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7561 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7562 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7563 mode = TYPE_MODE (TREE_TYPE (exp));
7565 else
7567 mode = TYPE_MODE (TREE_TYPE (exp));
7568 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7570 if (mode == BLKmode)
7571 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7572 else
7573 *pbitsize = GET_MODE_BITSIZE (mode);
7576 if (size_tree != 0)
7578 if (! tree_fits_uhwi_p (size_tree))
7579 mode = BLKmode, *pbitsize = -1;
7580 else
7581 *pbitsize = tree_to_uhwi (size_tree);
7584 *preversep = reverse_storage_order_for_component_p (exp);
7586 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7587 and find the ultimate containing object. */
7588 while (1)
7590 switch (TREE_CODE (exp))
7592 case BIT_FIELD_REF:
7593 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7594 break;
7596 case COMPONENT_REF:
7598 tree field = TREE_OPERAND (exp, 1);
7599 tree this_offset = component_ref_field_offset (exp);
7601 /* If this field hasn't been filled in yet, don't go past it.
7602 This should only happen when folding expressions made during
7603 type construction. */
7604 if (this_offset == 0)
7605 break;
7607 offset = size_binop (PLUS_EXPR, offset, this_offset);
7608 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7610 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7612 break;
7614 case ARRAY_REF:
7615 case ARRAY_RANGE_REF:
7617 tree index = TREE_OPERAND (exp, 1);
7618 tree low_bound = array_ref_low_bound (exp);
7619 tree unit_size = array_ref_element_size (exp);
7621 /* We assume all arrays have sizes that are a multiple of a byte.
7622 First subtract the lower bound, if any, in the type of the
7623 index, then convert to sizetype and multiply by the size of
7624 the array element. */
7625 if (! integer_zerop (low_bound))
7626 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7627 index, low_bound);
7629 offset = size_binop (PLUS_EXPR, offset,
7630 size_binop (MULT_EXPR,
7631 fold_convert (sizetype, index),
7632 unit_size));
7634 break;
7636 case REALPART_EXPR:
7637 break;
7639 case IMAGPART_EXPR:
7640 bit_offset += *pbitsize;
7641 break;
7643 case VIEW_CONVERT_EXPR:
7644 break;
7646 case MEM_REF:
7647 /* Hand back the decl for MEM[&decl, off]. */
7648 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7650 tree off = TREE_OPERAND (exp, 1);
7651 if (!integer_zerop (off))
7653 poly_offset_int boff = mem_ref_offset (exp);
7654 boff <<= LOG2_BITS_PER_UNIT;
7655 bit_offset += boff;
7657 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7659 goto done;
7661 default:
7662 goto done;
7665 /* If any reference in the chain is volatile, the effect is volatile. */
7666 if (TREE_THIS_VOLATILE (exp))
7667 *pvolatilep = 1;
7669 exp = TREE_OPERAND (exp, 0);
7671 done:
7673 /* If OFFSET is constant, see if we can return the whole thing as a
7674 constant bit position. Make sure to handle overflow during
7675 this conversion. */
7676 if (poly_int_tree_p (offset))
7678 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7679 TYPE_PRECISION (sizetype));
7680 tem <<= LOG2_BITS_PER_UNIT;
7681 tem += bit_offset;
7682 if (tem.to_shwi (pbitpos))
7683 *poffset = offset = NULL_TREE;
7686 /* Otherwise, split it up. */
7687 if (offset)
7689 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7690 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7692 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7693 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7694 offset = size_binop (PLUS_EXPR, offset,
7695 build_int_cst (sizetype, bytes.force_shwi ()));
7698 *poffset = offset;
7701 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7702 if (mode == VOIDmode
7703 && blkmode_bitfield
7704 && multiple_p (*pbitpos, BITS_PER_UNIT)
7705 && multiple_p (*pbitsize, BITS_PER_UNIT))
7706 *pmode = BLKmode;
7707 else
7708 *pmode = mode;
7710 return exp;
7713 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7715 static unsigned HOST_WIDE_INT
7716 target_align (const_tree target)
7718 /* We might have a chain of nested references with intermediate misaligning
7719 bitfields components, so need to recurse to find out. */
7721 unsigned HOST_WIDE_INT this_align, outer_align;
7723 switch (TREE_CODE (target))
7725 case BIT_FIELD_REF:
7726 return 1;
7728 case COMPONENT_REF:
7729 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7730 outer_align = target_align (TREE_OPERAND (target, 0));
7731 return MIN (this_align, outer_align);
7733 case ARRAY_REF:
7734 case ARRAY_RANGE_REF:
7735 this_align = TYPE_ALIGN (TREE_TYPE (target));
7736 outer_align = target_align (TREE_OPERAND (target, 0));
7737 return MIN (this_align, outer_align);
7739 CASE_CONVERT:
7740 case NON_LVALUE_EXPR:
7741 case VIEW_CONVERT_EXPR:
7742 this_align = TYPE_ALIGN (TREE_TYPE (target));
7743 outer_align = target_align (TREE_OPERAND (target, 0));
7744 return MAX (this_align, outer_align);
7746 default:
7747 return TYPE_ALIGN (TREE_TYPE (target));
7752 /* Given an rtx VALUE that may contain additions and multiplications, return
7753 an equivalent value that just refers to a register, memory, or constant.
7754 This is done by generating instructions to perform the arithmetic and
7755 returning a pseudo-register containing the value.
7757 The returned value may be a REG, SUBREG, MEM or constant. */
7760 force_operand (rtx value, rtx target)
7762 rtx op1, op2;
7763 /* Use subtarget as the target for operand 0 of a binary operation. */
7764 rtx subtarget = get_subtarget (target);
7765 enum rtx_code code = GET_CODE (value);
7767 /* Check for subreg applied to an expression produced by loop optimizer. */
7768 if (code == SUBREG
7769 && !REG_P (SUBREG_REG (value))
7770 && !MEM_P (SUBREG_REG (value)))
7772 value
7773 = simplify_gen_subreg (GET_MODE (value),
7774 force_reg (GET_MODE (SUBREG_REG (value)),
7775 force_operand (SUBREG_REG (value),
7776 NULL_RTX)),
7777 GET_MODE (SUBREG_REG (value)),
7778 SUBREG_BYTE (value));
7779 code = GET_CODE (value);
7782 /* Check for a PIC address load. */
7783 if ((code == PLUS || code == MINUS)
7784 && XEXP (value, 0) == pic_offset_table_rtx
7785 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7786 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7787 || GET_CODE (XEXP (value, 1)) == CONST))
7789 if (!subtarget)
7790 subtarget = gen_reg_rtx (GET_MODE (value));
7791 emit_move_insn (subtarget, value);
7792 return subtarget;
7795 if (ARITHMETIC_P (value))
7797 op2 = XEXP (value, 1);
7798 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7799 subtarget = 0;
7800 if (code == MINUS && CONST_INT_P (op2))
7802 code = PLUS;
7803 op2 = negate_rtx (GET_MODE (value), op2);
7806 /* Check for an addition with OP2 a constant integer and our first
7807 operand a PLUS of a virtual register and something else. In that
7808 case, we want to emit the sum of the virtual register and the
7809 constant first and then add the other value. This allows virtual
7810 register instantiation to simply modify the constant rather than
7811 creating another one around this addition. */
7812 if (code == PLUS && CONST_INT_P (op2)
7813 && GET_CODE (XEXP (value, 0)) == PLUS
7814 && REG_P (XEXP (XEXP (value, 0), 0))
7815 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7816 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7818 rtx temp = expand_simple_binop (GET_MODE (value), code,
7819 XEXP (XEXP (value, 0), 0), op2,
7820 subtarget, 0, OPTAB_LIB_WIDEN);
7821 return expand_simple_binop (GET_MODE (value), code, temp,
7822 force_operand (XEXP (XEXP (value,
7823 0), 1), 0),
7824 target, 0, OPTAB_LIB_WIDEN);
7827 op1 = force_operand (XEXP (value, 0), subtarget);
7828 op2 = force_operand (op2, NULL_RTX);
7829 switch (code)
7831 case MULT:
7832 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7833 case DIV:
7834 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7835 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7836 target, 1, OPTAB_LIB_WIDEN);
7837 else
7838 return expand_divmod (0,
7839 FLOAT_MODE_P (GET_MODE (value))
7840 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7841 GET_MODE (value), op1, op2, target, 0);
7842 case MOD:
7843 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7844 target, 0);
7845 case UDIV:
7846 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7847 target, 1);
7848 case UMOD:
7849 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7850 target, 1);
7851 case ASHIFTRT:
7852 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7853 target, 0, OPTAB_LIB_WIDEN);
7854 default:
7855 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7856 target, 1, OPTAB_LIB_WIDEN);
7859 if (UNARY_P (value))
7861 if (!target)
7862 target = gen_reg_rtx (GET_MODE (value));
7863 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7864 switch (code)
7866 case ZERO_EXTEND:
7867 case SIGN_EXTEND:
7868 case TRUNCATE:
7869 case FLOAT_EXTEND:
7870 case FLOAT_TRUNCATE:
7871 convert_move (target, op1, code == ZERO_EXTEND);
7872 return target;
7874 case FIX:
7875 case UNSIGNED_FIX:
7876 expand_fix (target, op1, code == UNSIGNED_FIX);
7877 return target;
7879 case FLOAT:
7880 case UNSIGNED_FLOAT:
7881 expand_float (target, op1, code == UNSIGNED_FLOAT);
7882 return target;
7884 default:
7885 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7889 #ifdef INSN_SCHEDULING
7890 /* On machines that have insn scheduling, we want all memory reference to be
7891 explicit, so we need to deal with such paradoxical SUBREGs. */
7892 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7893 value
7894 = simplify_gen_subreg (GET_MODE (value),
7895 force_reg (GET_MODE (SUBREG_REG (value)),
7896 force_operand (SUBREG_REG (value),
7897 NULL_RTX)),
7898 GET_MODE (SUBREG_REG (value)),
7899 SUBREG_BYTE (value));
7900 #endif
7902 return value;
7905 /* Subroutine of expand_expr: return nonzero iff there is no way that
7906 EXP can reference X, which is being modified. TOP_P is nonzero if this
7907 call is going to be used to determine whether we need a temporary
7908 for EXP, as opposed to a recursive call to this function.
7910 It is always safe for this routine to return zero since it merely
7911 searches for optimization opportunities. */
7914 safe_from_p (const_rtx x, tree exp, int top_p)
7916 rtx exp_rtl = 0;
7917 int i, nops;
7919 if (x == 0
7920 /* If EXP has varying size, we MUST use a target since we currently
7921 have no way of allocating temporaries of variable size
7922 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7923 So we assume here that something at a higher level has prevented a
7924 clash. This is somewhat bogus, but the best we can do. Only
7925 do this when X is BLKmode and when we are at the top level. */
7926 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7927 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7928 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7929 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7930 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7931 != INTEGER_CST)
7932 && GET_MODE (x) == BLKmode)
7933 /* If X is in the outgoing argument area, it is always safe. */
7934 || (MEM_P (x)
7935 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7936 || (GET_CODE (XEXP (x, 0)) == PLUS
7937 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7938 return 1;
7940 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7941 find the underlying pseudo. */
7942 if (GET_CODE (x) == SUBREG)
7944 x = SUBREG_REG (x);
7945 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7946 return 0;
7949 /* Now look at our tree code and possibly recurse. */
7950 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7952 case tcc_declaration:
7953 exp_rtl = DECL_RTL_IF_SET (exp);
7954 break;
7956 case tcc_constant:
7957 return 1;
7959 case tcc_exceptional:
7960 if (TREE_CODE (exp) == TREE_LIST)
7962 while (1)
7964 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7965 return 0;
7966 exp = TREE_CHAIN (exp);
7967 if (!exp)
7968 return 1;
7969 if (TREE_CODE (exp) != TREE_LIST)
7970 return safe_from_p (x, exp, 0);
7973 else if (TREE_CODE (exp) == CONSTRUCTOR)
7975 constructor_elt *ce;
7976 unsigned HOST_WIDE_INT idx;
7978 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7979 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7980 || !safe_from_p (x, ce->value, 0))
7981 return 0;
7982 return 1;
7984 else if (TREE_CODE (exp) == ERROR_MARK)
7985 return 1; /* An already-visited SAVE_EXPR? */
7986 else
7987 return 0;
7989 case tcc_statement:
7990 /* The only case we look at here is the DECL_INITIAL inside a
7991 DECL_EXPR. */
7992 return (TREE_CODE (exp) != DECL_EXPR
7993 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7994 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7995 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7997 case tcc_binary:
7998 case tcc_comparison:
7999 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8000 return 0;
8001 /* Fall through. */
8003 case tcc_unary:
8004 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8006 case tcc_expression:
8007 case tcc_reference:
8008 case tcc_vl_exp:
8009 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8010 the expression. If it is set, we conflict iff we are that rtx or
8011 both are in memory. Otherwise, we check all operands of the
8012 expression recursively. */
8014 switch (TREE_CODE (exp))
8016 case ADDR_EXPR:
8017 /* If the operand is static or we are static, we can't conflict.
8018 Likewise if we don't conflict with the operand at all. */
8019 if (staticp (TREE_OPERAND (exp, 0))
8020 || TREE_STATIC (exp)
8021 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8022 return 1;
8024 /* Otherwise, the only way this can conflict is if we are taking
8025 the address of a DECL a that address if part of X, which is
8026 very rare. */
8027 exp = TREE_OPERAND (exp, 0);
8028 if (DECL_P (exp))
8030 if (!DECL_RTL_SET_P (exp)
8031 || !MEM_P (DECL_RTL (exp)))
8032 return 0;
8033 else
8034 exp_rtl = XEXP (DECL_RTL (exp), 0);
8036 break;
8038 case MEM_REF:
8039 if (MEM_P (x)
8040 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8041 get_alias_set (exp)))
8042 return 0;
8043 break;
8045 case CALL_EXPR:
8046 /* Assume that the call will clobber all hard registers and
8047 all of memory. */
8048 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8049 || MEM_P (x))
8050 return 0;
8051 break;
8053 case WITH_CLEANUP_EXPR:
8054 case CLEANUP_POINT_EXPR:
8055 /* Lowered by gimplify.c. */
8056 gcc_unreachable ();
8058 case SAVE_EXPR:
8059 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8061 default:
8062 break;
8065 /* If we have an rtx, we do not need to scan our operands. */
8066 if (exp_rtl)
8067 break;
8069 nops = TREE_OPERAND_LENGTH (exp);
8070 for (i = 0; i < nops; i++)
8071 if (TREE_OPERAND (exp, i) != 0
8072 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8073 return 0;
8075 break;
8077 case tcc_type:
8078 /* Should never get a type here. */
8079 gcc_unreachable ();
8082 /* If we have an rtl, find any enclosed object. Then see if we conflict
8083 with it. */
8084 if (exp_rtl)
8086 if (GET_CODE (exp_rtl) == SUBREG)
8088 exp_rtl = SUBREG_REG (exp_rtl);
8089 if (REG_P (exp_rtl)
8090 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8091 return 0;
8094 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8095 are memory and they conflict. */
8096 return ! (rtx_equal_p (x, exp_rtl)
8097 || (MEM_P (x) && MEM_P (exp_rtl)
8098 && true_dependence (exp_rtl, VOIDmode, x)));
8101 /* If we reach here, it is safe. */
8102 return 1;
8106 /* Return the highest power of two that EXP is known to be a multiple of.
8107 This is used in updating alignment of MEMs in array references. */
8109 unsigned HOST_WIDE_INT
8110 highest_pow2_factor (const_tree exp)
8112 unsigned HOST_WIDE_INT ret;
8113 int trailing_zeros = tree_ctz (exp);
8114 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8115 return BIGGEST_ALIGNMENT;
8116 ret = HOST_WIDE_INT_1U << trailing_zeros;
8117 if (ret > BIGGEST_ALIGNMENT)
8118 return BIGGEST_ALIGNMENT;
8119 return ret;
8122 /* Similar, except that the alignment requirements of TARGET are
8123 taken into account. Assume it is at least as aligned as its
8124 type, unless it is a COMPONENT_REF in which case the layout of
8125 the structure gives the alignment. */
8127 static unsigned HOST_WIDE_INT
8128 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8130 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8131 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8133 return MAX (factor, talign);
8136 /* Convert the tree comparison code TCODE to the rtl one where the
8137 signedness is UNSIGNEDP. */
8139 static enum rtx_code
8140 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8142 enum rtx_code code;
8143 switch (tcode)
8145 case EQ_EXPR:
8146 code = EQ;
8147 break;
8148 case NE_EXPR:
8149 code = NE;
8150 break;
8151 case LT_EXPR:
8152 code = unsignedp ? LTU : LT;
8153 break;
8154 case LE_EXPR:
8155 code = unsignedp ? LEU : LE;
8156 break;
8157 case GT_EXPR:
8158 code = unsignedp ? GTU : GT;
8159 break;
8160 case GE_EXPR:
8161 code = unsignedp ? GEU : GE;
8162 break;
8163 case UNORDERED_EXPR:
8164 code = UNORDERED;
8165 break;
8166 case ORDERED_EXPR:
8167 code = ORDERED;
8168 break;
8169 case UNLT_EXPR:
8170 code = UNLT;
8171 break;
8172 case UNLE_EXPR:
8173 code = UNLE;
8174 break;
8175 case UNGT_EXPR:
8176 code = UNGT;
8177 break;
8178 case UNGE_EXPR:
8179 code = UNGE;
8180 break;
8181 case UNEQ_EXPR:
8182 code = UNEQ;
8183 break;
8184 case LTGT_EXPR:
8185 code = LTGT;
8186 break;
8188 default:
8189 gcc_unreachable ();
8191 return code;
8194 /* Subroutine of expand_expr. Expand the two operands of a binary
8195 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8196 The value may be stored in TARGET if TARGET is nonzero. The
8197 MODIFIER argument is as documented by expand_expr. */
8199 void
8200 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8201 enum expand_modifier modifier)
8203 if (! safe_from_p (target, exp1, 1))
8204 target = 0;
8205 if (operand_equal_p (exp0, exp1, 0))
8207 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8208 *op1 = copy_rtx (*op0);
8210 else
8212 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8213 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8218 /* Return a MEM that contains constant EXP. DEFER is as for
8219 output_constant_def and MODIFIER is as for expand_expr. */
8221 static rtx
8222 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8224 rtx mem;
8226 mem = output_constant_def (exp, defer);
8227 if (modifier != EXPAND_INITIALIZER)
8228 mem = use_anchored_address (mem);
8229 return mem;
8232 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8233 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8235 static rtx
8236 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8237 enum expand_modifier modifier, addr_space_t as)
8239 rtx result, subtarget;
8240 tree inner, offset;
8241 poly_int64 bitsize, bitpos;
8242 int unsignedp, reversep, volatilep = 0;
8243 machine_mode mode1;
8245 /* If we are taking the address of a constant and are at the top level,
8246 we have to use output_constant_def since we can't call force_const_mem
8247 at top level. */
8248 /* ??? This should be considered a front-end bug. We should not be
8249 generating ADDR_EXPR of something that isn't an LVALUE. The only
8250 exception here is STRING_CST. */
8251 if (CONSTANT_CLASS_P (exp))
8253 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8254 if (modifier < EXPAND_SUM)
8255 result = force_operand (result, target);
8256 return result;
8259 /* Everything must be something allowed by is_gimple_addressable. */
8260 switch (TREE_CODE (exp))
8262 case INDIRECT_REF:
8263 /* This case will happen via recursion for &a->b. */
8264 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8266 case MEM_REF:
8268 tree tem = TREE_OPERAND (exp, 0);
8269 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8270 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8271 return expand_expr (tem, target, tmode, modifier);
8274 case TARGET_MEM_REF:
8275 return addr_for_mem_ref (exp, as, true);
8277 case CONST_DECL:
8278 /* Expand the initializer like constants above. */
8279 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8280 0, modifier), 0);
8281 if (modifier < EXPAND_SUM)
8282 result = force_operand (result, target);
8283 return result;
8285 case REALPART_EXPR:
8286 /* The real part of the complex number is always first, therefore
8287 the address is the same as the address of the parent object. */
8288 offset = 0;
8289 bitpos = 0;
8290 inner = TREE_OPERAND (exp, 0);
8291 break;
8293 case IMAGPART_EXPR:
8294 /* The imaginary part of the complex number is always second.
8295 The expression is therefore always offset by the size of the
8296 scalar type. */
8297 offset = 0;
8298 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8299 inner = TREE_OPERAND (exp, 0);
8300 break;
8302 case COMPOUND_LITERAL_EXPR:
8303 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8304 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8305 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8306 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8307 the initializers aren't gimplified. */
8308 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8309 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8310 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8311 target, tmode, modifier, as);
8312 /* FALLTHRU */
8313 default:
8314 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8315 expand_expr, as that can have various side effects; LABEL_DECLs for
8316 example, may not have their DECL_RTL set yet. Expand the rtl of
8317 CONSTRUCTORs too, which should yield a memory reference for the
8318 constructor's contents. Assume language specific tree nodes can
8319 be expanded in some interesting way. */
8320 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8321 if (DECL_P (exp)
8322 || TREE_CODE (exp) == CONSTRUCTOR
8323 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8325 result = expand_expr (exp, target, tmode,
8326 modifier == EXPAND_INITIALIZER
8327 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8329 /* If the DECL isn't in memory, then the DECL wasn't properly
8330 marked TREE_ADDRESSABLE, which will be either a front-end
8331 or a tree optimizer bug. */
8333 gcc_assert (MEM_P (result));
8334 result = XEXP (result, 0);
8336 /* ??? Is this needed anymore? */
8337 if (DECL_P (exp))
8338 TREE_USED (exp) = 1;
8340 if (modifier != EXPAND_INITIALIZER
8341 && modifier != EXPAND_CONST_ADDRESS
8342 && modifier != EXPAND_SUM)
8343 result = force_operand (result, target);
8344 return result;
8347 /* Pass FALSE as the last argument to get_inner_reference although
8348 we are expanding to RTL. The rationale is that we know how to
8349 handle "aligning nodes" here: we can just bypass them because
8350 they won't change the final object whose address will be returned
8351 (they actually exist only for that purpose). */
8352 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8353 &unsignedp, &reversep, &volatilep);
8354 break;
8357 /* We must have made progress. */
8358 gcc_assert (inner != exp);
8360 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8361 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8362 inner alignment, force the inner to be sufficiently aligned. */
8363 if (CONSTANT_CLASS_P (inner)
8364 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8366 inner = copy_node (inner);
8367 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8368 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8369 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8371 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8373 if (offset)
8375 rtx tmp;
8377 if (modifier != EXPAND_NORMAL)
8378 result = force_operand (result, NULL);
8379 tmp = expand_expr (offset, NULL_RTX, tmode,
8380 modifier == EXPAND_INITIALIZER
8381 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8383 /* expand_expr is allowed to return an object in a mode other
8384 than TMODE. If it did, we need to convert. */
8385 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8386 tmp = convert_modes (tmode, GET_MODE (tmp),
8387 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8388 result = convert_memory_address_addr_space (tmode, result, as);
8389 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8391 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8392 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8393 else
8395 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8396 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8397 1, OPTAB_LIB_WIDEN);
8401 if (maybe_ne (bitpos, 0))
8403 /* Someone beforehand should have rejected taking the address
8404 of an object that isn't byte-aligned. */
8405 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8406 result = convert_memory_address_addr_space (tmode, result, as);
8407 result = plus_constant (tmode, result, bytepos);
8408 if (modifier < EXPAND_SUM)
8409 result = force_operand (result, target);
8412 return result;
8415 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8416 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8418 static rtx
8419 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8420 enum expand_modifier modifier)
8422 addr_space_t as = ADDR_SPACE_GENERIC;
8423 scalar_int_mode address_mode = Pmode;
8424 scalar_int_mode pointer_mode = ptr_mode;
8425 machine_mode rmode;
8426 rtx result;
8428 /* Target mode of VOIDmode says "whatever's natural". */
8429 if (tmode == VOIDmode)
8430 tmode = TYPE_MODE (TREE_TYPE (exp));
8432 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8434 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8435 address_mode = targetm.addr_space.address_mode (as);
8436 pointer_mode = targetm.addr_space.pointer_mode (as);
8439 /* We can get called with some Weird Things if the user does silliness
8440 like "(short) &a". In that case, convert_memory_address won't do
8441 the right thing, so ignore the given target mode. */
8442 scalar_int_mode new_tmode = (tmode == pointer_mode
8443 ? pointer_mode
8444 : address_mode);
8446 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8447 new_tmode, modifier, as);
8449 /* Despite expand_expr claims concerning ignoring TMODE when not
8450 strictly convenient, stuff breaks if we don't honor it. Note
8451 that combined with the above, we only do this for pointer modes. */
8452 rmode = GET_MODE (result);
8453 if (rmode == VOIDmode)
8454 rmode = new_tmode;
8455 if (rmode != new_tmode)
8456 result = convert_memory_address_addr_space (new_tmode, result, as);
8458 return result;
8461 /* Generate code for computing CONSTRUCTOR EXP.
8462 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8463 is TRUE, instead of creating a temporary variable in memory
8464 NULL is returned and the caller needs to handle it differently. */
8466 static rtx
8467 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8468 bool avoid_temp_mem)
8470 tree type = TREE_TYPE (exp);
8471 machine_mode mode = TYPE_MODE (type);
8473 /* Try to avoid creating a temporary at all. This is possible
8474 if all of the initializer is zero.
8475 FIXME: try to handle all [0..255] initializers we can handle
8476 with memset. */
8477 if (TREE_STATIC (exp)
8478 && !TREE_ADDRESSABLE (exp)
8479 && target != 0 && mode == BLKmode
8480 && all_zeros_p (exp))
8482 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8483 return target;
8486 /* All elts simple constants => refer to a constant in memory. But
8487 if this is a non-BLKmode mode, let it store a field at a time
8488 since that should make a CONST_INT, CONST_WIDE_INT or
8489 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8490 use, it is best to store directly into the target unless the type
8491 is large enough that memcpy will be used. If we are making an
8492 initializer and all operands are constant, put it in memory as
8493 well.
8495 FIXME: Avoid trying to fill vector constructors piece-meal.
8496 Output them with output_constant_def below unless we're sure
8497 they're zeros. This should go away when vector initializers
8498 are treated like VECTOR_CST instead of arrays. */
8499 if ((TREE_STATIC (exp)
8500 && ((mode == BLKmode
8501 && ! (target != 0 && safe_from_p (target, exp, 1)))
8502 || TREE_ADDRESSABLE (exp)
8503 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8504 && (! can_move_by_pieces
8505 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8506 TYPE_ALIGN (type)))
8507 && ! mostly_zeros_p (exp))))
8508 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8509 && TREE_CONSTANT (exp)))
8511 rtx constructor;
8513 if (avoid_temp_mem)
8514 return NULL_RTX;
8516 constructor = expand_expr_constant (exp, 1, modifier);
8518 if (modifier != EXPAND_CONST_ADDRESS
8519 && modifier != EXPAND_INITIALIZER
8520 && modifier != EXPAND_SUM)
8521 constructor = validize_mem (constructor);
8523 return constructor;
8526 /* If the CTOR is available in static storage and not mostly
8527 zeros and we can move it by pieces prefer to do so since
8528 that's usually more efficient than performing a series of
8529 stores from immediates. */
8530 if (avoid_temp_mem
8531 && TREE_STATIC (exp)
8532 && TREE_CONSTANT (exp)
8533 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8534 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8535 TYPE_ALIGN (type))
8536 && ! mostly_zeros_p (exp))
8537 return NULL_RTX;
8539 /* Handle calls that pass values in multiple non-contiguous
8540 locations. The Irix 6 ABI has examples of this. */
8541 if (target == 0 || ! safe_from_p (target, exp, 1)
8542 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8543 /* Also make a temporary if the store is to volatile memory, to
8544 avoid individual accesses to aggregate members. */
8545 || (GET_CODE (target) == MEM
8546 && MEM_VOLATILE_P (target)
8547 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8549 if (avoid_temp_mem)
8550 return NULL_RTX;
8552 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8555 store_constructor (exp, target, 0, int_expr_size (exp), false);
8556 return target;
8560 /* expand_expr: generate code for computing expression EXP.
8561 An rtx for the computed value is returned. The value is never null.
8562 In the case of a void EXP, const0_rtx is returned.
8564 The value may be stored in TARGET if TARGET is nonzero.
8565 TARGET is just a suggestion; callers must assume that
8566 the rtx returned may not be the same as TARGET.
8568 If TARGET is CONST0_RTX, it means that the value will be ignored.
8570 If TMODE is not VOIDmode, it suggests generating the
8571 result in mode TMODE. But this is done only when convenient.
8572 Otherwise, TMODE is ignored and the value generated in its natural mode.
8573 TMODE is just a suggestion; callers must assume that
8574 the rtx returned may not have mode TMODE.
8576 Note that TARGET may have neither TMODE nor MODE. In that case, it
8577 probably will not be used.
8579 If MODIFIER is EXPAND_SUM then when EXP is an addition
8580 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8581 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8582 products as above, or REG or MEM, or constant.
8583 Ordinarily in such cases we would output mul or add instructions
8584 and then return a pseudo reg containing the sum.
8586 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8587 it also marks a label as absolutely required (it can't be dead).
8588 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8589 This is used for outputting expressions used in initializers.
8591 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8592 with a constant address even if that address is not normally legitimate.
8593 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8595 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8596 a call parameter. Such targets require special care as we haven't yet
8597 marked TARGET so that it's safe from being trashed by libcalls. We
8598 don't want to use TARGET for anything but the final result;
8599 Intermediate values must go elsewhere. Additionally, calls to
8600 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8602 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8603 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8604 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8605 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8606 recursively.
8607 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8608 then *ALT_RTL is set to TARGET (before legitimziation).
8610 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8611 In this case, we don't adjust a returned MEM rtx that wouldn't be
8612 sufficiently aligned for its mode; instead, it's up to the caller
8613 to deal with it afterwards. This is used to make sure that unaligned
8614 base objects for which out-of-bounds accesses are supported, for
8615 example record types with trailing arrays, aren't realigned behind
8616 the back of the caller.
8617 The normal operating mode is to pass FALSE for this parameter. */
8620 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8621 enum expand_modifier modifier, rtx *alt_rtl,
8622 bool inner_reference_p)
8624 rtx ret;
8626 /* Handle ERROR_MARK before anybody tries to access its type. */
8627 if (TREE_CODE (exp) == ERROR_MARK
8628 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8630 ret = CONST0_RTX (tmode);
8631 return ret ? ret : const0_rtx;
8634 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8635 inner_reference_p);
8636 return ret;
8639 /* Try to expand the conditional expression which is represented by
8640 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8641 return the rtl reg which represents the result. Otherwise return
8642 NULL_RTX. */
8644 static rtx
8645 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8646 tree treeop1 ATTRIBUTE_UNUSED,
8647 tree treeop2 ATTRIBUTE_UNUSED)
8649 rtx insn;
8650 rtx op00, op01, op1, op2;
8651 enum rtx_code comparison_code;
8652 machine_mode comparison_mode;
8653 gimple *srcstmt;
8654 rtx temp;
8655 tree type = TREE_TYPE (treeop1);
8656 int unsignedp = TYPE_UNSIGNED (type);
8657 machine_mode mode = TYPE_MODE (type);
8658 machine_mode orig_mode = mode;
8659 static bool expanding_cond_expr_using_cmove = false;
8661 /* Conditional move expansion can end up TERing two operands which,
8662 when recursively hitting conditional expressions can result in
8663 exponential behavior if the cmove expansion ultimatively fails.
8664 It's hardly profitable to TER a cmove into a cmove so avoid doing
8665 that by failing early if we end up recursing. */
8666 if (expanding_cond_expr_using_cmove)
8667 return NULL_RTX;
8669 /* If we cannot do a conditional move on the mode, try doing it
8670 with the promoted mode. */
8671 if (!can_conditionally_move_p (mode))
8673 mode = promote_mode (type, mode, &unsignedp);
8674 if (!can_conditionally_move_p (mode))
8675 return NULL_RTX;
8676 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8678 else
8679 temp = assign_temp (type, 0, 1);
8681 expanding_cond_expr_using_cmove = true;
8682 start_sequence ();
8683 expand_operands (treeop1, treeop2,
8684 temp, &op1, &op2, EXPAND_NORMAL);
8686 if (TREE_CODE (treeop0) == SSA_NAME
8687 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8689 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8690 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8691 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8692 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8693 comparison_mode = TYPE_MODE (type);
8694 unsignedp = TYPE_UNSIGNED (type);
8695 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8697 else if (COMPARISON_CLASS_P (treeop0))
8699 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8700 enum tree_code cmpcode = TREE_CODE (treeop0);
8701 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8702 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8703 unsignedp = TYPE_UNSIGNED (type);
8704 comparison_mode = TYPE_MODE (type);
8705 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8707 else
8709 op00 = expand_normal (treeop0);
8710 op01 = const0_rtx;
8711 comparison_code = NE;
8712 comparison_mode = GET_MODE (op00);
8713 if (comparison_mode == VOIDmode)
8714 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8716 expanding_cond_expr_using_cmove = false;
8718 if (GET_MODE (op1) != mode)
8719 op1 = gen_lowpart (mode, op1);
8721 if (GET_MODE (op2) != mode)
8722 op2 = gen_lowpart (mode, op2);
8724 /* Try to emit the conditional move. */
8725 insn = emit_conditional_move (temp, comparison_code,
8726 op00, op01, comparison_mode,
8727 op1, op2, mode,
8728 unsignedp);
8730 /* If we could do the conditional move, emit the sequence,
8731 and return. */
8732 if (insn)
8734 rtx_insn *seq = get_insns ();
8735 end_sequence ();
8736 emit_insn (seq);
8737 return convert_modes (orig_mode, mode, temp, 0);
8740 /* Otherwise discard the sequence and fall back to code with
8741 branches. */
8742 end_sequence ();
8743 return NULL_RTX;
8746 /* A helper function for expand_expr_real_2 to be used with a
8747 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8748 is nonzero, with alignment ALIGN in bits.
8749 Store the value at TARGET if possible (if TARGET is nonzero).
8750 Regardless of TARGET, we return the rtx for where the value is placed.
8751 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8752 then *ALT_RTL is set to TARGET (before legitimziation). */
8754 static rtx
8755 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8756 unsigned int align, rtx target, rtx *alt_rtl)
8758 enum insn_code icode;
8760 if ((icode = optab_handler (movmisalign_optab, mode))
8761 != CODE_FOR_nothing)
8763 class expand_operand ops[2];
8765 /* We've already validated the memory, and we're creating a
8766 new pseudo destination. The predicates really can't fail,
8767 nor can the generator. */
8768 create_output_operand (&ops[0], NULL_RTX, mode);
8769 create_fixed_operand (&ops[1], temp);
8770 expand_insn (icode, 2, ops);
8771 temp = ops[0].value;
8773 else if (targetm.slow_unaligned_access (mode, align))
8774 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8775 0, unsignedp, target,
8776 mode, mode, false, alt_rtl);
8777 return temp;
8780 /* Helper function of expand_expr_2, expand a division or modulo.
8781 op0 and op1 should be already expanded treeop0 and treeop1, using
8782 expand_operands. */
8784 static rtx
8785 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8786 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8788 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8789 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8790 if (SCALAR_INT_MODE_P (mode)
8791 && optimize >= 2
8792 && get_range_pos_neg (treeop0) == 1
8793 && get_range_pos_neg (treeop1) == 1)
8795 /* If both arguments are known to be positive when interpreted
8796 as signed, we can expand it as both signed and unsigned
8797 division or modulo. Choose the cheaper sequence in that case. */
8798 bool speed_p = optimize_insn_for_speed_p ();
8799 do_pending_stack_adjust ();
8800 start_sequence ();
8801 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8802 rtx_insn *uns_insns = get_insns ();
8803 end_sequence ();
8804 start_sequence ();
8805 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8806 rtx_insn *sgn_insns = get_insns ();
8807 end_sequence ();
8808 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8809 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8811 /* If costs are the same then use as tie breaker the other other
8812 factor. */
8813 if (uns_cost == sgn_cost)
8815 uns_cost = seq_cost (uns_insns, !speed_p);
8816 sgn_cost = seq_cost (sgn_insns, !speed_p);
8819 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8821 emit_insn (uns_insns);
8822 return uns_ret;
8824 emit_insn (sgn_insns);
8825 return sgn_ret;
8827 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8831 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8832 enum expand_modifier modifier)
8834 rtx op0, op1, op2, temp;
8835 rtx_code_label *lab;
8836 tree type;
8837 int unsignedp;
8838 machine_mode mode;
8839 scalar_int_mode int_mode;
8840 enum tree_code code = ops->code;
8841 optab this_optab;
8842 rtx subtarget, original_target;
8843 int ignore;
8844 bool reduce_bit_field;
8845 location_t loc = ops->location;
8846 tree treeop0, treeop1, treeop2;
8847 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8848 ? reduce_to_bit_field_precision ((expr), \
8849 target, \
8850 type) \
8851 : (expr))
8853 type = ops->type;
8854 mode = TYPE_MODE (type);
8855 unsignedp = TYPE_UNSIGNED (type);
8857 treeop0 = ops->op0;
8858 treeop1 = ops->op1;
8859 treeop2 = ops->op2;
8861 /* We should be called only on simple (binary or unary) expressions,
8862 exactly those that are valid in gimple expressions that aren't
8863 GIMPLE_SINGLE_RHS (or invalid). */
8864 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8865 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8866 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8868 ignore = (target == const0_rtx
8869 || ((CONVERT_EXPR_CODE_P (code)
8870 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8871 && TREE_CODE (type) == VOID_TYPE));
8873 /* We should be called only if we need the result. */
8874 gcc_assert (!ignore);
8876 /* An operation in what may be a bit-field type needs the
8877 result to be reduced to the precision of the bit-field type,
8878 which is narrower than that of the type's mode. */
8879 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8880 && !type_has_mode_precision_p (type));
8882 if (reduce_bit_field
8883 && (modifier == EXPAND_STACK_PARM
8884 || (target && GET_MODE (target) != mode)))
8885 target = 0;
8887 /* Use subtarget as the target for operand 0 of a binary operation. */
8888 subtarget = get_subtarget (target);
8889 original_target = target;
8891 switch (code)
8893 case NON_LVALUE_EXPR:
8894 case PAREN_EXPR:
8895 CASE_CONVERT:
8896 if (treeop0 == error_mark_node)
8897 return const0_rtx;
8899 if (TREE_CODE (type) == UNION_TYPE)
8901 tree valtype = TREE_TYPE (treeop0);
8903 /* If both input and output are BLKmode, this conversion isn't doing
8904 anything except possibly changing memory attribute. */
8905 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8907 rtx result = expand_expr (treeop0, target, tmode,
8908 modifier);
8910 result = copy_rtx (result);
8911 set_mem_attributes (result, type, 0);
8912 return result;
8915 if (target == 0)
8917 if (TYPE_MODE (type) != BLKmode)
8918 target = gen_reg_rtx (TYPE_MODE (type));
8919 else
8920 target = assign_temp (type, 1, 1);
8923 if (MEM_P (target))
8924 /* Store data into beginning of memory target. */
8925 store_expr (treeop0,
8926 adjust_address (target, TYPE_MODE (valtype), 0),
8927 modifier == EXPAND_STACK_PARM,
8928 false, TYPE_REVERSE_STORAGE_ORDER (type));
8930 else
8932 gcc_assert (REG_P (target)
8933 && !TYPE_REVERSE_STORAGE_ORDER (type));
8935 /* Store this field into a union of the proper type. */
8936 poly_uint64 op0_size
8937 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
8938 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
8939 store_field (target,
8940 /* The conversion must be constructed so that
8941 we know at compile time how many bits
8942 to preserve. */
8943 ordered_min (op0_size, union_size),
8944 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8945 false, false);
8948 /* Return the entire union. */
8949 return target;
8952 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8954 op0 = expand_expr (treeop0, target, VOIDmode,
8955 modifier);
8957 /* If the signedness of the conversion differs and OP0 is
8958 a promoted SUBREG, clear that indication since we now
8959 have to do the proper extension. */
8960 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8961 && GET_CODE (op0) == SUBREG)
8962 SUBREG_PROMOTED_VAR_P (op0) = 0;
8964 return REDUCE_BIT_FIELD (op0);
8967 op0 = expand_expr (treeop0, NULL_RTX, mode,
8968 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8969 if (GET_MODE (op0) == mode)
8972 /* If OP0 is a constant, just convert it into the proper mode. */
8973 else if (CONSTANT_P (op0))
8975 tree inner_type = TREE_TYPE (treeop0);
8976 machine_mode inner_mode = GET_MODE (op0);
8978 if (inner_mode == VOIDmode)
8979 inner_mode = TYPE_MODE (inner_type);
8981 if (modifier == EXPAND_INITIALIZER)
8982 op0 = lowpart_subreg (mode, op0, inner_mode);
8983 else
8984 op0= convert_modes (mode, inner_mode, op0,
8985 TYPE_UNSIGNED (inner_type));
8988 else if (modifier == EXPAND_INITIALIZER)
8989 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8990 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8992 else if (target == 0)
8993 op0 = convert_to_mode (mode, op0,
8994 TYPE_UNSIGNED (TREE_TYPE
8995 (treeop0)));
8996 else
8998 convert_move (target, op0,
8999 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9000 op0 = target;
9003 return REDUCE_BIT_FIELD (op0);
9005 case ADDR_SPACE_CONVERT_EXPR:
9007 tree treeop0_type = TREE_TYPE (treeop0);
9009 gcc_assert (POINTER_TYPE_P (type));
9010 gcc_assert (POINTER_TYPE_P (treeop0_type));
9012 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9013 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9015 /* Conversions between pointers to the same address space should
9016 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9017 gcc_assert (as_to != as_from);
9019 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9021 /* Ask target code to handle conversion between pointers
9022 to overlapping address spaces. */
9023 if (targetm.addr_space.subset_p (as_to, as_from)
9024 || targetm.addr_space.subset_p (as_from, as_to))
9026 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9028 else
9030 /* For disjoint address spaces, converting anything but a null
9031 pointer invokes undefined behavior. We truncate or extend the
9032 value as if we'd converted via integers, which handles 0 as
9033 required, and all others as the programmer likely expects. */
9034 #ifndef POINTERS_EXTEND_UNSIGNED
9035 const int POINTERS_EXTEND_UNSIGNED = 1;
9036 #endif
9037 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9038 op0, POINTERS_EXTEND_UNSIGNED);
9040 gcc_assert (op0);
9041 return op0;
9044 case POINTER_PLUS_EXPR:
9045 /* Even though the sizetype mode and the pointer's mode can be different
9046 expand is able to handle this correctly and get the correct result out
9047 of the PLUS_EXPR code. */
9048 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9049 if sizetype precision is smaller than pointer precision. */
9050 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9051 treeop1 = fold_convert_loc (loc, type,
9052 fold_convert_loc (loc, ssizetype,
9053 treeop1));
9054 /* If sizetype precision is larger than pointer precision, truncate the
9055 offset to have matching modes. */
9056 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9057 treeop1 = fold_convert_loc (loc, type, treeop1);
9058 /* FALLTHRU */
9060 case PLUS_EXPR:
9061 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9062 something else, make sure we add the register to the constant and
9063 then to the other thing. This case can occur during strength
9064 reduction and doing it this way will produce better code if the
9065 frame pointer or argument pointer is eliminated.
9067 fold-const.c will ensure that the constant is always in the inner
9068 PLUS_EXPR, so the only case we need to do anything about is if
9069 sp, ap, or fp is our second argument, in which case we must swap
9070 the innermost first argument and our second argument. */
9072 if (TREE_CODE (treeop0) == PLUS_EXPR
9073 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9074 && VAR_P (treeop1)
9075 && (DECL_RTL (treeop1) == frame_pointer_rtx
9076 || DECL_RTL (treeop1) == stack_pointer_rtx
9077 || DECL_RTL (treeop1) == arg_pointer_rtx))
9079 gcc_unreachable ();
9082 /* If the result is to be ptr_mode and we are adding an integer to
9083 something, we might be forming a constant. So try to use
9084 plus_constant. If it produces a sum and we can't accept it,
9085 use force_operand. This allows P = &ARR[const] to generate
9086 efficient code on machines where a SYMBOL_REF is not a valid
9087 address.
9089 If this is an EXPAND_SUM call, always return the sum. */
9090 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9091 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9093 if (modifier == EXPAND_STACK_PARM)
9094 target = 0;
9095 if (TREE_CODE (treeop0) == INTEGER_CST
9096 && HWI_COMPUTABLE_MODE_P (mode)
9097 && TREE_CONSTANT (treeop1))
9099 rtx constant_part;
9100 HOST_WIDE_INT wc;
9101 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9103 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9104 EXPAND_SUM);
9105 /* Use wi::shwi to ensure that the constant is
9106 truncated according to the mode of OP1, then sign extended
9107 to a HOST_WIDE_INT. Using the constant directly can result
9108 in non-canonical RTL in a 64x32 cross compile. */
9109 wc = TREE_INT_CST_LOW (treeop0);
9110 constant_part =
9111 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9112 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9113 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9114 op1 = force_operand (op1, target);
9115 return REDUCE_BIT_FIELD (op1);
9118 else if (TREE_CODE (treeop1) == INTEGER_CST
9119 && HWI_COMPUTABLE_MODE_P (mode)
9120 && TREE_CONSTANT (treeop0))
9122 rtx constant_part;
9123 HOST_WIDE_INT wc;
9124 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9126 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9127 (modifier == EXPAND_INITIALIZER
9128 ? EXPAND_INITIALIZER : EXPAND_SUM));
9129 if (! CONSTANT_P (op0))
9131 op1 = expand_expr (treeop1, NULL_RTX,
9132 VOIDmode, modifier);
9133 /* Return a PLUS if modifier says it's OK. */
9134 if (modifier == EXPAND_SUM
9135 || modifier == EXPAND_INITIALIZER)
9136 return simplify_gen_binary (PLUS, mode, op0, op1);
9137 goto binop2;
9139 /* Use wi::shwi to ensure that the constant is
9140 truncated according to the mode of OP1, then sign extended
9141 to a HOST_WIDE_INT. Using the constant directly can result
9142 in non-canonical RTL in a 64x32 cross compile. */
9143 wc = TREE_INT_CST_LOW (treeop1);
9144 constant_part
9145 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9146 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9147 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9148 op0 = force_operand (op0, target);
9149 return REDUCE_BIT_FIELD (op0);
9153 /* Use TER to expand pointer addition of a negated value
9154 as pointer subtraction. */
9155 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9156 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9157 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9158 && TREE_CODE (treeop1) == SSA_NAME
9159 && TYPE_MODE (TREE_TYPE (treeop0))
9160 == TYPE_MODE (TREE_TYPE (treeop1)))
9162 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9163 if (def)
9165 treeop1 = gimple_assign_rhs1 (def);
9166 code = MINUS_EXPR;
9167 goto do_minus;
9171 /* No sense saving up arithmetic to be done
9172 if it's all in the wrong mode to form part of an address.
9173 And force_operand won't know whether to sign-extend or
9174 zero-extend. */
9175 if (modifier != EXPAND_INITIALIZER
9176 && (modifier != EXPAND_SUM || mode != ptr_mode))
9178 expand_operands (treeop0, treeop1,
9179 subtarget, &op0, &op1, modifier);
9180 if (op0 == const0_rtx)
9181 return op1;
9182 if (op1 == const0_rtx)
9183 return op0;
9184 goto binop2;
9187 expand_operands (treeop0, treeop1,
9188 subtarget, &op0, &op1, modifier);
9189 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9191 case MINUS_EXPR:
9192 case POINTER_DIFF_EXPR:
9193 do_minus:
9194 /* For initializers, we are allowed to return a MINUS of two
9195 symbolic constants. Here we handle all cases when both operands
9196 are constant. */
9197 /* Handle difference of two symbolic constants,
9198 for the sake of an initializer. */
9199 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9200 && really_constant_p (treeop0)
9201 && really_constant_p (treeop1))
9203 expand_operands (treeop0, treeop1,
9204 NULL_RTX, &op0, &op1, modifier);
9205 return simplify_gen_binary (MINUS, mode, op0, op1);
9208 /* No sense saving up arithmetic to be done
9209 if it's all in the wrong mode to form part of an address.
9210 And force_operand won't know whether to sign-extend or
9211 zero-extend. */
9212 if (modifier != EXPAND_INITIALIZER
9213 && (modifier != EXPAND_SUM || mode != ptr_mode))
9214 goto binop;
9216 expand_operands (treeop0, treeop1,
9217 subtarget, &op0, &op1, modifier);
9219 /* Convert A - const to A + (-const). */
9220 if (CONST_INT_P (op1))
9222 op1 = negate_rtx (mode, op1);
9223 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9226 goto binop2;
9228 case WIDEN_MULT_PLUS_EXPR:
9229 case WIDEN_MULT_MINUS_EXPR:
9230 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9231 op2 = expand_normal (treeop2);
9232 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9233 target, unsignedp);
9234 return target;
9236 case WIDEN_PLUS_EXPR:
9237 case WIDEN_MINUS_EXPR:
9238 case WIDEN_MULT_EXPR:
9239 /* If first operand is constant, swap them.
9240 Thus the following special case checks need only
9241 check the second operand. */
9242 if (TREE_CODE (treeop0) == INTEGER_CST)
9243 std::swap (treeop0, treeop1);
9245 /* First, check if we have a multiplication of one signed and one
9246 unsigned operand. */
9247 if (TREE_CODE (treeop1) != INTEGER_CST
9248 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9249 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9251 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9252 this_optab = usmul_widen_optab;
9253 if (find_widening_optab_handler (this_optab, mode, innermode)
9254 != CODE_FOR_nothing)
9256 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9257 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9258 EXPAND_NORMAL);
9259 else
9260 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9261 EXPAND_NORMAL);
9262 /* op0 and op1 might still be constant, despite the above
9263 != INTEGER_CST check. Handle it. */
9264 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9266 op0 = convert_modes (mode, innermode, op0, true);
9267 op1 = convert_modes (mode, innermode, op1, false);
9268 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9269 target, unsignedp));
9271 goto binop3;
9274 /* Check for a multiplication with matching signedness. */
9275 else if ((TREE_CODE (treeop1) == INTEGER_CST
9276 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9277 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9278 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9280 tree op0type = TREE_TYPE (treeop0);
9281 machine_mode innermode = TYPE_MODE (op0type);
9282 bool zextend_p = TYPE_UNSIGNED (op0type);
9283 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9284 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9286 if (TREE_CODE (treeop0) != INTEGER_CST)
9288 if (find_widening_optab_handler (this_optab, mode, innermode)
9289 != CODE_FOR_nothing)
9291 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9292 EXPAND_NORMAL);
9293 /* op0 and op1 might still be constant, despite the above
9294 != INTEGER_CST check. Handle it. */
9295 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9297 widen_mult_const:
9298 op0 = convert_modes (mode, innermode, op0, zextend_p);
9300 = convert_modes (mode, innermode, op1,
9301 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9302 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9303 target,
9304 unsignedp));
9306 temp = expand_widening_mult (mode, op0, op1, target,
9307 unsignedp, this_optab);
9308 return REDUCE_BIT_FIELD (temp);
9310 if (find_widening_optab_handler (other_optab, mode, innermode)
9311 != CODE_FOR_nothing
9312 && innermode == word_mode)
9314 rtx htem, hipart;
9315 op0 = expand_normal (treeop0);
9316 op1 = expand_normal (treeop1);
9317 /* op0 and op1 might be constants, despite the above
9318 != INTEGER_CST check. Handle it. */
9319 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9320 goto widen_mult_const;
9321 temp = expand_binop (mode, other_optab, op0, op1, target,
9322 unsignedp, OPTAB_LIB_WIDEN);
9323 hipart = gen_highpart (word_mode, temp);
9324 htem = expand_mult_highpart_adjust (word_mode, hipart,
9325 op0, op1, hipart,
9326 zextend_p);
9327 if (htem != hipart)
9328 emit_move_insn (hipart, htem);
9329 return REDUCE_BIT_FIELD (temp);
9333 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9334 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9335 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9336 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9338 case MULT_EXPR:
9339 /* If this is a fixed-point operation, then we cannot use the code
9340 below because "expand_mult" doesn't support sat/no-sat fixed-point
9341 multiplications. */
9342 if (ALL_FIXED_POINT_MODE_P (mode))
9343 goto binop;
9345 /* If first operand is constant, swap them.
9346 Thus the following special case checks need only
9347 check the second operand. */
9348 if (TREE_CODE (treeop0) == INTEGER_CST)
9349 std::swap (treeop0, treeop1);
9351 /* Attempt to return something suitable for generating an
9352 indexed address, for machines that support that. */
9354 if (modifier == EXPAND_SUM && mode == ptr_mode
9355 && tree_fits_shwi_p (treeop1))
9357 tree exp1 = treeop1;
9359 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9360 EXPAND_SUM);
9362 if (!REG_P (op0))
9363 op0 = force_operand (op0, NULL_RTX);
9364 if (!REG_P (op0))
9365 op0 = copy_to_mode_reg (mode, op0);
9367 op1 = gen_int_mode (tree_to_shwi (exp1),
9368 TYPE_MODE (TREE_TYPE (exp1)));
9369 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9372 if (modifier == EXPAND_STACK_PARM)
9373 target = 0;
9375 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9377 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9378 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9379 if (def_stmt0
9380 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9381 def_stmt0 = NULL;
9382 if (def_stmt1
9383 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9384 def_stmt1 = NULL;
9386 if (def_stmt0 || def_stmt1)
9388 /* X / Y * Y can be expanded as X - X % Y too.
9389 Choose the cheaper sequence of those two. */
9390 if (def_stmt0)
9391 treeop0 = gimple_assign_rhs1 (def_stmt0);
9392 else
9394 treeop1 = treeop0;
9395 treeop0 = gimple_assign_rhs1 (def_stmt1);
9397 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9398 EXPAND_NORMAL);
9399 bool speed_p = optimize_insn_for_speed_p ();
9400 do_pending_stack_adjust ();
9401 start_sequence ();
9402 rtx divmul_ret
9403 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9404 op0, op1, NULL_RTX, unsignedp);
9405 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9406 unsignedp);
9407 rtx_insn *divmul_insns = get_insns ();
9408 end_sequence ();
9409 start_sequence ();
9410 rtx modsub_ret
9411 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9412 op0, op1, NULL_RTX, unsignedp);
9413 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9414 optab_default);
9415 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9416 target, unsignedp, OPTAB_LIB_WIDEN);
9417 rtx_insn *modsub_insns = get_insns ();
9418 end_sequence ();
9419 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9420 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9421 /* If costs are the same then use as tie breaker the other other
9422 factor. */
9423 if (divmul_cost == modsub_cost)
9425 divmul_cost = seq_cost (divmul_insns, !speed_p);
9426 modsub_cost = seq_cost (modsub_insns, !speed_p);
9429 if (divmul_cost <= modsub_cost)
9431 emit_insn (divmul_insns);
9432 return REDUCE_BIT_FIELD (divmul_ret);
9434 emit_insn (modsub_insns);
9435 return REDUCE_BIT_FIELD (modsub_ret);
9439 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9440 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9442 case TRUNC_MOD_EXPR:
9443 case FLOOR_MOD_EXPR:
9444 case CEIL_MOD_EXPR:
9445 case ROUND_MOD_EXPR:
9447 case TRUNC_DIV_EXPR:
9448 case FLOOR_DIV_EXPR:
9449 case CEIL_DIV_EXPR:
9450 case ROUND_DIV_EXPR:
9451 case EXACT_DIV_EXPR:
9452 /* If this is a fixed-point operation, then we cannot use the code
9453 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9454 divisions. */
9455 if (ALL_FIXED_POINT_MODE_P (mode))
9456 goto binop;
9458 if (modifier == EXPAND_STACK_PARM)
9459 target = 0;
9460 /* Possible optimization: compute the dividend with EXPAND_SUM
9461 then if the divisor is constant can optimize the case
9462 where some terms of the dividend have coeffs divisible by it. */
9463 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9464 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9465 target, unsignedp);
9467 case RDIV_EXPR:
9468 goto binop;
9470 case MULT_HIGHPART_EXPR:
9471 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9472 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9473 gcc_assert (temp);
9474 return temp;
9476 case FIXED_CONVERT_EXPR:
9477 op0 = expand_normal (treeop0);
9478 if (target == 0 || modifier == EXPAND_STACK_PARM)
9479 target = gen_reg_rtx (mode);
9481 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9482 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9483 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9484 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9485 else
9486 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9487 return target;
9489 case FIX_TRUNC_EXPR:
9490 op0 = expand_normal (treeop0);
9491 if (target == 0 || modifier == EXPAND_STACK_PARM)
9492 target = gen_reg_rtx (mode);
9493 expand_fix (target, op0, unsignedp);
9494 return target;
9496 case FLOAT_EXPR:
9497 op0 = expand_normal (treeop0);
9498 if (target == 0 || modifier == EXPAND_STACK_PARM)
9499 target = gen_reg_rtx (mode);
9500 /* expand_float can't figure out what to do if FROM has VOIDmode.
9501 So give it the correct mode. With -O, cse will optimize this. */
9502 if (GET_MODE (op0) == VOIDmode)
9503 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9504 op0);
9505 expand_float (target, op0,
9506 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9507 return target;
9509 case NEGATE_EXPR:
9510 op0 = expand_expr (treeop0, subtarget,
9511 VOIDmode, EXPAND_NORMAL);
9512 if (modifier == EXPAND_STACK_PARM)
9513 target = 0;
9514 temp = expand_unop (mode,
9515 optab_for_tree_code (NEGATE_EXPR, type,
9516 optab_default),
9517 op0, target, 0);
9518 gcc_assert (temp);
9519 return REDUCE_BIT_FIELD (temp);
9521 case ABS_EXPR:
9522 case ABSU_EXPR:
9523 op0 = expand_expr (treeop0, subtarget,
9524 VOIDmode, EXPAND_NORMAL);
9525 if (modifier == EXPAND_STACK_PARM)
9526 target = 0;
9528 /* ABS_EXPR is not valid for complex arguments. */
9529 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9530 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9532 /* Unsigned abs is simply the operand. Testing here means we don't
9533 risk generating incorrect code below. */
9534 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9535 return op0;
9537 return expand_abs (mode, op0, target, unsignedp,
9538 safe_from_p (target, treeop0, 1));
9540 case MAX_EXPR:
9541 case MIN_EXPR:
9542 target = original_target;
9543 if (target == 0
9544 || modifier == EXPAND_STACK_PARM
9545 || (MEM_P (target) && MEM_VOLATILE_P (target))
9546 || GET_MODE (target) != mode
9547 || (REG_P (target)
9548 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9549 target = gen_reg_rtx (mode);
9550 expand_operands (treeop0, treeop1,
9551 target, &op0, &op1, EXPAND_NORMAL);
9553 /* First try to do it with a special MIN or MAX instruction.
9554 If that does not win, use a conditional jump to select the proper
9555 value. */
9556 this_optab = optab_for_tree_code (code, type, optab_default);
9557 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9558 OPTAB_WIDEN);
9559 if (temp != 0)
9560 return temp;
9562 if (VECTOR_TYPE_P (type))
9563 gcc_unreachable ();
9565 /* At this point, a MEM target is no longer useful; we will get better
9566 code without it. */
9568 if (! REG_P (target))
9569 target = gen_reg_rtx (mode);
9571 /* If op1 was placed in target, swap op0 and op1. */
9572 if (target != op0 && target == op1)
9573 std::swap (op0, op1);
9575 /* We generate better code and avoid problems with op1 mentioning
9576 target by forcing op1 into a pseudo if it isn't a constant. */
9577 if (! CONSTANT_P (op1))
9578 op1 = force_reg (mode, op1);
9581 enum rtx_code comparison_code;
9582 rtx cmpop1 = op1;
9584 if (code == MAX_EXPR)
9585 comparison_code = unsignedp ? GEU : GE;
9586 else
9587 comparison_code = unsignedp ? LEU : LE;
9589 /* Canonicalize to comparisons against 0. */
9590 if (op1 == const1_rtx)
9592 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9593 or (a != 0 ? a : 1) for unsigned.
9594 For MIN we are safe converting (a <= 1 ? a : 1)
9595 into (a <= 0 ? a : 1) */
9596 cmpop1 = const0_rtx;
9597 if (code == MAX_EXPR)
9598 comparison_code = unsignedp ? NE : GT;
9600 if (op1 == constm1_rtx && !unsignedp)
9602 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9603 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9604 cmpop1 = const0_rtx;
9605 if (code == MIN_EXPR)
9606 comparison_code = LT;
9609 /* Use a conditional move if possible. */
9610 if (can_conditionally_move_p (mode))
9612 rtx insn;
9614 start_sequence ();
9616 /* Try to emit the conditional move. */
9617 insn = emit_conditional_move (target, comparison_code,
9618 op0, cmpop1, mode,
9619 op0, op1, mode,
9620 unsignedp);
9622 /* If we could do the conditional move, emit the sequence,
9623 and return. */
9624 if (insn)
9626 rtx_insn *seq = get_insns ();
9627 end_sequence ();
9628 emit_insn (seq);
9629 return target;
9632 /* Otherwise discard the sequence and fall back to code with
9633 branches. */
9634 end_sequence ();
9637 if (target != op0)
9638 emit_move_insn (target, op0);
9640 lab = gen_label_rtx ();
9641 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9642 unsignedp, mode, NULL_RTX, NULL, lab,
9643 profile_probability::uninitialized ());
9645 emit_move_insn (target, op1);
9646 emit_label (lab);
9647 return target;
9649 case BIT_NOT_EXPR:
9650 op0 = expand_expr (treeop0, subtarget,
9651 VOIDmode, EXPAND_NORMAL);
9652 if (modifier == EXPAND_STACK_PARM)
9653 target = 0;
9654 /* In case we have to reduce the result to bitfield precision
9655 for unsigned bitfield expand this as XOR with a proper constant
9656 instead. */
9657 if (reduce_bit_field && TYPE_UNSIGNED (type))
9659 int_mode = SCALAR_INT_TYPE_MODE (type);
9660 wide_int mask = wi::mask (TYPE_PRECISION (type),
9661 false, GET_MODE_PRECISION (int_mode));
9663 temp = expand_binop (int_mode, xor_optab, op0,
9664 immed_wide_int_const (mask, int_mode),
9665 target, 1, OPTAB_LIB_WIDEN);
9667 else
9668 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9669 gcc_assert (temp);
9670 return temp;
9672 /* ??? Can optimize bitwise operations with one arg constant.
9673 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9674 and (a bitwise1 b) bitwise2 b (etc)
9675 but that is probably not worth while. */
9677 case BIT_AND_EXPR:
9678 case BIT_IOR_EXPR:
9679 case BIT_XOR_EXPR:
9680 goto binop;
9682 case LROTATE_EXPR:
9683 case RROTATE_EXPR:
9684 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9685 || type_has_mode_precision_p (type));
9686 /* fall through */
9688 case LSHIFT_EXPR:
9689 case RSHIFT_EXPR:
9691 /* If this is a fixed-point operation, then we cannot use the code
9692 below because "expand_shift" doesn't support sat/no-sat fixed-point
9693 shifts. */
9694 if (ALL_FIXED_POINT_MODE_P (mode))
9695 goto binop;
9697 if (! safe_from_p (subtarget, treeop1, 1))
9698 subtarget = 0;
9699 if (modifier == EXPAND_STACK_PARM)
9700 target = 0;
9701 op0 = expand_expr (treeop0, subtarget,
9702 VOIDmode, EXPAND_NORMAL);
9704 /* Left shift optimization when shifting across word_size boundary.
9706 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9707 there isn't native instruction to support this wide mode
9708 left shift. Given below scenario:
9710 Type A = (Type) B << C
9712 |< T >|
9713 | dest_high | dest_low |
9715 | word_size |
9717 If the shift amount C caused we shift B to across the word
9718 size boundary, i.e part of B shifted into high half of
9719 destination register, and part of B remains in the low
9720 half, then GCC will use the following left shift expand
9721 logic:
9723 1. Initialize dest_low to B.
9724 2. Initialize every bit of dest_high to the sign bit of B.
9725 3. Logic left shift dest_low by C bit to finalize dest_low.
9726 The value of dest_low before this shift is kept in a temp D.
9727 4. Logic left shift dest_high by C.
9728 5. Logic right shift D by (word_size - C).
9729 6. Or the result of 4 and 5 to finalize dest_high.
9731 While, by checking gimple statements, if operand B is
9732 coming from signed extension, then we can simplify above
9733 expand logic into:
9735 1. dest_high = src_low >> (word_size - C).
9736 2. dest_low = src_low << C.
9738 We can use one arithmetic right shift to finish all the
9739 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9740 needed from 6 into 2.
9742 The case is similar for zero extension, except that we
9743 initialize dest_high to zero rather than copies of the sign
9744 bit from B. Furthermore, we need to use a logical right shift
9745 in this case.
9747 The choice of sign-extension versus zero-extension is
9748 determined entirely by whether or not B is signed and is
9749 independent of the current setting of unsignedp. */
9751 temp = NULL_RTX;
9752 if (code == LSHIFT_EXPR
9753 && target
9754 && REG_P (target)
9755 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9756 && mode == int_mode
9757 && TREE_CONSTANT (treeop1)
9758 && TREE_CODE (treeop0) == SSA_NAME)
9760 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9761 if (is_gimple_assign (def)
9762 && gimple_assign_rhs_code (def) == NOP_EXPR)
9764 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9765 (TREE_TYPE (gimple_assign_rhs1 (def)));
9767 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9768 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9769 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9770 >= GET_MODE_BITSIZE (word_mode)))
9772 rtx_insn *seq, *seq_old;
9773 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9774 int_mode);
9775 bool extend_unsigned
9776 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9777 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9778 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9779 rtx dest_high = simplify_gen_subreg (word_mode, target,
9780 int_mode, high_off);
9781 HOST_WIDE_INT ramount = (BITS_PER_WORD
9782 - TREE_INT_CST_LOW (treeop1));
9783 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9785 start_sequence ();
9786 /* dest_high = src_low >> (word_size - C). */
9787 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9788 rshift, dest_high,
9789 extend_unsigned);
9790 if (temp != dest_high)
9791 emit_move_insn (dest_high, temp);
9793 /* dest_low = src_low << C. */
9794 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9795 treeop1, dest_low, unsignedp);
9796 if (temp != dest_low)
9797 emit_move_insn (dest_low, temp);
9799 seq = get_insns ();
9800 end_sequence ();
9801 temp = target ;
9803 if (have_insn_for (ASHIFT, int_mode))
9805 bool speed_p = optimize_insn_for_speed_p ();
9806 start_sequence ();
9807 rtx ret_old = expand_variable_shift (code, int_mode,
9808 op0, treeop1,
9809 target,
9810 unsignedp);
9812 seq_old = get_insns ();
9813 end_sequence ();
9814 if (seq_cost (seq, speed_p)
9815 >= seq_cost (seq_old, speed_p))
9817 seq = seq_old;
9818 temp = ret_old;
9821 emit_insn (seq);
9826 if (temp == NULL_RTX)
9827 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9828 unsignedp);
9829 if (code == LSHIFT_EXPR)
9830 temp = REDUCE_BIT_FIELD (temp);
9831 return temp;
9834 /* Could determine the answer when only additive constants differ. Also,
9835 the addition of one can be handled by changing the condition. */
9836 case LT_EXPR:
9837 case LE_EXPR:
9838 case GT_EXPR:
9839 case GE_EXPR:
9840 case EQ_EXPR:
9841 case NE_EXPR:
9842 case UNORDERED_EXPR:
9843 case ORDERED_EXPR:
9844 case UNLT_EXPR:
9845 case UNLE_EXPR:
9846 case UNGT_EXPR:
9847 case UNGE_EXPR:
9848 case UNEQ_EXPR:
9849 case LTGT_EXPR:
9851 temp = do_store_flag (ops,
9852 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9853 tmode != VOIDmode ? tmode : mode);
9854 if (temp)
9855 return temp;
9857 /* Use a compare and a jump for BLKmode comparisons, or for function
9858 type comparisons is have_canonicalize_funcptr_for_compare. */
9860 if ((target == 0
9861 || modifier == EXPAND_STACK_PARM
9862 || ! safe_from_p (target, treeop0, 1)
9863 || ! safe_from_p (target, treeop1, 1)
9864 /* Make sure we don't have a hard reg (such as function's return
9865 value) live across basic blocks, if not optimizing. */
9866 || (!optimize && REG_P (target)
9867 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9868 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9870 emit_move_insn (target, const0_rtx);
9872 rtx_code_label *lab1 = gen_label_rtx ();
9873 jumpifnot_1 (code, treeop0, treeop1, lab1,
9874 profile_probability::uninitialized ());
9876 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9877 emit_move_insn (target, constm1_rtx);
9878 else
9879 emit_move_insn (target, const1_rtx);
9881 emit_label (lab1);
9882 return target;
9884 case COMPLEX_EXPR:
9885 /* Get the rtx code of the operands. */
9886 op0 = expand_normal (treeop0);
9887 op1 = expand_normal (treeop1);
9889 if (!target)
9890 target = gen_reg_rtx (TYPE_MODE (type));
9891 else
9892 /* If target overlaps with op1, then either we need to force
9893 op1 into a pseudo (if target also overlaps with op0),
9894 or write the complex parts in reverse order. */
9895 switch (GET_CODE (target))
9897 case CONCAT:
9898 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9900 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9902 complex_expr_force_op1:
9903 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9904 emit_move_insn (temp, op1);
9905 op1 = temp;
9906 break;
9908 complex_expr_swap_order:
9909 /* Move the imaginary (op1) and real (op0) parts to their
9910 location. */
9911 write_complex_part (target, op1, true);
9912 write_complex_part (target, op0, false);
9914 return target;
9916 break;
9917 case MEM:
9918 temp = adjust_address_nv (target,
9919 GET_MODE_INNER (GET_MODE (target)), 0);
9920 if (reg_overlap_mentioned_p (temp, op1))
9922 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9923 temp = adjust_address_nv (target, imode,
9924 GET_MODE_SIZE (imode));
9925 if (reg_overlap_mentioned_p (temp, op0))
9926 goto complex_expr_force_op1;
9927 goto complex_expr_swap_order;
9929 break;
9930 default:
9931 if (reg_overlap_mentioned_p (target, op1))
9933 if (reg_overlap_mentioned_p (target, op0))
9934 goto complex_expr_force_op1;
9935 goto complex_expr_swap_order;
9937 break;
9940 /* Move the real (op0) and imaginary (op1) parts to their location. */
9941 write_complex_part (target, op0, false);
9942 write_complex_part (target, op1, true);
9944 return target;
9946 case WIDEN_SUM_EXPR:
9948 tree oprnd0 = treeop0;
9949 tree oprnd1 = treeop1;
9951 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9952 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9953 target, unsignedp);
9954 return target;
9957 case VEC_UNPACK_HI_EXPR:
9958 case VEC_UNPACK_LO_EXPR:
9959 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
9960 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
9962 op0 = expand_normal (treeop0);
9963 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9964 target, unsignedp);
9965 gcc_assert (temp);
9966 return temp;
9969 case VEC_UNPACK_FLOAT_HI_EXPR:
9970 case VEC_UNPACK_FLOAT_LO_EXPR:
9972 op0 = expand_normal (treeop0);
9973 /* The signedness is determined from input operand. */
9974 temp = expand_widen_pattern_expr
9975 (ops, op0, NULL_RTX, NULL_RTX,
9976 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9978 gcc_assert (temp);
9979 return temp;
9982 case VEC_WIDEN_PLUS_HI_EXPR:
9983 case VEC_WIDEN_PLUS_LO_EXPR:
9984 case VEC_WIDEN_MINUS_HI_EXPR:
9985 case VEC_WIDEN_MINUS_LO_EXPR:
9986 case VEC_WIDEN_MULT_HI_EXPR:
9987 case VEC_WIDEN_MULT_LO_EXPR:
9988 case VEC_WIDEN_MULT_EVEN_EXPR:
9989 case VEC_WIDEN_MULT_ODD_EXPR:
9990 case VEC_WIDEN_LSHIFT_HI_EXPR:
9991 case VEC_WIDEN_LSHIFT_LO_EXPR:
9992 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9993 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9994 target, unsignedp);
9995 gcc_assert (target);
9996 return target;
9998 case VEC_PACK_SAT_EXPR:
9999 case VEC_PACK_FIX_TRUNC_EXPR:
10000 mode = TYPE_MODE (TREE_TYPE (treeop0));
10001 goto binop;
10003 case VEC_PACK_TRUNC_EXPR:
10004 if (VECTOR_BOOLEAN_TYPE_P (type)
10005 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10006 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10007 && SCALAR_INT_MODE_P (mode))
10009 class expand_operand eops[4];
10010 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10011 expand_operands (treeop0, treeop1,
10012 subtarget, &op0, &op1, EXPAND_NORMAL);
10013 this_optab = vec_pack_sbool_trunc_optab;
10014 enum insn_code icode = optab_handler (this_optab, imode);
10015 create_output_operand (&eops[0], target, mode);
10016 create_convert_operand_from (&eops[1], op0, imode, false);
10017 create_convert_operand_from (&eops[2], op1, imode, false);
10018 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10019 create_input_operand (&eops[3], temp, imode);
10020 expand_insn (icode, 4, eops);
10021 return eops[0].value;
10023 mode = TYPE_MODE (TREE_TYPE (treeop0));
10024 goto binop;
10026 case VEC_PACK_FLOAT_EXPR:
10027 mode = TYPE_MODE (TREE_TYPE (treeop0));
10028 expand_operands (treeop0, treeop1,
10029 subtarget, &op0, &op1, EXPAND_NORMAL);
10030 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10031 optab_default);
10032 target = expand_binop (mode, this_optab, op0, op1, target,
10033 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10034 OPTAB_LIB_WIDEN);
10035 gcc_assert (target);
10036 return target;
10038 case VEC_PERM_EXPR:
10040 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10041 vec_perm_builder sel;
10042 if (TREE_CODE (treeop2) == VECTOR_CST
10043 && tree_to_vec_perm_builder (&sel, treeop2))
10045 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10046 temp = expand_vec_perm_const (mode, op0, op1, sel,
10047 sel_mode, target);
10049 else
10051 op2 = expand_normal (treeop2);
10052 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10054 gcc_assert (temp);
10055 return temp;
10058 case DOT_PROD_EXPR:
10060 tree oprnd0 = treeop0;
10061 tree oprnd1 = treeop1;
10062 tree oprnd2 = treeop2;
10064 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10065 op2 = expand_normal (oprnd2);
10066 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10067 target, unsignedp);
10068 return target;
10071 case SAD_EXPR:
10073 tree oprnd0 = treeop0;
10074 tree oprnd1 = treeop1;
10075 tree oprnd2 = treeop2;
10077 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10078 op2 = expand_normal (oprnd2);
10079 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10080 target, unsignedp);
10081 return target;
10084 case REALIGN_LOAD_EXPR:
10086 tree oprnd0 = treeop0;
10087 tree oprnd1 = treeop1;
10088 tree oprnd2 = treeop2;
10090 this_optab = optab_for_tree_code (code, type, optab_default);
10091 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10092 op2 = expand_normal (oprnd2);
10093 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10094 target, unsignedp);
10095 gcc_assert (temp);
10096 return temp;
10099 case COND_EXPR:
10101 /* A COND_EXPR with its type being VOID_TYPE represents a
10102 conditional jump and is handled in
10103 expand_gimple_cond_expr. */
10104 gcc_assert (!VOID_TYPE_P (type));
10106 /* Note that COND_EXPRs whose type is a structure or union
10107 are required to be constructed to contain assignments of
10108 a temporary variable, so that we can evaluate them here
10109 for side effect only. If type is void, we must do likewise. */
10111 gcc_assert (!TREE_ADDRESSABLE (type)
10112 && !ignore
10113 && TREE_TYPE (treeop1) != void_type_node
10114 && TREE_TYPE (treeop2) != void_type_node);
10116 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10117 if (temp)
10118 return temp;
10120 /* If we are not to produce a result, we have no target. Otherwise,
10121 if a target was specified use it; it will not be used as an
10122 intermediate target unless it is safe. If no target, use a
10123 temporary. */
10125 if (modifier != EXPAND_STACK_PARM
10126 && original_target
10127 && safe_from_p (original_target, treeop0, 1)
10128 && GET_MODE (original_target) == mode
10129 && !MEM_P (original_target))
10130 temp = original_target;
10131 else
10132 temp = assign_temp (type, 0, 1);
10134 do_pending_stack_adjust ();
10135 NO_DEFER_POP;
10136 rtx_code_label *lab0 = gen_label_rtx ();
10137 rtx_code_label *lab1 = gen_label_rtx ();
10138 jumpifnot (treeop0, lab0,
10139 profile_probability::uninitialized ());
10140 store_expr (treeop1, temp,
10141 modifier == EXPAND_STACK_PARM,
10142 false, false);
10144 emit_jump_insn (targetm.gen_jump (lab1));
10145 emit_barrier ();
10146 emit_label (lab0);
10147 store_expr (treeop2, temp,
10148 modifier == EXPAND_STACK_PARM,
10149 false, false);
10151 emit_label (lab1);
10152 OK_DEFER_POP;
10153 return temp;
10156 case VEC_DUPLICATE_EXPR:
10157 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10158 target = expand_vector_broadcast (mode, op0);
10159 gcc_assert (target);
10160 return target;
10162 case VEC_SERIES_EXPR:
10163 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10164 return expand_vec_series_expr (mode, op0, op1, target);
10166 case BIT_INSERT_EXPR:
10168 unsigned bitpos = tree_to_uhwi (treeop2);
10169 unsigned bitsize;
10170 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10171 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10172 else
10173 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10174 op0 = expand_normal (treeop0);
10175 op1 = expand_normal (treeop1);
10176 rtx dst = gen_reg_rtx (mode);
10177 emit_move_insn (dst, op0);
10178 store_bit_field (dst, bitsize, bitpos, 0, 0,
10179 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
10180 return dst;
10183 default:
10184 gcc_unreachable ();
10187 /* Here to do an ordinary binary operator. */
10188 binop:
10189 expand_operands (treeop0, treeop1,
10190 subtarget, &op0, &op1, EXPAND_NORMAL);
10191 binop2:
10192 this_optab = optab_for_tree_code (code, type, optab_default);
10193 binop3:
10194 if (modifier == EXPAND_STACK_PARM)
10195 target = 0;
10196 temp = expand_binop (mode, this_optab, op0, op1, target,
10197 unsignedp, OPTAB_LIB_WIDEN);
10198 gcc_assert (temp);
10199 /* Bitwise operations do not need bitfield reduction as we expect their
10200 operands being properly truncated. */
10201 if (code == BIT_XOR_EXPR
10202 || code == BIT_AND_EXPR
10203 || code == BIT_IOR_EXPR)
10204 return temp;
10205 return REDUCE_BIT_FIELD (temp);
10207 #undef REDUCE_BIT_FIELD
10210 /* Return TRUE if expression STMT is suitable for replacement.
10211 Never consider memory loads as replaceable, because those don't ever lead
10212 into constant expressions. */
10214 static bool
10215 stmt_is_replaceable_p (gimple *stmt)
10217 if (ssa_is_replaceable_p (stmt))
10219 /* Don't move around loads. */
10220 if (!gimple_assign_single_p (stmt)
10221 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10222 return true;
10224 return false;
10228 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10229 enum expand_modifier modifier, rtx *alt_rtl,
10230 bool inner_reference_p)
10232 rtx op0, op1, temp, decl_rtl;
10233 tree type;
10234 int unsignedp;
10235 machine_mode mode, dmode;
10236 enum tree_code code = TREE_CODE (exp);
10237 rtx subtarget, original_target;
10238 int ignore;
10239 tree context;
10240 bool reduce_bit_field;
10241 location_t loc = EXPR_LOCATION (exp);
10242 struct separate_ops ops;
10243 tree treeop0, treeop1, treeop2;
10244 tree ssa_name = NULL_TREE;
10245 gimple *g;
10247 type = TREE_TYPE (exp);
10248 mode = TYPE_MODE (type);
10249 unsignedp = TYPE_UNSIGNED (type);
10251 treeop0 = treeop1 = treeop2 = NULL_TREE;
10252 if (!VL_EXP_CLASS_P (exp))
10253 switch (TREE_CODE_LENGTH (code))
10255 default:
10256 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10257 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10258 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10259 case 0: break;
10261 ops.code = code;
10262 ops.type = type;
10263 ops.op0 = treeop0;
10264 ops.op1 = treeop1;
10265 ops.op2 = treeop2;
10266 ops.location = loc;
10268 ignore = (target == const0_rtx
10269 || ((CONVERT_EXPR_CODE_P (code)
10270 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10271 && TREE_CODE (type) == VOID_TYPE));
10273 /* An operation in what may be a bit-field type needs the
10274 result to be reduced to the precision of the bit-field type,
10275 which is narrower than that of the type's mode. */
10276 reduce_bit_field = (!ignore
10277 && INTEGRAL_TYPE_P (type)
10278 && !type_has_mode_precision_p (type));
10280 /* If we are going to ignore this result, we need only do something
10281 if there is a side-effect somewhere in the expression. If there
10282 is, short-circuit the most common cases here. Note that we must
10283 not call expand_expr with anything but const0_rtx in case this
10284 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10286 if (ignore)
10288 if (! TREE_SIDE_EFFECTS (exp))
10289 return const0_rtx;
10291 /* Ensure we reference a volatile object even if value is ignored, but
10292 don't do this if all we are doing is taking its address. */
10293 if (TREE_THIS_VOLATILE (exp)
10294 && TREE_CODE (exp) != FUNCTION_DECL
10295 && mode != VOIDmode && mode != BLKmode
10296 && modifier != EXPAND_CONST_ADDRESS)
10298 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10299 if (MEM_P (temp))
10300 copy_to_reg (temp);
10301 return const0_rtx;
10304 if (TREE_CODE_CLASS (code) == tcc_unary
10305 || code == BIT_FIELD_REF
10306 || code == COMPONENT_REF
10307 || code == INDIRECT_REF)
10308 return expand_expr (treeop0, const0_rtx, VOIDmode,
10309 modifier);
10311 else if (TREE_CODE_CLASS (code) == tcc_binary
10312 || TREE_CODE_CLASS (code) == tcc_comparison
10313 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10315 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10316 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10317 return const0_rtx;
10320 target = 0;
10323 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10324 target = 0;
10326 /* Use subtarget as the target for operand 0 of a binary operation. */
10327 subtarget = get_subtarget (target);
10328 original_target = target;
10330 switch (code)
10332 case LABEL_DECL:
10334 tree function = decl_function_context (exp);
10336 temp = label_rtx (exp);
10337 temp = gen_rtx_LABEL_REF (Pmode, temp);
10339 if (function != current_function_decl
10340 && function != 0)
10341 LABEL_REF_NONLOCAL_P (temp) = 1;
10343 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10344 return temp;
10347 case SSA_NAME:
10348 /* ??? ivopts calls expander, without any preparation from
10349 out-of-ssa. So fake instructions as if this was an access to the
10350 base variable. This unnecessarily allocates a pseudo, see how we can
10351 reuse it, if partition base vars have it set already. */
10352 if (!currently_expanding_to_rtl)
10354 tree var = SSA_NAME_VAR (exp);
10355 if (var && DECL_RTL_SET_P (var))
10356 return DECL_RTL (var);
10357 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10358 LAST_VIRTUAL_REGISTER + 1);
10361 g = get_gimple_for_ssa_name (exp);
10362 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10363 if (g == NULL
10364 && modifier == EXPAND_INITIALIZER
10365 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10366 && (optimize || !SSA_NAME_VAR (exp)
10367 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10368 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10369 g = SSA_NAME_DEF_STMT (exp);
10370 if (g)
10372 rtx r;
10373 location_t saved_loc = curr_insn_location ();
10374 loc = gimple_location (g);
10375 if (loc != UNKNOWN_LOCATION)
10376 set_curr_insn_location (loc);
10377 ops.code = gimple_assign_rhs_code (g);
10378 switch (get_gimple_rhs_class (ops.code))
10380 case GIMPLE_TERNARY_RHS:
10381 ops.op2 = gimple_assign_rhs3 (g);
10382 /* Fallthru */
10383 case GIMPLE_BINARY_RHS:
10384 ops.op1 = gimple_assign_rhs2 (g);
10386 /* Try to expand conditonal compare. */
10387 if (targetm.gen_ccmp_first)
10389 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10390 r = expand_ccmp_expr (g, mode);
10391 if (r)
10392 break;
10394 /* Fallthru */
10395 case GIMPLE_UNARY_RHS:
10396 ops.op0 = gimple_assign_rhs1 (g);
10397 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10398 ops.location = loc;
10399 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10400 break;
10401 case GIMPLE_SINGLE_RHS:
10403 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10404 tmode, modifier, alt_rtl,
10405 inner_reference_p);
10406 break;
10408 default:
10409 gcc_unreachable ();
10411 set_curr_insn_location (saved_loc);
10412 if (REG_P (r) && !REG_EXPR (r))
10413 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10414 return r;
10417 ssa_name = exp;
10418 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10419 exp = SSA_NAME_VAR (ssa_name);
10420 goto expand_decl_rtl;
10422 case VAR_DECL:
10423 /* Allow accel compiler to handle variables that require special
10424 treatment, e.g. if they have been modified in some way earlier in
10425 compilation by the adjust_private_decl OpenACC hook. */
10426 if (flag_openacc && targetm.goacc.expand_var_decl)
10428 temp = targetm.goacc.expand_var_decl (exp);
10429 if (temp)
10430 return temp;
10432 /* ... fall through ... */
10434 case PARM_DECL:
10435 /* If a static var's type was incomplete when the decl was written,
10436 but the type is complete now, lay out the decl now. */
10437 if (DECL_SIZE (exp) == 0
10438 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10439 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10440 layout_decl (exp, 0);
10442 /* fall through */
10444 case FUNCTION_DECL:
10445 case RESULT_DECL:
10446 decl_rtl = DECL_RTL (exp);
10447 expand_decl_rtl:
10448 gcc_assert (decl_rtl);
10450 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10451 settings for VECTOR_TYPE_P that might switch for the function. */
10452 if (currently_expanding_to_rtl
10453 && code == VAR_DECL && MEM_P (decl_rtl)
10454 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10455 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10456 else
10457 decl_rtl = copy_rtx (decl_rtl);
10459 /* Record writes to register variables. */
10460 if (modifier == EXPAND_WRITE
10461 && REG_P (decl_rtl)
10462 && HARD_REGISTER_P (decl_rtl))
10463 add_to_hard_reg_set (&crtl->asm_clobbers,
10464 GET_MODE (decl_rtl), REGNO (decl_rtl));
10466 /* Ensure variable marked as used even if it doesn't go through
10467 a parser. If it hasn't be used yet, write out an external
10468 definition. */
10469 if (exp)
10470 TREE_USED (exp) = 1;
10472 /* Show we haven't gotten RTL for this yet. */
10473 temp = 0;
10475 /* Variables inherited from containing functions should have
10476 been lowered by this point. */
10477 if (exp)
10478 context = decl_function_context (exp);
10479 gcc_assert (!exp
10480 || SCOPE_FILE_SCOPE_P (context)
10481 || context == current_function_decl
10482 || TREE_STATIC (exp)
10483 || DECL_EXTERNAL (exp)
10484 /* ??? C++ creates functions that are not TREE_STATIC. */
10485 || TREE_CODE (exp) == FUNCTION_DECL);
10487 /* This is the case of an array whose size is to be determined
10488 from its initializer, while the initializer is still being parsed.
10489 ??? We aren't parsing while expanding anymore. */
10491 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10492 temp = validize_mem (decl_rtl);
10494 /* If DECL_RTL is memory, we are in the normal case and the
10495 address is not valid, get the address into a register. */
10497 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10499 if (alt_rtl)
10500 *alt_rtl = decl_rtl;
10501 decl_rtl = use_anchored_address (decl_rtl);
10502 if (modifier != EXPAND_CONST_ADDRESS
10503 && modifier != EXPAND_SUM
10504 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10505 : GET_MODE (decl_rtl),
10506 XEXP (decl_rtl, 0),
10507 MEM_ADDR_SPACE (decl_rtl)))
10508 temp = replace_equiv_address (decl_rtl,
10509 copy_rtx (XEXP (decl_rtl, 0)));
10512 /* If we got something, return it. But first, set the alignment
10513 if the address is a register. */
10514 if (temp != 0)
10516 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10517 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10519 else if (MEM_P (decl_rtl))
10520 temp = decl_rtl;
10522 if (temp != 0)
10524 if (MEM_P (temp)
10525 && modifier != EXPAND_WRITE
10526 && modifier != EXPAND_MEMORY
10527 && modifier != EXPAND_INITIALIZER
10528 && modifier != EXPAND_CONST_ADDRESS
10529 && modifier != EXPAND_SUM
10530 && !inner_reference_p
10531 && mode != BLKmode
10532 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10533 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10534 MEM_ALIGN (temp), NULL_RTX, NULL);
10536 return temp;
10539 if (exp)
10540 dmode = DECL_MODE (exp);
10541 else
10542 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10544 /* If the mode of DECL_RTL does not match that of the decl,
10545 there are two cases: we are dealing with a BLKmode value
10546 that is returned in a register, or we are dealing with
10547 a promoted value. In the latter case, return a SUBREG
10548 of the wanted mode, but mark it so that we know that it
10549 was already extended. */
10550 if (REG_P (decl_rtl)
10551 && dmode != BLKmode
10552 && GET_MODE (decl_rtl) != dmode)
10554 machine_mode pmode;
10556 /* Get the signedness to be used for this variable. Ensure we get
10557 the same mode we got when the variable was declared. */
10558 if (code != SSA_NAME)
10559 pmode = promote_decl_mode (exp, &unsignedp);
10560 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10561 && gimple_code (g) == GIMPLE_CALL
10562 && !gimple_call_internal_p (g))
10563 pmode = promote_function_mode (type, mode, &unsignedp,
10564 gimple_call_fntype (g),
10566 else
10567 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10568 gcc_assert (GET_MODE (decl_rtl) == pmode);
10570 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10571 SUBREG_PROMOTED_VAR_P (temp) = 1;
10572 SUBREG_PROMOTED_SET (temp, unsignedp);
10573 return temp;
10576 return decl_rtl;
10578 case INTEGER_CST:
10580 /* Given that TYPE_PRECISION (type) is not always equal to
10581 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10582 the former to the latter according to the signedness of the
10583 type. */
10584 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10585 temp = immed_wide_int_const
10586 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10587 return temp;
10590 case VECTOR_CST:
10592 tree tmp = NULL_TREE;
10593 if (VECTOR_MODE_P (mode))
10594 return const_vector_from_tree (exp);
10595 scalar_int_mode int_mode;
10596 if (is_int_mode (mode, &int_mode))
10598 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10599 if (type_for_mode)
10600 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10601 type_for_mode, exp);
10603 if (!tmp)
10605 vec<constructor_elt, va_gc> *v;
10606 /* Constructors need to be fixed-length. FIXME. */
10607 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10608 vec_alloc (v, nunits);
10609 for (unsigned int i = 0; i < nunits; ++i)
10610 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10611 tmp = build_constructor (type, v);
10613 return expand_expr (tmp, ignore ? const0_rtx : target,
10614 tmode, modifier);
10617 case CONST_DECL:
10618 if (modifier == EXPAND_WRITE)
10620 /* Writing into CONST_DECL is always invalid, but handle it
10621 gracefully. */
10622 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10623 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10624 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10625 EXPAND_NORMAL, as);
10626 op0 = memory_address_addr_space (mode, op0, as);
10627 temp = gen_rtx_MEM (mode, op0);
10628 set_mem_addr_space (temp, as);
10629 return temp;
10631 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10633 case REAL_CST:
10634 /* If optimized, generate immediate CONST_DOUBLE
10635 which will be turned into memory by reload if necessary.
10637 We used to force a register so that loop.c could see it. But
10638 this does not allow gen_* patterns to perform optimizations with
10639 the constants. It also produces two insns in cases like "x = 1.0;".
10640 On most machines, floating-point constants are not permitted in
10641 many insns, so we'd end up copying it to a register in any case.
10643 Now, we do the copying in expand_binop, if appropriate. */
10644 return const_double_from_real_value (TREE_REAL_CST (exp),
10645 TYPE_MODE (TREE_TYPE (exp)));
10647 case FIXED_CST:
10648 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10649 TYPE_MODE (TREE_TYPE (exp)));
10651 case COMPLEX_CST:
10652 /* Handle evaluating a complex constant in a CONCAT target. */
10653 if (original_target && GET_CODE (original_target) == CONCAT)
10655 rtx rtarg, itarg;
10657 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10658 rtarg = XEXP (original_target, 0);
10659 itarg = XEXP (original_target, 1);
10661 /* Move the real and imaginary parts separately. */
10662 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10663 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10665 if (op0 != rtarg)
10666 emit_move_insn (rtarg, op0);
10667 if (op1 != itarg)
10668 emit_move_insn (itarg, op1);
10670 return original_target;
10673 /* fall through */
10675 case STRING_CST:
10676 temp = expand_expr_constant (exp, 1, modifier);
10678 /* temp contains a constant address.
10679 On RISC machines where a constant address isn't valid,
10680 make some insns to get that address into a register. */
10681 if (modifier != EXPAND_CONST_ADDRESS
10682 && modifier != EXPAND_INITIALIZER
10683 && modifier != EXPAND_SUM
10684 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10685 MEM_ADDR_SPACE (temp)))
10686 return replace_equiv_address (temp,
10687 copy_rtx (XEXP (temp, 0)));
10688 return temp;
10690 case POLY_INT_CST:
10691 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10693 case SAVE_EXPR:
10695 tree val = treeop0;
10696 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10697 inner_reference_p);
10699 if (!SAVE_EXPR_RESOLVED_P (exp))
10701 /* We can indeed still hit this case, typically via builtin
10702 expanders calling save_expr immediately before expanding
10703 something. Assume this means that we only have to deal
10704 with non-BLKmode values. */
10705 gcc_assert (GET_MODE (ret) != BLKmode);
10707 val = build_decl (curr_insn_location (),
10708 VAR_DECL, NULL, TREE_TYPE (exp));
10709 DECL_ARTIFICIAL (val) = 1;
10710 DECL_IGNORED_P (val) = 1;
10711 treeop0 = val;
10712 TREE_OPERAND (exp, 0) = treeop0;
10713 SAVE_EXPR_RESOLVED_P (exp) = 1;
10715 if (!CONSTANT_P (ret))
10716 ret = copy_to_reg (ret);
10717 SET_DECL_RTL (val, ret);
10720 return ret;
10724 case CONSTRUCTOR:
10725 /* If we don't need the result, just ensure we evaluate any
10726 subexpressions. */
10727 if (ignore)
10729 unsigned HOST_WIDE_INT idx;
10730 tree value;
10732 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10733 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10735 return const0_rtx;
10738 return expand_constructor (exp, target, modifier, false);
10740 case TARGET_MEM_REF:
10742 addr_space_t as
10743 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10744 unsigned int align;
10746 op0 = addr_for_mem_ref (exp, as, true);
10747 op0 = memory_address_addr_space (mode, op0, as);
10748 temp = gen_rtx_MEM (mode, op0);
10749 set_mem_attributes (temp, exp, 0);
10750 set_mem_addr_space (temp, as);
10751 align = get_object_alignment (exp);
10752 if (modifier != EXPAND_WRITE
10753 && modifier != EXPAND_MEMORY
10754 && mode != BLKmode
10755 && align < GET_MODE_ALIGNMENT (mode))
10756 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10757 align, NULL_RTX, NULL);
10758 return temp;
10761 case MEM_REF:
10763 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10764 addr_space_t as
10765 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10766 machine_mode address_mode;
10767 tree base = TREE_OPERAND (exp, 0);
10768 gimple *def_stmt;
10769 unsigned align;
10770 /* Handle expansion of non-aliased memory with non-BLKmode. That
10771 might end up in a register. */
10772 if (mem_ref_refers_to_non_mem_p (exp))
10774 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10775 base = TREE_OPERAND (base, 0);
10776 poly_uint64 type_size;
10777 if (known_eq (offset, 0)
10778 && !reverse
10779 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10780 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10781 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10782 target, tmode, modifier);
10783 if (TYPE_MODE (type) == BLKmode)
10785 temp = assign_stack_temp (DECL_MODE (base),
10786 GET_MODE_SIZE (DECL_MODE (base)));
10787 store_expr (base, temp, 0, false, false);
10788 temp = adjust_address (temp, BLKmode, offset);
10789 set_mem_size (temp, int_size_in_bytes (type));
10790 return temp;
10792 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10793 bitsize_int (offset * BITS_PER_UNIT));
10794 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10795 return expand_expr (exp, target, tmode, modifier);
10797 address_mode = targetm.addr_space.address_mode (as);
10798 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10800 tree mask = gimple_assign_rhs2 (def_stmt);
10801 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10802 gimple_assign_rhs1 (def_stmt), mask);
10803 TREE_OPERAND (exp, 0) = base;
10805 align = get_object_alignment (exp);
10806 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10807 op0 = memory_address_addr_space (mode, op0, as);
10808 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10810 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10811 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10812 op0 = memory_address_addr_space (mode, op0, as);
10814 temp = gen_rtx_MEM (mode, op0);
10815 set_mem_attributes (temp, exp, 0);
10816 set_mem_addr_space (temp, as);
10817 if (TREE_THIS_VOLATILE (exp))
10818 MEM_VOLATILE_P (temp) = 1;
10819 if (modifier != EXPAND_WRITE
10820 && modifier != EXPAND_MEMORY
10821 && !inner_reference_p
10822 && mode != BLKmode
10823 && align < GET_MODE_ALIGNMENT (mode))
10824 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
10825 modifier == EXPAND_STACK_PARM
10826 ? NULL_RTX : target, alt_rtl);
10827 if (reverse
10828 && modifier != EXPAND_MEMORY
10829 && modifier != EXPAND_WRITE)
10830 temp = flip_storage_order (mode, temp);
10831 return temp;
10834 case ARRAY_REF:
10837 tree array = treeop0;
10838 tree index = treeop1;
10839 tree init;
10841 /* Fold an expression like: "foo"[2].
10842 This is not done in fold so it won't happen inside &.
10843 Don't fold if this is for wide characters since it's too
10844 difficult to do correctly and this is a very rare case. */
10846 if (modifier != EXPAND_CONST_ADDRESS
10847 && modifier != EXPAND_INITIALIZER
10848 && modifier != EXPAND_MEMORY)
10850 tree t = fold_read_from_constant_string (exp);
10852 if (t)
10853 return expand_expr (t, target, tmode, modifier);
10856 /* If this is a constant index into a constant array,
10857 just get the value from the array. Handle both the cases when
10858 we have an explicit constructor and when our operand is a variable
10859 that was declared const. */
10861 if (modifier != EXPAND_CONST_ADDRESS
10862 && modifier != EXPAND_INITIALIZER
10863 && modifier != EXPAND_MEMORY
10864 && TREE_CODE (array) == CONSTRUCTOR
10865 && ! TREE_SIDE_EFFECTS (array)
10866 && TREE_CODE (index) == INTEGER_CST)
10868 unsigned HOST_WIDE_INT ix;
10869 tree field, value;
10871 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10872 field, value)
10873 if (tree_int_cst_equal (field, index))
10875 if (!TREE_SIDE_EFFECTS (value))
10876 return expand_expr (fold (value), target, tmode, modifier);
10877 break;
10881 else if (optimize >= 1
10882 && modifier != EXPAND_CONST_ADDRESS
10883 && modifier != EXPAND_INITIALIZER
10884 && modifier != EXPAND_MEMORY
10885 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10886 && TREE_CODE (index) == INTEGER_CST
10887 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10888 && (init = ctor_for_folding (array)) != error_mark_node)
10890 if (init == NULL_TREE)
10892 tree value = build_zero_cst (type);
10893 if (TREE_CODE (value) == CONSTRUCTOR)
10895 /* If VALUE is a CONSTRUCTOR, this optimization is only
10896 useful if this doesn't store the CONSTRUCTOR into
10897 memory. If it does, it is more efficient to just
10898 load the data from the array directly. */
10899 rtx ret = expand_constructor (value, target,
10900 modifier, true);
10901 if (ret == NULL_RTX)
10902 value = NULL_TREE;
10905 if (value)
10906 return expand_expr (value, target, tmode, modifier);
10908 else if (TREE_CODE (init) == CONSTRUCTOR)
10910 unsigned HOST_WIDE_INT ix;
10911 tree field, value;
10913 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10914 field, value)
10915 if (tree_int_cst_equal (field, index))
10917 if (TREE_SIDE_EFFECTS (value))
10918 break;
10920 if (TREE_CODE (value) == CONSTRUCTOR)
10922 /* If VALUE is a CONSTRUCTOR, this
10923 optimization is only useful if
10924 this doesn't store the CONSTRUCTOR
10925 into memory. If it does, it is more
10926 efficient to just load the data from
10927 the array directly. */
10928 rtx ret = expand_constructor (value, target,
10929 modifier, true);
10930 if (ret == NULL_RTX)
10931 break;
10934 return
10935 expand_expr (fold (value), target, tmode, modifier);
10938 else if (TREE_CODE (init) == STRING_CST)
10940 tree low_bound = array_ref_low_bound (exp);
10941 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10943 /* Optimize the special case of a zero lower bound.
10945 We convert the lower bound to sizetype to avoid problems
10946 with constant folding. E.g. suppose the lower bound is
10947 1 and its mode is QI. Without the conversion
10948 (ARRAY + (INDEX - (unsigned char)1))
10949 becomes
10950 (ARRAY + (-(unsigned char)1) + INDEX)
10951 which becomes
10952 (ARRAY + 255 + INDEX). Oops! */
10953 if (!integer_zerop (low_bound))
10954 index1 = size_diffop_loc (loc, index1,
10955 fold_convert_loc (loc, sizetype,
10956 low_bound));
10958 if (tree_fits_uhwi_p (index1)
10959 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10961 tree char_type = TREE_TYPE (TREE_TYPE (init));
10962 scalar_int_mode char_mode;
10964 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
10965 && GET_MODE_SIZE (char_mode) == 1)
10966 return gen_int_mode (TREE_STRING_POINTER (init)
10967 [TREE_INT_CST_LOW (index1)],
10968 char_mode);
10973 goto normal_inner_ref;
10975 case COMPONENT_REF:
10976 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
10977 /* Fall through. */
10978 case BIT_FIELD_REF:
10979 case ARRAY_RANGE_REF:
10980 normal_inner_ref:
10982 machine_mode mode1, mode2;
10983 poly_int64 bitsize, bitpos, bytepos;
10984 tree offset;
10985 int reversep, volatilep = 0, must_force_mem;
10986 tree tem
10987 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10988 &unsignedp, &reversep, &volatilep);
10989 rtx orig_op0, memloc;
10990 bool clear_mem_expr = false;
10992 /* If we got back the original object, something is wrong. Perhaps
10993 we are evaluating an expression too early. In any event, don't
10994 infinitely recurse. */
10995 gcc_assert (tem != exp);
10997 /* If TEM's type is a union of variable size, pass TARGET to the inner
10998 computation, since it will need a temporary and TARGET is known
10999 to have to do. This occurs in unchecked conversion in Ada. */
11000 orig_op0 = op0
11001 = expand_expr_real (tem,
11002 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11003 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11004 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11005 != INTEGER_CST)
11006 && modifier != EXPAND_STACK_PARM
11007 ? target : NULL_RTX),
11008 VOIDmode,
11009 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11010 NULL, true);
11012 /* If the field has a mode, we want to access it in the
11013 field's mode, not the computed mode.
11014 If a MEM has VOIDmode (external with incomplete type),
11015 use BLKmode for it instead. */
11016 if (MEM_P (op0))
11018 if (mode1 != VOIDmode)
11019 op0 = adjust_address (op0, mode1, 0);
11020 else if (GET_MODE (op0) == VOIDmode)
11021 op0 = adjust_address (op0, BLKmode, 0);
11024 mode2
11025 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11027 /* Make sure bitpos is not negative, it can wreak havoc later. */
11028 if (maybe_lt (bitpos, 0))
11030 gcc_checking_assert (offset == NULL_TREE);
11031 offset = size_int (bits_to_bytes_round_down (bitpos));
11032 bitpos = num_trailing_bits (bitpos);
11035 /* If we have either an offset, a BLKmode result, or a reference
11036 outside the underlying object, we must force it to memory.
11037 Such a case can occur in Ada if we have unchecked conversion
11038 of an expression from a scalar type to an aggregate type or
11039 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11040 passed a partially uninitialized object or a view-conversion
11041 to a larger size. */
11042 must_force_mem = (offset
11043 || mode1 == BLKmode
11044 || (mode == BLKmode
11045 && !int_mode_for_size (bitsize, 1).exists ())
11046 || maybe_gt (bitpos + bitsize,
11047 GET_MODE_BITSIZE (mode2)));
11049 /* Handle CONCAT first. */
11050 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11052 if (known_eq (bitpos, 0)
11053 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11054 && COMPLEX_MODE_P (mode1)
11055 && COMPLEX_MODE_P (GET_MODE (op0))
11056 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11057 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11059 if (reversep)
11060 op0 = flip_storage_order (GET_MODE (op0), op0);
11061 if (mode1 != GET_MODE (op0))
11063 rtx parts[2];
11064 for (int i = 0; i < 2; i++)
11066 rtx op = read_complex_part (op0, i != 0);
11067 if (GET_CODE (op) == SUBREG)
11068 op = force_reg (GET_MODE (op), op);
11069 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11070 if (temp)
11071 op = temp;
11072 else
11074 if (!REG_P (op) && !MEM_P (op))
11075 op = force_reg (GET_MODE (op), op);
11076 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11078 parts[i] = op;
11080 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11082 return op0;
11084 if (known_eq (bitpos, 0)
11085 && known_eq (bitsize,
11086 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11087 && maybe_ne (bitsize, 0))
11089 op0 = XEXP (op0, 0);
11090 mode2 = GET_MODE (op0);
11092 else if (known_eq (bitpos,
11093 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11094 && known_eq (bitsize,
11095 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11096 && maybe_ne (bitpos, 0)
11097 && maybe_ne (bitsize, 0))
11099 op0 = XEXP (op0, 1);
11100 bitpos = 0;
11101 mode2 = GET_MODE (op0);
11103 else
11104 /* Otherwise force into memory. */
11105 must_force_mem = 1;
11108 /* If this is a constant, put it in a register if it is a legitimate
11109 constant and we don't need a memory reference. */
11110 if (CONSTANT_P (op0)
11111 && mode2 != BLKmode
11112 && targetm.legitimate_constant_p (mode2, op0)
11113 && !must_force_mem)
11114 op0 = force_reg (mode2, op0);
11116 /* Otherwise, if this is a constant, try to force it to the constant
11117 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11118 is a legitimate constant. */
11119 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11120 op0 = validize_mem (memloc);
11122 /* Otherwise, if this is a constant or the object is not in memory
11123 and need be, put it there. */
11124 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11126 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11127 emit_move_insn (memloc, op0);
11128 op0 = memloc;
11129 clear_mem_expr = true;
11132 if (offset)
11134 machine_mode address_mode;
11135 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11136 EXPAND_SUM);
11138 gcc_assert (MEM_P (op0));
11140 address_mode = get_address_mode (op0);
11141 if (GET_MODE (offset_rtx) != address_mode)
11143 /* We cannot be sure that the RTL in offset_rtx is valid outside
11144 of a memory address context, so force it into a register
11145 before attempting to convert it to the desired mode. */
11146 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11147 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11150 /* See the comment in expand_assignment for the rationale. */
11151 if (mode1 != VOIDmode
11152 && maybe_ne (bitpos, 0)
11153 && maybe_gt (bitsize, 0)
11154 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11155 && multiple_p (bitpos, bitsize)
11156 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11157 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11159 op0 = adjust_address (op0, mode1, bytepos);
11160 bitpos = 0;
11163 op0 = offset_address (op0, offset_rtx,
11164 highest_pow2_factor (offset));
11167 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11168 record its alignment as BIGGEST_ALIGNMENT. */
11169 if (MEM_P (op0)
11170 && known_eq (bitpos, 0)
11171 && offset != 0
11172 && is_aligning_offset (offset, tem))
11173 set_mem_align (op0, BIGGEST_ALIGNMENT);
11175 /* Don't forget about volatility even if this is a bitfield. */
11176 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11178 if (op0 == orig_op0)
11179 op0 = copy_rtx (op0);
11181 MEM_VOLATILE_P (op0) = 1;
11184 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11186 if (op0 == orig_op0)
11187 op0 = copy_rtx (op0);
11189 set_mem_align (op0, BITS_PER_UNIT);
11192 /* In cases where an aligned union has an unaligned object
11193 as a field, we might be extracting a BLKmode value from
11194 an integer-mode (e.g., SImode) object. Handle this case
11195 by doing the extract into an object as wide as the field
11196 (which we know to be the width of a basic mode), then
11197 storing into memory, and changing the mode to BLKmode. */
11198 if (mode1 == VOIDmode
11199 || REG_P (op0) || GET_CODE (op0) == SUBREG
11200 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11201 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11202 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11203 && modifier != EXPAND_CONST_ADDRESS
11204 && modifier != EXPAND_INITIALIZER
11205 && modifier != EXPAND_MEMORY)
11206 /* If the bitfield is volatile and the bitsize
11207 is narrower than the access size of the bitfield,
11208 we need to extract bitfields from the access. */
11209 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11210 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11211 && mode1 != BLKmode
11212 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11213 /* If the field isn't aligned enough to fetch as a memref,
11214 fetch it as a bit field. */
11215 || (mode1 != BLKmode
11216 && (((MEM_P (op0)
11217 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11218 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11219 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11220 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11221 && modifier != EXPAND_MEMORY
11222 && ((modifier == EXPAND_CONST_ADDRESS
11223 || modifier == EXPAND_INITIALIZER)
11224 ? STRICT_ALIGNMENT
11225 : targetm.slow_unaligned_access (mode1,
11226 MEM_ALIGN (op0))))
11227 || !multiple_p (bitpos, BITS_PER_UNIT)))
11228 /* If the type and the field are a constant size and the
11229 size of the type isn't the same size as the bitfield,
11230 we must use bitfield operations. */
11231 || (known_size_p (bitsize)
11232 && TYPE_SIZE (TREE_TYPE (exp))
11233 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11234 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11235 bitsize)))
11237 machine_mode ext_mode = mode;
11239 if (ext_mode == BLKmode
11240 && ! (target != 0 && MEM_P (op0)
11241 && MEM_P (target)
11242 && multiple_p (bitpos, BITS_PER_UNIT)))
11243 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11245 if (ext_mode == BLKmode)
11247 if (target == 0)
11248 target = assign_temp (type, 1, 1);
11250 /* ??? Unlike the similar test a few lines below, this one is
11251 very likely obsolete. */
11252 if (known_eq (bitsize, 0))
11253 return target;
11255 /* In this case, BITPOS must start at a byte boundary and
11256 TARGET, if specified, must be a MEM. */
11257 gcc_assert (MEM_P (op0)
11258 && (!target || MEM_P (target)));
11260 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11261 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11262 emit_block_move (target,
11263 adjust_address (op0, VOIDmode, bytepos),
11264 gen_int_mode (bytesize, Pmode),
11265 (modifier == EXPAND_STACK_PARM
11266 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11268 return target;
11271 /* If we have nothing to extract, the result will be 0 for targets
11272 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11273 return 0 for the sake of consistency, as reading a zero-sized
11274 bitfield is valid in Ada and the value is fully specified. */
11275 if (known_eq (bitsize, 0))
11276 return const0_rtx;
11278 op0 = validize_mem (op0);
11280 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11281 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11283 /* If the result has aggregate type and the extraction is done in
11284 an integral mode, then the field may be not aligned on a byte
11285 boundary; in this case, if it has reverse storage order, it
11286 needs to be extracted as a scalar field with reverse storage
11287 order and put back into memory order afterwards. */
11288 if (AGGREGATE_TYPE_P (type)
11289 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11290 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11292 gcc_checking_assert (known_ge (bitpos, 0));
11293 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11294 (modifier == EXPAND_STACK_PARM
11295 ? NULL_RTX : target),
11296 ext_mode, ext_mode, reversep, alt_rtl);
11298 /* If the result has aggregate type and the mode of OP0 is an
11299 integral mode then, if BITSIZE is narrower than this mode
11300 and this is for big-endian data, we must put the field
11301 into the high-order bits. And we must also put it back
11302 into memory order if it has been previously reversed. */
11303 scalar_int_mode op0_mode;
11304 if (AGGREGATE_TYPE_P (type)
11305 && is_int_mode (GET_MODE (op0), &op0_mode))
11307 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11309 gcc_checking_assert (known_le (bitsize, size));
11310 if (maybe_lt (bitsize, size)
11311 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11312 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11313 size - bitsize, op0, 1);
11315 if (reversep)
11316 op0 = flip_storage_order (op0_mode, op0);
11319 /* If the result type is BLKmode, store the data into a temporary
11320 of the appropriate type, but with the mode corresponding to the
11321 mode for the data we have (op0's mode). */
11322 if (mode == BLKmode)
11324 rtx new_rtx
11325 = assign_stack_temp_for_type (ext_mode,
11326 GET_MODE_BITSIZE (ext_mode),
11327 type);
11328 emit_move_insn (new_rtx, op0);
11329 op0 = copy_rtx (new_rtx);
11330 PUT_MODE (op0, BLKmode);
11333 return op0;
11336 /* If the result is BLKmode, use that to access the object
11337 now as well. */
11338 if (mode == BLKmode)
11339 mode1 = BLKmode;
11341 /* Get a reference to just this component. */
11342 bytepos = bits_to_bytes_round_down (bitpos);
11343 if (modifier == EXPAND_CONST_ADDRESS
11344 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11345 op0 = adjust_address_nv (op0, mode1, bytepos);
11346 else
11347 op0 = adjust_address (op0, mode1, bytepos);
11349 if (op0 == orig_op0)
11350 op0 = copy_rtx (op0);
11352 /* Don't set memory attributes if the base expression is
11353 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11354 we should just honor its original memory attributes. */
11355 if (!(TREE_CODE (tem) == SSA_NAME
11356 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11357 set_mem_attributes (op0, exp, 0);
11359 if (REG_P (XEXP (op0, 0)))
11360 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11362 /* If op0 is a temporary because the original expressions was forced
11363 to memory, clear MEM_EXPR so that the original expression cannot
11364 be marked as addressable through MEM_EXPR of the temporary. */
11365 if (clear_mem_expr)
11366 set_mem_expr (op0, NULL_TREE);
11368 MEM_VOLATILE_P (op0) |= volatilep;
11370 if (reversep
11371 && modifier != EXPAND_MEMORY
11372 && modifier != EXPAND_WRITE)
11373 op0 = flip_storage_order (mode1, op0);
11375 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11376 || modifier == EXPAND_CONST_ADDRESS
11377 || modifier == EXPAND_INITIALIZER)
11378 return op0;
11380 if (target == 0)
11381 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11383 convert_move (target, op0, unsignedp);
11384 return target;
11387 case OBJ_TYPE_REF:
11388 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11390 case CALL_EXPR:
11391 /* All valid uses of __builtin_va_arg_pack () are removed during
11392 inlining. */
11393 if (CALL_EXPR_VA_ARG_PACK (exp))
11394 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
11396 tree fndecl = get_callee_fndecl (exp), attr;
11398 if (fndecl
11399 /* Don't diagnose the error attribute in thunks, those are
11400 artificially created. */
11401 && !CALL_FROM_THUNK_P (exp)
11402 && (attr = lookup_attribute ("error",
11403 DECL_ATTRIBUTES (fndecl))) != NULL)
11405 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11406 error ("%Kcall to %qs declared with attribute error: %s", exp,
11407 identifier_to_locale (ident),
11408 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11410 if (fndecl
11411 /* Don't diagnose the warning attribute in thunks, those are
11412 artificially created. */
11413 && !CALL_FROM_THUNK_P (exp)
11414 && (attr = lookup_attribute ("warning",
11415 DECL_ATTRIBUTES (fndecl))) != NULL)
11417 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11418 warning_at (tree_nonartificial_location (exp),
11419 OPT_Wattribute_warning,
11420 "%Kcall to %qs declared with attribute warning: %s",
11421 exp, identifier_to_locale (ident),
11422 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11425 /* Check for a built-in function. */
11426 if (fndecl && fndecl_built_in_p (fndecl))
11428 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11429 return expand_builtin (exp, target, subtarget, tmode, ignore);
11432 return expand_call (exp, target, ignore);
11434 case VIEW_CONVERT_EXPR:
11435 op0 = NULL_RTX;
11437 /* If we are converting to BLKmode, try to avoid an intermediate
11438 temporary by fetching an inner memory reference. */
11439 if (mode == BLKmode
11440 && poly_int_tree_p (TYPE_SIZE (type))
11441 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11442 && handled_component_p (treeop0))
11444 machine_mode mode1;
11445 poly_int64 bitsize, bitpos, bytepos;
11446 tree offset;
11447 int reversep, volatilep = 0;
11448 tree tem
11449 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11450 &unsignedp, &reversep, &volatilep);
11452 /* ??? We should work harder and deal with non-zero offsets. */
11453 if (!offset
11454 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11455 && !reversep
11456 && known_size_p (bitsize)
11457 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11459 /* See the normal_inner_ref case for the rationale. */
11460 rtx orig_op0
11461 = expand_expr_real (tem,
11462 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11463 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11464 != INTEGER_CST)
11465 && modifier != EXPAND_STACK_PARM
11466 ? target : NULL_RTX),
11467 VOIDmode,
11468 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11469 NULL, true);
11471 if (MEM_P (orig_op0))
11473 op0 = orig_op0;
11475 /* Get a reference to just this component. */
11476 if (modifier == EXPAND_CONST_ADDRESS
11477 || modifier == EXPAND_SUM
11478 || modifier == EXPAND_INITIALIZER)
11479 op0 = adjust_address_nv (op0, mode, bytepos);
11480 else
11481 op0 = adjust_address (op0, mode, bytepos);
11483 if (op0 == orig_op0)
11484 op0 = copy_rtx (op0);
11486 set_mem_attributes (op0, treeop0, 0);
11487 if (REG_P (XEXP (op0, 0)))
11488 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11490 MEM_VOLATILE_P (op0) |= volatilep;
11495 if (!op0)
11496 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11497 NULL, inner_reference_p);
11499 /* If the input and output modes are both the same, we are done. */
11500 if (mode == GET_MODE (op0))
11502 /* If neither mode is BLKmode, and both modes are the same size
11503 then we can use gen_lowpart. */
11504 else if (mode != BLKmode
11505 && GET_MODE (op0) != BLKmode
11506 && known_eq (GET_MODE_PRECISION (mode),
11507 GET_MODE_PRECISION (GET_MODE (op0)))
11508 && !COMPLEX_MODE_P (GET_MODE (op0)))
11510 if (GET_CODE (op0) == SUBREG)
11511 op0 = force_reg (GET_MODE (op0), op0);
11512 temp = gen_lowpart_common (mode, op0);
11513 if (temp)
11514 op0 = temp;
11515 else
11517 if (!REG_P (op0) && !MEM_P (op0))
11518 op0 = force_reg (GET_MODE (op0), op0);
11519 op0 = gen_lowpart (mode, op0);
11522 /* If both types are integral, convert from one mode to the other. */
11523 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11524 op0 = convert_modes (mode, GET_MODE (op0), op0,
11525 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11526 /* If the output type is a bit-field type, do an extraction. */
11527 else if (reduce_bit_field)
11528 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11529 TYPE_UNSIGNED (type), NULL_RTX,
11530 mode, mode, false, NULL);
11531 /* As a last resort, spill op0 to memory, and reload it in a
11532 different mode. */
11533 else if (!MEM_P (op0))
11535 /* If the operand is not a MEM, force it into memory. Since we
11536 are going to be changing the mode of the MEM, don't call
11537 force_const_mem for constants because we don't allow pool
11538 constants to change mode. */
11539 tree inner_type = TREE_TYPE (treeop0);
11541 gcc_assert (!TREE_ADDRESSABLE (exp));
11543 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11544 target
11545 = assign_stack_temp_for_type
11546 (TYPE_MODE (inner_type),
11547 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11549 emit_move_insn (target, op0);
11550 op0 = target;
11553 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11554 output type is such that the operand is known to be aligned, indicate
11555 that it is. Otherwise, we need only be concerned about alignment for
11556 non-BLKmode results. */
11557 if (MEM_P (op0))
11559 enum insn_code icode;
11561 if (modifier != EXPAND_WRITE
11562 && modifier != EXPAND_MEMORY
11563 && !inner_reference_p
11564 && mode != BLKmode
11565 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11567 /* If the target does have special handling for unaligned
11568 loads of mode then use them. */
11569 if ((icode = optab_handler (movmisalign_optab, mode))
11570 != CODE_FOR_nothing)
11572 rtx reg;
11574 op0 = adjust_address (op0, mode, 0);
11575 /* We've already validated the memory, and we're creating a
11576 new pseudo destination. The predicates really can't
11577 fail. */
11578 reg = gen_reg_rtx (mode);
11580 /* Nor can the insn generator. */
11581 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11582 emit_insn (insn);
11583 return reg;
11585 else if (STRICT_ALIGNMENT)
11587 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11588 poly_uint64 temp_size = mode_size;
11589 if (GET_MODE (op0) != BLKmode)
11590 temp_size = upper_bound (temp_size,
11591 GET_MODE_SIZE (GET_MODE (op0)));
11592 rtx new_rtx
11593 = assign_stack_temp_for_type (mode, temp_size, type);
11594 rtx new_with_op0_mode
11595 = adjust_address (new_rtx, GET_MODE (op0), 0);
11597 gcc_assert (!TREE_ADDRESSABLE (exp));
11599 if (GET_MODE (op0) == BLKmode)
11601 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11602 emit_block_move (new_with_op0_mode, op0, size_rtx,
11603 (modifier == EXPAND_STACK_PARM
11604 ? BLOCK_OP_CALL_PARM
11605 : BLOCK_OP_NORMAL));
11607 else
11608 emit_move_insn (new_with_op0_mode, op0);
11610 op0 = new_rtx;
11614 op0 = adjust_address (op0, mode, 0);
11617 return op0;
11619 case MODIFY_EXPR:
11621 tree lhs = treeop0;
11622 tree rhs = treeop1;
11623 gcc_assert (ignore);
11625 /* Check for |= or &= of a bitfield of size one into another bitfield
11626 of size 1. In this case, (unless we need the result of the
11627 assignment) we can do this more efficiently with a
11628 test followed by an assignment, if necessary.
11630 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11631 things change so we do, this code should be enhanced to
11632 support it. */
11633 if (TREE_CODE (lhs) == COMPONENT_REF
11634 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11635 || TREE_CODE (rhs) == BIT_AND_EXPR)
11636 && TREE_OPERAND (rhs, 0) == lhs
11637 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11638 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11639 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11641 rtx_code_label *label = gen_label_rtx ();
11642 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11643 profile_probability prob = profile_probability::uninitialized ();
11644 if (value)
11645 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11646 else
11647 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11648 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11649 false);
11650 do_pending_stack_adjust ();
11651 emit_label (label);
11652 return const0_rtx;
11655 expand_assignment (lhs, rhs, false);
11656 return const0_rtx;
11659 case ADDR_EXPR:
11660 return expand_expr_addr_expr (exp, target, tmode, modifier);
11662 case REALPART_EXPR:
11663 op0 = expand_normal (treeop0);
11664 return read_complex_part (op0, false);
11666 case IMAGPART_EXPR:
11667 op0 = expand_normal (treeop0);
11668 return read_complex_part (op0, true);
11670 case RETURN_EXPR:
11671 case LABEL_EXPR:
11672 case GOTO_EXPR:
11673 case SWITCH_EXPR:
11674 case ASM_EXPR:
11675 /* Expanded in cfgexpand.c. */
11676 gcc_unreachable ();
11678 case TRY_CATCH_EXPR:
11679 case CATCH_EXPR:
11680 case EH_FILTER_EXPR:
11681 case TRY_FINALLY_EXPR:
11682 case EH_ELSE_EXPR:
11683 /* Lowered by tree-eh.c. */
11684 gcc_unreachable ();
11686 case WITH_CLEANUP_EXPR:
11687 case CLEANUP_POINT_EXPR:
11688 case TARGET_EXPR:
11689 case CASE_LABEL_EXPR:
11690 case VA_ARG_EXPR:
11691 case BIND_EXPR:
11692 case INIT_EXPR:
11693 case CONJ_EXPR:
11694 case COMPOUND_EXPR:
11695 case PREINCREMENT_EXPR:
11696 case PREDECREMENT_EXPR:
11697 case POSTINCREMENT_EXPR:
11698 case POSTDECREMENT_EXPR:
11699 case LOOP_EXPR:
11700 case EXIT_EXPR:
11701 case COMPOUND_LITERAL_EXPR:
11702 /* Lowered by gimplify.c. */
11703 gcc_unreachable ();
11705 case FDESC_EXPR:
11706 /* Function descriptors are not valid except for as
11707 initialization constants, and should not be expanded. */
11708 gcc_unreachable ();
11710 case WITH_SIZE_EXPR:
11711 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11712 have pulled out the size to use in whatever context it needed. */
11713 return expand_expr_real (treeop0, original_target, tmode,
11714 modifier, alt_rtl, inner_reference_p);
11716 default:
11717 return expand_expr_real_2 (&ops, target, tmode, modifier);
11721 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11722 signedness of TYPE), possibly returning the result in TARGET.
11723 TYPE is known to be a partial integer type. */
11724 static rtx
11725 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11727 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11728 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11729 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11730 && (!target || GET_MODE (target) == mode));
11732 /* For constant values, reduce using wide_int_to_tree. */
11733 if (poly_int_rtx_p (exp))
11735 auto value = wi::to_poly_wide (exp, mode);
11736 tree t = wide_int_to_tree (type, value);
11737 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11739 else if (TYPE_UNSIGNED (type))
11741 rtx mask = immed_wide_int_const
11742 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11743 return expand_and (mode, exp, mask, target);
11745 else
11747 int count = GET_MODE_PRECISION (mode) - prec;
11748 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11749 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11753 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11754 when applied to the address of EXP produces an address known to be
11755 aligned more than BIGGEST_ALIGNMENT. */
11757 static int
11758 is_aligning_offset (const_tree offset, const_tree exp)
11760 /* Strip off any conversions. */
11761 while (CONVERT_EXPR_P (offset))
11762 offset = TREE_OPERAND (offset, 0);
11764 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11765 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11766 if (TREE_CODE (offset) != BIT_AND_EXPR
11767 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11768 || compare_tree_int (TREE_OPERAND (offset, 1),
11769 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11770 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11771 return 0;
11773 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11774 It must be NEGATE_EXPR. Then strip any more conversions. */
11775 offset = TREE_OPERAND (offset, 0);
11776 while (CONVERT_EXPR_P (offset))
11777 offset = TREE_OPERAND (offset, 0);
11779 if (TREE_CODE (offset) != NEGATE_EXPR)
11780 return 0;
11782 offset = TREE_OPERAND (offset, 0);
11783 while (CONVERT_EXPR_P (offset))
11784 offset = TREE_OPERAND (offset, 0);
11786 /* This must now be the address of EXP. */
11787 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11790 /* Return a STRING_CST corresponding to ARG's constant initializer either
11791 if it's a string constant, or, when VALREP is set, any other constant,
11792 or null otherwise.
11793 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
11794 within the byte string that ARG is references. If nonnull set *MEM_SIZE
11795 to the size of the byte string. If nonnull, set *DECL to the constant
11796 declaration ARG refers to. */
11798 static tree
11799 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
11800 bool valrep = false)
11802 tree dummy = NULL_TREE;
11803 if (!mem_size)
11804 mem_size = &dummy;
11806 /* Store the type of the original expression before conversions
11807 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
11808 removed. */
11809 tree argtype = TREE_TYPE (arg);
11811 tree array;
11812 STRIP_NOPS (arg);
11814 /* Non-constant index into the character array in an ARRAY_REF
11815 expression or null. */
11816 tree varidx = NULL_TREE;
11818 poly_int64 base_off = 0;
11820 if (TREE_CODE (arg) == ADDR_EXPR)
11822 arg = TREE_OPERAND (arg, 0);
11823 tree ref = arg;
11824 if (TREE_CODE (arg) == ARRAY_REF)
11826 tree idx = TREE_OPERAND (arg, 1);
11827 if (TREE_CODE (idx) != INTEGER_CST)
11829 /* From a pointer (but not array) argument extract the variable
11830 index to prevent get_addr_base_and_unit_offset() from failing
11831 due to it. Use it later to compute the non-constant offset
11832 into the string and return it to the caller. */
11833 varidx = idx;
11834 ref = TREE_OPERAND (arg, 0);
11836 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11837 return NULL_TREE;
11839 if (!integer_zerop (array_ref_low_bound (arg)))
11840 return NULL_TREE;
11842 if (!integer_onep (array_ref_element_size (arg)))
11843 return NULL_TREE;
11846 array = get_addr_base_and_unit_offset (ref, &base_off);
11847 if (!array
11848 || (TREE_CODE (array) != VAR_DECL
11849 && TREE_CODE (array) != CONST_DECL
11850 && TREE_CODE (array) != STRING_CST))
11851 return NULL_TREE;
11853 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11855 tree arg0 = TREE_OPERAND (arg, 0);
11856 tree arg1 = TREE_OPERAND (arg, 1);
11858 tree offset;
11859 tree str = string_constant (arg0, &offset, mem_size, decl);
11860 if (!str)
11862 str = string_constant (arg1, &offset, mem_size, decl);
11863 arg1 = arg0;
11866 if (str)
11868 /* Avoid pointers to arrays (see bug 86622). */
11869 if (POINTER_TYPE_P (TREE_TYPE (arg))
11870 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
11871 && !(decl && !*decl)
11872 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11873 && tree_fits_uhwi_p (*mem_size)
11874 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11875 return NULL_TREE;
11877 tree type = TREE_TYPE (offset);
11878 arg1 = fold_convert (type, arg1);
11879 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
11880 return str;
11882 return NULL_TREE;
11884 else if (TREE_CODE (arg) == SSA_NAME)
11886 gimple *stmt = SSA_NAME_DEF_STMT (arg);
11887 if (!is_gimple_assign (stmt))
11888 return NULL_TREE;
11890 tree rhs1 = gimple_assign_rhs1 (stmt);
11891 tree_code code = gimple_assign_rhs_code (stmt);
11892 if (code == ADDR_EXPR)
11893 return string_constant (rhs1, ptr_offset, mem_size, decl);
11894 else if (code != POINTER_PLUS_EXPR)
11895 return NULL_TREE;
11897 tree offset;
11898 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
11900 /* Avoid pointers to arrays (see bug 86622). */
11901 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
11902 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
11903 && !(decl && !*decl)
11904 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11905 && tree_fits_uhwi_p (*mem_size)
11906 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11907 return NULL_TREE;
11909 tree rhs2 = gimple_assign_rhs2 (stmt);
11910 tree type = TREE_TYPE (offset);
11911 rhs2 = fold_convert (type, rhs2);
11912 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
11913 return str;
11915 return NULL_TREE;
11917 else if (DECL_P (arg))
11918 array = arg;
11919 else
11920 return NULL_TREE;
11922 tree offset = wide_int_to_tree (sizetype, base_off);
11923 if (varidx)
11925 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
11926 return NULL_TREE;
11928 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
11929 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
11930 if (TREE_CODE (chartype) != INTEGER_TYPE)
11931 return NULL;
11933 offset = fold_convert (sizetype, varidx);
11936 if (TREE_CODE (array) == STRING_CST)
11938 *ptr_offset = fold_convert (sizetype, offset);
11939 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
11940 if (decl)
11941 *decl = NULL_TREE;
11942 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
11943 >= TREE_STRING_LENGTH (array));
11944 return array;
11947 tree init = ctor_for_folding (array);
11948 if (!init || init == error_mark_node)
11949 return NULL_TREE;
11951 if (valrep)
11953 HOST_WIDE_INT cstoff;
11954 if (!base_off.is_constant (&cstoff))
11955 return NULL_TREE;
11957 /* Check that the host and target are sane. */
11958 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
11959 return NULL_TREE;
11961 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
11962 if (typesz <= 0 || (int) typesz != typesz)
11963 return NULL_TREE;
11965 HOST_WIDE_INT size = typesz;
11966 if (VAR_P (array)
11967 && DECL_SIZE_UNIT (array)
11968 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
11970 size = tree_to_shwi (DECL_SIZE_UNIT (array));
11971 gcc_checking_assert (size >= typesz);
11974 /* If value representation was requested convert the initializer
11975 for the whole array or object into a string of bytes forming
11976 its value representation and return it. */
11977 unsigned char *bytes = XNEWVEC (unsigned char, size);
11978 int r = native_encode_initializer (init, bytes, size);
11979 if (r < typesz)
11981 XDELETEVEC (bytes);
11982 return NULL_TREE;
11985 if (r < size)
11986 memset (bytes + r, '\0', size - r);
11988 const char *p = reinterpret_cast<const char *>(bytes);
11989 init = build_string_literal (size, p, char_type_node);
11990 init = TREE_OPERAND (init, 0);
11991 init = TREE_OPERAND (init, 0);
11992 XDELETE (bytes);
11994 *mem_size = size_int (TREE_STRING_LENGTH (init));
11995 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
11997 if (decl)
11998 *decl = array;
12000 return init;
12003 if (TREE_CODE (init) == CONSTRUCTOR)
12005 /* Convert the 64-bit constant offset to a wider type to avoid
12006 overflow and use it to obtain the initializer for the subobject
12007 it points into. */
12008 offset_int wioff;
12009 if (!base_off.is_constant (&wioff))
12010 return NULL_TREE;
12012 wioff *= BITS_PER_UNIT;
12013 if (!wi::fits_uhwi_p (wioff))
12014 return NULL_TREE;
12016 base_off = wioff.to_uhwi ();
12017 unsigned HOST_WIDE_INT fieldoff = 0;
12018 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12019 &fieldoff);
12020 if (!init || init == error_mark_node)
12021 return NULL_TREE;
12023 HOST_WIDE_INT cstoff;
12024 if (!base_off.is_constant (&cstoff))
12025 return NULL_TREE;
12027 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12028 tree off = build_int_cst (sizetype, cstoff);
12029 if (varidx)
12030 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12031 else
12032 offset = off;
12035 *ptr_offset = offset;
12037 tree inittype = TREE_TYPE (init);
12039 if (TREE_CODE (init) == INTEGER_CST
12040 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12041 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12043 /* Check that the host and target are sane. */
12044 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12045 return NULL_TREE;
12047 /* For a reference to (address of) a single constant character,
12048 store the native representation of the character in CHARBUF.
12049 If the reference is to an element of an array or a member
12050 of a struct, only consider narrow characters until ctors
12051 for wide character arrays are transformed to STRING_CSTs
12052 like those for narrow arrays. */
12053 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12054 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12055 if (len > 0)
12057 /* Construct a string literal with elements of INITTYPE and
12058 the representation above. Then strip
12059 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12060 init = build_string_literal (len, (char *)charbuf, inittype);
12061 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12065 tree initsize = TYPE_SIZE_UNIT (inittype);
12067 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12069 /* Fold an empty/zero constructor for an implicitly initialized
12070 object or subobject into the empty string. */
12072 /* Determine the character type from that of the original
12073 expression. */
12074 tree chartype = argtype;
12075 if (POINTER_TYPE_P (chartype))
12076 chartype = TREE_TYPE (chartype);
12077 while (TREE_CODE (chartype) == ARRAY_TYPE)
12078 chartype = TREE_TYPE (chartype);
12080 if (INTEGRAL_TYPE_P (chartype)
12081 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12083 /* Convert a char array to an empty STRING_CST having an array
12084 of the expected type and size. */
12085 if (!initsize)
12086 initsize = integer_zero_node;
12088 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12089 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12090 return NULL_TREE;
12092 init = build_string_literal (size, NULL, chartype, size);
12093 init = TREE_OPERAND (init, 0);
12094 init = TREE_OPERAND (init, 0);
12096 *ptr_offset = integer_zero_node;
12100 if (decl)
12101 *decl = array;
12103 if (TREE_CODE (init) != STRING_CST)
12104 return NULL_TREE;
12106 *mem_size = initsize;
12108 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12110 return init;
12113 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12114 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12115 non-constant) offset in bytes within the string that ARG is accessing.
12116 If MEM_SIZE is non-zero the storage size of the memory is returned.
12117 If DECL is non-zero the constant declaration is returned if available. */
12119 tree
12120 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12122 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12125 /* Similar to string_constant, return a STRING_CST corresponding
12126 to the value representation of the first argument if it's
12127 a constant. */
12129 tree
12130 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12132 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12135 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12136 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12137 for C2 > 0 to x & C3 == C2
12138 for C2 < 0 to x & C3 == (C2 & C3). */
12139 enum tree_code
12140 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12142 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12143 tree treeop0 = gimple_assign_rhs1 (stmt);
12144 tree treeop1 = gimple_assign_rhs2 (stmt);
12145 tree type = TREE_TYPE (*arg0);
12146 scalar_int_mode mode;
12147 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12148 return code;
12149 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12150 || TYPE_PRECISION (type) <= 1
12151 || TYPE_UNSIGNED (type)
12152 /* Signed x % c == 0 should have been optimized into unsigned modulo
12153 earlier. */
12154 || integer_zerop (*arg1)
12155 /* If c is known to be non-negative, modulo will be expanded as unsigned
12156 modulo. */
12157 || get_range_pos_neg (treeop0) == 1)
12158 return code;
12160 /* x % c == d where d < 0 && d <= -c should be always false. */
12161 if (tree_int_cst_sgn (*arg1) == -1
12162 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12163 return code;
12165 int prec = TYPE_PRECISION (type);
12166 wide_int w = wi::to_wide (treeop1) - 1;
12167 w |= wi::shifted_mask (0, prec - 1, true, prec);
12168 tree c3 = wide_int_to_tree (type, w);
12169 tree c4 = *arg1;
12170 if (tree_int_cst_sgn (*arg1) == -1)
12171 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12173 rtx op0 = expand_normal (treeop0);
12174 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12176 bool speed_p = optimize_insn_for_speed_p ();
12178 do_pending_stack_adjust ();
12180 location_t loc = gimple_location (stmt);
12181 struct separate_ops ops;
12182 ops.code = TRUNC_MOD_EXPR;
12183 ops.location = loc;
12184 ops.type = TREE_TYPE (treeop0);
12185 ops.op0 = treeop0;
12186 ops.op1 = treeop1;
12187 ops.op2 = NULL_TREE;
12188 start_sequence ();
12189 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12190 EXPAND_NORMAL);
12191 rtx_insn *moinsns = get_insns ();
12192 end_sequence ();
12194 unsigned mocost = seq_cost (moinsns, speed_p);
12195 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12196 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12198 ops.code = BIT_AND_EXPR;
12199 ops.location = loc;
12200 ops.type = TREE_TYPE (treeop0);
12201 ops.op0 = treeop0;
12202 ops.op1 = c3;
12203 ops.op2 = NULL_TREE;
12204 start_sequence ();
12205 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12206 EXPAND_NORMAL);
12207 rtx_insn *muinsns = get_insns ();
12208 end_sequence ();
12210 unsigned mucost = seq_cost (muinsns, speed_p);
12211 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12212 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12214 if (mocost <= mucost)
12216 emit_insn (moinsns);
12217 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12218 return code;
12221 emit_insn (muinsns);
12222 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12223 *arg1 = c4;
12224 return code;
12227 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12228 If C1 is odd to:
12229 (X - C2) * C3 <= C4 (or >), where
12230 C3 is modular multiplicative inverse of C1 and 1<<prec and
12231 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12232 if C2 > ((1<<prec) - 1) % C1).
12233 If C1 is even, S = ctz (C1) and C2 is 0, use
12234 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12235 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12237 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12238 unsigned):
12239 (X * C3) + C4 <= 2 * C4, where
12240 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12241 C4 is ((1<<(prec - 1) - 1) / C1).
12242 If C1 is even, S = ctz(C1), use
12243 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12244 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12245 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12247 See the Hacker's Delight book, section 10-17. */
12248 enum tree_code
12249 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12251 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12252 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12254 if (optimize < 2)
12255 return code;
12257 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12258 if (stmt == NULL)
12259 return code;
12261 tree treeop0 = gimple_assign_rhs1 (stmt);
12262 tree treeop1 = gimple_assign_rhs2 (stmt);
12263 if (TREE_CODE (treeop0) != SSA_NAME
12264 || TREE_CODE (treeop1) != INTEGER_CST
12265 /* Don't optimize the undefined behavior case x % 0;
12266 x % 1 should have been optimized into zero, punt if
12267 it makes it here for whatever reason;
12268 x % -c should have been optimized into x % c. */
12269 || compare_tree_int (treeop1, 2) <= 0
12270 /* Likewise x % c == d where d >= c should be always false. */
12271 || tree_int_cst_le (treeop1, *arg1))
12272 return code;
12274 /* Unsigned x % pow2 is handled right already, for signed
12275 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12276 if (integer_pow2p (treeop1))
12277 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12279 tree type = TREE_TYPE (*arg0);
12280 scalar_int_mode mode;
12281 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12282 return code;
12283 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12284 || TYPE_PRECISION (type) <= 1)
12285 return code;
12287 signop sgn = UNSIGNED;
12288 /* If both operands are known to have the sign bit clear, handle
12289 even the signed modulo case as unsigned. treeop1 is always
12290 positive >= 2, checked above. */
12291 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12292 sgn = SIGNED;
12294 if (!TYPE_UNSIGNED (type))
12296 if (tree_int_cst_sgn (*arg1) == -1)
12297 return code;
12298 type = unsigned_type_for (type);
12299 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12300 return code;
12303 int prec = TYPE_PRECISION (type);
12304 wide_int w = wi::to_wide (treeop1);
12305 int shift = wi::ctz (w);
12306 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12307 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12308 If C1 is odd, we can handle all cases by subtracting
12309 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12310 e.g. by testing for overflow on the subtraction, punt on that for now
12311 though. */
12312 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12314 if (sgn == SIGNED)
12315 return code;
12316 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12317 if (wi::gtu_p (wi::to_wide (*arg1), x))
12318 return code;
12321 imm_use_iterator imm_iter;
12322 use_operand_p use_p;
12323 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12325 gimple *use_stmt = USE_STMT (use_p);
12326 /* Punt if treeop0 is used in the same bb in a division
12327 or another modulo with the same divisor. We should expect
12328 the division and modulo combined together. */
12329 if (use_stmt == stmt
12330 || gimple_bb (use_stmt) != gimple_bb (stmt))
12331 continue;
12332 if (!is_gimple_assign (use_stmt)
12333 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12334 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12335 continue;
12336 if (gimple_assign_rhs1 (use_stmt) != treeop0
12337 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12338 continue;
12339 return code;
12342 w = wi::lrshift (w, shift);
12343 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12344 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12345 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12346 tree c3 = wide_int_to_tree (type, m);
12347 tree c5 = NULL_TREE;
12348 wide_int d, e;
12349 if (sgn == UNSIGNED)
12351 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12352 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12353 otherwise use < or subtract one from C4. E.g. for
12354 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12355 x % 3U == 1 already needs to be
12356 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12357 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12358 d -= 1;
12359 if (shift)
12360 d = wi::lrshift (d, shift);
12362 else
12364 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12365 if (!shift)
12366 d = wi::lshift (e, 1);
12367 else
12369 e = wi::bit_and (e, wi::mask (shift, true, prec));
12370 d = wi::lrshift (e, shift - 1);
12372 c5 = wide_int_to_tree (type, e);
12374 tree c4 = wide_int_to_tree (type, d);
12376 rtx op0 = expand_normal (treeop0);
12377 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12379 bool speed_p = optimize_insn_for_speed_p ();
12381 do_pending_stack_adjust ();
12383 location_t loc = gimple_location (stmt);
12384 struct separate_ops ops;
12385 ops.code = TRUNC_MOD_EXPR;
12386 ops.location = loc;
12387 ops.type = TREE_TYPE (treeop0);
12388 ops.op0 = treeop0;
12389 ops.op1 = treeop1;
12390 ops.op2 = NULL_TREE;
12391 start_sequence ();
12392 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12393 EXPAND_NORMAL);
12394 rtx_insn *moinsns = get_insns ();
12395 end_sequence ();
12397 unsigned mocost = seq_cost (moinsns, speed_p);
12398 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12399 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12401 tree t = fold_convert_loc (loc, type, treeop0);
12402 if (!integer_zerop (*arg1))
12403 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12404 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12405 if (sgn == SIGNED)
12406 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12407 if (shift)
12409 tree s = build_int_cst (NULL_TREE, shift);
12410 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12413 start_sequence ();
12414 rtx mur = expand_normal (t);
12415 rtx_insn *muinsns = get_insns ();
12416 end_sequence ();
12418 unsigned mucost = seq_cost (muinsns, speed_p);
12419 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12420 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12422 if (mocost <= mucost)
12424 emit_insn (moinsns);
12425 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12426 return code;
12429 emit_insn (muinsns);
12430 *arg0 = make_tree (type, mur);
12431 *arg1 = c4;
12432 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12435 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12437 void
12438 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12440 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12441 || code == LT_EXPR || code == LE_EXPR);
12442 gcc_checking_assert (integer_zerop (*arg1));
12444 if (!optimize)
12445 return;
12447 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12448 if (stmt == NULL)
12449 return;
12451 tree treeop0 = gimple_assign_rhs1 (stmt);
12452 tree treeop1 = gimple_assign_rhs2 (stmt);
12453 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12454 return;
12456 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12457 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12458 "assuming signed overflow does not occur when "
12459 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12460 op_symbol_code (code), op_symbol_code (code));
12462 *arg0 = treeop0;
12463 *arg1 = treeop1;
12466 /* Generate code to calculate OPS, and exploded expression
12467 using a store-flag instruction and return an rtx for the result.
12468 OPS reflects a comparison.
12470 If TARGET is nonzero, store the result there if convenient.
12472 Return zero if there is no suitable set-flag instruction
12473 available on this machine.
12475 Once expand_expr has been called on the arguments of the comparison,
12476 we are committed to doing the store flag, since it is not safe to
12477 re-evaluate the expression. We emit the store-flag insn by calling
12478 emit_store_flag, but only expand the arguments if we have a reason
12479 to believe that emit_store_flag will be successful. If we think that
12480 it will, but it isn't, we have to simulate the store-flag with a
12481 set/jump/set sequence. */
12483 static rtx
12484 do_store_flag (sepops ops, rtx target, machine_mode mode)
12486 enum rtx_code code;
12487 tree arg0, arg1, type;
12488 machine_mode operand_mode;
12489 int unsignedp;
12490 rtx op0, op1;
12491 rtx subtarget = target;
12492 location_t loc = ops->location;
12494 arg0 = ops->op0;
12495 arg1 = ops->op1;
12497 /* Don't crash if the comparison was erroneous. */
12498 if (arg0 == error_mark_node || arg1 == error_mark_node)
12499 return const0_rtx;
12501 type = TREE_TYPE (arg0);
12502 operand_mode = TYPE_MODE (type);
12503 unsignedp = TYPE_UNSIGNED (type);
12505 /* We won't bother with BLKmode store-flag operations because it would mean
12506 passing a lot of information to emit_store_flag. */
12507 if (operand_mode == BLKmode)
12508 return 0;
12510 /* We won't bother with store-flag operations involving function pointers
12511 when function pointers must be canonicalized before comparisons. */
12512 if (targetm.have_canonicalize_funcptr_for_compare ()
12513 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12514 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12515 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12516 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12517 return 0;
12519 STRIP_NOPS (arg0);
12520 STRIP_NOPS (arg1);
12522 /* For vector typed comparisons emit code to generate the desired
12523 all-ones or all-zeros mask. */
12524 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12526 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12527 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12528 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12529 return expand_vec_cmp_expr (ops->type, ifexp, target);
12530 else
12531 gcc_unreachable ();
12534 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12535 into (x - C2) * C3 < C4. */
12536 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12537 && TREE_CODE (arg0) == SSA_NAME
12538 && TREE_CODE (arg1) == INTEGER_CST)
12540 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12541 &arg0, &arg1);
12542 if (new_code != ops->code)
12544 struct separate_ops nops = *ops;
12545 nops.code = ops->code = new_code;
12546 nops.op0 = arg0;
12547 nops.op1 = arg1;
12548 nops.type = TREE_TYPE (arg0);
12549 return do_store_flag (&nops, target, mode);
12553 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12554 if (!unsignedp
12555 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12556 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12557 && integer_zerop (arg1)
12558 && TREE_CODE (arg0) == SSA_NAME)
12559 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12561 /* Get the rtx comparison code to use. We know that EXP is a comparison
12562 operation of some type. Some comparisons against 1 and -1 can be
12563 converted to comparisons with zero. Do so here so that the tests
12564 below will be aware that we have a comparison with zero. These
12565 tests will not catch constants in the first operand, but constants
12566 are rarely passed as the first operand. */
12568 switch (ops->code)
12570 case EQ_EXPR:
12571 code = EQ;
12572 break;
12573 case NE_EXPR:
12574 code = NE;
12575 break;
12576 case LT_EXPR:
12577 if (integer_onep (arg1))
12578 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12579 else
12580 code = unsignedp ? LTU : LT;
12581 break;
12582 case LE_EXPR:
12583 if (! unsignedp && integer_all_onesp (arg1))
12584 arg1 = integer_zero_node, code = LT;
12585 else
12586 code = unsignedp ? LEU : LE;
12587 break;
12588 case GT_EXPR:
12589 if (! unsignedp && integer_all_onesp (arg1))
12590 arg1 = integer_zero_node, code = GE;
12591 else
12592 code = unsignedp ? GTU : GT;
12593 break;
12594 case GE_EXPR:
12595 if (integer_onep (arg1))
12596 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12597 else
12598 code = unsignedp ? GEU : GE;
12599 break;
12601 case UNORDERED_EXPR:
12602 code = UNORDERED;
12603 break;
12604 case ORDERED_EXPR:
12605 code = ORDERED;
12606 break;
12607 case UNLT_EXPR:
12608 code = UNLT;
12609 break;
12610 case UNLE_EXPR:
12611 code = UNLE;
12612 break;
12613 case UNGT_EXPR:
12614 code = UNGT;
12615 break;
12616 case UNGE_EXPR:
12617 code = UNGE;
12618 break;
12619 case UNEQ_EXPR:
12620 code = UNEQ;
12621 break;
12622 case LTGT_EXPR:
12623 code = LTGT;
12624 break;
12626 default:
12627 gcc_unreachable ();
12630 /* Put a constant second. */
12631 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12632 || TREE_CODE (arg0) == FIXED_CST)
12634 std::swap (arg0, arg1);
12635 code = swap_condition (code);
12638 /* If this is an equality or inequality test of a single bit, we can
12639 do this by shifting the bit being tested to the low-order bit and
12640 masking the result with the constant 1. If the condition was EQ,
12641 we xor it with 1. This does not require an scc insn and is faster
12642 than an scc insn even if we have it.
12644 The code to make this transformation was moved into fold_single_bit_test,
12645 so we just call into the folder and expand its result. */
12647 if ((code == NE || code == EQ)
12648 && integer_zerop (arg1)
12649 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12651 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12652 if (srcstmt
12653 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12655 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12656 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12657 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12658 gimple_assign_rhs1 (srcstmt),
12659 gimple_assign_rhs2 (srcstmt));
12660 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12661 if (temp)
12662 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12666 if (! get_subtarget (target)
12667 || GET_MODE (subtarget) != operand_mode)
12668 subtarget = 0;
12670 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12672 if (target == 0)
12673 target = gen_reg_rtx (mode);
12675 /* Try a cstore if possible. */
12676 return emit_store_flag_force (target, code, op0, op1,
12677 operand_mode, unsignedp,
12678 (TYPE_PRECISION (ops->type) == 1
12679 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12682 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12683 0 otherwise (i.e. if there is no casesi instruction).
12685 DEFAULT_PROBABILITY is the probability of jumping to the default
12686 label. */
12688 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12689 rtx table_label, rtx default_label, rtx fallback_label,
12690 profile_probability default_probability)
12692 class expand_operand ops[5];
12693 scalar_int_mode index_mode = SImode;
12694 rtx op1, op2, index;
12696 if (! targetm.have_casesi ())
12697 return 0;
12699 /* The index must be some form of integer. Convert it to SImode. */
12700 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12701 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12703 rtx rangertx = expand_normal (range);
12705 /* We must handle the endpoints in the original mode. */
12706 index_expr = build2 (MINUS_EXPR, index_type,
12707 index_expr, minval);
12708 minval = integer_zero_node;
12709 index = expand_normal (index_expr);
12710 if (default_label)
12711 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12712 omode, 1, default_label,
12713 default_probability);
12714 /* Now we can safely truncate. */
12715 index = convert_to_mode (index_mode, index, 0);
12717 else
12719 if (omode != index_mode)
12721 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12722 index_expr = fold_convert (index_type, index_expr);
12725 index = expand_normal (index_expr);
12728 do_pending_stack_adjust ();
12730 op1 = expand_normal (minval);
12731 op2 = expand_normal (range);
12733 create_input_operand (&ops[0], index, index_mode);
12734 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12735 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12736 create_fixed_operand (&ops[3], table_label);
12737 create_fixed_operand (&ops[4], (default_label
12738 ? default_label
12739 : fallback_label));
12740 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12741 return 1;
12744 /* Attempt to generate a tablejump instruction; same concept. */
12745 /* Subroutine of the next function.
12747 INDEX is the value being switched on, with the lowest value
12748 in the table already subtracted.
12749 MODE is its expected mode (needed if INDEX is constant).
12750 RANGE is the length of the jump table.
12751 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12753 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12754 index value is out of range.
12755 DEFAULT_PROBABILITY is the probability of jumping to
12756 the default label. */
12758 static void
12759 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12760 rtx default_label, profile_probability default_probability)
12762 rtx temp, vector;
12764 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12765 cfun->cfg->max_jumptable_ents = INTVAL (range);
12767 /* Do an unsigned comparison (in the proper mode) between the index
12768 expression and the value which represents the length of the range.
12769 Since we just finished subtracting the lower bound of the range
12770 from the index expression, this comparison allows us to simultaneously
12771 check that the original index expression value is both greater than
12772 or equal to the minimum value of the range and less than or equal to
12773 the maximum value of the range. */
12775 if (default_label)
12776 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12777 default_label, default_probability);
12779 /* If index is in range, it must fit in Pmode.
12780 Convert to Pmode so we can index with it. */
12781 if (mode != Pmode)
12783 unsigned int width;
12785 /* We know the value of INDEX is between 0 and RANGE. If we have a
12786 sign-extended subreg, and RANGE does not have the sign bit set, then
12787 we have a value that is valid for both sign and zero extension. In
12788 this case, we get better code if we sign extend. */
12789 if (GET_CODE (index) == SUBREG
12790 && SUBREG_PROMOTED_VAR_P (index)
12791 && SUBREG_PROMOTED_SIGNED_P (index)
12792 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
12793 <= HOST_BITS_PER_WIDE_INT)
12794 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
12795 index = convert_to_mode (Pmode, index, 0);
12796 else
12797 index = convert_to_mode (Pmode, index, 1);
12800 /* Don't let a MEM slip through, because then INDEX that comes
12801 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
12802 and break_out_memory_refs will go to work on it and mess it up. */
12803 #ifdef PIC_CASE_VECTOR_ADDRESS
12804 if (flag_pic && !REG_P (index))
12805 index = copy_to_mode_reg (Pmode, index);
12806 #endif
12808 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
12809 GET_MODE_SIZE, because this indicates how large insns are. The other
12810 uses should all be Pmode, because they are addresses. This code
12811 could fail if addresses and insns are not the same size. */
12812 index = simplify_gen_binary (MULT, Pmode, index,
12813 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
12814 Pmode));
12815 index = simplify_gen_binary (PLUS, Pmode, index,
12816 gen_rtx_LABEL_REF (Pmode, table_label));
12818 #ifdef PIC_CASE_VECTOR_ADDRESS
12819 if (flag_pic)
12820 index = PIC_CASE_VECTOR_ADDRESS (index);
12821 else
12822 #endif
12823 index = memory_address (CASE_VECTOR_MODE, index);
12824 temp = gen_reg_rtx (CASE_VECTOR_MODE);
12825 vector = gen_const_mem (CASE_VECTOR_MODE, index);
12826 convert_move (temp, vector, 0);
12828 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
12830 /* If we are generating PIC code or if the table is PC-relative, the
12831 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
12832 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
12833 emit_barrier ();
12837 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
12838 rtx table_label, rtx default_label,
12839 profile_probability default_probability)
12841 rtx index;
12843 if (! targetm.have_tablejump ())
12844 return 0;
12846 index_expr = fold_build2 (MINUS_EXPR, index_type,
12847 fold_convert (index_type, index_expr),
12848 fold_convert (index_type, minval));
12849 index = expand_normal (index_expr);
12850 do_pending_stack_adjust ();
12852 do_tablejump (index, TYPE_MODE (index_type),
12853 convert_modes (TYPE_MODE (index_type),
12854 TYPE_MODE (TREE_TYPE (range)),
12855 expand_normal (range),
12856 TYPE_UNSIGNED (TREE_TYPE (range))),
12857 table_label, default_label, default_probability);
12858 return 1;
12861 /* Return a CONST_VECTOR rtx representing vector mask for
12862 a VECTOR_CST of booleans. */
12863 static rtx
12864 const_vector_mask_from_tree (tree exp)
12866 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12867 machine_mode inner = GET_MODE_INNER (mode);
12869 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12870 VECTOR_CST_NELTS_PER_PATTERN (exp));
12871 unsigned int count = builder.encoded_nelts ();
12872 for (unsigned int i = 0; i < count; ++i)
12874 tree elt = VECTOR_CST_ELT (exp, i);
12875 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
12876 if (integer_zerop (elt))
12877 builder.quick_push (CONST0_RTX (inner));
12878 else if (integer_onep (elt)
12879 || integer_minus_onep (elt))
12880 builder.quick_push (CONSTM1_RTX (inner));
12881 else
12882 gcc_unreachable ();
12884 return builder.build ();
12887 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
12888 static rtx
12889 const_vector_from_tree (tree exp)
12891 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12893 if (initializer_zerop (exp))
12894 return CONST0_RTX (mode);
12896 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
12897 return const_vector_mask_from_tree (exp);
12899 machine_mode inner = GET_MODE_INNER (mode);
12901 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12902 VECTOR_CST_NELTS_PER_PATTERN (exp));
12903 unsigned int count = builder.encoded_nelts ();
12904 for (unsigned int i = 0; i < count; ++i)
12906 tree elt = VECTOR_CST_ELT (exp, i);
12907 if (TREE_CODE (elt) == REAL_CST)
12908 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
12909 inner));
12910 else if (TREE_CODE (elt) == FIXED_CST)
12911 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
12912 inner));
12913 else
12914 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
12915 inner));
12917 return builder.build ();
12920 /* Build a decl for a personality function given a language prefix. */
12922 tree
12923 build_personality_function (const char *lang)
12925 const char *unwind_and_version;
12926 tree decl, type;
12927 char *name;
12929 switch (targetm_common.except_unwind_info (&global_options))
12931 case UI_NONE:
12932 return NULL;
12933 case UI_SJLJ:
12934 unwind_and_version = "_sj0";
12935 break;
12936 case UI_DWARF2:
12937 case UI_TARGET:
12938 unwind_and_version = "_v0";
12939 break;
12940 case UI_SEH:
12941 unwind_and_version = "_seh0";
12942 break;
12943 default:
12944 gcc_unreachable ();
12947 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
12949 type = build_function_type_list (unsigned_type_node,
12950 integer_type_node, integer_type_node,
12951 long_long_unsigned_type_node,
12952 ptr_type_node, ptr_type_node, NULL_TREE);
12953 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
12954 get_identifier (name), type);
12955 DECL_ARTIFICIAL (decl) = 1;
12956 DECL_EXTERNAL (decl) = 1;
12957 TREE_PUBLIC (decl) = 1;
12959 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
12960 are the flags assigned by targetm.encode_section_info. */
12961 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
12963 return decl;
12966 /* Extracts the personality function of DECL and returns the corresponding
12967 libfunc. */
12970 get_personality_function (tree decl)
12972 tree personality = DECL_FUNCTION_PERSONALITY (decl);
12973 enum eh_personality_kind pk;
12975 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
12976 if (pk == eh_personality_none)
12977 return NULL;
12979 if (!personality
12980 && pk == eh_personality_any)
12981 personality = lang_hooks.eh_personality ();
12983 if (pk == eh_personality_lang)
12984 gcc_assert (personality != NULL_TREE);
12986 return XEXP (DECL_RTL (personality), 0);
12989 /* Returns a tree for the size of EXP in bytes. */
12991 static tree
12992 tree_expr_size (const_tree exp)
12994 if (DECL_P (exp)
12995 && DECL_SIZE_UNIT (exp) != 0)
12996 return DECL_SIZE_UNIT (exp);
12997 else
12998 return size_in_bytes (TREE_TYPE (exp));
13001 /* Return an rtx for the size in bytes of the value of EXP. */
13004 expr_size (tree exp)
13006 tree size;
13008 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13009 size = TREE_OPERAND (exp, 1);
13010 else
13012 size = tree_expr_size (exp);
13013 gcc_assert (size);
13014 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13017 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13020 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13021 if the size can vary or is larger than an integer. */
13023 static HOST_WIDE_INT
13024 int_expr_size (tree exp)
13026 tree size;
13028 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13029 size = TREE_OPERAND (exp, 1);
13030 else
13032 size = tree_expr_size (exp);
13033 gcc_assert (size);
13036 if (size == 0 || !tree_fits_shwi_p (size))
13037 return -1;
13039 return tree_to_shwi (size);