Daily bump.
[official-gcc.git] / gcc / expr.c
blob0365625e7b83500ae0301a82c469687c9f8f231c
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);
1347 /* Derived class from op_by_pieces_d, providing support for block move
1348 operations. */
1350 #ifdef PUSH_ROUNDING
1351 #define PUSHG_P(to) ((to) == nullptr)
1352 #else
1353 #define PUSHG_P(to) false
1354 #endif
1356 class move_by_pieces_d : public op_by_pieces_d
1358 insn_gen_fn m_gen_fun;
1359 void generate (rtx, rtx, machine_mode);
1360 bool prepare_mode (machine_mode, unsigned int);
1362 public:
1363 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1364 unsigned int align)
1365 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1366 NULL, len, align, PUSHG_P (to))
1369 rtx finish_retmode (memop_ret);
1372 /* Return true if MODE can be used for a set of copies, given an
1373 alignment ALIGN. Prepare whatever data is necessary for later
1374 calls to generate. */
1376 bool
1377 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1379 insn_code icode = optab_handler (mov_optab, mode);
1380 m_gen_fun = GEN_FCN (icode);
1381 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1384 /* A callback used when iterating for a compare_by_pieces_operation.
1385 OP0 and OP1 are the values that have been loaded and should be
1386 compared in MODE. If OP0 is NULL, this means we should generate a
1387 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1388 gen function that should be used to generate the mode. */
1390 void
1391 move_by_pieces_d::generate (rtx op0, rtx op1,
1392 machine_mode mode ATTRIBUTE_UNUSED)
1394 #ifdef PUSH_ROUNDING
1395 if (op0 == NULL_RTX)
1397 emit_single_push_insn (mode, op1, NULL);
1398 return;
1400 #endif
1401 emit_insn (m_gen_fun (op0, op1));
1404 /* Perform the final adjustment at the end of a string to obtain the
1405 correct return value for the block operation.
1406 Return value is based on RETMODE argument. */
1409 move_by_pieces_d::finish_retmode (memop_ret retmode)
1411 gcc_assert (!m_reverse);
1412 if (retmode == RETURN_END_MINUS_ONE)
1414 m_to.maybe_postinc (-1);
1415 --m_offset;
1417 return m_to.adjust (QImode, m_offset);
1420 /* Generate several move instructions to copy LEN bytes from block FROM to
1421 block TO. (These are MEM rtx's with BLKmode).
1423 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1424 used to push FROM to the stack.
1426 ALIGN is maximum stack alignment we can assume.
1428 Return value is based on RETMODE argument. */
1431 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1432 unsigned int align, memop_ret retmode)
1434 #ifndef PUSH_ROUNDING
1435 if (to == NULL)
1436 gcc_unreachable ();
1437 #endif
1439 move_by_pieces_d data (to, from, len, align);
1441 data.run ();
1443 if (retmode != RETURN_BEGIN)
1444 return data.finish_retmode (retmode);
1445 else
1446 return to;
1449 /* Derived class from op_by_pieces_d, providing support for block move
1450 operations. */
1452 class store_by_pieces_d : public op_by_pieces_d
1454 insn_gen_fn m_gen_fun;
1455 void generate (rtx, rtx, machine_mode);
1456 bool prepare_mode (machine_mode, unsigned int);
1458 public:
1459 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1460 unsigned HOST_WIDE_INT len, unsigned int align,
1461 bool qi_vector_mode)
1462 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1463 cfn_data, len, align, false, qi_vector_mode)
1466 rtx finish_retmode (memop_ret);
1469 /* Return true if MODE can be used for a set of stores, given an
1470 alignment ALIGN. Prepare whatever data is necessary for later
1471 calls to generate. */
1473 bool
1474 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1476 insn_code icode = optab_handler (mov_optab, mode);
1477 m_gen_fun = GEN_FCN (icode);
1478 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1481 /* A callback used when iterating for a store_by_pieces_operation.
1482 OP0 and OP1 are the values that have been loaded and should be
1483 compared in MODE. If OP0 is NULL, this means we should generate a
1484 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1485 gen function that should be used to generate the mode. */
1487 void
1488 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1490 emit_insn (m_gen_fun (op0, op1));
1493 /* Perform the final adjustment at the end of a string to obtain the
1494 correct return value for the block operation.
1495 Return value is based on RETMODE argument. */
1498 store_by_pieces_d::finish_retmode (memop_ret retmode)
1500 gcc_assert (!m_reverse);
1501 if (retmode == RETURN_END_MINUS_ONE)
1503 m_to.maybe_postinc (-1);
1504 --m_offset;
1506 return m_to.adjust (QImode, m_offset);
1509 /* Determine whether the LEN bytes generated by CONSTFUN can be
1510 stored to memory using several move instructions. CONSTFUNDATA is
1511 a pointer which will be passed as argument in every CONSTFUN call.
1512 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1513 a memset operation and false if it's a copy of a constant string.
1514 Return nonzero if a call to store_by_pieces should succeed. */
1517 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1518 by_pieces_constfn constfun,
1519 void *constfundata, unsigned int align, bool memsetp)
1521 unsigned HOST_WIDE_INT l;
1522 unsigned int max_size;
1523 HOST_WIDE_INT offset = 0;
1524 enum insn_code icode;
1525 int reverse;
1526 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1527 rtx cst ATTRIBUTE_UNUSED;
1529 if (len == 0)
1530 return 1;
1532 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1533 memsetp
1534 ? SET_BY_PIECES
1535 : STORE_BY_PIECES,
1536 optimize_insn_for_speed_p ()))
1537 return 0;
1539 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1541 /* We would first store what we can in the largest integer mode, then go to
1542 successively smaller modes. */
1544 for (reverse = 0;
1545 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1546 reverse++)
1548 l = len;
1549 max_size = STORE_MAX_PIECES + 1;
1550 while (max_size > 1 && l > 0)
1552 fixed_size_mode mode
1553 = widest_fixed_size_mode_for_size (max_size, memsetp);
1555 icode = optab_handler (mov_optab, mode);
1556 if (icode != CODE_FOR_nothing
1557 && align >= GET_MODE_ALIGNMENT (mode))
1559 unsigned int size = GET_MODE_SIZE (mode);
1561 while (l >= size)
1563 if (reverse)
1564 offset -= size;
1566 cst = (*constfun) (constfundata, nullptr, offset, mode);
1567 /* All CONST_VECTORs can be loaded for memset since
1568 vec_duplicate_optab is a precondition to pick a
1569 vector mode for the memset expander. */
1570 if (!((memsetp && VECTOR_MODE_P (mode))
1571 || targetm.legitimate_constant_p (mode, cst)))
1572 return 0;
1574 if (!reverse)
1575 offset += size;
1577 l -= size;
1581 max_size = GET_MODE_SIZE (mode);
1584 /* The code above should have handled everything. */
1585 gcc_assert (!l);
1588 return 1;
1591 /* Generate several move instructions to store LEN bytes generated by
1592 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1593 pointer which will be passed as argument in every CONSTFUN call.
1594 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1595 a memset operation and false if it's a copy of a constant string.
1596 Return value is based on RETMODE argument. */
1599 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1600 by_pieces_constfn constfun,
1601 void *constfundata, unsigned int align, bool memsetp,
1602 memop_ret retmode)
1604 if (len == 0)
1606 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1607 return to;
1610 gcc_assert (targetm.use_by_pieces_infrastructure_p
1611 (len, align,
1612 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1613 optimize_insn_for_speed_p ()));
1615 store_by_pieces_d data (to, constfun, constfundata, len, align,
1616 memsetp);
1617 data.run ();
1619 if (retmode != RETURN_BEGIN)
1620 return data.finish_retmode (retmode);
1621 else
1622 return to;
1625 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1626 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1628 static void
1629 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1631 if (len == 0)
1632 return;
1634 /* Use builtin_memset_read_str to support vector mode broadcast. */
1635 char c = 0;
1636 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1637 true);
1638 data.run ();
1641 /* Context used by compare_by_pieces_genfn. It stores the fail label
1642 to jump to in case of miscomparison, and for branch ratios greater than 1,
1643 it stores an accumulator and the current and maximum counts before
1644 emitting another branch. */
1646 class compare_by_pieces_d : public op_by_pieces_d
1648 rtx_code_label *m_fail_label;
1649 rtx m_accumulator;
1650 int m_count, m_batch;
1652 void generate (rtx, rtx, machine_mode);
1653 bool prepare_mode (machine_mode, unsigned int);
1654 void finish_mode (machine_mode);
1655 public:
1656 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1657 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1658 rtx_code_label *fail_label)
1659 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1660 op1_cfn_data, len, align, false)
1662 m_fail_label = fail_label;
1666 /* A callback used when iterating for a compare_by_pieces_operation.
1667 OP0 and OP1 are the values that have been loaded and should be
1668 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1669 context structure. */
1671 void
1672 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1674 if (m_batch > 1)
1676 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1677 true, OPTAB_LIB_WIDEN);
1678 if (m_count != 0)
1679 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1680 true, OPTAB_LIB_WIDEN);
1681 m_accumulator = temp;
1683 if (++m_count < m_batch)
1684 return;
1686 m_count = 0;
1687 op0 = m_accumulator;
1688 op1 = const0_rtx;
1689 m_accumulator = NULL_RTX;
1691 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1692 m_fail_label, profile_probability::uninitialized ());
1695 /* Return true if MODE can be used for a set of moves and comparisons,
1696 given an alignment ALIGN. Prepare whatever data is necessary for
1697 later calls to generate. */
1699 bool
1700 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1702 insn_code icode = optab_handler (mov_optab, mode);
1703 if (icode == CODE_FOR_nothing
1704 || align < GET_MODE_ALIGNMENT (mode)
1705 || !can_compare_p (EQ, mode, ccp_jump))
1706 return false;
1707 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1708 if (m_batch < 0)
1709 return false;
1710 m_accumulator = NULL_RTX;
1711 m_count = 0;
1712 return true;
1715 /* Called after expanding a series of comparisons in MODE. If we have
1716 accumulated results for which we haven't emitted a branch yet, do
1717 so now. */
1719 void
1720 compare_by_pieces_d::finish_mode (machine_mode mode)
1722 if (m_accumulator != NULL_RTX)
1723 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1724 NULL_RTX, NULL, m_fail_label,
1725 profile_probability::uninitialized ());
1728 /* Generate several move instructions to compare LEN bytes from blocks
1729 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1731 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1732 used to push FROM to the stack.
1734 ALIGN is maximum stack alignment we can assume.
1736 Optionally, the caller can pass a constfn and associated data in A1_CFN
1737 and A1_CFN_DATA. describing that the second operand being compared is a
1738 known constant and how to obtain its data. */
1740 static rtx
1741 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1742 rtx target, unsigned int align,
1743 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1745 rtx_code_label *fail_label = gen_label_rtx ();
1746 rtx_code_label *end_label = gen_label_rtx ();
1748 if (target == NULL_RTX
1749 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1750 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1752 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1753 fail_label);
1755 data.run ();
1757 emit_move_insn (target, const0_rtx);
1758 emit_jump (end_label);
1759 emit_barrier ();
1760 emit_label (fail_label);
1761 emit_move_insn (target, const1_rtx);
1762 emit_label (end_label);
1764 return target;
1767 /* Emit code to move a block Y to a block X. This may be done with
1768 string-move instructions, with multiple scalar move instructions,
1769 or with a library call.
1771 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1772 SIZE is an rtx that says how long they are.
1773 ALIGN is the maximum alignment we can assume they have.
1774 METHOD describes what kind of copy this is, and what mechanisms may be used.
1775 MIN_SIZE is the minimal size of block to move
1776 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1777 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1779 Return the address of the new block, if memcpy is called and returns it,
1780 0 otherwise. */
1783 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1784 unsigned int expected_align, HOST_WIDE_INT expected_size,
1785 unsigned HOST_WIDE_INT min_size,
1786 unsigned HOST_WIDE_INT max_size,
1787 unsigned HOST_WIDE_INT probable_max_size,
1788 bool bail_out_libcall, bool *is_move_done,
1789 bool might_overlap)
1791 int may_use_call;
1792 rtx retval = 0;
1793 unsigned int align;
1795 if (is_move_done)
1796 *is_move_done = true;
1798 gcc_assert (size);
1799 if (CONST_INT_P (size) && INTVAL (size) == 0)
1800 return 0;
1802 switch (method)
1804 case BLOCK_OP_NORMAL:
1805 case BLOCK_OP_TAILCALL:
1806 may_use_call = 1;
1807 break;
1809 case BLOCK_OP_CALL_PARM:
1810 may_use_call = block_move_libcall_safe_for_call_parm ();
1812 /* Make inhibit_defer_pop nonzero around the library call
1813 to force it to pop the arguments right away. */
1814 NO_DEFER_POP;
1815 break;
1817 case BLOCK_OP_NO_LIBCALL:
1818 may_use_call = 0;
1819 break;
1821 case BLOCK_OP_NO_LIBCALL_RET:
1822 may_use_call = -1;
1823 break;
1825 default:
1826 gcc_unreachable ();
1829 gcc_assert (MEM_P (x) && MEM_P (y));
1830 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1831 gcc_assert (align >= BITS_PER_UNIT);
1833 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1834 block copy is more efficient for other large modes, e.g. DCmode. */
1835 x = adjust_address (x, BLKmode, 0);
1836 y = adjust_address (y, BLKmode, 0);
1838 /* If source and destination are the same, no need to copy anything. */
1839 if (rtx_equal_p (x, y)
1840 && !MEM_VOLATILE_P (x)
1841 && !MEM_VOLATILE_P (y))
1842 return 0;
1844 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1845 can be incorrect is coming from __builtin_memcpy. */
1846 poly_int64 const_size;
1847 if (poly_int_rtx_p (size, &const_size))
1849 x = shallow_copy_rtx (x);
1850 y = shallow_copy_rtx (y);
1851 set_mem_size (x, const_size);
1852 set_mem_size (y, const_size);
1855 bool pieces_ok = CONST_INT_P (size)
1856 && can_move_by_pieces (INTVAL (size), align);
1857 bool pattern_ok = false;
1859 if (!pieces_ok || might_overlap)
1861 pattern_ok
1862 = emit_block_move_via_pattern (x, y, size, align,
1863 expected_align, expected_size,
1864 min_size, max_size, probable_max_size,
1865 might_overlap);
1866 if (!pattern_ok && might_overlap)
1868 /* Do not try any of the other methods below as they are not safe
1869 for overlapping moves. */
1870 *is_move_done = false;
1871 return retval;
1875 if (pattern_ok)
1877 else if (pieces_ok)
1878 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1879 else if (may_use_call && !might_overlap
1880 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1881 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1883 if (bail_out_libcall)
1885 if (is_move_done)
1886 *is_move_done = false;
1887 return retval;
1890 if (may_use_call < 0)
1891 return pc_rtx;
1893 retval = emit_block_copy_via_libcall (x, y, size,
1894 method == BLOCK_OP_TAILCALL);
1896 else if (might_overlap)
1897 *is_move_done = false;
1898 else
1899 emit_block_move_via_loop (x, y, size, align);
1901 if (method == BLOCK_OP_CALL_PARM)
1902 OK_DEFER_POP;
1904 return retval;
1908 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1910 unsigned HOST_WIDE_INT max, min = 0;
1911 if (GET_CODE (size) == CONST_INT)
1912 min = max = UINTVAL (size);
1913 else
1914 max = GET_MODE_MASK (GET_MODE (size));
1915 return emit_block_move_hints (x, y, size, method, 0, -1,
1916 min, max, max);
1919 /* A subroutine of emit_block_move. Returns true if calling the
1920 block move libcall will not clobber any parameters which may have
1921 already been placed on the stack. */
1923 static bool
1924 block_move_libcall_safe_for_call_parm (void)
1926 tree fn;
1928 /* If arguments are pushed on the stack, then they're safe. */
1929 if (targetm.calls.push_argument (0))
1930 return true;
1932 /* If registers go on the stack anyway, any argument is sure to clobber
1933 an outgoing argument. */
1934 #if defined (REG_PARM_STACK_SPACE)
1935 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1936 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1937 depend on its argument. */
1938 (void) fn;
1939 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1940 && REG_PARM_STACK_SPACE (fn) != 0)
1941 return false;
1942 #endif
1944 /* If any argument goes in memory, then it might clobber an outgoing
1945 argument. */
1947 CUMULATIVE_ARGS args_so_far_v;
1948 cumulative_args_t args_so_far;
1949 tree arg;
1951 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1952 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1953 args_so_far = pack_cumulative_args (&args_so_far_v);
1955 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1956 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1958 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1959 function_arg_info arg_info (mode, /*named=*/true);
1960 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1961 if (!tmp || !REG_P (tmp))
1962 return false;
1963 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1964 return false;
1965 targetm.calls.function_arg_advance (args_so_far, arg_info);
1968 return true;
1971 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1972 return true if successful.
1974 X is the destination of the copy or move.
1975 Y is the source of the copy or move.
1976 SIZE is the size of the block to be moved.
1978 MIGHT_OVERLAP indicates this originated with expansion of a
1979 builtin_memmove() and the source and destination blocks may
1980 overlap.
1983 static bool
1984 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
1985 unsigned int expected_align,
1986 HOST_WIDE_INT expected_size,
1987 unsigned HOST_WIDE_INT min_size,
1988 unsigned HOST_WIDE_INT max_size,
1989 unsigned HOST_WIDE_INT probable_max_size,
1990 bool might_overlap)
1992 if (expected_align < align)
1993 expected_align = align;
1994 if (expected_size != -1)
1996 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1997 expected_size = probable_max_size;
1998 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1999 expected_size = min_size;
2002 /* Since this is a move insn, we don't care about volatility. */
2003 temporary_volatile_ok v (true);
2005 /* Try the most limited insn first, because there's no point
2006 including more than one in the machine description unless
2007 the more limited one has some advantage. */
2009 opt_scalar_int_mode mode_iter;
2010 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2012 scalar_int_mode mode = mode_iter.require ();
2013 enum insn_code code;
2014 if (might_overlap)
2015 code = direct_optab_handler (movmem_optab, mode);
2016 else
2017 code = direct_optab_handler (cpymem_optab, mode);
2019 if (code != CODE_FOR_nothing
2020 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2021 here because if SIZE is less than the mode mask, as it is
2022 returned by the macro, it will definitely be less than the
2023 actual mode mask. Since SIZE is within the Pmode address
2024 space, we limit MODE to Pmode. */
2025 && ((CONST_INT_P (size)
2026 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2027 <= (GET_MODE_MASK (mode) >> 1)))
2028 || max_size <= (GET_MODE_MASK (mode) >> 1)
2029 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2031 class expand_operand ops[9];
2032 unsigned int nops;
2034 /* ??? When called via emit_block_move_for_call, it'd be
2035 nice if there were some way to inform the backend, so
2036 that it doesn't fail the expansion because it thinks
2037 emitting the libcall would be more efficient. */
2038 nops = insn_data[(int) code].n_generator_args;
2039 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2041 create_fixed_operand (&ops[0], x);
2042 create_fixed_operand (&ops[1], y);
2043 /* The check above guarantees that this size conversion is valid. */
2044 create_convert_operand_to (&ops[2], size, mode, true);
2045 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2046 if (nops >= 6)
2048 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2049 create_integer_operand (&ops[5], expected_size);
2051 if (nops >= 8)
2053 create_integer_operand (&ops[6], min_size);
2054 /* If we cannot represent the maximal size,
2055 make parameter NULL. */
2056 if ((HOST_WIDE_INT) max_size != -1)
2057 create_integer_operand (&ops[7], max_size);
2058 else
2059 create_fixed_operand (&ops[7], NULL);
2061 if (nops == 9)
2063 /* If we cannot represent the maximal size,
2064 make parameter NULL. */
2065 if ((HOST_WIDE_INT) probable_max_size != -1)
2066 create_integer_operand (&ops[8], probable_max_size);
2067 else
2068 create_fixed_operand (&ops[8], NULL);
2070 if (maybe_expand_insn (code, nops, ops))
2071 return true;
2075 return false;
2078 /* A subroutine of emit_block_move. Copy the data via an explicit
2079 loop. This is used only when libcalls are forbidden. */
2080 /* ??? It'd be nice to copy in hunks larger than QImode. */
2082 static void
2083 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2084 unsigned int align ATTRIBUTE_UNUSED)
2086 rtx_code_label *cmp_label, *top_label;
2087 rtx iter, x_addr, y_addr, tmp;
2088 machine_mode x_addr_mode = get_address_mode (x);
2089 machine_mode y_addr_mode = get_address_mode (y);
2090 machine_mode iter_mode;
2092 iter_mode = GET_MODE (size);
2093 if (iter_mode == VOIDmode)
2094 iter_mode = word_mode;
2096 top_label = gen_label_rtx ();
2097 cmp_label = gen_label_rtx ();
2098 iter = gen_reg_rtx (iter_mode);
2100 emit_move_insn (iter, const0_rtx);
2102 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2103 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2104 do_pending_stack_adjust ();
2106 emit_jump (cmp_label);
2107 emit_label (top_label);
2109 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2110 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2112 if (x_addr_mode != y_addr_mode)
2113 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2114 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2116 x = change_address (x, QImode, x_addr);
2117 y = change_address (y, QImode, y_addr);
2119 emit_move_insn (x, y);
2121 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2122 true, OPTAB_LIB_WIDEN);
2123 if (tmp != iter)
2124 emit_move_insn (iter, tmp);
2126 emit_label (cmp_label);
2128 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2129 true, top_label,
2130 profile_probability::guessed_always ()
2131 .apply_scale (9, 10));
2134 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2135 TAILCALL is true if this is a tail call. */
2138 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2139 rtx size, bool tailcall)
2141 rtx dst_addr, src_addr;
2142 tree call_expr, dst_tree, src_tree, size_tree;
2143 machine_mode size_mode;
2145 /* Since dst and src are passed to a libcall, mark the corresponding
2146 tree EXPR as addressable. */
2147 tree dst_expr = MEM_EXPR (dst);
2148 tree src_expr = MEM_EXPR (src);
2149 if (dst_expr)
2150 mark_addressable (dst_expr);
2151 if (src_expr)
2152 mark_addressable (src_expr);
2154 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2155 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2156 dst_tree = make_tree (ptr_type_node, dst_addr);
2158 src_addr = copy_addr_to_reg (XEXP (src, 0));
2159 src_addr = convert_memory_address (ptr_mode, src_addr);
2160 src_tree = make_tree (ptr_type_node, src_addr);
2162 size_mode = TYPE_MODE (sizetype);
2163 size = convert_to_mode (size_mode, size, 1);
2164 size = copy_to_mode_reg (size_mode, size);
2165 size_tree = make_tree (sizetype, size);
2167 /* It is incorrect to use the libcall calling conventions for calls to
2168 memcpy/memmove/memcmp because they can be provided by the user. */
2169 tree fn = builtin_decl_implicit (fncode);
2170 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2171 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2173 return expand_call (call_expr, NULL_RTX, false);
2176 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2177 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2178 otherwise return null. */
2181 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2182 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2183 HOST_WIDE_INT align)
2185 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2187 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2188 target = NULL_RTX;
2190 class expand_operand ops[5];
2191 create_output_operand (&ops[0], target, insn_mode);
2192 create_fixed_operand (&ops[1], arg1_rtx);
2193 create_fixed_operand (&ops[2], arg2_rtx);
2194 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2195 TYPE_UNSIGNED (arg3_type));
2196 create_integer_operand (&ops[4], align);
2197 if (maybe_expand_insn (icode, 5, ops))
2198 return ops[0].value;
2199 return NULL_RTX;
2202 /* Expand a block compare between X and Y with length LEN using the
2203 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2204 of the expression that was used to calculate the length. ALIGN
2205 gives the known minimum common alignment. */
2207 static rtx
2208 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2209 unsigned align)
2211 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2212 implementing memcmp because it will stop if it encounters two
2213 zero bytes. */
2214 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2216 if (icode == CODE_FOR_nothing)
2217 return NULL_RTX;
2219 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2222 /* Emit code to compare a block Y to a block X. This may be done with
2223 string-compare instructions, with multiple scalar instructions,
2224 or with a library call.
2226 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2227 they are. LEN_TYPE is the type of the expression that was used to
2228 calculate it.
2230 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2231 value of a normal memcmp call, instead we can just compare for equality.
2232 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2233 returning NULL_RTX.
2235 Optionally, the caller can pass a constfn and associated data in Y_CFN
2236 and Y_CFN_DATA. describing that the second operand being compared is a
2237 known constant and how to obtain its data.
2238 Return the result of the comparison, or NULL_RTX if we failed to
2239 perform the operation. */
2242 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2243 bool equality_only, by_pieces_constfn y_cfn,
2244 void *y_cfndata)
2246 rtx result = 0;
2248 if (CONST_INT_P (len) && INTVAL (len) == 0)
2249 return const0_rtx;
2251 gcc_assert (MEM_P (x) && MEM_P (y));
2252 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2253 gcc_assert (align >= BITS_PER_UNIT);
2255 x = adjust_address (x, BLKmode, 0);
2256 y = adjust_address (y, BLKmode, 0);
2258 if (equality_only
2259 && CONST_INT_P (len)
2260 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2261 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2262 y_cfn, y_cfndata);
2263 else
2264 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2266 return result;
2269 /* Copy all or part of a value X into registers starting at REGNO.
2270 The number of registers to be filled is NREGS. */
2272 void
2273 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2275 if (nregs == 0)
2276 return;
2278 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2279 x = validize_mem (force_const_mem (mode, x));
2281 /* See if the machine can do this with a load multiple insn. */
2282 if (targetm.have_load_multiple ())
2284 rtx_insn *last = get_last_insn ();
2285 rtx first = gen_rtx_REG (word_mode, regno);
2286 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2287 GEN_INT (nregs)))
2289 emit_insn (pat);
2290 return;
2292 else
2293 delete_insns_since (last);
2296 for (int i = 0; i < nregs; i++)
2297 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2298 operand_subword_force (x, i, mode));
2301 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2302 The number of registers to be filled is NREGS. */
2304 void
2305 move_block_from_reg (int regno, rtx x, int nregs)
2307 if (nregs == 0)
2308 return;
2310 /* See if the machine can do this with a store multiple insn. */
2311 if (targetm.have_store_multiple ())
2313 rtx_insn *last = get_last_insn ();
2314 rtx first = gen_rtx_REG (word_mode, regno);
2315 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2316 GEN_INT (nregs)))
2318 emit_insn (pat);
2319 return;
2321 else
2322 delete_insns_since (last);
2325 for (int i = 0; i < nregs; i++)
2327 rtx tem = operand_subword (x, i, 1, BLKmode);
2329 gcc_assert (tem);
2331 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2335 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2336 ORIG, where ORIG is a non-consecutive group of registers represented by
2337 a PARALLEL. The clone is identical to the original except in that the
2338 original set of registers is replaced by a new set of pseudo registers.
2339 The new set has the same modes as the original set. */
2342 gen_group_rtx (rtx orig)
2344 int i, length;
2345 rtx *tmps;
2347 gcc_assert (GET_CODE (orig) == PARALLEL);
2349 length = XVECLEN (orig, 0);
2350 tmps = XALLOCAVEC (rtx, length);
2352 /* Skip a NULL entry in first slot. */
2353 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2355 if (i)
2356 tmps[0] = 0;
2358 for (; i < length; i++)
2360 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2361 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2363 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2366 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2369 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2370 except that values are placed in TMPS[i], and must later be moved
2371 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2373 static void
2374 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2375 poly_int64 ssize)
2377 rtx src;
2378 int start, i;
2379 machine_mode m = GET_MODE (orig_src);
2381 gcc_assert (GET_CODE (dst) == PARALLEL);
2383 if (m != VOIDmode
2384 && !SCALAR_INT_MODE_P (m)
2385 && !MEM_P (orig_src)
2386 && GET_CODE (orig_src) != CONCAT)
2388 scalar_int_mode imode;
2389 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2391 src = gen_reg_rtx (imode);
2392 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2394 else
2396 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2397 emit_move_insn (src, orig_src);
2399 emit_group_load_1 (tmps, dst, src, type, ssize);
2400 return;
2403 /* Check for a NULL entry, used to indicate that the parameter goes
2404 both on the stack and in registers. */
2405 if (XEXP (XVECEXP (dst, 0, 0), 0))
2406 start = 0;
2407 else
2408 start = 1;
2410 /* Process the pieces. */
2411 for (i = start; i < XVECLEN (dst, 0); i++)
2413 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2414 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2415 poly_int64 bytelen = GET_MODE_SIZE (mode);
2416 poly_int64 shift = 0;
2418 /* Handle trailing fragments that run over the size of the struct.
2419 It's the target's responsibility to make sure that the fragment
2420 cannot be strictly smaller in some cases and strictly larger
2421 in others. */
2422 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2423 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2425 /* Arrange to shift the fragment to where it belongs.
2426 extract_bit_field loads to the lsb of the reg. */
2427 if (
2428 #ifdef BLOCK_REG_PADDING
2429 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2430 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2431 #else
2432 BYTES_BIG_ENDIAN
2433 #endif
2435 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2436 bytelen = ssize - bytepos;
2437 gcc_assert (maybe_gt (bytelen, 0));
2440 /* If we won't be loading directly from memory, protect the real source
2441 from strange tricks we might play; but make sure that the source can
2442 be loaded directly into the destination. */
2443 src = orig_src;
2444 if (!MEM_P (orig_src)
2445 && (!CONSTANT_P (orig_src)
2446 || (GET_MODE (orig_src) != mode
2447 && GET_MODE (orig_src) != VOIDmode)))
2449 if (GET_MODE (orig_src) == VOIDmode)
2450 src = gen_reg_rtx (mode);
2451 else
2452 src = gen_reg_rtx (GET_MODE (orig_src));
2454 emit_move_insn (src, orig_src);
2457 /* Optimize the access just a bit. */
2458 if (MEM_P (src)
2459 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2460 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2461 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2462 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2464 tmps[i] = gen_reg_rtx (mode);
2465 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2467 else if (COMPLEX_MODE_P (mode)
2468 && GET_MODE (src) == mode
2469 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2470 /* Let emit_move_complex do the bulk of the work. */
2471 tmps[i] = src;
2472 else if (GET_CODE (src) == CONCAT)
2474 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2475 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2476 unsigned int elt;
2477 poly_int64 subpos;
2479 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2480 && known_le (subpos + bytelen, slen0))
2482 /* The following assumes that the concatenated objects all
2483 have the same size. In this case, a simple calculation
2484 can be used to determine the object and the bit field
2485 to be extracted. */
2486 tmps[i] = XEXP (src, elt);
2487 if (maybe_ne (subpos, 0)
2488 || maybe_ne (subpos + bytelen, slen0)
2489 || (!CONSTANT_P (tmps[i])
2490 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2491 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2492 subpos * BITS_PER_UNIT,
2493 1, NULL_RTX, mode, mode, false,
2494 NULL);
2496 else
2498 rtx mem;
2500 gcc_assert (known_eq (bytepos, 0));
2501 mem = assign_stack_temp (GET_MODE (src), slen);
2502 emit_move_insn (mem, src);
2503 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2504 0, 1, NULL_RTX, mode, mode, false,
2505 NULL);
2508 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2509 && XVECLEN (dst, 0) > 1)
2510 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2511 else if (CONSTANT_P (src))
2513 if (known_eq (bytelen, ssize))
2514 tmps[i] = src;
2515 else
2517 rtx first, second;
2519 /* TODO: const_wide_int can have sizes other than this... */
2520 gcc_assert (known_eq (2 * bytelen, ssize));
2521 split_double (src, &first, &second);
2522 if (i)
2523 tmps[i] = second;
2524 else
2525 tmps[i] = first;
2528 else if (REG_P (src) && GET_MODE (src) == mode)
2529 tmps[i] = src;
2530 else
2531 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2532 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2533 mode, mode, false, NULL);
2535 if (maybe_ne (shift, 0))
2536 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2537 shift, tmps[i], 0);
2541 /* Emit code to move a block SRC of type TYPE to a block DST,
2542 where DST is non-consecutive registers represented by a PARALLEL.
2543 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2544 if not known. */
2546 void
2547 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2549 rtx *tmps;
2550 int i;
2552 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2553 emit_group_load_1 (tmps, dst, src, type, ssize);
2555 /* Copy the extracted pieces into the proper (probable) hard regs. */
2556 for (i = 0; i < XVECLEN (dst, 0); i++)
2558 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2559 if (d == NULL)
2560 continue;
2561 emit_move_insn (d, tmps[i]);
2565 /* Similar, but load SRC into new pseudos in a format that looks like
2566 PARALLEL. This can later be fed to emit_group_move to get things
2567 in the right place. */
2570 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2572 rtvec vec;
2573 int i;
2575 vec = rtvec_alloc (XVECLEN (parallel, 0));
2576 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2578 /* Convert the vector to look just like the original PARALLEL, except
2579 with the computed values. */
2580 for (i = 0; i < XVECLEN (parallel, 0); i++)
2582 rtx e = XVECEXP (parallel, 0, i);
2583 rtx d = XEXP (e, 0);
2585 if (d)
2587 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2588 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2590 RTVEC_ELT (vec, i) = e;
2593 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2596 /* Emit code to move a block SRC to block DST, where SRC and DST are
2597 non-consecutive groups of registers, each represented by a PARALLEL. */
2599 void
2600 emit_group_move (rtx dst, rtx src)
2602 int i;
2604 gcc_assert (GET_CODE (src) == PARALLEL
2605 && GET_CODE (dst) == PARALLEL
2606 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2608 /* Skip first entry if NULL. */
2609 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2610 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2611 XEXP (XVECEXP (src, 0, i), 0));
2614 /* Move a group of registers represented by a PARALLEL into pseudos. */
2617 emit_group_move_into_temps (rtx src)
2619 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2620 int i;
2622 for (i = 0; i < XVECLEN (src, 0); i++)
2624 rtx e = XVECEXP (src, 0, i);
2625 rtx d = XEXP (e, 0);
2627 if (d)
2628 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2629 RTVEC_ELT (vec, i) = e;
2632 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2635 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2636 where SRC is non-consecutive registers represented by a PARALLEL.
2637 SSIZE represents the total size of block ORIG_DST, or -1 if not
2638 known. */
2640 void
2641 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2642 poly_int64 ssize)
2644 rtx *tmps, dst;
2645 int start, finish, i;
2646 machine_mode m = GET_MODE (orig_dst);
2648 gcc_assert (GET_CODE (src) == PARALLEL);
2650 if (!SCALAR_INT_MODE_P (m)
2651 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2653 scalar_int_mode imode;
2654 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2656 dst = gen_reg_rtx (imode);
2657 emit_group_store (dst, src, type, ssize);
2658 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2660 else
2662 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2663 emit_group_store (dst, src, type, ssize);
2665 emit_move_insn (orig_dst, dst);
2666 return;
2669 /* Check for a NULL entry, used to indicate that the parameter goes
2670 both on the stack and in registers. */
2671 if (XEXP (XVECEXP (src, 0, 0), 0))
2672 start = 0;
2673 else
2674 start = 1;
2675 finish = XVECLEN (src, 0);
2677 tmps = XALLOCAVEC (rtx, finish);
2679 /* Copy the (probable) hard regs into pseudos. */
2680 for (i = start; i < finish; i++)
2682 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2683 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2685 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2686 emit_move_insn (tmps[i], reg);
2688 else
2689 tmps[i] = reg;
2692 /* If we won't be storing directly into memory, protect the real destination
2693 from strange tricks we might play. */
2694 dst = orig_dst;
2695 if (GET_CODE (dst) == PARALLEL)
2697 rtx temp;
2699 /* We can get a PARALLEL dst if there is a conditional expression in
2700 a return statement. In that case, the dst and src are the same,
2701 so no action is necessary. */
2702 if (rtx_equal_p (dst, src))
2703 return;
2705 /* It is unclear if we can ever reach here, but we may as well handle
2706 it. Allocate a temporary, and split this into a store/load to/from
2707 the temporary. */
2708 temp = assign_stack_temp (GET_MODE (dst), ssize);
2709 emit_group_store (temp, src, type, ssize);
2710 emit_group_load (dst, temp, type, ssize);
2711 return;
2713 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2715 machine_mode outer = GET_MODE (dst);
2716 machine_mode inner;
2717 poly_int64 bytepos;
2718 bool done = false;
2719 rtx temp;
2721 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2722 dst = gen_reg_rtx (outer);
2724 /* Make life a bit easier for combine. */
2725 /* If the first element of the vector is the low part
2726 of the destination mode, use a paradoxical subreg to
2727 initialize the destination. */
2728 if (start < finish)
2730 inner = GET_MODE (tmps[start]);
2731 bytepos = subreg_lowpart_offset (inner, outer);
2732 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2733 bytepos))
2735 temp = simplify_gen_subreg (outer, tmps[start],
2736 inner, 0);
2737 if (temp)
2739 emit_move_insn (dst, temp);
2740 done = true;
2741 start++;
2746 /* If the first element wasn't the low part, try the last. */
2747 if (!done
2748 && start < finish - 1)
2750 inner = GET_MODE (tmps[finish - 1]);
2751 bytepos = subreg_lowpart_offset (inner, outer);
2752 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2753 finish - 1), 1)),
2754 bytepos))
2756 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2757 inner, 0);
2758 if (temp)
2760 emit_move_insn (dst, temp);
2761 done = true;
2762 finish--;
2767 /* Otherwise, simply initialize the result to zero. */
2768 if (!done)
2769 emit_move_insn (dst, CONST0_RTX (outer));
2772 /* Process the pieces. */
2773 for (i = start; i < finish; i++)
2775 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2776 machine_mode mode = GET_MODE (tmps[i]);
2777 poly_int64 bytelen = GET_MODE_SIZE (mode);
2778 poly_uint64 adj_bytelen;
2779 rtx dest = dst;
2781 /* Handle trailing fragments that run over the size of the struct.
2782 It's the target's responsibility to make sure that the fragment
2783 cannot be strictly smaller in some cases and strictly larger
2784 in others. */
2785 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2786 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2787 adj_bytelen = ssize - bytepos;
2788 else
2789 adj_bytelen = bytelen;
2791 if (GET_CODE (dst) == CONCAT)
2793 if (known_le (bytepos + adj_bytelen,
2794 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2795 dest = XEXP (dst, 0);
2796 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2798 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2799 dest = XEXP (dst, 1);
2801 else
2803 machine_mode dest_mode = GET_MODE (dest);
2804 machine_mode tmp_mode = GET_MODE (tmps[i]);
2806 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2808 if (GET_MODE_ALIGNMENT (dest_mode)
2809 >= GET_MODE_ALIGNMENT (tmp_mode))
2811 dest = assign_stack_temp (dest_mode,
2812 GET_MODE_SIZE (dest_mode));
2813 emit_move_insn (adjust_address (dest,
2814 tmp_mode,
2815 bytepos),
2816 tmps[i]);
2817 dst = dest;
2819 else
2821 dest = assign_stack_temp (tmp_mode,
2822 GET_MODE_SIZE (tmp_mode));
2823 emit_move_insn (dest, tmps[i]);
2824 dst = adjust_address (dest, dest_mode, bytepos);
2826 break;
2830 /* Handle trailing fragments that run over the size of the struct. */
2831 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2833 /* store_bit_field always takes its value from the lsb.
2834 Move the fragment to the lsb if it's not already there. */
2835 if (
2836 #ifdef BLOCK_REG_PADDING
2837 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2838 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2839 #else
2840 BYTES_BIG_ENDIAN
2841 #endif
2844 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2845 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2846 shift, tmps[i], 0);
2849 /* Make sure not to write past the end of the struct. */
2850 store_bit_field (dest,
2851 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2852 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2853 VOIDmode, tmps[i], false);
2856 /* Optimize the access just a bit. */
2857 else if (MEM_P (dest)
2858 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2859 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2860 && multiple_p (bytepos * BITS_PER_UNIT,
2861 GET_MODE_ALIGNMENT (mode))
2862 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2863 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2865 else
2866 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2867 0, 0, mode, tmps[i], false);
2870 /* Copy from the pseudo into the (probable) hard reg. */
2871 if (orig_dst != dst)
2872 emit_move_insn (orig_dst, dst);
2875 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2876 of the value stored in X. */
2879 maybe_emit_group_store (rtx x, tree type)
2881 machine_mode mode = TYPE_MODE (type);
2882 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2883 if (GET_CODE (x) == PARALLEL)
2885 rtx result = gen_reg_rtx (mode);
2886 emit_group_store (result, x, type, int_size_in_bytes (type));
2887 return result;
2889 return x;
2892 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2894 This is used on targets that return BLKmode values in registers. */
2896 static void
2897 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2899 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2900 rtx src = NULL, dst = NULL;
2901 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2902 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2903 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2904 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2905 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2906 fixed_size_mode copy_mode;
2908 /* BLKmode registers created in the back-end shouldn't have survived. */
2909 gcc_assert (mode != BLKmode);
2911 /* If the structure doesn't take up a whole number of words, see whether
2912 SRCREG is padded on the left or on the right. If it's on the left,
2913 set PADDING_CORRECTION to the number of bits to skip.
2915 In most ABIs, the structure will be returned at the least end of
2916 the register, which translates to right padding on little-endian
2917 targets and left padding on big-endian targets. The opposite
2918 holds if the structure is returned at the most significant
2919 end of the register. */
2920 if (bytes % UNITS_PER_WORD != 0
2921 && (targetm.calls.return_in_msb (type)
2922 ? !BYTES_BIG_ENDIAN
2923 : BYTES_BIG_ENDIAN))
2924 padding_correction
2925 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2927 /* We can use a single move if we have an exact mode for the size. */
2928 else if (MEM_P (target)
2929 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2930 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2931 && bytes == GET_MODE_SIZE (mode))
2933 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2934 return;
2937 /* And if we additionally have the same mode for a register. */
2938 else if (REG_P (target)
2939 && GET_MODE (target) == mode
2940 && bytes == GET_MODE_SIZE (mode))
2942 emit_move_insn (target, srcreg);
2943 return;
2946 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2947 into a new pseudo which is a full word. */
2948 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2950 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2951 mode = word_mode;
2954 /* Copy the structure BITSIZE bits at a time. If the target lives in
2955 memory, take care of not reading/writing past its end by selecting
2956 a copy mode suited to BITSIZE. This should always be possible given
2957 how it is computed.
2959 If the target lives in register, make sure not to select a copy mode
2960 larger than the mode of the register.
2962 We could probably emit more efficient code for machines which do not use
2963 strict alignment, but it doesn't seem worth the effort at the current
2964 time. */
2966 copy_mode = word_mode;
2967 if (MEM_P (target))
2969 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2970 if (mem_mode.exists ())
2971 copy_mode = mem_mode.require ();
2973 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2974 copy_mode = tmode;
2976 for (bitpos = 0, xbitpos = padding_correction;
2977 bitpos < bytes * BITS_PER_UNIT;
2978 bitpos += bitsize, xbitpos += bitsize)
2980 /* We need a new source operand each time xbitpos is on a
2981 word boundary and when xbitpos == padding_correction
2982 (the first time through). */
2983 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2984 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2986 /* We need a new destination operand each time bitpos is on
2987 a word boundary. */
2988 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2989 dst = target;
2990 else if (bitpos % BITS_PER_WORD == 0)
2991 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2993 /* Use xbitpos for the source extraction (right justified) and
2994 bitpos for the destination store (left justified). */
2995 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2996 extract_bit_field (src, bitsize,
2997 xbitpos % BITS_PER_WORD, 1,
2998 NULL_RTX, copy_mode, copy_mode,
2999 false, NULL),
3000 false);
3004 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3005 register if it contains any data, otherwise return null.
3007 This is used on targets that return BLKmode values in registers. */
3010 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3012 int i, n_regs;
3013 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3014 unsigned int bitsize;
3015 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3016 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3017 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3018 fixed_size_mode dst_mode;
3019 scalar_int_mode min_mode;
3021 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3023 x = expand_normal (src);
3025 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3026 if (bytes == 0)
3027 return NULL_RTX;
3029 /* If the structure doesn't take up a whole number of words, see
3030 whether the register value should be padded on the left or on
3031 the right. Set PADDING_CORRECTION to the number of padding
3032 bits needed on the left side.
3034 In most ABIs, the structure will be returned at the least end of
3035 the register, which translates to right padding on little-endian
3036 targets and left padding on big-endian targets. The opposite
3037 holds if the structure is returned at the most significant
3038 end of the register. */
3039 if (bytes % UNITS_PER_WORD != 0
3040 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3041 ? !BYTES_BIG_ENDIAN
3042 : BYTES_BIG_ENDIAN))
3043 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3044 * BITS_PER_UNIT));
3046 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3047 dst_words = XALLOCAVEC (rtx, n_regs);
3048 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3049 min_mode = smallest_int_mode_for_size (bitsize);
3051 /* Copy the structure BITSIZE bits at a time. */
3052 for (bitpos = 0, xbitpos = padding_correction;
3053 bitpos < bytes * BITS_PER_UNIT;
3054 bitpos += bitsize, xbitpos += bitsize)
3056 /* We need a new destination pseudo each time xbitpos is
3057 on a word boundary and when xbitpos == padding_correction
3058 (the first time through). */
3059 if (xbitpos % BITS_PER_WORD == 0
3060 || xbitpos == padding_correction)
3062 /* Generate an appropriate register. */
3063 dst_word = gen_reg_rtx (word_mode);
3064 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3066 /* Clear the destination before we move anything into it. */
3067 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3070 /* Find the largest integer mode that can be used to copy all or as
3071 many bits as possible of the structure if the target supports larger
3072 copies. There are too many corner cases here w.r.t to alignments on
3073 the read/writes. So if there is any padding just use single byte
3074 operations. */
3075 opt_scalar_int_mode mode_iter;
3076 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3078 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3080 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3081 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3082 && msize <= BITS_PER_WORD)
3083 bitsize = msize;
3084 else
3085 break;
3089 /* We need a new source operand each time bitpos is on a word
3090 boundary. */
3091 if (bitpos % BITS_PER_WORD == 0)
3092 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3094 /* Use bitpos for the source extraction (left justified) and
3095 xbitpos for the destination store (right justified). */
3096 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3097 0, 0, word_mode,
3098 extract_bit_field (src_word, bitsize,
3099 bitpos % BITS_PER_WORD, 1,
3100 NULL_RTX, word_mode, word_mode,
3101 false, NULL),
3102 false);
3105 if (mode == BLKmode)
3107 /* Find the smallest integer mode large enough to hold the
3108 entire structure. */
3109 opt_scalar_int_mode mode_iter;
3110 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3111 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3112 break;
3114 /* A suitable mode should have been found. */
3115 mode = mode_iter.require ();
3118 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3119 dst_mode = word_mode;
3120 else
3121 dst_mode = mode;
3122 dst = gen_reg_rtx (dst_mode);
3124 for (i = 0; i < n_regs; i++)
3125 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3127 if (mode != dst_mode)
3128 dst = gen_lowpart (mode, dst);
3130 return dst;
3133 /* Add a USE expression for REG to the (possibly empty) list pointed
3134 to by CALL_FUSAGE. REG must denote a hard register. */
3136 void
3137 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3139 gcc_assert (REG_P (reg));
3141 if (!HARD_REGISTER_P (reg))
3142 return;
3144 *call_fusage
3145 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3148 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3149 to by CALL_FUSAGE. REG must denote a hard register. */
3151 void
3152 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3154 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3156 *call_fusage
3157 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3160 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3161 starting at REGNO. All of these registers must be hard registers. */
3163 void
3164 use_regs (rtx *call_fusage, int regno, int nregs)
3166 int i;
3168 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3170 for (i = 0; i < nregs; i++)
3171 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3174 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3175 PARALLEL REGS. This is for calls that pass values in multiple
3176 non-contiguous locations. The Irix 6 ABI has examples of this. */
3178 void
3179 use_group_regs (rtx *call_fusage, rtx regs)
3181 int i;
3183 for (i = 0; i < XVECLEN (regs, 0); i++)
3185 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3187 /* A NULL entry means the parameter goes both on the stack and in
3188 registers. This can also be a MEM for targets that pass values
3189 partially on the stack and partially in registers. */
3190 if (reg != 0 && REG_P (reg))
3191 use_reg (call_fusage, reg);
3195 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3196 assigment and the code of the expresion on the RHS is CODE. Return
3197 NULL otherwise. */
3199 static gimple *
3200 get_def_for_expr (tree name, enum tree_code code)
3202 gimple *def_stmt;
3204 if (TREE_CODE (name) != SSA_NAME)
3205 return NULL;
3207 def_stmt = get_gimple_for_ssa_name (name);
3208 if (!def_stmt
3209 || gimple_assign_rhs_code (def_stmt) != code)
3210 return NULL;
3212 return def_stmt;
3215 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3216 assigment and the class of the expresion on the RHS is CLASS. Return
3217 NULL otherwise. */
3219 static gimple *
3220 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3222 gimple *def_stmt;
3224 if (TREE_CODE (name) != SSA_NAME)
3225 return NULL;
3227 def_stmt = get_gimple_for_ssa_name (name);
3228 if (!def_stmt
3229 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3230 return NULL;
3232 return def_stmt;
3235 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3236 its length in bytes. */
3239 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3240 unsigned int expected_align, HOST_WIDE_INT expected_size,
3241 unsigned HOST_WIDE_INT min_size,
3242 unsigned HOST_WIDE_INT max_size,
3243 unsigned HOST_WIDE_INT probable_max_size,
3244 unsigned ctz_size)
3246 machine_mode mode = GET_MODE (object);
3247 unsigned int align;
3249 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3251 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3252 just move a zero. Otherwise, do this a piece at a time. */
3253 poly_int64 size_val;
3254 if (mode != BLKmode
3255 && poly_int_rtx_p (size, &size_val)
3256 && known_eq (size_val, GET_MODE_SIZE (mode)))
3258 rtx zero = CONST0_RTX (mode);
3259 if (zero != NULL)
3261 emit_move_insn (object, zero);
3262 return NULL;
3265 if (COMPLEX_MODE_P (mode))
3267 zero = CONST0_RTX (GET_MODE_INNER (mode));
3268 if (zero != NULL)
3270 write_complex_part (object, zero, 0);
3271 write_complex_part (object, zero, 1);
3272 return NULL;
3277 if (size == const0_rtx)
3278 return NULL;
3280 align = MEM_ALIGN (object);
3282 if (CONST_INT_P (size)
3283 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3284 CLEAR_BY_PIECES,
3285 optimize_insn_for_speed_p ()))
3286 clear_by_pieces (object, INTVAL (size), align);
3287 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3288 expected_align, expected_size,
3289 min_size, max_size, probable_max_size))
3291 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3292 min_size, max_size,
3293 NULL_RTX, 0, align))
3295 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3296 return set_storage_via_libcall (object, size, const0_rtx,
3297 method == BLOCK_OP_TAILCALL);
3298 else
3299 gcc_unreachable ();
3301 return NULL;
3305 clear_storage (rtx object, rtx size, enum block_op_methods method)
3307 unsigned HOST_WIDE_INT max, min = 0;
3308 if (GET_CODE (size) == CONST_INT)
3309 min = max = UINTVAL (size);
3310 else
3311 max = GET_MODE_MASK (GET_MODE (size));
3312 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3316 /* A subroutine of clear_storage. Expand a call to memset.
3317 Return the return value of memset, 0 otherwise. */
3320 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3322 tree call_expr, fn, object_tree, size_tree, val_tree;
3323 machine_mode size_mode;
3325 object = copy_addr_to_reg (XEXP (object, 0));
3326 object_tree = make_tree (ptr_type_node, object);
3328 if (!CONST_INT_P (val))
3329 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3330 val_tree = make_tree (integer_type_node, val);
3332 size_mode = TYPE_MODE (sizetype);
3333 size = convert_to_mode (size_mode, size, 1);
3334 size = copy_to_mode_reg (size_mode, size);
3335 size_tree = make_tree (sizetype, size);
3337 /* It is incorrect to use the libcall calling conventions for calls to
3338 memset because it can be provided by the user. */
3339 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3340 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3341 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3343 return expand_call (call_expr, NULL_RTX, false);
3346 /* Expand a setmem pattern; return true if successful. */
3348 bool
3349 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3350 unsigned int expected_align, HOST_WIDE_INT expected_size,
3351 unsigned HOST_WIDE_INT min_size,
3352 unsigned HOST_WIDE_INT max_size,
3353 unsigned HOST_WIDE_INT probable_max_size)
3355 /* Try the most limited insn first, because there's no point
3356 including more than one in the machine description unless
3357 the more limited one has some advantage. */
3359 if (expected_align < align)
3360 expected_align = align;
3361 if (expected_size != -1)
3363 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3364 expected_size = max_size;
3365 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3366 expected_size = min_size;
3369 opt_scalar_int_mode mode_iter;
3370 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3372 scalar_int_mode mode = mode_iter.require ();
3373 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3375 if (code != CODE_FOR_nothing
3376 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3377 here because if SIZE is less than the mode mask, as it is
3378 returned by the macro, it will definitely be less than the
3379 actual mode mask. Since SIZE is within the Pmode address
3380 space, we limit MODE to Pmode. */
3381 && ((CONST_INT_P (size)
3382 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3383 <= (GET_MODE_MASK (mode) >> 1)))
3384 || max_size <= (GET_MODE_MASK (mode) >> 1)
3385 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3387 class expand_operand ops[9];
3388 unsigned int nops;
3390 nops = insn_data[(int) code].n_generator_args;
3391 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3393 create_fixed_operand (&ops[0], object);
3394 /* The check above guarantees that this size conversion is valid. */
3395 create_convert_operand_to (&ops[1], size, mode, true);
3396 create_convert_operand_from (&ops[2], val, byte_mode, true);
3397 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3398 if (nops >= 6)
3400 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3401 create_integer_operand (&ops[5], expected_size);
3403 if (nops >= 8)
3405 create_integer_operand (&ops[6], min_size);
3406 /* If we cannot represent the maximal size,
3407 make parameter NULL. */
3408 if ((HOST_WIDE_INT) max_size != -1)
3409 create_integer_operand (&ops[7], max_size);
3410 else
3411 create_fixed_operand (&ops[7], NULL);
3413 if (nops == 9)
3415 /* If we cannot represent the maximal size,
3416 make parameter NULL. */
3417 if ((HOST_WIDE_INT) probable_max_size != -1)
3418 create_integer_operand (&ops[8], probable_max_size);
3419 else
3420 create_fixed_operand (&ops[8], NULL);
3422 if (maybe_expand_insn (code, nops, ops))
3423 return true;
3427 return false;
3431 /* Write to one of the components of the complex value CPLX. Write VAL to
3432 the real part if IMAG_P is false, and the imaginary part if its true. */
3434 void
3435 write_complex_part (rtx cplx, rtx val, bool imag_p)
3437 machine_mode cmode;
3438 scalar_mode imode;
3439 unsigned ibitsize;
3441 if (GET_CODE (cplx) == CONCAT)
3443 emit_move_insn (XEXP (cplx, imag_p), val);
3444 return;
3447 cmode = GET_MODE (cplx);
3448 imode = GET_MODE_INNER (cmode);
3449 ibitsize = GET_MODE_BITSIZE (imode);
3451 /* For MEMs simplify_gen_subreg may generate an invalid new address
3452 because, e.g., the original address is considered mode-dependent
3453 by the target, which restricts simplify_subreg from invoking
3454 adjust_address_nv. Instead of preparing fallback support for an
3455 invalid address, we call adjust_address_nv directly. */
3456 if (MEM_P (cplx))
3458 emit_move_insn (adjust_address_nv (cplx, imode,
3459 imag_p ? GET_MODE_SIZE (imode) : 0),
3460 val);
3461 return;
3464 /* If the sub-object is at least word sized, then we know that subregging
3465 will work. This special case is important, since store_bit_field
3466 wants to operate on integer modes, and there's rarely an OImode to
3467 correspond to TCmode. */
3468 if (ibitsize >= BITS_PER_WORD
3469 /* For hard regs we have exact predicates. Assume we can split
3470 the original object if it spans an even number of hard regs.
3471 This special case is important for SCmode on 64-bit platforms
3472 where the natural size of floating-point regs is 32-bit. */
3473 || (REG_P (cplx)
3474 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3475 && REG_NREGS (cplx) % 2 == 0))
3477 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3478 imag_p ? GET_MODE_SIZE (imode) : 0);
3479 if (part)
3481 emit_move_insn (part, val);
3482 return;
3484 else
3485 /* simplify_gen_subreg may fail for sub-word MEMs. */
3486 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3489 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3490 false);
3493 /* Extract one of the components of the complex value CPLX. Extract the
3494 real part if IMAG_P is false, and the imaginary part if it's true. */
3497 read_complex_part (rtx cplx, bool imag_p)
3499 machine_mode cmode;
3500 scalar_mode imode;
3501 unsigned ibitsize;
3503 if (GET_CODE (cplx) == CONCAT)
3504 return XEXP (cplx, imag_p);
3506 cmode = GET_MODE (cplx);
3507 imode = GET_MODE_INNER (cmode);
3508 ibitsize = GET_MODE_BITSIZE (imode);
3510 /* Special case reads from complex constants that got spilled to memory. */
3511 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3513 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3514 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3516 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3517 if (CONSTANT_CLASS_P (part))
3518 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3522 /* For MEMs simplify_gen_subreg may generate an invalid new address
3523 because, e.g., the original address is considered mode-dependent
3524 by the target, which restricts simplify_subreg from invoking
3525 adjust_address_nv. Instead of preparing fallback support for an
3526 invalid address, we call adjust_address_nv directly. */
3527 if (MEM_P (cplx))
3528 return adjust_address_nv (cplx, imode,
3529 imag_p ? GET_MODE_SIZE (imode) : 0);
3531 /* If the sub-object is at least word sized, then we know that subregging
3532 will work. This special case is important, since extract_bit_field
3533 wants to operate on integer modes, and there's rarely an OImode to
3534 correspond to TCmode. */
3535 if (ibitsize >= BITS_PER_WORD
3536 /* For hard regs we have exact predicates. Assume we can split
3537 the original object if it spans an even number of hard regs.
3538 This special case is important for SCmode on 64-bit platforms
3539 where the natural size of floating-point regs is 32-bit. */
3540 || (REG_P (cplx)
3541 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3542 && REG_NREGS (cplx) % 2 == 0))
3544 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3545 imag_p ? GET_MODE_SIZE (imode) : 0);
3546 if (ret)
3547 return ret;
3548 else
3549 /* simplify_gen_subreg may fail for sub-word MEMs. */
3550 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3553 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3554 true, NULL_RTX, imode, imode, false, NULL);
3557 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3558 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3559 represented in NEW_MODE. If FORCE is true, this will never happen, as
3560 we'll force-create a SUBREG if needed. */
3562 static rtx
3563 emit_move_change_mode (machine_mode new_mode,
3564 machine_mode old_mode, rtx x, bool force)
3566 rtx ret;
3568 if (push_operand (x, GET_MODE (x)))
3570 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3571 MEM_COPY_ATTRIBUTES (ret, x);
3573 else if (MEM_P (x))
3575 /* We don't have to worry about changing the address since the
3576 size in bytes is supposed to be the same. */
3577 if (reload_in_progress)
3579 /* Copy the MEM to change the mode and move any
3580 substitutions from the old MEM to the new one. */
3581 ret = adjust_address_nv (x, new_mode, 0);
3582 copy_replacements (x, ret);
3584 else
3585 ret = adjust_address (x, new_mode, 0);
3587 else
3589 /* Note that we do want simplify_subreg's behavior of validating
3590 that the new mode is ok for a hard register. If we were to use
3591 simplify_gen_subreg, we would create the subreg, but would
3592 probably run into the target not being able to implement it. */
3593 /* Except, of course, when FORCE is true, when this is exactly what
3594 we want. Which is needed for CCmodes on some targets. */
3595 if (force)
3596 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3597 else
3598 ret = simplify_subreg (new_mode, x, old_mode, 0);
3601 return ret;
3604 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3605 an integer mode of the same size as MODE. Returns the instruction
3606 emitted, or NULL if such a move could not be generated. */
3608 static rtx_insn *
3609 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3611 scalar_int_mode imode;
3612 enum insn_code code;
3614 /* There must exist a mode of the exact size we require. */
3615 if (!int_mode_for_mode (mode).exists (&imode))
3616 return NULL;
3618 /* The target must support moves in this mode. */
3619 code = optab_handler (mov_optab, imode);
3620 if (code == CODE_FOR_nothing)
3621 return NULL;
3623 x = emit_move_change_mode (imode, mode, x, force);
3624 if (x == NULL_RTX)
3625 return NULL;
3626 y = emit_move_change_mode (imode, mode, y, force);
3627 if (y == NULL_RTX)
3628 return NULL;
3629 return emit_insn (GEN_FCN (code) (x, y));
3632 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3633 Return an equivalent MEM that does not use an auto-increment. */
3636 emit_move_resolve_push (machine_mode mode, rtx x)
3638 enum rtx_code code = GET_CODE (XEXP (x, 0));
3639 rtx temp;
3641 poly_int64 adjust = GET_MODE_SIZE (mode);
3642 #ifdef PUSH_ROUNDING
3643 adjust = PUSH_ROUNDING (adjust);
3644 #endif
3645 if (code == PRE_DEC || code == POST_DEC)
3646 adjust = -adjust;
3647 else if (code == PRE_MODIFY || code == POST_MODIFY)
3649 rtx expr = XEXP (XEXP (x, 0), 1);
3651 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3652 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3653 if (GET_CODE (expr) == MINUS)
3654 val = -val;
3655 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3656 adjust = val;
3659 /* Do not use anti_adjust_stack, since we don't want to update
3660 stack_pointer_delta. */
3661 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3662 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3663 0, OPTAB_LIB_WIDEN);
3664 if (temp != stack_pointer_rtx)
3665 emit_move_insn (stack_pointer_rtx, temp);
3667 switch (code)
3669 case PRE_INC:
3670 case PRE_DEC:
3671 case PRE_MODIFY:
3672 temp = stack_pointer_rtx;
3673 break;
3674 case POST_INC:
3675 case POST_DEC:
3676 case POST_MODIFY:
3677 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3678 break;
3679 default:
3680 gcc_unreachable ();
3683 return replace_equiv_address (x, temp);
3686 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3687 X is known to satisfy push_operand, and MODE is known to be complex.
3688 Returns the last instruction emitted. */
3690 rtx_insn *
3691 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3693 scalar_mode submode = GET_MODE_INNER (mode);
3694 bool imag_first;
3696 #ifdef PUSH_ROUNDING
3697 poly_int64 submodesize = GET_MODE_SIZE (submode);
3699 /* In case we output to the stack, but the size is smaller than the
3700 machine can push exactly, we need to use move instructions. */
3701 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3703 x = emit_move_resolve_push (mode, x);
3704 return emit_move_insn (x, y);
3706 #endif
3708 /* Note that the real part always precedes the imag part in memory
3709 regardless of machine's endianness. */
3710 switch (GET_CODE (XEXP (x, 0)))
3712 case PRE_DEC:
3713 case POST_DEC:
3714 imag_first = true;
3715 break;
3716 case PRE_INC:
3717 case POST_INC:
3718 imag_first = false;
3719 break;
3720 default:
3721 gcc_unreachable ();
3724 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3725 read_complex_part (y, imag_first));
3726 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3727 read_complex_part (y, !imag_first));
3730 /* A subroutine of emit_move_complex. Perform the move from Y to X
3731 via two moves of the parts. Returns the last instruction emitted. */
3733 rtx_insn *
3734 emit_move_complex_parts (rtx x, rtx y)
3736 /* Show the output dies here. This is necessary for SUBREGs
3737 of pseudos since we cannot track their lifetimes correctly;
3738 hard regs shouldn't appear here except as return values. */
3739 if (!reload_completed && !reload_in_progress
3740 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3741 emit_clobber (x);
3743 write_complex_part (x, read_complex_part (y, false), false);
3744 write_complex_part (x, read_complex_part (y, true), true);
3746 return get_last_insn ();
3749 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3750 MODE is known to be complex. Returns the last instruction emitted. */
3752 static rtx_insn *
3753 emit_move_complex (machine_mode mode, rtx x, rtx y)
3755 bool try_int;
3757 /* Need to take special care for pushes, to maintain proper ordering
3758 of the data, and possibly extra padding. */
3759 if (push_operand (x, mode))
3760 return emit_move_complex_push (mode, x, y);
3762 /* See if we can coerce the target into moving both values at once, except
3763 for floating point where we favor moving as parts if this is easy. */
3764 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3765 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3766 && !(REG_P (x)
3767 && HARD_REGISTER_P (x)
3768 && REG_NREGS (x) == 1)
3769 && !(REG_P (y)
3770 && HARD_REGISTER_P (y)
3771 && REG_NREGS (y) == 1))
3772 try_int = false;
3773 /* Not possible if the values are inherently not adjacent. */
3774 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3775 try_int = false;
3776 /* Is possible if both are registers (or subregs of registers). */
3777 else if (register_operand (x, mode) && register_operand (y, mode))
3778 try_int = true;
3779 /* If one of the operands is a memory, and alignment constraints
3780 are friendly enough, we may be able to do combined memory operations.
3781 We do not attempt this if Y is a constant because that combination is
3782 usually better with the by-parts thing below. */
3783 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3784 && (!STRICT_ALIGNMENT
3785 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3786 try_int = true;
3787 else
3788 try_int = false;
3790 if (try_int)
3792 rtx_insn *ret;
3794 /* For memory to memory moves, optimal behavior can be had with the
3795 existing block move logic. But use normal expansion if optimizing
3796 for size. */
3797 if (MEM_P (x) && MEM_P (y))
3799 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3800 (optimize_insn_for_speed_p()
3801 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3802 return get_last_insn ();
3805 ret = emit_move_via_integer (mode, x, y, true);
3806 if (ret)
3807 return ret;
3810 return emit_move_complex_parts (x, y);
3813 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3814 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3816 static rtx_insn *
3817 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3819 rtx_insn *ret;
3821 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3822 if (mode != CCmode)
3824 enum insn_code code = optab_handler (mov_optab, CCmode);
3825 if (code != CODE_FOR_nothing)
3827 x = emit_move_change_mode (CCmode, mode, x, true);
3828 y = emit_move_change_mode (CCmode, mode, y, true);
3829 return emit_insn (GEN_FCN (code) (x, y));
3833 /* Otherwise, find the MODE_INT mode of the same width. */
3834 ret = emit_move_via_integer (mode, x, y, false);
3835 gcc_assert (ret != NULL);
3836 return ret;
3839 /* Return true if word I of OP lies entirely in the
3840 undefined bits of a paradoxical subreg. */
3842 static bool
3843 undefined_operand_subword_p (const_rtx op, int i)
3845 if (GET_CODE (op) != SUBREG)
3846 return false;
3847 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3848 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3849 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3850 || known_le (offset, -UNITS_PER_WORD));
3853 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3854 MODE is any multi-word or full-word mode that lacks a move_insn
3855 pattern. Note that you will get better code if you define such
3856 patterns, even if they must turn into multiple assembler instructions. */
3858 static rtx_insn *
3859 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3861 rtx_insn *last_insn = 0;
3862 rtx_insn *seq;
3863 rtx inner;
3864 bool need_clobber;
3865 int i, mode_size;
3867 /* This function can only handle cases where the number of words is
3868 known at compile time. */
3869 mode_size = GET_MODE_SIZE (mode).to_constant ();
3870 gcc_assert (mode_size >= UNITS_PER_WORD);
3872 /* If X is a push on the stack, do the push now and replace
3873 X with a reference to the stack pointer. */
3874 if (push_operand (x, mode))
3875 x = emit_move_resolve_push (mode, x);
3877 /* If we are in reload, see if either operand is a MEM whose address
3878 is scheduled for replacement. */
3879 if (reload_in_progress && MEM_P (x)
3880 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3881 x = replace_equiv_address_nv (x, inner);
3882 if (reload_in_progress && MEM_P (y)
3883 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3884 y = replace_equiv_address_nv (y, inner);
3886 start_sequence ();
3888 need_clobber = false;
3889 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3891 /* Do not generate code for a move if it would go entirely
3892 to the non-existing bits of a paradoxical subreg. */
3893 if (undefined_operand_subword_p (x, i))
3894 continue;
3896 rtx xpart = operand_subword (x, i, 1, mode);
3897 rtx ypart;
3899 /* Do not generate code for a move if it would come entirely
3900 from the undefined bits of a paradoxical subreg. */
3901 if (undefined_operand_subword_p (y, i))
3902 continue;
3904 ypart = operand_subword (y, i, 1, mode);
3906 /* If we can't get a part of Y, put Y into memory if it is a
3907 constant. Otherwise, force it into a register. Then we must
3908 be able to get a part of Y. */
3909 if (ypart == 0 && CONSTANT_P (y))
3911 y = use_anchored_address (force_const_mem (mode, y));
3912 ypart = operand_subword (y, i, 1, mode);
3914 else if (ypart == 0)
3915 ypart = operand_subword_force (y, i, mode);
3917 gcc_assert (xpart && ypart);
3919 need_clobber |= (GET_CODE (xpart) == SUBREG);
3921 last_insn = emit_move_insn (xpart, ypart);
3924 seq = get_insns ();
3925 end_sequence ();
3927 /* Show the output dies here. This is necessary for SUBREGs
3928 of pseudos since we cannot track their lifetimes correctly;
3929 hard regs shouldn't appear here except as return values.
3930 We never want to emit such a clobber after reload. */
3931 if (x != y
3932 && ! (lra_in_progress || reload_in_progress || reload_completed)
3933 && need_clobber != 0)
3934 emit_clobber (x);
3936 emit_insn (seq);
3938 return last_insn;
3941 /* Low level part of emit_move_insn.
3942 Called just like emit_move_insn, but assumes X and Y
3943 are basically valid. */
3945 rtx_insn *
3946 emit_move_insn_1 (rtx x, rtx y)
3948 machine_mode mode = GET_MODE (x);
3949 enum insn_code code;
3951 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3953 code = optab_handler (mov_optab, mode);
3954 if (code != CODE_FOR_nothing)
3955 return emit_insn (GEN_FCN (code) (x, y));
3957 /* Expand complex moves by moving real part and imag part. */
3958 if (COMPLEX_MODE_P (mode))
3959 return emit_move_complex (mode, x, y);
3961 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3962 || ALL_FIXED_POINT_MODE_P (mode))
3964 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3966 /* If we can't find an integer mode, use multi words. */
3967 if (result)
3968 return result;
3969 else
3970 return emit_move_multi_word (mode, x, y);
3973 if (GET_MODE_CLASS (mode) == MODE_CC)
3974 return emit_move_ccmode (mode, x, y);
3976 /* Try using a move pattern for the corresponding integer mode. This is
3977 only safe when simplify_subreg can convert MODE constants into integer
3978 constants. At present, it can only do this reliably if the value
3979 fits within a HOST_WIDE_INT. */
3980 if (!CONSTANT_P (y)
3981 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3983 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3985 if (ret)
3987 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3988 return ret;
3992 return emit_move_multi_word (mode, x, y);
3995 /* Generate code to copy Y into X.
3996 Both Y and X must have the same mode, except that
3997 Y can be a constant with VOIDmode.
3998 This mode cannot be BLKmode; use emit_block_move for that.
4000 Return the last instruction emitted. */
4002 rtx_insn *
4003 emit_move_insn (rtx x, rtx y)
4005 machine_mode mode = GET_MODE (x);
4006 rtx y_cst = NULL_RTX;
4007 rtx_insn *last_insn;
4008 rtx set;
4010 gcc_assert (mode != BLKmode
4011 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4013 /* If we have a copy that looks like one of the following patterns:
4014 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4015 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4016 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4017 (set (subreg:M1 (reg:M2 ...)) (constant C))
4018 where mode M1 is equal in size to M2, try to detect whether the
4019 mode change involves an implicit round trip through memory.
4020 If so, see if we can avoid that by removing the subregs and
4021 doing the move in mode M2 instead. */
4023 rtx x_inner = NULL_RTX;
4024 rtx y_inner = NULL_RTX;
4026 auto candidate_subreg_p = [&](rtx subreg) {
4027 return (REG_P (SUBREG_REG (subreg))
4028 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4029 GET_MODE_SIZE (GET_MODE (subreg)))
4030 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4031 != CODE_FOR_nothing);
4034 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4035 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4036 && !push_operand (mem, GET_MODE (mem))
4037 /* Not a candiate if innermode requires too much alignment. */
4038 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4039 || targetm.slow_unaligned_access (GET_MODE (mem),
4040 MEM_ALIGN (mem))
4041 || !targetm.slow_unaligned_access (innermode,
4042 MEM_ALIGN (mem))));
4045 if (SUBREG_P (x) && candidate_subreg_p (x))
4046 x_inner = SUBREG_REG (x);
4048 if (SUBREG_P (y) && candidate_subreg_p (y))
4049 y_inner = SUBREG_REG (y);
4051 if (x_inner != NULL_RTX
4052 && y_inner != NULL_RTX
4053 && GET_MODE (x_inner) == GET_MODE (y_inner)
4054 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4056 x = x_inner;
4057 y = y_inner;
4058 mode = GET_MODE (x_inner);
4060 else if (x_inner != NULL_RTX
4061 && MEM_P (y)
4062 && candidate_mem_p (GET_MODE (x_inner), y))
4064 x = x_inner;
4065 y = adjust_address (y, GET_MODE (x_inner), 0);
4066 mode = GET_MODE (x_inner);
4068 else if (y_inner != NULL_RTX
4069 && MEM_P (x)
4070 && candidate_mem_p (GET_MODE (y_inner), x))
4072 x = adjust_address (x, GET_MODE (y_inner), 0);
4073 y = y_inner;
4074 mode = GET_MODE (y_inner);
4076 else if (x_inner != NULL_RTX
4077 && CONSTANT_P (y)
4078 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4079 mode, ALL_REGS)
4080 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4082 x = x_inner;
4083 y = y_inner;
4084 mode = GET_MODE (x_inner);
4087 if (CONSTANT_P (y))
4089 if (optimize
4090 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4091 && (last_insn = compress_float_constant (x, y)))
4092 return last_insn;
4094 y_cst = y;
4096 if (!targetm.legitimate_constant_p (mode, y))
4098 y = force_const_mem (mode, y);
4100 /* If the target's cannot_force_const_mem prevented the spill,
4101 assume that the target's move expanders will also take care
4102 of the non-legitimate constant. */
4103 if (!y)
4104 y = y_cst;
4105 else
4106 y = use_anchored_address (y);
4110 /* If X or Y are memory references, verify that their addresses are valid
4111 for the machine. */
4112 if (MEM_P (x)
4113 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4114 MEM_ADDR_SPACE (x))
4115 && ! push_operand (x, GET_MODE (x))))
4116 x = validize_mem (x);
4118 if (MEM_P (y)
4119 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4120 MEM_ADDR_SPACE (y)))
4121 y = validize_mem (y);
4123 gcc_assert (mode != BLKmode);
4125 last_insn = emit_move_insn_1 (x, y);
4127 if (y_cst && REG_P (x)
4128 && (set = single_set (last_insn)) != NULL_RTX
4129 && SET_DEST (set) == x
4130 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4131 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4133 return last_insn;
4136 /* Generate the body of an instruction to copy Y into X.
4137 It may be a list of insns, if one insn isn't enough. */
4139 rtx_insn *
4140 gen_move_insn (rtx x, rtx y)
4142 rtx_insn *seq;
4144 start_sequence ();
4145 emit_move_insn_1 (x, y);
4146 seq = get_insns ();
4147 end_sequence ();
4148 return seq;
4151 /* If Y is representable exactly in a narrower mode, and the target can
4152 perform the extension directly from constant or memory, then emit the
4153 move as an extension. */
4155 static rtx_insn *
4156 compress_float_constant (rtx x, rtx y)
4158 machine_mode dstmode = GET_MODE (x);
4159 machine_mode orig_srcmode = GET_MODE (y);
4160 machine_mode srcmode;
4161 const REAL_VALUE_TYPE *r;
4162 int oldcost, newcost;
4163 bool speed = optimize_insn_for_speed_p ();
4165 r = CONST_DOUBLE_REAL_VALUE (y);
4167 if (targetm.legitimate_constant_p (dstmode, y))
4168 oldcost = set_src_cost (y, orig_srcmode, speed);
4169 else
4170 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4172 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4174 enum insn_code ic;
4175 rtx trunc_y;
4176 rtx_insn *last_insn;
4178 /* Skip if the target can't extend this way. */
4179 ic = can_extend_p (dstmode, srcmode, 0);
4180 if (ic == CODE_FOR_nothing)
4181 continue;
4183 /* Skip if the narrowed value isn't exact. */
4184 if (! exact_real_truncate (srcmode, r))
4185 continue;
4187 trunc_y = const_double_from_real_value (*r, srcmode);
4189 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4191 /* Skip if the target needs extra instructions to perform
4192 the extension. */
4193 if (!insn_operand_matches (ic, 1, trunc_y))
4194 continue;
4195 /* This is valid, but may not be cheaper than the original. */
4196 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4197 dstmode, speed);
4198 if (oldcost < newcost)
4199 continue;
4201 else if (float_extend_from_mem[dstmode][srcmode])
4203 trunc_y = force_const_mem (srcmode, trunc_y);
4204 /* This is valid, but may not be cheaper than the original. */
4205 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4206 dstmode, speed);
4207 if (oldcost < newcost)
4208 continue;
4209 trunc_y = validize_mem (trunc_y);
4211 else
4212 continue;
4214 /* For CSE's benefit, force the compressed constant pool entry
4215 into a new pseudo. This constant may be used in different modes,
4216 and if not, combine will put things back together for us. */
4217 trunc_y = force_reg (srcmode, trunc_y);
4219 /* If x is a hard register, perform the extension into a pseudo,
4220 so that e.g. stack realignment code is aware of it. */
4221 rtx target = x;
4222 if (REG_P (x) && HARD_REGISTER_P (x))
4223 target = gen_reg_rtx (dstmode);
4225 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4226 last_insn = get_last_insn ();
4228 if (REG_P (target))
4229 set_unique_reg_note (last_insn, REG_EQUAL, y);
4231 if (target != x)
4232 return emit_move_insn (x, target);
4233 return last_insn;
4236 return NULL;
4239 /* Pushing data onto the stack. */
4241 /* Push a block of length SIZE (perhaps variable)
4242 and return an rtx to address the beginning of the block.
4243 The value may be virtual_outgoing_args_rtx.
4245 EXTRA is the number of bytes of padding to push in addition to SIZE.
4246 BELOW nonzero means this padding comes at low addresses;
4247 otherwise, the padding comes at high addresses. */
4250 push_block (rtx size, poly_int64 extra, int below)
4252 rtx temp;
4254 size = convert_modes (Pmode, ptr_mode, size, 1);
4255 if (CONSTANT_P (size))
4256 anti_adjust_stack (plus_constant (Pmode, size, extra));
4257 else if (REG_P (size) && known_eq (extra, 0))
4258 anti_adjust_stack (size);
4259 else
4261 temp = copy_to_mode_reg (Pmode, size);
4262 if (maybe_ne (extra, 0))
4263 temp = expand_binop (Pmode, add_optab, temp,
4264 gen_int_mode (extra, Pmode),
4265 temp, 0, OPTAB_LIB_WIDEN);
4266 anti_adjust_stack (temp);
4269 if (STACK_GROWS_DOWNWARD)
4271 temp = virtual_outgoing_args_rtx;
4272 if (maybe_ne (extra, 0) && below)
4273 temp = plus_constant (Pmode, temp, extra);
4275 else
4277 poly_int64 csize;
4278 if (poly_int_rtx_p (size, &csize))
4279 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4280 -csize - (below ? 0 : extra));
4281 else if (maybe_ne (extra, 0) && !below)
4282 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4283 negate_rtx (Pmode, plus_constant (Pmode, size,
4284 extra)));
4285 else
4286 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4287 negate_rtx (Pmode, size));
4290 return memory_address (NARROWEST_INT_MODE, temp);
4293 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4295 static rtx
4296 mem_autoinc_base (rtx mem)
4298 if (MEM_P (mem))
4300 rtx addr = XEXP (mem, 0);
4301 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4302 return XEXP (addr, 0);
4304 return NULL;
4307 /* A utility routine used here, in reload, and in try_split. The insns
4308 after PREV up to and including LAST are known to adjust the stack,
4309 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4310 placing notes as appropriate. PREV may be NULL, indicating the
4311 entire insn sequence prior to LAST should be scanned.
4313 The set of allowed stack pointer modifications is small:
4314 (1) One or more auto-inc style memory references (aka pushes),
4315 (2) One or more addition/subtraction with the SP as destination,
4316 (3) A single move insn with the SP as destination,
4317 (4) A call_pop insn,
4318 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4320 Insns in the sequence that do not modify the SP are ignored,
4321 except for noreturn calls.
4323 The return value is the amount of adjustment that can be trivially
4324 verified, via immediate operand or auto-inc. If the adjustment
4325 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4327 poly_int64
4328 find_args_size_adjust (rtx_insn *insn)
4330 rtx dest, set, pat;
4331 int i;
4333 pat = PATTERN (insn);
4334 set = NULL;
4336 /* Look for a call_pop pattern. */
4337 if (CALL_P (insn))
4339 /* We have to allow non-call_pop patterns for the case
4340 of emit_single_push_insn of a TLS address. */
4341 if (GET_CODE (pat) != PARALLEL)
4342 return 0;
4344 /* All call_pop have a stack pointer adjust in the parallel.
4345 The call itself is always first, and the stack adjust is
4346 usually last, so search from the end. */
4347 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4349 set = XVECEXP (pat, 0, i);
4350 if (GET_CODE (set) != SET)
4351 continue;
4352 dest = SET_DEST (set);
4353 if (dest == stack_pointer_rtx)
4354 break;
4356 /* We'd better have found the stack pointer adjust. */
4357 if (i == 0)
4358 return 0;
4359 /* Fall through to process the extracted SET and DEST
4360 as if it was a standalone insn. */
4362 else if (GET_CODE (pat) == SET)
4363 set = pat;
4364 else if ((set = single_set (insn)) != NULL)
4366 else if (GET_CODE (pat) == PARALLEL)
4368 /* ??? Some older ports use a parallel with a stack adjust
4369 and a store for a PUSH_ROUNDING pattern, rather than a
4370 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4371 /* ??? See h8300 and m68k, pushqi1. */
4372 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4374 set = XVECEXP (pat, 0, i);
4375 if (GET_CODE (set) != SET)
4376 continue;
4377 dest = SET_DEST (set);
4378 if (dest == stack_pointer_rtx)
4379 break;
4381 /* We do not expect an auto-inc of the sp in the parallel. */
4382 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4383 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4384 != stack_pointer_rtx);
4386 if (i < 0)
4387 return 0;
4389 else
4390 return 0;
4392 dest = SET_DEST (set);
4394 /* Look for direct modifications of the stack pointer. */
4395 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4397 /* Look for a trivial adjustment, otherwise assume nothing. */
4398 /* Note that the SPU restore_stack_block pattern refers to
4399 the stack pointer in V4SImode. Consider that non-trivial. */
4400 poly_int64 offset;
4401 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4402 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4403 return offset;
4404 /* ??? Reload can generate no-op moves, which will be cleaned
4405 up later. Recognize it and continue searching. */
4406 else if (rtx_equal_p (dest, SET_SRC (set)))
4407 return 0;
4408 else
4409 return HOST_WIDE_INT_MIN;
4411 else
4413 rtx mem, addr;
4415 /* Otherwise only think about autoinc patterns. */
4416 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4418 mem = dest;
4419 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4420 != stack_pointer_rtx);
4422 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4423 mem = SET_SRC (set);
4424 else
4425 return 0;
4427 addr = XEXP (mem, 0);
4428 switch (GET_CODE (addr))
4430 case PRE_INC:
4431 case POST_INC:
4432 return GET_MODE_SIZE (GET_MODE (mem));
4433 case PRE_DEC:
4434 case POST_DEC:
4435 return -GET_MODE_SIZE (GET_MODE (mem));
4436 case PRE_MODIFY:
4437 case POST_MODIFY:
4438 addr = XEXP (addr, 1);
4439 gcc_assert (GET_CODE (addr) == PLUS);
4440 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4441 return rtx_to_poly_int64 (XEXP (addr, 1));
4442 default:
4443 gcc_unreachable ();
4448 poly_int64
4449 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4450 poly_int64 end_args_size)
4452 poly_int64 args_size = end_args_size;
4453 bool saw_unknown = false;
4454 rtx_insn *insn;
4456 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4458 if (!NONDEBUG_INSN_P (insn))
4459 continue;
4461 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4462 a call argument containing a TLS address that itself requires
4463 a call to __tls_get_addr. The handling of stack_pointer_delta
4464 in emit_single_push_insn is supposed to ensure that any such
4465 notes are already correct. */
4466 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4467 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4469 poly_int64 this_delta = find_args_size_adjust (insn);
4470 if (known_eq (this_delta, 0))
4472 if (!CALL_P (insn)
4473 || ACCUMULATE_OUTGOING_ARGS
4474 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4475 continue;
4478 gcc_assert (!saw_unknown);
4479 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4480 saw_unknown = true;
4482 if (!note)
4483 add_args_size_note (insn, args_size);
4484 if (STACK_GROWS_DOWNWARD)
4485 this_delta = -poly_uint64 (this_delta);
4487 if (saw_unknown)
4488 args_size = HOST_WIDE_INT_MIN;
4489 else
4490 args_size -= this_delta;
4493 return args_size;
4496 #ifdef PUSH_ROUNDING
4497 /* Emit single push insn. */
4499 static void
4500 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4502 rtx dest_addr;
4503 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4504 rtx dest;
4505 enum insn_code icode;
4507 /* If there is push pattern, use it. Otherwise try old way of throwing
4508 MEM representing push operation to move expander. */
4509 icode = optab_handler (push_optab, mode);
4510 if (icode != CODE_FOR_nothing)
4512 class expand_operand ops[1];
4514 create_input_operand (&ops[0], x, mode);
4515 if (maybe_expand_insn (icode, 1, ops))
4516 return;
4518 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4519 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4520 /* If we are to pad downward, adjust the stack pointer first and
4521 then store X into the stack location using an offset. This is
4522 because emit_move_insn does not know how to pad; it does not have
4523 access to type. */
4524 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4526 emit_move_insn (stack_pointer_rtx,
4527 expand_binop (Pmode,
4528 STACK_GROWS_DOWNWARD ? sub_optab
4529 : add_optab,
4530 stack_pointer_rtx,
4531 gen_int_mode (rounded_size, Pmode),
4532 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4534 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4535 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4536 /* We have already decremented the stack pointer, so get the
4537 previous value. */
4538 offset += rounded_size;
4540 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4541 /* We have already incremented the stack pointer, so get the
4542 previous value. */
4543 offset -= rounded_size;
4545 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4547 else
4549 if (STACK_GROWS_DOWNWARD)
4550 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4551 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4552 else
4553 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4554 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4556 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4559 dest = gen_rtx_MEM (mode, dest_addr);
4561 if (type != 0)
4563 set_mem_attributes (dest, type, 1);
4565 if (cfun->tail_call_marked)
4566 /* Function incoming arguments may overlap with sibling call
4567 outgoing arguments and we cannot allow reordering of reads
4568 from function arguments with stores to outgoing arguments
4569 of sibling calls. */
4570 set_mem_alias_set (dest, 0);
4572 emit_move_insn (dest, x);
4575 /* Emit and annotate a single push insn. */
4577 static void
4578 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4580 poly_int64 delta, old_delta = stack_pointer_delta;
4581 rtx_insn *prev = get_last_insn ();
4582 rtx_insn *last;
4584 emit_single_push_insn_1 (mode, x, type);
4586 /* Adjust stack_pointer_delta to describe the situation after the push
4587 we just performed. Note that we must do this after the push rather
4588 than before the push in case calculating X needs pushes and pops of
4589 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4590 for such pushes and pops must not include the effect of the future
4591 push of X. */
4592 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4594 last = get_last_insn ();
4596 /* Notice the common case where we emitted exactly one insn. */
4597 if (PREV_INSN (last) == prev)
4599 add_args_size_note (last, stack_pointer_delta);
4600 return;
4603 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4604 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4605 || known_eq (delta, old_delta));
4607 #endif
4609 /* If reading SIZE bytes from X will end up reading from
4610 Y return the number of bytes that overlap. Return -1
4611 if there is no overlap or -2 if we can't determine
4612 (for example when X and Y have different base registers). */
4614 static int
4615 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4617 rtx tmp = plus_constant (Pmode, x, size);
4618 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4620 if (!CONST_INT_P (sub))
4621 return -2;
4623 HOST_WIDE_INT val = INTVAL (sub);
4625 return IN_RANGE (val, 1, size) ? val : -1;
4628 /* Generate code to push X onto the stack, assuming it has mode MODE and
4629 type TYPE.
4630 MODE is redundant except when X is a CONST_INT (since they don't
4631 carry mode info).
4632 SIZE is an rtx for the size of data to be copied (in bytes),
4633 needed only if X is BLKmode.
4634 Return true if successful. May return false if asked to push a
4635 partial argument during a sibcall optimization (as specified by
4636 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4637 to not overlap.
4639 ALIGN (in bits) is maximum alignment we can assume.
4641 If PARTIAL and REG are both nonzero, then copy that many of the first
4642 bytes of X into registers starting with REG, and push the rest of X.
4643 The amount of space pushed is decreased by PARTIAL bytes.
4644 REG must be a hard register in this case.
4645 If REG is zero but PARTIAL is not, take any all others actions for an
4646 argument partially in registers, but do not actually load any
4647 registers.
4649 EXTRA is the amount in bytes of extra space to leave next to this arg.
4650 This is ignored if an argument block has already been allocated.
4652 On a machine that lacks real push insns, ARGS_ADDR is the address of
4653 the bottom of the argument block for this call. We use indexing off there
4654 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4655 argument block has not been preallocated.
4657 ARGS_SO_FAR is the size of args previously pushed for this call.
4659 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4660 for arguments passed in registers. If nonzero, it will be the number
4661 of bytes required. */
4663 bool
4664 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4665 unsigned int align, int partial, rtx reg, poly_int64 extra,
4666 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4667 rtx alignment_pad, bool sibcall_p)
4669 rtx xinner;
4670 pad_direction stack_direction
4671 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4673 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4674 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4675 Default is below for small data on big-endian machines; else above. */
4676 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4678 /* Invert direction if stack is post-decrement.
4679 FIXME: why? */
4680 if (STACK_PUSH_CODE == POST_DEC)
4681 if (where_pad != PAD_NONE)
4682 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4684 xinner = x;
4686 int nregs = partial / UNITS_PER_WORD;
4687 rtx *tmp_regs = NULL;
4688 int overlapping = 0;
4690 if (mode == BLKmode
4691 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4693 /* Copy a block into the stack, entirely or partially. */
4695 rtx temp;
4696 int used;
4697 int offset;
4698 int skip;
4700 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4701 used = partial - offset;
4703 if (mode != BLKmode)
4705 /* A value is to be stored in an insufficiently aligned
4706 stack slot; copy via a suitably aligned slot if
4707 necessary. */
4708 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4709 if (!MEM_P (xinner))
4711 temp = assign_temp (type, 1, 1);
4712 emit_move_insn (temp, xinner);
4713 xinner = temp;
4717 gcc_assert (size);
4719 /* USED is now the # of bytes we need not copy to the stack
4720 because registers will take care of them. */
4722 if (partial != 0)
4723 xinner = adjust_address (xinner, BLKmode, used);
4725 /* If the partial register-part of the arg counts in its stack size,
4726 skip the part of stack space corresponding to the registers.
4727 Otherwise, start copying to the beginning of the stack space,
4728 by setting SKIP to 0. */
4729 skip = (reg_parm_stack_space == 0) ? 0 : used;
4731 #ifdef PUSH_ROUNDING
4732 /* NB: Let the backend known the number of bytes to push and
4733 decide if push insns should be generated. */
4734 unsigned int push_size;
4735 if (CONST_INT_P (size))
4736 push_size = INTVAL (size);
4737 else
4738 push_size = 0;
4740 /* Do it with several push insns if that doesn't take lots of insns
4741 and if there is no difficulty with push insns that skip bytes
4742 on the stack for alignment purposes. */
4743 if (args_addr == 0
4744 && targetm.calls.push_argument (push_size)
4745 && CONST_INT_P (size)
4746 && skip == 0
4747 && MEM_ALIGN (xinner) >= align
4748 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4749 /* Here we avoid the case of a structure whose weak alignment
4750 forces many pushes of a small amount of data,
4751 and such small pushes do rounding that causes trouble. */
4752 && ((!targetm.slow_unaligned_access (word_mode, align))
4753 || align >= BIGGEST_ALIGNMENT
4754 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4755 align / BITS_PER_UNIT))
4756 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4758 /* Push padding now if padding above and stack grows down,
4759 or if padding below and stack grows up.
4760 But if space already allocated, this has already been done. */
4761 if (maybe_ne (extra, 0)
4762 && args_addr == 0
4763 && where_pad != PAD_NONE
4764 && where_pad != stack_direction)
4765 anti_adjust_stack (gen_int_mode (extra, Pmode));
4767 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4768 RETURN_BEGIN);
4770 else
4771 #endif /* PUSH_ROUNDING */
4773 rtx target;
4775 /* Otherwise make space on the stack and copy the data
4776 to the address of that space. */
4778 /* Deduct words put into registers from the size we must copy. */
4779 if (partial != 0)
4781 if (CONST_INT_P (size))
4782 size = GEN_INT (INTVAL (size) - used);
4783 else
4784 size = expand_binop (GET_MODE (size), sub_optab, size,
4785 gen_int_mode (used, GET_MODE (size)),
4786 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4789 /* Get the address of the stack space.
4790 In this case, we do not deal with EXTRA separately.
4791 A single stack adjust will do. */
4792 poly_int64 const_args_so_far;
4793 if (! args_addr)
4795 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4796 extra = 0;
4798 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4799 temp = memory_address (BLKmode,
4800 plus_constant (Pmode, args_addr,
4801 skip + const_args_so_far));
4802 else
4803 temp = memory_address (BLKmode,
4804 plus_constant (Pmode,
4805 gen_rtx_PLUS (Pmode,
4806 args_addr,
4807 args_so_far),
4808 skip));
4810 if (!ACCUMULATE_OUTGOING_ARGS)
4812 /* If the source is referenced relative to the stack pointer,
4813 copy it to another register to stabilize it. We do not need
4814 to do this if we know that we won't be changing sp. */
4816 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4817 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4818 temp = copy_to_reg (temp);
4821 target = gen_rtx_MEM (BLKmode, temp);
4823 /* We do *not* set_mem_attributes here, because incoming arguments
4824 may overlap with sibling call outgoing arguments and we cannot
4825 allow reordering of reads from function arguments with stores
4826 to outgoing arguments of sibling calls. We do, however, want
4827 to record the alignment of the stack slot. */
4828 /* ALIGN may well be better aligned than TYPE, e.g. due to
4829 PARM_BOUNDARY. Assume the caller isn't lying. */
4830 set_mem_align (target, align);
4832 /* If part should go in registers and pushing to that part would
4833 overwrite some of the values that need to go into regs, load the
4834 overlapping values into temporary pseudos to be moved into the hard
4835 regs at the end after the stack pushing has completed.
4836 We cannot load them directly into the hard regs here because
4837 they can be clobbered by the block move expansions.
4838 See PR 65358. */
4840 if (partial > 0 && reg != 0 && mode == BLKmode
4841 && GET_CODE (reg) != PARALLEL)
4843 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4844 if (overlapping > 0)
4846 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4847 overlapping /= UNITS_PER_WORD;
4849 tmp_regs = XALLOCAVEC (rtx, overlapping);
4851 for (int i = 0; i < overlapping; i++)
4852 tmp_regs[i] = gen_reg_rtx (word_mode);
4854 for (int i = 0; i < overlapping; i++)
4855 emit_move_insn (tmp_regs[i],
4856 operand_subword_force (target, i, mode));
4858 else if (overlapping == -1)
4859 overlapping = 0;
4860 /* Could not determine whether there is overlap.
4861 Fail the sibcall. */
4862 else
4864 overlapping = 0;
4865 if (sibcall_p)
4866 return false;
4869 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4872 else if (partial > 0)
4874 /* Scalar partly in registers. This case is only supported
4875 for fixed-wdth modes. */
4876 int num_words = GET_MODE_SIZE (mode).to_constant ();
4877 num_words /= UNITS_PER_WORD;
4878 int i;
4879 int not_stack;
4880 /* # bytes of start of argument
4881 that we must make space for but need not store. */
4882 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4883 int args_offset = INTVAL (args_so_far);
4884 int skip;
4886 /* Push padding now if padding above and stack grows down,
4887 or if padding below and stack grows up.
4888 But if space already allocated, this has already been done. */
4889 if (maybe_ne (extra, 0)
4890 && args_addr == 0
4891 && where_pad != PAD_NONE
4892 && where_pad != stack_direction)
4893 anti_adjust_stack (gen_int_mode (extra, Pmode));
4895 /* If we make space by pushing it, we might as well push
4896 the real data. Otherwise, we can leave OFFSET nonzero
4897 and leave the space uninitialized. */
4898 if (args_addr == 0)
4899 offset = 0;
4901 /* Now NOT_STACK gets the number of words that we don't need to
4902 allocate on the stack. Convert OFFSET to words too. */
4903 not_stack = (partial - offset) / UNITS_PER_WORD;
4904 offset /= UNITS_PER_WORD;
4906 /* If the partial register-part of the arg counts in its stack size,
4907 skip the part of stack space corresponding to the registers.
4908 Otherwise, start copying to the beginning of the stack space,
4909 by setting SKIP to 0. */
4910 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4912 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4913 x = validize_mem (force_const_mem (mode, x));
4915 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4916 SUBREGs of such registers are not allowed. */
4917 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4918 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4919 x = copy_to_reg (x);
4921 /* Loop over all the words allocated on the stack for this arg. */
4922 /* We can do it by words, because any scalar bigger than a word
4923 has a size a multiple of a word. */
4924 for (i = num_words - 1; i >= not_stack; i--)
4925 if (i >= not_stack + offset)
4926 if (!emit_push_insn (operand_subword_force (x, i, mode),
4927 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4928 0, args_addr,
4929 GEN_INT (args_offset + ((i - not_stack + skip)
4930 * UNITS_PER_WORD)),
4931 reg_parm_stack_space, alignment_pad, sibcall_p))
4932 return false;
4934 else
4936 rtx addr;
4937 rtx dest;
4939 /* Push padding now if padding above and stack grows down,
4940 or if padding below and stack grows up.
4941 But if space already allocated, this has already been done. */
4942 if (maybe_ne (extra, 0)
4943 && args_addr == 0
4944 && where_pad != PAD_NONE
4945 && where_pad != stack_direction)
4946 anti_adjust_stack (gen_int_mode (extra, Pmode));
4948 #ifdef PUSH_ROUNDING
4949 if (args_addr == 0 && targetm.calls.push_argument (0))
4950 emit_single_push_insn (mode, x, type);
4951 else
4952 #endif
4954 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4955 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4957 /* We do *not* set_mem_attributes here, because incoming arguments
4958 may overlap with sibling call outgoing arguments and we cannot
4959 allow reordering of reads from function arguments with stores
4960 to outgoing arguments of sibling calls. We do, however, want
4961 to record the alignment of the stack slot. */
4962 /* ALIGN may well be better aligned than TYPE, e.g. due to
4963 PARM_BOUNDARY. Assume the caller isn't lying. */
4964 set_mem_align (dest, align);
4966 emit_move_insn (dest, x);
4970 /* Move the partial arguments into the registers and any overlapping
4971 values that we moved into the pseudos in tmp_regs. */
4972 if (partial > 0 && reg != 0)
4974 /* Handle calls that pass values in multiple non-contiguous locations.
4975 The Irix 6 ABI has examples of this. */
4976 if (GET_CODE (reg) == PARALLEL)
4977 emit_group_load (reg, x, type, -1);
4978 else
4980 gcc_assert (partial % UNITS_PER_WORD == 0);
4981 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4983 for (int i = 0; i < overlapping; i++)
4984 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4985 + nregs - overlapping + i),
4986 tmp_regs[i]);
4991 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4992 anti_adjust_stack (gen_int_mode (extra, Pmode));
4994 if (alignment_pad && args_addr == 0)
4995 anti_adjust_stack (alignment_pad);
4997 return true;
5000 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5001 operations. */
5003 static rtx
5004 get_subtarget (rtx x)
5006 return (optimize
5007 || x == 0
5008 /* Only registers can be subtargets. */
5009 || !REG_P (x)
5010 /* Don't use hard regs to avoid extending their life. */
5011 || REGNO (x) < FIRST_PSEUDO_REGISTER
5012 ? 0 : x);
5015 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5016 FIELD is a bitfield. Returns true if the optimization was successful,
5017 and there's nothing else to do. */
5019 static bool
5020 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5021 poly_uint64 pbitpos,
5022 poly_uint64 pbitregion_start,
5023 poly_uint64 pbitregion_end,
5024 machine_mode mode1, rtx str_rtx,
5025 tree to, tree src, bool reverse)
5027 /* str_mode is not guaranteed to be a scalar type. */
5028 machine_mode str_mode = GET_MODE (str_rtx);
5029 unsigned int str_bitsize;
5030 tree op0, op1;
5031 rtx value, result;
5032 optab binop;
5033 gimple *srcstmt;
5034 enum tree_code code;
5036 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5037 if (mode1 != VOIDmode
5038 || !pbitsize.is_constant (&bitsize)
5039 || !pbitpos.is_constant (&bitpos)
5040 || !pbitregion_start.is_constant (&bitregion_start)
5041 || !pbitregion_end.is_constant (&bitregion_end)
5042 || bitsize >= BITS_PER_WORD
5043 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5044 || str_bitsize > BITS_PER_WORD
5045 || TREE_SIDE_EFFECTS (to)
5046 || TREE_THIS_VOLATILE (to))
5047 return false;
5049 STRIP_NOPS (src);
5050 if (TREE_CODE (src) != SSA_NAME)
5051 return false;
5052 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5053 return false;
5055 srcstmt = get_gimple_for_ssa_name (src);
5056 if (!srcstmt
5057 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5058 return false;
5060 code = gimple_assign_rhs_code (srcstmt);
5062 op0 = gimple_assign_rhs1 (srcstmt);
5064 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5065 to find its initialization. Hopefully the initialization will
5066 be from a bitfield load. */
5067 if (TREE_CODE (op0) == SSA_NAME)
5069 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5071 /* We want to eventually have OP0 be the same as TO, which
5072 should be a bitfield. */
5073 if (!op0stmt
5074 || !is_gimple_assign (op0stmt)
5075 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5076 return false;
5077 op0 = gimple_assign_rhs1 (op0stmt);
5080 op1 = gimple_assign_rhs2 (srcstmt);
5082 if (!operand_equal_p (to, op0, 0))
5083 return false;
5085 if (MEM_P (str_rtx))
5087 unsigned HOST_WIDE_INT offset1;
5089 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5090 str_bitsize = BITS_PER_WORD;
5092 scalar_int_mode best_mode;
5093 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5094 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5095 return false;
5096 str_mode = best_mode;
5097 str_bitsize = GET_MODE_BITSIZE (best_mode);
5099 offset1 = bitpos;
5100 bitpos %= str_bitsize;
5101 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5102 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5104 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5105 return false;
5107 /* If the bit field covers the whole REG/MEM, store_field
5108 will likely generate better code. */
5109 if (bitsize >= str_bitsize)
5110 return false;
5112 /* We can't handle fields split across multiple entities. */
5113 if (bitpos + bitsize > str_bitsize)
5114 return false;
5116 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5117 bitpos = str_bitsize - bitpos - bitsize;
5119 switch (code)
5121 case PLUS_EXPR:
5122 case MINUS_EXPR:
5123 /* For now, just optimize the case of the topmost bitfield
5124 where we don't need to do any masking and also
5125 1 bit bitfields where xor can be used.
5126 We might win by one instruction for the other bitfields
5127 too if insv/extv instructions aren't used, so that
5128 can be added later. */
5129 if ((reverse || bitpos + bitsize != str_bitsize)
5130 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5131 break;
5133 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5134 value = convert_modes (str_mode,
5135 TYPE_MODE (TREE_TYPE (op1)), value,
5136 TYPE_UNSIGNED (TREE_TYPE (op1)));
5138 /* We may be accessing data outside the field, which means
5139 we can alias adjacent data. */
5140 if (MEM_P (str_rtx))
5142 str_rtx = shallow_copy_rtx (str_rtx);
5143 set_mem_alias_set (str_rtx, 0);
5144 set_mem_expr (str_rtx, 0);
5147 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5149 value = expand_and (str_mode, value, const1_rtx, NULL);
5150 binop = xor_optab;
5152 else
5153 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5155 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5156 if (reverse)
5157 value = flip_storage_order (str_mode, value);
5158 result = expand_binop (str_mode, binop, str_rtx,
5159 value, str_rtx, 1, OPTAB_WIDEN);
5160 if (result != str_rtx)
5161 emit_move_insn (str_rtx, result);
5162 return true;
5164 case BIT_IOR_EXPR:
5165 case BIT_XOR_EXPR:
5166 if (TREE_CODE (op1) != INTEGER_CST)
5167 break;
5168 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5169 value = convert_modes (str_mode,
5170 TYPE_MODE (TREE_TYPE (op1)), value,
5171 TYPE_UNSIGNED (TREE_TYPE (op1)));
5173 /* We may be accessing data outside the field, which means
5174 we can alias adjacent data. */
5175 if (MEM_P (str_rtx))
5177 str_rtx = shallow_copy_rtx (str_rtx);
5178 set_mem_alias_set (str_rtx, 0);
5179 set_mem_expr (str_rtx, 0);
5182 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5183 if (bitpos + bitsize != str_bitsize)
5185 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5186 str_mode);
5187 value = expand_and (str_mode, value, mask, NULL_RTX);
5189 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5190 if (reverse)
5191 value = flip_storage_order (str_mode, value);
5192 result = expand_binop (str_mode, binop, str_rtx,
5193 value, str_rtx, 1, OPTAB_WIDEN);
5194 if (result != str_rtx)
5195 emit_move_insn (str_rtx, result);
5196 return true;
5198 default:
5199 break;
5202 return false;
5205 /* In the C++ memory model, consecutive bit fields in a structure are
5206 considered one memory location.
5208 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5209 returns the bit range of consecutive bits in which this COMPONENT_REF
5210 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5211 and *OFFSET may be adjusted in the process.
5213 If the access does not need to be restricted, 0 is returned in both
5214 *BITSTART and *BITEND. */
5216 void
5217 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5218 poly_int64_pod *bitpos, tree *offset)
5220 poly_int64 bitoffset;
5221 tree field, repr;
5223 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5225 field = TREE_OPERAND (exp, 1);
5226 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5227 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5228 need to limit the range we can access. */
5229 if (!repr)
5231 *bitstart = *bitend = 0;
5232 return;
5235 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5236 part of a larger bit field, then the representative does not serve any
5237 useful purpose. This can occur in Ada. */
5238 if (handled_component_p (TREE_OPERAND (exp, 0)))
5240 machine_mode rmode;
5241 poly_int64 rbitsize, rbitpos;
5242 tree roffset;
5243 int unsignedp, reversep, volatilep = 0;
5244 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5245 &roffset, &rmode, &unsignedp, &reversep,
5246 &volatilep);
5247 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5249 *bitstart = *bitend = 0;
5250 return;
5254 /* Compute the adjustment to bitpos from the offset of the field
5255 relative to the representative. DECL_FIELD_OFFSET of field and
5256 repr are the same by construction if they are not constants,
5257 see finish_bitfield_layout. */
5258 poly_uint64 field_offset, repr_offset;
5259 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5260 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5261 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5262 else
5263 bitoffset = 0;
5264 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5265 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5267 /* If the adjustment is larger than bitpos, we would have a negative bit
5268 position for the lower bound and this may wreak havoc later. Adjust
5269 offset and bitpos to make the lower bound non-negative in that case. */
5270 if (maybe_gt (bitoffset, *bitpos))
5272 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5273 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5275 *bitpos += adjust_bits;
5276 if (*offset == NULL_TREE)
5277 *offset = size_int (-adjust_bytes);
5278 else
5279 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5280 *bitstart = 0;
5282 else
5283 *bitstart = *bitpos - bitoffset;
5285 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5288 /* Returns true if BASE is a DECL that does not reside in memory and
5289 has non-BLKmode. DECL_RTL must not be a MEM; if
5290 DECL_RTL was not set yet, return false. */
5292 bool
5293 non_mem_decl_p (tree base)
5295 if (!DECL_P (base)
5296 || TREE_ADDRESSABLE (base)
5297 || DECL_MODE (base) == BLKmode)
5298 return false;
5300 if (!DECL_RTL_SET_P (base))
5301 return false;
5303 return (!MEM_P (DECL_RTL (base)));
5306 /* Returns true if REF refers to an object that does not
5307 reside in memory and has non-BLKmode. */
5309 bool
5310 mem_ref_refers_to_non_mem_p (tree ref)
5312 tree base;
5314 if (TREE_CODE (ref) == MEM_REF
5315 || TREE_CODE (ref) == TARGET_MEM_REF)
5317 tree addr = TREE_OPERAND (ref, 0);
5319 if (TREE_CODE (addr) != ADDR_EXPR)
5320 return false;
5322 base = TREE_OPERAND (addr, 0);
5324 else
5325 base = ref;
5327 return non_mem_decl_p (base);
5330 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5331 is true, try generating a nontemporal store. */
5333 void
5334 expand_assignment (tree to, tree from, bool nontemporal)
5336 rtx to_rtx = 0;
5337 rtx result;
5338 machine_mode mode;
5339 unsigned int align;
5340 enum insn_code icode;
5342 /* Don't crash if the lhs of the assignment was erroneous. */
5343 if (TREE_CODE (to) == ERROR_MARK)
5345 expand_normal (from);
5346 return;
5349 /* Optimize away no-op moves without side-effects. */
5350 if (operand_equal_p (to, from, 0))
5351 return;
5353 /* Handle misaligned stores. */
5354 mode = TYPE_MODE (TREE_TYPE (to));
5355 if ((TREE_CODE (to) == MEM_REF
5356 || TREE_CODE (to) == TARGET_MEM_REF
5357 || DECL_P (to))
5358 && mode != BLKmode
5359 && !mem_ref_refers_to_non_mem_p (to)
5360 && ((align = get_object_alignment (to))
5361 < GET_MODE_ALIGNMENT (mode))
5362 && (((icode = optab_handler (movmisalign_optab, mode))
5363 != CODE_FOR_nothing)
5364 || targetm.slow_unaligned_access (mode, align)))
5366 rtx reg, mem;
5368 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5369 /* Handle PARALLEL. */
5370 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5371 reg = force_not_mem (reg);
5372 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5373 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5374 reg = flip_storage_order (mode, reg);
5376 if (icode != CODE_FOR_nothing)
5378 class expand_operand ops[2];
5380 create_fixed_operand (&ops[0], mem);
5381 create_input_operand (&ops[1], reg, mode);
5382 /* The movmisalign<mode> pattern cannot fail, else the assignment
5383 would silently be omitted. */
5384 expand_insn (icode, 2, ops);
5386 else
5387 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5388 false);
5389 return;
5392 /* Assignment of a structure component needs special treatment
5393 if the structure component's rtx is not simply a MEM.
5394 Assignment of an array element at a constant index, and assignment of
5395 an array element in an unaligned packed structure field, has the same
5396 problem. Same for (partially) storing into a non-memory object. */
5397 if (handled_component_p (to)
5398 || (TREE_CODE (to) == MEM_REF
5399 && (REF_REVERSE_STORAGE_ORDER (to)
5400 || mem_ref_refers_to_non_mem_p (to)))
5401 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5403 machine_mode mode1;
5404 poly_int64 bitsize, bitpos;
5405 poly_uint64 bitregion_start = 0;
5406 poly_uint64 bitregion_end = 0;
5407 tree offset;
5408 int unsignedp, reversep, volatilep = 0;
5409 tree tem;
5411 push_temp_slots ();
5412 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5413 &unsignedp, &reversep, &volatilep);
5415 /* Make sure bitpos is not negative, it can wreak havoc later. */
5416 if (maybe_lt (bitpos, 0))
5418 gcc_assert (offset == NULL_TREE);
5419 offset = size_int (bits_to_bytes_round_down (bitpos));
5420 bitpos = num_trailing_bits (bitpos);
5423 if (TREE_CODE (to) == COMPONENT_REF
5424 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5425 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5426 /* The C++ memory model naturally applies to byte-aligned fields.
5427 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5428 BITSIZE are not byte-aligned, there is no need to limit the range
5429 we can access. This can occur with packed structures in Ada. */
5430 else if (maybe_gt (bitsize, 0)
5431 && multiple_p (bitsize, BITS_PER_UNIT)
5432 && multiple_p (bitpos, BITS_PER_UNIT))
5434 bitregion_start = bitpos;
5435 bitregion_end = bitpos + bitsize - 1;
5438 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5440 /* If the field has a mode, we want to access it in the
5441 field's mode, not the computed mode.
5442 If a MEM has VOIDmode (external with incomplete type),
5443 use BLKmode for it instead. */
5444 if (MEM_P (to_rtx))
5446 if (mode1 != VOIDmode)
5447 to_rtx = adjust_address (to_rtx, mode1, 0);
5448 else if (GET_MODE (to_rtx) == VOIDmode)
5449 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5452 if (offset != 0)
5454 machine_mode address_mode;
5455 rtx offset_rtx;
5457 if (!MEM_P (to_rtx))
5459 /* We can get constant negative offsets into arrays with broken
5460 user code. Translate this to a trap instead of ICEing. */
5461 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5462 expand_builtin_trap ();
5463 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5466 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5467 address_mode = get_address_mode (to_rtx);
5468 if (GET_MODE (offset_rtx) != address_mode)
5470 /* We cannot be sure that the RTL in offset_rtx is valid outside
5471 of a memory address context, so force it into a register
5472 before attempting to convert it to the desired mode. */
5473 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5474 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5477 /* If we have an expression in OFFSET_RTX and a non-zero
5478 byte offset in BITPOS, adding the byte offset before the
5479 OFFSET_RTX results in better intermediate code, which makes
5480 later rtl optimization passes perform better.
5482 We prefer intermediate code like this:
5484 r124:DI=r123:DI+0x18
5485 [r124:DI]=r121:DI
5487 ... instead of ...
5489 r124:DI=r123:DI+0x10
5490 [r124:DI+0x8]=r121:DI
5492 This is only done for aligned data values, as these can
5493 be expected to result in single move instructions. */
5494 poly_int64 bytepos;
5495 if (mode1 != VOIDmode
5496 && maybe_ne (bitpos, 0)
5497 && maybe_gt (bitsize, 0)
5498 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5499 && multiple_p (bitpos, bitsize)
5500 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5501 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5503 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5504 bitregion_start = 0;
5505 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5506 bitregion_end -= bitpos;
5507 bitpos = 0;
5510 to_rtx = offset_address (to_rtx, offset_rtx,
5511 highest_pow2_factor_for_target (to,
5512 offset));
5515 /* No action is needed if the target is not a memory and the field
5516 lies completely outside that target. This can occur if the source
5517 code contains an out-of-bounds access to a small array. */
5518 if (!MEM_P (to_rtx)
5519 && GET_MODE (to_rtx) != BLKmode
5520 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5522 expand_normal (from);
5523 result = NULL;
5525 /* Handle expand_expr of a complex value returning a CONCAT. */
5526 else if (GET_CODE (to_rtx) == CONCAT)
5528 machine_mode to_mode = GET_MODE (to_rtx);
5529 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5530 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5531 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5532 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5533 && known_eq (bitpos, 0)
5534 && known_eq (bitsize, mode_bitsize))
5535 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5536 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5537 && known_eq (bitsize, inner_bitsize)
5538 && (known_eq (bitpos, 0)
5539 || known_eq (bitpos, inner_bitsize)))
5540 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5541 false, nontemporal, reversep);
5542 else if (known_le (bitpos + bitsize, inner_bitsize))
5543 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5544 bitregion_start, bitregion_end,
5545 mode1, from, get_alias_set (to),
5546 nontemporal, reversep);
5547 else if (known_ge (bitpos, inner_bitsize))
5548 result = store_field (XEXP (to_rtx, 1), bitsize,
5549 bitpos - inner_bitsize,
5550 bitregion_start, bitregion_end,
5551 mode1, from, get_alias_set (to),
5552 nontemporal, reversep);
5553 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5555 result = expand_normal (from);
5556 if (GET_CODE (result) == CONCAT)
5558 to_mode = GET_MODE_INNER (to_mode);
5559 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5560 rtx from_real
5561 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5562 from_mode, 0);
5563 rtx from_imag
5564 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5565 from_mode, 0);
5566 if (!from_real || !from_imag)
5567 goto concat_store_slow;
5568 emit_move_insn (XEXP (to_rtx, 0), from_real);
5569 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5571 else
5573 machine_mode from_mode
5574 = GET_MODE (result) == VOIDmode
5575 ? TYPE_MODE (TREE_TYPE (from))
5576 : GET_MODE (result);
5577 rtx from_rtx;
5578 if (MEM_P (result))
5579 from_rtx = change_address (result, to_mode, NULL_RTX);
5580 else
5581 from_rtx
5582 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5583 if (from_rtx)
5585 emit_move_insn (XEXP (to_rtx, 0),
5586 read_complex_part (from_rtx, false));
5587 emit_move_insn (XEXP (to_rtx, 1),
5588 read_complex_part (from_rtx, true));
5590 else
5592 to_mode = GET_MODE_INNER (to_mode);
5593 rtx from_real
5594 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5595 rtx from_imag
5596 = simplify_gen_subreg (to_mode, result, from_mode,
5597 GET_MODE_SIZE (to_mode));
5598 if (!from_real || !from_imag)
5599 goto concat_store_slow;
5600 emit_move_insn (XEXP (to_rtx, 0), from_real);
5601 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5605 else
5607 concat_store_slow:;
5608 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5609 GET_MODE_SIZE (GET_MODE (to_rtx)));
5610 write_complex_part (temp, XEXP (to_rtx, 0), false);
5611 write_complex_part (temp, XEXP (to_rtx, 1), true);
5612 result = store_field (temp, bitsize, bitpos,
5613 bitregion_start, bitregion_end,
5614 mode1, from, get_alias_set (to),
5615 nontemporal, reversep);
5616 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5617 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5620 /* For calls to functions returning variable length structures, if TO_RTX
5621 is not a MEM, go through a MEM because we must not create temporaries
5622 of the VLA type. */
5623 else if (!MEM_P (to_rtx)
5624 && TREE_CODE (from) == CALL_EXPR
5625 && COMPLETE_TYPE_P (TREE_TYPE (from))
5626 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5628 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5629 GET_MODE_SIZE (GET_MODE (to_rtx)));
5630 result = store_field (temp, bitsize, bitpos, bitregion_start,
5631 bitregion_end, mode1, from, get_alias_set (to),
5632 nontemporal, reversep);
5633 emit_move_insn (to_rtx, temp);
5635 else
5637 if (MEM_P (to_rtx))
5639 /* If the field is at offset zero, we could have been given the
5640 DECL_RTX of the parent struct. Don't munge it. */
5641 to_rtx = shallow_copy_rtx (to_rtx);
5642 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5643 if (volatilep)
5644 MEM_VOLATILE_P (to_rtx) = 1;
5647 gcc_checking_assert (known_ge (bitpos, 0));
5648 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5649 bitregion_start, bitregion_end,
5650 mode1, to_rtx, to, from,
5651 reversep))
5652 result = NULL;
5653 else if (SUBREG_P (to_rtx)
5654 && SUBREG_PROMOTED_VAR_P (to_rtx))
5656 /* If to_rtx is a promoted subreg, we need to zero or sign
5657 extend the value afterwards. */
5658 if (TREE_CODE (to) == MEM_REF
5659 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5660 && !REF_REVERSE_STORAGE_ORDER (to)
5661 && known_eq (bitpos, 0)
5662 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5663 result = store_expr (from, to_rtx, 0, nontemporal, false);
5664 else
5666 rtx to_rtx1
5667 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5668 SUBREG_REG (to_rtx),
5669 subreg_promoted_mode (to_rtx));
5670 result = store_field (to_rtx1, bitsize, bitpos,
5671 bitregion_start, bitregion_end,
5672 mode1, from, get_alias_set (to),
5673 nontemporal, reversep);
5674 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5675 SUBREG_PROMOTED_SIGN (to_rtx));
5678 else
5679 result = store_field (to_rtx, bitsize, bitpos,
5680 bitregion_start, bitregion_end,
5681 mode1, from, get_alias_set (to),
5682 nontemporal, reversep);
5685 if (result)
5686 preserve_temp_slots (result);
5687 pop_temp_slots ();
5688 return;
5691 /* If the rhs is a function call and its value is not an aggregate,
5692 call the function before we start to compute the lhs.
5693 This is needed for correct code for cases such as
5694 val = setjmp (buf) on machines where reference to val
5695 requires loading up part of an address in a separate insn.
5697 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5698 since it might be a promoted variable where the zero- or sign- extension
5699 needs to be done. Handling this in the normal way is safe because no
5700 computation is done before the call. The same is true for SSA names. */
5701 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5702 && COMPLETE_TYPE_P (TREE_TYPE (from))
5703 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5704 && ! (((VAR_P (to)
5705 || TREE_CODE (to) == PARM_DECL
5706 || TREE_CODE (to) == RESULT_DECL)
5707 && REG_P (DECL_RTL (to)))
5708 || TREE_CODE (to) == SSA_NAME))
5710 rtx value;
5712 push_temp_slots ();
5713 value = expand_normal (from);
5715 if (to_rtx == 0)
5716 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5718 /* Handle calls that return values in multiple non-contiguous locations.
5719 The Irix 6 ABI has examples of this. */
5720 if (GET_CODE (to_rtx) == PARALLEL)
5722 if (GET_CODE (value) == PARALLEL)
5723 emit_group_move (to_rtx, value);
5724 else
5725 emit_group_load (to_rtx, value, TREE_TYPE (from),
5726 int_size_in_bytes (TREE_TYPE (from)));
5728 else if (GET_CODE (value) == PARALLEL)
5729 emit_group_store (to_rtx, value, TREE_TYPE (from),
5730 int_size_in_bytes (TREE_TYPE (from)));
5731 else if (GET_MODE (to_rtx) == BLKmode)
5733 /* Handle calls that return BLKmode values in registers. */
5734 if (REG_P (value))
5735 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5736 else
5737 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5739 else
5741 if (POINTER_TYPE_P (TREE_TYPE (to)))
5742 value = convert_memory_address_addr_space
5743 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5744 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5746 emit_move_insn (to_rtx, value);
5749 preserve_temp_slots (to_rtx);
5750 pop_temp_slots ();
5751 return;
5754 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5755 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5757 /* Don't move directly into a return register. */
5758 if (TREE_CODE (to) == RESULT_DECL
5759 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5761 rtx temp;
5763 push_temp_slots ();
5765 /* If the source is itself a return value, it still is in a pseudo at
5766 this point so we can move it back to the return register directly. */
5767 if (REG_P (to_rtx)
5768 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5769 && TREE_CODE (from) != CALL_EXPR)
5770 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5771 else
5772 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5774 /* Handle calls that return values in multiple non-contiguous locations.
5775 The Irix 6 ABI has examples of this. */
5776 if (GET_CODE (to_rtx) == PARALLEL)
5778 if (GET_CODE (temp) == PARALLEL)
5779 emit_group_move (to_rtx, temp);
5780 else
5781 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5782 int_size_in_bytes (TREE_TYPE (from)));
5784 else if (temp)
5785 emit_move_insn (to_rtx, temp);
5787 preserve_temp_slots (to_rtx);
5788 pop_temp_slots ();
5789 return;
5792 /* In case we are returning the contents of an object which overlaps
5793 the place the value is being stored, use a safe function when copying
5794 a value through a pointer into a structure value return block. */
5795 if (TREE_CODE (to) == RESULT_DECL
5796 && TREE_CODE (from) == INDIRECT_REF
5797 && ADDR_SPACE_GENERIC_P
5798 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5799 && refs_may_alias_p (to, from)
5800 && cfun->returns_struct
5801 && !cfun->returns_pcc_struct)
5803 rtx from_rtx, size;
5805 push_temp_slots ();
5806 size = expr_size (from);
5807 from_rtx = expand_normal (from);
5809 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5811 preserve_temp_slots (to_rtx);
5812 pop_temp_slots ();
5813 return;
5816 /* Compute FROM and store the value in the rtx we got. */
5818 push_temp_slots ();
5819 result = store_expr (from, to_rtx, 0, nontemporal, false);
5820 preserve_temp_slots (result);
5821 pop_temp_slots ();
5822 return;
5825 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5826 succeeded, false otherwise. */
5828 bool
5829 emit_storent_insn (rtx to, rtx from)
5831 class expand_operand ops[2];
5832 machine_mode mode = GET_MODE (to);
5833 enum insn_code code = optab_handler (storent_optab, mode);
5835 if (code == CODE_FOR_nothing)
5836 return false;
5838 create_fixed_operand (&ops[0], to);
5839 create_input_operand (&ops[1], from, mode);
5840 return maybe_expand_insn (code, 2, ops);
5843 /* Helper function for store_expr storing of STRING_CST. */
5845 static rtx
5846 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
5847 fixed_size_mode mode)
5849 tree str = (tree) data;
5851 gcc_assert (offset >= 0);
5852 if (offset >= TREE_STRING_LENGTH (str))
5853 return const0_rtx;
5855 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5856 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5858 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5859 size_t l = TREE_STRING_LENGTH (str) - offset;
5860 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5861 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5862 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
5865 /* The by-pieces infrastructure does not try to pick a vector mode
5866 for storing STRING_CST. */
5867 return c_readstr (TREE_STRING_POINTER (str) + offset,
5868 as_a <scalar_int_mode> (mode), false);
5871 /* Generate code for computing expression EXP,
5872 and storing the value into TARGET.
5874 If the mode is BLKmode then we may return TARGET itself.
5875 It turns out that in BLKmode it doesn't cause a problem.
5876 because C has no operators that could combine two different
5877 assignments into the same BLKmode object with different values
5878 with no sequence point. Will other languages need this to
5879 be more thorough?
5881 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5882 stack, and block moves may need to be treated specially.
5884 If NONTEMPORAL is true, try using a nontemporal store instruction.
5886 If REVERSE is true, the store is to be done in reverse order. */
5889 store_expr (tree exp, rtx target, int call_param_p,
5890 bool nontemporal, bool reverse)
5892 rtx temp;
5893 rtx alt_rtl = NULL_RTX;
5894 location_t loc = curr_insn_location ();
5895 bool shortened_string_cst = false;
5897 if (VOID_TYPE_P (TREE_TYPE (exp)))
5899 /* C++ can generate ?: expressions with a throw expression in one
5900 branch and an rvalue in the other. Here, we resolve attempts to
5901 store the throw expression's nonexistent result. */
5902 gcc_assert (!call_param_p);
5903 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5904 return NULL_RTX;
5906 if (TREE_CODE (exp) == COMPOUND_EXPR)
5908 /* Perform first part of compound expression, then assign from second
5909 part. */
5910 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5911 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5912 return store_expr (TREE_OPERAND (exp, 1), target,
5913 call_param_p, nontemporal, reverse);
5915 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5917 /* For conditional expression, get safe form of the target. Then
5918 test the condition, doing the appropriate assignment on either
5919 side. This avoids the creation of unnecessary temporaries.
5920 For non-BLKmode, it is more efficient not to do this. */
5922 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5924 do_pending_stack_adjust ();
5925 NO_DEFER_POP;
5926 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5927 profile_probability::uninitialized ());
5928 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5929 nontemporal, reverse);
5930 emit_jump_insn (targetm.gen_jump (lab2));
5931 emit_barrier ();
5932 emit_label (lab1);
5933 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5934 nontemporal, reverse);
5935 emit_label (lab2);
5936 OK_DEFER_POP;
5938 return NULL_RTX;
5940 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5941 /* If this is a scalar in a register that is stored in a wider mode
5942 than the declared mode, compute the result into its declared mode
5943 and then convert to the wider mode. Our value is the computed
5944 expression. */
5946 rtx inner_target = 0;
5947 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5948 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5950 /* We can do the conversion inside EXP, which will often result
5951 in some optimizations. Do the conversion in two steps: first
5952 change the signedness, if needed, then the extend. But don't
5953 do this if the type of EXP is a subtype of something else
5954 since then the conversion might involve more than just
5955 converting modes. */
5956 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5957 && TREE_TYPE (TREE_TYPE (exp)) == 0
5958 && GET_MODE_PRECISION (outer_mode)
5959 == TYPE_PRECISION (TREE_TYPE (exp)))
5961 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5962 TYPE_UNSIGNED (TREE_TYPE (exp))))
5964 /* Some types, e.g. Fortran's logical*4, won't have a signed
5965 version, so use the mode instead. */
5966 tree ntype
5967 = (signed_or_unsigned_type_for
5968 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5969 if (ntype == NULL)
5970 ntype = lang_hooks.types.type_for_mode
5971 (TYPE_MODE (TREE_TYPE (exp)),
5972 SUBREG_PROMOTED_SIGN (target));
5974 exp = fold_convert_loc (loc, ntype, exp);
5977 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5978 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5979 exp);
5981 inner_target = SUBREG_REG (target);
5984 temp = expand_expr (exp, inner_target, VOIDmode,
5985 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5988 /* If TEMP is a VOIDmode constant, use convert_modes to make
5989 sure that we properly convert it. */
5990 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5992 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5993 temp, SUBREG_PROMOTED_SIGN (target));
5994 temp = convert_modes (inner_mode, outer_mode, temp,
5995 SUBREG_PROMOTED_SIGN (target));
5998 convert_move (SUBREG_REG (target), temp,
5999 SUBREG_PROMOTED_SIGN (target));
6001 return NULL_RTX;
6003 else if ((TREE_CODE (exp) == STRING_CST
6004 || (TREE_CODE (exp) == MEM_REF
6005 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6006 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6007 == STRING_CST
6008 && integer_zerop (TREE_OPERAND (exp, 1))))
6009 && !nontemporal && !call_param_p
6010 && MEM_P (target))
6012 /* Optimize initialization of an array with a STRING_CST. */
6013 HOST_WIDE_INT exp_len, str_copy_len;
6014 rtx dest_mem;
6015 tree str = TREE_CODE (exp) == STRING_CST
6016 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6018 exp_len = int_expr_size (exp);
6019 if (exp_len <= 0)
6020 goto normal_expr;
6022 if (TREE_STRING_LENGTH (str) <= 0)
6023 goto normal_expr;
6025 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6026 MEM_ALIGN (target), false))
6028 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6029 MEM_ALIGN (target), false, RETURN_BEGIN);
6030 return NULL_RTX;
6033 str_copy_len = TREE_STRING_LENGTH (str);
6034 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6036 str_copy_len += STORE_MAX_PIECES - 1;
6037 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6039 if (str_copy_len >= exp_len)
6040 goto normal_expr;
6042 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6043 (void *) str, MEM_ALIGN (target), false))
6044 goto normal_expr;
6046 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6047 (void *) str, MEM_ALIGN (target), false,
6048 RETURN_END);
6049 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6050 exp_len - str_copy_len),
6051 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6052 return NULL_RTX;
6054 else
6056 rtx tmp_target;
6058 normal_expr:
6059 /* If we want to use a nontemporal or a reverse order store, force the
6060 value into a register first. */
6061 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6062 tree rexp = exp;
6063 if (TREE_CODE (exp) == STRING_CST
6064 && tmp_target == target
6065 && GET_MODE (target) == BLKmode
6066 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6068 rtx size = expr_size (exp);
6069 if (CONST_INT_P (size)
6070 && size != const0_rtx
6071 && (UINTVAL (size)
6072 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6074 /* If the STRING_CST has much larger array type than
6075 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6076 it into the rodata section as the code later on will use
6077 memset zero for the remainder anyway. See PR95052. */
6078 tmp_target = NULL_RTX;
6079 rexp = copy_node (exp);
6080 tree index
6081 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6082 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6083 index);
6084 shortened_string_cst = true;
6087 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6088 (call_param_p
6089 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6090 &alt_rtl, false);
6091 if (shortened_string_cst)
6093 gcc_assert (MEM_P (temp));
6094 temp = change_address (temp, BLKmode, NULL_RTX);
6098 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6099 the same as that of TARGET, adjust the constant. This is needed, for
6100 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6101 only a word-sized value. */
6102 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6103 && TREE_CODE (exp) != ERROR_MARK
6104 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6106 gcc_assert (!shortened_string_cst);
6107 if (GET_MODE_CLASS (GET_MODE (target))
6108 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6109 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6110 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6112 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6113 TYPE_MODE (TREE_TYPE (exp)), 0);
6114 if (t)
6115 temp = t;
6117 if (GET_MODE (temp) == VOIDmode)
6118 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6119 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6122 /* If value was not generated in the target, store it there.
6123 Convert the value to TARGET's type first if necessary and emit the
6124 pending incrementations that have been queued when expanding EXP.
6125 Note that we cannot emit the whole queue blindly because this will
6126 effectively disable the POST_INC optimization later.
6128 If TEMP and TARGET compare equal according to rtx_equal_p, but
6129 one or both of them are volatile memory refs, we have to distinguish
6130 two cases:
6131 - expand_expr has used TARGET. In this case, we must not generate
6132 another copy. This can be detected by TARGET being equal according
6133 to == .
6134 - expand_expr has not used TARGET - that means that the source just
6135 happens to have the same RTX form. Since temp will have been created
6136 by expand_expr, it will compare unequal according to == .
6137 We must generate a copy in this case, to reach the correct number
6138 of volatile memory references. */
6140 if ((! rtx_equal_p (temp, target)
6141 || (temp != target && (side_effects_p (temp)
6142 || side_effects_p (target))))
6143 && TREE_CODE (exp) != ERROR_MARK
6144 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6145 but TARGET is not valid memory reference, TEMP will differ
6146 from TARGET although it is really the same location. */
6147 && !(alt_rtl
6148 && rtx_equal_p (alt_rtl, target)
6149 && !side_effects_p (alt_rtl)
6150 && !side_effects_p (target))
6151 /* If there's nothing to copy, don't bother. Don't call
6152 expr_size unless necessary, because some front-ends (C++)
6153 expr_size-hook must not be given objects that are not
6154 supposed to be bit-copied or bit-initialized. */
6155 && expr_size (exp) != const0_rtx)
6157 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6159 gcc_assert (!shortened_string_cst);
6160 if (GET_MODE (target) == BLKmode)
6162 /* Handle calls that return BLKmode values in registers. */
6163 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6164 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6165 else
6166 store_bit_field (target,
6167 rtx_to_poly_int64 (expr_size (exp))
6168 * BITS_PER_UNIT,
6169 0, 0, 0, GET_MODE (temp), temp, reverse);
6171 else
6172 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6175 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6177 /* Handle copying a string constant into an array. The string
6178 constant may be shorter than the array. So copy just the string's
6179 actual length, and clear the rest. First get the size of the data
6180 type of the string, which is actually the size of the target. */
6181 rtx size = expr_size (exp);
6183 if (CONST_INT_P (size)
6184 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6185 emit_block_move (target, temp, size,
6186 (call_param_p
6187 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6188 else
6190 machine_mode pointer_mode
6191 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6192 machine_mode address_mode = get_address_mode (target);
6194 /* Compute the size of the data to copy from the string. */
6195 tree copy_size
6196 = size_binop_loc (loc, MIN_EXPR,
6197 make_tree (sizetype, size),
6198 size_int (TREE_STRING_LENGTH (exp)));
6199 rtx copy_size_rtx
6200 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6201 (call_param_p
6202 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6203 rtx_code_label *label = 0;
6205 /* Copy that much. */
6206 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6207 TYPE_UNSIGNED (sizetype));
6208 emit_block_move (target, temp, copy_size_rtx,
6209 (call_param_p
6210 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6212 /* Figure out how much is left in TARGET that we have to clear.
6213 Do all calculations in pointer_mode. */
6214 poly_int64 const_copy_size;
6215 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6217 size = plus_constant (address_mode, size, -const_copy_size);
6218 target = adjust_address (target, BLKmode, const_copy_size);
6220 else
6222 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6223 copy_size_rtx, NULL_RTX, 0,
6224 OPTAB_LIB_WIDEN);
6226 if (GET_MODE (copy_size_rtx) != address_mode)
6227 copy_size_rtx = convert_to_mode (address_mode,
6228 copy_size_rtx,
6229 TYPE_UNSIGNED (sizetype));
6231 target = offset_address (target, copy_size_rtx,
6232 highest_pow2_factor (copy_size));
6233 label = gen_label_rtx ();
6234 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6235 GET_MODE (size), 0, label);
6238 if (size != const0_rtx)
6239 clear_storage (target, size, BLOCK_OP_NORMAL);
6241 if (label)
6242 emit_label (label);
6245 else if (shortened_string_cst)
6246 gcc_unreachable ();
6247 /* Handle calls that return values in multiple non-contiguous locations.
6248 The Irix 6 ABI has examples of this. */
6249 else if (GET_CODE (target) == PARALLEL)
6251 if (GET_CODE (temp) == PARALLEL)
6252 emit_group_move (target, temp);
6253 else
6254 emit_group_load (target, temp, TREE_TYPE (exp),
6255 int_size_in_bytes (TREE_TYPE (exp)));
6257 else if (GET_CODE (temp) == PARALLEL)
6258 emit_group_store (target, temp, TREE_TYPE (exp),
6259 int_size_in_bytes (TREE_TYPE (exp)));
6260 else if (GET_MODE (temp) == BLKmode)
6261 emit_block_move (target, temp, expr_size (exp),
6262 (call_param_p
6263 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6264 /* If we emit a nontemporal store, there is nothing else to do. */
6265 else if (nontemporal && emit_storent_insn (target, temp))
6267 else
6269 if (reverse)
6270 temp = flip_storage_order (GET_MODE (target), temp);
6271 temp = force_operand (temp, target);
6272 if (temp != target)
6273 emit_move_insn (target, temp);
6276 else
6277 gcc_assert (!shortened_string_cst);
6279 return NULL_RTX;
6282 /* Return true if field F of structure TYPE is a flexible array. */
6284 static bool
6285 flexible_array_member_p (const_tree f, const_tree type)
6287 const_tree tf;
6289 tf = TREE_TYPE (f);
6290 return (DECL_CHAIN (f) == NULL
6291 && TREE_CODE (tf) == ARRAY_TYPE
6292 && TYPE_DOMAIN (tf)
6293 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6294 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6295 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6296 && int_size_in_bytes (type) >= 0);
6299 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6300 must have in order for it to completely initialize a value of type TYPE.
6301 Return -1 if the number isn't known.
6303 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6305 static HOST_WIDE_INT
6306 count_type_elements (const_tree type, bool for_ctor_p)
6308 switch (TREE_CODE (type))
6310 case ARRAY_TYPE:
6312 tree nelts;
6314 nelts = array_type_nelts (type);
6315 if (nelts && tree_fits_uhwi_p (nelts))
6317 unsigned HOST_WIDE_INT n;
6319 n = tree_to_uhwi (nelts) + 1;
6320 if (n == 0 || for_ctor_p)
6321 return n;
6322 else
6323 return n * count_type_elements (TREE_TYPE (type), false);
6325 return for_ctor_p ? -1 : 1;
6328 case RECORD_TYPE:
6330 unsigned HOST_WIDE_INT n;
6331 tree f;
6333 n = 0;
6334 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6335 if (TREE_CODE (f) == FIELD_DECL)
6337 if (!for_ctor_p)
6338 n += count_type_elements (TREE_TYPE (f), false);
6339 else if (!flexible_array_member_p (f, type))
6340 /* Don't count flexible arrays, which are not supposed
6341 to be initialized. */
6342 n += 1;
6345 return n;
6348 case UNION_TYPE:
6349 case QUAL_UNION_TYPE:
6351 tree f;
6352 HOST_WIDE_INT n, m;
6354 gcc_assert (!for_ctor_p);
6355 /* Estimate the number of scalars in each field and pick the
6356 maximum. Other estimates would do instead; the idea is simply
6357 to make sure that the estimate is not sensitive to the ordering
6358 of the fields. */
6359 n = 1;
6360 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6361 if (TREE_CODE (f) == FIELD_DECL)
6363 m = count_type_elements (TREE_TYPE (f), false);
6364 /* If the field doesn't span the whole union, add an extra
6365 scalar for the rest. */
6366 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6367 TYPE_SIZE (type)) != 1)
6368 m++;
6369 if (n < m)
6370 n = m;
6372 return n;
6375 case COMPLEX_TYPE:
6376 return 2;
6378 case VECTOR_TYPE:
6380 unsigned HOST_WIDE_INT nelts;
6381 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6382 return nelts;
6383 else
6384 return -1;
6387 case INTEGER_TYPE:
6388 case REAL_TYPE:
6389 case FIXED_POINT_TYPE:
6390 case ENUMERAL_TYPE:
6391 case BOOLEAN_TYPE:
6392 case POINTER_TYPE:
6393 case OFFSET_TYPE:
6394 case REFERENCE_TYPE:
6395 case NULLPTR_TYPE:
6396 return 1;
6398 case ERROR_MARK:
6399 return 0;
6401 case VOID_TYPE:
6402 case OPAQUE_TYPE:
6403 case METHOD_TYPE:
6404 case FUNCTION_TYPE:
6405 case LANG_TYPE:
6406 default:
6407 gcc_unreachable ();
6411 /* Helper for categorize_ctor_elements. Identical interface. */
6413 static bool
6414 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6415 HOST_WIDE_INT *p_unique_nz_elts,
6416 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6418 unsigned HOST_WIDE_INT idx;
6419 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6420 tree value, purpose, elt_type;
6422 /* Whether CTOR is a valid constant initializer, in accordance with what
6423 initializer_constant_valid_p does. If inferred from the constructor
6424 elements, true until proven otherwise. */
6425 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6426 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6428 nz_elts = 0;
6429 unique_nz_elts = 0;
6430 init_elts = 0;
6431 num_fields = 0;
6432 elt_type = NULL_TREE;
6434 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6436 HOST_WIDE_INT mult = 1;
6438 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6440 tree lo_index = TREE_OPERAND (purpose, 0);
6441 tree hi_index = TREE_OPERAND (purpose, 1);
6443 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6444 mult = (tree_to_uhwi (hi_index)
6445 - tree_to_uhwi (lo_index) + 1);
6447 num_fields += mult;
6448 elt_type = TREE_TYPE (value);
6450 switch (TREE_CODE (value))
6452 case CONSTRUCTOR:
6454 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6456 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6457 &ic, p_complete);
6459 nz_elts += mult * nz;
6460 unique_nz_elts += unz;
6461 init_elts += mult * ic;
6463 if (const_from_elts_p && const_p)
6464 const_p = const_elt_p;
6466 break;
6468 case INTEGER_CST:
6469 case REAL_CST:
6470 case FIXED_CST:
6471 if (!initializer_zerop (value))
6473 nz_elts += mult;
6474 unique_nz_elts++;
6476 init_elts += mult;
6477 break;
6479 case STRING_CST:
6480 nz_elts += mult * TREE_STRING_LENGTH (value);
6481 unique_nz_elts += TREE_STRING_LENGTH (value);
6482 init_elts += mult * TREE_STRING_LENGTH (value);
6483 break;
6485 case COMPLEX_CST:
6486 if (!initializer_zerop (TREE_REALPART (value)))
6488 nz_elts += mult;
6489 unique_nz_elts++;
6491 if (!initializer_zerop (TREE_IMAGPART (value)))
6493 nz_elts += mult;
6494 unique_nz_elts++;
6496 init_elts += 2 * mult;
6497 break;
6499 case VECTOR_CST:
6501 /* We can only construct constant-length vectors using
6502 CONSTRUCTOR. */
6503 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6504 for (unsigned int i = 0; i < nunits; ++i)
6506 tree v = VECTOR_CST_ELT (value, i);
6507 if (!initializer_zerop (v))
6509 nz_elts += mult;
6510 unique_nz_elts++;
6512 init_elts += mult;
6515 break;
6517 default:
6519 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6520 nz_elts += mult * tc;
6521 unique_nz_elts += tc;
6522 init_elts += mult * tc;
6524 if (const_from_elts_p && const_p)
6525 const_p
6526 = initializer_constant_valid_p (value,
6527 elt_type,
6528 TYPE_REVERSE_STORAGE_ORDER
6529 (TREE_TYPE (ctor)))
6530 != NULL_TREE;
6532 break;
6536 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6537 num_fields, elt_type))
6538 *p_complete = false;
6540 *p_nz_elts += nz_elts;
6541 *p_unique_nz_elts += unique_nz_elts;
6542 *p_init_elts += init_elts;
6544 return const_p;
6547 /* Examine CTOR to discover:
6548 * how many scalar fields are set to nonzero values,
6549 and place it in *P_NZ_ELTS;
6550 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6551 high - low + 1 (this can be useful for callers to determine ctors
6552 that could be cheaply initialized with - perhaps nested - loops
6553 compared to copied from huge read-only data),
6554 and place it in *P_UNIQUE_NZ_ELTS;
6555 * how many scalar fields in total are in CTOR,
6556 and place it in *P_ELT_COUNT.
6557 * whether the constructor is complete -- in the sense that every
6558 meaningful byte is explicitly given a value --
6559 and place it in *P_COMPLETE.
6561 Return whether or not CTOR is a valid static constant initializer, the same
6562 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6564 bool
6565 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6566 HOST_WIDE_INT *p_unique_nz_elts,
6567 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6569 *p_nz_elts = 0;
6570 *p_unique_nz_elts = 0;
6571 *p_init_elts = 0;
6572 *p_complete = true;
6574 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6575 p_init_elts, p_complete);
6578 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6579 of which had type LAST_TYPE. Each element was itself a complete
6580 initializer, in the sense that every meaningful byte was explicitly
6581 given a value. Return true if the same is true for the constructor
6582 as a whole. */
6584 bool
6585 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6586 const_tree last_type)
6588 if (TREE_CODE (type) == UNION_TYPE
6589 || TREE_CODE (type) == QUAL_UNION_TYPE)
6591 if (num_elts == 0)
6592 return false;
6594 gcc_assert (num_elts == 1 && last_type);
6596 /* ??? We could look at each element of the union, and find the
6597 largest element. Which would avoid comparing the size of the
6598 initialized element against any tail padding in the union.
6599 Doesn't seem worth the effort... */
6600 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6603 return count_type_elements (type, true) == num_elts;
6606 /* Return 1 if EXP contains mostly (3/4) zeros. */
6608 static int
6609 mostly_zeros_p (const_tree exp)
6611 if (TREE_CODE (exp) == CONSTRUCTOR)
6613 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6614 bool complete_p;
6616 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6617 &complete_p);
6618 return !complete_p || nz_elts < init_elts / 4;
6621 return initializer_zerop (exp);
6624 /* Return 1 if EXP contains all zeros. */
6626 static int
6627 all_zeros_p (const_tree exp)
6629 if (TREE_CODE (exp) == CONSTRUCTOR)
6631 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6632 bool complete_p;
6634 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6635 &complete_p);
6636 return nz_elts == 0;
6639 return initializer_zerop (exp);
6642 /* Helper function for store_constructor.
6643 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6644 CLEARED is as for store_constructor.
6645 ALIAS_SET is the alias set to use for any stores.
6646 If REVERSE is true, the store is to be done in reverse order.
6648 This provides a recursive shortcut back to store_constructor when it isn't
6649 necessary to go through store_field. This is so that we can pass through
6650 the cleared field to let store_constructor know that we may not have to
6651 clear a substructure if the outer structure has already been cleared. */
6653 static void
6654 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6655 poly_uint64 bitregion_start,
6656 poly_uint64 bitregion_end,
6657 machine_mode mode,
6658 tree exp, int cleared,
6659 alias_set_type alias_set, bool reverse)
6661 poly_int64 bytepos;
6662 poly_uint64 bytesize;
6663 if (TREE_CODE (exp) == CONSTRUCTOR
6664 /* We can only call store_constructor recursively if the size and
6665 bit position are on a byte boundary. */
6666 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6667 && maybe_ne (bitsize, 0U)
6668 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6669 /* If we have a nonzero bitpos for a register target, then we just
6670 let store_field do the bitfield handling. This is unlikely to
6671 generate unnecessary clear instructions anyways. */
6672 && (known_eq (bitpos, 0) || MEM_P (target)))
6674 if (MEM_P (target))
6676 machine_mode target_mode = GET_MODE (target);
6677 if (target_mode != BLKmode
6678 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6679 target_mode = BLKmode;
6680 target = adjust_address (target, target_mode, bytepos);
6684 /* Update the alias set, if required. */
6685 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6686 && MEM_ALIAS_SET (target) != 0)
6688 target = copy_rtx (target);
6689 set_mem_alias_set (target, alias_set);
6692 store_constructor (exp, target, cleared, bytesize, reverse);
6694 else
6695 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6696 exp, alias_set, false, reverse);
6700 /* Returns the number of FIELD_DECLs in TYPE. */
6702 static int
6703 fields_length (const_tree type)
6705 tree t = TYPE_FIELDS (type);
6706 int count = 0;
6708 for (; t; t = DECL_CHAIN (t))
6709 if (TREE_CODE (t) == FIELD_DECL)
6710 ++count;
6712 return count;
6716 /* Store the value of constructor EXP into the rtx TARGET.
6717 TARGET is either a REG or a MEM; we know it cannot conflict, since
6718 safe_from_p has been called.
6719 CLEARED is true if TARGET is known to have been zero'd.
6720 SIZE is the number of bytes of TARGET we are allowed to modify: this
6721 may not be the same as the size of EXP if we are assigning to a field
6722 which has been packed to exclude padding bits.
6723 If REVERSE is true, the store is to be done in reverse order. */
6725 static void
6726 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6727 bool reverse)
6729 tree type = TREE_TYPE (exp);
6730 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6731 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6733 switch (TREE_CODE (type))
6735 case RECORD_TYPE:
6736 case UNION_TYPE:
6737 case QUAL_UNION_TYPE:
6739 unsigned HOST_WIDE_INT idx;
6740 tree field, value;
6742 /* The storage order is specified for every aggregate type. */
6743 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6745 /* If size is zero or the target is already cleared, do nothing. */
6746 if (known_eq (size, 0) || cleared)
6747 cleared = 1;
6748 /* We either clear the aggregate or indicate the value is dead. */
6749 else if ((TREE_CODE (type) == UNION_TYPE
6750 || TREE_CODE (type) == QUAL_UNION_TYPE)
6751 && ! CONSTRUCTOR_ELTS (exp))
6752 /* If the constructor is empty, clear the union. */
6754 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6755 cleared = 1;
6758 /* If we are building a static constructor into a register,
6759 set the initial value as zero so we can fold the value into
6760 a constant. But if more than one register is involved,
6761 this probably loses. */
6762 else if (REG_P (target) && TREE_STATIC (exp)
6763 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6764 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6766 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6767 cleared = 1;
6770 /* If the constructor has fewer fields than the structure or
6771 if we are initializing the structure to mostly zeros, clear
6772 the whole structure first. Don't do this if TARGET is a
6773 register whose mode size isn't equal to SIZE since
6774 clear_storage can't handle this case. */
6775 else if (known_size_p (size)
6776 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6777 || mostly_zeros_p (exp))
6778 && (!REG_P (target)
6779 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6781 clear_storage (target, gen_int_mode (size, Pmode),
6782 BLOCK_OP_NORMAL);
6783 cleared = 1;
6786 if (REG_P (target) && !cleared)
6787 emit_clobber (target);
6789 /* Store each element of the constructor into the
6790 corresponding field of TARGET. */
6791 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6793 machine_mode mode;
6794 HOST_WIDE_INT bitsize;
6795 HOST_WIDE_INT bitpos = 0;
6796 tree offset;
6797 rtx to_rtx = target;
6799 /* Just ignore missing fields. We cleared the whole
6800 structure, above, if any fields are missing. */
6801 if (field == 0)
6802 continue;
6804 if (cleared && initializer_zerop (value))
6805 continue;
6807 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6808 bitsize = tree_to_uhwi (DECL_SIZE (field));
6809 else
6810 gcc_unreachable ();
6812 mode = DECL_MODE (field);
6813 if (DECL_BIT_FIELD (field))
6814 mode = VOIDmode;
6816 offset = DECL_FIELD_OFFSET (field);
6817 if (tree_fits_shwi_p (offset)
6818 && tree_fits_shwi_p (bit_position (field)))
6820 bitpos = int_bit_position (field);
6821 offset = NULL_TREE;
6823 else
6824 gcc_unreachable ();
6826 /* If this initializes a field that is smaller than a
6827 word, at the start of a word, try to widen it to a full
6828 word. This special case allows us to output C++ member
6829 function initializations in a form that the optimizers
6830 can understand. */
6831 if (WORD_REGISTER_OPERATIONS
6832 && REG_P (target)
6833 && bitsize < BITS_PER_WORD
6834 && bitpos % BITS_PER_WORD == 0
6835 && GET_MODE_CLASS (mode) == MODE_INT
6836 && TREE_CODE (value) == INTEGER_CST
6837 && exp_size >= 0
6838 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6840 type = TREE_TYPE (value);
6842 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6844 type = lang_hooks.types.type_for_mode
6845 (word_mode, TYPE_UNSIGNED (type));
6846 value = fold_convert (type, value);
6847 /* Make sure the bits beyond the original bitsize are zero
6848 so that we can correctly avoid extra zeroing stores in
6849 later constructor elements. */
6850 tree bitsize_mask
6851 = wide_int_to_tree (type, wi::mask (bitsize, false,
6852 BITS_PER_WORD));
6853 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6856 if (BYTES_BIG_ENDIAN)
6857 value
6858 = fold_build2 (LSHIFT_EXPR, type, value,
6859 build_int_cst (type,
6860 BITS_PER_WORD - bitsize));
6861 bitsize = BITS_PER_WORD;
6862 mode = word_mode;
6865 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6866 && DECL_NONADDRESSABLE_P (field))
6868 to_rtx = copy_rtx (to_rtx);
6869 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6872 store_constructor_field (to_rtx, bitsize, bitpos,
6873 0, bitregion_end, mode,
6874 value, cleared,
6875 get_alias_set (TREE_TYPE (field)),
6876 reverse);
6878 break;
6880 case ARRAY_TYPE:
6882 tree value, index;
6883 unsigned HOST_WIDE_INT i;
6884 int need_to_clear;
6885 tree domain;
6886 tree elttype = TREE_TYPE (type);
6887 int const_bounds_p;
6888 HOST_WIDE_INT minelt = 0;
6889 HOST_WIDE_INT maxelt = 0;
6891 /* The storage order is specified for every aggregate type. */
6892 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6894 domain = TYPE_DOMAIN (type);
6895 const_bounds_p = (TYPE_MIN_VALUE (domain)
6896 && TYPE_MAX_VALUE (domain)
6897 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6898 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6900 /* If we have constant bounds for the range of the type, get them. */
6901 if (const_bounds_p)
6903 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6904 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6907 /* If the constructor has fewer elements than the array, clear
6908 the whole array first. Similarly if this is static
6909 constructor of a non-BLKmode object. */
6910 if (cleared)
6911 need_to_clear = 0;
6912 else if (REG_P (target) && TREE_STATIC (exp))
6913 need_to_clear = 1;
6914 else
6916 unsigned HOST_WIDE_INT idx;
6917 HOST_WIDE_INT count = 0, zero_count = 0;
6918 need_to_clear = ! const_bounds_p;
6920 /* This loop is a more accurate version of the loop in
6921 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6922 is also needed to check for missing elements. */
6923 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6925 HOST_WIDE_INT this_node_count;
6927 if (need_to_clear)
6928 break;
6930 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6932 tree lo_index = TREE_OPERAND (index, 0);
6933 tree hi_index = TREE_OPERAND (index, 1);
6935 if (! tree_fits_uhwi_p (lo_index)
6936 || ! tree_fits_uhwi_p (hi_index))
6938 need_to_clear = 1;
6939 break;
6942 this_node_count = (tree_to_uhwi (hi_index)
6943 - tree_to_uhwi (lo_index) + 1);
6945 else
6946 this_node_count = 1;
6948 count += this_node_count;
6949 if (mostly_zeros_p (value))
6950 zero_count += this_node_count;
6953 /* Clear the entire array first if there are any missing
6954 elements, or if the incidence of zero elements is >=
6955 75%. */
6956 if (! need_to_clear
6957 && (count < maxelt - minelt + 1
6958 || 4 * zero_count >= 3 * count))
6959 need_to_clear = 1;
6962 if (need_to_clear && maybe_gt (size, 0))
6964 if (REG_P (target))
6965 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6966 else
6967 clear_storage (target, gen_int_mode (size, Pmode),
6968 BLOCK_OP_NORMAL);
6969 cleared = 1;
6972 if (!cleared && REG_P (target))
6973 /* Inform later passes that the old value is dead. */
6974 emit_clobber (target);
6976 /* Store each element of the constructor into the
6977 corresponding element of TARGET, determined by counting the
6978 elements. */
6979 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6981 machine_mode mode;
6982 poly_int64 bitsize;
6983 HOST_WIDE_INT bitpos;
6984 rtx xtarget = target;
6986 if (cleared && initializer_zerop (value))
6987 continue;
6989 mode = TYPE_MODE (elttype);
6990 if (mode != BLKmode)
6991 bitsize = GET_MODE_BITSIZE (mode);
6992 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
6993 bitsize = -1;
6995 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6997 tree lo_index = TREE_OPERAND (index, 0);
6998 tree hi_index = TREE_OPERAND (index, 1);
6999 rtx index_r, pos_rtx;
7000 HOST_WIDE_INT lo, hi, count;
7001 tree position;
7003 /* If the range is constant and "small", unroll the loop. */
7004 if (const_bounds_p
7005 && tree_fits_shwi_p (lo_index)
7006 && tree_fits_shwi_p (hi_index)
7007 && (lo = tree_to_shwi (lo_index),
7008 hi = tree_to_shwi (hi_index),
7009 count = hi - lo + 1,
7010 (!MEM_P (target)
7011 || count <= 2
7012 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7013 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7014 <= 40 * 8)))))
7016 lo -= minelt; hi -= minelt;
7017 for (; lo <= hi; lo++)
7019 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7021 if (MEM_P (target)
7022 && !MEM_KEEP_ALIAS_SET_P (target)
7023 && TREE_CODE (type) == ARRAY_TYPE
7024 && TYPE_NONALIASED_COMPONENT (type))
7026 target = copy_rtx (target);
7027 MEM_KEEP_ALIAS_SET_P (target) = 1;
7030 store_constructor_field
7031 (target, bitsize, bitpos, 0, bitregion_end,
7032 mode, value, cleared,
7033 get_alias_set (elttype), reverse);
7036 else
7038 rtx_code_label *loop_start = gen_label_rtx ();
7039 rtx_code_label *loop_end = gen_label_rtx ();
7040 tree exit_cond;
7042 expand_normal (hi_index);
7044 index = build_decl (EXPR_LOCATION (exp),
7045 VAR_DECL, NULL_TREE, domain);
7046 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7047 SET_DECL_RTL (index, index_r);
7048 store_expr (lo_index, index_r, 0, false, reverse);
7050 /* Build the head of the loop. */
7051 do_pending_stack_adjust ();
7052 emit_label (loop_start);
7054 /* Assign value to element index. */
7055 position =
7056 fold_convert (ssizetype,
7057 fold_build2 (MINUS_EXPR,
7058 TREE_TYPE (index),
7059 index,
7060 TYPE_MIN_VALUE (domain)));
7062 position =
7063 size_binop (MULT_EXPR, position,
7064 fold_convert (ssizetype,
7065 TYPE_SIZE_UNIT (elttype)));
7067 pos_rtx = expand_normal (position);
7068 xtarget = offset_address (target, pos_rtx,
7069 highest_pow2_factor (position));
7070 xtarget = adjust_address (xtarget, mode, 0);
7071 if (TREE_CODE (value) == CONSTRUCTOR)
7072 store_constructor (value, xtarget, cleared,
7073 exact_div (bitsize, BITS_PER_UNIT),
7074 reverse);
7075 else
7076 store_expr (value, xtarget, 0, false, reverse);
7078 /* Generate a conditional jump to exit the loop. */
7079 exit_cond = build2 (LT_EXPR, integer_type_node,
7080 index, hi_index);
7081 jumpif (exit_cond, loop_end,
7082 profile_probability::uninitialized ());
7084 /* Update the loop counter, and jump to the head of
7085 the loop. */
7086 expand_assignment (index,
7087 build2 (PLUS_EXPR, TREE_TYPE (index),
7088 index, integer_one_node),
7089 false);
7091 emit_jump (loop_start);
7093 /* Build the end of the loop. */
7094 emit_label (loop_end);
7097 else if ((index != 0 && ! tree_fits_shwi_p (index))
7098 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7100 tree position;
7102 if (index == 0)
7103 index = ssize_int (1);
7105 if (minelt)
7106 index = fold_convert (ssizetype,
7107 fold_build2 (MINUS_EXPR,
7108 TREE_TYPE (index),
7109 index,
7110 TYPE_MIN_VALUE (domain)));
7112 position =
7113 size_binop (MULT_EXPR, index,
7114 fold_convert (ssizetype,
7115 TYPE_SIZE_UNIT (elttype)));
7116 xtarget = offset_address (target,
7117 expand_normal (position),
7118 highest_pow2_factor (position));
7119 xtarget = adjust_address (xtarget, mode, 0);
7120 store_expr (value, xtarget, 0, false, reverse);
7122 else
7124 if (index != 0)
7125 bitpos = ((tree_to_shwi (index) - minelt)
7126 * tree_to_uhwi (TYPE_SIZE (elttype)));
7127 else
7128 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7130 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7131 && TREE_CODE (type) == ARRAY_TYPE
7132 && TYPE_NONALIASED_COMPONENT (type))
7134 target = copy_rtx (target);
7135 MEM_KEEP_ALIAS_SET_P (target) = 1;
7137 store_constructor_field (target, bitsize, bitpos, 0,
7138 bitregion_end, mode, value,
7139 cleared, get_alias_set (elttype),
7140 reverse);
7143 break;
7146 case VECTOR_TYPE:
7148 unsigned HOST_WIDE_INT idx;
7149 constructor_elt *ce;
7150 int i;
7151 int need_to_clear;
7152 insn_code icode = CODE_FOR_nothing;
7153 tree elt;
7154 tree elttype = TREE_TYPE (type);
7155 int elt_size = vector_element_bits (type);
7156 machine_mode eltmode = TYPE_MODE (elttype);
7157 HOST_WIDE_INT bitsize;
7158 HOST_WIDE_INT bitpos;
7159 rtvec vector = NULL;
7160 poly_uint64 n_elts;
7161 unsigned HOST_WIDE_INT const_n_elts;
7162 alias_set_type alias;
7163 bool vec_vec_init_p = false;
7164 machine_mode mode = GET_MODE (target);
7166 gcc_assert (eltmode != BLKmode);
7168 /* Try using vec_duplicate_optab for uniform vectors. */
7169 if (!TREE_SIDE_EFFECTS (exp)
7170 && VECTOR_MODE_P (mode)
7171 && eltmode == GET_MODE_INNER (mode)
7172 && ((icode = optab_handler (vec_duplicate_optab, mode))
7173 != CODE_FOR_nothing)
7174 && (elt = uniform_vector_p (exp))
7175 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7177 class expand_operand ops[2];
7178 create_output_operand (&ops[0], target, mode);
7179 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7180 expand_insn (icode, 2, ops);
7181 if (!rtx_equal_p (target, ops[0].value))
7182 emit_move_insn (target, ops[0].value);
7183 break;
7186 n_elts = TYPE_VECTOR_SUBPARTS (type);
7187 if (REG_P (target)
7188 && VECTOR_MODE_P (mode)
7189 && n_elts.is_constant (&const_n_elts))
7191 machine_mode emode = eltmode;
7192 bool vector_typed_elts_p = false;
7194 if (CONSTRUCTOR_NELTS (exp)
7195 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7196 == VECTOR_TYPE))
7198 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7199 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7200 * TYPE_VECTOR_SUBPARTS (etype),
7201 n_elts));
7202 emode = TYPE_MODE (etype);
7203 vector_typed_elts_p = true;
7205 icode = convert_optab_handler (vec_init_optab, mode, emode);
7206 if (icode != CODE_FOR_nothing)
7208 unsigned int n = const_n_elts;
7210 if (vector_typed_elts_p)
7212 n = CONSTRUCTOR_NELTS (exp);
7213 vec_vec_init_p = true;
7215 vector = rtvec_alloc (n);
7216 for (unsigned int k = 0; k < n; k++)
7217 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7221 /* Compute the size of the elements in the CTOR. It differs
7222 from the size of the vector type elements only when the
7223 CTOR elements are vectors themselves. */
7224 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7225 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7226 : elttype);
7227 if (VECTOR_TYPE_P (val_type))
7228 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7229 else
7230 bitsize = elt_size;
7232 /* If the constructor has fewer elements than the vector,
7233 clear the whole array first. Similarly if this is static
7234 constructor of a non-BLKmode object. */
7235 if (cleared)
7236 need_to_clear = 0;
7237 else if (REG_P (target) && TREE_STATIC (exp))
7238 need_to_clear = 1;
7239 else
7241 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7242 tree value;
7244 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7246 int n_elts_here = bitsize / elt_size;
7247 count += n_elts_here;
7248 if (mostly_zeros_p (value))
7249 zero_count += n_elts_here;
7252 /* Clear the entire vector first if there are any missing elements,
7253 or if the incidence of zero elements is >= 75%. */
7254 need_to_clear = (maybe_lt (count, n_elts)
7255 || 4 * zero_count >= 3 * count);
7258 if (need_to_clear && maybe_gt (size, 0) && !vector)
7260 if (REG_P (target))
7261 emit_move_insn (target, CONST0_RTX (mode));
7262 else
7263 clear_storage (target, gen_int_mode (size, Pmode),
7264 BLOCK_OP_NORMAL);
7265 cleared = 1;
7268 /* Inform later passes that the old value is dead. */
7269 if (!cleared && !vector && REG_P (target))
7270 emit_move_insn (target, CONST0_RTX (mode));
7272 if (MEM_P (target))
7273 alias = MEM_ALIAS_SET (target);
7274 else
7275 alias = get_alias_set (elttype);
7277 /* Store each element of the constructor into the corresponding
7278 element of TARGET, determined by counting the elements. */
7279 for (idx = 0, i = 0;
7280 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7281 idx++, i += bitsize / elt_size)
7283 HOST_WIDE_INT eltpos;
7284 tree value = ce->value;
7286 if (cleared && initializer_zerop (value))
7287 continue;
7289 if (ce->index)
7290 eltpos = tree_to_uhwi (ce->index);
7291 else
7292 eltpos = i;
7294 if (vector)
7296 if (vec_vec_init_p)
7298 gcc_assert (ce->index == NULL_TREE);
7299 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7300 eltpos = idx;
7302 else
7303 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7304 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7306 else
7308 machine_mode value_mode
7309 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7310 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7311 bitpos = eltpos * elt_size;
7312 store_constructor_field (target, bitsize, bitpos, 0,
7313 bitregion_end, value_mode,
7314 value, cleared, alias, reverse);
7318 if (vector)
7319 emit_insn (GEN_FCN (icode) (target,
7320 gen_rtx_PARALLEL (mode, vector)));
7321 break;
7324 default:
7325 gcc_unreachable ();
7329 /* Store the value of EXP (an expression tree)
7330 into a subfield of TARGET which has mode MODE and occupies
7331 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7332 If MODE is VOIDmode, it means that we are storing into a bit-field.
7334 BITREGION_START is bitpos of the first bitfield in this region.
7335 BITREGION_END is the bitpos of the ending bitfield in this region.
7336 These two fields are 0, if the C++ memory model does not apply,
7337 or we are not interested in keeping track of bitfield regions.
7339 Always return const0_rtx unless we have something particular to
7340 return.
7342 ALIAS_SET is the alias set for the destination. This value will
7343 (in general) be different from that for TARGET, since TARGET is a
7344 reference to the containing structure.
7346 If NONTEMPORAL is true, try generating a nontemporal store.
7348 If REVERSE is true, the store is to be done in reverse order. */
7350 static rtx
7351 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7352 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7353 machine_mode mode, tree exp,
7354 alias_set_type alias_set, bool nontemporal, bool reverse)
7356 if (TREE_CODE (exp) == ERROR_MARK)
7357 return const0_rtx;
7359 /* If we have nothing to store, do nothing unless the expression has
7360 side-effects. Don't do that for zero sized addressable lhs of
7361 calls. */
7362 if (known_eq (bitsize, 0)
7363 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7364 || TREE_CODE (exp) != CALL_EXPR))
7365 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7367 if (GET_CODE (target) == CONCAT)
7369 /* We're storing into a struct containing a single __complex. */
7371 gcc_assert (known_eq (bitpos, 0));
7372 return store_expr (exp, target, 0, nontemporal, reverse);
7375 /* If the structure is in a register or if the component
7376 is a bit field, we cannot use addressing to access it.
7377 Use bit-field techniques or SUBREG to store in it. */
7379 poly_int64 decl_bitsize;
7380 if (mode == VOIDmode
7381 || (mode != BLKmode && ! direct_store[(int) mode]
7382 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7383 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7384 || REG_P (target)
7385 || GET_CODE (target) == SUBREG
7386 /* If the field isn't aligned enough to store as an ordinary memref,
7387 store it as a bit field. */
7388 || (mode != BLKmode
7389 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7390 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7391 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7392 || !multiple_p (bitpos, BITS_PER_UNIT)))
7393 || (known_size_p (bitsize)
7394 && mode != BLKmode
7395 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7396 /* If the RHS and field are a constant size and the size of the
7397 RHS isn't the same size as the bitfield, we must use bitfield
7398 operations. */
7399 || (known_size_p (bitsize)
7400 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7401 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7402 bitsize)
7403 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7404 we will handle specially below. */
7405 && !(TREE_CODE (exp) == CONSTRUCTOR
7406 && multiple_p (bitsize, BITS_PER_UNIT))
7407 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7408 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7409 includes some extra padding. store_expr / expand_expr will in
7410 that case call get_inner_reference that will have the bitsize
7411 we check here and thus the block move will not clobber the
7412 padding that shouldn't be clobbered. In the future we could
7413 replace the TREE_ADDRESSABLE check with a check that
7414 get_base_address needs to live in memory. */
7415 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7416 || TREE_CODE (exp) != COMPONENT_REF
7417 || !multiple_p (bitsize, BITS_PER_UNIT)
7418 || !multiple_p (bitpos, BITS_PER_UNIT)
7419 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7420 &decl_bitsize)
7421 || maybe_ne (decl_bitsize, bitsize))
7422 /* A call with an addressable return type and return-slot
7423 optimization must not need bitfield operations but we must
7424 pass down the original target. */
7425 && (TREE_CODE (exp) != CALL_EXPR
7426 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7427 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7428 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7429 decl we must use bitfield operations. */
7430 || (known_size_p (bitsize)
7431 && TREE_CODE (exp) == MEM_REF
7432 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7433 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7434 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7435 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7437 rtx temp;
7438 gimple *nop_def;
7440 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7441 implies a mask operation. If the precision is the same size as
7442 the field we're storing into, that mask is redundant. This is
7443 particularly common with bit field assignments generated by the
7444 C front end. */
7445 nop_def = get_def_for_expr (exp, NOP_EXPR);
7446 if (nop_def)
7448 tree type = TREE_TYPE (exp);
7449 if (INTEGRAL_TYPE_P (type)
7450 && maybe_ne (TYPE_PRECISION (type),
7451 GET_MODE_BITSIZE (TYPE_MODE (type)))
7452 && known_eq (bitsize, TYPE_PRECISION (type)))
7454 tree op = gimple_assign_rhs1 (nop_def);
7455 type = TREE_TYPE (op);
7456 if (INTEGRAL_TYPE_P (type)
7457 && known_ge (TYPE_PRECISION (type), bitsize))
7458 exp = op;
7462 temp = expand_normal (exp);
7464 /* We don't support variable-sized BLKmode bitfields, since our
7465 handling of BLKmode is bound up with the ability to break
7466 things into words. */
7467 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7469 /* Handle calls that return values in multiple non-contiguous locations.
7470 The Irix 6 ABI has examples of this. */
7471 if (GET_CODE (temp) == PARALLEL)
7473 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7474 machine_mode temp_mode = GET_MODE (temp);
7475 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7476 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7477 rtx temp_target = gen_reg_rtx (temp_mode);
7478 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7479 temp = temp_target;
7482 /* Handle calls that return BLKmode values in registers. */
7483 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7485 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7486 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7487 temp = temp_target;
7490 /* If the value has aggregate type and an integral mode then, if BITSIZE
7491 is narrower than this mode and this is for big-endian data, we first
7492 need to put the value into the low-order bits for store_bit_field,
7493 except when MODE is BLKmode and BITSIZE larger than the word size
7494 (see the handling of fields larger than a word in store_bit_field).
7495 Moreover, the field may be not aligned on a byte boundary; in this
7496 case, if it has reverse storage order, it needs to be accessed as a
7497 scalar field with reverse storage order and we must first put the
7498 value into target order. */
7499 scalar_int_mode temp_mode;
7500 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7501 && is_int_mode (GET_MODE (temp), &temp_mode))
7503 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7505 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7507 if (reverse)
7508 temp = flip_storage_order (temp_mode, temp);
7510 gcc_checking_assert (known_le (bitsize, size));
7511 if (maybe_lt (bitsize, size)
7512 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7513 /* Use of to_constant for BLKmode was checked above. */
7514 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7515 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7516 size - bitsize, NULL_RTX, 1);
7519 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7520 if (mode != VOIDmode && mode != BLKmode
7521 && mode != TYPE_MODE (TREE_TYPE (exp)))
7522 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7524 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7525 and BITPOS must be aligned on a byte boundary. If so, we simply do
7526 a block copy. Likewise for a BLKmode-like TARGET. */
7527 if (GET_MODE (temp) == BLKmode
7528 && (GET_MODE (target) == BLKmode
7529 || (MEM_P (target)
7530 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7531 && multiple_p (bitpos, BITS_PER_UNIT)
7532 && multiple_p (bitsize, BITS_PER_UNIT))))
7534 gcc_assert (MEM_P (target) && MEM_P (temp));
7535 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7536 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7538 target = adjust_address (target, VOIDmode, bytepos);
7539 emit_block_move (target, temp,
7540 gen_int_mode (bytesize, Pmode),
7541 BLOCK_OP_NORMAL);
7543 return const0_rtx;
7546 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7547 word size, we need to load the value (see again store_bit_field). */
7548 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7550 temp_mode = smallest_int_mode_for_size (bitsize);
7551 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7552 temp_mode, false, NULL);
7555 /* Store the value in the bitfield. */
7556 gcc_checking_assert (known_ge (bitpos, 0));
7557 store_bit_field (target, bitsize, bitpos,
7558 bitregion_start, bitregion_end,
7559 mode, temp, reverse);
7561 return const0_rtx;
7563 else
7565 /* Now build a reference to just the desired component. */
7566 rtx to_rtx = adjust_address (target, mode,
7567 exact_div (bitpos, BITS_PER_UNIT));
7569 if (to_rtx == target)
7570 to_rtx = copy_rtx (to_rtx);
7572 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7573 set_mem_alias_set (to_rtx, alias_set);
7575 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7576 into a target smaller than its type; handle that case now. */
7577 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7579 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7580 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7581 return to_rtx;
7584 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7588 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7589 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7590 codes and find the ultimate containing object, which we return.
7592 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7593 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7594 storage order of the field.
7595 If the position of the field is variable, we store a tree
7596 giving the variable offset (in units) in *POFFSET.
7597 This offset is in addition to the bit position.
7598 If the position is not variable, we store 0 in *POFFSET.
7600 If any of the extraction expressions is volatile,
7601 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7603 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7604 Otherwise, it is a mode that can be used to access the field.
7606 If the field describes a variable-sized object, *PMODE is set to
7607 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7608 this case, but the address of the object can be found. */
7610 tree
7611 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7612 poly_int64_pod *pbitpos, tree *poffset,
7613 machine_mode *pmode, int *punsignedp,
7614 int *preversep, int *pvolatilep)
7616 tree size_tree = 0;
7617 machine_mode mode = VOIDmode;
7618 bool blkmode_bitfield = false;
7619 tree offset = size_zero_node;
7620 poly_offset_int bit_offset = 0;
7622 /* First get the mode, signedness, storage order and size. We do this from
7623 just the outermost expression. */
7624 *pbitsize = -1;
7625 if (TREE_CODE (exp) == COMPONENT_REF)
7627 tree field = TREE_OPERAND (exp, 1);
7628 size_tree = DECL_SIZE (field);
7629 if (flag_strict_volatile_bitfields > 0
7630 && TREE_THIS_VOLATILE (exp)
7631 && DECL_BIT_FIELD_TYPE (field)
7632 && DECL_MODE (field) != BLKmode)
7633 /* Volatile bitfields should be accessed in the mode of the
7634 field's type, not the mode computed based on the bit
7635 size. */
7636 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7637 else if (!DECL_BIT_FIELD (field))
7639 mode = DECL_MODE (field);
7640 /* For vector fields re-check the target flags, as DECL_MODE
7641 could have been set with different target flags than
7642 the current function has. */
7643 if (mode == BLKmode
7644 && VECTOR_TYPE_P (TREE_TYPE (field))
7645 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7646 mode = TYPE_MODE (TREE_TYPE (field));
7648 else if (DECL_MODE (field) == BLKmode)
7649 blkmode_bitfield = true;
7651 *punsignedp = DECL_UNSIGNED (field);
7653 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7655 size_tree = TREE_OPERAND (exp, 1);
7656 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7657 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7659 /* For vector element types with the correct size of access or for
7660 vector typed accesses use the mode of the access type. */
7661 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7662 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7663 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7664 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7665 mode = TYPE_MODE (TREE_TYPE (exp));
7667 else
7669 mode = TYPE_MODE (TREE_TYPE (exp));
7670 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7672 if (mode == BLKmode)
7673 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7674 else
7675 *pbitsize = GET_MODE_BITSIZE (mode);
7678 if (size_tree != 0)
7680 if (! tree_fits_uhwi_p (size_tree))
7681 mode = BLKmode, *pbitsize = -1;
7682 else
7683 *pbitsize = tree_to_uhwi (size_tree);
7686 *preversep = reverse_storage_order_for_component_p (exp);
7688 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7689 and find the ultimate containing object. */
7690 while (1)
7692 switch (TREE_CODE (exp))
7694 case BIT_FIELD_REF:
7695 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7696 break;
7698 case COMPONENT_REF:
7700 tree field = TREE_OPERAND (exp, 1);
7701 tree this_offset = component_ref_field_offset (exp);
7703 /* If this field hasn't been filled in yet, don't go past it.
7704 This should only happen when folding expressions made during
7705 type construction. */
7706 if (this_offset == 0)
7707 break;
7709 offset = size_binop (PLUS_EXPR, offset, this_offset);
7710 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7712 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7714 break;
7716 case ARRAY_REF:
7717 case ARRAY_RANGE_REF:
7719 tree index = TREE_OPERAND (exp, 1);
7720 tree low_bound = array_ref_low_bound (exp);
7721 tree unit_size = array_ref_element_size (exp);
7723 /* We assume all arrays have sizes that are a multiple of a byte.
7724 First subtract the lower bound, if any, in the type of the
7725 index, then convert to sizetype and multiply by the size of
7726 the array element. */
7727 if (! integer_zerop (low_bound))
7728 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7729 index, low_bound);
7731 offset = size_binop (PLUS_EXPR, offset,
7732 size_binop (MULT_EXPR,
7733 fold_convert (sizetype, index),
7734 unit_size));
7736 break;
7738 case REALPART_EXPR:
7739 break;
7741 case IMAGPART_EXPR:
7742 bit_offset += *pbitsize;
7743 break;
7745 case VIEW_CONVERT_EXPR:
7746 break;
7748 case MEM_REF:
7749 /* Hand back the decl for MEM[&decl, off]. */
7750 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7752 tree off = TREE_OPERAND (exp, 1);
7753 if (!integer_zerop (off))
7755 poly_offset_int boff = mem_ref_offset (exp);
7756 boff <<= LOG2_BITS_PER_UNIT;
7757 bit_offset += boff;
7759 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7761 goto done;
7763 default:
7764 goto done;
7767 /* If any reference in the chain is volatile, the effect is volatile. */
7768 if (TREE_THIS_VOLATILE (exp))
7769 *pvolatilep = 1;
7771 exp = TREE_OPERAND (exp, 0);
7773 done:
7775 /* If OFFSET is constant, see if we can return the whole thing as a
7776 constant bit position. Make sure to handle overflow during
7777 this conversion. */
7778 if (poly_int_tree_p (offset))
7780 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7781 TYPE_PRECISION (sizetype));
7782 tem <<= LOG2_BITS_PER_UNIT;
7783 tem += bit_offset;
7784 if (tem.to_shwi (pbitpos))
7785 *poffset = offset = NULL_TREE;
7788 /* Otherwise, split it up. */
7789 if (offset)
7791 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7792 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7794 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7795 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7796 offset = size_binop (PLUS_EXPR, offset,
7797 build_int_cst (sizetype, bytes.force_shwi ()));
7800 *poffset = offset;
7803 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7804 if (mode == VOIDmode
7805 && blkmode_bitfield
7806 && multiple_p (*pbitpos, BITS_PER_UNIT)
7807 && multiple_p (*pbitsize, BITS_PER_UNIT))
7808 *pmode = BLKmode;
7809 else
7810 *pmode = mode;
7812 return exp;
7815 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7817 static unsigned HOST_WIDE_INT
7818 target_align (const_tree target)
7820 /* We might have a chain of nested references with intermediate misaligning
7821 bitfields components, so need to recurse to find out. */
7823 unsigned HOST_WIDE_INT this_align, outer_align;
7825 switch (TREE_CODE (target))
7827 case BIT_FIELD_REF:
7828 return 1;
7830 case COMPONENT_REF:
7831 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7832 outer_align = target_align (TREE_OPERAND (target, 0));
7833 return MIN (this_align, outer_align);
7835 case ARRAY_REF:
7836 case ARRAY_RANGE_REF:
7837 this_align = TYPE_ALIGN (TREE_TYPE (target));
7838 outer_align = target_align (TREE_OPERAND (target, 0));
7839 return MIN (this_align, outer_align);
7841 CASE_CONVERT:
7842 case NON_LVALUE_EXPR:
7843 case VIEW_CONVERT_EXPR:
7844 this_align = TYPE_ALIGN (TREE_TYPE (target));
7845 outer_align = target_align (TREE_OPERAND (target, 0));
7846 return MAX (this_align, outer_align);
7848 default:
7849 return TYPE_ALIGN (TREE_TYPE (target));
7854 /* Given an rtx VALUE that may contain additions and multiplications, return
7855 an equivalent value that just refers to a register, memory, or constant.
7856 This is done by generating instructions to perform the arithmetic and
7857 returning a pseudo-register containing the value.
7859 The returned value may be a REG, SUBREG, MEM or constant. */
7862 force_operand (rtx value, rtx target)
7864 rtx op1, op2;
7865 /* Use subtarget as the target for operand 0 of a binary operation. */
7866 rtx subtarget = get_subtarget (target);
7867 enum rtx_code code = GET_CODE (value);
7869 /* Check for subreg applied to an expression produced by loop optimizer. */
7870 if (code == SUBREG
7871 && !REG_P (SUBREG_REG (value))
7872 && !MEM_P (SUBREG_REG (value)))
7874 value
7875 = simplify_gen_subreg (GET_MODE (value),
7876 force_reg (GET_MODE (SUBREG_REG (value)),
7877 force_operand (SUBREG_REG (value),
7878 NULL_RTX)),
7879 GET_MODE (SUBREG_REG (value)),
7880 SUBREG_BYTE (value));
7881 code = GET_CODE (value);
7884 /* Check for a PIC address load. */
7885 if ((code == PLUS || code == MINUS)
7886 && XEXP (value, 0) == pic_offset_table_rtx
7887 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7888 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7889 || GET_CODE (XEXP (value, 1)) == CONST))
7891 if (!subtarget)
7892 subtarget = gen_reg_rtx (GET_MODE (value));
7893 emit_move_insn (subtarget, value);
7894 return subtarget;
7897 if (ARITHMETIC_P (value))
7899 op2 = XEXP (value, 1);
7900 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7901 subtarget = 0;
7902 if (code == MINUS && CONST_INT_P (op2))
7904 code = PLUS;
7905 op2 = negate_rtx (GET_MODE (value), op2);
7908 /* Check for an addition with OP2 a constant integer and our first
7909 operand a PLUS of a virtual register and something else. In that
7910 case, we want to emit the sum of the virtual register and the
7911 constant first and then add the other value. This allows virtual
7912 register instantiation to simply modify the constant rather than
7913 creating another one around this addition. */
7914 if (code == PLUS && CONST_INT_P (op2)
7915 && GET_CODE (XEXP (value, 0)) == PLUS
7916 && REG_P (XEXP (XEXP (value, 0), 0))
7917 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7918 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7920 rtx temp = expand_simple_binop (GET_MODE (value), code,
7921 XEXP (XEXP (value, 0), 0), op2,
7922 subtarget, 0, OPTAB_LIB_WIDEN);
7923 return expand_simple_binop (GET_MODE (value), code, temp,
7924 force_operand (XEXP (XEXP (value,
7925 0), 1), 0),
7926 target, 0, OPTAB_LIB_WIDEN);
7929 op1 = force_operand (XEXP (value, 0), subtarget);
7930 op2 = force_operand (op2, NULL_RTX);
7931 switch (code)
7933 case MULT:
7934 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7935 case DIV:
7936 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7937 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7938 target, 1, OPTAB_LIB_WIDEN);
7939 else
7940 return expand_divmod (0,
7941 FLOAT_MODE_P (GET_MODE (value))
7942 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7943 GET_MODE (value), op1, op2, target, 0);
7944 case MOD:
7945 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7946 target, 0);
7947 case UDIV:
7948 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7949 target, 1);
7950 case UMOD:
7951 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7952 target, 1);
7953 case ASHIFTRT:
7954 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7955 target, 0, OPTAB_LIB_WIDEN);
7956 default:
7957 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7958 target, 1, OPTAB_LIB_WIDEN);
7961 if (UNARY_P (value))
7963 if (!target)
7964 target = gen_reg_rtx (GET_MODE (value));
7965 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7966 switch (code)
7968 case ZERO_EXTEND:
7969 case SIGN_EXTEND:
7970 case TRUNCATE:
7971 case FLOAT_EXTEND:
7972 case FLOAT_TRUNCATE:
7973 convert_move (target, op1, code == ZERO_EXTEND);
7974 return target;
7976 case FIX:
7977 case UNSIGNED_FIX:
7978 expand_fix (target, op1, code == UNSIGNED_FIX);
7979 return target;
7981 case FLOAT:
7982 case UNSIGNED_FLOAT:
7983 expand_float (target, op1, code == UNSIGNED_FLOAT);
7984 return target;
7986 default:
7987 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7991 #ifdef INSN_SCHEDULING
7992 /* On machines that have insn scheduling, we want all memory reference to be
7993 explicit, so we need to deal with such paradoxical SUBREGs. */
7994 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7995 value
7996 = simplify_gen_subreg (GET_MODE (value),
7997 force_reg (GET_MODE (SUBREG_REG (value)),
7998 force_operand (SUBREG_REG (value),
7999 NULL_RTX)),
8000 GET_MODE (SUBREG_REG (value)),
8001 SUBREG_BYTE (value));
8002 #endif
8004 return value;
8007 /* Subroutine of expand_expr: return nonzero iff there is no way that
8008 EXP can reference X, which is being modified. TOP_P is nonzero if this
8009 call is going to be used to determine whether we need a temporary
8010 for EXP, as opposed to a recursive call to this function.
8012 It is always safe for this routine to return zero since it merely
8013 searches for optimization opportunities. */
8016 safe_from_p (const_rtx x, tree exp, int top_p)
8018 rtx exp_rtl = 0;
8019 int i, nops;
8021 if (x == 0
8022 /* If EXP has varying size, we MUST use a target since we currently
8023 have no way of allocating temporaries of variable size
8024 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8025 So we assume here that something at a higher level has prevented a
8026 clash. This is somewhat bogus, but the best we can do. Only
8027 do this when X is BLKmode and when we are at the top level. */
8028 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8029 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8030 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8031 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8032 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8033 != INTEGER_CST)
8034 && GET_MODE (x) == BLKmode)
8035 /* If X is in the outgoing argument area, it is always safe. */
8036 || (MEM_P (x)
8037 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8038 || (GET_CODE (XEXP (x, 0)) == PLUS
8039 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8040 return 1;
8042 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8043 find the underlying pseudo. */
8044 if (GET_CODE (x) == SUBREG)
8046 x = SUBREG_REG (x);
8047 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8048 return 0;
8051 /* Now look at our tree code and possibly recurse. */
8052 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8054 case tcc_declaration:
8055 exp_rtl = DECL_RTL_IF_SET (exp);
8056 break;
8058 case tcc_constant:
8059 return 1;
8061 case tcc_exceptional:
8062 if (TREE_CODE (exp) == TREE_LIST)
8064 while (1)
8066 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8067 return 0;
8068 exp = TREE_CHAIN (exp);
8069 if (!exp)
8070 return 1;
8071 if (TREE_CODE (exp) != TREE_LIST)
8072 return safe_from_p (x, exp, 0);
8075 else if (TREE_CODE (exp) == CONSTRUCTOR)
8077 constructor_elt *ce;
8078 unsigned HOST_WIDE_INT idx;
8080 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8081 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8082 || !safe_from_p (x, ce->value, 0))
8083 return 0;
8084 return 1;
8086 else if (TREE_CODE (exp) == ERROR_MARK)
8087 return 1; /* An already-visited SAVE_EXPR? */
8088 else
8089 return 0;
8091 case tcc_statement:
8092 /* The only case we look at here is the DECL_INITIAL inside a
8093 DECL_EXPR. */
8094 return (TREE_CODE (exp) != DECL_EXPR
8095 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8096 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8097 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8099 case tcc_binary:
8100 case tcc_comparison:
8101 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8102 return 0;
8103 /* Fall through. */
8105 case tcc_unary:
8106 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8108 case tcc_expression:
8109 case tcc_reference:
8110 case tcc_vl_exp:
8111 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8112 the expression. If it is set, we conflict iff we are that rtx or
8113 both are in memory. Otherwise, we check all operands of the
8114 expression recursively. */
8116 switch (TREE_CODE (exp))
8118 case ADDR_EXPR:
8119 /* If the operand is static or we are static, we can't conflict.
8120 Likewise if we don't conflict with the operand at all. */
8121 if (staticp (TREE_OPERAND (exp, 0))
8122 || TREE_STATIC (exp)
8123 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8124 return 1;
8126 /* Otherwise, the only way this can conflict is if we are taking
8127 the address of a DECL a that address if part of X, which is
8128 very rare. */
8129 exp = TREE_OPERAND (exp, 0);
8130 if (DECL_P (exp))
8132 if (!DECL_RTL_SET_P (exp)
8133 || !MEM_P (DECL_RTL (exp)))
8134 return 0;
8135 else
8136 exp_rtl = XEXP (DECL_RTL (exp), 0);
8138 break;
8140 case MEM_REF:
8141 if (MEM_P (x)
8142 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8143 get_alias_set (exp)))
8144 return 0;
8145 break;
8147 case CALL_EXPR:
8148 /* Assume that the call will clobber all hard registers and
8149 all of memory. */
8150 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8151 || MEM_P (x))
8152 return 0;
8153 break;
8155 case WITH_CLEANUP_EXPR:
8156 case CLEANUP_POINT_EXPR:
8157 /* Lowered by gimplify.c. */
8158 gcc_unreachable ();
8160 case SAVE_EXPR:
8161 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8163 default:
8164 break;
8167 /* If we have an rtx, we do not need to scan our operands. */
8168 if (exp_rtl)
8169 break;
8171 nops = TREE_OPERAND_LENGTH (exp);
8172 for (i = 0; i < nops; i++)
8173 if (TREE_OPERAND (exp, i) != 0
8174 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8175 return 0;
8177 break;
8179 case tcc_type:
8180 /* Should never get a type here. */
8181 gcc_unreachable ();
8184 /* If we have an rtl, find any enclosed object. Then see if we conflict
8185 with it. */
8186 if (exp_rtl)
8188 if (GET_CODE (exp_rtl) == SUBREG)
8190 exp_rtl = SUBREG_REG (exp_rtl);
8191 if (REG_P (exp_rtl)
8192 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8193 return 0;
8196 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8197 are memory and they conflict. */
8198 return ! (rtx_equal_p (x, exp_rtl)
8199 || (MEM_P (x) && MEM_P (exp_rtl)
8200 && true_dependence (exp_rtl, VOIDmode, x)));
8203 /* If we reach here, it is safe. */
8204 return 1;
8208 /* Return the highest power of two that EXP is known to be a multiple of.
8209 This is used in updating alignment of MEMs in array references. */
8211 unsigned HOST_WIDE_INT
8212 highest_pow2_factor (const_tree exp)
8214 unsigned HOST_WIDE_INT ret;
8215 int trailing_zeros = tree_ctz (exp);
8216 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8217 return BIGGEST_ALIGNMENT;
8218 ret = HOST_WIDE_INT_1U << trailing_zeros;
8219 if (ret > BIGGEST_ALIGNMENT)
8220 return BIGGEST_ALIGNMENT;
8221 return ret;
8224 /* Similar, except that the alignment requirements of TARGET are
8225 taken into account. Assume it is at least as aligned as its
8226 type, unless it is a COMPONENT_REF in which case the layout of
8227 the structure gives the alignment. */
8229 static unsigned HOST_WIDE_INT
8230 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8232 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8233 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8235 return MAX (factor, talign);
8238 /* Convert the tree comparison code TCODE to the rtl one where the
8239 signedness is UNSIGNEDP. */
8241 static enum rtx_code
8242 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8244 enum rtx_code code;
8245 switch (tcode)
8247 case EQ_EXPR:
8248 code = EQ;
8249 break;
8250 case NE_EXPR:
8251 code = NE;
8252 break;
8253 case LT_EXPR:
8254 code = unsignedp ? LTU : LT;
8255 break;
8256 case LE_EXPR:
8257 code = unsignedp ? LEU : LE;
8258 break;
8259 case GT_EXPR:
8260 code = unsignedp ? GTU : GT;
8261 break;
8262 case GE_EXPR:
8263 code = unsignedp ? GEU : GE;
8264 break;
8265 case UNORDERED_EXPR:
8266 code = UNORDERED;
8267 break;
8268 case ORDERED_EXPR:
8269 code = ORDERED;
8270 break;
8271 case UNLT_EXPR:
8272 code = UNLT;
8273 break;
8274 case UNLE_EXPR:
8275 code = UNLE;
8276 break;
8277 case UNGT_EXPR:
8278 code = UNGT;
8279 break;
8280 case UNGE_EXPR:
8281 code = UNGE;
8282 break;
8283 case UNEQ_EXPR:
8284 code = UNEQ;
8285 break;
8286 case LTGT_EXPR:
8287 code = LTGT;
8288 break;
8290 default:
8291 gcc_unreachable ();
8293 return code;
8296 /* Subroutine of expand_expr. Expand the two operands of a binary
8297 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8298 The value may be stored in TARGET if TARGET is nonzero. The
8299 MODIFIER argument is as documented by expand_expr. */
8301 void
8302 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8303 enum expand_modifier modifier)
8305 if (! safe_from_p (target, exp1, 1))
8306 target = 0;
8307 if (operand_equal_p (exp0, exp1, 0))
8309 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8310 *op1 = copy_rtx (*op0);
8312 else
8314 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8315 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8320 /* Return a MEM that contains constant EXP. DEFER is as for
8321 output_constant_def and MODIFIER is as for expand_expr. */
8323 static rtx
8324 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8326 rtx mem;
8328 mem = output_constant_def (exp, defer);
8329 if (modifier != EXPAND_INITIALIZER)
8330 mem = use_anchored_address (mem);
8331 return mem;
8334 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8335 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8337 static rtx
8338 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8339 enum expand_modifier modifier, addr_space_t as)
8341 rtx result, subtarget;
8342 tree inner, offset;
8343 poly_int64 bitsize, bitpos;
8344 int unsignedp, reversep, volatilep = 0;
8345 machine_mode mode1;
8347 /* If we are taking the address of a constant and are at the top level,
8348 we have to use output_constant_def since we can't call force_const_mem
8349 at top level. */
8350 /* ??? This should be considered a front-end bug. We should not be
8351 generating ADDR_EXPR of something that isn't an LVALUE. The only
8352 exception here is STRING_CST. */
8353 if (CONSTANT_CLASS_P (exp))
8355 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8356 if (modifier < EXPAND_SUM)
8357 result = force_operand (result, target);
8358 return result;
8361 /* Everything must be something allowed by is_gimple_addressable. */
8362 switch (TREE_CODE (exp))
8364 case INDIRECT_REF:
8365 /* This case will happen via recursion for &a->b. */
8366 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8368 case MEM_REF:
8370 tree tem = TREE_OPERAND (exp, 0);
8371 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8372 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8373 return expand_expr (tem, target, tmode, modifier);
8376 case TARGET_MEM_REF:
8377 return addr_for_mem_ref (exp, as, true);
8379 case CONST_DECL:
8380 /* Expand the initializer like constants above. */
8381 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8382 0, modifier), 0);
8383 if (modifier < EXPAND_SUM)
8384 result = force_operand (result, target);
8385 return result;
8387 case REALPART_EXPR:
8388 /* The real part of the complex number is always first, therefore
8389 the address is the same as the address of the parent object. */
8390 offset = 0;
8391 bitpos = 0;
8392 inner = TREE_OPERAND (exp, 0);
8393 break;
8395 case IMAGPART_EXPR:
8396 /* The imaginary part of the complex number is always second.
8397 The expression is therefore always offset by the size of the
8398 scalar type. */
8399 offset = 0;
8400 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8401 inner = TREE_OPERAND (exp, 0);
8402 break;
8404 case COMPOUND_LITERAL_EXPR:
8405 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8406 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8407 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8408 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8409 the initializers aren't gimplified. */
8410 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8411 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8412 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8413 target, tmode, modifier, as);
8414 /* FALLTHRU */
8415 default:
8416 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8417 expand_expr, as that can have various side effects; LABEL_DECLs for
8418 example, may not have their DECL_RTL set yet. Expand the rtl of
8419 CONSTRUCTORs too, which should yield a memory reference for the
8420 constructor's contents. Assume language specific tree nodes can
8421 be expanded in some interesting way. */
8422 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8423 if (DECL_P (exp)
8424 || TREE_CODE (exp) == CONSTRUCTOR
8425 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8427 result = expand_expr (exp, target, tmode,
8428 modifier == EXPAND_INITIALIZER
8429 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8431 /* If the DECL isn't in memory, then the DECL wasn't properly
8432 marked TREE_ADDRESSABLE, which will be either a front-end
8433 or a tree optimizer bug. */
8435 gcc_assert (MEM_P (result));
8436 result = XEXP (result, 0);
8438 /* ??? Is this needed anymore? */
8439 if (DECL_P (exp))
8440 TREE_USED (exp) = 1;
8442 if (modifier != EXPAND_INITIALIZER
8443 && modifier != EXPAND_CONST_ADDRESS
8444 && modifier != EXPAND_SUM)
8445 result = force_operand (result, target);
8446 return result;
8449 /* Pass FALSE as the last argument to get_inner_reference although
8450 we are expanding to RTL. The rationale is that we know how to
8451 handle "aligning nodes" here: we can just bypass them because
8452 they won't change the final object whose address will be returned
8453 (they actually exist only for that purpose). */
8454 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8455 &unsignedp, &reversep, &volatilep);
8456 break;
8459 /* We must have made progress. */
8460 gcc_assert (inner != exp);
8462 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8463 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8464 inner alignment, force the inner to be sufficiently aligned. */
8465 if (CONSTANT_CLASS_P (inner)
8466 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8468 inner = copy_node (inner);
8469 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8470 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8471 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8473 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8475 if (offset)
8477 rtx tmp;
8479 if (modifier != EXPAND_NORMAL)
8480 result = force_operand (result, NULL);
8481 tmp = expand_expr (offset, NULL_RTX, tmode,
8482 modifier == EXPAND_INITIALIZER
8483 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8485 /* expand_expr is allowed to return an object in a mode other
8486 than TMODE. If it did, we need to convert. */
8487 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8488 tmp = convert_modes (tmode, GET_MODE (tmp),
8489 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8490 result = convert_memory_address_addr_space (tmode, result, as);
8491 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8493 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8494 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8495 else
8497 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8498 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8499 1, OPTAB_LIB_WIDEN);
8503 if (maybe_ne (bitpos, 0))
8505 /* Someone beforehand should have rejected taking the address
8506 of an object that isn't byte-aligned. */
8507 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8508 result = convert_memory_address_addr_space (tmode, result, as);
8509 result = plus_constant (tmode, result, bytepos);
8510 if (modifier < EXPAND_SUM)
8511 result = force_operand (result, target);
8514 return result;
8517 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8518 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8520 static rtx
8521 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8522 enum expand_modifier modifier)
8524 addr_space_t as = ADDR_SPACE_GENERIC;
8525 scalar_int_mode address_mode = Pmode;
8526 scalar_int_mode pointer_mode = ptr_mode;
8527 machine_mode rmode;
8528 rtx result;
8530 /* Target mode of VOIDmode says "whatever's natural". */
8531 if (tmode == VOIDmode)
8532 tmode = TYPE_MODE (TREE_TYPE (exp));
8534 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8536 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8537 address_mode = targetm.addr_space.address_mode (as);
8538 pointer_mode = targetm.addr_space.pointer_mode (as);
8541 /* We can get called with some Weird Things if the user does silliness
8542 like "(short) &a". In that case, convert_memory_address won't do
8543 the right thing, so ignore the given target mode. */
8544 scalar_int_mode new_tmode = (tmode == pointer_mode
8545 ? pointer_mode
8546 : address_mode);
8548 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8549 new_tmode, modifier, as);
8551 /* Despite expand_expr claims concerning ignoring TMODE when not
8552 strictly convenient, stuff breaks if we don't honor it. Note
8553 that combined with the above, we only do this for pointer modes. */
8554 rmode = GET_MODE (result);
8555 if (rmode == VOIDmode)
8556 rmode = new_tmode;
8557 if (rmode != new_tmode)
8558 result = convert_memory_address_addr_space (new_tmode, result, as);
8560 return result;
8563 /* Generate code for computing CONSTRUCTOR EXP.
8564 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8565 is TRUE, instead of creating a temporary variable in memory
8566 NULL is returned and the caller needs to handle it differently. */
8568 static rtx
8569 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8570 bool avoid_temp_mem)
8572 tree type = TREE_TYPE (exp);
8573 machine_mode mode = TYPE_MODE (type);
8575 /* Try to avoid creating a temporary at all. This is possible
8576 if all of the initializer is zero.
8577 FIXME: try to handle all [0..255] initializers we can handle
8578 with memset. */
8579 if (TREE_STATIC (exp)
8580 && !TREE_ADDRESSABLE (exp)
8581 && target != 0 && mode == BLKmode
8582 && all_zeros_p (exp))
8584 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8585 return target;
8588 /* All elts simple constants => refer to a constant in memory. But
8589 if this is a non-BLKmode mode, let it store a field at a time
8590 since that should make a CONST_INT, CONST_WIDE_INT or
8591 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8592 use, it is best to store directly into the target unless the type
8593 is large enough that memcpy will be used. If we are making an
8594 initializer and all operands are constant, put it in memory as
8595 well.
8597 FIXME: Avoid trying to fill vector constructors piece-meal.
8598 Output them with output_constant_def below unless we're sure
8599 they're zeros. This should go away when vector initializers
8600 are treated like VECTOR_CST instead of arrays. */
8601 if ((TREE_STATIC (exp)
8602 && ((mode == BLKmode
8603 && ! (target != 0 && safe_from_p (target, exp, 1)))
8604 || TREE_ADDRESSABLE (exp)
8605 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8606 && (! can_move_by_pieces
8607 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8608 TYPE_ALIGN (type)))
8609 && ! mostly_zeros_p (exp))))
8610 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8611 && TREE_CONSTANT (exp)))
8613 rtx constructor;
8615 if (avoid_temp_mem)
8616 return NULL_RTX;
8618 constructor = expand_expr_constant (exp, 1, modifier);
8620 if (modifier != EXPAND_CONST_ADDRESS
8621 && modifier != EXPAND_INITIALIZER
8622 && modifier != EXPAND_SUM)
8623 constructor = validize_mem (constructor);
8625 return constructor;
8628 /* If the CTOR is available in static storage and not mostly
8629 zeros and we can move it by pieces prefer to do so since
8630 that's usually more efficient than performing a series of
8631 stores from immediates. */
8632 if (avoid_temp_mem
8633 && TREE_STATIC (exp)
8634 && TREE_CONSTANT (exp)
8635 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8636 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8637 TYPE_ALIGN (type))
8638 && ! mostly_zeros_p (exp))
8639 return NULL_RTX;
8641 /* Handle calls that pass values in multiple non-contiguous
8642 locations. The Irix 6 ABI has examples of this. */
8643 if (target == 0 || ! safe_from_p (target, exp, 1)
8644 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8645 /* Also make a temporary if the store is to volatile memory, to
8646 avoid individual accesses to aggregate members. */
8647 || (GET_CODE (target) == MEM
8648 && MEM_VOLATILE_P (target)
8649 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8651 if (avoid_temp_mem)
8652 return NULL_RTX;
8654 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8657 store_constructor (exp, target, 0, int_expr_size (exp), false);
8658 return target;
8662 /* expand_expr: generate code for computing expression EXP.
8663 An rtx for the computed value is returned. The value is never null.
8664 In the case of a void EXP, const0_rtx is returned.
8666 The value may be stored in TARGET if TARGET is nonzero.
8667 TARGET is just a suggestion; callers must assume that
8668 the rtx returned may not be the same as TARGET.
8670 If TARGET is CONST0_RTX, it means that the value will be ignored.
8672 If TMODE is not VOIDmode, it suggests generating the
8673 result in mode TMODE. But this is done only when convenient.
8674 Otherwise, TMODE is ignored and the value generated in its natural mode.
8675 TMODE is just a suggestion; callers must assume that
8676 the rtx returned may not have mode TMODE.
8678 Note that TARGET may have neither TMODE nor MODE. In that case, it
8679 probably will not be used.
8681 If MODIFIER is EXPAND_SUM then when EXP is an addition
8682 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8683 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8684 products as above, or REG or MEM, or constant.
8685 Ordinarily in such cases we would output mul or add instructions
8686 and then return a pseudo reg containing the sum.
8688 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8689 it also marks a label as absolutely required (it can't be dead).
8690 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8691 This is used for outputting expressions used in initializers.
8693 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8694 with a constant address even if that address is not normally legitimate.
8695 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8697 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8698 a call parameter. Such targets require special care as we haven't yet
8699 marked TARGET so that it's safe from being trashed by libcalls. We
8700 don't want to use TARGET for anything but the final result;
8701 Intermediate values must go elsewhere. Additionally, calls to
8702 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8704 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8705 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8706 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8707 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8708 recursively.
8709 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8710 then *ALT_RTL is set to TARGET (before legitimziation).
8712 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8713 In this case, we don't adjust a returned MEM rtx that wouldn't be
8714 sufficiently aligned for its mode; instead, it's up to the caller
8715 to deal with it afterwards. This is used to make sure that unaligned
8716 base objects for which out-of-bounds accesses are supported, for
8717 example record types with trailing arrays, aren't realigned behind
8718 the back of the caller.
8719 The normal operating mode is to pass FALSE for this parameter. */
8722 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8723 enum expand_modifier modifier, rtx *alt_rtl,
8724 bool inner_reference_p)
8726 rtx ret;
8728 /* Handle ERROR_MARK before anybody tries to access its type. */
8729 if (TREE_CODE (exp) == ERROR_MARK
8730 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8732 ret = CONST0_RTX (tmode);
8733 return ret ? ret : const0_rtx;
8736 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8737 inner_reference_p);
8738 return ret;
8741 /* Try to expand the conditional expression which is represented by
8742 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8743 return the rtl reg which represents the result. Otherwise return
8744 NULL_RTX. */
8746 static rtx
8747 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8748 tree treeop1 ATTRIBUTE_UNUSED,
8749 tree treeop2 ATTRIBUTE_UNUSED)
8751 rtx insn;
8752 rtx op00, op01, op1, op2;
8753 enum rtx_code comparison_code;
8754 machine_mode comparison_mode;
8755 gimple *srcstmt;
8756 rtx temp;
8757 tree type = TREE_TYPE (treeop1);
8758 int unsignedp = TYPE_UNSIGNED (type);
8759 machine_mode mode = TYPE_MODE (type);
8760 machine_mode orig_mode = mode;
8761 static bool expanding_cond_expr_using_cmove = false;
8763 /* Conditional move expansion can end up TERing two operands which,
8764 when recursively hitting conditional expressions can result in
8765 exponential behavior if the cmove expansion ultimatively fails.
8766 It's hardly profitable to TER a cmove into a cmove so avoid doing
8767 that by failing early if we end up recursing. */
8768 if (expanding_cond_expr_using_cmove)
8769 return NULL_RTX;
8771 /* If we cannot do a conditional move on the mode, try doing it
8772 with the promoted mode. */
8773 if (!can_conditionally_move_p (mode))
8775 mode = promote_mode (type, mode, &unsignedp);
8776 if (!can_conditionally_move_p (mode))
8777 return NULL_RTX;
8778 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8780 else
8781 temp = assign_temp (type, 0, 1);
8783 expanding_cond_expr_using_cmove = true;
8784 start_sequence ();
8785 expand_operands (treeop1, treeop2,
8786 temp, &op1, &op2, EXPAND_NORMAL);
8788 if (TREE_CODE (treeop0) == SSA_NAME
8789 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8791 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8792 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8793 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8794 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8795 comparison_mode = TYPE_MODE (type);
8796 unsignedp = TYPE_UNSIGNED (type);
8797 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8799 else if (COMPARISON_CLASS_P (treeop0))
8801 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8802 enum tree_code cmpcode = TREE_CODE (treeop0);
8803 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8804 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8805 unsignedp = TYPE_UNSIGNED (type);
8806 comparison_mode = TYPE_MODE (type);
8807 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8809 else
8811 op00 = expand_normal (treeop0);
8812 op01 = const0_rtx;
8813 comparison_code = NE;
8814 comparison_mode = GET_MODE (op00);
8815 if (comparison_mode == VOIDmode)
8816 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8818 expanding_cond_expr_using_cmove = false;
8820 if (GET_MODE (op1) != mode)
8821 op1 = gen_lowpart (mode, op1);
8823 if (GET_MODE (op2) != mode)
8824 op2 = gen_lowpart (mode, op2);
8826 /* Try to emit the conditional move. */
8827 insn = emit_conditional_move (temp, comparison_code,
8828 op00, op01, comparison_mode,
8829 op1, op2, mode,
8830 unsignedp);
8832 /* If we could do the conditional move, emit the sequence,
8833 and return. */
8834 if (insn)
8836 rtx_insn *seq = get_insns ();
8837 end_sequence ();
8838 emit_insn (seq);
8839 return convert_modes (orig_mode, mode, temp, 0);
8842 /* Otherwise discard the sequence and fall back to code with
8843 branches. */
8844 end_sequence ();
8845 return NULL_RTX;
8848 /* A helper function for expand_expr_real_2 to be used with a
8849 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8850 is nonzero, with alignment ALIGN in bits.
8851 Store the value at TARGET if possible (if TARGET is nonzero).
8852 Regardless of TARGET, we return the rtx for where the value is placed.
8853 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8854 then *ALT_RTL is set to TARGET (before legitimziation). */
8856 static rtx
8857 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8858 unsigned int align, rtx target, rtx *alt_rtl)
8860 enum insn_code icode;
8862 if ((icode = optab_handler (movmisalign_optab, mode))
8863 != CODE_FOR_nothing)
8865 class expand_operand ops[2];
8867 /* We've already validated the memory, and we're creating a
8868 new pseudo destination. The predicates really can't fail,
8869 nor can the generator. */
8870 create_output_operand (&ops[0], NULL_RTX, mode);
8871 create_fixed_operand (&ops[1], temp);
8872 expand_insn (icode, 2, ops);
8873 temp = ops[0].value;
8875 else if (targetm.slow_unaligned_access (mode, align))
8876 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8877 0, unsignedp, target,
8878 mode, mode, false, alt_rtl);
8879 return temp;
8882 /* Helper function of expand_expr_2, expand a division or modulo.
8883 op0 and op1 should be already expanded treeop0 and treeop1, using
8884 expand_operands. */
8886 static rtx
8887 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
8888 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
8890 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8891 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
8892 if (SCALAR_INT_MODE_P (mode)
8893 && optimize >= 2
8894 && get_range_pos_neg (treeop0) == 1
8895 && get_range_pos_neg (treeop1) == 1)
8897 /* If both arguments are known to be positive when interpreted
8898 as signed, we can expand it as both signed and unsigned
8899 division or modulo. Choose the cheaper sequence in that case. */
8900 bool speed_p = optimize_insn_for_speed_p ();
8901 do_pending_stack_adjust ();
8902 start_sequence ();
8903 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8904 rtx_insn *uns_insns = get_insns ();
8905 end_sequence ();
8906 start_sequence ();
8907 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8908 rtx_insn *sgn_insns = get_insns ();
8909 end_sequence ();
8910 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8911 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8913 /* If costs are the same then use as tie breaker the other other
8914 factor. */
8915 if (uns_cost == sgn_cost)
8917 uns_cost = seq_cost (uns_insns, !speed_p);
8918 sgn_cost = seq_cost (sgn_insns, !speed_p);
8921 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8923 emit_insn (uns_insns);
8924 return uns_ret;
8926 emit_insn (sgn_insns);
8927 return sgn_ret;
8929 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8933 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8934 enum expand_modifier modifier)
8936 rtx op0, op1, op2, temp;
8937 rtx_code_label *lab;
8938 tree type;
8939 int unsignedp;
8940 machine_mode mode;
8941 scalar_int_mode int_mode;
8942 enum tree_code code = ops->code;
8943 optab this_optab;
8944 rtx subtarget, original_target;
8945 int ignore;
8946 bool reduce_bit_field;
8947 location_t loc = ops->location;
8948 tree treeop0, treeop1, treeop2;
8949 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8950 ? reduce_to_bit_field_precision ((expr), \
8951 target, \
8952 type) \
8953 : (expr))
8955 type = ops->type;
8956 mode = TYPE_MODE (type);
8957 unsignedp = TYPE_UNSIGNED (type);
8959 treeop0 = ops->op0;
8960 treeop1 = ops->op1;
8961 treeop2 = ops->op2;
8963 /* We should be called only on simple (binary or unary) expressions,
8964 exactly those that are valid in gimple expressions that aren't
8965 GIMPLE_SINGLE_RHS (or invalid). */
8966 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8967 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8968 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8970 ignore = (target == const0_rtx
8971 || ((CONVERT_EXPR_CODE_P (code)
8972 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8973 && TREE_CODE (type) == VOID_TYPE));
8975 /* We should be called only if we need the result. */
8976 gcc_assert (!ignore);
8978 /* An operation in what may be a bit-field type needs the
8979 result to be reduced to the precision of the bit-field type,
8980 which is narrower than that of the type's mode. */
8981 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8982 && !type_has_mode_precision_p (type));
8984 if (reduce_bit_field
8985 && (modifier == EXPAND_STACK_PARM
8986 || (target && GET_MODE (target) != mode)))
8987 target = 0;
8989 /* Use subtarget as the target for operand 0 of a binary operation. */
8990 subtarget = get_subtarget (target);
8991 original_target = target;
8993 switch (code)
8995 case NON_LVALUE_EXPR:
8996 case PAREN_EXPR:
8997 CASE_CONVERT:
8998 if (treeop0 == error_mark_node)
8999 return const0_rtx;
9001 if (TREE_CODE (type) == UNION_TYPE)
9003 tree valtype = TREE_TYPE (treeop0);
9005 /* If both input and output are BLKmode, this conversion isn't doing
9006 anything except possibly changing memory attribute. */
9007 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9009 rtx result = expand_expr (treeop0, target, tmode,
9010 modifier);
9012 result = copy_rtx (result);
9013 set_mem_attributes (result, type, 0);
9014 return result;
9017 if (target == 0)
9019 if (TYPE_MODE (type) != BLKmode)
9020 target = gen_reg_rtx (TYPE_MODE (type));
9021 else
9022 target = assign_temp (type, 1, 1);
9025 if (MEM_P (target))
9026 /* Store data into beginning of memory target. */
9027 store_expr (treeop0,
9028 adjust_address (target, TYPE_MODE (valtype), 0),
9029 modifier == EXPAND_STACK_PARM,
9030 false, TYPE_REVERSE_STORAGE_ORDER (type));
9032 else
9034 gcc_assert (REG_P (target)
9035 && !TYPE_REVERSE_STORAGE_ORDER (type));
9037 /* Store this field into a union of the proper type. */
9038 poly_uint64 op0_size
9039 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9040 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9041 store_field (target,
9042 /* The conversion must be constructed so that
9043 we know at compile time how many bits
9044 to preserve. */
9045 ordered_min (op0_size, union_size),
9046 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9047 false, false);
9050 /* Return the entire union. */
9051 return target;
9054 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9056 op0 = expand_expr (treeop0, target, VOIDmode,
9057 modifier);
9059 /* If the signedness of the conversion differs and OP0 is
9060 a promoted SUBREG, clear that indication since we now
9061 have to do the proper extension. */
9062 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9063 && GET_CODE (op0) == SUBREG)
9064 SUBREG_PROMOTED_VAR_P (op0) = 0;
9066 return REDUCE_BIT_FIELD (op0);
9069 op0 = expand_expr (treeop0, NULL_RTX, mode,
9070 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9071 if (GET_MODE (op0) == mode)
9074 /* If OP0 is a constant, just convert it into the proper mode. */
9075 else if (CONSTANT_P (op0))
9077 tree inner_type = TREE_TYPE (treeop0);
9078 machine_mode inner_mode = GET_MODE (op0);
9080 if (inner_mode == VOIDmode)
9081 inner_mode = TYPE_MODE (inner_type);
9083 if (modifier == EXPAND_INITIALIZER)
9084 op0 = lowpart_subreg (mode, op0, inner_mode);
9085 else
9086 op0= convert_modes (mode, inner_mode, op0,
9087 TYPE_UNSIGNED (inner_type));
9090 else if (modifier == EXPAND_INITIALIZER)
9091 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9092 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9094 else if (target == 0)
9095 op0 = convert_to_mode (mode, op0,
9096 TYPE_UNSIGNED (TREE_TYPE
9097 (treeop0)));
9098 else
9100 convert_move (target, op0,
9101 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9102 op0 = target;
9105 return REDUCE_BIT_FIELD (op0);
9107 case ADDR_SPACE_CONVERT_EXPR:
9109 tree treeop0_type = TREE_TYPE (treeop0);
9111 gcc_assert (POINTER_TYPE_P (type));
9112 gcc_assert (POINTER_TYPE_P (treeop0_type));
9114 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9115 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9117 /* Conversions between pointers to the same address space should
9118 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9119 gcc_assert (as_to != as_from);
9121 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9123 /* Ask target code to handle conversion between pointers
9124 to overlapping address spaces. */
9125 if (targetm.addr_space.subset_p (as_to, as_from)
9126 || targetm.addr_space.subset_p (as_from, as_to))
9128 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9130 else
9132 /* For disjoint address spaces, converting anything but a null
9133 pointer invokes undefined behavior. We truncate or extend the
9134 value as if we'd converted via integers, which handles 0 as
9135 required, and all others as the programmer likely expects. */
9136 #ifndef POINTERS_EXTEND_UNSIGNED
9137 const int POINTERS_EXTEND_UNSIGNED = 1;
9138 #endif
9139 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9140 op0, POINTERS_EXTEND_UNSIGNED);
9142 gcc_assert (op0);
9143 return op0;
9146 case POINTER_PLUS_EXPR:
9147 /* Even though the sizetype mode and the pointer's mode can be different
9148 expand is able to handle this correctly and get the correct result out
9149 of the PLUS_EXPR code. */
9150 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9151 if sizetype precision is smaller than pointer precision. */
9152 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9153 treeop1 = fold_convert_loc (loc, type,
9154 fold_convert_loc (loc, ssizetype,
9155 treeop1));
9156 /* If sizetype precision is larger than pointer precision, truncate the
9157 offset to have matching modes. */
9158 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9159 treeop1 = fold_convert_loc (loc, type, treeop1);
9160 /* FALLTHRU */
9162 case PLUS_EXPR:
9163 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9164 something else, make sure we add the register to the constant and
9165 then to the other thing. This case can occur during strength
9166 reduction and doing it this way will produce better code if the
9167 frame pointer or argument pointer is eliminated.
9169 fold-const.c will ensure that the constant is always in the inner
9170 PLUS_EXPR, so the only case we need to do anything about is if
9171 sp, ap, or fp is our second argument, in which case we must swap
9172 the innermost first argument and our second argument. */
9174 if (TREE_CODE (treeop0) == PLUS_EXPR
9175 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9176 && VAR_P (treeop1)
9177 && (DECL_RTL (treeop1) == frame_pointer_rtx
9178 || DECL_RTL (treeop1) == stack_pointer_rtx
9179 || DECL_RTL (treeop1) == arg_pointer_rtx))
9181 gcc_unreachable ();
9184 /* If the result is to be ptr_mode and we are adding an integer to
9185 something, we might be forming a constant. So try to use
9186 plus_constant. If it produces a sum and we can't accept it,
9187 use force_operand. This allows P = &ARR[const] to generate
9188 efficient code on machines where a SYMBOL_REF is not a valid
9189 address.
9191 If this is an EXPAND_SUM call, always return the sum. */
9192 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9193 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9195 if (modifier == EXPAND_STACK_PARM)
9196 target = 0;
9197 if (TREE_CODE (treeop0) == INTEGER_CST
9198 && HWI_COMPUTABLE_MODE_P (mode)
9199 && TREE_CONSTANT (treeop1))
9201 rtx constant_part;
9202 HOST_WIDE_INT wc;
9203 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9205 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9206 EXPAND_SUM);
9207 /* Use wi::shwi to ensure that the constant is
9208 truncated according to the mode of OP1, then sign extended
9209 to a HOST_WIDE_INT. Using the constant directly can result
9210 in non-canonical RTL in a 64x32 cross compile. */
9211 wc = TREE_INT_CST_LOW (treeop0);
9212 constant_part =
9213 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9214 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9215 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9216 op1 = force_operand (op1, target);
9217 return REDUCE_BIT_FIELD (op1);
9220 else if (TREE_CODE (treeop1) == INTEGER_CST
9221 && HWI_COMPUTABLE_MODE_P (mode)
9222 && TREE_CONSTANT (treeop0))
9224 rtx constant_part;
9225 HOST_WIDE_INT wc;
9226 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9228 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9229 (modifier == EXPAND_INITIALIZER
9230 ? EXPAND_INITIALIZER : EXPAND_SUM));
9231 if (! CONSTANT_P (op0))
9233 op1 = expand_expr (treeop1, NULL_RTX,
9234 VOIDmode, modifier);
9235 /* Return a PLUS if modifier says it's OK. */
9236 if (modifier == EXPAND_SUM
9237 || modifier == EXPAND_INITIALIZER)
9238 return simplify_gen_binary (PLUS, mode, op0, op1);
9239 goto binop2;
9241 /* Use wi::shwi to ensure that the constant is
9242 truncated according to the mode of OP1, then sign extended
9243 to a HOST_WIDE_INT. Using the constant directly can result
9244 in non-canonical RTL in a 64x32 cross compile. */
9245 wc = TREE_INT_CST_LOW (treeop1);
9246 constant_part
9247 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9248 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9249 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9250 op0 = force_operand (op0, target);
9251 return REDUCE_BIT_FIELD (op0);
9255 /* Use TER to expand pointer addition of a negated value
9256 as pointer subtraction. */
9257 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9258 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9259 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9260 && TREE_CODE (treeop1) == SSA_NAME
9261 && TYPE_MODE (TREE_TYPE (treeop0))
9262 == TYPE_MODE (TREE_TYPE (treeop1)))
9264 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9265 if (def)
9267 treeop1 = gimple_assign_rhs1 (def);
9268 code = MINUS_EXPR;
9269 goto do_minus;
9273 /* No sense saving up arithmetic to be done
9274 if it's all in the wrong mode to form part of an address.
9275 And force_operand won't know whether to sign-extend or
9276 zero-extend. */
9277 if (modifier != EXPAND_INITIALIZER
9278 && (modifier != EXPAND_SUM || mode != ptr_mode))
9280 expand_operands (treeop0, treeop1,
9281 subtarget, &op0, &op1, modifier);
9282 if (op0 == const0_rtx)
9283 return op1;
9284 if (op1 == const0_rtx)
9285 return op0;
9286 goto binop2;
9289 expand_operands (treeop0, treeop1,
9290 subtarget, &op0, &op1, modifier);
9291 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9293 case MINUS_EXPR:
9294 case POINTER_DIFF_EXPR:
9295 do_minus:
9296 /* For initializers, we are allowed to return a MINUS of two
9297 symbolic constants. Here we handle all cases when both operands
9298 are constant. */
9299 /* Handle difference of two symbolic constants,
9300 for the sake of an initializer. */
9301 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9302 && really_constant_p (treeop0)
9303 && really_constant_p (treeop1))
9305 expand_operands (treeop0, treeop1,
9306 NULL_RTX, &op0, &op1, modifier);
9307 return simplify_gen_binary (MINUS, mode, op0, op1);
9310 /* No sense saving up arithmetic to be done
9311 if it's all in the wrong mode to form part of an address.
9312 And force_operand won't know whether to sign-extend or
9313 zero-extend. */
9314 if (modifier != EXPAND_INITIALIZER
9315 && (modifier != EXPAND_SUM || mode != ptr_mode))
9316 goto binop;
9318 expand_operands (treeop0, treeop1,
9319 subtarget, &op0, &op1, modifier);
9321 /* Convert A - const to A + (-const). */
9322 if (CONST_INT_P (op1))
9324 op1 = negate_rtx (mode, op1);
9325 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9328 goto binop2;
9330 case WIDEN_MULT_PLUS_EXPR:
9331 case WIDEN_MULT_MINUS_EXPR:
9332 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9333 op2 = expand_normal (treeop2);
9334 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9335 target, unsignedp);
9336 return target;
9338 case WIDEN_PLUS_EXPR:
9339 case WIDEN_MINUS_EXPR:
9340 case WIDEN_MULT_EXPR:
9341 /* If first operand is constant, swap them.
9342 Thus the following special case checks need only
9343 check the second operand. */
9344 if (TREE_CODE (treeop0) == INTEGER_CST)
9345 std::swap (treeop0, treeop1);
9347 /* First, check if we have a multiplication of one signed and one
9348 unsigned operand. */
9349 if (TREE_CODE (treeop1) != INTEGER_CST
9350 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9351 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9353 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9354 this_optab = usmul_widen_optab;
9355 if (find_widening_optab_handler (this_optab, mode, innermode)
9356 != CODE_FOR_nothing)
9358 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9359 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9360 EXPAND_NORMAL);
9361 else
9362 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9363 EXPAND_NORMAL);
9364 /* op0 and op1 might still be constant, despite the above
9365 != INTEGER_CST check. Handle it. */
9366 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9368 op0 = convert_modes (mode, innermode, op0, true);
9369 op1 = convert_modes (mode, innermode, op1, false);
9370 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9371 target, unsignedp));
9373 goto binop3;
9376 /* Check for a multiplication with matching signedness. */
9377 else if ((TREE_CODE (treeop1) == INTEGER_CST
9378 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9379 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9380 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9382 tree op0type = TREE_TYPE (treeop0);
9383 machine_mode innermode = TYPE_MODE (op0type);
9384 bool zextend_p = TYPE_UNSIGNED (op0type);
9385 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9386 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9388 if (TREE_CODE (treeop0) != INTEGER_CST)
9390 if (find_widening_optab_handler (this_optab, mode, innermode)
9391 != CODE_FOR_nothing)
9393 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9394 EXPAND_NORMAL);
9395 /* op0 and op1 might still be constant, despite the above
9396 != INTEGER_CST check. Handle it. */
9397 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9399 widen_mult_const:
9400 op0 = convert_modes (mode, innermode, op0, zextend_p);
9402 = convert_modes (mode, innermode, op1,
9403 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9404 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9405 target,
9406 unsignedp));
9408 temp = expand_widening_mult (mode, op0, op1, target,
9409 unsignedp, this_optab);
9410 return REDUCE_BIT_FIELD (temp);
9412 if (find_widening_optab_handler (other_optab, mode, innermode)
9413 != CODE_FOR_nothing
9414 && innermode == word_mode)
9416 rtx htem, hipart;
9417 op0 = expand_normal (treeop0);
9418 op1 = expand_normal (treeop1);
9419 /* op0 and op1 might be constants, despite the above
9420 != INTEGER_CST check. Handle it. */
9421 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9422 goto widen_mult_const;
9423 temp = expand_binop (mode, other_optab, op0, op1, target,
9424 unsignedp, OPTAB_LIB_WIDEN);
9425 hipart = gen_highpart (word_mode, temp);
9426 htem = expand_mult_highpart_adjust (word_mode, hipart,
9427 op0, op1, hipart,
9428 zextend_p);
9429 if (htem != hipart)
9430 emit_move_insn (hipart, htem);
9431 return REDUCE_BIT_FIELD (temp);
9435 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9436 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9437 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9438 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9440 case MULT_EXPR:
9441 /* If this is a fixed-point operation, then we cannot use the code
9442 below because "expand_mult" doesn't support sat/no-sat fixed-point
9443 multiplications. */
9444 if (ALL_FIXED_POINT_MODE_P (mode))
9445 goto binop;
9447 /* If first operand is constant, swap them.
9448 Thus the following special case checks need only
9449 check the second operand. */
9450 if (TREE_CODE (treeop0) == INTEGER_CST)
9451 std::swap (treeop0, treeop1);
9453 /* Attempt to return something suitable for generating an
9454 indexed address, for machines that support that. */
9456 if (modifier == EXPAND_SUM && mode == ptr_mode
9457 && tree_fits_shwi_p (treeop1))
9459 tree exp1 = treeop1;
9461 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9462 EXPAND_SUM);
9464 if (!REG_P (op0))
9465 op0 = force_operand (op0, NULL_RTX);
9466 if (!REG_P (op0))
9467 op0 = copy_to_mode_reg (mode, op0);
9469 op1 = gen_int_mode (tree_to_shwi (exp1),
9470 TYPE_MODE (TREE_TYPE (exp1)));
9471 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9474 if (modifier == EXPAND_STACK_PARM)
9475 target = 0;
9477 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9479 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9480 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9481 if (def_stmt0
9482 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9483 def_stmt0 = NULL;
9484 if (def_stmt1
9485 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9486 def_stmt1 = NULL;
9488 if (def_stmt0 || def_stmt1)
9490 /* X / Y * Y can be expanded as X - X % Y too.
9491 Choose the cheaper sequence of those two. */
9492 if (def_stmt0)
9493 treeop0 = gimple_assign_rhs1 (def_stmt0);
9494 else
9496 treeop1 = treeop0;
9497 treeop0 = gimple_assign_rhs1 (def_stmt1);
9499 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9500 EXPAND_NORMAL);
9501 bool speed_p = optimize_insn_for_speed_p ();
9502 do_pending_stack_adjust ();
9503 start_sequence ();
9504 rtx divmul_ret
9505 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9506 op0, op1, NULL_RTX, unsignedp);
9507 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9508 unsignedp);
9509 rtx_insn *divmul_insns = get_insns ();
9510 end_sequence ();
9511 start_sequence ();
9512 rtx modsub_ret
9513 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9514 op0, op1, NULL_RTX, unsignedp);
9515 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9516 optab_default);
9517 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9518 target, unsignedp, OPTAB_LIB_WIDEN);
9519 rtx_insn *modsub_insns = get_insns ();
9520 end_sequence ();
9521 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9522 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9523 /* If costs are the same then use as tie breaker the other other
9524 factor. */
9525 if (divmul_cost == modsub_cost)
9527 divmul_cost = seq_cost (divmul_insns, !speed_p);
9528 modsub_cost = seq_cost (modsub_insns, !speed_p);
9531 if (divmul_cost <= modsub_cost)
9533 emit_insn (divmul_insns);
9534 return REDUCE_BIT_FIELD (divmul_ret);
9536 emit_insn (modsub_insns);
9537 return REDUCE_BIT_FIELD (modsub_ret);
9541 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9542 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9544 case TRUNC_MOD_EXPR:
9545 case FLOOR_MOD_EXPR:
9546 case CEIL_MOD_EXPR:
9547 case ROUND_MOD_EXPR:
9549 case TRUNC_DIV_EXPR:
9550 case FLOOR_DIV_EXPR:
9551 case CEIL_DIV_EXPR:
9552 case ROUND_DIV_EXPR:
9553 case EXACT_DIV_EXPR:
9554 /* If this is a fixed-point operation, then we cannot use the code
9555 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9556 divisions. */
9557 if (ALL_FIXED_POINT_MODE_P (mode))
9558 goto binop;
9560 if (modifier == EXPAND_STACK_PARM)
9561 target = 0;
9562 /* Possible optimization: compute the dividend with EXPAND_SUM
9563 then if the divisor is constant can optimize the case
9564 where some terms of the dividend have coeffs divisible by it. */
9565 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9566 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9567 target, unsignedp);
9569 case RDIV_EXPR:
9570 goto binop;
9572 case MULT_HIGHPART_EXPR:
9573 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9574 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9575 gcc_assert (temp);
9576 return temp;
9578 case FIXED_CONVERT_EXPR:
9579 op0 = expand_normal (treeop0);
9580 if (target == 0 || modifier == EXPAND_STACK_PARM)
9581 target = gen_reg_rtx (mode);
9583 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9584 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9585 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9586 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9587 else
9588 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9589 return target;
9591 case FIX_TRUNC_EXPR:
9592 op0 = expand_normal (treeop0);
9593 if (target == 0 || modifier == EXPAND_STACK_PARM)
9594 target = gen_reg_rtx (mode);
9595 expand_fix (target, op0, unsignedp);
9596 return target;
9598 case FLOAT_EXPR:
9599 op0 = expand_normal (treeop0);
9600 if (target == 0 || modifier == EXPAND_STACK_PARM)
9601 target = gen_reg_rtx (mode);
9602 /* expand_float can't figure out what to do if FROM has VOIDmode.
9603 So give it the correct mode. With -O, cse will optimize this. */
9604 if (GET_MODE (op0) == VOIDmode)
9605 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9606 op0);
9607 expand_float (target, op0,
9608 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9609 return target;
9611 case NEGATE_EXPR:
9612 op0 = expand_expr (treeop0, subtarget,
9613 VOIDmode, EXPAND_NORMAL);
9614 if (modifier == EXPAND_STACK_PARM)
9615 target = 0;
9616 temp = expand_unop (mode,
9617 optab_for_tree_code (NEGATE_EXPR, type,
9618 optab_default),
9619 op0, target, 0);
9620 gcc_assert (temp);
9621 return REDUCE_BIT_FIELD (temp);
9623 case ABS_EXPR:
9624 case ABSU_EXPR:
9625 op0 = expand_expr (treeop0, subtarget,
9626 VOIDmode, EXPAND_NORMAL);
9627 if (modifier == EXPAND_STACK_PARM)
9628 target = 0;
9630 /* ABS_EXPR is not valid for complex arguments. */
9631 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9632 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9634 /* Unsigned abs is simply the operand. Testing here means we don't
9635 risk generating incorrect code below. */
9636 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9637 return op0;
9639 return expand_abs (mode, op0, target, unsignedp,
9640 safe_from_p (target, treeop0, 1));
9642 case MAX_EXPR:
9643 case MIN_EXPR:
9644 target = original_target;
9645 if (target == 0
9646 || modifier == EXPAND_STACK_PARM
9647 || (MEM_P (target) && MEM_VOLATILE_P (target))
9648 || GET_MODE (target) != mode
9649 || (REG_P (target)
9650 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9651 target = gen_reg_rtx (mode);
9652 expand_operands (treeop0, treeop1,
9653 target, &op0, &op1, EXPAND_NORMAL);
9655 /* First try to do it with a special MIN or MAX instruction.
9656 If that does not win, use a conditional jump to select the proper
9657 value. */
9658 this_optab = optab_for_tree_code (code, type, optab_default);
9659 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9660 OPTAB_WIDEN);
9661 if (temp != 0)
9662 return temp;
9664 if (VECTOR_TYPE_P (type))
9665 gcc_unreachable ();
9667 /* At this point, a MEM target is no longer useful; we will get better
9668 code without it. */
9670 if (! REG_P (target))
9671 target = gen_reg_rtx (mode);
9673 /* If op1 was placed in target, swap op0 and op1. */
9674 if (target != op0 && target == op1)
9675 std::swap (op0, op1);
9677 /* We generate better code and avoid problems with op1 mentioning
9678 target by forcing op1 into a pseudo if it isn't a constant. */
9679 if (! CONSTANT_P (op1))
9680 op1 = force_reg (mode, op1);
9683 enum rtx_code comparison_code;
9684 rtx cmpop1 = op1;
9686 if (code == MAX_EXPR)
9687 comparison_code = unsignedp ? GEU : GE;
9688 else
9689 comparison_code = unsignedp ? LEU : LE;
9691 /* Canonicalize to comparisons against 0. */
9692 if (op1 == const1_rtx)
9694 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9695 or (a != 0 ? a : 1) for unsigned.
9696 For MIN we are safe converting (a <= 1 ? a : 1)
9697 into (a <= 0 ? a : 1) */
9698 cmpop1 = const0_rtx;
9699 if (code == MAX_EXPR)
9700 comparison_code = unsignedp ? NE : GT;
9702 if (op1 == constm1_rtx && !unsignedp)
9704 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9705 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9706 cmpop1 = const0_rtx;
9707 if (code == MIN_EXPR)
9708 comparison_code = LT;
9711 /* Use a conditional move if possible. */
9712 if (can_conditionally_move_p (mode))
9714 rtx insn;
9716 start_sequence ();
9718 /* Try to emit the conditional move. */
9719 insn = emit_conditional_move (target, comparison_code,
9720 op0, cmpop1, mode,
9721 op0, op1, mode,
9722 unsignedp);
9724 /* If we could do the conditional move, emit the sequence,
9725 and return. */
9726 if (insn)
9728 rtx_insn *seq = get_insns ();
9729 end_sequence ();
9730 emit_insn (seq);
9731 return target;
9734 /* Otherwise discard the sequence and fall back to code with
9735 branches. */
9736 end_sequence ();
9739 if (target != op0)
9740 emit_move_insn (target, op0);
9742 lab = gen_label_rtx ();
9743 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9744 unsignedp, mode, NULL_RTX, NULL, lab,
9745 profile_probability::uninitialized ());
9747 emit_move_insn (target, op1);
9748 emit_label (lab);
9749 return target;
9751 case BIT_NOT_EXPR:
9752 op0 = expand_expr (treeop0, subtarget,
9753 VOIDmode, EXPAND_NORMAL);
9754 if (modifier == EXPAND_STACK_PARM)
9755 target = 0;
9756 /* In case we have to reduce the result to bitfield precision
9757 for unsigned bitfield expand this as XOR with a proper constant
9758 instead. */
9759 if (reduce_bit_field && TYPE_UNSIGNED (type))
9761 int_mode = SCALAR_INT_TYPE_MODE (type);
9762 wide_int mask = wi::mask (TYPE_PRECISION (type),
9763 false, GET_MODE_PRECISION (int_mode));
9765 temp = expand_binop (int_mode, xor_optab, op0,
9766 immed_wide_int_const (mask, int_mode),
9767 target, 1, OPTAB_LIB_WIDEN);
9769 else
9770 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9771 gcc_assert (temp);
9772 return temp;
9774 /* ??? Can optimize bitwise operations with one arg constant.
9775 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9776 and (a bitwise1 b) bitwise2 b (etc)
9777 but that is probably not worth while. */
9779 case BIT_AND_EXPR:
9780 case BIT_IOR_EXPR:
9781 case BIT_XOR_EXPR:
9782 goto binop;
9784 case LROTATE_EXPR:
9785 case RROTATE_EXPR:
9786 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9787 || type_has_mode_precision_p (type));
9788 /* fall through */
9790 case LSHIFT_EXPR:
9791 case RSHIFT_EXPR:
9793 /* If this is a fixed-point operation, then we cannot use the code
9794 below because "expand_shift" doesn't support sat/no-sat fixed-point
9795 shifts. */
9796 if (ALL_FIXED_POINT_MODE_P (mode))
9797 goto binop;
9799 if (! safe_from_p (subtarget, treeop1, 1))
9800 subtarget = 0;
9801 if (modifier == EXPAND_STACK_PARM)
9802 target = 0;
9803 op0 = expand_expr (treeop0, subtarget,
9804 VOIDmode, EXPAND_NORMAL);
9806 /* Left shift optimization when shifting across word_size boundary.
9808 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9809 there isn't native instruction to support this wide mode
9810 left shift. Given below scenario:
9812 Type A = (Type) B << C
9814 |< T >|
9815 | dest_high | dest_low |
9817 | word_size |
9819 If the shift amount C caused we shift B to across the word
9820 size boundary, i.e part of B shifted into high half of
9821 destination register, and part of B remains in the low
9822 half, then GCC will use the following left shift expand
9823 logic:
9825 1. Initialize dest_low to B.
9826 2. Initialize every bit of dest_high to the sign bit of B.
9827 3. Logic left shift dest_low by C bit to finalize dest_low.
9828 The value of dest_low before this shift is kept in a temp D.
9829 4. Logic left shift dest_high by C.
9830 5. Logic right shift D by (word_size - C).
9831 6. Or the result of 4 and 5 to finalize dest_high.
9833 While, by checking gimple statements, if operand B is
9834 coming from signed extension, then we can simplify above
9835 expand logic into:
9837 1. dest_high = src_low >> (word_size - C).
9838 2. dest_low = src_low << C.
9840 We can use one arithmetic right shift to finish all the
9841 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9842 needed from 6 into 2.
9844 The case is similar for zero extension, except that we
9845 initialize dest_high to zero rather than copies of the sign
9846 bit from B. Furthermore, we need to use a logical right shift
9847 in this case.
9849 The choice of sign-extension versus zero-extension is
9850 determined entirely by whether or not B is signed and is
9851 independent of the current setting of unsignedp. */
9853 temp = NULL_RTX;
9854 if (code == LSHIFT_EXPR
9855 && target
9856 && REG_P (target)
9857 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9858 && mode == int_mode
9859 && TREE_CONSTANT (treeop1)
9860 && TREE_CODE (treeop0) == SSA_NAME)
9862 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9863 if (is_gimple_assign (def)
9864 && gimple_assign_rhs_code (def) == NOP_EXPR)
9866 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9867 (TREE_TYPE (gimple_assign_rhs1 (def)));
9869 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9870 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9871 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9872 >= GET_MODE_BITSIZE (word_mode)))
9874 rtx_insn *seq, *seq_old;
9875 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9876 int_mode);
9877 bool extend_unsigned
9878 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9879 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9880 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9881 rtx dest_high = simplify_gen_subreg (word_mode, target,
9882 int_mode, high_off);
9883 HOST_WIDE_INT ramount = (BITS_PER_WORD
9884 - TREE_INT_CST_LOW (treeop1));
9885 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9887 start_sequence ();
9888 /* dest_high = src_low >> (word_size - C). */
9889 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9890 rshift, dest_high,
9891 extend_unsigned);
9892 if (temp != dest_high)
9893 emit_move_insn (dest_high, temp);
9895 /* dest_low = src_low << C. */
9896 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9897 treeop1, dest_low, unsignedp);
9898 if (temp != dest_low)
9899 emit_move_insn (dest_low, temp);
9901 seq = get_insns ();
9902 end_sequence ();
9903 temp = target ;
9905 if (have_insn_for (ASHIFT, int_mode))
9907 bool speed_p = optimize_insn_for_speed_p ();
9908 start_sequence ();
9909 rtx ret_old = expand_variable_shift (code, int_mode,
9910 op0, treeop1,
9911 target,
9912 unsignedp);
9914 seq_old = get_insns ();
9915 end_sequence ();
9916 if (seq_cost (seq, speed_p)
9917 >= seq_cost (seq_old, speed_p))
9919 seq = seq_old;
9920 temp = ret_old;
9923 emit_insn (seq);
9928 if (temp == NULL_RTX)
9929 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9930 unsignedp);
9931 if (code == LSHIFT_EXPR)
9932 temp = REDUCE_BIT_FIELD (temp);
9933 return temp;
9936 /* Could determine the answer when only additive constants differ. Also,
9937 the addition of one can be handled by changing the condition. */
9938 case LT_EXPR:
9939 case LE_EXPR:
9940 case GT_EXPR:
9941 case GE_EXPR:
9942 case EQ_EXPR:
9943 case NE_EXPR:
9944 case UNORDERED_EXPR:
9945 case ORDERED_EXPR:
9946 case UNLT_EXPR:
9947 case UNLE_EXPR:
9948 case UNGT_EXPR:
9949 case UNGE_EXPR:
9950 case UNEQ_EXPR:
9951 case LTGT_EXPR:
9953 temp = do_store_flag (ops,
9954 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9955 tmode != VOIDmode ? tmode : mode);
9956 if (temp)
9957 return temp;
9959 /* Use a compare and a jump for BLKmode comparisons, or for function
9960 type comparisons is have_canonicalize_funcptr_for_compare. */
9962 if ((target == 0
9963 || modifier == EXPAND_STACK_PARM
9964 || ! safe_from_p (target, treeop0, 1)
9965 || ! safe_from_p (target, treeop1, 1)
9966 /* Make sure we don't have a hard reg (such as function's return
9967 value) live across basic blocks, if not optimizing. */
9968 || (!optimize && REG_P (target)
9969 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9970 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9972 emit_move_insn (target, const0_rtx);
9974 rtx_code_label *lab1 = gen_label_rtx ();
9975 jumpifnot_1 (code, treeop0, treeop1, lab1,
9976 profile_probability::uninitialized ());
9978 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9979 emit_move_insn (target, constm1_rtx);
9980 else
9981 emit_move_insn (target, const1_rtx);
9983 emit_label (lab1);
9984 return target;
9986 case COMPLEX_EXPR:
9987 /* Get the rtx code of the operands. */
9988 op0 = expand_normal (treeop0);
9989 op1 = expand_normal (treeop1);
9991 if (!target)
9992 target = gen_reg_rtx (TYPE_MODE (type));
9993 else
9994 /* If target overlaps with op1, then either we need to force
9995 op1 into a pseudo (if target also overlaps with op0),
9996 or write the complex parts in reverse order. */
9997 switch (GET_CODE (target))
9999 case CONCAT:
10000 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10002 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10004 complex_expr_force_op1:
10005 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10006 emit_move_insn (temp, op1);
10007 op1 = temp;
10008 break;
10010 complex_expr_swap_order:
10011 /* Move the imaginary (op1) and real (op0) parts to their
10012 location. */
10013 write_complex_part (target, op1, true);
10014 write_complex_part (target, op0, false);
10016 return target;
10018 break;
10019 case MEM:
10020 temp = adjust_address_nv (target,
10021 GET_MODE_INNER (GET_MODE (target)), 0);
10022 if (reg_overlap_mentioned_p (temp, op1))
10024 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10025 temp = adjust_address_nv (target, imode,
10026 GET_MODE_SIZE (imode));
10027 if (reg_overlap_mentioned_p (temp, op0))
10028 goto complex_expr_force_op1;
10029 goto complex_expr_swap_order;
10031 break;
10032 default:
10033 if (reg_overlap_mentioned_p (target, op1))
10035 if (reg_overlap_mentioned_p (target, op0))
10036 goto complex_expr_force_op1;
10037 goto complex_expr_swap_order;
10039 break;
10042 /* Move the real (op0) and imaginary (op1) parts to their location. */
10043 write_complex_part (target, op0, false);
10044 write_complex_part (target, op1, true);
10046 return target;
10048 case WIDEN_SUM_EXPR:
10050 tree oprnd0 = treeop0;
10051 tree oprnd1 = treeop1;
10053 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10054 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10055 target, unsignedp);
10056 return target;
10059 case VEC_UNPACK_HI_EXPR:
10060 case VEC_UNPACK_LO_EXPR:
10061 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10062 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10064 op0 = expand_normal (treeop0);
10065 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10066 target, unsignedp);
10067 gcc_assert (temp);
10068 return temp;
10071 case VEC_UNPACK_FLOAT_HI_EXPR:
10072 case VEC_UNPACK_FLOAT_LO_EXPR:
10074 op0 = expand_normal (treeop0);
10075 /* The signedness is determined from input operand. */
10076 temp = expand_widen_pattern_expr
10077 (ops, op0, NULL_RTX, NULL_RTX,
10078 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10080 gcc_assert (temp);
10081 return temp;
10084 case VEC_WIDEN_PLUS_HI_EXPR:
10085 case VEC_WIDEN_PLUS_LO_EXPR:
10086 case VEC_WIDEN_MINUS_HI_EXPR:
10087 case VEC_WIDEN_MINUS_LO_EXPR:
10088 case VEC_WIDEN_MULT_HI_EXPR:
10089 case VEC_WIDEN_MULT_LO_EXPR:
10090 case VEC_WIDEN_MULT_EVEN_EXPR:
10091 case VEC_WIDEN_MULT_ODD_EXPR:
10092 case VEC_WIDEN_LSHIFT_HI_EXPR:
10093 case VEC_WIDEN_LSHIFT_LO_EXPR:
10094 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10095 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10096 target, unsignedp);
10097 gcc_assert (target);
10098 return target;
10100 case VEC_PACK_SAT_EXPR:
10101 case VEC_PACK_FIX_TRUNC_EXPR:
10102 mode = TYPE_MODE (TREE_TYPE (treeop0));
10103 subtarget = NULL_RTX;
10104 goto binop;
10106 case VEC_PACK_TRUNC_EXPR:
10107 if (VECTOR_BOOLEAN_TYPE_P (type)
10108 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10109 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10110 && SCALAR_INT_MODE_P (mode))
10112 class expand_operand eops[4];
10113 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10114 expand_operands (treeop0, treeop1,
10115 subtarget, &op0, &op1, EXPAND_NORMAL);
10116 this_optab = vec_pack_sbool_trunc_optab;
10117 enum insn_code icode = optab_handler (this_optab, imode);
10118 create_output_operand (&eops[0], target, mode);
10119 create_convert_operand_from (&eops[1], op0, imode, false);
10120 create_convert_operand_from (&eops[2], op1, imode, false);
10121 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10122 create_input_operand (&eops[3], temp, imode);
10123 expand_insn (icode, 4, eops);
10124 return eops[0].value;
10126 mode = TYPE_MODE (TREE_TYPE (treeop0));
10127 subtarget = NULL_RTX;
10128 goto binop;
10130 case VEC_PACK_FLOAT_EXPR:
10131 mode = TYPE_MODE (TREE_TYPE (treeop0));
10132 expand_operands (treeop0, treeop1,
10133 subtarget, &op0, &op1, EXPAND_NORMAL);
10134 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10135 optab_default);
10136 target = expand_binop (mode, this_optab, op0, op1, target,
10137 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10138 OPTAB_LIB_WIDEN);
10139 gcc_assert (target);
10140 return target;
10142 case VEC_PERM_EXPR:
10144 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10145 vec_perm_builder sel;
10146 if (TREE_CODE (treeop2) == VECTOR_CST
10147 && tree_to_vec_perm_builder (&sel, treeop2))
10149 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10150 temp = expand_vec_perm_const (mode, op0, op1, sel,
10151 sel_mode, target);
10153 else
10155 op2 = expand_normal (treeop2);
10156 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10158 gcc_assert (temp);
10159 return temp;
10162 case DOT_PROD_EXPR:
10164 tree oprnd0 = treeop0;
10165 tree oprnd1 = treeop1;
10166 tree oprnd2 = treeop2;
10168 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10169 op2 = expand_normal (oprnd2);
10170 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10171 target, unsignedp);
10172 return target;
10175 case SAD_EXPR:
10177 tree oprnd0 = treeop0;
10178 tree oprnd1 = treeop1;
10179 tree oprnd2 = treeop2;
10181 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10182 op2 = expand_normal (oprnd2);
10183 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10184 target, unsignedp);
10185 return target;
10188 case REALIGN_LOAD_EXPR:
10190 tree oprnd0 = treeop0;
10191 tree oprnd1 = treeop1;
10192 tree oprnd2 = treeop2;
10194 this_optab = optab_for_tree_code (code, type, optab_default);
10195 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10196 op2 = expand_normal (oprnd2);
10197 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10198 target, unsignedp);
10199 gcc_assert (temp);
10200 return temp;
10203 case COND_EXPR:
10205 /* A COND_EXPR with its type being VOID_TYPE represents a
10206 conditional jump and is handled in
10207 expand_gimple_cond_expr. */
10208 gcc_assert (!VOID_TYPE_P (type));
10210 /* Note that COND_EXPRs whose type is a structure or union
10211 are required to be constructed to contain assignments of
10212 a temporary variable, so that we can evaluate them here
10213 for side effect only. If type is void, we must do likewise. */
10215 gcc_assert (!TREE_ADDRESSABLE (type)
10216 && !ignore
10217 && TREE_TYPE (treeop1) != void_type_node
10218 && TREE_TYPE (treeop2) != void_type_node);
10220 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10221 if (temp)
10222 return temp;
10224 /* If we are not to produce a result, we have no target. Otherwise,
10225 if a target was specified use it; it will not be used as an
10226 intermediate target unless it is safe. If no target, use a
10227 temporary. */
10229 if (modifier != EXPAND_STACK_PARM
10230 && original_target
10231 && safe_from_p (original_target, treeop0, 1)
10232 && GET_MODE (original_target) == mode
10233 && !MEM_P (original_target))
10234 temp = original_target;
10235 else
10236 temp = assign_temp (type, 0, 1);
10238 do_pending_stack_adjust ();
10239 NO_DEFER_POP;
10240 rtx_code_label *lab0 = gen_label_rtx ();
10241 rtx_code_label *lab1 = gen_label_rtx ();
10242 jumpifnot (treeop0, lab0,
10243 profile_probability::uninitialized ());
10244 store_expr (treeop1, temp,
10245 modifier == EXPAND_STACK_PARM,
10246 false, false);
10248 emit_jump_insn (targetm.gen_jump (lab1));
10249 emit_barrier ();
10250 emit_label (lab0);
10251 store_expr (treeop2, temp,
10252 modifier == EXPAND_STACK_PARM,
10253 false, false);
10255 emit_label (lab1);
10256 OK_DEFER_POP;
10257 return temp;
10260 case VEC_DUPLICATE_EXPR:
10261 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10262 target = expand_vector_broadcast (mode, op0);
10263 gcc_assert (target);
10264 return target;
10266 case VEC_SERIES_EXPR:
10267 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10268 return expand_vec_series_expr (mode, op0, op1, target);
10270 case BIT_INSERT_EXPR:
10272 unsigned bitpos = tree_to_uhwi (treeop2);
10273 unsigned bitsize;
10274 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10275 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10276 else
10277 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10278 op0 = expand_normal (treeop0);
10279 op1 = expand_normal (treeop1);
10280 rtx dst = gen_reg_rtx (mode);
10281 emit_move_insn (dst, op0);
10282 store_bit_field (dst, bitsize, bitpos, 0, 0,
10283 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
10284 return dst;
10287 default:
10288 gcc_unreachable ();
10291 /* Here to do an ordinary binary operator. */
10292 binop:
10293 expand_operands (treeop0, treeop1,
10294 subtarget, &op0, &op1, EXPAND_NORMAL);
10295 binop2:
10296 this_optab = optab_for_tree_code (code, type, optab_default);
10297 binop3:
10298 if (modifier == EXPAND_STACK_PARM)
10299 target = 0;
10300 temp = expand_binop (mode, this_optab, op0, op1, target,
10301 unsignedp, OPTAB_LIB_WIDEN);
10302 gcc_assert (temp);
10303 /* Bitwise operations do not need bitfield reduction as we expect their
10304 operands being properly truncated. */
10305 if (code == BIT_XOR_EXPR
10306 || code == BIT_AND_EXPR
10307 || code == BIT_IOR_EXPR)
10308 return temp;
10309 return REDUCE_BIT_FIELD (temp);
10311 #undef REDUCE_BIT_FIELD
10314 /* Return TRUE if expression STMT is suitable for replacement.
10315 Never consider memory loads as replaceable, because those don't ever lead
10316 into constant expressions. */
10318 static bool
10319 stmt_is_replaceable_p (gimple *stmt)
10321 if (ssa_is_replaceable_p (stmt))
10323 /* Don't move around loads. */
10324 if (!gimple_assign_single_p (stmt)
10325 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10326 return true;
10328 return false;
10332 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10333 enum expand_modifier modifier, rtx *alt_rtl,
10334 bool inner_reference_p)
10336 rtx op0, op1, temp, decl_rtl;
10337 tree type;
10338 int unsignedp;
10339 machine_mode mode, dmode;
10340 enum tree_code code = TREE_CODE (exp);
10341 rtx subtarget, original_target;
10342 int ignore;
10343 tree context;
10344 bool reduce_bit_field;
10345 location_t loc = EXPR_LOCATION (exp);
10346 struct separate_ops ops;
10347 tree treeop0, treeop1, treeop2;
10348 tree ssa_name = NULL_TREE;
10349 gimple *g;
10351 type = TREE_TYPE (exp);
10352 mode = TYPE_MODE (type);
10353 unsignedp = TYPE_UNSIGNED (type);
10355 treeop0 = treeop1 = treeop2 = NULL_TREE;
10356 if (!VL_EXP_CLASS_P (exp))
10357 switch (TREE_CODE_LENGTH (code))
10359 default:
10360 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10361 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10362 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10363 case 0: break;
10365 ops.code = code;
10366 ops.type = type;
10367 ops.op0 = treeop0;
10368 ops.op1 = treeop1;
10369 ops.op2 = treeop2;
10370 ops.location = loc;
10372 ignore = (target == const0_rtx
10373 || ((CONVERT_EXPR_CODE_P (code)
10374 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10375 && TREE_CODE (type) == VOID_TYPE));
10377 /* An operation in what may be a bit-field type needs the
10378 result to be reduced to the precision of the bit-field type,
10379 which is narrower than that of the type's mode. */
10380 reduce_bit_field = (!ignore
10381 && INTEGRAL_TYPE_P (type)
10382 && !type_has_mode_precision_p (type));
10384 /* If we are going to ignore this result, we need only do something
10385 if there is a side-effect somewhere in the expression. If there
10386 is, short-circuit the most common cases here. Note that we must
10387 not call expand_expr with anything but const0_rtx in case this
10388 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10390 if (ignore)
10392 if (! TREE_SIDE_EFFECTS (exp))
10393 return const0_rtx;
10395 /* Ensure we reference a volatile object even if value is ignored, but
10396 don't do this if all we are doing is taking its address. */
10397 if (TREE_THIS_VOLATILE (exp)
10398 && TREE_CODE (exp) != FUNCTION_DECL
10399 && mode != VOIDmode && mode != BLKmode
10400 && modifier != EXPAND_CONST_ADDRESS)
10402 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10403 if (MEM_P (temp))
10404 copy_to_reg (temp);
10405 return const0_rtx;
10408 if (TREE_CODE_CLASS (code) == tcc_unary
10409 || code == BIT_FIELD_REF
10410 || code == COMPONENT_REF
10411 || code == INDIRECT_REF)
10412 return expand_expr (treeop0, const0_rtx, VOIDmode,
10413 modifier);
10415 else if (TREE_CODE_CLASS (code) == tcc_binary
10416 || TREE_CODE_CLASS (code) == tcc_comparison
10417 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10419 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10420 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10421 return const0_rtx;
10424 target = 0;
10427 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10428 target = 0;
10430 /* Use subtarget as the target for operand 0 of a binary operation. */
10431 subtarget = get_subtarget (target);
10432 original_target = target;
10434 switch (code)
10436 case LABEL_DECL:
10438 tree function = decl_function_context (exp);
10440 temp = label_rtx (exp);
10441 temp = gen_rtx_LABEL_REF (Pmode, temp);
10443 if (function != current_function_decl
10444 && function != 0)
10445 LABEL_REF_NONLOCAL_P (temp) = 1;
10447 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10448 return temp;
10451 case SSA_NAME:
10452 /* ??? ivopts calls expander, without any preparation from
10453 out-of-ssa. So fake instructions as if this was an access to the
10454 base variable. This unnecessarily allocates a pseudo, see how we can
10455 reuse it, if partition base vars have it set already. */
10456 if (!currently_expanding_to_rtl)
10458 tree var = SSA_NAME_VAR (exp);
10459 if (var && DECL_RTL_SET_P (var))
10460 return DECL_RTL (var);
10461 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10462 LAST_VIRTUAL_REGISTER + 1);
10465 g = get_gimple_for_ssa_name (exp);
10466 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10467 if (g == NULL
10468 && modifier == EXPAND_INITIALIZER
10469 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10470 && (optimize || !SSA_NAME_VAR (exp)
10471 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10472 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10473 g = SSA_NAME_DEF_STMT (exp);
10474 if (g)
10476 rtx r;
10477 location_t saved_loc = curr_insn_location ();
10478 loc = gimple_location (g);
10479 if (loc != UNKNOWN_LOCATION)
10480 set_curr_insn_location (loc);
10481 ops.code = gimple_assign_rhs_code (g);
10482 switch (get_gimple_rhs_class (ops.code))
10484 case GIMPLE_TERNARY_RHS:
10485 ops.op2 = gimple_assign_rhs3 (g);
10486 /* Fallthru */
10487 case GIMPLE_BINARY_RHS:
10488 ops.op1 = gimple_assign_rhs2 (g);
10490 /* Try to expand conditonal compare. */
10491 if (targetm.gen_ccmp_first)
10493 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10494 r = expand_ccmp_expr (g, mode);
10495 if (r)
10496 break;
10498 /* Fallthru */
10499 case GIMPLE_UNARY_RHS:
10500 ops.op0 = gimple_assign_rhs1 (g);
10501 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10502 ops.location = loc;
10503 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10504 break;
10505 case GIMPLE_SINGLE_RHS:
10507 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10508 tmode, modifier, alt_rtl,
10509 inner_reference_p);
10510 break;
10512 default:
10513 gcc_unreachable ();
10515 set_curr_insn_location (saved_loc);
10516 if (REG_P (r) && !REG_EXPR (r))
10517 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10518 return r;
10521 ssa_name = exp;
10522 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10523 exp = SSA_NAME_VAR (ssa_name);
10524 goto expand_decl_rtl;
10526 case VAR_DECL:
10527 /* Allow accel compiler to handle variables that require special
10528 treatment, e.g. if they have been modified in some way earlier in
10529 compilation by the adjust_private_decl OpenACC hook. */
10530 if (flag_openacc && targetm.goacc.expand_var_decl)
10532 temp = targetm.goacc.expand_var_decl (exp);
10533 if (temp)
10534 return temp;
10536 /* ... fall through ... */
10538 case PARM_DECL:
10539 /* If a static var's type was incomplete when the decl was written,
10540 but the type is complete now, lay out the decl now. */
10541 if (DECL_SIZE (exp) == 0
10542 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10543 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10544 layout_decl (exp, 0);
10546 /* fall through */
10548 case FUNCTION_DECL:
10549 case RESULT_DECL:
10550 decl_rtl = DECL_RTL (exp);
10551 expand_decl_rtl:
10552 gcc_assert (decl_rtl);
10554 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10555 settings for VECTOR_TYPE_P that might switch for the function. */
10556 if (currently_expanding_to_rtl
10557 && code == VAR_DECL && MEM_P (decl_rtl)
10558 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10559 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10560 else
10561 decl_rtl = copy_rtx (decl_rtl);
10563 /* Record writes to register variables. */
10564 if (modifier == EXPAND_WRITE
10565 && REG_P (decl_rtl)
10566 && HARD_REGISTER_P (decl_rtl))
10567 add_to_hard_reg_set (&crtl->asm_clobbers,
10568 GET_MODE (decl_rtl), REGNO (decl_rtl));
10570 /* Ensure variable marked as used even if it doesn't go through
10571 a parser. If it hasn't be used yet, write out an external
10572 definition. */
10573 if (exp)
10574 TREE_USED (exp) = 1;
10576 /* Show we haven't gotten RTL for this yet. */
10577 temp = 0;
10579 /* Variables inherited from containing functions should have
10580 been lowered by this point. */
10581 if (exp)
10582 context = decl_function_context (exp);
10583 gcc_assert (!exp
10584 || SCOPE_FILE_SCOPE_P (context)
10585 || context == current_function_decl
10586 || TREE_STATIC (exp)
10587 || DECL_EXTERNAL (exp)
10588 /* ??? C++ creates functions that are not TREE_STATIC. */
10589 || TREE_CODE (exp) == FUNCTION_DECL);
10591 /* This is the case of an array whose size is to be determined
10592 from its initializer, while the initializer is still being parsed.
10593 ??? We aren't parsing while expanding anymore. */
10595 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10596 temp = validize_mem (decl_rtl);
10598 /* If DECL_RTL is memory, we are in the normal case and the
10599 address is not valid, get the address into a register. */
10601 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10603 if (alt_rtl)
10604 *alt_rtl = decl_rtl;
10605 decl_rtl = use_anchored_address (decl_rtl);
10606 if (modifier != EXPAND_CONST_ADDRESS
10607 && modifier != EXPAND_SUM
10608 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10609 : GET_MODE (decl_rtl),
10610 XEXP (decl_rtl, 0),
10611 MEM_ADDR_SPACE (decl_rtl)))
10612 temp = replace_equiv_address (decl_rtl,
10613 copy_rtx (XEXP (decl_rtl, 0)));
10616 /* If we got something, return it. But first, set the alignment
10617 if the address is a register. */
10618 if (temp != 0)
10620 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10621 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10623 else if (MEM_P (decl_rtl))
10624 temp = decl_rtl;
10626 if (temp != 0)
10628 if (MEM_P (temp)
10629 && modifier != EXPAND_WRITE
10630 && modifier != EXPAND_MEMORY
10631 && modifier != EXPAND_INITIALIZER
10632 && modifier != EXPAND_CONST_ADDRESS
10633 && modifier != EXPAND_SUM
10634 && !inner_reference_p
10635 && mode != BLKmode
10636 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10637 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10638 MEM_ALIGN (temp), NULL_RTX, NULL);
10640 return temp;
10643 if (exp)
10644 dmode = DECL_MODE (exp);
10645 else
10646 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10648 /* If the mode of DECL_RTL does not match that of the decl,
10649 there are two cases: we are dealing with a BLKmode value
10650 that is returned in a register, or we are dealing with
10651 a promoted value. In the latter case, return a SUBREG
10652 of the wanted mode, but mark it so that we know that it
10653 was already extended. */
10654 if (REG_P (decl_rtl)
10655 && dmode != BLKmode
10656 && GET_MODE (decl_rtl) != dmode)
10658 machine_mode pmode;
10660 /* Get the signedness to be used for this variable. Ensure we get
10661 the same mode we got when the variable was declared. */
10662 if (code != SSA_NAME)
10663 pmode = promote_decl_mode (exp, &unsignedp);
10664 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10665 && gimple_code (g) == GIMPLE_CALL
10666 && !gimple_call_internal_p (g))
10667 pmode = promote_function_mode (type, mode, &unsignedp,
10668 gimple_call_fntype (g),
10670 else
10671 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10672 gcc_assert (GET_MODE (decl_rtl) == pmode);
10674 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10675 SUBREG_PROMOTED_VAR_P (temp) = 1;
10676 SUBREG_PROMOTED_SET (temp, unsignedp);
10677 return temp;
10680 return decl_rtl;
10682 case INTEGER_CST:
10684 /* Given that TYPE_PRECISION (type) is not always equal to
10685 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10686 the former to the latter according to the signedness of the
10687 type. */
10688 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10689 temp = immed_wide_int_const
10690 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10691 return temp;
10694 case VECTOR_CST:
10696 tree tmp = NULL_TREE;
10697 if (VECTOR_MODE_P (mode))
10698 return const_vector_from_tree (exp);
10699 scalar_int_mode int_mode;
10700 if (is_int_mode (mode, &int_mode))
10702 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10703 if (type_for_mode)
10704 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10705 type_for_mode, exp);
10707 if (!tmp)
10709 vec<constructor_elt, va_gc> *v;
10710 /* Constructors need to be fixed-length. FIXME. */
10711 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10712 vec_alloc (v, nunits);
10713 for (unsigned int i = 0; i < nunits; ++i)
10714 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10715 tmp = build_constructor (type, v);
10717 return expand_expr (tmp, ignore ? const0_rtx : target,
10718 tmode, modifier);
10721 case CONST_DECL:
10722 if (modifier == EXPAND_WRITE)
10724 /* Writing into CONST_DECL is always invalid, but handle it
10725 gracefully. */
10726 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10727 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10728 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10729 EXPAND_NORMAL, as);
10730 op0 = memory_address_addr_space (mode, op0, as);
10731 temp = gen_rtx_MEM (mode, op0);
10732 set_mem_addr_space (temp, as);
10733 return temp;
10735 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10737 case REAL_CST:
10738 /* If optimized, generate immediate CONST_DOUBLE
10739 which will be turned into memory by reload if necessary.
10741 We used to force a register so that loop.c could see it. But
10742 this does not allow gen_* patterns to perform optimizations with
10743 the constants. It also produces two insns in cases like "x = 1.0;".
10744 On most machines, floating-point constants are not permitted in
10745 many insns, so we'd end up copying it to a register in any case.
10747 Now, we do the copying in expand_binop, if appropriate. */
10748 return const_double_from_real_value (TREE_REAL_CST (exp),
10749 TYPE_MODE (TREE_TYPE (exp)));
10751 case FIXED_CST:
10752 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10753 TYPE_MODE (TREE_TYPE (exp)));
10755 case COMPLEX_CST:
10756 /* Handle evaluating a complex constant in a CONCAT target. */
10757 if (original_target && GET_CODE (original_target) == CONCAT)
10759 rtx rtarg, itarg;
10761 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10762 rtarg = XEXP (original_target, 0);
10763 itarg = XEXP (original_target, 1);
10765 /* Move the real and imaginary parts separately. */
10766 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10767 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10769 if (op0 != rtarg)
10770 emit_move_insn (rtarg, op0);
10771 if (op1 != itarg)
10772 emit_move_insn (itarg, op1);
10774 return original_target;
10777 /* fall through */
10779 case STRING_CST:
10780 temp = expand_expr_constant (exp, 1, modifier);
10782 /* temp contains a constant address.
10783 On RISC machines where a constant address isn't valid,
10784 make some insns to get that address into a register. */
10785 if (modifier != EXPAND_CONST_ADDRESS
10786 && modifier != EXPAND_INITIALIZER
10787 && modifier != EXPAND_SUM
10788 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10789 MEM_ADDR_SPACE (temp)))
10790 return replace_equiv_address (temp,
10791 copy_rtx (XEXP (temp, 0)));
10792 return temp;
10794 case POLY_INT_CST:
10795 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10797 case SAVE_EXPR:
10799 tree val = treeop0;
10800 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10801 inner_reference_p);
10803 if (!SAVE_EXPR_RESOLVED_P (exp))
10805 /* We can indeed still hit this case, typically via builtin
10806 expanders calling save_expr immediately before expanding
10807 something. Assume this means that we only have to deal
10808 with non-BLKmode values. */
10809 gcc_assert (GET_MODE (ret) != BLKmode);
10811 val = build_decl (curr_insn_location (),
10812 VAR_DECL, NULL, TREE_TYPE (exp));
10813 DECL_ARTIFICIAL (val) = 1;
10814 DECL_IGNORED_P (val) = 1;
10815 treeop0 = val;
10816 TREE_OPERAND (exp, 0) = treeop0;
10817 SAVE_EXPR_RESOLVED_P (exp) = 1;
10819 if (!CONSTANT_P (ret))
10820 ret = copy_to_reg (ret);
10821 SET_DECL_RTL (val, ret);
10824 return ret;
10828 case CONSTRUCTOR:
10829 /* If we don't need the result, just ensure we evaluate any
10830 subexpressions. */
10831 if (ignore)
10833 unsigned HOST_WIDE_INT idx;
10834 tree value;
10836 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10837 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10839 return const0_rtx;
10842 return expand_constructor (exp, target, modifier, false);
10844 case TARGET_MEM_REF:
10846 addr_space_t as
10847 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10848 unsigned int align;
10850 op0 = addr_for_mem_ref (exp, as, true);
10851 op0 = memory_address_addr_space (mode, op0, as);
10852 temp = gen_rtx_MEM (mode, op0);
10853 set_mem_attributes (temp, exp, 0);
10854 set_mem_addr_space (temp, as);
10855 align = get_object_alignment (exp);
10856 if (modifier != EXPAND_WRITE
10857 && modifier != EXPAND_MEMORY
10858 && mode != BLKmode
10859 && align < GET_MODE_ALIGNMENT (mode))
10860 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10861 align, NULL_RTX, NULL);
10862 return temp;
10865 case MEM_REF:
10867 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10868 addr_space_t as
10869 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10870 machine_mode address_mode;
10871 tree base = TREE_OPERAND (exp, 0);
10872 gimple *def_stmt;
10873 unsigned align;
10874 /* Handle expansion of non-aliased memory with non-BLKmode. That
10875 might end up in a register. */
10876 if (mem_ref_refers_to_non_mem_p (exp))
10878 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10879 base = TREE_OPERAND (base, 0);
10880 poly_uint64 type_size;
10881 if (known_eq (offset, 0)
10882 && !reverse
10883 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10884 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10885 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10886 target, tmode, modifier);
10887 if (TYPE_MODE (type) == BLKmode)
10889 temp = assign_stack_temp (DECL_MODE (base),
10890 GET_MODE_SIZE (DECL_MODE (base)));
10891 store_expr (base, temp, 0, false, false);
10892 temp = adjust_address (temp, BLKmode, offset);
10893 set_mem_size (temp, int_size_in_bytes (type));
10894 return temp;
10896 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10897 bitsize_int (offset * BITS_PER_UNIT));
10898 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10899 return expand_expr (exp, target, tmode, modifier);
10901 address_mode = targetm.addr_space.address_mode (as);
10902 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10904 tree mask = gimple_assign_rhs2 (def_stmt);
10905 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10906 gimple_assign_rhs1 (def_stmt), mask);
10907 TREE_OPERAND (exp, 0) = base;
10909 align = get_object_alignment (exp);
10910 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10911 op0 = memory_address_addr_space (mode, op0, as);
10912 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10914 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10915 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10916 op0 = memory_address_addr_space (mode, op0, as);
10918 temp = gen_rtx_MEM (mode, op0);
10919 set_mem_attributes (temp, exp, 0);
10920 set_mem_addr_space (temp, as);
10921 if (TREE_THIS_VOLATILE (exp))
10922 MEM_VOLATILE_P (temp) = 1;
10923 if (modifier != EXPAND_WRITE
10924 && modifier != EXPAND_MEMORY
10925 && !inner_reference_p
10926 && mode != BLKmode
10927 && align < GET_MODE_ALIGNMENT (mode))
10928 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
10929 modifier == EXPAND_STACK_PARM
10930 ? NULL_RTX : target, alt_rtl);
10931 if (reverse
10932 && modifier != EXPAND_MEMORY
10933 && modifier != EXPAND_WRITE)
10934 temp = flip_storage_order (mode, temp);
10935 return temp;
10938 case ARRAY_REF:
10941 tree array = treeop0;
10942 tree index = treeop1;
10943 tree init;
10945 /* Fold an expression like: "foo"[2].
10946 This is not done in fold so it won't happen inside &.
10947 Don't fold if this is for wide characters since it's too
10948 difficult to do correctly and this is a very rare case. */
10950 if (modifier != EXPAND_CONST_ADDRESS
10951 && modifier != EXPAND_INITIALIZER
10952 && modifier != EXPAND_MEMORY)
10954 tree t = fold_read_from_constant_string (exp);
10956 if (t)
10957 return expand_expr (t, target, tmode, modifier);
10960 /* If this is a constant index into a constant array,
10961 just get the value from the array. Handle both the cases when
10962 we have an explicit constructor and when our operand is a variable
10963 that was declared const. */
10965 if (modifier != EXPAND_CONST_ADDRESS
10966 && modifier != EXPAND_INITIALIZER
10967 && modifier != EXPAND_MEMORY
10968 && TREE_CODE (array) == CONSTRUCTOR
10969 && ! TREE_SIDE_EFFECTS (array)
10970 && TREE_CODE (index) == INTEGER_CST)
10972 unsigned HOST_WIDE_INT ix;
10973 tree field, value;
10975 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10976 field, value)
10977 if (tree_int_cst_equal (field, index))
10979 if (!TREE_SIDE_EFFECTS (value))
10980 return expand_expr (fold (value), target, tmode, modifier);
10981 break;
10985 else if (optimize >= 1
10986 && modifier != EXPAND_CONST_ADDRESS
10987 && modifier != EXPAND_INITIALIZER
10988 && modifier != EXPAND_MEMORY
10989 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10990 && TREE_CODE (index) == INTEGER_CST
10991 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10992 && (init = ctor_for_folding (array)) != error_mark_node)
10994 if (init == NULL_TREE)
10996 tree value = build_zero_cst (type);
10997 if (TREE_CODE (value) == CONSTRUCTOR)
10999 /* If VALUE is a CONSTRUCTOR, this optimization is only
11000 useful if this doesn't store the CONSTRUCTOR into
11001 memory. If it does, it is more efficient to just
11002 load the data from the array directly. */
11003 rtx ret = expand_constructor (value, target,
11004 modifier, true);
11005 if (ret == NULL_RTX)
11006 value = NULL_TREE;
11009 if (value)
11010 return expand_expr (value, target, tmode, modifier);
11012 else if (TREE_CODE (init) == CONSTRUCTOR)
11014 unsigned HOST_WIDE_INT ix;
11015 tree field, value;
11017 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11018 field, value)
11019 if (tree_int_cst_equal (field, index))
11021 if (TREE_SIDE_EFFECTS (value))
11022 break;
11024 if (TREE_CODE (value) == CONSTRUCTOR)
11026 /* If VALUE is a CONSTRUCTOR, this
11027 optimization is only useful if
11028 this doesn't store the CONSTRUCTOR
11029 into memory. If it does, it is more
11030 efficient to just load the data from
11031 the array directly. */
11032 rtx ret = expand_constructor (value, target,
11033 modifier, true);
11034 if (ret == NULL_RTX)
11035 break;
11038 return
11039 expand_expr (fold (value), target, tmode, modifier);
11042 else if (TREE_CODE (init) == STRING_CST)
11044 tree low_bound = array_ref_low_bound (exp);
11045 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11047 /* Optimize the special case of a zero lower bound.
11049 We convert the lower bound to sizetype to avoid problems
11050 with constant folding. E.g. suppose the lower bound is
11051 1 and its mode is QI. Without the conversion
11052 (ARRAY + (INDEX - (unsigned char)1))
11053 becomes
11054 (ARRAY + (-(unsigned char)1) + INDEX)
11055 which becomes
11056 (ARRAY + 255 + INDEX). Oops! */
11057 if (!integer_zerop (low_bound))
11058 index1 = size_diffop_loc (loc, index1,
11059 fold_convert_loc (loc, sizetype,
11060 low_bound));
11062 if (tree_fits_uhwi_p (index1)
11063 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11065 tree char_type = TREE_TYPE (TREE_TYPE (init));
11066 scalar_int_mode char_mode;
11068 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11069 && GET_MODE_SIZE (char_mode) == 1)
11070 return gen_int_mode (TREE_STRING_POINTER (init)
11071 [TREE_INT_CST_LOW (index1)],
11072 char_mode);
11077 goto normal_inner_ref;
11079 case COMPONENT_REF:
11080 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11081 /* Fall through. */
11082 case BIT_FIELD_REF:
11083 case ARRAY_RANGE_REF:
11084 normal_inner_ref:
11086 machine_mode mode1, mode2;
11087 poly_int64 bitsize, bitpos, bytepos;
11088 tree offset;
11089 int reversep, volatilep = 0, must_force_mem;
11090 tree tem
11091 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11092 &unsignedp, &reversep, &volatilep);
11093 rtx orig_op0, memloc;
11094 bool clear_mem_expr = false;
11096 /* If we got back the original object, something is wrong. Perhaps
11097 we are evaluating an expression too early. In any event, don't
11098 infinitely recurse. */
11099 gcc_assert (tem != exp);
11101 /* If TEM's type is a union of variable size, pass TARGET to the inner
11102 computation, since it will need a temporary and TARGET is known
11103 to have to do. This occurs in unchecked conversion in Ada. */
11104 orig_op0 = op0
11105 = expand_expr_real (tem,
11106 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11107 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11108 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11109 != INTEGER_CST)
11110 && modifier != EXPAND_STACK_PARM
11111 ? target : NULL_RTX),
11112 VOIDmode,
11113 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11114 NULL, true);
11116 /* If the field has a mode, we want to access it in the
11117 field's mode, not the computed mode.
11118 If a MEM has VOIDmode (external with incomplete type),
11119 use BLKmode for it instead. */
11120 if (MEM_P (op0))
11122 if (mode1 != VOIDmode)
11123 op0 = adjust_address (op0, mode1, 0);
11124 else if (GET_MODE (op0) == VOIDmode)
11125 op0 = adjust_address (op0, BLKmode, 0);
11128 mode2
11129 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11131 /* Make sure bitpos is not negative, it can wreak havoc later. */
11132 if (maybe_lt (bitpos, 0))
11134 gcc_checking_assert (offset == NULL_TREE);
11135 offset = size_int (bits_to_bytes_round_down (bitpos));
11136 bitpos = num_trailing_bits (bitpos);
11139 /* If we have either an offset, a BLKmode result, or a reference
11140 outside the underlying object, we must force it to memory.
11141 Such a case can occur in Ada if we have unchecked conversion
11142 of an expression from a scalar type to an aggregate type or
11143 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11144 passed a partially uninitialized object or a view-conversion
11145 to a larger size. */
11146 must_force_mem = (offset
11147 || mode1 == BLKmode
11148 || (mode == BLKmode
11149 && !int_mode_for_size (bitsize, 1).exists ())
11150 || maybe_gt (bitpos + bitsize,
11151 GET_MODE_BITSIZE (mode2)));
11153 /* Handle CONCAT first. */
11154 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11156 if (known_eq (bitpos, 0)
11157 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11158 && COMPLEX_MODE_P (mode1)
11159 && COMPLEX_MODE_P (GET_MODE (op0))
11160 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11161 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11163 if (reversep)
11164 op0 = flip_storage_order (GET_MODE (op0), op0);
11165 if (mode1 != GET_MODE (op0))
11167 rtx parts[2];
11168 for (int i = 0; i < 2; i++)
11170 rtx op = read_complex_part (op0, i != 0);
11171 if (GET_CODE (op) == SUBREG)
11172 op = force_reg (GET_MODE (op), op);
11173 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11174 if (temp)
11175 op = temp;
11176 else
11178 if (!REG_P (op) && !MEM_P (op))
11179 op = force_reg (GET_MODE (op), op);
11180 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11182 parts[i] = op;
11184 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11186 return op0;
11188 if (known_eq (bitpos, 0)
11189 && known_eq (bitsize,
11190 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11191 && maybe_ne (bitsize, 0))
11193 op0 = XEXP (op0, 0);
11194 mode2 = GET_MODE (op0);
11196 else if (known_eq (bitpos,
11197 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11198 && known_eq (bitsize,
11199 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11200 && maybe_ne (bitpos, 0)
11201 && maybe_ne (bitsize, 0))
11203 op0 = XEXP (op0, 1);
11204 bitpos = 0;
11205 mode2 = GET_MODE (op0);
11207 else
11208 /* Otherwise force into memory. */
11209 must_force_mem = 1;
11212 /* If this is a constant, put it in a register if it is a legitimate
11213 constant and we don't need a memory reference. */
11214 if (CONSTANT_P (op0)
11215 && mode2 != BLKmode
11216 && targetm.legitimate_constant_p (mode2, op0)
11217 && !must_force_mem)
11218 op0 = force_reg (mode2, op0);
11220 /* Otherwise, if this is a constant, try to force it to the constant
11221 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11222 is a legitimate constant. */
11223 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11224 op0 = validize_mem (memloc);
11226 /* Otherwise, if this is a constant or the object is not in memory
11227 and need be, put it there. */
11228 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11230 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11231 emit_move_insn (memloc, op0);
11232 op0 = memloc;
11233 clear_mem_expr = true;
11236 if (offset)
11238 machine_mode address_mode;
11239 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11240 EXPAND_SUM);
11242 gcc_assert (MEM_P (op0));
11244 address_mode = get_address_mode (op0);
11245 if (GET_MODE (offset_rtx) != address_mode)
11247 /* We cannot be sure that the RTL in offset_rtx is valid outside
11248 of a memory address context, so force it into a register
11249 before attempting to convert it to the desired mode. */
11250 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11251 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11254 /* See the comment in expand_assignment for the rationale. */
11255 if (mode1 != VOIDmode
11256 && maybe_ne (bitpos, 0)
11257 && maybe_gt (bitsize, 0)
11258 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11259 && multiple_p (bitpos, bitsize)
11260 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11261 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11263 op0 = adjust_address (op0, mode1, bytepos);
11264 bitpos = 0;
11267 op0 = offset_address (op0, offset_rtx,
11268 highest_pow2_factor (offset));
11271 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11272 record its alignment as BIGGEST_ALIGNMENT. */
11273 if (MEM_P (op0)
11274 && known_eq (bitpos, 0)
11275 && offset != 0
11276 && is_aligning_offset (offset, tem))
11277 set_mem_align (op0, BIGGEST_ALIGNMENT);
11279 /* Don't forget about volatility even if this is a bitfield. */
11280 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11282 if (op0 == orig_op0)
11283 op0 = copy_rtx (op0);
11285 MEM_VOLATILE_P (op0) = 1;
11288 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11290 if (op0 == orig_op0)
11291 op0 = copy_rtx (op0);
11293 set_mem_align (op0, BITS_PER_UNIT);
11296 /* In cases where an aligned union has an unaligned object
11297 as a field, we might be extracting a BLKmode value from
11298 an integer-mode (e.g., SImode) object. Handle this case
11299 by doing the extract into an object as wide as the field
11300 (which we know to be the width of a basic mode), then
11301 storing into memory, and changing the mode to BLKmode. */
11302 if (mode1 == VOIDmode
11303 || REG_P (op0) || GET_CODE (op0) == SUBREG
11304 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11305 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11306 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11307 && modifier != EXPAND_CONST_ADDRESS
11308 && modifier != EXPAND_INITIALIZER
11309 && modifier != EXPAND_MEMORY)
11310 /* If the bitfield is volatile and the bitsize
11311 is narrower than the access size of the bitfield,
11312 we need to extract bitfields from the access. */
11313 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11314 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11315 && mode1 != BLKmode
11316 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11317 /* If the field isn't aligned enough to fetch as a memref,
11318 fetch it as a bit field. */
11319 || (mode1 != BLKmode
11320 && (((MEM_P (op0)
11321 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11322 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11323 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11324 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11325 && modifier != EXPAND_MEMORY
11326 && ((modifier == EXPAND_CONST_ADDRESS
11327 || modifier == EXPAND_INITIALIZER)
11328 ? STRICT_ALIGNMENT
11329 : targetm.slow_unaligned_access (mode1,
11330 MEM_ALIGN (op0))))
11331 || !multiple_p (bitpos, BITS_PER_UNIT)))
11332 /* If the type and the field are a constant size and the
11333 size of the type isn't the same size as the bitfield,
11334 we must use bitfield operations. */
11335 || (known_size_p (bitsize)
11336 && TYPE_SIZE (TREE_TYPE (exp))
11337 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11338 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11339 bitsize)))
11341 machine_mode ext_mode = mode;
11343 if (ext_mode == BLKmode
11344 && ! (target != 0 && MEM_P (op0)
11345 && MEM_P (target)
11346 && multiple_p (bitpos, BITS_PER_UNIT)))
11347 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11349 if (ext_mode == BLKmode)
11351 if (target == 0)
11352 target = assign_temp (type, 1, 1);
11354 /* ??? Unlike the similar test a few lines below, this one is
11355 very likely obsolete. */
11356 if (known_eq (bitsize, 0))
11357 return target;
11359 /* In this case, BITPOS must start at a byte boundary and
11360 TARGET, if specified, must be a MEM. */
11361 gcc_assert (MEM_P (op0)
11362 && (!target || MEM_P (target)));
11364 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11365 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11366 emit_block_move (target,
11367 adjust_address (op0, VOIDmode, bytepos),
11368 gen_int_mode (bytesize, Pmode),
11369 (modifier == EXPAND_STACK_PARM
11370 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11372 return target;
11375 /* If we have nothing to extract, the result will be 0 for targets
11376 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11377 return 0 for the sake of consistency, as reading a zero-sized
11378 bitfield is valid in Ada and the value is fully specified. */
11379 if (known_eq (bitsize, 0))
11380 return const0_rtx;
11382 op0 = validize_mem (op0);
11384 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11385 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11387 /* If the result has aggregate type and the extraction is done in
11388 an integral mode, then the field may be not aligned on a byte
11389 boundary; in this case, if it has reverse storage order, it
11390 needs to be extracted as a scalar field with reverse storage
11391 order and put back into memory order afterwards. */
11392 if (AGGREGATE_TYPE_P (type)
11393 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11394 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11396 gcc_checking_assert (known_ge (bitpos, 0));
11397 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11398 (modifier == EXPAND_STACK_PARM
11399 ? NULL_RTX : target),
11400 ext_mode, ext_mode, reversep, alt_rtl);
11402 /* If the result has aggregate type and the mode of OP0 is an
11403 integral mode then, if BITSIZE is narrower than this mode
11404 and this is for big-endian data, we must put the field
11405 into the high-order bits. And we must also put it back
11406 into memory order if it has been previously reversed. */
11407 scalar_int_mode op0_mode;
11408 if (AGGREGATE_TYPE_P (type)
11409 && is_int_mode (GET_MODE (op0), &op0_mode))
11411 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11413 gcc_checking_assert (known_le (bitsize, size));
11414 if (maybe_lt (bitsize, size)
11415 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11416 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11417 size - bitsize, op0, 1);
11419 if (reversep)
11420 op0 = flip_storage_order (op0_mode, op0);
11423 /* If the result type is BLKmode, store the data into a temporary
11424 of the appropriate type, but with the mode corresponding to the
11425 mode for the data we have (op0's mode). */
11426 if (mode == BLKmode)
11428 rtx new_rtx
11429 = assign_stack_temp_for_type (ext_mode,
11430 GET_MODE_BITSIZE (ext_mode),
11431 type);
11432 emit_move_insn (new_rtx, op0);
11433 op0 = copy_rtx (new_rtx);
11434 PUT_MODE (op0, BLKmode);
11437 return op0;
11440 /* If the result is BLKmode, use that to access the object
11441 now as well. */
11442 if (mode == BLKmode)
11443 mode1 = BLKmode;
11445 /* Get a reference to just this component. */
11446 bytepos = bits_to_bytes_round_down (bitpos);
11447 if (modifier == EXPAND_CONST_ADDRESS
11448 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11449 op0 = adjust_address_nv (op0, mode1, bytepos);
11450 else
11451 op0 = adjust_address (op0, mode1, bytepos);
11453 if (op0 == orig_op0)
11454 op0 = copy_rtx (op0);
11456 /* Don't set memory attributes if the base expression is
11457 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11458 we should just honor its original memory attributes. */
11459 if (!(TREE_CODE (tem) == SSA_NAME
11460 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11461 set_mem_attributes (op0, exp, 0);
11463 if (REG_P (XEXP (op0, 0)))
11464 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11466 /* If op0 is a temporary because the original expressions was forced
11467 to memory, clear MEM_EXPR so that the original expression cannot
11468 be marked as addressable through MEM_EXPR of the temporary. */
11469 if (clear_mem_expr)
11470 set_mem_expr (op0, NULL_TREE);
11472 MEM_VOLATILE_P (op0) |= volatilep;
11474 if (reversep
11475 && modifier != EXPAND_MEMORY
11476 && modifier != EXPAND_WRITE)
11477 op0 = flip_storage_order (mode1, op0);
11479 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11480 || modifier == EXPAND_CONST_ADDRESS
11481 || modifier == EXPAND_INITIALIZER)
11482 return op0;
11484 if (target == 0)
11485 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11487 convert_move (target, op0, unsignedp);
11488 return target;
11491 case OBJ_TYPE_REF:
11492 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11494 case CALL_EXPR:
11495 /* All valid uses of __builtin_va_arg_pack () are removed during
11496 inlining. */
11497 if (CALL_EXPR_VA_ARG_PACK (exp))
11498 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11500 tree fndecl = get_callee_fndecl (exp), attr;
11502 if (fndecl
11503 /* Don't diagnose the error attribute in thunks, those are
11504 artificially created. */
11505 && !CALL_FROM_THUNK_P (exp)
11506 && (attr = lookup_attribute ("error",
11507 DECL_ATTRIBUTES (fndecl))) != NULL)
11509 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11510 error ("call to %qs declared with attribute error: %s",
11511 identifier_to_locale (ident),
11512 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11514 if (fndecl
11515 /* Don't diagnose the warning attribute in thunks, those are
11516 artificially created. */
11517 && !CALL_FROM_THUNK_P (exp)
11518 && (attr = lookup_attribute ("warning",
11519 DECL_ATTRIBUTES (fndecl))) != NULL)
11521 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11522 warning_at (EXPR_LOCATION (exp),
11523 OPT_Wattribute_warning,
11524 "call to %qs declared with attribute warning: %s",
11525 identifier_to_locale (ident),
11526 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11529 /* Check for a built-in function. */
11530 if (fndecl && fndecl_built_in_p (fndecl))
11532 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11533 return expand_builtin (exp, target, subtarget, tmode, ignore);
11536 return expand_call (exp, target, ignore);
11538 case VIEW_CONVERT_EXPR:
11539 op0 = NULL_RTX;
11541 /* If we are converting to BLKmode, try to avoid an intermediate
11542 temporary by fetching an inner memory reference. */
11543 if (mode == BLKmode
11544 && poly_int_tree_p (TYPE_SIZE (type))
11545 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11546 && handled_component_p (treeop0))
11548 machine_mode mode1;
11549 poly_int64 bitsize, bitpos, bytepos;
11550 tree offset;
11551 int reversep, volatilep = 0;
11552 tree tem
11553 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11554 &unsignedp, &reversep, &volatilep);
11556 /* ??? We should work harder and deal with non-zero offsets. */
11557 if (!offset
11558 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11559 && !reversep
11560 && known_size_p (bitsize)
11561 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11563 /* See the normal_inner_ref case for the rationale. */
11564 rtx orig_op0
11565 = expand_expr_real (tem,
11566 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11567 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11568 != INTEGER_CST)
11569 && modifier != EXPAND_STACK_PARM
11570 ? target : NULL_RTX),
11571 VOIDmode,
11572 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11573 NULL, true);
11575 if (MEM_P (orig_op0))
11577 op0 = orig_op0;
11579 /* Get a reference to just this component. */
11580 if (modifier == EXPAND_CONST_ADDRESS
11581 || modifier == EXPAND_SUM
11582 || modifier == EXPAND_INITIALIZER)
11583 op0 = adjust_address_nv (op0, mode, bytepos);
11584 else
11585 op0 = adjust_address (op0, mode, bytepos);
11587 if (op0 == orig_op0)
11588 op0 = copy_rtx (op0);
11590 set_mem_attributes (op0, treeop0, 0);
11591 if (REG_P (XEXP (op0, 0)))
11592 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11594 MEM_VOLATILE_P (op0) |= volatilep;
11599 if (!op0)
11600 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11601 NULL, inner_reference_p);
11603 /* If the input and output modes are both the same, we are done. */
11604 if (mode == GET_MODE (op0))
11606 /* If neither mode is BLKmode, and both modes are the same size
11607 then we can use gen_lowpart. */
11608 else if (mode != BLKmode
11609 && GET_MODE (op0) != BLKmode
11610 && known_eq (GET_MODE_PRECISION (mode),
11611 GET_MODE_PRECISION (GET_MODE (op0)))
11612 && !COMPLEX_MODE_P (GET_MODE (op0)))
11614 if (GET_CODE (op0) == SUBREG)
11615 op0 = force_reg (GET_MODE (op0), op0);
11616 temp = gen_lowpart_common (mode, op0);
11617 if (temp)
11618 op0 = temp;
11619 else
11621 if (!REG_P (op0) && !MEM_P (op0))
11622 op0 = force_reg (GET_MODE (op0), op0);
11623 op0 = gen_lowpart (mode, op0);
11626 /* If both types are integral, convert from one mode to the other. */
11627 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11628 op0 = convert_modes (mode, GET_MODE (op0), op0,
11629 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11630 /* If the output type is a bit-field type, do an extraction. */
11631 else if (reduce_bit_field)
11632 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11633 TYPE_UNSIGNED (type), NULL_RTX,
11634 mode, mode, false, NULL);
11635 /* As a last resort, spill op0 to memory, and reload it in a
11636 different mode. */
11637 else if (!MEM_P (op0))
11639 /* If the operand is not a MEM, force it into memory. Since we
11640 are going to be changing the mode of the MEM, don't call
11641 force_const_mem for constants because we don't allow pool
11642 constants to change mode. */
11643 tree inner_type = TREE_TYPE (treeop0);
11645 gcc_assert (!TREE_ADDRESSABLE (exp));
11647 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11648 target
11649 = assign_stack_temp_for_type
11650 (TYPE_MODE (inner_type),
11651 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11653 emit_move_insn (target, op0);
11654 op0 = target;
11657 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11658 output type is such that the operand is known to be aligned, indicate
11659 that it is. Otherwise, we need only be concerned about alignment for
11660 non-BLKmode results. */
11661 if (MEM_P (op0))
11663 enum insn_code icode;
11665 if (modifier != EXPAND_WRITE
11666 && modifier != EXPAND_MEMORY
11667 && !inner_reference_p
11668 && mode != BLKmode
11669 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11671 /* If the target does have special handling for unaligned
11672 loads of mode then use them. */
11673 if ((icode = optab_handler (movmisalign_optab, mode))
11674 != CODE_FOR_nothing)
11676 rtx reg;
11678 op0 = adjust_address (op0, mode, 0);
11679 /* We've already validated the memory, and we're creating a
11680 new pseudo destination. The predicates really can't
11681 fail. */
11682 reg = gen_reg_rtx (mode);
11684 /* Nor can the insn generator. */
11685 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11686 emit_insn (insn);
11687 return reg;
11689 else if (STRICT_ALIGNMENT)
11691 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11692 poly_uint64 temp_size = mode_size;
11693 if (GET_MODE (op0) != BLKmode)
11694 temp_size = upper_bound (temp_size,
11695 GET_MODE_SIZE (GET_MODE (op0)));
11696 rtx new_rtx
11697 = assign_stack_temp_for_type (mode, temp_size, type);
11698 rtx new_with_op0_mode
11699 = adjust_address (new_rtx, GET_MODE (op0), 0);
11701 gcc_assert (!TREE_ADDRESSABLE (exp));
11703 if (GET_MODE (op0) == BLKmode)
11705 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11706 emit_block_move (new_with_op0_mode, op0, size_rtx,
11707 (modifier == EXPAND_STACK_PARM
11708 ? BLOCK_OP_CALL_PARM
11709 : BLOCK_OP_NORMAL));
11711 else
11712 emit_move_insn (new_with_op0_mode, op0);
11714 op0 = new_rtx;
11718 op0 = adjust_address (op0, mode, 0);
11721 return op0;
11723 case MODIFY_EXPR:
11725 tree lhs = treeop0;
11726 tree rhs = treeop1;
11727 gcc_assert (ignore);
11729 /* Check for |= or &= of a bitfield of size one into another bitfield
11730 of size 1. In this case, (unless we need the result of the
11731 assignment) we can do this more efficiently with a
11732 test followed by an assignment, if necessary.
11734 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11735 things change so we do, this code should be enhanced to
11736 support it. */
11737 if (TREE_CODE (lhs) == COMPONENT_REF
11738 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11739 || TREE_CODE (rhs) == BIT_AND_EXPR)
11740 && TREE_OPERAND (rhs, 0) == lhs
11741 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11742 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11743 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11745 rtx_code_label *label = gen_label_rtx ();
11746 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11747 profile_probability prob = profile_probability::uninitialized ();
11748 if (value)
11749 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11750 else
11751 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11752 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11753 false);
11754 do_pending_stack_adjust ();
11755 emit_label (label);
11756 return const0_rtx;
11759 expand_assignment (lhs, rhs, false);
11760 return const0_rtx;
11763 case ADDR_EXPR:
11764 return expand_expr_addr_expr (exp, target, tmode, modifier);
11766 case REALPART_EXPR:
11767 op0 = expand_normal (treeop0);
11768 return read_complex_part (op0, false);
11770 case IMAGPART_EXPR:
11771 op0 = expand_normal (treeop0);
11772 return read_complex_part (op0, true);
11774 case RETURN_EXPR:
11775 case LABEL_EXPR:
11776 case GOTO_EXPR:
11777 case SWITCH_EXPR:
11778 case ASM_EXPR:
11779 /* Expanded in cfgexpand.c. */
11780 gcc_unreachable ();
11782 case TRY_CATCH_EXPR:
11783 case CATCH_EXPR:
11784 case EH_FILTER_EXPR:
11785 case TRY_FINALLY_EXPR:
11786 case EH_ELSE_EXPR:
11787 /* Lowered by tree-eh.c. */
11788 gcc_unreachable ();
11790 case WITH_CLEANUP_EXPR:
11791 case CLEANUP_POINT_EXPR:
11792 case TARGET_EXPR:
11793 case CASE_LABEL_EXPR:
11794 case VA_ARG_EXPR:
11795 case BIND_EXPR:
11796 case INIT_EXPR:
11797 case CONJ_EXPR:
11798 case COMPOUND_EXPR:
11799 case PREINCREMENT_EXPR:
11800 case PREDECREMENT_EXPR:
11801 case POSTINCREMENT_EXPR:
11802 case POSTDECREMENT_EXPR:
11803 case LOOP_EXPR:
11804 case EXIT_EXPR:
11805 case COMPOUND_LITERAL_EXPR:
11806 /* Lowered by gimplify.c. */
11807 gcc_unreachable ();
11809 case FDESC_EXPR:
11810 /* Function descriptors are not valid except for as
11811 initialization constants, and should not be expanded. */
11812 gcc_unreachable ();
11814 case WITH_SIZE_EXPR:
11815 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11816 have pulled out the size to use in whatever context it needed. */
11817 return expand_expr_real (treeop0, original_target, tmode,
11818 modifier, alt_rtl, inner_reference_p);
11820 default:
11821 return expand_expr_real_2 (&ops, target, tmode, modifier);
11825 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11826 signedness of TYPE), possibly returning the result in TARGET.
11827 TYPE is known to be a partial integer type. */
11828 static rtx
11829 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11831 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11832 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11833 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11834 && (!target || GET_MODE (target) == mode));
11836 /* For constant values, reduce using wide_int_to_tree. */
11837 if (poly_int_rtx_p (exp))
11839 auto value = wi::to_poly_wide (exp, mode);
11840 tree t = wide_int_to_tree (type, value);
11841 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11843 else if (TYPE_UNSIGNED (type))
11845 rtx mask = immed_wide_int_const
11846 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11847 return expand_and (mode, exp, mask, target);
11849 else
11851 int count = GET_MODE_PRECISION (mode) - prec;
11852 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11853 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11857 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11858 when applied to the address of EXP produces an address known to be
11859 aligned more than BIGGEST_ALIGNMENT. */
11861 static int
11862 is_aligning_offset (const_tree offset, const_tree exp)
11864 /* Strip off any conversions. */
11865 while (CONVERT_EXPR_P (offset))
11866 offset = TREE_OPERAND (offset, 0);
11868 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11869 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11870 if (TREE_CODE (offset) != BIT_AND_EXPR
11871 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11872 || compare_tree_int (TREE_OPERAND (offset, 1),
11873 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11874 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11875 return 0;
11877 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11878 It must be NEGATE_EXPR. Then strip any more conversions. */
11879 offset = TREE_OPERAND (offset, 0);
11880 while (CONVERT_EXPR_P (offset))
11881 offset = TREE_OPERAND (offset, 0);
11883 if (TREE_CODE (offset) != NEGATE_EXPR)
11884 return 0;
11886 offset = TREE_OPERAND (offset, 0);
11887 while (CONVERT_EXPR_P (offset))
11888 offset = TREE_OPERAND (offset, 0);
11890 /* This must now be the address of EXP. */
11891 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11894 /* Return a STRING_CST corresponding to ARG's constant initializer either
11895 if it's a string constant, or, when VALREP is set, any other constant,
11896 or null otherwise.
11897 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
11898 within the byte string that ARG is references. If nonnull set *MEM_SIZE
11899 to the size of the byte string. If nonnull, set *DECL to the constant
11900 declaration ARG refers to. */
11902 static tree
11903 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
11904 bool valrep = false)
11906 tree dummy = NULL_TREE;
11907 if (!mem_size)
11908 mem_size = &dummy;
11910 /* Store the type of the original expression before conversions
11911 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
11912 removed. */
11913 tree argtype = TREE_TYPE (arg);
11915 tree array;
11916 STRIP_NOPS (arg);
11918 /* Non-constant index into the character array in an ARRAY_REF
11919 expression or null. */
11920 tree varidx = NULL_TREE;
11922 poly_int64 base_off = 0;
11924 if (TREE_CODE (arg) == ADDR_EXPR)
11926 arg = TREE_OPERAND (arg, 0);
11927 tree ref = arg;
11928 if (TREE_CODE (arg) == ARRAY_REF)
11930 tree idx = TREE_OPERAND (arg, 1);
11931 if (TREE_CODE (idx) != INTEGER_CST)
11933 /* From a pointer (but not array) argument extract the variable
11934 index to prevent get_addr_base_and_unit_offset() from failing
11935 due to it. Use it later to compute the non-constant offset
11936 into the string and return it to the caller. */
11937 varidx = idx;
11938 ref = TREE_OPERAND (arg, 0);
11940 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11941 return NULL_TREE;
11943 if (!integer_zerop (array_ref_low_bound (arg)))
11944 return NULL_TREE;
11946 if (!integer_onep (array_ref_element_size (arg)))
11947 return NULL_TREE;
11950 array = get_addr_base_and_unit_offset (ref, &base_off);
11951 if (!array
11952 || (TREE_CODE (array) != VAR_DECL
11953 && TREE_CODE (array) != CONST_DECL
11954 && TREE_CODE (array) != STRING_CST))
11955 return NULL_TREE;
11957 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11959 tree arg0 = TREE_OPERAND (arg, 0);
11960 tree arg1 = TREE_OPERAND (arg, 1);
11962 tree offset;
11963 tree str = string_constant (arg0, &offset, mem_size, decl);
11964 if (!str)
11966 str = string_constant (arg1, &offset, mem_size, decl);
11967 arg1 = arg0;
11970 if (str)
11972 /* Avoid pointers to arrays (see bug 86622). */
11973 if (POINTER_TYPE_P (TREE_TYPE (arg))
11974 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
11975 && !(decl && !*decl)
11976 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11977 && tree_fits_uhwi_p (*mem_size)
11978 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11979 return NULL_TREE;
11981 tree type = TREE_TYPE (offset);
11982 arg1 = fold_convert (type, arg1);
11983 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
11984 return str;
11986 return NULL_TREE;
11988 else if (TREE_CODE (arg) == SSA_NAME)
11990 gimple *stmt = SSA_NAME_DEF_STMT (arg);
11991 if (!is_gimple_assign (stmt))
11992 return NULL_TREE;
11994 tree rhs1 = gimple_assign_rhs1 (stmt);
11995 tree_code code = gimple_assign_rhs_code (stmt);
11996 if (code == ADDR_EXPR)
11997 return string_constant (rhs1, ptr_offset, mem_size, decl);
11998 else if (code != POINTER_PLUS_EXPR)
11999 return NULL_TREE;
12001 tree offset;
12002 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12004 /* Avoid pointers to arrays (see bug 86622). */
12005 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12006 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12007 && !(decl && !*decl)
12008 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12009 && tree_fits_uhwi_p (*mem_size)
12010 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12011 return NULL_TREE;
12013 tree rhs2 = gimple_assign_rhs2 (stmt);
12014 tree type = TREE_TYPE (offset);
12015 rhs2 = fold_convert (type, rhs2);
12016 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12017 return str;
12019 return NULL_TREE;
12021 else if (DECL_P (arg))
12022 array = arg;
12023 else
12024 return NULL_TREE;
12026 tree offset = wide_int_to_tree (sizetype, base_off);
12027 if (varidx)
12029 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12030 return NULL_TREE;
12032 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12033 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12034 if (TREE_CODE (chartype) != INTEGER_TYPE)
12035 return NULL;
12037 offset = fold_convert (sizetype, varidx);
12040 if (TREE_CODE (array) == STRING_CST)
12042 *ptr_offset = fold_convert (sizetype, offset);
12043 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12044 if (decl)
12045 *decl = NULL_TREE;
12046 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12047 >= TREE_STRING_LENGTH (array));
12048 return array;
12051 tree init = ctor_for_folding (array);
12052 if (!init || init == error_mark_node)
12053 return NULL_TREE;
12055 if (valrep)
12057 HOST_WIDE_INT cstoff;
12058 if (!base_off.is_constant (&cstoff))
12059 return NULL_TREE;
12061 /* Check that the host and target are sane. */
12062 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12063 return NULL_TREE;
12065 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12066 if (typesz <= 0 || (int) typesz != typesz)
12067 return NULL_TREE;
12069 HOST_WIDE_INT size = typesz;
12070 if (VAR_P (array)
12071 && DECL_SIZE_UNIT (array)
12072 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12074 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12075 gcc_checking_assert (size >= typesz);
12078 /* If value representation was requested convert the initializer
12079 for the whole array or object into a string of bytes forming
12080 its value representation and return it. */
12081 unsigned char *bytes = XNEWVEC (unsigned char, size);
12082 int r = native_encode_initializer (init, bytes, size);
12083 if (r < typesz)
12085 XDELETEVEC (bytes);
12086 return NULL_TREE;
12089 if (r < size)
12090 memset (bytes + r, '\0', size - r);
12092 const char *p = reinterpret_cast<const char *>(bytes);
12093 init = build_string_literal (size, p, char_type_node);
12094 init = TREE_OPERAND (init, 0);
12095 init = TREE_OPERAND (init, 0);
12096 XDELETE (bytes);
12098 *mem_size = size_int (TREE_STRING_LENGTH (init));
12099 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12101 if (decl)
12102 *decl = array;
12104 return init;
12107 if (TREE_CODE (init) == CONSTRUCTOR)
12109 /* Convert the 64-bit constant offset to a wider type to avoid
12110 overflow and use it to obtain the initializer for the subobject
12111 it points into. */
12112 offset_int wioff;
12113 if (!base_off.is_constant (&wioff))
12114 return NULL_TREE;
12116 wioff *= BITS_PER_UNIT;
12117 if (!wi::fits_uhwi_p (wioff))
12118 return NULL_TREE;
12120 base_off = wioff.to_uhwi ();
12121 unsigned HOST_WIDE_INT fieldoff = 0;
12122 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12123 &fieldoff);
12124 if (!init || init == error_mark_node)
12125 return NULL_TREE;
12127 HOST_WIDE_INT cstoff;
12128 if (!base_off.is_constant (&cstoff))
12129 return NULL_TREE;
12131 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12132 tree off = build_int_cst (sizetype, cstoff);
12133 if (varidx)
12134 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12135 else
12136 offset = off;
12139 *ptr_offset = offset;
12141 tree inittype = TREE_TYPE (init);
12143 if (TREE_CODE (init) == INTEGER_CST
12144 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12145 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12147 /* Check that the host and target are sane. */
12148 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12149 return NULL_TREE;
12151 /* For a reference to (address of) a single constant character,
12152 store the native representation of the character in CHARBUF.
12153 If the reference is to an element of an array or a member
12154 of a struct, only consider narrow characters until ctors
12155 for wide character arrays are transformed to STRING_CSTs
12156 like those for narrow arrays. */
12157 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12158 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12159 if (len > 0)
12161 /* Construct a string literal with elements of INITTYPE and
12162 the representation above. Then strip
12163 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12164 init = build_string_literal (len, (char *)charbuf, inittype);
12165 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12169 tree initsize = TYPE_SIZE_UNIT (inittype);
12171 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12173 /* Fold an empty/zero constructor for an implicitly initialized
12174 object or subobject into the empty string. */
12176 /* Determine the character type from that of the original
12177 expression. */
12178 tree chartype = argtype;
12179 if (POINTER_TYPE_P (chartype))
12180 chartype = TREE_TYPE (chartype);
12181 while (TREE_CODE (chartype) == ARRAY_TYPE)
12182 chartype = TREE_TYPE (chartype);
12184 if (INTEGRAL_TYPE_P (chartype)
12185 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12187 /* Convert a char array to an empty STRING_CST having an array
12188 of the expected type and size. */
12189 if (!initsize)
12190 initsize = integer_zero_node;
12192 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12193 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12194 return NULL_TREE;
12196 init = build_string_literal (size, NULL, chartype, size);
12197 init = TREE_OPERAND (init, 0);
12198 init = TREE_OPERAND (init, 0);
12200 *ptr_offset = integer_zero_node;
12204 if (decl)
12205 *decl = array;
12207 if (TREE_CODE (init) != STRING_CST)
12208 return NULL_TREE;
12210 *mem_size = initsize;
12212 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12214 return init;
12217 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12218 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12219 non-constant) offset in bytes within the string that ARG is accessing.
12220 If MEM_SIZE is non-zero the storage size of the memory is returned.
12221 If DECL is non-zero the constant declaration is returned if available. */
12223 tree
12224 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12226 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12229 /* Similar to string_constant, return a STRING_CST corresponding
12230 to the value representation of the first argument if it's
12231 a constant. */
12233 tree
12234 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12236 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12239 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12240 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12241 for C2 > 0 to x & C3 == C2
12242 for C2 < 0 to x & C3 == (C2 & C3). */
12243 enum tree_code
12244 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12246 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12247 tree treeop0 = gimple_assign_rhs1 (stmt);
12248 tree treeop1 = gimple_assign_rhs2 (stmt);
12249 tree type = TREE_TYPE (*arg0);
12250 scalar_int_mode mode;
12251 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12252 return code;
12253 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12254 || TYPE_PRECISION (type) <= 1
12255 || TYPE_UNSIGNED (type)
12256 /* Signed x % c == 0 should have been optimized into unsigned modulo
12257 earlier. */
12258 || integer_zerop (*arg1)
12259 /* If c is known to be non-negative, modulo will be expanded as unsigned
12260 modulo. */
12261 || get_range_pos_neg (treeop0) == 1)
12262 return code;
12264 /* x % c == d where d < 0 && d <= -c should be always false. */
12265 if (tree_int_cst_sgn (*arg1) == -1
12266 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12267 return code;
12269 int prec = TYPE_PRECISION (type);
12270 wide_int w = wi::to_wide (treeop1) - 1;
12271 w |= wi::shifted_mask (0, prec - 1, true, prec);
12272 tree c3 = wide_int_to_tree (type, w);
12273 tree c4 = *arg1;
12274 if (tree_int_cst_sgn (*arg1) == -1)
12275 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12277 rtx op0 = expand_normal (treeop0);
12278 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12280 bool speed_p = optimize_insn_for_speed_p ();
12282 do_pending_stack_adjust ();
12284 location_t loc = gimple_location (stmt);
12285 struct separate_ops ops;
12286 ops.code = TRUNC_MOD_EXPR;
12287 ops.location = loc;
12288 ops.type = TREE_TYPE (treeop0);
12289 ops.op0 = treeop0;
12290 ops.op1 = treeop1;
12291 ops.op2 = NULL_TREE;
12292 start_sequence ();
12293 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12294 EXPAND_NORMAL);
12295 rtx_insn *moinsns = get_insns ();
12296 end_sequence ();
12298 unsigned mocost = seq_cost (moinsns, speed_p);
12299 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12300 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12302 ops.code = BIT_AND_EXPR;
12303 ops.location = loc;
12304 ops.type = TREE_TYPE (treeop0);
12305 ops.op0 = treeop0;
12306 ops.op1 = c3;
12307 ops.op2 = NULL_TREE;
12308 start_sequence ();
12309 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12310 EXPAND_NORMAL);
12311 rtx_insn *muinsns = get_insns ();
12312 end_sequence ();
12314 unsigned mucost = seq_cost (muinsns, speed_p);
12315 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12316 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12318 if (mocost <= mucost)
12320 emit_insn (moinsns);
12321 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12322 return code;
12325 emit_insn (muinsns);
12326 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12327 *arg1 = c4;
12328 return code;
12331 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12332 If C1 is odd to:
12333 (X - C2) * C3 <= C4 (or >), where
12334 C3 is modular multiplicative inverse of C1 and 1<<prec and
12335 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12336 if C2 > ((1<<prec) - 1) % C1).
12337 If C1 is even, S = ctz (C1) and C2 is 0, use
12338 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12339 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12341 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12342 unsigned):
12343 (X * C3) + C4 <= 2 * C4, where
12344 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12345 C4 is ((1<<(prec - 1) - 1) / C1).
12346 If C1 is even, S = ctz(C1), use
12347 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12348 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12349 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12351 See the Hacker's Delight book, section 10-17. */
12352 enum tree_code
12353 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12355 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12356 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12358 if (optimize < 2)
12359 return code;
12361 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12362 if (stmt == NULL)
12363 return code;
12365 tree treeop0 = gimple_assign_rhs1 (stmt);
12366 tree treeop1 = gimple_assign_rhs2 (stmt);
12367 if (TREE_CODE (treeop0) != SSA_NAME
12368 || TREE_CODE (treeop1) != INTEGER_CST
12369 /* Don't optimize the undefined behavior case x % 0;
12370 x % 1 should have been optimized into zero, punt if
12371 it makes it here for whatever reason;
12372 x % -c should have been optimized into x % c. */
12373 || compare_tree_int (treeop1, 2) <= 0
12374 /* Likewise x % c == d where d >= c should be always false. */
12375 || tree_int_cst_le (treeop1, *arg1))
12376 return code;
12378 /* Unsigned x % pow2 is handled right already, for signed
12379 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12380 if (integer_pow2p (treeop1))
12381 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12383 tree type = TREE_TYPE (*arg0);
12384 scalar_int_mode mode;
12385 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12386 return code;
12387 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12388 || TYPE_PRECISION (type) <= 1)
12389 return code;
12391 signop sgn = UNSIGNED;
12392 /* If both operands are known to have the sign bit clear, handle
12393 even the signed modulo case as unsigned. treeop1 is always
12394 positive >= 2, checked above. */
12395 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12396 sgn = SIGNED;
12398 if (!TYPE_UNSIGNED (type))
12400 if (tree_int_cst_sgn (*arg1) == -1)
12401 return code;
12402 type = unsigned_type_for (type);
12403 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12404 return code;
12407 int prec = TYPE_PRECISION (type);
12408 wide_int w = wi::to_wide (treeop1);
12409 int shift = wi::ctz (w);
12410 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12411 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12412 If C1 is odd, we can handle all cases by subtracting
12413 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12414 e.g. by testing for overflow on the subtraction, punt on that for now
12415 though. */
12416 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12418 if (sgn == SIGNED)
12419 return code;
12420 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12421 if (wi::gtu_p (wi::to_wide (*arg1), x))
12422 return code;
12425 imm_use_iterator imm_iter;
12426 use_operand_p use_p;
12427 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12429 gimple *use_stmt = USE_STMT (use_p);
12430 /* Punt if treeop0 is used in the same bb in a division
12431 or another modulo with the same divisor. We should expect
12432 the division and modulo combined together. */
12433 if (use_stmt == stmt
12434 || gimple_bb (use_stmt) != gimple_bb (stmt))
12435 continue;
12436 if (!is_gimple_assign (use_stmt)
12437 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12438 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12439 continue;
12440 if (gimple_assign_rhs1 (use_stmt) != treeop0
12441 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12442 continue;
12443 return code;
12446 w = wi::lrshift (w, shift);
12447 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12448 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12449 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12450 tree c3 = wide_int_to_tree (type, m);
12451 tree c5 = NULL_TREE;
12452 wide_int d, e;
12453 if (sgn == UNSIGNED)
12455 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12456 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12457 otherwise use < or subtract one from C4. E.g. for
12458 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12459 x % 3U == 1 already needs to be
12460 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12461 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12462 d -= 1;
12463 if (shift)
12464 d = wi::lrshift (d, shift);
12466 else
12468 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12469 if (!shift)
12470 d = wi::lshift (e, 1);
12471 else
12473 e = wi::bit_and (e, wi::mask (shift, true, prec));
12474 d = wi::lrshift (e, shift - 1);
12476 c5 = wide_int_to_tree (type, e);
12478 tree c4 = wide_int_to_tree (type, d);
12480 rtx op0 = expand_normal (treeop0);
12481 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12483 bool speed_p = optimize_insn_for_speed_p ();
12485 do_pending_stack_adjust ();
12487 location_t loc = gimple_location (stmt);
12488 struct separate_ops ops;
12489 ops.code = TRUNC_MOD_EXPR;
12490 ops.location = loc;
12491 ops.type = TREE_TYPE (treeop0);
12492 ops.op0 = treeop0;
12493 ops.op1 = treeop1;
12494 ops.op2 = NULL_TREE;
12495 start_sequence ();
12496 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12497 EXPAND_NORMAL);
12498 rtx_insn *moinsns = get_insns ();
12499 end_sequence ();
12501 unsigned mocost = seq_cost (moinsns, speed_p);
12502 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12503 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12505 tree t = fold_convert_loc (loc, type, treeop0);
12506 if (!integer_zerop (*arg1))
12507 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12508 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12509 if (sgn == SIGNED)
12510 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12511 if (shift)
12513 tree s = build_int_cst (NULL_TREE, shift);
12514 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12517 start_sequence ();
12518 rtx mur = expand_normal (t);
12519 rtx_insn *muinsns = get_insns ();
12520 end_sequence ();
12522 unsigned mucost = seq_cost (muinsns, speed_p);
12523 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12524 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12526 if (mocost <= mucost)
12528 emit_insn (moinsns);
12529 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12530 return code;
12533 emit_insn (muinsns);
12534 *arg0 = make_tree (type, mur);
12535 *arg1 = c4;
12536 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12539 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12541 void
12542 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12544 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12545 || code == LT_EXPR || code == LE_EXPR);
12546 gcc_checking_assert (integer_zerop (*arg1));
12548 if (!optimize)
12549 return;
12551 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12552 if (stmt == NULL)
12553 return;
12555 tree treeop0 = gimple_assign_rhs1 (stmt);
12556 tree treeop1 = gimple_assign_rhs2 (stmt);
12557 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12558 return;
12560 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12561 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12562 "assuming signed overflow does not occur when "
12563 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12564 op_symbol_code (code), op_symbol_code (code));
12566 *arg0 = treeop0;
12567 *arg1 = treeop1;
12570 /* Generate code to calculate OPS, and exploded expression
12571 using a store-flag instruction and return an rtx for the result.
12572 OPS reflects a comparison.
12574 If TARGET is nonzero, store the result there if convenient.
12576 Return zero if there is no suitable set-flag instruction
12577 available on this machine.
12579 Once expand_expr has been called on the arguments of the comparison,
12580 we are committed to doing the store flag, since it is not safe to
12581 re-evaluate the expression. We emit the store-flag insn by calling
12582 emit_store_flag, but only expand the arguments if we have a reason
12583 to believe that emit_store_flag will be successful. If we think that
12584 it will, but it isn't, we have to simulate the store-flag with a
12585 set/jump/set sequence. */
12587 static rtx
12588 do_store_flag (sepops ops, rtx target, machine_mode mode)
12590 enum rtx_code code;
12591 tree arg0, arg1, type;
12592 machine_mode operand_mode;
12593 int unsignedp;
12594 rtx op0, op1;
12595 rtx subtarget = target;
12596 location_t loc = ops->location;
12598 arg0 = ops->op0;
12599 arg1 = ops->op1;
12601 /* Don't crash if the comparison was erroneous. */
12602 if (arg0 == error_mark_node || arg1 == error_mark_node)
12603 return const0_rtx;
12605 type = TREE_TYPE (arg0);
12606 operand_mode = TYPE_MODE (type);
12607 unsignedp = TYPE_UNSIGNED (type);
12609 /* We won't bother with BLKmode store-flag operations because it would mean
12610 passing a lot of information to emit_store_flag. */
12611 if (operand_mode == BLKmode)
12612 return 0;
12614 /* We won't bother with store-flag operations involving function pointers
12615 when function pointers must be canonicalized before comparisons. */
12616 if (targetm.have_canonicalize_funcptr_for_compare ()
12617 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12618 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12619 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12620 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12621 return 0;
12623 STRIP_NOPS (arg0);
12624 STRIP_NOPS (arg1);
12626 /* For vector typed comparisons emit code to generate the desired
12627 all-ones or all-zeros mask. */
12628 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12630 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12631 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12632 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12633 return expand_vec_cmp_expr (ops->type, ifexp, target);
12634 else
12635 gcc_unreachable ();
12638 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12639 into (x - C2) * C3 < C4. */
12640 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12641 && TREE_CODE (arg0) == SSA_NAME
12642 && TREE_CODE (arg1) == INTEGER_CST)
12644 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12645 &arg0, &arg1);
12646 if (new_code != ops->code)
12648 struct separate_ops nops = *ops;
12649 nops.code = ops->code = new_code;
12650 nops.op0 = arg0;
12651 nops.op1 = arg1;
12652 nops.type = TREE_TYPE (arg0);
12653 return do_store_flag (&nops, target, mode);
12657 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12658 if (!unsignedp
12659 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12660 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12661 && integer_zerop (arg1)
12662 && TREE_CODE (arg0) == SSA_NAME)
12663 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12665 /* Get the rtx comparison code to use. We know that EXP is a comparison
12666 operation of some type. Some comparisons against 1 and -1 can be
12667 converted to comparisons with zero. Do so here so that the tests
12668 below will be aware that we have a comparison with zero. These
12669 tests will not catch constants in the first operand, but constants
12670 are rarely passed as the first operand. */
12672 switch (ops->code)
12674 case EQ_EXPR:
12675 code = EQ;
12676 break;
12677 case NE_EXPR:
12678 code = NE;
12679 break;
12680 case LT_EXPR:
12681 if (integer_onep (arg1))
12682 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12683 else
12684 code = unsignedp ? LTU : LT;
12685 break;
12686 case LE_EXPR:
12687 if (! unsignedp && integer_all_onesp (arg1))
12688 arg1 = integer_zero_node, code = LT;
12689 else
12690 code = unsignedp ? LEU : LE;
12691 break;
12692 case GT_EXPR:
12693 if (! unsignedp && integer_all_onesp (arg1))
12694 arg1 = integer_zero_node, code = GE;
12695 else
12696 code = unsignedp ? GTU : GT;
12697 break;
12698 case GE_EXPR:
12699 if (integer_onep (arg1))
12700 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12701 else
12702 code = unsignedp ? GEU : GE;
12703 break;
12705 case UNORDERED_EXPR:
12706 code = UNORDERED;
12707 break;
12708 case ORDERED_EXPR:
12709 code = ORDERED;
12710 break;
12711 case UNLT_EXPR:
12712 code = UNLT;
12713 break;
12714 case UNLE_EXPR:
12715 code = UNLE;
12716 break;
12717 case UNGT_EXPR:
12718 code = UNGT;
12719 break;
12720 case UNGE_EXPR:
12721 code = UNGE;
12722 break;
12723 case UNEQ_EXPR:
12724 code = UNEQ;
12725 break;
12726 case LTGT_EXPR:
12727 code = LTGT;
12728 break;
12730 default:
12731 gcc_unreachable ();
12734 /* Put a constant second. */
12735 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12736 || TREE_CODE (arg0) == FIXED_CST)
12738 std::swap (arg0, arg1);
12739 code = swap_condition (code);
12742 /* If this is an equality or inequality test of a single bit, we can
12743 do this by shifting the bit being tested to the low-order bit and
12744 masking the result with the constant 1. If the condition was EQ,
12745 we xor it with 1. This does not require an scc insn and is faster
12746 than an scc insn even if we have it.
12748 The code to make this transformation was moved into fold_single_bit_test,
12749 so we just call into the folder and expand its result. */
12751 if ((code == NE || code == EQ)
12752 && integer_zerop (arg1)
12753 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12755 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12756 if (srcstmt
12757 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12759 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12760 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12761 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12762 gimple_assign_rhs1 (srcstmt),
12763 gimple_assign_rhs2 (srcstmt));
12764 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12765 if (temp)
12766 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12770 if (! get_subtarget (target)
12771 || GET_MODE (subtarget) != operand_mode)
12772 subtarget = 0;
12774 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12776 if (target == 0)
12777 target = gen_reg_rtx (mode);
12779 /* Try a cstore if possible. */
12780 return emit_store_flag_force (target, code, op0, op1,
12781 operand_mode, unsignedp,
12782 (TYPE_PRECISION (ops->type) == 1
12783 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12786 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12787 0 otherwise (i.e. if there is no casesi instruction).
12789 DEFAULT_PROBABILITY is the probability of jumping to the default
12790 label. */
12792 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12793 rtx table_label, rtx default_label, rtx fallback_label,
12794 profile_probability default_probability)
12796 class expand_operand ops[5];
12797 scalar_int_mode index_mode = SImode;
12798 rtx op1, op2, index;
12800 if (! targetm.have_casesi ())
12801 return 0;
12803 /* The index must be some form of integer. Convert it to SImode. */
12804 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12805 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12807 rtx rangertx = expand_normal (range);
12809 /* We must handle the endpoints in the original mode. */
12810 index_expr = build2 (MINUS_EXPR, index_type,
12811 index_expr, minval);
12812 minval = integer_zero_node;
12813 index = expand_normal (index_expr);
12814 if (default_label)
12815 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12816 omode, 1, default_label,
12817 default_probability);
12818 /* Now we can safely truncate. */
12819 index = convert_to_mode (index_mode, index, 0);
12821 else
12823 if (omode != index_mode)
12825 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12826 index_expr = fold_convert (index_type, index_expr);
12829 index = expand_normal (index_expr);
12832 do_pending_stack_adjust ();
12834 op1 = expand_normal (minval);
12835 op2 = expand_normal (range);
12837 create_input_operand (&ops[0], index, index_mode);
12838 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12839 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12840 create_fixed_operand (&ops[3], table_label);
12841 create_fixed_operand (&ops[4], (default_label
12842 ? default_label
12843 : fallback_label));
12844 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12845 return 1;
12848 /* Attempt to generate a tablejump instruction; same concept. */
12849 /* Subroutine of the next function.
12851 INDEX is the value being switched on, with the lowest value
12852 in the table already subtracted.
12853 MODE is its expected mode (needed if INDEX is constant).
12854 RANGE is the length of the jump table.
12855 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12857 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12858 index value is out of range.
12859 DEFAULT_PROBABILITY is the probability of jumping to
12860 the default label. */
12862 static void
12863 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12864 rtx default_label, profile_probability default_probability)
12866 rtx temp, vector;
12868 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12869 cfun->cfg->max_jumptable_ents = INTVAL (range);
12871 /* Do an unsigned comparison (in the proper mode) between the index
12872 expression and the value which represents the length of the range.
12873 Since we just finished subtracting the lower bound of the range
12874 from the index expression, this comparison allows us to simultaneously
12875 check that the original index expression value is both greater than
12876 or equal to the minimum value of the range and less than or equal to
12877 the maximum value of the range. */
12879 if (default_label)
12880 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12881 default_label, default_probability);
12883 /* If index is in range, it must fit in Pmode.
12884 Convert to Pmode so we can index with it. */
12885 if (mode != Pmode)
12887 unsigned int width;
12889 /* We know the value of INDEX is between 0 and RANGE. If we have a
12890 sign-extended subreg, and RANGE does not have the sign bit set, then
12891 we have a value that is valid for both sign and zero extension. In
12892 this case, we get better code if we sign extend. */
12893 if (GET_CODE (index) == SUBREG
12894 && SUBREG_PROMOTED_VAR_P (index)
12895 && SUBREG_PROMOTED_SIGNED_P (index)
12896 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
12897 <= HOST_BITS_PER_WIDE_INT)
12898 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
12899 index = convert_to_mode (Pmode, index, 0);
12900 else
12901 index = convert_to_mode (Pmode, index, 1);
12904 /* Don't let a MEM slip through, because then INDEX that comes
12905 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
12906 and break_out_memory_refs will go to work on it and mess it up. */
12907 #ifdef PIC_CASE_VECTOR_ADDRESS
12908 if (flag_pic && !REG_P (index))
12909 index = copy_to_mode_reg (Pmode, index);
12910 #endif
12912 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
12913 GET_MODE_SIZE, because this indicates how large insns are. The other
12914 uses should all be Pmode, because they are addresses. This code
12915 could fail if addresses and insns are not the same size. */
12916 index = simplify_gen_binary (MULT, Pmode, index,
12917 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
12918 Pmode));
12919 index = simplify_gen_binary (PLUS, Pmode, index,
12920 gen_rtx_LABEL_REF (Pmode, table_label));
12922 #ifdef PIC_CASE_VECTOR_ADDRESS
12923 if (flag_pic)
12924 index = PIC_CASE_VECTOR_ADDRESS (index);
12925 else
12926 #endif
12927 index = memory_address (CASE_VECTOR_MODE, index);
12928 temp = gen_reg_rtx (CASE_VECTOR_MODE);
12929 vector = gen_const_mem (CASE_VECTOR_MODE, index);
12930 convert_move (temp, vector, 0);
12932 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
12934 /* If we are generating PIC code or if the table is PC-relative, the
12935 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
12936 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
12937 emit_barrier ();
12941 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
12942 rtx table_label, rtx default_label,
12943 profile_probability default_probability)
12945 rtx index;
12947 if (! targetm.have_tablejump ())
12948 return 0;
12950 index_expr = fold_build2 (MINUS_EXPR, index_type,
12951 fold_convert (index_type, index_expr),
12952 fold_convert (index_type, minval));
12953 index = expand_normal (index_expr);
12954 do_pending_stack_adjust ();
12956 do_tablejump (index, TYPE_MODE (index_type),
12957 convert_modes (TYPE_MODE (index_type),
12958 TYPE_MODE (TREE_TYPE (range)),
12959 expand_normal (range),
12960 TYPE_UNSIGNED (TREE_TYPE (range))),
12961 table_label, default_label, default_probability);
12962 return 1;
12965 /* Return a CONST_VECTOR rtx representing vector mask for
12966 a VECTOR_CST of booleans. */
12967 static rtx
12968 const_vector_mask_from_tree (tree exp)
12970 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12971 machine_mode inner = GET_MODE_INNER (mode);
12973 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12974 VECTOR_CST_NELTS_PER_PATTERN (exp));
12975 unsigned int count = builder.encoded_nelts ();
12976 for (unsigned int i = 0; i < count; ++i)
12978 tree elt = VECTOR_CST_ELT (exp, i);
12979 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
12980 if (integer_zerop (elt))
12981 builder.quick_push (CONST0_RTX (inner));
12982 else if (integer_onep (elt)
12983 || integer_minus_onep (elt))
12984 builder.quick_push (CONSTM1_RTX (inner));
12985 else
12986 gcc_unreachable ();
12988 return builder.build ();
12991 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
12992 static rtx
12993 const_vector_from_tree (tree exp)
12995 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12997 if (initializer_zerop (exp))
12998 return CONST0_RTX (mode);
13000 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13001 return const_vector_mask_from_tree (exp);
13003 machine_mode inner = GET_MODE_INNER (mode);
13005 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13006 VECTOR_CST_NELTS_PER_PATTERN (exp));
13007 unsigned int count = builder.encoded_nelts ();
13008 for (unsigned int i = 0; i < count; ++i)
13010 tree elt = VECTOR_CST_ELT (exp, i);
13011 if (TREE_CODE (elt) == REAL_CST)
13012 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13013 inner));
13014 else if (TREE_CODE (elt) == FIXED_CST)
13015 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13016 inner));
13017 else
13018 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13019 inner));
13021 return builder.build ();
13024 /* Build a decl for a personality function given a language prefix. */
13026 tree
13027 build_personality_function (const char *lang)
13029 const char *unwind_and_version;
13030 tree decl, type;
13031 char *name;
13033 switch (targetm_common.except_unwind_info (&global_options))
13035 case UI_NONE:
13036 return NULL;
13037 case UI_SJLJ:
13038 unwind_and_version = "_sj0";
13039 break;
13040 case UI_DWARF2:
13041 case UI_TARGET:
13042 unwind_and_version = "_v0";
13043 break;
13044 case UI_SEH:
13045 unwind_and_version = "_seh0";
13046 break;
13047 default:
13048 gcc_unreachable ();
13051 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13053 type = build_function_type_list (unsigned_type_node,
13054 integer_type_node, integer_type_node,
13055 long_long_unsigned_type_node,
13056 ptr_type_node, ptr_type_node, NULL_TREE);
13057 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13058 get_identifier (name), type);
13059 DECL_ARTIFICIAL (decl) = 1;
13060 DECL_EXTERNAL (decl) = 1;
13061 TREE_PUBLIC (decl) = 1;
13063 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13064 are the flags assigned by targetm.encode_section_info. */
13065 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13067 return decl;
13070 /* Extracts the personality function of DECL and returns the corresponding
13071 libfunc. */
13074 get_personality_function (tree decl)
13076 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13077 enum eh_personality_kind pk;
13079 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13080 if (pk == eh_personality_none)
13081 return NULL;
13083 if (!personality
13084 && pk == eh_personality_any)
13085 personality = lang_hooks.eh_personality ();
13087 if (pk == eh_personality_lang)
13088 gcc_assert (personality != NULL_TREE);
13090 return XEXP (DECL_RTL (personality), 0);
13093 /* Returns a tree for the size of EXP in bytes. */
13095 static tree
13096 tree_expr_size (const_tree exp)
13098 if (DECL_P (exp)
13099 && DECL_SIZE_UNIT (exp) != 0)
13100 return DECL_SIZE_UNIT (exp);
13101 else
13102 return size_in_bytes (TREE_TYPE (exp));
13105 /* Return an rtx for the size in bytes of the value of EXP. */
13108 expr_size (tree exp)
13110 tree size;
13112 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13113 size = TREE_OPERAND (exp, 1);
13114 else
13116 size = tree_expr_size (exp);
13117 gcc_assert (size);
13118 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13121 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13124 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13125 if the size can vary or is larger than an integer. */
13127 static HOST_WIDE_INT
13128 int_expr_size (tree exp)
13130 tree size;
13132 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13133 size = TREE_OPERAND (exp, 1);
13134 else
13136 size = tree_expr_size (exp);
13137 gcc_assert (size);
13140 if (size == 0 || !tree_fits_shwi_p (size))
13141 return -1;
13143 return tree_to_shwi (size);