Fix typo in t-dimode
[official-gcc.git] / gcc / expr.c
blob5673902b1fcddb665c1544694c28bb862a29b3d3
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 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2512 && XVECLEN (dst, 0) > 1)
2513 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2514 else if (CONSTANT_P (src))
2516 if (known_eq (bytelen, ssize))
2517 tmps[i] = src;
2518 else
2520 rtx first, second;
2522 /* TODO: const_wide_int can have sizes other than this... */
2523 gcc_assert (known_eq (2 * bytelen, ssize));
2524 split_double (src, &first, &second);
2525 if (i)
2526 tmps[i] = second;
2527 else
2528 tmps[i] = first;
2531 else if (REG_P (src) && GET_MODE (src) == mode)
2532 tmps[i] = src;
2533 else
2534 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2535 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2536 mode, mode, false, NULL);
2538 if (maybe_ne (shift, 0))
2539 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2540 shift, tmps[i], 0);
2544 /* Emit code to move a block SRC of type TYPE to a block DST,
2545 where DST is non-consecutive registers represented by a PARALLEL.
2546 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2547 if not known. */
2549 void
2550 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2552 rtx *tmps;
2553 int i;
2555 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2556 emit_group_load_1 (tmps, dst, src, type, ssize);
2558 /* Copy the extracted pieces into the proper (probable) hard regs. */
2559 for (i = 0; i < XVECLEN (dst, 0); i++)
2561 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2562 if (d == NULL)
2563 continue;
2564 emit_move_insn (d, tmps[i]);
2568 /* Similar, but load SRC into new pseudos in a format that looks like
2569 PARALLEL. This can later be fed to emit_group_move to get things
2570 in the right place. */
2573 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2575 rtvec vec;
2576 int i;
2578 vec = rtvec_alloc (XVECLEN (parallel, 0));
2579 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2581 /* Convert the vector to look just like the original PARALLEL, except
2582 with the computed values. */
2583 for (i = 0; i < XVECLEN (parallel, 0); i++)
2585 rtx e = XVECEXP (parallel, 0, i);
2586 rtx d = XEXP (e, 0);
2588 if (d)
2590 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2591 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2593 RTVEC_ELT (vec, i) = e;
2596 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2599 /* Emit code to move a block SRC to block DST, where SRC and DST are
2600 non-consecutive groups of registers, each represented by a PARALLEL. */
2602 void
2603 emit_group_move (rtx dst, rtx src)
2605 int i;
2607 gcc_assert (GET_CODE (src) == PARALLEL
2608 && GET_CODE (dst) == PARALLEL
2609 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2611 /* Skip first entry if NULL. */
2612 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2613 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2614 XEXP (XVECEXP (src, 0, i), 0));
2617 /* Move a group of registers represented by a PARALLEL into pseudos. */
2620 emit_group_move_into_temps (rtx src)
2622 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2623 int i;
2625 for (i = 0; i < XVECLEN (src, 0); i++)
2627 rtx e = XVECEXP (src, 0, i);
2628 rtx d = XEXP (e, 0);
2630 if (d)
2631 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2632 RTVEC_ELT (vec, i) = e;
2635 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2638 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2639 where SRC is non-consecutive registers represented by a PARALLEL.
2640 SSIZE represents the total size of block ORIG_DST, or -1 if not
2641 known. */
2643 void
2644 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2645 poly_int64 ssize)
2647 rtx *tmps, dst;
2648 int start, finish, i;
2649 machine_mode m = GET_MODE (orig_dst);
2651 gcc_assert (GET_CODE (src) == PARALLEL);
2653 if (!SCALAR_INT_MODE_P (m)
2654 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2656 scalar_int_mode imode;
2657 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2659 dst = gen_reg_rtx (imode);
2660 emit_group_store (dst, src, type, ssize);
2661 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2663 else
2665 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2666 emit_group_store (dst, src, type, ssize);
2668 emit_move_insn (orig_dst, dst);
2669 return;
2672 /* Check for a NULL entry, used to indicate that the parameter goes
2673 both on the stack and in registers. */
2674 if (XEXP (XVECEXP (src, 0, 0), 0))
2675 start = 0;
2676 else
2677 start = 1;
2678 finish = XVECLEN (src, 0);
2680 tmps = XALLOCAVEC (rtx, finish);
2682 /* Copy the (probable) hard regs into pseudos. */
2683 for (i = start; i < finish; i++)
2685 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2686 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2688 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2689 emit_move_insn (tmps[i], reg);
2691 else
2692 tmps[i] = reg;
2695 /* If we won't be storing directly into memory, protect the real destination
2696 from strange tricks we might play. */
2697 dst = orig_dst;
2698 if (GET_CODE (dst) == PARALLEL)
2700 rtx temp;
2702 /* We can get a PARALLEL dst if there is a conditional expression in
2703 a return statement. In that case, the dst and src are the same,
2704 so no action is necessary. */
2705 if (rtx_equal_p (dst, src))
2706 return;
2708 /* It is unclear if we can ever reach here, but we may as well handle
2709 it. Allocate a temporary, and split this into a store/load to/from
2710 the temporary. */
2711 temp = assign_stack_temp (GET_MODE (dst), ssize);
2712 emit_group_store (temp, src, type, ssize);
2713 emit_group_load (dst, temp, type, ssize);
2714 return;
2716 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2718 machine_mode outer = GET_MODE (dst);
2719 machine_mode inner;
2720 poly_int64 bytepos;
2721 bool done = false;
2722 rtx temp;
2724 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2725 dst = gen_reg_rtx (outer);
2727 /* Make life a bit easier for combine. */
2728 /* If the first element of the vector is the low part
2729 of the destination mode, use a paradoxical subreg to
2730 initialize the destination. */
2731 if (start < finish)
2733 inner = GET_MODE (tmps[start]);
2734 bytepos = subreg_lowpart_offset (inner, outer);
2735 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2736 bytepos))
2738 temp = simplify_gen_subreg (outer, tmps[start],
2739 inner, 0);
2740 if (temp)
2742 emit_move_insn (dst, temp);
2743 done = true;
2744 start++;
2749 /* If the first element wasn't the low part, try the last. */
2750 if (!done
2751 && start < finish - 1)
2753 inner = GET_MODE (tmps[finish - 1]);
2754 bytepos = subreg_lowpart_offset (inner, outer);
2755 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2756 finish - 1), 1)),
2757 bytepos))
2759 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2760 inner, 0);
2761 if (temp)
2763 emit_move_insn (dst, temp);
2764 done = true;
2765 finish--;
2770 /* Otherwise, simply initialize the result to zero. */
2771 if (!done)
2772 emit_move_insn (dst, CONST0_RTX (outer));
2775 /* Process the pieces. */
2776 for (i = start; i < finish; i++)
2778 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2779 machine_mode mode = GET_MODE (tmps[i]);
2780 poly_int64 bytelen = GET_MODE_SIZE (mode);
2781 poly_uint64 adj_bytelen;
2782 rtx dest = dst;
2784 /* Handle trailing fragments that run over the size of the struct.
2785 It's the target's responsibility to make sure that the fragment
2786 cannot be strictly smaller in some cases and strictly larger
2787 in others. */
2788 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2789 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2790 adj_bytelen = ssize - bytepos;
2791 else
2792 adj_bytelen = bytelen;
2794 if (GET_CODE (dst) == CONCAT)
2796 if (known_le (bytepos + adj_bytelen,
2797 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2798 dest = XEXP (dst, 0);
2799 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2801 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2802 dest = XEXP (dst, 1);
2804 else
2806 machine_mode dest_mode = GET_MODE (dest);
2807 machine_mode tmp_mode = GET_MODE (tmps[i]);
2809 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2811 if (GET_MODE_ALIGNMENT (dest_mode)
2812 >= GET_MODE_ALIGNMENT (tmp_mode))
2814 dest = assign_stack_temp (dest_mode,
2815 GET_MODE_SIZE (dest_mode));
2816 emit_move_insn (adjust_address (dest,
2817 tmp_mode,
2818 bytepos),
2819 tmps[i]);
2820 dst = dest;
2822 else
2824 dest = assign_stack_temp (tmp_mode,
2825 GET_MODE_SIZE (tmp_mode));
2826 emit_move_insn (dest, tmps[i]);
2827 dst = adjust_address (dest, dest_mode, bytepos);
2829 break;
2833 /* Handle trailing fragments that run over the size of the struct. */
2834 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2836 /* store_bit_field always takes its value from the lsb.
2837 Move the fragment to the lsb if it's not already there. */
2838 if (
2839 #ifdef BLOCK_REG_PADDING
2840 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2841 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2842 #else
2843 BYTES_BIG_ENDIAN
2844 #endif
2847 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2848 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2849 shift, tmps[i], 0);
2852 /* Make sure not to write past the end of the struct. */
2853 store_bit_field (dest,
2854 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2855 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2856 VOIDmode, tmps[i], false);
2859 /* Optimize the access just a bit. */
2860 else if (MEM_P (dest)
2861 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2862 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2863 && multiple_p (bytepos * BITS_PER_UNIT,
2864 GET_MODE_ALIGNMENT (mode))
2865 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2866 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2868 else
2869 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2870 0, 0, mode, tmps[i], false);
2873 /* Copy from the pseudo into the (probable) hard reg. */
2874 if (orig_dst != dst)
2875 emit_move_insn (orig_dst, dst);
2878 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2879 of the value stored in X. */
2882 maybe_emit_group_store (rtx x, tree type)
2884 machine_mode mode = TYPE_MODE (type);
2885 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2886 if (GET_CODE (x) == PARALLEL)
2888 rtx result = gen_reg_rtx (mode);
2889 emit_group_store (result, x, type, int_size_in_bytes (type));
2890 return result;
2892 return x;
2895 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2897 This is used on targets that return BLKmode values in registers. */
2899 static void
2900 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2902 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2903 rtx src = NULL, dst = NULL;
2904 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2905 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2906 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2907 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2908 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2909 fixed_size_mode copy_mode;
2911 /* BLKmode registers created in the back-end shouldn't have survived. */
2912 gcc_assert (mode != BLKmode);
2914 /* If the structure doesn't take up a whole number of words, see whether
2915 SRCREG is padded on the left or on the right. If it's on the left,
2916 set PADDING_CORRECTION to the number of bits to skip.
2918 In most ABIs, the structure will be returned at the least end of
2919 the register, which translates to right padding on little-endian
2920 targets and left padding on big-endian targets. The opposite
2921 holds if the structure is returned at the most significant
2922 end of the register. */
2923 if (bytes % UNITS_PER_WORD != 0
2924 && (targetm.calls.return_in_msb (type)
2925 ? !BYTES_BIG_ENDIAN
2926 : BYTES_BIG_ENDIAN))
2927 padding_correction
2928 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2930 /* We can use a single move if we have an exact mode for the size. */
2931 else if (MEM_P (target)
2932 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2933 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2934 && bytes == GET_MODE_SIZE (mode))
2936 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2937 return;
2940 /* And if we additionally have the same mode for a register. */
2941 else if (REG_P (target)
2942 && GET_MODE (target) == mode
2943 && bytes == GET_MODE_SIZE (mode))
2945 emit_move_insn (target, srcreg);
2946 return;
2949 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2950 into a new pseudo which is a full word. */
2951 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2953 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2954 mode = word_mode;
2957 /* Copy the structure BITSIZE bits at a time. If the target lives in
2958 memory, take care of not reading/writing past its end by selecting
2959 a copy mode suited to BITSIZE. This should always be possible given
2960 how it is computed.
2962 If the target lives in register, make sure not to select a copy mode
2963 larger than the mode of the register.
2965 We could probably emit more efficient code for machines which do not use
2966 strict alignment, but it doesn't seem worth the effort at the current
2967 time. */
2969 copy_mode = word_mode;
2970 if (MEM_P (target))
2972 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2973 if (mem_mode.exists ())
2974 copy_mode = mem_mode.require ();
2976 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2977 copy_mode = tmode;
2979 for (bitpos = 0, xbitpos = padding_correction;
2980 bitpos < bytes * BITS_PER_UNIT;
2981 bitpos += bitsize, xbitpos += bitsize)
2983 /* We need a new source operand each time xbitpos is on a
2984 word boundary and when xbitpos == padding_correction
2985 (the first time through). */
2986 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2987 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2989 /* We need a new destination operand each time bitpos is on
2990 a word boundary. */
2991 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2992 dst = target;
2993 else if (bitpos % BITS_PER_WORD == 0)
2994 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2996 /* Use xbitpos for the source extraction (right justified) and
2997 bitpos for the destination store (left justified). */
2998 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2999 extract_bit_field (src, bitsize,
3000 xbitpos % BITS_PER_WORD, 1,
3001 NULL_RTX, copy_mode, copy_mode,
3002 false, NULL),
3003 false);
3007 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3008 register if it contains any data, otherwise return null.
3010 This is used on targets that return BLKmode values in registers. */
3013 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3015 int i, n_regs;
3016 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3017 unsigned int bitsize;
3018 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3019 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3020 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3021 fixed_size_mode dst_mode;
3022 scalar_int_mode min_mode;
3024 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3026 x = expand_normal (src);
3028 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3029 if (bytes == 0)
3030 return NULL_RTX;
3032 /* If the structure doesn't take up a whole number of words, see
3033 whether the register value should be padded on the left or on
3034 the right. Set PADDING_CORRECTION to the number of padding
3035 bits needed on the left side.
3037 In most ABIs, the structure will be returned at the least end of
3038 the register, which translates to right padding on little-endian
3039 targets and left padding on big-endian targets. The opposite
3040 holds if the structure is returned at the most significant
3041 end of the register. */
3042 if (bytes % UNITS_PER_WORD != 0
3043 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3044 ? !BYTES_BIG_ENDIAN
3045 : BYTES_BIG_ENDIAN))
3046 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3047 * BITS_PER_UNIT));
3049 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3050 dst_words = XALLOCAVEC (rtx, n_regs);
3051 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3052 min_mode = smallest_int_mode_for_size (bitsize);
3054 /* Copy the structure BITSIZE bits at a time. */
3055 for (bitpos = 0, xbitpos = padding_correction;
3056 bitpos < bytes * BITS_PER_UNIT;
3057 bitpos += bitsize, xbitpos += bitsize)
3059 /* We need a new destination pseudo each time xbitpos is
3060 on a word boundary and when xbitpos == padding_correction
3061 (the first time through). */
3062 if (xbitpos % BITS_PER_WORD == 0
3063 || xbitpos == padding_correction)
3065 /* Generate an appropriate register. */
3066 dst_word = gen_reg_rtx (word_mode);
3067 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3069 /* Clear the destination before we move anything into it. */
3070 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3073 /* Find the largest integer mode that can be used to copy all or as
3074 many bits as possible of the structure if the target supports larger
3075 copies. There are too many corner cases here w.r.t to alignments on
3076 the read/writes. So if there is any padding just use single byte
3077 operations. */
3078 opt_scalar_int_mode mode_iter;
3079 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3081 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3083 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3084 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3085 && msize <= BITS_PER_WORD)
3086 bitsize = msize;
3087 else
3088 break;
3092 /* We need a new source operand each time bitpos is on a word
3093 boundary. */
3094 if (bitpos % BITS_PER_WORD == 0)
3095 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3097 /* Use bitpos for the source extraction (left justified) and
3098 xbitpos for the destination store (right justified). */
3099 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3100 0, 0, word_mode,
3101 extract_bit_field (src_word, bitsize,
3102 bitpos % BITS_PER_WORD, 1,
3103 NULL_RTX, word_mode, word_mode,
3104 false, NULL),
3105 false);
3108 if (mode == BLKmode)
3110 /* Find the smallest integer mode large enough to hold the
3111 entire structure. */
3112 opt_scalar_int_mode mode_iter;
3113 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3114 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3115 break;
3117 /* A suitable mode should have been found. */
3118 mode = mode_iter.require ();
3121 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3122 dst_mode = word_mode;
3123 else
3124 dst_mode = mode;
3125 dst = gen_reg_rtx (dst_mode);
3127 for (i = 0; i < n_regs; i++)
3128 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3130 if (mode != dst_mode)
3131 dst = gen_lowpart (mode, dst);
3133 return dst;
3136 /* Add a USE expression for REG to the (possibly empty) list pointed
3137 to by CALL_FUSAGE. REG must denote a hard register. */
3139 void
3140 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3142 gcc_assert (REG_P (reg));
3144 if (!HARD_REGISTER_P (reg))
3145 return;
3147 *call_fusage
3148 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3151 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3152 to by CALL_FUSAGE. REG must denote a hard register. */
3154 void
3155 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3157 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3159 *call_fusage
3160 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3163 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3164 starting at REGNO. All of these registers must be hard registers. */
3166 void
3167 use_regs (rtx *call_fusage, int regno, int nregs)
3169 int i;
3171 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3173 for (i = 0; i < nregs; i++)
3174 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3177 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3178 PARALLEL REGS. This is for calls that pass values in multiple
3179 non-contiguous locations. The Irix 6 ABI has examples of this. */
3181 void
3182 use_group_regs (rtx *call_fusage, rtx regs)
3184 int i;
3186 for (i = 0; i < XVECLEN (regs, 0); i++)
3188 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3190 /* A NULL entry means the parameter goes both on the stack and in
3191 registers. This can also be a MEM for targets that pass values
3192 partially on the stack and partially in registers. */
3193 if (reg != 0 && REG_P (reg))
3194 use_reg (call_fusage, reg);
3198 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3199 assigment and the code of the expresion on the RHS is CODE. Return
3200 NULL otherwise. */
3202 static gimple *
3203 get_def_for_expr (tree name, enum tree_code code)
3205 gimple *def_stmt;
3207 if (TREE_CODE (name) != SSA_NAME)
3208 return NULL;
3210 def_stmt = get_gimple_for_ssa_name (name);
3211 if (!def_stmt
3212 || gimple_assign_rhs_code (def_stmt) != code)
3213 return NULL;
3215 return def_stmt;
3218 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3219 assigment and the class of the expresion on the RHS is CLASS. Return
3220 NULL otherwise. */
3222 static gimple *
3223 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3225 gimple *def_stmt;
3227 if (TREE_CODE (name) != SSA_NAME)
3228 return NULL;
3230 def_stmt = get_gimple_for_ssa_name (name);
3231 if (!def_stmt
3232 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3233 return NULL;
3235 return def_stmt;
3238 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3239 its length in bytes. */
3242 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3243 unsigned int expected_align, HOST_WIDE_INT expected_size,
3244 unsigned HOST_WIDE_INT min_size,
3245 unsigned HOST_WIDE_INT max_size,
3246 unsigned HOST_WIDE_INT probable_max_size,
3247 unsigned ctz_size)
3249 machine_mode mode = GET_MODE (object);
3250 unsigned int align;
3252 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3254 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3255 just move a zero. Otherwise, do this a piece at a time. */
3256 poly_int64 size_val;
3257 if (mode != BLKmode
3258 && poly_int_rtx_p (size, &size_val)
3259 && known_eq (size_val, GET_MODE_SIZE (mode)))
3261 rtx zero = CONST0_RTX (mode);
3262 if (zero != NULL)
3264 emit_move_insn (object, zero);
3265 return NULL;
3268 if (COMPLEX_MODE_P (mode))
3270 zero = CONST0_RTX (GET_MODE_INNER (mode));
3271 if (zero != NULL)
3273 write_complex_part (object, zero, 0);
3274 write_complex_part (object, zero, 1);
3275 return NULL;
3280 if (size == const0_rtx)
3281 return NULL;
3283 align = MEM_ALIGN (object);
3285 if (CONST_INT_P (size)
3286 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3287 CLEAR_BY_PIECES,
3288 optimize_insn_for_speed_p ()))
3289 clear_by_pieces (object, INTVAL (size), align);
3290 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3291 expected_align, expected_size,
3292 min_size, max_size, probable_max_size))
3294 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3295 min_size, max_size,
3296 NULL_RTX, 0, align))
3298 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3299 return set_storage_via_libcall (object, size, const0_rtx,
3300 method == BLOCK_OP_TAILCALL);
3301 else
3302 gcc_unreachable ();
3304 return NULL;
3308 clear_storage (rtx object, rtx size, enum block_op_methods method)
3310 unsigned HOST_WIDE_INT max, min = 0;
3311 if (GET_CODE (size) == CONST_INT)
3312 min = max = UINTVAL (size);
3313 else
3314 max = GET_MODE_MASK (GET_MODE (size));
3315 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3319 /* A subroutine of clear_storage. Expand a call to memset.
3320 Return the return value of memset, 0 otherwise. */
3323 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3325 tree call_expr, fn, object_tree, size_tree, val_tree;
3326 machine_mode size_mode;
3328 object = copy_addr_to_reg (XEXP (object, 0));
3329 object_tree = make_tree (ptr_type_node, object);
3331 if (!CONST_INT_P (val))
3332 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3333 val_tree = make_tree (integer_type_node, val);
3335 size_mode = TYPE_MODE (sizetype);
3336 size = convert_to_mode (size_mode, size, 1);
3337 size = copy_to_mode_reg (size_mode, size);
3338 size_tree = make_tree (sizetype, size);
3340 /* It is incorrect to use the libcall calling conventions for calls to
3341 memset because it can be provided by the user. */
3342 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3343 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3344 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3346 return expand_call (call_expr, NULL_RTX, false);
3349 /* Expand a setmem pattern; return true if successful. */
3351 bool
3352 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3353 unsigned int expected_align, HOST_WIDE_INT expected_size,
3354 unsigned HOST_WIDE_INT min_size,
3355 unsigned HOST_WIDE_INT max_size,
3356 unsigned HOST_WIDE_INT probable_max_size)
3358 /* Try the most limited insn first, because there's no point
3359 including more than one in the machine description unless
3360 the more limited one has some advantage. */
3362 if (expected_align < align)
3363 expected_align = align;
3364 if (expected_size != -1)
3366 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3367 expected_size = max_size;
3368 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3369 expected_size = min_size;
3372 opt_scalar_int_mode mode_iter;
3373 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3375 scalar_int_mode mode = mode_iter.require ();
3376 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3378 if (code != CODE_FOR_nothing
3379 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3380 here because if SIZE is less than the mode mask, as it is
3381 returned by the macro, it will definitely be less than the
3382 actual mode mask. Since SIZE is within the Pmode address
3383 space, we limit MODE to Pmode. */
3384 && ((CONST_INT_P (size)
3385 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3386 <= (GET_MODE_MASK (mode) >> 1)))
3387 || max_size <= (GET_MODE_MASK (mode) >> 1)
3388 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3390 class expand_operand ops[9];
3391 unsigned int nops;
3393 nops = insn_data[(int) code].n_generator_args;
3394 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3396 create_fixed_operand (&ops[0], object);
3397 /* The check above guarantees that this size conversion is valid. */
3398 create_convert_operand_to (&ops[1], size, mode, true);
3399 create_convert_operand_from (&ops[2], val, byte_mode, true);
3400 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3401 if (nops >= 6)
3403 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3404 create_integer_operand (&ops[5], expected_size);
3406 if (nops >= 8)
3408 create_integer_operand (&ops[6], min_size);
3409 /* If we cannot represent the maximal size,
3410 make parameter NULL. */
3411 if ((HOST_WIDE_INT) max_size != -1)
3412 create_integer_operand (&ops[7], max_size);
3413 else
3414 create_fixed_operand (&ops[7], NULL);
3416 if (nops == 9)
3418 /* If we cannot represent the maximal size,
3419 make parameter NULL. */
3420 if ((HOST_WIDE_INT) probable_max_size != -1)
3421 create_integer_operand (&ops[8], probable_max_size);
3422 else
3423 create_fixed_operand (&ops[8], NULL);
3425 if (maybe_expand_insn (code, nops, ops))
3426 return true;
3430 return false;
3434 /* Write to one of the components of the complex value CPLX. Write VAL to
3435 the real part if IMAG_P is false, and the imaginary part if its true. */
3437 void
3438 write_complex_part (rtx cplx, rtx val, bool imag_p)
3440 machine_mode cmode;
3441 scalar_mode imode;
3442 unsigned ibitsize;
3444 if (GET_CODE (cplx) == CONCAT)
3446 emit_move_insn (XEXP (cplx, imag_p), val);
3447 return;
3450 cmode = GET_MODE (cplx);
3451 imode = GET_MODE_INNER (cmode);
3452 ibitsize = GET_MODE_BITSIZE (imode);
3454 /* For MEMs simplify_gen_subreg may generate an invalid new address
3455 because, e.g., the original address is considered mode-dependent
3456 by the target, which restricts simplify_subreg from invoking
3457 adjust_address_nv. Instead of preparing fallback support for an
3458 invalid address, we call adjust_address_nv directly. */
3459 if (MEM_P (cplx))
3461 emit_move_insn (adjust_address_nv (cplx, imode,
3462 imag_p ? GET_MODE_SIZE (imode) : 0),
3463 val);
3464 return;
3467 /* If the sub-object is at least word sized, then we know that subregging
3468 will work. This special case is important, since store_bit_field
3469 wants to operate on integer modes, and there's rarely an OImode to
3470 correspond to TCmode. */
3471 if (ibitsize >= BITS_PER_WORD
3472 /* For hard regs we have exact predicates. Assume we can split
3473 the original object if it spans an even number of hard regs.
3474 This special case is important for SCmode on 64-bit platforms
3475 where the natural size of floating-point regs is 32-bit. */
3476 || (REG_P (cplx)
3477 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3478 && REG_NREGS (cplx) % 2 == 0))
3480 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3481 imag_p ? GET_MODE_SIZE (imode) : 0);
3482 if (part)
3484 emit_move_insn (part, val);
3485 return;
3487 else
3488 /* simplify_gen_subreg may fail for sub-word MEMs. */
3489 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3492 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3493 false);
3496 /* Extract one of the components of the complex value CPLX. Extract the
3497 real part if IMAG_P is false, and the imaginary part if it's true. */
3500 read_complex_part (rtx cplx, bool imag_p)
3502 machine_mode cmode;
3503 scalar_mode imode;
3504 unsigned ibitsize;
3506 if (GET_CODE (cplx) == CONCAT)
3507 return XEXP (cplx, imag_p);
3509 cmode = GET_MODE (cplx);
3510 imode = GET_MODE_INNER (cmode);
3511 ibitsize = GET_MODE_BITSIZE (imode);
3513 /* Special case reads from complex constants that got spilled to memory. */
3514 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3516 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3517 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3519 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3520 if (CONSTANT_CLASS_P (part))
3521 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3525 /* For MEMs simplify_gen_subreg may generate an invalid new address
3526 because, e.g., the original address is considered mode-dependent
3527 by the target, which restricts simplify_subreg from invoking
3528 adjust_address_nv. Instead of preparing fallback support for an
3529 invalid address, we call adjust_address_nv directly. */
3530 if (MEM_P (cplx))
3531 return adjust_address_nv (cplx, imode,
3532 imag_p ? GET_MODE_SIZE (imode) : 0);
3534 /* If the sub-object is at least word sized, then we know that subregging
3535 will work. This special case is important, since extract_bit_field
3536 wants to operate on integer modes, and there's rarely an OImode to
3537 correspond to TCmode. */
3538 if (ibitsize >= BITS_PER_WORD
3539 /* For hard regs we have exact predicates. Assume we can split
3540 the original object if it spans an even number of hard regs.
3541 This special case is important for SCmode on 64-bit platforms
3542 where the natural size of floating-point regs is 32-bit. */
3543 || (REG_P (cplx)
3544 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3545 && REG_NREGS (cplx) % 2 == 0))
3547 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3548 imag_p ? GET_MODE_SIZE (imode) : 0);
3549 if (ret)
3550 return ret;
3551 else
3552 /* simplify_gen_subreg may fail for sub-word MEMs. */
3553 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3556 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3557 true, NULL_RTX, imode, imode, false, NULL);
3560 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3561 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3562 represented in NEW_MODE. If FORCE is true, this will never happen, as
3563 we'll force-create a SUBREG if needed. */
3565 static rtx
3566 emit_move_change_mode (machine_mode new_mode,
3567 machine_mode old_mode, rtx x, bool force)
3569 rtx ret;
3571 if (push_operand (x, GET_MODE (x)))
3573 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3574 MEM_COPY_ATTRIBUTES (ret, x);
3576 else if (MEM_P (x))
3578 /* We don't have to worry about changing the address since the
3579 size in bytes is supposed to be the same. */
3580 if (reload_in_progress)
3582 /* Copy the MEM to change the mode and move any
3583 substitutions from the old MEM to the new one. */
3584 ret = adjust_address_nv (x, new_mode, 0);
3585 copy_replacements (x, ret);
3587 else
3588 ret = adjust_address (x, new_mode, 0);
3590 else
3592 /* Note that we do want simplify_subreg's behavior of validating
3593 that the new mode is ok for a hard register. If we were to use
3594 simplify_gen_subreg, we would create the subreg, but would
3595 probably run into the target not being able to implement it. */
3596 /* Except, of course, when FORCE is true, when this is exactly what
3597 we want. Which is needed for CCmodes on some targets. */
3598 if (force)
3599 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3600 else
3601 ret = simplify_subreg (new_mode, x, old_mode, 0);
3604 return ret;
3607 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3608 an integer mode of the same size as MODE. Returns the instruction
3609 emitted, or NULL if such a move could not be generated. */
3611 static rtx_insn *
3612 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3614 scalar_int_mode imode;
3615 enum insn_code code;
3617 /* There must exist a mode of the exact size we require. */
3618 if (!int_mode_for_mode (mode).exists (&imode))
3619 return NULL;
3621 /* The target must support moves in this mode. */
3622 code = optab_handler (mov_optab, imode);
3623 if (code == CODE_FOR_nothing)
3624 return NULL;
3626 x = emit_move_change_mode (imode, mode, x, force);
3627 if (x == NULL_RTX)
3628 return NULL;
3629 y = emit_move_change_mode (imode, mode, y, force);
3630 if (y == NULL_RTX)
3631 return NULL;
3632 return emit_insn (GEN_FCN (code) (x, y));
3635 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3636 Return an equivalent MEM that does not use an auto-increment. */
3639 emit_move_resolve_push (machine_mode mode, rtx x)
3641 enum rtx_code code = GET_CODE (XEXP (x, 0));
3642 rtx temp;
3644 poly_int64 adjust = GET_MODE_SIZE (mode);
3645 #ifdef PUSH_ROUNDING
3646 adjust = PUSH_ROUNDING (adjust);
3647 #endif
3648 if (code == PRE_DEC || code == POST_DEC)
3649 adjust = -adjust;
3650 else if (code == PRE_MODIFY || code == POST_MODIFY)
3652 rtx expr = XEXP (XEXP (x, 0), 1);
3654 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3655 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3656 if (GET_CODE (expr) == MINUS)
3657 val = -val;
3658 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3659 adjust = val;
3662 /* Do not use anti_adjust_stack, since we don't want to update
3663 stack_pointer_delta. */
3664 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3665 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3666 0, OPTAB_LIB_WIDEN);
3667 if (temp != stack_pointer_rtx)
3668 emit_move_insn (stack_pointer_rtx, temp);
3670 switch (code)
3672 case PRE_INC:
3673 case PRE_DEC:
3674 case PRE_MODIFY:
3675 temp = stack_pointer_rtx;
3676 break;
3677 case POST_INC:
3678 case POST_DEC:
3679 case POST_MODIFY:
3680 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3681 break;
3682 default:
3683 gcc_unreachable ();
3686 return replace_equiv_address (x, temp);
3689 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3690 X is known to satisfy push_operand, and MODE is known to be complex.
3691 Returns the last instruction emitted. */
3693 rtx_insn *
3694 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3696 scalar_mode submode = GET_MODE_INNER (mode);
3697 bool imag_first;
3699 #ifdef PUSH_ROUNDING
3700 poly_int64 submodesize = GET_MODE_SIZE (submode);
3702 /* In case we output to the stack, but the size is smaller than the
3703 machine can push exactly, we need to use move instructions. */
3704 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3706 x = emit_move_resolve_push (mode, x);
3707 return emit_move_insn (x, y);
3709 #endif
3711 /* Note that the real part always precedes the imag part in memory
3712 regardless of machine's endianness. */
3713 switch (GET_CODE (XEXP (x, 0)))
3715 case PRE_DEC:
3716 case POST_DEC:
3717 imag_first = true;
3718 break;
3719 case PRE_INC:
3720 case POST_INC:
3721 imag_first = false;
3722 break;
3723 default:
3724 gcc_unreachable ();
3727 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3728 read_complex_part (y, imag_first));
3729 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3730 read_complex_part (y, !imag_first));
3733 /* A subroutine of emit_move_complex. Perform the move from Y to X
3734 via two moves of the parts. Returns the last instruction emitted. */
3736 rtx_insn *
3737 emit_move_complex_parts (rtx x, rtx y)
3739 /* Show the output dies here. This is necessary for SUBREGs
3740 of pseudos since we cannot track their lifetimes correctly;
3741 hard regs shouldn't appear here except as return values. */
3742 if (!reload_completed && !reload_in_progress
3743 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3744 emit_clobber (x);
3746 write_complex_part (x, read_complex_part (y, false), false);
3747 write_complex_part (x, read_complex_part (y, true), true);
3749 return get_last_insn ();
3752 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3753 MODE is known to be complex. Returns the last instruction emitted. */
3755 static rtx_insn *
3756 emit_move_complex (machine_mode mode, rtx x, rtx y)
3758 bool try_int;
3760 /* Need to take special care for pushes, to maintain proper ordering
3761 of the data, and possibly extra padding. */
3762 if (push_operand (x, mode))
3763 return emit_move_complex_push (mode, x, y);
3765 /* See if we can coerce the target into moving both values at once, except
3766 for floating point where we favor moving as parts if this is easy. */
3767 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3768 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3769 && !(REG_P (x)
3770 && HARD_REGISTER_P (x)
3771 && REG_NREGS (x) == 1)
3772 && !(REG_P (y)
3773 && HARD_REGISTER_P (y)
3774 && REG_NREGS (y) == 1))
3775 try_int = false;
3776 /* Not possible if the values are inherently not adjacent. */
3777 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3778 try_int = false;
3779 /* Is possible if both are registers (or subregs of registers). */
3780 else if (register_operand (x, mode) && register_operand (y, mode))
3781 try_int = true;
3782 /* If one of the operands is a memory, and alignment constraints
3783 are friendly enough, we may be able to do combined memory operations.
3784 We do not attempt this if Y is a constant because that combination is
3785 usually better with the by-parts thing below. */
3786 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3787 && (!STRICT_ALIGNMENT
3788 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3789 try_int = true;
3790 else
3791 try_int = false;
3793 if (try_int)
3795 rtx_insn *ret;
3797 /* For memory to memory moves, optimal behavior can be had with the
3798 existing block move logic. But use normal expansion if optimizing
3799 for size. */
3800 if (MEM_P (x) && MEM_P (y))
3802 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3803 (optimize_insn_for_speed_p()
3804 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3805 return get_last_insn ();
3808 ret = emit_move_via_integer (mode, x, y, true);
3809 if (ret)
3810 return ret;
3813 return emit_move_complex_parts (x, y);
3816 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3817 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3819 static rtx_insn *
3820 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3822 rtx_insn *ret;
3824 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3825 if (mode != CCmode)
3827 enum insn_code code = optab_handler (mov_optab, CCmode);
3828 if (code != CODE_FOR_nothing)
3830 x = emit_move_change_mode (CCmode, mode, x, true);
3831 y = emit_move_change_mode (CCmode, mode, y, true);
3832 return emit_insn (GEN_FCN (code) (x, y));
3836 /* Otherwise, find the MODE_INT mode of the same width. */
3837 ret = emit_move_via_integer (mode, x, y, false);
3838 gcc_assert (ret != NULL);
3839 return ret;
3842 /* Return true if word I of OP lies entirely in the
3843 undefined bits of a paradoxical subreg. */
3845 static bool
3846 undefined_operand_subword_p (const_rtx op, int i)
3848 if (GET_CODE (op) != SUBREG)
3849 return false;
3850 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3851 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3852 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3853 || known_le (offset, -UNITS_PER_WORD));
3856 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3857 MODE is any multi-word or full-word mode that lacks a move_insn
3858 pattern. Note that you will get better code if you define such
3859 patterns, even if they must turn into multiple assembler instructions. */
3861 static rtx_insn *
3862 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3864 rtx_insn *last_insn = 0;
3865 rtx_insn *seq;
3866 rtx inner;
3867 bool need_clobber;
3868 int i, mode_size;
3870 /* This function can only handle cases where the number of words is
3871 known at compile time. */
3872 mode_size = GET_MODE_SIZE (mode).to_constant ();
3873 gcc_assert (mode_size >= UNITS_PER_WORD);
3875 /* If X is a push on the stack, do the push now and replace
3876 X with a reference to the stack pointer. */
3877 if (push_operand (x, mode))
3878 x = emit_move_resolve_push (mode, x);
3880 /* If we are in reload, see if either operand is a MEM whose address
3881 is scheduled for replacement. */
3882 if (reload_in_progress && MEM_P (x)
3883 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3884 x = replace_equiv_address_nv (x, inner);
3885 if (reload_in_progress && MEM_P (y)
3886 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3887 y = replace_equiv_address_nv (y, inner);
3889 start_sequence ();
3891 need_clobber = false;
3892 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3894 /* Do not generate code for a move if it would go entirely
3895 to the non-existing bits of a paradoxical subreg. */
3896 if (undefined_operand_subword_p (x, i))
3897 continue;
3899 rtx xpart = operand_subword (x, i, 1, mode);
3900 rtx ypart;
3902 /* Do not generate code for a move if it would come entirely
3903 from the undefined bits of a paradoxical subreg. */
3904 if (undefined_operand_subword_p (y, i))
3905 continue;
3907 ypart = operand_subword (y, i, 1, mode);
3909 /* If we can't get a part of Y, put Y into memory if it is a
3910 constant. Otherwise, force it into a register. Then we must
3911 be able to get a part of Y. */
3912 if (ypart == 0 && CONSTANT_P (y))
3914 y = use_anchored_address (force_const_mem (mode, y));
3915 ypart = operand_subword (y, i, 1, mode);
3917 else if (ypart == 0)
3918 ypart = operand_subword_force (y, i, mode);
3920 gcc_assert (xpart && ypart);
3922 need_clobber |= (GET_CODE (xpart) == SUBREG);
3924 last_insn = emit_move_insn (xpart, ypart);
3927 seq = get_insns ();
3928 end_sequence ();
3930 /* Show the output dies here. This is necessary for SUBREGs
3931 of pseudos since we cannot track their lifetimes correctly;
3932 hard regs shouldn't appear here except as return values.
3933 We never want to emit such a clobber after reload. */
3934 if (x != y
3935 && ! (reload_in_progress || reload_completed)
3936 && need_clobber != 0)
3937 emit_clobber (x);
3939 emit_insn (seq);
3941 return last_insn;
3944 /* Low level part of emit_move_insn.
3945 Called just like emit_move_insn, but assumes X and Y
3946 are basically valid. */
3948 rtx_insn *
3949 emit_move_insn_1 (rtx x, rtx y)
3951 machine_mode mode = GET_MODE (x);
3952 enum insn_code code;
3954 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3956 code = optab_handler (mov_optab, mode);
3957 if (code != CODE_FOR_nothing)
3958 return emit_insn (GEN_FCN (code) (x, y));
3960 /* Expand complex moves by moving real part and imag part. */
3961 if (COMPLEX_MODE_P (mode))
3962 return emit_move_complex (mode, x, y);
3964 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3965 || ALL_FIXED_POINT_MODE_P (mode))
3967 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3969 /* If we can't find an integer mode, use multi words. */
3970 if (result)
3971 return result;
3972 else
3973 return emit_move_multi_word (mode, x, y);
3976 if (GET_MODE_CLASS (mode) == MODE_CC)
3977 return emit_move_ccmode (mode, x, y);
3979 /* Try using a move pattern for the corresponding integer mode. This is
3980 only safe when simplify_subreg can convert MODE constants into integer
3981 constants. At present, it can only do this reliably if the value
3982 fits within a HOST_WIDE_INT. */
3983 if (!CONSTANT_P (y)
3984 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3986 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3988 if (ret)
3990 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3991 return ret;
3995 return emit_move_multi_word (mode, x, y);
3998 /* Generate code to copy Y into X.
3999 Both Y and X must have the same mode, except that
4000 Y can be a constant with VOIDmode.
4001 This mode cannot be BLKmode; use emit_block_move for that.
4003 Return the last instruction emitted. */
4005 rtx_insn *
4006 emit_move_insn (rtx x, rtx y)
4008 machine_mode mode = GET_MODE (x);
4009 rtx y_cst = NULL_RTX;
4010 rtx_insn *last_insn;
4011 rtx set;
4013 gcc_assert (mode != BLKmode
4014 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4016 /* If we have a copy that looks like one of the following patterns:
4017 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4018 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4019 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4020 (set (subreg:M1 (reg:M2 ...)) (constant C))
4021 where mode M1 is equal in size to M2, try to detect whether the
4022 mode change involves an implicit round trip through memory.
4023 If so, see if we can avoid that by removing the subregs and
4024 doing the move in mode M2 instead. */
4026 rtx x_inner = NULL_RTX;
4027 rtx y_inner = NULL_RTX;
4029 auto candidate_subreg_p = [&](rtx subreg) {
4030 return (REG_P (SUBREG_REG (subreg))
4031 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4032 GET_MODE_SIZE (GET_MODE (subreg)))
4033 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4034 != CODE_FOR_nothing);
4037 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4038 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4039 && !push_operand (mem, GET_MODE (mem))
4040 /* Not a candiate if innermode requires too much alignment. */
4041 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4042 || targetm.slow_unaligned_access (GET_MODE (mem),
4043 MEM_ALIGN (mem))
4044 || !targetm.slow_unaligned_access (innermode,
4045 MEM_ALIGN (mem))));
4048 if (SUBREG_P (x) && candidate_subreg_p (x))
4049 x_inner = SUBREG_REG (x);
4051 if (SUBREG_P (y) && candidate_subreg_p (y))
4052 y_inner = SUBREG_REG (y);
4054 if (x_inner != NULL_RTX
4055 && y_inner != NULL_RTX
4056 && GET_MODE (x_inner) == GET_MODE (y_inner)
4057 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4059 x = x_inner;
4060 y = y_inner;
4061 mode = GET_MODE (x_inner);
4063 else if (x_inner != NULL_RTX
4064 && MEM_P (y)
4065 && candidate_mem_p (GET_MODE (x_inner), y))
4067 x = x_inner;
4068 y = adjust_address (y, GET_MODE (x_inner), 0);
4069 mode = GET_MODE (x_inner);
4071 else if (y_inner != NULL_RTX
4072 && MEM_P (x)
4073 && candidate_mem_p (GET_MODE (y_inner), x))
4075 x = adjust_address (x, GET_MODE (y_inner), 0);
4076 y = y_inner;
4077 mode = GET_MODE (y_inner);
4079 else if (x_inner != NULL_RTX
4080 && CONSTANT_P (y)
4081 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4082 mode, ALL_REGS)
4083 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4085 x = x_inner;
4086 y = y_inner;
4087 mode = GET_MODE (x_inner);
4090 if (CONSTANT_P (y))
4092 if (optimize
4093 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4094 && (last_insn = compress_float_constant (x, y)))
4095 return last_insn;
4097 y_cst = y;
4099 if (!targetm.legitimate_constant_p (mode, y))
4101 y = force_const_mem (mode, y);
4103 /* If the target's cannot_force_const_mem prevented the spill,
4104 assume that the target's move expanders will also take care
4105 of the non-legitimate constant. */
4106 if (!y)
4107 y = y_cst;
4108 else
4109 y = use_anchored_address (y);
4113 /* If X or Y are memory references, verify that their addresses are valid
4114 for the machine. */
4115 if (MEM_P (x)
4116 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4117 MEM_ADDR_SPACE (x))
4118 && ! push_operand (x, GET_MODE (x))))
4119 x = validize_mem (x);
4121 if (MEM_P (y)
4122 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4123 MEM_ADDR_SPACE (y)))
4124 y = validize_mem (y);
4126 gcc_assert (mode != BLKmode);
4128 last_insn = emit_move_insn_1 (x, y);
4130 if (y_cst && REG_P (x)
4131 && (set = single_set (last_insn)) != NULL_RTX
4132 && SET_DEST (set) == x
4133 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4134 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4136 return last_insn;
4139 /* Generate the body of an instruction to copy Y into X.
4140 It may be a list of insns, if one insn isn't enough. */
4142 rtx_insn *
4143 gen_move_insn (rtx x, rtx y)
4145 rtx_insn *seq;
4147 start_sequence ();
4148 emit_move_insn_1 (x, y);
4149 seq = get_insns ();
4150 end_sequence ();
4151 return seq;
4154 /* If Y is representable exactly in a narrower mode, and the target can
4155 perform the extension directly from constant or memory, then emit the
4156 move as an extension. */
4158 static rtx_insn *
4159 compress_float_constant (rtx x, rtx y)
4161 machine_mode dstmode = GET_MODE (x);
4162 machine_mode orig_srcmode = GET_MODE (y);
4163 machine_mode srcmode;
4164 const REAL_VALUE_TYPE *r;
4165 int oldcost, newcost;
4166 bool speed = optimize_insn_for_speed_p ();
4168 r = CONST_DOUBLE_REAL_VALUE (y);
4170 if (targetm.legitimate_constant_p (dstmode, y))
4171 oldcost = set_src_cost (y, orig_srcmode, speed);
4172 else
4173 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4175 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4177 enum insn_code ic;
4178 rtx trunc_y;
4179 rtx_insn *last_insn;
4181 /* Skip if the target can't extend this way. */
4182 ic = can_extend_p (dstmode, srcmode, 0);
4183 if (ic == CODE_FOR_nothing)
4184 continue;
4186 /* Skip if the narrowed value isn't exact. */
4187 if (! exact_real_truncate (srcmode, r))
4188 continue;
4190 trunc_y = const_double_from_real_value (*r, srcmode);
4192 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4194 /* Skip if the target needs extra instructions to perform
4195 the extension. */
4196 if (!insn_operand_matches (ic, 1, trunc_y))
4197 continue;
4198 /* This is valid, but may not be cheaper than the original. */
4199 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4200 dstmode, speed);
4201 if (oldcost < newcost)
4202 continue;
4204 else if (float_extend_from_mem[dstmode][srcmode])
4206 trunc_y = force_const_mem (srcmode, trunc_y);
4207 /* This is valid, but may not be cheaper than the original. */
4208 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4209 dstmode, speed);
4210 if (oldcost < newcost)
4211 continue;
4212 trunc_y = validize_mem (trunc_y);
4214 else
4215 continue;
4217 /* For CSE's benefit, force the compressed constant pool entry
4218 into a new pseudo. This constant may be used in different modes,
4219 and if not, combine will put things back together for us. */
4220 trunc_y = force_reg (srcmode, trunc_y);
4222 /* If x is a hard register, perform the extension into a pseudo,
4223 so that e.g. stack realignment code is aware of it. */
4224 rtx target = x;
4225 if (REG_P (x) && HARD_REGISTER_P (x))
4226 target = gen_reg_rtx (dstmode);
4228 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4229 last_insn = get_last_insn ();
4231 if (REG_P (target))
4232 set_unique_reg_note (last_insn, REG_EQUAL, y);
4234 if (target != x)
4235 return emit_move_insn (x, target);
4236 return last_insn;
4239 return NULL;
4242 /* Pushing data onto the stack. */
4244 /* Push a block of length SIZE (perhaps variable)
4245 and return an rtx to address the beginning of the block.
4246 The value may be virtual_outgoing_args_rtx.
4248 EXTRA is the number of bytes of padding to push in addition to SIZE.
4249 BELOW nonzero means this padding comes at low addresses;
4250 otherwise, the padding comes at high addresses. */
4253 push_block (rtx size, poly_int64 extra, int below)
4255 rtx temp;
4257 size = convert_modes (Pmode, ptr_mode, size, 1);
4258 if (CONSTANT_P (size))
4259 anti_adjust_stack (plus_constant (Pmode, size, extra));
4260 else if (REG_P (size) && known_eq (extra, 0))
4261 anti_adjust_stack (size);
4262 else
4264 temp = copy_to_mode_reg (Pmode, size);
4265 if (maybe_ne (extra, 0))
4266 temp = expand_binop (Pmode, add_optab, temp,
4267 gen_int_mode (extra, Pmode),
4268 temp, 0, OPTAB_LIB_WIDEN);
4269 anti_adjust_stack (temp);
4272 if (STACK_GROWS_DOWNWARD)
4274 temp = virtual_outgoing_args_rtx;
4275 if (maybe_ne (extra, 0) && below)
4276 temp = plus_constant (Pmode, temp, extra);
4278 else
4280 poly_int64 csize;
4281 if (poly_int_rtx_p (size, &csize))
4282 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4283 -csize - (below ? 0 : extra));
4284 else if (maybe_ne (extra, 0) && !below)
4285 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4286 negate_rtx (Pmode, plus_constant (Pmode, size,
4287 extra)));
4288 else
4289 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4290 negate_rtx (Pmode, size));
4293 return memory_address (NARROWEST_INT_MODE, temp);
4296 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4298 static rtx
4299 mem_autoinc_base (rtx mem)
4301 if (MEM_P (mem))
4303 rtx addr = XEXP (mem, 0);
4304 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4305 return XEXP (addr, 0);
4307 return NULL;
4310 /* A utility routine used here, in reload, and in try_split. The insns
4311 after PREV up to and including LAST are known to adjust the stack,
4312 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4313 placing notes as appropriate. PREV may be NULL, indicating the
4314 entire insn sequence prior to LAST should be scanned.
4316 The set of allowed stack pointer modifications is small:
4317 (1) One or more auto-inc style memory references (aka pushes),
4318 (2) One or more addition/subtraction with the SP as destination,
4319 (3) A single move insn with the SP as destination,
4320 (4) A call_pop insn,
4321 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4323 Insns in the sequence that do not modify the SP are ignored,
4324 except for noreturn calls.
4326 The return value is the amount of adjustment that can be trivially
4327 verified, via immediate operand or auto-inc. If the adjustment
4328 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4330 poly_int64
4331 find_args_size_adjust (rtx_insn *insn)
4333 rtx dest, set, pat;
4334 int i;
4336 pat = PATTERN (insn);
4337 set = NULL;
4339 /* Look for a call_pop pattern. */
4340 if (CALL_P (insn))
4342 /* We have to allow non-call_pop patterns for the case
4343 of emit_single_push_insn of a TLS address. */
4344 if (GET_CODE (pat) != PARALLEL)
4345 return 0;
4347 /* All call_pop have a stack pointer adjust in the parallel.
4348 The call itself is always first, and the stack adjust is
4349 usually last, so search from the end. */
4350 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4352 set = XVECEXP (pat, 0, i);
4353 if (GET_CODE (set) != SET)
4354 continue;
4355 dest = SET_DEST (set);
4356 if (dest == stack_pointer_rtx)
4357 break;
4359 /* We'd better have found the stack pointer adjust. */
4360 if (i == 0)
4361 return 0;
4362 /* Fall through to process the extracted SET and DEST
4363 as if it was a standalone insn. */
4365 else if (GET_CODE (pat) == SET)
4366 set = pat;
4367 else if ((set = single_set (insn)) != NULL)
4369 else if (GET_CODE (pat) == PARALLEL)
4371 /* ??? Some older ports use a parallel with a stack adjust
4372 and a store for a PUSH_ROUNDING pattern, rather than a
4373 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4374 /* ??? See h8300 and m68k, pushqi1. */
4375 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4377 set = XVECEXP (pat, 0, i);
4378 if (GET_CODE (set) != SET)
4379 continue;
4380 dest = SET_DEST (set);
4381 if (dest == stack_pointer_rtx)
4382 break;
4384 /* We do not expect an auto-inc of the sp in the parallel. */
4385 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4386 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4387 != stack_pointer_rtx);
4389 if (i < 0)
4390 return 0;
4392 else
4393 return 0;
4395 dest = SET_DEST (set);
4397 /* Look for direct modifications of the stack pointer. */
4398 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4400 /* Look for a trivial adjustment, otherwise assume nothing. */
4401 /* Note that the SPU restore_stack_block pattern refers to
4402 the stack pointer in V4SImode. Consider that non-trivial. */
4403 poly_int64 offset;
4404 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4405 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4406 return offset;
4407 /* ??? Reload can generate no-op moves, which will be cleaned
4408 up later. Recognize it and continue searching. */
4409 else if (rtx_equal_p (dest, SET_SRC (set)))
4410 return 0;
4411 else
4412 return HOST_WIDE_INT_MIN;
4414 else
4416 rtx mem, addr;
4418 /* Otherwise only think about autoinc patterns. */
4419 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4421 mem = dest;
4422 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4423 != stack_pointer_rtx);
4425 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4426 mem = SET_SRC (set);
4427 else
4428 return 0;
4430 addr = XEXP (mem, 0);
4431 switch (GET_CODE (addr))
4433 case PRE_INC:
4434 case POST_INC:
4435 return GET_MODE_SIZE (GET_MODE (mem));
4436 case PRE_DEC:
4437 case POST_DEC:
4438 return -GET_MODE_SIZE (GET_MODE (mem));
4439 case PRE_MODIFY:
4440 case POST_MODIFY:
4441 addr = XEXP (addr, 1);
4442 gcc_assert (GET_CODE (addr) == PLUS);
4443 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4444 return rtx_to_poly_int64 (XEXP (addr, 1));
4445 default:
4446 gcc_unreachable ();
4451 poly_int64
4452 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4453 poly_int64 end_args_size)
4455 poly_int64 args_size = end_args_size;
4456 bool saw_unknown = false;
4457 rtx_insn *insn;
4459 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4461 if (!NONDEBUG_INSN_P (insn))
4462 continue;
4464 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4465 a call argument containing a TLS address that itself requires
4466 a call to __tls_get_addr. The handling of stack_pointer_delta
4467 in emit_single_push_insn is supposed to ensure that any such
4468 notes are already correct. */
4469 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4470 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4472 poly_int64 this_delta = find_args_size_adjust (insn);
4473 if (known_eq (this_delta, 0))
4475 if (!CALL_P (insn)
4476 || ACCUMULATE_OUTGOING_ARGS
4477 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4478 continue;
4481 gcc_assert (!saw_unknown);
4482 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4483 saw_unknown = true;
4485 if (!note)
4486 add_args_size_note (insn, args_size);
4487 if (STACK_GROWS_DOWNWARD)
4488 this_delta = -poly_uint64 (this_delta);
4490 if (saw_unknown)
4491 args_size = HOST_WIDE_INT_MIN;
4492 else
4493 args_size -= this_delta;
4496 return args_size;
4499 #ifdef PUSH_ROUNDING
4500 /* Emit single push insn. */
4502 static void
4503 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4505 rtx dest_addr;
4506 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4507 rtx dest;
4508 enum insn_code icode;
4510 /* If there is push pattern, use it. Otherwise try old way of throwing
4511 MEM representing push operation to move expander. */
4512 icode = optab_handler (push_optab, mode);
4513 if (icode != CODE_FOR_nothing)
4515 class expand_operand ops[1];
4517 create_input_operand (&ops[0], x, mode);
4518 if (maybe_expand_insn (icode, 1, ops))
4519 return;
4521 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4522 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4523 /* If we are to pad downward, adjust the stack pointer first and
4524 then store X into the stack location using an offset. This is
4525 because emit_move_insn does not know how to pad; it does not have
4526 access to type. */
4527 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4529 emit_move_insn (stack_pointer_rtx,
4530 expand_binop (Pmode,
4531 STACK_GROWS_DOWNWARD ? sub_optab
4532 : add_optab,
4533 stack_pointer_rtx,
4534 gen_int_mode (rounded_size, Pmode),
4535 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4537 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4538 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4539 /* We have already decremented the stack pointer, so get the
4540 previous value. */
4541 offset += rounded_size;
4543 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4544 /* We have already incremented the stack pointer, so get the
4545 previous value. */
4546 offset -= rounded_size;
4548 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4550 else
4552 if (STACK_GROWS_DOWNWARD)
4553 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4554 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4555 else
4556 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4557 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4559 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4562 dest = gen_rtx_MEM (mode, dest_addr);
4564 if (type != 0)
4566 set_mem_attributes (dest, type, 1);
4568 if (cfun->tail_call_marked)
4569 /* Function incoming arguments may overlap with sibling call
4570 outgoing arguments and we cannot allow reordering of reads
4571 from function arguments with stores to outgoing arguments
4572 of sibling calls. */
4573 set_mem_alias_set (dest, 0);
4575 emit_move_insn (dest, x);
4578 /* Emit and annotate a single push insn. */
4580 static void
4581 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4583 poly_int64 delta, old_delta = stack_pointer_delta;
4584 rtx_insn *prev = get_last_insn ();
4585 rtx_insn *last;
4587 emit_single_push_insn_1 (mode, x, type);
4589 /* Adjust stack_pointer_delta to describe the situation after the push
4590 we just performed. Note that we must do this after the push rather
4591 than before the push in case calculating X needs pushes and pops of
4592 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4593 for such pushes and pops must not include the effect of the future
4594 push of X. */
4595 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4597 last = get_last_insn ();
4599 /* Notice the common case where we emitted exactly one insn. */
4600 if (PREV_INSN (last) == prev)
4602 add_args_size_note (last, stack_pointer_delta);
4603 return;
4606 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4607 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4608 || known_eq (delta, old_delta));
4610 #endif
4612 /* If reading SIZE bytes from X will end up reading from
4613 Y return the number of bytes that overlap. Return -1
4614 if there is no overlap or -2 if we can't determine
4615 (for example when X and Y have different base registers). */
4617 static int
4618 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4620 rtx tmp = plus_constant (Pmode, x, size);
4621 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4623 if (!CONST_INT_P (sub))
4624 return -2;
4626 HOST_WIDE_INT val = INTVAL (sub);
4628 return IN_RANGE (val, 1, size) ? val : -1;
4631 /* Generate code to push X onto the stack, assuming it has mode MODE and
4632 type TYPE.
4633 MODE is redundant except when X is a CONST_INT (since they don't
4634 carry mode info).
4635 SIZE is an rtx for the size of data to be copied (in bytes),
4636 needed only if X is BLKmode.
4637 Return true if successful. May return false if asked to push a
4638 partial argument during a sibcall optimization (as specified by
4639 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4640 to not overlap.
4642 ALIGN (in bits) is maximum alignment we can assume.
4644 If PARTIAL and REG are both nonzero, then copy that many of the first
4645 bytes of X into registers starting with REG, and push the rest of X.
4646 The amount of space pushed is decreased by PARTIAL bytes.
4647 REG must be a hard register in this case.
4648 If REG is zero but PARTIAL is not, take any all others actions for an
4649 argument partially in registers, but do not actually load any
4650 registers.
4652 EXTRA is the amount in bytes of extra space to leave next to this arg.
4653 This is ignored if an argument block has already been allocated.
4655 On a machine that lacks real push insns, ARGS_ADDR is the address of
4656 the bottom of the argument block for this call. We use indexing off there
4657 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4658 argument block has not been preallocated.
4660 ARGS_SO_FAR is the size of args previously pushed for this call.
4662 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4663 for arguments passed in registers. If nonzero, it will be the number
4664 of bytes required. */
4666 bool
4667 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4668 unsigned int align, int partial, rtx reg, poly_int64 extra,
4669 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4670 rtx alignment_pad, bool sibcall_p)
4672 rtx xinner;
4673 pad_direction stack_direction
4674 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4676 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4677 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4678 Default is below for small data on big-endian machines; else above. */
4679 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4681 /* Invert direction if stack is post-decrement.
4682 FIXME: why? */
4683 if (STACK_PUSH_CODE == POST_DEC)
4684 if (where_pad != PAD_NONE)
4685 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4687 xinner = x;
4689 int nregs = partial / UNITS_PER_WORD;
4690 rtx *tmp_regs = NULL;
4691 int overlapping = 0;
4693 if (mode == BLKmode
4694 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4696 /* Copy a block into the stack, entirely or partially. */
4698 rtx temp;
4699 int used;
4700 int offset;
4701 int skip;
4703 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4704 used = partial - offset;
4706 if (mode != BLKmode)
4708 /* A value is to be stored in an insufficiently aligned
4709 stack slot; copy via a suitably aligned slot if
4710 necessary. */
4711 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4712 if (!MEM_P (xinner))
4714 temp = assign_temp (type, 1, 1);
4715 emit_move_insn (temp, xinner);
4716 xinner = temp;
4720 gcc_assert (size);
4722 /* USED is now the # of bytes we need not copy to the stack
4723 because registers will take care of them. */
4725 if (partial != 0)
4726 xinner = adjust_address (xinner, BLKmode, used);
4728 /* If the partial register-part of the arg counts in its stack size,
4729 skip the part of stack space corresponding to the registers.
4730 Otherwise, start copying to the beginning of the stack space,
4731 by setting SKIP to 0. */
4732 skip = (reg_parm_stack_space == 0) ? 0 : used;
4734 #ifdef PUSH_ROUNDING
4735 /* NB: Let the backend known the number of bytes to push and
4736 decide if push insns should be generated. */
4737 unsigned int push_size;
4738 if (CONST_INT_P (size))
4739 push_size = INTVAL (size);
4740 else
4741 push_size = 0;
4743 /* Do it with several push insns if that doesn't take lots of insns
4744 and if there is no difficulty with push insns that skip bytes
4745 on the stack for alignment purposes. */
4746 if (args_addr == 0
4747 && targetm.calls.push_argument (push_size)
4748 && CONST_INT_P (size)
4749 && skip == 0
4750 && MEM_ALIGN (xinner) >= align
4751 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4752 /* Here we avoid the case of a structure whose weak alignment
4753 forces many pushes of a small amount of data,
4754 and such small pushes do rounding that causes trouble. */
4755 && ((!targetm.slow_unaligned_access (word_mode, align))
4756 || align >= BIGGEST_ALIGNMENT
4757 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4758 align / BITS_PER_UNIT))
4759 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4761 /* Push padding now if padding above and stack grows down,
4762 or if padding below and stack grows up.
4763 But if space already allocated, this has already been done. */
4764 if (maybe_ne (extra, 0)
4765 && args_addr == 0
4766 && where_pad != PAD_NONE
4767 && where_pad != stack_direction)
4768 anti_adjust_stack (gen_int_mode (extra, Pmode));
4770 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4771 RETURN_BEGIN);
4773 else
4774 #endif /* PUSH_ROUNDING */
4776 rtx target;
4778 /* Otherwise make space on the stack and copy the data
4779 to the address of that space. */
4781 /* Deduct words put into registers from the size we must copy. */
4782 if (partial != 0)
4784 if (CONST_INT_P (size))
4785 size = GEN_INT (INTVAL (size) - used);
4786 else
4787 size = expand_binop (GET_MODE (size), sub_optab, size,
4788 gen_int_mode (used, GET_MODE (size)),
4789 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4792 /* Get the address of the stack space.
4793 In this case, we do not deal with EXTRA separately.
4794 A single stack adjust will do. */
4795 poly_int64 const_args_so_far;
4796 if (! args_addr)
4798 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4799 extra = 0;
4801 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4802 temp = memory_address (BLKmode,
4803 plus_constant (Pmode, args_addr,
4804 skip + const_args_so_far));
4805 else
4806 temp = memory_address (BLKmode,
4807 plus_constant (Pmode,
4808 gen_rtx_PLUS (Pmode,
4809 args_addr,
4810 args_so_far),
4811 skip));
4813 if (!ACCUMULATE_OUTGOING_ARGS)
4815 /* If the source is referenced relative to the stack pointer,
4816 copy it to another register to stabilize it. We do not need
4817 to do this if we know that we won't be changing sp. */
4819 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4820 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4821 temp = copy_to_reg (temp);
4824 target = gen_rtx_MEM (BLKmode, temp);
4826 /* We do *not* set_mem_attributes here, because incoming arguments
4827 may overlap with sibling call outgoing arguments and we cannot
4828 allow reordering of reads from function arguments with stores
4829 to outgoing arguments of sibling calls. We do, however, want
4830 to record the alignment of the stack slot. */
4831 /* ALIGN may well be better aligned than TYPE, e.g. due to
4832 PARM_BOUNDARY. Assume the caller isn't lying. */
4833 set_mem_align (target, align);
4835 /* If part should go in registers and pushing to that part would
4836 overwrite some of the values that need to go into regs, load the
4837 overlapping values into temporary pseudos to be moved into the hard
4838 regs at the end after the stack pushing has completed.
4839 We cannot load them directly into the hard regs here because
4840 they can be clobbered by the block move expansions.
4841 See PR 65358. */
4843 if (partial > 0 && reg != 0 && mode == BLKmode
4844 && GET_CODE (reg) != PARALLEL)
4846 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4847 if (overlapping > 0)
4849 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4850 overlapping /= UNITS_PER_WORD;
4852 tmp_regs = XALLOCAVEC (rtx, overlapping);
4854 for (int i = 0; i < overlapping; i++)
4855 tmp_regs[i] = gen_reg_rtx (word_mode);
4857 for (int i = 0; i < overlapping; i++)
4858 emit_move_insn (tmp_regs[i],
4859 operand_subword_force (target, i, mode));
4861 else if (overlapping == -1)
4862 overlapping = 0;
4863 /* Could not determine whether there is overlap.
4864 Fail the sibcall. */
4865 else
4867 overlapping = 0;
4868 if (sibcall_p)
4869 return false;
4872 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4875 else if (partial > 0)
4877 /* Scalar partly in registers. This case is only supported
4878 for fixed-wdth modes. */
4879 int num_words = GET_MODE_SIZE (mode).to_constant ();
4880 num_words /= UNITS_PER_WORD;
4881 int i;
4882 int not_stack;
4883 /* # bytes of start of argument
4884 that we must make space for but need not store. */
4885 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4886 int args_offset = INTVAL (args_so_far);
4887 int skip;
4889 /* Push padding now if padding above and stack grows down,
4890 or if padding below and stack grows up.
4891 But if space already allocated, this has already been done. */
4892 if (maybe_ne (extra, 0)
4893 && args_addr == 0
4894 && where_pad != PAD_NONE
4895 && where_pad != stack_direction)
4896 anti_adjust_stack (gen_int_mode (extra, Pmode));
4898 /* If we make space by pushing it, we might as well push
4899 the real data. Otherwise, we can leave OFFSET nonzero
4900 and leave the space uninitialized. */
4901 if (args_addr == 0)
4902 offset = 0;
4904 /* Now NOT_STACK gets the number of words that we don't need to
4905 allocate on the stack. Convert OFFSET to words too. */
4906 not_stack = (partial - offset) / UNITS_PER_WORD;
4907 offset /= UNITS_PER_WORD;
4909 /* If the partial register-part of the arg counts in its stack size,
4910 skip the part of stack space corresponding to the registers.
4911 Otherwise, start copying to the beginning of the stack space,
4912 by setting SKIP to 0. */
4913 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4915 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4916 x = validize_mem (force_const_mem (mode, x));
4918 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4919 SUBREGs of such registers are not allowed. */
4920 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4921 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4922 x = copy_to_reg (x);
4924 /* Loop over all the words allocated on the stack for this arg. */
4925 /* We can do it by words, because any scalar bigger than a word
4926 has a size a multiple of a word. */
4927 for (i = num_words - 1; i >= not_stack; i--)
4928 if (i >= not_stack + offset)
4929 if (!emit_push_insn (operand_subword_force (x, i, mode),
4930 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4931 0, args_addr,
4932 GEN_INT (args_offset + ((i - not_stack + skip)
4933 * UNITS_PER_WORD)),
4934 reg_parm_stack_space, alignment_pad, sibcall_p))
4935 return false;
4937 else
4939 rtx addr;
4940 rtx dest;
4942 /* Push padding now if padding above and stack grows down,
4943 or if padding below and stack grows up.
4944 But if space already allocated, this has already been done. */
4945 if (maybe_ne (extra, 0)
4946 && args_addr == 0
4947 && where_pad != PAD_NONE
4948 && where_pad != stack_direction)
4949 anti_adjust_stack (gen_int_mode (extra, Pmode));
4951 #ifdef PUSH_ROUNDING
4952 if (args_addr == 0 && targetm.calls.push_argument (0))
4953 emit_single_push_insn (mode, x, type);
4954 else
4955 #endif
4957 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4958 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4960 /* We do *not* set_mem_attributes here, because incoming arguments
4961 may overlap with sibling call outgoing arguments and we cannot
4962 allow reordering of reads from function arguments with stores
4963 to outgoing arguments of sibling calls. We do, however, want
4964 to record the alignment of the stack slot. */
4965 /* ALIGN may well be better aligned than TYPE, e.g. due to
4966 PARM_BOUNDARY. Assume the caller isn't lying. */
4967 set_mem_align (dest, align);
4969 emit_move_insn (dest, x);
4973 /* Move the partial arguments into the registers and any overlapping
4974 values that we moved into the pseudos in tmp_regs. */
4975 if (partial > 0 && reg != 0)
4977 /* Handle calls that pass values in multiple non-contiguous locations.
4978 The Irix 6 ABI has examples of this. */
4979 if (GET_CODE (reg) == PARALLEL)
4980 emit_group_load (reg, x, type, -1);
4981 else
4983 gcc_assert (partial % UNITS_PER_WORD == 0);
4984 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4986 for (int i = 0; i < overlapping; i++)
4987 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4988 + nregs - overlapping + i),
4989 tmp_regs[i]);
4994 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4995 anti_adjust_stack (gen_int_mode (extra, Pmode));
4997 if (alignment_pad && args_addr == 0)
4998 anti_adjust_stack (alignment_pad);
5000 return true;
5003 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5004 operations. */
5006 static rtx
5007 get_subtarget (rtx x)
5009 return (optimize
5010 || x == 0
5011 /* Only registers can be subtargets. */
5012 || !REG_P (x)
5013 /* Don't use hard regs to avoid extending their life. */
5014 || REGNO (x) < FIRST_PSEUDO_REGISTER
5015 ? 0 : x);
5018 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5019 FIELD is a bitfield. Returns true if the optimization was successful,
5020 and there's nothing else to do. */
5022 static bool
5023 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5024 poly_uint64 pbitpos,
5025 poly_uint64 pbitregion_start,
5026 poly_uint64 pbitregion_end,
5027 machine_mode mode1, rtx str_rtx,
5028 tree to, tree src, bool reverse)
5030 /* str_mode is not guaranteed to be a scalar type. */
5031 machine_mode str_mode = GET_MODE (str_rtx);
5032 unsigned int str_bitsize;
5033 tree op0, op1;
5034 rtx value, result;
5035 optab binop;
5036 gimple *srcstmt;
5037 enum tree_code code;
5039 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5040 if (mode1 != VOIDmode
5041 || !pbitsize.is_constant (&bitsize)
5042 || !pbitpos.is_constant (&bitpos)
5043 || !pbitregion_start.is_constant (&bitregion_start)
5044 || !pbitregion_end.is_constant (&bitregion_end)
5045 || bitsize >= BITS_PER_WORD
5046 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5047 || str_bitsize > BITS_PER_WORD
5048 || TREE_SIDE_EFFECTS (to)
5049 || TREE_THIS_VOLATILE (to))
5050 return false;
5052 STRIP_NOPS (src);
5053 if (TREE_CODE (src) != SSA_NAME)
5054 return false;
5055 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5056 return false;
5058 srcstmt = get_gimple_for_ssa_name (src);
5059 if (!srcstmt
5060 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5061 return false;
5063 code = gimple_assign_rhs_code (srcstmt);
5065 op0 = gimple_assign_rhs1 (srcstmt);
5067 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5068 to find its initialization. Hopefully the initialization will
5069 be from a bitfield load. */
5070 if (TREE_CODE (op0) == SSA_NAME)
5072 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5074 /* We want to eventually have OP0 be the same as TO, which
5075 should be a bitfield. */
5076 if (!op0stmt
5077 || !is_gimple_assign (op0stmt)
5078 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5079 return false;
5080 op0 = gimple_assign_rhs1 (op0stmt);
5083 op1 = gimple_assign_rhs2 (srcstmt);
5085 if (!operand_equal_p (to, op0, 0))
5086 return false;
5088 if (MEM_P (str_rtx))
5090 unsigned HOST_WIDE_INT offset1;
5092 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5093 str_bitsize = BITS_PER_WORD;
5095 scalar_int_mode best_mode;
5096 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5097 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5098 return false;
5099 str_mode = best_mode;
5100 str_bitsize = GET_MODE_BITSIZE (best_mode);
5102 offset1 = bitpos;
5103 bitpos %= str_bitsize;
5104 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5105 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5107 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5108 return false;
5110 /* If the bit field covers the whole REG/MEM, store_field
5111 will likely generate better code. */
5112 if (bitsize >= str_bitsize)
5113 return false;
5115 /* We can't handle fields split across multiple entities. */
5116 if (bitpos + bitsize > str_bitsize)
5117 return false;
5119 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5120 bitpos = str_bitsize - bitpos - bitsize;
5122 switch (code)
5124 case PLUS_EXPR:
5125 case MINUS_EXPR:
5126 /* For now, just optimize the case of the topmost bitfield
5127 where we don't need to do any masking and also
5128 1 bit bitfields where xor can be used.
5129 We might win by one instruction for the other bitfields
5130 too if insv/extv instructions aren't used, so that
5131 can be added later. */
5132 if ((reverse || bitpos + bitsize != str_bitsize)
5133 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5134 break;
5136 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5137 value = convert_modes (str_mode,
5138 TYPE_MODE (TREE_TYPE (op1)), value,
5139 TYPE_UNSIGNED (TREE_TYPE (op1)));
5141 /* We may be accessing data outside the field, which means
5142 we can alias adjacent data. */
5143 if (MEM_P (str_rtx))
5145 str_rtx = shallow_copy_rtx (str_rtx);
5146 set_mem_alias_set (str_rtx, 0);
5147 set_mem_expr (str_rtx, 0);
5150 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5152 value = expand_and (str_mode, value, const1_rtx, NULL);
5153 binop = xor_optab;
5155 else
5156 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5158 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5159 if (reverse)
5160 value = flip_storage_order (str_mode, value);
5161 result = expand_binop (str_mode, binop, str_rtx,
5162 value, str_rtx, 1, OPTAB_WIDEN);
5163 if (result != str_rtx)
5164 emit_move_insn (str_rtx, result);
5165 return true;
5167 case BIT_IOR_EXPR:
5168 case BIT_XOR_EXPR:
5169 if (TREE_CODE (op1) != INTEGER_CST)
5170 break;
5171 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5172 value = convert_modes (str_mode,
5173 TYPE_MODE (TREE_TYPE (op1)), value,
5174 TYPE_UNSIGNED (TREE_TYPE (op1)));
5176 /* We may be accessing data outside the field, which means
5177 we can alias adjacent data. */
5178 if (MEM_P (str_rtx))
5180 str_rtx = shallow_copy_rtx (str_rtx);
5181 set_mem_alias_set (str_rtx, 0);
5182 set_mem_expr (str_rtx, 0);
5185 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5186 if (bitpos + bitsize != str_bitsize)
5188 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5189 str_mode);
5190 value = expand_and (str_mode, value, mask, NULL_RTX);
5192 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5193 if (reverse)
5194 value = flip_storage_order (str_mode, value);
5195 result = expand_binop (str_mode, binop, str_rtx,
5196 value, str_rtx, 1, OPTAB_WIDEN);
5197 if (result != str_rtx)
5198 emit_move_insn (str_rtx, result);
5199 return true;
5201 default:
5202 break;
5205 return false;
5208 /* In the C++ memory model, consecutive bit fields in a structure are
5209 considered one memory location.
5211 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5212 returns the bit range of consecutive bits in which this COMPONENT_REF
5213 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5214 and *OFFSET may be adjusted in the process.
5216 If the access does not need to be restricted, 0 is returned in both
5217 *BITSTART and *BITEND. */
5219 void
5220 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5221 poly_int64_pod *bitpos, tree *offset)
5223 poly_int64 bitoffset;
5224 tree field, repr;
5226 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5228 field = TREE_OPERAND (exp, 1);
5229 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5230 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5231 need to limit the range we can access. */
5232 if (!repr)
5234 *bitstart = *bitend = 0;
5235 return;
5238 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5239 part of a larger bit field, then the representative does not serve any
5240 useful purpose. This can occur in Ada. */
5241 if (handled_component_p (TREE_OPERAND (exp, 0)))
5243 machine_mode rmode;
5244 poly_int64 rbitsize, rbitpos;
5245 tree roffset;
5246 int unsignedp, reversep, volatilep = 0;
5247 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5248 &roffset, &rmode, &unsignedp, &reversep,
5249 &volatilep);
5250 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5252 *bitstart = *bitend = 0;
5253 return;
5257 /* Compute the adjustment to bitpos from the offset of the field
5258 relative to the representative. DECL_FIELD_OFFSET of field and
5259 repr are the same by construction if they are not constants,
5260 see finish_bitfield_layout. */
5261 poly_uint64 field_offset, repr_offset;
5262 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5263 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5264 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5265 else
5266 bitoffset = 0;
5267 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5268 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5270 /* If the adjustment is larger than bitpos, we would have a negative bit
5271 position for the lower bound and this may wreak havoc later. Adjust
5272 offset and bitpos to make the lower bound non-negative in that case. */
5273 if (maybe_gt (bitoffset, *bitpos))
5275 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5276 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5278 *bitpos += adjust_bits;
5279 if (*offset == NULL_TREE)
5280 *offset = size_int (-adjust_bytes);
5281 else
5282 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5283 *bitstart = 0;
5285 else
5286 *bitstart = *bitpos - bitoffset;
5288 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5291 /* Returns true if BASE is a DECL that does not reside in memory and
5292 has non-BLKmode. DECL_RTL must not be a MEM; if
5293 DECL_RTL was not set yet, return false. */
5295 bool
5296 non_mem_decl_p (tree base)
5298 if (!DECL_P (base)
5299 || TREE_ADDRESSABLE (base)
5300 || DECL_MODE (base) == BLKmode)
5301 return false;
5303 if (!DECL_RTL_SET_P (base))
5304 return false;
5306 return (!MEM_P (DECL_RTL (base)));
5309 /* Returns true if REF refers to an object that does not
5310 reside in memory and has non-BLKmode. */
5312 bool
5313 mem_ref_refers_to_non_mem_p (tree ref)
5315 tree base;
5317 if (TREE_CODE (ref) == MEM_REF
5318 || TREE_CODE (ref) == TARGET_MEM_REF)
5320 tree addr = TREE_OPERAND (ref, 0);
5322 if (TREE_CODE (addr) != ADDR_EXPR)
5323 return false;
5325 base = TREE_OPERAND (addr, 0);
5327 else
5328 base = ref;
5330 return non_mem_decl_p (base);
5333 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5334 is true, try generating a nontemporal store. */
5336 void
5337 expand_assignment (tree to, tree from, bool nontemporal)
5339 rtx to_rtx = 0;
5340 rtx result;
5341 machine_mode mode;
5342 unsigned int align;
5343 enum insn_code icode;
5345 /* Don't crash if the lhs of the assignment was erroneous. */
5346 if (TREE_CODE (to) == ERROR_MARK)
5348 expand_normal (from);
5349 return;
5352 /* Optimize away no-op moves without side-effects. */
5353 if (operand_equal_p (to, from, 0))
5354 return;
5356 /* Handle misaligned stores. */
5357 mode = TYPE_MODE (TREE_TYPE (to));
5358 if ((TREE_CODE (to) == MEM_REF
5359 || TREE_CODE (to) == TARGET_MEM_REF
5360 || DECL_P (to))
5361 && mode != BLKmode
5362 && !mem_ref_refers_to_non_mem_p (to)
5363 && ((align = get_object_alignment (to))
5364 < GET_MODE_ALIGNMENT (mode))
5365 && (((icode = optab_handler (movmisalign_optab, mode))
5366 != CODE_FOR_nothing)
5367 || targetm.slow_unaligned_access (mode, align)))
5369 rtx reg, mem;
5371 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5372 /* Handle PARALLEL. */
5373 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5374 reg = force_not_mem (reg);
5375 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5376 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5377 reg = flip_storage_order (mode, reg);
5379 if (icode != CODE_FOR_nothing)
5381 class expand_operand ops[2];
5383 create_fixed_operand (&ops[0], mem);
5384 create_input_operand (&ops[1], reg, mode);
5385 /* The movmisalign<mode> pattern cannot fail, else the assignment
5386 would silently be omitted. */
5387 expand_insn (icode, 2, ops);
5389 else
5390 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5391 false);
5392 return;
5395 /* Assignment of a structure component needs special treatment
5396 if the structure component's rtx is not simply a MEM.
5397 Assignment of an array element at a constant index, and assignment of
5398 an array element in an unaligned packed structure field, has the same
5399 problem. Same for (partially) storing into a non-memory object. */
5400 if (handled_component_p (to)
5401 || (TREE_CODE (to) == MEM_REF
5402 && (REF_REVERSE_STORAGE_ORDER (to)
5403 || mem_ref_refers_to_non_mem_p (to)))
5404 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5406 machine_mode mode1;
5407 poly_int64 bitsize, bitpos;
5408 poly_uint64 bitregion_start = 0;
5409 poly_uint64 bitregion_end = 0;
5410 tree offset;
5411 int unsignedp, reversep, volatilep = 0;
5412 tree tem;
5414 push_temp_slots ();
5415 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5416 &unsignedp, &reversep, &volatilep);
5418 /* Make sure bitpos is not negative, it can wreak havoc later. */
5419 if (maybe_lt (bitpos, 0))
5421 gcc_assert (offset == NULL_TREE);
5422 offset = size_int (bits_to_bytes_round_down (bitpos));
5423 bitpos = num_trailing_bits (bitpos);
5426 if (TREE_CODE (to) == COMPONENT_REF
5427 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5428 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5429 /* The C++ memory model naturally applies to byte-aligned fields.
5430 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5431 BITSIZE are not byte-aligned, there is no need to limit the range
5432 we can access. This can occur with packed structures in Ada. */
5433 else if (maybe_gt (bitsize, 0)
5434 && multiple_p (bitsize, BITS_PER_UNIT)
5435 && multiple_p (bitpos, BITS_PER_UNIT))
5437 bitregion_start = bitpos;
5438 bitregion_end = bitpos + bitsize - 1;
5441 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5443 /* If the field has a mode, we want to access it in the
5444 field's mode, not the computed mode.
5445 If a MEM has VOIDmode (external with incomplete type),
5446 use BLKmode for it instead. */
5447 if (MEM_P (to_rtx))
5449 if (mode1 != VOIDmode)
5450 to_rtx = adjust_address (to_rtx, mode1, 0);
5451 else if (GET_MODE (to_rtx) == VOIDmode)
5452 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5455 if (offset != 0)
5457 machine_mode address_mode;
5458 rtx offset_rtx;
5460 if (!MEM_P (to_rtx))
5462 /* We can get constant negative offsets into arrays with broken
5463 user code. Translate this to a trap instead of ICEing. */
5464 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5465 expand_builtin_trap ();
5466 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5469 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5470 address_mode = get_address_mode (to_rtx);
5471 if (GET_MODE (offset_rtx) != address_mode)
5473 /* We cannot be sure that the RTL in offset_rtx is valid outside
5474 of a memory address context, so force it into a register
5475 before attempting to convert it to the desired mode. */
5476 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5477 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5480 /* If we have an expression in OFFSET_RTX and a non-zero
5481 byte offset in BITPOS, adding the byte offset before the
5482 OFFSET_RTX results in better intermediate code, which makes
5483 later rtl optimization passes perform better.
5485 We prefer intermediate code like this:
5487 r124:DI=r123:DI+0x18
5488 [r124:DI]=r121:DI
5490 ... instead of ...
5492 r124:DI=r123:DI+0x10
5493 [r124:DI+0x8]=r121:DI
5495 This is only done for aligned data values, as these can
5496 be expected to result in single move instructions. */
5497 poly_int64 bytepos;
5498 if (mode1 != VOIDmode
5499 && maybe_ne (bitpos, 0)
5500 && maybe_gt (bitsize, 0)
5501 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5502 && multiple_p (bitpos, bitsize)
5503 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5504 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5506 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5507 bitregion_start = 0;
5508 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5509 bitregion_end -= bitpos;
5510 bitpos = 0;
5513 to_rtx = offset_address (to_rtx, offset_rtx,
5514 highest_pow2_factor_for_target (to,
5515 offset));
5518 /* No action is needed if the target is not a memory and the field
5519 lies completely outside that target. This can occur if the source
5520 code contains an out-of-bounds access to a small array. */
5521 if (!MEM_P (to_rtx)
5522 && GET_MODE (to_rtx) != BLKmode
5523 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5525 expand_normal (from);
5526 result = NULL;
5528 /* Handle expand_expr of a complex value returning a CONCAT. */
5529 else if (GET_CODE (to_rtx) == CONCAT)
5531 machine_mode to_mode = GET_MODE (to_rtx);
5532 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5533 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5534 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5535 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5536 && known_eq (bitpos, 0)
5537 && known_eq (bitsize, mode_bitsize))
5538 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5539 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5540 && known_eq (bitsize, inner_bitsize)
5541 && (known_eq (bitpos, 0)
5542 || known_eq (bitpos, inner_bitsize)))
5543 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5544 false, nontemporal, reversep);
5545 else if (known_le (bitpos + bitsize, inner_bitsize))
5546 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5547 bitregion_start, bitregion_end,
5548 mode1, from, get_alias_set (to),
5549 nontemporal, reversep);
5550 else if (known_ge (bitpos, inner_bitsize))
5551 result = store_field (XEXP (to_rtx, 1), bitsize,
5552 bitpos - inner_bitsize,
5553 bitregion_start, bitregion_end,
5554 mode1, from, get_alias_set (to),
5555 nontemporal, reversep);
5556 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5558 result = expand_normal (from);
5559 if (GET_CODE (result) == CONCAT)
5561 to_mode = GET_MODE_INNER (to_mode);
5562 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5563 rtx from_real
5564 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5565 from_mode, 0);
5566 rtx from_imag
5567 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5568 from_mode, 0);
5569 if (!from_real || !from_imag)
5570 goto concat_store_slow;
5571 emit_move_insn (XEXP (to_rtx, 0), from_real);
5572 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5574 else
5576 machine_mode from_mode
5577 = GET_MODE (result) == VOIDmode
5578 ? TYPE_MODE (TREE_TYPE (from))
5579 : GET_MODE (result);
5580 rtx from_rtx;
5581 if (MEM_P (result))
5582 from_rtx = change_address (result, to_mode, NULL_RTX);
5583 else
5584 from_rtx
5585 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5586 if (from_rtx)
5588 emit_move_insn (XEXP (to_rtx, 0),
5589 read_complex_part (from_rtx, false));
5590 emit_move_insn (XEXP (to_rtx, 1),
5591 read_complex_part (from_rtx, true));
5593 else
5595 to_mode = GET_MODE_INNER (to_mode);
5596 rtx from_real
5597 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5598 rtx from_imag
5599 = simplify_gen_subreg (to_mode, result, from_mode,
5600 GET_MODE_SIZE (to_mode));
5601 if (!from_real || !from_imag)
5602 goto concat_store_slow;
5603 emit_move_insn (XEXP (to_rtx, 0), from_real);
5604 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5608 else
5610 concat_store_slow:;
5611 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5612 GET_MODE_SIZE (GET_MODE (to_rtx)));
5613 write_complex_part (temp, XEXP (to_rtx, 0), false);
5614 write_complex_part (temp, XEXP (to_rtx, 1), true);
5615 result = store_field (temp, bitsize, bitpos,
5616 bitregion_start, bitregion_end,
5617 mode1, from, get_alias_set (to),
5618 nontemporal, reversep);
5619 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5620 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5623 /* For calls to functions returning variable length structures, if TO_RTX
5624 is not a MEM, go through a MEM because we must not create temporaries
5625 of the VLA type. */
5626 else if (!MEM_P (to_rtx)
5627 && TREE_CODE (from) == CALL_EXPR
5628 && COMPLETE_TYPE_P (TREE_TYPE (from))
5629 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5631 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5632 GET_MODE_SIZE (GET_MODE (to_rtx)));
5633 result = store_field (temp, bitsize, bitpos, bitregion_start,
5634 bitregion_end, mode1, from, get_alias_set (to),
5635 nontemporal, reversep);
5636 emit_move_insn (to_rtx, temp);
5638 else
5640 if (MEM_P (to_rtx))
5642 /* If the field is at offset zero, we could have been given the
5643 DECL_RTX of the parent struct. Don't munge it. */
5644 to_rtx = shallow_copy_rtx (to_rtx);
5645 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5646 if (volatilep)
5647 MEM_VOLATILE_P (to_rtx) = 1;
5650 gcc_checking_assert (known_ge (bitpos, 0));
5651 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5652 bitregion_start, bitregion_end,
5653 mode1, to_rtx, to, from,
5654 reversep))
5655 result = NULL;
5656 else if (SUBREG_P (to_rtx)
5657 && SUBREG_PROMOTED_VAR_P (to_rtx))
5659 /* If to_rtx is a promoted subreg, we need to zero or sign
5660 extend the value afterwards. */
5661 if (TREE_CODE (to) == MEM_REF
5662 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5663 && !REF_REVERSE_STORAGE_ORDER (to)
5664 && known_eq (bitpos, 0)
5665 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5666 result = store_expr (from, to_rtx, 0, nontemporal, false);
5667 else
5669 rtx to_rtx1
5670 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5671 SUBREG_REG (to_rtx),
5672 subreg_promoted_mode (to_rtx));
5673 result = store_field (to_rtx1, bitsize, bitpos,
5674 bitregion_start, bitregion_end,
5675 mode1, from, get_alias_set (to),
5676 nontemporal, reversep);
5677 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5678 SUBREG_PROMOTED_SIGN (to_rtx));
5681 else
5682 result = store_field (to_rtx, bitsize, bitpos,
5683 bitregion_start, bitregion_end,
5684 mode1, from, get_alias_set (to),
5685 nontemporal, reversep);
5688 if (result)
5689 preserve_temp_slots (result);
5690 pop_temp_slots ();
5691 return;
5694 /* If the rhs is a function call and its value is not an aggregate,
5695 call the function before we start to compute the lhs.
5696 This is needed for correct code for cases such as
5697 val = setjmp (buf) on machines where reference to val
5698 requires loading up part of an address in a separate insn.
5700 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5701 since it might be a promoted variable where the zero- or sign- extension
5702 needs to be done. Handling this in the normal way is safe because no
5703 computation is done before the call. The same is true for SSA names. */
5704 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5705 && COMPLETE_TYPE_P (TREE_TYPE (from))
5706 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5707 && ! (((VAR_P (to)
5708 || TREE_CODE (to) == PARM_DECL
5709 || TREE_CODE (to) == RESULT_DECL)
5710 && REG_P (DECL_RTL (to)))
5711 || TREE_CODE (to) == SSA_NAME))
5713 rtx value;
5715 push_temp_slots ();
5716 value = expand_normal (from);
5718 if (to_rtx == 0)
5719 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5721 /* Handle calls that return values in multiple non-contiguous locations.
5722 The Irix 6 ABI has examples of this. */
5723 if (GET_CODE (to_rtx) == PARALLEL)
5725 if (GET_CODE (value) == PARALLEL)
5726 emit_group_move (to_rtx, value);
5727 else
5728 emit_group_load (to_rtx, value, TREE_TYPE (from),
5729 int_size_in_bytes (TREE_TYPE (from)));
5731 else if (GET_CODE (value) == PARALLEL)
5732 emit_group_store (to_rtx, value, TREE_TYPE (from),
5733 int_size_in_bytes (TREE_TYPE (from)));
5734 else if (GET_MODE (to_rtx) == BLKmode)
5736 /* Handle calls that return BLKmode values in registers. */
5737 if (REG_P (value))
5738 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5739 else
5740 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5742 else
5744 if (POINTER_TYPE_P (TREE_TYPE (to)))
5745 value = convert_memory_address_addr_space
5746 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5747 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5749 emit_move_insn (to_rtx, value);
5752 preserve_temp_slots (to_rtx);
5753 pop_temp_slots ();
5754 return;
5757 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5758 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5760 /* Don't move directly into a return register. */
5761 if (TREE_CODE (to) == RESULT_DECL
5762 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5764 rtx temp;
5766 push_temp_slots ();
5768 /* If the source is itself a return value, it still is in a pseudo at
5769 this point so we can move it back to the return register directly. */
5770 if (REG_P (to_rtx)
5771 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5772 && TREE_CODE (from) != CALL_EXPR)
5773 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5774 else
5775 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5777 /* Handle calls that return values in multiple non-contiguous locations.
5778 The Irix 6 ABI has examples of this. */
5779 if (GET_CODE (to_rtx) == PARALLEL)
5781 if (GET_CODE (temp) == PARALLEL)
5782 emit_group_move (to_rtx, temp);
5783 else
5784 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5785 int_size_in_bytes (TREE_TYPE (from)));
5787 else if (temp)
5788 emit_move_insn (to_rtx, temp);
5790 preserve_temp_slots (to_rtx);
5791 pop_temp_slots ();
5792 return;
5795 /* In case we are returning the contents of an object which overlaps
5796 the place the value is being stored, use a safe function when copying
5797 a value through a pointer into a structure value return block. */
5798 if (TREE_CODE (to) == RESULT_DECL
5799 && TREE_CODE (from) == INDIRECT_REF
5800 && ADDR_SPACE_GENERIC_P
5801 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5802 && refs_may_alias_p (to, from)
5803 && cfun->returns_struct
5804 && !cfun->returns_pcc_struct)
5806 rtx from_rtx, size;
5808 push_temp_slots ();
5809 size = expr_size (from);
5810 from_rtx = expand_normal (from);
5812 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5814 preserve_temp_slots (to_rtx);
5815 pop_temp_slots ();
5816 return;
5819 /* Compute FROM and store the value in the rtx we got. */
5821 push_temp_slots ();
5822 result = store_expr (from, to_rtx, 0, nontemporal, false);
5823 preserve_temp_slots (result);
5824 pop_temp_slots ();
5825 return;
5828 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5829 succeeded, false otherwise. */
5831 bool
5832 emit_storent_insn (rtx to, rtx from)
5834 class expand_operand ops[2];
5835 machine_mode mode = GET_MODE (to);
5836 enum insn_code code = optab_handler (storent_optab, mode);
5838 if (code == CODE_FOR_nothing)
5839 return false;
5841 create_fixed_operand (&ops[0], to);
5842 create_input_operand (&ops[1], from, mode);
5843 return maybe_expand_insn (code, 2, ops);
5846 /* Helper function for store_expr storing of STRING_CST. */
5848 static rtx
5849 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5850 fixed_size_mode mode)
5852 tree str = (tree) data;
5854 gcc_assert (offset >= 0);
5855 if (offset >= TREE_STRING_LENGTH (str))
5856 return const0_rtx;
5858 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5859 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5861 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5862 size_t l = TREE_STRING_LENGTH (str) - offset;
5863 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5864 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5865 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
5868 /* The by-pieces infrastructure does not try to pick a vector mode
5869 for storing STRING_CST. */
5870 return c_readstr (TREE_STRING_POINTER (str) + offset,
5871 as_a <scalar_int_mode> (mode), false);
5874 /* Generate code for computing expression EXP,
5875 and storing the value into TARGET.
5877 If the mode is BLKmode then we may return TARGET itself.
5878 It turns out that in BLKmode it doesn't cause a problem.
5879 because C has no operators that could combine two different
5880 assignments into the same BLKmode object with different values
5881 with no sequence point. Will other languages need this to
5882 be more thorough?
5884 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5885 stack, and block moves may need to be treated specially.
5887 If NONTEMPORAL is true, try using a nontemporal store instruction.
5889 If REVERSE is true, the store is to be done in reverse order. */
5892 store_expr (tree exp, rtx target, int call_param_p,
5893 bool nontemporal, bool reverse)
5895 rtx temp;
5896 rtx alt_rtl = NULL_RTX;
5897 location_t loc = curr_insn_location ();
5898 bool shortened_string_cst = false;
5900 if (VOID_TYPE_P (TREE_TYPE (exp)))
5902 /* C++ can generate ?: expressions with a throw expression in one
5903 branch and an rvalue in the other. Here, we resolve attempts to
5904 store the throw expression's nonexistent result. */
5905 gcc_assert (!call_param_p);
5906 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5907 return NULL_RTX;
5909 if (TREE_CODE (exp) == COMPOUND_EXPR)
5911 /* Perform first part of compound expression, then assign from second
5912 part. */
5913 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5914 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5915 return store_expr (TREE_OPERAND (exp, 1), target,
5916 call_param_p, nontemporal, reverse);
5918 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5920 /* For conditional expression, get safe form of the target. Then
5921 test the condition, doing the appropriate assignment on either
5922 side. This avoids the creation of unnecessary temporaries.
5923 For non-BLKmode, it is more efficient not to do this. */
5925 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5927 do_pending_stack_adjust ();
5928 NO_DEFER_POP;
5929 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5930 profile_probability::uninitialized ());
5931 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5932 nontemporal, reverse);
5933 emit_jump_insn (targetm.gen_jump (lab2));
5934 emit_barrier ();
5935 emit_label (lab1);
5936 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5937 nontemporal, reverse);
5938 emit_label (lab2);
5939 OK_DEFER_POP;
5941 return NULL_RTX;
5943 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5944 /* If this is a scalar in a register that is stored in a wider mode
5945 than the declared mode, compute the result into its declared mode
5946 and then convert to the wider mode. Our value is the computed
5947 expression. */
5949 rtx inner_target = 0;
5950 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5951 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5953 /* We can do the conversion inside EXP, which will often result
5954 in some optimizations. Do the conversion in two steps: first
5955 change the signedness, if needed, then the extend. But don't
5956 do this if the type of EXP is a subtype of something else
5957 since then the conversion might involve more than just
5958 converting modes. */
5959 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5960 && TREE_TYPE (TREE_TYPE (exp)) == 0
5961 && GET_MODE_PRECISION (outer_mode)
5962 == TYPE_PRECISION (TREE_TYPE (exp)))
5964 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5965 TYPE_UNSIGNED (TREE_TYPE (exp))))
5967 /* Some types, e.g. Fortran's logical*4, won't have a signed
5968 version, so use the mode instead. */
5969 tree ntype
5970 = (signed_or_unsigned_type_for
5971 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5972 if (ntype == NULL)
5973 ntype = lang_hooks.types.type_for_mode
5974 (TYPE_MODE (TREE_TYPE (exp)),
5975 SUBREG_PROMOTED_SIGN (target));
5977 exp = fold_convert_loc (loc, ntype, exp);
5980 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5981 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5982 exp);
5984 inner_target = SUBREG_REG (target);
5987 temp = expand_expr (exp, inner_target, VOIDmode,
5988 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5991 /* If TEMP is a VOIDmode constant, use convert_modes to make
5992 sure that we properly convert it. */
5993 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5995 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5996 temp, SUBREG_PROMOTED_SIGN (target));
5997 temp = convert_modes (inner_mode, outer_mode, temp,
5998 SUBREG_PROMOTED_SIGN (target));
6001 convert_move (SUBREG_REG (target), temp,
6002 SUBREG_PROMOTED_SIGN (target));
6004 return NULL_RTX;
6006 else if ((TREE_CODE (exp) == STRING_CST
6007 || (TREE_CODE (exp) == MEM_REF
6008 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6009 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6010 == STRING_CST
6011 && integer_zerop (TREE_OPERAND (exp, 1))))
6012 && !nontemporal && !call_param_p
6013 && MEM_P (target))
6015 /* Optimize initialization of an array with a STRING_CST. */
6016 HOST_WIDE_INT exp_len, str_copy_len;
6017 rtx dest_mem;
6018 tree str = TREE_CODE (exp) == STRING_CST
6019 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6021 exp_len = int_expr_size (exp);
6022 if (exp_len <= 0)
6023 goto normal_expr;
6025 if (TREE_STRING_LENGTH (str) <= 0)
6026 goto normal_expr;
6028 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6029 MEM_ALIGN (target), false))
6031 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6032 MEM_ALIGN (target), false, RETURN_BEGIN);
6033 return NULL_RTX;
6036 str_copy_len = TREE_STRING_LENGTH (str);
6037 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6039 str_copy_len += STORE_MAX_PIECES - 1;
6040 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6042 if (str_copy_len >= exp_len)
6043 goto normal_expr;
6045 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6046 (void *) str, MEM_ALIGN (target), false))
6047 goto normal_expr;
6049 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6050 (void *) str, MEM_ALIGN (target), false,
6051 RETURN_END);
6052 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6053 exp_len - str_copy_len),
6054 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6055 return NULL_RTX;
6057 else
6059 rtx tmp_target;
6061 normal_expr:
6062 /* If we want to use a nontemporal or a reverse order store, force the
6063 value into a register first. */
6064 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6065 tree rexp = exp;
6066 if (TREE_CODE (exp) == STRING_CST
6067 && tmp_target == target
6068 && GET_MODE (target) == BLKmode
6069 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6071 rtx size = expr_size (exp);
6072 if (CONST_INT_P (size)
6073 && size != const0_rtx
6074 && (UINTVAL (size)
6075 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6077 /* If the STRING_CST has much larger array type than
6078 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6079 it into the rodata section as the code later on will use
6080 memset zero for the remainder anyway. See PR95052. */
6081 tmp_target = NULL_RTX;
6082 rexp = copy_node (exp);
6083 tree index
6084 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6085 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6086 index);
6087 shortened_string_cst = true;
6090 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6091 (call_param_p
6092 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6093 &alt_rtl, false);
6094 if (shortened_string_cst)
6096 gcc_assert (MEM_P (temp));
6097 temp = change_address (temp, BLKmode, NULL_RTX);
6101 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6102 the same as that of TARGET, adjust the constant. This is needed, for
6103 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6104 only a word-sized value. */
6105 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6106 && TREE_CODE (exp) != ERROR_MARK
6107 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6109 gcc_assert (!shortened_string_cst);
6110 if (GET_MODE_CLASS (GET_MODE (target))
6111 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6112 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6113 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6115 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6116 TYPE_MODE (TREE_TYPE (exp)), 0);
6117 if (t)
6118 temp = t;
6120 if (GET_MODE (temp) == VOIDmode)
6121 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6122 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6125 /* If value was not generated in the target, store it there.
6126 Convert the value to TARGET's type first if necessary and emit the
6127 pending incrementations that have been queued when expanding EXP.
6128 Note that we cannot emit the whole queue blindly because this will
6129 effectively disable the POST_INC optimization later.
6131 If TEMP and TARGET compare equal according to rtx_equal_p, but
6132 one or both of them are volatile memory refs, we have to distinguish
6133 two cases:
6134 - expand_expr has used TARGET. In this case, we must not generate
6135 another copy. This can be detected by TARGET being equal according
6136 to == .
6137 - expand_expr has not used TARGET - that means that the source just
6138 happens to have the same RTX form. Since temp will have been created
6139 by expand_expr, it will compare unequal according to == .
6140 We must generate a copy in this case, to reach the correct number
6141 of volatile memory references. */
6143 if ((! rtx_equal_p (temp, target)
6144 || (temp != target && (side_effects_p (temp)
6145 || side_effects_p (target))))
6146 && TREE_CODE (exp) != ERROR_MARK
6147 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6148 but TARGET is not valid memory reference, TEMP will differ
6149 from TARGET although it is really the same location. */
6150 && !(alt_rtl
6151 && rtx_equal_p (alt_rtl, target)
6152 && !side_effects_p (alt_rtl)
6153 && !side_effects_p (target))
6154 /* If there's nothing to copy, don't bother. Don't call
6155 expr_size unless necessary, because some front-ends (C++)
6156 expr_size-hook must not be given objects that are not
6157 supposed to be bit-copied or bit-initialized. */
6158 && expr_size (exp) != const0_rtx)
6160 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6162 gcc_assert (!shortened_string_cst);
6163 if (GET_MODE (target) == BLKmode)
6165 /* Handle calls that return BLKmode values in registers. */
6166 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6167 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6168 else
6169 store_bit_field (target,
6170 rtx_to_poly_int64 (expr_size (exp))
6171 * BITS_PER_UNIT,
6172 0, 0, 0, GET_MODE (temp), temp, reverse);
6174 else
6175 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6178 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6180 /* Handle copying a string constant into an array. The string
6181 constant may be shorter than the array. So copy just the string's
6182 actual length, and clear the rest. First get the size of the data
6183 type of the string, which is actually the size of the target. */
6184 rtx size = expr_size (exp);
6186 if (CONST_INT_P (size)
6187 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6188 emit_block_move (target, temp, size,
6189 (call_param_p
6190 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6191 else
6193 machine_mode pointer_mode
6194 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6195 machine_mode address_mode = get_address_mode (target);
6197 /* Compute the size of the data to copy from the string. */
6198 tree copy_size
6199 = size_binop_loc (loc, MIN_EXPR,
6200 make_tree (sizetype, size),
6201 size_int (TREE_STRING_LENGTH (exp)));
6202 rtx copy_size_rtx
6203 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6204 (call_param_p
6205 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6206 rtx_code_label *label = 0;
6208 /* Copy that much. */
6209 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6210 TYPE_UNSIGNED (sizetype));
6211 emit_block_move (target, temp, copy_size_rtx,
6212 (call_param_p
6213 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6215 /* Figure out how much is left in TARGET that we have to clear.
6216 Do all calculations in pointer_mode. */
6217 poly_int64 const_copy_size;
6218 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6220 size = plus_constant (address_mode, size, -const_copy_size);
6221 target = adjust_address (target, BLKmode, const_copy_size);
6223 else
6225 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6226 copy_size_rtx, NULL_RTX, 0,
6227 OPTAB_LIB_WIDEN);
6229 if (GET_MODE (copy_size_rtx) != address_mode)
6230 copy_size_rtx = convert_to_mode (address_mode,
6231 copy_size_rtx,
6232 TYPE_UNSIGNED (sizetype));
6234 target = offset_address (target, copy_size_rtx,
6235 highest_pow2_factor (copy_size));
6236 label = gen_label_rtx ();
6237 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6238 GET_MODE (size), 0, label);
6241 if (size != const0_rtx)
6242 clear_storage (target, size, BLOCK_OP_NORMAL);
6244 if (label)
6245 emit_label (label);
6248 else if (shortened_string_cst)
6249 gcc_unreachable ();
6250 /* Handle calls that return values in multiple non-contiguous locations.
6251 The Irix 6 ABI has examples of this. */
6252 else if (GET_CODE (target) == PARALLEL)
6254 if (GET_CODE (temp) == PARALLEL)
6255 emit_group_move (target, temp);
6256 else
6257 emit_group_load (target, temp, TREE_TYPE (exp),
6258 int_size_in_bytes (TREE_TYPE (exp)));
6260 else if (GET_CODE (temp) == PARALLEL)
6261 emit_group_store (target, temp, TREE_TYPE (exp),
6262 int_size_in_bytes (TREE_TYPE (exp)));
6263 else if (GET_MODE (temp) == BLKmode)
6264 emit_block_move (target, temp, expr_size (exp),
6265 (call_param_p
6266 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6267 /* If we emit a nontemporal store, there is nothing else to do. */
6268 else if (nontemporal && emit_storent_insn (target, temp))
6270 else
6272 if (reverse)
6273 temp = flip_storage_order (GET_MODE (target), temp);
6274 temp = force_operand (temp, target);
6275 if (temp != target)
6276 emit_move_insn (target, temp);
6279 else
6280 gcc_assert (!shortened_string_cst);
6282 return NULL_RTX;
6285 /* Return true if field F of structure TYPE is a flexible array. */
6287 static bool
6288 flexible_array_member_p (const_tree f, const_tree type)
6290 const_tree tf;
6292 tf = TREE_TYPE (f);
6293 return (DECL_CHAIN (f) == NULL
6294 && TREE_CODE (tf) == ARRAY_TYPE
6295 && TYPE_DOMAIN (tf)
6296 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6297 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6298 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6299 && int_size_in_bytes (type) >= 0);
6302 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6303 must have in order for it to completely initialize a value of type TYPE.
6304 Return -1 if the number isn't known.
6306 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6308 static HOST_WIDE_INT
6309 count_type_elements (const_tree type, bool for_ctor_p)
6311 switch (TREE_CODE (type))
6313 case ARRAY_TYPE:
6315 tree nelts;
6317 nelts = array_type_nelts (type);
6318 if (nelts && tree_fits_uhwi_p (nelts))
6320 unsigned HOST_WIDE_INT n;
6322 n = tree_to_uhwi (nelts) + 1;
6323 if (n == 0 || for_ctor_p)
6324 return n;
6325 else
6326 return n * count_type_elements (TREE_TYPE (type), false);
6328 return for_ctor_p ? -1 : 1;
6331 case RECORD_TYPE:
6333 unsigned HOST_WIDE_INT n;
6334 tree f;
6336 n = 0;
6337 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6338 if (TREE_CODE (f) == FIELD_DECL)
6340 if (!for_ctor_p)
6341 n += count_type_elements (TREE_TYPE (f), false);
6342 else if (!flexible_array_member_p (f, type))
6343 /* Don't count flexible arrays, which are not supposed
6344 to be initialized. */
6345 n += 1;
6348 return n;
6351 case UNION_TYPE:
6352 case QUAL_UNION_TYPE:
6354 tree f;
6355 HOST_WIDE_INT n, m;
6357 gcc_assert (!for_ctor_p);
6358 /* Estimate the number of scalars in each field and pick the
6359 maximum. Other estimates would do instead; the idea is simply
6360 to make sure that the estimate is not sensitive to the ordering
6361 of the fields. */
6362 n = 1;
6363 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6364 if (TREE_CODE (f) == FIELD_DECL)
6366 m = count_type_elements (TREE_TYPE (f), false);
6367 /* If the field doesn't span the whole union, add an extra
6368 scalar for the rest. */
6369 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6370 TYPE_SIZE (type)) != 1)
6371 m++;
6372 if (n < m)
6373 n = m;
6375 return n;
6378 case COMPLEX_TYPE:
6379 return 2;
6381 case VECTOR_TYPE:
6383 unsigned HOST_WIDE_INT nelts;
6384 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6385 return nelts;
6386 else
6387 return -1;
6390 case INTEGER_TYPE:
6391 case REAL_TYPE:
6392 case FIXED_POINT_TYPE:
6393 case ENUMERAL_TYPE:
6394 case BOOLEAN_TYPE:
6395 case POINTER_TYPE:
6396 case OFFSET_TYPE:
6397 case REFERENCE_TYPE:
6398 case NULLPTR_TYPE:
6399 return 1;
6401 case ERROR_MARK:
6402 return 0;
6404 case VOID_TYPE:
6405 case OPAQUE_TYPE:
6406 case METHOD_TYPE:
6407 case FUNCTION_TYPE:
6408 case LANG_TYPE:
6409 default:
6410 gcc_unreachable ();
6414 /* Helper for categorize_ctor_elements. Identical interface. */
6416 static bool
6417 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6418 HOST_WIDE_INT *p_unique_nz_elts,
6419 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6421 unsigned HOST_WIDE_INT idx;
6422 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6423 tree value, purpose, elt_type;
6425 /* Whether CTOR is a valid constant initializer, in accordance with what
6426 initializer_constant_valid_p does. If inferred from the constructor
6427 elements, true until proven otherwise. */
6428 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6429 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6431 nz_elts = 0;
6432 unique_nz_elts = 0;
6433 init_elts = 0;
6434 num_fields = 0;
6435 elt_type = NULL_TREE;
6437 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6439 HOST_WIDE_INT mult = 1;
6441 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6443 tree lo_index = TREE_OPERAND (purpose, 0);
6444 tree hi_index = TREE_OPERAND (purpose, 1);
6446 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6447 mult = (tree_to_uhwi (hi_index)
6448 - tree_to_uhwi (lo_index) + 1);
6450 num_fields += mult;
6451 elt_type = TREE_TYPE (value);
6453 switch (TREE_CODE (value))
6455 case CONSTRUCTOR:
6457 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6459 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6460 &ic, p_complete);
6462 nz_elts += mult * nz;
6463 unique_nz_elts += unz;
6464 init_elts += mult * ic;
6466 if (const_from_elts_p && const_p)
6467 const_p = const_elt_p;
6469 break;
6471 case INTEGER_CST:
6472 case REAL_CST:
6473 case FIXED_CST:
6474 if (!initializer_zerop (value))
6476 nz_elts += mult;
6477 unique_nz_elts++;
6479 init_elts += mult;
6480 break;
6482 case STRING_CST:
6483 nz_elts += mult * TREE_STRING_LENGTH (value);
6484 unique_nz_elts += TREE_STRING_LENGTH (value);
6485 init_elts += mult * TREE_STRING_LENGTH (value);
6486 break;
6488 case COMPLEX_CST:
6489 if (!initializer_zerop (TREE_REALPART (value)))
6491 nz_elts += mult;
6492 unique_nz_elts++;
6494 if (!initializer_zerop (TREE_IMAGPART (value)))
6496 nz_elts += mult;
6497 unique_nz_elts++;
6499 init_elts += 2 * mult;
6500 break;
6502 case VECTOR_CST:
6504 /* We can only construct constant-length vectors using
6505 CONSTRUCTOR. */
6506 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6507 for (unsigned int i = 0; i < nunits; ++i)
6509 tree v = VECTOR_CST_ELT (value, i);
6510 if (!initializer_zerop (v))
6512 nz_elts += mult;
6513 unique_nz_elts++;
6515 init_elts += mult;
6518 break;
6520 default:
6522 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6523 nz_elts += mult * tc;
6524 unique_nz_elts += tc;
6525 init_elts += mult * tc;
6527 if (const_from_elts_p && const_p)
6528 const_p
6529 = initializer_constant_valid_p (value,
6530 elt_type,
6531 TYPE_REVERSE_STORAGE_ORDER
6532 (TREE_TYPE (ctor)))
6533 != NULL_TREE;
6535 break;
6539 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6540 num_fields, elt_type))
6541 *p_complete = false;
6543 *p_nz_elts += nz_elts;
6544 *p_unique_nz_elts += unique_nz_elts;
6545 *p_init_elts += init_elts;
6547 return const_p;
6550 /* Examine CTOR to discover:
6551 * how many scalar fields are set to nonzero values,
6552 and place it in *P_NZ_ELTS;
6553 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6554 high - low + 1 (this can be useful for callers to determine ctors
6555 that could be cheaply initialized with - perhaps nested - loops
6556 compared to copied from huge read-only data),
6557 and place it in *P_UNIQUE_NZ_ELTS;
6558 * how many scalar fields in total are in CTOR,
6559 and place it in *P_ELT_COUNT.
6560 * whether the constructor is complete -- in the sense that every
6561 meaningful byte is explicitly given a value --
6562 and place it in *P_COMPLETE.
6564 Return whether or not CTOR is a valid static constant initializer, the same
6565 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6567 bool
6568 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6569 HOST_WIDE_INT *p_unique_nz_elts,
6570 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6572 *p_nz_elts = 0;
6573 *p_unique_nz_elts = 0;
6574 *p_init_elts = 0;
6575 *p_complete = true;
6577 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6578 p_init_elts, p_complete);
6581 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6582 of which had type LAST_TYPE. Each element was itself a complete
6583 initializer, in the sense that every meaningful byte was explicitly
6584 given a value. Return true if the same is true for the constructor
6585 as a whole. */
6587 bool
6588 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6589 const_tree last_type)
6591 if (TREE_CODE (type) == UNION_TYPE
6592 || TREE_CODE (type) == QUAL_UNION_TYPE)
6594 if (num_elts == 0)
6595 return false;
6597 gcc_assert (num_elts == 1 && last_type);
6599 /* ??? We could look at each element of the union, and find the
6600 largest element. Which would avoid comparing the size of the
6601 initialized element against any tail padding in the union.
6602 Doesn't seem worth the effort... */
6603 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6606 return count_type_elements (type, true) == num_elts;
6609 /* Return 1 if EXP contains mostly (3/4) zeros. */
6611 static int
6612 mostly_zeros_p (const_tree exp)
6614 if (TREE_CODE (exp) == CONSTRUCTOR)
6616 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6617 bool complete_p;
6619 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6620 &complete_p);
6621 return !complete_p || nz_elts < init_elts / 4;
6624 return initializer_zerop (exp);
6627 /* Return 1 if EXP contains all zeros. */
6629 static int
6630 all_zeros_p (const_tree exp)
6632 if (TREE_CODE (exp) == CONSTRUCTOR)
6634 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6635 bool complete_p;
6637 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6638 &complete_p);
6639 return nz_elts == 0;
6642 return initializer_zerop (exp);
6645 /* Helper function for store_constructor.
6646 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6647 CLEARED is as for store_constructor.
6648 ALIAS_SET is the alias set to use for any stores.
6649 If REVERSE is true, the store is to be done in reverse order.
6651 This provides a recursive shortcut back to store_constructor when it isn't
6652 necessary to go through store_field. This is so that we can pass through
6653 the cleared field to let store_constructor know that we may not have to
6654 clear a substructure if the outer structure has already been cleared. */
6656 static void
6657 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6658 poly_uint64 bitregion_start,
6659 poly_uint64 bitregion_end,
6660 machine_mode mode,
6661 tree exp, int cleared,
6662 alias_set_type alias_set, bool reverse)
6664 poly_int64 bytepos;
6665 poly_uint64 bytesize;
6666 if (TREE_CODE (exp) == CONSTRUCTOR
6667 /* We can only call store_constructor recursively if the size and
6668 bit position are on a byte boundary. */
6669 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6670 && maybe_ne (bitsize, 0U)
6671 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6672 /* If we have a nonzero bitpos for a register target, then we just
6673 let store_field do the bitfield handling. This is unlikely to
6674 generate unnecessary clear instructions anyways. */
6675 && (known_eq (bitpos, 0) || MEM_P (target)))
6677 if (MEM_P (target))
6679 machine_mode target_mode = GET_MODE (target);
6680 if (target_mode != BLKmode
6681 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6682 target_mode = BLKmode;
6683 target = adjust_address (target, target_mode, bytepos);
6687 /* Update the alias set, if required. */
6688 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6689 && MEM_ALIAS_SET (target) != 0)
6691 target = copy_rtx (target);
6692 set_mem_alias_set (target, alias_set);
6695 store_constructor (exp, target, cleared, bytesize, reverse);
6697 else
6698 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6699 exp, alias_set, false, reverse);
6703 /* Returns the number of FIELD_DECLs in TYPE. */
6705 static int
6706 fields_length (const_tree type)
6708 tree t = TYPE_FIELDS (type);
6709 int count = 0;
6711 for (; t; t = DECL_CHAIN (t))
6712 if (TREE_CODE (t) == FIELD_DECL)
6713 ++count;
6715 return count;
6719 /* Store the value of constructor EXP into the rtx TARGET.
6720 TARGET is either a REG or a MEM; we know it cannot conflict, since
6721 safe_from_p has been called.
6722 CLEARED is true if TARGET is known to have been zero'd.
6723 SIZE is the number of bytes of TARGET we are allowed to modify: this
6724 may not be the same as the size of EXP if we are assigning to a field
6725 which has been packed to exclude padding bits.
6726 If REVERSE is true, the store is to be done in reverse order. */
6728 static void
6729 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6730 bool reverse)
6732 tree type = TREE_TYPE (exp);
6733 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6734 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6736 switch (TREE_CODE (type))
6738 case RECORD_TYPE:
6739 case UNION_TYPE:
6740 case QUAL_UNION_TYPE:
6742 unsigned HOST_WIDE_INT idx;
6743 tree field, value;
6745 /* The storage order is specified for every aggregate type. */
6746 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6748 /* If size is zero or the target is already cleared, do nothing. */
6749 if (known_eq (size, 0) || cleared)
6750 cleared = 1;
6751 /* We either clear the aggregate or indicate the value is dead. */
6752 else if ((TREE_CODE (type) == UNION_TYPE
6753 || TREE_CODE (type) == QUAL_UNION_TYPE)
6754 && ! CONSTRUCTOR_ELTS (exp))
6755 /* If the constructor is empty, clear the union. */
6757 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6758 cleared = 1;
6761 /* If we are building a static constructor into a register,
6762 set the initial value as zero so we can fold the value into
6763 a constant. But if more than one register is involved,
6764 this probably loses. */
6765 else if (REG_P (target) && TREE_STATIC (exp)
6766 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6767 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6769 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6770 cleared = 1;
6773 /* If the constructor has fewer fields than the structure or
6774 if we are initializing the structure to mostly zeros, clear
6775 the whole structure first. Don't do this if TARGET is a
6776 register whose mode size isn't equal to SIZE since
6777 clear_storage can't handle this case. */
6778 else if (known_size_p (size)
6779 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6780 || mostly_zeros_p (exp))
6781 && (!REG_P (target)
6782 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6784 clear_storage (target, gen_int_mode (size, Pmode),
6785 BLOCK_OP_NORMAL);
6786 cleared = 1;
6789 if (REG_P (target) && !cleared)
6790 emit_clobber (target);
6792 /* Store each element of the constructor into the
6793 corresponding field of TARGET. */
6794 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6796 machine_mode mode;
6797 HOST_WIDE_INT bitsize;
6798 HOST_WIDE_INT bitpos = 0;
6799 tree offset;
6800 rtx to_rtx = target;
6802 /* Just ignore missing fields. We cleared the whole
6803 structure, above, if any fields are missing. */
6804 if (field == 0)
6805 continue;
6807 if (cleared && initializer_zerop (value))
6808 continue;
6810 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6811 bitsize = tree_to_uhwi (DECL_SIZE (field));
6812 else
6813 gcc_unreachable ();
6815 mode = DECL_MODE (field);
6816 if (DECL_BIT_FIELD (field))
6817 mode = VOIDmode;
6819 offset = DECL_FIELD_OFFSET (field);
6820 if (tree_fits_shwi_p (offset)
6821 && tree_fits_shwi_p (bit_position (field)))
6823 bitpos = int_bit_position (field);
6824 offset = NULL_TREE;
6826 else
6827 gcc_unreachable ();
6829 /* If this initializes a field that is smaller than a
6830 word, at the start of a word, try to widen it to a full
6831 word. This special case allows us to output C++ member
6832 function initializations in a form that the optimizers
6833 can understand. */
6834 if (WORD_REGISTER_OPERATIONS
6835 && REG_P (target)
6836 && bitsize < BITS_PER_WORD
6837 && bitpos % BITS_PER_WORD == 0
6838 && GET_MODE_CLASS (mode) == MODE_INT
6839 && TREE_CODE (value) == INTEGER_CST
6840 && exp_size >= 0
6841 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6843 type = TREE_TYPE (value);
6845 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6847 type = lang_hooks.types.type_for_mode
6848 (word_mode, TYPE_UNSIGNED (type));
6849 value = fold_convert (type, value);
6850 /* Make sure the bits beyond the original bitsize are zero
6851 so that we can correctly avoid extra zeroing stores in
6852 later constructor elements. */
6853 tree bitsize_mask
6854 = wide_int_to_tree (type, wi::mask (bitsize, false,
6855 BITS_PER_WORD));
6856 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6859 if (BYTES_BIG_ENDIAN)
6860 value
6861 = fold_build2 (LSHIFT_EXPR, type, value,
6862 build_int_cst (type,
6863 BITS_PER_WORD - bitsize));
6864 bitsize = BITS_PER_WORD;
6865 mode = word_mode;
6868 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6869 && DECL_NONADDRESSABLE_P (field))
6871 to_rtx = copy_rtx (to_rtx);
6872 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6875 store_constructor_field (to_rtx, bitsize, bitpos,
6876 0, bitregion_end, mode,
6877 value, cleared,
6878 get_alias_set (TREE_TYPE (field)),
6879 reverse);
6881 break;
6883 case ARRAY_TYPE:
6885 tree value, index;
6886 unsigned HOST_WIDE_INT i;
6887 int need_to_clear;
6888 tree domain;
6889 tree elttype = TREE_TYPE (type);
6890 int const_bounds_p;
6891 HOST_WIDE_INT minelt = 0;
6892 HOST_WIDE_INT maxelt = 0;
6894 /* The storage order is specified for every aggregate type. */
6895 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6897 domain = TYPE_DOMAIN (type);
6898 const_bounds_p = (TYPE_MIN_VALUE (domain)
6899 && TYPE_MAX_VALUE (domain)
6900 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6901 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6903 /* If we have constant bounds for the range of the type, get them. */
6904 if (const_bounds_p)
6906 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6907 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6910 /* If the constructor has fewer elements than the array, clear
6911 the whole array first. Similarly if this is static
6912 constructor of a non-BLKmode object. */
6913 if (cleared)
6914 need_to_clear = 0;
6915 else if (REG_P (target) && TREE_STATIC (exp))
6916 need_to_clear = 1;
6917 else
6919 unsigned HOST_WIDE_INT idx;
6920 HOST_WIDE_INT count = 0, zero_count = 0;
6921 need_to_clear = ! const_bounds_p;
6923 /* This loop is a more accurate version of the loop in
6924 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6925 is also needed to check for missing elements. */
6926 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6928 HOST_WIDE_INT this_node_count;
6930 if (need_to_clear)
6931 break;
6933 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6935 tree lo_index = TREE_OPERAND (index, 0);
6936 tree hi_index = TREE_OPERAND (index, 1);
6938 if (! tree_fits_uhwi_p (lo_index)
6939 || ! tree_fits_uhwi_p (hi_index))
6941 need_to_clear = 1;
6942 break;
6945 this_node_count = (tree_to_uhwi (hi_index)
6946 - tree_to_uhwi (lo_index) + 1);
6948 else
6949 this_node_count = 1;
6951 count += this_node_count;
6952 if (mostly_zeros_p (value))
6953 zero_count += this_node_count;
6956 /* Clear the entire array first if there are any missing
6957 elements, or if the incidence of zero elements is >=
6958 75%. */
6959 if (! need_to_clear
6960 && (count < maxelt - minelt + 1
6961 || 4 * zero_count >= 3 * count))
6962 need_to_clear = 1;
6965 if (need_to_clear && maybe_gt (size, 0))
6967 if (REG_P (target))
6968 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6969 else
6970 clear_storage (target, gen_int_mode (size, Pmode),
6971 BLOCK_OP_NORMAL);
6972 cleared = 1;
6975 if (!cleared && REG_P (target))
6976 /* Inform later passes that the old value is dead. */
6977 emit_clobber (target);
6979 /* Store each element of the constructor into the
6980 corresponding element of TARGET, determined by counting the
6981 elements. */
6982 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6984 machine_mode mode;
6985 poly_int64 bitsize;
6986 HOST_WIDE_INT bitpos;
6987 rtx xtarget = target;
6989 if (cleared && initializer_zerop (value))
6990 continue;
6992 mode = TYPE_MODE (elttype);
6993 if (mode != BLKmode)
6994 bitsize = GET_MODE_BITSIZE (mode);
6995 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
6996 bitsize = -1;
6998 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7000 tree lo_index = TREE_OPERAND (index, 0);
7001 tree hi_index = TREE_OPERAND (index, 1);
7002 rtx index_r, pos_rtx;
7003 HOST_WIDE_INT lo, hi, count;
7004 tree position;
7006 /* If the range is constant and "small", unroll the loop. */
7007 if (const_bounds_p
7008 && tree_fits_shwi_p (lo_index)
7009 && tree_fits_shwi_p (hi_index)
7010 && (lo = tree_to_shwi (lo_index),
7011 hi = tree_to_shwi (hi_index),
7012 count = hi - lo + 1,
7013 (!MEM_P (target)
7014 || count <= 2
7015 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7016 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7017 <= 40 * 8)))))
7019 lo -= minelt; hi -= minelt;
7020 for (; lo <= hi; lo++)
7022 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7024 if (MEM_P (target)
7025 && !MEM_KEEP_ALIAS_SET_P (target)
7026 && TREE_CODE (type) == ARRAY_TYPE
7027 && TYPE_NONALIASED_COMPONENT (type))
7029 target = copy_rtx (target);
7030 MEM_KEEP_ALIAS_SET_P (target) = 1;
7033 store_constructor_field
7034 (target, bitsize, bitpos, 0, bitregion_end,
7035 mode, value, cleared,
7036 get_alias_set (elttype), reverse);
7039 else
7041 rtx_code_label *loop_start = gen_label_rtx ();
7042 rtx_code_label *loop_end = gen_label_rtx ();
7043 tree exit_cond;
7045 expand_normal (hi_index);
7047 index = build_decl (EXPR_LOCATION (exp),
7048 VAR_DECL, NULL_TREE, domain);
7049 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7050 SET_DECL_RTL (index, index_r);
7051 store_expr (lo_index, index_r, 0, false, reverse);
7053 /* Build the head of the loop. */
7054 do_pending_stack_adjust ();
7055 emit_label (loop_start);
7057 /* Assign value to element index. */
7058 position =
7059 fold_convert (ssizetype,
7060 fold_build2 (MINUS_EXPR,
7061 TREE_TYPE (index),
7062 index,
7063 TYPE_MIN_VALUE (domain)));
7065 position =
7066 size_binop (MULT_EXPR, position,
7067 fold_convert (ssizetype,
7068 TYPE_SIZE_UNIT (elttype)));
7070 pos_rtx = expand_normal (position);
7071 xtarget = offset_address (target, pos_rtx,
7072 highest_pow2_factor (position));
7073 xtarget = adjust_address (xtarget, mode, 0);
7074 if (TREE_CODE (value) == CONSTRUCTOR)
7075 store_constructor (value, xtarget, cleared,
7076 exact_div (bitsize, BITS_PER_UNIT),
7077 reverse);
7078 else
7079 store_expr (value, xtarget, 0, false, reverse);
7081 /* Generate a conditional jump to exit the loop. */
7082 exit_cond = build2 (LT_EXPR, integer_type_node,
7083 index, hi_index);
7084 jumpif (exit_cond, loop_end,
7085 profile_probability::uninitialized ());
7087 /* Update the loop counter, and jump to the head of
7088 the loop. */
7089 expand_assignment (index,
7090 build2 (PLUS_EXPR, TREE_TYPE (index),
7091 index, integer_one_node),
7092 false);
7094 emit_jump (loop_start);
7096 /* Build the end of the loop. */
7097 emit_label (loop_end);
7100 else if ((index != 0 && ! tree_fits_shwi_p (index))
7101 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7103 tree position;
7105 if (index == 0)
7106 index = ssize_int (1);
7108 if (minelt)
7109 index = fold_convert (ssizetype,
7110 fold_build2 (MINUS_EXPR,
7111 TREE_TYPE (index),
7112 index,
7113 TYPE_MIN_VALUE (domain)));
7115 position =
7116 size_binop (MULT_EXPR, index,
7117 fold_convert (ssizetype,
7118 TYPE_SIZE_UNIT (elttype)));
7119 xtarget = offset_address (target,
7120 expand_normal (position),
7121 highest_pow2_factor (position));
7122 xtarget = adjust_address (xtarget, mode, 0);
7123 store_expr (value, xtarget, 0, false, reverse);
7125 else
7127 if (index != 0)
7128 bitpos = ((tree_to_shwi (index) - minelt)
7129 * tree_to_uhwi (TYPE_SIZE (elttype)));
7130 else
7131 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7133 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7134 && TREE_CODE (type) == ARRAY_TYPE
7135 && TYPE_NONALIASED_COMPONENT (type))
7137 target = copy_rtx (target);
7138 MEM_KEEP_ALIAS_SET_P (target) = 1;
7140 store_constructor_field (target, bitsize, bitpos, 0,
7141 bitregion_end, mode, value,
7142 cleared, get_alias_set (elttype),
7143 reverse);
7146 break;
7149 case VECTOR_TYPE:
7151 unsigned HOST_WIDE_INT idx;
7152 constructor_elt *ce;
7153 int i;
7154 int need_to_clear;
7155 insn_code icode = CODE_FOR_nothing;
7156 tree elt;
7157 tree elttype = TREE_TYPE (type);
7158 int elt_size = vector_element_bits (type);
7159 machine_mode eltmode = TYPE_MODE (elttype);
7160 HOST_WIDE_INT bitsize;
7161 HOST_WIDE_INT bitpos;
7162 rtvec vector = NULL;
7163 poly_uint64 n_elts;
7164 unsigned HOST_WIDE_INT const_n_elts;
7165 alias_set_type alias;
7166 bool vec_vec_init_p = false;
7167 machine_mode mode = GET_MODE (target);
7169 gcc_assert (eltmode != BLKmode);
7171 /* Try using vec_duplicate_optab for uniform vectors. */
7172 if (!TREE_SIDE_EFFECTS (exp)
7173 && VECTOR_MODE_P (mode)
7174 && eltmode == GET_MODE_INNER (mode)
7175 && ((icode = optab_handler (vec_duplicate_optab, mode))
7176 != CODE_FOR_nothing)
7177 && (elt = uniform_vector_p (exp))
7178 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7180 class expand_operand ops[2];
7181 create_output_operand (&ops[0], target, mode);
7182 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7183 expand_insn (icode, 2, ops);
7184 if (!rtx_equal_p (target, ops[0].value))
7185 emit_move_insn (target, ops[0].value);
7186 break;
7189 n_elts = TYPE_VECTOR_SUBPARTS (type);
7190 if (REG_P (target)
7191 && VECTOR_MODE_P (mode)
7192 && n_elts.is_constant (&const_n_elts))
7194 machine_mode emode = eltmode;
7195 bool vector_typed_elts_p = false;
7197 if (CONSTRUCTOR_NELTS (exp)
7198 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7199 == VECTOR_TYPE))
7201 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7202 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7203 * TYPE_VECTOR_SUBPARTS (etype),
7204 n_elts));
7205 emode = TYPE_MODE (etype);
7206 vector_typed_elts_p = true;
7208 icode = convert_optab_handler (vec_init_optab, mode, emode);
7209 if (icode != CODE_FOR_nothing)
7211 unsigned int n = const_n_elts;
7213 if (vector_typed_elts_p)
7215 n = CONSTRUCTOR_NELTS (exp);
7216 vec_vec_init_p = true;
7218 vector = rtvec_alloc (n);
7219 for (unsigned int k = 0; k < n; k++)
7220 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7224 /* Compute the size of the elements in the CTOR. It differs
7225 from the size of the vector type elements only when the
7226 CTOR elements are vectors themselves. */
7227 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7228 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7229 : elttype);
7230 if (VECTOR_TYPE_P (val_type))
7231 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7232 else
7233 bitsize = elt_size;
7235 /* If the constructor has fewer elements than the vector,
7236 clear the whole array first. Similarly if this is static
7237 constructor of a non-BLKmode object. */
7238 if (cleared)
7239 need_to_clear = 0;
7240 else if (REG_P (target) && TREE_STATIC (exp))
7241 need_to_clear = 1;
7242 else
7244 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7245 tree value;
7247 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7249 int n_elts_here = bitsize / elt_size;
7250 count += n_elts_here;
7251 if (mostly_zeros_p (value))
7252 zero_count += n_elts_here;
7255 /* Clear the entire vector first if there are any missing elements,
7256 or if the incidence of zero elements is >= 75%. */
7257 need_to_clear = (maybe_lt (count, n_elts)
7258 || 4 * zero_count >= 3 * count);
7261 if (need_to_clear && maybe_gt (size, 0) && !vector)
7263 if (REG_P (target))
7264 emit_move_insn (target, CONST0_RTX (mode));
7265 else
7266 clear_storage (target, gen_int_mode (size, Pmode),
7267 BLOCK_OP_NORMAL);
7268 cleared = 1;
7271 /* Inform later passes that the old value is dead. */
7272 if (!cleared && !vector && REG_P (target))
7273 emit_move_insn (target, CONST0_RTX (mode));
7275 if (MEM_P (target))
7276 alias = MEM_ALIAS_SET (target);
7277 else
7278 alias = get_alias_set (elttype);
7280 /* Store each element of the constructor into the corresponding
7281 element of TARGET, determined by counting the elements. */
7282 for (idx = 0, i = 0;
7283 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7284 idx++, i += bitsize / elt_size)
7286 HOST_WIDE_INT eltpos;
7287 tree value = ce->value;
7289 if (cleared && initializer_zerop (value))
7290 continue;
7292 if (ce->index)
7293 eltpos = tree_to_uhwi (ce->index);
7294 else
7295 eltpos = i;
7297 if (vector)
7299 if (vec_vec_init_p)
7301 gcc_assert (ce->index == NULL_TREE);
7302 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7303 eltpos = idx;
7305 else
7306 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7307 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7309 else
7311 machine_mode value_mode
7312 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7313 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7314 bitpos = eltpos * elt_size;
7315 store_constructor_field (target, bitsize, bitpos, 0,
7316 bitregion_end, value_mode,
7317 value, cleared, alias, reverse);
7321 if (vector)
7322 emit_insn (GEN_FCN (icode) (target,
7323 gen_rtx_PARALLEL (mode, vector)));
7324 break;
7327 default:
7328 gcc_unreachable ();
7332 /* Store the value of EXP (an expression tree)
7333 into a subfield of TARGET which has mode MODE and occupies
7334 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7335 If MODE is VOIDmode, it means that we are storing into a bit-field.
7337 BITREGION_START is bitpos of the first bitfield in this region.
7338 BITREGION_END is the bitpos of the ending bitfield in this region.
7339 These two fields are 0, if the C++ memory model does not apply,
7340 or we are not interested in keeping track of bitfield regions.
7342 Always return const0_rtx unless we have something particular to
7343 return.
7345 ALIAS_SET is the alias set for the destination. This value will
7346 (in general) be different from that for TARGET, since TARGET is a
7347 reference to the containing structure.
7349 If NONTEMPORAL is true, try generating a nontemporal store.
7351 If REVERSE is true, the store is to be done in reverse order. */
7353 static rtx
7354 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7355 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7356 machine_mode mode, tree exp,
7357 alias_set_type alias_set, bool nontemporal, bool reverse)
7359 if (TREE_CODE (exp) == ERROR_MARK)
7360 return const0_rtx;
7362 /* If we have nothing to store, do nothing unless the expression has
7363 side-effects. Don't do that for zero sized addressable lhs of
7364 calls. */
7365 if (known_eq (bitsize, 0)
7366 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7367 || TREE_CODE (exp) != CALL_EXPR))
7368 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7370 if (GET_CODE (target) == CONCAT)
7372 /* We're storing into a struct containing a single __complex. */
7374 gcc_assert (known_eq (bitpos, 0));
7375 return store_expr (exp, target, 0, nontemporal, reverse);
7378 /* If the structure is in a register or if the component
7379 is a bit field, we cannot use addressing to access it.
7380 Use bit-field techniques or SUBREG to store in it. */
7382 poly_int64 decl_bitsize;
7383 if (mode == VOIDmode
7384 || (mode != BLKmode && ! direct_store[(int) mode]
7385 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7386 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7387 || REG_P (target)
7388 || GET_CODE (target) == SUBREG
7389 /* If the field isn't aligned enough to store as an ordinary memref,
7390 store it as a bit field. */
7391 || (mode != BLKmode
7392 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7393 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7394 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7395 || !multiple_p (bitpos, BITS_PER_UNIT)))
7396 || (known_size_p (bitsize)
7397 && mode != BLKmode
7398 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7399 /* If the RHS and field are a constant size and the size of the
7400 RHS isn't the same size as the bitfield, we must use bitfield
7401 operations. */
7402 || (known_size_p (bitsize)
7403 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7404 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7405 bitsize)
7406 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7407 we will handle specially below. */
7408 && !(TREE_CODE (exp) == CONSTRUCTOR
7409 && multiple_p (bitsize, BITS_PER_UNIT))
7410 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7411 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7412 includes some extra padding. store_expr / expand_expr will in
7413 that case call get_inner_reference that will have the bitsize
7414 we check here and thus the block move will not clobber the
7415 padding that shouldn't be clobbered. In the future we could
7416 replace the TREE_ADDRESSABLE check with a check that
7417 get_base_address needs to live in memory. */
7418 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7419 || TREE_CODE (exp) != COMPONENT_REF
7420 || !multiple_p (bitsize, BITS_PER_UNIT)
7421 || !multiple_p (bitpos, BITS_PER_UNIT)
7422 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7423 &decl_bitsize)
7424 || maybe_ne (decl_bitsize, bitsize))
7425 /* A call with an addressable return type and return-slot
7426 optimization must not need bitfield operations but we must
7427 pass down the original target. */
7428 && (TREE_CODE (exp) != CALL_EXPR
7429 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7430 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7431 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7432 decl we must use bitfield operations. */
7433 || (known_size_p (bitsize)
7434 && TREE_CODE (exp) == MEM_REF
7435 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7436 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7437 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7438 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7440 rtx temp;
7441 gimple *nop_def;
7443 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7444 implies a mask operation. If the precision is the same size as
7445 the field we're storing into, that mask is redundant. This is
7446 particularly common with bit field assignments generated by the
7447 C front end. */
7448 nop_def = get_def_for_expr (exp, NOP_EXPR);
7449 if (nop_def)
7451 tree type = TREE_TYPE (exp);
7452 if (INTEGRAL_TYPE_P (type)
7453 && maybe_ne (TYPE_PRECISION (type),
7454 GET_MODE_BITSIZE (TYPE_MODE (type)))
7455 && known_eq (bitsize, TYPE_PRECISION (type)))
7457 tree op = gimple_assign_rhs1 (nop_def);
7458 type = TREE_TYPE (op);
7459 if (INTEGRAL_TYPE_P (type)
7460 && known_ge (TYPE_PRECISION (type), bitsize))
7461 exp = op;
7465 temp = expand_normal (exp);
7467 /* We don't support variable-sized BLKmode bitfields, since our
7468 handling of BLKmode is bound up with the ability to break
7469 things into words. */
7470 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7472 /* Handle calls that return values in multiple non-contiguous locations.
7473 The Irix 6 ABI has examples of this. */
7474 if (GET_CODE (temp) == PARALLEL)
7476 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7477 machine_mode temp_mode = GET_MODE (temp);
7478 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7479 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7480 rtx temp_target = gen_reg_rtx (temp_mode);
7481 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7482 temp = temp_target;
7485 /* Handle calls that return BLKmode values in registers. */
7486 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7488 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7489 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7490 temp = temp_target;
7493 /* If the value has aggregate type and an integral mode then, if BITSIZE
7494 is narrower than this mode and this is for big-endian data, we first
7495 need to put the value into the low-order bits for store_bit_field,
7496 except when MODE is BLKmode and BITSIZE larger than the word size
7497 (see the handling of fields larger than a word in store_bit_field).
7498 Moreover, the field may be not aligned on a byte boundary; in this
7499 case, if it has reverse storage order, it needs to be accessed as a
7500 scalar field with reverse storage order and we must first put the
7501 value into target order. */
7502 scalar_int_mode temp_mode;
7503 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7504 && is_int_mode (GET_MODE (temp), &temp_mode))
7506 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7508 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7510 if (reverse)
7511 temp = flip_storage_order (temp_mode, temp);
7513 gcc_checking_assert (known_le (bitsize, size));
7514 if (maybe_lt (bitsize, size)
7515 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7516 /* Use of to_constant for BLKmode was checked above. */
7517 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7518 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7519 size - bitsize, NULL_RTX, 1);
7522 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7523 if (mode != VOIDmode && mode != BLKmode
7524 && mode != TYPE_MODE (TREE_TYPE (exp)))
7525 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7527 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7528 and BITPOS must be aligned on a byte boundary. If so, we simply do
7529 a block copy. Likewise for a BLKmode-like TARGET. */
7530 if (GET_MODE (temp) == BLKmode
7531 && (GET_MODE (target) == BLKmode
7532 || (MEM_P (target)
7533 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7534 && multiple_p (bitpos, BITS_PER_UNIT)
7535 && multiple_p (bitsize, BITS_PER_UNIT))))
7537 gcc_assert (MEM_P (target) && MEM_P (temp));
7538 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7539 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7541 target = adjust_address (target, VOIDmode, bytepos);
7542 emit_block_move (target, temp,
7543 gen_int_mode (bytesize, Pmode),
7544 BLOCK_OP_NORMAL);
7546 return const0_rtx;
7549 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7550 word size, we need to load the value (see again store_bit_field). */
7551 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7553 temp_mode = smallest_int_mode_for_size (bitsize);
7554 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7555 temp_mode, false, NULL);
7558 /* Store the value in the bitfield. */
7559 gcc_checking_assert (known_ge (bitpos, 0));
7560 store_bit_field (target, bitsize, bitpos,
7561 bitregion_start, bitregion_end,
7562 mode, temp, reverse);
7564 return const0_rtx;
7566 else
7568 /* Now build a reference to just the desired component. */
7569 rtx to_rtx = adjust_address (target, mode,
7570 exact_div (bitpos, BITS_PER_UNIT));
7572 if (to_rtx == target)
7573 to_rtx = copy_rtx (to_rtx);
7575 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7576 set_mem_alias_set (to_rtx, alias_set);
7578 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7579 into a target smaller than its type; handle that case now. */
7580 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7582 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7583 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7584 return to_rtx;
7587 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7591 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7592 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7593 codes and find the ultimate containing object, which we return.
7595 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7596 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7597 storage order of the field.
7598 If the position of the field is variable, we store a tree
7599 giving the variable offset (in units) in *POFFSET.
7600 This offset is in addition to the bit position.
7601 If the position is not variable, we store 0 in *POFFSET.
7603 If any of the extraction expressions is volatile,
7604 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7606 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7607 Otherwise, it is a mode that can be used to access the field.
7609 If the field describes a variable-sized object, *PMODE is set to
7610 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7611 this case, but the address of the object can be found. */
7613 tree
7614 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7615 poly_int64_pod *pbitpos, tree *poffset,
7616 machine_mode *pmode, int *punsignedp,
7617 int *preversep, int *pvolatilep)
7619 tree size_tree = 0;
7620 machine_mode mode = VOIDmode;
7621 bool blkmode_bitfield = false;
7622 tree offset = size_zero_node;
7623 poly_offset_int bit_offset = 0;
7625 /* First get the mode, signedness, storage order and size. We do this from
7626 just the outermost expression. */
7627 *pbitsize = -1;
7628 if (TREE_CODE (exp) == COMPONENT_REF)
7630 tree field = TREE_OPERAND (exp, 1);
7631 size_tree = DECL_SIZE (field);
7632 if (flag_strict_volatile_bitfields > 0
7633 && TREE_THIS_VOLATILE (exp)
7634 && DECL_BIT_FIELD_TYPE (field)
7635 && DECL_MODE (field) != BLKmode)
7636 /* Volatile bitfields should be accessed in the mode of the
7637 field's type, not the mode computed based on the bit
7638 size. */
7639 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7640 else if (!DECL_BIT_FIELD (field))
7642 mode = DECL_MODE (field);
7643 /* For vector fields re-check the target flags, as DECL_MODE
7644 could have been set with different target flags than
7645 the current function has. */
7646 if (mode == BLKmode
7647 && VECTOR_TYPE_P (TREE_TYPE (field))
7648 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7649 mode = TYPE_MODE (TREE_TYPE (field));
7651 else if (DECL_MODE (field) == BLKmode)
7652 blkmode_bitfield = true;
7654 *punsignedp = DECL_UNSIGNED (field);
7656 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7658 size_tree = TREE_OPERAND (exp, 1);
7659 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7660 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7662 /* For vector element types with the correct size of access or for
7663 vector typed accesses use the mode of the access type. */
7664 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7665 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7666 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7667 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7668 mode = TYPE_MODE (TREE_TYPE (exp));
7670 else
7672 mode = TYPE_MODE (TREE_TYPE (exp));
7673 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7675 if (mode == BLKmode)
7676 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7677 else
7678 *pbitsize = GET_MODE_BITSIZE (mode);
7681 if (size_tree != 0)
7683 if (! tree_fits_uhwi_p (size_tree))
7684 mode = BLKmode, *pbitsize = -1;
7685 else
7686 *pbitsize = tree_to_uhwi (size_tree);
7689 *preversep = reverse_storage_order_for_component_p (exp);
7691 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7692 and find the ultimate containing object. */
7693 while (1)
7695 switch (TREE_CODE (exp))
7697 case BIT_FIELD_REF:
7698 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7699 break;
7701 case COMPONENT_REF:
7703 tree field = TREE_OPERAND (exp, 1);
7704 tree this_offset = component_ref_field_offset (exp);
7706 /* If this field hasn't been filled in yet, don't go past it.
7707 This should only happen when folding expressions made during
7708 type construction. */
7709 if (this_offset == 0)
7710 break;
7712 offset = size_binop (PLUS_EXPR, offset, this_offset);
7713 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7715 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7717 break;
7719 case ARRAY_REF:
7720 case ARRAY_RANGE_REF:
7722 tree index = TREE_OPERAND (exp, 1);
7723 tree low_bound = array_ref_low_bound (exp);
7724 tree unit_size = array_ref_element_size (exp);
7726 /* We assume all arrays have sizes that are a multiple of a byte.
7727 First subtract the lower bound, if any, in the type of the
7728 index, then convert to sizetype and multiply by the size of
7729 the array element. */
7730 if (! integer_zerop (low_bound))
7731 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7732 index, low_bound);
7734 offset = size_binop (PLUS_EXPR, offset,
7735 size_binop (MULT_EXPR,
7736 fold_convert (sizetype, index),
7737 unit_size));
7739 break;
7741 case REALPART_EXPR:
7742 break;
7744 case IMAGPART_EXPR:
7745 bit_offset += *pbitsize;
7746 break;
7748 case VIEW_CONVERT_EXPR:
7749 break;
7751 case MEM_REF:
7752 /* Hand back the decl for MEM[&decl, off]. */
7753 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7755 tree off = TREE_OPERAND (exp, 1);
7756 if (!integer_zerop (off))
7758 poly_offset_int boff = mem_ref_offset (exp);
7759 boff <<= LOG2_BITS_PER_UNIT;
7760 bit_offset += boff;
7762 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7764 goto done;
7766 default:
7767 goto done;
7770 /* If any reference in the chain is volatile, the effect is volatile. */
7771 if (TREE_THIS_VOLATILE (exp))
7772 *pvolatilep = 1;
7774 exp = TREE_OPERAND (exp, 0);
7776 done:
7778 /* If OFFSET is constant, see if we can return the whole thing as a
7779 constant bit position. Make sure to handle overflow during
7780 this conversion. */
7781 if (poly_int_tree_p (offset))
7783 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7784 TYPE_PRECISION (sizetype));
7785 tem <<= LOG2_BITS_PER_UNIT;
7786 tem += bit_offset;
7787 if (tem.to_shwi (pbitpos))
7788 *poffset = offset = NULL_TREE;
7791 /* Otherwise, split it up. */
7792 if (offset)
7794 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7795 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7797 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7798 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7799 offset = size_binop (PLUS_EXPR, offset,
7800 build_int_cst (sizetype, bytes.force_shwi ()));
7803 *poffset = offset;
7806 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7807 if (mode == VOIDmode
7808 && blkmode_bitfield
7809 && multiple_p (*pbitpos, BITS_PER_UNIT)
7810 && multiple_p (*pbitsize, BITS_PER_UNIT))
7811 *pmode = BLKmode;
7812 else
7813 *pmode = mode;
7815 return exp;
7818 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7820 static unsigned HOST_WIDE_INT
7821 target_align (const_tree target)
7823 /* We might have a chain of nested references with intermediate misaligning
7824 bitfields components, so need to recurse to find out. */
7826 unsigned HOST_WIDE_INT this_align, outer_align;
7828 switch (TREE_CODE (target))
7830 case BIT_FIELD_REF:
7831 return 1;
7833 case COMPONENT_REF:
7834 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7835 outer_align = target_align (TREE_OPERAND (target, 0));
7836 return MIN (this_align, outer_align);
7838 case ARRAY_REF:
7839 case ARRAY_RANGE_REF:
7840 this_align = TYPE_ALIGN (TREE_TYPE (target));
7841 outer_align = target_align (TREE_OPERAND (target, 0));
7842 return MIN (this_align, outer_align);
7844 CASE_CONVERT:
7845 case NON_LVALUE_EXPR:
7846 case VIEW_CONVERT_EXPR:
7847 this_align = TYPE_ALIGN (TREE_TYPE (target));
7848 outer_align = target_align (TREE_OPERAND (target, 0));
7849 return MAX (this_align, outer_align);
7851 default:
7852 return TYPE_ALIGN (TREE_TYPE (target));
7857 /* Given an rtx VALUE that may contain additions and multiplications, return
7858 an equivalent value that just refers to a register, memory, or constant.
7859 This is done by generating instructions to perform the arithmetic and
7860 returning a pseudo-register containing the value.
7862 The returned value may be a REG, SUBREG, MEM or constant. */
7865 force_operand (rtx value, rtx target)
7867 rtx op1, op2;
7868 /* Use subtarget as the target for operand 0 of a binary operation. */
7869 rtx subtarget = get_subtarget (target);
7870 enum rtx_code code = GET_CODE (value);
7872 /* Check for subreg applied to an expression produced by loop optimizer. */
7873 if (code == SUBREG
7874 && !REG_P (SUBREG_REG (value))
7875 && !MEM_P (SUBREG_REG (value)))
7877 value
7878 = simplify_gen_subreg (GET_MODE (value),
7879 force_reg (GET_MODE (SUBREG_REG (value)),
7880 force_operand (SUBREG_REG (value),
7881 NULL_RTX)),
7882 GET_MODE (SUBREG_REG (value)),
7883 SUBREG_BYTE (value));
7884 code = GET_CODE (value);
7887 /* Check for a PIC address load. */
7888 if ((code == PLUS || code == MINUS)
7889 && XEXP (value, 0) == pic_offset_table_rtx
7890 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7891 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7892 || GET_CODE (XEXP (value, 1)) == CONST))
7894 if (!subtarget)
7895 subtarget = gen_reg_rtx (GET_MODE (value));
7896 emit_move_insn (subtarget, value);
7897 return subtarget;
7900 if (ARITHMETIC_P (value))
7902 op2 = XEXP (value, 1);
7903 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7904 subtarget = 0;
7905 if (code == MINUS && CONST_INT_P (op2))
7907 code = PLUS;
7908 op2 = negate_rtx (GET_MODE (value), op2);
7911 /* Check for an addition with OP2 a constant integer and our first
7912 operand a PLUS of a virtual register and something else. In that
7913 case, we want to emit the sum of the virtual register and the
7914 constant first and then add the other value. This allows virtual
7915 register instantiation to simply modify the constant rather than
7916 creating another one around this addition. */
7917 if (code == PLUS && CONST_INT_P (op2)
7918 && GET_CODE (XEXP (value, 0)) == PLUS
7919 && REG_P (XEXP (XEXP (value, 0), 0))
7920 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7921 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7923 rtx temp = expand_simple_binop (GET_MODE (value), code,
7924 XEXP (XEXP (value, 0), 0), op2,
7925 subtarget, 0, OPTAB_LIB_WIDEN);
7926 return expand_simple_binop (GET_MODE (value), code, temp,
7927 force_operand (XEXP (XEXP (value,
7928 0), 1), 0),
7929 target, 0, OPTAB_LIB_WIDEN);
7932 op1 = force_operand (XEXP (value, 0), subtarget);
7933 op2 = force_operand (op2, NULL_RTX);
7934 switch (code)
7936 case MULT:
7937 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7938 case DIV:
7939 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7940 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7941 target, 1, OPTAB_LIB_WIDEN);
7942 else
7943 return expand_divmod (0,
7944 FLOAT_MODE_P (GET_MODE (value))
7945 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7946 GET_MODE (value), op1, op2, target, 0);
7947 case MOD:
7948 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7949 target, 0);
7950 case UDIV:
7951 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7952 target, 1);
7953 case UMOD:
7954 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7955 target, 1);
7956 case ASHIFTRT:
7957 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7958 target, 0, OPTAB_LIB_WIDEN);
7959 default:
7960 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7961 target, 1, OPTAB_LIB_WIDEN);
7964 if (UNARY_P (value))
7966 if (!target)
7967 target = gen_reg_rtx (GET_MODE (value));
7968 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7969 switch (code)
7971 case ZERO_EXTEND:
7972 case SIGN_EXTEND:
7973 case TRUNCATE:
7974 case FLOAT_EXTEND:
7975 case FLOAT_TRUNCATE:
7976 convert_move (target, op1, code == ZERO_EXTEND);
7977 return target;
7979 case FIX:
7980 case UNSIGNED_FIX:
7981 expand_fix (target, op1, code == UNSIGNED_FIX);
7982 return target;
7984 case FLOAT:
7985 case UNSIGNED_FLOAT:
7986 expand_float (target, op1, code == UNSIGNED_FLOAT);
7987 return target;
7989 default:
7990 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7994 #ifdef INSN_SCHEDULING
7995 /* On machines that have insn scheduling, we want all memory reference to be
7996 explicit, so we need to deal with such paradoxical SUBREGs. */
7997 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7998 value
7999 = simplify_gen_subreg (GET_MODE (value),
8000 force_reg (GET_MODE (SUBREG_REG (value)),
8001 force_operand (SUBREG_REG (value),
8002 NULL_RTX)),
8003 GET_MODE (SUBREG_REG (value)),
8004 SUBREG_BYTE (value));
8005 #endif
8007 return value;
8010 /* Subroutine of expand_expr: return nonzero iff there is no way that
8011 EXP can reference X, which is being modified. TOP_P is nonzero if this
8012 call is going to be used to determine whether we need a temporary
8013 for EXP, as opposed to a recursive call to this function.
8015 It is always safe for this routine to return zero since it merely
8016 searches for optimization opportunities. */
8019 safe_from_p (const_rtx x, tree exp, int top_p)
8021 rtx exp_rtl = 0;
8022 int i, nops;
8024 if (x == 0
8025 /* If EXP has varying size, we MUST use a target since we currently
8026 have no way of allocating temporaries of variable size
8027 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8028 So we assume here that something at a higher level has prevented a
8029 clash. This is somewhat bogus, but the best we can do. Only
8030 do this when X is BLKmode and when we are at the top level. */
8031 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8032 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8033 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8034 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8035 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8036 != INTEGER_CST)
8037 && GET_MODE (x) == BLKmode)
8038 /* If X is in the outgoing argument area, it is always safe. */
8039 || (MEM_P (x)
8040 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8041 || (GET_CODE (XEXP (x, 0)) == PLUS
8042 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8043 return 1;
8045 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8046 find the underlying pseudo. */
8047 if (GET_CODE (x) == SUBREG)
8049 x = SUBREG_REG (x);
8050 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8051 return 0;
8054 /* Now look at our tree code and possibly recurse. */
8055 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8057 case tcc_declaration:
8058 exp_rtl = DECL_RTL_IF_SET (exp);
8059 break;
8061 case tcc_constant:
8062 return 1;
8064 case tcc_exceptional:
8065 if (TREE_CODE (exp) == TREE_LIST)
8067 while (1)
8069 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8070 return 0;
8071 exp = TREE_CHAIN (exp);
8072 if (!exp)
8073 return 1;
8074 if (TREE_CODE (exp) != TREE_LIST)
8075 return safe_from_p (x, exp, 0);
8078 else if (TREE_CODE (exp) == CONSTRUCTOR)
8080 constructor_elt *ce;
8081 unsigned HOST_WIDE_INT idx;
8083 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8084 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8085 || !safe_from_p (x, ce->value, 0))
8086 return 0;
8087 return 1;
8089 else if (TREE_CODE (exp) == ERROR_MARK)
8090 return 1; /* An already-visited SAVE_EXPR? */
8091 else
8092 return 0;
8094 case tcc_statement:
8095 /* The only case we look at here is the DECL_INITIAL inside a
8096 DECL_EXPR. */
8097 return (TREE_CODE (exp) != DECL_EXPR
8098 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8099 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8100 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8102 case tcc_binary:
8103 case tcc_comparison:
8104 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8105 return 0;
8106 /* Fall through. */
8108 case tcc_unary:
8109 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8111 case tcc_expression:
8112 case tcc_reference:
8113 case tcc_vl_exp:
8114 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8115 the expression. If it is set, we conflict iff we are that rtx or
8116 both are in memory. Otherwise, we check all operands of the
8117 expression recursively. */
8119 switch (TREE_CODE (exp))
8121 case ADDR_EXPR:
8122 /* If the operand is static or we are static, we can't conflict.
8123 Likewise if we don't conflict with the operand at all. */
8124 if (staticp (TREE_OPERAND (exp, 0))
8125 || TREE_STATIC (exp)
8126 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8127 return 1;
8129 /* Otherwise, the only way this can conflict is if we are taking
8130 the address of a DECL a that address if part of X, which is
8131 very rare. */
8132 exp = TREE_OPERAND (exp, 0);
8133 if (DECL_P (exp))
8135 if (!DECL_RTL_SET_P (exp)
8136 || !MEM_P (DECL_RTL (exp)))
8137 return 0;
8138 else
8139 exp_rtl = XEXP (DECL_RTL (exp), 0);
8141 break;
8143 case MEM_REF:
8144 if (MEM_P (x)
8145 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8146 get_alias_set (exp)))
8147 return 0;
8148 break;
8150 case CALL_EXPR:
8151 /* Assume that the call will clobber all hard registers and
8152 all of memory. */
8153 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8154 || MEM_P (x))
8155 return 0;
8156 break;
8158 case WITH_CLEANUP_EXPR:
8159 case CLEANUP_POINT_EXPR:
8160 /* Lowered by gimplify.c. */
8161 gcc_unreachable ();
8163 case SAVE_EXPR:
8164 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8166 default:
8167 break;
8170 /* If we have an rtx, we do not need to scan our operands. */
8171 if (exp_rtl)
8172 break;
8174 nops = TREE_OPERAND_LENGTH (exp);
8175 for (i = 0; i < nops; i++)
8176 if (TREE_OPERAND (exp, i) != 0
8177 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8178 return 0;
8180 break;
8182 case tcc_type:
8183 /* Should never get a type here. */
8184 gcc_unreachable ();
8187 /* If we have an rtl, find any enclosed object. Then see if we conflict
8188 with it. */
8189 if (exp_rtl)
8191 if (GET_CODE (exp_rtl) == SUBREG)
8193 exp_rtl = SUBREG_REG (exp_rtl);
8194 if (REG_P (exp_rtl)
8195 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8196 return 0;
8199 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8200 are memory and they conflict. */
8201 return ! (rtx_equal_p (x, exp_rtl)
8202 || (MEM_P (x) && MEM_P (exp_rtl)
8203 && true_dependence (exp_rtl, VOIDmode, x)));
8206 /* If we reach here, it is safe. */
8207 return 1;
8211 /* Return the highest power of two that EXP is known to be a multiple of.
8212 This is used in updating alignment of MEMs in array references. */
8214 unsigned HOST_WIDE_INT
8215 highest_pow2_factor (const_tree exp)
8217 unsigned HOST_WIDE_INT ret;
8218 int trailing_zeros = tree_ctz (exp);
8219 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8220 return BIGGEST_ALIGNMENT;
8221 ret = HOST_WIDE_INT_1U << trailing_zeros;
8222 if (ret > BIGGEST_ALIGNMENT)
8223 return BIGGEST_ALIGNMENT;
8224 return ret;
8227 /* Similar, except that the alignment requirements of TARGET are
8228 taken into account. Assume it is at least as aligned as its
8229 type, unless it is a COMPONENT_REF in which case the layout of
8230 the structure gives the alignment. */
8232 static unsigned HOST_WIDE_INT
8233 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8235 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8236 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8238 return MAX (factor, talign);
8241 /* Convert the tree comparison code TCODE to the rtl one where the
8242 signedness is UNSIGNEDP. */
8244 static enum rtx_code
8245 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8247 enum rtx_code code;
8248 switch (tcode)
8250 case EQ_EXPR:
8251 code = EQ;
8252 break;
8253 case NE_EXPR:
8254 code = NE;
8255 break;
8256 case LT_EXPR:
8257 code = unsignedp ? LTU : LT;
8258 break;
8259 case LE_EXPR:
8260 code = unsignedp ? LEU : LE;
8261 break;
8262 case GT_EXPR:
8263 code = unsignedp ? GTU : GT;
8264 break;
8265 case GE_EXPR:
8266 code = unsignedp ? GEU : GE;
8267 break;
8268 case UNORDERED_EXPR:
8269 code = UNORDERED;
8270 break;
8271 case ORDERED_EXPR:
8272 code = ORDERED;
8273 break;
8274 case UNLT_EXPR:
8275 code = UNLT;
8276 break;
8277 case UNLE_EXPR:
8278 code = UNLE;
8279 break;
8280 case UNGT_EXPR:
8281 code = UNGT;
8282 break;
8283 case UNGE_EXPR:
8284 code = UNGE;
8285 break;
8286 case UNEQ_EXPR:
8287 code = UNEQ;
8288 break;
8289 case LTGT_EXPR:
8290 code = LTGT;
8291 break;
8293 default:
8294 gcc_unreachable ();
8296 return code;
8299 /* Subroutine of expand_expr. Expand the two operands of a binary
8300 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8301 The value may be stored in TARGET if TARGET is nonzero. The
8302 MODIFIER argument is as documented by expand_expr. */
8304 void
8305 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8306 enum expand_modifier modifier)
8308 if (! safe_from_p (target, exp1, 1))
8309 target = 0;
8310 if (operand_equal_p (exp0, exp1, 0))
8312 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8313 *op1 = copy_rtx (*op0);
8315 else
8317 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8318 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8323 /* Return a MEM that contains constant EXP. DEFER is as for
8324 output_constant_def and MODIFIER is as for expand_expr. */
8326 static rtx
8327 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8329 rtx mem;
8331 mem = output_constant_def (exp, defer);
8332 if (modifier != EXPAND_INITIALIZER)
8333 mem = use_anchored_address (mem);
8334 return mem;
8337 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8338 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8340 static rtx
8341 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8342 enum expand_modifier modifier, addr_space_t as)
8344 rtx result, subtarget;
8345 tree inner, offset;
8346 poly_int64 bitsize, bitpos;
8347 int unsignedp, reversep, volatilep = 0;
8348 machine_mode mode1;
8350 /* If we are taking the address of a constant and are at the top level,
8351 we have to use output_constant_def since we can't call force_const_mem
8352 at top level. */
8353 /* ??? This should be considered a front-end bug. We should not be
8354 generating ADDR_EXPR of something that isn't an LVALUE. The only
8355 exception here is STRING_CST. */
8356 if (CONSTANT_CLASS_P (exp))
8358 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8359 if (modifier < EXPAND_SUM)
8360 result = force_operand (result, target);
8361 return result;
8364 /* Everything must be something allowed by is_gimple_addressable. */
8365 switch (TREE_CODE (exp))
8367 case INDIRECT_REF:
8368 /* This case will happen via recursion for &a->b. */
8369 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8371 case MEM_REF:
8373 tree tem = TREE_OPERAND (exp, 0);
8374 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8375 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8376 return expand_expr (tem, target, tmode, modifier);
8379 case TARGET_MEM_REF:
8380 return addr_for_mem_ref (exp, as, true);
8382 case CONST_DECL:
8383 /* Expand the initializer like constants above. */
8384 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8385 0, modifier), 0);
8386 if (modifier < EXPAND_SUM)
8387 result = force_operand (result, target);
8388 return result;
8390 case REALPART_EXPR:
8391 /* The real part of the complex number is always first, therefore
8392 the address is the same as the address of the parent object. */
8393 offset = 0;
8394 bitpos = 0;
8395 inner = TREE_OPERAND (exp, 0);
8396 break;
8398 case IMAGPART_EXPR:
8399 /* The imaginary part of the complex number is always second.
8400 The expression is therefore always offset by the size of the
8401 scalar type. */
8402 offset = 0;
8403 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8404 inner = TREE_OPERAND (exp, 0);
8405 break;
8407 case COMPOUND_LITERAL_EXPR:
8408 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8409 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8410 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8411 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8412 the initializers aren't gimplified. */
8413 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8414 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8415 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8416 target, tmode, modifier, as);
8417 /* FALLTHRU */
8418 default:
8419 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8420 expand_expr, as that can have various side effects; LABEL_DECLs for
8421 example, may not have their DECL_RTL set yet. Expand the rtl of
8422 CONSTRUCTORs too, which should yield a memory reference for the
8423 constructor's contents. Assume language specific tree nodes can
8424 be expanded in some interesting way. */
8425 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8426 if (DECL_P (exp)
8427 || TREE_CODE (exp) == CONSTRUCTOR
8428 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8430 result = expand_expr (exp, target, tmode,
8431 modifier == EXPAND_INITIALIZER
8432 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8434 /* If the DECL isn't in memory, then the DECL wasn't properly
8435 marked TREE_ADDRESSABLE, which will be either a front-end
8436 or a tree optimizer bug. */
8438 gcc_assert (MEM_P (result));
8439 result = XEXP (result, 0);
8441 /* ??? Is this needed anymore? */
8442 if (DECL_P (exp))
8443 TREE_USED (exp) = 1;
8445 if (modifier != EXPAND_INITIALIZER
8446 && modifier != EXPAND_CONST_ADDRESS
8447 && modifier != EXPAND_SUM)
8448 result = force_operand (result, target);
8449 return result;
8452 /* Pass FALSE as the last argument to get_inner_reference although
8453 we are expanding to RTL. The rationale is that we know how to
8454 handle "aligning nodes" here: we can just bypass them because
8455 they won't change the final object whose address will be returned
8456 (they actually exist only for that purpose). */
8457 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8458 &unsignedp, &reversep, &volatilep);
8459 break;
8462 /* We must have made progress. */
8463 gcc_assert (inner != exp);
8465 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8466 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8467 inner alignment, force the inner to be sufficiently aligned. */
8468 if (CONSTANT_CLASS_P (inner)
8469 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8471 inner = copy_node (inner);
8472 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8473 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8474 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8476 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8478 if (offset)
8480 rtx tmp;
8482 if (modifier != EXPAND_NORMAL)
8483 result = force_operand (result, NULL);
8484 tmp = expand_expr (offset, NULL_RTX, tmode,
8485 modifier == EXPAND_INITIALIZER
8486 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8488 /* expand_expr is allowed to return an object in a mode other
8489 than TMODE. If it did, we need to convert. */
8490 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8491 tmp = convert_modes (tmode, GET_MODE (tmp),
8492 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8493 result = convert_memory_address_addr_space (tmode, result, as);
8494 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8496 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8497 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8498 else
8500 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8501 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8502 1, OPTAB_LIB_WIDEN);
8506 if (maybe_ne (bitpos, 0))
8508 /* Someone beforehand should have rejected taking the address
8509 of an object that isn't byte-aligned. */
8510 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8511 result = convert_memory_address_addr_space (tmode, result, as);
8512 result = plus_constant (tmode, result, bytepos);
8513 if (modifier < EXPAND_SUM)
8514 result = force_operand (result, target);
8517 return result;
8520 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8521 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8523 static rtx
8524 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8525 enum expand_modifier modifier)
8527 addr_space_t as = ADDR_SPACE_GENERIC;
8528 scalar_int_mode address_mode = Pmode;
8529 scalar_int_mode pointer_mode = ptr_mode;
8530 machine_mode rmode;
8531 rtx result;
8533 /* Target mode of VOIDmode says "whatever's natural". */
8534 if (tmode == VOIDmode)
8535 tmode = TYPE_MODE (TREE_TYPE (exp));
8537 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8539 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8540 address_mode = targetm.addr_space.address_mode (as);
8541 pointer_mode = targetm.addr_space.pointer_mode (as);
8544 /* We can get called with some Weird Things if the user does silliness
8545 like "(short) &a". In that case, convert_memory_address won't do
8546 the right thing, so ignore the given target mode. */
8547 scalar_int_mode new_tmode = (tmode == pointer_mode
8548 ? pointer_mode
8549 : address_mode);
8551 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8552 new_tmode, modifier, as);
8554 /* Despite expand_expr claims concerning ignoring TMODE when not
8555 strictly convenient, stuff breaks if we don't honor it. Note
8556 that combined with the above, we only do this for pointer modes. */
8557 rmode = GET_MODE (result);
8558 if (rmode == VOIDmode)
8559 rmode = new_tmode;
8560 if (rmode != new_tmode)
8561 result = convert_memory_address_addr_space (new_tmode, result, as);
8563 return result;
8566 /* Generate code for computing CONSTRUCTOR EXP.
8567 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8568 is TRUE, instead of creating a temporary variable in memory
8569 NULL is returned and the caller needs to handle it differently. */
8571 static rtx
8572 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8573 bool avoid_temp_mem)
8575 tree type = TREE_TYPE (exp);
8576 machine_mode mode = TYPE_MODE (type);
8578 /* Try to avoid creating a temporary at all. This is possible
8579 if all of the initializer is zero.
8580 FIXME: try to handle all [0..255] initializers we can handle
8581 with memset. */
8582 if (TREE_STATIC (exp)
8583 && !TREE_ADDRESSABLE (exp)
8584 && target != 0 && mode == BLKmode
8585 && all_zeros_p (exp))
8587 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8588 return target;
8591 /* All elts simple constants => refer to a constant in memory. But
8592 if this is a non-BLKmode mode, let it store a field at a time
8593 since that should make a CONST_INT, CONST_WIDE_INT or
8594 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8595 use, it is best to store directly into the target unless the type
8596 is large enough that memcpy will be used. If we are making an
8597 initializer and all operands are constant, put it in memory as
8598 well.
8600 FIXME: Avoid trying to fill vector constructors piece-meal.
8601 Output them with output_constant_def below unless we're sure
8602 they're zeros. This should go away when vector initializers
8603 are treated like VECTOR_CST instead of arrays. */
8604 if ((TREE_STATIC (exp)
8605 && ((mode == BLKmode
8606 && ! (target != 0 && safe_from_p (target, exp, 1)))
8607 || TREE_ADDRESSABLE (exp)
8608 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8609 && (! can_move_by_pieces
8610 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8611 TYPE_ALIGN (type)))
8612 && ! mostly_zeros_p (exp))))
8613 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8614 && TREE_CONSTANT (exp)))
8616 rtx constructor;
8618 if (avoid_temp_mem)
8619 return NULL_RTX;
8621 constructor = expand_expr_constant (exp, 1, modifier);
8623 if (modifier != EXPAND_CONST_ADDRESS
8624 && modifier != EXPAND_INITIALIZER
8625 && modifier != EXPAND_SUM)
8626 constructor = validize_mem (constructor);
8628 return constructor;
8631 /* If the CTOR is available in static storage and not mostly
8632 zeros and we can move it by pieces prefer to do so since
8633 that's usually more efficient than performing a series of
8634 stores from immediates. */
8635 if (avoid_temp_mem
8636 && TREE_STATIC (exp)
8637 && TREE_CONSTANT (exp)
8638 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8639 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8640 TYPE_ALIGN (type))
8641 && ! mostly_zeros_p (exp))
8642 return NULL_RTX;
8644 /* Handle calls that pass values in multiple non-contiguous
8645 locations. The Irix 6 ABI has examples of this. */
8646 if (target == 0 || ! safe_from_p (target, exp, 1)
8647 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8648 /* Also make a temporary if the store is to volatile memory, to
8649 avoid individual accesses to aggregate members. */
8650 || (GET_CODE (target) == MEM
8651 && MEM_VOLATILE_P (target)
8652 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8654 if (avoid_temp_mem)
8655 return NULL_RTX;
8657 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8660 store_constructor (exp, target, 0, int_expr_size (exp), false);
8661 return target;
8665 /* expand_expr: generate code for computing expression EXP.
8666 An rtx for the computed value is returned. The value is never null.
8667 In the case of a void EXP, const0_rtx is returned.
8669 The value may be stored in TARGET if TARGET is nonzero.
8670 TARGET is just a suggestion; callers must assume that
8671 the rtx returned may not be the same as TARGET.
8673 If TARGET is CONST0_RTX, it means that the value will be ignored.
8675 If TMODE is not VOIDmode, it suggests generating the
8676 result in mode TMODE. But this is done only when convenient.
8677 Otherwise, TMODE is ignored and the value generated in its natural mode.
8678 TMODE is just a suggestion; callers must assume that
8679 the rtx returned may not have mode TMODE.
8681 Note that TARGET may have neither TMODE nor MODE. In that case, it
8682 probably will not be used.
8684 If MODIFIER is EXPAND_SUM then when EXP is an addition
8685 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8686 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8687 products as above, or REG or MEM, or constant.
8688 Ordinarily in such cases we would output mul or add instructions
8689 and then return a pseudo reg containing the sum.
8691 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8692 it also marks a label as absolutely required (it can't be dead).
8693 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8694 This is used for outputting expressions used in initializers.
8696 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8697 with a constant address even if that address is not normally legitimate.
8698 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8700 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8701 a call parameter. Such targets require special care as we haven't yet
8702 marked TARGET so that it's safe from being trashed by libcalls. We
8703 don't want to use TARGET for anything but the final result;
8704 Intermediate values must go elsewhere. Additionally, calls to
8705 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8707 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8708 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8709 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8710 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8711 recursively.
8712 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8713 then *ALT_RTL is set to TARGET (before legitimziation).
8715 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8716 In this case, we don't adjust a returned MEM rtx that wouldn't be
8717 sufficiently aligned for its mode; instead, it's up to the caller
8718 to deal with it afterwards. This is used to make sure that unaligned
8719 base objects for which out-of-bounds accesses are supported, for
8720 example record types with trailing arrays, aren't realigned behind
8721 the back of the caller.
8722 The normal operating mode is to pass FALSE for this parameter. */
8725 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8726 enum expand_modifier modifier, rtx *alt_rtl,
8727 bool inner_reference_p)
8729 rtx ret;
8731 /* Handle ERROR_MARK before anybody tries to access its type. */
8732 if (TREE_CODE (exp) == ERROR_MARK
8733 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8735 ret = CONST0_RTX (tmode);
8736 return ret ? ret : const0_rtx;
8739 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8740 inner_reference_p);
8741 return ret;
8744 /* Try to expand the conditional expression which is represented by
8745 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8746 return the rtl reg which represents the result. Otherwise return
8747 NULL_RTX. */
8749 static rtx
8750 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8751 tree treeop1 ATTRIBUTE_UNUSED,
8752 tree treeop2 ATTRIBUTE_UNUSED)
8754 rtx insn;
8755 rtx op00, op01, op1, op2;
8756 enum rtx_code comparison_code;
8757 machine_mode comparison_mode;
8758 gimple *srcstmt;
8759 rtx temp;
8760 tree type = TREE_TYPE (treeop1);
8761 int unsignedp = TYPE_UNSIGNED (type);
8762 machine_mode mode = TYPE_MODE (type);
8763 machine_mode orig_mode = mode;
8764 static bool expanding_cond_expr_using_cmove = false;
8766 /* Conditional move expansion can end up TERing two operands which,
8767 when recursively hitting conditional expressions can result in
8768 exponential behavior if the cmove expansion ultimatively fails.
8769 It's hardly profitable to TER a cmove into a cmove so avoid doing
8770 that by failing early if we end up recursing. */
8771 if (expanding_cond_expr_using_cmove)
8772 return NULL_RTX;
8774 /* If we cannot do a conditional move on the mode, try doing it
8775 with the promoted mode. */
8776 if (!can_conditionally_move_p (mode))
8778 mode = promote_mode (type, mode, &unsignedp);
8779 if (!can_conditionally_move_p (mode))
8780 return NULL_RTX;
8781 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8783 else
8784 temp = assign_temp (type, 0, 1);
8786 expanding_cond_expr_using_cmove = true;
8787 start_sequence ();
8788 expand_operands (treeop1, treeop2,
8789 temp, &op1, &op2, EXPAND_NORMAL);
8791 if (TREE_CODE (treeop0) == SSA_NAME
8792 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8794 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8795 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8796 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8797 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8798 comparison_mode = TYPE_MODE (type);
8799 unsignedp = TYPE_UNSIGNED (type);
8800 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8802 else if (COMPARISON_CLASS_P (treeop0))
8804 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8805 enum tree_code cmpcode = TREE_CODE (treeop0);
8806 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8807 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8808 unsignedp = TYPE_UNSIGNED (type);
8809 comparison_mode = TYPE_MODE (type);
8810 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8812 else
8814 op00 = expand_normal (treeop0);
8815 op01 = const0_rtx;
8816 comparison_code = NE;
8817 comparison_mode = GET_MODE (op00);
8818 if (comparison_mode == VOIDmode)
8819 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8821 expanding_cond_expr_using_cmove = false;
8823 if (GET_MODE (op1) != mode)
8824 op1 = gen_lowpart (mode, op1);
8826 if (GET_MODE (op2) != mode)
8827 op2 = gen_lowpart (mode, op2);
8829 /* Try to emit the conditional move. */
8830 insn = emit_conditional_move (temp, comparison_code,
8831 op00, op01, comparison_mode,
8832 op1, op2, mode,
8833 unsignedp);
8835 /* If we could do the conditional move, emit the sequence,
8836 and return. */
8837 if (insn)
8839 rtx_insn *seq = get_insns ();
8840 end_sequence ();
8841 emit_insn (seq);
8842 return convert_modes (orig_mode, mode, temp, 0);
8845 /* Otherwise discard the sequence and fall back to code with
8846 branches. */
8847 end_sequence ();
8848 return NULL_RTX;
8851 /* A helper function for expand_expr_real_2 to be used with a
8852 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8853 is nonzero, with alignment ALIGN in bits.
8854 Store the value at TARGET if possible (if TARGET is nonzero).
8855 Regardless of TARGET, we return the rtx for where the value is placed.
8856 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8857 then *ALT_RTL is set to TARGET (before legitimziation). */
8859 static rtx
8860 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8861 unsigned int align, rtx target, rtx *alt_rtl)
8863 enum insn_code icode;
8865 if ((icode = optab_handler (movmisalign_optab, mode))
8866 != CODE_FOR_nothing)
8868 class expand_operand ops[2];
8870 /* We've already validated the memory, and we're creating a
8871 new pseudo destination. The predicates really can't fail,
8872 nor can the generator. */
8873 create_output_operand (&ops[0], NULL_RTX, mode);
8874 create_fixed_operand (&ops[1], temp);
8875 expand_insn (icode, 2, ops);
8876 temp = ops[0].value;
8878 else if (targetm.slow_unaligned_access (mode, align))
8879 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8880 0, unsignedp, target,
8881 mode, mode, false, alt_rtl);
8882 return temp;
8885 /* Helper function of expand_expr_2, expand a division or modulo.
8886 op0 and op1 should be already expanded treeop0 and treeop1, using
8887 expand_operands. */
8889 static rtx
8890 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8891 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8893 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8894 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8895 if (SCALAR_INT_MODE_P (mode)
8896 && optimize >= 2
8897 && get_range_pos_neg (treeop0) == 1
8898 && get_range_pos_neg (treeop1) == 1)
8900 /* If both arguments are known to be positive when interpreted
8901 as signed, we can expand it as both signed and unsigned
8902 division or modulo. Choose the cheaper sequence in that case. */
8903 bool speed_p = optimize_insn_for_speed_p ();
8904 do_pending_stack_adjust ();
8905 start_sequence ();
8906 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8907 rtx_insn *uns_insns = get_insns ();
8908 end_sequence ();
8909 start_sequence ();
8910 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8911 rtx_insn *sgn_insns = get_insns ();
8912 end_sequence ();
8913 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8914 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8916 /* If costs are the same then use as tie breaker the other other
8917 factor. */
8918 if (uns_cost == sgn_cost)
8920 uns_cost = seq_cost (uns_insns, !speed_p);
8921 sgn_cost = seq_cost (sgn_insns, !speed_p);
8924 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8926 emit_insn (uns_insns);
8927 return uns_ret;
8929 emit_insn (sgn_insns);
8930 return sgn_ret;
8932 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8936 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8937 enum expand_modifier modifier)
8939 rtx op0, op1, op2, temp;
8940 rtx_code_label *lab;
8941 tree type;
8942 int unsignedp;
8943 machine_mode mode;
8944 scalar_int_mode int_mode;
8945 enum tree_code code = ops->code;
8946 optab this_optab;
8947 rtx subtarget, original_target;
8948 int ignore;
8949 bool reduce_bit_field;
8950 location_t loc = ops->location;
8951 tree treeop0, treeop1, treeop2;
8952 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8953 ? reduce_to_bit_field_precision ((expr), \
8954 target, \
8955 type) \
8956 : (expr))
8958 type = ops->type;
8959 mode = TYPE_MODE (type);
8960 unsignedp = TYPE_UNSIGNED (type);
8962 treeop0 = ops->op0;
8963 treeop1 = ops->op1;
8964 treeop2 = ops->op2;
8966 /* We should be called only on simple (binary or unary) expressions,
8967 exactly those that are valid in gimple expressions that aren't
8968 GIMPLE_SINGLE_RHS (or invalid). */
8969 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8970 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8971 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8973 ignore = (target == const0_rtx
8974 || ((CONVERT_EXPR_CODE_P (code)
8975 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8976 && TREE_CODE (type) == VOID_TYPE));
8978 /* We should be called only if we need the result. */
8979 gcc_assert (!ignore);
8981 /* An operation in what may be a bit-field type needs the
8982 result to be reduced to the precision of the bit-field type,
8983 which is narrower than that of the type's mode. */
8984 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8985 && !type_has_mode_precision_p (type));
8987 if (reduce_bit_field
8988 && (modifier == EXPAND_STACK_PARM
8989 || (target && GET_MODE (target) != mode)))
8990 target = 0;
8992 /* Use subtarget as the target for operand 0 of a binary operation. */
8993 subtarget = get_subtarget (target);
8994 original_target = target;
8996 switch (code)
8998 case NON_LVALUE_EXPR:
8999 case PAREN_EXPR:
9000 CASE_CONVERT:
9001 if (treeop0 == error_mark_node)
9002 return const0_rtx;
9004 if (TREE_CODE (type) == UNION_TYPE)
9006 tree valtype = TREE_TYPE (treeop0);
9008 /* If both input and output are BLKmode, this conversion isn't doing
9009 anything except possibly changing memory attribute. */
9010 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9012 rtx result = expand_expr (treeop0, target, tmode,
9013 modifier);
9015 result = copy_rtx (result);
9016 set_mem_attributes (result, type, 0);
9017 return result;
9020 if (target == 0)
9022 if (TYPE_MODE (type) != BLKmode)
9023 target = gen_reg_rtx (TYPE_MODE (type));
9024 else
9025 target = assign_temp (type, 1, 1);
9028 if (MEM_P (target))
9029 /* Store data into beginning of memory target. */
9030 store_expr (treeop0,
9031 adjust_address (target, TYPE_MODE (valtype), 0),
9032 modifier == EXPAND_STACK_PARM,
9033 false, TYPE_REVERSE_STORAGE_ORDER (type));
9035 else
9037 gcc_assert (REG_P (target)
9038 && !TYPE_REVERSE_STORAGE_ORDER (type));
9040 /* Store this field into a union of the proper type. */
9041 poly_uint64 op0_size
9042 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9043 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9044 store_field (target,
9045 /* The conversion must be constructed so that
9046 we know at compile time how many bits
9047 to preserve. */
9048 ordered_min (op0_size, union_size),
9049 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9050 false, false);
9053 /* Return the entire union. */
9054 return target;
9057 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9059 op0 = expand_expr (treeop0, target, VOIDmode,
9060 modifier);
9062 /* If the signedness of the conversion differs and OP0 is
9063 a promoted SUBREG, clear that indication since we now
9064 have to do the proper extension. */
9065 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9066 && GET_CODE (op0) == SUBREG)
9067 SUBREG_PROMOTED_VAR_P (op0) = 0;
9069 return REDUCE_BIT_FIELD (op0);
9072 op0 = expand_expr (treeop0, NULL_RTX, mode,
9073 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9074 if (GET_MODE (op0) == mode)
9077 /* If OP0 is a constant, just convert it into the proper mode. */
9078 else if (CONSTANT_P (op0))
9080 tree inner_type = TREE_TYPE (treeop0);
9081 machine_mode inner_mode = GET_MODE (op0);
9083 if (inner_mode == VOIDmode)
9084 inner_mode = TYPE_MODE (inner_type);
9086 if (modifier == EXPAND_INITIALIZER)
9087 op0 = lowpart_subreg (mode, op0, inner_mode);
9088 else
9089 op0= convert_modes (mode, inner_mode, op0,
9090 TYPE_UNSIGNED (inner_type));
9093 else if (modifier == EXPAND_INITIALIZER)
9094 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9095 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9097 else if (target == 0)
9098 op0 = convert_to_mode (mode, op0,
9099 TYPE_UNSIGNED (TREE_TYPE
9100 (treeop0)));
9101 else
9103 convert_move (target, op0,
9104 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9105 op0 = target;
9108 return REDUCE_BIT_FIELD (op0);
9110 case ADDR_SPACE_CONVERT_EXPR:
9112 tree treeop0_type = TREE_TYPE (treeop0);
9114 gcc_assert (POINTER_TYPE_P (type));
9115 gcc_assert (POINTER_TYPE_P (treeop0_type));
9117 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9118 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9120 /* Conversions between pointers to the same address space should
9121 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9122 gcc_assert (as_to != as_from);
9124 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9126 /* Ask target code to handle conversion between pointers
9127 to overlapping address spaces. */
9128 if (targetm.addr_space.subset_p (as_to, as_from)
9129 || targetm.addr_space.subset_p (as_from, as_to))
9131 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9133 else
9135 /* For disjoint address spaces, converting anything but a null
9136 pointer invokes undefined behavior. We truncate or extend the
9137 value as if we'd converted via integers, which handles 0 as
9138 required, and all others as the programmer likely expects. */
9139 #ifndef POINTERS_EXTEND_UNSIGNED
9140 const int POINTERS_EXTEND_UNSIGNED = 1;
9141 #endif
9142 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9143 op0, POINTERS_EXTEND_UNSIGNED);
9145 gcc_assert (op0);
9146 return op0;
9149 case POINTER_PLUS_EXPR:
9150 /* Even though the sizetype mode and the pointer's mode can be different
9151 expand is able to handle this correctly and get the correct result out
9152 of the PLUS_EXPR code. */
9153 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9154 if sizetype precision is smaller than pointer precision. */
9155 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9156 treeop1 = fold_convert_loc (loc, type,
9157 fold_convert_loc (loc, ssizetype,
9158 treeop1));
9159 /* If sizetype precision is larger than pointer precision, truncate the
9160 offset to have matching modes. */
9161 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9162 treeop1 = fold_convert_loc (loc, type, treeop1);
9163 /* FALLTHRU */
9165 case PLUS_EXPR:
9166 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9167 something else, make sure we add the register to the constant and
9168 then to the other thing. This case can occur during strength
9169 reduction and doing it this way will produce better code if the
9170 frame pointer or argument pointer is eliminated.
9172 fold-const.c will ensure that the constant is always in the inner
9173 PLUS_EXPR, so the only case we need to do anything about is if
9174 sp, ap, or fp is our second argument, in which case we must swap
9175 the innermost first argument and our second argument. */
9177 if (TREE_CODE (treeop0) == PLUS_EXPR
9178 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9179 && VAR_P (treeop1)
9180 && (DECL_RTL (treeop1) == frame_pointer_rtx
9181 || DECL_RTL (treeop1) == stack_pointer_rtx
9182 || DECL_RTL (treeop1) == arg_pointer_rtx))
9184 gcc_unreachable ();
9187 /* If the result is to be ptr_mode and we are adding an integer to
9188 something, we might be forming a constant. So try to use
9189 plus_constant. If it produces a sum and we can't accept it,
9190 use force_operand. This allows P = &ARR[const] to generate
9191 efficient code on machines where a SYMBOL_REF is not a valid
9192 address.
9194 If this is an EXPAND_SUM call, always return the sum. */
9195 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9196 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9198 if (modifier == EXPAND_STACK_PARM)
9199 target = 0;
9200 if (TREE_CODE (treeop0) == INTEGER_CST
9201 && HWI_COMPUTABLE_MODE_P (mode)
9202 && TREE_CONSTANT (treeop1))
9204 rtx constant_part;
9205 HOST_WIDE_INT wc;
9206 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9208 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9209 EXPAND_SUM);
9210 /* Use wi::shwi to ensure that the constant is
9211 truncated according to the mode of OP1, then sign extended
9212 to a HOST_WIDE_INT. Using the constant directly can result
9213 in non-canonical RTL in a 64x32 cross compile. */
9214 wc = TREE_INT_CST_LOW (treeop0);
9215 constant_part =
9216 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9217 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9218 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9219 op1 = force_operand (op1, target);
9220 return REDUCE_BIT_FIELD (op1);
9223 else if (TREE_CODE (treeop1) == INTEGER_CST
9224 && HWI_COMPUTABLE_MODE_P (mode)
9225 && TREE_CONSTANT (treeop0))
9227 rtx constant_part;
9228 HOST_WIDE_INT wc;
9229 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9231 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9232 (modifier == EXPAND_INITIALIZER
9233 ? EXPAND_INITIALIZER : EXPAND_SUM));
9234 if (! CONSTANT_P (op0))
9236 op1 = expand_expr (treeop1, NULL_RTX,
9237 VOIDmode, modifier);
9238 /* Return a PLUS if modifier says it's OK. */
9239 if (modifier == EXPAND_SUM
9240 || modifier == EXPAND_INITIALIZER)
9241 return simplify_gen_binary (PLUS, mode, op0, op1);
9242 goto binop2;
9244 /* Use wi::shwi to ensure that the constant is
9245 truncated according to the mode of OP1, then sign extended
9246 to a HOST_WIDE_INT. Using the constant directly can result
9247 in non-canonical RTL in a 64x32 cross compile. */
9248 wc = TREE_INT_CST_LOW (treeop1);
9249 constant_part
9250 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9251 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9252 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9253 op0 = force_operand (op0, target);
9254 return REDUCE_BIT_FIELD (op0);
9258 /* Use TER to expand pointer addition of a negated value
9259 as pointer subtraction. */
9260 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9261 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9262 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9263 && TREE_CODE (treeop1) == SSA_NAME
9264 && TYPE_MODE (TREE_TYPE (treeop0))
9265 == TYPE_MODE (TREE_TYPE (treeop1)))
9267 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9268 if (def)
9270 treeop1 = gimple_assign_rhs1 (def);
9271 code = MINUS_EXPR;
9272 goto do_minus;
9276 /* No sense saving up arithmetic to be done
9277 if it's all in the wrong mode to form part of an address.
9278 And force_operand won't know whether to sign-extend or
9279 zero-extend. */
9280 if (modifier != EXPAND_INITIALIZER
9281 && (modifier != EXPAND_SUM || mode != ptr_mode))
9283 expand_operands (treeop0, treeop1,
9284 subtarget, &op0, &op1, modifier);
9285 if (op0 == const0_rtx)
9286 return op1;
9287 if (op1 == const0_rtx)
9288 return op0;
9289 goto binop2;
9292 expand_operands (treeop0, treeop1,
9293 subtarget, &op0, &op1, modifier);
9294 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9296 case MINUS_EXPR:
9297 case POINTER_DIFF_EXPR:
9298 do_minus:
9299 /* For initializers, we are allowed to return a MINUS of two
9300 symbolic constants. Here we handle all cases when both operands
9301 are constant. */
9302 /* Handle difference of two symbolic constants,
9303 for the sake of an initializer. */
9304 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9305 && really_constant_p (treeop0)
9306 && really_constant_p (treeop1))
9308 expand_operands (treeop0, treeop1,
9309 NULL_RTX, &op0, &op1, modifier);
9310 return simplify_gen_binary (MINUS, mode, op0, op1);
9313 /* No sense saving up arithmetic to be done
9314 if it's all in the wrong mode to form part of an address.
9315 And force_operand won't know whether to sign-extend or
9316 zero-extend. */
9317 if (modifier != EXPAND_INITIALIZER
9318 && (modifier != EXPAND_SUM || mode != ptr_mode))
9319 goto binop;
9321 expand_operands (treeop0, treeop1,
9322 subtarget, &op0, &op1, modifier);
9324 /* Convert A - const to A + (-const). */
9325 if (CONST_INT_P (op1))
9327 op1 = negate_rtx (mode, op1);
9328 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9331 goto binop2;
9333 case WIDEN_MULT_PLUS_EXPR:
9334 case WIDEN_MULT_MINUS_EXPR:
9335 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9336 op2 = expand_normal (treeop2);
9337 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9338 target, unsignedp);
9339 return target;
9341 case WIDEN_PLUS_EXPR:
9342 case WIDEN_MINUS_EXPR:
9343 case WIDEN_MULT_EXPR:
9344 /* If first operand is constant, swap them.
9345 Thus the following special case checks need only
9346 check the second operand. */
9347 if (TREE_CODE (treeop0) == INTEGER_CST)
9348 std::swap (treeop0, treeop1);
9350 /* First, check if we have a multiplication of one signed and one
9351 unsigned operand. */
9352 if (TREE_CODE (treeop1) != INTEGER_CST
9353 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9354 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9356 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9357 this_optab = usmul_widen_optab;
9358 if (find_widening_optab_handler (this_optab, mode, innermode)
9359 != CODE_FOR_nothing)
9361 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9362 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9363 EXPAND_NORMAL);
9364 else
9365 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9366 EXPAND_NORMAL);
9367 /* op0 and op1 might still be constant, despite the above
9368 != INTEGER_CST check. Handle it. */
9369 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9371 op0 = convert_modes (mode, innermode, op0, true);
9372 op1 = convert_modes (mode, innermode, op1, false);
9373 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9374 target, unsignedp));
9376 goto binop3;
9379 /* Check for a multiplication with matching signedness. */
9380 else if ((TREE_CODE (treeop1) == INTEGER_CST
9381 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9382 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9383 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9385 tree op0type = TREE_TYPE (treeop0);
9386 machine_mode innermode = TYPE_MODE (op0type);
9387 bool zextend_p = TYPE_UNSIGNED (op0type);
9388 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9389 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9391 if (TREE_CODE (treeop0) != INTEGER_CST)
9393 if (find_widening_optab_handler (this_optab, mode, innermode)
9394 != CODE_FOR_nothing)
9396 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9397 EXPAND_NORMAL);
9398 /* op0 and op1 might still be constant, despite the above
9399 != INTEGER_CST check. Handle it. */
9400 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9402 widen_mult_const:
9403 op0 = convert_modes (mode, innermode, op0, zextend_p);
9405 = convert_modes (mode, innermode, op1,
9406 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9407 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9408 target,
9409 unsignedp));
9411 temp = expand_widening_mult (mode, op0, op1, target,
9412 unsignedp, this_optab);
9413 return REDUCE_BIT_FIELD (temp);
9415 if (find_widening_optab_handler (other_optab, mode, innermode)
9416 != CODE_FOR_nothing
9417 && innermode == word_mode)
9419 rtx htem, hipart;
9420 op0 = expand_normal (treeop0);
9421 op1 = expand_normal (treeop1);
9422 /* op0 and op1 might be constants, despite the above
9423 != INTEGER_CST check. Handle it. */
9424 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9425 goto widen_mult_const;
9426 temp = expand_binop (mode, other_optab, op0, op1, target,
9427 unsignedp, OPTAB_LIB_WIDEN);
9428 hipart = gen_highpart (word_mode, temp);
9429 htem = expand_mult_highpart_adjust (word_mode, hipart,
9430 op0, op1, hipart,
9431 zextend_p);
9432 if (htem != hipart)
9433 emit_move_insn (hipart, htem);
9434 return REDUCE_BIT_FIELD (temp);
9438 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9439 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9440 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9441 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9443 case MULT_EXPR:
9444 /* If this is a fixed-point operation, then we cannot use the code
9445 below because "expand_mult" doesn't support sat/no-sat fixed-point
9446 multiplications. */
9447 if (ALL_FIXED_POINT_MODE_P (mode))
9448 goto binop;
9450 /* If first operand is constant, swap them.
9451 Thus the following special case checks need only
9452 check the second operand. */
9453 if (TREE_CODE (treeop0) == INTEGER_CST)
9454 std::swap (treeop0, treeop1);
9456 /* Attempt to return something suitable for generating an
9457 indexed address, for machines that support that. */
9459 if (modifier == EXPAND_SUM && mode == ptr_mode
9460 && tree_fits_shwi_p (treeop1))
9462 tree exp1 = treeop1;
9464 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9465 EXPAND_SUM);
9467 if (!REG_P (op0))
9468 op0 = force_operand (op0, NULL_RTX);
9469 if (!REG_P (op0))
9470 op0 = copy_to_mode_reg (mode, op0);
9472 op1 = gen_int_mode (tree_to_shwi (exp1),
9473 TYPE_MODE (TREE_TYPE (exp1)));
9474 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9477 if (modifier == EXPAND_STACK_PARM)
9478 target = 0;
9480 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9482 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9483 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9484 if (def_stmt0
9485 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9486 def_stmt0 = NULL;
9487 if (def_stmt1
9488 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9489 def_stmt1 = NULL;
9491 if (def_stmt0 || def_stmt1)
9493 /* X / Y * Y can be expanded as X - X % Y too.
9494 Choose the cheaper sequence of those two. */
9495 if (def_stmt0)
9496 treeop0 = gimple_assign_rhs1 (def_stmt0);
9497 else
9499 treeop1 = treeop0;
9500 treeop0 = gimple_assign_rhs1 (def_stmt1);
9502 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9503 EXPAND_NORMAL);
9504 bool speed_p = optimize_insn_for_speed_p ();
9505 do_pending_stack_adjust ();
9506 start_sequence ();
9507 rtx divmul_ret
9508 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9509 op0, op1, NULL_RTX, unsignedp);
9510 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9511 unsignedp);
9512 rtx_insn *divmul_insns = get_insns ();
9513 end_sequence ();
9514 start_sequence ();
9515 rtx modsub_ret
9516 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9517 op0, op1, NULL_RTX, unsignedp);
9518 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9519 optab_default);
9520 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9521 target, unsignedp, OPTAB_LIB_WIDEN);
9522 rtx_insn *modsub_insns = get_insns ();
9523 end_sequence ();
9524 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9525 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9526 /* If costs are the same then use as tie breaker the other other
9527 factor. */
9528 if (divmul_cost == modsub_cost)
9530 divmul_cost = seq_cost (divmul_insns, !speed_p);
9531 modsub_cost = seq_cost (modsub_insns, !speed_p);
9534 if (divmul_cost <= modsub_cost)
9536 emit_insn (divmul_insns);
9537 return REDUCE_BIT_FIELD (divmul_ret);
9539 emit_insn (modsub_insns);
9540 return REDUCE_BIT_FIELD (modsub_ret);
9544 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9545 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9547 case TRUNC_MOD_EXPR:
9548 case FLOOR_MOD_EXPR:
9549 case CEIL_MOD_EXPR:
9550 case ROUND_MOD_EXPR:
9552 case TRUNC_DIV_EXPR:
9553 case FLOOR_DIV_EXPR:
9554 case CEIL_DIV_EXPR:
9555 case ROUND_DIV_EXPR:
9556 case EXACT_DIV_EXPR:
9557 /* If this is a fixed-point operation, then we cannot use the code
9558 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9559 divisions. */
9560 if (ALL_FIXED_POINT_MODE_P (mode))
9561 goto binop;
9563 if (modifier == EXPAND_STACK_PARM)
9564 target = 0;
9565 /* Possible optimization: compute the dividend with EXPAND_SUM
9566 then if the divisor is constant can optimize the case
9567 where some terms of the dividend have coeffs divisible by it. */
9568 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9569 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9570 target, unsignedp);
9572 case RDIV_EXPR:
9573 goto binop;
9575 case MULT_HIGHPART_EXPR:
9576 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9577 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9578 gcc_assert (temp);
9579 return temp;
9581 case FIXED_CONVERT_EXPR:
9582 op0 = expand_normal (treeop0);
9583 if (target == 0 || modifier == EXPAND_STACK_PARM)
9584 target = gen_reg_rtx (mode);
9586 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9587 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9588 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9589 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9590 else
9591 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9592 return target;
9594 case FIX_TRUNC_EXPR:
9595 op0 = expand_normal (treeop0);
9596 if (target == 0 || modifier == EXPAND_STACK_PARM)
9597 target = gen_reg_rtx (mode);
9598 expand_fix (target, op0, unsignedp);
9599 return target;
9601 case FLOAT_EXPR:
9602 op0 = expand_normal (treeop0);
9603 if (target == 0 || modifier == EXPAND_STACK_PARM)
9604 target = gen_reg_rtx (mode);
9605 /* expand_float can't figure out what to do if FROM has VOIDmode.
9606 So give it the correct mode. With -O, cse will optimize this. */
9607 if (GET_MODE (op0) == VOIDmode)
9608 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9609 op0);
9610 expand_float (target, op0,
9611 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9612 return target;
9614 case NEGATE_EXPR:
9615 op0 = expand_expr (treeop0, subtarget,
9616 VOIDmode, EXPAND_NORMAL);
9617 if (modifier == EXPAND_STACK_PARM)
9618 target = 0;
9619 temp = expand_unop (mode,
9620 optab_for_tree_code (NEGATE_EXPR, type,
9621 optab_default),
9622 op0, target, 0);
9623 gcc_assert (temp);
9624 return REDUCE_BIT_FIELD (temp);
9626 case ABS_EXPR:
9627 case ABSU_EXPR:
9628 op0 = expand_expr (treeop0, subtarget,
9629 VOIDmode, EXPAND_NORMAL);
9630 if (modifier == EXPAND_STACK_PARM)
9631 target = 0;
9633 /* ABS_EXPR is not valid for complex arguments. */
9634 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9635 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9637 /* Unsigned abs is simply the operand. Testing here means we don't
9638 risk generating incorrect code below. */
9639 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9640 return op0;
9642 return expand_abs (mode, op0, target, unsignedp,
9643 safe_from_p (target, treeop0, 1));
9645 case MAX_EXPR:
9646 case MIN_EXPR:
9647 target = original_target;
9648 if (target == 0
9649 || modifier == EXPAND_STACK_PARM
9650 || (MEM_P (target) && MEM_VOLATILE_P (target))
9651 || GET_MODE (target) != mode
9652 || (REG_P (target)
9653 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9654 target = gen_reg_rtx (mode);
9655 expand_operands (treeop0, treeop1,
9656 target, &op0, &op1, EXPAND_NORMAL);
9658 /* First try to do it with a special MIN or MAX instruction.
9659 If that does not win, use a conditional jump to select the proper
9660 value. */
9661 this_optab = optab_for_tree_code (code, type, optab_default);
9662 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9663 OPTAB_WIDEN);
9664 if (temp != 0)
9665 return temp;
9667 if (VECTOR_TYPE_P (type))
9668 gcc_unreachable ();
9670 /* At this point, a MEM target is no longer useful; we will get better
9671 code without it. */
9673 if (! REG_P (target))
9674 target = gen_reg_rtx (mode);
9676 /* If op1 was placed in target, swap op0 and op1. */
9677 if (target != op0 && target == op1)
9678 std::swap (op0, op1);
9680 /* We generate better code and avoid problems with op1 mentioning
9681 target by forcing op1 into a pseudo if it isn't a constant. */
9682 if (! CONSTANT_P (op1))
9683 op1 = force_reg (mode, op1);
9686 enum rtx_code comparison_code;
9687 rtx cmpop1 = op1;
9689 if (code == MAX_EXPR)
9690 comparison_code = unsignedp ? GEU : GE;
9691 else
9692 comparison_code = unsignedp ? LEU : LE;
9694 /* Canonicalize to comparisons against 0. */
9695 if (op1 == const1_rtx)
9697 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9698 or (a != 0 ? a : 1) for unsigned.
9699 For MIN we are safe converting (a <= 1 ? a : 1)
9700 into (a <= 0 ? a : 1) */
9701 cmpop1 = const0_rtx;
9702 if (code == MAX_EXPR)
9703 comparison_code = unsignedp ? NE : GT;
9705 if (op1 == constm1_rtx && !unsignedp)
9707 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9708 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9709 cmpop1 = const0_rtx;
9710 if (code == MIN_EXPR)
9711 comparison_code = LT;
9714 /* Use a conditional move if possible. */
9715 if (can_conditionally_move_p (mode))
9717 rtx insn;
9719 start_sequence ();
9721 /* Try to emit the conditional move. */
9722 insn = emit_conditional_move (target, comparison_code,
9723 op0, cmpop1, mode,
9724 op0, op1, mode,
9725 unsignedp);
9727 /* If we could do the conditional move, emit the sequence,
9728 and return. */
9729 if (insn)
9731 rtx_insn *seq = get_insns ();
9732 end_sequence ();
9733 emit_insn (seq);
9734 return target;
9737 /* Otherwise discard the sequence and fall back to code with
9738 branches. */
9739 end_sequence ();
9742 if (target != op0)
9743 emit_move_insn (target, op0);
9745 lab = gen_label_rtx ();
9746 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9747 unsignedp, mode, NULL_RTX, NULL, lab,
9748 profile_probability::uninitialized ());
9750 emit_move_insn (target, op1);
9751 emit_label (lab);
9752 return target;
9754 case BIT_NOT_EXPR:
9755 op0 = expand_expr (treeop0, subtarget,
9756 VOIDmode, EXPAND_NORMAL);
9757 if (modifier == EXPAND_STACK_PARM)
9758 target = 0;
9759 /* In case we have to reduce the result to bitfield precision
9760 for unsigned bitfield expand this as XOR with a proper constant
9761 instead. */
9762 if (reduce_bit_field && TYPE_UNSIGNED (type))
9764 int_mode = SCALAR_INT_TYPE_MODE (type);
9765 wide_int mask = wi::mask (TYPE_PRECISION (type),
9766 false, GET_MODE_PRECISION (int_mode));
9768 temp = expand_binop (int_mode, xor_optab, op0,
9769 immed_wide_int_const (mask, int_mode),
9770 target, 1, OPTAB_LIB_WIDEN);
9772 else
9773 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9774 gcc_assert (temp);
9775 return temp;
9777 /* ??? Can optimize bitwise operations with one arg constant.
9778 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9779 and (a bitwise1 b) bitwise2 b (etc)
9780 but that is probably not worth while. */
9782 case BIT_AND_EXPR:
9783 case BIT_IOR_EXPR:
9784 case BIT_XOR_EXPR:
9785 goto binop;
9787 case LROTATE_EXPR:
9788 case RROTATE_EXPR:
9789 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9790 || type_has_mode_precision_p (type));
9791 /* fall through */
9793 case LSHIFT_EXPR:
9794 case RSHIFT_EXPR:
9796 /* If this is a fixed-point operation, then we cannot use the code
9797 below because "expand_shift" doesn't support sat/no-sat fixed-point
9798 shifts. */
9799 if (ALL_FIXED_POINT_MODE_P (mode))
9800 goto binop;
9802 if (! safe_from_p (subtarget, treeop1, 1))
9803 subtarget = 0;
9804 if (modifier == EXPAND_STACK_PARM)
9805 target = 0;
9806 op0 = expand_expr (treeop0, subtarget,
9807 VOIDmode, EXPAND_NORMAL);
9809 /* Left shift optimization when shifting across word_size boundary.
9811 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9812 there isn't native instruction to support this wide mode
9813 left shift. Given below scenario:
9815 Type A = (Type) B << C
9817 |< T >|
9818 | dest_high | dest_low |
9820 | word_size |
9822 If the shift amount C caused we shift B to across the word
9823 size boundary, i.e part of B shifted into high half of
9824 destination register, and part of B remains in the low
9825 half, then GCC will use the following left shift expand
9826 logic:
9828 1. Initialize dest_low to B.
9829 2. Initialize every bit of dest_high to the sign bit of B.
9830 3. Logic left shift dest_low by C bit to finalize dest_low.
9831 The value of dest_low before this shift is kept in a temp D.
9832 4. Logic left shift dest_high by C.
9833 5. Logic right shift D by (word_size - C).
9834 6. Or the result of 4 and 5 to finalize dest_high.
9836 While, by checking gimple statements, if operand B is
9837 coming from signed extension, then we can simplify above
9838 expand logic into:
9840 1. dest_high = src_low >> (word_size - C).
9841 2. dest_low = src_low << C.
9843 We can use one arithmetic right shift to finish all the
9844 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9845 needed from 6 into 2.
9847 The case is similar for zero extension, except that we
9848 initialize dest_high to zero rather than copies of the sign
9849 bit from B. Furthermore, we need to use a logical right shift
9850 in this case.
9852 The choice of sign-extension versus zero-extension is
9853 determined entirely by whether or not B is signed and is
9854 independent of the current setting of unsignedp. */
9856 temp = NULL_RTX;
9857 if (code == LSHIFT_EXPR
9858 && target
9859 && REG_P (target)
9860 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9861 && mode == int_mode
9862 && TREE_CONSTANT (treeop1)
9863 && TREE_CODE (treeop0) == SSA_NAME)
9865 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9866 if (is_gimple_assign (def)
9867 && gimple_assign_rhs_code (def) == NOP_EXPR)
9869 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9870 (TREE_TYPE (gimple_assign_rhs1 (def)));
9872 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9873 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9874 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9875 >= GET_MODE_BITSIZE (word_mode)))
9877 rtx_insn *seq, *seq_old;
9878 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9879 int_mode);
9880 bool extend_unsigned
9881 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9882 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9883 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9884 rtx dest_high = simplify_gen_subreg (word_mode, target,
9885 int_mode, high_off);
9886 HOST_WIDE_INT ramount = (BITS_PER_WORD
9887 - TREE_INT_CST_LOW (treeop1));
9888 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9890 start_sequence ();
9891 /* dest_high = src_low >> (word_size - C). */
9892 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9893 rshift, dest_high,
9894 extend_unsigned);
9895 if (temp != dest_high)
9896 emit_move_insn (dest_high, temp);
9898 /* dest_low = src_low << C. */
9899 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9900 treeop1, dest_low, unsignedp);
9901 if (temp != dest_low)
9902 emit_move_insn (dest_low, temp);
9904 seq = get_insns ();
9905 end_sequence ();
9906 temp = target ;
9908 if (have_insn_for (ASHIFT, int_mode))
9910 bool speed_p = optimize_insn_for_speed_p ();
9911 start_sequence ();
9912 rtx ret_old = expand_variable_shift (code, int_mode,
9913 op0, treeop1,
9914 target,
9915 unsignedp);
9917 seq_old = get_insns ();
9918 end_sequence ();
9919 if (seq_cost (seq, speed_p)
9920 >= seq_cost (seq_old, speed_p))
9922 seq = seq_old;
9923 temp = ret_old;
9926 emit_insn (seq);
9931 if (temp == NULL_RTX)
9932 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9933 unsignedp);
9934 if (code == LSHIFT_EXPR)
9935 temp = REDUCE_BIT_FIELD (temp);
9936 return temp;
9939 /* Could determine the answer when only additive constants differ. Also,
9940 the addition of one can be handled by changing the condition. */
9941 case LT_EXPR:
9942 case LE_EXPR:
9943 case GT_EXPR:
9944 case GE_EXPR:
9945 case EQ_EXPR:
9946 case NE_EXPR:
9947 case UNORDERED_EXPR:
9948 case ORDERED_EXPR:
9949 case UNLT_EXPR:
9950 case UNLE_EXPR:
9951 case UNGT_EXPR:
9952 case UNGE_EXPR:
9953 case UNEQ_EXPR:
9954 case LTGT_EXPR:
9956 temp = do_store_flag (ops,
9957 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9958 tmode != VOIDmode ? tmode : mode);
9959 if (temp)
9960 return temp;
9962 /* Use a compare and a jump for BLKmode comparisons, or for function
9963 type comparisons is have_canonicalize_funcptr_for_compare. */
9965 if ((target == 0
9966 || modifier == EXPAND_STACK_PARM
9967 || ! safe_from_p (target, treeop0, 1)
9968 || ! safe_from_p (target, treeop1, 1)
9969 /* Make sure we don't have a hard reg (such as function's return
9970 value) live across basic blocks, if not optimizing. */
9971 || (!optimize && REG_P (target)
9972 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9973 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9975 emit_move_insn (target, const0_rtx);
9977 rtx_code_label *lab1 = gen_label_rtx ();
9978 jumpifnot_1 (code, treeop0, treeop1, lab1,
9979 profile_probability::uninitialized ());
9981 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9982 emit_move_insn (target, constm1_rtx);
9983 else
9984 emit_move_insn (target, const1_rtx);
9986 emit_label (lab1);
9987 return target;
9989 case COMPLEX_EXPR:
9990 /* Get the rtx code of the operands. */
9991 op0 = expand_normal (treeop0);
9992 op1 = expand_normal (treeop1);
9994 if (!target)
9995 target = gen_reg_rtx (TYPE_MODE (type));
9996 else
9997 /* If target overlaps with op1, then either we need to force
9998 op1 into a pseudo (if target also overlaps with op0),
9999 or write the complex parts in reverse order. */
10000 switch (GET_CODE (target))
10002 case CONCAT:
10003 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10005 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10007 complex_expr_force_op1:
10008 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10009 emit_move_insn (temp, op1);
10010 op1 = temp;
10011 break;
10013 complex_expr_swap_order:
10014 /* Move the imaginary (op1) and real (op0) parts to their
10015 location. */
10016 write_complex_part (target, op1, true);
10017 write_complex_part (target, op0, false);
10019 return target;
10021 break;
10022 case MEM:
10023 temp = adjust_address_nv (target,
10024 GET_MODE_INNER (GET_MODE (target)), 0);
10025 if (reg_overlap_mentioned_p (temp, op1))
10027 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10028 temp = adjust_address_nv (target, imode,
10029 GET_MODE_SIZE (imode));
10030 if (reg_overlap_mentioned_p (temp, op0))
10031 goto complex_expr_force_op1;
10032 goto complex_expr_swap_order;
10034 break;
10035 default:
10036 if (reg_overlap_mentioned_p (target, op1))
10038 if (reg_overlap_mentioned_p (target, op0))
10039 goto complex_expr_force_op1;
10040 goto complex_expr_swap_order;
10042 break;
10045 /* Move the real (op0) and imaginary (op1) parts to their location. */
10046 write_complex_part (target, op0, false);
10047 write_complex_part (target, op1, true);
10049 return target;
10051 case WIDEN_SUM_EXPR:
10053 tree oprnd0 = treeop0;
10054 tree oprnd1 = treeop1;
10056 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10057 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10058 target, unsignedp);
10059 return target;
10062 case VEC_UNPACK_HI_EXPR:
10063 case VEC_UNPACK_LO_EXPR:
10064 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10065 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10067 op0 = expand_normal (treeop0);
10068 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10069 target, unsignedp);
10070 gcc_assert (temp);
10071 return temp;
10074 case VEC_UNPACK_FLOAT_HI_EXPR:
10075 case VEC_UNPACK_FLOAT_LO_EXPR:
10077 op0 = expand_normal (treeop0);
10078 /* The signedness is determined from input operand. */
10079 temp = expand_widen_pattern_expr
10080 (ops, op0, NULL_RTX, NULL_RTX,
10081 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10083 gcc_assert (temp);
10084 return temp;
10087 case VEC_WIDEN_PLUS_HI_EXPR:
10088 case VEC_WIDEN_PLUS_LO_EXPR:
10089 case VEC_WIDEN_MINUS_HI_EXPR:
10090 case VEC_WIDEN_MINUS_LO_EXPR:
10091 case VEC_WIDEN_MULT_HI_EXPR:
10092 case VEC_WIDEN_MULT_LO_EXPR:
10093 case VEC_WIDEN_MULT_EVEN_EXPR:
10094 case VEC_WIDEN_MULT_ODD_EXPR:
10095 case VEC_WIDEN_LSHIFT_HI_EXPR:
10096 case VEC_WIDEN_LSHIFT_LO_EXPR:
10097 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10098 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10099 target, unsignedp);
10100 gcc_assert (target);
10101 return target;
10103 case VEC_PACK_SAT_EXPR:
10104 case VEC_PACK_FIX_TRUNC_EXPR:
10105 mode = TYPE_MODE (TREE_TYPE (treeop0));
10106 subtarget = NULL_RTX;
10107 goto binop;
10109 case VEC_PACK_TRUNC_EXPR:
10110 if (VECTOR_BOOLEAN_TYPE_P (type)
10111 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10112 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10113 && SCALAR_INT_MODE_P (mode))
10115 class expand_operand eops[4];
10116 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10117 expand_operands (treeop0, treeop1,
10118 subtarget, &op0, &op1, EXPAND_NORMAL);
10119 this_optab = vec_pack_sbool_trunc_optab;
10120 enum insn_code icode = optab_handler (this_optab, imode);
10121 create_output_operand (&eops[0], target, mode);
10122 create_convert_operand_from (&eops[1], op0, imode, false);
10123 create_convert_operand_from (&eops[2], op1, imode, false);
10124 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10125 create_input_operand (&eops[3], temp, imode);
10126 expand_insn (icode, 4, eops);
10127 return eops[0].value;
10129 mode = TYPE_MODE (TREE_TYPE (treeop0));
10130 subtarget = NULL_RTX;
10131 goto binop;
10133 case VEC_PACK_FLOAT_EXPR:
10134 mode = TYPE_MODE (TREE_TYPE (treeop0));
10135 expand_operands (treeop0, treeop1,
10136 subtarget, &op0, &op1, EXPAND_NORMAL);
10137 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10138 optab_default);
10139 target = expand_binop (mode, this_optab, op0, op1, target,
10140 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10141 OPTAB_LIB_WIDEN);
10142 gcc_assert (target);
10143 return target;
10145 case VEC_PERM_EXPR:
10147 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10148 vec_perm_builder sel;
10149 if (TREE_CODE (treeop2) == VECTOR_CST
10150 && tree_to_vec_perm_builder (&sel, treeop2))
10152 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10153 temp = expand_vec_perm_const (mode, op0, op1, sel,
10154 sel_mode, target);
10156 else
10158 op2 = expand_normal (treeop2);
10159 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10161 gcc_assert (temp);
10162 return temp;
10165 case DOT_PROD_EXPR:
10167 tree oprnd0 = treeop0;
10168 tree oprnd1 = treeop1;
10169 tree oprnd2 = treeop2;
10171 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10172 op2 = expand_normal (oprnd2);
10173 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10174 target, unsignedp);
10175 return target;
10178 case SAD_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 REALIGN_LOAD_EXPR:
10193 tree oprnd0 = treeop0;
10194 tree oprnd1 = treeop1;
10195 tree oprnd2 = treeop2;
10197 this_optab = optab_for_tree_code (code, type, optab_default);
10198 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10199 op2 = expand_normal (oprnd2);
10200 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10201 target, unsignedp);
10202 gcc_assert (temp);
10203 return temp;
10206 case COND_EXPR:
10208 /* A COND_EXPR with its type being VOID_TYPE represents a
10209 conditional jump and is handled in
10210 expand_gimple_cond_expr. */
10211 gcc_assert (!VOID_TYPE_P (type));
10213 /* Note that COND_EXPRs whose type is a structure or union
10214 are required to be constructed to contain assignments of
10215 a temporary variable, so that we can evaluate them here
10216 for side effect only. If type is void, we must do likewise. */
10218 gcc_assert (!TREE_ADDRESSABLE (type)
10219 && !ignore
10220 && TREE_TYPE (treeop1) != void_type_node
10221 && TREE_TYPE (treeop2) != void_type_node);
10223 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10224 if (temp)
10225 return temp;
10227 /* If we are not to produce a result, we have no target. Otherwise,
10228 if a target was specified use it; it will not be used as an
10229 intermediate target unless it is safe. If no target, use a
10230 temporary. */
10232 if (modifier != EXPAND_STACK_PARM
10233 && original_target
10234 && safe_from_p (original_target, treeop0, 1)
10235 && GET_MODE (original_target) == mode
10236 && !MEM_P (original_target))
10237 temp = original_target;
10238 else
10239 temp = assign_temp (type, 0, 1);
10241 do_pending_stack_adjust ();
10242 NO_DEFER_POP;
10243 rtx_code_label *lab0 = gen_label_rtx ();
10244 rtx_code_label *lab1 = gen_label_rtx ();
10245 jumpifnot (treeop0, lab0,
10246 profile_probability::uninitialized ());
10247 store_expr (treeop1, temp,
10248 modifier == EXPAND_STACK_PARM,
10249 false, false);
10251 emit_jump_insn (targetm.gen_jump (lab1));
10252 emit_barrier ();
10253 emit_label (lab0);
10254 store_expr (treeop2, temp,
10255 modifier == EXPAND_STACK_PARM,
10256 false, false);
10258 emit_label (lab1);
10259 OK_DEFER_POP;
10260 return temp;
10263 case VEC_DUPLICATE_EXPR:
10264 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10265 target = expand_vector_broadcast (mode, op0);
10266 gcc_assert (target);
10267 return target;
10269 case VEC_SERIES_EXPR:
10270 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10271 return expand_vec_series_expr (mode, op0, op1, target);
10273 case BIT_INSERT_EXPR:
10275 unsigned bitpos = tree_to_uhwi (treeop2);
10276 unsigned bitsize;
10277 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10278 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10279 else
10280 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10281 op0 = expand_normal (treeop0);
10282 op1 = expand_normal (treeop1);
10283 rtx dst = gen_reg_rtx (mode);
10284 emit_move_insn (dst, op0);
10285 store_bit_field (dst, bitsize, bitpos, 0, 0,
10286 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
10287 return dst;
10290 default:
10291 gcc_unreachable ();
10294 /* Here to do an ordinary binary operator. */
10295 binop:
10296 expand_operands (treeop0, treeop1,
10297 subtarget, &op0, &op1, EXPAND_NORMAL);
10298 binop2:
10299 this_optab = optab_for_tree_code (code, type, optab_default);
10300 binop3:
10301 if (modifier == EXPAND_STACK_PARM)
10302 target = 0;
10303 temp = expand_binop (mode, this_optab, op0, op1, target,
10304 unsignedp, OPTAB_LIB_WIDEN);
10305 gcc_assert (temp);
10306 /* Bitwise operations do not need bitfield reduction as we expect their
10307 operands being properly truncated. */
10308 if (code == BIT_XOR_EXPR
10309 || code == BIT_AND_EXPR
10310 || code == BIT_IOR_EXPR)
10311 return temp;
10312 return REDUCE_BIT_FIELD (temp);
10314 #undef REDUCE_BIT_FIELD
10317 /* Return TRUE if expression STMT is suitable for replacement.
10318 Never consider memory loads as replaceable, because those don't ever lead
10319 into constant expressions. */
10321 static bool
10322 stmt_is_replaceable_p (gimple *stmt)
10324 if (ssa_is_replaceable_p (stmt))
10326 /* Don't move around loads. */
10327 if (!gimple_assign_single_p (stmt)
10328 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10329 return true;
10331 return false;
10335 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10336 enum expand_modifier modifier, rtx *alt_rtl,
10337 bool inner_reference_p)
10339 rtx op0, op1, temp, decl_rtl;
10340 tree type;
10341 int unsignedp;
10342 machine_mode mode, dmode;
10343 enum tree_code code = TREE_CODE (exp);
10344 rtx subtarget, original_target;
10345 int ignore;
10346 tree context;
10347 bool reduce_bit_field;
10348 location_t loc = EXPR_LOCATION (exp);
10349 struct separate_ops ops;
10350 tree treeop0, treeop1, treeop2;
10351 tree ssa_name = NULL_TREE;
10352 gimple *g;
10354 type = TREE_TYPE (exp);
10355 mode = TYPE_MODE (type);
10356 unsignedp = TYPE_UNSIGNED (type);
10358 treeop0 = treeop1 = treeop2 = NULL_TREE;
10359 if (!VL_EXP_CLASS_P (exp))
10360 switch (TREE_CODE_LENGTH (code))
10362 default:
10363 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10364 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10365 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10366 case 0: break;
10368 ops.code = code;
10369 ops.type = type;
10370 ops.op0 = treeop0;
10371 ops.op1 = treeop1;
10372 ops.op2 = treeop2;
10373 ops.location = loc;
10375 ignore = (target == const0_rtx
10376 || ((CONVERT_EXPR_CODE_P (code)
10377 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10378 && TREE_CODE (type) == VOID_TYPE));
10380 /* An operation in what may be a bit-field type needs the
10381 result to be reduced to the precision of the bit-field type,
10382 which is narrower than that of the type's mode. */
10383 reduce_bit_field = (!ignore
10384 && INTEGRAL_TYPE_P (type)
10385 && !type_has_mode_precision_p (type));
10387 /* If we are going to ignore this result, we need only do something
10388 if there is a side-effect somewhere in the expression. If there
10389 is, short-circuit the most common cases here. Note that we must
10390 not call expand_expr with anything but const0_rtx in case this
10391 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10393 if (ignore)
10395 if (! TREE_SIDE_EFFECTS (exp))
10396 return const0_rtx;
10398 /* Ensure we reference a volatile object even if value is ignored, but
10399 don't do this if all we are doing is taking its address. */
10400 if (TREE_THIS_VOLATILE (exp)
10401 && TREE_CODE (exp) != FUNCTION_DECL
10402 && mode != VOIDmode && mode != BLKmode
10403 && modifier != EXPAND_CONST_ADDRESS)
10405 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10406 if (MEM_P (temp))
10407 copy_to_reg (temp);
10408 return const0_rtx;
10411 if (TREE_CODE_CLASS (code) == tcc_unary
10412 || code == BIT_FIELD_REF
10413 || code == COMPONENT_REF
10414 || code == INDIRECT_REF)
10415 return expand_expr (treeop0, const0_rtx, VOIDmode,
10416 modifier);
10418 else if (TREE_CODE_CLASS (code) == tcc_binary
10419 || TREE_CODE_CLASS (code) == tcc_comparison
10420 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10422 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10423 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10424 return const0_rtx;
10427 target = 0;
10430 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10431 target = 0;
10433 /* Use subtarget as the target for operand 0 of a binary operation. */
10434 subtarget = get_subtarget (target);
10435 original_target = target;
10437 switch (code)
10439 case LABEL_DECL:
10441 tree function = decl_function_context (exp);
10443 temp = label_rtx (exp);
10444 temp = gen_rtx_LABEL_REF (Pmode, temp);
10446 if (function != current_function_decl
10447 && function != 0)
10448 LABEL_REF_NONLOCAL_P (temp) = 1;
10450 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10451 return temp;
10454 case SSA_NAME:
10455 /* ??? ivopts calls expander, without any preparation from
10456 out-of-ssa. So fake instructions as if this was an access to the
10457 base variable. This unnecessarily allocates a pseudo, see how we can
10458 reuse it, if partition base vars have it set already. */
10459 if (!currently_expanding_to_rtl)
10461 tree var = SSA_NAME_VAR (exp);
10462 if (var && DECL_RTL_SET_P (var))
10463 return DECL_RTL (var);
10464 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10465 LAST_VIRTUAL_REGISTER + 1);
10468 g = get_gimple_for_ssa_name (exp);
10469 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10470 if (g == NULL
10471 && modifier == EXPAND_INITIALIZER
10472 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10473 && (optimize || !SSA_NAME_VAR (exp)
10474 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10475 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10476 g = SSA_NAME_DEF_STMT (exp);
10477 if (g)
10479 rtx r;
10480 location_t saved_loc = curr_insn_location ();
10481 loc = gimple_location (g);
10482 if (loc != UNKNOWN_LOCATION)
10483 set_curr_insn_location (loc);
10484 ops.code = gimple_assign_rhs_code (g);
10485 switch (get_gimple_rhs_class (ops.code))
10487 case GIMPLE_TERNARY_RHS:
10488 ops.op2 = gimple_assign_rhs3 (g);
10489 /* Fallthru */
10490 case GIMPLE_BINARY_RHS:
10491 ops.op1 = gimple_assign_rhs2 (g);
10493 /* Try to expand conditonal compare. */
10494 if (targetm.gen_ccmp_first)
10496 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10497 r = expand_ccmp_expr (g, mode);
10498 if (r)
10499 break;
10501 /* Fallthru */
10502 case GIMPLE_UNARY_RHS:
10503 ops.op0 = gimple_assign_rhs1 (g);
10504 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10505 ops.location = loc;
10506 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10507 break;
10508 case GIMPLE_SINGLE_RHS:
10510 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10511 tmode, modifier, alt_rtl,
10512 inner_reference_p);
10513 break;
10515 default:
10516 gcc_unreachable ();
10518 set_curr_insn_location (saved_loc);
10519 if (REG_P (r) && !REG_EXPR (r))
10520 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10521 return r;
10524 ssa_name = exp;
10525 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10526 exp = SSA_NAME_VAR (ssa_name);
10527 goto expand_decl_rtl;
10529 case VAR_DECL:
10530 /* Allow accel compiler to handle variables that require special
10531 treatment, e.g. if they have been modified in some way earlier in
10532 compilation by the adjust_private_decl OpenACC hook. */
10533 if (flag_openacc && targetm.goacc.expand_var_decl)
10535 temp = targetm.goacc.expand_var_decl (exp);
10536 if (temp)
10537 return temp;
10539 /* ... fall through ... */
10541 case PARM_DECL:
10542 /* If a static var's type was incomplete when the decl was written,
10543 but the type is complete now, lay out the decl now. */
10544 if (DECL_SIZE (exp) == 0
10545 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10546 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10547 layout_decl (exp, 0);
10549 /* fall through */
10551 case FUNCTION_DECL:
10552 case RESULT_DECL:
10553 decl_rtl = DECL_RTL (exp);
10554 expand_decl_rtl:
10555 gcc_assert (decl_rtl);
10557 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10558 settings for VECTOR_TYPE_P that might switch for the function. */
10559 if (currently_expanding_to_rtl
10560 && code == VAR_DECL && MEM_P (decl_rtl)
10561 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10562 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10563 else
10564 decl_rtl = copy_rtx (decl_rtl);
10566 /* Record writes to register variables. */
10567 if (modifier == EXPAND_WRITE
10568 && REG_P (decl_rtl)
10569 && HARD_REGISTER_P (decl_rtl))
10570 add_to_hard_reg_set (&crtl->asm_clobbers,
10571 GET_MODE (decl_rtl), REGNO (decl_rtl));
10573 /* Ensure variable marked as used even if it doesn't go through
10574 a parser. If it hasn't be used yet, write out an external
10575 definition. */
10576 if (exp)
10577 TREE_USED (exp) = 1;
10579 /* Show we haven't gotten RTL for this yet. */
10580 temp = 0;
10582 /* Variables inherited from containing functions should have
10583 been lowered by this point. */
10584 if (exp)
10585 context = decl_function_context (exp);
10586 gcc_assert (!exp
10587 || SCOPE_FILE_SCOPE_P (context)
10588 || context == current_function_decl
10589 || TREE_STATIC (exp)
10590 || DECL_EXTERNAL (exp)
10591 /* ??? C++ creates functions that are not TREE_STATIC. */
10592 || TREE_CODE (exp) == FUNCTION_DECL);
10594 /* This is the case of an array whose size is to be determined
10595 from its initializer, while the initializer is still being parsed.
10596 ??? We aren't parsing while expanding anymore. */
10598 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10599 temp = validize_mem (decl_rtl);
10601 /* If DECL_RTL is memory, we are in the normal case and the
10602 address is not valid, get the address into a register. */
10604 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10606 if (alt_rtl)
10607 *alt_rtl = decl_rtl;
10608 decl_rtl = use_anchored_address (decl_rtl);
10609 if (modifier != EXPAND_CONST_ADDRESS
10610 && modifier != EXPAND_SUM
10611 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10612 : GET_MODE (decl_rtl),
10613 XEXP (decl_rtl, 0),
10614 MEM_ADDR_SPACE (decl_rtl)))
10615 temp = replace_equiv_address (decl_rtl,
10616 copy_rtx (XEXP (decl_rtl, 0)));
10619 /* If we got something, return it. But first, set the alignment
10620 if the address is a register. */
10621 if (temp != 0)
10623 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10624 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10626 else if (MEM_P (decl_rtl))
10627 temp = decl_rtl;
10629 if (temp != 0)
10631 if (MEM_P (temp)
10632 && modifier != EXPAND_WRITE
10633 && modifier != EXPAND_MEMORY
10634 && modifier != EXPAND_INITIALIZER
10635 && modifier != EXPAND_CONST_ADDRESS
10636 && modifier != EXPAND_SUM
10637 && !inner_reference_p
10638 && mode != BLKmode
10639 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10640 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10641 MEM_ALIGN (temp), NULL_RTX, NULL);
10643 return temp;
10646 if (exp)
10647 dmode = DECL_MODE (exp);
10648 else
10649 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10651 /* If the mode of DECL_RTL does not match that of the decl,
10652 there are two cases: we are dealing with a BLKmode value
10653 that is returned in a register, or we are dealing with
10654 a promoted value. In the latter case, return a SUBREG
10655 of the wanted mode, but mark it so that we know that it
10656 was already extended. */
10657 if (REG_P (decl_rtl)
10658 && dmode != BLKmode
10659 && GET_MODE (decl_rtl) != dmode)
10661 machine_mode pmode;
10663 /* Get the signedness to be used for this variable. Ensure we get
10664 the same mode we got when the variable was declared. */
10665 if (code != SSA_NAME)
10666 pmode = promote_decl_mode (exp, &unsignedp);
10667 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10668 && gimple_code (g) == GIMPLE_CALL
10669 && !gimple_call_internal_p (g))
10670 pmode = promote_function_mode (type, mode, &unsignedp,
10671 gimple_call_fntype (g),
10673 else
10674 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10675 gcc_assert (GET_MODE (decl_rtl) == pmode);
10677 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10678 SUBREG_PROMOTED_VAR_P (temp) = 1;
10679 SUBREG_PROMOTED_SET (temp, unsignedp);
10680 return temp;
10683 return decl_rtl;
10685 case INTEGER_CST:
10687 /* Given that TYPE_PRECISION (type) is not always equal to
10688 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10689 the former to the latter according to the signedness of the
10690 type. */
10691 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10692 temp = immed_wide_int_const
10693 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10694 return temp;
10697 case VECTOR_CST:
10699 tree tmp = NULL_TREE;
10700 if (VECTOR_MODE_P (mode))
10701 return const_vector_from_tree (exp);
10702 scalar_int_mode int_mode;
10703 if (is_int_mode (mode, &int_mode))
10705 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10706 if (type_for_mode)
10707 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10708 type_for_mode, exp);
10710 if (!tmp)
10712 vec<constructor_elt, va_gc> *v;
10713 /* Constructors need to be fixed-length. FIXME. */
10714 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10715 vec_alloc (v, nunits);
10716 for (unsigned int i = 0; i < nunits; ++i)
10717 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10718 tmp = build_constructor (type, v);
10720 return expand_expr (tmp, ignore ? const0_rtx : target,
10721 tmode, modifier);
10724 case CONST_DECL:
10725 if (modifier == EXPAND_WRITE)
10727 /* Writing into CONST_DECL is always invalid, but handle it
10728 gracefully. */
10729 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10730 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10731 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10732 EXPAND_NORMAL, as);
10733 op0 = memory_address_addr_space (mode, op0, as);
10734 temp = gen_rtx_MEM (mode, op0);
10735 set_mem_addr_space (temp, as);
10736 return temp;
10738 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10740 case REAL_CST:
10741 /* If optimized, generate immediate CONST_DOUBLE
10742 which will be turned into memory by reload if necessary.
10744 We used to force a register so that loop.c could see it. But
10745 this does not allow gen_* patterns to perform optimizations with
10746 the constants. It also produces two insns in cases like "x = 1.0;".
10747 On most machines, floating-point constants are not permitted in
10748 many insns, so we'd end up copying it to a register in any case.
10750 Now, we do the copying in expand_binop, if appropriate. */
10751 return const_double_from_real_value (TREE_REAL_CST (exp),
10752 TYPE_MODE (TREE_TYPE (exp)));
10754 case FIXED_CST:
10755 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10756 TYPE_MODE (TREE_TYPE (exp)));
10758 case COMPLEX_CST:
10759 /* Handle evaluating a complex constant in a CONCAT target. */
10760 if (original_target && GET_CODE (original_target) == CONCAT)
10762 rtx rtarg, itarg;
10764 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10765 rtarg = XEXP (original_target, 0);
10766 itarg = XEXP (original_target, 1);
10768 /* Move the real and imaginary parts separately. */
10769 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10770 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10772 if (op0 != rtarg)
10773 emit_move_insn (rtarg, op0);
10774 if (op1 != itarg)
10775 emit_move_insn (itarg, op1);
10777 return original_target;
10780 /* fall through */
10782 case STRING_CST:
10783 temp = expand_expr_constant (exp, 1, modifier);
10785 /* temp contains a constant address.
10786 On RISC machines where a constant address isn't valid,
10787 make some insns to get that address into a register. */
10788 if (modifier != EXPAND_CONST_ADDRESS
10789 && modifier != EXPAND_INITIALIZER
10790 && modifier != EXPAND_SUM
10791 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10792 MEM_ADDR_SPACE (temp)))
10793 return replace_equiv_address (temp,
10794 copy_rtx (XEXP (temp, 0)));
10795 return temp;
10797 case POLY_INT_CST:
10798 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10800 case SAVE_EXPR:
10802 tree val = treeop0;
10803 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10804 inner_reference_p);
10806 if (!SAVE_EXPR_RESOLVED_P (exp))
10808 /* We can indeed still hit this case, typically via builtin
10809 expanders calling save_expr immediately before expanding
10810 something. Assume this means that we only have to deal
10811 with non-BLKmode values. */
10812 gcc_assert (GET_MODE (ret) != BLKmode);
10814 val = build_decl (curr_insn_location (),
10815 VAR_DECL, NULL, TREE_TYPE (exp));
10816 DECL_ARTIFICIAL (val) = 1;
10817 DECL_IGNORED_P (val) = 1;
10818 treeop0 = val;
10819 TREE_OPERAND (exp, 0) = treeop0;
10820 SAVE_EXPR_RESOLVED_P (exp) = 1;
10822 if (!CONSTANT_P (ret))
10823 ret = copy_to_reg (ret);
10824 SET_DECL_RTL (val, ret);
10827 return ret;
10831 case CONSTRUCTOR:
10832 /* If we don't need the result, just ensure we evaluate any
10833 subexpressions. */
10834 if (ignore)
10836 unsigned HOST_WIDE_INT idx;
10837 tree value;
10839 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10840 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10842 return const0_rtx;
10845 return expand_constructor (exp, target, modifier, false);
10847 case TARGET_MEM_REF:
10849 addr_space_t as
10850 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10851 unsigned int align;
10853 op0 = addr_for_mem_ref (exp, as, true);
10854 op0 = memory_address_addr_space (mode, op0, as);
10855 temp = gen_rtx_MEM (mode, op0);
10856 set_mem_attributes (temp, exp, 0);
10857 set_mem_addr_space (temp, as);
10858 align = get_object_alignment (exp);
10859 if (modifier != EXPAND_WRITE
10860 && modifier != EXPAND_MEMORY
10861 && mode != BLKmode
10862 && align < GET_MODE_ALIGNMENT (mode))
10863 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10864 align, NULL_RTX, NULL);
10865 return temp;
10868 case MEM_REF:
10870 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10871 addr_space_t as
10872 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10873 machine_mode address_mode;
10874 tree base = TREE_OPERAND (exp, 0);
10875 gimple *def_stmt;
10876 unsigned align;
10877 /* Handle expansion of non-aliased memory with non-BLKmode. That
10878 might end up in a register. */
10879 if (mem_ref_refers_to_non_mem_p (exp))
10881 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10882 base = TREE_OPERAND (base, 0);
10883 poly_uint64 type_size;
10884 if (known_eq (offset, 0)
10885 && !reverse
10886 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10887 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10888 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10889 target, tmode, modifier);
10890 if (TYPE_MODE (type) == BLKmode)
10892 temp = assign_stack_temp (DECL_MODE (base),
10893 GET_MODE_SIZE (DECL_MODE (base)));
10894 store_expr (base, temp, 0, false, false);
10895 temp = adjust_address (temp, BLKmode, offset);
10896 set_mem_size (temp, int_size_in_bytes (type));
10897 return temp;
10899 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10900 bitsize_int (offset * BITS_PER_UNIT));
10901 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10902 return expand_expr (exp, target, tmode, modifier);
10904 address_mode = targetm.addr_space.address_mode (as);
10905 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10907 tree mask = gimple_assign_rhs2 (def_stmt);
10908 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10909 gimple_assign_rhs1 (def_stmt), mask);
10910 TREE_OPERAND (exp, 0) = base;
10912 align = get_object_alignment (exp);
10913 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10914 op0 = memory_address_addr_space (mode, op0, as);
10915 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10917 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10918 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10919 op0 = memory_address_addr_space (mode, op0, as);
10921 temp = gen_rtx_MEM (mode, op0);
10922 set_mem_attributes (temp, exp, 0);
10923 set_mem_addr_space (temp, as);
10924 if (TREE_THIS_VOLATILE (exp))
10925 MEM_VOLATILE_P (temp) = 1;
10926 if (modifier != EXPAND_WRITE
10927 && modifier != EXPAND_MEMORY
10928 && !inner_reference_p
10929 && mode != BLKmode
10930 && align < GET_MODE_ALIGNMENT (mode))
10931 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
10932 modifier == EXPAND_STACK_PARM
10933 ? NULL_RTX : target, alt_rtl);
10934 if (reverse
10935 && modifier != EXPAND_MEMORY
10936 && modifier != EXPAND_WRITE)
10937 temp = flip_storage_order (mode, temp);
10938 return temp;
10941 case ARRAY_REF:
10944 tree array = treeop0;
10945 tree index = treeop1;
10946 tree init;
10948 /* Fold an expression like: "foo"[2].
10949 This is not done in fold so it won't happen inside &.
10950 Don't fold if this is for wide characters since it's too
10951 difficult to do correctly and this is a very rare case. */
10953 if (modifier != EXPAND_CONST_ADDRESS
10954 && modifier != EXPAND_INITIALIZER
10955 && modifier != EXPAND_MEMORY)
10957 tree t = fold_read_from_constant_string (exp);
10959 if (t)
10960 return expand_expr (t, target, tmode, modifier);
10963 /* If this is a constant index into a constant array,
10964 just get the value from the array. Handle both the cases when
10965 we have an explicit constructor and when our operand is a variable
10966 that was declared const. */
10968 if (modifier != EXPAND_CONST_ADDRESS
10969 && modifier != EXPAND_INITIALIZER
10970 && modifier != EXPAND_MEMORY
10971 && TREE_CODE (array) == CONSTRUCTOR
10972 && ! TREE_SIDE_EFFECTS (array)
10973 && TREE_CODE (index) == INTEGER_CST)
10975 unsigned HOST_WIDE_INT ix;
10976 tree field, value;
10978 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10979 field, value)
10980 if (tree_int_cst_equal (field, index))
10982 if (!TREE_SIDE_EFFECTS (value))
10983 return expand_expr (fold (value), target, tmode, modifier);
10984 break;
10988 else if (optimize >= 1
10989 && modifier != EXPAND_CONST_ADDRESS
10990 && modifier != EXPAND_INITIALIZER
10991 && modifier != EXPAND_MEMORY
10992 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10993 && TREE_CODE (index) == INTEGER_CST
10994 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10995 && (init = ctor_for_folding (array)) != error_mark_node)
10997 if (init == NULL_TREE)
10999 tree value = build_zero_cst (type);
11000 if (TREE_CODE (value) == CONSTRUCTOR)
11002 /* If VALUE is a CONSTRUCTOR, this optimization is only
11003 useful if this doesn't store the CONSTRUCTOR into
11004 memory. If it does, it is more efficient to just
11005 load the data from the array directly. */
11006 rtx ret = expand_constructor (value, target,
11007 modifier, true);
11008 if (ret == NULL_RTX)
11009 value = NULL_TREE;
11012 if (value)
11013 return expand_expr (value, target, tmode, modifier);
11015 else if (TREE_CODE (init) == CONSTRUCTOR)
11017 unsigned HOST_WIDE_INT ix;
11018 tree field, value;
11020 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11021 field, value)
11022 if (tree_int_cst_equal (field, index))
11024 if (TREE_SIDE_EFFECTS (value))
11025 break;
11027 if (TREE_CODE (value) == CONSTRUCTOR)
11029 /* If VALUE is a CONSTRUCTOR, this
11030 optimization is only useful if
11031 this doesn't store the CONSTRUCTOR
11032 into memory. If it does, it is more
11033 efficient to just load the data from
11034 the array directly. */
11035 rtx ret = expand_constructor (value, target,
11036 modifier, true);
11037 if (ret == NULL_RTX)
11038 break;
11041 return
11042 expand_expr (fold (value), target, tmode, modifier);
11045 else if (TREE_CODE (init) == STRING_CST)
11047 tree low_bound = array_ref_low_bound (exp);
11048 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11050 /* Optimize the special case of a zero lower bound.
11052 We convert the lower bound to sizetype to avoid problems
11053 with constant folding. E.g. suppose the lower bound is
11054 1 and its mode is QI. Without the conversion
11055 (ARRAY + (INDEX - (unsigned char)1))
11056 becomes
11057 (ARRAY + (-(unsigned char)1) + INDEX)
11058 which becomes
11059 (ARRAY + 255 + INDEX). Oops! */
11060 if (!integer_zerop (low_bound))
11061 index1 = size_diffop_loc (loc, index1,
11062 fold_convert_loc (loc, sizetype,
11063 low_bound));
11065 if (tree_fits_uhwi_p (index1)
11066 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11068 tree char_type = TREE_TYPE (TREE_TYPE (init));
11069 scalar_int_mode char_mode;
11071 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11072 && GET_MODE_SIZE (char_mode) == 1)
11073 return gen_int_mode (TREE_STRING_POINTER (init)
11074 [TREE_INT_CST_LOW (index1)],
11075 char_mode);
11080 goto normal_inner_ref;
11082 case COMPONENT_REF:
11083 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11084 /* Fall through. */
11085 case BIT_FIELD_REF:
11086 case ARRAY_RANGE_REF:
11087 normal_inner_ref:
11089 machine_mode mode1, mode2;
11090 poly_int64 bitsize, bitpos, bytepos;
11091 tree offset;
11092 int reversep, volatilep = 0, must_force_mem;
11093 tree tem
11094 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11095 &unsignedp, &reversep, &volatilep);
11096 rtx orig_op0, memloc;
11097 bool clear_mem_expr = false;
11099 /* If we got back the original object, something is wrong. Perhaps
11100 we are evaluating an expression too early. In any event, don't
11101 infinitely recurse. */
11102 gcc_assert (tem != exp);
11104 /* If TEM's type is a union of variable size, pass TARGET to the inner
11105 computation, since it will need a temporary and TARGET is known
11106 to have to do. This occurs in unchecked conversion in Ada. */
11107 orig_op0 = op0
11108 = expand_expr_real (tem,
11109 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11110 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11111 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11112 != INTEGER_CST)
11113 && modifier != EXPAND_STACK_PARM
11114 ? target : NULL_RTX),
11115 VOIDmode,
11116 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11117 NULL, true);
11119 /* If the field has a mode, we want to access it in the
11120 field's mode, not the computed mode.
11121 If a MEM has VOIDmode (external with incomplete type),
11122 use BLKmode for it instead. */
11123 if (MEM_P (op0))
11125 if (mode1 != VOIDmode)
11126 op0 = adjust_address (op0, mode1, 0);
11127 else if (GET_MODE (op0) == VOIDmode)
11128 op0 = adjust_address (op0, BLKmode, 0);
11131 mode2
11132 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11134 /* Make sure bitpos is not negative, it can wreak havoc later. */
11135 if (maybe_lt (bitpos, 0))
11137 gcc_checking_assert (offset == NULL_TREE);
11138 offset = size_int (bits_to_bytes_round_down (bitpos));
11139 bitpos = num_trailing_bits (bitpos);
11142 /* If we have either an offset, a BLKmode result, or a reference
11143 outside the underlying object, we must force it to memory.
11144 Such a case can occur in Ada if we have unchecked conversion
11145 of an expression from a scalar type to an aggregate type or
11146 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11147 passed a partially uninitialized object or a view-conversion
11148 to a larger size. */
11149 must_force_mem = (offset
11150 || mode1 == BLKmode
11151 || (mode == BLKmode
11152 && !int_mode_for_size (bitsize, 1).exists ())
11153 || maybe_gt (bitpos + bitsize,
11154 GET_MODE_BITSIZE (mode2)));
11156 /* Handle CONCAT first. */
11157 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11159 if (known_eq (bitpos, 0)
11160 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11161 && COMPLEX_MODE_P (mode1)
11162 && COMPLEX_MODE_P (GET_MODE (op0))
11163 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11164 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11166 if (reversep)
11167 op0 = flip_storage_order (GET_MODE (op0), op0);
11168 if (mode1 != GET_MODE (op0))
11170 rtx parts[2];
11171 for (int i = 0; i < 2; i++)
11173 rtx op = read_complex_part (op0, i != 0);
11174 if (GET_CODE (op) == SUBREG)
11175 op = force_reg (GET_MODE (op), op);
11176 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11177 if (temp)
11178 op = temp;
11179 else
11181 if (!REG_P (op) && !MEM_P (op))
11182 op = force_reg (GET_MODE (op), op);
11183 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11185 parts[i] = op;
11187 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11189 return op0;
11191 if (known_eq (bitpos, 0)
11192 && known_eq (bitsize,
11193 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11194 && maybe_ne (bitsize, 0))
11196 op0 = XEXP (op0, 0);
11197 mode2 = GET_MODE (op0);
11199 else if (known_eq (bitpos,
11200 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11201 && known_eq (bitsize,
11202 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11203 && maybe_ne (bitpos, 0)
11204 && maybe_ne (bitsize, 0))
11206 op0 = XEXP (op0, 1);
11207 bitpos = 0;
11208 mode2 = GET_MODE (op0);
11210 else
11211 /* Otherwise force into memory. */
11212 must_force_mem = 1;
11215 /* If this is a constant, put it in a register if it is a legitimate
11216 constant and we don't need a memory reference. */
11217 if (CONSTANT_P (op0)
11218 && mode2 != BLKmode
11219 && targetm.legitimate_constant_p (mode2, op0)
11220 && !must_force_mem)
11221 op0 = force_reg (mode2, op0);
11223 /* Otherwise, if this is a constant, try to force it to the constant
11224 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11225 is a legitimate constant. */
11226 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11227 op0 = validize_mem (memloc);
11229 /* Otherwise, if this is a constant or the object is not in memory
11230 and need be, put it there. */
11231 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11233 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11234 emit_move_insn (memloc, op0);
11235 op0 = memloc;
11236 clear_mem_expr = true;
11239 if (offset)
11241 machine_mode address_mode;
11242 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11243 EXPAND_SUM);
11245 gcc_assert (MEM_P (op0));
11247 address_mode = get_address_mode (op0);
11248 if (GET_MODE (offset_rtx) != address_mode)
11250 /* We cannot be sure that the RTL in offset_rtx is valid outside
11251 of a memory address context, so force it into a register
11252 before attempting to convert it to the desired mode. */
11253 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11254 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11257 /* See the comment in expand_assignment for the rationale. */
11258 if (mode1 != VOIDmode
11259 && maybe_ne (bitpos, 0)
11260 && maybe_gt (bitsize, 0)
11261 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11262 && multiple_p (bitpos, bitsize)
11263 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11264 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11266 op0 = adjust_address (op0, mode1, bytepos);
11267 bitpos = 0;
11270 op0 = offset_address (op0, offset_rtx,
11271 highest_pow2_factor (offset));
11274 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11275 record its alignment as BIGGEST_ALIGNMENT. */
11276 if (MEM_P (op0)
11277 && known_eq (bitpos, 0)
11278 && offset != 0
11279 && is_aligning_offset (offset, tem))
11280 set_mem_align (op0, BIGGEST_ALIGNMENT);
11282 /* Don't forget about volatility even if this is a bitfield. */
11283 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11285 if (op0 == orig_op0)
11286 op0 = copy_rtx (op0);
11288 MEM_VOLATILE_P (op0) = 1;
11291 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11293 if (op0 == orig_op0)
11294 op0 = copy_rtx (op0);
11296 set_mem_align (op0, BITS_PER_UNIT);
11299 /* In cases where an aligned union has an unaligned object
11300 as a field, we might be extracting a BLKmode value from
11301 an integer-mode (e.g., SImode) object. Handle this case
11302 by doing the extract into an object as wide as the field
11303 (which we know to be the width of a basic mode), then
11304 storing into memory, and changing the mode to BLKmode. */
11305 if (mode1 == VOIDmode
11306 || REG_P (op0) || GET_CODE (op0) == SUBREG
11307 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11308 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11309 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11310 && modifier != EXPAND_CONST_ADDRESS
11311 && modifier != EXPAND_INITIALIZER
11312 && modifier != EXPAND_MEMORY)
11313 /* If the bitfield is volatile and the bitsize
11314 is narrower than the access size of the bitfield,
11315 we need to extract bitfields from the access. */
11316 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11317 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11318 && mode1 != BLKmode
11319 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11320 /* If the field isn't aligned enough to fetch as a memref,
11321 fetch it as a bit field. */
11322 || (mode1 != BLKmode
11323 && (((MEM_P (op0)
11324 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11325 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11326 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11327 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11328 && modifier != EXPAND_MEMORY
11329 && ((modifier == EXPAND_CONST_ADDRESS
11330 || modifier == EXPAND_INITIALIZER)
11331 ? STRICT_ALIGNMENT
11332 : targetm.slow_unaligned_access (mode1,
11333 MEM_ALIGN (op0))))
11334 || !multiple_p (bitpos, BITS_PER_UNIT)))
11335 /* If the type and the field are a constant size and the
11336 size of the type isn't the same size as the bitfield,
11337 we must use bitfield operations. */
11338 || (known_size_p (bitsize)
11339 && TYPE_SIZE (TREE_TYPE (exp))
11340 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11341 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11342 bitsize)))
11344 machine_mode ext_mode = mode;
11346 if (ext_mode == BLKmode
11347 && ! (target != 0 && MEM_P (op0)
11348 && MEM_P (target)
11349 && multiple_p (bitpos, BITS_PER_UNIT)))
11350 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11352 if (ext_mode == BLKmode)
11354 if (target == 0)
11355 target = assign_temp (type, 1, 1);
11357 /* ??? Unlike the similar test a few lines below, this one is
11358 very likely obsolete. */
11359 if (known_eq (bitsize, 0))
11360 return target;
11362 /* In this case, BITPOS must start at a byte boundary and
11363 TARGET, if specified, must be a MEM. */
11364 gcc_assert (MEM_P (op0)
11365 && (!target || MEM_P (target)));
11367 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11368 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11369 emit_block_move (target,
11370 adjust_address (op0, VOIDmode, bytepos),
11371 gen_int_mode (bytesize, Pmode),
11372 (modifier == EXPAND_STACK_PARM
11373 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11375 return target;
11378 /* If we have nothing to extract, the result will be 0 for targets
11379 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11380 return 0 for the sake of consistency, as reading a zero-sized
11381 bitfield is valid in Ada and the value is fully specified. */
11382 if (known_eq (bitsize, 0))
11383 return const0_rtx;
11385 op0 = validize_mem (op0);
11387 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11388 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11390 /* If the result has aggregate type and the extraction is done in
11391 an integral mode, then the field may be not aligned on a byte
11392 boundary; in this case, if it has reverse storage order, it
11393 needs to be extracted as a scalar field with reverse storage
11394 order and put back into memory order afterwards. */
11395 if (AGGREGATE_TYPE_P (type)
11396 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11397 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11399 gcc_checking_assert (known_ge (bitpos, 0));
11400 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11401 (modifier == EXPAND_STACK_PARM
11402 ? NULL_RTX : target),
11403 ext_mode, ext_mode, reversep, alt_rtl);
11405 /* If the result has aggregate type and the mode of OP0 is an
11406 integral mode then, if BITSIZE is narrower than this mode
11407 and this is for big-endian data, we must put the field
11408 into the high-order bits. And we must also put it back
11409 into memory order if it has been previously reversed. */
11410 scalar_int_mode op0_mode;
11411 if (AGGREGATE_TYPE_P (type)
11412 && is_int_mode (GET_MODE (op0), &op0_mode))
11414 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11416 gcc_checking_assert (known_le (bitsize, size));
11417 if (maybe_lt (bitsize, size)
11418 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11419 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11420 size - bitsize, op0, 1);
11422 if (reversep)
11423 op0 = flip_storage_order (op0_mode, op0);
11426 /* If the result type is BLKmode, store the data into a temporary
11427 of the appropriate type, but with the mode corresponding to the
11428 mode for the data we have (op0's mode). */
11429 if (mode == BLKmode)
11431 rtx new_rtx
11432 = assign_stack_temp_for_type (ext_mode,
11433 GET_MODE_BITSIZE (ext_mode),
11434 type);
11435 emit_move_insn (new_rtx, op0);
11436 op0 = copy_rtx (new_rtx);
11437 PUT_MODE (op0, BLKmode);
11440 return op0;
11443 /* If the result is BLKmode, use that to access the object
11444 now as well. */
11445 if (mode == BLKmode)
11446 mode1 = BLKmode;
11448 /* Get a reference to just this component. */
11449 bytepos = bits_to_bytes_round_down (bitpos);
11450 if (modifier == EXPAND_CONST_ADDRESS
11451 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11452 op0 = adjust_address_nv (op0, mode1, bytepos);
11453 else
11454 op0 = adjust_address (op0, mode1, bytepos);
11456 if (op0 == orig_op0)
11457 op0 = copy_rtx (op0);
11459 /* Don't set memory attributes if the base expression is
11460 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11461 we should just honor its original memory attributes. */
11462 if (!(TREE_CODE (tem) == SSA_NAME
11463 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11464 set_mem_attributes (op0, exp, 0);
11466 if (REG_P (XEXP (op0, 0)))
11467 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11469 /* If op0 is a temporary because the original expressions was forced
11470 to memory, clear MEM_EXPR so that the original expression cannot
11471 be marked as addressable through MEM_EXPR of the temporary. */
11472 if (clear_mem_expr)
11473 set_mem_expr (op0, NULL_TREE);
11475 MEM_VOLATILE_P (op0) |= volatilep;
11477 if (reversep
11478 && modifier != EXPAND_MEMORY
11479 && modifier != EXPAND_WRITE)
11480 op0 = flip_storage_order (mode1, op0);
11482 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11483 || modifier == EXPAND_CONST_ADDRESS
11484 || modifier == EXPAND_INITIALIZER)
11485 return op0;
11487 if (target == 0)
11488 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11490 convert_move (target, op0, unsignedp);
11491 return target;
11494 case OBJ_TYPE_REF:
11495 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11497 case CALL_EXPR:
11498 /* All valid uses of __builtin_va_arg_pack () are removed during
11499 inlining. */
11500 if (CALL_EXPR_VA_ARG_PACK (exp))
11501 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11503 tree fndecl = get_callee_fndecl (exp), attr;
11505 if (fndecl
11506 /* Don't diagnose the error attribute in thunks, those are
11507 artificially created. */
11508 && !CALL_FROM_THUNK_P (exp)
11509 && (attr = lookup_attribute ("error",
11510 DECL_ATTRIBUTES (fndecl))) != NULL)
11512 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11513 error ("call to %qs declared with attribute error: %s",
11514 identifier_to_locale (ident),
11515 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11517 if (fndecl
11518 /* Don't diagnose the warning attribute in thunks, those are
11519 artificially created. */
11520 && !CALL_FROM_THUNK_P (exp)
11521 && (attr = lookup_attribute ("warning",
11522 DECL_ATTRIBUTES (fndecl))) != NULL)
11524 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11525 warning_at (EXPR_LOCATION (exp),
11526 OPT_Wattribute_warning,
11527 "call to %qs declared with attribute warning: %s",
11528 identifier_to_locale (ident),
11529 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11532 /* Check for a built-in function. */
11533 if (fndecl && fndecl_built_in_p (fndecl))
11535 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11536 return expand_builtin (exp, target, subtarget, tmode, ignore);
11539 return expand_call (exp, target, ignore);
11541 case VIEW_CONVERT_EXPR:
11542 op0 = NULL_RTX;
11544 /* If we are converting to BLKmode, try to avoid an intermediate
11545 temporary by fetching an inner memory reference. */
11546 if (mode == BLKmode
11547 && poly_int_tree_p (TYPE_SIZE (type))
11548 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11549 && handled_component_p (treeop0))
11551 machine_mode mode1;
11552 poly_int64 bitsize, bitpos, bytepos;
11553 tree offset;
11554 int reversep, volatilep = 0;
11555 tree tem
11556 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11557 &unsignedp, &reversep, &volatilep);
11559 /* ??? We should work harder and deal with non-zero offsets. */
11560 if (!offset
11561 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11562 && !reversep
11563 && known_size_p (bitsize)
11564 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11566 /* See the normal_inner_ref case for the rationale. */
11567 rtx orig_op0
11568 = expand_expr_real (tem,
11569 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11570 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11571 != INTEGER_CST)
11572 && modifier != EXPAND_STACK_PARM
11573 ? target : NULL_RTX),
11574 VOIDmode,
11575 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11576 NULL, true);
11578 if (MEM_P (orig_op0))
11580 op0 = orig_op0;
11582 /* Get a reference to just this component. */
11583 if (modifier == EXPAND_CONST_ADDRESS
11584 || modifier == EXPAND_SUM
11585 || modifier == EXPAND_INITIALIZER)
11586 op0 = adjust_address_nv (op0, mode, bytepos);
11587 else
11588 op0 = adjust_address (op0, mode, bytepos);
11590 if (op0 == orig_op0)
11591 op0 = copy_rtx (op0);
11593 set_mem_attributes (op0, treeop0, 0);
11594 if (REG_P (XEXP (op0, 0)))
11595 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11597 MEM_VOLATILE_P (op0) |= volatilep;
11602 if (!op0)
11603 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11604 NULL, inner_reference_p);
11606 /* If the input and output modes are both the same, we are done. */
11607 if (mode == GET_MODE (op0))
11609 /* If neither mode is BLKmode, and both modes are the same size
11610 then we can use gen_lowpart. */
11611 else if (mode != BLKmode
11612 && GET_MODE (op0) != BLKmode
11613 && known_eq (GET_MODE_PRECISION (mode),
11614 GET_MODE_PRECISION (GET_MODE (op0)))
11615 && !COMPLEX_MODE_P (GET_MODE (op0)))
11617 if (GET_CODE (op0) == SUBREG)
11618 op0 = force_reg (GET_MODE (op0), op0);
11619 temp = gen_lowpart_common (mode, op0);
11620 if (temp)
11621 op0 = temp;
11622 else
11624 if (!REG_P (op0) && !MEM_P (op0))
11625 op0 = force_reg (GET_MODE (op0), op0);
11626 op0 = gen_lowpart (mode, op0);
11629 /* If both types are integral, convert from one mode to the other. */
11630 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11631 op0 = convert_modes (mode, GET_MODE (op0), op0,
11632 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11633 /* If the output type is a bit-field type, do an extraction. */
11634 else if (reduce_bit_field)
11635 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11636 TYPE_UNSIGNED (type), NULL_RTX,
11637 mode, mode, false, NULL);
11638 /* As a last resort, spill op0 to memory, and reload it in a
11639 different mode. */
11640 else if (!MEM_P (op0))
11642 /* If the operand is not a MEM, force it into memory. Since we
11643 are going to be changing the mode of the MEM, don't call
11644 force_const_mem for constants because we don't allow pool
11645 constants to change mode. */
11646 tree inner_type = TREE_TYPE (treeop0);
11648 gcc_assert (!TREE_ADDRESSABLE (exp));
11650 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11651 target
11652 = assign_stack_temp_for_type
11653 (TYPE_MODE (inner_type),
11654 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11656 emit_move_insn (target, op0);
11657 op0 = target;
11660 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11661 output type is such that the operand is known to be aligned, indicate
11662 that it is. Otherwise, we need only be concerned about alignment for
11663 non-BLKmode results. */
11664 if (MEM_P (op0))
11666 enum insn_code icode;
11668 if (modifier != EXPAND_WRITE
11669 && modifier != EXPAND_MEMORY
11670 && !inner_reference_p
11671 && mode != BLKmode
11672 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11674 /* If the target does have special handling for unaligned
11675 loads of mode then use them. */
11676 if ((icode = optab_handler (movmisalign_optab, mode))
11677 != CODE_FOR_nothing)
11679 rtx reg;
11681 op0 = adjust_address (op0, mode, 0);
11682 /* We've already validated the memory, and we're creating a
11683 new pseudo destination. The predicates really can't
11684 fail. */
11685 reg = gen_reg_rtx (mode);
11687 /* Nor can the insn generator. */
11688 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11689 emit_insn (insn);
11690 return reg;
11692 else if (STRICT_ALIGNMENT)
11694 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11695 poly_uint64 temp_size = mode_size;
11696 if (GET_MODE (op0) != BLKmode)
11697 temp_size = upper_bound (temp_size,
11698 GET_MODE_SIZE (GET_MODE (op0)));
11699 rtx new_rtx
11700 = assign_stack_temp_for_type (mode, temp_size, type);
11701 rtx new_with_op0_mode
11702 = adjust_address (new_rtx, GET_MODE (op0), 0);
11704 gcc_assert (!TREE_ADDRESSABLE (exp));
11706 if (GET_MODE (op0) == BLKmode)
11708 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11709 emit_block_move (new_with_op0_mode, op0, size_rtx,
11710 (modifier == EXPAND_STACK_PARM
11711 ? BLOCK_OP_CALL_PARM
11712 : BLOCK_OP_NORMAL));
11714 else
11715 emit_move_insn (new_with_op0_mode, op0);
11717 op0 = new_rtx;
11721 op0 = adjust_address (op0, mode, 0);
11724 return op0;
11726 case MODIFY_EXPR:
11728 tree lhs = treeop0;
11729 tree rhs = treeop1;
11730 gcc_assert (ignore);
11732 /* Check for |= or &= of a bitfield of size one into another bitfield
11733 of size 1. In this case, (unless we need the result of the
11734 assignment) we can do this more efficiently with a
11735 test followed by an assignment, if necessary.
11737 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11738 things change so we do, this code should be enhanced to
11739 support it. */
11740 if (TREE_CODE (lhs) == COMPONENT_REF
11741 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11742 || TREE_CODE (rhs) == BIT_AND_EXPR)
11743 && TREE_OPERAND (rhs, 0) == lhs
11744 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11745 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11746 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11748 rtx_code_label *label = gen_label_rtx ();
11749 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11750 profile_probability prob = profile_probability::uninitialized ();
11751 if (value)
11752 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11753 else
11754 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11755 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11756 false);
11757 do_pending_stack_adjust ();
11758 emit_label (label);
11759 return const0_rtx;
11762 expand_assignment (lhs, rhs, false);
11763 return const0_rtx;
11766 case ADDR_EXPR:
11767 return expand_expr_addr_expr (exp, target, tmode, modifier);
11769 case REALPART_EXPR:
11770 op0 = expand_normal (treeop0);
11771 return read_complex_part (op0, false);
11773 case IMAGPART_EXPR:
11774 op0 = expand_normal (treeop0);
11775 return read_complex_part (op0, true);
11777 case RETURN_EXPR:
11778 case LABEL_EXPR:
11779 case GOTO_EXPR:
11780 case SWITCH_EXPR:
11781 case ASM_EXPR:
11782 /* Expanded in cfgexpand.c. */
11783 gcc_unreachable ();
11785 case TRY_CATCH_EXPR:
11786 case CATCH_EXPR:
11787 case EH_FILTER_EXPR:
11788 case TRY_FINALLY_EXPR:
11789 case EH_ELSE_EXPR:
11790 /* Lowered by tree-eh.c. */
11791 gcc_unreachable ();
11793 case WITH_CLEANUP_EXPR:
11794 case CLEANUP_POINT_EXPR:
11795 case TARGET_EXPR:
11796 case CASE_LABEL_EXPR:
11797 case VA_ARG_EXPR:
11798 case BIND_EXPR:
11799 case INIT_EXPR:
11800 case CONJ_EXPR:
11801 case COMPOUND_EXPR:
11802 case PREINCREMENT_EXPR:
11803 case PREDECREMENT_EXPR:
11804 case POSTINCREMENT_EXPR:
11805 case POSTDECREMENT_EXPR:
11806 case LOOP_EXPR:
11807 case EXIT_EXPR:
11808 case COMPOUND_LITERAL_EXPR:
11809 /* Lowered by gimplify.c. */
11810 gcc_unreachable ();
11812 case FDESC_EXPR:
11813 /* Function descriptors are not valid except for as
11814 initialization constants, and should not be expanded. */
11815 gcc_unreachable ();
11817 case WITH_SIZE_EXPR:
11818 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11819 have pulled out the size to use in whatever context it needed. */
11820 return expand_expr_real (treeop0, original_target, tmode,
11821 modifier, alt_rtl, inner_reference_p);
11823 default:
11824 return expand_expr_real_2 (&ops, target, tmode, modifier);
11828 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11829 signedness of TYPE), possibly returning the result in TARGET.
11830 TYPE is known to be a partial integer type. */
11831 static rtx
11832 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11834 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11835 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11836 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11837 && (!target || GET_MODE (target) == mode));
11839 /* For constant values, reduce using wide_int_to_tree. */
11840 if (poly_int_rtx_p (exp))
11842 auto value = wi::to_poly_wide (exp, mode);
11843 tree t = wide_int_to_tree (type, value);
11844 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11846 else if (TYPE_UNSIGNED (type))
11848 rtx mask = immed_wide_int_const
11849 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11850 return expand_and (mode, exp, mask, target);
11852 else
11854 int count = GET_MODE_PRECISION (mode) - prec;
11855 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11856 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11860 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11861 when applied to the address of EXP produces an address known to be
11862 aligned more than BIGGEST_ALIGNMENT. */
11864 static int
11865 is_aligning_offset (const_tree offset, const_tree exp)
11867 /* Strip off any conversions. */
11868 while (CONVERT_EXPR_P (offset))
11869 offset = TREE_OPERAND (offset, 0);
11871 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11872 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11873 if (TREE_CODE (offset) != BIT_AND_EXPR
11874 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11875 || compare_tree_int (TREE_OPERAND (offset, 1),
11876 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11877 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11878 return 0;
11880 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11881 It must be NEGATE_EXPR. Then strip any more conversions. */
11882 offset = TREE_OPERAND (offset, 0);
11883 while (CONVERT_EXPR_P (offset))
11884 offset = TREE_OPERAND (offset, 0);
11886 if (TREE_CODE (offset) != NEGATE_EXPR)
11887 return 0;
11889 offset = TREE_OPERAND (offset, 0);
11890 while (CONVERT_EXPR_P (offset))
11891 offset = TREE_OPERAND (offset, 0);
11893 /* This must now be the address of EXP. */
11894 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11897 /* Return a STRING_CST corresponding to ARG's constant initializer either
11898 if it's a string constant, or, when VALREP is set, any other constant,
11899 or null otherwise.
11900 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
11901 within the byte string that ARG is references. If nonnull set *MEM_SIZE
11902 to the size of the byte string. If nonnull, set *DECL to the constant
11903 declaration ARG refers to. */
11905 static tree
11906 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
11907 bool valrep = false)
11909 tree dummy = NULL_TREE;
11910 if (!mem_size)
11911 mem_size = &dummy;
11913 /* Store the type of the original expression before conversions
11914 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
11915 removed. */
11916 tree argtype = TREE_TYPE (arg);
11918 tree array;
11919 STRIP_NOPS (arg);
11921 /* Non-constant index into the character array in an ARRAY_REF
11922 expression or null. */
11923 tree varidx = NULL_TREE;
11925 poly_int64 base_off = 0;
11927 if (TREE_CODE (arg) == ADDR_EXPR)
11929 arg = TREE_OPERAND (arg, 0);
11930 tree ref = arg;
11931 if (TREE_CODE (arg) == ARRAY_REF)
11933 tree idx = TREE_OPERAND (arg, 1);
11934 if (TREE_CODE (idx) != INTEGER_CST)
11936 /* From a pointer (but not array) argument extract the variable
11937 index to prevent get_addr_base_and_unit_offset() from failing
11938 due to it. Use it later to compute the non-constant offset
11939 into the string and return it to the caller. */
11940 varidx = idx;
11941 ref = TREE_OPERAND (arg, 0);
11943 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11944 return NULL_TREE;
11946 if (!integer_zerop (array_ref_low_bound (arg)))
11947 return NULL_TREE;
11949 if (!integer_onep (array_ref_element_size (arg)))
11950 return NULL_TREE;
11953 array = get_addr_base_and_unit_offset (ref, &base_off);
11954 if (!array
11955 || (TREE_CODE (array) != VAR_DECL
11956 && TREE_CODE (array) != CONST_DECL
11957 && TREE_CODE (array) != STRING_CST))
11958 return NULL_TREE;
11960 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11962 tree arg0 = TREE_OPERAND (arg, 0);
11963 tree arg1 = TREE_OPERAND (arg, 1);
11965 tree offset;
11966 tree str = string_constant (arg0, &offset, mem_size, decl);
11967 if (!str)
11969 str = string_constant (arg1, &offset, mem_size, decl);
11970 arg1 = arg0;
11973 if (str)
11975 /* Avoid pointers to arrays (see bug 86622). */
11976 if (POINTER_TYPE_P (TREE_TYPE (arg))
11977 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
11978 && !(decl && !*decl)
11979 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11980 && tree_fits_uhwi_p (*mem_size)
11981 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11982 return NULL_TREE;
11984 tree type = TREE_TYPE (offset);
11985 arg1 = fold_convert (type, arg1);
11986 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
11987 return str;
11989 return NULL_TREE;
11991 else if (TREE_CODE (arg) == SSA_NAME)
11993 gimple *stmt = SSA_NAME_DEF_STMT (arg);
11994 if (!is_gimple_assign (stmt))
11995 return NULL_TREE;
11997 tree rhs1 = gimple_assign_rhs1 (stmt);
11998 tree_code code = gimple_assign_rhs_code (stmt);
11999 if (code == ADDR_EXPR)
12000 return string_constant (rhs1, ptr_offset, mem_size, decl);
12001 else if (code != POINTER_PLUS_EXPR)
12002 return NULL_TREE;
12004 tree offset;
12005 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12007 /* Avoid pointers to arrays (see bug 86622). */
12008 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12009 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12010 && !(decl && !*decl)
12011 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12012 && tree_fits_uhwi_p (*mem_size)
12013 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12014 return NULL_TREE;
12016 tree rhs2 = gimple_assign_rhs2 (stmt);
12017 tree type = TREE_TYPE (offset);
12018 rhs2 = fold_convert (type, rhs2);
12019 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12020 return str;
12022 return NULL_TREE;
12024 else if (DECL_P (arg))
12025 array = arg;
12026 else
12027 return NULL_TREE;
12029 tree offset = wide_int_to_tree (sizetype, base_off);
12030 if (varidx)
12032 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12033 return NULL_TREE;
12035 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12036 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12037 if (TREE_CODE (chartype) != INTEGER_TYPE)
12038 return NULL;
12040 offset = fold_convert (sizetype, varidx);
12043 if (TREE_CODE (array) == STRING_CST)
12045 *ptr_offset = fold_convert (sizetype, offset);
12046 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12047 if (decl)
12048 *decl = NULL_TREE;
12049 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12050 >= TREE_STRING_LENGTH (array));
12051 return array;
12054 tree init = ctor_for_folding (array);
12055 if (!init || init == error_mark_node)
12056 return NULL_TREE;
12058 if (valrep)
12060 HOST_WIDE_INT cstoff;
12061 if (!base_off.is_constant (&cstoff))
12062 return NULL_TREE;
12064 /* Check that the host and target are sane. */
12065 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12066 return NULL_TREE;
12068 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12069 if (typesz <= 0 || (int) typesz != typesz)
12070 return NULL_TREE;
12072 HOST_WIDE_INT size = typesz;
12073 if (VAR_P (array)
12074 && DECL_SIZE_UNIT (array)
12075 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12077 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12078 gcc_checking_assert (size >= typesz);
12081 /* If value representation was requested convert the initializer
12082 for the whole array or object into a string of bytes forming
12083 its value representation and return it. */
12084 unsigned char *bytes = XNEWVEC (unsigned char, size);
12085 int r = native_encode_initializer (init, bytes, size);
12086 if (r < typesz)
12088 XDELETEVEC (bytes);
12089 return NULL_TREE;
12092 if (r < size)
12093 memset (bytes + r, '\0', size - r);
12095 const char *p = reinterpret_cast<const char *>(bytes);
12096 init = build_string_literal (size, p, char_type_node);
12097 init = TREE_OPERAND (init, 0);
12098 init = TREE_OPERAND (init, 0);
12099 XDELETE (bytes);
12101 *mem_size = size_int (TREE_STRING_LENGTH (init));
12102 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12104 if (decl)
12105 *decl = array;
12107 return init;
12110 if (TREE_CODE (init) == CONSTRUCTOR)
12112 /* Convert the 64-bit constant offset to a wider type to avoid
12113 overflow and use it to obtain the initializer for the subobject
12114 it points into. */
12115 offset_int wioff;
12116 if (!base_off.is_constant (&wioff))
12117 return NULL_TREE;
12119 wioff *= BITS_PER_UNIT;
12120 if (!wi::fits_uhwi_p (wioff))
12121 return NULL_TREE;
12123 base_off = wioff.to_uhwi ();
12124 unsigned HOST_WIDE_INT fieldoff = 0;
12125 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12126 &fieldoff);
12127 if (!init || init == error_mark_node)
12128 return NULL_TREE;
12130 HOST_WIDE_INT cstoff;
12131 if (!base_off.is_constant (&cstoff))
12132 return NULL_TREE;
12134 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12135 tree off = build_int_cst (sizetype, cstoff);
12136 if (varidx)
12137 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12138 else
12139 offset = off;
12142 *ptr_offset = offset;
12144 tree inittype = TREE_TYPE (init);
12146 if (TREE_CODE (init) == INTEGER_CST
12147 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12148 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12150 /* Check that the host and target are sane. */
12151 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12152 return NULL_TREE;
12154 /* For a reference to (address of) a single constant character,
12155 store the native representation of the character in CHARBUF.
12156 If the reference is to an element of an array or a member
12157 of a struct, only consider narrow characters until ctors
12158 for wide character arrays are transformed to STRING_CSTs
12159 like those for narrow arrays. */
12160 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12161 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12162 if (len > 0)
12164 /* Construct a string literal with elements of INITTYPE and
12165 the representation above. Then strip
12166 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12167 init = build_string_literal (len, (char *)charbuf, inittype);
12168 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12172 tree initsize = TYPE_SIZE_UNIT (inittype);
12174 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12176 /* Fold an empty/zero constructor for an implicitly initialized
12177 object or subobject into the empty string. */
12179 /* Determine the character type from that of the original
12180 expression. */
12181 tree chartype = argtype;
12182 if (POINTER_TYPE_P (chartype))
12183 chartype = TREE_TYPE (chartype);
12184 while (TREE_CODE (chartype) == ARRAY_TYPE)
12185 chartype = TREE_TYPE (chartype);
12187 if (INTEGRAL_TYPE_P (chartype)
12188 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12190 /* Convert a char array to an empty STRING_CST having an array
12191 of the expected type and size. */
12192 if (!initsize)
12193 initsize = integer_zero_node;
12195 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12196 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12197 return NULL_TREE;
12199 init = build_string_literal (size, NULL, chartype, size);
12200 init = TREE_OPERAND (init, 0);
12201 init = TREE_OPERAND (init, 0);
12203 *ptr_offset = integer_zero_node;
12207 if (decl)
12208 *decl = array;
12210 if (TREE_CODE (init) != STRING_CST)
12211 return NULL_TREE;
12213 *mem_size = initsize;
12215 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12217 return init;
12220 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12221 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12222 non-constant) offset in bytes within the string that ARG is accessing.
12223 If MEM_SIZE is non-zero the storage size of the memory is returned.
12224 If DECL is non-zero the constant declaration is returned if available. */
12226 tree
12227 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12229 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12232 /* Similar to string_constant, return a STRING_CST corresponding
12233 to the value representation of the first argument if it's
12234 a constant. */
12236 tree
12237 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12239 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12242 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12243 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12244 for C2 > 0 to x & C3 == C2
12245 for C2 < 0 to x & C3 == (C2 & C3). */
12246 enum tree_code
12247 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12249 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12250 tree treeop0 = gimple_assign_rhs1 (stmt);
12251 tree treeop1 = gimple_assign_rhs2 (stmt);
12252 tree type = TREE_TYPE (*arg0);
12253 scalar_int_mode mode;
12254 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12255 return code;
12256 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12257 || TYPE_PRECISION (type) <= 1
12258 || TYPE_UNSIGNED (type)
12259 /* Signed x % c == 0 should have been optimized into unsigned modulo
12260 earlier. */
12261 || integer_zerop (*arg1)
12262 /* If c is known to be non-negative, modulo will be expanded as unsigned
12263 modulo. */
12264 || get_range_pos_neg (treeop0) == 1)
12265 return code;
12267 /* x % c == d where d < 0 && d <= -c should be always false. */
12268 if (tree_int_cst_sgn (*arg1) == -1
12269 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12270 return code;
12272 int prec = TYPE_PRECISION (type);
12273 wide_int w = wi::to_wide (treeop1) - 1;
12274 w |= wi::shifted_mask (0, prec - 1, true, prec);
12275 tree c3 = wide_int_to_tree (type, w);
12276 tree c4 = *arg1;
12277 if (tree_int_cst_sgn (*arg1) == -1)
12278 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12280 rtx op0 = expand_normal (treeop0);
12281 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12283 bool speed_p = optimize_insn_for_speed_p ();
12285 do_pending_stack_adjust ();
12287 location_t loc = gimple_location (stmt);
12288 struct separate_ops ops;
12289 ops.code = TRUNC_MOD_EXPR;
12290 ops.location = loc;
12291 ops.type = TREE_TYPE (treeop0);
12292 ops.op0 = treeop0;
12293 ops.op1 = treeop1;
12294 ops.op2 = NULL_TREE;
12295 start_sequence ();
12296 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12297 EXPAND_NORMAL);
12298 rtx_insn *moinsns = get_insns ();
12299 end_sequence ();
12301 unsigned mocost = seq_cost (moinsns, speed_p);
12302 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12303 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12305 ops.code = BIT_AND_EXPR;
12306 ops.location = loc;
12307 ops.type = TREE_TYPE (treeop0);
12308 ops.op0 = treeop0;
12309 ops.op1 = c3;
12310 ops.op2 = NULL_TREE;
12311 start_sequence ();
12312 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12313 EXPAND_NORMAL);
12314 rtx_insn *muinsns = get_insns ();
12315 end_sequence ();
12317 unsigned mucost = seq_cost (muinsns, speed_p);
12318 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12319 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12321 if (mocost <= mucost)
12323 emit_insn (moinsns);
12324 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12325 return code;
12328 emit_insn (muinsns);
12329 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12330 *arg1 = c4;
12331 return code;
12334 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12335 If C1 is odd to:
12336 (X - C2) * C3 <= C4 (or >), where
12337 C3 is modular multiplicative inverse of C1 and 1<<prec and
12338 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12339 if C2 > ((1<<prec) - 1) % C1).
12340 If C1 is even, S = ctz (C1) and C2 is 0, use
12341 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12342 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12344 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12345 unsigned):
12346 (X * C3) + C4 <= 2 * C4, where
12347 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12348 C4 is ((1<<(prec - 1) - 1) / C1).
12349 If C1 is even, S = ctz(C1), use
12350 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12351 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12352 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12354 See the Hacker's Delight book, section 10-17. */
12355 enum tree_code
12356 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12358 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12359 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12361 if (optimize < 2)
12362 return code;
12364 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12365 if (stmt == NULL)
12366 return code;
12368 tree treeop0 = gimple_assign_rhs1 (stmt);
12369 tree treeop1 = gimple_assign_rhs2 (stmt);
12370 if (TREE_CODE (treeop0) != SSA_NAME
12371 || TREE_CODE (treeop1) != INTEGER_CST
12372 /* Don't optimize the undefined behavior case x % 0;
12373 x % 1 should have been optimized into zero, punt if
12374 it makes it here for whatever reason;
12375 x % -c should have been optimized into x % c. */
12376 || compare_tree_int (treeop1, 2) <= 0
12377 /* Likewise x % c == d where d >= c should be always false. */
12378 || tree_int_cst_le (treeop1, *arg1))
12379 return code;
12381 /* Unsigned x % pow2 is handled right already, for signed
12382 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12383 if (integer_pow2p (treeop1))
12384 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12386 tree type = TREE_TYPE (*arg0);
12387 scalar_int_mode mode;
12388 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12389 return code;
12390 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12391 || TYPE_PRECISION (type) <= 1)
12392 return code;
12394 signop sgn = UNSIGNED;
12395 /* If both operands are known to have the sign bit clear, handle
12396 even the signed modulo case as unsigned. treeop1 is always
12397 positive >= 2, checked above. */
12398 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12399 sgn = SIGNED;
12401 if (!TYPE_UNSIGNED (type))
12403 if (tree_int_cst_sgn (*arg1) == -1)
12404 return code;
12405 type = unsigned_type_for (type);
12406 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12407 return code;
12410 int prec = TYPE_PRECISION (type);
12411 wide_int w = wi::to_wide (treeop1);
12412 int shift = wi::ctz (w);
12413 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12414 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12415 If C1 is odd, we can handle all cases by subtracting
12416 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12417 e.g. by testing for overflow on the subtraction, punt on that for now
12418 though. */
12419 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12421 if (sgn == SIGNED)
12422 return code;
12423 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12424 if (wi::gtu_p (wi::to_wide (*arg1), x))
12425 return code;
12428 imm_use_iterator imm_iter;
12429 use_operand_p use_p;
12430 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12432 gimple *use_stmt = USE_STMT (use_p);
12433 /* Punt if treeop0 is used in the same bb in a division
12434 or another modulo with the same divisor. We should expect
12435 the division and modulo combined together. */
12436 if (use_stmt == stmt
12437 || gimple_bb (use_stmt) != gimple_bb (stmt))
12438 continue;
12439 if (!is_gimple_assign (use_stmt)
12440 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12441 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12442 continue;
12443 if (gimple_assign_rhs1 (use_stmt) != treeop0
12444 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12445 continue;
12446 return code;
12449 w = wi::lrshift (w, shift);
12450 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12451 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12452 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12453 tree c3 = wide_int_to_tree (type, m);
12454 tree c5 = NULL_TREE;
12455 wide_int d, e;
12456 if (sgn == UNSIGNED)
12458 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12459 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12460 otherwise use < or subtract one from C4. E.g. for
12461 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12462 x % 3U == 1 already needs to be
12463 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12464 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12465 d -= 1;
12466 if (shift)
12467 d = wi::lrshift (d, shift);
12469 else
12471 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12472 if (!shift)
12473 d = wi::lshift (e, 1);
12474 else
12476 e = wi::bit_and (e, wi::mask (shift, true, prec));
12477 d = wi::lrshift (e, shift - 1);
12479 c5 = wide_int_to_tree (type, e);
12481 tree c4 = wide_int_to_tree (type, d);
12483 rtx op0 = expand_normal (treeop0);
12484 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12486 bool speed_p = optimize_insn_for_speed_p ();
12488 do_pending_stack_adjust ();
12490 location_t loc = gimple_location (stmt);
12491 struct separate_ops ops;
12492 ops.code = TRUNC_MOD_EXPR;
12493 ops.location = loc;
12494 ops.type = TREE_TYPE (treeop0);
12495 ops.op0 = treeop0;
12496 ops.op1 = treeop1;
12497 ops.op2 = NULL_TREE;
12498 start_sequence ();
12499 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12500 EXPAND_NORMAL);
12501 rtx_insn *moinsns = get_insns ();
12502 end_sequence ();
12504 unsigned mocost = seq_cost (moinsns, speed_p);
12505 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12506 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12508 tree t = fold_convert_loc (loc, type, treeop0);
12509 if (!integer_zerop (*arg1))
12510 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12511 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12512 if (sgn == SIGNED)
12513 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12514 if (shift)
12516 tree s = build_int_cst (NULL_TREE, shift);
12517 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12520 start_sequence ();
12521 rtx mur = expand_normal (t);
12522 rtx_insn *muinsns = get_insns ();
12523 end_sequence ();
12525 unsigned mucost = seq_cost (muinsns, speed_p);
12526 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12527 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12529 if (mocost <= mucost)
12531 emit_insn (moinsns);
12532 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12533 return code;
12536 emit_insn (muinsns);
12537 *arg0 = make_tree (type, mur);
12538 *arg1 = c4;
12539 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12542 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12544 void
12545 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12547 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12548 || code == LT_EXPR || code == LE_EXPR);
12549 gcc_checking_assert (integer_zerop (*arg1));
12551 if (!optimize)
12552 return;
12554 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12555 if (stmt == NULL)
12556 return;
12558 tree treeop0 = gimple_assign_rhs1 (stmt);
12559 tree treeop1 = gimple_assign_rhs2 (stmt);
12560 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12561 return;
12563 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12564 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12565 "assuming signed overflow does not occur when "
12566 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12567 op_symbol_code (code), op_symbol_code (code));
12569 *arg0 = treeop0;
12570 *arg1 = treeop1;
12573 /* Generate code to calculate OPS, and exploded expression
12574 using a store-flag instruction and return an rtx for the result.
12575 OPS reflects a comparison.
12577 If TARGET is nonzero, store the result there if convenient.
12579 Return zero if there is no suitable set-flag instruction
12580 available on this machine.
12582 Once expand_expr has been called on the arguments of the comparison,
12583 we are committed to doing the store flag, since it is not safe to
12584 re-evaluate the expression. We emit the store-flag insn by calling
12585 emit_store_flag, but only expand the arguments if we have a reason
12586 to believe that emit_store_flag will be successful. If we think that
12587 it will, but it isn't, we have to simulate the store-flag with a
12588 set/jump/set sequence. */
12590 static rtx
12591 do_store_flag (sepops ops, rtx target, machine_mode mode)
12593 enum rtx_code code;
12594 tree arg0, arg1, type;
12595 machine_mode operand_mode;
12596 int unsignedp;
12597 rtx op0, op1;
12598 rtx subtarget = target;
12599 location_t loc = ops->location;
12601 arg0 = ops->op0;
12602 arg1 = ops->op1;
12604 /* Don't crash if the comparison was erroneous. */
12605 if (arg0 == error_mark_node || arg1 == error_mark_node)
12606 return const0_rtx;
12608 type = TREE_TYPE (arg0);
12609 operand_mode = TYPE_MODE (type);
12610 unsignedp = TYPE_UNSIGNED (type);
12612 /* We won't bother with BLKmode store-flag operations because it would mean
12613 passing a lot of information to emit_store_flag. */
12614 if (operand_mode == BLKmode)
12615 return 0;
12617 /* We won't bother with store-flag operations involving function pointers
12618 when function pointers must be canonicalized before comparisons. */
12619 if (targetm.have_canonicalize_funcptr_for_compare ()
12620 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12621 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12622 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12623 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12624 return 0;
12626 STRIP_NOPS (arg0);
12627 STRIP_NOPS (arg1);
12629 /* For vector typed comparisons emit code to generate the desired
12630 all-ones or all-zeros mask. */
12631 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12633 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12634 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12635 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12636 return expand_vec_cmp_expr (ops->type, ifexp, target);
12637 else
12638 gcc_unreachable ();
12641 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12642 into (x - C2) * C3 < C4. */
12643 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12644 && TREE_CODE (arg0) == SSA_NAME
12645 && TREE_CODE (arg1) == INTEGER_CST)
12647 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12648 &arg0, &arg1);
12649 if (new_code != ops->code)
12651 struct separate_ops nops = *ops;
12652 nops.code = ops->code = new_code;
12653 nops.op0 = arg0;
12654 nops.op1 = arg1;
12655 nops.type = TREE_TYPE (arg0);
12656 return do_store_flag (&nops, target, mode);
12660 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12661 if (!unsignedp
12662 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12663 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12664 && integer_zerop (arg1)
12665 && TREE_CODE (arg0) == SSA_NAME)
12666 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12668 /* Get the rtx comparison code to use. We know that EXP is a comparison
12669 operation of some type. Some comparisons against 1 and -1 can be
12670 converted to comparisons with zero. Do so here so that the tests
12671 below will be aware that we have a comparison with zero. These
12672 tests will not catch constants in the first operand, but constants
12673 are rarely passed as the first operand. */
12675 switch (ops->code)
12677 case EQ_EXPR:
12678 code = EQ;
12679 break;
12680 case NE_EXPR:
12681 code = NE;
12682 break;
12683 case LT_EXPR:
12684 if (integer_onep (arg1))
12685 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12686 else
12687 code = unsignedp ? LTU : LT;
12688 break;
12689 case LE_EXPR:
12690 if (! unsignedp && integer_all_onesp (arg1))
12691 arg1 = integer_zero_node, code = LT;
12692 else
12693 code = unsignedp ? LEU : LE;
12694 break;
12695 case GT_EXPR:
12696 if (! unsignedp && integer_all_onesp (arg1))
12697 arg1 = integer_zero_node, code = GE;
12698 else
12699 code = unsignedp ? GTU : GT;
12700 break;
12701 case GE_EXPR:
12702 if (integer_onep (arg1))
12703 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12704 else
12705 code = unsignedp ? GEU : GE;
12706 break;
12708 case UNORDERED_EXPR:
12709 code = UNORDERED;
12710 break;
12711 case ORDERED_EXPR:
12712 code = ORDERED;
12713 break;
12714 case UNLT_EXPR:
12715 code = UNLT;
12716 break;
12717 case UNLE_EXPR:
12718 code = UNLE;
12719 break;
12720 case UNGT_EXPR:
12721 code = UNGT;
12722 break;
12723 case UNGE_EXPR:
12724 code = UNGE;
12725 break;
12726 case UNEQ_EXPR:
12727 code = UNEQ;
12728 break;
12729 case LTGT_EXPR:
12730 code = LTGT;
12731 break;
12733 default:
12734 gcc_unreachable ();
12737 /* Put a constant second. */
12738 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12739 || TREE_CODE (arg0) == FIXED_CST)
12741 std::swap (arg0, arg1);
12742 code = swap_condition (code);
12745 /* If this is an equality or inequality test of a single bit, we can
12746 do this by shifting the bit being tested to the low-order bit and
12747 masking the result with the constant 1. If the condition was EQ,
12748 we xor it with 1. This does not require an scc insn and is faster
12749 than an scc insn even if we have it.
12751 The code to make this transformation was moved into fold_single_bit_test,
12752 so we just call into the folder and expand its result. */
12754 if ((code == NE || code == EQ)
12755 && integer_zerop (arg1)
12756 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12758 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12759 if (srcstmt
12760 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12762 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12763 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12764 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12765 gimple_assign_rhs1 (srcstmt),
12766 gimple_assign_rhs2 (srcstmt));
12767 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12768 if (temp)
12769 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12773 if (! get_subtarget (target)
12774 || GET_MODE (subtarget) != operand_mode)
12775 subtarget = 0;
12777 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12779 if (target == 0)
12780 target = gen_reg_rtx (mode);
12782 /* Try a cstore if possible. */
12783 return emit_store_flag_force (target, code, op0, op1,
12784 operand_mode, unsignedp,
12785 (TYPE_PRECISION (ops->type) == 1
12786 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12789 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12790 0 otherwise (i.e. if there is no casesi instruction).
12792 DEFAULT_PROBABILITY is the probability of jumping to the default
12793 label. */
12795 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12796 rtx table_label, rtx default_label, rtx fallback_label,
12797 profile_probability default_probability)
12799 class expand_operand ops[5];
12800 scalar_int_mode index_mode = SImode;
12801 rtx op1, op2, index;
12803 if (! targetm.have_casesi ())
12804 return 0;
12806 /* The index must be some form of integer. Convert it to SImode. */
12807 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12808 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12810 rtx rangertx = expand_normal (range);
12812 /* We must handle the endpoints in the original mode. */
12813 index_expr = build2 (MINUS_EXPR, index_type,
12814 index_expr, minval);
12815 minval = integer_zero_node;
12816 index = expand_normal (index_expr);
12817 if (default_label)
12818 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12819 omode, 1, default_label,
12820 default_probability);
12821 /* Now we can safely truncate. */
12822 index = convert_to_mode (index_mode, index, 0);
12824 else
12826 if (omode != index_mode)
12828 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12829 index_expr = fold_convert (index_type, index_expr);
12832 index = expand_normal (index_expr);
12835 do_pending_stack_adjust ();
12837 op1 = expand_normal (minval);
12838 op2 = expand_normal (range);
12840 create_input_operand (&ops[0], index, index_mode);
12841 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12842 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12843 create_fixed_operand (&ops[3], table_label);
12844 create_fixed_operand (&ops[4], (default_label
12845 ? default_label
12846 : fallback_label));
12847 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12848 return 1;
12851 /* Attempt to generate a tablejump instruction; same concept. */
12852 /* Subroutine of the next function.
12854 INDEX is the value being switched on, with the lowest value
12855 in the table already subtracted.
12856 MODE is its expected mode (needed if INDEX is constant).
12857 RANGE is the length of the jump table.
12858 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12860 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12861 index value is out of range.
12862 DEFAULT_PROBABILITY is the probability of jumping to
12863 the default label. */
12865 static void
12866 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12867 rtx default_label, profile_probability default_probability)
12869 rtx temp, vector;
12871 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12872 cfun->cfg->max_jumptable_ents = INTVAL (range);
12874 /* Do an unsigned comparison (in the proper mode) between the index
12875 expression and the value which represents the length of the range.
12876 Since we just finished subtracting the lower bound of the range
12877 from the index expression, this comparison allows us to simultaneously
12878 check that the original index expression value is both greater than
12879 or equal to the minimum value of the range and less than or equal to
12880 the maximum value of the range. */
12882 if (default_label)
12883 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12884 default_label, default_probability);
12886 /* If index is in range, it must fit in Pmode.
12887 Convert to Pmode so we can index with it. */
12888 if (mode != Pmode)
12890 unsigned int width;
12892 /* We know the value of INDEX is between 0 and RANGE. If we have a
12893 sign-extended subreg, and RANGE does not have the sign bit set, then
12894 we have a value that is valid for both sign and zero extension. In
12895 this case, we get better code if we sign extend. */
12896 if (GET_CODE (index) == SUBREG
12897 && SUBREG_PROMOTED_VAR_P (index)
12898 && SUBREG_PROMOTED_SIGNED_P (index)
12899 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
12900 <= HOST_BITS_PER_WIDE_INT)
12901 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
12902 index = convert_to_mode (Pmode, index, 0);
12903 else
12904 index = convert_to_mode (Pmode, index, 1);
12907 /* Don't let a MEM slip through, because then INDEX that comes
12908 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
12909 and break_out_memory_refs will go to work on it and mess it up. */
12910 #ifdef PIC_CASE_VECTOR_ADDRESS
12911 if (flag_pic && !REG_P (index))
12912 index = copy_to_mode_reg (Pmode, index);
12913 #endif
12915 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
12916 GET_MODE_SIZE, because this indicates how large insns are. The other
12917 uses should all be Pmode, because they are addresses. This code
12918 could fail if addresses and insns are not the same size. */
12919 index = simplify_gen_binary (MULT, Pmode, index,
12920 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
12921 Pmode));
12922 index = simplify_gen_binary (PLUS, Pmode, index,
12923 gen_rtx_LABEL_REF (Pmode, table_label));
12925 #ifdef PIC_CASE_VECTOR_ADDRESS
12926 if (flag_pic)
12927 index = PIC_CASE_VECTOR_ADDRESS (index);
12928 else
12929 #endif
12930 index = memory_address (CASE_VECTOR_MODE, index);
12931 temp = gen_reg_rtx (CASE_VECTOR_MODE);
12932 vector = gen_const_mem (CASE_VECTOR_MODE, index);
12933 convert_move (temp, vector, 0);
12935 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
12937 /* If we are generating PIC code or if the table is PC-relative, the
12938 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
12939 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
12940 emit_barrier ();
12944 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
12945 rtx table_label, rtx default_label,
12946 profile_probability default_probability)
12948 rtx index;
12950 if (! targetm.have_tablejump ())
12951 return 0;
12953 index_expr = fold_build2 (MINUS_EXPR, index_type,
12954 fold_convert (index_type, index_expr),
12955 fold_convert (index_type, minval));
12956 index = expand_normal (index_expr);
12957 do_pending_stack_adjust ();
12959 do_tablejump (index, TYPE_MODE (index_type),
12960 convert_modes (TYPE_MODE (index_type),
12961 TYPE_MODE (TREE_TYPE (range)),
12962 expand_normal (range),
12963 TYPE_UNSIGNED (TREE_TYPE (range))),
12964 table_label, default_label, default_probability);
12965 return 1;
12968 /* Return a CONST_VECTOR rtx representing vector mask for
12969 a VECTOR_CST of booleans. */
12970 static rtx
12971 const_vector_mask_from_tree (tree exp)
12973 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12974 machine_mode inner = GET_MODE_INNER (mode);
12976 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12977 VECTOR_CST_NELTS_PER_PATTERN (exp));
12978 unsigned int count = builder.encoded_nelts ();
12979 for (unsigned int i = 0; i < count; ++i)
12981 tree elt = VECTOR_CST_ELT (exp, i);
12982 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
12983 if (integer_zerop (elt))
12984 builder.quick_push (CONST0_RTX (inner));
12985 else if (integer_onep (elt)
12986 || integer_minus_onep (elt))
12987 builder.quick_push (CONSTM1_RTX (inner));
12988 else
12989 gcc_unreachable ();
12991 return builder.build ();
12994 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
12995 static rtx
12996 const_vector_from_tree (tree exp)
12998 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13000 if (initializer_zerop (exp))
13001 return CONST0_RTX (mode);
13003 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13004 return const_vector_mask_from_tree (exp);
13006 machine_mode inner = GET_MODE_INNER (mode);
13008 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13009 VECTOR_CST_NELTS_PER_PATTERN (exp));
13010 unsigned int count = builder.encoded_nelts ();
13011 for (unsigned int i = 0; i < count; ++i)
13013 tree elt = VECTOR_CST_ELT (exp, i);
13014 if (TREE_CODE (elt) == REAL_CST)
13015 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13016 inner));
13017 else if (TREE_CODE (elt) == FIXED_CST)
13018 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13019 inner));
13020 else
13021 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13022 inner));
13024 return builder.build ();
13027 /* Build a decl for a personality function given a language prefix. */
13029 tree
13030 build_personality_function (const char *lang)
13032 const char *unwind_and_version;
13033 tree decl, type;
13034 char *name;
13036 switch (targetm_common.except_unwind_info (&global_options))
13038 case UI_NONE:
13039 return NULL;
13040 case UI_SJLJ:
13041 unwind_and_version = "_sj0";
13042 break;
13043 case UI_DWARF2:
13044 case UI_TARGET:
13045 unwind_and_version = "_v0";
13046 break;
13047 case UI_SEH:
13048 unwind_and_version = "_seh0";
13049 break;
13050 default:
13051 gcc_unreachable ();
13054 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13056 type = build_function_type_list (unsigned_type_node,
13057 integer_type_node, integer_type_node,
13058 long_long_unsigned_type_node,
13059 ptr_type_node, ptr_type_node, NULL_TREE);
13060 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13061 get_identifier (name), type);
13062 DECL_ARTIFICIAL (decl) = 1;
13063 DECL_EXTERNAL (decl) = 1;
13064 TREE_PUBLIC (decl) = 1;
13066 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13067 are the flags assigned by targetm.encode_section_info. */
13068 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13070 return decl;
13073 /* Extracts the personality function of DECL and returns the corresponding
13074 libfunc. */
13077 get_personality_function (tree decl)
13079 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13080 enum eh_personality_kind pk;
13082 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13083 if (pk == eh_personality_none)
13084 return NULL;
13086 if (!personality
13087 && pk == eh_personality_any)
13088 personality = lang_hooks.eh_personality ();
13090 if (pk == eh_personality_lang)
13091 gcc_assert (personality != NULL_TREE);
13093 return XEXP (DECL_RTL (personality), 0);
13096 /* Returns a tree for the size of EXP in bytes. */
13098 static tree
13099 tree_expr_size (const_tree exp)
13101 if (DECL_P (exp)
13102 && DECL_SIZE_UNIT (exp) != 0)
13103 return DECL_SIZE_UNIT (exp);
13104 else
13105 return size_in_bytes (TREE_TYPE (exp));
13108 /* Return an rtx for the size in bytes of the value of EXP. */
13111 expr_size (tree exp)
13113 tree size;
13115 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13116 size = TREE_OPERAND (exp, 1);
13117 else
13119 size = tree_expr_size (exp);
13120 gcc_assert (size);
13121 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13124 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13127 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13128 if the size can vary or is larger than an integer. */
13130 static HOST_WIDE_INT
13131 int_expr_size (tree exp)
13133 tree size;
13135 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13136 size = TREE_OPERAND (exp, 1);
13137 else
13139 size = tree_expr_size (exp);
13140 gcc_assert (size);
13143 if (size == 0 || !tree_fits_shwi_p (size))
13144 return -1;
13146 return tree_to_shwi (size);