Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / expr.c
blobe0bcbccd9053df168c2e861414729fc7cf017f85
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 scalar_int_mode int_orig_mode;
240 scalar_int_mode int_inner_mode;
241 machine_mode orig_mode = GET_MODE (from);
243 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
244 from_mode = to_int_mode;
246 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
247 the original mode, but narrower than the inner mode. */
248 if (GET_CODE (from) == SUBREG
249 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
250 && GET_MODE_PRECISION (to_int_mode)
251 > GET_MODE_PRECISION (int_orig_mode)
252 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)),
253 &int_inner_mode)
254 && GET_MODE_PRECISION (int_inner_mode)
255 > GET_MODE_PRECISION (to_int_mode))
257 SUBREG_PROMOTED_VAR_P (from) = 1;
258 SUBREG_PROMOTED_SET (from, unsignedp);
262 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
264 if (to_mode == from_mode
265 || (from_mode == VOIDmode && CONSTANT_P (from)))
267 emit_move_insn (to, from);
268 return;
271 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
273 if (GET_MODE_UNIT_PRECISION (to_mode)
274 > GET_MODE_UNIT_PRECISION (from_mode))
276 optab op = unsignedp ? zext_optab : sext_optab;
277 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
278 if (icode != CODE_FOR_nothing)
280 emit_unop_insn (icode, to, from,
281 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
282 return;
286 if (GET_MODE_UNIT_PRECISION (to_mode)
287 < GET_MODE_UNIT_PRECISION (from_mode))
289 insn_code icode = convert_optab_handler (trunc_optab,
290 to_mode, from_mode);
291 if (icode != CODE_FOR_nothing)
293 emit_unop_insn (icode, to, from, TRUNCATE);
294 return;
298 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
299 GET_MODE_BITSIZE (to_mode)));
301 if (VECTOR_MODE_P (to_mode))
302 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
303 else
304 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
306 emit_move_insn (to, from);
307 return;
310 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
312 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
313 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
314 return;
317 convert_mode_scalar (to, from, unsignedp);
320 /* Like convert_move, but deals only with scalar modes. */
322 static void
323 convert_mode_scalar (rtx to, rtx from, int unsignedp)
325 /* Both modes should be scalar types. */
326 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
327 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
328 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
329 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
330 enum insn_code code;
331 rtx libcall;
333 gcc_assert (to_real == from_real);
335 /* rtx code for making an equivalent value. */
336 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
337 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
339 if (to_real)
341 rtx value;
342 rtx_insn *insns;
343 convert_optab tab;
345 gcc_assert ((GET_MODE_PRECISION (from_mode)
346 != GET_MODE_PRECISION (to_mode))
347 || (DECIMAL_FLOAT_MODE_P (from_mode)
348 != DECIMAL_FLOAT_MODE_P (to_mode)));
350 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
351 /* Conversion between decimal float and binary float, same size. */
352 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
353 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
354 tab = sext_optab;
355 else
356 tab = trunc_optab;
358 /* Try converting directly if the insn is supported. */
360 code = convert_optab_handler (tab, to_mode, from_mode);
361 if (code != CODE_FOR_nothing)
363 emit_unop_insn (code, to, from,
364 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
365 return;
368 /* Otherwise use a libcall. */
369 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
371 /* Is this conversion implemented yet? */
372 gcc_assert (libcall);
374 start_sequence ();
375 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
376 from, from_mode);
377 insns = get_insns ();
378 end_sequence ();
379 emit_libcall_block (insns, to, value,
380 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
381 from)
382 : gen_rtx_FLOAT_EXTEND (to_mode, from));
383 return;
386 /* Handle pointer conversion. */ /* SPEE 900220. */
387 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
389 convert_optab ctab;
391 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
392 ctab = trunc_optab;
393 else if (unsignedp)
394 ctab = zext_optab;
395 else
396 ctab = sext_optab;
398 if (convert_optab_handler (ctab, to_mode, from_mode)
399 != CODE_FOR_nothing)
401 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
402 to, from, UNKNOWN);
403 return;
407 /* Targets are expected to provide conversion insns between PxImode and
408 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
409 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
411 scalar_int_mode full_mode
412 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
414 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
415 != CODE_FOR_nothing);
417 if (full_mode != from_mode)
418 from = convert_to_mode (full_mode, from, unsignedp);
419 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
420 to, from, UNKNOWN);
421 return;
423 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
425 rtx new_from;
426 scalar_int_mode full_mode
427 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
428 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
429 enum insn_code icode;
431 icode = convert_optab_handler (ctab, full_mode, from_mode);
432 gcc_assert (icode != CODE_FOR_nothing);
434 if (to_mode == full_mode)
436 emit_unop_insn (icode, to, from, UNKNOWN);
437 return;
440 new_from = gen_reg_rtx (full_mode);
441 emit_unop_insn (icode, new_from, from, UNKNOWN);
443 /* else proceed to integer conversions below. */
444 from_mode = full_mode;
445 from = new_from;
448 /* Make sure both are fixed-point modes or both are not. */
449 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
450 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
451 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
453 /* If we widen from_mode to to_mode and they are in the same class,
454 we won't saturate the result.
455 Otherwise, always saturate the result to play safe. */
456 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
457 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
458 expand_fixed_convert (to, from, 0, 0);
459 else
460 expand_fixed_convert (to, from, 0, 1);
461 return;
464 /* Now both modes are integers. */
466 /* Handle expanding beyond a word. */
467 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
468 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
470 rtx_insn *insns;
471 rtx lowpart;
472 rtx fill_value;
473 rtx lowfrom;
474 int i;
475 scalar_mode lowpart_mode;
476 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
478 /* Try converting directly if the insn is supported. */
479 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
480 != CODE_FOR_nothing)
482 /* If FROM is a SUBREG, put it into a register. Do this
483 so that we always generate the same set of insns for
484 better cse'ing; if an intermediate assignment occurred,
485 we won't be doing the operation directly on the SUBREG. */
486 if (optimize > 0 && GET_CODE (from) == SUBREG)
487 from = force_reg (from_mode, from);
488 emit_unop_insn (code, to, from, equiv_code);
489 return;
491 /* Next, try converting via full word. */
492 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
493 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
494 != CODE_FOR_nothing))
496 rtx word_to = gen_reg_rtx (word_mode);
497 if (REG_P (to))
499 if (reg_overlap_mentioned_p (to, from))
500 from = force_reg (from_mode, from);
501 emit_clobber (to);
503 convert_move (word_to, from, unsignedp);
504 emit_unop_insn (code, to, word_to, equiv_code);
505 return;
508 /* No special multiword conversion insn; do it by hand. */
509 start_sequence ();
511 /* Since we will turn this into a no conflict block, we must ensure
512 the source does not overlap the target so force it into an isolated
513 register when maybe so. Likewise for any MEM input, since the
514 conversion sequence might require several references to it and we
515 must ensure we're getting the same value every time. */
517 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
518 from = force_reg (from_mode, from);
520 /* Get a copy of FROM widened to a word, if necessary. */
521 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
522 lowpart_mode = word_mode;
523 else
524 lowpart_mode = from_mode;
526 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
528 lowpart = gen_lowpart (lowpart_mode, to);
529 emit_move_insn (lowpart, lowfrom);
531 /* Compute the value to put in each remaining word. */
532 if (unsignedp)
533 fill_value = const0_rtx;
534 else
535 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
536 LT, lowfrom, const0_rtx,
537 lowpart_mode, 0, -1);
539 /* Fill the remaining words. */
540 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
542 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
543 rtx subword = operand_subword (to, index, 1, to_mode);
545 gcc_assert (subword);
547 if (fill_value != subword)
548 emit_move_insn (subword, fill_value);
551 insns = get_insns ();
552 end_sequence ();
554 emit_insn (insns);
555 return;
558 /* Truncating multi-word to a word or less. */
559 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
560 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
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 convert_move (to, gen_lowpart (word_mode, from), 0);
571 return;
574 /* Now follow all the conversions between integers
575 no more than a word long. */
577 /* For truncation, usually we can just refer to FROM in a narrower mode. */
578 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
579 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
581 if (!((MEM_P (from)
582 && ! MEM_VOLATILE_P (from)
583 && direct_load[(int) to_mode]
584 && ! mode_dependent_address_p (XEXP (from, 0),
585 MEM_ADDR_SPACE (from)))
586 || REG_P (from)
587 || GET_CODE (from) == SUBREG))
588 from = force_reg (from_mode, from);
589 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
590 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
591 from = copy_to_reg (from);
592 emit_move_insn (to, gen_lowpart (to_mode, from));
593 return;
596 /* Handle extension. */
597 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
599 /* Convert directly if that works. */
600 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
601 != CODE_FOR_nothing)
603 emit_unop_insn (code, to, from, equiv_code);
604 return;
606 else
608 rtx tmp;
609 int shift_amount;
611 /* Search for a mode to convert via. */
612 opt_scalar_mode intermediate_iter;
613 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
615 scalar_mode intermediate = intermediate_iter.require ();
616 if (((can_extend_p (to_mode, intermediate, unsignedp)
617 != CODE_FOR_nothing)
618 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
619 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
620 intermediate)))
621 && (can_extend_p (intermediate, from_mode, unsignedp)
622 != CODE_FOR_nothing))
624 convert_move (to, convert_to_mode (intermediate, from,
625 unsignedp), unsignedp);
626 return;
630 /* No suitable intermediate mode.
631 Generate what we need with shifts. */
632 shift_amount = (GET_MODE_PRECISION (to_mode)
633 - GET_MODE_PRECISION (from_mode));
634 from = gen_lowpart (to_mode, force_reg (from_mode, from));
635 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
636 to, unsignedp);
637 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
638 to, unsignedp);
639 if (tmp != to)
640 emit_move_insn (to, tmp);
641 return;
645 /* Support special truncate insns for certain modes. */
646 if (convert_optab_handler (trunc_optab, to_mode,
647 from_mode) != CODE_FOR_nothing)
649 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
650 to, from, UNKNOWN);
651 return;
654 /* Handle truncation of volatile memrefs, and so on;
655 the things that couldn't be truncated directly,
656 and for which there was no special instruction.
658 ??? Code above formerly short-circuited this, for most integer
659 mode pairs, with a force_reg in from_mode followed by a recursive
660 call to this routine. Appears always to have been wrong. */
661 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
663 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
664 emit_move_insn (to, temp);
665 return;
668 /* Mode combination is not recognized. */
669 gcc_unreachable ();
672 /* Return an rtx for a value that would result
673 from converting X to mode MODE.
674 Both X and MODE may be floating, or both integer.
675 UNSIGNEDP is nonzero if X is an unsigned value.
676 This can be done by referring to a part of X in place
677 or by copying to a new temporary with conversion. */
680 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
682 return convert_modes (mode, VOIDmode, x, unsignedp);
685 /* Return an rtx for a value that would result
686 from converting X from mode OLDMODE to mode MODE.
687 Both modes may be floating, or both integer.
688 UNSIGNEDP is nonzero if X is an unsigned value.
690 This can be done by referring to a part of X in place
691 or by copying to a new temporary with conversion.
693 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
696 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
698 rtx temp;
699 scalar_int_mode int_mode;
701 /* If FROM is a SUBREG that indicates that we have already done at least
702 the required extension, strip it. */
704 if (GET_CODE (x) == SUBREG
705 && SUBREG_PROMOTED_VAR_P (x)
706 && is_a <scalar_int_mode> (mode, &int_mode)
707 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
708 >= GET_MODE_PRECISION (int_mode))
709 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
711 scalar_int_mode int_orig_mode;
712 scalar_int_mode int_inner_mode;
713 machine_mode orig_mode = GET_MODE (x);
714 x = gen_lowpart (int_mode, SUBREG_REG (x));
716 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
717 the original mode, but narrower than the inner mode. */
718 if (GET_CODE (x) == SUBREG
719 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
720 && GET_MODE_PRECISION (int_mode)
721 > GET_MODE_PRECISION (int_orig_mode)
722 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
723 &int_inner_mode)
724 && GET_MODE_PRECISION (int_inner_mode)
725 > GET_MODE_PRECISION (int_mode))
727 SUBREG_PROMOTED_VAR_P (x) = 1;
728 SUBREG_PROMOTED_SET (x, unsignedp);
732 if (GET_MODE (x) != VOIDmode)
733 oldmode = GET_MODE (x);
735 if (mode == oldmode)
736 return x;
738 if (CONST_SCALAR_INT_P (x)
739 && is_a <scalar_int_mode> (mode, &int_mode))
741 /* If the caller did not tell us the old mode, then there is not
742 much to do with respect to canonicalization. We have to
743 assume that all the bits are significant. */
744 if (!is_a <scalar_int_mode> (oldmode))
745 oldmode = MAX_MODE_INT;
746 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
747 GET_MODE_PRECISION (int_mode),
748 unsignedp ? UNSIGNED : SIGNED);
749 return immed_wide_int_const (w, int_mode);
752 /* We can do this with a gen_lowpart if both desired and current modes
753 are integer, and this is either a constant integer, a register, or a
754 non-volatile MEM. */
755 scalar_int_mode int_oldmode;
756 if (is_int_mode (mode, &int_mode)
757 && is_int_mode (oldmode, &int_oldmode)
758 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
759 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
760 || CONST_POLY_INT_P (x)
761 || (REG_P (x)
762 && (!HARD_REGISTER_P (x)
763 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
764 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
765 return gen_lowpart (int_mode, x);
767 /* Converting from integer constant into mode is always equivalent to an
768 subreg operation. */
769 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
771 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
772 GET_MODE_BITSIZE (oldmode)));
773 return simplify_gen_subreg (mode, x, oldmode, 0);
776 temp = gen_reg_rtx (mode);
777 convert_move (temp, x, unsignedp);
778 return temp;
781 /* Return the largest alignment we can use for doing a move (or store)
782 of MAX_PIECES. ALIGN is the largest alignment we could use. */
784 static unsigned int
785 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
787 scalar_int_mode tmode
788 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
790 if (align >= GET_MODE_ALIGNMENT (tmode))
791 align = GET_MODE_ALIGNMENT (tmode);
792 else
794 scalar_int_mode xmode = NARROWEST_INT_MODE;
795 opt_scalar_int_mode mode_iter;
796 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
798 tmode = mode_iter.require ();
799 if (GET_MODE_SIZE (tmode) > max_pieces
800 || targetm.slow_unaligned_access (tmode, align))
801 break;
802 xmode = tmode;
805 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
808 return align;
811 /* Return the widest QI vector, if QI_MODE is true, or integer mode
812 that is narrower than SIZE bytes. */
814 static fixed_size_mode
815 widest_fixed_size_mode_for_size (unsigned int size, bool qi_vector)
817 fixed_size_mode result = NARROWEST_INT_MODE;
819 gcc_checking_assert (size > 1);
821 /* Use QI vector only if size is wider than a WORD. */
822 if (qi_vector && size > UNITS_PER_WORD)
824 machine_mode mode;
825 fixed_size_mode candidate;
826 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
827 if (is_a<fixed_size_mode> (mode, &candidate)
828 && GET_MODE_INNER (candidate) == QImode)
830 if (GET_MODE_SIZE (candidate) >= size)
831 break;
832 if (optab_handler (vec_duplicate_optab, candidate)
833 != CODE_FOR_nothing)
834 result = candidate;
837 if (result != NARROWEST_INT_MODE)
838 return result;
841 opt_scalar_int_mode tmode;
842 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
843 if (GET_MODE_SIZE (tmode.require ()) < size)
844 result = tmode.require ();
846 return result;
849 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
850 and should be performed piecewise. */
852 static bool
853 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
854 enum by_pieces_operation op)
856 return targetm.use_by_pieces_infrastructure_p (len, align, op,
857 optimize_insn_for_speed_p ());
860 /* Determine whether the LEN bytes can be moved by using several move
861 instructions. Return nonzero if a call to move_by_pieces should
862 succeed. */
864 bool
865 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
867 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
870 /* Return number of insns required to perform operation OP by pieces
871 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
873 unsigned HOST_WIDE_INT
874 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
875 unsigned int max_size, by_pieces_operation op)
877 unsigned HOST_WIDE_INT n_insns = 0;
878 fixed_size_mode mode;
880 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
882 /* NB: Round up L and ALIGN to the widest integer mode for
883 MAX_SIZE. */
884 mode = widest_fixed_size_mode_for_size (max_size,
885 op == SET_BY_PIECES);
886 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
888 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
889 if (up > l)
890 l = up;
891 align = GET_MODE_ALIGNMENT (mode);
895 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
897 while (max_size > 1 && l > 0)
899 mode = widest_fixed_size_mode_for_size (max_size,
900 op == SET_BY_PIECES);
901 enum insn_code icode;
903 unsigned int modesize = GET_MODE_SIZE (mode);
905 icode = optab_handler (mov_optab, mode);
906 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
908 unsigned HOST_WIDE_INT n_pieces = l / modesize;
909 l %= modesize;
910 switch (op)
912 default:
913 n_insns += n_pieces;
914 break;
916 case COMPARE_BY_PIECES:
917 int batch = targetm.compare_by_pieces_branch_ratio (mode);
918 int batch_ops = 4 * batch - 1;
919 unsigned HOST_WIDE_INT full = n_pieces / batch;
920 n_insns += full * batch_ops;
921 if (n_pieces % batch != 0)
922 n_insns++;
923 break;
927 max_size = modesize;
930 gcc_assert (!l);
931 return n_insns;
934 /* Used when performing piecewise block operations, holds information
935 about one of the memory objects involved. The member functions
936 can be used to generate code for loading from the object and
937 updating the address when iterating. */
939 class pieces_addr
941 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
942 stack pushes. */
943 rtx m_obj;
944 /* The address of the object. Can differ from that seen in the
945 MEM rtx if we copied the address to a register. */
946 rtx m_addr;
947 /* Nonzero if the address on the object has an autoincrement already,
948 signifies whether that was an increment or decrement. */
949 signed char m_addr_inc;
950 /* Nonzero if we intend to use autoinc without the address already
951 having autoinc form. We will insert add insns around each memory
952 reference, expecting later passes to form autoinc addressing modes.
953 The only supported options are predecrement and postincrement. */
954 signed char m_explicit_inc;
955 /* True if we have either of the two possible cases of using
956 autoincrement. */
957 bool m_auto;
958 /* True if this is an address to be used for load operations rather
959 than stores. */
960 bool m_is_load;
962 /* Optionally, a function to obtain constants for any given offset into
963 the objects, and data associated with it. */
964 by_pieces_constfn m_constfn;
965 void *m_cfndata;
966 public:
967 pieces_addr (rtx, bool, by_pieces_constfn, void *);
968 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
969 void increment_address (HOST_WIDE_INT);
970 void maybe_predec (HOST_WIDE_INT);
971 void maybe_postinc (HOST_WIDE_INT);
972 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
973 int get_addr_inc ()
975 return m_addr_inc;
979 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
980 true if the operation to be performed on this object is a load
981 rather than a store. For stores, OBJ can be NULL, in which case we
982 assume the operation is a stack push. For loads, the optional
983 CONSTFN and its associated CFNDATA can be used in place of the
984 memory load. */
986 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
987 void *cfndata)
988 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
990 m_addr_inc = 0;
991 m_auto = false;
992 if (obj)
994 rtx addr = XEXP (obj, 0);
995 rtx_code code = GET_CODE (addr);
996 m_addr = addr;
997 bool dec = code == PRE_DEC || code == POST_DEC;
998 bool inc = code == PRE_INC || code == POST_INC;
999 m_auto = inc || dec;
1000 if (m_auto)
1001 m_addr_inc = dec ? -1 : 1;
1003 /* While we have always looked for these codes here, the code
1004 implementing the memory operation has never handled them.
1005 Support could be added later if necessary or beneficial. */
1006 gcc_assert (code != PRE_INC && code != POST_DEC);
1008 else
1010 m_addr = NULL_RTX;
1011 if (!is_load)
1013 m_auto = true;
1014 if (STACK_GROWS_DOWNWARD)
1015 m_addr_inc = -1;
1016 else
1017 m_addr_inc = 1;
1019 else
1020 gcc_assert (constfn != NULL);
1022 m_explicit_inc = 0;
1023 if (constfn)
1024 gcc_assert (is_load);
1027 /* Decide whether to use autoinc for an address involved in a memory op.
1028 MODE is the mode of the accesses, REVERSE is true if we've decided to
1029 perform the operation starting from the end, and LEN is the length of
1030 the operation. Don't override an earlier decision to set m_auto. */
1032 void
1033 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1034 HOST_WIDE_INT len)
1036 if (m_auto || m_obj == NULL_RTX)
1037 return;
1039 bool use_predec = (m_is_load
1040 ? USE_LOAD_PRE_DECREMENT (mode)
1041 : USE_STORE_PRE_DECREMENT (mode));
1042 bool use_postinc = (m_is_load
1043 ? USE_LOAD_POST_INCREMENT (mode)
1044 : USE_STORE_POST_INCREMENT (mode));
1045 machine_mode addr_mode = get_address_mode (m_obj);
1047 if (use_predec && reverse)
1049 m_addr = copy_to_mode_reg (addr_mode,
1050 plus_constant (addr_mode,
1051 m_addr, len));
1052 m_auto = true;
1053 m_explicit_inc = -1;
1055 else if (use_postinc && !reverse)
1057 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1058 m_auto = true;
1059 m_explicit_inc = 1;
1061 else if (CONSTANT_P (m_addr))
1062 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1065 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1066 are using autoincrement for this address, we don't add the offset,
1067 but we still modify the MEM's properties. */
1070 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1071 by_pieces_prev *prev)
1073 if (m_constfn)
1074 /* Pass the previous data to m_constfn. */
1075 return m_constfn (m_cfndata, prev, offset, mode);
1076 if (m_obj == NULL_RTX)
1077 return NULL_RTX;
1078 if (m_auto)
1079 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1080 else
1081 return adjust_address (m_obj, mode, offset);
1084 /* Emit an add instruction to increment the address by SIZE. */
1086 void
1087 pieces_addr::increment_address (HOST_WIDE_INT size)
1089 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1090 emit_insn (gen_add2_insn (m_addr, amount));
1093 /* If we are supposed to decrement the address after each access, emit code
1094 to do so now. Increment by SIZE (which has should have the correct sign
1095 already). */
1097 void
1098 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1100 if (m_explicit_inc >= 0)
1101 return;
1102 gcc_assert (HAVE_PRE_DECREMENT);
1103 increment_address (size);
1106 /* If we are supposed to decrement the address after each access, emit code
1107 to do so now. Increment by SIZE. */
1109 void
1110 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1112 if (m_explicit_inc <= 0)
1113 return;
1114 gcc_assert (HAVE_POST_INCREMENT);
1115 increment_address (size);
1118 /* This structure is used by do_op_by_pieces to describe the operation
1119 to be performed. */
1121 class op_by_pieces_d
1123 private:
1124 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1125 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1127 protected:
1128 pieces_addr m_to, m_from;
1129 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1130 use it to check the valid mode size. */
1131 const unsigned HOST_WIDE_INT m_len;
1132 HOST_WIDE_INT m_offset;
1133 unsigned int m_align;
1134 unsigned int m_max_size;
1135 bool m_reverse;
1136 /* True if this is a stack push. */
1137 bool m_push;
1138 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1139 bool m_overlap_op_by_pieces;
1140 /* True if QI vector mode can be used. */
1141 bool m_qi_vector_mode;
1143 /* Virtual functions, overriden by derived classes for the specific
1144 operation. */
1145 virtual void generate (rtx, rtx, machine_mode) = 0;
1146 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1147 virtual void finish_mode (machine_mode)
1151 public:
1152 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1153 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1154 bool = false);
1155 void run ();
1158 /* The constructor for an op_by_pieces_d structure. We require two
1159 objects named TO and FROM, which are identified as loads or stores
1160 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1161 and its associated FROM_CFN_DATA can be used to replace loads with
1162 constant values. MAX_PIECES describes the maximum number of bytes
1163 at a time which can be moved efficiently. LEN describes the length
1164 of the operation. */
1166 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1167 bool to_load, rtx from, bool from_load,
1168 by_pieces_constfn from_cfn,
1169 void *from_cfn_data,
1170 unsigned HOST_WIDE_INT len,
1171 unsigned int align, bool push,
1172 bool qi_vector_mode)
1173 : m_to (to, to_load, NULL, NULL),
1174 m_from (from, from_load, from_cfn, from_cfn_data),
1175 m_len (len), m_max_size (max_pieces + 1),
1176 m_push (push), m_qi_vector_mode (qi_vector_mode)
1178 int toi = m_to.get_addr_inc ();
1179 int fromi = m_from.get_addr_inc ();
1180 if (toi >= 0 && fromi >= 0)
1181 m_reverse = false;
1182 else if (toi <= 0 && fromi <= 0)
1183 m_reverse = true;
1184 else
1185 gcc_unreachable ();
1187 m_offset = m_reverse ? len : 0;
1188 align = MIN (to ? MEM_ALIGN (to) : align,
1189 from ? MEM_ALIGN (from) : align);
1191 /* If copying requires more than two move insns,
1192 copy addresses to registers (to make displacements shorter)
1193 and use post-increment if available. */
1194 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1196 /* Find the mode of the largest comparison. */
1197 fixed_size_mode mode
1198 = widest_fixed_size_mode_for_size (m_max_size,
1199 m_qi_vector_mode);
1201 m_from.decide_autoinc (mode, m_reverse, len);
1202 m_to.decide_autoinc (mode, m_reverse, len);
1205 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1206 m_align = align;
1208 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1211 /* This function returns the largest usable integer mode for LEN bytes
1212 whose size is no bigger than size of MODE. */
1214 fixed_size_mode
1215 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1217 unsigned int size;
1220 size = GET_MODE_SIZE (mode);
1221 if (len >= size && prepare_mode (mode, m_align))
1222 break;
1223 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1224 mode = widest_fixed_size_mode_for_size (size, m_qi_vector_mode);
1226 while (1);
1227 return mode;
1230 /* Return the smallest integer or QI vector mode that is not narrower
1231 than SIZE bytes. */
1233 fixed_size_mode
1234 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1236 /* Use QI vector only for > size of WORD. */
1237 if (m_qi_vector_mode && size > UNITS_PER_WORD)
1239 machine_mode mode;
1240 fixed_size_mode candidate;
1241 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1242 if (is_a<fixed_size_mode> (mode, &candidate)
1243 && GET_MODE_INNER (candidate) == QImode)
1245 /* Don't return a mode wider than M_LEN. */
1246 if (GET_MODE_SIZE (candidate) > m_len)
1247 break;
1249 if (GET_MODE_SIZE (candidate) >= size
1250 && (optab_handler (vec_duplicate_optab, candidate)
1251 != CODE_FOR_nothing))
1252 return candidate;
1256 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1259 /* This function contains the main loop used for expanding a block
1260 operation. First move what we can in the largest integer mode,
1261 then go to successively smaller modes. For every access, call
1262 GENFUN with the two operands and the EXTRA_DATA. */
1264 void
1265 op_by_pieces_d::run ()
1267 if (m_len == 0)
1268 return;
1270 unsigned HOST_WIDE_INT length = m_len;
1272 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1273 fixed_size_mode mode
1274 = widest_fixed_size_mode_for_size (m_max_size, m_qi_vector_mode);
1275 mode = get_usable_mode (mode, length);
1277 by_pieces_prev to_prev = { nullptr, mode };
1278 by_pieces_prev from_prev = { nullptr, mode };
1282 unsigned int size = GET_MODE_SIZE (mode);
1283 rtx to1 = NULL_RTX, from1;
1285 while (length >= size)
1287 if (m_reverse)
1288 m_offset -= size;
1290 to1 = m_to.adjust (mode, m_offset, &to_prev);
1291 to_prev.data = to1;
1292 to_prev.mode = mode;
1293 from1 = m_from.adjust (mode, m_offset, &from_prev);
1294 from_prev.data = from1;
1295 from_prev.mode = mode;
1297 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1298 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1300 generate (to1, from1, mode);
1302 m_to.maybe_postinc (size);
1303 m_from.maybe_postinc (size);
1305 if (!m_reverse)
1306 m_offset += size;
1308 length -= size;
1311 finish_mode (mode);
1313 if (length == 0)
1314 return;
1316 if (!m_push && m_overlap_op_by_pieces)
1318 /* NB: Generate overlapping operations if it is not a stack
1319 push since stack push must not overlap. Get the smallest
1320 fixed size mode for M_LEN bytes. */
1321 mode = smallest_fixed_size_mode_for_size (length);
1322 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1323 int gap = GET_MODE_SIZE (mode) - length;
1324 if (gap > 0)
1326 /* If size of MODE > M_LEN, generate the last operation
1327 in MODE for the remaining bytes with ovelapping memory
1328 from the previois operation. */
1329 if (m_reverse)
1330 m_offset += gap;
1331 else
1332 m_offset -= gap;
1333 length += gap;
1336 else
1338 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1339 mode = widest_fixed_size_mode_for_size (size,
1340 m_qi_vector_mode);
1341 mode = get_usable_mode (mode, length);
1344 while (1);
1346 /* The code above should have handled everything. */
1347 gcc_assert (!length);
1350 /* Derived class from op_by_pieces_d, providing support for block move
1351 operations. */
1353 #ifdef PUSH_ROUNDING
1354 #define PUSHG_P(to) ((to) == nullptr)
1355 #else
1356 #define PUSHG_P(to) false
1357 #endif
1359 class move_by_pieces_d : public op_by_pieces_d
1361 insn_gen_fn m_gen_fun;
1362 void generate (rtx, rtx, machine_mode);
1363 bool prepare_mode (machine_mode, unsigned int);
1365 public:
1366 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1367 unsigned int align)
1368 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1369 NULL, len, align, PUSHG_P (to))
1372 rtx finish_retmode (memop_ret);
1375 /* Return true if MODE can be used for a set of copies, given an
1376 alignment ALIGN. Prepare whatever data is necessary for later
1377 calls to generate. */
1379 bool
1380 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1382 insn_code icode = optab_handler (mov_optab, mode);
1383 m_gen_fun = GEN_FCN (icode);
1384 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1387 /* A callback used when iterating for a compare_by_pieces_operation.
1388 OP0 and OP1 are the values that have been loaded and should be
1389 compared in MODE. If OP0 is NULL, this means we should generate a
1390 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1391 gen function that should be used to generate the mode. */
1393 void
1394 move_by_pieces_d::generate (rtx op0, rtx op1,
1395 machine_mode mode ATTRIBUTE_UNUSED)
1397 #ifdef PUSH_ROUNDING
1398 if (op0 == NULL_RTX)
1400 emit_single_push_insn (mode, op1, NULL);
1401 return;
1403 #endif
1404 emit_insn (m_gen_fun (op0, op1));
1407 /* Perform the final adjustment at the end of a string to obtain the
1408 correct return value for the block operation.
1409 Return value is based on RETMODE argument. */
1412 move_by_pieces_d::finish_retmode (memop_ret retmode)
1414 gcc_assert (!m_reverse);
1415 if (retmode == RETURN_END_MINUS_ONE)
1417 m_to.maybe_postinc (-1);
1418 --m_offset;
1420 return m_to.adjust (QImode, m_offset);
1423 /* Generate several move instructions to copy LEN bytes from block FROM to
1424 block TO. (These are MEM rtx's with BLKmode).
1426 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1427 used to push FROM to the stack.
1429 ALIGN is maximum stack alignment we can assume.
1431 Return value is based on RETMODE argument. */
1434 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1435 unsigned int align, memop_ret retmode)
1437 #ifndef PUSH_ROUNDING
1438 if (to == NULL)
1439 gcc_unreachable ();
1440 #endif
1442 move_by_pieces_d data (to, from, len, align);
1444 data.run ();
1446 if (retmode != RETURN_BEGIN)
1447 return data.finish_retmode (retmode);
1448 else
1449 return to;
1452 /* Derived class from op_by_pieces_d, providing support for block move
1453 operations. */
1455 class store_by_pieces_d : public op_by_pieces_d
1457 insn_gen_fn m_gen_fun;
1458 void generate (rtx, rtx, machine_mode);
1459 bool prepare_mode (machine_mode, unsigned int);
1461 public:
1462 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1463 unsigned HOST_WIDE_INT len, unsigned int align,
1464 bool qi_vector_mode)
1465 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1466 cfn_data, len, align, false, qi_vector_mode)
1469 rtx finish_retmode (memop_ret);
1472 /* Return true if MODE can be used for a set of stores, given an
1473 alignment ALIGN. Prepare whatever data is necessary for later
1474 calls to generate. */
1476 bool
1477 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1479 insn_code icode = optab_handler (mov_optab, mode);
1480 m_gen_fun = GEN_FCN (icode);
1481 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1484 /* A callback used when iterating for a store_by_pieces_operation.
1485 OP0 and OP1 are the values that have been loaded and should be
1486 compared in MODE. If OP0 is NULL, this means we should generate a
1487 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1488 gen function that should be used to generate the mode. */
1490 void
1491 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1493 emit_insn (m_gen_fun (op0, op1));
1496 /* Perform the final adjustment at the end of a string to obtain the
1497 correct return value for the block operation.
1498 Return value is based on RETMODE argument. */
1501 store_by_pieces_d::finish_retmode (memop_ret retmode)
1503 gcc_assert (!m_reverse);
1504 if (retmode == RETURN_END_MINUS_ONE)
1506 m_to.maybe_postinc (-1);
1507 --m_offset;
1509 return m_to.adjust (QImode, m_offset);
1512 /* Determine whether the LEN bytes generated by CONSTFUN can be
1513 stored to memory using several move instructions. CONSTFUNDATA is
1514 a pointer which will be passed as argument in every CONSTFUN call.
1515 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1516 a memset operation and false if it's a copy of a constant string.
1517 Return nonzero if a call to store_by_pieces should succeed. */
1520 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1521 by_pieces_constfn constfun,
1522 void *constfundata, unsigned int align, bool memsetp)
1524 unsigned HOST_WIDE_INT l;
1525 unsigned int max_size;
1526 HOST_WIDE_INT offset = 0;
1527 enum insn_code icode;
1528 int reverse;
1529 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1530 rtx cst ATTRIBUTE_UNUSED;
1532 if (len == 0)
1533 return 1;
1535 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1536 memsetp
1537 ? SET_BY_PIECES
1538 : STORE_BY_PIECES,
1539 optimize_insn_for_speed_p ()))
1540 return 0;
1542 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1544 /* We would first store what we can in the largest integer mode, then go to
1545 successively smaller modes. */
1547 for (reverse = 0;
1548 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1549 reverse++)
1551 l = len;
1552 max_size = STORE_MAX_PIECES + 1;
1553 while (max_size > 1 && l > 0)
1555 fixed_size_mode mode
1556 = widest_fixed_size_mode_for_size (max_size, memsetp);
1558 icode = optab_handler (mov_optab, mode);
1559 if (icode != CODE_FOR_nothing
1560 && align >= GET_MODE_ALIGNMENT (mode))
1562 unsigned int size = GET_MODE_SIZE (mode);
1564 while (l >= size)
1566 if (reverse)
1567 offset -= size;
1569 cst = (*constfun) (constfundata, nullptr, offset, mode);
1570 /* All CONST_VECTORs can be loaded for memset since
1571 vec_duplicate_optab is a precondition to pick a
1572 vector mode for the memset expander. */
1573 if (!((memsetp && VECTOR_MODE_P (mode))
1574 || targetm.legitimate_constant_p (mode, cst)))
1575 return 0;
1577 if (!reverse)
1578 offset += size;
1580 l -= size;
1584 max_size = GET_MODE_SIZE (mode);
1587 /* The code above should have handled everything. */
1588 gcc_assert (!l);
1591 return 1;
1594 /* Generate several move instructions to store LEN bytes generated by
1595 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1596 pointer which will be passed as argument in every CONSTFUN call.
1597 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1598 a memset operation and false if it's a copy of a constant string.
1599 Return value is based on RETMODE argument. */
1602 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1603 by_pieces_constfn constfun,
1604 void *constfundata, unsigned int align, bool memsetp,
1605 memop_ret retmode)
1607 if (len == 0)
1609 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1610 return to;
1613 gcc_assert (targetm.use_by_pieces_infrastructure_p
1614 (len, align,
1615 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1616 optimize_insn_for_speed_p ()));
1618 store_by_pieces_d data (to, constfun, constfundata, len, align,
1619 memsetp);
1620 data.run ();
1622 if (retmode != RETURN_BEGIN)
1623 return data.finish_retmode (retmode);
1624 else
1625 return to;
1628 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1629 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1631 static void
1632 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1634 if (len == 0)
1635 return;
1637 /* Use builtin_memset_read_str to support vector mode broadcast. */
1638 char c = 0;
1639 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1640 true);
1641 data.run ();
1644 /* Context used by compare_by_pieces_genfn. It stores the fail label
1645 to jump to in case of miscomparison, and for branch ratios greater than 1,
1646 it stores an accumulator and the current and maximum counts before
1647 emitting another branch. */
1649 class compare_by_pieces_d : public op_by_pieces_d
1651 rtx_code_label *m_fail_label;
1652 rtx m_accumulator;
1653 int m_count, m_batch;
1655 void generate (rtx, rtx, machine_mode);
1656 bool prepare_mode (machine_mode, unsigned int);
1657 void finish_mode (machine_mode);
1658 public:
1659 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1660 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1661 rtx_code_label *fail_label)
1662 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1663 op1_cfn_data, len, align, false)
1665 m_fail_label = fail_label;
1669 /* A callback used when iterating for a compare_by_pieces_operation.
1670 OP0 and OP1 are the values that have been loaded and should be
1671 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1672 context structure. */
1674 void
1675 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1677 if (m_batch > 1)
1679 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1680 true, OPTAB_LIB_WIDEN);
1681 if (m_count != 0)
1682 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1683 true, OPTAB_LIB_WIDEN);
1684 m_accumulator = temp;
1686 if (++m_count < m_batch)
1687 return;
1689 m_count = 0;
1690 op0 = m_accumulator;
1691 op1 = const0_rtx;
1692 m_accumulator = NULL_RTX;
1694 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1695 m_fail_label, profile_probability::uninitialized ());
1698 /* Return true if MODE can be used for a set of moves and comparisons,
1699 given an alignment ALIGN. Prepare whatever data is necessary for
1700 later calls to generate. */
1702 bool
1703 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1705 insn_code icode = optab_handler (mov_optab, mode);
1706 if (icode == CODE_FOR_nothing
1707 || align < GET_MODE_ALIGNMENT (mode)
1708 || !can_compare_p (EQ, mode, ccp_jump))
1709 return false;
1710 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1711 if (m_batch < 0)
1712 return false;
1713 m_accumulator = NULL_RTX;
1714 m_count = 0;
1715 return true;
1718 /* Called after expanding a series of comparisons in MODE. If we have
1719 accumulated results for which we haven't emitted a branch yet, do
1720 so now. */
1722 void
1723 compare_by_pieces_d::finish_mode (machine_mode mode)
1725 if (m_accumulator != NULL_RTX)
1726 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1727 NULL_RTX, NULL, m_fail_label,
1728 profile_probability::uninitialized ());
1731 /* Generate several move instructions to compare LEN bytes from blocks
1732 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1734 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1735 used to push FROM to the stack.
1737 ALIGN is maximum stack alignment we can assume.
1739 Optionally, the caller can pass a constfn and associated data in A1_CFN
1740 and A1_CFN_DATA. describing that the second operand being compared is a
1741 known constant and how to obtain its data. */
1743 static rtx
1744 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1745 rtx target, unsigned int align,
1746 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1748 rtx_code_label *fail_label = gen_label_rtx ();
1749 rtx_code_label *end_label = gen_label_rtx ();
1751 if (target == NULL_RTX
1752 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1753 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1755 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1756 fail_label);
1758 data.run ();
1760 emit_move_insn (target, const0_rtx);
1761 emit_jump (end_label);
1762 emit_barrier ();
1763 emit_label (fail_label);
1764 emit_move_insn (target, const1_rtx);
1765 emit_label (end_label);
1767 return target;
1770 /* Emit code to move a block Y to a block X. This may be done with
1771 string-move instructions, with multiple scalar move instructions,
1772 or with a library call.
1774 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1775 SIZE is an rtx that says how long they are.
1776 ALIGN is the maximum alignment we can assume they have.
1777 METHOD describes what kind of copy this is, and what mechanisms may be used.
1778 MIN_SIZE is the minimal size of block to move
1779 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1780 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1782 Return the address of the new block, if memcpy is called and returns it,
1783 0 otherwise. */
1786 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1787 unsigned int expected_align, HOST_WIDE_INT expected_size,
1788 unsigned HOST_WIDE_INT min_size,
1789 unsigned HOST_WIDE_INT max_size,
1790 unsigned HOST_WIDE_INT probable_max_size,
1791 bool bail_out_libcall, bool *is_move_done,
1792 bool might_overlap)
1794 int may_use_call;
1795 rtx retval = 0;
1796 unsigned int align;
1798 if (is_move_done)
1799 *is_move_done = true;
1801 gcc_assert (size);
1802 if (CONST_INT_P (size) && INTVAL (size) == 0)
1803 return 0;
1805 switch (method)
1807 case BLOCK_OP_NORMAL:
1808 case BLOCK_OP_TAILCALL:
1809 may_use_call = 1;
1810 break;
1812 case BLOCK_OP_CALL_PARM:
1813 may_use_call = block_move_libcall_safe_for_call_parm ();
1815 /* Make inhibit_defer_pop nonzero around the library call
1816 to force it to pop the arguments right away. */
1817 NO_DEFER_POP;
1818 break;
1820 case BLOCK_OP_NO_LIBCALL:
1821 may_use_call = 0;
1822 break;
1824 case BLOCK_OP_NO_LIBCALL_RET:
1825 may_use_call = -1;
1826 break;
1828 default:
1829 gcc_unreachable ();
1832 gcc_assert (MEM_P (x) && MEM_P (y));
1833 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1834 gcc_assert (align >= BITS_PER_UNIT);
1836 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1837 block copy is more efficient for other large modes, e.g. DCmode. */
1838 x = adjust_address (x, BLKmode, 0);
1839 y = adjust_address (y, BLKmode, 0);
1841 /* If source and destination are the same, no need to copy anything. */
1842 if (rtx_equal_p (x, y)
1843 && !MEM_VOLATILE_P (x)
1844 && !MEM_VOLATILE_P (y))
1845 return 0;
1847 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1848 can be incorrect is coming from __builtin_memcpy. */
1849 poly_int64 const_size;
1850 if (poly_int_rtx_p (size, &const_size))
1852 x = shallow_copy_rtx (x);
1853 y = shallow_copy_rtx (y);
1854 set_mem_size (x, const_size);
1855 set_mem_size (y, const_size);
1858 bool pieces_ok = CONST_INT_P (size)
1859 && can_move_by_pieces (INTVAL (size), align);
1860 bool pattern_ok = false;
1862 if (!pieces_ok || might_overlap)
1864 pattern_ok
1865 = emit_block_move_via_pattern (x, y, size, align,
1866 expected_align, expected_size,
1867 min_size, max_size, probable_max_size,
1868 might_overlap);
1869 if (!pattern_ok && might_overlap)
1871 /* Do not try any of the other methods below as they are not safe
1872 for overlapping moves. */
1873 *is_move_done = false;
1874 return retval;
1878 if (pattern_ok)
1880 else if (pieces_ok)
1881 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1882 else if (may_use_call && !might_overlap
1883 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1884 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1886 if (bail_out_libcall)
1888 if (is_move_done)
1889 *is_move_done = false;
1890 return retval;
1893 if (may_use_call < 0)
1894 return pc_rtx;
1896 retval = emit_block_copy_via_libcall (x, y, size,
1897 method == BLOCK_OP_TAILCALL);
1899 else if (might_overlap)
1900 *is_move_done = false;
1901 else
1902 emit_block_move_via_loop (x, y, size, align);
1904 if (method == BLOCK_OP_CALL_PARM)
1905 OK_DEFER_POP;
1907 return retval;
1911 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1913 unsigned HOST_WIDE_INT max, min = 0;
1914 if (GET_CODE (size) == CONST_INT)
1915 min = max = UINTVAL (size);
1916 else
1917 max = GET_MODE_MASK (GET_MODE (size));
1918 return emit_block_move_hints (x, y, size, method, 0, -1,
1919 min, max, max);
1922 /* A subroutine of emit_block_move. Returns true if calling the
1923 block move libcall will not clobber any parameters which may have
1924 already been placed on the stack. */
1926 static bool
1927 block_move_libcall_safe_for_call_parm (void)
1929 tree fn;
1931 /* If arguments are pushed on the stack, then they're safe. */
1932 if (targetm.calls.push_argument (0))
1933 return true;
1935 /* If registers go on the stack anyway, any argument is sure to clobber
1936 an outgoing argument. */
1937 #if defined (REG_PARM_STACK_SPACE)
1938 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1939 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1940 depend on its argument. */
1941 (void) fn;
1942 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1943 && REG_PARM_STACK_SPACE (fn) != 0)
1944 return false;
1945 #endif
1947 /* If any argument goes in memory, then it might clobber an outgoing
1948 argument. */
1950 CUMULATIVE_ARGS args_so_far_v;
1951 cumulative_args_t args_so_far;
1952 tree arg;
1954 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1955 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1956 args_so_far = pack_cumulative_args (&args_so_far_v);
1958 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1959 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1961 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1962 function_arg_info arg_info (mode, /*named=*/true);
1963 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1964 if (!tmp || !REG_P (tmp))
1965 return false;
1966 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1967 return false;
1968 targetm.calls.function_arg_advance (args_so_far, arg_info);
1971 return true;
1974 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1975 return true if successful.
1977 X is the destination of the copy or move.
1978 Y is the source of the copy or move.
1979 SIZE is the size of the block to be moved.
1981 MIGHT_OVERLAP indicates this originated with expansion of a
1982 builtin_memmove() and the source and destination blocks may
1983 overlap.
1986 static bool
1987 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
1988 unsigned int expected_align,
1989 HOST_WIDE_INT expected_size,
1990 unsigned HOST_WIDE_INT min_size,
1991 unsigned HOST_WIDE_INT max_size,
1992 unsigned HOST_WIDE_INT probable_max_size,
1993 bool might_overlap)
1995 if (expected_align < align)
1996 expected_align = align;
1997 if (expected_size != -1)
1999 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2000 expected_size = probable_max_size;
2001 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2002 expected_size = min_size;
2005 /* Since this is a move insn, we don't care about volatility. */
2006 temporary_volatile_ok v (true);
2008 /* Try the most limited insn first, because there's no point
2009 including more than one in the machine description unless
2010 the more limited one has some advantage. */
2012 opt_scalar_int_mode mode_iter;
2013 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2015 scalar_int_mode mode = mode_iter.require ();
2016 enum insn_code code;
2017 if (might_overlap)
2018 code = direct_optab_handler (movmem_optab, mode);
2019 else
2020 code = direct_optab_handler (cpymem_optab, mode);
2022 if (code != CODE_FOR_nothing
2023 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2024 here because if SIZE is less than the mode mask, as it is
2025 returned by the macro, it will definitely be less than the
2026 actual mode mask. Since SIZE is within the Pmode address
2027 space, we limit MODE to Pmode. */
2028 && ((CONST_INT_P (size)
2029 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2030 <= (GET_MODE_MASK (mode) >> 1)))
2031 || max_size <= (GET_MODE_MASK (mode) >> 1)
2032 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2034 class expand_operand ops[9];
2035 unsigned int nops;
2037 /* ??? When called via emit_block_move_for_call, it'd be
2038 nice if there were some way to inform the backend, so
2039 that it doesn't fail the expansion because it thinks
2040 emitting the libcall would be more efficient. */
2041 nops = insn_data[(int) code].n_generator_args;
2042 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2044 create_fixed_operand (&ops[0], x);
2045 create_fixed_operand (&ops[1], y);
2046 /* The check above guarantees that this size conversion is valid. */
2047 create_convert_operand_to (&ops[2], size, mode, true);
2048 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2049 if (nops >= 6)
2051 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2052 create_integer_operand (&ops[5], expected_size);
2054 if (nops >= 8)
2056 create_integer_operand (&ops[6], min_size);
2057 /* If we cannot represent the maximal size,
2058 make parameter NULL. */
2059 if ((HOST_WIDE_INT) max_size != -1)
2060 create_integer_operand (&ops[7], max_size);
2061 else
2062 create_fixed_operand (&ops[7], NULL);
2064 if (nops == 9)
2066 /* If we cannot represent the maximal size,
2067 make parameter NULL. */
2068 if ((HOST_WIDE_INT) probable_max_size != -1)
2069 create_integer_operand (&ops[8], probable_max_size);
2070 else
2071 create_fixed_operand (&ops[8], NULL);
2073 if (maybe_expand_insn (code, nops, ops))
2074 return true;
2078 return false;
2081 /* A subroutine of emit_block_move. Copy the data via an explicit
2082 loop. This is used only when libcalls are forbidden. */
2083 /* ??? It'd be nice to copy in hunks larger than QImode. */
2085 static void
2086 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2087 unsigned int align ATTRIBUTE_UNUSED)
2089 rtx_code_label *cmp_label, *top_label;
2090 rtx iter, x_addr, y_addr, tmp;
2091 machine_mode x_addr_mode = get_address_mode (x);
2092 machine_mode y_addr_mode = get_address_mode (y);
2093 machine_mode iter_mode;
2095 iter_mode = GET_MODE (size);
2096 if (iter_mode == VOIDmode)
2097 iter_mode = word_mode;
2099 top_label = gen_label_rtx ();
2100 cmp_label = gen_label_rtx ();
2101 iter = gen_reg_rtx (iter_mode);
2103 emit_move_insn (iter, const0_rtx);
2105 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2106 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2107 do_pending_stack_adjust ();
2109 emit_jump (cmp_label);
2110 emit_label (top_label);
2112 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2113 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2115 if (x_addr_mode != y_addr_mode)
2116 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2117 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2119 x = change_address (x, QImode, x_addr);
2120 y = change_address (y, QImode, y_addr);
2122 emit_move_insn (x, y);
2124 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2125 true, OPTAB_LIB_WIDEN);
2126 if (tmp != iter)
2127 emit_move_insn (iter, tmp);
2129 emit_label (cmp_label);
2131 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2132 true, top_label,
2133 profile_probability::guessed_always ()
2134 .apply_scale (9, 10));
2137 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2138 TAILCALL is true if this is a tail call. */
2141 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2142 rtx size, bool tailcall)
2144 rtx dst_addr, src_addr;
2145 tree call_expr, dst_tree, src_tree, size_tree;
2146 machine_mode size_mode;
2148 /* Since dst and src are passed to a libcall, mark the corresponding
2149 tree EXPR as addressable. */
2150 tree dst_expr = MEM_EXPR (dst);
2151 tree src_expr = MEM_EXPR (src);
2152 if (dst_expr)
2153 mark_addressable (dst_expr);
2154 if (src_expr)
2155 mark_addressable (src_expr);
2157 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2158 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2159 dst_tree = make_tree (ptr_type_node, dst_addr);
2161 src_addr = copy_addr_to_reg (XEXP (src, 0));
2162 src_addr = convert_memory_address (ptr_mode, src_addr);
2163 src_tree = make_tree (ptr_type_node, src_addr);
2165 size_mode = TYPE_MODE (sizetype);
2166 size = convert_to_mode (size_mode, size, 1);
2167 size = copy_to_mode_reg (size_mode, size);
2168 size_tree = make_tree (sizetype, size);
2170 /* It is incorrect to use the libcall calling conventions for calls to
2171 memcpy/memmove/memcmp because they can be provided by the user. */
2172 tree fn = builtin_decl_implicit (fncode);
2173 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2174 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2176 return expand_call (call_expr, NULL_RTX, false);
2179 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2180 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2181 otherwise return null. */
2184 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2185 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2186 HOST_WIDE_INT align)
2188 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2190 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2191 target = NULL_RTX;
2193 class expand_operand ops[5];
2194 create_output_operand (&ops[0], target, insn_mode);
2195 create_fixed_operand (&ops[1], arg1_rtx);
2196 create_fixed_operand (&ops[2], arg2_rtx);
2197 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2198 TYPE_UNSIGNED (arg3_type));
2199 create_integer_operand (&ops[4], align);
2200 if (maybe_expand_insn (icode, 5, ops))
2201 return ops[0].value;
2202 return NULL_RTX;
2205 /* Expand a block compare between X and Y with length LEN using the
2206 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2207 of the expression that was used to calculate the length. ALIGN
2208 gives the known minimum common alignment. */
2210 static rtx
2211 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2212 unsigned align)
2214 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2215 implementing memcmp because it will stop if it encounters two
2216 zero bytes. */
2217 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2219 if (icode == CODE_FOR_nothing)
2220 return NULL_RTX;
2222 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2225 /* Emit code to compare a block Y to a block X. This may be done with
2226 string-compare instructions, with multiple scalar instructions,
2227 or with a library call.
2229 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2230 they are. LEN_TYPE is the type of the expression that was used to
2231 calculate it.
2233 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2234 value of a normal memcmp call, instead we can just compare for equality.
2235 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2236 returning NULL_RTX.
2238 Optionally, the caller can pass a constfn and associated data in Y_CFN
2239 and Y_CFN_DATA. describing that the second operand being compared is a
2240 known constant and how to obtain its data.
2241 Return the result of the comparison, or NULL_RTX if we failed to
2242 perform the operation. */
2245 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2246 bool equality_only, by_pieces_constfn y_cfn,
2247 void *y_cfndata)
2249 rtx result = 0;
2251 if (CONST_INT_P (len) && INTVAL (len) == 0)
2252 return const0_rtx;
2254 gcc_assert (MEM_P (x) && MEM_P (y));
2255 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2256 gcc_assert (align >= BITS_PER_UNIT);
2258 x = adjust_address (x, BLKmode, 0);
2259 y = adjust_address (y, BLKmode, 0);
2261 if (equality_only
2262 && CONST_INT_P (len)
2263 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2264 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2265 y_cfn, y_cfndata);
2266 else
2267 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2269 return result;
2272 /* Copy all or part of a value X into registers starting at REGNO.
2273 The number of registers to be filled is NREGS. */
2275 void
2276 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2278 if (nregs == 0)
2279 return;
2281 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2282 x = validize_mem (force_const_mem (mode, x));
2284 /* See if the machine can do this with a load multiple insn. */
2285 if (targetm.have_load_multiple ())
2287 rtx_insn *last = get_last_insn ();
2288 rtx first = gen_rtx_REG (word_mode, regno);
2289 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2290 GEN_INT (nregs)))
2292 emit_insn (pat);
2293 return;
2295 else
2296 delete_insns_since (last);
2299 for (int i = 0; i < nregs; i++)
2300 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2301 operand_subword_force (x, i, mode));
2304 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2305 The number of registers to be filled is NREGS. */
2307 void
2308 move_block_from_reg (int regno, rtx x, int nregs)
2310 if (nregs == 0)
2311 return;
2313 /* See if the machine can do this with a store multiple insn. */
2314 if (targetm.have_store_multiple ())
2316 rtx_insn *last = get_last_insn ();
2317 rtx first = gen_rtx_REG (word_mode, regno);
2318 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2319 GEN_INT (nregs)))
2321 emit_insn (pat);
2322 return;
2324 else
2325 delete_insns_since (last);
2328 for (int i = 0; i < nregs; i++)
2330 rtx tem = operand_subword (x, i, 1, BLKmode);
2332 gcc_assert (tem);
2334 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2338 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2339 ORIG, where ORIG is a non-consecutive group of registers represented by
2340 a PARALLEL. The clone is identical to the original except in that the
2341 original set of registers is replaced by a new set of pseudo registers.
2342 The new set has the same modes as the original set. */
2345 gen_group_rtx (rtx orig)
2347 int i, length;
2348 rtx *tmps;
2350 gcc_assert (GET_CODE (orig) == PARALLEL);
2352 length = XVECLEN (orig, 0);
2353 tmps = XALLOCAVEC (rtx, length);
2355 /* Skip a NULL entry in first slot. */
2356 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2358 if (i)
2359 tmps[0] = 0;
2361 for (; i < length; i++)
2363 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2364 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2366 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2369 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2372 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2373 except that values are placed in TMPS[i], and must later be moved
2374 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2376 static void
2377 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2378 poly_int64 ssize)
2380 rtx src;
2381 int start, i;
2382 machine_mode m = GET_MODE (orig_src);
2384 gcc_assert (GET_CODE (dst) == PARALLEL);
2386 if (m != VOIDmode
2387 && !SCALAR_INT_MODE_P (m)
2388 && !MEM_P (orig_src)
2389 && GET_CODE (orig_src) != CONCAT)
2391 scalar_int_mode imode;
2392 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2394 src = gen_reg_rtx (imode);
2395 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2397 else
2399 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2400 emit_move_insn (src, orig_src);
2402 emit_group_load_1 (tmps, dst, src, type, ssize);
2403 return;
2406 /* Check for a NULL entry, used to indicate that the parameter goes
2407 both on the stack and in registers. */
2408 if (XEXP (XVECEXP (dst, 0, 0), 0))
2409 start = 0;
2410 else
2411 start = 1;
2413 /* Process the pieces. */
2414 for (i = start; i < XVECLEN (dst, 0); i++)
2416 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2417 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2418 poly_int64 bytelen = GET_MODE_SIZE (mode);
2419 poly_int64 shift = 0;
2421 /* Handle trailing fragments that run over the size of the struct.
2422 It's the target's responsibility to make sure that the fragment
2423 cannot be strictly smaller in some cases and strictly larger
2424 in others. */
2425 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2426 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2428 /* Arrange to shift the fragment to where it belongs.
2429 extract_bit_field loads to the lsb of the reg. */
2430 if (
2431 #ifdef BLOCK_REG_PADDING
2432 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2433 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2434 #else
2435 BYTES_BIG_ENDIAN
2436 #endif
2438 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2439 bytelen = ssize - bytepos;
2440 gcc_assert (maybe_gt (bytelen, 0));
2443 /* If we won't be loading directly from memory, protect the real source
2444 from strange tricks we might play; but make sure that the source can
2445 be loaded directly into the destination. */
2446 src = orig_src;
2447 if (!MEM_P (orig_src)
2448 && (!CONSTANT_P (orig_src)
2449 || (GET_MODE (orig_src) != mode
2450 && GET_MODE (orig_src) != VOIDmode)))
2452 if (GET_MODE (orig_src) == VOIDmode)
2453 src = gen_reg_rtx (mode);
2454 else
2455 src = gen_reg_rtx (GET_MODE (orig_src));
2457 emit_move_insn (src, orig_src);
2460 /* Optimize the access just a bit. */
2461 if (MEM_P (src)
2462 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2463 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2464 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2465 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2467 tmps[i] = gen_reg_rtx (mode);
2468 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2470 else if (COMPLEX_MODE_P (mode)
2471 && GET_MODE (src) == mode
2472 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2473 /* Let emit_move_complex do the bulk of the work. */
2474 tmps[i] = src;
2475 else if (GET_CODE (src) == CONCAT)
2477 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2478 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2479 unsigned int elt;
2480 poly_int64 subpos;
2482 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2483 && known_le (subpos + bytelen, slen0))
2485 /* The following assumes that the concatenated objects all
2486 have the same size. In this case, a simple calculation
2487 can be used to determine the object and the bit field
2488 to be extracted. */
2489 tmps[i] = XEXP (src, elt);
2490 if (maybe_ne (subpos, 0)
2491 || maybe_ne (subpos + bytelen, slen0)
2492 || (!CONSTANT_P (tmps[i])
2493 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2494 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2495 subpos * BITS_PER_UNIT,
2496 1, NULL_RTX, mode, mode, false,
2497 NULL);
2499 else
2501 rtx mem;
2503 gcc_assert (known_eq (bytepos, 0));
2504 mem = assign_stack_temp (GET_MODE (src), slen);
2505 emit_move_insn (mem, src);
2506 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2507 0, 1, NULL_RTX, mode, mode, false,
2508 NULL);
2511 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2512 SIMD register, which is currently broken. While we get GCC
2513 to emit proper RTL for these cases, let's dump to memory. */
2514 else if (VECTOR_MODE_P (GET_MODE (dst))
2515 && REG_P (src))
2517 poly_uint64 slen = GET_MODE_SIZE (GET_MODE (src));
2518 rtx mem;
2520 mem = assign_stack_temp (GET_MODE (src), slen);
2521 emit_move_insn (mem, src);
2522 tmps[i] = adjust_address (mem, mode, bytepos);
2524 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2525 && XVECLEN (dst, 0) > 1)
2526 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2527 else if (CONSTANT_P (src))
2529 if (known_eq (bytelen, ssize))
2530 tmps[i] = src;
2531 else
2533 rtx first, second;
2535 /* TODO: const_wide_int can have sizes other than this... */
2536 gcc_assert (known_eq (2 * bytelen, ssize));
2537 split_double (src, &first, &second);
2538 if (i)
2539 tmps[i] = second;
2540 else
2541 tmps[i] = first;
2544 else if (REG_P (src) && GET_MODE (src) == mode)
2545 tmps[i] = src;
2546 else
2547 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2548 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2549 mode, mode, false, NULL);
2551 if (maybe_ne (shift, 0))
2552 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2553 shift, tmps[i], 0);
2557 /* Emit code to move a block SRC of type TYPE to a block DST,
2558 where DST is non-consecutive registers represented by a PARALLEL.
2559 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2560 if not known. */
2562 void
2563 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2565 rtx *tmps;
2566 int i;
2568 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2569 emit_group_load_1 (tmps, dst, src, type, ssize);
2571 /* Copy the extracted pieces into the proper (probable) hard regs. */
2572 for (i = 0; i < XVECLEN (dst, 0); i++)
2574 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2575 if (d == NULL)
2576 continue;
2577 emit_move_insn (d, tmps[i]);
2581 /* Similar, but load SRC into new pseudos in a format that looks like
2582 PARALLEL. This can later be fed to emit_group_move to get things
2583 in the right place. */
2586 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2588 rtvec vec;
2589 int i;
2591 vec = rtvec_alloc (XVECLEN (parallel, 0));
2592 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2594 /* Convert the vector to look just like the original PARALLEL, except
2595 with the computed values. */
2596 for (i = 0; i < XVECLEN (parallel, 0); i++)
2598 rtx e = XVECEXP (parallel, 0, i);
2599 rtx d = XEXP (e, 0);
2601 if (d)
2603 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2604 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2606 RTVEC_ELT (vec, i) = e;
2609 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2612 /* Emit code to move a block SRC to block DST, where SRC and DST are
2613 non-consecutive groups of registers, each represented by a PARALLEL. */
2615 void
2616 emit_group_move (rtx dst, rtx src)
2618 int i;
2620 gcc_assert (GET_CODE (src) == PARALLEL
2621 && GET_CODE (dst) == PARALLEL
2622 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2624 /* Skip first entry if NULL. */
2625 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2626 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2627 XEXP (XVECEXP (src, 0, i), 0));
2630 /* Move a group of registers represented by a PARALLEL into pseudos. */
2633 emit_group_move_into_temps (rtx src)
2635 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2636 int i;
2638 for (i = 0; i < XVECLEN (src, 0); i++)
2640 rtx e = XVECEXP (src, 0, i);
2641 rtx d = XEXP (e, 0);
2643 if (d)
2644 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2645 RTVEC_ELT (vec, i) = e;
2648 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2651 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2652 where SRC is non-consecutive registers represented by a PARALLEL.
2653 SSIZE represents the total size of block ORIG_DST, or -1 if not
2654 known. */
2656 void
2657 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2658 poly_int64 ssize)
2660 rtx *tmps, dst;
2661 int start, finish, i;
2662 machine_mode m = GET_MODE (orig_dst);
2664 gcc_assert (GET_CODE (src) == PARALLEL);
2666 if (!SCALAR_INT_MODE_P (m)
2667 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2669 scalar_int_mode imode;
2670 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2672 dst = gen_reg_rtx (imode);
2673 emit_group_store (dst, src, type, ssize);
2674 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2676 else
2678 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2679 emit_group_store (dst, src, type, ssize);
2681 emit_move_insn (orig_dst, dst);
2682 return;
2685 /* Check for a NULL entry, used to indicate that the parameter goes
2686 both on the stack and in registers. */
2687 if (XEXP (XVECEXP (src, 0, 0), 0))
2688 start = 0;
2689 else
2690 start = 1;
2691 finish = XVECLEN (src, 0);
2693 tmps = XALLOCAVEC (rtx, finish);
2695 /* Copy the (probable) hard regs into pseudos. */
2696 for (i = start; i < finish; i++)
2698 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2699 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2701 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2702 emit_move_insn (tmps[i], reg);
2704 else
2705 tmps[i] = reg;
2708 /* If we won't be storing directly into memory, protect the real destination
2709 from strange tricks we might play. */
2710 dst = orig_dst;
2711 if (GET_CODE (dst) == PARALLEL)
2713 rtx temp;
2715 /* We can get a PARALLEL dst if there is a conditional expression in
2716 a return statement. In that case, the dst and src are the same,
2717 so no action is necessary. */
2718 if (rtx_equal_p (dst, src))
2719 return;
2721 /* It is unclear if we can ever reach here, but we may as well handle
2722 it. Allocate a temporary, and split this into a store/load to/from
2723 the temporary. */
2724 temp = assign_stack_temp (GET_MODE (dst), ssize);
2725 emit_group_store (temp, src, type, ssize);
2726 emit_group_load (dst, temp, type, ssize);
2727 return;
2729 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2731 machine_mode outer = GET_MODE (dst);
2732 machine_mode inner;
2733 poly_int64 bytepos;
2734 bool done = false;
2735 rtx temp;
2737 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2738 dst = gen_reg_rtx (outer);
2740 /* Make life a bit easier for combine. */
2741 /* If the first element of the vector is the low part
2742 of the destination mode, use a paradoxical subreg to
2743 initialize the destination. */
2744 if (start < finish)
2746 inner = GET_MODE (tmps[start]);
2747 bytepos = subreg_lowpart_offset (inner, outer);
2748 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2749 bytepos))
2751 temp = simplify_gen_subreg (outer, tmps[start],
2752 inner, 0);
2753 if (temp)
2755 emit_move_insn (dst, temp);
2756 done = true;
2757 start++;
2762 /* If the first element wasn't the low part, try the last. */
2763 if (!done
2764 && start < finish - 1)
2766 inner = GET_MODE (tmps[finish - 1]);
2767 bytepos = subreg_lowpart_offset (inner, outer);
2768 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2769 finish - 1), 1)),
2770 bytepos))
2772 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2773 inner, 0);
2774 if (temp)
2776 emit_move_insn (dst, temp);
2777 done = true;
2778 finish--;
2783 /* Otherwise, simply initialize the result to zero. */
2784 if (!done)
2785 emit_move_insn (dst, CONST0_RTX (outer));
2788 /* Process the pieces. */
2789 for (i = start; i < finish; i++)
2791 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2792 machine_mode mode = GET_MODE (tmps[i]);
2793 poly_int64 bytelen = GET_MODE_SIZE (mode);
2794 poly_uint64 adj_bytelen;
2795 rtx dest = dst;
2797 /* Handle trailing fragments that run over the size of the struct.
2798 It's the target's responsibility to make sure that the fragment
2799 cannot be strictly smaller in some cases and strictly larger
2800 in others. */
2801 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2802 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2803 adj_bytelen = ssize - bytepos;
2804 else
2805 adj_bytelen = bytelen;
2807 if (GET_CODE (dst) == CONCAT)
2809 if (known_le (bytepos + adj_bytelen,
2810 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2811 dest = XEXP (dst, 0);
2812 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2814 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2815 dest = XEXP (dst, 1);
2817 else
2819 machine_mode dest_mode = GET_MODE (dest);
2820 machine_mode tmp_mode = GET_MODE (tmps[i]);
2822 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2824 if (GET_MODE_ALIGNMENT (dest_mode)
2825 >= GET_MODE_ALIGNMENT (tmp_mode))
2827 dest = assign_stack_temp (dest_mode,
2828 GET_MODE_SIZE (dest_mode));
2829 emit_move_insn (adjust_address (dest,
2830 tmp_mode,
2831 bytepos),
2832 tmps[i]);
2833 dst = dest;
2835 else
2837 dest = assign_stack_temp (tmp_mode,
2838 GET_MODE_SIZE (tmp_mode));
2839 emit_move_insn (dest, tmps[i]);
2840 dst = adjust_address (dest, dest_mode, bytepos);
2842 break;
2846 /* Handle trailing fragments that run over the size of the struct. */
2847 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2849 /* store_bit_field always takes its value from the lsb.
2850 Move the fragment to the lsb if it's not already there. */
2851 if (
2852 #ifdef BLOCK_REG_PADDING
2853 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2854 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2855 #else
2856 BYTES_BIG_ENDIAN
2857 #endif
2860 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2861 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2862 shift, tmps[i], 0);
2865 /* Make sure not to write past the end of the struct. */
2866 store_bit_field (dest,
2867 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2868 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2869 VOIDmode, tmps[i], false);
2872 /* Optimize the access just a bit. */
2873 else if (MEM_P (dest)
2874 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2875 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2876 && multiple_p (bytepos * BITS_PER_UNIT,
2877 GET_MODE_ALIGNMENT (mode))
2878 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2879 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2881 else
2882 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2883 0, 0, mode, tmps[i], false);
2886 /* Copy from the pseudo into the (probable) hard reg. */
2887 if (orig_dst != dst)
2888 emit_move_insn (orig_dst, dst);
2891 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2892 of the value stored in X. */
2895 maybe_emit_group_store (rtx x, tree type)
2897 machine_mode mode = TYPE_MODE (type);
2898 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2899 if (GET_CODE (x) == PARALLEL)
2901 rtx result = gen_reg_rtx (mode);
2902 emit_group_store (result, x, type, int_size_in_bytes (type));
2903 return result;
2905 return x;
2908 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2910 This is used on targets that return BLKmode values in registers. */
2912 static void
2913 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2915 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2916 rtx src = NULL, dst = NULL;
2917 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2918 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2919 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2920 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2921 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2922 fixed_size_mode copy_mode;
2924 /* BLKmode registers created in the back-end shouldn't have survived. */
2925 gcc_assert (mode != BLKmode);
2927 /* If the structure doesn't take up a whole number of words, see whether
2928 SRCREG is padded on the left or on the right. If it's on the left,
2929 set PADDING_CORRECTION to the number of bits to skip.
2931 In most ABIs, the structure will be returned at the least end of
2932 the register, which translates to right padding on little-endian
2933 targets and left padding on big-endian targets. The opposite
2934 holds if the structure is returned at the most significant
2935 end of the register. */
2936 if (bytes % UNITS_PER_WORD != 0
2937 && (targetm.calls.return_in_msb (type)
2938 ? !BYTES_BIG_ENDIAN
2939 : BYTES_BIG_ENDIAN))
2940 padding_correction
2941 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2943 /* We can use a single move if we have an exact mode for the size. */
2944 else if (MEM_P (target)
2945 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2946 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2947 && bytes == GET_MODE_SIZE (mode))
2949 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2950 return;
2953 /* And if we additionally have the same mode for a register. */
2954 else if (REG_P (target)
2955 && GET_MODE (target) == mode
2956 && bytes == GET_MODE_SIZE (mode))
2958 emit_move_insn (target, srcreg);
2959 return;
2962 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2963 into a new pseudo which is a full word. */
2964 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2966 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2967 mode = word_mode;
2970 /* Copy the structure BITSIZE bits at a time. If the target lives in
2971 memory, take care of not reading/writing past its end by selecting
2972 a copy mode suited to BITSIZE. This should always be possible given
2973 how it is computed.
2975 If the target lives in register, make sure not to select a copy mode
2976 larger than the mode of the register.
2978 We could probably emit more efficient code for machines which do not use
2979 strict alignment, but it doesn't seem worth the effort at the current
2980 time. */
2982 copy_mode = word_mode;
2983 if (MEM_P (target))
2985 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2986 if (mem_mode.exists ())
2987 copy_mode = mem_mode.require ();
2989 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2990 copy_mode = tmode;
2992 for (bitpos = 0, xbitpos = padding_correction;
2993 bitpos < bytes * BITS_PER_UNIT;
2994 bitpos += bitsize, xbitpos += bitsize)
2996 /* We need a new source operand each time xbitpos is on a
2997 word boundary and when xbitpos == padding_correction
2998 (the first time through). */
2999 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3000 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3002 /* We need a new destination operand each time bitpos is on
3003 a word boundary. */
3004 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3005 dst = target;
3006 else if (bitpos % BITS_PER_WORD == 0)
3007 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3009 /* Use xbitpos for the source extraction (right justified) and
3010 bitpos for the destination store (left justified). */
3011 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3012 extract_bit_field (src, bitsize,
3013 xbitpos % BITS_PER_WORD, 1,
3014 NULL_RTX, copy_mode, copy_mode,
3015 false, NULL),
3016 false);
3020 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3021 register if it contains any data, otherwise return null.
3023 This is used on targets that return BLKmode values in registers. */
3026 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3028 int i, n_regs;
3029 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3030 unsigned int bitsize;
3031 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3032 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3033 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3034 fixed_size_mode dst_mode;
3035 scalar_int_mode min_mode;
3037 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3039 x = expand_normal (src);
3041 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3042 if (bytes == 0)
3043 return NULL_RTX;
3045 /* If the structure doesn't take up a whole number of words, see
3046 whether the register value should be padded on the left or on
3047 the right. Set PADDING_CORRECTION to the number of padding
3048 bits needed on the left side.
3050 In most ABIs, the structure will be returned at the least end of
3051 the register, which translates to right padding on little-endian
3052 targets and left padding on big-endian targets. The opposite
3053 holds if the structure is returned at the most significant
3054 end of the register. */
3055 if (bytes % UNITS_PER_WORD != 0
3056 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3057 ? !BYTES_BIG_ENDIAN
3058 : BYTES_BIG_ENDIAN))
3059 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3060 * BITS_PER_UNIT));
3062 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3063 dst_words = XALLOCAVEC (rtx, n_regs);
3064 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3065 min_mode = smallest_int_mode_for_size (bitsize);
3067 /* Copy the structure BITSIZE bits at a time. */
3068 for (bitpos = 0, xbitpos = padding_correction;
3069 bitpos < bytes * BITS_PER_UNIT;
3070 bitpos += bitsize, xbitpos += bitsize)
3072 /* We need a new destination pseudo each time xbitpos is
3073 on a word boundary and when xbitpos == padding_correction
3074 (the first time through). */
3075 if (xbitpos % BITS_PER_WORD == 0
3076 || xbitpos == padding_correction)
3078 /* Generate an appropriate register. */
3079 dst_word = gen_reg_rtx (word_mode);
3080 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3082 /* Clear the destination before we move anything into it. */
3083 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3086 /* Find the largest integer mode that can be used to copy all or as
3087 many bits as possible of the structure if the target supports larger
3088 copies. There are too many corner cases here w.r.t to alignments on
3089 the read/writes. So if there is any padding just use single byte
3090 operations. */
3091 opt_scalar_int_mode mode_iter;
3092 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3094 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3096 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3097 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3098 && msize <= BITS_PER_WORD)
3099 bitsize = msize;
3100 else
3101 break;
3105 /* We need a new source operand each time bitpos is on a word
3106 boundary. */
3107 if (bitpos % BITS_PER_WORD == 0)
3108 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3110 /* Use bitpos for the source extraction (left justified) and
3111 xbitpos for the destination store (right justified). */
3112 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3113 0, 0, word_mode,
3114 extract_bit_field (src_word, bitsize,
3115 bitpos % BITS_PER_WORD, 1,
3116 NULL_RTX, word_mode, word_mode,
3117 false, NULL),
3118 false);
3121 if (mode == BLKmode)
3123 /* Find the smallest integer mode large enough to hold the
3124 entire structure. */
3125 opt_scalar_int_mode mode_iter;
3126 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3127 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3128 break;
3130 /* A suitable mode should have been found. */
3131 mode = mode_iter.require ();
3134 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3135 dst_mode = word_mode;
3136 else
3137 dst_mode = mode;
3138 dst = gen_reg_rtx (dst_mode);
3140 for (i = 0; i < n_regs; i++)
3141 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3143 if (mode != dst_mode)
3144 dst = gen_lowpart (mode, dst);
3146 return dst;
3149 /* Add a USE expression for REG to the (possibly empty) list pointed
3150 to by CALL_FUSAGE. REG must denote a hard register. */
3152 void
3153 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3155 gcc_assert (REG_P (reg));
3157 if (!HARD_REGISTER_P (reg))
3158 return;
3160 *call_fusage
3161 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3164 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3165 to by CALL_FUSAGE. REG must denote a hard register. */
3167 void
3168 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3170 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3172 *call_fusage
3173 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3176 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3177 starting at REGNO. All of these registers must be hard registers. */
3179 void
3180 use_regs (rtx *call_fusage, int regno, int nregs)
3182 int i;
3184 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3186 for (i = 0; i < nregs; i++)
3187 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3190 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3191 PARALLEL REGS. This is for calls that pass values in multiple
3192 non-contiguous locations. The Irix 6 ABI has examples of this. */
3194 void
3195 use_group_regs (rtx *call_fusage, rtx regs)
3197 int i;
3199 for (i = 0; i < XVECLEN (regs, 0); i++)
3201 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3203 /* A NULL entry means the parameter goes both on the stack and in
3204 registers. This can also be a MEM for targets that pass values
3205 partially on the stack and partially in registers. */
3206 if (reg != 0 && REG_P (reg))
3207 use_reg (call_fusage, reg);
3211 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3212 assigment and the code of the expresion on the RHS is CODE. Return
3213 NULL otherwise. */
3215 static gimple *
3216 get_def_for_expr (tree name, enum tree_code code)
3218 gimple *def_stmt;
3220 if (TREE_CODE (name) != SSA_NAME)
3221 return NULL;
3223 def_stmt = get_gimple_for_ssa_name (name);
3224 if (!def_stmt
3225 || gimple_assign_rhs_code (def_stmt) != code)
3226 return NULL;
3228 return def_stmt;
3231 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3232 assigment and the class of the expresion on the RHS is CLASS. Return
3233 NULL otherwise. */
3235 static gimple *
3236 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3238 gimple *def_stmt;
3240 if (TREE_CODE (name) != SSA_NAME)
3241 return NULL;
3243 def_stmt = get_gimple_for_ssa_name (name);
3244 if (!def_stmt
3245 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3246 return NULL;
3248 return def_stmt;
3251 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3252 its length in bytes. */
3255 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3256 unsigned int expected_align, HOST_WIDE_INT expected_size,
3257 unsigned HOST_WIDE_INT min_size,
3258 unsigned HOST_WIDE_INT max_size,
3259 unsigned HOST_WIDE_INT probable_max_size,
3260 unsigned ctz_size)
3262 machine_mode mode = GET_MODE (object);
3263 unsigned int align;
3265 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3267 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3268 just move a zero. Otherwise, do this a piece at a time. */
3269 poly_int64 size_val;
3270 if (mode != BLKmode
3271 && poly_int_rtx_p (size, &size_val)
3272 && known_eq (size_val, GET_MODE_SIZE (mode)))
3274 rtx zero = CONST0_RTX (mode);
3275 if (zero != NULL)
3277 emit_move_insn (object, zero);
3278 return NULL;
3281 if (COMPLEX_MODE_P (mode))
3283 zero = CONST0_RTX (GET_MODE_INNER (mode));
3284 if (zero != NULL)
3286 write_complex_part (object, zero, 0);
3287 write_complex_part (object, zero, 1);
3288 return NULL;
3293 if (size == const0_rtx)
3294 return NULL;
3296 align = MEM_ALIGN (object);
3298 if (CONST_INT_P (size)
3299 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3300 CLEAR_BY_PIECES,
3301 optimize_insn_for_speed_p ()))
3302 clear_by_pieces (object, INTVAL (size), align);
3303 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3304 expected_align, expected_size,
3305 min_size, max_size, probable_max_size))
3307 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3308 min_size, max_size,
3309 NULL_RTX, 0, align))
3311 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3312 return set_storage_via_libcall (object, size, const0_rtx,
3313 method == BLOCK_OP_TAILCALL);
3314 else
3315 gcc_unreachable ();
3317 return NULL;
3321 clear_storage (rtx object, rtx size, enum block_op_methods method)
3323 unsigned HOST_WIDE_INT max, min = 0;
3324 if (GET_CODE (size) == CONST_INT)
3325 min = max = UINTVAL (size);
3326 else
3327 max = GET_MODE_MASK (GET_MODE (size));
3328 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3332 /* A subroutine of clear_storage. Expand a call to memset.
3333 Return the return value of memset, 0 otherwise. */
3336 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3338 tree call_expr, fn, object_tree, size_tree, val_tree;
3339 machine_mode size_mode;
3341 object = copy_addr_to_reg (XEXP (object, 0));
3342 object_tree = make_tree (ptr_type_node, object);
3344 if (!CONST_INT_P (val))
3345 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3346 val_tree = make_tree (integer_type_node, val);
3348 size_mode = TYPE_MODE (sizetype);
3349 size = convert_to_mode (size_mode, size, 1);
3350 size = copy_to_mode_reg (size_mode, size);
3351 size_tree = make_tree (sizetype, size);
3353 /* It is incorrect to use the libcall calling conventions for calls to
3354 memset because it can be provided by the user. */
3355 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3356 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3357 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3359 return expand_call (call_expr, NULL_RTX, false);
3362 /* Expand a setmem pattern; return true if successful. */
3364 bool
3365 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3366 unsigned int expected_align, HOST_WIDE_INT expected_size,
3367 unsigned HOST_WIDE_INT min_size,
3368 unsigned HOST_WIDE_INT max_size,
3369 unsigned HOST_WIDE_INT probable_max_size)
3371 /* Try the most limited insn first, because there's no point
3372 including more than one in the machine description unless
3373 the more limited one has some advantage. */
3375 if (expected_align < align)
3376 expected_align = align;
3377 if (expected_size != -1)
3379 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3380 expected_size = max_size;
3381 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3382 expected_size = min_size;
3385 opt_scalar_int_mode mode_iter;
3386 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3388 scalar_int_mode mode = mode_iter.require ();
3389 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3391 if (code != CODE_FOR_nothing
3392 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3393 here because if SIZE is less than the mode mask, as it is
3394 returned by the macro, it will definitely be less than the
3395 actual mode mask. Since SIZE is within the Pmode address
3396 space, we limit MODE to Pmode. */
3397 && ((CONST_INT_P (size)
3398 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3399 <= (GET_MODE_MASK (mode) >> 1)))
3400 || max_size <= (GET_MODE_MASK (mode) >> 1)
3401 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3403 class expand_operand ops[9];
3404 unsigned int nops;
3406 nops = insn_data[(int) code].n_generator_args;
3407 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3409 create_fixed_operand (&ops[0], object);
3410 /* The check above guarantees that this size conversion is valid. */
3411 create_convert_operand_to (&ops[1], size, mode, true);
3412 create_convert_operand_from (&ops[2], val, byte_mode, true);
3413 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3414 if (nops >= 6)
3416 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3417 create_integer_operand (&ops[5], expected_size);
3419 if (nops >= 8)
3421 create_integer_operand (&ops[6], min_size);
3422 /* If we cannot represent the maximal size,
3423 make parameter NULL. */
3424 if ((HOST_WIDE_INT) max_size != -1)
3425 create_integer_operand (&ops[7], max_size);
3426 else
3427 create_fixed_operand (&ops[7], NULL);
3429 if (nops == 9)
3431 /* If we cannot represent the maximal size,
3432 make parameter NULL. */
3433 if ((HOST_WIDE_INT) probable_max_size != -1)
3434 create_integer_operand (&ops[8], probable_max_size);
3435 else
3436 create_fixed_operand (&ops[8], NULL);
3438 if (maybe_expand_insn (code, nops, ops))
3439 return true;
3443 return false;
3447 /* Write to one of the components of the complex value CPLX. Write VAL to
3448 the real part if IMAG_P is false, and the imaginary part if its true. */
3450 void
3451 write_complex_part (rtx cplx, rtx val, bool imag_p)
3453 machine_mode cmode;
3454 scalar_mode imode;
3455 unsigned ibitsize;
3457 if (GET_CODE (cplx) == CONCAT)
3459 emit_move_insn (XEXP (cplx, imag_p), val);
3460 return;
3463 cmode = GET_MODE (cplx);
3464 imode = GET_MODE_INNER (cmode);
3465 ibitsize = GET_MODE_BITSIZE (imode);
3467 /* For MEMs simplify_gen_subreg may generate an invalid new address
3468 because, e.g., the original address is considered mode-dependent
3469 by the target, which restricts simplify_subreg from invoking
3470 adjust_address_nv. Instead of preparing fallback support for an
3471 invalid address, we call adjust_address_nv directly. */
3472 if (MEM_P (cplx))
3474 emit_move_insn (adjust_address_nv (cplx, imode,
3475 imag_p ? GET_MODE_SIZE (imode) : 0),
3476 val);
3477 return;
3480 /* If the sub-object is at least word sized, then we know that subregging
3481 will work. This special case is important, since store_bit_field
3482 wants to operate on integer modes, and there's rarely an OImode to
3483 correspond to TCmode. */
3484 if (ibitsize >= BITS_PER_WORD
3485 /* For hard regs we have exact predicates. Assume we can split
3486 the original object if it spans an even number of hard regs.
3487 This special case is important for SCmode on 64-bit platforms
3488 where the natural size of floating-point regs is 32-bit. */
3489 || (REG_P (cplx)
3490 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3491 && REG_NREGS (cplx) % 2 == 0))
3493 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3494 imag_p ? GET_MODE_SIZE (imode) : 0);
3495 if (part)
3497 emit_move_insn (part, val);
3498 return;
3500 else
3501 /* simplify_gen_subreg may fail for sub-word MEMs. */
3502 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3505 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3506 false);
3509 /* Extract one of the components of the complex value CPLX. Extract the
3510 real part if IMAG_P is false, and the imaginary part if it's true. */
3513 read_complex_part (rtx cplx, bool imag_p)
3515 machine_mode cmode;
3516 scalar_mode imode;
3517 unsigned ibitsize;
3519 if (GET_CODE (cplx) == CONCAT)
3520 return XEXP (cplx, imag_p);
3522 cmode = GET_MODE (cplx);
3523 imode = GET_MODE_INNER (cmode);
3524 ibitsize = GET_MODE_BITSIZE (imode);
3526 /* Special case reads from complex constants that got spilled to memory. */
3527 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3529 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3530 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3532 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3533 if (CONSTANT_CLASS_P (part))
3534 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3538 /* For MEMs simplify_gen_subreg may generate an invalid new address
3539 because, e.g., the original address is considered mode-dependent
3540 by the target, which restricts simplify_subreg from invoking
3541 adjust_address_nv. Instead of preparing fallback support for an
3542 invalid address, we call adjust_address_nv directly. */
3543 if (MEM_P (cplx))
3544 return adjust_address_nv (cplx, imode,
3545 imag_p ? GET_MODE_SIZE (imode) : 0);
3547 /* If the sub-object is at least word sized, then we know that subregging
3548 will work. This special case is important, since extract_bit_field
3549 wants to operate on integer modes, and there's rarely an OImode to
3550 correspond to TCmode. */
3551 if (ibitsize >= BITS_PER_WORD
3552 /* For hard regs we have exact predicates. Assume we can split
3553 the original object if it spans an even number of hard regs.
3554 This special case is important for SCmode on 64-bit platforms
3555 where the natural size of floating-point regs is 32-bit. */
3556 || (REG_P (cplx)
3557 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3558 && REG_NREGS (cplx) % 2 == 0))
3560 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3561 imag_p ? GET_MODE_SIZE (imode) : 0);
3562 if (ret)
3563 return ret;
3564 else
3565 /* simplify_gen_subreg may fail for sub-word MEMs. */
3566 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3569 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3570 true, NULL_RTX, imode, imode, false, NULL);
3573 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3574 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3575 represented in NEW_MODE. If FORCE is true, this will never happen, as
3576 we'll force-create a SUBREG if needed. */
3578 static rtx
3579 emit_move_change_mode (machine_mode new_mode,
3580 machine_mode old_mode, rtx x, bool force)
3582 rtx ret;
3584 if (push_operand (x, GET_MODE (x)))
3586 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3587 MEM_COPY_ATTRIBUTES (ret, x);
3589 else if (MEM_P (x))
3591 /* We don't have to worry about changing the address since the
3592 size in bytes is supposed to be the same. */
3593 if (reload_in_progress)
3595 /* Copy the MEM to change the mode and move any
3596 substitutions from the old MEM to the new one. */
3597 ret = adjust_address_nv (x, new_mode, 0);
3598 copy_replacements (x, ret);
3600 else
3601 ret = adjust_address (x, new_mode, 0);
3603 else
3605 /* Note that we do want simplify_subreg's behavior of validating
3606 that the new mode is ok for a hard register. If we were to use
3607 simplify_gen_subreg, we would create the subreg, but would
3608 probably run into the target not being able to implement it. */
3609 /* Except, of course, when FORCE is true, when this is exactly what
3610 we want. Which is needed for CCmodes on some targets. */
3611 if (force)
3612 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3613 else
3614 ret = simplify_subreg (new_mode, x, old_mode, 0);
3617 return ret;
3620 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3621 an integer mode of the same size as MODE. Returns the instruction
3622 emitted, or NULL if such a move could not be generated. */
3624 static rtx_insn *
3625 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3627 scalar_int_mode imode;
3628 enum insn_code code;
3630 /* There must exist a mode of the exact size we require. */
3631 if (!int_mode_for_mode (mode).exists (&imode))
3632 return NULL;
3634 /* The target must support moves in this mode. */
3635 code = optab_handler (mov_optab, imode);
3636 if (code == CODE_FOR_nothing)
3637 return NULL;
3639 x = emit_move_change_mode (imode, mode, x, force);
3640 if (x == NULL_RTX)
3641 return NULL;
3642 y = emit_move_change_mode (imode, mode, y, force);
3643 if (y == NULL_RTX)
3644 return NULL;
3645 return emit_insn (GEN_FCN (code) (x, y));
3648 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3649 Return an equivalent MEM that does not use an auto-increment. */
3652 emit_move_resolve_push (machine_mode mode, rtx x)
3654 enum rtx_code code = GET_CODE (XEXP (x, 0));
3655 rtx temp;
3657 poly_int64 adjust = GET_MODE_SIZE (mode);
3658 #ifdef PUSH_ROUNDING
3659 adjust = PUSH_ROUNDING (adjust);
3660 #endif
3661 if (code == PRE_DEC || code == POST_DEC)
3662 adjust = -adjust;
3663 else if (code == PRE_MODIFY || code == POST_MODIFY)
3665 rtx expr = XEXP (XEXP (x, 0), 1);
3667 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3668 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3669 if (GET_CODE (expr) == MINUS)
3670 val = -val;
3671 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3672 adjust = val;
3675 /* Do not use anti_adjust_stack, since we don't want to update
3676 stack_pointer_delta. */
3677 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3678 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3679 0, OPTAB_LIB_WIDEN);
3680 if (temp != stack_pointer_rtx)
3681 emit_move_insn (stack_pointer_rtx, temp);
3683 switch (code)
3685 case PRE_INC:
3686 case PRE_DEC:
3687 case PRE_MODIFY:
3688 temp = stack_pointer_rtx;
3689 break;
3690 case POST_INC:
3691 case POST_DEC:
3692 case POST_MODIFY:
3693 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3694 break;
3695 default:
3696 gcc_unreachable ();
3699 return replace_equiv_address (x, temp);
3702 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3703 X is known to satisfy push_operand, and MODE is known to be complex.
3704 Returns the last instruction emitted. */
3706 rtx_insn *
3707 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3709 scalar_mode submode = GET_MODE_INNER (mode);
3710 bool imag_first;
3712 #ifdef PUSH_ROUNDING
3713 poly_int64 submodesize = GET_MODE_SIZE (submode);
3715 /* In case we output to the stack, but the size is smaller than the
3716 machine can push exactly, we need to use move instructions. */
3717 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3719 x = emit_move_resolve_push (mode, x);
3720 return emit_move_insn (x, y);
3722 #endif
3724 /* Note that the real part always precedes the imag part in memory
3725 regardless of machine's endianness. */
3726 switch (GET_CODE (XEXP (x, 0)))
3728 case PRE_DEC:
3729 case POST_DEC:
3730 imag_first = true;
3731 break;
3732 case PRE_INC:
3733 case POST_INC:
3734 imag_first = false;
3735 break;
3736 default:
3737 gcc_unreachable ();
3740 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3741 read_complex_part (y, imag_first));
3742 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3743 read_complex_part (y, !imag_first));
3746 /* A subroutine of emit_move_complex. Perform the move from Y to X
3747 via two moves of the parts. Returns the last instruction emitted. */
3749 rtx_insn *
3750 emit_move_complex_parts (rtx x, rtx y)
3752 /* Show the output dies here. This is necessary for SUBREGs
3753 of pseudos since we cannot track their lifetimes correctly;
3754 hard regs shouldn't appear here except as return values. */
3755 if (!reload_completed && !reload_in_progress
3756 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3757 emit_clobber (x);
3759 write_complex_part (x, read_complex_part (y, false), false);
3760 write_complex_part (x, read_complex_part (y, true), true);
3762 return get_last_insn ();
3765 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3766 MODE is known to be complex. Returns the last instruction emitted. */
3768 static rtx_insn *
3769 emit_move_complex (machine_mode mode, rtx x, rtx y)
3771 bool try_int;
3773 /* Need to take special care for pushes, to maintain proper ordering
3774 of the data, and possibly extra padding. */
3775 if (push_operand (x, mode))
3776 return emit_move_complex_push (mode, x, y);
3778 /* See if we can coerce the target into moving both values at once, except
3779 for floating point where we favor moving as parts if this is easy. */
3780 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3781 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3782 && !(REG_P (x)
3783 && HARD_REGISTER_P (x)
3784 && REG_NREGS (x) == 1)
3785 && !(REG_P (y)
3786 && HARD_REGISTER_P (y)
3787 && REG_NREGS (y) == 1))
3788 try_int = false;
3789 /* Not possible if the values are inherently not adjacent. */
3790 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3791 try_int = false;
3792 /* Is possible if both are registers (or subregs of registers). */
3793 else if (register_operand (x, mode) && register_operand (y, mode))
3794 try_int = true;
3795 /* If one of the operands is a memory, and alignment constraints
3796 are friendly enough, we may be able to do combined memory operations.
3797 We do not attempt this if Y is a constant because that combination is
3798 usually better with the by-parts thing below. */
3799 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3800 && (!STRICT_ALIGNMENT
3801 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3802 try_int = true;
3803 else
3804 try_int = false;
3806 if (try_int)
3808 rtx_insn *ret;
3810 /* For memory to memory moves, optimal behavior can be had with the
3811 existing block move logic. But use normal expansion if optimizing
3812 for size. */
3813 if (MEM_P (x) && MEM_P (y))
3815 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3816 (optimize_insn_for_speed_p()
3817 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3818 return get_last_insn ();
3821 ret = emit_move_via_integer (mode, x, y, true);
3822 if (ret)
3823 return ret;
3826 return emit_move_complex_parts (x, y);
3829 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3830 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3832 static rtx_insn *
3833 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3835 rtx_insn *ret;
3837 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3838 if (mode != CCmode)
3840 enum insn_code code = optab_handler (mov_optab, CCmode);
3841 if (code != CODE_FOR_nothing)
3843 x = emit_move_change_mode (CCmode, mode, x, true);
3844 y = emit_move_change_mode (CCmode, mode, y, true);
3845 return emit_insn (GEN_FCN (code) (x, y));
3849 /* Otherwise, find the MODE_INT mode of the same width. */
3850 ret = emit_move_via_integer (mode, x, y, false);
3851 gcc_assert (ret != NULL);
3852 return ret;
3855 /* Return true if word I of OP lies entirely in the
3856 undefined bits of a paradoxical subreg. */
3858 static bool
3859 undefined_operand_subword_p (const_rtx op, int i)
3861 if (GET_CODE (op) != SUBREG)
3862 return false;
3863 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3864 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3865 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3866 || known_le (offset, -UNITS_PER_WORD));
3869 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3870 MODE is any multi-word or full-word mode that lacks a move_insn
3871 pattern. Note that you will get better code if you define such
3872 patterns, even if they must turn into multiple assembler instructions. */
3874 static rtx_insn *
3875 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3877 rtx_insn *last_insn = 0;
3878 rtx_insn *seq;
3879 rtx inner;
3880 bool need_clobber;
3881 int i, mode_size;
3883 /* This function can only handle cases where the number of words is
3884 known at compile time. */
3885 mode_size = GET_MODE_SIZE (mode).to_constant ();
3886 gcc_assert (mode_size >= UNITS_PER_WORD);
3888 /* If X is a push on the stack, do the push now and replace
3889 X with a reference to the stack pointer. */
3890 if (push_operand (x, mode))
3891 x = emit_move_resolve_push (mode, x);
3893 /* If we are in reload, see if either operand is a MEM whose address
3894 is scheduled for replacement. */
3895 if (reload_in_progress && MEM_P (x)
3896 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3897 x = replace_equiv_address_nv (x, inner);
3898 if (reload_in_progress && MEM_P (y)
3899 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3900 y = replace_equiv_address_nv (y, inner);
3902 start_sequence ();
3904 need_clobber = false;
3905 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3907 /* Do not generate code for a move if it would go entirely
3908 to the non-existing bits of a paradoxical subreg. */
3909 if (undefined_operand_subword_p (x, i))
3910 continue;
3912 rtx xpart = operand_subword (x, i, 1, mode);
3913 rtx ypart;
3915 /* Do not generate code for a move if it would come entirely
3916 from the undefined bits of a paradoxical subreg. */
3917 if (undefined_operand_subword_p (y, i))
3918 continue;
3920 ypart = operand_subword (y, i, 1, mode);
3922 /* If we can't get a part of Y, put Y into memory if it is a
3923 constant. Otherwise, force it into a register. Then we must
3924 be able to get a part of Y. */
3925 if (ypart == 0 && CONSTANT_P (y))
3927 y = use_anchored_address (force_const_mem (mode, y));
3928 ypart = operand_subword (y, i, 1, mode);
3930 else if (ypart == 0)
3931 ypart = operand_subword_force (y, i, mode);
3933 gcc_assert (xpart && ypart);
3935 need_clobber |= (GET_CODE (xpart) == SUBREG);
3937 last_insn = emit_move_insn (xpart, ypart);
3940 seq = get_insns ();
3941 end_sequence ();
3943 /* Show the output dies here. This is necessary for SUBREGs
3944 of pseudos since we cannot track their lifetimes correctly;
3945 hard regs shouldn't appear here except as return values.
3946 We never want to emit such a clobber after reload. */
3947 if (x != y
3948 && ! (reload_in_progress || reload_completed)
3949 && need_clobber != 0)
3950 emit_clobber (x);
3952 emit_insn (seq);
3954 return last_insn;
3957 /* Low level part of emit_move_insn.
3958 Called just like emit_move_insn, but assumes X and Y
3959 are basically valid. */
3961 rtx_insn *
3962 emit_move_insn_1 (rtx x, rtx y)
3964 machine_mode mode = GET_MODE (x);
3965 enum insn_code code;
3967 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3969 code = optab_handler (mov_optab, mode);
3970 if (code != CODE_FOR_nothing)
3971 return emit_insn (GEN_FCN (code) (x, y));
3973 /* Expand complex moves by moving real part and imag part. */
3974 if (COMPLEX_MODE_P (mode))
3975 return emit_move_complex (mode, x, y);
3977 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3978 || ALL_FIXED_POINT_MODE_P (mode))
3980 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3982 /* If we can't find an integer mode, use multi words. */
3983 if (result)
3984 return result;
3985 else
3986 return emit_move_multi_word (mode, x, y);
3989 if (GET_MODE_CLASS (mode) == MODE_CC)
3990 return emit_move_ccmode (mode, x, y);
3992 /* Try using a move pattern for the corresponding integer mode. This is
3993 only safe when simplify_subreg can convert MODE constants into integer
3994 constants. At present, it can only do this reliably if the value
3995 fits within a HOST_WIDE_INT. */
3996 if (!CONSTANT_P (y)
3997 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3999 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4001 if (ret)
4003 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4004 return ret;
4008 return emit_move_multi_word (mode, x, y);
4011 /* Generate code to copy Y into X.
4012 Both Y and X must have the same mode, except that
4013 Y can be a constant with VOIDmode.
4014 This mode cannot be BLKmode; use emit_block_move for that.
4016 Return the last instruction emitted. */
4018 rtx_insn *
4019 emit_move_insn (rtx x, rtx y)
4021 machine_mode mode = GET_MODE (x);
4022 rtx y_cst = NULL_RTX;
4023 rtx_insn *last_insn;
4024 rtx set;
4026 gcc_assert (mode != BLKmode
4027 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4029 /* If we have a copy that looks like one of the following patterns:
4030 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4031 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4032 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4033 (set (subreg:M1 (reg:M2 ...)) (constant C))
4034 where mode M1 is equal in size to M2, try to detect whether the
4035 mode change involves an implicit round trip through memory.
4036 If so, see if we can avoid that by removing the subregs and
4037 doing the move in mode M2 instead. */
4039 rtx x_inner = NULL_RTX;
4040 rtx y_inner = NULL_RTX;
4042 auto candidate_subreg_p = [&](rtx subreg) {
4043 return (REG_P (SUBREG_REG (subreg))
4044 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4045 GET_MODE_SIZE (GET_MODE (subreg)))
4046 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4047 != CODE_FOR_nothing);
4050 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4051 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4052 && !push_operand (mem, GET_MODE (mem))
4053 /* Not a candiate if innermode requires too much alignment. */
4054 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4055 || targetm.slow_unaligned_access (GET_MODE (mem),
4056 MEM_ALIGN (mem))
4057 || !targetm.slow_unaligned_access (innermode,
4058 MEM_ALIGN (mem))));
4061 if (SUBREG_P (x) && candidate_subreg_p (x))
4062 x_inner = SUBREG_REG (x);
4064 if (SUBREG_P (y) && candidate_subreg_p (y))
4065 y_inner = SUBREG_REG (y);
4067 if (x_inner != NULL_RTX
4068 && y_inner != NULL_RTX
4069 && GET_MODE (x_inner) == GET_MODE (y_inner)
4070 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4072 x = x_inner;
4073 y = y_inner;
4074 mode = GET_MODE (x_inner);
4076 else if (x_inner != NULL_RTX
4077 && MEM_P (y)
4078 && candidate_mem_p (GET_MODE (x_inner), y))
4080 x = x_inner;
4081 y = adjust_address (y, GET_MODE (x_inner), 0);
4082 mode = GET_MODE (x_inner);
4084 else if (y_inner != NULL_RTX
4085 && MEM_P (x)
4086 && candidate_mem_p (GET_MODE (y_inner), x))
4088 x = adjust_address (x, GET_MODE (y_inner), 0);
4089 y = y_inner;
4090 mode = GET_MODE (y_inner);
4092 else if (x_inner != NULL_RTX
4093 && CONSTANT_P (y)
4094 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4095 mode, ALL_REGS)
4096 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4098 x = x_inner;
4099 y = y_inner;
4100 mode = GET_MODE (x_inner);
4103 if (CONSTANT_P (y))
4105 if (optimize
4106 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4107 && (last_insn = compress_float_constant (x, y)))
4108 return last_insn;
4110 y_cst = y;
4112 if (!targetm.legitimate_constant_p (mode, y))
4114 y = force_const_mem (mode, y);
4116 /* If the target's cannot_force_const_mem prevented the spill,
4117 assume that the target's move expanders will also take care
4118 of the non-legitimate constant. */
4119 if (!y)
4120 y = y_cst;
4121 else
4122 y = use_anchored_address (y);
4126 /* If X or Y are memory references, verify that their addresses are valid
4127 for the machine. */
4128 if (MEM_P (x)
4129 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4130 MEM_ADDR_SPACE (x))
4131 && ! push_operand (x, GET_MODE (x))))
4132 x = validize_mem (x);
4134 if (MEM_P (y)
4135 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4136 MEM_ADDR_SPACE (y)))
4137 y = validize_mem (y);
4139 gcc_assert (mode != BLKmode);
4141 last_insn = emit_move_insn_1 (x, y);
4143 if (y_cst && REG_P (x)
4144 && (set = single_set (last_insn)) != NULL_RTX
4145 && SET_DEST (set) == x
4146 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4147 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4149 return last_insn;
4152 /* Generate the body of an instruction to copy Y into X.
4153 It may be a list of insns, if one insn isn't enough. */
4155 rtx_insn *
4156 gen_move_insn (rtx x, rtx y)
4158 rtx_insn *seq;
4160 start_sequence ();
4161 emit_move_insn_1 (x, y);
4162 seq = get_insns ();
4163 end_sequence ();
4164 return seq;
4167 /* If Y is representable exactly in a narrower mode, and the target can
4168 perform the extension directly from constant or memory, then emit the
4169 move as an extension. */
4171 static rtx_insn *
4172 compress_float_constant (rtx x, rtx y)
4174 machine_mode dstmode = GET_MODE (x);
4175 machine_mode orig_srcmode = GET_MODE (y);
4176 machine_mode srcmode;
4177 const REAL_VALUE_TYPE *r;
4178 int oldcost, newcost;
4179 bool speed = optimize_insn_for_speed_p ();
4181 r = CONST_DOUBLE_REAL_VALUE (y);
4183 if (targetm.legitimate_constant_p (dstmode, y))
4184 oldcost = set_src_cost (y, orig_srcmode, speed);
4185 else
4186 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4188 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4190 enum insn_code ic;
4191 rtx trunc_y;
4192 rtx_insn *last_insn;
4194 /* Skip if the target can't extend this way. */
4195 ic = can_extend_p (dstmode, srcmode, 0);
4196 if (ic == CODE_FOR_nothing)
4197 continue;
4199 /* Skip if the narrowed value isn't exact. */
4200 if (! exact_real_truncate (srcmode, r))
4201 continue;
4203 trunc_y = const_double_from_real_value (*r, srcmode);
4205 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4207 /* Skip if the target needs extra instructions to perform
4208 the extension. */
4209 if (!insn_operand_matches (ic, 1, trunc_y))
4210 continue;
4211 /* This is valid, but may not be cheaper than the original. */
4212 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4213 dstmode, speed);
4214 if (oldcost < newcost)
4215 continue;
4217 else if (float_extend_from_mem[dstmode][srcmode])
4219 trunc_y = force_const_mem (srcmode, trunc_y);
4220 /* This is valid, but may not be cheaper than the original. */
4221 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4222 dstmode, speed);
4223 if (oldcost < newcost)
4224 continue;
4225 trunc_y = validize_mem (trunc_y);
4227 else
4228 continue;
4230 /* For CSE's benefit, force the compressed constant pool entry
4231 into a new pseudo. This constant may be used in different modes,
4232 and if not, combine will put things back together for us. */
4233 trunc_y = force_reg (srcmode, trunc_y);
4235 /* If x is a hard register, perform the extension into a pseudo,
4236 so that e.g. stack realignment code is aware of it. */
4237 rtx target = x;
4238 if (REG_P (x) && HARD_REGISTER_P (x))
4239 target = gen_reg_rtx (dstmode);
4241 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4242 last_insn = get_last_insn ();
4244 if (REG_P (target))
4245 set_unique_reg_note (last_insn, REG_EQUAL, y);
4247 if (target != x)
4248 return emit_move_insn (x, target);
4249 return last_insn;
4252 return NULL;
4255 /* Pushing data onto the stack. */
4257 /* Push a block of length SIZE (perhaps variable)
4258 and return an rtx to address the beginning of the block.
4259 The value may be virtual_outgoing_args_rtx.
4261 EXTRA is the number of bytes of padding to push in addition to SIZE.
4262 BELOW nonzero means this padding comes at low addresses;
4263 otherwise, the padding comes at high addresses. */
4266 push_block (rtx size, poly_int64 extra, int below)
4268 rtx temp;
4270 size = convert_modes (Pmode, ptr_mode, size, 1);
4271 if (CONSTANT_P (size))
4272 anti_adjust_stack (plus_constant (Pmode, size, extra));
4273 else if (REG_P (size) && known_eq (extra, 0))
4274 anti_adjust_stack (size);
4275 else
4277 temp = copy_to_mode_reg (Pmode, size);
4278 if (maybe_ne (extra, 0))
4279 temp = expand_binop (Pmode, add_optab, temp,
4280 gen_int_mode (extra, Pmode),
4281 temp, 0, OPTAB_LIB_WIDEN);
4282 anti_adjust_stack (temp);
4285 if (STACK_GROWS_DOWNWARD)
4287 temp = virtual_outgoing_args_rtx;
4288 if (maybe_ne (extra, 0) && below)
4289 temp = plus_constant (Pmode, temp, extra);
4291 else
4293 poly_int64 csize;
4294 if (poly_int_rtx_p (size, &csize))
4295 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4296 -csize - (below ? 0 : extra));
4297 else if (maybe_ne (extra, 0) && !below)
4298 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4299 negate_rtx (Pmode, plus_constant (Pmode, size,
4300 extra)));
4301 else
4302 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4303 negate_rtx (Pmode, size));
4306 return memory_address (NARROWEST_INT_MODE, temp);
4309 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4311 static rtx
4312 mem_autoinc_base (rtx mem)
4314 if (MEM_P (mem))
4316 rtx addr = XEXP (mem, 0);
4317 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4318 return XEXP (addr, 0);
4320 return NULL;
4323 /* A utility routine used here, in reload, and in try_split. The insns
4324 after PREV up to and including LAST are known to adjust the stack,
4325 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4326 placing notes as appropriate. PREV may be NULL, indicating the
4327 entire insn sequence prior to LAST should be scanned.
4329 The set of allowed stack pointer modifications is small:
4330 (1) One or more auto-inc style memory references (aka pushes),
4331 (2) One or more addition/subtraction with the SP as destination,
4332 (3) A single move insn with the SP as destination,
4333 (4) A call_pop insn,
4334 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4336 Insns in the sequence that do not modify the SP are ignored,
4337 except for noreturn calls.
4339 The return value is the amount of adjustment that can be trivially
4340 verified, via immediate operand or auto-inc. If the adjustment
4341 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4343 poly_int64
4344 find_args_size_adjust (rtx_insn *insn)
4346 rtx dest, set, pat;
4347 int i;
4349 pat = PATTERN (insn);
4350 set = NULL;
4352 /* Look for a call_pop pattern. */
4353 if (CALL_P (insn))
4355 /* We have to allow non-call_pop patterns for the case
4356 of emit_single_push_insn of a TLS address. */
4357 if (GET_CODE (pat) != PARALLEL)
4358 return 0;
4360 /* All call_pop have a stack pointer adjust in the parallel.
4361 The call itself is always first, and the stack adjust is
4362 usually last, so search from the end. */
4363 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4365 set = XVECEXP (pat, 0, i);
4366 if (GET_CODE (set) != SET)
4367 continue;
4368 dest = SET_DEST (set);
4369 if (dest == stack_pointer_rtx)
4370 break;
4372 /* We'd better have found the stack pointer adjust. */
4373 if (i == 0)
4374 return 0;
4375 /* Fall through to process the extracted SET and DEST
4376 as if it was a standalone insn. */
4378 else if (GET_CODE (pat) == SET)
4379 set = pat;
4380 else if ((set = single_set (insn)) != NULL)
4382 else if (GET_CODE (pat) == PARALLEL)
4384 /* ??? Some older ports use a parallel with a stack adjust
4385 and a store for a PUSH_ROUNDING pattern, rather than a
4386 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4387 /* ??? See h8300 and m68k, pushqi1. */
4388 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4390 set = XVECEXP (pat, 0, i);
4391 if (GET_CODE (set) != SET)
4392 continue;
4393 dest = SET_DEST (set);
4394 if (dest == stack_pointer_rtx)
4395 break;
4397 /* We do not expect an auto-inc of the sp in the parallel. */
4398 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4399 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4400 != stack_pointer_rtx);
4402 if (i < 0)
4403 return 0;
4405 else
4406 return 0;
4408 dest = SET_DEST (set);
4410 /* Look for direct modifications of the stack pointer. */
4411 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4413 /* Look for a trivial adjustment, otherwise assume nothing. */
4414 /* Note that the SPU restore_stack_block pattern refers to
4415 the stack pointer in V4SImode. Consider that non-trivial. */
4416 poly_int64 offset;
4417 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4418 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4419 return offset;
4420 /* ??? Reload can generate no-op moves, which will be cleaned
4421 up later. Recognize it and continue searching. */
4422 else if (rtx_equal_p (dest, SET_SRC (set)))
4423 return 0;
4424 else
4425 return HOST_WIDE_INT_MIN;
4427 else
4429 rtx mem, addr;
4431 /* Otherwise only think about autoinc patterns. */
4432 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4434 mem = dest;
4435 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4436 != stack_pointer_rtx);
4438 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4439 mem = SET_SRC (set);
4440 else
4441 return 0;
4443 addr = XEXP (mem, 0);
4444 switch (GET_CODE (addr))
4446 case PRE_INC:
4447 case POST_INC:
4448 return GET_MODE_SIZE (GET_MODE (mem));
4449 case PRE_DEC:
4450 case POST_DEC:
4451 return -GET_MODE_SIZE (GET_MODE (mem));
4452 case PRE_MODIFY:
4453 case POST_MODIFY:
4454 addr = XEXP (addr, 1);
4455 gcc_assert (GET_CODE (addr) == PLUS);
4456 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4457 return rtx_to_poly_int64 (XEXP (addr, 1));
4458 default:
4459 gcc_unreachable ();
4464 poly_int64
4465 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4466 poly_int64 end_args_size)
4468 poly_int64 args_size = end_args_size;
4469 bool saw_unknown = false;
4470 rtx_insn *insn;
4472 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4474 if (!NONDEBUG_INSN_P (insn))
4475 continue;
4477 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4478 a call argument containing a TLS address that itself requires
4479 a call to __tls_get_addr. The handling of stack_pointer_delta
4480 in emit_single_push_insn is supposed to ensure that any such
4481 notes are already correct. */
4482 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4483 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4485 poly_int64 this_delta = find_args_size_adjust (insn);
4486 if (known_eq (this_delta, 0))
4488 if (!CALL_P (insn)
4489 || ACCUMULATE_OUTGOING_ARGS
4490 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4491 continue;
4494 gcc_assert (!saw_unknown);
4495 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4496 saw_unknown = true;
4498 if (!note)
4499 add_args_size_note (insn, args_size);
4500 if (STACK_GROWS_DOWNWARD)
4501 this_delta = -poly_uint64 (this_delta);
4503 if (saw_unknown)
4504 args_size = HOST_WIDE_INT_MIN;
4505 else
4506 args_size -= this_delta;
4509 return args_size;
4512 #ifdef PUSH_ROUNDING
4513 /* Emit single push insn. */
4515 static void
4516 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4518 rtx dest_addr;
4519 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4520 rtx dest;
4521 enum insn_code icode;
4523 /* If there is push pattern, use it. Otherwise try old way of throwing
4524 MEM representing push operation to move expander. */
4525 icode = optab_handler (push_optab, mode);
4526 if (icode != CODE_FOR_nothing)
4528 class expand_operand ops[1];
4530 create_input_operand (&ops[0], x, mode);
4531 if (maybe_expand_insn (icode, 1, ops))
4532 return;
4534 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4535 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4536 /* If we are to pad downward, adjust the stack pointer first and
4537 then store X into the stack location using an offset. This is
4538 because emit_move_insn does not know how to pad; it does not have
4539 access to type. */
4540 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4542 emit_move_insn (stack_pointer_rtx,
4543 expand_binop (Pmode,
4544 STACK_GROWS_DOWNWARD ? sub_optab
4545 : add_optab,
4546 stack_pointer_rtx,
4547 gen_int_mode (rounded_size, Pmode),
4548 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4550 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4551 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4552 /* We have already decremented the stack pointer, so get the
4553 previous value. */
4554 offset += rounded_size;
4556 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4557 /* We have already incremented the stack pointer, so get the
4558 previous value. */
4559 offset -= rounded_size;
4561 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4563 else
4565 if (STACK_GROWS_DOWNWARD)
4566 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4567 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4568 else
4569 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4570 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4572 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4575 dest = gen_rtx_MEM (mode, dest_addr);
4577 if (type != 0)
4579 set_mem_attributes (dest, type, 1);
4581 if (cfun->tail_call_marked)
4582 /* Function incoming arguments may overlap with sibling call
4583 outgoing arguments and we cannot allow reordering of reads
4584 from function arguments with stores to outgoing arguments
4585 of sibling calls. */
4586 set_mem_alias_set (dest, 0);
4588 emit_move_insn (dest, x);
4591 /* Emit and annotate a single push insn. */
4593 static void
4594 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4596 poly_int64 delta, old_delta = stack_pointer_delta;
4597 rtx_insn *prev = get_last_insn ();
4598 rtx_insn *last;
4600 emit_single_push_insn_1 (mode, x, type);
4602 /* Adjust stack_pointer_delta to describe the situation after the push
4603 we just performed. Note that we must do this after the push rather
4604 than before the push in case calculating X needs pushes and pops of
4605 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4606 for such pushes and pops must not include the effect of the future
4607 push of X. */
4608 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4610 last = get_last_insn ();
4612 /* Notice the common case where we emitted exactly one insn. */
4613 if (PREV_INSN (last) == prev)
4615 add_args_size_note (last, stack_pointer_delta);
4616 return;
4619 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4620 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4621 || known_eq (delta, old_delta));
4623 #endif
4625 /* If reading SIZE bytes from X will end up reading from
4626 Y return the number of bytes that overlap. Return -1
4627 if there is no overlap or -2 if we can't determine
4628 (for example when X and Y have different base registers). */
4630 static int
4631 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4633 rtx tmp = plus_constant (Pmode, x, size);
4634 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4636 if (!CONST_INT_P (sub))
4637 return -2;
4639 HOST_WIDE_INT val = INTVAL (sub);
4641 return IN_RANGE (val, 1, size) ? val : -1;
4644 /* Generate code to push X onto the stack, assuming it has mode MODE and
4645 type TYPE.
4646 MODE is redundant except when X is a CONST_INT (since they don't
4647 carry mode info).
4648 SIZE is an rtx for the size of data to be copied (in bytes),
4649 needed only if X is BLKmode.
4650 Return true if successful. May return false if asked to push a
4651 partial argument during a sibcall optimization (as specified by
4652 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4653 to not overlap.
4655 ALIGN (in bits) is maximum alignment we can assume.
4657 If PARTIAL and REG are both nonzero, then copy that many of the first
4658 bytes of X into registers starting with REG, and push the rest of X.
4659 The amount of space pushed is decreased by PARTIAL bytes.
4660 REG must be a hard register in this case.
4661 If REG is zero but PARTIAL is not, take any all others actions for an
4662 argument partially in registers, but do not actually load any
4663 registers.
4665 EXTRA is the amount in bytes of extra space to leave next to this arg.
4666 This is ignored if an argument block has already been allocated.
4668 On a machine that lacks real push insns, ARGS_ADDR is the address of
4669 the bottom of the argument block for this call. We use indexing off there
4670 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4671 argument block has not been preallocated.
4673 ARGS_SO_FAR is the size of args previously pushed for this call.
4675 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4676 for arguments passed in registers. If nonzero, it will be the number
4677 of bytes required. */
4679 bool
4680 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4681 unsigned int align, int partial, rtx reg, poly_int64 extra,
4682 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4683 rtx alignment_pad, bool sibcall_p)
4685 rtx xinner;
4686 pad_direction stack_direction
4687 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4689 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4690 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4691 Default is below for small data on big-endian machines; else above. */
4692 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4694 /* Invert direction if stack is post-decrement.
4695 FIXME: why? */
4696 if (STACK_PUSH_CODE == POST_DEC)
4697 if (where_pad != PAD_NONE)
4698 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4700 xinner = x;
4702 int nregs = partial / UNITS_PER_WORD;
4703 rtx *tmp_regs = NULL;
4704 int overlapping = 0;
4706 if (mode == BLKmode
4707 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4709 /* Copy a block into the stack, entirely or partially. */
4711 rtx temp;
4712 int used;
4713 int offset;
4714 int skip;
4716 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4717 used = partial - offset;
4719 if (mode != BLKmode)
4721 /* A value is to be stored in an insufficiently aligned
4722 stack slot; copy via a suitably aligned slot if
4723 necessary. */
4724 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4725 if (!MEM_P (xinner))
4727 temp = assign_temp (type, 1, 1);
4728 emit_move_insn (temp, xinner);
4729 xinner = temp;
4733 gcc_assert (size);
4735 /* USED is now the # of bytes we need not copy to the stack
4736 because registers will take care of them. */
4738 if (partial != 0)
4739 xinner = adjust_address (xinner, BLKmode, used);
4741 /* If the partial register-part of the arg counts in its stack size,
4742 skip the part of stack space corresponding to the registers.
4743 Otherwise, start copying to the beginning of the stack space,
4744 by setting SKIP to 0. */
4745 skip = (reg_parm_stack_space == 0) ? 0 : used;
4747 #ifdef PUSH_ROUNDING
4748 /* NB: Let the backend known the number of bytes to push and
4749 decide if push insns should be generated. */
4750 unsigned int push_size;
4751 if (CONST_INT_P (size))
4752 push_size = INTVAL (size);
4753 else
4754 push_size = 0;
4756 /* Do it with several push insns if that doesn't take lots of insns
4757 and if there is no difficulty with push insns that skip bytes
4758 on the stack for alignment purposes. */
4759 if (args_addr == 0
4760 && targetm.calls.push_argument (push_size)
4761 && CONST_INT_P (size)
4762 && skip == 0
4763 && MEM_ALIGN (xinner) >= align
4764 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4765 /* Here we avoid the case of a structure whose weak alignment
4766 forces many pushes of a small amount of data,
4767 and such small pushes do rounding that causes trouble. */
4768 && ((!targetm.slow_unaligned_access (word_mode, align))
4769 || align >= BIGGEST_ALIGNMENT
4770 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4771 align / BITS_PER_UNIT))
4772 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4774 /* Push padding now if padding above and stack grows down,
4775 or if padding below and stack grows up.
4776 But if space already allocated, this has already been done. */
4777 if (maybe_ne (extra, 0)
4778 && args_addr == 0
4779 && where_pad != PAD_NONE
4780 && where_pad != stack_direction)
4781 anti_adjust_stack (gen_int_mode (extra, Pmode));
4783 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4784 RETURN_BEGIN);
4786 else
4787 #endif /* PUSH_ROUNDING */
4789 rtx target;
4791 /* Otherwise make space on the stack and copy the data
4792 to the address of that space. */
4794 /* Deduct words put into registers from the size we must copy. */
4795 if (partial != 0)
4797 if (CONST_INT_P (size))
4798 size = GEN_INT (INTVAL (size) - used);
4799 else
4800 size = expand_binop (GET_MODE (size), sub_optab, size,
4801 gen_int_mode (used, GET_MODE (size)),
4802 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4805 /* Get the address of the stack space.
4806 In this case, we do not deal with EXTRA separately.
4807 A single stack adjust will do. */
4808 poly_int64 const_args_so_far;
4809 if (! args_addr)
4811 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4812 extra = 0;
4814 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4815 temp = memory_address (BLKmode,
4816 plus_constant (Pmode, args_addr,
4817 skip + const_args_so_far));
4818 else
4819 temp = memory_address (BLKmode,
4820 plus_constant (Pmode,
4821 gen_rtx_PLUS (Pmode,
4822 args_addr,
4823 args_so_far),
4824 skip));
4826 if (!ACCUMULATE_OUTGOING_ARGS)
4828 /* If the source is referenced relative to the stack pointer,
4829 copy it to another register to stabilize it. We do not need
4830 to do this if we know that we won't be changing sp. */
4832 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4833 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4834 temp = copy_to_reg (temp);
4837 target = gen_rtx_MEM (BLKmode, temp);
4839 /* We do *not* set_mem_attributes here, because incoming arguments
4840 may overlap with sibling call outgoing arguments and we cannot
4841 allow reordering of reads from function arguments with stores
4842 to outgoing arguments of sibling calls. We do, however, want
4843 to record the alignment of the stack slot. */
4844 /* ALIGN may well be better aligned than TYPE, e.g. due to
4845 PARM_BOUNDARY. Assume the caller isn't lying. */
4846 set_mem_align (target, align);
4848 /* If part should go in registers and pushing to that part would
4849 overwrite some of the values that need to go into regs, load the
4850 overlapping values into temporary pseudos to be moved into the hard
4851 regs at the end after the stack pushing has completed.
4852 We cannot load them directly into the hard regs here because
4853 they can be clobbered by the block move expansions.
4854 See PR 65358. */
4856 if (partial > 0 && reg != 0 && mode == BLKmode
4857 && GET_CODE (reg) != PARALLEL)
4859 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4860 if (overlapping > 0)
4862 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4863 overlapping /= UNITS_PER_WORD;
4865 tmp_regs = XALLOCAVEC (rtx, overlapping);
4867 for (int i = 0; i < overlapping; i++)
4868 tmp_regs[i] = gen_reg_rtx (word_mode);
4870 for (int i = 0; i < overlapping; i++)
4871 emit_move_insn (tmp_regs[i],
4872 operand_subword_force (target, i, mode));
4874 else if (overlapping == -1)
4875 overlapping = 0;
4876 /* Could not determine whether there is overlap.
4877 Fail the sibcall. */
4878 else
4880 overlapping = 0;
4881 if (sibcall_p)
4882 return false;
4885 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4888 else if (partial > 0)
4890 /* Scalar partly in registers. This case is only supported
4891 for fixed-wdth modes. */
4892 int num_words = GET_MODE_SIZE (mode).to_constant ();
4893 num_words /= UNITS_PER_WORD;
4894 int i;
4895 int not_stack;
4896 /* # bytes of start of argument
4897 that we must make space for but need not store. */
4898 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4899 int args_offset = INTVAL (args_so_far);
4900 int skip;
4902 /* Push padding now if padding above and stack grows down,
4903 or if padding below and stack grows up.
4904 But if space already allocated, this has already been done. */
4905 if (maybe_ne (extra, 0)
4906 && args_addr == 0
4907 && where_pad != PAD_NONE
4908 && where_pad != stack_direction)
4909 anti_adjust_stack (gen_int_mode (extra, Pmode));
4911 /* If we make space by pushing it, we might as well push
4912 the real data. Otherwise, we can leave OFFSET nonzero
4913 and leave the space uninitialized. */
4914 if (args_addr == 0)
4915 offset = 0;
4917 /* Now NOT_STACK gets the number of words that we don't need to
4918 allocate on the stack. Convert OFFSET to words too. */
4919 not_stack = (partial - offset) / UNITS_PER_WORD;
4920 offset /= UNITS_PER_WORD;
4922 /* If the partial register-part of the arg counts in its stack size,
4923 skip the part of stack space corresponding to the registers.
4924 Otherwise, start copying to the beginning of the stack space,
4925 by setting SKIP to 0. */
4926 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4928 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4929 x = validize_mem (force_const_mem (mode, x));
4931 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4932 SUBREGs of such registers are not allowed. */
4933 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4934 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4935 x = copy_to_reg (x);
4937 /* Loop over all the words allocated on the stack for this arg. */
4938 /* We can do it by words, because any scalar bigger than a word
4939 has a size a multiple of a word. */
4940 for (i = num_words - 1; i >= not_stack; i--)
4941 if (i >= not_stack + offset)
4942 if (!emit_push_insn (operand_subword_force (x, i, mode),
4943 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4944 0, args_addr,
4945 GEN_INT (args_offset + ((i - not_stack + skip)
4946 * UNITS_PER_WORD)),
4947 reg_parm_stack_space, alignment_pad, sibcall_p))
4948 return false;
4950 else
4952 rtx addr;
4953 rtx dest;
4955 /* Push padding now if padding above and stack grows down,
4956 or if padding below and stack grows up.
4957 But if space already allocated, this has already been done. */
4958 if (maybe_ne (extra, 0)
4959 && args_addr == 0
4960 && where_pad != PAD_NONE
4961 && where_pad != stack_direction)
4962 anti_adjust_stack (gen_int_mode (extra, Pmode));
4964 #ifdef PUSH_ROUNDING
4965 if (args_addr == 0 && targetm.calls.push_argument (0))
4966 emit_single_push_insn (mode, x, type);
4967 else
4968 #endif
4970 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4971 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4973 /* We do *not* set_mem_attributes here, because incoming arguments
4974 may overlap with sibling call outgoing arguments and we cannot
4975 allow reordering of reads from function arguments with stores
4976 to outgoing arguments of sibling calls. We do, however, want
4977 to record the alignment of the stack slot. */
4978 /* ALIGN may well be better aligned than TYPE, e.g. due to
4979 PARM_BOUNDARY. Assume the caller isn't lying. */
4980 set_mem_align (dest, align);
4982 emit_move_insn (dest, x);
4986 /* Move the partial arguments into the registers and any overlapping
4987 values that we moved into the pseudos in tmp_regs. */
4988 if (partial > 0 && reg != 0)
4990 /* Handle calls that pass values in multiple non-contiguous locations.
4991 The Irix 6 ABI has examples of this. */
4992 if (GET_CODE (reg) == PARALLEL)
4993 emit_group_load (reg, x, type, -1);
4994 else
4996 gcc_assert (partial % UNITS_PER_WORD == 0);
4997 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4999 for (int i = 0; i < overlapping; i++)
5000 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5001 + nregs - overlapping + i),
5002 tmp_regs[i]);
5007 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5008 anti_adjust_stack (gen_int_mode (extra, Pmode));
5010 if (alignment_pad && args_addr == 0)
5011 anti_adjust_stack (alignment_pad);
5013 return true;
5016 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5017 operations. */
5019 static rtx
5020 get_subtarget (rtx x)
5022 return (optimize
5023 || x == 0
5024 /* Only registers can be subtargets. */
5025 || !REG_P (x)
5026 /* Don't use hard regs to avoid extending their life. */
5027 || REGNO (x) < FIRST_PSEUDO_REGISTER
5028 ? 0 : x);
5031 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5032 FIELD is a bitfield. Returns true if the optimization was successful,
5033 and there's nothing else to do. */
5035 static bool
5036 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5037 poly_uint64 pbitpos,
5038 poly_uint64 pbitregion_start,
5039 poly_uint64 pbitregion_end,
5040 machine_mode mode1, rtx str_rtx,
5041 tree to, tree src, bool reverse)
5043 /* str_mode is not guaranteed to be a scalar type. */
5044 machine_mode str_mode = GET_MODE (str_rtx);
5045 unsigned int str_bitsize;
5046 tree op0, op1;
5047 rtx value, result;
5048 optab binop;
5049 gimple *srcstmt;
5050 enum tree_code code;
5052 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5053 if (mode1 != VOIDmode
5054 || !pbitsize.is_constant (&bitsize)
5055 || !pbitpos.is_constant (&bitpos)
5056 || !pbitregion_start.is_constant (&bitregion_start)
5057 || !pbitregion_end.is_constant (&bitregion_end)
5058 || bitsize >= BITS_PER_WORD
5059 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5060 || str_bitsize > BITS_PER_WORD
5061 || TREE_SIDE_EFFECTS (to)
5062 || TREE_THIS_VOLATILE (to))
5063 return false;
5065 STRIP_NOPS (src);
5066 if (TREE_CODE (src) != SSA_NAME)
5067 return false;
5068 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5069 return false;
5071 srcstmt = get_gimple_for_ssa_name (src);
5072 if (!srcstmt
5073 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5074 return false;
5076 code = gimple_assign_rhs_code (srcstmt);
5078 op0 = gimple_assign_rhs1 (srcstmt);
5080 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5081 to find its initialization. Hopefully the initialization will
5082 be from a bitfield load. */
5083 if (TREE_CODE (op0) == SSA_NAME)
5085 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5087 /* We want to eventually have OP0 be the same as TO, which
5088 should be a bitfield. */
5089 if (!op0stmt
5090 || !is_gimple_assign (op0stmt)
5091 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5092 return false;
5093 op0 = gimple_assign_rhs1 (op0stmt);
5096 op1 = gimple_assign_rhs2 (srcstmt);
5098 if (!operand_equal_p (to, op0, 0))
5099 return false;
5101 if (MEM_P (str_rtx))
5103 unsigned HOST_WIDE_INT offset1;
5105 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5106 str_bitsize = BITS_PER_WORD;
5108 scalar_int_mode best_mode;
5109 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5110 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5111 return false;
5112 str_mode = best_mode;
5113 str_bitsize = GET_MODE_BITSIZE (best_mode);
5115 offset1 = bitpos;
5116 bitpos %= str_bitsize;
5117 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5118 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5120 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5121 return false;
5123 /* If the bit field covers the whole REG/MEM, store_field
5124 will likely generate better code. */
5125 if (bitsize >= str_bitsize)
5126 return false;
5128 /* We can't handle fields split across multiple entities. */
5129 if (bitpos + bitsize > str_bitsize)
5130 return false;
5132 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5133 bitpos = str_bitsize - bitpos - bitsize;
5135 switch (code)
5137 case PLUS_EXPR:
5138 case MINUS_EXPR:
5139 /* For now, just optimize the case of the topmost bitfield
5140 where we don't need to do any masking and also
5141 1 bit bitfields where xor can be used.
5142 We might win by one instruction for the other bitfields
5143 too if insv/extv instructions aren't used, so that
5144 can be added later. */
5145 if ((reverse || bitpos + bitsize != str_bitsize)
5146 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5147 break;
5149 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5150 value = convert_modes (str_mode,
5151 TYPE_MODE (TREE_TYPE (op1)), value,
5152 TYPE_UNSIGNED (TREE_TYPE (op1)));
5154 /* We may be accessing data outside the field, which means
5155 we can alias adjacent data. */
5156 if (MEM_P (str_rtx))
5158 str_rtx = shallow_copy_rtx (str_rtx);
5159 set_mem_alias_set (str_rtx, 0);
5160 set_mem_expr (str_rtx, 0);
5163 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5165 value = expand_and (str_mode, value, const1_rtx, NULL);
5166 binop = xor_optab;
5168 else
5169 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5171 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5172 if (reverse)
5173 value = flip_storage_order (str_mode, value);
5174 result = expand_binop (str_mode, binop, str_rtx,
5175 value, str_rtx, 1, OPTAB_WIDEN);
5176 if (result != str_rtx)
5177 emit_move_insn (str_rtx, result);
5178 return true;
5180 case BIT_IOR_EXPR:
5181 case BIT_XOR_EXPR:
5182 if (TREE_CODE (op1) != INTEGER_CST)
5183 break;
5184 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5185 value = convert_modes (str_mode,
5186 TYPE_MODE (TREE_TYPE (op1)), value,
5187 TYPE_UNSIGNED (TREE_TYPE (op1)));
5189 /* We may be accessing data outside the field, which means
5190 we can alias adjacent data. */
5191 if (MEM_P (str_rtx))
5193 str_rtx = shallow_copy_rtx (str_rtx);
5194 set_mem_alias_set (str_rtx, 0);
5195 set_mem_expr (str_rtx, 0);
5198 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5199 if (bitpos + bitsize != str_bitsize)
5201 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5202 str_mode);
5203 value = expand_and (str_mode, value, mask, NULL_RTX);
5205 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5206 if (reverse)
5207 value = flip_storage_order (str_mode, value);
5208 result = expand_binop (str_mode, binop, str_rtx,
5209 value, str_rtx, 1, OPTAB_WIDEN);
5210 if (result != str_rtx)
5211 emit_move_insn (str_rtx, result);
5212 return true;
5214 default:
5215 break;
5218 return false;
5221 /* In the C++ memory model, consecutive bit fields in a structure are
5222 considered one memory location.
5224 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5225 returns the bit range of consecutive bits in which this COMPONENT_REF
5226 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5227 and *OFFSET may be adjusted in the process.
5229 If the access does not need to be restricted, 0 is returned in both
5230 *BITSTART and *BITEND. */
5232 void
5233 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5234 poly_int64_pod *bitpos, tree *offset)
5236 poly_int64 bitoffset;
5237 tree field, repr;
5239 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5241 field = TREE_OPERAND (exp, 1);
5242 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5243 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5244 need to limit the range we can access. */
5245 if (!repr)
5247 *bitstart = *bitend = 0;
5248 return;
5251 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5252 part of a larger bit field, then the representative does not serve any
5253 useful purpose. This can occur in Ada. */
5254 if (handled_component_p (TREE_OPERAND (exp, 0)))
5256 machine_mode rmode;
5257 poly_int64 rbitsize, rbitpos;
5258 tree roffset;
5259 int unsignedp, reversep, volatilep = 0;
5260 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5261 &roffset, &rmode, &unsignedp, &reversep,
5262 &volatilep);
5263 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5265 *bitstart = *bitend = 0;
5266 return;
5270 /* Compute the adjustment to bitpos from the offset of the field
5271 relative to the representative. DECL_FIELD_OFFSET of field and
5272 repr are the same by construction if they are not constants,
5273 see finish_bitfield_layout. */
5274 poly_uint64 field_offset, repr_offset;
5275 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5276 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5277 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5278 else
5279 bitoffset = 0;
5280 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5281 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5283 /* If the adjustment is larger than bitpos, we would have a negative bit
5284 position for the lower bound and this may wreak havoc later. Adjust
5285 offset and bitpos to make the lower bound non-negative in that case. */
5286 if (maybe_gt (bitoffset, *bitpos))
5288 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5289 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5291 *bitpos += adjust_bits;
5292 if (*offset == NULL_TREE)
5293 *offset = size_int (-adjust_bytes);
5294 else
5295 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5296 *bitstart = 0;
5298 else
5299 *bitstart = *bitpos - bitoffset;
5301 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5304 /* Returns true if BASE is a DECL that does not reside in memory and
5305 has non-BLKmode. DECL_RTL must not be a MEM; if
5306 DECL_RTL was not set yet, return false. */
5308 static inline bool
5309 non_mem_decl_p (tree base)
5311 if (!DECL_P (base)
5312 || TREE_ADDRESSABLE (base)
5313 || DECL_MODE (base) == BLKmode)
5314 return false;
5316 if (!DECL_RTL_SET_P (base))
5317 return false;
5319 return (!MEM_P (DECL_RTL (base)));
5322 /* Returns true if REF refers to an object that does not
5323 reside in memory and has non-BLKmode. */
5325 static inline bool
5326 mem_ref_refers_to_non_mem_p (tree ref)
5328 tree base;
5330 if (TREE_CODE (ref) == MEM_REF
5331 || TREE_CODE (ref) == TARGET_MEM_REF)
5333 tree addr = TREE_OPERAND (ref, 0);
5335 if (TREE_CODE (addr) != ADDR_EXPR)
5336 return false;
5338 base = TREE_OPERAND (addr, 0);
5340 else
5341 base = ref;
5343 return non_mem_decl_p (base);
5346 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5347 is true, try generating a nontemporal store. */
5349 void
5350 expand_assignment (tree to, tree from, bool nontemporal)
5352 rtx to_rtx = 0;
5353 rtx result;
5354 machine_mode mode;
5355 unsigned int align;
5356 enum insn_code icode;
5358 /* Don't crash if the lhs of the assignment was erroneous. */
5359 if (TREE_CODE (to) == ERROR_MARK)
5361 expand_normal (from);
5362 return;
5365 /* Optimize away no-op moves without side-effects. */
5366 if (operand_equal_p (to, from, 0))
5367 return;
5369 /* Handle misaligned stores. */
5370 mode = TYPE_MODE (TREE_TYPE (to));
5371 if ((TREE_CODE (to) == MEM_REF
5372 || TREE_CODE (to) == TARGET_MEM_REF
5373 || DECL_P (to))
5374 && mode != BLKmode
5375 && !mem_ref_refers_to_non_mem_p (to)
5376 && ((align = get_object_alignment (to))
5377 < GET_MODE_ALIGNMENT (mode))
5378 && (((icode = optab_handler (movmisalign_optab, mode))
5379 != CODE_FOR_nothing)
5380 || targetm.slow_unaligned_access (mode, align)))
5382 rtx reg, mem;
5384 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5385 /* Handle PARALLEL. */
5386 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5387 reg = force_not_mem (reg);
5388 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5389 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5390 reg = flip_storage_order (mode, reg);
5392 if (icode != CODE_FOR_nothing)
5394 class expand_operand ops[2];
5396 create_fixed_operand (&ops[0], mem);
5397 create_input_operand (&ops[1], reg, mode);
5398 /* The movmisalign<mode> pattern cannot fail, else the assignment
5399 would silently be omitted. */
5400 expand_insn (icode, 2, ops);
5402 else
5403 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5404 false);
5405 return;
5408 /* Assignment of a structure component needs special treatment
5409 if the structure component's rtx is not simply a MEM.
5410 Assignment of an array element at a constant index, and assignment of
5411 an array element in an unaligned packed structure field, has the same
5412 problem. Same for (partially) storing into a non-memory object. */
5413 if (handled_component_p (to)
5414 || (TREE_CODE (to) == MEM_REF
5415 && (REF_REVERSE_STORAGE_ORDER (to)
5416 || mem_ref_refers_to_non_mem_p (to)))
5417 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5419 machine_mode mode1;
5420 poly_int64 bitsize, bitpos;
5421 poly_uint64 bitregion_start = 0;
5422 poly_uint64 bitregion_end = 0;
5423 tree offset;
5424 int unsignedp, reversep, volatilep = 0;
5425 tree tem;
5427 push_temp_slots ();
5428 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5429 &unsignedp, &reversep, &volatilep);
5431 /* Make sure bitpos is not negative, it can wreak havoc later. */
5432 if (maybe_lt (bitpos, 0))
5434 gcc_assert (offset == NULL_TREE);
5435 offset = size_int (bits_to_bytes_round_down (bitpos));
5436 bitpos = num_trailing_bits (bitpos);
5439 if (TREE_CODE (to) == COMPONENT_REF
5440 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5441 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5442 /* The C++ memory model naturally applies to byte-aligned fields.
5443 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5444 BITSIZE are not byte-aligned, there is no need to limit the range
5445 we can access. This can occur with packed structures in Ada. */
5446 else if (maybe_gt (bitsize, 0)
5447 && multiple_p (bitsize, BITS_PER_UNIT)
5448 && multiple_p (bitpos, BITS_PER_UNIT))
5450 bitregion_start = bitpos;
5451 bitregion_end = bitpos + bitsize - 1;
5454 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5456 /* If the field has a mode, we want to access it in the
5457 field's mode, not the computed mode.
5458 If a MEM has VOIDmode (external with incomplete type),
5459 use BLKmode for it instead. */
5460 if (MEM_P (to_rtx))
5462 if (mode1 != VOIDmode)
5463 to_rtx = adjust_address (to_rtx, mode1, 0);
5464 else if (GET_MODE (to_rtx) == VOIDmode)
5465 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5468 if (offset != 0)
5470 machine_mode address_mode;
5471 rtx offset_rtx;
5473 if (!MEM_P (to_rtx))
5475 /* We can get constant negative offsets into arrays with broken
5476 user code. Translate this to a trap instead of ICEing. */
5477 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5478 expand_builtin_trap ();
5479 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5482 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5483 address_mode = get_address_mode (to_rtx);
5484 if (GET_MODE (offset_rtx) != address_mode)
5486 /* We cannot be sure that the RTL in offset_rtx is valid outside
5487 of a memory address context, so force it into a register
5488 before attempting to convert it to the desired mode. */
5489 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5490 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5493 /* If we have an expression in OFFSET_RTX and a non-zero
5494 byte offset in BITPOS, adding the byte offset before the
5495 OFFSET_RTX results in better intermediate code, which makes
5496 later rtl optimization passes perform better.
5498 We prefer intermediate code like this:
5500 r124:DI=r123:DI+0x18
5501 [r124:DI]=r121:DI
5503 ... instead of ...
5505 r124:DI=r123:DI+0x10
5506 [r124:DI+0x8]=r121:DI
5508 This is only done for aligned data values, as these can
5509 be expected to result in single move instructions. */
5510 poly_int64 bytepos;
5511 if (mode1 != VOIDmode
5512 && maybe_ne (bitpos, 0)
5513 && maybe_gt (bitsize, 0)
5514 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5515 && multiple_p (bitpos, bitsize)
5516 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5517 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5519 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5520 bitregion_start = 0;
5521 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5522 bitregion_end -= bitpos;
5523 bitpos = 0;
5526 to_rtx = offset_address (to_rtx, offset_rtx,
5527 highest_pow2_factor_for_target (to,
5528 offset));
5531 /* No action is needed if the target is not a memory and the field
5532 lies completely outside that target. This can occur if the source
5533 code contains an out-of-bounds access to a small array. */
5534 if (!MEM_P (to_rtx)
5535 && GET_MODE (to_rtx) != BLKmode
5536 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5538 expand_normal (from);
5539 result = NULL;
5541 /* Handle expand_expr of a complex value returning a CONCAT. */
5542 else if (GET_CODE (to_rtx) == CONCAT)
5544 machine_mode to_mode = GET_MODE (to_rtx);
5545 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5546 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5547 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5548 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5549 && known_eq (bitpos, 0)
5550 && known_eq (bitsize, mode_bitsize))
5551 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5552 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5553 && known_eq (bitsize, inner_bitsize)
5554 && (known_eq (bitpos, 0)
5555 || known_eq (bitpos, inner_bitsize)))
5556 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5557 false, nontemporal, reversep);
5558 else if (known_le (bitpos + bitsize, inner_bitsize))
5559 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5560 bitregion_start, bitregion_end,
5561 mode1, from, get_alias_set (to),
5562 nontemporal, reversep);
5563 else if (known_ge (bitpos, inner_bitsize))
5564 result = store_field (XEXP (to_rtx, 1), bitsize,
5565 bitpos - inner_bitsize,
5566 bitregion_start, bitregion_end,
5567 mode1, from, get_alias_set (to),
5568 nontemporal, reversep);
5569 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5571 result = expand_normal (from);
5572 if (GET_CODE (result) == CONCAT)
5574 to_mode = GET_MODE_INNER (to_mode);
5575 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5576 rtx from_real
5577 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5578 from_mode, 0);
5579 rtx from_imag
5580 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5581 from_mode, 0);
5582 if (!from_real || !from_imag)
5583 goto concat_store_slow;
5584 emit_move_insn (XEXP (to_rtx, 0), from_real);
5585 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5587 else
5589 machine_mode from_mode
5590 = GET_MODE (result) == VOIDmode
5591 ? TYPE_MODE (TREE_TYPE (from))
5592 : GET_MODE (result);
5593 rtx from_rtx;
5594 if (MEM_P (result))
5595 from_rtx = change_address (result, to_mode, NULL_RTX);
5596 else
5597 from_rtx
5598 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5599 if (from_rtx)
5601 emit_move_insn (XEXP (to_rtx, 0),
5602 read_complex_part (from_rtx, false));
5603 emit_move_insn (XEXP (to_rtx, 1),
5604 read_complex_part (from_rtx, true));
5606 else
5608 to_mode = GET_MODE_INNER (to_mode);
5609 rtx from_real
5610 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5611 rtx from_imag
5612 = simplify_gen_subreg (to_mode, result, from_mode,
5613 GET_MODE_SIZE (to_mode));
5614 if (!from_real || !from_imag)
5615 goto concat_store_slow;
5616 emit_move_insn (XEXP (to_rtx, 0), from_real);
5617 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5621 else
5623 concat_store_slow:;
5624 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5625 GET_MODE_SIZE (GET_MODE (to_rtx)));
5626 write_complex_part (temp, XEXP (to_rtx, 0), false);
5627 write_complex_part (temp, XEXP (to_rtx, 1), true);
5628 result = store_field (temp, bitsize, bitpos,
5629 bitregion_start, bitregion_end,
5630 mode1, from, get_alias_set (to),
5631 nontemporal, reversep);
5632 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5633 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5636 /* For calls to functions returning variable length structures, if TO_RTX
5637 is not a MEM, go through a MEM because we must not create temporaries
5638 of the VLA type. */
5639 else if (!MEM_P (to_rtx)
5640 && TREE_CODE (from) == CALL_EXPR
5641 && COMPLETE_TYPE_P (TREE_TYPE (from))
5642 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5644 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5645 GET_MODE_SIZE (GET_MODE (to_rtx)));
5646 result = store_field (temp, bitsize, bitpos, bitregion_start,
5647 bitregion_end, mode1, from, get_alias_set (to),
5648 nontemporal, reversep);
5649 emit_move_insn (to_rtx, temp);
5651 else
5653 if (MEM_P (to_rtx))
5655 /* If the field is at offset zero, we could have been given the
5656 DECL_RTX of the parent struct. Don't munge it. */
5657 to_rtx = shallow_copy_rtx (to_rtx);
5658 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5659 if (volatilep)
5660 MEM_VOLATILE_P (to_rtx) = 1;
5663 gcc_checking_assert (known_ge (bitpos, 0));
5664 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5665 bitregion_start, bitregion_end,
5666 mode1, to_rtx, to, from,
5667 reversep))
5668 result = NULL;
5669 else if (SUBREG_P (to_rtx)
5670 && SUBREG_PROMOTED_VAR_P (to_rtx))
5672 /* If to_rtx is a promoted subreg, we need to zero or sign
5673 extend the value afterwards. */
5674 if (TREE_CODE (to) == MEM_REF
5675 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5676 && !REF_REVERSE_STORAGE_ORDER (to)
5677 && known_eq (bitpos, 0)
5678 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5679 result = store_expr (from, to_rtx, 0, nontemporal, false);
5680 else
5682 rtx to_rtx1
5683 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5684 SUBREG_REG (to_rtx),
5685 subreg_promoted_mode (to_rtx));
5686 result = store_field (to_rtx1, bitsize, bitpos,
5687 bitregion_start, bitregion_end,
5688 mode1, from, get_alias_set (to),
5689 nontemporal, reversep);
5690 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5691 SUBREG_PROMOTED_SIGN (to_rtx));
5694 else
5695 result = store_field (to_rtx, bitsize, bitpos,
5696 bitregion_start, bitregion_end,
5697 mode1, from, get_alias_set (to),
5698 nontemporal, reversep);
5701 if (result)
5702 preserve_temp_slots (result);
5703 pop_temp_slots ();
5704 return;
5707 /* If the rhs is a function call and its value is not an aggregate,
5708 call the function before we start to compute the lhs.
5709 This is needed for correct code for cases such as
5710 val = setjmp (buf) on machines where reference to val
5711 requires loading up part of an address in a separate insn.
5713 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5714 since it might be a promoted variable where the zero- or sign- extension
5715 needs to be done. Handling this in the normal way is safe because no
5716 computation is done before the call. The same is true for SSA names. */
5717 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5718 && COMPLETE_TYPE_P (TREE_TYPE (from))
5719 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5720 && ! (((VAR_P (to)
5721 || TREE_CODE (to) == PARM_DECL
5722 || TREE_CODE (to) == RESULT_DECL)
5723 && REG_P (DECL_RTL (to)))
5724 || TREE_CODE (to) == SSA_NAME))
5726 rtx value;
5728 push_temp_slots ();
5729 value = expand_normal (from);
5731 if (to_rtx == 0)
5732 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5734 /* Handle calls that return values in multiple non-contiguous locations.
5735 The Irix 6 ABI has examples of this. */
5736 if (GET_CODE (to_rtx) == PARALLEL)
5738 if (GET_CODE (value) == PARALLEL)
5739 emit_group_move (to_rtx, value);
5740 else
5741 emit_group_load (to_rtx, value, TREE_TYPE (from),
5742 int_size_in_bytes (TREE_TYPE (from)));
5744 else if (GET_CODE (value) == PARALLEL)
5745 emit_group_store (to_rtx, value, TREE_TYPE (from),
5746 int_size_in_bytes (TREE_TYPE (from)));
5747 else if (GET_MODE (to_rtx) == BLKmode)
5749 /* Handle calls that return BLKmode values in registers. */
5750 if (REG_P (value))
5751 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5752 else
5753 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5755 else
5757 if (POINTER_TYPE_P (TREE_TYPE (to)))
5758 value = convert_memory_address_addr_space
5759 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5760 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5762 emit_move_insn (to_rtx, value);
5765 preserve_temp_slots (to_rtx);
5766 pop_temp_slots ();
5767 return;
5770 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5771 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5773 /* Don't move directly into a return register. */
5774 if (TREE_CODE (to) == RESULT_DECL
5775 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5777 rtx temp;
5779 push_temp_slots ();
5781 /* If the source is itself a return value, it still is in a pseudo at
5782 this point so we can move it back to the return register directly. */
5783 if (REG_P (to_rtx)
5784 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5785 && TREE_CODE (from) != CALL_EXPR)
5786 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5787 else
5788 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5790 /* Handle calls that return values in multiple non-contiguous locations.
5791 The Irix 6 ABI has examples of this. */
5792 if (GET_CODE (to_rtx) == PARALLEL)
5794 if (GET_CODE (temp) == PARALLEL)
5795 emit_group_move (to_rtx, temp);
5796 else
5797 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5798 int_size_in_bytes (TREE_TYPE (from)));
5800 else if (temp)
5801 emit_move_insn (to_rtx, temp);
5803 preserve_temp_slots (to_rtx);
5804 pop_temp_slots ();
5805 return;
5808 /* In case we are returning the contents of an object which overlaps
5809 the place the value is being stored, use a safe function when copying
5810 a value through a pointer into a structure value return block. */
5811 if (TREE_CODE (to) == RESULT_DECL
5812 && TREE_CODE (from) == INDIRECT_REF
5813 && ADDR_SPACE_GENERIC_P
5814 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5815 && refs_may_alias_p (to, from)
5816 && cfun->returns_struct
5817 && !cfun->returns_pcc_struct)
5819 rtx from_rtx, size;
5821 push_temp_slots ();
5822 size = expr_size (from);
5823 from_rtx = expand_normal (from);
5825 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5827 preserve_temp_slots (to_rtx);
5828 pop_temp_slots ();
5829 return;
5832 /* Compute FROM and store the value in the rtx we got. */
5834 push_temp_slots ();
5835 result = store_expr (from, to_rtx, 0, nontemporal, false);
5836 preserve_temp_slots (result);
5837 pop_temp_slots ();
5838 return;
5841 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5842 succeeded, false otherwise. */
5844 bool
5845 emit_storent_insn (rtx to, rtx from)
5847 class expand_operand ops[2];
5848 machine_mode mode = GET_MODE (to);
5849 enum insn_code code = optab_handler (storent_optab, mode);
5851 if (code == CODE_FOR_nothing)
5852 return false;
5854 create_fixed_operand (&ops[0], to);
5855 create_input_operand (&ops[1], from, mode);
5856 return maybe_expand_insn (code, 2, ops);
5859 /* Helper function for store_expr storing of STRING_CST. */
5861 static rtx
5862 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5863 fixed_size_mode mode)
5865 tree str = (tree) data;
5867 gcc_assert (offset >= 0);
5868 if (offset >= TREE_STRING_LENGTH (str))
5869 return const0_rtx;
5871 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5872 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5874 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5875 size_t l = TREE_STRING_LENGTH (str) - offset;
5876 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5877 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5878 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
5881 /* The by-pieces infrastructure does not try to pick a vector mode
5882 for storing STRING_CST. */
5883 return c_readstr (TREE_STRING_POINTER (str) + offset,
5884 as_a <scalar_int_mode> (mode), false);
5887 /* Generate code for computing expression EXP,
5888 and storing the value into TARGET.
5890 If the mode is BLKmode then we may return TARGET itself.
5891 It turns out that in BLKmode it doesn't cause a problem.
5892 because C has no operators that could combine two different
5893 assignments into the same BLKmode object with different values
5894 with no sequence point. Will other languages need this to
5895 be more thorough?
5897 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5898 stack, and block moves may need to be treated specially.
5900 If NONTEMPORAL is true, try using a nontemporal store instruction.
5902 If REVERSE is true, the store is to be done in reverse order. */
5905 store_expr (tree exp, rtx target, int call_param_p,
5906 bool nontemporal, bool reverse)
5908 rtx temp;
5909 rtx alt_rtl = NULL_RTX;
5910 location_t loc = curr_insn_location ();
5911 bool shortened_string_cst = false;
5913 if (VOID_TYPE_P (TREE_TYPE (exp)))
5915 /* C++ can generate ?: expressions with a throw expression in one
5916 branch and an rvalue in the other. Here, we resolve attempts to
5917 store the throw expression's nonexistent result. */
5918 gcc_assert (!call_param_p);
5919 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5920 return NULL_RTX;
5922 if (TREE_CODE (exp) == COMPOUND_EXPR)
5924 /* Perform first part of compound expression, then assign from second
5925 part. */
5926 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5927 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5928 return store_expr (TREE_OPERAND (exp, 1), target,
5929 call_param_p, nontemporal, reverse);
5931 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5933 /* For conditional expression, get safe form of the target. Then
5934 test the condition, doing the appropriate assignment on either
5935 side. This avoids the creation of unnecessary temporaries.
5936 For non-BLKmode, it is more efficient not to do this. */
5938 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5940 do_pending_stack_adjust ();
5941 NO_DEFER_POP;
5942 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5943 profile_probability::uninitialized ());
5944 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5945 nontemporal, reverse);
5946 emit_jump_insn (targetm.gen_jump (lab2));
5947 emit_barrier ();
5948 emit_label (lab1);
5949 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5950 nontemporal, reverse);
5951 emit_label (lab2);
5952 OK_DEFER_POP;
5954 return NULL_RTX;
5956 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5957 /* If this is a scalar in a register that is stored in a wider mode
5958 than the declared mode, compute the result into its declared mode
5959 and then convert to the wider mode. Our value is the computed
5960 expression. */
5962 rtx inner_target = 0;
5963 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5964 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5966 /* We can do the conversion inside EXP, which will often result
5967 in some optimizations. Do the conversion in two steps: first
5968 change the signedness, if needed, then the extend. But don't
5969 do this if the type of EXP is a subtype of something else
5970 since then the conversion might involve more than just
5971 converting modes. */
5972 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5973 && TREE_TYPE (TREE_TYPE (exp)) == 0
5974 && GET_MODE_PRECISION (outer_mode)
5975 == TYPE_PRECISION (TREE_TYPE (exp)))
5977 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5978 TYPE_UNSIGNED (TREE_TYPE (exp))))
5980 /* Some types, e.g. Fortran's logical*4, won't have a signed
5981 version, so use the mode instead. */
5982 tree ntype
5983 = (signed_or_unsigned_type_for
5984 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5985 if (ntype == NULL)
5986 ntype = lang_hooks.types.type_for_mode
5987 (TYPE_MODE (TREE_TYPE (exp)),
5988 SUBREG_PROMOTED_SIGN (target));
5990 exp = fold_convert_loc (loc, ntype, exp);
5993 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5994 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5995 exp);
5997 inner_target = SUBREG_REG (target);
6000 temp = expand_expr (exp, inner_target, VOIDmode,
6001 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6004 /* If TEMP is a VOIDmode constant, use convert_modes to make
6005 sure that we properly convert it. */
6006 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6008 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6009 temp, SUBREG_PROMOTED_SIGN (target));
6010 temp = convert_modes (inner_mode, outer_mode, temp,
6011 SUBREG_PROMOTED_SIGN (target));
6014 convert_move (SUBREG_REG (target), temp,
6015 SUBREG_PROMOTED_SIGN (target));
6017 return NULL_RTX;
6019 else if ((TREE_CODE (exp) == STRING_CST
6020 || (TREE_CODE (exp) == MEM_REF
6021 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6022 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6023 == STRING_CST
6024 && integer_zerop (TREE_OPERAND (exp, 1))))
6025 && !nontemporal && !call_param_p
6026 && MEM_P (target))
6028 /* Optimize initialization of an array with a STRING_CST. */
6029 HOST_WIDE_INT exp_len, str_copy_len;
6030 rtx dest_mem;
6031 tree str = TREE_CODE (exp) == STRING_CST
6032 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6034 exp_len = int_expr_size (exp);
6035 if (exp_len <= 0)
6036 goto normal_expr;
6038 if (TREE_STRING_LENGTH (str) <= 0)
6039 goto normal_expr;
6041 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6042 MEM_ALIGN (target), false))
6044 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6045 MEM_ALIGN (target), false, RETURN_BEGIN);
6046 return NULL_RTX;
6049 str_copy_len = TREE_STRING_LENGTH (str);
6050 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6052 str_copy_len += STORE_MAX_PIECES - 1;
6053 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6055 if (str_copy_len >= exp_len)
6056 goto normal_expr;
6058 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6059 (void *) str, MEM_ALIGN (target), false))
6060 goto normal_expr;
6062 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6063 (void *) str, MEM_ALIGN (target), false,
6064 RETURN_END);
6065 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6066 exp_len - str_copy_len),
6067 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6068 return NULL_RTX;
6070 else
6072 rtx tmp_target;
6074 normal_expr:
6075 /* If we want to use a nontemporal or a reverse order store, force the
6076 value into a register first. */
6077 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6078 tree rexp = exp;
6079 if (TREE_CODE (exp) == STRING_CST
6080 && tmp_target == target
6081 && GET_MODE (target) == BLKmode
6082 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6084 rtx size = expr_size (exp);
6085 if (CONST_INT_P (size)
6086 && size != const0_rtx
6087 && (UINTVAL (size)
6088 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6090 /* If the STRING_CST has much larger array type than
6091 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6092 it into the rodata section as the code later on will use
6093 memset zero for the remainder anyway. See PR95052. */
6094 tmp_target = NULL_RTX;
6095 rexp = copy_node (exp);
6096 tree index
6097 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6098 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6099 index);
6100 shortened_string_cst = true;
6103 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6104 (call_param_p
6105 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6106 &alt_rtl, false);
6107 if (shortened_string_cst)
6109 gcc_assert (MEM_P (temp));
6110 temp = change_address (temp, BLKmode, NULL_RTX);
6114 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6115 the same as that of TARGET, adjust the constant. This is needed, for
6116 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6117 only a word-sized value. */
6118 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6119 && TREE_CODE (exp) != ERROR_MARK
6120 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6122 gcc_assert (!shortened_string_cst);
6123 if (GET_MODE_CLASS (GET_MODE (target))
6124 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6125 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6126 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6128 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6129 TYPE_MODE (TREE_TYPE (exp)), 0);
6130 if (t)
6131 temp = t;
6133 if (GET_MODE (temp) == VOIDmode)
6134 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6135 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6138 /* If value was not generated in the target, store it there.
6139 Convert the value to TARGET's type first if necessary and emit the
6140 pending incrementations that have been queued when expanding EXP.
6141 Note that we cannot emit the whole queue blindly because this will
6142 effectively disable the POST_INC optimization later.
6144 If TEMP and TARGET compare equal according to rtx_equal_p, but
6145 one or both of them are volatile memory refs, we have to distinguish
6146 two cases:
6147 - expand_expr has used TARGET. In this case, we must not generate
6148 another copy. This can be detected by TARGET being equal according
6149 to == .
6150 - expand_expr has not used TARGET - that means that the source just
6151 happens to have the same RTX form. Since temp will have been created
6152 by expand_expr, it will compare unequal according to == .
6153 We must generate a copy in this case, to reach the correct number
6154 of volatile memory references. */
6156 if ((! rtx_equal_p (temp, target)
6157 || (temp != target && (side_effects_p (temp)
6158 || side_effects_p (target))))
6159 && TREE_CODE (exp) != ERROR_MARK
6160 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6161 but TARGET is not valid memory reference, TEMP will differ
6162 from TARGET although it is really the same location. */
6163 && !(alt_rtl
6164 && rtx_equal_p (alt_rtl, target)
6165 && !side_effects_p (alt_rtl)
6166 && !side_effects_p (target))
6167 /* If there's nothing to copy, don't bother. Don't call
6168 expr_size unless necessary, because some front-ends (C++)
6169 expr_size-hook must not be given objects that are not
6170 supposed to be bit-copied or bit-initialized. */
6171 && expr_size (exp) != const0_rtx)
6173 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6175 gcc_assert (!shortened_string_cst);
6176 if (GET_MODE (target) == BLKmode)
6178 /* Handle calls that return BLKmode values in registers. */
6179 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6180 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6181 else
6182 store_bit_field (target,
6183 rtx_to_poly_int64 (expr_size (exp))
6184 * BITS_PER_UNIT,
6185 0, 0, 0, GET_MODE (temp), temp, reverse);
6187 else
6188 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6191 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6193 /* Handle copying a string constant into an array. The string
6194 constant may be shorter than the array. So copy just the string's
6195 actual length, and clear the rest. First get the size of the data
6196 type of the string, which is actually the size of the target. */
6197 rtx size = expr_size (exp);
6199 if (CONST_INT_P (size)
6200 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6201 emit_block_move (target, temp, size,
6202 (call_param_p
6203 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6204 else
6206 machine_mode pointer_mode
6207 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6208 machine_mode address_mode = get_address_mode (target);
6210 /* Compute the size of the data to copy from the string. */
6211 tree copy_size
6212 = size_binop_loc (loc, MIN_EXPR,
6213 make_tree (sizetype, size),
6214 size_int (TREE_STRING_LENGTH (exp)));
6215 rtx copy_size_rtx
6216 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6217 (call_param_p
6218 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6219 rtx_code_label *label = 0;
6221 /* Copy that much. */
6222 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6223 TYPE_UNSIGNED (sizetype));
6224 emit_block_move (target, temp, copy_size_rtx,
6225 (call_param_p
6226 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6228 /* Figure out how much is left in TARGET that we have to clear.
6229 Do all calculations in pointer_mode. */
6230 poly_int64 const_copy_size;
6231 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6233 size = plus_constant (address_mode, size, -const_copy_size);
6234 target = adjust_address (target, BLKmode, const_copy_size);
6236 else
6238 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6239 copy_size_rtx, NULL_RTX, 0,
6240 OPTAB_LIB_WIDEN);
6242 if (GET_MODE (copy_size_rtx) != address_mode)
6243 copy_size_rtx = convert_to_mode (address_mode,
6244 copy_size_rtx,
6245 TYPE_UNSIGNED (sizetype));
6247 target = offset_address (target, copy_size_rtx,
6248 highest_pow2_factor (copy_size));
6249 label = gen_label_rtx ();
6250 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6251 GET_MODE (size), 0, label);
6254 if (size != const0_rtx)
6255 clear_storage (target, size, BLOCK_OP_NORMAL);
6257 if (label)
6258 emit_label (label);
6261 else if (shortened_string_cst)
6262 gcc_unreachable ();
6263 /* Handle calls that return values in multiple non-contiguous locations.
6264 The Irix 6 ABI has examples of this. */
6265 else if (GET_CODE (target) == PARALLEL)
6267 if (GET_CODE (temp) == PARALLEL)
6268 emit_group_move (target, temp);
6269 else
6270 emit_group_load (target, temp, TREE_TYPE (exp),
6271 int_size_in_bytes (TREE_TYPE (exp)));
6273 else if (GET_CODE (temp) == PARALLEL)
6274 emit_group_store (target, temp, TREE_TYPE (exp),
6275 int_size_in_bytes (TREE_TYPE (exp)));
6276 else if (GET_MODE (temp) == BLKmode)
6277 emit_block_move (target, temp, expr_size (exp),
6278 (call_param_p
6279 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6280 /* If we emit a nontemporal store, there is nothing else to do. */
6281 else if (nontemporal && emit_storent_insn (target, temp))
6283 else
6285 if (reverse)
6286 temp = flip_storage_order (GET_MODE (target), temp);
6287 temp = force_operand (temp, target);
6288 if (temp != target)
6289 emit_move_insn (target, temp);
6292 else
6293 gcc_assert (!shortened_string_cst);
6295 return NULL_RTX;
6298 /* Return true if field F of structure TYPE is a flexible array. */
6300 static bool
6301 flexible_array_member_p (const_tree f, const_tree type)
6303 const_tree tf;
6305 tf = TREE_TYPE (f);
6306 return (DECL_CHAIN (f) == NULL
6307 && TREE_CODE (tf) == ARRAY_TYPE
6308 && TYPE_DOMAIN (tf)
6309 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6310 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6311 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6312 && int_size_in_bytes (type) >= 0);
6315 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6316 must have in order for it to completely initialize a value of type TYPE.
6317 Return -1 if the number isn't known.
6319 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6321 static HOST_WIDE_INT
6322 count_type_elements (const_tree type, bool for_ctor_p)
6324 switch (TREE_CODE (type))
6326 case ARRAY_TYPE:
6328 tree nelts;
6330 nelts = array_type_nelts (type);
6331 if (nelts && tree_fits_uhwi_p (nelts))
6333 unsigned HOST_WIDE_INT n;
6335 n = tree_to_uhwi (nelts) + 1;
6336 if (n == 0 || for_ctor_p)
6337 return n;
6338 else
6339 return n * count_type_elements (TREE_TYPE (type), false);
6341 return for_ctor_p ? -1 : 1;
6344 case RECORD_TYPE:
6346 unsigned HOST_WIDE_INT n;
6347 tree f;
6349 n = 0;
6350 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6351 if (TREE_CODE (f) == FIELD_DECL)
6353 if (!for_ctor_p)
6354 n += count_type_elements (TREE_TYPE (f), false);
6355 else if (!flexible_array_member_p (f, type))
6356 /* Don't count flexible arrays, which are not supposed
6357 to be initialized. */
6358 n += 1;
6361 return n;
6364 case UNION_TYPE:
6365 case QUAL_UNION_TYPE:
6367 tree f;
6368 HOST_WIDE_INT n, m;
6370 gcc_assert (!for_ctor_p);
6371 /* Estimate the number of scalars in each field and pick the
6372 maximum. Other estimates would do instead; the idea is simply
6373 to make sure that the estimate is not sensitive to the ordering
6374 of the fields. */
6375 n = 1;
6376 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6377 if (TREE_CODE (f) == FIELD_DECL)
6379 m = count_type_elements (TREE_TYPE (f), false);
6380 /* If the field doesn't span the whole union, add an extra
6381 scalar for the rest. */
6382 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6383 TYPE_SIZE (type)) != 1)
6384 m++;
6385 if (n < m)
6386 n = m;
6388 return n;
6391 case COMPLEX_TYPE:
6392 return 2;
6394 case VECTOR_TYPE:
6396 unsigned HOST_WIDE_INT nelts;
6397 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6398 return nelts;
6399 else
6400 return -1;
6403 case INTEGER_TYPE:
6404 case REAL_TYPE:
6405 case FIXED_POINT_TYPE:
6406 case ENUMERAL_TYPE:
6407 case BOOLEAN_TYPE:
6408 case POINTER_TYPE:
6409 case OFFSET_TYPE:
6410 case REFERENCE_TYPE:
6411 case NULLPTR_TYPE:
6412 return 1;
6414 case ERROR_MARK:
6415 return 0;
6417 case VOID_TYPE:
6418 case OPAQUE_TYPE:
6419 case METHOD_TYPE:
6420 case FUNCTION_TYPE:
6421 case LANG_TYPE:
6422 default:
6423 gcc_unreachable ();
6427 /* Helper for categorize_ctor_elements. Identical interface. */
6429 static bool
6430 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6431 HOST_WIDE_INT *p_unique_nz_elts,
6432 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6434 unsigned HOST_WIDE_INT idx;
6435 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6436 tree value, purpose, elt_type;
6438 /* Whether CTOR is a valid constant initializer, in accordance with what
6439 initializer_constant_valid_p does. If inferred from the constructor
6440 elements, true until proven otherwise. */
6441 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6442 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6444 nz_elts = 0;
6445 unique_nz_elts = 0;
6446 init_elts = 0;
6447 num_fields = 0;
6448 elt_type = NULL_TREE;
6450 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6452 HOST_WIDE_INT mult = 1;
6454 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6456 tree lo_index = TREE_OPERAND (purpose, 0);
6457 tree hi_index = TREE_OPERAND (purpose, 1);
6459 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6460 mult = (tree_to_uhwi (hi_index)
6461 - tree_to_uhwi (lo_index) + 1);
6463 num_fields += mult;
6464 elt_type = TREE_TYPE (value);
6466 switch (TREE_CODE (value))
6468 case CONSTRUCTOR:
6470 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6472 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6473 &ic, p_complete);
6475 nz_elts += mult * nz;
6476 unique_nz_elts += unz;
6477 init_elts += mult * ic;
6479 if (const_from_elts_p && const_p)
6480 const_p = const_elt_p;
6482 break;
6484 case INTEGER_CST:
6485 case REAL_CST:
6486 case FIXED_CST:
6487 if (!initializer_zerop (value))
6489 nz_elts += mult;
6490 unique_nz_elts++;
6492 init_elts += mult;
6493 break;
6495 case STRING_CST:
6496 nz_elts += mult * TREE_STRING_LENGTH (value);
6497 unique_nz_elts += TREE_STRING_LENGTH (value);
6498 init_elts += mult * TREE_STRING_LENGTH (value);
6499 break;
6501 case COMPLEX_CST:
6502 if (!initializer_zerop (TREE_REALPART (value)))
6504 nz_elts += mult;
6505 unique_nz_elts++;
6507 if (!initializer_zerop (TREE_IMAGPART (value)))
6509 nz_elts += mult;
6510 unique_nz_elts++;
6512 init_elts += 2 * mult;
6513 break;
6515 case VECTOR_CST:
6517 /* We can only construct constant-length vectors using
6518 CONSTRUCTOR. */
6519 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6520 for (unsigned int i = 0; i < nunits; ++i)
6522 tree v = VECTOR_CST_ELT (value, i);
6523 if (!initializer_zerop (v))
6525 nz_elts += mult;
6526 unique_nz_elts++;
6528 init_elts += mult;
6531 break;
6533 default:
6535 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6536 nz_elts += mult * tc;
6537 unique_nz_elts += tc;
6538 init_elts += mult * tc;
6540 if (const_from_elts_p && const_p)
6541 const_p
6542 = initializer_constant_valid_p (value,
6543 elt_type,
6544 TYPE_REVERSE_STORAGE_ORDER
6545 (TREE_TYPE (ctor)))
6546 != NULL_TREE;
6548 break;
6552 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6553 num_fields, elt_type))
6554 *p_complete = false;
6556 *p_nz_elts += nz_elts;
6557 *p_unique_nz_elts += unique_nz_elts;
6558 *p_init_elts += init_elts;
6560 return const_p;
6563 /* Examine CTOR to discover:
6564 * how many scalar fields are set to nonzero values,
6565 and place it in *P_NZ_ELTS;
6566 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6567 high - low + 1 (this can be useful for callers to determine ctors
6568 that could be cheaply initialized with - perhaps nested - loops
6569 compared to copied from huge read-only data),
6570 and place it in *P_UNIQUE_NZ_ELTS;
6571 * how many scalar fields in total are in CTOR,
6572 and place it in *P_ELT_COUNT.
6573 * whether the constructor is complete -- in the sense that every
6574 meaningful byte is explicitly given a value --
6575 and place it in *P_COMPLETE.
6577 Return whether or not CTOR is a valid static constant initializer, the same
6578 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6580 bool
6581 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6582 HOST_WIDE_INT *p_unique_nz_elts,
6583 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6585 *p_nz_elts = 0;
6586 *p_unique_nz_elts = 0;
6587 *p_init_elts = 0;
6588 *p_complete = true;
6590 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6591 p_init_elts, p_complete);
6594 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6595 of which had type LAST_TYPE. Each element was itself a complete
6596 initializer, in the sense that every meaningful byte was explicitly
6597 given a value. Return true if the same is true for the constructor
6598 as a whole. */
6600 bool
6601 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6602 const_tree last_type)
6604 if (TREE_CODE (type) == UNION_TYPE
6605 || TREE_CODE (type) == QUAL_UNION_TYPE)
6607 if (num_elts == 0)
6608 return false;
6610 gcc_assert (num_elts == 1 && last_type);
6612 /* ??? We could look at each element of the union, and find the
6613 largest element. Which would avoid comparing the size of the
6614 initialized element against any tail padding in the union.
6615 Doesn't seem worth the effort... */
6616 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6619 return count_type_elements (type, true) == num_elts;
6622 /* Return 1 if EXP contains mostly (3/4) zeros. */
6624 static int
6625 mostly_zeros_p (const_tree exp)
6627 if (TREE_CODE (exp) == CONSTRUCTOR)
6629 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6630 bool complete_p;
6632 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6633 &complete_p);
6634 return !complete_p || nz_elts < init_elts / 4;
6637 return initializer_zerop (exp);
6640 /* Return 1 if EXP contains all zeros. */
6642 static int
6643 all_zeros_p (const_tree exp)
6645 if (TREE_CODE (exp) == CONSTRUCTOR)
6647 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6648 bool complete_p;
6650 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6651 &complete_p);
6652 return nz_elts == 0;
6655 return initializer_zerop (exp);
6658 /* Helper function for store_constructor.
6659 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6660 CLEARED is as for store_constructor.
6661 ALIAS_SET is the alias set to use for any stores.
6662 If REVERSE is true, the store is to be done in reverse order.
6664 This provides a recursive shortcut back to store_constructor when it isn't
6665 necessary to go through store_field. This is so that we can pass through
6666 the cleared field to let store_constructor know that we may not have to
6667 clear a substructure if the outer structure has already been cleared. */
6669 static void
6670 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6671 poly_uint64 bitregion_start,
6672 poly_uint64 bitregion_end,
6673 machine_mode mode,
6674 tree exp, int cleared,
6675 alias_set_type alias_set, bool reverse)
6677 poly_int64 bytepos;
6678 poly_uint64 bytesize;
6679 if (TREE_CODE (exp) == CONSTRUCTOR
6680 /* We can only call store_constructor recursively if the size and
6681 bit position are on a byte boundary. */
6682 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6683 && maybe_ne (bitsize, 0U)
6684 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6685 /* If we have a nonzero bitpos for a register target, then we just
6686 let store_field do the bitfield handling. This is unlikely to
6687 generate unnecessary clear instructions anyways. */
6688 && (known_eq (bitpos, 0) || MEM_P (target)))
6690 if (MEM_P (target))
6692 machine_mode target_mode = GET_MODE (target);
6693 if (target_mode != BLKmode
6694 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6695 target_mode = BLKmode;
6696 target = adjust_address (target, target_mode, bytepos);
6700 /* Update the alias set, if required. */
6701 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6702 && MEM_ALIAS_SET (target) != 0)
6704 target = copy_rtx (target);
6705 set_mem_alias_set (target, alias_set);
6708 store_constructor (exp, target, cleared, bytesize, reverse);
6710 else
6711 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6712 exp, alias_set, false, reverse);
6716 /* Returns the number of FIELD_DECLs in TYPE. */
6718 static int
6719 fields_length (const_tree type)
6721 tree t = TYPE_FIELDS (type);
6722 int count = 0;
6724 for (; t; t = DECL_CHAIN (t))
6725 if (TREE_CODE (t) == FIELD_DECL)
6726 ++count;
6728 return count;
6732 /* Store the value of constructor EXP into the rtx TARGET.
6733 TARGET is either a REG or a MEM; we know it cannot conflict, since
6734 safe_from_p has been called.
6735 CLEARED is true if TARGET is known to have been zero'd.
6736 SIZE is the number of bytes of TARGET we are allowed to modify: this
6737 may not be the same as the size of EXP if we are assigning to a field
6738 which has been packed to exclude padding bits.
6739 If REVERSE is true, the store is to be done in reverse order. */
6741 static void
6742 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6743 bool reverse)
6745 tree type = TREE_TYPE (exp);
6746 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6747 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6749 switch (TREE_CODE (type))
6751 case RECORD_TYPE:
6752 case UNION_TYPE:
6753 case QUAL_UNION_TYPE:
6755 unsigned HOST_WIDE_INT idx;
6756 tree field, value;
6758 /* The storage order is specified for every aggregate type. */
6759 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6761 /* If size is zero or the target is already cleared, do nothing. */
6762 if (known_eq (size, 0) || cleared)
6763 cleared = 1;
6764 /* We either clear the aggregate or indicate the value is dead. */
6765 else if ((TREE_CODE (type) == UNION_TYPE
6766 || TREE_CODE (type) == QUAL_UNION_TYPE)
6767 && ! CONSTRUCTOR_ELTS (exp))
6768 /* If the constructor is empty, clear the union. */
6770 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6771 cleared = 1;
6774 /* If we are building a static constructor into a register,
6775 set the initial value as zero so we can fold the value into
6776 a constant. But if more than one register is involved,
6777 this probably loses. */
6778 else if (REG_P (target) && TREE_STATIC (exp)
6779 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6780 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6782 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6783 cleared = 1;
6786 /* If the constructor has fewer fields than the structure or
6787 if we are initializing the structure to mostly zeros, clear
6788 the whole structure first. Don't do this if TARGET is a
6789 register whose mode size isn't equal to SIZE since
6790 clear_storage can't handle this case. */
6791 else if (known_size_p (size)
6792 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6793 || mostly_zeros_p (exp))
6794 && (!REG_P (target)
6795 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6797 clear_storage (target, gen_int_mode (size, Pmode),
6798 BLOCK_OP_NORMAL);
6799 cleared = 1;
6802 if (REG_P (target) && !cleared)
6803 emit_clobber (target);
6805 /* Store each element of the constructor into the
6806 corresponding field of TARGET. */
6807 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6809 machine_mode mode;
6810 HOST_WIDE_INT bitsize;
6811 HOST_WIDE_INT bitpos = 0;
6812 tree offset;
6813 rtx to_rtx = target;
6815 /* Just ignore missing fields. We cleared the whole
6816 structure, above, if any fields are missing. */
6817 if (field == 0)
6818 continue;
6820 if (cleared && initializer_zerop (value))
6821 continue;
6823 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6824 bitsize = tree_to_uhwi (DECL_SIZE (field));
6825 else
6826 gcc_unreachable ();
6828 mode = DECL_MODE (field);
6829 if (DECL_BIT_FIELD (field))
6830 mode = VOIDmode;
6832 offset = DECL_FIELD_OFFSET (field);
6833 if (tree_fits_shwi_p (offset)
6834 && tree_fits_shwi_p (bit_position (field)))
6836 bitpos = int_bit_position (field);
6837 offset = NULL_TREE;
6839 else
6840 gcc_unreachable ();
6842 /* If this initializes a field that is smaller than a
6843 word, at the start of a word, try to widen it to a full
6844 word. This special case allows us to output C++ member
6845 function initializations in a form that the optimizers
6846 can understand. */
6847 if (WORD_REGISTER_OPERATIONS
6848 && REG_P (target)
6849 && bitsize < BITS_PER_WORD
6850 && bitpos % BITS_PER_WORD == 0
6851 && GET_MODE_CLASS (mode) == MODE_INT
6852 && TREE_CODE (value) == INTEGER_CST
6853 && exp_size >= 0
6854 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6856 type = TREE_TYPE (value);
6858 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6860 type = lang_hooks.types.type_for_mode
6861 (word_mode, TYPE_UNSIGNED (type));
6862 value = fold_convert (type, value);
6863 /* Make sure the bits beyond the original bitsize are zero
6864 so that we can correctly avoid extra zeroing stores in
6865 later constructor elements. */
6866 tree bitsize_mask
6867 = wide_int_to_tree (type, wi::mask (bitsize, false,
6868 BITS_PER_WORD));
6869 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6872 if (BYTES_BIG_ENDIAN)
6873 value
6874 = fold_build2 (LSHIFT_EXPR, type, value,
6875 build_int_cst (type,
6876 BITS_PER_WORD - bitsize));
6877 bitsize = BITS_PER_WORD;
6878 mode = word_mode;
6881 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6882 && DECL_NONADDRESSABLE_P (field))
6884 to_rtx = copy_rtx (to_rtx);
6885 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6888 store_constructor_field (to_rtx, bitsize, bitpos,
6889 0, bitregion_end, mode,
6890 value, cleared,
6891 get_alias_set (TREE_TYPE (field)),
6892 reverse);
6894 break;
6896 case ARRAY_TYPE:
6898 tree value, index;
6899 unsigned HOST_WIDE_INT i;
6900 int need_to_clear;
6901 tree domain;
6902 tree elttype = TREE_TYPE (type);
6903 int const_bounds_p;
6904 HOST_WIDE_INT minelt = 0;
6905 HOST_WIDE_INT maxelt = 0;
6907 /* The storage order is specified for every aggregate type. */
6908 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6910 domain = TYPE_DOMAIN (type);
6911 const_bounds_p = (TYPE_MIN_VALUE (domain)
6912 && TYPE_MAX_VALUE (domain)
6913 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6914 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6916 /* If we have constant bounds for the range of the type, get them. */
6917 if (const_bounds_p)
6919 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6920 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6923 /* If the constructor has fewer elements than the array, clear
6924 the whole array first. Similarly if this is static
6925 constructor of a non-BLKmode object. */
6926 if (cleared)
6927 need_to_clear = 0;
6928 else if (REG_P (target) && TREE_STATIC (exp))
6929 need_to_clear = 1;
6930 else
6932 unsigned HOST_WIDE_INT idx;
6933 HOST_WIDE_INT count = 0, zero_count = 0;
6934 need_to_clear = ! const_bounds_p;
6936 /* This loop is a more accurate version of the loop in
6937 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6938 is also needed to check for missing elements. */
6939 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6941 HOST_WIDE_INT this_node_count;
6943 if (need_to_clear)
6944 break;
6946 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6948 tree lo_index = TREE_OPERAND (index, 0);
6949 tree hi_index = TREE_OPERAND (index, 1);
6951 if (! tree_fits_uhwi_p (lo_index)
6952 || ! tree_fits_uhwi_p (hi_index))
6954 need_to_clear = 1;
6955 break;
6958 this_node_count = (tree_to_uhwi (hi_index)
6959 - tree_to_uhwi (lo_index) + 1);
6961 else
6962 this_node_count = 1;
6964 count += this_node_count;
6965 if (mostly_zeros_p (value))
6966 zero_count += this_node_count;
6969 /* Clear the entire array first if there are any missing
6970 elements, or if the incidence of zero elements is >=
6971 75%. */
6972 if (! need_to_clear
6973 && (count < maxelt - minelt + 1
6974 || 4 * zero_count >= 3 * count))
6975 need_to_clear = 1;
6978 if (need_to_clear && maybe_gt (size, 0))
6980 if (REG_P (target))
6981 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6982 else
6983 clear_storage (target, gen_int_mode (size, Pmode),
6984 BLOCK_OP_NORMAL);
6985 cleared = 1;
6988 if (!cleared && REG_P (target))
6989 /* Inform later passes that the old value is dead. */
6990 emit_clobber (target);
6992 /* Store each element of the constructor into the
6993 corresponding element of TARGET, determined by counting the
6994 elements. */
6995 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6997 machine_mode mode;
6998 poly_int64 bitsize;
6999 HOST_WIDE_INT bitpos;
7000 rtx xtarget = target;
7002 if (cleared && initializer_zerop (value))
7003 continue;
7005 mode = TYPE_MODE (elttype);
7006 if (mode != BLKmode)
7007 bitsize = GET_MODE_BITSIZE (mode);
7008 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7009 bitsize = -1;
7011 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7013 tree lo_index = TREE_OPERAND (index, 0);
7014 tree hi_index = TREE_OPERAND (index, 1);
7015 rtx index_r, pos_rtx;
7016 HOST_WIDE_INT lo, hi, count;
7017 tree position;
7019 /* If the range is constant and "small", unroll the loop. */
7020 if (const_bounds_p
7021 && tree_fits_shwi_p (lo_index)
7022 && tree_fits_shwi_p (hi_index)
7023 && (lo = tree_to_shwi (lo_index),
7024 hi = tree_to_shwi (hi_index),
7025 count = hi - lo + 1,
7026 (!MEM_P (target)
7027 || count <= 2
7028 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7029 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7030 <= 40 * 8)))))
7032 lo -= minelt; hi -= minelt;
7033 for (; lo <= hi; lo++)
7035 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7037 if (MEM_P (target)
7038 && !MEM_KEEP_ALIAS_SET_P (target)
7039 && TREE_CODE (type) == ARRAY_TYPE
7040 && TYPE_NONALIASED_COMPONENT (type))
7042 target = copy_rtx (target);
7043 MEM_KEEP_ALIAS_SET_P (target) = 1;
7046 store_constructor_field
7047 (target, bitsize, bitpos, 0, bitregion_end,
7048 mode, value, cleared,
7049 get_alias_set (elttype), reverse);
7052 else
7054 rtx_code_label *loop_start = gen_label_rtx ();
7055 rtx_code_label *loop_end = gen_label_rtx ();
7056 tree exit_cond;
7058 expand_normal (hi_index);
7060 index = build_decl (EXPR_LOCATION (exp),
7061 VAR_DECL, NULL_TREE, domain);
7062 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7063 SET_DECL_RTL (index, index_r);
7064 store_expr (lo_index, index_r, 0, false, reverse);
7066 /* Build the head of the loop. */
7067 do_pending_stack_adjust ();
7068 emit_label (loop_start);
7070 /* Assign value to element index. */
7071 position =
7072 fold_convert (ssizetype,
7073 fold_build2 (MINUS_EXPR,
7074 TREE_TYPE (index),
7075 index,
7076 TYPE_MIN_VALUE (domain)));
7078 position =
7079 size_binop (MULT_EXPR, position,
7080 fold_convert (ssizetype,
7081 TYPE_SIZE_UNIT (elttype)));
7083 pos_rtx = expand_normal (position);
7084 xtarget = offset_address (target, pos_rtx,
7085 highest_pow2_factor (position));
7086 xtarget = adjust_address (xtarget, mode, 0);
7087 if (TREE_CODE (value) == CONSTRUCTOR)
7088 store_constructor (value, xtarget, cleared,
7089 exact_div (bitsize, BITS_PER_UNIT),
7090 reverse);
7091 else
7092 store_expr (value, xtarget, 0, false, reverse);
7094 /* Generate a conditional jump to exit the loop. */
7095 exit_cond = build2 (LT_EXPR, integer_type_node,
7096 index, hi_index);
7097 jumpif (exit_cond, loop_end,
7098 profile_probability::uninitialized ());
7100 /* Update the loop counter, and jump to the head of
7101 the loop. */
7102 expand_assignment (index,
7103 build2 (PLUS_EXPR, TREE_TYPE (index),
7104 index, integer_one_node),
7105 false);
7107 emit_jump (loop_start);
7109 /* Build the end of the loop. */
7110 emit_label (loop_end);
7113 else if ((index != 0 && ! tree_fits_shwi_p (index))
7114 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7116 tree position;
7118 if (index == 0)
7119 index = ssize_int (1);
7121 if (minelt)
7122 index = fold_convert (ssizetype,
7123 fold_build2 (MINUS_EXPR,
7124 TREE_TYPE (index),
7125 index,
7126 TYPE_MIN_VALUE (domain)));
7128 position =
7129 size_binop (MULT_EXPR, index,
7130 fold_convert (ssizetype,
7131 TYPE_SIZE_UNIT (elttype)));
7132 xtarget = offset_address (target,
7133 expand_normal (position),
7134 highest_pow2_factor (position));
7135 xtarget = adjust_address (xtarget, mode, 0);
7136 store_expr (value, xtarget, 0, false, reverse);
7138 else
7140 if (index != 0)
7141 bitpos = ((tree_to_shwi (index) - minelt)
7142 * tree_to_uhwi (TYPE_SIZE (elttype)));
7143 else
7144 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7146 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7147 && TREE_CODE (type) == ARRAY_TYPE
7148 && TYPE_NONALIASED_COMPONENT (type))
7150 target = copy_rtx (target);
7151 MEM_KEEP_ALIAS_SET_P (target) = 1;
7153 store_constructor_field (target, bitsize, bitpos, 0,
7154 bitregion_end, mode, value,
7155 cleared, get_alias_set (elttype),
7156 reverse);
7159 break;
7162 case VECTOR_TYPE:
7164 unsigned HOST_WIDE_INT idx;
7165 constructor_elt *ce;
7166 int i;
7167 int need_to_clear;
7168 insn_code icode = CODE_FOR_nothing;
7169 tree elt;
7170 tree elttype = TREE_TYPE (type);
7171 int elt_size = vector_element_bits (type);
7172 machine_mode eltmode = TYPE_MODE (elttype);
7173 HOST_WIDE_INT bitsize;
7174 HOST_WIDE_INT bitpos;
7175 rtvec vector = NULL;
7176 poly_uint64 n_elts;
7177 unsigned HOST_WIDE_INT const_n_elts;
7178 alias_set_type alias;
7179 bool vec_vec_init_p = false;
7180 machine_mode mode = GET_MODE (target);
7182 gcc_assert (eltmode != BLKmode);
7184 /* Try using vec_duplicate_optab for uniform vectors. */
7185 if (!TREE_SIDE_EFFECTS (exp)
7186 && VECTOR_MODE_P (mode)
7187 && eltmode == GET_MODE_INNER (mode)
7188 && ((icode = optab_handler (vec_duplicate_optab, mode))
7189 != CODE_FOR_nothing)
7190 && (elt = uniform_vector_p (exp))
7191 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7193 class expand_operand ops[2];
7194 create_output_operand (&ops[0], target, mode);
7195 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7196 expand_insn (icode, 2, ops);
7197 if (!rtx_equal_p (target, ops[0].value))
7198 emit_move_insn (target, ops[0].value);
7199 break;
7202 n_elts = TYPE_VECTOR_SUBPARTS (type);
7203 if (REG_P (target)
7204 && VECTOR_MODE_P (mode)
7205 && n_elts.is_constant (&const_n_elts))
7207 machine_mode emode = eltmode;
7208 bool vector_typed_elts_p = false;
7210 if (CONSTRUCTOR_NELTS (exp)
7211 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7212 == VECTOR_TYPE))
7214 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7215 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7216 * TYPE_VECTOR_SUBPARTS (etype),
7217 n_elts));
7218 emode = TYPE_MODE (etype);
7219 vector_typed_elts_p = true;
7221 icode = convert_optab_handler (vec_init_optab, mode, emode);
7222 if (icode != CODE_FOR_nothing)
7224 unsigned int n = const_n_elts;
7226 if (vector_typed_elts_p)
7228 n = CONSTRUCTOR_NELTS (exp);
7229 vec_vec_init_p = true;
7231 vector = rtvec_alloc (n);
7232 for (unsigned int k = 0; k < n; k++)
7233 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7237 /* Compute the size of the elements in the CTOR. It differs
7238 from the size of the vector type elements only when the
7239 CTOR elements are vectors themselves. */
7240 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7241 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7242 : elttype);
7243 if (VECTOR_TYPE_P (val_type))
7244 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7245 else
7246 bitsize = elt_size;
7248 /* If the constructor has fewer elements than the vector,
7249 clear the whole array first. Similarly if this is static
7250 constructor of a non-BLKmode object. */
7251 if (cleared)
7252 need_to_clear = 0;
7253 else if (REG_P (target) && TREE_STATIC (exp))
7254 need_to_clear = 1;
7255 else
7257 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7258 tree value;
7260 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7262 int n_elts_here = bitsize / elt_size;
7263 count += n_elts_here;
7264 if (mostly_zeros_p (value))
7265 zero_count += n_elts_here;
7268 /* Clear the entire vector first if there are any missing elements,
7269 or if the incidence of zero elements is >= 75%. */
7270 need_to_clear = (maybe_lt (count, n_elts)
7271 || 4 * zero_count >= 3 * count);
7274 if (need_to_clear && maybe_gt (size, 0) && !vector)
7276 if (REG_P (target))
7277 emit_move_insn (target, CONST0_RTX (mode));
7278 else
7279 clear_storage (target, gen_int_mode (size, Pmode),
7280 BLOCK_OP_NORMAL);
7281 cleared = 1;
7284 /* Inform later passes that the old value is dead. */
7285 if (!cleared && !vector && REG_P (target))
7286 emit_move_insn (target, CONST0_RTX (mode));
7288 if (MEM_P (target))
7289 alias = MEM_ALIAS_SET (target);
7290 else
7291 alias = get_alias_set (elttype);
7293 /* Store each element of the constructor into the corresponding
7294 element of TARGET, determined by counting the elements. */
7295 for (idx = 0, i = 0;
7296 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7297 idx++, i += bitsize / elt_size)
7299 HOST_WIDE_INT eltpos;
7300 tree value = ce->value;
7302 if (cleared && initializer_zerop (value))
7303 continue;
7305 if (ce->index)
7306 eltpos = tree_to_uhwi (ce->index);
7307 else
7308 eltpos = i;
7310 if (vector)
7312 if (vec_vec_init_p)
7314 gcc_assert (ce->index == NULL_TREE);
7315 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7316 eltpos = idx;
7318 else
7319 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7320 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7322 else
7324 machine_mode value_mode
7325 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7326 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7327 bitpos = eltpos * elt_size;
7328 store_constructor_field (target, bitsize, bitpos, 0,
7329 bitregion_end, value_mode,
7330 value, cleared, alias, reverse);
7334 if (vector)
7335 emit_insn (GEN_FCN (icode) (target,
7336 gen_rtx_PARALLEL (mode, vector)));
7337 break;
7340 default:
7341 gcc_unreachable ();
7345 /* Store the value of EXP (an expression tree)
7346 into a subfield of TARGET which has mode MODE and occupies
7347 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7348 If MODE is VOIDmode, it means that we are storing into a bit-field.
7350 BITREGION_START is bitpos of the first bitfield in this region.
7351 BITREGION_END is the bitpos of the ending bitfield in this region.
7352 These two fields are 0, if the C++ memory model does not apply,
7353 or we are not interested in keeping track of bitfield regions.
7355 Always return const0_rtx unless we have something particular to
7356 return.
7358 ALIAS_SET is the alias set for the destination. This value will
7359 (in general) be different from that for TARGET, since TARGET is a
7360 reference to the containing structure.
7362 If NONTEMPORAL is true, try generating a nontemporal store.
7364 If REVERSE is true, the store is to be done in reverse order. */
7366 static rtx
7367 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7368 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7369 machine_mode mode, tree exp,
7370 alias_set_type alias_set, bool nontemporal, bool reverse)
7372 if (TREE_CODE (exp) == ERROR_MARK)
7373 return const0_rtx;
7375 /* If we have nothing to store, do nothing unless the expression has
7376 side-effects. Don't do that for zero sized addressable lhs of
7377 calls. */
7378 if (known_eq (bitsize, 0)
7379 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7380 || TREE_CODE (exp) != CALL_EXPR))
7381 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7383 if (GET_CODE (target) == CONCAT)
7385 /* We're storing into a struct containing a single __complex. */
7387 gcc_assert (known_eq (bitpos, 0));
7388 return store_expr (exp, target, 0, nontemporal, reverse);
7391 /* If the structure is in a register or if the component
7392 is a bit field, we cannot use addressing to access it.
7393 Use bit-field techniques or SUBREG to store in it. */
7395 poly_int64 decl_bitsize;
7396 if (mode == VOIDmode
7397 || (mode != BLKmode && ! direct_store[(int) mode]
7398 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7399 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7400 || REG_P (target)
7401 || GET_CODE (target) == SUBREG
7402 /* If the field isn't aligned enough to store as an ordinary memref,
7403 store it as a bit field. */
7404 || (mode != BLKmode
7405 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7406 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7407 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7408 || !multiple_p (bitpos, BITS_PER_UNIT)))
7409 || (known_size_p (bitsize)
7410 && mode != BLKmode
7411 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7412 /* If the RHS and field are a constant size and the size of the
7413 RHS isn't the same size as the bitfield, we must use bitfield
7414 operations. */
7415 || (known_size_p (bitsize)
7416 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7417 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7418 bitsize)
7419 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7420 we will handle specially below. */
7421 && !(TREE_CODE (exp) == CONSTRUCTOR
7422 && multiple_p (bitsize, BITS_PER_UNIT))
7423 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7424 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7425 includes some extra padding. store_expr / expand_expr will in
7426 that case call get_inner_reference that will have the bitsize
7427 we check here and thus the block move will not clobber the
7428 padding that shouldn't be clobbered. In the future we could
7429 replace the TREE_ADDRESSABLE check with a check that
7430 get_base_address needs to live in memory. */
7431 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7432 || TREE_CODE (exp) != COMPONENT_REF
7433 || !multiple_p (bitsize, BITS_PER_UNIT)
7434 || !multiple_p (bitpos, BITS_PER_UNIT)
7435 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7436 &decl_bitsize)
7437 || maybe_ne (decl_bitsize, bitsize))
7438 /* A call with an addressable return type and return-slot
7439 optimization must not need bitfield operations but we must
7440 pass down the original target. */
7441 && (TREE_CODE (exp) != CALL_EXPR
7442 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7443 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7444 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7445 decl we must use bitfield operations. */
7446 || (known_size_p (bitsize)
7447 && TREE_CODE (exp) == MEM_REF
7448 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7449 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7450 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7451 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7453 rtx temp;
7454 gimple *nop_def;
7456 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7457 implies a mask operation. If the precision is the same size as
7458 the field we're storing into, that mask is redundant. This is
7459 particularly common with bit field assignments generated by the
7460 C front end. */
7461 nop_def = get_def_for_expr (exp, NOP_EXPR);
7462 if (nop_def)
7464 tree type = TREE_TYPE (exp);
7465 if (INTEGRAL_TYPE_P (type)
7466 && maybe_ne (TYPE_PRECISION (type),
7467 GET_MODE_BITSIZE (TYPE_MODE (type)))
7468 && known_eq (bitsize, TYPE_PRECISION (type)))
7470 tree op = gimple_assign_rhs1 (nop_def);
7471 type = TREE_TYPE (op);
7472 if (INTEGRAL_TYPE_P (type)
7473 && known_ge (TYPE_PRECISION (type), bitsize))
7474 exp = op;
7478 temp = expand_normal (exp);
7480 /* We don't support variable-sized BLKmode bitfields, since our
7481 handling of BLKmode is bound up with the ability to break
7482 things into words. */
7483 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7485 /* Handle calls that return values in multiple non-contiguous locations.
7486 The Irix 6 ABI has examples of this. */
7487 if (GET_CODE (temp) == PARALLEL)
7489 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7490 machine_mode temp_mode = GET_MODE (temp);
7491 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7492 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7493 rtx temp_target = gen_reg_rtx (temp_mode);
7494 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7495 temp = temp_target;
7498 /* Handle calls that return BLKmode values in registers. */
7499 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7501 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7502 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7503 temp = temp_target;
7506 /* If the value has aggregate type and an integral mode then, if BITSIZE
7507 is narrower than this mode and this is for big-endian data, we first
7508 need to put the value into the low-order bits for store_bit_field,
7509 except when MODE is BLKmode and BITSIZE larger than the word size
7510 (see the handling of fields larger than a word in store_bit_field).
7511 Moreover, the field may be not aligned on a byte boundary; in this
7512 case, if it has reverse storage order, it needs to be accessed as a
7513 scalar field with reverse storage order and we must first put the
7514 value into target order. */
7515 scalar_int_mode temp_mode;
7516 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7517 && is_int_mode (GET_MODE (temp), &temp_mode))
7519 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7521 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7523 if (reverse)
7524 temp = flip_storage_order (temp_mode, temp);
7526 gcc_checking_assert (known_le (bitsize, size));
7527 if (maybe_lt (bitsize, size)
7528 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7529 /* Use of to_constant for BLKmode was checked above. */
7530 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7531 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7532 size - bitsize, NULL_RTX, 1);
7535 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7536 if (mode != VOIDmode && mode != BLKmode
7537 && mode != TYPE_MODE (TREE_TYPE (exp)))
7538 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7540 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7541 and BITPOS must be aligned on a byte boundary. If so, we simply do
7542 a block copy. Likewise for a BLKmode-like TARGET. */
7543 if (GET_MODE (temp) == BLKmode
7544 && (GET_MODE (target) == BLKmode
7545 || (MEM_P (target)
7546 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7547 && multiple_p (bitpos, BITS_PER_UNIT)
7548 && multiple_p (bitsize, BITS_PER_UNIT))))
7550 gcc_assert (MEM_P (target) && MEM_P (temp));
7551 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7552 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7554 target = adjust_address (target, VOIDmode, bytepos);
7555 emit_block_move (target, temp,
7556 gen_int_mode (bytesize, Pmode),
7557 BLOCK_OP_NORMAL);
7559 return const0_rtx;
7562 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7563 word size, we need to load the value (see again store_bit_field). */
7564 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7566 temp_mode = smallest_int_mode_for_size (bitsize);
7567 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7568 temp_mode, false, NULL);
7571 /* Store the value in the bitfield. */
7572 gcc_checking_assert (known_ge (bitpos, 0));
7573 store_bit_field (target, bitsize, bitpos,
7574 bitregion_start, bitregion_end,
7575 mode, temp, reverse);
7577 return const0_rtx;
7579 else
7581 /* Now build a reference to just the desired component. */
7582 rtx to_rtx = adjust_address (target, mode,
7583 exact_div (bitpos, BITS_PER_UNIT));
7585 if (to_rtx == target)
7586 to_rtx = copy_rtx (to_rtx);
7588 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7589 set_mem_alias_set (to_rtx, alias_set);
7591 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7592 into a target smaller than its type; handle that case now. */
7593 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7595 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7596 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7597 return to_rtx;
7600 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7604 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7605 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7606 codes and find the ultimate containing object, which we return.
7608 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7609 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7610 storage order of the field.
7611 If the position of the field is variable, we store a tree
7612 giving the variable offset (in units) in *POFFSET.
7613 This offset is in addition to the bit position.
7614 If the position is not variable, we store 0 in *POFFSET.
7616 If any of the extraction expressions is volatile,
7617 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7619 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7620 Otherwise, it is a mode that can be used to access the field.
7622 If the field describes a variable-sized object, *PMODE is set to
7623 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7624 this case, but the address of the object can be found. */
7626 tree
7627 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7628 poly_int64_pod *pbitpos, tree *poffset,
7629 machine_mode *pmode, int *punsignedp,
7630 int *preversep, int *pvolatilep)
7632 tree size_tree = 0;
7633 machine_mode mode = VOIDmode;
7634 bool blkmode_bitfield = false;
7635 tree offset = size_zero_node;
7636 poly_offset_int bit_offset = 0;
7638 /* First get the mode, signedness, storage order and size. We do this from
7639 just the outermost expression. */
7640 *pbitsize = -1;
7641 if (TREE_CODE (exp) == COMPONENT_REF)
7643 tree field = TREE_OPERAND (exp, 1);
7644 size_tree = DECL_SIZE (field);
7645 if (flag_strict_volatile_bitfields > 0
7646 && TREE_THIS_VOLATILE (exp)
7647 && DECL_BIT_FIELD_TYPE (field)
7648 && DECL_MODE (field) != BLKmode)
7649 /* Volatile bitfields should be accessed in the mode of the
7650 field's type, not the mode computed based on the bit
7651 size. */
7652 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7653 else if (!DECL_BIT_FIELD (field))
7655 mode = DECL_MODE (field);
7656 /* For vector fields re-check the target flags, as DECL_MODE
7657 could have been set with different target flags than
7658 the current function has. */
7659 if (mode == BLKmode
7660 && VECTOR_TYPE_P (TREE_TYPE (field))
7661 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7662 mode = TYPE_MODE (TREE_TYPE (field));
7664 else if (DECL_MODE (field) == BLKmode)
7665 blkmode_bitfield = true;
7667 *punsignedp = DECL_UNSIGNED (field);
7669 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7671 size_tree = TREE_OPERAND (exp, 1);
7672 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7673 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7675 /* For vector element types with the correct size of access or for
7676 vector typed accesses use the mode of the access type. */
7677 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7678 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7679 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7680 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7681 mode = TYPE_MODE (TREE_TYPE (exp));
7683 else
7685 mode = TYPE_MODE (TREE_TYPE (exp));
7686 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7688 if (mode == BLKmode)
7689 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7690 else
7691 *pbitsize = GET_MODE_BITSIZE (mode);
7694 if (size_tree != 0)
7696 if (! tree_fits_uhwi_p (size_tree))
7697 mode = BLKmode, *pbitsize = -1;
7698 else
7699 *pbitsize = tree_to_uhwi (size_tree);
7702 *preversep = reverse_storage_order_for_component_p (exp);
7704 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7705 and find the ultimate containing object. */
7706 while (1)
7708 switch (TREE_CODE (exp))
7710 case BIT_FIELD_REF:
7711 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7712 break;
7714 case COMPONENT_REF:
7716 tree field = TREE_OPERAND (exp, 1);
7717 tree this_offset = component_ref_field_offset (exp);
7719 /* If this field hasn't been filled in yet, don't go past it.
7720 This should only happen when folding expressions made during
7721 type construction. */
7722 if (this_offset == 0)
7723 break;
7725 offset = size_binop (PLUS_EXPR, offset, this_offset);
7726 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7728 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7730 break;
7732 case ARRAY_REF:
7733 case ARRAY_RANGE_REF:
7735 tree index = TREE_OPERAND (exp, 1);
7736 tree low_bound = array_ref_low_bound (exp);
7737 tree unit_size = array_ref_element_size (exp);
7739 /* We assume all arrays have sizes that are a multiple of a byte.
7740 First subtract the lower bound, if any, in the type of the
7741 index, then convert to sizetype and multiply by the size of
7742 the array element. */
7743 if (! integer_zerop (low_bound))
7744 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7745 index, low_bound);
7747 offset = size_binop (PLUS_EXPR, offset,
7748 size_binop (MULT_EXPR,
7749 fold_convert (sizetype, index),
7750 unit_size));
7752 break;
7754 case REALPART_EXPR:
7755 break;
7757 case IMAGPART_EXPR:
7758 bit_offset += *pbitsize;
7759 break;
7761 case VIEW_CONVERT_EXPR:
7762 break;
7764 case MEM_REF:
7765 /* Hand back the decl for MEM[&decl, off]. */
7766 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7768 tree off = TREE_OPERAND (exp, 1);
7769 if (!integer_zerop (off))
7771 poly_offset_int boff = mem_ref_offset (exp);
7772 boff <<= LOG2_BITS_PER_UNIT;
7773 bit_offset += boff;
7775 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7777 goto done;
7779 default:
7780 goto done;
7783 /* If any reference in the chain is volatile, the effect is volatile. */
7784 if (TREE_THIS_VOLATILE (exp))
7785 *pvolatilep = 1;
7787 exp = TREE_OPERAND (exp, 0);
7789 done:
7791 /* If OFFSET is constant, see if we can return the whole thing as a
7792 constant bit position. Make sure to handle overflow during
7793 this conversion. */
7794 if (poly_int_tree_p (offset))
7796 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7797 TYPE_PRECISION (sizetype));
7798 tem <<= LOG2_BITS_PER_UNIT;
7799 tem += bit_offset;
7800 if (tem.to_shwi (pbitpos))
7801 *poffset = offset = NULL_TREE;
7804 /* Otherwise, split it up. */
7805 if (offset)
7807 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7808 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7810 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7811 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7812 offset = size_binop (PLUS_EXPR, offset,
7813 build_int_cst (sizetype, bytes.force_shwi ()));
7816 *poffset = offset;
7819 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7820 if (mode == VOIDmode
7821 && blkmode_bitfield
7822 && multiple_p (*pbitpos, BITS_PER_UNIT)
7823 && multiple_p (*pbitsize, BITS_PER_UNIT))
7824 *pmode = BLKmode;
7825 else
7826 *pmode = mode;
7828 return exp;
7831 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7833 static unsigned HOST_WIDE_INT
7834 target_align (const_tree target)
7836 /* We might have a chain of nested references with intermediate misaligning
7837 bitfields components, so need to recurse to find out. */
7839 unsigned HOST_WIDE_INT this_align, outer_align;
7841 switch (TREE_CODE (target))
7843 case BIT_FIELD_REF:
7844 return 1;
7846 case COMPONENT_REF:
7847 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7848 outer_align = target_align (TREE_OPERAND (target, 0));
7849 return MIN (this_align, outer_align);
7851 case ARRAY_REF:
7852 case ARRAY_RANGE_REF:
7853 this_align = TYPE_ALIGN (TREE_TYPE (target));
7854 outer_align = target_align (TREE_OPERAND (target, 0));
7855 return MIN (this_align, outer_align);
7857 CASE_CONVERT:
7858 case NON_LVALUE_EXPR:
7859 case VIEW_CONVERT_EXPR:
7860 this_align = TYPE_ALIGN (TREE_TYPE (target));
7861 outer_align = target_align (TREE_OPERAND (target, 0));
7862 return MAX (this_align, outer_align);
7864 default:
7865 return TYPE_ALIGN (TREE_TYPE (target));
7870 /* Given an rtx VALUE that may contain additions and multiplications, return
7871 an equivalent value that just refers to a register, memory, or constant.
7872 This is done by generating instructions to perform the arithmetic and
7873 returning a pseudo-register containing the value.
7875 The returned value may be a REG, SUBREG, MEM or constant. */
7878 force_operand (rtx value, rtx target)
7880 rtx op1, op2;
7881 /* Use subtarget as the target for operand 0 of a binary operation. */
7882 rtx subtarget = get_subtarget (target);
7883 enum rtx_code code = GET_CODE (value);
7885 /* Check for subreg applied to an expression produced by loop optimizer. */
7886 if (code == SUBREG
7887 && !REG_P (SUBREG_REG (value))
7888 && !MEM_P (SUBREG_REG (value)))
7890 value
7891 = simplify_gen_subreg (GET_MODE (value),
7892 force_reg (GET_MODE (SUBREG_REG (value)),
7893 force_operand (SUBREG_REG (value),
7894 NULL_RTX)),
7895 GET_MODE (SUBREG_REG (value)),
7896 SUBREG_BYTE (value));
7897 code = GET_CODE (value);
7900 /* Check for a PIC address load. */
7901 if ((code == PLUS || code == MINUS)
7902 && XEXP (value, 0) == pic_offset_table_rtx
7903 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7904 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7905 || GET_CODE (XEXP (value, 1)) == CONST))
7907 if (!subtarget)
7908 subtarget = gen_reg_rtx (GET_MODE (value));
7909 emit_move_insn (subtarget, value);
7910 return subtarget;
7913 if (ARITHMETIC_P (value))
7915 op2 = XEXP (value, 1);
7916 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7917 subtarget = 0;
7918 if (code == MINUS && CONST_INT_P (op2))
7920 code = PLUS;
7921 op2 = negate_rtx (GET_MODE (value), op2);
7924 /* Check for an addition with OP2 a constant integer and our first
7925 operand a PLUS of a virtual register and something else. In that
7926 case, we want to emit the sum of the virtual register and the
7927 constant first and then add the other value. This allows virtual
7928 register instantiation to simply modify the constant rather than
7929 creating another one around this addition. */
7930 if (code == PLUS && CONST_INT_P (op2)
7931 && GET_CODE (XEXP (value, 0)) == PLUS
7932 && REG_P (XEXP (XEXP (value, 0), 0))
7933 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7934 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7936 rtx temp = expand_simple_binop (GET_MODE (value), code,
7937 XEXP (XEXP (value, 0), 0), op2,
7938 subtarget, 0, OPTAB_LIB_WIDEN);
7939 return expand_simple_binop (GET_MODE (value), code, temp,
7940 force_operand (XEXP (XEXP (value,
7941 0), 1), 0),
7942 target, 0, OPTAB_LIB_WIDEN);
7945 op1 = force_operand (XEXP (value, 0), subtarget);
7946 op2 = force_operand (op2, NULL_RTX);
7947 switch (code)
7949 case MULT:
7950 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7951 case DIV:
7952 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7953 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7954 target, 1, OPTAB_LIB_WIDEN);
7955 else
7956 return expand_divmod (0,
7957 FLOAT_MODE_P (GET_MODE (value))
7958 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7959 GET_MODE (value), op1, op2, target, 0);
7960 case MOD:
7961 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7962 target, 0);
7963 case UDIV:
7964 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7965 target, 1);
7966 case UMOD:
7967 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7968 target, 1);
7969 case ASHIFTRT:
7970 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7971 target, 0, OPTAB_LIB_WIDEN);
7972 default:
7973 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7974 target, 1, OPTAB_LIB_WIDEN);
7977 if (UNARY_P (value))
7979 if (!target)
7980 target = gen_reg_rtx (GET_MODE (value));
7981 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7982 switch (code)
7984 case ZERO_EXTEND:
7985 case SIGN_EXTEND:
7986 case TRUNCATE:
7987 case FLOAT_EXTEND:
7988 case FLOAT_TRUNCATE:
7989 convert_move (target, op1, code == ZERO_EXTEND);
7990 return target;
7992 case FIX:
7993 case UNSIGNED_FIX:
7994 expand_fix (target, op1, code == UNSIGNED_FIX);
7995 return target;
7997 case FLOAT:
7998 case UNSIGNED_FLOAT:
7999 expand_float (target, op1, code == UNSIGNED_FLOAT);
8000 return target;
8002 default:
8003 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8007 #ifdef INSN_SCHEDULING
8008 /* On machines that have insn scheduling, we want all memory reference to be
8009 explicit, so we need to deal with such paradoxical SUBREGs. */
8010 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8011 value
8012 = simplify_gen_subreg (GET_MODE (value),
8013 force_reg (GET_MODE (SUBREG_REG (value)),
8014 force_operand (SUBREG_REG (value),
8015 NULL_RTX)),
8016 GET_MODE (SUBREG_REG (value)),
8017 SUBREG_BYTE (value));
8018 #endif
8020 return value;
8023 /* Subroutine of expand_expr: return nonzero iff there is no way that
8024 EXP can reference X, which is being modified. TOP_P is nonzero if this
8025 call is going to be used to determine whether we need a temporary
8026 for EXP, as opposed to a recursive call to this function.
8028 It is always safe for this routine to return zero since it merely
8029 searches for optimization opportunities. */
8032 safe_from_p (const_rtx x, tree exp, int top_p)
8034 rtx exp_rtl = 0;
8035 int i, nops;
8037 if (x == 0
8038 /* If EXP has varying size, we MUST use a target since we currently
8039 have no way of allocating temporaries of variable size
8040 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8041 So we assume here that something at a higher level has prevented a
8042 clash. This is somewhat bogus, but the best we can do. Only
8043 do this when X is BLKmode and when we are at the top level. */
8044 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8045 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8046 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8047 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8048 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8049 != INTEGER_CST)
8050 && GET_MODE (x) == BLKmode)
8051 /* If X is in the outgoing argument area, it is always safe. */
8052 || (MEM_P (x)
8053 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8054 || (GET_CODE (XEXP (x, 0)) == PLUS
8055 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8056 return 1;
8058 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8059 find the underlying pseudo. */
8060 if (GET_CODE (x) == SUBREG)
8062 x = SUBREG_REG (x);
8063 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8064 return 0;
8067 /* Now look at our tree code and possibly recurse. */
8068 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8070 case tcc_declaration:
8071 exp_rtl = DECL_RTL_IF_SET (exp);
8072 break;
8074 case tcc_constant:
8075 return 1;
8077 case tcc_exceptional:
8078 if (TREE_CODE (exp) == TREE_LIST)
8080 while (1)
8082 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8083 return 0;
8084 exp = TREE_CHAIN (exp);
8085 if (!exp)
8086 return 1;
8087 if (TREE_CODE (exp) != TREE_LIST)
8088 return safe_from_p (x, exp, 0);
8091 else if (TREE_CODE (exp) == CONSTRUCTOR)
8093 constructor_elt *ce;
8094 unsigned HOST_WIDE_INT idx;
8096 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8097 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8098 || !safe_from_p (x, ce->value, 0))
8099 return 0;
8100 return 1;
8102 else if (TREE_CODE (exp) == ERROR_MARK)
8103 return 1; /* An already-visited SAVE_EXPR? */
8104 else
8105 return 0;
8107 case tcc_statement:
8108 /* The only case we look at here is the DECL_INITIAL inside a
8109 DECL_EXPR. */
8110 return (TREE_CODE (exp) != DECL_EXPR
8111 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8112 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8113 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8115 case tcc_binary:
8116 case tcc_comparison:
8117 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8118 return 0;
8119 /* Fall through. */
8121 case tcc_unary:
8122 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8124 case tcc_expression:
8125 case tcc_reference:
8126 case tcc_vl_exp:
8127 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8128 the expression. If it is set, we conflict iff we are that rtx or
8129 both are in memory. Otherwise, we check all operands of the
8130 expression recursively. */
8132 switch (TREE_CODE (exp))
8134 case ADDR_EXPR:
8135 /* If the operand is static or we are static, we can't conflict.
8136 Likewise if we don't conflict with the operand at all. */
8137 if (staticp (TREE_OPERAND (exp, 0))
8138 || TREE_STATIC (exp)
8139 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8140 return 1;
8142 /* Otherwise, the only way this can conflict is if we are taking
8143 the address of a DECL a that address if part of X, which is
8144 very rare. */
8145 exp = TREE_OPERAND (exp, 0);
8146 if (DECL_P (exp))
8148 if (!DECL_RTL_SET_P (exp)
8149 || !MEM_P (DECL_RTL (exp)))
8150 return 0;
8151 else
8152 exp_rtl = XEXP (DECL_RTL (exp), 0);
8154 break;
8156 case MEM_REF:
8157 if (MEM_P (x)
8158 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8159 get_alias_set (exp)))
8160 return 0;
8161 break;
8163 case CALL_EXPR:
8164 /* Assume that the call will clobber all hard registers and
8165 all of memory. */
8166 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8167 || MEM_P (x))
8168 return 0;
8169 break;
8171 case WITH_CLEANUP_EXPR:
8172 case CLEANUP_POINT_EXPR:
8173 /* Lowered by gimplify.c. */
8174 gcc_unreachable ();
8176 case SAVE_EXPR:
8177 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8179 default:
8180 break;
8183 /* If we have an rtx, we do not need to scan our operands. */
8184 if (exp_rtl)
8185 break;
8187 nops = TREE_OPERAND_LENGTH (exp);
8188 for (i = 0; i < nops; i++)
8189 if (TREE_OPERAND (exp, i) != 0
8190 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8191 return 0;
8193 break;
8195 case tcc_type:
8196 /* Should never get a type here. */
8197 gcc_unreachable ();
8200 /* If we have an rtl, find any enclosed object. Then see if we conflict
8201 with it. */
8202 if (exp_rtl)
8204 if (GET_CODE (exp_rtl) == SUBREG)
8206 exp_rtl = SUBREG_REG (exp_rtl);
8207 if (REG_P (exp_rtl)
8208 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8209 return 0;
8212 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8213 are memory and they conflict. */
8214 return ! (rtx_equal_p (x, exp_rtl)
8215 || (MEM_P (x) && MEM_P (exp_rtl)
8216 && true_dependence (exp_rtl, VOIDmode, x)));
8219 /* If we reach here, it is safe. */
8220 return 1;
8224 /* Return the highest power of two that EXP is known to be a multiple of.
8225 This is used in updating alignment of MEMs in array references. */
8227 unsigned HOST_WIDE_INT
8228 highest_pow2_factor (const_tree exp)
8230 unsigned HOST_WIDE_INT ret;
8231 int trailing_zeros = tree_ctz (exp);
8232 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8233 return BIGGEST_ALIGNMENT;
8234 ret = HOST_WIDE_INT_1U << trailing_zeros;
8235 if (ret > BIGGEST_ALIGNMENT)
8236 return BIGGEST_ALIGNMENT;
8237 return ret;
8240 /* Similar, except that the alignment requirements of TARGET are
8241 taken into account. Assume it is at least as aligned as its
8242 type, unless it is a COMPONENT_REF in which case the layout of
8243 the structure gives the alignment. */
8245 static unsigned HOST_WIDE_INT
8246 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8248 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8249 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8251 return MAX (factor, talign);
8254 /* Convert the tree comparison code TCODE to the rtl one where the
8255 signedness is UNSIGNEDP. */
8257 static enum rtx_code
8258 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8260 enum rtx_code code;
8261 switch (tcode)
8263 case EQ_EXPR:
8264 code = EQ;
8265 break;
8266 case NE_EXPR:
8267 code = NE;
8268 break;
8269 case LT_EXPR:
8270 code = unsignedp ? LTU : LT;
8271 break;
8272 case LE_EXPR:
8273 code = unsignedp ? LEU : LE;
8274 break;
8275 case GT_EXPR:
8276 code = unsignedp ? GTU : GT;
8277 break;
8278 case GE_EXPR:
8279 code = unsignedp ? GEU : GE;
8280 break;
8281 case UNORDERED_EXPR:
8282 code = UNORDERED;
8283 break;
8284 case ORDERED_EXPR:
8285 code = ORDERED;
8286 break;
8287 case UNLT_EXPR:
8288 code = UNLT;
8289 break;
8290 case UNLE_EXPR:
8291 code = UNLE;
8292 break;
8293 case UNGT_EXPR:
8294 code = UNGT;
8295 break;
8296 case UNGE_EXPR:
8297 code = UNGE;
8298 break;
8299 case UNEQ_EXPR:
8300 code = UNEQ;
8301 break;
8302 case LTGT_EXPR:
8303 code = LTGT;
8304 break;
8306 default:
8307 gcc_unreachable ();
8309 return code;
8312 /* Subroutine of expand_expr. Expand the two operands of a binary
8313 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8314 The value may be stored in TARGET if TARGET is nonzero. The
8315 MODIFIER argument is as documented by expand_expr. */
8317 void
8318 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8319 enum expand_modifier modifier)
8321 if (! safe_from_p (target, exp1, 1))
8322 target = 0;
8323 if (operand_equal_p (exp0, exp1, 0))
8325 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8326 *op1 = copy_rtx (*op0);
8328 else
8330 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8331 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8336 /* Return a MEM that contains constant EXP. DEFER is as for
8337 output_constant_def and MODIFIER is as for expand_expr. */
8339 static rtx
8340 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8342 rtx mem;
8344 mem = output_constant_def (exp, defer);
8345 if (modifier != EXPAND_INITIALIZER)
8346 mem = use_anchored_address (mem);
8347 return mem;
8350 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8351 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8353 static rtx
8354 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8355 enum expand_modifier modifier, addr_space_t as)
8357 rtx result, subtarget;
8358 tree inner, offset;
8359 poly_int64 bitsize, bitpos;
8360 int unsignedp, reversep, volatilep = 0;
8361 machine_mode mode1;
8363 /* If we are taking the address of a constant and are at the top level,
8364 we have to use output_constant_def since we can't call force_const_mem
8365 at top level. */
8366 /* ??? This should be considered a front-end bug. We should not be
8367 generating ADDR_EXPR of something that isn't an LVALUE. The only
8368 exception here is STRING_CST. */
8369 if (CONSTANT_CLASS_P (exp))
8371 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8372 if (modifier < EXPAND_SUM)
8373 result = force_operand (result, target);
8374 return result;
8377 /* Everything must be something allowed by is_gimple_addressable. */
8378 switch (TREE_CODE (exp))
8380 case INDIRECT_REF:
8381 /* This case will happen via recursion for &a->b. */
8382 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8384 case MEM_REF:
8386 tree tem = TREE_OPERAND (exp, 0);
8387 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8388 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8389 return expand_expr (tem, target, tmode, modifier);
8392 case TARGET_MEM_REF:
8393 return addr_for_mem_ref (exp, as, true);
8395 case CONST_DECL:
8396 /* Expand the initializer like constants above. */
8397 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8398 0, modifier), 0);
8399 if (modifier < EXPAND_SUM)
8400 result = force_operand (result, target);
8401 return result;
8403 case REALPART_EXPR:
8404 /* The real part of the complex number is always first, therefore
8405 the address is the same as the address of the parent object. */
8406 offset = 0;
8407 bitpos = 0;
8408 inner = TREE_OPERAND (exp, 0);
8409 break;
8411 case IMAGPART_EXPR:
8412 /* The imaginary part of the complex number is always second.
8413 The expression is therefore always offset by the size of the
8414 scalar type. */
8415 offset = 0;
8416 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8417 inner = TREE_OPERAND (exp, 0);
8418 break;
8420 case COMPOUND_LITERAL_EXPR:
8421 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8422 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8423 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8424 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8425 the initializers aren't gimplified. */
8426 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8427 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8428 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8429 target, tmode, modifier, as);
8430 /* FALLTHRU */
8431 default:
8432 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8433 expand_expr, as that can have various side effects; LABEL_DECLs for
8434 example, may not have their DECL_RTL set yet. Expand the rtl of
8435 CONSTRUCTORs too, which should yield a memory reference for the
8436 constructor's contents. Assume language specific tree nodes can
8437 be expanded in some interesting way. */
8438 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8439 if (DECL_P (exp)
8440 || TREE_CODE (exp) == CONSTRUCTOR
8441 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8443 result = expand_expr (exp, target, tmode,
8444 modifier == EXPAND_INITIALIZER
8445 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8447 /* If the DECL isn't in memory, then the DECL wasn't properly
8448 marked TREE_ADDRESSABLE, which will be either a front-end
8449 or a tree optimizer bug. */
8451 gcc_assert (MEM_P (result));
8452 result = XEXP (result, 0);
8454 /* ??? Is this needed anymore? */
8455 if (DECL_P (exp))
8456 TREE_USED (exp) = 1;
8458 if (modifier != EXPAND_INITIALIZER
8459 && modifier != EXPAND_CONST_ADDRESS
8460 && modifier != EXPAND_SUM)
8461 result = force_operand (result, target);
8462 return result;
8465 /* Pass FALSE as the last argument to get_inner_reference although
8466 we are expanding to RTL. The rationale is that we know how to
8467 handle "aligning nodes" here: we can just bypass them because
8468 they won't change the final object whose address will be returned
8469 (they actually exist only for that purpose). */
8470 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8471 &unsignedp, &reversep, &volatilep);
8472 break;
8475 /* We must have made progress. */
8476 gcc_assert (inner != exp);
8478 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8479 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8480 inner alignment, force the inner to be sufficiently aligned. */
8481 if (CONSTANT_CLASS_P (inner)
8482 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8484 inner = copy_node (inner);
8485 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8486 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8487 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8489 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8491 if (offset)
8493 rtx tmp;
8495 if (modifier != EXPAND_NORMAL)
8496 result = force_operand (result, NULL);
8497 tmp = expand_expr (offset, NULL_RTX, tmode,
8498 modifier == EXPAND_INITIALIZER
8499 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8501 /* expand_expr is allowed to return an object in a mode other
8502 than TMODE. If it did, we need to convert. */
8503 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8504 tmp = convert_modes (tmode, GET_MODE (tmp),
8505 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8506 result = convert_memory_address_addr_space (tmode, result, as);
8507 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8509 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8510 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8511 else
8513 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8514 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8515 1, OPTAB_LIB_WIDEN);
8519 if (maybe_ne (bitpos, 0))
8521 /* Someone beforehand should have rejected taking the address
8522 of an object that isn't byte-aligned. */
8523 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8524 result = convert_memory_address_addr_space (tmode, result, as);
8525 result = plus_constant (tmode, result, bytepos);
8526 if (modifier < EXPAND_SUM)
8527 result = force_operand (result, target);
8530 return result;
8533 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8534 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8536 static rtx
8537 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8538 enum expand_modifier modifier)
8540 addr_space_t as = ADDR_SPACE_GENERIC;
8541 scalar_int_mode address_mode = Pmode;
8542 scalar_int_mode pointer_mode = ptr_mode;
8543 machine_mode rmode;
8544 rtx result;
8546 /* Target mode of VOIDmode says "whatever's natural". */
8547 if (tmode == VOIDmode)
8548 tmode = TYPE_MODE (TREE_TYPE (exp));
8550 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8552 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8553 address_mode = targetm.addr_space.address_mode (as);
8554 pointer_mode = targetm.addr_space.pointer_mode (as);
8557 /* We can get called with some Weird Things if the user does silliness
8558 like "(short) &a". In that case, convert_memory_address won't do
8559 the right thing, so ignore the given target mode. */
8560 scalar_int_mode new_tmode = (tmode == pointer_mode
8561 ? pointer_mode
8562 : address_mode);
8564 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8565 new_tmode, modifier, as);
8567 /* Despite expand_expr claims concerning ignoring TMODE when not
8568 strictly convenient, stuff breaks if we don't honor it. Note
8569 that combined with the above, we only do this for pointer modes. */
8570 rmode = GET_MODE (result);
8571 if (rmode == VOIDmode)
8572 rmode = new_tmode;
8573 if (rmode != new_tmode)
8574 result = convert_memory_address_addr_space (new_tmode, result, as);
8576 return result;
8579 /* Generate code for computing CONSTRUCTOR EXP.
8580 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8581 is TRUE, instead of creating a temporary variable in memory
8582 NULL is returned and the caller needs to handle it differently. */
8584 static rtx
8585 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8586 bool avoid_temp_mem)
8588 tree type = TREE_TYPE (exp);
8589 machine_mode mode = TYPE_MODE (type);
8591 /* Try to avoid creating a temporary at all. This is possible
8592 if all of the initializer is zero.
8593 FIXME: try to handle all [0..255] initializers we can handle
8594 with memset. */
8595 if (TREE_STATIC (exp)
8596 && !TREE_ADDRESSABLE (exp)
8597 && target != 0 && mode == BLKmode
8598 && all_zeros_p (exp))
8600 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8601 return target;
8604 /* All elts simple constants => refer to a constant in memory. But
8605 if this is a non-BLKmode mode, let it store a field at a time
8606 since that should make a CONST_INT, CONST_WIDE_INT or
8607 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8608 use, it is best to store directly into the target unless the type
8609 is large enough that memcpy will be used. If we are making an
8610 initializer and all operands are constant, put it in memory as
8611 well.
8613 FIXME: Avoid trying to fill vector constructors piece-meal.
8614 Output them with output_constant_def below unless we're sure
8615 they're zeros. This should go away when vector initializers
8616 are treated like VECTOR_CST instead of arrays. */
8617 if ((TREE_STATIC (exp)
8618 && ((mode == BLKmode
8619 && ! (target != 0 && safe_from_p (target, exp, 1)))
8620 || TREE_ADDRESSABLE (exp)
8621 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8622 && (! can_move_by_pieces
8623 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8624 TYPE_ALIGN (type)))
8625 && ! mostly_zeros_p (exp))))
8626 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8627 && TREE_CONSTANT (exp)))
8629 rtx constructor;
8631 if (avoid_temp_mem)
8632 return NULL_RTX;
8634 constructor = expand_expr_constant (exp, 1, modifier);
8636 if (modifier != EXPAND_CONST_ADDRESS
8637 && modifier != EXPAND_INITIALIZER
8638 && modifier != EXPAND_SUM)
8639 constructor = validize_mem (constructor);
8641 return constructor;
8644 /* If the CTOR is available in static storage and not mostly
8645 zeros and we can move it by pieces prefer to do so since
8646 that's usually more efficient than performing a series of
8647 stores from immediates. */
8648 if (avoid_temp_mem
8649 && TREE_STATIC (exp)
8650 && TREE_CONSTANT (exp)
8651 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8652 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8653 TYPE_ALIGN (type))
8654 && ! mostly_zeros_p (exp))
8655 return NULL_RTX;
8657 /* Handle calls that pass values in multiple non-contiguous
8658 locations. The Irix 6 ABI has examples of this. */
8659 if (target == 0 || ! safe_from_p (target, exp, 1)
8660 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8661 /* Also make a temporary if the store is to volatile memory, to
8662 avoid individual accesses to aggregate members. */
8663 || (GET_CODE (target) == MEM
8664 && MEM_VOLATILE_P (target)
8665 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8667 if (avoid_temp_mem)
8668 return NULL_RTX;
8670 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8673 store_constructor (exp, target, 0, int_expr_size (exp), false);
8674 return target;
8678 /* expand_expr: generate code for computing expression EXP.
8679 An rtx for the computed value is returned. The value is never null.
8680 In the case of a void EXP, const0_rtx is returned.
8682 The value may be stored in TARGET if TARGET is nonzero.
8683 TARGET is just a suggestion; callers must assume that
8684 the rtx returned may not be the same as TARGET.
8686 If TARGET is CONST0_RTX, it means that the value will be ignored.
8688 If TMODE is not VOIDmode, it suggests generating the
8689 result in mode TMODE. But this is done only when convenient.
8690 Otherwise, TMODE is ignored and the value generated in its natural mode.
8691 TMODE is just a suggestion; callers must assume that
8692 the rtx returned may not have mode TMODE.
8694 Note that TARGET may have neither TMODE nor MODE. In that case, it
8695 probably will not be used.
8697 If MODIFIER is EXPAND_SUM then when EXP is an addition
8698 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8699 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8700 products as above, or REG or MEM, or constant.
8701 Ordinarily in such cases we would output mul or add instructions
8702 and then return a pseudo reg containing the sum.
8704 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8705 it also marks a label as absolutely required (it can't be dead).
8706 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8707 This is used for outputting expressions used in initializers.
8709 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8710 with a constant address even if that address is not normally legitimate.
8711 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8713 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8714 a call parameter. Such targets require special care as we haven't yet
8715 marked TARGET so that it's safe from being trashed by libcalls. We
8716 don't want to use TARGET for anything but the final result;
8717 Intermediate values must go elsewhere. Additionally, calls to
8718 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8720 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8721 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8722 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8723 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8724 recursively.
8725 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8726 then *ALT_RTL is set to TARGET (before legitimziation).
8728 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8729 In this case, we don't adjust a returned MEM rtx that wouldn't be
8730 sufficiently aligned for its mode; instead, it's up to the caller
8731 to deal with it afterwards. This is used to make sure that unaligned
8732 base objects for which out-of-bounds accesses are supported, for
8733 example record types with trailing arrays, aren't realigned behind
8734 the back of the caller.
8735 The normal operating mode is to pass FALSE for this parameter. */
8738 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8739 enum expand_modifier modifier, rtx *alt_rtl,
8740 bool inner_reference_p)
8742 rtx ret;
8744 /* Handle ERROR_MARK before anybody tries to access its type. */
8745 if (TREE_CODE (exp) == ERROR_MARK
8746 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8748 ret = CONST0_RTX (tmode);
8749 return ret ? ret : const0_rtx;
8752 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8753 inner_reference_p);
8754 return ret;
8757 /* Try to expand the conditional expression which is represented by
8758 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8759 return the rtl reg which represents the result. Otherwise return
8760 NULL_RTX. */
8762 static rtx
8763 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8764 tree treeop1 ATTRIBUTE_UNUSED,
8765 tree treeop2 ATTRIBUTE_UNUSED)
8767 rtx insn;
8768 rtx op00, op01, op1, op2;
8769 enum rtx_code comparison_code;
8770 machine_mode comparison_mode;
8771 gimple *srcstmt;
8772 rtx temp;
8773 tree type = TREE_TYPE (treeop1);
8774 int unsignedp = TYPE_UNSIGNED (type);
8775 machine_mode mode = TYPE_MODE (type);
8776 machine_mode orig_mode = mode;
8777 static bool expanding_cond_expr_using_cmove = false;
8779 /* Conditional move expansion can end up TERing two operands which,
8780 when recursively hitting conditional expressions can result in
8781 exponential behavior if the cmove expansion ultimatively fails.
8782 It's hardly profitable to TER a cmove into a cmove so avoid doing
8783 that by failing early if we end up recursing. */
8784 if (expanding_cond_expr_using_cmove)
8785 return NULL_RTX;
8787 /* If we cannot do a conditional move on the mode, try doing it
8788 with the promoted mode. */
8789 if (!can_conditionally_move_p (mode))
8791 mode = promote_mode (type, mode, &unsignedp);
8792 if (!can_conditionally_move_p (mode))
8793 return NULL_RTX;
8794 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8796 else
8797 temp = assign_temp (type, 0, 1);
8799 expanding_cond_expr_using_cmove = true;
8800 start_sequence ();
8801 expand_operands (treeop1, treeop2,
8802 temp, &op1, &op2, EXPAND_NORMAL);
8804 if (TREE_CODE (treeop0) == SSA_NAME
8805 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8807 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8808 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8809 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8810 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8811 comparison_mode = TYPE_MODE (type);
8812 unsignedp = TYPE_UNSIGNED (type);
8813 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8815 else if (COMPARISON_CLASS_P (treeop0))
8817 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8818 enum tree_code cmpcode = TREE_CODE (treeop0);
8819 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8820 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8821 unsignedp = TYPE_UNSIGNED (type);
8822 comparison_mode = TYPE_MODE (type);
8823 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8825 else
8827 op00 = expand_normal (treeop0);
8828 op01 = const0_rtx;
8829 comparison_code = NE;
8830 comparison_mode = GET_MODE (op00);
8831 if (comparison_mode == VOIDmode)
8832 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8834 expanding_cond_expr_using_cmove = false;
8836 if (GET_MODE (op1) != mode)
8837 op1 = gen_lowpart (mode, op1);
8839 if (GET_MODE (op2) != mode)
8840 op2 = gen_lowpart (mode, op2);
8842 /* Try to emit the conditional move. */
8843 insn = emit_conditional_move (temp, comparison_code,
8844 op00, op01, comparison_mode,
8845 op1, op2, mode,
8846 unsignedp);
8848 /* If we could do the conditional move, emit the sequence,
8849 and return. */
8850 if (insn)
8852 rtx_insn *seq = get_insns ();
8853 end_sequence ();
8854 emit_insn (seq);
8855 return convert_modes (orig_mode, mode, temp, 0);
8858 /* Otherwise discard the sequence and fall back to code with
8859 branches. */
8860 end_sequence ();
8861 return NULL_RTX;
8864 /* A helper function for expand_expr_real_2 to be used with a
8865 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8866 is nonzero, with alignment ALIGN in bits.
8867 Store the value at TARGET if possible (if TARGET is nonzero).
8868 Regardless of TARGET, we return the rtx for where the value is placed.
8869 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8870 then *ALT_RTL is set to TARGET (before legitimziation). */
8872 static rtx
8873 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8874 unsigned int align, rtx target, rtx *alt_rtl)
8876 enum insn_code icode;
8878 if ((icode = optab_handler (movmisalign_optab, mode))
8879 != CODE_FOR_nothing)
8881 class expand_operand ops[2];
8883 /* We've already validated the memory, and we're creating a
8884 new pseudo destination. The predicates really can't fail,
8885 nor can the generator. */
8886 create_output_operand (&ops[0], NULL_RTX, mode);
8887 create_fixed_operand (&ops[1], temp);
8888 expand_insn (icode, 2, ops);
8889 temp = ops[0].value;
8891 else if (targetm.slow_unaligned_access (mode, align))
8892 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8893 0, unsignedp, target,
8894 mode, mode, false, alt_rtl);
8895 return temp;
8898 /* Helper function of expand_expr_2, expand a division or modulo.
8899 op0 and op1 should be already expanded treeop0 and treeop1, using
8900 expand_operands. */
8902 static rtx
8903 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8904 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8906 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8907 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8908 if (SCALAR_INT_MODE_P (mode)
8909 && optimize >= 2
8910 && get_range_pos_neg (treeop0) == 1
8911 && get_range_pos_neg (treeop1) == 1)
8913 /* If both arguments are known to be positive when interpreted
8914 as signed, we can expand it as both signed and unsigned
8915 division or modulo. Choose the cheaper sequence in that case. */
8916 bool speed_p = optimize_insn_for_speed_p ();
8917 do_pending_stack_adjust ();
8918 start_sequence ();
8919 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8920 rtx_insn *uns_insns = get_insns ();
8921 end_sequence ();
8922 start_sequence ();
8923 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8924 rtx_insn *sgn_insns = get_insns ();
8925 end_sequence ();
8926 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8927 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8929 /* If costs are the same then use as tie breaker the other other
8930 factor. */
8931 if (uns_cost == sgn_cost)
8933 uns_cost = seq_cost (uns_insns, !speed_p);
8934 sgn_cost = seq_cost (sgn_insns, !speed_p);
8937 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8939 emit_insn (uns_insns);
8940 return uns_ret;
8942 emit_insn (sgn_insns);
8943 return sgn_ret;
8945 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8949 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8950 enum expand_modifier modifier)
8952 rtx op0, op1, op2, temp;
8953 rtx_code_label *lab;
8954 tree type;
8955 int unsignedp;
8956 machine_mode mode;
8957 scalar_int_mode int_mode;
8958 enum tree_code code = ops->code;
8959 optab this_optab;
8960 rtx subtarget, original_target;
8961 int ignore;
8962 bool reduce_bit_field;
8963 location_t loc = ops->location;
8964 tree treeop0, treeop1, treeop2;
8965 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8966 ? reduce_to_bit_field_precision ((expr), \
8967 target, \
8968 type) \
8969 : (expr))
8971 type = ops->type;
8972 mode = TYPE_MODE (type);
8973 unsignedp = TYPE_UNSIGNED (type);
8975 treeop0 = ops->op0;
8976 treeop1 = ops->op1;
8977 treeop2 = ops->op2;
8979 /* We should be called only on simple (binary or unary) expressions,
8980 exactly those that are valid in gimple expressions that aren't
8981 GIMPLE_SINGLE_RHS (or invalid). */
8982 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8983 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8984 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8986 ignore = (target == const0_rtx
8987 || ((CONVERT_EXPR_CODE_P (code)
8988 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8989 && TREE_CODE (type) == VOID_TYPE));
8991 /* We should be called only if we need the result. */
8992 gcc_assert (!ignore);
8994 /* An operation in what may be a bit-field type needs the
8995 result to be reduced to the precision of the bit-field type,
8996 which is narrower than that of the type's mode. */
8997 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8998 && !type_has_mode_precision_p (type));
9000 if (reduce_bit_field
9001 && (modifier == EXPAND_STACK_PARM
9002 || (target && GET_MODE (target) != mode)))
9003 target = 0;
9005 /* Use subtarget as the target for operand 0 of a binary operation. */
9006 subtarget = get_subtarget (target);
9007 original_target = target;
9009 switch (code)
9011 case NON_LVALUE_EXPR:
9012 case PAREN_EXPR:
9013 CASE_CONVERT:
9014 if (treeop0 == error_mark_node)
9015 return const0_rtx;
9017 if (TREE_CODE (type) == UNION_TYPE)
9019 tree valtype = TREE_TYPE (treeop0);
9021 /* If both input and output are BLKmode, this conversion isn't doing
9022 anything except possibly changing memory attribute. */
9023 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9025 rtx result = expand_expr (treeop0, target, tmode,
9026 modifier);
9028 result = copy_rtx (result);
9029 set_mem_attributes (result, type, 0);
9030 return result;
9033 if (target == 0)
9035 if (TYPE_MODE (type) != BLKmode)
9036 target = gen_reg_rtx (TYPE_MODE (type));
9037 else
9038 target = assign_temp (type, 1, 1);
9041 if (MEM_P (target))
9042 /* Store data into beginning of memory target. */
9043 store_expr (treeop0,
9044 adjust_address (target, TYPE_MODE (valtype), 0),
9045 modifier == EXPAND_STACK_PARM,
9046 false, TYPE_REVERSE_STORAGE_ORDER (type));
9048 else
9050 gcc_assert (REG_P (target)
9051 && !TYPE_REVERSE_STORAGE_ORDER (type));
9053 /* Store this field into a union of the proper type. */
9054 poly_uint64 op0_size
9055 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9056 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9057 store_field (target,
9058 /* The conversion must be constructed so that
9059 we know at compile time how many bits
9060 to preserve. */
9061 ordered_min (op0_size, union_size),
9062 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9063 false, false);
9066 /* Return the entire union. */
9067 return target;
9070 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9072 op0 = expand_expr (treeop0, target, VOIDmode,
9073 modifier);
9075 /* If the signedness of the conversion differs and OP0 is
9076 a promoted SUBREG, clear that indication since we now
9077 have to do the proper extension. */
9078 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9079 && GET_CODE (op0) == SUBREG)
9080 SUBREG_PROMOTED_VAR_P (op0) = 0;
9082 return REDUCE_BIT_FIELD (op0);
9085 op0 = expand_expr (treeop0, NULL_RTX, mode,
9086 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9087 if (GET_MODE (op0) == mode)
9090 /* If OP0 is a constant, just convert it into the proper mode. */
9091 else if (CONSTANT_P (op0))
9093 tree inner_type = TREE_TYPE (treeop0);
9094 machine_mode inner_mode = GET_MODE (op0);
9096 if (inner_mode == VOIDmode)
9097 inner_mode = TYPE_MODE (inner_type);
9099 if (modifier == EXPAND_INITIALIZER)
9100 op0 = lowpart_subreg (mode, op0, inner_mode);
9101 else
9102 op0= convert_modes (mode, inner_mode, op0,
9103 TYPE_UNSIGNED (inner_type));
9106 else if (modifier == EXPAND_INITIALIZER)
9107 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9108 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9110 else if (target == 0)
9111 op0 = convert_to_mode (mode, op0,
9112 TYPE_UNSIGNED (TREE_TYPE
9113 (treeop0)));
9114 else
9116 convert_move (target, op0,
9117 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9118 op0 = target;
9121 return REDUCE_BIT_FIELD (op0);
9123 case ADDR_SPACE_CONVERT_EXPR:
9125 tree treeop0_type = TREE_TYPE (treeop0);
9127 gcc_assert (POINTER_TYPE_P (type));
9128 gcc_assert (POINTER_TYPE_P (treeop0_type));
9130 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9131 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9133 /* Conversions between pointers to the same address space should
9134 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9135 gcc_assert (as_to != as_from);
9137 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9139 /* Ask target code to handle conversion between pointers
9140 to overlapping address spaces. */
9141 if (targetm.addr_space.subset_p (as_to, as_from)
9142 || targetm.addr_space.subset_p (as_from, as_to))
9144 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9146 else
9148 /* For disjoint address spaces, converting anything but a null
9149 pointer invokes undefined behavior. We truncate or extend the
9150 value as if we'd converted via integers, which handles 0 as
9151 required, and all others as the programmer likely expects. */
9152 #ifndef POINTERS_EXTEND_UNSIGNED
9153 const int POINTERS_EXTEND_UNSIGNED = 1;
9154 #endif
9155 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9156 op0, POINTERS_EXTEND_UNSIGNED);
9158 gcc_assert (op0);
9159 return op0;
9162 case POINTER_PLUS_EXPR:
9163 /* Even though the sizetype mode and the pointer's mode can be different
9164 expand is able to handle this correctly and get the correct result out
9165 of the PLUS_EXPR code. */
9166 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9167 if sizetype precision is smaller than pointer precision. */
9168 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9169 treeop1 = fold_convert_loc (loc, type,
9170 fold_convert_loc (loc, ssizetype,
9171 treeop1));
9172 /* If sizetype precision is larger than pointer precision, truncate the
9173 offset to have matching modes. */
9174 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9175 treeop1 = fold_convert_loc (loc, type, treeop1);
9176 /* FALLTHRU */
9178 case PLUS_EXPR:
9179 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9180 something else, make sure we add the register to the constant and
9181 then to the other thing. This case can occur during strength
9182 reduction and doing it this way will produce better code if the
9183 frame pointer or argument pointer is eliminated.
9185 fold-const.c will ensure that the constant is always in the inner
9186 PLUS_EXPR, so the only case we need to do anything about is if
9187 sp, ap, or fp is our second argument, in which case we must swap
9188 the innermost first argument and our second argument. */
9190 if (TREE_CODE (treeop0) == PLUS_EXPR
9191 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9192 && VAR_P (treeop1)
9193 && (DECL_RTL (treeop1) == frame_pointer_rtx
9194 || DECL_RTL (treeop1) == stack_pointer_rtx
9195 || DECL_RTL (treeop1) == arg_pointer_rtx))
9197 gcc_unreachable ();
9200 /* If the result is to be ptr_mode and we are adding an integer to
9201 something, we might be forming a constant. So try to use
9202 plus_constant. If it produces a sum and we can't accept it,
9203 use force_operand. This allows P = &ARR[const] to generate
9204 efficient code on machines where a SYMBOL_REF is not a valid
9205 address.
9207 If this is an EXPAND_SUM call, always return the sum. */
9208 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9209 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9211 if (modifier == EXPAND_STACK_PARM)
9212 target = 0;
9213 if (TREE_CODE (treeop0) == INTEGER_CST
9214 && HWI_COMPUTABLE_MODE_P (mode)
9215 && TREE_CONSTANT (treeop1))
9217 rtx constant_part;
9218 HOST_WIDE_INT wc;
9219 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9221 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9222 EXPAND_SUM);
9223 /* Use wi::shwi to ensure that the constant is
9224 truncated according to the mode of OP1, then sign extended
9225 to a HOST_WIDE_INT. Using the constant directly can result
9226 in non-canonical RTL in a 64x32 cross compile. */
9227 wc = TREE_INT_CST_LOW (treeop0);
9228 constant_part =
9229 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9230 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9231 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9232 op1 = force_operand (op1, target);
9233 return REDUCE_BIT_FIELD (op1);
9236 else if (TREE_CODE (treeop1) == INTEGER_CST
9237 && HWI_COMPUTABLE_MODE_P (mode)
9238 && TREE_CONSTANT (treeop0))
9240 rtx constant_part;
9241 HOST_WIDE_INT wc;
9242 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9244 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9245 (modifier == EXPAND_INITIALIZER
9246 ? EXPAND_INITIALIZER : EXPAND_SUM));
9247 if (! CONSTANT_P (op0))
9249 op1 = expand_expr (treeop1, NULL_RTX,
9250 VOIDmode, modifier);
9251 /* Return a PLUS if modifier says it's OK. */
9252 if (modifier == EXPAND_SUM
9253 || modifier == EXPAND_INITIALIZER)
9254 return simplify_gen_binary (PLUS, mode, op0, op1);
9255 goto binop2;
9257 /* Use wi::shwi to ensure that the constant is
9258 truncated according to the mode of OP1, then sign extended
9259 to a HOST_WIDE_INT. Using the constant directly can result
9260 in non-canonical RTL in a 64x32 cross compile. */
9261 wc = TREE_INT_CST_LOW (treeop1);
9262 constant_part
9263 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9264 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9265 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9266 op0 = force_operand (op0, target);
9267 return REDUCE_BIT_FIELD (op0);
9271 /* Use TER to expand pointer addition of a negated value
9272 as pointer subtraction. */
9273 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9274 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9275 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9276 && TREE_CODE (treeop1) == SSA_NAME
9277 && TYPE_MODE (TREE_TYPE (treeop0))
9278 == TYPE_MODE (TREE_TYPE (treeop1)))
9280 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9281 if (def)
9283 treeop1 = gimple_assign_rhs1 (def);
9284 code = MINUS_EXPR;
9285 goto do_minus;
9289 /* No sense saving up arithmetic to be done
9290 if it's all in the wrong mode to form part of an address.
9291 And force_operand won't know whether to sign-extend or
9292 zero-extend. */
9293 if (modifier != EXPAND_INITIALIZER
9294 && (modifier != EXPAND_SUM || mode != ptr_mode))
9296 expand_operands (treeop0, treeop1,
9297 subtarget, &op0, &op1, modifier);
9298 if (op0 == const0_rtx)
9299 return op1;
9300 if (op1 == const0_rtx)
9301 return op0;
9302 goto binop2;
9305 expand_operands (treeop0, treeop1,
9306 subtarget, &op0, &op1, modifier);
9307 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9309 case MINUS_EXPR:
9310 case POINTER_DIFF_EXPR:
9311 do_minus:
9312 /* For initializers, we are allowed to return a MINUS of two
9313 symbolic constants. Here we handle all cases when both operands
9314 are constant. */
9315 /* Handle difference of two symbolic constants,
9316 for the sake of an initializer. */
9317 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9318 && really_constant_p (treeop0)
9319 && really_constant_p (treeop1))
9321 expand_operands (treeop0, treeop1,
9322 NULL_RTX, &op0, &op1, modifier);
9323 return simplify_gen_binary (MINUS, mode, op0, op1);
9326 /* No sense saving up arithmetic to be done
9327 if it's all in the wrong mode to form part of an address.
9328 And force_operand won't know whether to sign-extend or
9329 zero-extend. */
9330 if (modifier != EXPAND_INITIALIZER
9331 && (modifier != EXPAND_SUM || mode != ptr_mode))
9332 goto binop;
9334 expand_operands (treeop0, treeop1,
9335 subtarget, &op0, &op1, modifier);
9337 /* Convert A - const to A + (-const). */
9338 if (CONST_INT_P (op1))
9340 op1 = negate_rtx (mode, op1);
9341 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9344 goto binop2;
9346 case WIDEN_MULT_PLUS_EXPR:
9347 case WIDEN_MULT_MINUS_EXPR:
9348 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9349 op2 = expand_normal (treeop2);
9350 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9351 target, unsignedp);
9352 return target;
9354 case WIDEN_PLUS_EXPR:
9355 case WIDEN_MINUS_EXPR:
9356 case WIDEN_MULT_EXPR:
9357 /* If first operand is constant, swap them.
9358 Thus the following special case checks need only
9359 check the second operand. */
9360 if (TREE_CODE (treeop0) == INTEGER_CST)
9361 std::swap (treeop0, treeop1);
9363 /* First, check if we have a multiplication of one signed and one
9364 unsigned operand. */
9365 if (TREE_CODE (treeop1) != INTEGER_CST
9366 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9367 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9369 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9370 this_optab = usmul_widen_optab;
9371 if (find_widening_optab_handler (this_optab, mode, innermode)
9372 != CODE_FOR_nothing)
9374 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9375 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9376 EXPAND_NORMAL);
9377 else
9378 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9379 EXPAND_NORMAL);
9380 /* op0 and op1 might still be constant, despite the above
9381 != INTEGER_CST check. Handle it. */
9382 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9384 op0 = convert_modes (mode, innermode, op0, true);
9385 op1 = convert_modes (mode, innermode, op1, false);
9386 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9387 target, unsignedp));
9389 goto binop3;
9392 /* Check for a multiplication with matching signedness. */
9393 else if ((TREE_CODE (treeop1) == INTEGER_CST
9394 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9395 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9396 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9398 tree op0type = TREE_TYPE (treeop0);
9399 machine_mode innermode = TYPE_MODE (op0type);
9400 bool zextend_p = TYPE_UNSIGNED (op0type);
9401 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9402 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9404 if (TREE_CODE (treeop0) != INTEGER_CST)
9406 if (find_widening_optab_handler (this_optab, mode, innermode)
9407 != CODE_FOR_nothing)
9409 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9410 EXPAND_NORMAL);
9411 /* op0 and op1 might still be constant, despite the above
9412 != INTEGER_CST check. Handle it. */
9413 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9415 widen_mult_const:
9416 op0 = convert_modes (mode, innermode, op0, zextend_p);
9418 = convert_modes (mode, innermode, op1,
9419 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9420 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9421 target,
9422 unsignedp));
9424 temp = expand_widening_mult (mode, op0, op1, target,
9425 unsignedp, this_optab);
9426 return REDUCE_BIT_FIELD (temp);
9428 if (find_widening_optab_handler (other_optab, mode, innermode)
9429 != CODE_FOR_nothing
9430 && innermode == word_mode)
9432 rtx htem, hipart;
9433 op0 = expand_normal (treeop0);
9434 op1 = expand_normal (treeop1);
9435 /* op0 and op1 might be constants, despite the above
9436 != INTEGER_CST check. Handle it. */
9437 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9438 goto widen_mult_const;
9439 temp = expand_binop (mode, other_optab, op0, op1, target,
9440 unsignedp, OPTAB_LIB_WIDEN);
9441 hipart = gen_highpart (word_mode, temp);
9442 htem = expand_mult_highpart_adjust (word_mode, hipart,
9443 op0, op1, hipart,
9444 zextend_p);
9445 if (htem != hipart)
9446 emit_move_insn (hipart, htem);
9447 return REDUCE_BIT_FIELD (temp);
9451 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9452 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9453 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9454 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9456 case MULT_EXPR:
9457 /* If this is a fixed-point operation, then we cannot use the code
9458 below because "expand_mult" doesn't support sat/no-sat fixed-point
9459 multiplications. */
9460 if (ALL_FIXED_POINT_MODE_P (mode))
9461 goto binop;
9463 /* If first operand is constant, swap them.
9464 Thus the following special case checks need only
9465 check the second operand. */
9466 if (TREE_CODE (treeop0) == INTEGER_CST)
9467 std::swap (treeop0, treeop1);
9469 /* Attempt to return something suitable for generating an
9470 indexed address, for machines that support that. */
9472 if (modifier == EXPAND_SUM && mode == ptr_mode
9473 && tree_fits_shwi_p (treeop1))
9475 tree exp1 = treeop1;
9477 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9478 EXPAND_SUM);
9480 if (!REG_P (op0))
9481 op0 = force_operand (op0, NULL_RTX);
9482 if (!REG_P (op0))
9483 op0 = copy_to_mode_reg (mode, op0);
9485 op1 = gen_int_mode (tree_to_shwi (exp1),
9486 TYPE_MODE (TREE_TYPE (exp1)));
9487 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9490 if (modifier == EXPAND_STACK_PARM)
9491 target = 0;
9493 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9495 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9496 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9497 if (def_stmt0
9498 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9499 def_stmt0 = NULL;
9500 if (def_stmt1
9501 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9502 def_stmt1 = NULL;
9504 if (def_stmt0 || def_stmt1)
9506 /* X / Y * Y can be expanded as X - X % Y too.
9507 Choose the cheaper sequence of those two. */
9508 if (def_stmt0)
9509 treeop0 = gimple_assign_rhs1 (def_stmt0);
9510 else
9512 treeop1 = treeop0;
9513 treeop0 = gimple_assign_rhs1 (def_stmt1);
9515 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9516 EXPAND_NORMAL);
9517 bool speed_p = optimize_insn_for_speed_p ();
9518 do_pending_stack_adjust ();
9519 start_sequence ();
9520 rtx divmul_ret
9521 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9522 op0, op1, NULL_RTX, unsignedp);
9523 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9524 unsignedp);
9525 rtx_insn *divmul_insns = get_insns ();
9526 end_sequence ();
9527 start_sequence ();
9528 rtx modsub_ret
9529 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9530 op0, op1, NULL_RTX, unsignedp);
9531 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9532 optab_default);
9533 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9534 target, unsignedp, OPTAB_LIB_WIDEN);
9535 rtx_insn *modsub_insns = get_insns ();
9536 end_sequence ();
9537 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9538 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9539 /* If costs are the same then use as tie breaker the other other
9540 factor. */
9541 if (divmul_cost == modsub_cost)
9543 divmul_cost = seq_cost (divmul_insns, !speed_p);
9544 modsub_cost = seq_cost (modsub_insns, !speed_p);
9547 if (divmul_cost <= modsub_cost)
9549 emit_insn (divmul_insns);
9550 return REDUCE_BIT_FIELD (divmul_ret);
9552 emit_insn (modsub_insns);
9553 return REDUCE_BIT_FIELD (modsub_ret);
9557 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9558 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9560 case TRUNC_MOD_EXPR:
9561 case FLOOR_MOD_EXPR:
9562 case CEIL_MOD_EXPR:
9563 case ROUND_MOD_EXPR:
9565 case TRUNC_DIV_EXPR:
9566 case FLOOR_DIV_EXPR:
9567 case CEIL_DIV_EXPR:
9568 case ROUND_DIV_EXPR:
9569 case EXACT_DIV_EXPR:
9570 /* If this is a fixed-point operation, then we cannot use the code
9571 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9572 divisions. */
9573 if (ALL_FIXED_POINT_MODE_P (mode))
9574 goto binop;
9576 if (modifier == EXPAND_STACK_PARM)
9577 target = 0;
9578 /* Possible optimization: compute the dividend with EXPAND_SUM
9579 then if the divisor is constant can optimize the case
9580 where some terms of the dividend have coeffs divisible by it. */
9581 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9582 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9583 target, unsignedp);
9585 case RDIV_EXPR:
9586 goto binop;
9588 case MULT_HIGHPART_EXPR:
9589 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9590 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9591 gcc_assert (temp);
9592 return temp;
9594 case FIXED_CONVERT_EXPR:
9595 op0 = expand_normal (treeop0);
9596 if (target == 0 || modifier == EXPAND_STACK_PARM)
9597 target = gen_reg_rtx (mode);
9599 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9600 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9601 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9602 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9603 else
9604 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9605 return target;
9607 case FIX_TRUNC_EXPR:
9608 op0 = expand_normal (treeop0);
9609 if (target == 0 || modifier == EXPAND_STACK_PARM)
9610 target = gen_reg_rtx (mode);
9611 expand_fix (target, op0, unsignedp);
9612 return target;
9614 case FLOAT_EXPR:
9615 op0 = expand_normal (treeop0);
9616 if (target == 0 || modifier == EXPAND_STACK_PARM)
9617 target = gen_reg_rtx (mode);
9618 /* expand_float can't figure out what to do if FROM has VOIDmode.
9619 So give it the correct mode. With -O, cse will optimize this. */
9620 if (GET_MODE (op0) == VOIDmode)
9621 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9622 op0);
9623 expand_float (target, op0,
9624 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9625 return target;
9627 case NEGATE_EXPR:
9628 op0 = expand_expr (treeop0, subtarget,
9629 VOIDmode, EXPAND_NORMAL);
9630 if (modifier == EXPAND_STACK_PARM)
9631 target = 0;
9632 temp = expand_unop (mode,
9633 optab_for_tree_code (NEGATE_EXPR, type,
9634 optab_default),
9635 op0, target, 0);
9636 gcc_assert (temp);
9637 return REDUCE_BIT_FIELD (temp);
9639 case ABS_EXPR:
9640 case ABSU_EXPR:
9641 op0 = expand_expr (treeop0, subtarget,
9642 VOIDmode, EXPAND_NORMAL);
9643 if (modifier == EXPAND_STACK_PARM)
9644 target = 0;
9646 /* ABS_EXPR is not valid for complex arguments. */
9647 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9648 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9650 /* Unsigned abs is simply the operand. Testing here means we don't
9651 risk generating incorrect code below. */
9652 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9653 return op0;
9655 return expand_abs (mode, op0, target, unsignedp,
9656 safe_from_p (target, treeop0, 1));
9658 case MAX_EXPR:
9659 case MIN_EXPR:
9660 target = original_target;
9661 if (target == 0
9662 || modifier == EXPAND_STACK_PARM
9663 || (MEM_P (target) && MEM_VOLATILE_P (target))
9664 || GET_MODE (target) != mode
9665 || (REG_P (target)
9666 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9667 target = gen_reg_rtx (mode);
9668 expand_operands (treeop0, treeop1,
9669 target, &op0, &op1, EXPAND_NORMAL);
9671 /* First try to do it with a special MIN or MAX instruction.
9672 If that does not win, use a conditional jump to select the proper
9673 value. */
9674 this_optab = optab_for_tree_code (code, type, optab_default);
9675 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9676 OPTAB_WIDEN);
9677 if (temp != 0)
9678 return temp;
9680 if (VECTOR_TYPE_P (type))
9681 gcc_unreachable ();
9683 /* At this point, a MEM target is no longer useful; we will get better
9684 code without it. */
9686 if (! REG_P (target))
9687 target = gen_reg_rtx (mode);
9689 /* If op1 was placed in target, swap op0 and op1. */
9690 if (target != op0 && target == op1)
9691 std::swap (op0, op1);
9693 /* We generate better code and avoid problems with op1 mentioning
9694 target by forcing op1 into a pseudo if it isn't a constant. */
9695 if (! CONSTANT_P (op1))
9696 op1 = force_reg (mode, op1);
9699 enum rtx_code comparison_code;
9700 rtx cmpop1 = op1;
9702 if (code == MAX_EXPR)
9703 comparison_code = unsignedp ? GEU : GE;
9704 else
9705 comparison_code = unsignedp ? LEU : LE;
9707 /* Canonicalize to comparisons against 0. */
9708 if (op1 == const1_rtx)
9710 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9711 or (a != 0 ? a : 1) for unsigned.
9712 For MIN we are safe converting (a <= 1 ? a : 1)
9713 into (a <= 0 ? a : 1) */
9714 cmpop1 = const0_rtx;
9715 if (code == MAX_EXPR)
9716 comparison_code = unsignedp ? NE : GT;
9718 if (op1 == constm1_rtx && !unsignedp)
9720 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9721 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9722 cmpop1 = const0_rtx;
9723 if (code == MIN_EXPR)
9724 comparison_code = LT;
9727 /* Use a conditional move if possible. */
9728 if (can_conditionally_move_p (mode))
9730 rtx insn;
9732 start_sequence ();
9734 /* Try to emit the conditional move. */
9735 insn = emit_conditional_move (target, comparison_code,
9736 op0, cmpop1, mode,
9737 op0, op1, mode,
9738 unsignedp);
9740 /* If we could do the conditional move, emit the sequence,
9741 and return. */
9742 if (insn)
9744 rtx_insn *seq = get_insns ();
9745 end_sequence ();
9746 emit_insn (seq);
9747 return target;
9750 /* Otherwise discard the sequence and fall back to code with
9751 branches. */
9752 end_sequence ();
9755 if (target != op0)
9756 emit_move_insn (target, op0);
9758 lab = gen_label_rtx ();
9759 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9760 unsignedp, mode, NULL_RTX, NULL, lab,
9761 profile_probability::uninitialized ());
9763 emit_move_insn (target, op1);
9764 emit_label (lab);
9765 return target;
9767 case BIT_NOT_EXPR:
9768 op0 = expand_expr (treeop0, subtarget,
9769 VOIDmode, EXPAND_NORMAL);
9770 if (modifier == EXPAND_STACK_PARM)
9771 target = 0;
9772 /* In case we have to reduce the result to bitfield precision
9773 for unsigned bitfield expand this as XOR with a proper constant
9774 instead. */
9775 if (reduce_bit_field && TYPE_UNSIGNED (type))
9777 int_mode = SCALAR_INT_TYPE_MODE (type);
9778 wide_int mask = wi::mask (TYPE_PRECISION (type),
9779 false, GET_MODE_PRECISION (int_mode));
9781 temp = expand_binop (int_mode, xor_optab, op0,
9782 immed_wide_int_const (mask, int_mode),
9783 target, 1, OPTAB_LIB_WIDEN);
9785 else
9786 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9787 gcc_assert (temp);
9788 return temp;
9790 /* ??? Can optimize bitwise operations with one arg constant.
9791 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9792 and (a bitwise1 b) bitwise2 b (etc)
9793 but that is probably not worth while. */
9795 case BIT_AND_EXPR:
9796 case BIT_IOR_EXPR:
9797 case BIT_XOR_EXPR:
9798 goto binop;
9800 case LROTATE_EXPR:
9801 case RROTATE_EXPR:
9802 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9803 || type_has_mode_precision_p (type));
9804 /* fall through */
9806 case LSHIFT_EXPR:
9807 case RSHIFT_EXPR:
9809 /* If this is a fixed-point operation, then we cannot use the code
9810 below because "expand_shift" doesn't support sat/no-sat fixed-point
9811 shifts. */
9812 if (ALL_FIXED_POINT_MODE_P (mode))
9813 goto binop;
9815 if (! safe_from_p (subtarget, treeop1, 1))
9816 subtarget = 0;
9817 if (modifier == EXPAND_STACK_PARM)
9818 target = 0;
9819 op0 = expand_expr (treeop0, subtarget,
9820 VOIDmode, EXPAND_NORMAL);
9822 /* Left shift optimization when shifting across word_size boundary.
9824 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9825 there isn't native instruction to support this wide mode
9826 left shift. Given below scenario:
9828 Type A = (Type) B << C
9830 |< T >|
9831 | dest_high | dest_low |
9833 | word_size |
9835 If the shift amount C caused we shift B to across the word
9836 size boundary, i.e part of B shifted into high half of
9837 destination register, and part of B remains in the low
9838 half, then GCC will use the following left shift expand
9839 logic:
9841 1. Initialize dest_low to B.
9842 2. Initialize every bit of dest_high to the sign bit of B.
9843 3. Logic left shift dest_low by C bit to finalize dest_low.
9844 The value of dest_low before this shift is kept in a temp D.
9845 4. Logic left shift dest_high by C.
9846 5. Logic right shift D by (word_size - C).
9847 6. Or the result of 4 and 5 to finalize dest_high.
9849 While, by checking gimple statements, if operand B is
9850 coming from signed extension, then we can simplify above
9851 expand logic into:
9853 1. dest_high = src_low >> (word_size - C).
9854 2. dest_low = src_low << C.
9856 We can use one arithmetic right shift to finish all the
9857 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9858 needed from 6 into 2.
9860 The case is similar for zero extension, except that we
9861 initialize dest_high to zero rather than copies of the sign
9862 bit from B. Furthermore, we need to use a logical right shift
9863 in this case.
9865 The choice of sign-extension versus zero-extension is
9866 determined entirely by whether or not B is signed and is
9867 independent of the current setting of unsignedp. */
9869 temp = NULL_RTX;
9870 if (code == LSHIFT_EXPR
9871 && target
9872 && REG_P (target)
9873 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9874 && mode == int_mode
9875 && TREE_CONSTANT (treeop1)
9876 && TREE_CODE (treeop0) == SSA_NAME)
9878 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9879 if (is_gimple_assign (def)
9880 && gimple_assign_rhs_code (def) == NOP_EXPR)
9882 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9883 (TREE_TYPE (gimple_assign_rhs1 (def)));
9885 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9886 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9887 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9888 >= GET_MODE_BITSIZE (word_mode)))
9890 rtx_insn *seq, *seq_old;
9891 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9892 int_mode);
9893 bool extend_unsigned
9894 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9895 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9896 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9897 rtx dest_high = simplify_gen_subreg (word_mode, target,
9898 int_mode, high_off);
9899 HOST_WIDE_INT ramount = (BITS_PER_WORD
9900 - TREE_INT_CST_LOW (treeop1));
9901 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9903 start_sequence ();
9904 /* dest_high = src_low >> (word_size - C). */
9905 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9906 rshift, dest_high,
9907 extend_unsigned);
9908 if (temp != dest_high)
9909 emit_move_insn (dest_high, temp);
9911 /* dest_low = src_low << C. */
9912 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9913 treeop1, dest_low, unsignedp);
9914 if (temp != dest_low)
9915 emit_move_insn (dest_low, temp);
9917 seq = get_insns ();
9918 end_sequence ();
9919 temp = target ;
9921 if (have_insn_for (ASHIFT, int_mode))
9923 bool speed_p = optimize_insn_for_speed_p ();
9924 start_sequence ();
9925 rtx ret_old = expand_variable_shift (code, int_mode,
9926 op0, treeop1,
9927 target,
9928 unsignedp);
9930 seq_old = get_insns ();
9931 end_sequence ();
9932 if (seq_cost (seq, speed_p)
9933 >= seq_cost (seq_old, speed_p))
9935 seq = seq_old;
9936 temp = ret_old;
9939 emit_insn (seq);
9944 if (temp == NULL_RTX)
9945 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9946 unsignedp);
9947 if (code == LSHIFT_EXPR)
9948 temp = REDUCE_BIT_FIELD (temp);
9949 return temp;
9952 /* Could determine the answer when only additive constants differ. Also,
9953 the addition of one can be handled by changing the condition. */
9954 case LT_EXPR:
9955 case LE_EXPR:
9956 case GT_EXPR:
9957 case GE_EXPR:
9958 case EQ_EXPR:
9959 case NE_EXPR:
9960 case UNORDERED_EXPR:
9961 case ORDERED_EXPR:
9962 case UNLT_EXPR:
9963 case UNLE_EXPR:
9964 case UNGT_EXPR:
9965 case UNGE_EXPR:
9966 case UNEQ_EXPR:
9967 case LTGT_EXPR:
9969 temp = do_store_flag (ops,
9970 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9971 tmode != VOIDmode ? tmode : mode);
9972 if (temp)
9973 return temp;
9975 /* Use a compare and a jump for BLKmode comparisons, or for function
9976 type comparisons is have_canonicalize_funcptr_for_compare. */
9978 if ((target == 0
9979 || modifier == EXPAND_STACK_PARM
9980 || ! safe_from_p (target, treeop0, 1)
9981 || ! safe_from_p (target, treeop1, 1)
9982 /* Make sure we don't have a hard reg (such as function's return
9983 value) live across basic blocks, if not optimizing. */
9984 || (!optimize && REG_P (target)
9985 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9986 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9988 emit_move_insn (target, const0_rtx);
9990 rtx_code_label *lab1 = gen_label_rtx ();
9991 jumpifnot_1 (code, treeop0, treeop1, lab1,
9992 profile_probability::uninitialized ());
9994 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9995 emit_move_insn (target, constm1_rtx);
9996 else
9997 emit_move_insn (target, const1_rtx);
9999 emit_label (lab1);
10000 return target;
10002 case COMPLEX_EXPR:
10003 /* Get the rtx code of the operands. */
10004 op0 = expand_normal (treeop0);
10005 op1 = expand_normal (treeop1);
10007 if (!target)
10008 target = gen_reg_rtx (TYPE_MODE (type));
10009 else
10010 /* If target overlaps with op1, then either we need to force
10011 op1 into a pseudo (if target also overlaps with op0),
10012 or write the complex parts in reverse order. */
10013 switch (GET_CODE (target))
10015 case CONCAT:
10016 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10018 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10020 complex_expr_force_op1:
10021 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10022 emit_move_insn (temp, op1);
10023 op1 = temp;
10024 break;
10026 complex_expr_swap_order:
10027 /* Move the imaginary (op1) and real (op0) parts to their
10028 location. */
10029 write_complex_part (target, op1, true);
10030 write_complex_part (target, op0, false);
10032 return target;
10034 break;
10035 case MEM:
10036 temp = adjust_address_nv (target,
10037 GET_MODE_INNER (GET_MODE (target)), 0);
10038 if (reg_overlap_mentioned_p (temp, op1))
10040 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10041 temp = adjust_address_nv (target, imode,
10042 GET_MODE_SIZE (imode));
10043 if (reg_overlap_mentioned_p (temp, op0))
10044 goto complex_expr_force_op1;
10045 goto complex_expr_swap_order;
10047 break;
10048 default:
10049 if (reg_overlap_mentioned_p (target, op1))
10051 if (reg_overlap_mentioned_p (target, op0))
10052 goto complex_expr_force_op1;
10053 goto complex_expr_swap_order;
10055 break;
10058 /* Move the real (op0) and imaginary (op1) parts to their location. */
10059 write_complex_part (target, op0, false);
10060 write_complex_part (target, op1, true);
10062 return target;
10064 case WIDEN_SUM_EXPR:
10066 tree oprnd0 = treeop0;
10067 tree oprnd1 = treeop1;
10069 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10070 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10071 target, unsignedp);
10072 return target;
10075 case VEC_UNPACK_HI_EXPR:
10076 case VEC_UNPACK_LO_EXPR:
10077 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10078 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10080 op0 = expand_normal (treeop0);
10081 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10082 target, unsignedp);
10083 gcc_assert (temp);
10084 return temp;
10087 case VEC_UNPACK_FLOAT_HI_EXPR:
10088 case VEC_UNPACK_FLOAT_LO_EXPR:
10090 op0 = expand_normal (treeop0);
10091 /* The signedness is determined from input operand. */
10092 temp = expand_widen_pattern_expr
10093 (ops, op0, NULL_RTX, NULL_RTX,
10094 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10096 gcc_assert (temp);
10097 return temp;
10100 case VEC_WIDEN_PLUS_HI_EXPR:
10101 case VEC_WIDEN_PLUS_LO_EXPR:
10102 case VEC_WIDEN_MINUS_HI_EXPR:
10103 case VEC_WIDEN_MINUS_LO_EXPR:
10104 case VEC_WIDEN_MULT_HI_EXPR:
10105 case VEC_WIDEN_MULT_LO_EXPR:
10106 case VEC_WIDEN_MULT_EVEN_EXPR:
10107 case VEC_WIDEN_MULT_ODD_EXPR:
10108 case VEC_WIDEN_LSHIFT_HI_EXPR:
10109 case VEC_WIDEN_LSHIFT_LO_EXPR:
10110 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10111 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10112 target, unsignedp);
10113 gcc_assert (target);
10114 return target;
10116 case VEC_PACK_SAT_EXPR:
10117 case VEC_PACK_FIX_TRUNC_EXPR:
10118 mode = TYPE_MODE (TREE_TYPE (treeop0));
10119 subtarget = NULL_RTX;
10120 goto binop;
10122 case VEC_PACK_TRUNC_EXPR:
10123 if (VECTOR_BOOLEAN_TYPE_P (type)
10124 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10125 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10126 && SCALAR_INT_MODE_P (mode))
10128 class expand_operand eops[4];
10129 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10130 expand_operands (treeop0, treeop1,
10131 subtarget, &op0, &op1, EXPAND_NORMAL);
10132 this_optab = vec_pack_sbool_trunc_optab;
10133 enum insn_code icode = optab_handler (this_optab, imode);
10134 create_output_operand (&eops[0], target, mode);
10135 create_convert_operand_from (&eops[1], op0, imode, false);
10136 create_convert_operand_from (&eops[2], op1, imode, false);
10137 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10138 create_input_operand (&eops[3], temp, imode);
10139 expand_insn (icode, 4, eops);
10140 return eops[0].value;
10142 mode = TYPE_MODE (TREE_TYPE (treeop0));
10143 subtarget = NULL_RTX;
10144 goto binop;
10146 case VEC_PACK_FLOAT_EXPR:
10147 mode = TYPE_MODE (TREE_TYPE (treeop0));
10148 expand_operands (treeop0, treeop1,
10149 subtarget, &op0, &op1, EXPAND_NORMAL);
10150 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10151 optab_default);
10152 target = expand_binop (mode, this_optab, op0, op1, target,
10153 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10154 OPTAB_LIB_WIDEN);
10155 gcc_assert (target);
10156 return target;
10158 case VEC_PERM_EXPR:
10160 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10161 vec_perm_builder sel;
10162 if (TREE_CODE (treeop2) == VECTOR_CST
10163 && tree_to_vec_perm_builder (&sel, treeop2))
10165 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10166 temp = expand_vec_perm_const (mode, op0, op1, sel,
10167 sel_mode, target);
10169 else
10171 op2 = expand_normal (treeop2);
10172 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10174 gcc_assert (temp);
10175 return temp;
10178 case DOT_PROD_EXPR:
10180 tree oprnd0 = treeop0;
10181 tree oprnd1 = treeop1;
10182 tree oprnd2 = treeop2;
10184 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10185 op2 = expand_normal (oprnd2);
10186 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10187 target, unsignedp);
10188 return target;
10191 case SAD_EXPR:
10193 tree oprnd0 = treeop0;
10194 tree oprnd1 = treeop1;
10195 tree oprnd2 = treeop2;
10197 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10198 op2 = expand_normal (oprnd2);
10199 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10200 target, unsignedp);
10201 return target;
10204 case REALIGN_LOAD_EXPR:
10206 tree oprnd0 = treeop0;
10207 tree oprnd1 = treeop1;
10208 tree oprnd2 = treeop2;
10210 this_optab = optab_for_tree_code (code, type, optab_default);
10211 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10212 op2 = expand_normal (oprnd2);
10213 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10214 target, unsignedp);
10215 gcc_assert (temp);
10216 return temp;
10219 case COND_EXPR:
10221 /* A COND_EXPR with its type being VOID_TYPE represents a
10222 conditional jump and is handled in
10223 expand_gimple_cond_expr. */
10224 gcc_assert (!VOID_TYPE_P (type));
10226 /* Note that COND_EXPRs whose type is a structure or union
10227 are required to be constructed to contain assignments of
10228 a temporary variable, so that we can evaluate them here
10229 for side effect only. If type is void, we must do likewise. */
10231 gcc_assert (!TREE_ADDRESSABLE (type)
10232 && !ignore
10233 && TREE_TYPE (treeop1) != void_type_node
10234 && TREE_TYPE (treeop2) != void_type_node);
10236 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10237 if (temp)
10238 return temp;
10240 /* If we are not to produce a result, we have no target. Otherwise,
10241 if a target was specified use it; it will not be used as an
10242 intermediate target unless it is safe. If no target, use a
10243 temporary. */
10245 if (modifier != EXPAND_STACK_PARM
10246 && original_target
10247 && safe_from_p (original_target, treeop0, 1)
10248 && GET_MODE (original_target) == mode
10249 && !MEM_P (original_target))
10250 temp = original_target;
10251 else
10252 temp = assign_temp (type, 0, 1);
10254 do_pending_stack_adjust ();
10255 NO_DEFER_POP;
10256 rtx_code_label *lab0 = gen_label_rtx ();
10257 rtx_code_label *lab1 = gen_label_rtx ();
10258 jumpifnot (treeop0, lab0,
10259 profile_probability::uninitialized ());
10260 store_expr (treeop1, temp,
10261 modifier == EXPAND_STACK_PARM,
10262 false, false);
10264 emit_jump_insn (targetm.gen_jump (lab1));
10265 emit_barrier ();
10266 emit_label (lab0);
10267 store_expr (treeop2, temp,
10268 modifier == EXPAND_STACK_PARM,
10269 false, false);
10271 emit_label (lab1);
10272 OK_DEFER_POP;
10273 return temp;
10276 case VEC_DUPLICATE_EXPR:
10277 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10278 target = expand_vector_broadcast (mode, op0);
10279 gcc_assert (target);
10280 return target;
10282 case VEC_SERIES_EXPR:
10283 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10284 return expand_vec_series_expr (mode, op0, op1, target);
10286 case BIT_INSERT_EXPR:
10288 unsigned bitpos = tree_to_uhwi (treeop2);
10289 unsigned bitsize;
10290 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10291 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10292 else
10293 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10294 op0 = expand_normal (treeop0);
10295 op1 = expand_normal (treeop1);
10296 rtx dst = gen_reg_rtx (mode);
10297 emit_move_insn (dst, op0);
10298 store_bit_field (dst, bitsize, bitpos, 0, 0,
10299 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
10300 return dst;
10303 default:
10304 gcc_unreachable ();
10307 /* Here to do an ordinary binary operator. */
10308 binop:
10309 expand_operands (treeop0, treeop1,
10310 subtarget, &op0, &op1, EXPAND_NORMAL);
10311 binop2:
10312 this_optab = optab_for_tree_code (code, type, optab_default);
10313 binop3:
10314 if (modifier == EXPAND_STACK_PARM)
10315 target = 0;
10316 temp = expand_binop (mode, this_optab, op0, op1, target,
10317 unsignedp, OPTAB_LIB_WIDEN);
10318 gcc_assert (temp);
10319 /* Bitwise operations do not need bitfield reduction as we expect their
10320 operands being properly truncated. */
10321 if (code == BIT_XOR_EXPR
10322 || code == BIT_AND_EXPR
10323 || code == BIT_IOR_EXPR)
10324 return temp;
10325 return REDUCE_BIT_FIELD (temp);
10327 #undef REDUCE_BIT_FIELD
10330 /* Return TRUE if expression STMT is suitable for replacement.
10331 Never consider memory loads as replaceable, because those don't ever lead
10332 into constant expressions. */
10334 static bool
10335 stmt_is_replaceable_p (gimple *stmt)
10337 if (ssa_is_replaceable_p (stmt))
10339 /* Don't move around loads. */
10340 if (!gimple_assign_single_p (stmt)
10341 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10342 return true;
10344 return false;
10348 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10349 enum expand_modifier modifier, rtx *alt_rtl,
10350 bool inner_reference_p)
10352 rtx op0, op1, temp, decl_rtl;
10353 tree type;
10354 int unsignedp;
10355 machine_mode mode, dmode;
10356 enum tree_code code = TREE_CODE (exp);
10357 rtx subtarget, original_target;
10358 int ignore;
10359 tree context;
10360 bool reduce_bit_field;
10361 location_t loc = EXPR_LOCATION (exp);
10362 struct separate_ops ops;
10363 tree treeop0, treeop1, treeop2;
10364 tree ssa_name = NULL_TREE;
10365 gimple *g;
10367 type = TREE_TYPE (exp);
10368 mode = TYPE_MODE (type);
10369 unsignedp = TYPE_UNSIGNED (type);
10371 treeop0 = treeop1 = treeop2 = NULL_TREE;
10372 if (!VL_EXP_CLASS_P (exp))
10373 switch (TREE_CODE_LENGTH (code))
10375 default:
10376 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10377 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10378 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10379 case 0: break;
10381 ops.code = code;
10382 ops.type = type;
10383 ops.op0 = treeop0;
10384 ops.op1 = treeop1;
10385 ops.op2 = treeop2;
10386 ops.location = loc;
10388 ignore = (target == const0_rtx
10389 || ((CONVERT_EXPR_CODE_P (code)
10390 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10391 && TREE_CODE (type) == VOID_TYPE));
10393 /* An operation in what may be a bit-field type needs the
10394 result to be reduced to the precision of the bit-field type,
10395 which is narrower than that of the type's mode. */
10396 reduce_bit_field = (!ignore
10397 && INTEGRAL_TYPE_P (type)
10398 && !type_has_mode_precision_p (type));
10400 /* If we are going to ignore this result, we need only do something
10401 if there is a side-effect somewhere in the expression. If there
10402 is, short-circuit the most common cases here. Note that we must
10403 not call expand_expr with anything but const0_rtx in case this
10404 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10406 if (ignore)
10408 if (! TREE_SIDE_EFFECTS (exp))
10409 return const0_rtx;
10411 /* Ensure we reference a volatile object even if value is ignored, but
10412 don't do this if all we are doing is taking its address. */
10413 if (TREE_THIS_VOLATILE (exp)
10414 && TREE_CODE (exp) != FUNCTION_DECL
10415 && mode != VOIDmode && mode != BLKmode
10416 && modifier != EXPAND_CONST_ADDRESS)
10418 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10419 if (MEM_P (temp))
10420 copy_to_reg (temp);
10421 return const0_rtx;
10424 if (TREE_CODE_CLASS (code) == tcc_unary
10425 || code == BIT_FIELD_REF
10426 || code == COMPONENT_REF
10427 || code == INDIRECT_REF)
10428 return expand_expr (treeop0, const0_rtx, VOIDmode,
10429 modifier);
10431 else if (TREE_CODE_CLASS (code) == tcc_binary
10432 || TREE_CODE_CLASS (code) == tcc_comparison
10433 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10435 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10436 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10437 return const0_rtx;
10440 target = 0;
10443 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10444 target = 0;
10446 /* Use subtarget as the target for operand 0 of a binary operation. */
10447 subtarget = get_subtarget (target);
10448 original_target = target;
10450 switch (code)
10452 case LABEL_DECL:
10454 tree function = decl_function_context (exp);
10456 temp = label_rtx (exp);
10457 temp = gen_rtx_LABEL_REF (Pmode, temp);
10459 if (function != current_function_decl
10460 && function != 0)
10461 LABEL_REF_NONLOCAL_P (temp) = 1;
10463 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10464 return temp;
10467 case SSA_NAME:
10468 /* ??? ivopts calls expander, without any preparation from
10469 out-of-ssa. So fake instructions as if this was an access to the
10470 base variable. This unnecessarily allocates a pseudo, see how we can
10471 reuse it, if partition base vars have it set already. */
10472 if (!currently_expanding_to_rtl)
10474 tree var = SSA_NAME_VAR (exp);
10475 if (var && DECL_RTL_SET_P (var))
10476 return DECL_RTL (var);
10477 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10478 LAST_VIRTUAL_REGISTER + 1);
10481 g = get_gimple_for_ssa_name (exp);
10482 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10483 if (g == NULL
10484 && modifier == EXPAND_INITIALIZER
10485 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10486 && (optimize || !SSA_NAME_VAR (exp)
10487 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10488 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10489 g = SSA_NAME_DEF_STMT (exp);
10490 if (g)
10492 rtx r;
10493 location_t saved_loc = curr_insn_location ();
10494 loc = gimple_location (g);
10495 if (loc != UNKNOWN_LOCATION)
10496 set_curr_insn_location (loc);
10497 ops.code = gimple_assign_rhs_code (g);
10498 switch (get_gimple_rhs_class (ops.code))
10500 case GIMPLE_TERNARY_RHS:
10501 ops.op2 = gimple_assign_rhs3 (g);
10502 /* Fallthru */
10503 case GIMPLE_BINARY_RHS:
10504 ops.op1 = gimple_assign_rhs2 (g);
10506 /* Try to expand conditonal compare. */
10507 if (targetm.gen_ccmp_first)
10509 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10510 r = expand_ccmp_expr (g, mode);
10511 if (r)
10512 break;
10514 /* Fallthru */
10515 case GIMPLE_UNARY_RHS:
10516 ops.op0 = gimple_assign_rhs1 (g);
10517 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10518 ops.location = loc;
10519 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10520 break;
10521 case GIMPLE_SINGLE_RHS:
10523 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10524 tmode, modifier, alt_rtl,
10525 inner_reference_p);
10526 break;
10528 default:
10529 gcc_unreachable ();
10531 set_curr_insn_location (saved_loc);
10532 if (REG_P (r) && !REG_EXPR (r))
10533 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10534 return r;
10537 ssa_name = exp;
10538 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10539 exp = SSA_NAME_VAR (ssa_name);
10540 goto expand_decl_rtl;
10542 case VAR_DECL:
10543 /* Allow accel compiler to handle variables that require special
10544 treatment, e.g. if they have been modified in some way earlier in
10545 compilation by the adjust_private_decl OpenACC hook. */
10546 if (flag_openacc && targetm.goacc.expand_var_decl)
10548 temp = targetm.goacc.expand_var_decl (exp);
10549 if (temp)
10550 return temp;
10552 /* ... fall through ... */
10554 case PARM_DECL:
10555 /* If a static var's type was incomplete when the decl was written,
10556 but the type is complete now, lay out the decl now. */
10557 if (DECL_SIZE (exp) == 0
10558 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10559 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10560 layout_decl (exp, 0);
10562 /* fall through */
10564 case FUNCTION_DECL:
10565 case RESULT_DECL:
10566 decl_rtl = DECL_RTL (exp);
10567 expand_decl_rtl:
10568 gcc_assert (decl_rtl);
10570 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10571 settings for VECTOR_TYPE_P that might switch for the function. */
10572 if (currently_expanding_to_rtl
10573 && code == VAR_DECL && MEM_P (decl_rtl)
10574 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10575 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10576 else
10577 decl_rtl = copy_rtx (decl_rtl);
10579 /* Record writes to register variables. */
10580 if (modifier == EXPAND_WRITE
10581 && REG_P (decl_rtl)
10582 && HARD_REGISTER_P (decl_rtl))
10583 add_to_hard_reg_set (&crtl->asm_clobbers,
10584 GET_MODE (decl_rtl), REGNO (decl_rtl));
10586 /* Ensure variable marked as used even if it doesn't go through
10587 a parser. If it hasn't be used yet, write out an external
10588 definition. */
10589 if (exp)
10590 TREE_USED (exp) = 1;
10592 /* Show we haven't gotten RTL for this yet. */
10593 temp = 0;
10595 /* Variables inherited from containing functions should have
10596 been lowered by this point. */
10597 if (exp)
10598 context = decl_function_context (exp);
10599 gcc_assert (!exp
10600 || SCOPE_FILE_SCOPE_P (context)
10601 || context == current_function_decl
10602 || TREE_STATIC (exp)
10603 || DECL_EXTERNAL (exp)
10604 /* ??? C++ creates functions that are not TREE_STATIC. */
10605 || TREE_CODE (exp) == FUNCTION_DECL);
10607 /* This is the case of an array whose size is to be determined
10608 from its initializer, while the initializer is still being parsed.
10609 ??? We aren't parsing while expanding anymore. */
10611 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10612 temp = validize_mem (decl_rtl);
10614 /* If DECL_RTL is memory, we are in the normal case and the
10615 address is not valid, get the address into a register. */
10617 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10619 if (alt_rtl)
10620 *alt_rtl = decl_rtl;
10621 decl_rtl = use_anchored_address (decl_rtl);
10622 if (modifier != EXPAND_CONST_ADDRESS
10623 && modifier != EXPAND_SUM
10624 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10625 : GET_MODE (decl_rtl),
10626 XEXP (decl_rtl, 0),
10627 MEM_ADDR_SPACE (decl_rtl)))
10628 temp = replace_equiv_address (decl_rtl,
10629 copy_rtx (XEXP (decl_rtl, 0)));
10632 /* If we got something, return it. But first, set the alignment
10633 if the address is a register. */
10634 if (temp != 0)
10636 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10637 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10639 else if (MEM_P (decl_rtl))
10640 temp = decl_rtl;
10642 if (temp != 0)
10644 if (MEM_P (temp)
10645 && modifier != EXPAND_WRITE
10646 && modifier != EXPAND_MEMORY
10647 && modifier != EXPAND_INITIALIZER
10648 && modifier != EXPAND_CONST_ADDRESS
10649 && modifier != EXPAND_SUM
10650 && !inner_reference_p
10651 && mode != BLKmode
10652 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10653 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10654 MEM_ALIGN (temp), NULL_RTX, NULL);
10656 return temp;
10659 if (exp)
10660 dmode = DECL_MODE (exp);
10661 else
10662 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10664 /* If the mode of DECL_RTL does not match that of the decl,
10665 there are two cases: we are dealing with a BLKmode value
10666 that is returned in a register, or we are dealing with
10667 a promoted value. In the latter case, return a SUBREG
10668 of the wanted mode, but mark it so that we know that it
10669 was already extended. */
10670 if (REG_P (decl_rtl)
10671 && dmode != BLKmode
10672 && GET_MODE (decl_rtl) != dmode)
10674 machine_mode pmode;
10676 /* Get the signedness to be used for this variable. Ensure we get
10677 the same mode we got when the variable was declared. */
10678 if (code != SSA_NAME)
10679 pmode = promote_decl_mode (exp, &unsignedp);
10680 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10681 && gimple_code (g) == GIMPLE_CALL
10682 && !gimple_call_internal_p (g))
10683 pmode = promote_function_mode (type, mode, &unsignedp,
10684 gimple_call_fntype (g),
10686 else
10687 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10688 gcc_assert (GET_MODE (decl_rtl) == pmode);
10690 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10691 SUBREG_PROMOTED_VAR_P (temp) = 1;
10692 SUBREG_PROMOTED_SET (temp, unsignedp);
10693 return temp;
10696 return decl_rtl;
10698 case INTEGER_CST:
10700 /* Given that TYPE_PRECISION (type) is not always equal to
10701 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10702 the former to the latter according to the signedness of the
10703 type. */
10704 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10705 temp = immed_wide_int_const
10706 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10707 return temp;
10710 case VECTOR_CST:
10712 tree tmp = NULL_TREE;
10713 if (VECTOR_MODE_P (mode))
10714 return const_vector_from_tree (exp);
10715 scalar_int_mode int_mode;
10716 if (is_int_mode (mode, &int_mode))
10718 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10719 if (type_for_mode)
10720 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10721 type_for_mode, exp);
10723 if (!tmp)
10725 vec<constructor_elt, va_gc> *v;
10726 /* Constructors need to be fixed-length. FIXME. */
10727 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10728 vec_alloc (v, nunits);
10729 for (unsigned int i = 0; i < nunits; ++i)
10730 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10731 tmp = build_constructor (type, v);
10733 return expand_expr (tmp, ignore ? const0_rtx : target,
10734 tmode, modifier);
10737 case CONST_DECL:
10738 if (modifier == EXPAND_WRITE)
10740 /* Writing into CONST_DECL is always invalid, but handle it
10741 gracefully. */
10742 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10743 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10744 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10745 EXPAND_NORMAL, as);
10746 op0 = memory_address_addr_space (mode, op0, as);
10747 temp = gen_rtx_MEM (mode, op0);
10748 set_mem_addr_space (temp, as);
10749 return temp;
10751 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10753 case REAL_CST:
10754 /* If optimized, generate immediate CONST_DOUBLE
10755 which will be turned into memory by reload if necessary.
10757 We used to force a register so that loop.c could see it. But
10758 this does not allow gen_* patterns to perform optimizations with
10759 the constants. It also produces two insns in cases like "x = 1.0;".
10760 On most machines, floating-point constants are not permitted in
10761 many insns, so we'd end up copying it to a register in any case.
10763 Now, we do the copying in expand_binop, if appropriate. */
10764 return const_double_from_real_value (TREE_REAL_CST (exp),
10765 TYPE_MODE (TREE_TYPE (exp)));
10767 case FIXED_CST:
10768 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10769 TYPE_MODE (TREE_TYPE (exp)));
10771 case COMPLEX_CST:
10772 /* Handle evaluating a complex constant in a CONCAT target. */
10773 if (original_target && GET_CODE (original_target) == CONCAT)
10775 rtx rtarg, itarg;
10777 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10778 rtarg = XEXP (original_target, 0);
10779 itarg = XEXP (original_target, 1);
10781 /* Move the real and imaginary parts separately. */
10782 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10783 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10785 if (op0 != rtarg)
10786 emit_move_insn (rtarg, op0);
10787 if (op1 != itarg)
10788 emit_move_insn (itarg, op1);
10790 return original_target;
10793 /* fall through */
10795 case STRING_CST:
10796 temp = expand_expr_constant (exp, 1, modifier);
10798 /* temp contains a constant address.
10799 On RISC machines where a constant address isn't valid,
10800 make some insns to get that address into a register. */
10801 if (modifier != EXPAND_CONST_ADDRESS
10802 && modifier != EXPAND_INITIALIZER
10803 && modifier != EXPAND_SUM
10804 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10805 MEM_ADDR_SPACE (temp)))
10806 return replace_equiv_address (temp,
10807 copy_rtx (XEXP (temp, 0)));
10808 return temp;
10810 case POLY_INT_CST:
10811 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10813 case SAVE_EXPR:
10815 tree val = treeop0;
10816 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10817 inner_reference_p);
10819 if (!SAVE_EXPR_RESOLVED_P (exp))
10821 /* We can indeed still hit this case, typically via builtin
10822 expanders calling save_expr immediately before expanding
10823 something. Assume this means that we only have to deal
10824 with non-BLKmode values. */
10825 gcc_assert (GET_MODE (ret) != BLKmode);
10827 val = build_decl (curr_insn_location (),
10828 VAR_DECL, NULL, TREE_TYPE (exp));
10829 DECL_ARTIFICIAL (val) = 1;
10830 DECL_IGNORED_P (val) = 1;
10831 treeop0 = val;
10832 TREE_OPERAND (exp, 0) = treeop0;
10833 SAVE_EXPR_RESOLVED_P (exp) = 1;
10835 if (!CONSTANT_P (ret))
10836 ret = copy_to_reg (ret);
10837 SET_DECL_RTL (val, ret);
10840 return ret;
10844 case CONSTRUCTOR:
10845 /* If we don't need the result, just ensure we evaluate any
10846 subexpressions. */
10847 if (ignore)
10849 unsigned HOST_WIDE_INT idx;
10850 tree value;
10852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10853 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10855 return const0_rtx;
10858 return expand_constructor (exp, target, modifier, false);
10860 case TARGET_MEM_REF:
10862 addr_space_t as
10863 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10864 unsigned int align;
10866 op0 = addr_for_mem_ref (exp, as, true);
10867 op0 = memory_address_addr_space (mode, op0, as);
10868 temp = gen_rtx_MEM (mode, op0);
10869 set_mem_attributes (temp, exp, 0);
10870 set_mem_addr_space (temp, as);
10871 align = get_object_alignment (exp);
10872 if (modifier != EXPAND_WRITE
10873 && modifier != EXPAND_MEMORY
10874 && mode != BLKmode
10875 && align < GET_MODE_ALIGNMENT (mode))
10876 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10877 align, NULL_RTX, NULL);
10878 return temp;
10881 case MEM_REF:
10883 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10884 addr_space_t as
10885 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10886 machine_mode address_mode;
10887 tree base = TREE_OPERAND (exp, 0);
10888 gimple *def_stmt;
10889 unsigned align;
10890 /* Handle expansion of non-aliased memory with non-BLKmode. That
10891 might end up in a register. */
10892 if (mem_ref_refers_to_non_mem_p (exp))
10894 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10895 base = TREE_OPERAND (base, 0);
10896 poly_uint64 type_size;
10897 if (known_eq (offset, 0)
10898 && !reverse
10899 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10900 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10901 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10902 target, tmode, modifier);
10903 if (TYPE_MODE (type) == BLKmode)
10905 temp = assign_stack_temp (DECL_MODE (base),
10906 GET_MODE_SIZE (DECL_MODE (base)));
10907 store_expr (base, temp, 0, false, false);
10908 temp = adjust_address (temp, BLKmode, offset);
10909 set_mem_size (temp, int_size_in_bytes (type));
10910 return temp;
10912 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10913 bitsize_int (offset * BITS_PER_UNIT));
10914 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10915 return expand_expr (exp, target, tmode, modifier);
10917 address_mode = targetm.addr_space.address_mode (as);
10918 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10920 tree mask = gimple_assign_rhs2 (def_stmt);
10921 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10922 gimple_assign_rhs1 (def_stmt), mask);
10923 TREE_OPERAND (exp, 0) = base;
10925 align = get_object_alignment (exp);
10926 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10927 op0 = memory_address_addr_space (mode, op0, as);
10928 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10930 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10931 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10932 op0 = memory_address_addr_space (mode, op0, as);
10934 temp = gen_rtx_MEM (mode, op0);
10935 set_mem_attributes (temp, exp, 0);
10936 set_mem_addr_space (temp, as);
10937 if (TREE_THIS_VOLATILE (exp))
10938 MEM_VOLATILE_P (temp) = 1;
10939 if (modifier != EXPAND_WRITE
10940 && modifier != EXPAND_MEMORY
10941 && !inner_reference_p
10942 && mode != BLKmode
10943 && align < GET_MODE_ALIGNMENT (mode))
10944 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
10945 modifier == EXPAND_STACK_PARM
10946 ? NULL_RTX : target, alt_rtl);
10947 if (reverse
10948 && modifier != EXPAND_MEMORY
10949 && modifier != EXPAND_WRITE)
10950 temp = flip_storage_order (mode, temp);
10951 return temp;
10954 case ARRAY_REF:
10957 tree array = treeop0;
10958 tree index = treeop1;
10959 tree init;
10961 /* Fold an expression like: "foo"[2].
10962 This is not done in fold so it won't happen inside &.
10963 Don't fold if this is for wide characters since it's too
10964 difficult to do correctly and this is a very rare case. */
10966 if (modifier != EXPAND_CONST_ADDRESS
10967 && modifier != EXPAND_INITIALIZER
10968 && modifier != EXPAND_MEMORY)
10970 tree t = fold_read_from_constant_string (exp);
10972 if (t)
10973 return expand_expr (t, target, tmode, modifier);
10976 /* If this is a constant index into a constant array,
10977 just get the value from the array. Handle both the cases when
10978 we have an explicit constructor and when our operand is a variable
10979 that was declared const. */
10981 if (modifier != EXPAND_CONST_ADDRESS
10982 && modifier != EXPAND_INITIALIZER
10983 && modifier != EXPAND_MEMORY
10984 && TREE_CODE (array) == CONSTRUCTOR
10985 && ! TREE_SIDE_EFFECTS (array)
10986 && TREE_CODE (index) == INTEGER_CST)
10988 unsigned HOST_WIDE_INT ix;
10989 tree field, value;
10991 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10992 field, value)
10993 if (tree_int_cst_equal (field, index))
10995 if (!TREE_SIDE_EFFECTS (value))
10996 return expand_expr (fold (value), target, tmode, modifier);
10997 break;
11001 else if (optimize >= 1
11002 && modifier != EXPAND_CONST_ADDRESS
11003 && modifier != EXPAND_INITIALIZER
11004 && modifier != EXPAND_MEMORY
11005 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11006 && TREE_CODE (index) == INTEGER_CST
11007 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11008 && (init = ctor_for_folding (array)) != error_mark_node)
11010 if (init == NULL_TREE)
11012 tree value = build_zero_cst (type);
11013 if (TREE_CODE (value) == CONSTRUCTOR)
11015 /* If VALUE is a CONSTRUCTOR, this optimization is only
11016 useful if this doesn't store the CONSTRUCTOR into
11017 memory. If it does, it is more efficient to just
11018 load the data from the array directly. */
11019 rtx ret = expand_constructor (value, target,
11020 modifier, true);
11021 if (ret == NULL_RTX)
11022 value = NULL_TREE;
11025 if (value)
11026 return expand_expr (value, target, tmode, modifier);
11028 else if (TREE_CODE (init) == CONSTRUCTOR)
11030 unsigned HOST_WIDE_INT ix;
11031 tree field, value;
11033 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11034 field, value)
11035 if (tree_int_cst_equal (field, index))
11037 if (TREE_SIDE_EFFECTS (value))
11038 break;
11040 if (TREE_CODE (value) == CONSTRUCTOR)
11042 /* If VALUE is a CONSTRUCTOR, this
11043 optimization is only useful if
11044 this doesn't store the CONSTRUCTOR
11045 into memory. If it does, it is more
11046 efficient to just load the data from
11047 the array directly. */
11048 rtx ret = expand_constructor (value, target,
11049 modifier, true);
11050 if (ret == NULL_RTX)
11051 break;
11054 return
11055 expand_expr (fold (value), target, tmode, modifier);
11058 else if (TREE_CODE (init) == STRING_CST)
11060 tree low_bound = array_ref_low_bound (exp);
11061 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11063 /* Optimize the special case of a zero lower bound.
11065 We convert the lower bound to sizetype to avoid problems
11066 with constant folding. E.g. suppose the lower bound is
11067 1 and its mode is QI. Without the conversion
11068 (ARRAY + (INDEX - (unsigned char)1))
11069 becomes
11070 (ARRAY + (-(unsigned char)1) + INDEX)
11071 which becomes
11072 (ARRAY + 255 + INDEX). Oops! */
11073 if (!integer_zerop (low_bound))
11074 index1 = size_diffop_loc (loc, index1,
11075 fold_convert_loc (loc, sizetype,
11076 low_bound));
11078 if (tree_fits_uhwi_p (index1)
11079 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11081 tree char_type = TREE_TYPE (TREE_TYPE (init));
11082 scalar_int_mode char_mode;
11084 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11085 && GET_MODE_SIZE (char_mode) == 1)
11086 return gen_int_mode (TREE_STRING_POINTER (init)
11087 [TREE_INT_CST_LOW (index1)],
11088 char_mode);
11093 goto normal_inner_ref;
11095 case COMPONENT_REF:
11096 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11097 /* Fall through. */
11098 case BIT_FIELD_REF:
11099 case ARRAY_RANGE_REF:
11100 normal_inner_ref:
11102 machine_mode mode1, mode2;
11103 poly_int64 bitsize, bitpos, bytepos;
11104 tree offset;
11105 int reversep, volatilep = 0, must_force_mem;
11106 tree tem
11107 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11108 &unsignedp, &reversep, &volatilep);
11109 rtx orig_op0, memloc;
11110 bool clear_mem_expr = false;
11112 /* If we got back the original object, something is wrong. Perhaps
11113 we are evaluating an expression too early. In any event, don't
11114 infinitely recurse. */
11115 gcc_assert (tem != exp);
11117 /* If TEM's type is a union of variable size, pass TARGET to the inner
11118 computation, since it will need a temporary and TARGET is known
11119 to have to do. This occurs in unchecked conversion in Ada. */
11120 orig_op0 = op0
11121 = expand_expr_real (tem,
11122 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11123 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11124 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11125 != INTEGER_CST)
11126 && modifier != EXPAND_STACK_PARM
11127 ? target : NULL_RTX),
11128 VOIDmode,
11129 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11130 NULL, true);
11132 /* If the field has a mode, we want to access it in the
11133 field's mode, not the computed mode.
11134 If a MEM has VOIDmode (external with incomplete type),
11135 use BLKmode for it instead. */
11136 if (MEM_P (op0))
11138 if (mode1 != VOIDmode)
11139 op0 = adjust_address (op0, mode1, 0);
11140 else if (GET_MODE (op0) == VOIDmode)
11141 op0 = adjust_address (op0, BLKmode, 0);
11144 mode2
11145 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11147 /* Make sure bitpos is not negative, it can wreak havoc later. */
11148 if (maybe_lt (bitpos, 0))
11150 gcc_checking_assert (offset == NULL_TREE);
11151 offset = size_int (bits_to_bytes_round_down (bitpos));
11152 bitpos = num_trailing_bits (bitpos);
11155 /* If we have either an offset, a BLKmode result, or a reference
11156 outside the underlying object, we must force it to memory.
11157 Such a case can occur in Ada if we have unchecked conversion
11158 of an expression from a scalar type to an aggregate type or
11159 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11160 passed a partially uninitialized object or a view-conversion
11161 to a larger size. */
11162 must_force_mem = (offset
11163 || mode1 == BLKmode
11164 || (mode == BLKmode
11165 && !int_mode_for_size (bitsize, 1).exists ())
11166 || maybe_gt (bitpos + bitsize,
11167 GET_MODE_BITSIZE (mode2)));
11169 /* Handle CONCAT first. */
11170 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11172 if (known_eq (bitpos, 0)
11173 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11174 && COMPLEX_MODE_P (mode1)
11175 && COMPLEX_MODE_P (GET_MODE (op0))
11176 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11177 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11179 if (reversep)
11180 op0 = flip_storage_order (GET_MODE (op0), op0);
11181 if (mode1 != GET_MODE (op0))
11183 rtx parts[2];
11184 for (int i = 0; i < 2; i++)
11186 rtx op = read_complex_part (op0, i != 0);
11187 if (GET_CODE (op) == SUBREG)
11188 op = force_reg (GET_MODE (op), op);
11189 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11190 if (temp)
11191 op = temp;
11192 else
11194 if (!REG_P (op) && !MEM_P (op))
11195 op = force_reg (GET_MODE (op), op);
11196 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11198 parts[i] = op;
11200 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11202 return op0;
11204 if (known_eq (bitpos, 0)
11205 && known_eq (bitsize,
11206 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11207 && maybe_ne (bitsize, 0))
11209 op0 = XEXP (op0, 0);
11210 mode2 = GET_MODE (op0);
11212 else if (known_eq (bitpos,
11213 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11214 && known_eq (bitsize,
11215 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11216 && maybe_ne (bitpos, 0)
11217 && maybe_ne (bitsize, 0))
11219 op0 = XEXP (op0, 1);
11220 bitpos = 0;
11221 mode2 = GET_MODE (op0);
11223 else
11224 /* Otherwise force into memory. */
11225 must_force_mem = 1;
11228 /* If this is a constant, put it in a register if it is a legitimate
11229 constant and we don't need a memory reference. */
11230 if (CONSTANT_P (op0)
11231 && mode2 != BLKmode
11232 && targetm.legitimate_constant_p (mode2, op0)
11233 && !must_force_mem)
11234 op0 = force_reg (mode2, op0);
11236 /* Otherwise, if this is a constant, try to force it to the constant
11237 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11238 is a legitimate constant. */
11239 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11240 op0 = validize_mem (memloc);
11242 /* Otherwise, if this is a constant or the object is not in memory
11243 and need be, put it there. */
11244 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11246 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11247 emit_move_insn (memloc, op0);
11248 op0 = memloc;
11249 clear_mem_expr = true;
11252 if (offset)
11254 machine_mode address_mode;
11255 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11256 EXPAND_SUM);
11258 gcc_assert (MEM_P (op0));
11260 address_mode = get_address_mode (op0);
11261 if (GET_MODE (offset_rtx) != address_mode)
11263 /* We cannot be sure that the RTL in offset_rtx is valid outside
11264 of a memory address context, so force it into a register
11265 before attempting to convert it to the desired mode. */
11266 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11267 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11270 /* See the comment in expand_assignment for the rationale. */
11271 if (mode1 != VOIDmode
11272 && maybe_ne (bitpos, 0)
11273 && maybe_gt (bitsize, 0)
11274 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11275 && multiple_p (bitpos, bitsize)
11276 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11277 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11279 op0 = adjust_address (op0, mode1, bytepos);
11280 bitpos = 0;
11283 op0 = offset_address (op0, offset_rtx,
11284 highest_pow2_factor (offset));
11287 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11288 record its alignment as BIGGEST_ALIGNMENT. */
11289 if (MEM_P (op0)
11290 && known_eq (bitpos, 0)
11291 && offset != 0
11292 && is_aligning_offset (offset, tem))
11293 set_mem_align (op0, BIGGEST_ALIGNMENT);
11295 /* Don't forget about volatility even if this is a bitfield. */
11296 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11298 if (op0 == orig_op0)
11299 op0 = copy_rtx (op0);
11301 MEM_VOLATILE_P (op0) = 1;
11304 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11306 if (op0 == orig_op0)
11307 op0 = copy_rtx (op0);
11309 set_mem_align (op0, BITS_PER_UNIT);
11312 /* In cases where an aligned union has an unaligned object
11313 as a field, we might be extracting a BLKmode value from
11314 an integer-mode (e.g., SImode) object. Handle this case
11315 by doing the extract into an object as wide as the field
11316 (which we know to be the width of a basic mode), then
11317 storing into memory, and changing the mode to BLKmode. */
11318 if (mode1 == VOIDmode
11319 || REG_P (op0) || GET_CODE (op0) == SUBREG
11320 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11321 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11322 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11323 && modifier != EXPAND_CONST_ADDRESS
11324 && modifier != EXPAND_INITIALIZER
11325 && modifier != EXPAND_MEMORY)
11326 /* If the bitfield is volatile and the bitsize
11327 is narrower than the access size of the bitfield,
11328 we need to extract bitfields from the access. */
11329 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11330 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11331 && mode1 != BLKmode
11332 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11333 /* If the field isn't aligned enough to fetch as a memref,
11334 fetch it as a bit field. */
11335 || (mode1 != BLKmode
11336 && (((MEM_P (op0)
11337 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11338 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11339 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11340 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11341 && modifier != EXPAND_MEMORY
11342 && ((modifier == EXPAND_CONST_ADDRESS
11343 || modifier == EXPAND_INITIALIZER)
11344 ? STRICT_ALIGNMENT
11345 : targetm.slow_unaligned_access (mode1,
11346 MEM_ALIGN (op0))))
11347 || !multiple_p (bitpos, BITS_PER_UNIT)))
11348 /* If the type and the field are a constant size and the
11349 size of the type isn't the same size as the bitfield,
11350 we must use bitfield operations. */
11351 || (known_size_p (bitsize)
11352 && TYPE_SIZE (TREE_TYPE (exp))
11353 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11354 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11355 bitsize)))
11357 machine_mode ext_mode = mode;
11359 if (ext_mode == BLKmode
11360 && ! (target != 0 && MEM_P (op0)
11361 && MEM_P (target)
11362 && multiple_p (bitpos, BITS_PER_UNIT)))
11363 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11365 if (ext_mode == BLKmode)
11367 if (target == 0)
11368 target = assign_temp (type, 1, 1);
11370 /* ??? Unlike the similar test a few lines below, this one is
11371 very likely obsolete. */
11372 if (known_eq (bitsize, 0))
11373 return target;
11375 /* In this case, BITPOS must start at a byte boundary and
11376 TARGET, if specified, must be a MEM. */
11377 gcc_assert (MEM_P (op0)
11378 && (!target || MEM_P (target)));
11380 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11381 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11382 emit_block_move (target,
11383 adjust_address (op0, VOIDmode, bytepos),
11384 gen_int_mode (bytesize, Pmode),
11385 (modifier == EXPAND_STACK_PARM
11386 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11388 return target;
11391 /* If we have nothing to extract, the result will be 0 for targets
11392 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11393 return 0 for the sake of consistency, as reading a zero-sized
11394 bitfield is valid in Ada and the value is fully specified. */
11395 if (known_eq (bitsize, 0))
11396 return const0_rtx;
11398 op0 = validize_mem (op0);
11400 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11401 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11403 /* If the result has aggregate type and the extraction is done in
11404 an integral mode, then the field may be not aligned on a byte
11405 boundary; in this case, if it has reverse storage order, it
11406 needs to be extracted as a scalar field with reverse storage
11407 order and put back into memory order afterwards. */
11408 if (AGGREGATE_TYPE_P (type)
11409 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11410 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11412 gcc_checking_assert (known_ge (bitpos, 0));
11413 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11414 (modifier == EXPAND_STACK_PARM
11415 ? NULL_RTX : target),
11416 ext_mode, ext_mode, reversep, alt_rtl);
11418 /* If the result has aggregate type and the mode of OP0 is an
11419 integral mode then, if BITSIZE is narrower than this mode
11420 and this is for big-endian data, we must put the field
11421 into the high-order bits. And we must also put it back
11422 into memory order if it has been previously reversed. */
11423 scalar_int_mode op0_mode;
11424 if (AGGREGATE_TYPE_P (type)
11425 && is_int_mode (GET_MODE (op0), &op0_mode))
11427 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11429 gcc_checking_assert (known_le (bitsize, size));
11430 if (maybe_lt (bitsize, size)
11431 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11432 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11433 size - bitsize, op0, 1);
11435 if (reversep)
11436 op0 = flip_storage_order (op0_mode, op0);
11439 /* If the result type is BLKmode, store the data into a temporary
11440 of the appropriate type, but with the mode corresponding to the
11441 mode for the data we have (op0's mode). */
11442 if (mode == BLKmode)
11444 rtx new_rtx
11445 = assign_stack_temp_for_type (ext_mode,
11446 GET_MODE_BITSIZE (ext_mode),
11447 type);
11448 emit_move_insn (new_rtx, op0);
11449 op0 = copy_rtx (new_rtx);
11450 PUT_MODE (op0, BLKmode);
11453 return op0;
11456 /* If the result is BLKmode, use that to access the object
11457 now as well. */
11458 if (mode == BLKmode)
11459 mode1 = BLKmode;
11461 /* Get a reference to just this component. */
11462 bytepos = bits_to_bytes_round_down (bitpos);
11463 if (modifier == EXPAND_CONST_ADDRESS
11464 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11465 op0 = adjust_address_nv (op0, mode1, bytepos);
11466 else
11467 op0 = adjust_address (op0, mode1, bytepos);
11469 if (op0 == orig_op0)
11470 op0 = copy_rtx (op0);
11472 /* Don't set memory attributes if the base expression is
11473 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11474 we should just honor its original memory attributes. */
11475 if (!(TREE_CODE (tem) == SSA_NAME
11476 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11477 set_mem_attributes (op0, exp, 0);
11479 if (REG_P (XEXP (op0, 0)))
11480 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11482 /* If op0 is a temporary because the original expressions was forced
11483 to memory, clear MEM_EXPR so that the original expression cannot
11484 be marked as addressable through MEM_EXPR of the temporary. */
11485 if (clear_mem_expr)
11486 set_mem_expr (op0, NULL_TREE);
11488 MEM_VOLATILE_P (op0) |= volatilep;
11490 if (reversep
11491 && modifier != EXPAND_MEMORY
11492 && modifier != EXPAND_WRITE)
11493 op0 = flip_storage_order (mode1, op0);
11495 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11496 || modifier == EXPAND_CONST_ADDRESS
11497 || modifier == EXPAND_INITIALIZER)
11498 return op0;
11500 if (target == 0)
11501 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11503 convert_move (target, op0, unsignedp);
11504 return target;
11507 case OBJ_TYPE_REF:
11508 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11510 case CALL_EXPR:
11511 /* All valid uses of __builtin_va_arg_pack () are removed during
11512 inlining. */
11513 if (CALL_EXPR_VA_ARG_PACK (exp))
11514 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11516 tree fndecl = get_callee_fndecl (exp), attr;
11518 if (fndecl
11519 /* Don't diagnose the error attribute in thunks, those are
11520 artificially created. */
11521 && !CALL_FROM_THUNK_P (exp)
11522 && (attr = lookup_attribute ("error",
11523 DECL_ATTRIBUTES (fndecl))) != NULL)
11525 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11526 error ("call to %qs declared with attribute error: %s",
11527 identifier_to_locale (ident),
11528 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11530 if (fndecl
11531 /* Don't diagnose the warning attribute in thunks, those are
11532 artificially created. */
11533 && !CALL_FROM_THUNK_P (exp)
11534 && (attr = lookup_attribute ("warning",
11535 DECL_ATTRIBUTES (fndecl))) != NULL)
11537 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11538 warning_at (EXPR_LOCATION (exp),
11539 OPT_Wattribute_warning,
11540 "call to %qs declared with attribute warning: %s",
11541 identifier_to_locale (ident),
11542 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11545 /* Check for a built-in function. */
11546 if (fndecl && fndecl_built_in_p (fndecl))
11548 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11549 return expand_builtin (exp, target, subtarget, tmode, ignore);
11552 return expand_call (exp, target, ignore);
11554 case VIEW_CONVERT_EXPR:
11555 op0 = NULL_RTX;
11557 /* If we are converting to BLKmode, try to avoid an intermediate
11558 temporary by fetching an inner memory reference. */
11559 if (mode == BLKmode
11560 && poly_int_tree_p (TYPE_SIZE (type))
11561 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11562 && handled_component_p (treeop0))
11564 machine_mode mode1;
11565 poly_int64 bitsize, bitpos, bytepos;
11566 tree offset;
11567 int reversep, volatilep = 0;
11568 tree tem
11569 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11570 &unsignedp, &reversep, &volatilep);
11572 /* ??? We should work harder and deal with non-zero offsets. */
11573 if (!offset
11574 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11575 && !reversep
11576 && known_size_p (bitsize)
11577 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11579 /* See the normal_inner_ref case for the rationale. */
11580 rtx orig_op0
11581 = expand_expr_real (tem,
11582 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11583 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11584 != INTEGER_CST)
11585 && modifier != EXPAND_STACK_PARM
11586 ? target : NULL_RTX),
11587 VOIDmode,
11588 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11589 NULL, true);
11591 if (MEM_P (orig_op0))
11593 op0 = orig_op0;
11595 /* Get a reference to just this component. */
11596 if (modifier == EXPAND_CONST_ADDRESS
11597 || modifier == EXPAND_SUM
11598 || modifier == EXPAND_INITIALIZER)
11599 op0 = adjust_address_nv (op0, mode, bytepos);
11600 else
11601 op0 = adjust_address (op0, mode, bytepos);
11603 if (op0 == orig_op0)
11604 op0 = copy_rtx (op0);
11606 set_mem_attributes (op0, treeop0, 0);
11607 if (REG_P (XEXP (op0, 0)))
11608 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11610 MEM_VOLATILE_P (op0) |= volatilep;
11615 if (!op0)
11616 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11617 NULL, inner_reference_p);
11619 /* If the input and output modes are both the same, we are done. */
11620 if (mode == GET_MODE (op0))
11622 /* If neither mode is BLKmode, and both modes are the same size
11623 then we can use gen_lowpart. */
11624 else if (mode != BLKmode
11625 && GET_MODE (op0) != BLKmode
11626 && known_eq (GET_MODE_PRECISION (mode),
11627 GET_MODE_PRECISION (GET_MODE (op0)))
11628 && !COMPLEX_MODE_P (GET_MODE (op0)))
11630 if (GET_CODE (op0) == SUBREG)
11631 op0 = force_reg (GET_MODE (op0), op0);
11632 temp = gen_lowpart_common (mode, op0);
11633 if (temp)
11634 op0 = temp;
11635 else
11637 if (!REG_P (op0) && !MEM_P (op0))
11638 op0 = force_reg (GET_MODE (op0), op0);
11639 op0 = gen_lowpart (mode, op0);
11642 /* If both types are integral, convert from one mode to the other. */
11643 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11644 op0 = convert_modes (mode, GET_MODE (op0), op0,
11645 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11646 /* If the output type is a bit-field type, do an extraction. */
11647 else if (reduce_bit_field)
11648 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11649 TYPE_UNSIGNED (type), NULL_RTX,
11650 mode, mode, false, NULL);
11651 /* As a last resort, spill op0 to memory, and reload it in a
11652 different mode. */
11653 else if (!MEM_P (op0))
11655 /* If the operand is not a MEM, force it into memory. Since we
11656 are going to be changing the mode of the MEM, don't call
11657 force_const_mem for constants because we don't allow pool
11658 constants to change mode. */
11659 tree inner_type = TREE_TYPE (treeop0);
11661 gcc_assert (!TREE_ADDRESSABLE (exp));
11663 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11664 target
11665 = assign_stack_temp_for_type
11666 (TYPE_MODE (inner_type),
11667 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11669 emit_move_insn (target, op0);
11670 op0 = target;
11673 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11674 output type is such that the operand is known to be aligned, indicate
11675 that it is. Otherwise, we need only be concerned about alignment for
11676 non-BLKmode results. */
11677 if (MEM_P (op0))
11679 enum insn_code icode;
11681 if (modifier != EXPAND_WRITE
11682 && modifier != EXPAND_MEMORY
11683 && !inner_reference_p
11684 && mode != BLKmode
11685 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11687 /* If the target does have special handling for unaligned
11688 loads of mode then use them. */
11689 if ((icode = optab_handler (movmisalign_optab, mode))
11690 != CODE_FOR_nothing)
11692 rtx reg;
11694 op0 = adjust_address (op0, mode, 0);
11695 /* We've already validated the memory, and we're creating a
11696 new pseudo destination. The predicates really can't
11697 fail. */
11698 reg = gen_reg_rtx (mode);
11700 /* Nor can the insn generator. */
11701 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11702 emit_insn (insn);
11703 return reg;
11705 else if (STRICT_ALIGNMENT)
11707 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11708 poly_uint64 temp_size = mode_size;
11709 if (GET_MODE (op0) != BLKmode)
11710 temp_size = upper_bound (temp_size,
11711 GET_MODE_SIZE (GET_MODE (op0)));
11712 rtx new_rtx
11713 = assign_stack_temp_for_type (mode, temp_size, type);
11714 rtx new_with_op0_mode
11715 = adjust_address (new_rtx, GET_MODE (op0), 0);
11717 gcc_assert (!TREE_ADDRESSABLE (exp));
11719 if (GET_MODE (op0) == BLKmode)
11721 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11722 emit_block_move (new_with_op0_mode, op0, size_rtx,
11723 (modifier == EXPAND_STACK_PARM
11724 ? BLOCK_OP_CALL_PARM
11725 : BLOCK_OP_NORMAL));
11727 else
11728 emit_move_insn (new_with_op0_mode, op0);
11730 op0 = new_rtx;
11734 op0 = adjust_address (op0, mode, 0);
11737 return op0;
11739 case MODIFY_EXPR:
11741 tree lhs = treeop0;
11742 tree rhs = treeop1;
11743 gcc_assert (ignore);
11745 /* Check for |= or &= of a bitfield of size one into another bitfield
11746 of size 1. In this case, (unless we need the result of the
11747 assignment) we can do this more efficiently with a
11748 test followed by an assignment, if necessary.
11750 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11751 things change so we do, this code should be enhanced to
11752 support it. */
11753 if (TREE_CODE (lhs) == COMPONENT_REF
11754 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11755 || TREE_CODE (rhs) == BIT_AND_EXPR)
11756 && TREE_OPERAND (rhs, 0) == lhs
11757 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11758 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11759 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11761 rtx_code_label *label = gen_label_rtx ();
11762 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11763 profile_probability prob = profile_probability::uninitialized ();
11764 if (value)
11765 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11766 else
11767 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11768 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11769 false);
11770 do_pending_stack_adjust ();
11771 emit_label (label);
11772 return const0_rtx;
11775 expand_assignment (lhs, rhs, false);
11776 return const0_rtx;
11779 case ADDR_EXPR:
11780 return expand_expr_addr_expr (exp, target, tmode, modifier);
11782 case REALPART_EXPR:
11783 op0 = expand_normal (treeop0);
11784 return read_complex_part (op0, false);
11786 case IMAGPART_EXPR:
11787 op0 = expand_normal (treeop0);
11788 return read_complex_part (op0, true);
11790 case RETURN_EXPR:
11791 case LABEL_EXPR:
11792 case GOTO_EXPR:
11793 case SWITCH_EXPR:
11794 case ASM_EXPR:
11795 /* Expanded in cfgexpand.c. */
11796 gcc_unreachable ();
11798 case TRY_CATCH_EXPR:
11799 case CATCH_EXPR:
11800 case EH_FILTER_EXPR:
11801 case TRY_FINALLY_EXPR:
11802 case EH_ELSE_EXPR:
11803 /* Lowered by tree-eh.c. */
11804 gcc_unreachable ();
11806 case WITH_CLEANUP_EXPR:
11807 case CLEANUP_POINT_EXPR:
11808 case TARGET_EXPR:
11809 case CASE_LABEL_EXPR:
11810 case VA_ARG_EXPR:
11811 case BIND_EXPR:
11812 case INIT_EXPR:
11813 case CONJ_EXPR:
11814 case COMPOUND_EXPR:
11815 case PREINCREMENT_EXPR:
11816 case PREDECREMENT_EXPR:
11817 case POSTINCREMENT_EXPR:
11818 case POSTDECREMENT_EXPR:
11819 case LOOP_EXPR:
11820 case EXIT_EXPR:
11821 case COMPOUND_LITERAL_EXPR:
11822 /* Lowered by gimplify.c. */
11823 gcc_unreachable ();
11825 case FDESC_EXPR:
11826 /* Function descriptors are not valid except for as
11827 initialization constants, and should not be expanded. */
11828 gcc_unreachable ();
11830 case WITH_SIZE_EXPR:
11831 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11832 have pulled out the size to use in whatever context it needed. */
11833 return expand_expr_real (treeop0, original_target, tmode,
11834 modifier, alt_rtl, inner_reference_p);
11836 default:
11837 return expand_expr_real_2 (&ops, target, tmode, modifier);
11841 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11842 signedness of TYPE), possibly returning the result in TARGET.
11843 TYPE is known to be a partial integer type. */
11844 static rtx
11845 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11847 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11848 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11849 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11850 && (!target || GET_MODE (target) == mode));
11852 /* For constant values, reduce using wide_int_to_tree. */
11853 if (poly_int_rtx_p (exp))
11855 auto value = wi::to_poly_wide (exp, mode);
11856 tree t = wide_int_to_tree (type, value);
11857 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11859 else if (TYPE_UNSIGNED (type))
11861 rtx mask = immed_wide_int_const
11862 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11863 return expand_and (mode, exp, mask, target);
11865 else
11867 int count = GET_MODE_PRECISION (mode) - prec;
11868 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11869 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11873 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11874 when applied to the address of EXP produces an address known to be
11875 aligned more than BIGGEST_ALIGNMENT. */
11877 static int
11878 is_aligning_offset (const_tree offset, const_tree exp)
11880 /* Strip off any conversions. */
11881 while (CONVERT_EXPR_P (offset))
11882 offset = TREE_OPERAND (offset, 0);
11884 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11885 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11886 if (TREE_CODE (offset) != BIT_AND_EXPR
11887 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11888 || compare_tree_int (TREE_OPERAND (offset, 1),
11889 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11890 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11891 return 0;
11893 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11894 It must be NEGATE_EXPR. Then strip any more conversions. */
11895 offset = TREE_OPERAND (offset, 0);
11896 while (CONVERT_EXPR_P (offset))
11897 offset = TREE_OPERAND (offset, 0);
11899 if (TREE_CODE (offset) != NEGATE_EXPR)
11900 return 0;
11902 offset = TREE_OPERAND (offset, 0);
11903 while (CONVERT_EXPR_P (offset))
11904 offset = TREE_OPERAND (offset, 0);
11906 /* This must now be the address of EXP. */
11907 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11910 /* Return a STRING_CST corresponding to ARG's constant initializer either
11911 if it's a string constant, or, when VALREP is set, any other constant,
11912 or null otherwise.
11913 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
11914 within the byte string that ARG is references. If nonnull set *MEM_SIZE
11915 to the size of the byte string. If nonnull, set *DECL to the constant
11916 declaration ARG refers to. */
11918 static tree
11919 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
11920 bool valrep = false)
11922 tree dummy = NULL_TREE;
11923 if (!mem_size)
11924 mem_size = &dummy;
11926 /* Store the type of the original expression before conversions
11927 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
11928 removed. */
11929 tree argtype = TREE_TYPE (arg);
11931 tree array;
11932 STRIP_NOPS (arg);
11934 /* Non-constant index into the character array in an ARRAY_REF
11935 expression or null. */
11936 tree varidx = NULL_TREE;
11938 poly_int64 base_off = 0;
11940 if (TREE_CODE (arg) == ADDR_EXPR)
11942 arg = TREE_OPERAND (arg, 0);
11943 tree ref = arg;
11944 if (TREE_CODE (arg) == ARRAY_REF)
11946 tree idx = TREE_OPERAND (arg, 1);
11947 if (TREE_CODE (idx) != INTEGER_CST)
11949 /* From a pointer (but not array) argument extract the variable
11950 index to prevent get_addr_base_and_unit_offset() from failing
11951 due to it. Use it later to compute the non-constant offset
11952 into the string and return it to the caller. */
11953 varidx = idx;
11954 ref = TREE_OPERAND (arg, 0);
11956 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11957 return NULL_TREE;
11959 if (!integer_zerop (array_ref_low_bound (arg)))
11960 return NULL_TREE;
11962 if (!integer_onep (array_ref_element_size (arg)))
11963 return NULL_TREE;
11966 array = get_addr_base_and_unit_offset (ref, &base_off);
11967 if (!array
11968 || (TREE_CODE (array) != VAR_DECL
11969 && TREE_CODE (array) != CONST_DECL
11970 && TREE_CODE (array) != STRING_CST))
11971 return NULL_TREE;
11973 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11975 tree arg0 = TREE_OPERAND (arg, 0);
11976 tree arg1 = TREE_OPERAND (arg, 1);
11978 tree offset;
11979 tree str = string_constant (arg0, &offset, mem_size, decl);
11980 if (!str)
11982 str = string_constant (arg1, &offset, mem_size, decl);
11983 arg1 = arg0;
11986 if (str)
11988 /* Avoid pointers to arrays (see bug 86622). */
11989 if (POINTER_TYPE_P (TREE_TYPE (arg))
11990 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
11991 && !(decl && !*decl)
11992 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11993 && tree_fits_uhwi_p (*mem_size)
11994 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11995 return NULL_TREE;
11997 tree type = TREE_TYPE (offset);
11998 arg1 = fold_convert (type, arg1);
11999 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12000 return str;
12002 return NULL_TREE;
12004 else if (TREE_CODE (arg) == SSA_NAME)
12006 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12007 if (!is_gimple_assign (stmt))
12008 return NULL_TREE;
12010 tree rhs1 = gimple_assign_rhs1 (stmt);
12011 tree_code code = gimple_assign_rhs_code (stmt);
12012 if (code == ADDR_EXPR)
12013 return string_constant (rhs1, ptr_offset, mem_size, decl);
12014 else if (code != POINTER_PLUS_EXPR)
12015 return NULL_TREE;
12017 tree offset;
12018 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12020 /* Avoid pointers to arrays (see bug 86622). */
12021 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12022 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12023 && !(decl && !*decl)
12024 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12025 && tree_fits_uhwi_p (*mem_size)
12026 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12027 return NULL_TREE;
12029 tree rhs2 = gimple_assign_rhs2 (stmt);
12030 tree type = TREE_TYPE (offset);
12031 rhs2 = fold_convert (type, rhs2);
12032 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12033 return str;
12035 return NULL_TREE;
12037 else if (DECL_P (arg))
12038 array = arg;
12039 else
12040 return NULL_TREE;
12042 tree offset = wide_int_to_tree (sizetype, base_off);
12043 if (varidx)
12045 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12046 return NULL_TREE;
12048 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12049 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12050 if (TREE_CODE (chartype) != INTEGER_TYPE)
12051 return NULL;
12053 offset = fold_convert (sizetype, varidx);
12056 if (TREE_CODE (array) == STRING_CST)
12058 *ptr_offset = fold_convert (sizetype, offset);
12059 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12060 if (decl)
12061 *decl = NULL_TREE;
12062 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12063 >= TREE_STRING_LENGTH (array));
12064 return array;
12067 tree init = ctor_for_folding (array);
12068 if (!init || init == error_mark_node)
12069 return NULL_TREE;
12071 if (valrep)
12073 HOST_WIDE_INT cstoff;
12074 if (!base_off.is_constant (&cstoff))
12075 return NULL_TREE;
12077 /* Check that the host and target are sane. */
12078 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12079 return NULL_TREE;
12081 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12082 if (typesz <= 0 || (int) typesz != typesz)
12083 return NULL_TREE;
12085 HOST_WIDE_INT size = typesz;
12086 if (VAR_P (array)
12087 && DECL_SIZE_UNIT (array)
12088 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12090 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12091 gcc_checking_assert (size >= typesz);
12094 /* If value representation was requested convert the initializer
12095 for the whole array or object into a string of bytes forming
12096 its value representation and return it. */
12097 unsigned char *bytes = XNEWVEC (unsigned char, size);
12098 int r = native_encode_initializer (init, bytes, size);
12099 if (r < typesz)
12101 XDELETEVEC (bytes);
12102 return NULL_TREE;
12105 if (r < size)
12106 memset (bytes + r, '\0', size - r);
12108 const char *p = reinterpret_cast<const char *>(bytes);
12109 init = build_string_literal (size, p, char_type_node);
12110 init = TREE_OPERAND (init, 0);
12111 init = TREE_OPERAND (init, 0);
12112 XDELETE (bytes);
12114 *mem_size = size_int (TREE_STRING_LENGTH (init));
12115 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12117 if (decl)
12118 *decl = array;
12120 return init;
12123 if (TREE_CODE (init) == CONSTRUCTOR)
12125 /* Convert the 64-bit constant offset to a wider type to avoid
12126 overflow and use it to obtain the initializer for the subobject
12127 it points into. */
12128 offset_int wioff;
12129 if (!base_off.is_constant (&wioff))
12130 return NULL_TREE;
12132 wioff *= BITS_PER_UNIT;
12133 if (!wi::fits_uhwi_p (wioff))
12134 return NULL_TREE;
12136 base_off = wioff.to_uhwi ();
12137 unsigned HOST_WIDE_INT fieldoff = 0;
12138 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12139 &fieldoff);
12140 if (!init || init == error_mark_node)
12141 return NULL_TREE;
12143 HOST_WIDE_INT cstoff;
12144 if (!base_off.is_constant (&cstoff))
12145 return NULL_TREE;
12147 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12148 tree off = build_int_cst (sizetype, cstoff);
12149 if (varidx)
12150 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12151 else
12152 offset = off;
12155 *ptr_offset = offset;
12157 tree inittype = TREE_TYPE (init);
12159 if (TREE_CODE (init) == INTEGER_CST
12160 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12161 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12163 /* Check that the host and target are sane. */
12164 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12165 return NULL_TREE;
12167 /* For a reference to (address of) a single constant character,
12168 store the native representation of the character in CHARBUF.
12169 If the reference is to an element of an array or a member
12170 of a struct, only consider narrow characters until ctors
12171 for wide character arrays are transformed to STRING_CSTs
12172 like those for narrow arrays. */
12173 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12174 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12175 if (len > 0)
12177 /* Construct a string literal with elements of INITTYPE and
12178 the representation above. Then strip
12179 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12180 init = build_string_literal (len, (char *)charbuf, inittype);
12181 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12185 tree initsize = TYPE_SIZE_UNIT (inittype);
12187 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12189 /* Fold an empty/zero constructor for an implicitly initialized
12190 object or subobject into the empty string. */
12192 /* Determine the character type from that of the original
12193 expression. */
12194 tree chartype = argtype;
12195 if (POINTER_TYPE_P (chartype))
12196 chartype = TREE_TYPE (chartype);
12197 while (TREE_CODE (chartype) == ARRAY_TYPE)
12198 chartype = TREE_TYPE (chartype);
12200 if (INTEGRAL_TYPE_P (chartype)
12201 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12203 /* Convert a char array to an empty STRING_CST having an array
12204 of the expected type and size. */
12205 if (!initsize)
12206 initsize = integer_zero_node;
12208 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12209 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12210 return NULL_TREE;
12212 init = build_string_literal (size, NULL, chartype, size);
12213 init = TREE_OPERAND (init, 0);
12214 init = TREE_OPERAND (init, 0);
12216 *ptr_offset = integer_zero_node;
12220 if (decl)
12221 *decl = array;
12223 if (TREE_CODE (init) != STRING_CST)
12224 return NULL_TREE;
12226 *mem_size = initsize;
12228 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12230 return init;
12233 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12234 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12235 non-constant) offset in bytes within the string that ARG is accessing.
12236 If MEM_SIZE is non-zero the storage size of the memory is returned.
12237 If DECL is non-zero the constant declaration is returned if available. */
12239 tree
12240 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12242 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12245 /* Similar to string_constant, return a STRING_CST corresponding
12246 to the value representation of the first argument if it's
12247 a constant. */
12249 tree
12250 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12252 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12255 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12256 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12257 for C2 > 0 to x & C3 == C2
12258 for C2 < 0 to x & C3 == (C2 & C3). */
12259 enum tree_code
12260 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12262 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12263 tree treeop0 = gimple_assign_rhs1 (stmt);
12264 tree treeop1 = gimple_assign_rhs2 (stmt);
12265 tree type = TREE_TYPE (*arg0);
12266 scalar_int_mode mode;
12267 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12268 return code;
12269 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12270 || TYPE_PRECISION (type) <= 1
12271 || TYPE_UNSIGNED (type)
12272 /* Signed x % c == 0 should have been optimized into unsigned modulo
12273 earlier. */
12274 || integer_zerop (*arg1)
12275 /* If c is known to be non-negative, modulo will be expanded as unsigned
12276 modulo. */
12277 || get_range_pos_neg (treeop0) == 1)
12278 return code;
12280 /* x % c == d where d < 0 && d <= -c should be always false. */
12281 if (tree_int_cst_sgn (*arg1) == -1
12282 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12283 return code;
12285 int prec = TYPE_PRECISION (type);
12286 wide_int w = wi::to_wide (treeop1) - 1;
12287 w |= wi::shifted_mask (0, prec - 1, true, prec);
12288 tree c3 = wide_int_to_tree (type, w);
12289 tree c4 = *arg1;
12290 if (tree_int_cst_sgn (*arg1) == -1)
12291 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12293 rtx op0 = expand_normal (treeop0);
12294 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12296 bool speed_p = optimize_insn_for_speed_p ();
12298 do_pending_stack_adjust ();
12300 location_t loc = gimple_location (stmt);
12301 struct separate_ops ops;
12302 ops.code = TRUNC_MOD_EXPR;
12303 ops.location = loc;
12304 ops.type = TREE_TYPE (treeop0);
12305 ops.op0 = treeop0;
12306 ops.op1 = treeop1;
12307 ops.op2 = NULL_TREE;
12308 start_sequence ();
12309 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12310 EXPAND_NORMAL);
12311 rtx_insn *moinsns = get_insns ();
12312 end_sequence ();
12314 unsigned mocost = seq_cost (moinsns, speed_p);
12315 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12316 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12318 ops.code = BIT_AND_EXPR;
12319 ops.location = loc;
12320 ops.type = TREE_TYPE (treeop0);
12321 ops.op0 = treeop0;
12322 ops.op1 = c3;
12323 ops.op2 = NULL_TREE;
12324 start_sequence ();
12325 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12326 EXPAND_NORMAL);
12327 rtx_insn *muinsns = get_insns ();
12328 end_sequence ();
12330 unsigned mucost = seq_cost (muinsns, speed_p);
12331 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12332 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12334 if (mocost <= mucost)
12336 emit_insn (moinsns);
12337 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12338 return code;
12341 emit_insn (muinsns);
12342 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12343 *arg1 = c4;
12344 return code;
12347 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12348 If C1 is odd to:
12349 (X - C2) * C3 <= C4 (or >), where
12350 C3 is modular multiplicative inverse of C1 and 1<<prec and
12351 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12352 if C2 > ((1<<prec) - 1) % C1).
12353 If C1 is even, S = ctz (C1) and C2 is 0, use
12354 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12355 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12357 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12358 unsigned):
12359 (X * C3) + C4 <= 2 * C4, where
12360 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12361 C4 is ((1<<(prec - 1) - 1) / C1).
12362 If C1 is even, S = ctz(C1), use
12363 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12364 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12365 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12367 See the Hacker's Delight book, section 10-17. */
12368 enum tree_code
12369 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12371 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12372 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12374 if (optimize < 2)
12375 return code;
12377 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12378 if (stmt == NULL)
12379 return code;
12381 tree treeop0 = gimple_assign_rhs1 (stmt);
12382 tree treeop1 = gimple_assign_rhs2 (stmt);
12383 if (TREE_CODE (treeop0) != SSA_NAME
12384 || TREE_CODE (treeop1) != INTEGER_CST
12385 /* Don't optimize the undefined behavior case x % 0;
12386 x % 1 should have been optimized into zero, punt if
12387 it makes it here for whatever reason;
12388 x % -c should have been optimized into x % c. */
12389 || compare_tree_int (treeop1, 2) <= 0
12390 /* Likewise x % c == d where d >= c should be always false. */
12391 || tree_int_cst_le (treeop1, *arg1))
12392 return code;
12394 /* Unsigned x % pow2 is handled right already, for signed
12395 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12396 if (integer_pow2p (treeop1))
12397 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12399 tree type = TREE_TYPE (*arg0);
12400 scalar_int_mode mode;
12401 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12402 return code;
12403 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12404 || TYPE_PRECISION (type) <= 1)
12405 return code;
12407 signop sgn = UNSIGNED;
12408 /* If both operands are known to have the sign bit clear, handle
12409 even the signed modulo case as unsigned. treeop1 is always
12410 positive >= 2, checked above. */
12411 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12412 sgn = SIGNED;
12414 if (!TYPE_UNSIGNED (type))
12416 if (tree_int_cst_sgn (*arg1) == -1)
12417 return code;
12418 type = unsigned_type_for (type);
12419 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12420 return code;
12423 int prec = TYPE_PRECISION (type);
12424 wide_int w = wi::to_wide (treeop1);
12425 int shift = wi::ctz (w);
12426 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12427 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12428 If C1 is odd, we can handle all cases by subtracting
12429 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12430 e.g. by testing for overflow on the subtraction, punt on that for now
12431 though. */
12432 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12434 if (sgn == SIGNED)
12435 return code;
12436 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12437 if (wi::gtu_p (wi::to_wide (*arg1), x))
12438 return code;
12441 imm_use_iterator imm_iter;
12442 use_operand_p use_p;
12443 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12445 gimple *use_stmt = USE_STMT (use_p);
12446 /* Punt if treeop0 is used in the same bb in a division
12447 or another modulo with the same divisor. We should expect
12448 the division and modulo combined together. */
12449 if (use_stmt == stmt
12450 || gimple_bb (use_stmt) != gimple_bb (stmt))
12451 continue;
12452 if (!is_gimple_assign (use_stmt)
12453 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12454 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12455 continue;
12456 if (gimple_assign_rhs1 (use_stmt) != treeop0
12457 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12458 continue;
12459 return code;
12462 w = wi::lrshift (w, shift);
12463 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12464 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12465 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12466 tree c3 = wide_int_to_tree (type, m);
12467 tree c5 = NULL_TREE;
12468 wide_int d, e;
12469 if (sgn == UNSIGNED)
12471 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12472 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12473 otherwise use < or subtract one from C4. E.g. for
12474 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12475 x % 3U == 1 already needs to be
12476 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12477 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12478 d -= 1;
12479 if (shift)
12480 d = wi::lrshift (d, shift);
12482 else
12484 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12485 if (!shift)
12486 d = wi::lshift (e, 1);
12487 else
12489 e = wi::bit_and (e, wi::mask (shift, true, prec));
12490 d = wi::lrshift (e, shift - 1);
12492 c5 = wide_int_to_tree (type, e);
12494 tree c4 = wide_int_to_tree (type, d);
12496 rtx op0 = expand_normal (treeop0);
12497 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12499 bool speed_p = optimize_insn_for_speed_p ();
12501 do_pending_stack_adjust ();
12503 location_t loc = gimple_location (stmt);
12504 struct separate_ops ops;
12505 ops.code = TRUNC_MOD_EXPR;
12506 ops.location = loc;
12507 ops.type = TREE_TYPE (treeop0);
12508 ops.op0 = treeop0;
12509 ops.op1 = treeop1;
12510 ops.op2 = NULL_TREE;
12511 start_sequence ();
12512 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12513 EXPAND_NORMAL);
12514 rtx_insn *moinsns = get_insns ();
12515 end_sequence ();
12517 unsigned mocost = seq_cost (moinsns, speed_p);
12518 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12519 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12521 tree t = fold_convert_loc (loc, type, treeop0);
12522 if (!integer_zerop (*arg1))
12523 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12524 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12525 if (sgn == SIGNED)
12526 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12527 if (shift)
12529 tree s = build_int_cst (NULL_TREE, shift);
12530 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12533 start_sequence ();
12534 rtx mur = expand_normal (t);
12535 rtx_insn *muinsns = get_insns ();
12536 end_sequence ();
12538 unsigned mucost = seq_cost (muinsns, speed_p);
12539 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12540 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12542 if (mocost <= mucost)
12544 emit_insn (moinsns);
12545 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12546 return code;
12549 emit_insn (muinsns);
12550 *arg0 = make_tree (type, mur);
12551 *arg1 = c4;
12552 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12555 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12557 void
12558 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12560 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12561 || code == LT_EXPR || code == LE_EXPR);
12562 gcc_checking_assert (integer_zerop (*arg1));
12564 if (!optimize)
12565 return;
12567 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12568 if (stmt == NULL)
12569 return;
12571 tree treeop0 = gimple_assign_rhs1 (stmt);
12572 tree treeop1 = gimple_assign_rhs2 (stmt);
12573 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12574 return;
12576 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12577 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12578 "assuming signed overflow does not occur when "
12579 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12580 op_symbol_code (code), op_symbol_code (code));
12582 *arg0 = treeop0;
12583 *arg1 = treeop1;
12586 /* Generate code to calculate OPS, and exploded expression
12587 using a store-flag instruction and return an rtx for the result.
12588 OPS reflects a comparison.
12590 If TARGET is nonzero, store the result there if convenient.
12592 Return zero if there is no suitable set-flag instruction
12593 available on this machine.
12595 Once expand_expr has been called on the arguments of the comparison,
12596 we are committed to doing the store flag, since it is not safe to
12597 re-evaluate the expression. We emit the store-flag insn by calling
12598 emit_store_flag, but only expand the arguments if we have a reason
12599 to believe that emit_store_flag will be successful. If we think that
12600 it will, but it isn't, we have to simulate the store-flag with a
12601 set/jump/set sequence. */
12603 static rtx
12604 do_store_flag (sepops ops, rtx target, machine_mode mode)
12606 enum rtx_code code;
12607 tree arg0, arg1, type;
12608 machine_mode operand_mode;
12609 int unsignedp;
12610 rtx op0, op1;
12611 rtx subtarget = target;
12612 location_t loc = ops->location;
12614 arg0 = ops->op0;
12615 arg1 = ops->op1;
12617 /* Don't crash if the comparison was erroneous. */
12618 if (arg0 == error_mark_node || arg1 == error_mark_node)
12619 return const0_rtx;
12621 type = TREE_TYPE (arg0);
12622 operand_mode = TYPE_MODE (type);
12623 unsignedp = TYPE_UNSIGNED (type);
12625 /* We won't bother with BLKmode store-flag operations because it would mean
12626 passing a lot of information to emit_store_flag. */
12627 if (operand_mode == BLKmode)
12628 return 0;
12630 /* We won't bother with store-flag operations involving function pointers
12631 when function pointers must be canonicalized before comparisons. */
12632 if (targetm.have_canonicalize_funcptr_for_compare ()
12633 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12634 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12635 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12636 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12637 return 0;
12639 STRIP_NOPS (arg0);
12640 STRIP_NOPS (arg1);
12642 /* For vector typed comparisons emit code to generate the desired
12643 all-ones or all-zeros mask. */
12644 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12646 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12647 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12648 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12649 return expand_vec_cmp_expr (ops->type, ifexp, target);
12650 else
12651 gcc_unreachable ();
12654 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12655 into (x - C2) * C3 < C4. */
12656 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12657 && TREE_CODE (arg0) == SSA_NAME
12658 && TREE_CODE (arg1) == INTEGER_CST)
12660 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12661 &arg0, &arg1);
12662 if (new_code != ops->code)
12664 struct separate_ops nops = *ops;
12665 nops.code = ops->code = new_code;
12666 nops.op0 = arg0;
12667 nops.op1 = arg1;
12668 nops.type = TREE_TYPE (arg0);
12669 return do_store_flag (&nops, target, mode);
12673 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12674 if (!unsignedp
12675 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12676 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12677 && integer_zerop (arg1)
12678 && TREE_CODE (arg0) == SSA_NAME)
12679 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12681 /* Get the rtx comparison code to use. We know that EXP is a comparison
12682 operation of some type. Some comparisons against 1 and -1 can be
12683 converted to comparisons with zero. Do so here so that the tests
12684 below will be aware that we have a comparison with zero. These
12685 tests will not catch constants in the first operand, but constants
12686 are rarely passed as the first operand. */
12688 switch (ops->code)
12690 case EQ_EXPR:
12691 code = EQ;
12692 break;
12693 case NE_EXPR:
12694 code = NE;
12695 break;
12696 case LT_EXPR:
12697 if (integer_onep (arg1))
12698 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12699 else
12700 code = unsignedp ? LTU : LT;
12701 break;
12702 case LE_EXPR:
12703 if (! unsignedp && integer_all_onesp (arg1))
12704 arg1 = integer_zero_node, code = LT;
12705 else
12706 code = unsignedp ? LEU : LE;
12707 break;
12708 case GT_EXPR:
12709 if (! unsignedp && integer_all_onesp (arg1))
12710 arg1 = integer_zero_node, code = GE;
12711 else
12712 code = unsignedp ? GTU : GT;
12713 break;
12714 case GE_EXPR:
12715 if (integer_onep (arg1))
12716 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12717 else
12718 code = unsignedp ? GEU : GE;
12719 break;
12721 case UNORDERED_EXPR:
12722 code = UNORDERED;
12723 break;
12724 case ORDERED_EXPR:
12725 code = ORDERED;
12726 break;
12727 case UNLT_EXPR:
12728 code = UNLT;
12729 break;
12730 case UNLE_EXPR:
12731 code = UNLE;
12732 break;
12733 case UNGT_EXPR:
12734 code = UNGT;
12735 break;
12736 case UNGE_EXPR:
12737 code = UNGE;
12738 break;
12739 case UNEQ_EXPR:
12740 code = UNEQ;
12741 break;
12742 case LTGT_EXPR:
12743 code = LTGT;
12744 break;
12746 default:
12747 gcc_unreachable ();
12750 /* Put a constant second. */
12751 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12752 || TREE_CODE (arg0) == FIXED_CST)
12754 std::swap (arg0, arg1);
12755 code = swap_condition (code);
12758 /* If this is an equality or inequality test of a single bit, we can
12759 do this by shifting the bit being tested to the low-order bit and
12760 masking the result with the constant 1. If the condition was EQ,
12761 we xor it with 1. This does not require an scc insn and is faster
12762 than an scc insn even if we have it.
12764 The code to make this transformation was moved into fold_single_bit_test,
12765 so we just call into the folder and expand its result. */
12767 if ((code == NE || code == EQ)
12768 && integer_zerop (arg1)
12769 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12771 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12772 if (srcstmt
12773 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12775 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12776 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12777 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12778 gimple_assign_rhs1 (srcstmt),
12779 gimple_assign_rhs2 (srcstmt));
12780 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12781 if (temp)
12782 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12786 if (! get_subtarget (target)
12787 || GET_MODE (subtarget) != operand_mode)
12788 subtarget = 0;
12790 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12792 if (target == 0)
12793 target = gen_reg_rtx (mode);
12795 /* Try a cstore if possible. */
12796 return emit_store_flag_force (target, code, op0, op1,
12797 operand_mode, unsignedp,
12798 (TYPE_PRECISION (ops->type) == 1
12799 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12802 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12803 0 otherwise (i.e. if there is no casesi instruction).
12805 DEFAULT_PROBABILITY is the probability of jumping to the default
12806 label. */
12808 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12809 rtx table_label, rtx default_label, rtx fallback_label,
12810 profile_probability default_probability)
12812 class expand_operand ops[5];
12813 scalar_int_mode index_mode = SImode;
12814 rtx op1, op2, index;
12816 if (! targetm.have_casesi ())
12817 return 0;
12819 /* The index must be some form of integer. Convert it to SImode. */
12820 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12821 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12823 rtx rangertx = expand_normal (range);
12825 /* We must handle the endpoints in the original mode. */
12826 index_expr = build2 (MINUS_EXPR, index_type,
12827 index_expr, minval);
12828 minval = integer_zero_node;
12829 index = expand_normal (index_expr);
12830 if (default_label)
12831 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12832 omode, 1, default_label,
12833 default_probability);
12834 /* Now we can safely truncate. */
12835 index = convert_to_mode (index_mode, index, 0);
12837 else
12839 if (omode != index_mode)
12841 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12842 index_expr = fold_convert (index_type, index_expr);
12845 index = expand_normal (index_expr);
12848 do_pending_stack_adjust ();
12850 op1 = expand_normal (minval);
12851 op2 = expand_normal (range);
12853 create_input_operand (&ops[0], index, index_mode);
12854 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12855 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12856 create_fixed_operand (&ops[3], table_label);
12857 create_fixed_operand (&ops[4], (default_label
12858 ? default_label
12859 : fallback_label));
12860 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12861 return 1;
12864 /* Attempt to generate a tablejump instruction; same concept. */
12865 /* Subroutine of the next function.
12867 INDEX is the value being switched on, with the lowest value
12868 in the table already subtracted.
12869 MODE is its expected mode (needed if INDEX is constant).
12870 RANGE is the length of the jump table.
12871 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12873 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12874 index value is out of range.
12875 DEFAULT_PROBABILITY is the probability of jumping to
12876 the default label. */
12878 static void
12879 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12880 rtx default_label, profile_probability default_probability)
12882 rtx temp, vector;
12884 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12885 cfun->cfg->max_jumptable_ents = INTVAL (range);
12887 /* Do an unsigned comparison (in the proper mode) between the index
12888 expression and the value which represents the length of the range.
12889 Since we just finished subtracting the lower bound of the range
12890 from the index expression, this comparison allows us to simultaneously
12891 check that the original index expression value is both greater than
12892 or equal to the minimum value of the range and less than or equal to
12893 the maximum value of the range. */
12895 if (default_label)
12896 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12897 default_label, default_probability);
12899 /* If index is in range, it must fit in Pmode.
12900 Convert to Pmode so we can index with it. */
12901 if (mode != Pmode)
12903 unsigned int width;
12905 /* We know the value of INDEX is between 0 and RANGE. If we have a
12906 sign-extended subreg, and RANGE does not have the sign bit set, then
12907 we have a value that is valid for both sign and zero extension. In
12908 this case, we get better code if we sign extend. */
12909 if (GET_CODE (index) == SUBREG
12910 && SUBREG_PROMOTED_VAR_P (index)
12911 && SUBREG_PROMOTED_SIGNED_P (index)
12912 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
12913 <= HOST_BITS_PER_WIDE_INT)
12914 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
12915 index = convert_to_mode (Pmode, index, 0);
12916 else
12917 index = convert_to_mode (Pmode, index, 1);
12920 /* Don't let a MEM slip through, because then INDEX that comes
12921 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
12922 and break_out_memory_refs will go to work on it and mess it up. */
12923 #ifdef PIC_CASE_VECTOR_ADDRESS
12924 if (flag_pic && !REG_P (index))
12925 index = copy_to_mode_reg (Pmode, index);
12926 #endif
12928 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
12929 GET_MODE_SIZE, because this indicates how large insns are. The other
12930 uses should all be Pmode, because they are addresses. This code
12931 could fail if addresses and insns are not the same size. */
12932 index = simplify_gen_binary (MULT, Pmode, index,
12933 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
12934 Pmode));
12935 index = simplify_gen_binary (PLUS, Pmode, index,
12936 gen_rtx_LABEL_REF (Pmode, table_label));
12938 #ifdef PIC_CASE_VECTOR_ADDRESS
12939 if (flag_pic)
12940 index = PIC_CASE_VECTOR_ADDRESS (index);
12941 else
12942 #endif
12943 index = memory_address (CASE_VECTOR_MODE, index);
12944 temp = gen_reg_rtx (CASE_VECTOR_MODE);
12945 vector = gen_const_mem (CASE_VECTOR_MODE, index);
12946 convert_move (temp, vector, 0);
12948 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
12950 /* If we are generating PIC code or if the table is PC-relative, the
12951 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
12952 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
12953 emit_barrier ();
12957 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
12958 rtx table_label, rtx default_label,
12959 profile_probability default_probability)
12961 rtx index;
12963 if (! targetm.have_tablejump ())
12964 return 0;
12966 index_expr = fold_build2 (MINUS_EXPR, index_type,
12967 fold_convert (index_type, index_expr),
12968 fold_convert (index_type, minval));
12969 index = expand_normal (index_expr);
12970 do_pending_stack_adjust ();
12972 do_tablejump (index, TYPE_MODE (index_type),
12973 convert_modes (TYPE_MODE (index_type),
12974 TYPE_MODE (TREE_TYPE (range)),
12975 expand_normal (range),
12976 TYPE_UNSIGNED (TREE_TYPE (range))),
12977 table_label, default_label, default_probability);
12978 return 1;
12981 /* Return a CONST_VECTOR rtx representing vector mask for
12982 a VECTOR_CST of booleans. */
12983 static rtx
12984 const_vector_mask_from_tree (tree exp)
12986 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12987 machine_mode inner = GET_MODE_INNER (mode);
12989 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12990 VECTOR_CST_NELTS_PER_PATTERN (exp));
12991 unsigned int count = builder.encoded_nelts ();
12992 for (unsigned int i = 0; i < count; ++i)
12994 tree elt = VECTOR_CST_ELT (exp, i);
12995 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
12996 if (integer_zerop (elt))
12997 builder.quick_push (CONST0_RTX (inner));
12998 else if (integer_onep (elt)
12999 || integer_minus_onep (elt))
13000 builder.quick_push (CONSTM1_RTX (inner));
13001 else
13002 gcc_unreachable ();
13004 return builder.build ();
13007 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13008 static rtx
13009 const_vector_from_tree (tree exp)
13011 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13013 if (initializer_zerop (exp))
13014 return CONST0_RTX (mode);
13016 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13017 return const_vector_mask_from_tree (exp);
13019 machine_mode inner = GET_MODE_INNER (mode);
13021 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13022 VECTOR_CST_NELTS_PER_PATTERN (exp));
13023 unsigned int count = builder.encoded_nelts ();
13024 for (unsigned int i = 0; i < count; ++i)
13026 tree elt = VECTOR_CST_ELT (exp, i);
13027 if (TREE_CODE (elt) == REAL_CST)
13028 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13029 inner));
13030 else if (TREE_CODE (elt) == FIXED_CST)
13031 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13032 inner));
13033 else
13034 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13035 inner));
13037 return builder.build ();
13040 /* Build a decl for a personality function given a language prefix. */
13042 tree
13043 build_personality_function (const char *lang)
13045 const char *unwind_and_version;
13046 tree decl, type;
13047 char *name;
13049 switch (targetm_common.except_unwind_info (&global_options))
13051 case UI_NONE:
13052 return NULL;
13053 case UI_SJLJ:
13054 unwind_and_version = "_sj0";
13055 break;
13056 case UI_DWARF2:
13057 case UI_TARGET:
13058 unwind_and_version = "_v0";
13059 break;
13060 case UI_SEH:
13061 unwind_and_version = "_seh0";
13062 break;
13063 default:
13064 gcc_unreachable ();
13067 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13069 type = build_function_type_list (unsigned_type_node,
13070 integer_type_node, integer_type_node,
13071 long_long_unsigned_type_node,
13072 ptr_type_node, ptr_type_node, NULL_TREE);
13073 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13074 get_identifier (name), type);
13075 DECL_ARTIFICIAL (decl) = 1;
13076 DECL_EXTERNAL (decl) = 1;
13077 TREE_PUBLIC (decl) = 1;
13079 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13080 are the flags assigned by targetm.encode_section_info. */
13081 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13083 return decl;
13086 /* Extracts the personality function of DECL and returns the corresponding
13087 libfunc. */
13090 get_personality_function (tree decl)
13092 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13093 enum eh_personality_kind pk;
13095 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13096 if (pk == eh_personality_none)
13097 return NULL;
13099 if (!personality
13100 && pk == eh_personality_any)
13101 personality = lang_hooks.eh_personality ();
13103 if (pk == eh_personality_lang)
13104 gcc_assert (personality != NULL_TREE);
13106 return XEXP (DECL_RTL (personality), 0);
13109 /* Returns a tree for the size of EXP in bytes. */
13111 static tree
13112 tree_expr_size (const_tree exp)
13114 if (DECL_P (exp)
13115 && DECL_SIZE_UNIT (exp) != 0)
13116 return DECL_SIZE_UNIT (exp);
13117 else
13118 return size_in_bytes (TREE_TYPE (exp));
13121 /* Return an rtx for the size in bytes of the value of EXP. */
13124 expr_size (tree exp)
13126 tree size;
13128 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13129 size = TREE_OPERAND (exp, 1);
13130 else
13132 size = tree_expr_size (exp);
13133 gcc_assert (size);
13134 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13137 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13140 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13141 if the size can vary or is larger than an integer. */
13143 static HOST_WIDE_INT
13144 int_expr_size (tree exp)
13146 tree size;
13148 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13149 size = TREE_OPERAND (exp, 1);
13150 else
13152 size = tree_expr_size (exp);
13153 gcc_assert (size);
13156 if (size == 0 || !tree_fits_shwi_p (size))
13157 return -1;
13159 return tree_to_shwi (size);