[RS6000] dg-do !compile and scan-assembler
[official-gcc.git] / gcc / expr.c
blobae16f07775870792729e3805436d7f2debafb6ca
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2020 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 "expmed.h"
33 #include "optabs.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"
67 /* If this is nonzero, we do not bother generating VOLATILE
68 around volatile memory references, and we are willing to
69 output indirect addresses. If cse is to follow, we reject
70 indirect addresses so a useful potential cse is generated;
71 if it is used only once, instruction combination will produce
72 the same indirect address eventually. */
73 int cse_not_expected;
75 static bool block_move_libcall_safe_for_call_parm (void);
76 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
77 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
78 unsigned HOST_WIDE_INT,
79 unsigned HOST_WIDE_INT, bool);
80 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
81 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
82 static rtx_insn *compress_float_constant (rtx, rtx);
83 static rtx get_subtarget (rtx);
84 static void store_constructor (tree, rtx, int, poly_int64, bool);
85 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
86 machine_mode, tree, alias_set_type, bool, bool);
88 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
90 static int is_aligning_offset (const_tree, const_tree);
91 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
92 static rtx do_store_flag (sepops, rtx, machine_mode);
93 #ifdef PUSH_ROUNDING
94 static void emit_single_push_insn (machine_mode, rtx, tree);
95 #endif
96 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
97 profile_probability);
98 static rtx const_vector_from_tree (tree);
99 static tree tree_expr_size (const_tree);
100 static HOST_WIDE_INT int_expr_size (tree);
101 static void convert_mode_scalar (rtx, rtx, int);
104 /* This is run to set up which modes can be used
105 directly in memory and to initialize the block move optab. It is run
106 at the beginning of compilation and when the target is reinitialized. */
108 void
109 init_expr_target (void)
111 rtx pat;
112 int num_clobbers;
113 rtx mem, mem1;
114 rtx reg;
116 /* Try indexing by frame ptr and try by stack ptr.
117 It is known that on the Convex the stack ptr isn't a valid index.
118 With luck, one or the other is valid on any machine. */
119 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
120 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
122 /* A scratch register we can modify in-place below to avoid
123 useless RTL allocations. */
124 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
126 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
127 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
128 PATTERN (insn) = pat;
130 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
131 mode = (machine_mode) ((int) mode + 1))
133 int regno;
135 direct_load[(int) mode] = direct_store[(int) mode] = 0;
136 PUT_MODE (mem, mode);
137 PUT_MODE (mem1, mode);
139 /* See if there is some register that can be used in this mode and
140 directly loaded or stored from memory. */
142 if (mode != VOIDmode && mode != BLKmode)
143 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
144 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
145 regno++)
147 if (!targetm.hard_regno_mode_ok (regno, mode))
148 continue;
150 set_mode_and_regno (reg, mode, regno);
152 SET_SRC (pat) = mem;
153 SET_DEST (pat) = reg;
154 if (recog (pat, insn, &num_clobbers) >= 0)
155 direct_load[(int) mode] = 1;
157 SET_SRC (pat) = mem1;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
162 SET_SRC (pat) = reg;
163 SET_DEST (pat) = mem;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_store[(int) mode] = 1;
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem1;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
174 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
176 opt_scalar_float_mode mode_iter;
177 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
179 scalar_float_mode mode = mode_iter.require ();
180 scalar_float_mode srcmode;
181 FOR_EACH_MODE_UNTIL (srcmode, mode)
183 enum insn_code ic;
185 ic = can_extend_p (mode, srcmode, 0);
186 if (ic == CODE_FOR_nothing)
187 continue;
189 PUT_MODE (mem, srcmode);
191 if (insn_operand_matches (ic, 1, mem))
192 float_extend_from_mem[mode][srcmode] = true;
197 /* This is run at the start of compiling a function. */
199 void
200 init_expr (void)
202 memset (&crtl->expr, 0, sizeof (crtl->expr));
205 /* Copy data from FROM to TO, where the machine modes are not the same.
206 Both modes may be integer, or both may be floating, or both may be
207 fixed-point.
208 UNSIGNEDP should be nonzero if FROM is an unsigned type.
209 This causes zero-extension instead of sign-extension. */
211 void
212 convert_move (rtx to, rtx from, int unsignedp)
214 machine_mode to_mode = GET_MODE (to);
215 machine_mode from_mode = GET_MODE (from);
217 gcc_assert (to_mode != BLKmode);
218 gcc_assert (from_mode != BLKmode);
220 /* If the source and destination are already the same, then there's
221 nothing to do. */
222 if (to == from)
223 return;
225 /* If FROM is a SUBREG that indicates that we have already done at least
226 the required extension, strip it. We don't handle such SUBREGs as
227 TO here. */
229 scalar_int_mode to_int_mode;
230 if (GET_CODE (from) == SUBREG
231 && SUBREG_PROMOTED_VAR_P (from)
232 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
233 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
234 >= GET_MODE_PRECISION (to_int_mode))
235 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
237 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
238 from_mode = to_int_mode;
241 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
243 if (to_mode == from_mode
244 || (from_mode == VOIDmode && CONSTANT_P (from)))
246 emit_move_insn (to, from);
247 return;
250 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
252 if (GET_MODE_UNIT_PRECISION (to_mode)
253 > GET_MODE_UNIT_PRECISION (from_mode))
255 optab op = unsignedp ? zext_optab : sext_optab;
256 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
257 if (icode != CODE_FOR_nothing)
259 emit_unop_insn (icode, to, from,
260 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
261 return;
265 if (GET_MODE_UNIT_PRECISION (to_mode)
266 < GET_MODE_UNIT_PRECISION (from_mode))
268 insn_code icode = convert_optab_handler (trunc_optab,
269 to_mode, from_mode);
270 if (icode != CODE_FOR_nothing)
272 emit_unop_insn (icode, to, from, TRUNCATE);
273 return;
277 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
278 GET_MODE_BITSIZE (to_mode)));
280 if (VECTOR_MODE_P (to_mode))
281 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
282 else
283 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
285 emit_move_insn (to, from);
286 return;
289 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
291 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
292 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
293 return;
296 convert_mode_scalar (to, from, unsignedp);
299 /* Like convert_move, but deals only with scalar modes. */
301 static void
302 convert_mode_scalar (rtx to, rtx from, int unsignedp)
304 /* Both modes should be scalar types. */
305 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
306 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
307 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
308 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
309 enum insn_code code;
310 rtx libcall;
312 gcc_assert (to_real == from_real);
314 /* rtx code for making an equivalent value. */
315 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
316 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
318 if (to_real)
320 rtx value;
321 rtx_insn *insns;
322 convert_optab tab;
324 gcc_assert ((GET_MODE_PRECISION (from_mode)
325 != GET_MODE_PRECISION (to_mode))
326 || (DECIMAL_FLOAT_MODE_P (from_mode)
327 != DECIMAL_FLOAT_MODE_P (to_mode)));
329 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
330 /* Conversion between decimal float and binary float, same size. */
331 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
332 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
333 tab = sext_optab;
334 else
335 tab = trunc_optab;
337 /* Try converting directly if the insn is supported. */
339 code = convert_optab_handler (tab, to_mode, from_mode);
340 if (code != CODE_FOR_nothing)
342 emit_unop_insn (code, to, from,
343 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
344 return;
347 /* Otherwise use a libcall. */
348 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
350 /* Is this conversion implemented yet? */
351 gcc_assert (libcall);
353 start_sequence ();
354 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
355 from, from_mode);
356 insns = get_insns ();
357 end_sequence ();
358 emit_libcall_block (insns, to, value,
359 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
360 from)
361 : gen_rtx_FLOAT_EXTEND (to_mode, from));
362 return;
365 /* Handle pointer conversion. */ /* SPEE 900220. */
366 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
368 convert_optab ctab;
370 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
371 ctab = trunc_optab;
372 else if (unsignedp)
373 ctab = zext_optab;
374 else
375 ctab = sext_optab;
377 if (convert_optab_handler (ctab, to_mode, from_mode)
378 != CODE_FOR_nothing)
380 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
381 to, from, UNKNOWN);
382 return;
386 /* Targets are expected to provide conversion insns between PxImode and
387 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
388 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
390 scalar_int_mode full_mode
391 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
393 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
394 != CODE_FOR_nothing);
396 if (full_mode != from_mode)
397 from = convert_to_mode (full_mode, from, unsignedp);
398 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
399 to, from, UNKNOWN);
400 return;
402 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
404 rtx new_from;
405 scalar_int_mode full_mode
406 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
407 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
408 enum insn_code icode;
410 icode = convert_optab_handler (ctab, full_mode, from_mode);
411 gcc_assert (icode != CODE_FOR_nothing);
413 if (to_mode == full_mode)
415 emit_unop_insn (icode, to, from, UNKNOWN);
416 return;
419 new_from = gen_reg_rtx (full_mode);
420 emit_unop_insn (icode, new_from, from, UNKNOWN);
422 /* else proceed to integer conversions below. */
423 from_mode = full_mode;
424 from = new_from;
427 /* Make sure both are fixed-point modes or both are not. */
428 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
429 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
430 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
432 /* If we widen from_mode to to_mode and they are in the same class,
433 we won't saturate the result.
434 Otherwise, always saturate the result to play safe. */
435 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
436 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
437 expand_fixed_convert (to, from, 0, 0);
438 else
439 expand_fixed_convert (to, from, 0, 1);
440 return;
443 /* Now both modes are integers. */
445 /* Handle expanding beyond a word. */
446 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
447 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
449 rtx_insn *insns;
450 rtx lowpart;
451 rtx fill_value;
452 rtx lowfrom;
453 int i;
454 scalar_mode lowpart_mode;
455 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
457 /* Try converting directly if the insn is supported. */
458 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
459 != CODE_FOR_nothing)
461 /* If FROM is a SUBREG, put it into a register. Do this
462 so that we always generate the same set of insns for
463 better cse'ing; if an intermediate assignment occurred,
464 we won't be doing the operation directly on the SUBREG. */
465 if (optimize > 0 && GET_CODE (from) == SUBREG)
466 from = force_reg (from_mode, from);
467 emit_unop_insn (code, to, from, equiv_code);
468 return;
470 /* Next, try converting via full word. */
471 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
472 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
473 != CODE_FOR_nothing))
475 rtx word_to = gen_reg_rtx (word_mode);
476 if (REG_P (to))
478 if (reg_overlap_mentioned_p (to, from))
479 from = force_reg (from_mode, from);
480 emit_clobber (to);
482 convert_move (word_to, from, unsignedp);
483 emit_unop_insn (code, to, word_to, equiv_code);
484 return;
487 /* No special multiword conversion insn; do it by hand. */
488 start_sequence ();
490 /* Since we will turn this into a no conflict block, we must ensure
491 the source does not overlap the target so force it into an isolated
492 register when maybe so. Likewise for any MEM input, since the
493 conversion sequence might require several references to it and we
494 must ensure we're getting the same value every time. */
496 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
497 from = force_reg (from_mode, from);
499 /* Get a copy of FROM widened to a word, if necessary. */
500 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
501 lowpart_mode = word_mode;
502 else
503 lowpart_mode = from_mode;
505 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
507 lowpart = gen_lowpart (lowpart_mode, to);
508 emit_move_insn (lowpart, lowfrom);
510 /* Compute the value to put in each remaining word. */
511 if (unsignedp)
512 fill_value = const0_rtx;
513 else
514 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
515 LT, lowfrom, const0_rtx,
516 lowpart_mode, 0, -1);
518 /* Fill the remaining words. */
519 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
521 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
522 rtx subword = operand_subword (to, index, 1, to_mode);
524 gcc_assert (subword);
526 if (fill_value != subword)
527 emit_move_insn (subword, fill_value);
530 insns = get_insns ();
531 end_sequence ();
533 emit_insn (insns);
534 return;
537 /* Truncating multi-word to a word or less. */
538 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
539 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
541 if (!((MEM_P (from)
542 && ! MEM_VOLATILE_P (from)
543 && direct_load[(int) to_mode]
544 && ! mode_dependent_address_p (XEXP (from, 0),
545 MEM_ADDR_SPACE (from)))
546 || REG_P (from)
547 || GET_CODE (from) == SUBREG))
548 from = force_reg (from_mode, from);
549 convert_move (to, gen_lowpart (word_mode, from), 0);
550 return;
553 /* Now follow all the conversions between integers
554 no more than a word long. */
556 /* For truncation, usually we can just refer to FROM in a narrower mode. */
557 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
558 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
560 if (!((MEM_P (from)
561 && ! MEM_VOLATILE_P (from)
562 && direct_load[(int) to_mode]
563 && ! mode_dependent_address_p (XEXP (from, 0),
564 MEM_ADDR_SPACE (from)))
565 || REG_P (from)
566 || GET_CODE (from) == SUBREG))
567 from = force_reg (from_mode, from);
568 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
569 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
570 from = copy_to_reg (from);
571 emit_move_insn (to, gen_lowpart (to_mode, from));
572 return;
575 /* Handle extension. */
576 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
578 /* Convert directly if that works. */
579 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
580 != CODE_FOR_nothing)
582 emit_unop_insn (code, to, from, equiv_code);
583 return;
585 else
587 rtx tmp;
588 int shift_amount;
590 /* Search for a mode to convert via. */
591 opt_scalar_mode intermediate_iter;
592 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
594 scalar_mode intermediate = intermediate_iter.require ();
595 if (((can_extend_p (to_mode, intermediate, unsignedp)
596 != CODE_FOR_nothing)
597 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
598 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
599 intermediate)))
600 && (can_extend_p (intermediate, from_mode, unsignedp)
601 != CODE_FOR_nothing))
603 convert_move (to, convert_to_mode (intermediate, from,
604 unsignedp), unsignedp);
605 return;
609 /* No suitable intermediate mode.
610 Generate what we need with shifts. */
611 shift_amount = (GET_MODE_PRECISION (to_mode)
612 - GET_MODE_PRECISION (from_mode));
613 from = gen_lowpart (to_mode, force_reg (from_mode, from));
614 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
615 to, unsignedp);
616 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
617 to, unsignedp);
618 if (tmp != to)
619 emit_move_insn (to, tmp);
620 return;
624 /* Support special truncate insns for certain modes. */
625 if (convert_optab_handler (trunc_optab, to_mode,
626 from_mode) != CODE_FOR_nothing)
628 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
629 to, from, UNKNOWN);
630 return;
633 /* Handle truncation of volatile memrefs, and so on;
634 the things that couldn't be truncated directly,
635 and for which there was no special instruction.
637 ??? Code above formerly short-circuited this, for most integer
638 mode pairs, with a force_reg in from_mode followed by a recursive
639 call to this routine. Appears always to have been wrong. */
640 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
642 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
643 emit_move_insn (to, temp);
644 return;
647 /* Mode combination is not recognized. */
648 gcc_unreachable ();
651 /* Return an rtx for a value that would result
652 from converting X to mode MODE.
653 Both X and MODE may be floating, or both integer.
654 UNSIGNEDP is nonzero if X is an unsigned value.
655 This can be done by referring to a part of X in place
656 or by copying to a new temporary with conversion. */
659 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
661 return convert_modes (mode, VOIDmode, x, unsignedp);
664 /* Return an rtx for a value that would result
665 from converting X from mode OLDMODE to mode MODE.
666 Both modes may be floating, or both integer.
667 UNSIGNEDP is nonzero if X is an unsigned value.
669 This can be done by referring to a part of X in place
670 or by copying to a new temporary with conversion.
672 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
675 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
677 rtx temp;
678 scalar_int_mode int_mode;
680 /* If FROM is a SUBREG that indicates that we have already done at least
681 the required extension, strip it. */
683 if (GET_CODE (x) == SUBREG
684 && SUBREG_PROMOTED_VAR_P (x)
685 && is_a <scalar_int_mode> (mode, &int_mode)
686 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
687 >= GET_MODE_PRECISION (int_mode))
688 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
689 x = gen_lowpart (int_mode, SUBREG_REG (x));
691 if (GET_MODE (x) != VOIDmode)
692 oldmode = GET_MODE (x);
694 if (mode == oldmode)
695 return x;
697 if (CONST_SCALAR_INT_P (x)
698 && is_a <scalar_int_mode> (mode, &int_mode))
700 /* If the caller did not tell us the old mode, then there is not
701 much to do with respect to canonicalization. We have to
702 assume that all the bits are significant. */
703 if (!is_a <scalar_int_mode> (oldmode))
704 oldmode = MAX_MODE_INT;
705 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
706 GET_MODE_PRECISION (int_mode),
707 unsignedp ? UNSIGNED : SIGNED);
708 return immed_wide_int_const (w, int_mode);
711 /* We can do this with a gen_lowpart if both desired and current modes
712 are integer, and this is either a constant integer, a register, or a
713 non-volatile MEM. */
714 scalar_int_mode int_oldmode;
715 if (is_int_mode (mode, &int_mode)
716 && is_int_mode (oldmode, &int_oldmode)
717 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
718 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
719 || CONST_POLY_INT_P (x)
720 || (REG_P (x)
721 && (!HARD_REGISTER_P (x)
722 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
723 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
724 return gen_lowpart (int_mode, x);
726 /* Converting from integer constant into mode is always equivalent to an
727 subreg operation. */
728 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
730 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
731 GET_MODE_BITSIZE (oldmode)));
732 return simplify_gen_subreg (mode, x, oldmode, 0);
735 temp = gen_reg_rtx (mode);
736 convert_move (temp, x, unsignedp);
737 return temp;
740 /* Return the largest alignment we can use for doing a move (or store)
741 of MAX_PIECES. ALIGN is the largest alignment we could use. */
743 static unsigned int
744 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
746 scalar_int_mode tmode
747 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
749 if (align >= GET_MODE_ALIGNMENT (tmode))
750 align = GET_MODE_ALIGNMENT (tmode);
751 else
753 scalar_int_mode xmode = NARROWEST_INT_MODE;
754 opt_scalar_int_mode mode_iter;
755 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
757 tmode = mode_iter.require ();
758 if (GET_MODE_SIZE (tmode) > max_pieces
759 || targetm.slow_unaligned_access (tmode, align))
760 break;
761 xmode = tmode;
764 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
767 return align;
770 /* Return the widest integer mode that is narrower than SIZE bytes. */
772 static scalar_int_mode
773 widest_int_mode_for_size (unsigned int size)
775 scalar_int_mode result = NARROWEST_INT_MODE;
777 gcc_checking_assert (size > 1);
779 opt_scalar_int_mode tmode;
780 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
781 if (GET_MODE_SIZE (tmode.require ()) < size)
782 result = tmode.require ();
784 return result;
787 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
788 and should be performed piecewise. */
790 static bool
791 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
792 enum by_pieces_operation op)
794 return targetm.use_by_pieces_infrastructure_p (len, align, op,
795 optimize_insn_for_speed_p ());
798 /* Determine whether the LEN bytes can be moved by using several move
799 instructions. Return nonzero if a call to move_by_pieces should
800 succeed. */
802 bool
803 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
805 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
808 /* Return number of insns required to perform operation OP by pieces
809 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
811 unsigned HOST_WIDE_INT
812 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
813 unsigned int max_size, by_pieces_operation op)
815 unsigned HOST_WIDE_INT n_insns = 0;
817 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
819 while (max_size > 1 && l > 0)
821 scalar_int_mode mode = widest_int_mode_for_size (max_size);
822 enum insn_code icode;
824 unsigned int modesize = GET_MODE_SIZE (mode);
826 icode = optab_handler (mov_optab, mode);
827 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
829 unsigned HOST_WIDE_INT n_pieces = l / modesize;
830 l %= modesize;
831 switch (op)
833 default:
834 n_insns += n_pieces;
835 break;
837 case COMPARE_BY_PIECES:
838 int batch = targetm.compare_by_pieces_branch_ratio (mode);
839 int batch_ops = 4 * batch - 1;
840 unsigned HOST_WIDE_INT full = n_pieces / batch;
841 n_insns += full * batch_ops;
842 if (n_pieces % batch != 0)
843 n_insns++;
844 break;
848 max_size = modesize;
851 gcc_assert (!l);
852 return n_insns;
855 /* Used when performing piecewise block operations, holds information
856 about one of the memory objects involved. The member functions
857 can be used to generate code for loading from the object and
858 updating the address when iterating. */
860 class pieces_addr
862 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
863 stack pushes. */
864 rtx m_obj;
865 /* The address of the object. Can differ from that seen in the
866 MEM rtx if we copied the address to a register. */
867 rtx m_addr;
868 /* Nonzero if the address on the object has an autoincrement already,
869 signifies whether that was an increment or decrement. */
870 signed char m_addr_inc;
871 /* Nonzero if we intend to use autoinc without the address already
872 having autoinc form. We will insert add insns around each memory
873 reference, expecting later passes to form autoinc addressing modes.
874 The only supported options are predecrement and postincrement. */
875 signed char m_explicit_inc;
876 /* True if we have either of the two possible cases of using
877 autoincrement. */
878 bool m_auto;
879 /* True if this is an address to be used for load operations rather
880 than stores. */
881 bool m_is_load;
883 /* Optionally, a function to obtain constants for any given offset into
884 the objects, and data associated with it. */
885 by_pieces_constfn m_constfn;
886 void *m_cfndata;
887 public:
888 pieces_addr (rtx, bool, by_pieces_constfn, void *);
889 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
890 void increment_address (HOST_WIDE_INT);
891 void maybe_predec (HOST_WIDE_INT);
892 void maybe_postinc (HOST_WIDE_INT);
893 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
894 int get_addr_inc ()
896 return m_addr_inc;
900 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
901 true if the operation to be performed on this object is a load
902 rather than a store. For stores, OBJ can be NULL, in which case we
903 assume the operation is a stack push. For loads, the optional
904 CONSTFN and its associated CFNDATA can be used in place of the
905 memory load. */
907 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
908 void *cfndata)
909 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
911 m_addr_inc = 0;
912 m_auto = false;
913 if (obj)
915 rtx addr = XEXP (obj, 0);
916 rtx_code code = GET_CODE (addr);
917 m_addr = addr;
918 bool dec = code == PRE_DEC || code == POST_DEC;
919 bool inc = code == PRE_INC || code == POST_INC;
920 m_auto = inc || dec;
921 if (m_auto)
922 m_addr_inc = dec ? -1 : 1;
924 /* While we have always looked for these codes here, the code
925 implementing the memory operation has never handled them.
926 Support could be added later if necessary or beneficial. */
927 gcc_assert (code != PRE_INC && code != POST_DEC);
929 else
931 m_addr = NULL_RTX;
932 if (!is_load)
934 m_auto = true;
935 if (STACK_GROWS_DOWNWARD)
936 m_addr_inc = -1;
937 else
938 m_addr_inc = 1;
940 else
941 gcc_assert (constfn != NULL);
943 m_explicit_inc = 0;
944 if (constfn)
945 gcc_assert (is_load);
948 /* Decide whether to use autoinc for an address involved in a memory op.
949 MODE is the mode of the accesses, REVERSE is true if we've decided to
950 perform the operation starting from the end, and LEN is the length of
951 the operation. Don't override an earlier decision to set m_auto. */
953 void
954 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
955 HOST_WIDE_INT len)
957 if (m_auto || m_obj == NULL_RTX)
958 return;
960 bool use_predec = (m_is_load
961 ? USE_LOAD_PRE_DECREMENT (mode)
962 : USE_STORE_PRE_DECREMENT (mode));
963 bool use_postinc = (m_is_load
964 ? USE_LOAD_POST_INCREMENT (mode)
965 : USE_STORE_POST_INCREMENT (mode));
966 machine_mode addr_mode = get_address_mode (m_obj);
968 if (use_predec && reverse)
970 m_addr = copy_to_mode_reg (addr_mode,
971 plus_constant (addr_mode,
972 m_addr, len));
973 m_auto = true;
974 m_explicit_inc = -1;
976 else if (use_postinc && !reverse)
978 m_addr = copy_to_mode_reg (addr_mode, m_addr);
979 m_auto = true;
980 m_explicit_inc = 1;
982 else if (CONSTANT_P (m_addr))
983 m_addr = copy_to_mode_reg (addr_mode, m_addr);
986 /* Adjust the address to refer to the data at OFFSET in MODE. If we
987 are using autoincrement for this address, we don't add the offset,
988 but we still modify the MEM's properties. */
991 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
993 if (m_constfn)
994 return m_constfn (m_cfndata, offset, mode);
995 if (m_obj == NULL_RTX)
996 return NULL_RTX;
997 if (m_auto)
998 return adjust_automodify_address (m_obj, mode, m_addr, offset);
999 else
1000 return adjust_address (m_obj, mode, offset);
1003 /* Emit an add instruction to increment the address by SIZE. */
1005 void
1006 pieces_addr::increment_address (HOST_WIDE_INT size)
1008 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1009 emit_insn (gen_add2_insn (m_addr, amount));
1012 /* If we are supposed to decrement the address after each access, emit code
1013 to do so now. Increment by SIZE (which has should have the correct sign
1014 already). */
1016 void
1017 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1019 if (m_explicit_inc >= 0)
1020 return;
1021 gcc_assert (HAVE_PRE_DECREMENT);
1022 increment_address (size);
1025 /* If we are supposed to decrement the address after each access, emit code
1026 to do so now. Increment by SIZE. */
1028 void
1029 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1031 if (m_explicit_inc <= 0)
1032 return;
1033 gcc_assert (HAVE_POST_INCREMENT);
1034 increment_address (size);
1037 /* This structure is used by do_op_by_pieces to describe the operation
1038 to be performed. */
1040 class op_by_pieces_d
1042 protected:
1043 pieces_addr m_to, m_from;
1044 unsigned HOST_WIDE_INT m_len;
1045 HOST_WIDE_INT m_offset;
1046 unsigned int m_align;
1047 unsigned int m_max_size;
1048 bool m_reverse;
1050 /* Virtual functions, overriden by derived classes for the specific
1051 operation. */
1052 virtual void generate (rtx, rtx, machine_mode) = 0;
1053 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1054 virtual void finish_mode (machine_mode)
1058 public:
1059 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1060 unsigned HOST_WIDE_INT, unsigned int);
1061 void run ();
1064 /* The constructor for an op_by_pieces_d structure. We require two
1065 objects named TO and FROM, which are identified as loads or stores
1066 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1067 and its associated FROM_CFN_DATA can be used to replace loads with
1068 constant values. LEN describes the length of the operation. */
1070 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1071 rtx from, bool from_load,
1072 by_pieces_constfn from_cfn,
1073 void *from_cfn_data,
1074 unsigned HOST_WIDE_INT len,
1075 unsigned int align)
1076 : m_to (to, to_load, NULL, NULL),
1077 m_from (from, from_load, from_cfn, from_cfn_data),
1078 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1080 int toi = m_to.get_addr_inc ();
1081 int fromi = m_from.get_addr_inc ();
1082 if (toi >= 0 && fromi >= 0)
1083 m_reverse = false;
1084 else if (toi <= 0 && fromi <= 0)
1085 m_reverse = true;
1086 else
1087 gcc_unreachable ();
1089 m_offset = m_reverse ? len : 0;
1090 align = MIN (to ? MEM_ALIGN (to) : align,
1091 from ? MEM_ALIGN (from) : align);
1093 /* If copying requires more than two move insns,
1094 copy addresses to registers (to make displacements shorter)
1095 and use post-increment if available. */
1096 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1098 /* Find the mode of the largest comparison. */
1099 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1101 m_from.decide_autoinc (mode, m_reverse, len);
1102 m_to.decide_autoinc (mode, m_reverse, len);
1105 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1106 m_align = align;
1109 /* This function contains the main loop used for expanding a block
1110 operation. First move what we can in the largest integer mode,
1111 then go to successively smaller modes. For every access, call
1112 GENFUN with the two operands and the EXTRA_DATA. */
1114 void
1115 op_by_pieces_d::run ()
1117 while (m_max_size > 1 && m_len > 0)
1119 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1121 if (prepare_mode (mode, m_align))
1123 unsigned int size = GET_MODE_SIZE (mode);
1124 rtx to1 = NULL_RTX, from1;
1126 while (m_len >= size)
1128 if (m_reverse)
1129 m_offset -= size;
1131 to1 = m_to.adjust (mode, m_offset);
1132 from1 = m_from.adjust (mode, m_offset);
1134 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1135 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1137 generate (to1, from1, mode);
1139 m_to.maybe_postinc (size);
1140 m_from.maybe_postinc (size);
1142 if (!m_reverse)
1143 m_offset += size;
1145 m_len -= size;
1148 finish_mode (mode);
1151 m_max_size = GET_MODE_SIZE (mode);
1154 /* The code above should have handled everything. */
1155 gcc_assert (!m_len);
1158 /* Derived class from op_by_pieces_d, providing support for block move
1159 operations. */
1161 class move_by_pieces_d : public op_by_pieces_d
1163 insn_gen_fn m_gen_fun;
1164 void generate (rtx, rtx, machine_mode);
1165 bool prepare_mode (machine_mode, unsigned int);
1167 public:
1168 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1169 unsigned int align)
1170 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1173 rtx finish_retmode (memop_ret);
1176 /* Return true if MODE can be used for a set of copies, given an
1177 alignment ALIGN. Prepare whatever data is necessary for later
1178 calls to generate. */
1180 bool
1181 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1183 insn_code icode = optab_handler (mov_optab, mode);
1184 m_gen_fun = GEN_FCN (icode);
1185 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1188 /* A callback used when iterating for a compare_by_pieces_operation.
1189 OP0 and OP1 are the values that have been loaded and should be
1190 compared in MODE. If OP0 is NULL, this means we should generate a
1191 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1192 gen function that should be used to generate the mode. */
1194 void
1195 move_by_pieces_d::generate (rtx op0, rtx op1,
1196 machine_mode mode ATTRIBUTE_UNUSED)
1198 #ifdef PUSH_ROUNDING
1199 if (op0 == NULL_RTX)
1201 emit_single_push_insn (mode, op1, NULL);
1202 return;
1204 #endif
1205 emit_insn (m_gen_fun (op0, op1));
1208 /* Perform the final adjustment at the end of a string to obtain the
1209 correct return value for the block operation.
1210 Return value is based on RETMODE argument. */
1213 move_by_pieces_d::finish_retmode (memop_ret retmode)
1215 gcc_assert (!m_reverse);
1216 if (retmode == RETURN_END_MINUS_ONE)
1218 m_to.maybe_postinc (-1);
1219 --m_offset;
1221 return m_to.adjust (QImode, m_offset);
1224 /* Generate several move instructions to copy LEN bytes from block FROM to
1225 block TO. (These are MEM rtx's with BLKmode).
1227 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1228 used to push FROM to the stack.
1230 ALIGN is maximum stack alignment we can assume.
1232 Return value is based on RETMODE argument. */
1235 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1236 unsigned int align, memop_ret retmode)
1238 #ifndef PUSH_ROUNDING
1239 if (to == NULL)
1240 gcc_unreachable ();
1241 #endif
1243 move_by_pieces_d data (to, from, len, align);
1245 data.run ();
1247 if (retmode != RETURN_BEGIN)
1248 return data.finish_retmode (retmode);
1249 else
1250 return to;
1253 /* Derived class from op_by_pieces_d, providing support for block move
1254 operations. */
1256 class store_by_pieces_d : public op_by_pieces_d
1258 insn_gen_fn m_gen_fun;
1259 void generate (rtx, rtx, machine_mode);
1260 bool prepare_mode (machine_mode, unsigned int);
1262 public:
1263 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1264 unsigned HOST_WIDE_INT len, unsigned int align)
1265 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1268 rtx finish_retmode (memop_ret);
1271 /* Return true if MODE can be used for a set of stores, given an
1272 alignment ALIGN. Prepare whatever data is necessary for later
1273 calls to generate. */
1275 bool
1276 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1278 insn_code icode = optab_handler (mov_optab, mode);
1279 m_gen_fun = GEN_FCN (icode);
1280 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1283 /* A callback used when iterating for a store_by_pieces_operation.
1284 OP0 and OP1 are the values that have been loaded and should be
1285 compared in MODE. If OP0 is NULL, this means we should generate a
1286 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1287 gen function that should be used to generate the mode. */
1289 void
1290 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1292 emit_insn (m_gen_fun (op0, op1));
1295 /* Perform the final adjustment at the end of a string to obtain the
1296 correct return value for the block operation.
1297 Return value is based on RETMODE argument. */
1300 store_by_pieces_d::finish_retmode (memop_ret retmode)
1302 gcc_assert (!m_reverse);
1303 if (retmode == RETURN_END_MINUS_ONE)
1305 m_to.maybe_postinc (-1);
1306 --m_offset;
1308 return m_to.adjust (QImode, m_offset);
1311 /* Determine whether the LEN bytes generated by CONSTFUN can be
1312 stored to memory using several move instructions. CONSTFUNDATA is
1313 a pointer which will be passed as argument in every CONSTFUN call.
1314 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1315 a memset operation and false if it's a copy of a constant string.
1316 Return nonzero if a call to store_by_pieces should succeed. */
1319 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1320 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1321 void *constfundata, unsigned int align, bool memsetp)
1323 unsigned HOST_WIDE_INT l;
1324 unsigned int max_size;
1325 HOST_WIDE_INT offset = 0;
1326 enum insn_code icode;
1327 int reverse;
1328 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1329 rtx cst ATTRIBUTE_UNUSED;
1331 if (len == 0)
1332 return 1;
1334 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1335 memsetp
1336 ? SET_BY_PIECES
1337 : STORE_BY_PIECES,
1338 optimize_insn_for_speed_p ()))
1339 return 0;
1341 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1343 /* We would first store what we can in the largest integer mode, then go to
1344 successively smaller modes. */
1346 for (reverse = 0;
1347 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1348 reverse++)
1350 l = len;
1351 max_size = STORE_MAX_PIECES + 1;
1352 while (max_size > 1 && l > 0)
1354 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1356 icode = optab_handler (mov_optab, mode);
1357 if (icode != CODE_FOR_nothing
1358 && align >= GET_MODE_ALIGNMENT (mode))
1360 unsigned int size = GET_MODE_SIZE (mode);
1362 while (l >= size)
1364 if (reverse)
1365 offset -= size;
1367 cst = (*constfun) (constfundata, offset, mode);
1368 if (!targetm.legitimate_constant_p (mode, cst))
1369 return 0;
1371 if (!reverse)
1372 offset += size;
1374 l -= size;
1378 max_size = GET_MODE_SIZE (mode);
1381 /* The code above should have handled everything. */
1382 gcc_assert (!l);
1385 return 1;
1388 /* Generate several move instructions to store LEN bytes generated by
1389 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1390 pointer which will be passed as argument in every CONSTFUN call.
1391 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1392 a memset operation and false if it's a copy of a constant string.
1393 Return value is based on RETMODE argument. */
1396 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1397 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1398 void *constfundata, unsigned int align, bool memsetp,
1399 memop_ret retmode)
1401 if (len == 0)
1403 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1404 return to;
1407 gcc_assert (targetm.use_by_pieces_infrastructure_p
1408 (len, align,
1409 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1410 optimize_insn_for_speed_p ()));
1412 store_by_pieces_d data (to, constfun, constfundata, len, align);
1413 data.run ();
1415 if (retmode != RETURN_BEGIN)
1416 return data.finish_retmode (retmode);
1417 else
1418 return to;
1421 /* Callback routine for clear_by_pieces.
1422 Return const0_rtx unconditionally. */
1424 static rtx
1425 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1427 return const0_rtx;
1430 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1431 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1433 static void
1434 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1436 if (len == 0)
1437 return;
1439 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1440 data.run ();
1443 /* Context used by compare_by_pieces_genfn. It stores the fail label
1444 to jump to in case of miscomparison, and for branch ratios greater than 1,
1445 it stores an accumulator and the current and maximum counts before
1446 emitting another branch. */
1448 class compare_by_pieces_d : public op_by_pieces_d
1450 rtx_code_label *m_fail_label;
1451 rtx m_accumulator;
1452 int m_count, m_batch;
1454 void generate (rtx, rtx, machine_mode);
1455 bool prepare_mode (machine_mode, unsigned int);
1456 void finish_mode (machine_mode);
1457 public:
1458 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1459 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1460 rtx_code_label *fail_label)
1461 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1463 m_fail_label = fail_label;
1467 /* A callback used when iterating for a compare_by_pieces_operation.
1468 OP0 and OP1 are the values that have been loaded and should be
1469 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1470 context structure. */
1472 void
1473 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1475 if (m_batch > 1)
1477 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1478 true, OPTAB_LIB_WIDEN);
1479 if (m_count != 0)
1480 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1481 true, OPTAB_LIB_WIDEN);
1482 m_accumulator = temp;
1484 if (++m_count < m_batch)
1485 return;
1487 m_count = 0;
1488 op0 = m_accumulator;
1489 op1 = const0_rtx;
1490 m_accumulator = NULL_RTX;
1492 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1493 m_fail_label, profile_probability::uninitialized ());
1496 /* Return true if MODE can be used for a set of moves and comparisons,
1497 given an alignment ALIGN. Prepare whatever data is necessary for
1498 later calls to generate. */
1500 bool
1501 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1503 insn_code icode = optab_handler (mov_optab, mode);
1504 if (icode == CODE_FOR_nothing
1505 || align < GET_MODE_ALIGNMENT (mode)
1506 || !can_compare_p (EQ, mode, ccp_jump))
1507 return false;
1508 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1509 if (m_batch < 0)
1510 return false;
1511 m_accumulator = NULL_RTX;
1512 m_count = 0;
1513 return true;
1516 /* Called after expanding a series of comparisons in MODE. If we have
1517 accumulated results for which we haven't emitted a branch yet, do
1518 so now. */
1520 void
1521 compare_by_pieces_d::finish_mode (machine_mode mode)
1523 if (m_accumulator != NULL_RTX)
1524 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1525 NULL_RTX, NULL, m_fail_label,
1526 profile_probability::uninitialized ());
1529 /* Generate several move instructions to compare LEN bytes from blocks
1530 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1532 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1533 used to push FROM to the stack.
1535 ALIGN is maximum stack alignment we can assume.
1537 Optionally, the caller can pass a constfn and associated data in A1_CFN
1538 and A1_CFN_DATA. describing that the second operand being compared is a
1539 known constant and how to obtain its data. */
1541 static rtx
1542 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1543 rtx target, unsigned int align,
1544 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1546 rtx_code_label *fail_label = gen_label_rtx ();
1547 rtx_code_label *end_label = gen_label_rtx ();
1549 if (target == NULL_RTX
1550 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1551 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1553 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1554 fail_label);
1556 data.run ();
1558 emit_move_insn (target, const0_rtx);
1559 emit_jump (end_label);
1560 emit_barrier ();
1561 emit_label (fail_label);
1562 emit_move_insn (target, const1_rtx);
1563 emit_label (end_label);
1565 return target;
1568 /* Emit code to move a block Y to a block X. This may be done with
1569 string-move instructions, with multiple scalar move instructions,
1570 or with a library call.
1572 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1573 SIZE is an rtx that says how long they are.
1574 ALIGN is the maximum alignment we can assume they have.
1575 METHOD describes what kind of copy this is, and what mechanisms may be used.
1576 MIN_SIZE is the minimal size of block to move
1577 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1578 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1580 Return the address of the new block, if memcpy is called and returns it,
1581 0 otherwise. */
1584 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1585 unsigned int expected_align, HOST_WIDE_INT expected_size,
1586 unsigned HOST_WIDE_INT min_size,
1587 unsigned HOST_WIDE_INT max_size,
1588 unsigned HOST_WIDE_INT probable_max_size,
1589 bool bail_out_libcall, bool *is_move_done,
1590 bool might_overlap)
1592 int may_use_call;
1593 rtx retval = 0;
1594 unsigned int align;
1596 if (is_move_done)
1597 *is_move_done = true;
1599 gcc_assert (size);
1600 if (CONST_INT_P (size) && INTVAL (size) == 0)
1601 return 0;
1603 switch (method)
1605 case BLOCK_OP_NORMAL:
1606 case BLOCK_OP_TAILCALL:
1607 may_use_call = 1;
1608 break;
1610 case BLOCK_OP_CALL_PARM:
1611 may_use_call = block_move_libcall_safe_for_call_parm ();
1613 /* Make inhibit_defer_pop nonzero around the library call
1614 to force it to pop the arguments right away. */
1615 NO_DEFER_POP;
1616 break;
1618 case BLOCK_OP_NO_LIBCALL:
1619 may_use_call = 0;
1620 break;
1622 case BLOCK_OP_NO_LIBCALL_RET:
1623 may_use_call = -1;
1624 break;
1626 default:
1627 gcc_unreachable ();
1630 gcc_assert (MEM_P (x) && MEM_P (y));
1631 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1632 gcc_assert (align >= BITS_PER_UNIT);
1634 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1635 block copy is more efficient for other large modes, e.g. DCmode. */
1636 x = adjust_address (x, BLKmode, 0);
1637 y = adjust_address (y, BLKmode, 0);
1639 /* If source and destination are the same, no need to copy anything. */
1640 if (rtx_equal_p (x, y)
1641 && !MEM_VOLATILE_P (x)
1642 && !MEM_VOLATILE_P (y))
1643 return 0;
1645 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1646 can be incorrect is coming from __builtin_memcpy. */
1647 poly_int64 const_size;
1648 if (poly_int_rtx_p (size, &const_size))
1650 x = shallow_copy_rtx (x);
1651 y = shallow_copy_rtx (y);
1652 set_mem_size (x, const_size);
1653 set_mem_size (y, const_size);
1656 bool pieces_ok = CONST_INT_P (size)
1657 && can_move_by_pieces (INTVAL (size), align);
1658 bool pattern_ok = false;
1660 if (!pieces_ok || might_overlap)
1662 pattern_ok
1663 = emit_block_move_via_pattern (x, y, size, align,
1664 expected_align, expected_size,
1665 min_size, max_size, probable_max_size,
1666 might_overlap);
1667 if (!pattern_ok && might_overlap)
1669 /* Do not try any of the other methods below as they are not safe
1670 for overlapping moves. */
1671 *is_move_done = false;
1672 return retval;
1676 if (pattern_ok)
1678 else if (pieces_ok)
1679 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
1680 else if (may_use_call && !might_overlap
1681 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1682 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1684 if (bail_out_libcall)
1686 if (is_move_done)
1687 *is_move_done = false;
1688 return retval;
1691 if (may_use_call < 0)
1692 return pc_rtx;
1694 retval = emit_block_copy_via_libcall (x, y, size,
1695 method == BLOCK_OP_TAILCALL);
1697 else if (might_overlap)
1698 *is_move_done = false;
1699 else
1700 emit_block_move_via_loop (x, y, size, align);
1702 if (method == BLOCK_OP_CALL_PARM)
1703 OK_DEFER_POP;
1705 return retval;
1709 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1711 unsigned HOST_WIDE_INT max, min = 0;
1712 if (GET_CODE (size) == CONST_INT)
1713 min = max = UINTVAL (size);
1714 else
1715 max = GET_MODE_MASK (GET_MODE (size));
1716 return emit_block_move_hints (x, y, size, method, 0, -1,
1717 min, max, max);
1720 /* A subroutine of emit_block_move. Returns true if calling the
1721 block move libcall will not clobber any parameters which may have
1722 already been placed on the stack. */
1724 static bool
1725 block_move_libcall_safe_for_call_parm (void)
1727 tree fn;
1729 /* If arguments are pushed on the stack, then they're safe. */
1730 if (PUSH_ARGS)
1731 return true;
1733 /* If registers go on the stack anyway, any argument is sure to clobber
1734 an outgoing argument. */
1735 #if defined (REG_PARM_STACK_SPACE)
1736 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1737 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1738 depend on its argument. */
1739 (void) fn;
1740 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1741 && REG_PARM_STACK_SPACE (fn) != 0)
1742 return false;
1743 #endif
1745 /* If any argument goes in memory, then it might clobber an outgoing
1746 argument. */
1748 CUMULATIVE_ARGS args_so_far_v;
1749 cumulative_args_t args_so_far;
1750 tree arg;
1752 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1753 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1754 args_so_far = pack_cumulative_args (&args_so_far_v);
1756 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1757 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1759 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1760 function_arg_info arg_info (mode, /*named=*/true);
1761 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
1762 if (!tmp || !REG_P (tmp))
1763 return false;
1764 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
1765 return false;
1766 targetm.calls.function_arg_advance (args_so_far, arg_info);
1769 return true;
1772 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
1773 return true if successful.
1775 X is the destination of the copy or move.
1776 Y is the source of the copy or move.
1777 SIZE is the size of the block to be moved.
1779 MIGHT_OVERLAP indicates this originated with expansion of a
1780 builtin_memmove() and the source and destination blocks may
1781 overlap.
1784 static bool
1785 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
1786 unsigned int expected_align,
1787 HOST_WIDE_INT expected_size,
1788 unsigned HOST_WIDE_INT min_size,
1789 unsigned HOST_WIDE_INT max_size,
1790 unsigned HOST_WIDE_INT probable_max_size,
1791 bool might_overlap)
1793 if (expected_align < align)
1794 expected_align = align;
1795 if (expected_size != -1)
1797 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1798 expected_size = probable_max_size;
1799 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1800 expected_size = min_size;
1803 /* Since this is a move insn, we don't care about volatility. */
1804 temporary_volatile_ok v (true);
1806 /* Try the most limited insn first, because there's no point
1807 including more than one in the machine description unless
1808 the more limited one has some advantage. */
1810 opt_scalar_int_mode mode_iter;
1811 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1813 scalar_int_mode mode = mode_iter.require ();
1814 enum insn_code code;
1815 if (might_overlap)
1816 code = direct_optab_handler (movmem_optab, mode);
1817 else
1818 code = direct_optab_handler (cpymem_optab, mode);
1820 if (code != CODE_FOR_nothing
1821 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1822 here because if SIZE is less than the mode mask, as it is
1823 returned by the macro, it will definitely be less than the
1824 actual mode mask. Since SIZE is within the Pmode address
1825 space, we limit MODE to Pmode. */
1826 && ((CONST_INT_P (size)
1827 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1828 <= (GET_MODE_MASK (mode) >> 1)))
1829 || max_size <= (GET_MODE_MASK (mode) >> 1)
1830 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1832 class expand_operand ops[9];
1833 unsigned int nops;
1835 /* ??? When called via emit_block_move_for_call, it'd be
1836 nice if there were some way to inform the backend, so
1837 that it doesn't fail the expansion because it thinks
1838 emitting the libcall would be more efficient. */
1839 nops = insn_data[(int) code].n_generator_args;
1840 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1842 create_fixed_operand (&ops[0], x);
1843 create_fixed_operand (&ops[1], y);
1844 /* The check above guarantees that this size conversion is valid. */
1845 create_convert_operand_to (&ops[2], size, mode, true);
1846 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1847 if (nops >= 6)
1849 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1850 create_integer_operand (&ops[5], expected_size);
1852 if (nops >= 8)
1854 create_integer_operand (&ops[6], min_size);
1855 /* If we cannot represent the maximal size,
1856 make parameter NULL. */
1857 if ((HOST_WIDE_INT) max_size != -1)
1858 create_integer_operand (&ops[7], max_size);
1859 else
1860 create_fixed_operand (&ops[7], NULL);
1862 if (nops == 9)
1864 /* If we cannot represent the maximal size,
1865 make parameter NULL. */
1866 if ((HOST_WIDE_INT) probable_max_size != -1)
1867 create_integer_operand (&ops[8], probable_max_size);
1868 else
1869 create_fixed_operand (&ops[8], NULL);
1871 if (maybe_expand_insn (code, nops, ops))
1872 return true;
1876 return false;
1879 /* A subroutine of emit_block_move. Copy the data via an explicit
1880 loop. This is used only when libcalls are forbidden. */
1881 /* ??? It'd be nice to copy in hunks larger than QImode. */
1883 static void
1884 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1885 unsigned int align ATTRIBUTE_UNUSED)
1887 rtx_code_label *cmp_label, *top_label;
1888 rtx iter, x_addr, y_addr, tmp;
1889 machine_mode x_addr_mode = get_address_mode (x);
1890 machine_mode y_addr_mode = get_address_mode (y);
1891 machine_mode iter_mode;
1893 iter_mode = GET_MODE (size);
1894 if (iter_mode == VOIDmode)
1895 iter_mode = word_mode;
1897 top_label = gen_label_rtx ();
1898 cmp_label = gen_label_rtx ();
1899 iter = gen_reg_rtx (iter_mode);
1901 emit_move_insn (iter, const0_rtx);
1903 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1904 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1905 do_pending_stack_adjust ();
1907 emit_jump (cmp_label);
1908 emit_label (top_label);
1910 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1911 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1913 if (x_addr_mode != y_addr_mode)
1914 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1915 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1917 x = change_address (x, QImode, x_addr);
1918 y = change_address (y, QImode, y_addr);
1920 emit_move_insn (x, y);
1922 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1923 true, OPTAB_LIB_WIDEN);
1924 if (tmp != iter)
1925 emit_move_insn (iter, tmp);
1927 emit_label (cmp_label);
1929 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1930 true, top_label,
1931 profile_probability::guessed_always ()
1932 .apply_scale (9, 10));
1935 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1936 TAILCALL is true if this is a tail call. */
1939 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1940 rtx size, bool tailcall)
1942 rtx dst_addr, src_addr;
1943 tree call_expr, dst_tree, src_tree, size_tree;
1944 machine_mode size_mode;
1946 /* Since dst and src are passed to a libcall, mark the corresponding
1947 tree EXPR as addressable. */
1948 tree dst_expr = MEM_EXPR (dst);
1949 tree src_expr = MEM_EXPR (src);
1950 if (dst_expr)
1951 mark_addressable (dst_expr);
1952 if (src_expr)
1953 mark_addressable (src_expr);
1955 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1956 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1957 dst_tree = make_tree (ptr_type_node, dst_addr);
1959 src_addr = copy_addr_to_reg (XEXP (src, 0));
1960 src_addr = convert_memory_address (ptr_mode, src_addr);
1961 src_tree = make_tree (ptr_type_node, src_addr);
1963 size_mode = TYPE_MODE (sizetype);
1964 size = convert_to_mode (size_mode, size, 1);
1965 size = copy_to_mode_reg (size_mode, size);
1966 size_tree = make_tree (sizetype, size);
1968 /* It is incorrect to use the libcall calling conventions for calls to
1969 memcpy/memmove/memcmp because they can be provided by the user. */
1970 tree fn = builtin_decl_implicit (fncode);
1971 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1972 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1974 return expand_call (call_expr, NULL_RTX, false);
1977 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1978 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1979 otherwise return null. */
1982 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1983 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1984 HOST_WIDE_INT align)
1986 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1988 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1989 target = NULL_RTX;
1991 class expand_operand ops[5];
1992 create_output_operand (&ops[0], target, insn_mode);
1993 create_fixed_operand (&ops[1], arg1_rtx);
1994 create_fixed_operand (&ops[2], arg2_rtx);
1995 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1996 TYPE_UNSIGNED (arg3_type));
1997 create_integer_operand (&ops[4], align);
1998 if (maybe_expand_insn (icode, 5, ops))
1999 return ops[0].value;
2000 return NULL_RTX;
2003 /* Expand a block compare between X and Y with length LEN using the
2004 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2005 of the expression that was used to calculate the length. ALIGN
2006 gives the known minimum common alignment. */
2008 static rtx
2009 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2010 unsigned align)
2012 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2013 implementing memcmp because it will stop if it encounters two
2014 zero bytes. */
2015 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2017 if (icode == CODE_FOR_nothing)
2018 return NULL_RTX;
2020 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2023 /* Emit code to compare a block Y to a block X. This may be done with
2024 string-compare instructions, with multiple scalar instructions,
2025 or with a library call.
2027 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2028 they are. LEN_TYPE is the type of the expression that was used to
2029 calculate it.
2031 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2032 value of a normal memcmp call, instead we can just compare for equality.
2033 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2034 returning NULL_RTX.
2036 Optionally, the caller can pass a constfn and associated data in Y_CFN
2037 and Y_CFN_DATA. describing that the second operand being compared is a
2038 known constant and how to obtain its data.
2039 Return the result of the comparison, or NULL_RTX if we failed to
2040 perform the operation. */
2043 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2044 bool equality_only, by_pieces_constfn y_cfn,
2045 void *y_cfndata)
2047 rtx result = 0;
2049 if (CONST_INT_P (len) && INTVAL (len) == 0)
2050 return const0_rtx;
2052 gcc_assert (MEM_P (x) && MEM_P (y));
2053 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2054 gcc_assert (align >= BITS_PER_UNIT);
2056 x = adjust_address (x, BLKmode, 0);
2057 y = adjust_address (y, BLKmode, 0);
2059 if (equality_only
2060 && CONST_INT_P (len)
2061 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2062 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2063 y_cfn, y_cfndata);
2064 else
2065 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2067 return result;
2070 /* Copy all or part of a value X into registers starting at REGNO.
2071 The number of registers to be filled is NREGS. */
2073 void
2074 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2076 if (nregs == 0)
2077 return;
2079 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2080 x = validize_mem (force_const_mem (mode, x));
2082 /* See if the machine can do this with a load multiple insn. */
2083 if (targetm.have_load_multiple ())
2085 rtx_insn *last = get_last_insn ();
2086 rtx first = gen_rtx_REG (word_mode, regno);
2087 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2088 GEN_INT (nregs)))
2090 emit_insn (pat);
2091 return;
2093 else
2094 delete_insns_since (last);
2097 for (int i = 0; i < nregs; i++)
2098 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2099 operand_subword_force (x, i, mode));
2102 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2103 The number of registers to be filled is NREGS. */
2105 void
2106 move_block_from_reg (int regno, rtx x, int nregs)
2108 if (nregs == 0)
2109 return;
2111 /* See if the machine can do this with a store multiple insn. */
2112 if (targetm.have_store_multiple ())
2114 rtx_insn *last = get_last_insn ();
2115 rtx first = gen_rtx_REG (word_mode, regno);
2116 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2117 GEN_INT (nregs)))
2119 emit_insn (pat);
2120 return;
2122 else
2123 delete_insns_since (last);
2126 for (int i = 0; i < nregs; i++)
2128 rtx tem = operand_subword (x, i, 1, BLKmode);
2130 gcc_assert (tem);
2132 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2136 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2137 ORIG, where ORIG is a non-consecutive group of registers represented by
2138 a PARALLEL. The clone is identical to the original except in that the
2139 original set of registers is replaced by a new set of pseudo registers.
2140 The new set has the same modes as the original set. */
2143 gen_group_rtx (rtx orig)
2145 int i, length;
2146 rtx *tmps;
2148 gcc_assert (GET_CODE (orig) == PARALLEL);
2150 length = XVECLEN (orig, 0);
2151 tmps = XALLOCAVEC (rtx, length);
2153 /* Skip a NULL entry in first slot. */
2154 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2156 if (i)
2157 tmps[0] = 0;
2159 for (; i < length; i++)
2161 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2162 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2164 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2167 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2170 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2171 except that values are placed in TMPS[i], and must later be moved
2172 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2174 static void
2175 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2176 poly_int64 ssize)
2178 rtx src;
2179 int start, i;
2180 machine_mode m = GET_MODE (orig_src);
2182 gcc_assert (GET_CODE (dst) == PARALLEL);
2184 if (m != VOIDmode
2185 && !SCALAR_INT_MODE_P (m)
2186 && !MEM_P (orig_src)
2187 && GET_CODE (orig_src) != CONCAT)
2189 scalar_int_mode imode;
2190 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2192 src = gen_reg_rtx (imode);
2193 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2195 else
2197 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2198 emit_move_insn (src, orig_src);
2200 emit_group_load_1 (tmps, dst, src, type, ssize);
2201 return;
2204 /* Check for a NULL entry, used to indicate that the parameter goes
2205 both on the stack and in registers. */
2206 if (XEXP (XVECEXP (dst, 0, 0), 0))
2207 start = 0;
2208 else
2209 start = 1;
2211 /* Process the pieces. */
2212 for (i = start; i < XVECLEN (dst, 0); i++)
2214 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2215 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2216 poly_int64 bytelen = GET_MODE_SIZE (mode);
2217 poly_int64 shift = 0;
2219 /* Handle trailing fragments that run over the size of the struct.
2220 It's the target's responsibility to make sure that the fragment
2221 cannot be strictly smaller in some cases and strictly larger
2222 in others. */
2223 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2224 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2226 /* Arrange to shift the fragment to where it belongs.
2227 extract_bit_field loads to the lsb of the reg. */
2228 if (
2229 #ifdef BLOCK_REG_PADDING
2230 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2231 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2232 #else
2233 BYTES_BIG_ENDIAN
2234 #endif
2236 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2237 bytelen = ssize - bytepos;
2238 gcc_assert (maybe_gt (bytelen, 0));
2241 /* If we won't be loading directly from memory, protect the real source
2242 from strange tricks we might play; but make sure that the source can
2243 be loaded directly into the destination. */
2244 src = orig_src;
2245 if (!MEM_P (orig_src)
2246 && (!CONSTANT_P (orig_src)
2247 || (GET_MODE (orig_src) != mode
2248 && GET_MODE (orig_src) != VOIDmode)))
2250 if (GET_MODE (orig_src) == VOIDmode)
2251 src = gen_reg_rtx (mode);
2252 else
2253 src = gen_reg_rtx (GET_MODE (orig_src));
2255 emit_move_insn (src, orig_src);
2258 /* Optimize the access just a bit. */
2259 if (MEM_P (src)
2260 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2261 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2262 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2263 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2265 tmps[i] = gen_reg_rtx (mode);
2266 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2268 else if (COMPLEX_MODE_P (mode)
2269 && GET_MODE (src) == mode
2270 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2271 /* Let emit_move_complex do the bulk of the work. */
2272 tmps[i] = src;
2273 else if (GET_CODE (src) == CONCAT)
2275 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2276 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2277 unsigned int elt;
2278 poly_int64 subpos;
2280 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2281 && known_le (subpos + bytelen, slen0))
2283 /* The following assumes that the concatenated objects all
2284 have the same size. In this case, a simple calculation
2285 can be used to determine the object and the bit field
2286 to be extracted. */
2287 tmps[i] = XEXP (src, elt);
2288 if (maybe_ne (subpos, 0)
2289 || maybe_ne (subpos + bytelen, slen0)
2290 || (!CONSTANT_P (tmps[i])
2291 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2292 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2293 subpos * BITS_PER_UNIT,
2294 1, NULL_RTX, mode, mode, false,
2295 NULL);
2297 else
2299 rtx mem;
2301 gcc_assert (known_eq (bytepos, 0));
2302 mem = assign_stack_temp (GET_MODE (src), slen);
2303 emit_move_insn (mem, src);
2304 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2305 0, 1, NULL_RTX, mode, mode, false,
2306 NULL);
2309 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2310 SIMD register, which is currently broken. While we get GCC
2311 to emit proper RTL for these cases, let's dump to memory. */
2312 else if (VECTOR_MODE_P (GET_MODE (dst))
2313 && REG_P (src))
2315 poly_uint64 slen = GET_MODE_SIZE (GET_MODE (src));
2316 rtx mem;
2318 mem = assign_stack_temp (GET_MODE (src), slen);
2319 emit_move_insn (mem, src);
2320 tmps[i] = adjust_address (mem, mode, bytepos);
2322 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2323 && XVECLEN (dst, 0) > 1)
2324 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2325 else if (CONSTANT_P (src))
2327 if (known_eq (bytelen, ssize))
2328 tmps[i] = src;
2329 else
2331 rtx first, second;
2333 /* TODO: const_wide_int can have sizes other than this... */
2334 gcc_assert (known_eq (2 * bytelen, ssize));
2335 split_double (src, &first, &second);
2336 if (i)
2337 tmps[i] = second;
2338 else
2339 tmps[i] = first;
2342 else if (REG_P (src) && GET_MODE (src) == mode)
2343 tmps[i] = src;
2344 else
2345 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2346 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2347 mode, mode, false, NULL);
2349 if (maybe_ne (shift, 0))
2350 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2351 shift, tmps[i], 0);
2355 /* Emit code to move a block SRC of type TYPE to a block DST,
2356 where DST is non-consecutive registers represented by a PARALLEL.
2357 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2358 if not known. */
2360 void
2361 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2363 rtx *tmps;
2364 int i;
2366 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2367 emit_group_load_1 (tmps, dst, src, type, ssize);
2369 /* Copy the extracted pieces into the proper (probable) hard regs. */
2370 for (i = 0; i < XVECLEN (dst, 0); i++)
2372 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2373 if (d == NULL)
2374 continue;
2375 emit_move_insn (d, tmps[i]);
2379 /* Similar, but load SRC into new pseudos in a format that looks like
2380 PARALLEL. This can later be fed to emit_group_move to get things
2381 in the right place. */
2384 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2386 rtvec vec;
2387 int i;
2389 vec = rtvec_alloc (XVECLEN (parallel, 0));
2390 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2392 /* Convert the vector to look just like the original PARALLEL, except
2393 with the computed values. */
2394 for (i = 0; i < XVECLEN (parallel, 0); i++)
2396 rtx e = XVECEXP (parallel, 0, i);
2397 rtx d = XEXP (e, 0);
2399 if (d)
2401 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2402 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2404 RTVEC_ELT (vec, i) = e;
2407 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2410 /* Emit code to move a block SRC to block DST, where SRC and DST are
2411 non-consecutive groups of registers, each represented by a PARALLEL. */
2413 void
2414 emit_group_move (rtx dst, rtx src)
2416 int i;
2418 gcc_assert (GET_CODE (src) == PARALLEL
2419 && GET_CODE (dst) == PARALLEL
2420 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2422 /* Skip first entry if NULL. */
2423 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2424 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2425 XEXP (XVECEXP (src, 0, i), 0));
2428 /* Move a group of registers represented by a PARALLEL into pseudos. */
2431 emit_group_move_into_temps (rtx src)
2433 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2434 int i;
2436 for (i = 0; i < XVECLEN (src, 0); i++)
2438 rtx e = XVECEXP (src, 0, i);
2439 rtx d = XEXP (e, 0);
2441 if (d)
2442 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2443 RTVEC_ELT (vec, i) = e;
2446 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2449 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2450 where SRC is non-consecutive registers represented by a PARALLEL.
2451 SSIZE represents the total size of block ORIG_DST, or -1 if not
2452 known. */
2454 void
2455 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2456 poly_int64 ssize)
2458 rtx *tmps, dst;
2459 int start, finish, i;
2460 machine_mode m = GET_MODE (orig_dst);
2462 gcc_assert (GET_CODE (src) == PARALLEL);
2464 if (!SCALAR_INT_MODE_P (m)
2465 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2467 scalar_int_mode imode;
2468 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2470 dst = gen_reg_rtx (imode);
2471 emit_group_store (dst, src, type, ssize);
2472 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2474 else
2476 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2477 emit_group_store (dst, src, type, ssize);
2479 emit_move_insn (orig_dst, dst);
2480 return;
2483 /* Check for a NULL entry, used to indicate that the parameter goes
2484 both on the stack and in registers. */
2485 if (XEXP (XVECEXP (src, 0, 0), 0))
2486 start = 0;
2487 else
2488 start = 1;
2489 finish = XVECLEN (src, 0);
2491 tmps = XALLOCAVEC (rtx, finish);
2493 /* Copy the (probable) hard regs into pseudos. */
2494 for (i = start; i < finish; i++)
2496 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2497 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2499 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2500 emit_move_insn (tmps[i], reg);
2502 else
2503 tmps[i] = reg;
2506 /* If we won't be storing directly into memory, protect the real destination
2507 from strange tricks we might play. */
2508 dst = orig_dst;
2509 if (GET_CODE (dst) == PARALLEL)
2511 rtx temp;
2513 /* We can get a PARALLEL dst if there is a conditional expression in
2514 a return statement. In that case, the dst and src are the same,
2515 so no action is necessary. */
2516 if (rtx_equal_p (dst, src))
2517 return;
2519 /* It is unclear if we can ever reach here, but we may as well handle
2520 it. Allocate a temporary, and split this into a store/load to/from
2521 the temporary. */
2522 temp = assign_stack_temp (GET_MODE (dst), ssize);
2523 emit_group_store (temp, src, type, ssize);
2524 emit_group_load (dst, temp, type, ssize);
2525 return;
2527 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2529 machine_mode outer = GET_MODE (dst);
2530 machine_mode inner;
2531 poly_int64 bytepos;
2532 bool done = false;
2533 rtx temp;
2535 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2536 dst = gen_reg_rtx (outer);
2538 /* Make life a bit easier for combine. */
2539 /* If the first element of the vector is the low part
2540 of the destination mode, use a paradoxical subreg to
2541 initialize the destination. */
2542 if (start < finish)
2544 inner = GET_MODE (tmps[start]);
2545 bytepos = subreg_lowpart_offset (inner, outer);
2546 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2547 bytepos))
2549 temp = simplify_gen_subreg (outer, tmps[start],
2550 inner, 0);
2551 if (temp)
2553 emit_move_insn (dst, temp);
2554 done = true;
2555 start++;
2560 /* If the first element wasn't the low part, try the last. */
2561 if (!done
2562 && start < finish - 1)
2564 inner = GET_MODE (tmps[finish - 1]);
2565 bytepos = subreg_lowpart_offset (inner, outer);
2566 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2567 finish - 1), 1)),
2568 bytepos))
2570 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2571 inner, 0);
2572 if (temp)
2574 emit_move_insn (dst, temp);
2575 done = true;
2576 finish--;
2581 /* Otherwise, simply initialize the result to zero. */
2582 if (!done)
2583 emit_move_insn (dst, CONST0_RTX (outer));
2586 /* Process the pieces. */
2587 for (i = start; i < finish; i++)
2589 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2590 machine_mode mode = GET_MODE (tmps[i]);
2591 poly_int64 bytelen = GET_MODE_SIZE (mode);
2592 poly_uint64 adj_bytelen;
2593 rtx dest = dst;
2595 /* Handle trailing fragments that run over the size of the struct.
2596 It's the target's responsibility to make sure that the fragment
2597 cannot be strictly smaller in some cases and strictly larger
2598 in others. */
2599 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2600 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2601 adj_bytelen = ssize - bytepos;
2602 else
2603 adj_bytelen = bytelen;
2605 if (GET_CODE (dst) == CONCAT)
2607 if (known_le (bytepos + adj_bytelen,
2608 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2609 dest = XEXP (dst, 0);
2610 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2612 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2613 dest = XEXP (dst, 1);
2615 else
2617 machine_mode dest_mode = GET_MODE (dest);
2618 machine_mode tmp_mode = GET_MODE (tmps[i]);
2620 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2622 if (GET_MODE_ALIGNMENT (dest_mode)
2623 >= GET_MODE_ALIGNMENT (tmp_mode))
2625 dest = assign_stack_temp (dest_mode,
2626 GET_MODE_SIZE (dest_mode));
2627 emit_move_insn (adjust_address (dest,
2628 tmp_mode,
2629 bytepos),
2630 tmps[i]);
2631 dst = dest;
2633 else
2635 dest = assign_stack_temp (tmp_mode,
2636 GET_MODE_SIZE (tmp_mode));
2637 emit_move_insn (dest, tmps[i]);
2638 dst = adjust_address (dest, dest_mode, bytepos);
2640 break;
2644 /* Handle trailing fragments that run over the size of the struct. */
2645 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2647 /* store_bit_field always takes its value from the lsb.
2648 Move the fragment to the lsb if it's not already there. */
2649 if (
2650 #ifdef BLOCK_REG_PADDING
2651 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2652 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2653 #else
2654 BYTES_BIG_ENDIAN
2655 #endif
2658 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2659 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2660 shift, tmps[i], 0);
2663 /* Make sure not to write past the end of the struct. */
2664 store_bit_field (dest,
2665 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2666 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2667 VOIDmode, tmps[i], false);
2670 /* Optimize the access just a bit. */
2671 else if (MEM_P (dest)
2672 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2673 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2674 && multiple_p (bytepos * BITS_PER_UNIT,
2675 GET_MODE_ALIGNMENT (mode))
2676 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2677 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2679 else
2680 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2681 0, 0, mode, tmps[i], false);
2684 /* Copy from the pseudo into the (probable) hard reg. */
2685 if (orig_dst != dst)
2686 emit_move_insn (orig_dst, dst);
2689 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2690 of the value stored in X. */
2693 maybe_emit_group_store (rtx x, tree type)
2695 machine_mode mode = TYPE_MODE (type);
2696 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2697 if (GET_CODE (x) == PARALLEL)
2699 rtx result = gen_reg_rtx (mode);
2700 emit_group_store (result, x, type, int_size_in_bytes (type));
2701 return result;
2703 return x;
2706 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2708 This is used on targets that return BLKmode values in registers. */
2710 static void
2711 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2713 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2714 rtx src = NULL, dst = NULL;
2715 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2716 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2717 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2718 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2719 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2720 fixed_size_mode copy_mode;
2722 /* BLKmode registers created in the back-end shouldn't have survived. */
2723 gcc_assert (mode != BLKmode);
2725 /* If the structure doesn't take up a whole number of words, see whether
2726 SRCREG is padded on the left or on the right. If it's on the left,
2727 set PADDING_CORRECTION to the number of bits to skip.
2729 In most ABIs, the structure will be returned at the least end of
2730 the register, which translates to right padding on little-endian
2731 targets and left padding on big-endian targets. The opposite
2732 holds if the structure is returned at the most significant
2733 end of the register. */
2734 if (bytes % UNITS_PER_WORD != 0
2735 && (targetm.calls.return_in_msb (type)
2736 ? !BYTES_BIG_ENDIAN
2737 : BYTES_BIG_ENDIAN))
2738 padding_correction
2739 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2741 /* We can use a single move if we have an exact mode for the size. */
2742 else if (MEM_P (target)
2743 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2744 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2745 && bytes == GET_MODE_SIZE (mode))
2747 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2748 return;
2751 /* And if we additionally have the same mode for a register. */
2752 else if (REG_P (target)
2753 && GET_MODE (target) == mode
2754 && bytes == GET_MODE_SIZE (mode))
2756 emit_move_insn (target, srcreg);
2757 return;
2760 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2761 into a new pseudo which is a full word. */
2762 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2764 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2765 mode = word_mode;
2768 /* Copy the structure BITSIZE bits at a time. If the target lives in
2769 memory, take care of not reading/writing past its end by selecting
2770 a copy mode suited to BITSIZE. This should always be possible given
2771 how it is computed.
2773 If the target lives in register, make sure not to select a copy mode
2774 larger than the mode of the register.
2776 We could probably emit more efficient code for machines which do not use
2777 strict alignment, but it doesn't seem worth the effort at the current
2778 time. */
2780 copy_mode = word_mode;
2781 if (MEM_P (target))
2783 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2784 if (mem_mode.exists ())
2785 copy_mode = mem_mode.require ();
2787 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2788 copy_mode = tmode;
2790 for (bitpos = 0, xbitpos = padding_correction;
2791 bitpos < bytes * BITS_PER_UNIT;
2792 bitpos += bitsize, xbitpos += bitsize)
2794 /* We need a new source operand each time xbitpos is on a
2795 word boundary and when xbitpos == padding_correction
2796 (the first time through). */
2797 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2798 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2800 /* We need a new destination operand each time bitpos is on
2801 a word boundary. */
2802 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2803 dst = target;
2804 else if (bitpos % BITS_PER_WORD == 0)
2805 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2807 /* Use xbitpos for the source extraction (right justified) and
2808 bitpos for the destination store (left justified). */
2809 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2810 extract_bit_field (src, bitsize,
2811 xbitpos % BITS_PER_WORD, 1,
2812 NULL_RTX, copy_mode, copy_mode,
2813 false, NULL),
2814 false);
2818 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2819 register if it contains any data, otherwise return null.
2821 This is used on targets that return BLKmode values in registers. */
2824 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2826 int i, n_regs;
2827 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2828 unsigned int bitsize;
2829 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2830 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2831 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2832 fixed_size_mode dst_mode;
2833 scalar_int_mode min_mode;
2835 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2837 x = expand_normal (src);
2839 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2840 if (bytes == 0)
2841 return NULL_RTX;
2843 /* If the structure doesn't take up a whole number of words, see
2844 whether the register value should be padded on the left or on
2845 the right. Set PADDING_CORRECTION to the number of padding
2846 bits needed on the left side.
2848 In most ABIs, the structure will be returned at the least end of
2849 the register, which translates to right padding on little-endian
2850 targets and left padding on big-endian targets. The opposite
2851 holds if the structure is returned at the most significant
2852 end of the register. */
2853 if (bytes % UNITS_PER_WORD != 0
2854 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2855 ? !BYTES_BIG_ENDIAN
2856 : BYTES_BIG_ENDIAN))
2857 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2858 * BITS_PER_UNIT));
2860 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2861 dst_words = XALLOCAVEC (rtx, n_regs);
2862 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2863 min_mode = smallest_int_mode_for_size (bitsize);
2865 /* Copy the structure BITSIZE bits at a time. */
2866 for (bitpos = 0, xbitpos = padding_correction;
2867 bitpos < bytes * BITS_PER_UNIT;
2868 bitpos += bitsize, xbitpos += bitsize)
2870 /* We need a new destination pseudo each time xbitpos is
2871 on a word boundary and when xbitpos == padding_correction
2872 (the first time through). */
2873 if (xbitpos % BITS_PER_WORD == 0
2874 || xbitpos == padding_correction)
2876 /* Generate an appropriate register. */
2877 dst_word = gen_reg_rtx (word_mode);
2878 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2880 /* Clear the destination before we move anything into it. */
2881 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2884 /* Find the largest integer mode that can be used to copy all or as
2885 many bits as possible of the structure if the target supports larger
2886 copies. There are too many corner cases here w.r.t to alignments on
2887 the read/writes. So if there is any padding just use single byte
2888 operations. */
2889 opt_scalar_int_mode mode_iter;
2890 if (padding_correction == 0 && !STRICT_ALIGNMENT)
2892 FOR_EACH_MODE_FROM (mode_iter, min_mode)
2894 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
2895 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
2896 && msize <= BITS_PER_WORD)
2897 bitsize = msize;
2898 else
2899 break;
2903 /* We need a new source operand each time bitpos is on a word
2904 boundary. */
2905 if (bitpos % BITS_PER_WORD == 0)
2906 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2908 /* Use bitpos for the source extraction (left justified) and
2909 xbitpos for the destination store (right justified). */
2910 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2911 0, 0, word_mode,
2912 extract_bit_field (src_word, bitsize,
2913 bitpos % BITS_PER_WORD, 1,
2914 NULL_RTX, word_mode, word_mode,
2915 false, NULL),
2916 false);
2919 if (mode == BLKmode)
2921 /* Find the smallest integer mode large enough to hold the
2922 entire structure. */
2923 opt_scalar_int_mode mode_iter;
2924 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2925 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2926 break;
2928 /* A suitable mode should have been found. */
2929 mode = mode_iter.require ();
2932 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2933 dst_mode = word_mode;
2934 else
2935 dst_mode = mode;
2936 dst = gen_reg_rtx (dst_mode);
2938 for (i = 0; i < n_regs; i++)
2939 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2941 if (mode != dst_mode)
2942 dst = gen_lowpart (mode, dst);
2944 return dst;
2947 /* Add a USE expression for REG to the (possibly empty) list pointed
2948 to by CALL_FUSAGE. REG must denote a hard register. */
2950 void
2951 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2953 gcc_assert (REG_P (reg));
2955 if (!HARD_REGISTER_P (reg))
2956 return;
2958 *call_fusage
2959 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2962 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2963 to by CALL_FUSAGE. REG must denote a hard register. */
2965 void
2966 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2968 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2970 *call_fusage
2971 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2974 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2975 starting at REGNO. All of these registers must be hard registers. */
2977 void
2978 use_regs (rtx *call_fusage, int regno, int nregs)
2980 int i;
2982 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2984 for (i = 0; i < nregs; i++)
2985 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2988 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2989 PARALLEL REGS. This is for calls that pass values in multiple
2990 non-contiguous locations. The Irix 6 ABI has examples of this. */
2992 void
2993 use_group_regs (rtx *call_fusage, rtx regs)
2995 int i;
2997 for (i = 0; i < XVECLEN (regs, 0); i++)
2999 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3001 /* A NULL entry means the parameter goes both on the stack and in
3002 registers. This can also be a MEM for targets that pass values
3003 partially on the stack and partially in registers. */
3004 if (reg != 0 && REG_P (reg))
3005 use_reg (call_fusage, reg);
3009 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3010 assigment and the code of the expresion on the RHS is CODE. Return
3011 NULL otherwise. */
3013 static gimple *
3014 get_def_for_expr (tree name, enum tree_code code)
3016 gimple *def_stmt;
3018 if (TREE_CODE (name) != SSA_NAME)
3019 return NULL;
3021 def_stmt = get_gimple_for_ssa_name (name);
3022 if (!def_stmt
3023 || gimple_assign_rhs_code (def_stmt) != code)
3024 return NULL;
3026 return def_stmt;
3029 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3030 assigment and the class of the expresion on the RHS is CLASS. Return
3031 NULL otherwise. */
3033 static gimple *
3034 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3036 gimple *def_stmt;
3038 if (TREE_CODE (name) != SSA_NAME)
3039 return NULL;
3041 def_stmt = get_gimple_for_ssa_name (name);
3042 if (!def_stmt
3043 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3044 return NULL;
3046 return def_stmt;
3049 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3050 its length in bytes. */
3053 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3054 unsigned int expected_align, HOST_WIDE_INT expected_size,
3055 unsigned HOST_WIDE_INT min_size,
3056 unsigned HOST_WIDE_INT max_size,
3057 unsigned HOST_WIDE_INT probable_max_size)
3059 machine_mode mode = GET_MODE (object);
3060 unsigned int align;
3062 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3064 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3065 just move a zero. Otherwise, do this a piece at a time. */
3066 poly_int64 size_val;
3067 if (mode != BLKmode
3068 && poly_int_rtx_p (size, &size_val)
3069 && known_eq (size_val, GET_MODE_SIZE (mode)))
3071 rtx zero = CONST0_RTX (mode);
3072 if (zero != NULL)
3074 emit_move_insn (object, zero);
3075 return NULL;
3078 if (COMPLEX_MODE_P (mode))
3080 zero = CONST0_RTX (GET_MODE_INNER (mode));
3081 if (zero != NULL)
3083 write_complex_part (object, zero, 0);
3084 write_complex_part (object, zero, 1);
3085 return NULL;
3090 if (size == const0_rtx)
3091 return NULL;
3093 align = MEM_ALIGN (object);
3095 if (CONST_INT_P (size)
3096 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3097 CLEAR_BY_PIECES,
3098 optimize_insn_for_speed_p ()))
3099 clear_by_pieces (object, INTVAL (size), align);
3100 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3101 expected_align, expected_size,
3102 min_size, max_size, probable_max_size))
3104 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3105 return set_storage_via_libcall (object, size, const0_rtx,
3106 method == BLOCK_OP_TAILCALL);
3107 else
3108 gcc_unreachable ();
3110 return NULL;
3114 clear_storage (rtx object, rtx size, enum block_op_methods method)
3116 unsigned HOST_WIDE_INT max, min = 0;
3117 if (GET_CODE (size) == CONST_INT)
3118 min = max = UINTVAL (size);
3119 else
3120 max = GET_MODE_MASK (GET_MODE (size));
3121 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3125 /* A subroutine of clear_storage. Expand a call to memset.
3126 Return the return value of memset, 0 otherwise. */
3129 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3131 tree call_expr, fn, object_tree, size_tree, val_tree;
3132 machine_mode size_mode;
3134 object = copy_addr_to_reg (XEXP (object, 0));
3135 object_tree = make_tree (ptr_type_node, object);
3137 if (!CONST_INT_P (val))
3138 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3139 val_tree = make_tree (integer_type_node, val);
3141 size_mode = TYPE_MODE (sizetype);
3142 size = convert_to_mode (size_mode, size, 1);
3143 size = copy_to_mode_reg (size_mode, size);
3144 size_tree = make_tree (sizetype, size);
3146 /* It is incorrect to use the libcall calling conventions for calls to
3147 memset because it can be provided by the user. */
3148 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3149 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3150 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3152 return expand_call (call_expr, NULL_RTX, false);
3155 /* Expand a setmem pattern; return true if successful. */
3157 bool
3158 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3159 unsigned int expected_align, HOST_WIDE_INT expected_size,
3160 unsigned HOST_WIDE_INT min_size,
3161 unsigned HOST_WIDE_INT max_size,
3162 unsigned HOST_WIDE_INT probable_max_size)
3164 /* Try the most limited insn first, because there's no point
3165 including more than one in the machine description unless
3166 the more limited one has some advantage. */
3168 if (expected_align < align)
3169 expected_align = align;
3170 if (expected_size != -1)
3172 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3173 expected_size = max_size;
3174 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3175 expected_size = min_size;
3178 opt_scalar_int_mode mode_iter;
3179 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3181 scalar_int_mode mode = mode_iter.require ();
3182 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3184 if (code != CODE_FOR_nothing
3185 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3186 here because if SIZE is less than the mode mask, as it is
3187 returned by the macro, it will definitely be less than the
3188 actual mode mask. Since SIZE is within the Pmode address
3189 space, we limit MODE to Pmode. */
3190 && ((CONST_INT_P (size)
3191 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3192 <= (GET_MODE_MASK (mode) >> 1)))
3193 || max_size <= (GET_MODE_MASK (mode) >> 1)
3194 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3196 class expand_operand ops[9];
3197 unsigned int nops;
3199 nops = insn_data[(int) code].n_generator_args;
3200 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3202 create_fixed_operand (&ops[0], object);
3203 /* The check above guarantees that this size conversion is valid. */
3204 create_convert_operand_to (&ops[1], size, mode, true);
3205 create_convert_operand_from (&ops[2], val, byte_mode, true);
3206 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3207 if (nops >= 6)
3209 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3210 create_integer_operand (&ops[5], expected_size);
3212 if (nops >= 8)
3214 create_integer_operand (&ops[6], min_size);
3215 /* If we cannot represent the maximal size,
3216 make parameter NULL. */
3217 if ((HOST_WIDE_INT) max_size != -1)
3218 create_integer_operand (&ops[7], max_size);
3219 else
3220 create_fixed_operand (&ops[7], NULL);
3222 if (nops == 9)
3224 /* If we cannot represent the maximal size,
3225 make parameter NULL. */
3226 if ((HOST_WIDE_INT) probable_max_size != -1)
3227 create_integer_operand (&ops[8], probable_max_size);
3228 else
3229 create_fixed_operand (&ops[8], NULL);
3231 if (maybe_expand_insn (code, nops, ops))
3232 return true;
3236 return false;
3240 /* Write to one of the components of the complex value CPLX. Write VAL to
3241 the real part if IMAG_P is false, and the imaginary part if its true. */
3243 void
3244 write_complex_part (rtx cplx, rtx val, bool imag_p)
3246 machine_mode cmode;
3247 scalar_mode imode;
3248 unsigned ibitsize;
3250 if (GET_CODE (cplx) == CONCAT)
3252 emit_move_insn (XEXP (cplx, imag_p), val);
3253 return;
3256 cmode = GET_MODE (cplx);
3257 imode = GET_MODE_INNER (cmode);
3258 ibitsize = GET_MODE_BITSIZE (imode);
3260 /* For MEMs simplify_gen_subreg may generate an invalid new address
3261 because, e.g., the original address is considered mode-dependent
3262 by the target, which restricts simplify_subreg from invoking
3263 adjust_address_nv. Instead of preparing fallback support for an
3264 invalid address, we call adjust_address_nv directly. */
3265 if (MEM_P (cplx))
3267 emit_move_insn (adjust_address_nv (cplx, imode,
3268 imag_p ? GET_MODE_SIZE (imode) : 0),
3269 val);
3270 return;
3273 /* If the sub-object is at least word sized, then we know that subregging
3274 will work. This special case is important, since store_bit_field
3275 wants to operate on integer modes, and there's rarely an OImode to
3276 correspond to TCmode. */
3277 if (ibitsize >= BITS_PER_WORD
3278 /* For hard regs we have exact predicates. Assume we can split
3279 the original object if it spans an even number of hard regs.
3280 This special case is important for SCmode on 64-bit platforms
3281 where the natural size of floating-point regs is 32-bit. */
3282 || (REG_P (cplx)
3283 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3284 && REG_NREGS (cplx) % 2 == 0))
3286 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3287 imag_p ? GET_MODE_SIZE (imode) : 0);
3288 if (part)
3290 emit_move_insn (part, val);
3291 return;
3293 else
3294 /* simplify_gen_subreg may fail for sub-word MEMs. */
3295 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3298 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3299 false);
3302 /* Extract one of the components of the complex value CPLX. Extract the
3303 real part if IMAG_P is false, and the imaginary part if it's true. */
3306 read_complex_part (rtx cplx, bool imag_p)
3308 machine_mode cmode;
3309 scalar_mode imode;
3310 unsigned ibitsize;
3312 if (GET_CODE (cplx) == CONCAT)
3313 return XEXP (cplx, imag_p);
3315 cmode = GET_MODE (cplx);
3316 imode = GET_MODE_INNER (cmode);
3317 ibitsize = GET_MODE_BITSIZE (imode);
3319 /* Special case reads from complex constants that got spilled to memory. */
3320 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3322 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3323 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3325 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3326 if (CONSTANT_CLASS_P (part))
3327 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3331 /* For MEMs simplify_gen_subreg may generate an invalid new address
3332 because, e.g., the original address is considered mode-dependent
3333 by the target, which restricts simplify_subreg from invoking
3334 adjust_address_nv. Instead of preparing fallback support for an
3335 invalid address, we call adjust_address_nv directly. */
3336 if (MEM_P (cplx))
3337 return adjust_address_nv (cplx, imode,
3338 imag_p ? GET_MODE_SIZE (imode) : 0);
3340 /* If the sub-object is at least word sized, then we know that subregging
3341 will work. This special case is important, since extract_bit_field
3342 wants to operate on integer modes, and there's rarely an OImode to
3343 correspond to TCmode. */
3344 if (ibitsize >= BITS_PER_WORD
3345 /* For hard regs we have exact predicates. Assume we can split
3346 the original object if it spans an even number of hard regs.
3347 This special case is important for SCmode on 64-bit platforms
3348 where the natural size of floating-point regs is 32-bit. */
3349 || (REG_P (cplx)
3350 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3351 && REG_NREGS (cplx) % 2 == 0))
3353 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3354 imag_p ? GET_MODE_SIZE (imode) : 0);
3355 if (ret)
3356 return ret;
3357 else
3358 /* simplify_gen_subreg may fail for sub-word MEMs. */
3359 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3362 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3363 true, NULL_RTX, imode, imode, false, NULL);
3366 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3367 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3368 represented in NEW_MODE. If FORCE is true, this will never happen, as
3369 we'll force-create a SUBREG if needed. */
3371 static rtx
3372 emit_move_change_mode (machine_mode new_mode,
3373 machine_mode old_mode, rtx x, bool force)
3375 rtx ret;
3377 if (push_operand (x, GET_MODE (x)))
3379 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3380 MEM_COPY_ATTRIBUTES (ret, x);
3382 else if (MEM_P (x))
3384 /* We don't have to worry about changing the address since the
3385 size in bytes is supposed to be the same. */
3386 if (reload_in_progress)
3388 /* Copy the MEM to change the mode and move any
3389 substitutions from the old MEM to the new one. */
3390 ret = adjust_address_nv (x, new_mode, 0);
3391 copy_replacements (x, ret);
3393 else
3394 ret = adjust_address (x, new_mode, 0);
3396 else
3398 /* Note that we do want simplify_subreg's behavior of validating
3399 that the new mode is ok for a hard register. If we were to use
3400 simplify_gen_subreg, we would create the subreg, but would
3401 probably run into the target not being able to implement it. */
3402 /* Except, of course, when FORCE is true, when this is exactly what
3403 we want. Which is needed for CCmodes on some targets. */
3404 if (force)
3405 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3406 else
3407 ret = simplify_subreg (new_mode, x, old_mode, 0);
3410 return ret;
3413 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3414 an integer mode of the same size as MODE. Returns the instruction
3415 emitted, or NULL if such a move could not be generated. */
3417 static rtx_insn *
3418 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3420 scalar_int_mode imode;
3421 enum insn_code code;
3423 /* There must exist a mode of the exact size we require. */
3424 if (!int_mode_for_mode (mode).exists (&imode))
3425 return NULL;
3427 /* The target must support moves in this mode. */
3428 code = optab_handler (mov_optab, imode);
3429 if (code == CODE_FOR_nothing)
3430 return NULL;
3432 x = emit_move_change_mode (imode, mode, x, force);
3433 if (x == NULL_RTX)
3434 return NULL;
3435 y = emit_move_change_mode (imode, mode, y, force);
3436 if (y == NULL_RTX)
3437 return NULL;
3438 return emit_insn (GEN_FCN (code) (x, y));
3441 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3442 Return an equivalent MEM that does not use an auto-increment. */
3445 emit_move_resolve_push (machine_mode mode, rtx x)
3447 enum rtx_code code = GET_CODE (XEXP (x, 0));
3448 rtx temp;
3450 poly_int64 adjust = GET_MODE_SIZE (mode);
3451 #ifdef PUSH_ROUNDING
3452 adjust = PUSH_ROUNDING (adjust);
3453 #endif
3454 if (code == PRE_DEC || code == POST_DEC)
3455 adjust = -adjust;
3456 else if (code == PRE_MODIFY || code == POST_MODIFY)
3458 rtx expr = XEXP (XEXP (x, 0), 1);
3460 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3461 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3462 if (GET_CODE (expr) == MINUS)
3463 val = -val;
3464 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3465 adjust = val;
3468 /* Do not use anti_adjust_stack, since we don't want to update
3469 stack_pointer_delta. */
3470 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3471 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3472 0, OPTAB_LIB_WIDEN);
3473 if (temp != stack_pointer_rtx)
3474 emit_move_insn (stack_pointer_rtx, temp);
3476 switch (code)
3478 case PRE_INC:
3479 case PRE_DEC:
3480 case PRE_MODIFY:
3481 temp = stack_pointer_rtx;
3482 break;
3483 case POST_INC:
3484 case POST_DEC:
3485 case POST_MODIFY:
3486 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3487 break;
3488 default:
3489 gcc_unreachable ();
3492 return replace_equiv_address (x, temp);
3495 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3496 X is known to satisfy push_operand, and MODE is known to be complex.
3497 Returns the last instruction emitted. */
3499 rtx_insn *
3500 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3502 scalar_mode submode = GET_MODE_INNER (mode);
3503 bool imag_first;
3505 #ifdef PUSH_ROUNDING
3506 poly_int64 submodesize = GET_MODE_SIZE (submode);
3508 /* In case we output to the stack, but the size is smaller than the
3509 machine can push exactly, we need to use move instructions. */
3510 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3512 x = emit_move_resolve_push (mode, x);
3513 return emit_move_insn (x, y);
3515 #endif
3517 /* Note that the real part always precedes the imag part in memory
3518 regardless of machine's endianness. */
3519 switch (GET_CODE (XEXP (x, 0)))
3521 case PRE_DEC:
3522 case POST_DEC:
3523 imag_first = true;
3524 break;
3525 case PRE_INC:
3526 case POST_INC:
3527 imag_first = false;
3528 break;
3529 default:
3530 gcc_unreachable ();
3533 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3534 read_complex_part (y, imag_first));
3535 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3536 read_complex_part (y, !imag_first));
3539 /* A subroutine of emit_move_complex. Perform the move from Y to X
3540 via two moves of the parts. Returns the last instruction emitted. */
3542 rtx_insn *
3543 emit_move_complex_parts (rtx x, rtx y)
3545 /* Show the output dies here. This is necessary for SUBREGs
3546 of pseudos since we cannot track their lifetimes correctly;
3547 hard regs shouldn't appear here except as return values. */
3548 if (!reload_completed && !reload_in_progress
3549 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3550 emit_clobber (x);
3552 write_complex_part (x, read_complex_part (y, false), false);
3553 write_complex_part (x, read_complex_part (y, true), true);
3555 return get_last_insn ();
3558 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3559 MODE is known to be complex. Returns the last instruction emitted. */
3561 static rtx_insn *
3562 emit_move_complex (machine_mode mode, rtx x, rtx y)
3564 bool try_int;
3566 /* Need to take special care for pushes, to maintain proper ordering
3567 of the data, and possibly extra padding. */
3568 if (push_operand (x, mode))
3569 return emit_move_complex_push (mode, x, y);
3571 /* See if we can coerce the target into moving both values at once, except
3572 for floating point where we favor moving as parts if this is easy. */
3573 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3574 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3575 && !(REG_P (x)
3576 && HARD_REGISTER_P (x)
3577 && REG_NREGS (x) == 1)
3578 && !(REG_P (y)
3579 && HARD_REGISTER_P (y)
3580 && REG_NREGS (y) == 1))
3581 try_int = false;
3582 /* Not possible if the values are inherently not adjacent. */
3583 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3584 try_int = false;
3585 /* Is possible if both are registers (or subregs of registers). */
3586 else if (register_operand (x, mode) && register_operand (y, mode))
3587 try_int = true;
3588 /* If one of the operands is a memory, and alignment constraints
3589 are friendly enough, we may be able to do combined memory operations.
3590 We do not attempt this if Y is a constant because that combination is
3591 usually better with the by-parts thing below. */
3592 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3593 && (!STRICT_ALIGNMENT
3594 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3595 try_int = true;
3596 else
3597 try_int = false;
3599 if (try_int)
3601 rtx_insn *ret;
3603 /* For memory to memory moves, optimal behavior can be had with the
3604 existing block move logic. But use normal expansion if optimizing
3605 for size. */
3606 if (MEM_P (x) && MEM_P (y))
3608 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3609 (optimize_insn_for_speed_p()
3610 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
3611 return get_last_insn ();
3614 ret = emit_move_via_integer (mode, x, y, true);
3615 if (ret)
3616 return ret;
3619 return emit_move_complex_parts (x, y);
3622 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3623 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3625 static rtx_insn *
3626 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3628 rtx_insn *ret;
3630 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3631 if (mode != CCmode)
3633 enum insn_code code = optab_handler (mov_optab, CCmode);
3634 if (code != CODE_FOR_nothing)
3636 x = emit_move_change_mode (CCmode, mode, x, true);
3637 y = emit_move_change_mode (CCmode, mode, y, true);
3638 return emit_insn (GEN_FCN (code) (x, y));
3642 /* Otherwise, find the MODE_INT mode of the same width. */
3643 ret = emit_move_via_integer (mode, x, y, false);
3644 gcc_assert (ret != NULL);
3645 return ret;
3648 /* Return true if word I of OP lies entirely in the
3649 undefined bits of a paradoxical subreg. */
3651 static bool
3652 undefined_operand_subword_p (const_rtx op, int i)
3654 if (GET_CODE (op) != SUBREG)
3655 return false;
3656 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3657 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3658 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3659 || known_le (offset, -UNITS_PER_WORD));
3662 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3663 MODE is any multi-word or full-word mode that lacks a move_insn
3664 pattern. Note that you will get better code if you define such
3665 patterns, even if they must turn into multiple assembler instructions. */
3667 static rtx_insn *
3668 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3670 rtx_insn *last_insn = 0;
3671 rtx_insn *seq;
3672 rtx inner;
3673 bool need_clobber;
3674 int i, mode_size;
3676 /* This function can only handle cases where the number of words is
3677 known at compile time. */
3678 mode_size = GET_MODE_SIZE (mode).to_constant ();
3679 gcc_assert (mode_size >= UNITS_PER_WORD);
3681 /* If X is a push on the stack, do the push now and replace
3682 X with a reference to the stack pointer. */
3683 if (push_operand (x, mode))
3684 x = emit_move_resolve_push (mode, x);
3686 /* If we are in reload, see if either operand is a MEM whose address
3687 is scheduled for replacement. */
3688 if (reload_in_progress && MEM_P (x)
3689 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3690 x = replace_equiv_address_nv (x, inner);
3691 if (reload_in_progress && MEM_P (y)
3692 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3693 y = replace_equiv_address_nv (y, inner);
3695 start_sequence ();
3697 need_clobber = false;
3698 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3700 /* Do not generate code for a move if it would go entirely
3701 to the non-existing bits of a paradoxical subreg. */
3702 if (undefined_operand_subword_p (x, i))
3703 continue;
3705 rtx xpart = operand_subword (x, i, 1, mode);
3706 rtx ypart;
3708 /* Do not generate code for a move if it would come entirely
3709 from the undefined bits of a paradoxical subreg. */
3710 if (undefined_operand_subword_p (y, i))
3711 continue;
3713 ypart = operand_subword (y, i, 1, mode);
3715 /* If we can't get a part of Y, put Y into memory if it is a
3716 constant. Otherwise, force it into a register. Then we must
3717 be able to get a part of Y. */
3718 if (ypart == 0 && CONSTANT_P (y))
3720 y = use_anchored_address (force_const_mem (mode, y));
3721 ypart = operand_subword (y, i, 1, mode);
3723 else if (ypart == 0)
3724 ypart = operand_subword_force (y, i, mode);
3726 gcc_assert (xpart && ypart);
3728 need_clobber |= (GET_CODE (xpart) == SUBREG);
3730 last_insn = emit_move_insn (xpart, ypart);
3733 seq = get_insns ();
3734 end_sequence ();
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 We never want to emit such a clobber after reload. */
3740 if (x != y
3741 && ! (reload_in_progress || reload_completed)
3742 && need_clobber != 0)
3743 emit_clobber (x);
3745 emit_insn (seq);
3747 return last_insn;
3750 /* Low level part of emit_move_insn.
3751 Called just like emit_move_insn, but assumes X and Y
3752 are basically valid. */
3754 rtx_insn *
3755 emit_move_insn_1 (rtx x, rtx y)
3757 machine_mode mode = GET_MODE (x);
3758 enum insn_code code;
3760 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3762 code = optab_handler (mov_optab, mode);
3763 if (code != CODE_FOR_nothing)
3764 return emit_insn (GEN_FCN (code) (x, y));
3766 /* Expand complex moves by moving real part and imag part. */
3767 if (COMPLEX_MODE_P (mode))
3768 return emit_move_complex (mode, x, y);
3770 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3771 || ALL_FIXED_POINT_MODE_P (mode))
3773 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3775 /* If we can't find an integer mode, use multi words. */
3776 if (result)
3777 return result;
3778 else
3779 return emit_move_multi_word (mode, x, y);
3782 if (GET_MODE_CLASS (mode) == MODE_CC)
3783 return emit_move_ccmode (mode, x, y);
3785 /* Try using a move pattern for the corresponding integer mode. This is
3786 only safe when simplify_subreg can convert MODE constants into integer
3787 constants. At present, it can only do this reliably if the value
3788 fits within a HOST_WIDE_INT. */
3789 if (!CONSTANT_P (y)
3790 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3792 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3794 if (ret)
3796 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3797 return ret;
3801 return emit_move_multi_word (mode, x, y);
3804 /* Generate code to copy Y into X.
3805 Both Y and X must have the same mode, except that
3806 Y can be a constant with VOIDmode.
3807 This mode cannot be BLKmode; use emit_block_move for that.
3809 Return the last instruction emitted. */
3811 rtx_insn *
3812 emit_move_insn (rtx x, rtx y)
3814 machine_mode mode = GET_MODE (x);
3815 rtx y_cst = NULL_RTX;
3816 rtx_insn *last_insn;
3817 rtx set;
3819 gcc_assert (mode != BLKmode
3820 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3822 /* If we have a copy that looks like one of the following patterns:
3823 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
3824 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
3825 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
3826 (set (subreg:M1 (reg:M2 ...)) (constant C))
3827 where mode M1 is equal in size to M2, try to detect whether the
3828 mode change involves an implicit round trip through memory.
3829 If so, see if we can avoid that by removing the subregs and
3830 doing the move in mode M2 instead. */
3832 rtx x_inner = NULL_RTX;
3833 rtx y_inner = NULL_RTX;
3835 auto candidate_subreg_p = [&](rtx subreg) {
3836 return (REG_P (SUBREG_REG (subreg))
3837 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
3838 GET_MODE_SIZE (GET_MODE (subreg)))
3839 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
3840 != CODE_FOR_nothing);
3843 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
3844 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
3845 && !push_operand (mem, GET_MODE (mem))
3846 /* Not a candiate if innermode requires too much alignment. */
3847 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
3848 || targetm.slow_unaligned_access (GET_MODE (mem),
3849 MEM_ALIGN (mem))
3850 || !targetm.slow_unaligned_access (innermode,
3851 MEM_ALIGN (mem))));
3854 if (SUBREG_P (x) && candidate_subreg_p (x))
3855 x_inner = SUBREG_REG (x);
3857 if (SUBREG_P (y) && candidate_subreg_p (y))
3858 y_inner = SUBREG_REG (y);
3860 if (x_inner != NULL_RTX
3861 && y_inner != NULL_RTX
3862 && GET_MODE (x_inner) == GET_MODE (y_inner)
3863 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
3865 x = x_inner;
3866 y = y_inner;
3867 mode = GET_MODE (x_inner);
3869 else if (x_inner != NULL_RTX
3870 && MEM_P (y)
3871 && candidate_mem_p (GET_MODE (x_inner), y))
3873 x = x_inner;
3874 y = adjust_address (y, GET_MODE (x_inner), 0);
3875 mode = GET_MODE (x_inner);
3877 else if (y_inner != NULL_RTX
3878 && MEM_P (x)
3879 && candidate_mem_p (GET_MODE (y_inner), x))
3881 x = adjust_address (x, GET_MODE (y_inner), 0);
3882 y = y_inner;
3883 mode = GET_MODE (y_inner);
3885 else if (x_inner != NULL_RTX
3886 && CONSTANT_P (y)
3887 && !targetm.can_change_mode_class (GET_MODE (x_inner),
3888 mode, ALL_REGS)
3889 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
3891 x = x_inner;
3892 y = y_inner;
3893 mode = GET_MODE (x_inner);
3896 if (CONSTANT_P (y))
3898 if (optimize
3899 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3900 && (last_insn = compress_float_constant (x, y)))
3901 return last_insn;
3903 y_cst = y;
3905 if (!targetm.legitimate_constant_p (mode, y))
3907 y = force_const_mem (mode, y);
3909 /* If the target's cannot_force_const_mem prevented the spill,
3910 assume that the target's move expanders will also take care
3911 of the non-legitimate constant. */
3912 if (!y)
3913 y = y_cst;
3914 else
3915 y = use_anchored_address (y);
3919 /* If X or Y are memory references, verify that their addresses are valid
3920 for the machine. */
3921 if (MEM_P (x)
3922 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3923 MEM_ADDR_SPACE (x))
3924 && ! push_operand (x, GET_MODE (x))))
3925 x = validize_mem (x);
3927 if (MEM_P (y)
3928 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3929 MEM_ADDR_SPACE (y)))
3930 y = validize_mem (y);
3932 gcc_assert (mode != BLKmode);
3934 last_insn = emit_move_insn_1 (x, y);
3936 if (y_cst && REG_P (x)
3937 && (set = single_set (last_insn)) != NULL_RTX
3938 && SET_DEST (set) == x
3939 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3940 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3942 return last_insn;
3945 /* Generate the body of an instruction to copy Y into X.
3946 It may be a list of insns, if one insn isn't enough. */
3948 rtx_insn *
3949 gen_move_insn (rtx x, rtx y)
3951 rtx_insn *seq;
3953 start_sequence ();
3954 emit_move_insn_1 (x, y);
3955 seq = get_insns ();
3956 end_sequence ();
3957 return seq;
3960 /* If Y is representable exactly in a narrower mode, and the target can
3961 perform the extension directly from constant or memory, then emit the
3962 move as an extension. */
3964 static rtx_insn *
3965 compress_float_constant (rtx x, rtx y)
3967 machine_mode dstmode = GET_MODE (x);
3968 machine_mode orig_srcmode = GET_MODE (y);
3969 machine_mode srcmode;
3970 const REAL_VALUE_TYPE *r;
3971 int oldcost, newcost;
3972 bool speed = optimize_insn_for_speed_p ();
3974 r = CONST_DOUBLE_REAL_VALUE (y);
3976 if (targetm.legitimate_constant_p (dstmode, y))
3977 oldcost = set_src_cost (y, orig_srcmode, speed);
3978 else
3979 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3981 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3983 enum insn_code ic;
3984 rtx trunc_y;
3985 rtx_insn *last_insn;
3987 /* Skip if the target can't extend this way. */
3988 ic = can_extend_p (dstmode, srcmode, 0);
3989 if (ic == CODE_FOR_nothing)
3990 continue;
3992 /* Skip if the narrowed value isn't exact. */
3993 if (! exact_real_truncate (srcmode, r))
3994 continue;
3996 trunc_y = const_double_from_real_value (*r, srcmode);
3998 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4000 /* Skip if the target needs extra instructions to perform
4001 the extension. */
4002 if (!insn_operand_matches (ic, 1, trunc_y))
4003 continue;
4004 /* This is valid, but may not be cheaper than the original. */
4005 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4006 dstmode, speed);
4007 if (oldcost < newcost)
4008 continue;
4010 else if (float_extend_from_mem[dstmode][srcmode])
4012 trunc_y = force_const_mem (srcmode, trunc_y);
4013 /* This is valid, but may not be cheaper than the original. */
4014 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4015 dstmode, speed);
4016 if (oldcost < newcost)
4017 continue;
4018 trunc_y = validize_mem (trunc_y);
4020 else
4021 continue;
4023 /* For CSE's benefit, force the compressed constant pool entry
4024 into a new pseudo. This constant may be used in different modes,
4025 and if not, combine will put things back together for us. */
4026 trunc_y = force_reg (srcmode, trunc_y);
4028 /* If x is a hard register, perform the extension into a pseudo,
4029 so that e.g. stack realignment code is aware of it. */
4030 rtx target = x;
4031 if (REG_P (x) && HARD_REGISTER_P (x))
4032 target = gen_reg_rtx (dstmode);
4034 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4035 last_insn = get_last_insn ();
4037 if (REG_P (target))
4038 set_unique_reg_note (last_insn, REG_EQUAL, y);
4040 if (target != x)
4041 return emit_move_insn (x, target);
4042 return last_insn;
4045 return NULL;
4048 /* Pushing data onto the stack. */
4050 /* Push a block of length SIZE (perhaps variable)
4051 and return an rtx to address the beginning of the block.
4052 The value may be virtual_outgoing_args_rtx.
4054 EXTRA is the number of bytes of padding to push in addition to SIZE.
4055 BELOW nonzero means this padding comes at low addresses;
4056 otherwise, the padding comes at high addresses. */
4059 push_block (rtx size, poly_int64 extra, int below)
4061 rtx temp;
4063 size = convert_modes (Pmode, ptr_mode, size, 1);
4064 if (CONSTANT_P (size))
4065 anti_adjust_stack (plus_constant (Pmode, size, extra));
4066 else if (REG_P (size) && known_eq (extra, 0))
4067 anti_adjust_stack (size);
4068 else
4070 temp = copy_to_mode_reg (Pmode, size);
4071 if (maybe_ne (extra, 0))
4072 temp = expand_binop (Pmode, add_optab, temp,
4073 gen_int_mode (extra, Pmode),
4074 temp, 0, OPTAB_LIB_WIDEN);
4075 anti_adjust_stack (temp);
4078 if (STACK_GROWS_DOWNWARD)
4080 temp = virtual_outgoing_args_rtx;
4081 if (maybe_ne (extra, 0) && below)
4082 temp = plus_constant (Pmode, temp, extra);
4084 else
4086 poly_int64 csize;
4087 if (poly_int_rtx_p (size, &csize))
4088 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4089 -csize - (below ? 0 : extra));
4090 else if (maybe_ne (extra, 0) && !below)
4091 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4092 negate_rtx (Pmode, plus_constant (Pmode, size,
4093 extra)));
4094 else
4095 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4096 negate_rtx (Pmode, size));
4099 return memory_address (NARROWEST_INT_MODE, temp);
4102 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4104 static rtx
4105 mem_autoinc_base (rtx mem)
4107 if (MEM_P (mem))
4109 rtx addr = XEXP (mem, 0);
4110 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4111 return XEXP (addr, 0);
4113 return NULL;
4116 /* A utility routine used here, in reload, and in try_split. The insns
4117 after PREV up to and including LAST are known to adjust the stack,
4118 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4119 placing notes as appropriate. PREV may be NULL, indicating the
4120 entire insn sequence prior to LAST should be scanned.
4122 The set of allowed stack pointer modifications is small:
4123 (1) One or more auto-inc style memory references (aka pushes),
4124 (2) One or more addition/subtraction with the SP as destination,
4125 (3) A single move insn with the SP as destination,
4126 (4) A call_pop insn,
4127 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4129 Insns in the sequence that do not modify the SP are ignored,
4130 except for noreturn calls.
4132 The return value is the amount of adjustment that can be trivially
4133 verified, via immediate operand or auto-inc. If the adjustment
4134 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4136 poly_int64
4137 find_args_size_adjust (rtx_insn *insn)
4139 rtx dest, set, pat;
4140 int i;
4142 pat = PATTERN (insn);
4143 set = NULL;
4145 /* Look for a call_pop pattern. */
4146 if (CALL_P (insn))
4148 /* We have to allow non-call_pop patterns for the case
4149 of emit_single_push_insn of a TLS address. */
4150 if (GET_CODE (pat) != PARALLEL)
4151 return 0;
4153 /* All call_pop have a stack pointer adjust in the parallel.
4154 The call itself is always first, and the stack adjust is
4155 usually last, so search from the end. */
4156 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4158 set = XVECEXP (pat, 0, i);
4159 if (GET_CODE (set) != SET)
4160 continue;
4161 dest = SET_DEST (set);
4162 if (dest == stack_pointer_rtx)
4163 break;
4165 /* We'd better have found the stack pointer adjust. */
4166 if (i == 0)
4167 return 0;
4168 /* Fall through to process the extracted SET and DEST
4169 as if it was a standalone insn. */
4171 else if (GET_CODE (pat) == SET)
4172 set = pat;
4173 else if ((set = single_set (insn)) != NULL)
4175 else if (GET_CODE (pat) == PARALLEL)
4177 /* ??? Some older ports use a parallel with a stack adjust
4178 and a store for a PUSH_ROUNDING pattern, rather than a
4179 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4180 /* ??? See h8300 and m68k, pushqi1. */
4181 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4183 set = XVECEXP (pat, 0, i);
4184 if (GET_CODE (set) != SET)
4185 continue;
4186 dest = SET_DEST (set);
4187 if (dest == stack_pointer_rtx)
4188 break;
4190 /* We do not expect an auto-inc of the sp in the parallel. */
4191 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4192 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4193 != stack_pointer_rtx);
4195 if (i < 0)
4196 return 0;
4198 else
4199 return 0;
4201 dest = SET_DEST (set);
4203 /* Look for direct modifications of the stack pointer. */
4204 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4206 /* Look for a trivial adjustment, otherwise assume nothing. */
4207 /* Note that the SPU restore_stack_block pattern refers to
4208 the stack pointer in V4SImode. Consider that non-trivial. */
4209 poly_int64 offset;
4210 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4211 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4212 return offset;
4213 /* ??? Reload can generate no-op moves, which will be cleaned
4214 up later. Recognize it and continue searching. */
4215 else if (rtx_equal_p (dest, SET_SRC (set)))
4216 return 0;
4217 else
4218 return HOST_WIDE_INT_MIN;
4220 else
4222 rtx mem, addr;
4224 /* Otherwise only think about autoinc patterns. */
4225 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4227 mem = dest;
4228 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4229 != stack_pointer_rtx);
4231 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4232 mem = SET_SRC (set);
4233 else
4234 return 0;
4236 addr = XEXP (mem, 0);
4237 switch (GET_CODE (addr))
4239 case PRE_INC:
4240 case POST_INC:
4241 return GET_MODE_SIZE (GET_MODE (mem));
4242 case PRE_DEC:
4243 case POST_DEC:
4244 return -GET_MODE_SIZE (GET_MODE (mem));
4245 case PRE_MODIFY:
4246 case POST_MODIFY:
4247 addr = XEXP (addr, 1);
4248 gcc_assert (GET_CODE (addr) == PLUS);
4249 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4250 return rtx_to_poly_int64 (XEXP (addr, 1));
4251 default:
4252 gcc_unreachable ();
4257 poly_int64
4258 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4259 poly_int64 end_args_size)
4261 poly_int64 args_size = end_args_size;
4262 bool saw_unknown = false;
4263 rtx_insn *insn;
4265 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4267 if (!NONDEBUG_INSN_P (insn))
4268 continue;
4270 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4271 a call argument containing a TLS address that itself requires
4272 a call to __tls_get_addr. The handling of stack_pointer_delta
4273 in emit_single_push_insn is supposed to ensure that any such
4274 notes are already correct. */
4275 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4276 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4278 poly_int64 this_delta = find_args_size_adjust (insn);
4279 if (known_eq (this_delta, 0))
4281 if (!CALL_P (insn)
4282 || ACCUMULATE_OUTGOING_ARGS
4283 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4284 continue;
4287 gcc_assert (!saw_unknown);
4288 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4289 saw_unknown = true;
4291 if (!note)
4292 add_args_size_note (insn, args_size);
4293 if (STACK_GROWS_DOWNWARD)
4294 this_delta = -poly_uint64 (this_delta);
4296 if (saw_unknown)
4297 args_size = HOST_WIDE_INT_MIN;
4298 else
4299 args_size -= this_delta;
4302 return args_size;
4305 #ifdef PUSH_ROUNDING
4306 /* Emit single push insn. */
4308 static void
4309 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4311 rtx dest_addr;
4312 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4313 rtx dest;
4314 enum insn_code icode;
4316 /* If there is push pattern, use it. Otherwise try old way of throwing
4317 MEM representing push operation to move expander. */
4318 icode = optab_handler (push_optab, mode);
4319 if (icode != CODE_FOR_nothing)
4321 class expand_operand ops[1];
4323 create_input_operand (&ops[0], x, mode);
4324 if (maybe_expand_insn (icode, 1, ops))
4325 return;
4327 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4328 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4329 /* If we are to pad downward, adjust the stack pointer first and
4330 then store X into the stack location using an offset. This is
4331 because emit_move_insn does not know how to pad; it does not have
4332 access to type. */
4333 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4335 emit_move_insn (stack_pointer_rtx,
4336 expand_binop (Pmode,
4337 STACK_GROWS_DOWNWARD ? sub_optab
4338 : add_optab,
4339 stack_pointer_rtx,
4340 gen_int_mode (rounded_size, Pmode),
4341 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4343 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4344 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4345 /* We have already decremented the stack pointer, so get the
4346 previous value. */
4347 offset += rounded_size;
4349 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4350 /* We have already incremented the stack pointer, so get the
4351 previous value. */
4352 offset -= rounded_size;
4354 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4356 else
4358 if (STACK_GROWS_DOWNWARD)
4359 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4360 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4361 else
4362 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4363 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4365 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4368 dest = gen_rtx_MEM (mode, dest_addr);
4370 if (type != 0)
4372 set_mem_attributes (dest, type, 1);
4374 if (cfun->tail_call_marked)
4375 /* Function incoming arguments may overlap with sibling call
4376 outgoing arguments and we cannot allow reordering of reads
4377 from function arguments with stores to outgoing arguments
4378 of sibling calls. */
4379 set_mem_alias_set (dest, 0);
4381 emit_move_insn (dest, x);
4384 /* Emit and annotate a single push insn. */
4386 static void
4387 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4389 poly_int64 delta, old_delta = stack_pointer_delta;
4390 rtx_insn *prev = get_last_insn ();
4391 rtx_insn *last;
4393 emit_single_push_insn_1 (mode, x, type);
4395 /* Adjust stack_pointer_delta to describe the situation after the push
4396 we just performed. Note that we must do this after the push rather
4397 than before the push in case calculating X needs pushes and pops of
4398 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4399 for such pushes and pops must not include the effect of the future
4400 push of X. */
4401 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4403 last = get_last_insn ();
4405 /* Notice the common case where we emitted exactly one insn. */
4406 if (PREV_INSN (last) == prev)
4408 add_args_size_note (last, stack_pointer_delta);
4409 return;
4412 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4413 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4414 || known_eq (delta, old_delta));
4416 #endif
4418 /* If reading SIZE bytes from X will end up reading from
4419 Y return the number of bytes that overlap. Return -1
4420 if there is no overlap or -2 if we can't determine
4421 (for example when X and Y have different base registers). */
4423 static int
4424 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4426 rtx tmp = plus_constant (Pmode, x, size);
4427 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4429 if (!CONST_INT_P (sub))
4430 return -2;
4432 HOST_WIDE_INT val = INTVAL (sub);
4434 return IN_RANGE (val, 1, size) ? val : -1;
4437 /* Generate code to push X onto the stack, assuming it has mode MODE and
4438 type TYPE.
4439 MODE is redundant except when X is a CONST_INT (since they don't
4440 carry mode info).
4441 SIZE is an rtx for the size of data to be copied (in bytes),
4442 needed only if X is BLKmode.
4443 Return true if successful. May return false if asked to push a
4444 partial argument during a sibcall optimization (as specified by
4445 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4446 to not overlap.
4448 ALIGN (in bits) is maximum alignment we can assume.
4450 If PARTIAL and REG are both nonzero, then copy that many of the first
4451 bytes of X into registers starting with REG, and push the rest of X.
4452 The amount of space pushed is decreased by PARTIAL bytes.
4453 REG must be a hard register in this case.
4454 If REG is zero but PARTIAL is not, take any all others actions for an
4455 argument partially in registers, but do not actually load any
4456 registers.
4458 EXTRA is the amount in bytes of extra space to leave next to this arg.
4459 This is ignored if an argument block has already been allocated.
4461 On a machine that lacks real push insns, ARGS_ADDR is the address of
4462 the bottom of the argument block for this call. We use indexing off there
4463 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4464 argument block has not been preallocated.
4466 ARGS_SO_FAR is the size of args previously pushed for this call.
4468 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4469 for arguments passed in registers. If nonzero, it will be the number
4470 of bytes required. */
4472 bool
4473 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4474 unsigned int align, int partial, rtx reg, poly_int64 extra,
4475 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4476 rtx alignment_pad, bool sibcall_p)
4478 rtx xinner;
4479 pad_direction stack_direction
4480 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4482 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4483 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4484 Default is below for small data on big-endian machines; else above. */
4485 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4487 /* Invert direction if stack is post-decrement.
4488 FIXME: why? */
4489 if (STACK_PUSH_CODE == POST_DEC)
4490 if (where_pad != PAD_NONE)
4491 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4493 xinner = x;
4495 int nregs = partial / UNITS_PER_WORD;
4496 rtx *tmp_regs = NULL;
4497 int overlapping = 0;
4499 if (mode == BLKmode
4500 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4502 /* Copy a block into the stack, entirely or partially. */
4504 rtx temp;
4505 int used;
4506 int offset;
4507 int skip;
4509 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4510 used = partial - offset;
4512 if (mode != BLKmode)
4514 /* A value is to be stored in an insufficiently aligned
4515 stack slot; copy via a suitably aligned slot if
4516 necessary. */
4517 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4518 if (!MEM_P (xinner))
4520 temp = assign_temp (type, 1, 1);
4521 emit_move_insn (temp, xinner);
4522 xinner = temp;
4526 gcc_assert (size);
4528 /* USED is now the # of bytes we need not copy to the stack
4529 because registers will take care of them. */
4531 if (partial != 0)
4532 xinner = adjust_address (xinner, BLKmode, used);
4534 /* If the partial register-part of the arg counts in its stack size,
4535 skip the part of stack space corresponding to the registers.
4536 Otherwise, start copying to the beginning of the stack space,
4537 by setting SKIP to 0. */
4538 skip = (reg_parm_stack_space == 0) ? 0 : used;
4540 #ifdef PUSH_ROUNDING
4541 /* Do it with several push insns if that doesn't take lots of insns
4542 and if there is no difficulty with push insns that skip bytes
4543 on the stack for alignment purposes. */
4544 if (args_addr == 0
4545 && PUSH_ARGS
4546 && CONST_INT_P (size)
4547 && skip == 0
4548 && MEM_ALIGN (xinner) >= align
4549 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4550 /* Here we avoid the case of a structure whose weak alignment
4551 forces many pushes of a small amount of data,
4552 and such small pushes do rounding that causes trouble. */
4553 && ((!targetm.slow_unaligned_access (word_mode, align))
4554 || align >= BIGGEST_ALIGNMENT
4555 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4556 align / BITS_PER_UNIT))
4557 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4559 /* Push padding now if padding above and stack grows down,
4560 or if padding below and stack grows up.
4561 But if space already allocated, this has already been done. */
4562 if (maybe_ne (extra, 0)
4563 && args_addr == 0
4564 && where_pad != PAD_NONE
4565 && where_pad != stack_direction)
4566 anti_adjust_stack (gen_int_mode (extra, Pmode));
4568 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4569 RETURN_BEGIN);
4571 else
4572 #endif /* PUSH_ROUNDING */
4574 rtx target;
4576 /* Otherwise make space on the stack and copy the data
4577 to the address of that space. */
4579 /* Deduct words put into registers from the size we must copy. */
4580 if (partial != 0)
4582 if (CONST_INT_P (size))
4583 size = GEN_INT (INTVAL (size) - used);
4584 else
4585 size = expand_binop (GET_MODE (size), sub_optab, size,
4586 gen_int_mode (used, GET_MODE (size)),
4587 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4590 /* Get the address of the stack space.
4591 In this case, we do not deal with EXTRA separately.
4592 A single stack adjust will do. */
4593 poly_int64 const_args_so_far;
4594 if (! args_addr)
4596 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4597 extra = 0;
4599 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
4600 temp = memory_address (BLKmode,
4601 plus_constant (Pmode, args_addr,
4602 skip + const_args_so_far));
4603 else
4604 temp = memory_address (BLKmode,
4605 plus_constant (Pmode,
4606 gen_rtx_PLUS (Pmode,
4607 args_addr,
4608 args_so_far),
4609 skip));
4611 if (!ACCUMULATE_OUTGOING_ARGS)
4613 /* If the source is referenced relative to the stack pointer,
4614 copy it to another register to stabilize it. We do not need
4615 to do this if we know that we won't be changing sp. */
4617 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4618 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4619 temp = copy_to_reg (temp);
4622 target = gen_rtx_MEM (BLKmode, temp);
4624 /* We do *not* set_mem_attributes here, because incoming arguments
4625 may overlap with sibling call outgoing arguments and we cannot
4626 allow reordering of reads from function arguments with stores
4627 to outgoing arguments of sibling calls. We do, however, want
4628 to record the alignment of the stack slot. */
4629 /* ALIGN may well be better aligned than TYPE, e.g. due to
4630 PARM_BOUNDARY. Assume the caller isn't lying. */
4631 set_mem_align (target, align);
4633 /* If part should go in registers and pushing to that part would
4634 overwrite some of the values that need to go into regs, load the
4635 overlapping values into temporary pseudos to be moved into the hard
4636 regs at the end after the stack pushing has completed.
4637 We cannot load them directly into the hard regs here because
4638 they can be clobbered by the block move expansions.
4639 See PR 65358. */
4641 if (partial > 0 && reg != 0 && mode == BLKmode
4642 && GET_CODE (reg) != PARALLEL)
4644 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4645 if (overlapping > 0)
4647 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4648 overlapping /= UNITS_PER_WORD;
4650 tmp_regs = XALLOCAVEC (rtx, overlapping);
4652 for (int i = 0; i < overlapping; i++)
4653 tmp_regs[i] = gen_reg_rtx (word_mode);
4655 for (int i = 0; i < overlapping; i++)
4656 emit_move_insn (tmp_regs[i],
4657 operand_subword_force (target, i, mode));
4659 else if (overlapping == -1)
4660 overlapping = 0;
4661 /* Could not determine whether there is overlap.
4662 Fail the sibcall. */
4663 else
4665 overlapping = 0;
4666 if (sibcall_p)
4667 return false;
4670 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4673 else if (partial > 0)
4675 /* Scalar partly in registers. This case is only supported
4676 for fixed-wdth modes. */
4677 int num_words = GET_MODE_SIZE (mode).to_constant ();
4678 num_words /= UNITS_PER_WORD;
4679 int i;
4680 int not_stack;
4681 /* # bytes of start of argument
4682 that we must make space for but need not store. */
4683 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4684 int args_offset = INTVAL (args_so_far);
4685 int skip;
4687 /* Push padding now if padding above and stack grows down,
4688 or if padding below and stack grows up.
4689 But if space already allocated, this has already been done. */
4690 if (maybe_ne (extra, 0)
4691 && args_addr == 0
4692 && where_pad != PAD_NONE
4693 && where_pad != stack_direction)
4694 anti_adjust_stack (gen_int_mode (extra, Pmode));
4696 /* If we make space by pushing it, we might as well push
4697 the real data. Otherwise, we can leave OFFSET nonzero
4698 and leave the space uninitialized. */
4699 if (args_addr == 0)
4700 offset = 0;
4702 /* Now NOT_STACK gets the number of words that we don't need to
4703 allocate on the stack. Convert OFFSET to words too. */
4704 not_stack = (partial - offset) / UNITS_PER_WORD;
4705 offset /= UNITS_PER_WORD;
4707 /* If the partial register-part of the arg counts in its stack size,
4708 skip the part of stack space corresponding to the registers.
4709 Otherwise, start copying to the beginning of the stack space,
4710 by setting SKIP to 0. */
4711 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4713 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4714 x = validize_mem (force_const_mem (mode, x));
4716 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4717 SUBREGs of such registers are not allowed. */
4718 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4719 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4720 x = copy_to_reg (x);
4722 /* Loop over all the words allocated on the stack for this arg. */
4723 /* We can do it by words, because any scalar bigger than a word
4724 has a size a multiple of a word. */
4725 for (i = num_words - 1; i >= not_stack; i--)
4726 if (i >= not_stack + offset)
4727 if (!emit_push_insn (operand_subword_force (x, i, mode),
4728 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4729 0, args_addr,
4730 GEN_INT (args_offset + ((i - not_stack + skip)
4731 * UNITS_PER_WORD)),
4732 reg_parm_stack_space, alignment_pad, sibcall_p))
4733 return false;
4735 else
4737 rtx addr;
4738 rtx dest;
4740 /* Push padding now if padding above and stack grows down,
4741 or if padding below and stack grows up.
4742 But if space already allocated, this has already been done. */
4743 if (maybe_ne (extra, 0)
4744 && args_addr == 0
4745 && where_pad != PAD_NONE
4746 && where_pad != stack_direction)
4747 anti_adjust_stack (gen_int_mode (extra, Pmode));
4749 #ifdef PUSH_ROUNDING
4750 if (args_addr == 0 && PUSH_ARGS)
4751 emit_single_push_insn (mode, x, type);
4752 else
4753 #endif
4755 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4756 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4758 /* We do *not* set_mem_attributes here, because incoming arguments
4759 may overlap with sibling call outgoing arguments and we cannot
4760 allow reordering of reads from function arguments with stores
4761 to outgoing arguments of sibling calls. We do, however, want
4762 to record the alignment of the stack slot. */
4763 /* ALIGN may well be better aligned than TYPE, e.g. due to
4764 PARM_BOUNDARY. Assume the caller isn't lying. */
4765 set_mem_align (dest, align);
4767 emit_move_insn (dest, x);
4771 /* Move the partial arguments into the registers and any overlapping
4772 values that we moved into the pseudos in tmp_regs. */
4773 if (partial > 0 && reg != 0)
4775 /* Handle calls that pass values in multiple non-contiguous locations.
4776 The Irix 6 ABI has examples of this. */
4777 if (GET_CODE (reg) == PARALLEL)
4778 emit_group_load (reg, x, type, -1);
4779 else
4781 gcc_assert (partial % UNITS_PER_WORD == 0);
4782 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4784 for (int i = 0; i < overlapping; i++)
4785 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4786 + nregs - overlapping + i),
4787 tmp_regs[i]);
4792 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4793 anti_adjust_stack (gen_int_mode (extra, Pmode));
4795 if (alignment_pad && args_addr == 0)
4796 anti_adjust_stack (alignment_pad);
4798 return true;
4801 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4802 operations. */
4804 static rtx
4805 get_subtarget (rtx x)
4807 return (optimize
4808 || x == 0
4809 /* Only registers can be subtargets. */
4810 || !REG_P (x)
4811 /* Don't use hard regs to avoid extending their life. */
4812 || REGNO (x) < FIRST_PSEUDO_REGISTER
4813 ? 0 : x);
4816 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4817 FIELD is a bitfield. Returns true if the optimization was successful,
4818 and there's nothing else to do. */
4820 static bool
4821 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4822 poly_uint64 pbitpos,
4823 poly_uint64 pbitregion_start,
4824 poly_uint64 pbitregion_end,
4825 machine_mode mode1, rtx str_rtx,
4826 tree to, tree src, bool reverse)
4828 /* str_mode is not guaranteed to be a scalar type. */
4829 machine_mode str_mode = GET_MODE (str_rtx);
4830 unsigned int str_bitsize;
4831 tree op0, op1;
4832 rtx value, result;
4833 optab binop;
4834 gimple *srcstmt;
4835 enum tree_code code;
4837 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4838 if (mode1 != VOIDmode
4839 || !pbitsize.is_constant (&bitsize)
4840 || !pbitpos.is_constant (&bitpos)
4841 || !pbitregion_start.is_constant (&bitregion_start)
4842 || !pbitregion_end.is_constant (&bitregion_end)
4843 || bitsize >= BITS_PER_WORD
4844 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
4845 || str_bitsize > BITS_PER_WORD
4846 || TREE_SIDE_EFFECTS (to)
4847 || TREE_THIS_VOLATILE (to))
4848 return false;
4850 STRIP_NOPS (src);
4851 if (TREE_CODE (src) != SSA_NAME)
4852 return false;
4853 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4854 return false;
4856 srcstmt = get_gimple_for_ssa_name (src);
4857 if (!srcstmt
4858 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4859 return false;
4861 code = gimple_assign_rhs_code (srcstmt);
4863 op0 = gimple_assign_rhs1 (srcstmt);
4865 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4866 to find its initialization. Hopefully the initialization will
4867 be from a bitfield load. */
4868 if (TREE_CODE (op0) == SSA_NAME)
4870 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4872 /* We want to eventually have OP0 be the same as TO, which
4873 should be a bitfield. */
4874 if (!op0stmt
4875 || !is_gimple_assign (op0stmt)
4876 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4877 return false;
4878 op0 = gimple_assign_rhs1 (op0stmt);
4881 op1 = gimple_assign_rhs2 (srcstmt);
4883 if (!operand_equal_p (to, op0, 0))
4884 return false;
4886 if (MEM_P (str_rtx))
4888 unsigned HOST_WIDE_INT offset1;
4890 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4891 str_bitsize = BITS_PER_WORD;
4893 scalar_int_mode best_mode;
4894 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4895 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4896 return false;
4897 str_mode = best_mode;
4898 str_bitsize = GET_MODE_BITSIZE (best_mode);
4900 offset1 = bitpos;
4901 bitpos %= str_bitsize;
4902 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4903 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4905 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4906 return false;
4908 /* If the bit field covers the whole REG/MEM, store_field
4909 will likely generate better code. */
4910 if (bitsize >= str_bitsize)
4911 return false;
4913 /* We can't handle fields split across multiple entities. */
4914 if (bitpos + bitsize > str_bitsize)
4915 return false;
4917 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4918 bitpos = str_bitsize - bitpos - bitsize;
4920 switch (code)
4922 case PLUS_EXPR:
4923 case MINUS_EXPR:
4924 /* For now, just optimize the case of the topmost bitfield
4925 where we don't need to do any masking and also
4926 1 bit bitfields where xor can be used.
4927 We might win by one instruction for the other bitfields
4928 too if insv/extv instructions aren't used, so that
4929 can be added later. */
4930 if ((reverse || bitpos + bitsize != str_bitsize)
4931 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4932 break;
4934 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4935 value = convert_modes (str_mode,
4936 TYPE_MODE (TREE_TYPE (op1)), value,
4937 TYPE_UNSIGNED (TREE_TYPE (op1)));
4939 /* We may be accessing data outside the field, which means
4940 we can alias adjacent data. */
4941 if (MEM_P (str_rtx))
4943 str_rtx = shallow_copy_rtx (str_rtx);
4944 set_mem_alias_set (str_rtx, 0);
4945 set_mem_expr (str_rtx, 0);
4948 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4950 value = expand_and (str_mode, value, const1_rtx, NULL);
4951 binop = xor_optab;
4953 else
4954 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4956 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4957 if (reverse)
4958 value = flip_storage_order (str_mode, value);
4959 result = expand_binop (str_mode, binop, str_rtx,
4960 value, str_rtx, 1, OPTAB_WIDEN);
4961 if (result != str_rtx)
4962 emit_move_insn (str_rtx, result);
4963 return true;
4965 case BIT_IOR_EXPR:
4966 case BIT_XOR_EXPR:
4967 if (TREE_CODE (op1) != INTEGER_CST)
4968 break;
4969 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4970 value = convert_modes (str_mode,
4971 TYPE_MODE (TREE_TYPE (op1)), value,
4972 TYPE_UNSIGNED (TREE_TYPE (op1)));
4974 /* We may be accessing data outside the field, which means
4975 we can alias adjacent data. */
4976 if (MEM_P (str_rtx))
4978 str_rtx = shallow_copy_rtx (str_rtx);
4979 set_mem_alias_set (str_rtx, 0);
4980 set_mem_expr (str_rtx, 0);
4983 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4984 if (bitpos + bitsize != str_bitsize)
4986 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4987 str_mode);
4988 value = expand_and (str_mode, value, mask, NULL_RTX);
4990 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4991 if (reverse)
4992 value = flip_storage_order (str_mode, value);
4993 result = expand_binop (str_mode, binop, str_rtx,
4994 value, str_rtx, 1, OPTAB_WIDEN);
4995 if (result != str_rtx)
4996 emit_move_insn (str_rtx, result);
4997 return true;
4999 default:
5000 break;
5003 return false;
5006 /* In the C++ memory model, consecutive bit fields in a structure are
5007 considered one memory location.
5009 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5010 returns the bit range of consecutive bits in which this COMPONENT_REF
5011 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5012 and *OFFSET may be adjusted in the process.
5014 If the access does not need to be restricted, 0 is returned in both
5015 *BITSTART and *BITEND. */
5017 void
5018 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5019 poly_int64_pod *bitpos, tree *offset)
5021 poly_int64 bitoffset;
5022 tree field, repr;
5024 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5026 field = TREE_OPERAND (exp, 1);
5027 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5028 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5029 need to limit the range we can access. */
5030 if (!repr)
5032 *bitstart = *bitend = 0;
5033 return;
5036 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5037 part of a larger bit field, then the representative does not serve any
5038 useful purpose. This can occur in Ada. */
5039 if (handled_component_p (TREE_OPERAND (exp, 0)))
5041 machine_mode rmode;
5042 poly_int64 rbitsize, rbitpos;
5043 tree roffset;
5044 int unsignedp, reversep, volatilep = 0;
5045 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5046 &roffset, &rmode, &unsignedp, &reversep,
5047 &volatilep);
5048 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5050 *bitstart = *bitend = 0;
5051 return;
5055 /* Compute the adjustment to bitpos from the offset of the field
5056 relative to the representative. DECL_FIELD_OFFSET of field and
5057 repr are the same by construction if they are not constants,
5058 see finish_bitfield_layout. */
5059 poly_uint64 field_offset, repr_offset;
5060 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5061 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5062 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5063 else
5064 bitoffset = 0;
5065 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5066 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5068 /* If the adjustment is larger than bitpos, we would have a negative bit
5069 position for the lower bound and this may wreak havoc later. Adjust
5070 offset and bitpos to make the lower bound non-negative in that case. */
5071 if (maybe_gt (bitoffset, *bitpos))
5073 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5074 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5076 *bitpos += adjust_bits;
5077 if (*offset == NULL_TREE)
5078 *offset = size_int (-adjust_bytes);
5079 else
5080 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5081 *bitstart = 0;
5083 else
5084 *bitstart = *bitpos - bitoffset;
5086 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5089 /* Returns true if BASE is a DECL that does not reside in memory and
5090 has non-BLKmode. DECL_RTL must not be a MEM; if
5091 DECL_RTL was not set yet, return false. */
5093 static inline bool
5094 non_mem_decl_p (tree base)
5096 if (!DECL_P (base)
5097 || TREE_ADDRESSABLE (base)
5098 || DECL_MODE (base) == BLKmode)
5099 return false;
5101 if (!DECL_RTL_SET_P (base))
5102 return false;
5104 return (!MEM_P (DECL_RTL (base)));
5107 /* Returns true if REF refers to an object that does not
5108 reside in memory and has non-BLKmode. */
5110 static inline bool
5111 mem_ref_refers_to_non_mem_p (tree ref)
5113 tree base;
5115 if (TREE_CODE (ref) == MEM_REF
5116 || TREE_CODE (ref) == TARGET_MEM_REF)
5118 tree addr = TREE_OPERAND (ref, 0);
5120 if (TREE_CODE (addr) != ADDR_EXPR)
5121 return false;
5123 base = TREE_OPERAND (addr, 0);
5125 else
5126 base = ref;
5128 return non_mem_decl_p (base);
5131 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5132 is true, try generating a nontemporal store. */
5134 void
5135 expand_assignment (tree to, tree from, bool nontemporal)
5137 rtx to_rtx = 0;
5138 rtx result;
5139 machine_mode mode;
5140 unsigned int align;
5141 enum insn_code icode;
5143 /* Don't crash if the lhs of the assignment was erroneous. */
5144 if (TREE_CODE (to) == ERROR_MARK)
5146 expand_normal (from);
5147 return;
5150 /* Optimize away no-op moves without side-effects. */
5151 if (operand_equal_p (to, from, 0))
5152 return;
5154 /* Handle misaligned stores. */
5155 mode = TYPE_MODE (TREE_TYPE (to));
5156 if ((TREE_CODE (to) == MEM_REF
5157 || TREE_CODE (to) == TARGET_MEM_REF
5158 || DECL_P (to))
5159 && mode != BLKmode
5160 && !mem_ref_refers_to_non_mem_p (to)
5161 && ((align = get_object_alignment (to))
5162 < GET_MODE_ALIGNMENT (mode))
5163 && (((icode = optab_handler (movmisalign_optab, mode))
5164 != CODE_FOR_nothing)
5165 || targetm.slow_unaligned_access (mode, align)))
5167 rtx reg, mem;
5169 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5170 /* Handle PARALLEL. */
5171 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5172 reg = force_not_mem (reg);
5173 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5174 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5175 reg = flip_storage_order (mode, reg);
5177 if (icode != CODE_FOR_nothing)
5179 class expand_operand ops[2];
5181 create_fixed_operand (&ops[0], mem);
5182 create_input_operand (&ops[1], reg, mode);
5183 /* The movmisalign<mode> pattern cannot fail, else the assignment
5184 would silently be omitted. */
5185 expand_insn (icode, 2, ops);
5187 else
5188 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5189 false);
5190 return;
5193 /* Assignment of a structure component needs special treatment
5194 if the structure component's rtx is not simply a MEM.
5195 Assignment of an array element at a constant index, and assignment of
5196 an array element in an unaligned packed structure field, has the same
5197 problem. Same for (partially) storing into a non-memory object. */
5198 if (handled_component_p (to)
5199 || (TREE_CODE (to) == MEM_REF
5200 && (REF_REVERSE_STORAGE_ORDER (to)
5201 || mem_ref_refers_to_non_mem_p (to)))
5202 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5204 machine_mode mode1;
5205 poly_int64 bitsize, bitpos;
5206 poly_uint64 bitregion_start = 0;
5207 poly_uint64 bitregion_end = 0;
5208 tree offset;
5209 int unsignedp, reversep, volatilep = 0;
5210 tree tem;
5212 push_temp_slots ();
5213 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5214 &unsignedp, &reversep, &volatilep);
5216 /* Make sure bitpos is not negative, it can wreak havoc later. */
5217 if (maybe_lt (bitpos, 0))
5219 gcc_assert (offset == NULL_TREE);
5220 offset = size_int (bits_to_bytes_round_down (bitpos));
5221 bitpos = num_trailing_bits (bitpos);
5224 if (TREE_CODE (to) == COMPONENT_REF
5225 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5226 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5227 /* The C++ memory model naturally applies to byte-aligned fields.
5228 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5229 BITSIZE are not byte-aligned, there is no need to limit the range
5230 we can access. This can occur with packed structures in Ada. */
5231 else if (maybe_gt (bitsize, 0)
5232 && multiple_p (bitsize, BITS_PER_UNIT)
5233 && multiple_p (bitpos, BITS_PER_UNIT))
5235 bitregion_start = bitpos;
5236 bitregion_end = bitpos + bitsize - 1;
5239 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5241 /* If the field has a mode, we want to access it in the
5242 field's mode, not the computed mode.
5243 If a MEM has VOIDmode (external with incomplete type),
5244 use BLKmode for it instead. */
5245 if (MEM_P (to_rtx))
5247 if (mode1 != VOIDmode)
5248 to_rtx = adjust_address (to_rtx, mode1, 0);
5249 else if (GET_MODE (to_rtx) == VOIDmode)
5250 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5253 if (offset != 0)
5255 machine_mode address_mode;
5256 rtx offset_rtx;
5258 if (!MEM_P (to_rtx))
5260 /* We can get constant negative offsets into arrays with broken
5261 user code. Translate this to a trap instead of ICEing. */
5262 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5263 expand_builtin_trap ();
5264 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5267 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5268 address_mode = get_address_mode (to_rtx);
5269 if (GET_MODE (offset_rtx) != address_mode)
5271 /* We cannot be sure that the RTL in offset_rtx is valid outside
5272 of a memory address context, so force it into a register
5273 before attempting to convert it to the desired mode. */
5274 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5275 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5278 /* If we have an expression in OFFSET_RTX and a non-zero
5279 byte offset in BITPOS, adding the byte offset before the
5280 OFFSET_RTX results in better intermediate code, which makes
5281 later rtl optimization passes perform better.
5283 We prefer intermediate code like this:
5285 r124:DI=r123:DI+0x18
5286 [r124:DI]=r121:DI
5288 ... instead of ...
5290 r124:DI=r123:DI+0x10
5291 [r124:DI+0x8]=r121:DI
5293 This is only done for aligned data values, as these can
5294 be expected to result in single move instructions. */
5295 poly_int64 bytepos;
5296 if (mode1 != VOIDmode
5297 && maybe_ne (bitpos, 0)
5298 && maybe_gt (bitsize, 0)
5299 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5300 && multiple_p (bitpos, bitsize)
5301 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5302 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5304 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5305 bitregion_start = 0;
5306 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5307 bitregion_end -= bitpos;
5308 bitpos = 0;
5311 to_rtx = offset_address (to_rtx, offset_rtx,
5312 highest_pow2_factor_for_target (to,
5313 offset));
5316 /* No action is needed if the target is not a memory and the field
5317 lies completely outside that target. This can occur if the source
5318 code contains an out-of-bounds access to a small array. */
5319 if (!MEM_P (to_rtx)
5320 && GET_MODE (to_rtx) != BLKmode
5321 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5323 expand_normal (from);
5324 result = NULL;
5326 /* Handle expand_expr of a complex value returning a CONCAT. */
5327 else if (GET_CODE (to_rtx) == CONCAT)
5329 machine_mode to_mode = GET_MODE (to_rtx);
5330 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5331 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5332 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5333 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5334 && known_eq (bitpos, 0)
5335 && known_eq (bitsize, mode_bitsize))
5336 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5337 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5338 && known_eq (bitsize, inner_bitsize)
5339 && (known_eq (bitpos, 0)
5340 || known_eq (bitpos, inner_bitsize)))
5341 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5342 false, nontemporal, reversep);
5343 else if (known_le (bitpos + bitsize, inner_bitsize))
5344 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5345 bitregion_start, bitregion_end,
5346 mode1, from, get_alias_set (to),
5347 nontemporal, reversep);
5348 else if (known_ge (bitpos, inner_bitsize))
5349 result = store_field (XEXP (to_rtx, 1), bitsize,
5350 bitpos - inner_bitsize,
5351 bitregion_start, bitregion_end,
5352 mode1, from, get_alias_set (to),
5353 nontemporal, reversep);
5354 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5356 result = expand_normal (from);
5357 if (GET_CODE (result) == CONCAT)
5359 to_mode = GET_MODE_INNER (to_mode);
5360 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5361 rtx from_real
5362 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5363 from_mode, 0);
5364 rtx from_imag
5365 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5366 from_mode, 0);
5367 if (!from_real || !from_imag)
5368 goto concat_store_slow;
5369 emit_move_insn (XEXP (to_rtx, 0), from_real);
5370 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5372 else
5374 machine_mode from_mode
5375 = GET_MODE (result) == VOIDmode
5376 ? TYPE_MODE (TREE_TYPE (from))
5377 : GET_MODE (result);
5378 rtx from_rtx;
5379 if (MEM_P (result))
5380 from_rtx = change_address (result, to_mode, NULL_RTX);
5381 else
5382 from_rtx
5383 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5384 if (from_rtx)
5386 emit_move_insn (XEXP (to_rtx, 0),
5387 read_complex_part (from_rtx, false));
5388 emit_move_insn (XEXP (to_rtx, 1),
5389 read_complex_part (from_rtx, true));
5391 else
5393 to_mode = GET_MODE_INNER (to_mode);
5394 rtx from_real
5395 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5396 rtx from_imag
5397 = simplify_gen_subreg (to_mode, result, from_mode,
5398 GET_MODE_SIZE (to_mode));
5399 if (!from_real || !from_imag)
5400 goto concat_store_slow;
5401 emit_move_insn (XEXP (to_rtx, 0), from_real);
5402 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5406 else
5408 concat_store_slow:;
5409 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5410 GET_MODE_SIZE (GET_MODE (to_rtx)));
5411 write_complex_part (temp, XEXP (to_rtx, 0), false);
5412 write_complex_part (temp, XEXP (to_rtx, 1), true);
5413 result = store_field (temp, bitsize, bitpos,
5414 bitregion_start, bitregion_end,
5415 mode1, from, get_alias_set (to),
5416 nontemporal, reversep);
5417 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5418 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5421 /* For calls to functions returning variable length structures, if TO_RTX
5422 is not a MEM, go through a MEM because we must not create temporaries
5423 of the VLA type. */
5424 else if (!MEM_P (to_rtx)
5425 && TREE_CODE (from) == CALL_EXPR
5426 && COMPLETE_TYPE_P (TREE_TYPE (from))
5427 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5429 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5430 GET_MODE_SIZE (GET_MODE (to_rtx)));
5431 result = store_field (temp, bitsize, bitpos, bitregion_start,
5432 bitregion_end, mode1, from, get_alias_set (to),
5433 nontemporal, reversep);
5434 emit_move_insn (to_rtx, temp);
5436 else
5438 if (MEM_P (to_rtx))
5440 /* If the field is at offset zero, we could have been given the
5441 DECL_RTX of the parent struct. Don't munge it. */
5442 to_rtx = shallow_copy_rtx (to_rtx);
5443 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5444 if (volatilep)
5445 MEM_VOLATILE_P (to_rtx) = 1;
5448 gcc_checking_assert (known_ge (bitpos, 0));
5449 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5450 bitregion_start, bitregion_end,
5451 mode1, to_rtx, to, from,
5452 reversep))
5453 result = NULL;
5454 else
5455 result = store_field (to_rtx, bitsize, bitpos,
5456 bitregion_start, bitregion_end,
5457 mode1, from, get_alias_set (to),
5458 nontemporal, reversep);
5461 if (result)
5462 preserve_temp_slots (result);
5463 pop_temp_slots ();
5464 return;
5467 /* If the rhs is a function call and its value is not an aggregate,
5468 call the function before we start to compute the lhs.
5469 This is needed for correct code for cases such as
5470 val = setjmp (buf) on machines where reference to val
5471 requires loading up part of an address in a separate insn.
5473 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5474 since it might be a promoted variable where the zero- or sign- extension
5475 needs to be done. Handling this in the normal way is safe because no
5476 computation is done before the call. The same is true for SSA names. */
5477 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5478 && COMPLETE_TYPE_P (TREE_TYPE (from))
5479 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5480 && ! (((VAR_P (to)
5481 || TREE_CODE (to) == PARM_DECL
5482 || TREE_CODE (to) == RESULT_DECL)
5483 && REG_P (DECL_RTL (to)))
5484 || TREE_CODE (to) == SSA_NAME))
5486 rtx value;
5488 push_temp_slots ();
5489 value = expand_normal (from);
5491 if (to_rtx == 0)
5492 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5494 /* Handle calls that return values in multiple non-contiguous locations.
5495 The Irix 6 ABI has examples of this. */
5496 if (GET_CODE (to_rtx) == PARALLEL)
5498 if (GET_CODE (value) == PARALLEL)
5499 emit_group_move (to_rtx, value);
5500 else
5501 emit_group_load (to_rtx, value, TREE_TYPE (from),
5502 int_size_in_bytes (TREE_TYPE (from)));
5504 else if (GET_CODE (value) == PARALLEL)
5505 emit_group_store (to_rtx, value, TREE_TYPE (from),
5506 int_size_in_bytes (TREE_TYPE (from)));
5507 else if (GET_MODE (to_rtx) == BLKmode)
5509 /* Handle calls that return BLKmode values in registers. */
5510 if (REG_P (value))
5511 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5512 else
5513 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5515 else
5517 if (POINTER_TYPE_P (TREE_TYPE (to)))
5518 value = convert_memory_address_addr_space
5519 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5520 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5522 emit_move_insn (to_rtx, value);
5525 preserve_temp_slots (to_rtx);
5526 pop_temp_slots ();
5527 return;
5530 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5531 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5533 /* Don't move directly into a return register. */
5534 if (TREE_CODE (to) == RESULT_DECL
5535 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5537 rtx temp;
5539 push_temp_slots ();
5541 /* If the source is itself a return value, it still is in a pseudo at
5542 this point so we can move it back to the return register directly. */
5543 if (REG_P (to_rtx)
5544 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5545 && TREE_CODE (from) != CALL_EXPR)
5546 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5547 else
5548 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5550 /* Handle calls that return values in multiple non-contiguous locations.
5551 The Irix 6 ABI has examples of this. */
5552 if (GET_CODE (to_rtx) == PARALLEL)
5554 if (GET_CODE (temp) == PARALLEL)
5555 emit_group_move (to_rtx, temp);
5556 else
5557 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5558 int_size_in_bytes (TREE_TYPE (from)));
5560 else if (temp)
5561 emit_move_insn (to_rtx, temp);
5563 preserve_temp_slots (to_rtx);
5564 pop_temp_slots ();
5565 return;
5568 /* In case we are returning the contents of an object which overlaps
5569 the place the value is being stored, use a safe function when copying
5570 a value through a pointer into a structure value return block. */
5571 if (TREE_CODE (to) == RESULT_DECL
5572 && TREE_CODE (from) == INDIRECT_REF
5573 && ADDR_SPACE_GENERIC_P
5574 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5575 && refs_may_alias_p (to, from)
5576 && cfun->returns_struct
5577 && !cfun->returns_pcc_struct)
5579 rtx from_rtx, size;
5581 push_temp_slots ();
5582 size = expr_size (from);
5583 from_rtx = expand_normal (from);
5585 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5587 preserve_temp_slots (to_rtx);
5588 pop_temp_slots ();
5589 return;
5592 /* Compute FROM and store the value in the rtx we got. */
5594 push_temp_slots ();
5595 result = store_expr (from, to_rtx, 0, nontemporal, false);
5596 preserve_temp_slots (result);
5597 pop_temp_slots ();
5598 return;
5601 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5602 succeeded, false otherwise. */
5604 bool
5605 emit_storent_insn (rtx to, rtx from)
5607 class expand_operand ops[2];
5608 machine_mode mode = GET_MODE (to);
5609 enum insn_code code = optab_handler (storent_optab, mode);
5611 if (code == CODE_FOR_nothing)
5612 return false;
5614 create_fixed_operand (&ops[0], to);
5615 create_input_operand (&ops[1], from, mode);
5616 return maybe_expand_insn (code, 2, ops);
5619 /* Helper function for store_expr storing of STRING_CST. */
5621 static rtx
5622 string_cst_read_str (void *data, HOST_WIDE_INT offset, scalar_int_mode mode)
5624 tree str = (tree) data;
5626 gcc_assert (offset >= 0);
5627 if (offset >= TREE_STRING_LENGTH (str))
5628 return const0_rtx;
5630 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
5631 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
5633 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
5634 size_t l = TREE_STRING_LENGTH (str) - offset;
5635 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
5636 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
5637 return c_readstr (p, mode, false);
5640 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
5643 /* Generate code for computing expression EXP,
5644 and storing the value into TARGET.
5646 If the mode is BLKmode then we may return TARGET itself.
5647 It turns out that in BLKmode it doesn't cause a problem.
5648 because C has no operators that could combine two different
5649 assignments into the same BLKmode object with different values
5650 with no sequence point. Will other languages need this to
5651 be more thorough?
5653 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5654 stack, and block moves may need to be treated specially.
5656 If NONTEMPORAL is true, try using a nontemporal store instruction.
5658 If REVERSE is true, the store is to be done in reverse order. */
5661 store_expr (tree exp, rtx target, int call_param_p,
5662 bool nontemporal, bool reverse)
5664 rtx temp;
5665 rtx alt_rtl = NULL_RTX;
5666 location_t loc = curr_insn_location ();
5667 bool shortened_string_cst = false;
5669 if (VOID_TYPE_P (TREE_TYPE (exp)))
5671 /* C++ can generate ?: expressions with a throw expression in one
5672 branch and an rvalue in the other. Here, we resolve attempts to
5673 store the throw expression's nonexistent result. */
5674 gcc_assert (!call_param_p);
5675 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5676 return NULL_RTX;
5678 if (TREE_CODE (exp) == COMPOUND_EXPR)
5680 /* Perform first part of compound expression, then assign from second
5681 part. */
5682 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5683 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5684 return store_expr (TREE_OPERAND (exp, 1), target,
5685 call_param_p, nontemporal, reverse);
5687 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5689 /* For conditional expression, get safe form of the target. Then
5690 test the condition, doing the appropriate assignment on either
5691 side. This avoids the creation of unnecessary temporaries.
5692 For non-BLKmode, it is more efficient not to do this. */
5694 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5696 do_pending_stack_adjust ();
5697 NO_DEFER_POP;
5698 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5699 profile_probability::uninitialized ());
5700 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5701 nontemporal, reverse);
5702 emit_jump_insn (targetm.gen_jump (lab2));
5703 emit_barrier ();
5704 emit_label (lab1);
5705 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5706 nontemporal, reverse);
5707 emit_label (lab2);
5708 OK_DEFER_POP;
5710 return NULL_RTX;
5712 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5713 /* If this is a scalar in a register that is stored in a wider mode
5714 than the declared mode, compute the result into its declared mode
5715 and then convert to the wider mode. Our value is the computed
5716 expression. */
5718 rtx inner_target = 0;
5719 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5720 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5722 /* We can do the conversion inside EXP, which will often result
5723 in some optimizations. Do the conversion in two steps: first
5724 change the signedness, if needed, then the extend. But don't
5725 do this if the type of EXP is a subtype of something else
5726 since then the conversion might involve more than just
5727 converting modes. */
5728 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5729 && TREE_TYPE (TREE_TYPE (exp)) == 0
5730 && GET_MODE_PRECISION (outer_mode)
5731 == TYPE_PRECISION (TREE_TYPE (exp)))
5733 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5734 TYPE_UNSIGNED (TREE_TYPE (exp))))
5736 /* Some types, e.g. Fortran's logical*4, won't have a signed
5737 version, so use the mode instead. */
5738 tree ntype
5739 = (signed_or_unsigned_type_for
5740 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5741 if (ntype == NULL)
5742 ntype = lang_hooks.types.type_for_mode
5743 (TYPE_MODE (TREE_TYPE (exp)),
5744 SUBREG_PROMOTED_SIGN (target));
5746 exp = fold_convert_loc (loc, ntype, exp);
5749 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5750 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5751 exp);
5753 inner_target = SUBREG_REG (target);
5756 temp = expand_expr (exp, inner_target, VOIDmode,
5757 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5760 /* If TEMP is a VOIDmode constant, use convert_modes to make
5761 sure that we properly convert it. */
5762 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5764 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5765 temp, SUBREG_PROMOTED_SIGN (target));
5766 temp = convert_modes (inner_mode, outer_mode, temp,
5767 SUBREG_PROMOTED_SIGN (target));
5770 convert_move (SUBREG_REG (target), temp,
5771 SUBREG_PROMOTED_SIGN (target));
5773 return NULL_RTX;
5775 else if ((TREE_CODE (exp) == STRING_CST
5776 || (TREE_CODE (exp) == MEM_REF
5777 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5778 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5779 == STRING_CST
5780 && integer_zerop (TREE_OPERAND (exp, 1))))
5781 && !nontemporal && !call_param_p
5782 && MEM_P (target))
5784 /* Optimize initialization of an array with a STRING_CST. */
5785 HOST_WIDE_INT exp_len, str_copy_len;
5786 rtx dest_mem;
5787 tree str = TREE_CODE (exp) == STRING_CST
5788 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5790 exp_len = int_expr_size (exp);
5791 if (exp_len <= 0)
5792 goto normal_expr;
5794 if (TREE_STRING_LENGTH (str) <= 0)
5795 goto normal_expr;
5797 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
5798 MEM_ALIGN (target), false))
5800 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
5801 MEM_ALIGN (target), false, RETURN_BEGIN);
5802 return NULL_RTX;
5805 str_copy_len = TREE_STRING_LENGTH (str);
5806 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
5808 str_copy_len += STORE_MAX_PIECES - 1;
5809 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5811 if (str_copy_len >= exp_len)
5812 goto normal_expr;
5814 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
5815 (void *) str, MEM_ALIGN (target), false))
5816 goto normal_expr;
5818 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
5819 (void *) str, MEM_ALIGN (target), false,
5820 RETURN_END);
5821 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
5822 exp_len - str_copy_len),
5823 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
5824 return NULL_RTX;
5826 else
5828 rtx tmp_target;
5830 normal_expr:
5831 /* If we want to use a nontemporal or a reverse order store, force the
5832 value into a register first. */
5833 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5834 tree rexp = exp;
5835 if (TREE_CODE (exp) == STRING_CST
5836 && tmp_target == target
5837 && GET_MODE (target) == BLKmode
5838 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
5840 rtx size = expr_size (exp);
5841 if (CONST_INT_P (size)
5842 && size != const0_rtx
5843 && (UINTVAL (size)
5844 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
5846 /* If the STRING_CST has much larger array type than
5847 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
5848 it into the rodata section as the code later on will use
5849 memset zero for the remainder anyway. See PR95052. */
5850 tmp_target = NULL_RTX;
5851 rexp = copy_node (exp);
5852 tree index
5853 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
5854 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
5855 index);
5856 shortened_string_cst = true;
5859 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
5860 (call_param_p
5861 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5862 &alt_rtl, false);
5863 if (shortened_string_cst)
5865 gcc_assert (MEM_P (temp));
5866 temp = change_address (temp, BLKmode, NULL_RTX);
5870 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5871 the same as that of TARGET, adjust the constant. This is needed, for
5872 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5873 only a word-sized value. */
5874 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5875 && TREE_CODE (exp) != ERROR_MARK
5876 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5878 gcc_assert (!shortened_string_cst);
5879 if (GET_MODE_CLASS (GET_MODE (target))
5880 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
5881 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
5882 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
5884 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
5885 TYPE_MODE (TREE_TYPE (exp)), 0);
5886 if (t)
5887 temp = t;
5889 if (GET_MODE (temp) == VOIDmode)
5890 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5891 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5894 /* If value was not generated in the target, store it there.
5895 Convert the value to TARGET's type first if necessary and emit the
5896 pending incrementations that have been queued when expanding EXP.
5897 Note that we cannot emit the whole queue blindly because this will
5898 effectively disable the POST_INC optimization later.
5900 If TEMP and TARGET compare equal according to rtx_equal_p, but
5901 one or both of them are volatile memory refs, we have to distinguish
5902 two cases:
5903 - expand_expr has used TARGET. In this case, we must not generate
5904 another copy. This can be detected by TARGET being equal according
5905 to == .
5906 - expand_expr has not used TARGET - that means that the source just
5907 happens to have the same RTX form. Since temp will have been created
5908 by expand_expr, it will compare unequal according to == .
5909 We must generate a copy in this case, to reach the correct number
5910 of volatile memory references. */
5912 if ((! rtx_equal_p (temp, target)
5913 || (temp != target && (side_effects_p (temp)
5914 || side_effects_p (target))))
5915 && TREE_CODE (exp) != ERROR_MARK
5916 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5917 but TARGET is not valid memory reference, TEMP will differ
5918 from TARGET although it is really the same location. */
5919 && !(alt_rtl
5920 && rtx_equal_p (alt_rtl, target)
5921 && !side_effects_p (alt_rtl)
5922 && !side_effects_p (target))
5923 /* If there's nothing to copy, don't bother. Don't call
5924 expr_size unless necessary, because some front-ends (C++)
5925 expr_size-hook must not be given objects that are not
5926 supposed to be bit-copied or bit-initialized. */
5927 && expr_size (exp) != const0_rtx)
5929 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5931 gcc_assert (!shortened_string_cst);
5932 if (GET_MODE (target) == BLKmode)
5934 /* Handle calls that return BLKmode values in registers. */
5935 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5936 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5937 else
5938 store_bit_field (target,
5939 rtx_to_poly_int64 (expr_size (exp))
5940 * BITS_PER_UNIT,
5941 0, 0, 0, GET_MODE (temp), temp, reverse);
5943 else
5944 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5947 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5949 /* Handle copying a string constant into an array. The string
5950 constant may be shorter than the array. So copy just the string's
5951 actual length, and clear the rest. First get the size of the data
5952 type of the string, which is actually the size of the target. */
5953 rtx size = expr_size (exp);
5955 if (CONST_INT_P (size)
5956 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5957 emit_block_move (target, temp, size,
5958 (call_param_p
5959 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5960 else
5962 machine_mode pointer_mode
5963 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5964 machine_mode address_mode = get_address_mode (target);
5966 /* Compute the size of the data to copy from the string. */
5967 tree copy_size
5968 = size_binop_loc (loc, MIN_EXPR,
5969 make_tree (sizetype, size),
5970 size_int (TREE_STRING_LENGTH (exp)));
5971 rtx copy_size_rtx
5972 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5973 (call_param_p
5974 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5975 rtx_code_label *label = 0;
5977 /* Copy that much. */
5978 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5979 TYPE_UNSIGNED (sizetype));
5980 emit_block_move (target, temp, copy_size_rtx,
5981 (call_param_p
5982 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5984 /* Figure out how much is left in TARGET that we have to clear.
5985 Do all calculations in pointer_mode. */
5986 poly_int64 const_copy_size;
5987 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
5989 size = plus_constant (address_mode, size, -const_copy_size);
5990 target = adjust_address (target, BLKmode, const_copy_size);
5992 else
5994 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5995 copy_size_rtx, NULL_RTX, 0,
5996 OPTAB_LIB_WIDEN);
5998 if (GET_MODE (copy_size_rtx) != address_mode)
5999 copy_size_rtx = convert_to_mode (address_mode,
6000 copy_size_rtx,
6001 TYPE_UNSIGNED (sizetype));
6003 target = offset_address (target, copy_size_rtx,
6004 highest_pow2_factor (copy_size));
6005 label = gen_label_rtx ();
6006 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6007 GET_MODE (size), 0, label);
6010 if (size != const0_rtx)
6011 clear_storage (target, size, BLOCK_OP_NORMAL);
6013 if (label)
6014 emit_label (label);
6017 else if (shortened_string_cst)
6018 gcc_unreachable ();
6019 /* Handle calls that return values in multiple non-contiguous locations.
6020 The Irix 6 ABI has examples of this. */
6021 else if (GET_CODE (target) == PARALLEL)
6023 if (GET_CODE (temp) == PARALLEL)
6024 emit_group_move (target, temp);
6025 else
6026 emit_group_load (target, temp, TREE_TYPE (exp),
6027 int_size_in_bytes (TREE_TYPE (exp)));
6029 else if (GET_CODE (temp) == PARALLEL)
6030 emit_group_store (target, temp, TREE_TYPE (exp),
6031 int_size_in_bytes (TREE_TYPE (exp)));
6032 else if (GET_MODE (temp) == BLKmode)
6033 emit_block_move (target, temp, expr_size (exp),
6034 (call_param_p
6035 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6036 /* If we emit a nontemporal store, there is nothing else to do. */
6037 else if (nontemporal && emit_storent_insn (target, temp))
6039 else
6041 if (reverse)
6042 temp = flip_storage_order (GET_MODE (target), temp);
6043 temp = force_operand (temp, target);
6044 if (temp != target)
6045 emit_move_insn (target, temp);
6048 else
6049 gcc_assert (!shortened_string_cst);
6051 return NULL_RTX;
6054 /* Return true if field F of structure TYPE is a flexible array. */
6056 static bool
6057 flexible_array_member_p (const_tree f, const_tree type)
6059 const_tree tf;
6061 tf = TREE_TYPE (f);
6062 return (DECL_CHAIN (f) == NULL
6063 && TREE_CODE (tf) == ARRAY_TYPE
6064 && TYPE_DOMAIN (tf)
6065 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6066 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6067 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6068 && int_size_in_bytes (type) >= 0);
6071 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6072 must have in order for it to completely initialize a value of type TYPE.
6073 Return -1 if the number isn't known.
6075 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6077 static HOST_WIDE_INT
6078 count_type_elements (const_tree type, bool for_ctor_p)
6080 switch (TREE_CODE (type))
6082 case ARRAY_TYPE:
6084 tree nelts;
6086 nelts = array_type_nelts (type);
6087 if (nelts && tree_fits_uhwi_p (nelts))
6089 unsigned HOST_WIDE_INT n;
6091 n = tree_to_uhwi (nelts) + 1;
6092 if (n == 0 || for_ctor_p)
6093 return n;
6094 else
6095 return n * count_type_elements (TREE_TYPE (type), false);
6097 return for_ctor_p ? -1 : 1;
6100 case RECORD_TYPE:
6102 unsigned HOST_WIDE_INT n;
6103 tree f;
6105 n = 0;
6106 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6107 if (TREE_CODE (f) == FIELD_DECL)
6109 if (!for_ctor_p)
6110 n += count_type_elements (TREE_TYPE (f), false);
6111 else if (!flexible_array_member_p (f, type))
6112 /* Don't count flexible arrays, which are not supposed
6113 to be initialized. */
6114 n += 1;
6117 return n;
6120 case UNION_TYPE:
6121 case QUAL_UNION_TYPE:
6123 tree f;
6124 HOST_WIDE_INT n, m;
6126 gcc_assert (!for_ctor_p);
6127 /* Estimate the number of scalars in each field and pick the
6128 maximum. Other estimates would do instead; the idea is simply
6129 to make sure that the estimate is not sensitive to the ordering
6130 of the fields. */
6131 n = 1;
6132 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6133 if (TREE_CODE (f) == FIELD_DECL)
6135 m = count_type_elements (TREE_TYPE (f), false);
6136 /* If the field doesn't span the whole union, add an extra
6137 scalar for the rest. */
6138 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6139 TYPE_SIZE (type)) != 1)
6140 m++;
6141 if (n < m)
6142 n = m;
6144 return n;
6147 case COMPLEX_TYPE:
6148 return 2;
6150 case VECTOR_TYPE:
6152 unsigned HOST_WIDE_INT nelts;
6153 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6154 return nelts;
6155 else
6156 return -1;
6159 case INTEGER_TYPE:
6160 case REAL_TYPE:
6161 case FIXED_POINT_TYPE:
6162 case ENUMERAL_TYPE:
6163 case BOOLEAN_TYPE:
6164 case POINTER_TYPE:
6165 case OFFSET_TYPE:
6166 case REFERENCE_TYPE:
6167 case NULLPTR_TYPE:
6168 return 1;
6170 case ERROR_MARK:
6171 return 0;
6173 case VOID_TYPE:
6174 case METHOD_TYPE:
6175 case FUNCTION_TYPE:
6176 case LANG_TYPE:
6177 default:
6178 gcc_unreachable ();
6182 /* Helper for categorize_ctor_elements. Identical interface. */
6184 static bool
6185 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6186 HOST_WIDE_INT *p_unique_nz_elts,
6187 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6189 unsigned HOST_WIDE_INT idx;
6190 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6191 tree value, purpose, elt_type;
6193 /* Whether CTOR is a valid constant initializer, in accordance with what
6194 initializer_constant_valid_p does. If inferred from the constructor
6195 elements, true until proven otherwise. */
6196 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6197 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6199 nz_elts = 0;
6200 unique_nz_elts = 0;
6201 init_elts = 0;
6202 num_fields = 0;
6203 elt_type = NULL_TREE;
6205 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6207 HOST_WIDE_INT mult = 1;
6209 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6211 tree lo_index = TREE_OPERAND (purpose, 0);
6212 tree hi_index = TREE_OPERAND (purpose, 1);
6214 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6215 mult = (tree_to_uhwi (hi_index)
6216 - tree_to_uhwi (lo_index) + 1);
6218 num_fields += mult;
6219 elt_type = TREE_TYPE (value);
6221 switch (TREE_CODE (value))
6223 case CONSTRUCTOR:
6225 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6227 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6228 &ic, p_complete);
6230 nz_elts += mult * nz;
6231 unique_nz_elts += unz;
6232 init_elts += mult * ic;
6234 if (const_from_elts_p && const_p)
6235 const_p = const_elt_p;
6237 break;
6239 case INTEGER_CST:
6240 case REAL_CST:
6241 case FIXED_CST:
6242 if (!initializer_zerop (value))
6244 nz_elts += mult;
6245 unique_nz_elts++;
6247 init_elts += mult;
6248 break;
6250 case STRING_CST:
6251 nz_elts += mult * TREE_STRING_LENGTH (value);
6252 unique_nz_elts += TREE_STRING_LENGTH (value);
6253 init_elts += mult * TREE_STRING_LENGTH (value);
6254 break;
6256 case COMPLEX_CST:
6257 if (!initializer_zerop (TREE_REALPART (value)))
6259 nz_elts += mult;
6260 unique_nz_elts++;
6262 if (!initializer_zerop (TREE_IMAGPART (value)))
6264 nz_elts += mult;
6265 unique_nz_elts++;
6267 init_elts += 2 * mult;
6268 break;
6270 case VECTOR_CST:
6272 /* We can only construct constant-length vectors using
6273 CONSTRUCTOR. */
6274 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6275 for (unsigned int i = 0; i < nunits; ++i)
6277 tree v = VECTOR_CST_ELT (value, i);
6278 if (!initializer_zerop (v))
6280 nz_elts += mult;
6281 unique_nz_elts++;
6283 init_elts += mult;
6286 break;
6288 default:
6290 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6291 nz_elts += mult * tc;
6292 unique_nz_elts += tc;
6293 init_elts += mult * tc;
6295 if (const_from_elts_p && const_p)
6296 const_p
6297 = initializer_constant_valid_p (value,
6298 elt_type,
6299 TYPE_REVERSE_STORAGE_ORDER
6300 (TREE_TYPE (ctor)))
6301 != NULL_TREE;
6303 break;
6307 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6308 num_fields, elt_type))
6309 *p_complete = false;
6311 *p_nz_elts += nz_elts;
6312 *p_unique_nz_elts += unique_nz_elts;
6313 *p_init_elts += init_elts;
6315 return const_p;
6318 /* Examine CTOR to discover:
6319 * how many scalar fields are set to nonzero values,
6320 and place it in *P_NZ_ELTS;
6321 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6322 high - low + 1 (this can be useful for callers to determine ctors
6323 that could be cheaply initialized with - perhaps nested - loops
6324 compared to copied from huge read-only data),
6325 and place it in *P_UNIQUE_NZ_ELTS;
6326 * how many scalar fields in total are in CTOR,
6327 and place it in *P_ELT_COUNT.
6328 * whether the constructor is complete -- in the sense that every
6329 meaningful byte is explicitly given a value --
6330 and place it in *P_COMPLETE.
6332 Return whether or not CTOR is a valid static constant initializer, the same
6333 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6335 bool
6336 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6337 HOST_WIDE_INT *p_unique_nz_elts,
6338 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6340 *p_nz_elts = 0;
6341 *p_unique_nz_elts = 0;
6342 *p_init_elts = 0;
6343 *p_complete = true;
6345 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6346 p_init_elts, p_complete);
6349 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6350 of which had type LAST_TYPE. Each element was itself a complete
6351 initializer, in the sense that every meaningful byte was explicitly
6352 given a value. Return true if the same is true for the constructor
6353 as a whole. */
6355 bool
6356 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6357 const_tree last_type)
6359 if (TREE_CODE (type) == UNION_TYPE
6360 || TREE_CODE (type) == QUAL_UNION_TYPE)
6362 if (num_elts == 0)
6363 return false;
6365 gcc_assert (num_elts == 1 && last_type);
6367 /* ??? We could look at each element of the union, and find the
6368 largest element. Which would avoid comparing the size of the
6369 initialized element against any tail padding in the union.
6370 Doesn't seem worth the effort... */
6371 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6374 return count_type_elements (type, true) == num_elts;
6377 /* Return 1 if EXP contains mostly (3/4) zeros. */
6379 static int
6380 mostly_zeros_p (const_tree exp)
6382 if (TREE_CODE (exp) == CONSTRUCTOR)
6384 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6385 bool complete_p;
6387 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6388 &complete_p);
6389 return !complete_p || nz_elts < init_elts / 4;
6392 return initializer_zerop (exp);
6395 /* Return 1 if EXP contains all zeros. */
6397 static int
6398 all_zeros_p (const_tree exp)
6400 if (TREE_CODE (exp) == CONSTRUCTOR)
6402 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6403 bool complete_p;
6405 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6406 &complete_p);
6407 return nz_elts == 0;
6410 return initializer_zerop (exp);
6413 /* Helper function for store_constructor.
6414 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6415 CLEARED is as for store_constructor.
6416 ALIAS_SET is the alias set to use for any stores.
6417 If REVERSE is true, the store is to be done in reverse order.
6419 This provides a recursive shortcut back to store_constructor when it isn't
6420 necessary to go through store_field. This is so that we can pass through
6421 the cleared field to let store_constructor know that we may not have to
6422 clear a substructure if the outer structure has already been cleared. */
6424 static void
6425 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6426 poly_uint64 bitregion_start,
6427 poly_uint64 bitregion_end,
6428 machine_mode mode,
6429 tree exp, int cleared,
6430 alias_set_type alias_set, bool reverse)
6432 poly_int64 bytepos;
6433 poly_uint64 bytesize;
6434 if (TREE_CODE (exp) == CONSTRUCTOR
6435 /* We can only call store_constructor recursively if the size and
6436 bit position are on a byte boundary. */
6437 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6438 && maybe_ne (bitsize, 0U)
6439 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6440 /* If we have a nonzero bitpos for a register target, then we just
6441 let store_field do the bitfield handling. This is unlikely to
6442 generate unnecessary clear instructions anyways. */
6443 && (known_eq (bitpos, 0) || MEM_P (target)))
6445 if (MEM_P (target))
6447 machine_mode target_mode = GET_MODE (target);
6448 if (target_mode != BLKmode
6449 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6450 target_mode = BLKmode;
6451 target = adjust_address (target, target_mode, bytepos);
6455 /* Update the alias set, if required. */
6456 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6457 && MEM_ALIAS_SET (target) != 0)
6459 target = copy_rtx (target);
6460 set_mem_alias_set (target, alias_set);
6463 store_constructor (exp, target, cleared, bytesize, reverse);
6465 else
6466 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6467 exp, alias_set, false, reverse);
6471 /* Returns the number of FIELD_DECLs in TYPE. */
6473 static int
6474 fields_length (const_tree type)
6476 tree t = TYPE_FIELDS (type);
6477 int count = 0;
6479 for (; t; t = DECL_CHAIN (t))
6480 if (TREE_CODE (t) == FIELD_DECL)
6481 ++count;
6483 return count;
6487 /* Store the value of constructor EXP into the rtx TARGET.
6488 TARGET is either a REG or a MEM; we know it cannot conflict, since
6489 safe_from_p has been called.
6490 CLEARED is true if TARGET is known to have been zero'd.
6491 SIZE is the number of bytes of TARGET we are allowed to modify: this
6492 may not be the same as the size of EXP if we are assigning to a field
6493 which has been packed to exclude padding bits.
6494 If REVERSE is true, the store is to be done in reverse order. */
6496 static void
6497 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6498 bool reverse)
6500 tree type = TREE_TYPE (exp);
6501 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6502 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6504 switch (TREE_CODE (type))
6506 case RECORD_TYPE:
6507 case UNION_TYPE:
6508 case QUAL_UNION_TYPE:
6510 unsigned HOST_WIDE_INT idx;
6511 tree field, value;
6513 /* The storage order is specified for every aggregate type. */
6514 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6516 /* If size is zero or the target is already cleared, do nothing. */
6517 if (known_eq (size, 0) || cleared)
6518 cleared = 1;
6519 /* We either clear the aggregate or indicate the value is dead. */
6520 else if ((TREE_CODE (type) == UNION_TYPE
6521 || TREE_CODE (type) == QUAL_UNION_TYPE)
6522 && ! CONSTRUCTOR_ELTS (exp))
6523 /* If the constructor is empty, clear the union. */
6525 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6526 cleared = 1;
6529 /* If we are building a static constructor into a register,
6530 set the initial value as zero so we can fold the value into
6531 a constant. But if more than one register is involved,
6532 this probably loses. */
6533 else if (REG_P (target) && TREE_STATIC (exp)
6534 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6535 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6537 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6538 cleared = 1;
6541 /* If the constructor has fewer fields than the structure or
6542 if we are initializing the structure to mostly zeros, clear
6543 the whole structure first. Don't do this if TARGET is a
6544 register whose mode size isn't equal to SIZE since
6545 clear_storage can't handle this case. */
6546 else if (known_size_p (size)
6547 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6548 || mostly_zeros_p (exp))
6549 && (!REG_P (target)
6550 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6552 clear_storage (target, gen_int_mode (size, Pmode),
6553 BLOCK_OP_NORMAL);
6554 cleared = 1;
6557 if (REG_P (target) && !cleared)
6558 emit_clobber (target);
6560 /* Store each element of the constructor into the
6561 corresponding field of TARGET. */
6562 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6564 machine_mode mode;
6565 HOST_WIDE_INT bitsize;
6566 HOST_WIDE_INT bitpos = 0;
6567 tree offset;
6568 rtx to_rtx = target;
6570 /* Just ignore missing fields. We cleared the whole
6571 structure, above, if any fields are missing. */
6572 if (field == 0)
6573 continue;
6575 if (cleared && initializer_zerop (value))
6576 continue;
6578 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6579 bitsize = tree_to_uhwi (DECL_SIZE (field));
6580 else
6581 gcc_unreachable ();
6583 mode = DECL_MODE (field);
6584 if (DECL_BIT_FIELD (field))
6585 mode = VOIDmode;
6587 offset = DECL_FIELD_OFFSET (field);
6588 if (tree_fits_shwi_p (offset)
6589 && tree_fits_shwi_p (bit_position (field)))
6591 bitpos = int_bit_position (field);
6592 offset = NULL_TREE;
6594 else
6595 gcc_unreachable ();
6597 /* If this initializes a field that is smaller than a
6598 word, at the start of a word, try to widen it to a full
6599 word. This special case allows us to output C++ member
6600 function initializations in a form that the optimizers
6601 can understand. */
6602 if (WORD_REGISTER_OPERATIONS
6603 && REG_P (target)
6604 && bitsize < BITS_PER_WORD
6605 && bitpos % BITS_PER_WORD == 0
6606 && GET_MODE_CLASS (mode) == MODE_INT
6607 && TREE_CODE (value) == INTEGER_CST
6608 && exp_size >= 0
6609 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6611 type = TREE_TYPE (value);
6613 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6615 type = lang_hooks.types.type_for_mode
6616 (word_mode, TYPE_UNSIGNED (type));
6617 value = fold_convert (type, value);
6618 /* Make sure the bits beyond the original bitsize are zero
6619 so that we can correctly avoid extra zeroing stores in
6620 later constructor elements. */
6621 tree bitsize_mask
6622 = wide_int_to_tree (type, wi::mask (bitsize, false,
6623 BITS_PER_WORD));
6624 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6627 if (BYTES_BIG_ENDIAN)
6628 value
6629 = fold_build2 (LSHIFT_EXPR, type, value,
6630 build_int_cst (type,
6631 BITS_PER_WORD - bitsize));
6632 bitsize = BITS_PER_WORD;
6633 mode = word_mode;
6636 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6637 && DECL_NONADDRESSABLE_P (field))
6639 to_rtx = copy_rtx (to_rtx);
6640 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6643 store_constructor_field (to_rtx, bitsize, bitpos,
6644 0, bitregion_end, mode,
6645 value, cleared,
6646 get_alias_set (TREE_TYPE (field)),
6647 reverse);
6649 break;
6651 case ARRAY_TYPE:
6653 tree value, index;
6654 unsigned HOST_WIDE_INT i;
6655 int need_to_clear;
6656 tree domain;
6657 tree elttype = TREE_TYPE (type);
6658 int const_bounds_p;
6659 HOST_WIDE_INT minelt = 0;
6660 HOST_WIDE_INT maxelt = 0;
6662 /* The storage order is specified for every aggregate type. */
6663 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6665 domain = TYPE_DOMAIN (type);
6666 const_bounds_p = (TYPE_MIN_VALUE (domain)
6667 && TYPE_MAX_VALUE (domain)
6668 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6669 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6671 /* If we have constant bounds for the range of the type, get them. */
6672 if (const_bounds_p)
6674 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6675 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6678 /* If the constructor has fewer elements than the array, clear
6679 the whole array first. Similarly if this is static
6680 constructor of a non-BLKmode object. */
6681 if (cleared)
6682 need_to_clear = 0;
6683 else if (REG_P (target) && TREE_STATIC (exp))
6684 need_to_clear = 1;
6685 else
6687 unsigned HOST_WIDE_INT idx;
6688 HOST_WIDE_INT count = 0, zero_count = 0;
6689 need_to_clear = ! const_bounds_p;
6691 /* This loop is a more accurate version of the loop in
6692 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6693 is also needed to check for missing elements. */
6694 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6696 HOST_WIDE_INT this_node_count;
6698 if (need_to_clear)
6699 break;
6701 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6703 tree lo_index = TREE_OPERAND (index, 0);
6704 tree hi_index = TREE_OPERAND (index, 1);
6706 if (! tree_fits_uhwi_p (lo_index)
6707 || ! tree_fits_uhwi_p (hi_index))
6709 need_to_clear = 1;
6710 break;
6713 this_node_count = (tree_to_uhwi (hi_index)
6714 - tree_to_uhwi (lo_index) + 1);
6716 else
6717 this_node_count = 1;
6719 count += this_node_count;
6720 if (mostly_zeros_p (value))
6721 zero_count += this_node_count;
6724 /* Clear the entire array first if there are any missing
6725 elements, or if the incidence of zero elements is >=
6726 75%. */
6727 if (! need_to_clear
6728 && (count < maxelt - minelt + 1
6729 || 4 * zero_count >= 3 * count))
6730 need_to_clear = 1;
6733 if (need_to_clear && maybe_gt (size, 0))
6735 if (REG_P (target))
6736 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6737 else
6738 clear_storage (target, gen_int_mode (size, Pmode),
6739 BLOCK_OP_NORMAL);
6740 cleared = 1;
6743 if (!cleared && REG_P (target))
6744 /* Inform later passes that the old value is dead. */
6745 emit_clobber (target);
6747 /* Store each element of the constructor into the
6748 corresponding element of TARGET, determined by counting the
6749 elements. */
6750 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6752 machine_mode mode;
6753 poly_int64 bitsize;
6754 HOST_WIDE_INT bitpos;
6755 rtx xtarget = target;
6757 if (cleared && initializer_zerop (value))
6758 continue;
6760 mode = TYPE_MODE (elttype);
6761 if (mode != BLKmode)
6762 bitsize = GET_MODE_BITSIZE (mode);
6763 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
6764 bitsize = -1;
6766 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6768 tree lo_index = TREE_OPERAND (index, 0);
6769 tree hi_index = TREE_OPERAND (index, 1);
6770 rtx index_r, pos_rtx;
6771 HOST_WIDE_INT lo, hi, count;
6772 tree position;
6774 /* If the range is constant and "small", unroll the loop. */
6775 if (const_bounds_p
6776 && tree_fits_shwi_p (lo_index)
6777 && tree_fits_shwi_p (hi_index)
6778 && (lo = tree_to_shwi (lo_index),
6779 hi = tree_to_shwi (hi_index),
6780 count = hi - lo + 1,
6781 (!MEM_P (target)
6782 || count <= 2
6783 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6784 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6785 <= 40 * 8)))))
6787 lo -= minelt; hi -= minelt;
6788 for (; lo <= hi; lo++)
6790 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6792 if (MEM_P (target)
6793 && !MEM_KEEP_ALIAS_SET_P (target)
6794 && TREE_CODE (type) == ARRAY_TYPE
6795 && TYPE_NONALIASED_COMPONENT (type))
6797 target = copy_rtx (target);
6798 MEM_KEEP_ALIAS_SET_P (target) = 1;
6801 store_constructor_field
6802 (target, bitsize, bitpos, 0, bitregion_end,
6803 mode, value, cleared,
6804 get_alias_set (elttype), reverse);
6807 else
6809 rtx_code_label *loop_start = gen_label_rtx ();
6810 rtx_code_label *loop_end = gen_label_rtx ();
6811 tree exit_cond;
6813 expand_normal (hi_index);
6815 index = build_decl (EXPR_LOCATION (exp),
6816 VAR_DECL, NULL_TREE, domain);
6817 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6818 SET_DECL_RTL (index, index_r);
6819 store_expr (lo_index, index_r, 0, false, reverse);
6821 /* Build the head of the loop. */
6822 do_pending_stack_adjust ();
6823 emit_label (loop_start);
6825 /* Assign value to element index. */
6826 position =
6827 fold_convert (ssizetype,
6828 fold_build2 (MINUS_EXPR,
6829 TREE_TYPE (index),
6830 index,
6831 TYPE_MIN_VALUE (domain)));
6833 position =
6834 size_binop (MULT_EXPR, position,
6835 fold_convert (ssizetype,
6836 TYPE_SIZE_UNIT (elttype)));
6838 pos_rtx = expand_normal (position);
6839 xtarget = offset_address (target, pos_rtx,
6840 highest_pow2_factor (position));
6841 xtarget = adjust_address (xtarget, mode, 0);
6842 if (TREE_CODE (value) == CONSTRUCTOR)
6843 store_constructor (value, xtarget, cleared,
6844 exact_div (bitsize, BITS_PER_UNIT),
6845 reverse);
6846 else
6847 store_expr (value, xtarget, 0, false, reverse);
6849 /* Generate a conditional jump to exit the loop. */
6850 exit_cond = build2 (LT_EXPR, integer_type_node,
6851 index, hi_index);
6852 jumpif (exit_cond, loop_end,
6853 profile_probability::uninitialized ());
6855 /* Update the loop counter, and jump to the head of
6856 the loop. */
6857 expand_assignment (index,
6858 build2 (PLUS_EXPR, TREE_TYPE (index),
6859 index, integer_one_node),
6860 false);
6862 emit_jump (loop_start);
6864 /* Build the end of the loop. */
6865 emit_label (loop_end);
6868 else if ((index != 0 && ! tree_fits_shwi_p (index))
6869 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6871 tree position;
6873 if (index == 0)
6874 index = ssize_int (1);
6876 if (minelt)
6877 index = fold_convert (ssizetype,
6878 fold_build2 (MINUS_EXPR,
6879 TREE_TYPE (index),
6880 index,
6881 TYPE_MIN_VALUE (domain)));
6883 position =
6884 size_binop (MULT_EXPR, index,
6885 fold_convert (ssizetype,
6886 TYPE_SIZE_UNIT (elttype)));
6887 xtarget = offset_address (target,
6888 expand_normal (position),
6889 highest_pow2_factor (position));
6890 xtarget = adjust_address (xtarget, mode, 0);
6891 store_expr (value, xtarget, 0, false, reverse);
6893 else
6895 if (index != 0)
6896 bitpos = ((tree_to_shwi (index) - minelt)
6897 * tree_to_uhwi (TYPE_SIZE (elttype)));
6898 else
6899 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6901 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6902 && TREE_CODE (type) == ARRAY_TYPE
6903 && TYPE_NONALIASED_COMPONENT (type))
6905 target = copy_rtx (target);
6906 MEM_KEEP_ALIAS_SET_P (target) = 1;
6908 store_constructor_field (target, bitsize, bitpos, 0,
6909 bitregion_end, mode, value,
6910 cleared, get_alias_set (elttype),
6911 reverse);
6914 break;
6917 case VECTOR_TYPE:
6919 unsigned HOST_WIDE_INT idx;
6920 constructor_elt *ce;
6921 int i;
6922 int need_to_clear;
6923 insn_code icode = CODE_FOR_nothing;
6924 tree elt;
6925 tree elttype = TREE_TYPE (type);
6926 int elt_size = vector_element_bits (type);
6927 machine_mode eltmode = TYPE_MODE (elttype);
6928 HOST_WIDE_INT bitsize;
6929 HOST_WIDE_INT bitpos;
6930 rtvec vector = NULL;
6931 poly_uint64 n_elts;
6932 unsigned HOST_WIDE_INT const_n_elts;
6933 alias_set_type alias;
6934 bool vec_vec_init_p = false;
6935 machine_mode mode = GET_MODE (target);
6937 gcc_assert (eltmode != BLKmode);
6939 /* Try using vec_duplicate_optab for uniform vectors. */
6940 if (!TREE_SIDE_EFFECTS (exp)
6941 && VECTOR_MODE_P (mode)
6942 && eltmode == GET_MODE_INNER (mode)
6943 && ((icode = optab_handler (vec_duplicate_optab, mode))
6944 != CODE_FOR_nothing)
6945 && (elt = uniform_vector_p (exp)))
6947 class expand_operand ops[2];
6948 create_output_operand (&ops[0], target, mode);
6949 create_input_operand (&ops[1], expand_normal (elt), eltmode);
6950 expand_insn (icode, 2, ops);
6951 if (!rtx_equal_p (target, ops[0].value))
6952 emit_move_insn (target, ops[0].value);
6953 break;
6956 n_elts = TYPE_VECTOR_SUBPARTS (type);
6957 if (REG_P (target)
6958 && VECTOR_MODE_P (mode)
6959 && n_elts.is_constant (&const_n_elts))
6961 machine_mode emode = eltmode;
6962 bool vector_typed_elts_p = false;
6964 if (CONSTRUCTOR_NELTS (exp)
6965 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6966 == VECTOR_TYPE))
6968 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6969 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
6970 * TYPE_VECTOR_SUBPARTS (etype),
6971 n_elts));
6972 emode = TYPE_MODE (etype);
6973 vector_typed_elts_p = true;
6975 icode = convert_optab_handler (vec_init_optab, mode, emode);
6976 if (icode != CODE_FOR_nothing)
6978 unsigned int n = const_n_elts;
6980 if (vector_typed_elts_p)
6982 n = CONSTRUCTOR_NELTS (exp);
6983 vec_vec_init_p = true;
6985 vector = rtvec_alloc (n);
6986 for (unsigned int k = 0; k < n; k++)
6987 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
6991 /* Compute the size of the elements in the CTOR. It differs
6992 from the size of the vector type elements only when the
6993 CTOR elements are vectors themselves. */
6994 tree val_type = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6995 if (VECTOR_TYPE_P (val_type))
6996 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
6997 else
6998 bitsize = elt_size;
7000 /* If the constructor has fewer elements than the vector,
7001 clear the whole array first. Similarly if this is static
7002 constructor of a non-BLKmode object. */
7003 if (cleared)
7004 need_to_clear = 0;
7005 else if (REG_P (target) && TREE_STATIC (exp))
7006 need_to_clear = 1;
7007 else
7009 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7010 tree value;
7012 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7014 int n_elts_here = bitsize / elt_size;
7015 count += n_elts_here;
7016 if (mostly_zeros_p (value))
7017 zero_count += n_elts_here;
7020 /* Clear the entire vector first if there are any missing elements,
7021 or if the incidence of zero elements is >= 75%. */
7022 need_to_clear = (maybe_lt (count, n_elts)
7023 || 4 * zero_count >= 3 * count);
7026 if (need_to_clear && maybe_gt (size, 0) && !vector)
7028 if (REG_P (target))
7029 emit_move_insn (target, CONST0_RTX (mode));
7030 else
7031 clear_storage (target, gen_int_mode (size, Pmode),
7032 BLOCK_OP_NORMAL);
7033 cleared = 1;
7036 /* Inform later passes that the old value is dead. */
7037 if (!cleared && !vector && REG_P (target))
7038 emit_move_insn (target, CONST0_RTX (mode));
7040 if (MEM_P (target))
7041 alias = MEM_ALIAS_SET (target);
7042 else
7043 alias = get_alias_set (elttype);
7045 /* Store each element of the constructor into the corresponding
7046 element of TARGET, determined by counting the elements. */
7047 for (idx = 0, i = 0;
7048 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7049 idx++, i += bitsize / elt_size)
7051 HOST_WIDE_INT eltpos;
7052 tree value = ce->value;
7054 if (cleared && initializer_zerop (value))
7055 continue;
7057 if (ce->index)
7058 eltpos = tree_to_uhwi (ce->index);
7059 else
7060 eltpos = i;
7062 if (vector)
7064 if (vec_vec_init_p)
7066 gcc_assert (ce->index == NULL_TREE);
7067 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7068 eltpos = idx;
7070 else
7071 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7072 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7074 else
7076 machine_mode value_mode
7077 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7078 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7079 bitpos = eltpos * elt_size;
7080 store_constructor_field (target, bitsize, bitpos, 0,
7081 bitregion_end, value_mode,
7082 value, cleared, alias, reverse);
7086 if (vector)
7087 emit_insn (GEN_FCN (icode) (target,
7088 gen_rtx_PARALLEL (mode, vector)));
7089 break;
7092 default:
7093 gcc_unreachable ();
7097 /* Store the value of EXP (an expression tree)
7098 into a subfield of TARGET which has mode MODE and occupies
7099 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7100 If MODE is VOIDmode, it means that we are storing into a bit-field.
7102 BITREGION_START is bitpos of the first bitfield in this region.
7103 BITREGION_END is the bitpos of the ending bitfield in this region.
7104 These two fields are 0, if the C++ memory model does not apply,
7105 or we are not interested in keeping track of bitfield regions.
7107 Always return const0_rtx unless we have something particular to
7108 return.
7110 ALIAS_SET is the alias set for the destination. This value will
7111 (in general) be different from that for TARGET, since TARGET is a
7112 reference to the containing structure.
7114 If NONTEMPORAL is true, try generating a nontemporal store.
7116 If REVERSE is true, the store is to be done in reverse order. */
7118 static rtx
7119 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7120 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7121 machine_mode mode, tree exp,
7122 alias_set_type alias_set, bool nontemporal, bool reverse)
7124 if (TREE_CODE (exp) == ERROR_MARK)
7125 return const0_rtx;
7127 /* If we have nothing to store, do nothing unless the expression has
7128 side-effects. Don't do that for zero sized addressable lhs of
7129 calls. */
7130 if (known_eq (bitsize, 0)
7131 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7132 || TREE_CODE (exp) != CALL_EXPR))
7133 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7135 if (GET_CODE (target) == CONCAT)
7137 /* We're storing into a struct containing a single __complex. */
7139 gcc_assert (known_eq (bitpos, 0));
7140 return store_expr (exp, target, 0, nontemporal, reverse);
7143 /* If the structure is in a register or if the component
7144 is a bit field, we cannot use addressing to access it.
7145 Use bit-field techniques or SUBREG to store in it. */
7147 poly_int64 decl_bitsize;
7148 if (mode == VOIDmode
7149 || (mode != BLKmode && ! direct_store[(int) mode]
7150 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7151 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7152 || REG_P (target)
7153 || GET_CODE (target) == SUBREG
7154 /* If the field isn't aligned enough to store as an ordinary memref,
7155 store it as a bit field. */
7156 || (mode != BLKmode
7157 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7158 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7159 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7160 || !multiple_p (bitpos, BITS_PER_UNIT)))
7161 || (known_size_p (bitsize)
7162 && mode != BLKmode
7163 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7164 /* If the RHS and field are a constant size and the size of the
7165 RHS isn't the same size as the bitfield, we must use bitfield
7166 operations. */
7167 || (known_size_p (bitsize)
7168 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7169 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7170 bitsize)
7171 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7172 we will handle specially below. */
7173 && !(TREE_CODE (exp) == CONSTRUCTOR
7174 && multiple_p (bitsize, BITS_PER_UNIT))
7175 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7176 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7177 includes some extra padding. store_expr / expand_expr will in
7178 that case call get_inner_reference that will have the bitsize
7179 we check here and thus the block move will not clobber the
7180 padding that shouldn't be clobbered. In the future we could
7181 replace the TREE_ADDRESSABLE check with a check that
7182 get_base_address needs to live in memory. */
7183 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7184 || TREE_CODE (exp) != COMPONENT_REF
7185 || !multiple_p (bitsize, BITS_PER_UNIT)
7186 || !multiple_p (bitpos, BITS_PER_UNIT)
7187 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7188 &decl_bitsize)
7189 || maybe_ne (decl_bitsize, bitsize)))
7190 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7191 decl we must use bitfield operations. */
7192 || (known_size_p (bitsize)
7193 && TREE_CODE (exp) == MEM_REF
7194 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7195 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7196 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7197 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7199 rtx temp;
7200 gimple *nop_def;
7202 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7203 implies a mask operation. If the precision is the same size as
7204 the field we're storing into, that mask is redundant. This is
7205 particularly common with bit field assignments generated by the
7206 C front end. */
7207 nop_def = get_def_for_expr (exp, NOP_EXPR);
7208 if (nop_def)
7210 tree type = TREE_TYPE (exp);
7211 if (INTEGRAL_TYPE_P (type)
7212 && maybe_ne (TYPE_PRECISION (type),
7213 GET_MODE_BITSIZE (TYPE_MODE (type)))
7214 && known_eq (bitsize, TYPE_PRECISION (type)))
7216 tree op = gimple_assign_rhs1 (nop_def);
7217 type = TREE_TYPE (op);
7218 if (INTEGRAL_TYPE_P (type)
7219 && known_ge (TYPE_PRECISION (type), bitsize))
7220 exp = op;
7224 temp = expand_normal (exp);
7226 /* We don't support variable-sized BLKmode bitfields, since our
7227 handling of BLKmode is bound up with the ability to break
7228 things into words. */
7229 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7231 /* Handle calls that return values in multiple non-contiguous locations.
7232 The Irix 6 ABI has examples of this. */
7233 if (GET_CODE (temp) == PARALLEL)
7235 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7236 machine_mode temp_mode = GET_MODE (temp);
7237 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7238 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7239 rtx temp_target = gen_reg_rtx (temp_mode);
7240 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7241 temp = temp_target;
7244 /* Handle calls that return BLKmode values in registers. */
7245 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7247 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7248 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7249 temp = temp_target;
7252 /* If the value has aggregate type and an integral mode then, if BITSIZE
7253 is narrower than this mode and this is for big-endian data, we first
7254 need to put the value into the low-order bits for store_bit_field,
7255 except when MODE is BLKmode and BITSIZE larger than the word size
7256 (see the handling of fields larger than a word in store_bit_field).
7257 Moreover, the field may be not aligned on a byte boundary; in this
7258 case, if it has reverse storage order, it needs to be accessed as a
7259 scalar field with reverse storage order and we must first put the
7260 value into target order. */
7261 scalar_int_mode temp_mode;
7262 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7263 && is_int_mode (GET_MODE (temp), &temp_mode))
7265 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7267 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7269 if (reverse)
7270 temp = flip_storage_order (temp_mode, temp);
7272 gcc_checking_assert (known_le (bitsize, size));
7273 if (maybe_lt (bitsize, size)
7274 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7275 /* Use of to_constant for BLKmode was checked above. */
7276 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7277 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7278 size - bitsize, NULL_RTX, 1);
7281 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7282 if (mode != VOIDmode && mode != BLKmode
7283 && mode != TYPE_MODE (TREE_TYPE (exp)))
7284 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7286 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7287 and BITPOS must be aligned on a byte boundary. If so, we simply do
7288 a block copy. Likewise for a BLKmode-like TARGET. */
7289 if (GET_MODE (temp) == BLKmode
7290 && (GET_MODE (target) == BLKmode
7291 || (MEM_P (target)
7292 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7293 && multiple_p (bitpos, BITS_PER_UNIT)
7294 && multiple_p (bitsize, BITS_PER_UNIT))))
7296 gcc_assert (MEM_P (target) && MEM_P (temp));
7297 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7298 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7300 target = adjust_address (target, VOIDmode, bytepos);
7301 emit_block_move (target, temp,
7302 gen_int_mode (bytesize, Pmode),
7303 BLOCK_OP_NORMAL);
7305 return const0_rtx;
7308 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7309 word size, we need to load the value (see again store_bit_field). */
7310 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7312 temp_mode = smallest_int_mode_for_size (bitsize);
7313 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7314 temp_mode, false, NULL);
7317 /* Store the value in the bitfield. */
7318 gcc_checking_assert (known_ge (bitpos, 0));
7319 store_bit_field (target, bitsize, bitpos,
7320 bitregion_start, bitregion_end,
7321 mode, temp, reverse);
7323 return const0_rtx;
7325 else
7327 /* Now build a reference to just the desired component. */
7328 rtx to_rtx = adjust_address (target, mode,
7329 exact_div (bitpos, BITS_PER_UNIT));
7331 if (to_rtx == target)
7332 to_rtx = copy_rtx (to_rtx);
7334 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7335 set_mem_alias_set (to_rtx, alias_set);
7337 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7338 into a target smaller than its type; handle that case now. */
7339 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7341 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7342 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7343 return to_rtx;
7346 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7350 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7351 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7352 codes and find the ultimate containing object, which we return.
7354 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7355 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7356 storage order of the field.
7357 If the position of the field is variable, we store a tree
7358 giving the variable offset (in units) in *POFFSET.
7359 This offset is in addition to the bit position.
7360 If the position is not variable, we store 0 in *POFFSET.
7362 If any of the extraction expressions is volatile,
7363 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7365 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7366 Otherwise, it is a mode that can be used to access the field.
7368 If the field describes a variable-sized object, *PMODE is set to
7369 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7370 this case, but the address of the object can be found. */
7372 tree
7373 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7374 poly_int64_pod *pbitpos, tree *poffset,
7375 machine_mode *pmode, int *punsignedp,
7376 int *preversep, int *pvolatilep)
7378 tree size_tree = 0;
7379 machine_mode mode = VOIDmode;
7380 bool blkmode_bitfield = false;
7381 tree offset = size_zero_node;
7382 poly_offset_int bit_offset = 0;
7384 /* First get the mode, signedness, storage order and size. We do this from
7385 just the outermost expression. */
7386 *pbitsize = -1;
7387 if (TREE_CODE (exp) == COMPONENT_REF)
7389 tree field = TREE_OPERAND (exp, 1);
7390 size_tree = DECL_SIZE (field);
7391 if (flag_strict_volatile_bitfields > 0
7392 && TREE_THIS_VOLATILE (exp)
7393 && DECL_BIT_FIELD_TYPE (field)
7394 && DECL_MODE (field) != BLKmode)
7395 /* Volatile bitfields should be accessed in the mode of the
7396 field's type, not the mode computed based on the bit
7397 size. */
7398 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7399 else if (!DECL_BIT_FIELD (field))
7401 mode = DECL_MODE (field);
7402 /* For vector fields re-check the target flags, as DECL_MODE
7403 could have been set with different target flags than
7404 the current function has. */
7405 if (mode == BLKmode
7406 && VECTOR_TYPE_P (TREE_TYPE (field))
7407 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7408 mode = TYPE_MODE (TREE_TYPE (field));
7410 else if (DECL_MODE (field) == BLKmode)
7411 blkmode_bitfield = true;
7413 *punsignedp = DECL_UNSIGNED (field);
7415 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7417 size_tree = TREE_OPERAND (exp, 1);
7418 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7419 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7421 /* For vector element types with the correct size of access or for
7422 vector typed accesses use the mode of the access type. */
7423 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7424 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7425 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7426 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7427 mode = TYPE_MODE (TREE_TYPE (exp));
7429 else
7431 mode = TYPE_MODE (TREE_TYPE (exp));
7432 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7434 if (mode == BLKmode)
7435 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7436 else
7437 *pbitsize = GET_MODE_BITSIZE (mode);
7440 if (size_tree != 0)
7442 if (! tree_fits_uhwi_p (size_tree))
7443 mode = BLKmode, *pbitsize = -1;
7444 else
7445 *pbitsize = tree_to_uhwi (size_tree);
7448 *preversep = reverse_storage_order_for_component_p (exp);
7450 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7451 and find the ultimate containing object. */
7452 while (1)
7454 switch (TREE_CODE (exp))
7456 case BIT_FIELD_REF:
7457 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7458 break;
7460 case COMPONENT_REF:
7462 tree field = TREE_OPERAND (exp, 1);
7463 tree this_offset = component_ref_field_offset (exp);
7465 /* If this field hasn't been filled in yet, don't go past it.
7466 This should only happen when folding expressions made during
7467 type construction. */
7468 if (this_offset == 0)
7469 break;
7471 offset = size_binop (PLUS_EXPR, offset, this_offset);
7472 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7474 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7476 break;
7478 case ARRAY_REF:
7479 case ARRAY_RANGE_REF:
7481 tree index = TREE_OPERAND (exp, 1);
7482 tree low_bound = array_ref_low_bound (exp);
7483 tree unit_size = array_ref_element_size (exp);
7485 /* We assume all arrays have sizes that are a multiple of a byte.
7486 First subtract the lower bound, if any, in the type of the
7487 index, then convert to sizetype and multiply by the size of
7488 the array element. */
7489 if (! integer_zerop (low_bound))
7490 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7491 index, low_bound);
7493 offset = size_binop (PLUS_EXPR, offset,
7494 size_binop (MULT_EXPR,
7495 fold_convert (sizetype, index),
7496 unit_size));
7498 break;
7500 case REALPART_EXPR:
7501 break;
7503 case IMAGPART_EXPR:
7504 bit_offset += *pbitsize;
7505 break;
7507 case VIEW_CONVERT_EXPR:
7508 break;
7510 case MEM_REF:
7511 /* Hand back the decl for MEM[&decl, off]. */
7512 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7514 tree off = TREE_OPERAND (exp, 1);
7515 if (!integer_zerop (off))
7517 poly_offset_int boff = mem_ref_offset (exp);
7518 boff <<= LOG2_BITS_PER_UNIT;
7519 bit_offset += boff;
7521 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7523 goto done;
7525 default:
7526 goto done;
7529 /* If any reference in the chain is volatile, the effect is volatile. */
7530 if (TREE_THIS_VOLATILE (exp))
7531 *pvolatilep = 1;
7533 exp = TREE_OPERAND (exp, 0);
7535 done:
7537 /* If OFFSET is constant, see if we can return the whole thing as a
7538 constant bit position. Make sure to handle overflow during
7539 this conversion. */
7540 if (poly_int_tree_p (offset))
7542 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7543 TYPE_PRECISION (sizetype));
7544 tem <<= LOG2_BITS_PER_UNIT;
7545 tem += bit_offset;
7546 if (tem.to_shwi (pbitpos))
7547 *poffset = offset = NULL_TREE;
7550 /* Otherwise, split it up. */
7551 if (offset)
7553 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7554 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7556 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7557 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7558 offset = size_binop (PLUS_EXPR, offset,
7559 build_int_cst (sizetype, bytes.force_shwi ()));
7562 *poffset = offset;
7565 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7566 if (mode == VOIDmode
7567 && blkmode_bitfield
7568 && multiple_p (*pbitpos, BITS_PER_UNIT)
7569 && multiple_p (*pbitsize, BITS_PER_UNIT))
7570 *pmode = BLKmode;
7571 else
7572 *pmode = mode;
7574 return exp;
7577 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7579 static unsigned HOST_WIDE_INT
7580 target_align (const_tree target)
7582 /* We might have a chain of nested references with intermediate misaligning
7583 bitfields components, so need to recurse to find out. */
7585 unsigned HOST_WIDE_INT this_align, outer_align;
7587 switch (TREE_CODE (target))
7589 case BIT_FIELD_REF:
7590 return 1;
7592 case COMPONENT_REF:
7593 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7594 outer_align = target_align (TREE_OPERAND (target, 0));
7595 return MIN (this_align, outer_align);
7597 case ARRAY_REF:
7598 case ARRAY_RANGE_REF:
7599 this_align = TYPE_ALIGN (TREE_TYPE (target));
7600 outer_align = target_align (TREE_OPERAND (target, 0));
7601 return MIN (this_align, outer_align);
7603 CASE_CONVERT:
7604 case NON_LVALUE_EXPR:
7605 case VIEW_CONVERT_EXPR:
7606 this_align = TYPE_ALIGN (TREE_TYPE (target));
7607 outer_align = target_align (TREE_OPERAND (target, 0));
7608 return MAX (this_align, outer_align);
7610 default:
7611 return TYPE_ALIGN (TREE_TYPE (target));
7616 /* Given an rtx VALUE that may contain additions and multiplications, return
7617 an equivalent value that just refers to a register, memory, or constant.
7618 This is done by generating instructions to perform the arithmetic and
7619 returning a pseudo-register containing the value.
7621 The returned value may be a REG, SUBREG, MEM or constant. */
7624 force_operand (rtx value, rtx target)
7626 rtx op1, op2;
7627 /* Use subtarget as the target for operand 0 of a binary operation. */
7628 rtx subtarget = get_subtarget (target);
7629 enum rtx_code code = GET_CODE (value);
7631 /* Check for subreg applied to an expression produced by loop optimizer. */
7632 if (code == SUBREG
7633 && !REG_P (SUBREG_REG (value))
7634 && !MEM_P (SUBREG_REG (value)))
7636 value
7637 = simplify_gen_subreg (GET_MODE (value),
7638 force_reg (GET_MODE (SUBREG_REG (value)),
7639 force_operand (SUBREG_REG (value),
7640 NULL_RTX)),
7641 GET_MODE (SUBREG_REG (value)),
7642 SUBREG_BYTE (value));
7643 code = GET_CODE (value);
7646 /* Check for a PIC address load. */
7647 if ((code == PLUS || code == MINUS)
7648 && XEXP (value, 0) == pic_offset_table_rtx
7649 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7650 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7651 || GET_CODE (XEXP (value, 1)) == CONST))
7653 if (!subtarget)
7654 subtarget = gen_reg_rtx (GET_MODE (value));
7655 emit_move_insn (subtarget, value);
7656 return subtarget;
7659 if (ARITHMETIC_P (value))
7661 op2 = XEXP (value, 1);
7662 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7663 subtarget = 0;
7664 if (code == MINUS && CONST_INT_P (op2))
7666 code = PLUS;
7667 op2 = negate_rtx (GET_MODE (value), op2);
7670 /* Check for an addition with OP2 a constant integer and our first
7671 operand a PLUS of a virtual register and something else. In that
7672 case, we want to emit the sum of the virtual register and the
7673 constant first and then add the other value. This allows virtual
7674 register instantiation to simply modify the constant rather than
7675 creating another one around this addition. */
7676 if (code == PLUS && CONST_INT_P (op2)
7677 && GET_CODE (XEXP (value, 0)) == PLUS
7678 && REG_P (XEXP (XEXP (value, 0), 0))
7679 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7680 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7682 rtx temp = expand_simple_binop (GET_MODE (value), code,
7683 XEXP (XEXP (value, 0), 0), op2,
7684 subtarget, 0, OPTAB_LIB_WIDEN);
7685 return expand_simple_binop (GET_MODE (value), code, temp,
7686 force_operand (XEXP (XEXP (value,
7687 0), 1), 0),
7688 target, 0, OPTAB_LIB_WIDEN);
7691 op1 = force_operand (XEXP (value, 0), subtarget);
7692 op2 = force_operand (op2, NULL_RTX);
7693 switch (code)
7695 case MULT:
7696 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7697 case DIV:
7698 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7699 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7700 target, 1, OPTAB_LIB_WIDEN);
7701 else
7702 return expand_divmod (0,
7703 FLOAT_MODE_P (GET_MODE (value))
7704 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7705 GET_MODE (value), op1, op2, target, 0);
7706 case MOD:
7707 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7708 target, 0);
7709 case UDIV:
7710 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7711 target, 1);
7712 case UMOD:
7713 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7714 target, 1);
7715 case ASHIFTRT:
7716 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7717 target, 0, OPTAB_LIB_WIDEN);
7718 default:
7719 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7720 target, 1, OPTAB_LIB_WIDEN);
7723 if (UNARY_P (value))
7725 if (!target)
7726 target = gen_reg_rtx (GET_MODE (value));
7727 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7728 switch (code)
7730 case ZERO_EXTEND:
7731 case SIGN_EXTEND:
7732 case TRUNCATE:
7733 case FLOAT_EXTEND:
7734 case FLOAT_TRUNCATE:
7735 convert_move (target, op1, code == ZERO_EXTEND);
7736 return target;
7738 case FIX:
7739 case UNSIGNED_FIX:
7740 expand_fix (target, op1, code == UNSIGNED_FIX);
7741 return target;
7743 case FLOAT:
7744 case UNSIGNED_FLOAT:
7745 expand_float (target, op1, code == UNSIGNED_FLOAT);
7746 return target;
7748 default:
7749 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7753 #ifdef INSN_SCHEDULING
7754 /* On machines that have insn scheduling, we want all memory reference to be
7755 explicit, so we need to deal with such paradoxical SUBREGs. */
7756 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7757 value
7758 = simplify_gen_subreg (GET_MODE (value),
7759 force_reg (GET_MODE (SUBREG_REG (value)),
7760 force_operand (SUBREG_REG (value),
7761 NULL_RTX)),
7762 GET_MODE (SUBREG_REG (value)),
7763 SUBREG_BYTE (value));
7764 #endif
7766 return value;
7769 /* Subroutine of expand_expr: return nonzero iff there is no way that
7770 EXP can reference X, which is being modified. TOP_P is nonzero if this
7771 call is going to be used to determine whether we need a temporary
7772 for EXP, as opposed to a recursive call to this function.
7774 It is always safe for this routine to return zero since it merely
7775 searches for optimization opportunities. */
7778 safe_from_p (const_rtx x, tree exp, int top_p)
7780 rtx exp_rtl = 0;
7781 int i, nops;
7783 if (x == 0
7784 /* If EXP has varying size, we MUST use a target since we currently
7785 have no way of allocating temporaries of variable size
7786 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7787 So we assume here that something at a higher level has prevented a
7788 clash. This is somewhat bogus, but the best we can do. Only
7789 do this when X is BLKmode and when we are at the top level. */
7790 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7791 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7792 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7793 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7794 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7795 != INTEGER_CST)
7796 && GET_MODE (x) == BLKmode)
7797 /* If X is in the outgoing argument area, it is always safe. */
7798 || (MEM_P (x)
7799 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7800 || (GET_CODE (XEXP (x, 0)) == PLUS
7801 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7802 return 1;
7804 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7805 find the underlying pseudo. */
7806 if (GET_CODE (x) == SUBREG)
7808 x = SUBREG_REG (x);
7809 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7810 return 0;
7813 /* Now look at our tree code and possibly recurse. */
7814 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7816 case tcc_declaration:
7817 exp_rtl = DECL_RTL_IF_SET (exp);
7818 break;
7820 case tcc_constant:
7821 return 1;
7823 case tcc_exceptional:
7824 if (TREE_CODE (exp) == TREE_LIST)
7826 while (1)
7828 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7829 return 0;
7830 exp = TREE_CHAIN (exp);
7831 if (!exp)
7832 return 1;
7833 if (TREE_CODE (exp) != TREE_LIST)
7834 return safe_from_p (x, exp, 0);
7837 else if (TREE_CODE (exp) == CONSTRUCTOR)
7839 constructor_elt *ce;
7840 unsigned HOST_WIDE_INT idx;
7842 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7843 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7844 || !safe_from_p (x, ce->value, 0))
7845 return 0;
7846 return 1;
7848 else if (TREE_CODE (exp) == ERROR_MARK)
7849 return 1; /* An already-visited SAVE_EXPR? */
7850 else
7851 return 0;
7853 case tcc_statement:
7854 /* The only case we look at here is the DECL_INITIAL inside a
7855 DECL_EXPR. */
7856 return (TREE_CODE (exp) != DECL_EXPR
7857 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7858 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7859 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7861 case tcc_binary:
7862 case tcc_comparison:
7863 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7864 return 0;
7865 /* Fall through. */
7867 case tcc_unary:
7868 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7870 case tcc_expression:
7871 case tcc_reference:
7872 case tcc_vl_exp:
7873 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7874 the expression. If it is set, we conflict iff we are that rtx or
7875 both are in memory. Otherwise, we check all operands of the
7876 expression recursively. */
7878 switch (TREE_CODE (exp))
7880 case ADDR_EXPR:
7881 /* If the operand is static or we are static, we can't conflict.
7882 Likewise if we don't conflict with the operand at all. */
7883 if (staticp (TREE_OPERAND (exp, 0))
7884 || TREE_STATIC (exp)
7885 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7886 return 1;
7888 /* Otherwise, the only way this can conflict is if we are taking
7889 the address of a DECL a that address if part of X, which is
7890 very rare. */
7891 exp = TREE_OPERAND (exp, 0);
7892 if (DECL_P (exp))
7894 if (!DECL_RTL_SET_P (exp)
7895 || !MEM_P (DECL_RTL (exp)))
7896 return 0;
7897 else
7898 exp_rtl = XEXP (DECL_RTL (exp), 0);
7900 break;
7902 case MEM_REF:
7903 if (MEM_P (x)
7904 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7905 get_alias_set (exp)))
7906 return 0;
7907 break;
7909 case CALL_EXPR:
7910 /* Assume that the call will clobber all hard registers and
7911 all of memory. */
7912 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7913 || MEM_P (x))
7914 return 0;
7915 break;
7917 case WITH_CLEANUP_EXPR:
7918 case CLEANUP_POINT_EXPR:
7919 /* Lowered by gimplify.c. */
7920 gcc_unreachable ();
7922 case SAVE_EXPR:
7923 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7925 default:
7926 break;
7929 /* If we have an rtx, we do not need to scan our operands. */
7930 if (exp_rtl)
7931 break;
7933 nops = TREE_OPERAND_LENGTH (exp);
7934 for (i = 0; i < nops; i++)
7935 if (TREE_OPERAND (exp, i) != 0
7936 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7937 return 0;
7939 break;
7941 case tcc_type:
7942 /* Should never get a type here. */
7943 gcc_unreachable ();
7946 /* If we have an rtl, find any enclosed object. Then see if we conflict
7947 with it. */
7948 if (exp_rtl)
7950 if (GET_CODE (exp_rtl) == SUBREG)
7952 exp_rtl = SUBREG_REG (exp_rtl);
7953 if (REG_P (exp_rtl)
7954 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7955 return 0;
7958 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7959 are memory and they conflict. */
7960 return ! (rtx_equal_p (x, exp_rtl)
7961 || (MEM_P (x) && MEM_P (exp_rtl)
7962 && true_dependence (exp_rtl, VOIDmode, x)));
7965 /* If we reach here, it is safe. */
7966 return 1;
7970 /* Return the highest power of two that EXP is known to be a multiple of.
7971 This is used in updating alignment of MEMs in array references. */
7973 unsigned HOST_WIDE_INT
7974 highest_pow2_factor (const_tree exp)
7976 unsigned HOST_WIDE_INT ret;
7977 int trailing_zeros = tree_ctz (exp);
7978 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7979 return BIGGEST_ALIGNMENT;
7980 ret = HOST_WIDE_INT_1U << trailing_zeros;
7981 if (ret > BIGGEST_ALIGNMENT)
7982 return BIGGEST_ALIGNMENT;
7983 return ret;
7986 /* Similar, except that the alignment requirements of TARGET are
7987 taken into account. Assume it is at least as aligned as its
7988 type, unless it is a COMPONENT_REF in which case the layout of
7989 the structure gives the alignment. */
7991 static unsigned HOST_WIDE_INT
7992 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7994 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7995 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7997 return MAX (factor, talign);
8000 /* Convert the tree comparison code TCODE to the rtl one where the
8001 signedness is UNSIGNEDP. */
8003 static enum rtx_code
8004 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8006 enum rtx_code code;
8007 switch (tcode)
8009 case EQ_EXPR:
8010 code = EQ;
8011 break;
8012 case NE_EXPR:
8013 code = NE;
8014 break;
8015 case LT_EXPR:
8016 code = unsignedp ? LTU : LT;
8017 break;
8018 case LE_EXPR:
8019 code = unsignedp ? LEU : LE;
8020 break;
8021 case GT_EXPR:
8022 code = unsignedp ? GTU : GT;
8023 break;
8024 case GE_EXPR:
8025 code = unsignedp ? GEU : GE;
8026 break;
8027 case UNORDERED_EXPR:
8028 code = UNORDERED;
8029 break;
8030 case ORDERED_EXPR:
8031 code = ORDERED;
8032 break;
8033 case UNLT_EXPR:
8034 code = UNLT;
8035 break;
8036 case UNLE_EXPR:
8037 code = UNLE;
8038 break;
8039 case UNGT_EXPR:
8040 code = UNGT;
8041 break;
8042 case UNGE_EXPR:
8043 code = UNGE;
8044 break;
8045 case UNEQ_EXPR:
8046 code = UNEQ;
8047 break;
8048 case LTGT_EXPR:
8049 code = LTGT;
8050 break;
8052 default:
8053 gcc_unreachable ();
8055 return code;
8058 /* Subroutine of expand_expr. Expand the two operands of a binary
8059 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8060 The value may be stored in TARGET if TARGET is nonzero. The
8061 MODIFIER argument is as documented by expand_expr. */
8063 void
8064 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8065 enum expand_modifier modifier)
8067 if (! safe_from_p (target, exp1, 1))
8068 target = 0;
8069 if (operand_equal_p (exp0, exp1, 0))
8071 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8072 *op1 = copy_rtx (*op0);
8074 else
8076 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8077 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8082 /* Return a MEM that contains constant EXP. DEFER is as for
8083 output_constant_def and MODIFIER is as for expand_expr. */
8085 static rtx
8086 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8088 rtx mem;
8090 mem = output_constant_def (exp, defer);
8091 if (modifier != EXPAND_INITIALIZER)
8092 mem = use_anchored_address (mem);
8093 return mem;
8096 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8097 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8099 static rtx
8100 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8101 enum expand_modifier modifier, addr_space_t as)
8103 rtx result, subtarget;
8104 tree inner, offset;
8105 poly_int64 bitsize, bitpos;
8106 int unsignedp, reversep, volatilep = 0;
8107 machine_mode mode1;
8109 /* If we are taking the address of a constant and are at the top level,
8110 we have to use output_constant_def since we can't call force_const_mem
8111 at top level. */
8112 /* ??? This should be considered a front-end bug. We should not be
8113 generating ADDR_EXPR of something that isn't an LVALUE. The only
8114 exception here is STRING_CST. */
8115 if (CONSTANT_CLASS_P (exp))
8117 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8118 if (modifier < EXPAND_SUM)
8119 result = force_operand (result, target);
8120 return result;
8123 /* Everything must be something allowed by is_gimple_addressable. */
8124 switch (TREE_CODE (exp))
8126 case INDIRECT_REF:
8127 /* This case will happen via recursion for &a->b. */
8128 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8130 case MEM_REF:
8132 tree tem = TREE_OPERAND (exp, 0);
8133 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8134 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8135 return expand_expr (tem, target, tmode, modifier);
8138 case TARGET_MEM_REF:
8139 return addr_for_mem_ref (exp, as, true);
8141 case CONST_DECL:
8142 /* Expand the initializer like constants above. */
8143 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8144 0, modifier), 0);
8145 if (modifier < EXPAND_SUM)
8146 result = force_operand (result, target);
8147 return result;
8149 case REALPART_EXPR:
8150 /* The real part of the complex number is always first, therefore
8151 the address is the same as the address of the parent object. */
8152 offset = 0;
8153 bitpos = 0;
8154 inner = TREE_OPERAND (exp, 0);
8155 break;
8157 case IMAGPART_EXPR:
8158 /* The imaginary part of the complex number is always second.
8159 The expression is therefore always offset by the size of the
8160 scalar type. */
8161 offset = 0;
8162 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8163 inner = TREE_OPERAND (exp, 0);
8164 break;
8166 case COMPOUND_LITERAL_EXPR:
8167 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8168 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8169 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8170 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8171 the initializers aren't gimplified. */
8172 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8173 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
8174 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8175 target, tmode, modifier, as);
8176 /* FALLTHRU */
8177 default:
8178 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8179 expand_expr, as that can have various side effects; LABEL_DECLs for
8180 example, may not have their DECL_RTL set yet. Expand the rtl of
8181 CONSTRUCTORs too, which should yield a memory reference for the
8182 constructor's contents. Assume language specific tree nodes can
8183 be expanded in some interesting way. */
8184 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8185 if (DECL_P (exp)
8186 || TREE_CODE (exp) == CONSTRUCTOR
8187 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8189 result = expand_expr (exp, target, tmode,
8190 modifier == EXPAND_INITIALIZER
8191 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8193 /* If the DECL isn't in memory, then the DECL wasn't properly
8194 marked TREE_ADDRESSABLE, which will be either a front-end
8195 or a tree optimizer bug. */
8197 gcc_assert (MEM_P (result));
8198 result = XEXP (result, 0);
8200 /* ??? Is this needed anymore? */
8201 if (DECL_P (exp))
8202 TREE_USED (exp) = 1;
8204 if (modifier != EXPAND_INITIALIZER
8205 && modifier != EXPAND_CONST_ADDRESS
8206 && modifier != EXPAND_SUM)
8207 result = force_operand (result, target);
8208 return result;
8211 /* Pass FALSE as the last argument to get_inner_reference although
8212 we are expanding to RTL. The rationale is that we know how to
8213 handle "aligning nodes" here: we can just bypass them because
8214 they won't change the final object whose address will be returned
8215 (they actually exist only for that purpose). */
8216 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8217 &unsignedp, &reversep, &volatilep);
8218 break;
8221 /* We must have made progress. */
8222 gcc_assert (inner != exp);
8224 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8225 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8226 inner alignment, force the inner to be sufficiently aligned. */
8227 if (CONSTANT_CLASS_P (inner)
8228 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8230 inner = copy_node (inner);
8231 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8232 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8233 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8235 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8237 if (offset)
8239 rtx tmp;
8241 if (modifier != EXPAND_NORMAL)
8242 result = force_operand (result, NULL);
8243 tmp = expand_expr (offset, NULL_RTX, tmode,
8244 modifier == EXPAND_INITIALIZER
8245 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8247 /* expand_expr is allowed to return an object in a mode other
8248 than TMODE. If it did, we need to convert. */
8249 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8250 tmp = convert_modes (tmode, GET_MODE (tmp),
8251 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8252 result = convert_memory_address_addr_space (tmode, result, as);
8253 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8255 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8256 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8257 else
8259 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8260 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8261 1, OPTAB_LIB_WIDEN);
8265 if (maybe_ne (bitpos, 0))
8267 /* Someone beforehand should have rejected taking the address
8268 of an object that isn't byte-aligned. */
8269 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8270 result = convert_memory_address_addr_space (tmode, result, as);
8271 result = plus_constant (tmode, result, bytepos);
8272 if (modifier < EXPAND_SUM)
8273 result = force_operand (result, target);
8276 return result;
8279 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8280 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8282 static rtx
8283 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8284 enum expand_modifier modifier)
8286 addr_space_t as = ADDR_SPACE_GENERIC;
8287 scalar_int_mode address_mode = Pmode;
8288 scalar_int_mode pointer_mode = ptr_mode;
8289 machine_mode rmode;
8290 rtx result;
8292 /* Target mode of VOIDmode says "whatever's natural". */
8293 if (tmode == VOIDmode)
8294 tmode = TYPE_MODE (TREE_TYPE (exp));
8296 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8298 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8299 address_mode = targetm.addr_space.address_mode (as);
8300 pointer_mode = targetm.addr_space.pointer_mode (as);
8303 /* We can get called with some Weird Things if the user does silliness
8304 like "(short) &a". In that case, convert_memory_address won't do
8305 the right thing, so ignore the given target mode. */
8306 scalar_int_mode new_tmode = (tmode == pointer_mode
8307 ? pointer_mode
8308 : address_mode);
8310 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8311 new_tmode, modifier, as);
8313 /* Despite expand_expr claims concerning ignoring TMODE when not
8314 strictly convenient, stuff breaks if we don't honor it. Note
8315 that combined with the above, we only do this for pointer modes. */
8316 rmode = GET_MODE (result);
8317 if (rmode == VOIDmode)
8318 rmode = new_tmode;
8319 if (rmode != new_tmode)
8320 result = convert_memory_address_addr_space (new_tmode, result, as);
8322 return result;
8325 /* Generate code for computing CONSTRUCTOR EXP.
8326 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8327 is TRUE, instead of creating a temporary variable in memory
8328 NULL is returned and the caller needs to handle it differently. */
8330 static rtx
8331 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8332 bool avoid_temp_mem)
8334 tree type = TREE_TYPE (exp);
8335 machine_mode mode = TYPE_MODE (type);
8337 /* Try to avoid creating a temporary at all. This is possible
8338 if all of the initializer is zero.
8339 FIXME: try to handle all [0..255] initializers we can handle
8340 with memset. */
8341 if (TREE_STATIC (exp)
8342 && !TREE_ADDRESSABLE (exp)
8343 && target != 0 && mode == BLKmode
8344 && all_zeros_p (exp))
8346 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8347 return target;
8350 /* All elts simple constants => refer to a constant in memory. But
8351 if this is a non-BLKmode mode, let it store a field at a time
8352 since that should make a CONST_INT, CONST_WIDE_INT or
8353 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8354 use, it is best to store directly into the target unless the type
8355 is large enough that memcpy will be used. If we are making an
8356 initializer and all operands are constant, put it in memory as
8357 well.
8359 FIXME: Avoid trying to fill vector constructors piece-meal.
8360 Output them with output_constant_def below unless we're sure
8361 they're zeros. This should go away when vector initializers
8362 are treated like VECTOR_CST instead of arrays. */
8363 if ((TREE_STATIC (exp)
8364 && ((mode == BLKmode
8365 && ! (target != 0 && safe_from_p (target, exp, 1)))
8366 || TREE_ADDRESSABLE (exp)
8367 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8368 && (! can_move_by_pieces
8369 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8370 TYPE_ALIGN (type)))
8371 && ! mostly_zeros_p (exp))))
8372 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8373 && TREE_CONSTANT (exp)))
8375 rtx constructor;
8377 if (avoid_temp_mem)
8378 return NULL_RTX;
8380 constructor = expand_expr_constant (exp, 1, modifier);
8382 if (modifier != EXPAND_CONST_ADDRESS
8383 && modifier != EXPAND_INITIALIZER
8384 && modifier != EXPAND_SUM)
8385 constructor = validize_mem (constructor);
8387 return constructor;
8390 /* Handle calls that pass values in multiple non-contiguous
8391 locations. The Irix 6 ABI has examples of this. */
8392 if (target == 0 || ! safe_from_p (target, exp, 1)
8393 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8394 /* Also make a temporary if the store is to volatile memory, to
8395 avoid individual accesses to aggregate members. */
8396 || (GET_CODE (target) == MEM
8397 && MEM_VOLATILE_P (target)
8398 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8400 if (avoid_temp_mem)
8401 return NULL_RTX;
8403 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8406 store_constructor (exp, target, 0, int_expr_size (exp), false);
8407 return target;
8411 /* expand_expr: generate code for computing expression EXP.
8412 An rtx for the computed value is returned. The value is never null.
8413 In the case of a void EXP, const0_rtx is returned.
8415 The value may be stored in TARGET if TARGET is nonzero.
8416 TARGET is just a suggestion; callers must assume that
8417 the rtx returned may not be the same as TARGET.
8419 If TARGET is CONST0_RTX, it means that the value will be ignored.
8421 If TMODE is not VOIDmode, it suggests generating the
8422 result in mode TMODE. But this is done only when convenient.
8423 Otherwise, TMODE is ignored and the value generated in its natural mode.
8424 TMODE is just a suggestion; callers must assume that
8425 the rtx returned may not have mode TMODE.
8427 Note that TARGET may have neither TMODE nor MODE. In that case, it
8428 probably will not be used.
8430 If MODIFIER is EXPAND_SUM then when EXP is an addition
8431 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8432 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8433 products as above, or REG or MEM, or constant.
8434 Ordinarily in such cases we would output mul or add instructions
8435 and then return a pseudo reg containing the sum.
8437 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8438 it also marks a label as absolutely required (it can't be dead).
8439 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8440 This is used for outputting expressions used in initializers.
8442 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8443 with a constant address even if that address is not normally legitimate.
8444 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8446 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8447 a call parameter. Such targets require special care as we haven't yet
8448 marked TARGET so that it's safe from being trashed by libcalls. We
8449 don't want to use TARGET for anything but the final result;
8450 Intermediate values must go elsewhere. Additionally, calls to
8451 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8453 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8454 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8455 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8456 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8457 recursively.
8458 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8459 then *ALT_RTL is set to TARGET (before legitimziation).
8461 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8462 In this case, we don't adjust a returned MEM rtx that wouldn't be
8463 sufficiently aligned for its mode; instead, it's up to the caller
8464 to deal with it afterwards. This is used to make sure that unaligned
8465 base objects for which out-of-bounds accesses are supported, for
8466 example record types with trailing arrays, aren't realigned behind
8467 the back of the caller.
8468 The normal operating mode is to pass FALSE for this parameter. */
8471 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8472 enum expand_modifier modifier, rtx *alt_rtl,
8473 bool inner_reference_p)
8475 rtx ret;
8477 /* Handle ERROR_MARK before anybody tries to access its type. */
8478 if (TREE_CODE (exp) == ERROR_MARK
8479 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8481 ret = CONST0_RTX (tmode);
8482 return ret ? ret : const0_rtx;
8485 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8486 inner_reference_p);
8487 return ret;
8490 /* Try to expand the conditional expression which is represented by
8491 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8492 return the rtl reg which represents the result. Otherwise return
8493 NULL_RTX. */
8495 static rtx
8496 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8497 tree treeop1 ATTRIBUTE_UNUSED,
8498 tree treeop2 ATTRIBUTE_UNUSED)
8500 rtx insn;
8501 rtx op00, op01, op1, op2;
8502 enum rtx_code comparison_code;
8503 machine_mode comparison_mode;
8504 gimple *srcstmt;
8505 rtx temp;
8506 tree type = TREE_TYPE (treeop1);
8507 int unsignedp = TYPE_UNSIGNED (type);
8508 machine_mode mode = TYPE_MODE (type);
8509 machine_mode orig_mode = mode;
8510 static bool expanding_cond_expr_using_cmove = false;
8512 /* Conditional move expansion can end up TERing two operands which,
8513 when recursively hitting conditional expressions can result in
8514 exponential behavior if the cmove expansion ultimatively fails.
8515 It's hardly profitable to TER a cmove into a cmove so avoid doing
8516 that by failing early if we end up recursing. */
8517 if (expanding_cond_expr_using_cmove)
8518 return NULL_RTX;
8520 /* If we cannot do a conditional move on the mode, try doing it
8521 with the promoted mode. */
8522 if (!can_conditionally_move_p (mode))
8524 mode = promote_mode (type, mode, &unsignedp);
8525 if (!can_conditionally_move_p (mode))
8526 return NULL_RTX;
8527 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8529 else
8530 temp = assign_temp (type, 0, 1);
8532 expanding_cond_expr_using_cmove = true;
8533 start_sequence ();
8534 expand_operands (treeop1, treeop2,
8535 temp, &op1, &op2, EXPAND_NORMAL);
8537 if (TREE_CODE (treeop0) == SSA_NAME
8538 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8540 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8541 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8542 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8543 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8544 comparison_mode = TYPE_MODE (type);
8545 unsignedp = TYPE_UNSIGNED (type);
8546 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8548 else if (COMPARISON_CLASS_P (treeop0))
8550 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8551 enum tree_code cmpcode = TREE_CODE (treeop0);
8552 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8553 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8554 unsignedp = TYPE_UNSIGNED (type);
8555 comparison_mode = TYPE_MODE (type);
8556 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8558 else
8560 op00 = expand_normal (treeop0);
8561 op01 = const0_rtx;
8562 comparison_code = NE;
8563 comparison_mode = GET_MODE (op00);
8564 if (comparison_mode == VOIDmode)
8565 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8567 expanding_cond_expr_using_cmove = false;
8569 if (GET_MODE (op1) != mode)
8570 op1 = gen_lowpart (mode, op1);
8572 if (GET_MODE (op2) != mode)
8573 op2 = gen_lowpart (mode, op2);
8575 /* Try to emit the conditional move. */
8576 insn = emit_conditional_move (temp, comparison_code,
8577 op00, op01, comparison_mode,
8578 op1, op2, mode,
8579 unsignedp);
8581 /* If we could do the conditional move, emit the sequence,
8582 and return. */
8583 if (insn)
8585 rtx_insn *seq = get_insns ();
8586 end_sequence ();
8587 emit_insn (seq);
8588 return convert_modes (orig_mode, mode, temp, 0);
8591 /* Otherwise discard the sequence and fall back to code with
8592 branches. */
8593 end_sequence ();
8594 return NULL_RTX;
8597 /* A helper function for expand_expr_real_2 to be used with a
8598 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
8599 is nonzero, with alignment ALIGN in bits.
8600 Store the value at TARGET if possible (if TARGET is nonzero).
8601 Regardless of TARGET, we return the rtx for where the value is placed.
8602 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8603 then *ALT_RTL is set to TARGET (before legitimziation). */
8605 static rtx
8606 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
8607 unsigned int align, rtx target, rtx *alt_rtl)
8609 enum insn_code icode;
8611 if ((icode = optab_handler (movmisalign_optab, mode))
8612 != CODE_FOR_nothing)
8614 class expand_operand ops[2];
8616 /* We've already validated the memory, and we're creating a
8617 new pseudo destination. The predicates really can't fail,
8618 nor can the generator. */
8619 create_output_operand (&ops[0], NULL_RTX, mode);
8620 create_fixed_operand (&ops[1], temp);
8621 expand_insn (icode, 2, ops);
8622 temp = ops[0].value;
8624 else if (targetm.slow_unaligned_access (mode, align))
8625 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
8626 0, unsignedp, target,
8627 mode, mode, false, alt_rtl);
8628 return temp;
8632 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8633 enum expand_modifier modifier)
8635 rtx op0, op1, op2, temp;
8636 rtx_code_label *lab;
8637 tree type;
8638 int unsignedp;
8639 machine_mode mode;
8640 scalar_int_mode int_mode;
8641 enum tree_code code = ops->code;
8642 optab this_optab;
8643 rtx subtarget, original_target;
8644 int ignore;
8645 bool reduce_bit_field;
8646 location_t loc = ops->location;
8647 tree treeop0, treeop1, treeop2;
8648 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8649 ? reduce_to_bit_field_precision ((expr), \
8650 target, \
8651 type) \
8652 : (expr))
8654 type = ops->type;
8655 mode = TYPE_MODE (type);
8656 unsignedp = TYPE_UNSIGNED (type);
8658 treeop0 = ops->op0;
8659 treeop1 = ops->op1;
8660 treeop2 = ops->op2;
8662 /* We should be called only on simple (binary or unary) expressions,
8663 exactly those that are valid in gimple expressions that aren't
8664 GIMPLE_SINGLE_RHS (or invalid). */
8665 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8666 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8667 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8669 ignore = (target == const0_rtx
8670 || ((CONVERT_EXPR_CODE_P (code)
8671 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8672 && TREE_CODE (type) == VOID_TYPE));
8674 /* We should be called only if we need the result. */
8675 gcc_assert (!ignore);
8677 /* An operation in what may be a bit-field type needs the
8678 result to be reduced to the precision of the bit-field type,
8679 which is narrower than that of the type's mode. */
8680 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8681 && !type_has_mode_precision_p (type));
8683 if (reduce_bit_field
8684 && (modifier == EXPAND_STACK_PARM
8685 || (target && GET_MODE (target) != mode)))
8686 target = 0;
8688 /* Use subtarget as the target for operand 0 of a binary operation. */
8689 subtarget = get_subtarget (target);
8690 original_target = target;
8692 switch (code)
8694 case NON_LVALUE_EXPR:
8695 case PAREN_EXPR:
8696 CASE_CONVERT:
8697 if (treeop0 == error_mark_node)
8698 return const0_rtx;
8700 if (TREE_CODE (type) == UNION_TYPE)
8702 tree valtype = TREE_TYPE (treeop0);
8704 /* If both input and output are BLKmode, this conversion isn't doing
8705 anything except possibly changing memory attribute. */
8706 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8708 rtx result = expand_expr (treeop0, target, tmode,
8709 modifier);
8711 result = copy_rtx (result);
8712 set_mem_attributes (result, type, 0);
8713 return result;
8716 if (target == 0)
8718 if (TYPE_MODE (type) != BLKmode)
8719 target = gen_reg_rtx (TYPE_MODE (type));
8720 else
8721 target = assign_temp (type, 1, 1);
8724 if (MEM_P (target))
8725 /* Store data into beginning of memory target. */
8726 store_expr (treeop0,
8727 adjust_address (target, TYPE_MODE (valtype), 0),
8728 modifier == EXPAND_STACK_PARM,
8729 false, TYPE_REVERSE_STORAGE_ORDER (type));
8731 else
8733 gcc_assert (REG_P (target)
8734 && !TYPE_REVERSE_STORAGE_ORDER (type));
8736 /* Store this field into a union of the proper type. */
8737 poly_uint64 op0_size
8738 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
8739 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
8740 store_field (target,
8741 /* The conversion must be constructed so that
8742 we know at compile time how many bits
8743 to preserve. */
8744 ordered_min (op0_size, union_size),
8745 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8746 false, false);
8749 /* Return the entire union. */
8750 return target;
8753 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8755 op0 = expand_expr (treeop0, target, VOIDmode,
8756 modifier);
8758 /* If the signedness of the conversion differs and OP0 is
8759 a promoted SUBREG, clear that indication since we now
8760 have to do the proper extension. */
8761 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8762 && GET_CODE (op0) == SUBREG)
8763 SUBREG_PROMOTED_VAR_P (op0) = 0;
8765 return REDUCE_BIT_FIELD (op0);
8768 op0 = expand_expr (treeop0, NULL_RTX, mode,
8769 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8770 if (GET_MODE (op0) == mode)
8773 /* If OP0 is a constant, just convert it into the proper mode. */
8774 else if (CONSTANT_P (op0))
8776 tree inner_type = TREE_TYPE (treeop0);
8777 machine_mode inner_mode = GET_MODE (op0);
8779 if (inner_mode == VOIDmode)
8780 inner_mode = TYPE_MODE (inner_type);
8782 if (modifier == EXPAND_INITIALIZER)
8783 op0 = lowpart_subreg (mode, op0, inner_mode);
8784 else
8785 op0= convert_modes (mode, inner_mode, op0,
8786 TYPE_UNSIGNED (inner_type));
8789 else if (modifier == EXPAND_INITIALIZER)
8790 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8791 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8793 else if (target == 0)
8794 op0 = convert_to_mode (mode, op0,
8795 TYPE_UNSIGNED (TREE_TYPE
8796 (treeop0)));
8797 else
8799 convert_move (target, op0,
8800 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8801 op0 = target;
8804 return REDUCE_BIT_FIELD (op0);
8806 case ADDR_SPACE_CONVERT_EXPR:
8808 tree treeop0_type = TREE_TYPE (treeop0);
8810 gcc_assert (POINTER_TYPE_P (type));
8811 gcc_assert (POINTER_TYPE_P (treeop0_type));
8813 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8814 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8816 /* Conversions between pointers to the same address space should
8817 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8818 gcc_assert (as_to != as_from);
8820 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8822 /* Ask target code to handle conversion between pointers
8823 to overlapping address spaces. */
8824 if (targetm.addr_space.subset_p (as_to, as_from)
8825 || targetm.addr_space.subset_p (as_from, as_to))
8827 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8829 else
8831 /* For disjoint address spaces, converting anything but a null
8832 pointer invokes undefined behavior. We truncate or extend the
8833 value as if we'd converted via integers, which handles 0 as
8834 required, and all others as the programmer likely expects. */
8835 #ifndef POINTERS_EXTEND_UNSIGNED
8836 const int POINTERS_EXTEND_UNSIGNED = 1;
8837 #endif
8838 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8839 op0, POINTERS_EXTEND_UNSIGNED);
8841 gcc_assert (op0);
8842 return op0;
8845 case POINTER_PLUS_EXPR:
8846 /* Even though the sizetype mode and the pointer's mode can be different
8847 expand is able to handle this correctly and get the correct result out
8848 of the PLUS_EXPR code. */
8849 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8850 if sizetype precision is smaller than pointer precision. */
8851 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8852 treeop1 = fold_convert_loc (loc, type,
8853 fold_convert_loc (loc, ssizetype,
8854 treeop1));
8855 /* If sizetype precision is larger than pointer precision, truncate the
8856 offset to have matching modes. */
8857 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8858 treeop1 = fold_convert_loc (loc, type, treeop1);
8859 /* FALLTHRU */
8861 case PLUS_EXPR:
8862 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8863 something else, make sure we add the register to the constant and
8864 then to the other thing. This case can occur during strength
8865 reduction and doing it this way will produce better code if the
8866 frame pointer or argument pointer is eliminated.
8868 fold-const.c will ensure that the constant is always in the inner
8869 PLUS_EXPR, so the only case we need to do anything about is if
8870 sp, ap, or fp is our second argument, in which case we must swap
8871 the innermost first argument and our second argument. */
8873 if (TREE_CODE (treeop0) == PLUS_EXPR
8874 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8875 && VAR_P (treeop1)
8876 && (DECL_RTL (treeop1) == frame_pointer_rtx
8877 || DECL_RTL (treeop1) == stack_pointer_rtx
8878 || DECL_RTL (treeop1) == arg_pointer_rtx))
8880 gcc_unreachable ();
8883 /* If the result is to be ptr_mode and we are adding an integer to
8884 something, we might be forming a constant. So try to use
8885 plus_constant. If it produces a sum and we can't accept it,
8886 use force_operand. This allows P = &ARR[const] to generate
8887 efficient code on machines where a SYMBOL_REF is not a valid
8888 address.
8890 If this is an EXPAND_SUM call, always return the sum. */
8891 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8892 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8894 if (modifier == EXPAND_STACK_PARM)
8895 target = 0;
8896 if (TREE_CODE (treeop0) == INTEGER_CST
8897 && HWI_COMPUTABLE_MODE_P (mode)
8898 && TREE_CONSTANT (treeop1))
8900 rtx constant_part;
8901 HOST_WIDE_INT wc;
8902 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8904 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8905 EXPAND_SUM);
8906 /* Use wi::shwi to ensure that the constant is
8907 truncated according to the mode of OP1, then sign extended
8908 to a HOST_WIDE_INT. Using the constant directly can result
8909 in non-canonical RTL in a 64x32 cross compile. */
8910 wc = TREE_INT_CST_LOW (treeop0);
8911 constant_part =
8912 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8913 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8914 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8915 op1 = force_operand (op1, target);
8916 return REDUCE_BIT_FIELD (op1);
8919 else if (TREE_CODE (treeop1) == INTEGER_CST
8920 && HWI_COMPUTABLE_MODE_P (mode)
8921 && TREE_CONSTANT (treeop0))
8923 rtx constant_part;
8924 HOST_WIDE_INT wc;
8925 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8927 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8928 (modifier == EXPAND_INITIALIZER
8929 ? EXPAND_INITIALIZER : EXPAND_SUM));
8930 if (! CONSTANT_P (op0))
8932 op1 = expand_expr (treeop1, NULL_RTX,
8933 VOIDmode, modifier);
8934 /* Return a PLUS if modifier says it's OK. */
8935 if (modifier == EXPAND_SUM
8936 || modifier == EXPAND_INITIALIZER)
8937 return simplify_gen_binary (PLUS, mode, op0, op1);
8938 goto binop2;
8940 /* Use wi::shwi to ensure that the constant is
8941 truncated according to the mode of OP1, then sign extended
8942 to a HOST_WIDE_INT. Using the constant directly can result
8943 in non-canonical RTL in a 64x32 cross compile. */
8944 wc = TREE_INT_CST_LOW (treeop1);
8945 constant_part
8946 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8947 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8948 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8949 op0 = force_operand (op0, target);
8950 return REDUCE_BIT_FIELD (op0);
8954 /* Use TER to expand pointer addition of a negated value
8955 as pointer subtraction. */
8956 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8957 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8958 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8959 && TREE_CODE (treeop1) == SSA_NAME
8960 && TYPE_MODE (TREE_TYPE (treeop0))
8961 == TYPE_MODE (TREE_TYPE (treeop1)))
8963 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8964 if (def)
8966 treeop1 = gimple_assign_rhs1 (def);
8967 code = MINUS_EXPR;
8968 goto do_minus;
8972 /* No sense saving up arithmetic to be done
8973 if it's all in the wrong mode to form part of an address.
8974 And force_operand won't know whether to sign-extend or
8975 zero-extend. */
8976 if (modifier != EXPAND_INITIALIZER
8977 && (modifier != EXPAND_SUM || mode != ptr_mode))
8979 expand_operands (treeop0, treeop1,
8980 subtarget, &op0, &op1, modifier);
8981 if (op0 == const0_rtx)
8982 return op1;
8983 if (op1 == const0_rtx)
8984 return op0;
8985 goto binop2;
8988 expand_operands (treeop0, treeop1,
8989 subtarget, &op0, &op1, modifier);
8990 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8992 case MINUS_EXPR:
8993 case POINTER_DIFF_EXPR:
8994 do_minus:
8995 /* For initializers, we are allowed to return a MINUS of two
8996 symbolic constants. Here we handle all cases when both operands
8997 are constant. */
8998 /* Handle difference of two symbolic constants,
8999 for the sake of an initializer. */
9000 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9001 && really_constant_p (treeop0)
9002 && really_constant_p (treeop1))
9004 expand_operands (treeop0, treeop1,
9005 NULL_RTX, &op0, &op1, modifier);
9006 return simplify_gen_binary (MINUS, mode, op0, op1);
9009 /* No sense saving up arithmetic to be done
9010 if it's all in the wrong mode to form part of an address.
9011 And force_operand won't know whether to sign-extend or
9012 zero-extend. */
9013 if (modifier != EXPAND_INITIALIZER
9014 && (modifier != EXPAND_SUM || mode != ptr_mode))
9015 goto binop;
9017 expand_operands (treeop0, treeop1,
9018 subtarget, &op0, &op1, modifier);
9020 /* Convert A - const to A + (-const). */
9021 if (CONST_INT_P (op1))
9023 op1 = negate_rtx (mode, op1);
9024 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9027 goto binop2;
9029 case WIDEN_MULT_PLUS_EXPR:
9030 case WIDEN_MULT_MINUS_EXPR:
9031 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9032 op2 = expand_normal (treeop2);
9033 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9034 target, unsignedp);
9035 return target;
9037 case WIDEN_MULT_EXPR:
9038 /* If first operand is constant, swap them.
9039 Thus the following special case checks need only
9040 check the second operand. */
9041 if (TREE_CODE (treeop0) == INTEGER_CST)
9042 std::swap (treeop0, treeop1);
9044 /* First, check if we have a multiplication of one signed and one
9045 unsigned operand. */
9046 if (TREE_CODE (treeop1) != INTEGER_CST
9047 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9048 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9050 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9051 this_optab = usmul_widen_optab;
9052 if (find_widening_optab_handler (this_optab, mode, innermode)
9053 != CODE_FOR_nothing)
9055 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9056 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9057 EXPAND_NORMAL);
9058 else
9059 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9060 EXPAND_NORMAL);
9061 /* op0 and op1 might still be constant, despite the above
9062 != INTEGER_CST check. Handle it. */
9063 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9065 op0 = convert_modes (mode, innermode, op0, true);
9066 op1 = convert_modes (mode, innermode, op1, false);
9067 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9068 target, unsignedp));
9070 goto binop3;
9073 /* Check for a multiplication with matching signedness. */
9074 else if ((TREE_CODE (treeop1) == INTEGER_CST
9075 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9076 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9077 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9079 tree op0type = TREE_TYPE (treeop0);
9080 machine_mode innermode = TYPE_MODE (op0type);
9081 bool zextend_p = TYPE_UNSIGNED (op0type);
9082 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9083 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9085 if (TREE_CODE (treeop0) != INTEGER_CST)
9087 if (find_widening_optab_handler (this_optab, mode, innermode)
9088 != CODE_FOR_nothing)
9090 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9091 EXPAND_NORMAL);
9092 /* op0 and op1 might still be constant, despite the above
9093 != INTEGER_CST check. Handle it. */
9094 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9096 widen_mult_const:
9097 op0 = convert_modes (mode, innermode, op0, zextend_p);
9099 = convert_modes (mode, innermode, op1,
9100 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9101 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9102 target,
9103 unsignedp));
9105 temp = expand_widening_mult (mode, op0, op1, target,
9106 unsignedp, this_optab);
9107 return REDUCE_BIT_FIELD (temp);
9109 if (find_widening_optab_handler (other_optab, mode, innermode)
9110 != CODE_FOR_nothing
9111 && innermode == word_mode)
9113 rtx htem, hipart;
9114 op0 = expand_normal (treeop0);
9115 op1 = expand_normal (treeop1);
9116 /* op0 and op1 might be constants, despite the above
9117 != INTEGER_CST check. Handle it. */
9118 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9119 goto widen_mult_const;
9120 temp = expand_binop (mode, other_optab, op0, op1, target,
9121 unsignedp, OPTAB_LIB_WIDEN);
9122 hipart = gen_highpart (word_mode, temp);
9123 htem = expand_mult_highpart_adjust (word_mode, hipart,
9124 op0, op1, hipart,
9125 zextend_p);
9126 if (htem != hipart)
9127 emit_move_insn (hipart, htem);
9128 return REDUCE_BIT_FIELD (temp);
9132 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9133 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9134 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9135 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9137 case MULT_EXPR:
9138 /* If this is a fixed-point operation, then we cannot use the code
9139 below because "expand_mult" doesn't support sat/no-sat fixed-point
9140 multiplications. */
9141 if (ALL_FIXED_POINT_MODE_P (mode))
9142 goto binop;
9144 /* If first operand is constant, swap them.
9145 Thus the following special case checks need only
9146 check the second operand. */
9147 if (TREE_CODE (treeop0) == INTEGER_CST)
9148 std::swap (treeop0, treeop1);
9150 /* Attempt to return something suitable for generating an
9151 indexed address, for machines that support that. */
9153 if (modifier == EXPAND_SUM && mode == ptr_mode
9154 && tree_fits_shwi_p (treeop1))
9156 tree exp1 = treeop1;
9158 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9159 EXPAND_SUM);
9161 if (!REG_P (op0))
9162 op0 = force_operand (op0, NULL_RTX);
9163 if (!REG_P (op0))
9164 op0 = copy_to_mode_reg (mode, op0);
9166 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
9167 gen_int_mode (tree_to_shwi (exp1),
9168 TYPE_MODE (TREE_TYPE (exp1)))));
9171 if (modifier == EXPAND_STACK_PARM)
9172 target = 0;
9174 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9175 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9177 case TRUNC_MOD_EXPR:
9178 case FLOOR_MOD_EXPR:
9179 case CEIL_MOD_EXPR:
9180 case ROUND_MOD_EXPR:
9182 case TRUNC_DIV_EXPR:
9183 case FLOOR_DIV_EXPR:
9184 case CEIL_DIV_EXPR:
9185 case ROUND_DIV_EXPR:
9186 case EXACT_DIV_EXPR:
9188 /* If this is a fixed-point operation, then we cannot use the code
9189 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9190 divisions. */
9191 if (ALL_FIXED_POINT_MODE_P (mode))
9192 goto binop;
9194 if (modifier == EXPAND_STACK_PARM)
9195 target = 0;
9196 /* Possible optimization: compute the dividend with EXPAND_SUM
9197 then if the divisor is constant can optimize the case
9198 where some terms of the dividend have coeffs divisible by it. */
9199 expand_operands (treeop0, treeop1,
9200 subtarget, &op0, &op1, EXPAND_NORMAL);
9201 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9202 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
9203 if (SCALAR_INT_MODE_P (mode)
9204 && optimize >= 2
9205 && get_range_pos_neg (treeop0) == 1
9206 && get_range_pos_neg (treeop1) == 1)
9208 /* If both arguments are known to be positive when interpreted
9209 as signed, we can expand it as both signed and unsigned
9210 division or modulo. Choose the cheaper sequence in that case. */
9211 bool speed_p = optimize_insn_for_speed_p ();
9212 do_pending_stack_adjust ();
9213 start_sequence ();
9214 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9215 rtx_insn *uns_insns = get_insns ();
9216 end_sequence ();
9217 start_sequence ();
9218 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9219 rtx_insn *sgn_insns = get_insns ();
9220 end_sequence ();
9221 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9222 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9224 /* If costs are the same then use as tie breaker the other
9225 other factor. */
9226 if (uns_cost == sgn_cost)
9228 uns_cost = seq_cost (uns_insns, !speed_p);
9229 sgn_cost = seq_cost (sgn_insns, !speed_p);
9232 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9234 emit_insn (uns_insns);
9235 return uns_ret;
9237 emit_insn (sgn_insns);
9238 return sgn_ret;
9240 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9242 case RDIV_EXPR:
9243 goto binop;
9245 case MULT_HIGHPART_EXPR:
9246 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9247 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9248 gcc_assert (temp);
9249 return temp;
9251 case FIXED_CONVERT_EXPR:
9252 op0 = expand_normal (treeop0);
9253 if (target == 0 || modifier == EXPAND_STACK_PARM)
9254 target = gen_reg_rtx (mode);
9256 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9257 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9258 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9259 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9260 else
9261 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9262 return target;
9264 case FIX_TRUNC_EXPR:
9265 op0 = expand_normal (treeop0);
9266 if (target == 0 || modifier == EXPAND_STACK_PARM)
9267 target = gen_reg_rtx (mode);
9268 expand_fix (target, op0, unsignedp);
9269 return target;
9271 case FLOAT_EXPR:
9272 op0 = expand_normal (treeop0);
9273 if (target == 0 || modifier == EXPAND_STACK_PARM)
9274 target = gen_reg_rtx (mode);
9275 /* expand_float can't figure out what to do if FROM has VOIDmode.
9276 So give it the correct mode. With -O, cse will optimize this. */
9277 if (GET_MODE (op0) == VOIDmode)
9278 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9279 op0);
9280 expand_float (target, op0,
9281 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9282 return target;
9284 case NEGATE_EXPR:
9285 op0 = expand_expr (treeop0, subtarget,
9286 VOIDmode, EXPAND_NORMAL);
9287 if (modifier == EXPAND_STACK_PARM)
9288 target = 0;
9289 temp = expand_unop (mode,
9290 optab_for_tree_code (NEGATE_EXPR, type,
9291 optab_default),
9292 op0, target, 0);
9293 gcc_assert (temp);
9294 return REDUCE_BIT_FIELD (temp);
9296 case ABS_EXPR:
9297 case ABSU_EXPR:
9298 op0 = expand_expr (treeop0, subtarget,
9299 VOIDmode, EXPAND_NORMAL);
9300 if (modifier == EXPAND_STACK_PARM)
9301 target = 0;
9303 /* ABS_EXPR is not valid for complex arguments. */
9304 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9305 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9307 /* Unsigned abs is simply the operand. Testing here means we don't
9308 risk generating incorrect code below. */
9309 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9310 return op0;
9312 return expand_abs (mode, op0, target, unsignedp,
9313 safe_from_p (target, treeop0, 1));
9315 case MAX_EXPR:
9316 case MIN_EXPR:
9317 target = original_target;
9318 if (target == 0
9319 || modifier == EXPAND_STACK_PARM
9320 || (MEM_P (target) && MEM_VOLATILE_P (target))
9321 || GET_MODE (target) != mode
9322 || (REG_P (target)
9323 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9324 target = gen_reg_rtx (mode);
9325 expand_operands (treeop0, treeop1,
9326 target, &op0, &op1, EXPAND_NORMAL);
9328 /* First try to do it with a special MIN or MAX instruction.
9329 If that does not win, use a conditional jump to select the proper
9330 value. */
9331 this_optab = optab_for_tree_code (code, type, optab_default);
9332 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9333 OPTAB_WIDEN);
9334 if (temp != 0)
9335 return temp;
9337 if (VECTOR_TYPE_P (type))
9338 gcc_unreachable ();
9340 /* At this point, a MEM target is no longer useful; we will get better
9341 code without it. */
9343 if (! REG_P (target))
9344 target = gen_reg_rtx (mode);
9346 /* If op1 was placed in target, swap op0 and op1. */
9347 if (target != op0 && target == op1)
9348 std::swap (op0, op1);
9350 /* We generate better code and avoid problems with op1 mentioning
9351 target by forcing op1 into a pseudo if it isn't a constant. */
9352 if (! CONSTANT_P (op1))
9353 op1 = force_reg (mode, op1);
9356 enum rtx_code comparison_code;
9357 rtx cmpop1 = op1;
9359 if (code == MAX_EXPR)
9360 comparison_code = unsignedp ? GEU : GE;
9361 else
9362 comparison_code = unsignedp ? LEU : LE;
9364 /* Canonicalize to comparisons against 0. */
9365 if (op1 == const1_rtx)
9367 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9368 or (a != 0 ? a : 1) for unsigned.
9369 For MIN we are safe converting (a <= 1 ? a : 1)
9370 into (a <= 0 ? a : 1) */
9371 cmpop1 = const0_rtx;
9372 if (code == MAX_EXPR)
9373 comparison_code = unsignedp ? NE : GT;
9375 if (op1 == constm1_rtx && !unsignedp)
9377 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9378 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9379 cmpop1 = const0_rtx;
9380 if (code == MIN_EXPR)
9381 comparison_code = LT;
9384 /* Use a conditional move if possible. */
9385 if (can_conditionally_move_p (mode))
9387 rtx insn;
9389 start_sequence ();
9391 /* Try to emit the conditional move. */
9392 insn = emit_conditional_move (target, comparison_code,
9393 op0, cmpop1, mode,
9394 op0, op1, mode,
9395 unsignedp);
9397 /* If we could do the conditional move, emit the sequence,
9398 and return. */
9399 if (insn)
9401 rtx_insn *seq = get_insns ();
9402 end_sequence ();
9403 emit_insn (seq);
9404 return target;
9407 /* Otherwise discard the sequence and fall back to code with
9408 branches. */
9409 end_sequence ();
9412 if (target != op0)
9413 emit_move_insn (target, op0);
9415 lab = gen_label_rtx ();
9416 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9417 unsignedp, mode, NULL_RTX, NULL, lab,
9418 profile_probability::uninitialized ());
9420 emit_move_insn (target, op1);
9421 emit_label (lab);
9422 return target;
9424 case BIT_NOT_EXPR:
9425 op0 = expand_expr (treeop0, subtarget,
9426 VOIDmode, EXPAND_NORMAL);
9427 if (modifier == EXPAND_STACK_PARM)
9428 target = 0;
9429 /* In case we have to reduce the result to bitfield precision
9430 for unsigned bitfield expand this as XOR with a proper constant
9431 instead. */
9432 if (reduce_bit_field && TYPE_UNSIGNED (type))
9434 int_mode = SCALAR_INT_TYPE_MODE (type);
9435 wide_int mask = wi::mask (TYPE_PRECISION (type),
9436 false, GET_MODE_PRECISION (int_mode));
9438 temp = expand_binop (int_mode, xor_optab, op0,
9439 immed_wide_int_const (mask, int_mode),
9440 target, 1, OPTAB_LIB_WIDEN);
9442 else
9443 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9444 gcc_assert (temp);
9445 return temp;
9447 /* ??? Can optimize bitwise operations with one arg constant.
9448 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9449 and (a bitwise1 b) bitwise2 b (etc)
9450 but that is probably not worth while. */
9452 case BIT_AND_EXPR:
9453 case BIT_IOR_EXPR:
9454 case BIT_XOR_EXPR:
9455 goto binop;
9457 case LROTATE_EXPR:
9458 case RROTATE_EXPR:
9459 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9460 || type_has_mode_precision_p (type));
9461 /* fall through */
9463 case LSHIFT_EXPR:
9464 case RSHIFT_EXPR:
9466 /* If this is a fixed-point operation, then we cannot use the code
9467 below because "expand_shift" doesn't support sat/no-sat fixed-point
9468 shifts. */
9469 if (ALL_FIXED_POINT_MODE_P (mode))
9470 goto binop;
9472 if (! safe_from_p (subtarget, treeop1, 1))
9473 subtarget = 0;
9474 if (modifier == EXPAND_STACK_PARM)
9475 target = 0;
9476 op0 = expand_expr (treeop0, subtarget,
9477 VOIDmode, EXPAND_NORMAL);
9479 /* Left shift optimization when shifting across word_size boundary.
9481 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9482 there isn't native instruction to support this wide mode
9483 left shift. Given below scenario:
9485 Type A = (Type) B << C
9487 |< T >|
9488 | dest_high | dest_low |
9490 | word_size |
9492 If the shift amount C caused we shift B to across the word
9493 size boundary, i.e part of B shifted into high half of
9494 destination register, and part of B remains in the low
9495 half, then GCC will use the following left shift expand
9496 logic:
9498 1. Initialize dest_low to B.
9499 2. Initialize every bit of dest_high to the sign bit of B.
9500 3. Logic left shift dest_low by C bit to finalize dest_low.
9501 The value of dest_low before this shift is kept in a temp D.
9502 4. Logic left shift dest_high by C.
9503 5. Logic right shift D by (word_size - C).
9504 6. Or the result of 4 and 5 to finalize dest_high.
9506 While, by checking gimple statements, if operand B is
9507 coming from signed extension, then we can simplify above
9508 expand logic into:
9510 1. dest_high = src_low >> (word_size - C).
9511 2. dest_low = src_low << C.
9513 We can use one arithmetic right shift to finish all the
9514 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9515 needed from 6 into 2.
9517 The case is similar for zero extension, except that we
9518 initialize dest_high to zero rather than copies of the sign
9519 bit from B. Furthermore, we need to use a logical right shift
9520 in this case.
9522 The choice of sign-extension versus zero-extension is
9523 determined entirely by whether or not B is signed and is
9524 independent of the current setting of unsignedp. */
9526 temp = NULL_RTX;
9527 if (code == LSHIFT_EXPR
9528 && target
9529 && REG_P (target)
9530 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9531 && mode == int_mode
9532 && TREE_CONSTANT (treeop1)
9533 && TREE_CODE (treeop0) == SSA_NAME)
9535 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9536 if (is_gimple_assign (def)
9537 && gimple_assign_rhs_code (def) == NOP_EXPR)
9539 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9540 (TREE_TYPE (gimple_assign_rhs1 (def)));
9542 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9543 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9544 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9545 >= GET_MODE_BITSIZE (word_mode)))
9547 rtx_insn *seq, *seq_old;
9548 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9549 int_mode);
9550 bool extend_unsigned
9551 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9552 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9553 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9554 rtx dest_high = simplify_gen_subreg (word_mode, target,
9555 int_mode, high_off);
9556 HOST_WIDE_INT ramount = (BITS_PER_WORD
9557 - TREE_INT_CST_LOW (treeop1));
9558 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9560 start_sequence ();
9561 /* dest_high = src_low >> (word_size - C). */
9562 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9563 rshift, dest_high,
9564 extend_unsigned);
9565 if (temp != dest_high)
9566 emit_move_insn (dest_high, temp);
9568 /* dest_low = src_low << C. */
9569 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9570 treeop1, dest_low, unsignedp);
9571 if (temp != dest_low)
9572 emit_move_insn (dest_low, temp);
9574 seq = get_insns ();
9575 end_sequence ();
9576 temp = target ;
9578 if (have_insn_for (ASHIFT, int_mode))
9580 bool speed_p = optimize_insn_for_speed_p ();
9581 start_sequence ();
9582 rtx ret_old = expand_variable_shift (code, int_mode,
9583 op0, treeop1,
9584 target,
9585 unsignedp);
9587 seq_old = get_insns ();
9588 end_sequence ();
9589 if (seq_cost (seq, speed_p)
9590 >= seq_cost (seq_old, speed_p))
9592 seq = seq_old;
9593 temp = ret_old;
9596 emit_insn (seq);
9601 if (temp == NULL_RTX)
9602 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9603 unsignedp);
9604 if (code == LSHIFT_EXPR)
9605 temp = REDUCE_BIT_FIELD (temp);
9606 return temp;
9609 /* Could determine the answer when only additive constants differ. Also,
9610 the addition of one can be handled by changing the condition. */
9611 case LT_EXPR:
9612 case LE_EXPR:
9613 case GT_EXPR:
9614 case GE_EXPR:
9615 case EQ_EXPR:
9616 case NE_EXPR:
9617 case UNORDERED_EXPR:
9618 case ORDERED_EXPR:
9619 case UNLT_EXPR:
9620 case UNLE_EXPR:
9621 case UNGT_EXPR:
9622 case UNGE_EXPR:
9623 case UNEQ_EXPR:
9624 case LTGT_EXPR:
9626 temp = do_store_flag (ops,
9627 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9628 tmode != VOIDmode ? tmode : mode);
9629 if (temp)
9630 return temp;
9632 /* Use a compare and a jump for BLKmode comparisons, or for function
9633 type comparisons is have_canonicalize_funcptr_for_compare. */
9635 if ((target == 0
9636 || modifier == EXPAND_STACK_PARM
9637 || ! safe_from_p (target, treeop0, 1)
9638 || ! safe_from_p (target, treeop1, 1)
9639 /* Make sure we don't have a hard reg (such as function's return
9640 value) live across basic blocks, if not optimizing. */
9641 || (!optimize && REG_P (target)
9642 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9643 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9645 emit_move_insn (target, const0_rtx);
9647 rtx_code_label *lab1 = gen_label_rtx ();
9648 jumpifnot_1 (code, treeop0, treeop1, lab1,
9649 profile_probability::uninitialized ());
9651 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9652 emit_move_insn (target, constm1_rtx);
9653 else
9654 emit_move_insn (target, const1_rtx);
9656 emit_label (lab1);
9657 return target;
9659 case COMPLEX_EXPR:
9660 /* Get the rtx code of the operands. */
9661 op0 = expand_normal (treeop0);
9662 op1 = expand_normal (treeop1);
9664 if (!target)
9665 target = gen_reg_rtx (TYPE_MODE (type));
9666 else
9667 /* If target overlaps with op1, then either we need to force
9668 op1 into a pseudo (if target also overlaps with op0),
9669 or write the complex parts in reverse order. */
9670 switch (GET_CODE (target))
9672 case CONCAT:
9673 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9675 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9677 complex_expr_force_op1:
9678 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9679 emit_move_insn (temp, op1);
9680 op1 = temp;
9681 break;
9683 complex_expr_swap_order:
9684 /* Move the imaginary (op1) and real (op0) parts to their
9685 location. */
9686 write_complex_part (target, op1, true);
9687 write_complex_part (target, op0, false);
9689 return target;
9691 break;
9692 case MEM:
9693 temp = adjust_address_nv (target,
9694 GET_MODE_INNER (GET_MODE (target)), 0);
9695 if (reg_overlap_mentioned_p (temp, op1))
9697 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9698 temp = adjust_address_nv (target, imode,
9699 GET_MODE_SIZE (imode));
9700 if (reg_overlap_mentioned_p (temp, op0))
9701 goto complex_expr_force_op1;
9702 goto complex_expr_swap_order;
9704 break;
9705 default:
9706 if (reg_overlap_mentioned_p (target, op1))
9708 if (reg_overlap_mentioned_p (target, op0))
9709 goto complex_expr_force_op1;
9710 goto complex_expr_swap_order;
9712 break;
9715 /* Move the real (op0) and imaginary (op1) parts to their location. */
9716 write_complex_part (target, op0, false);
9717 write_complex_part (target, op1, true);
9719 return target;
9721 case WIDEN_SUM_EXPR:
9723 tree oprnd0 = treeop0;
9724 tree oprnd1 = treeop1;
9726 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9727 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9728 target, unsignedp);
9729 return target;
9732 case VEC_UNPACK_HI_EXPR:
9733 case VEC_UNPACK_LO_EXPR:
9734 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
9735 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
9737 op0 = expand_normal (treeop0);
9738 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9739 target, unsignedp);
9740 gcc_assert (temp);
9741 return temp;
9744 case VEC_UNPACK_FLOAT_HI_EXPR:
9745 case VEC_UNPACK_FLOAT_LO_EXPR:
9747 op0 = expand_normal (treeop0);
9748 /* The signedness is determined from input operand. */
9749 temp = expand_widen_pattern_expr
9750 (ops, op0, NULL_RTX, NULL_RTX,
9751 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9753 gcc_assert (temp);
9754 return temp;
9757 case VEC_WIDEN_MULT_HI_EXPR:
9758 case VEC_WIDEN_MULT_LO_EXPR:
9759 case VEC_WIDEN_MULT_EVEN_EXPR:
9760 case VEC_WIDEN_MULT_ODD_EXPR:
9761 case VEC_WIDEN_LSHIFT_HI_EXPR:
9762 case VEC_WIDEN_LSHIFT_LO_EXPR:
9763 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9764 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9765 target, unsignedp);
9766 gcc_assert (target);
9767 return target;
9769 case VEC_PACK_SAT_EXPR:
9770 case VEC_PACK_FIX_TRUNC_EXPR:
9771 mode = TYPE_MODE (TREE_TYPE (treeop0));
9772 goto binop;
9774 case VEC_PACK_TRUNC_EXPR:
9775 if (VECTOR_BOOLEAN_TYPE_P (type)
9776 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
9777 && mode == TYPE_MODE (TREE_TYPE (treeop0))
9778 && SCALAR_INT_MODE_P (mode))
9780 class expand_operand eops[4];
9781 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
9782 expand_operands (treeop0, treeop1,
9783 subtarget, &op0, &op1, EXPAND_NORMAL);
9784 this_optab = vec_pack_sbool_trunc_optab;
9785 enum insn_code icode = optab_handler (this_optab, imode);
9786 create_output_operand (&eops[0], target, mode);
9787 create_convert_operand_from (&eops[1], op0, imode, false);
9788 create_convert_operand_from (&eops[2], op1, imode, false);
9789 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
9790 create_input_operand (&eops[3], temp, imode);
9791 expand_insn (icode, 4, eops);
9792 return eops[0].value;
9794 mode = TYPE_MODE (TREE_TYPE (treeop0));
9795 goto binop;
9797 case VEC_PACK_FLOAT_EXPR:
9798 mode = TYPE_MODE (TREE_TYPE (treeop0));
9799 expand_operands (treeop0, treeop1,
9800 subtarget, &op0, &op1, EXPAND_NORMAL);
9801 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
9802 optab_default);
9803 target = expand_binop (mode, this_optab, op0, op1, target,
9804 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
9805 OPTAB_LIB_WIDEN);
9806 gcc_assert (target);
9807 return target;
9809 case VEC_PERM_EXPR:
9811 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9812 vec_perm_builder sel;
9813 if (TREE_CODE (treeop2) == VECTOR_CST
9814 && tree_to_vec_perm_builder (&sel, treeop2))
9816 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
9817 temp = expand_vec_perm_const (mode, op0, op1, sel,
9818 sel_mode, target);
9820 else
9822 op2 = expand_normal (treeop2);
9823 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
9825 gcc_assert (temp);
9826 return temp;
9829 case DOT_PROD_EXPR:
9831 tree oprnd0 = treeop0;
9832 tree oprnd1 = treeop1;
9833 tree oprnd2 = treeop2;
9835 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9836 op2 = expand_normal (oprnd2);
9837 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9838 target, unsignedp);
9839 return target;
9842 case SAD_EXPR:
9844 tree oprnd0 = treeop0;
9845 tree oprnd1 = treeop1;
9846 tree oprnd2 = treeop2;
9848 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9849 op2 = expand_normal (oprnd2);
9850 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9851 target, unsignedp);
9852 return target;
9855 case REALIGN_LOAD_EXPR:
9857 tree oprnd0 = treeop0;
9858 tree oprnd1 = treeop1;
9859 tree oprnd2 = treeop2;
9861 this_optab = optab_for_tree_code (code, type, optab_default);
9862 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9863 op2 = expand_normal (oprnd2);
9864 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9865 target, unsignedp);
9866 gcc_assert (temp);
9867 return temp;
9870 case COND_EXPR:
9872 /* A COND_EXPR with its type being VOID_TYPE represents a
9873 conditional jump and is handled in
9874 expand_gimple_cond_expr. */
9875 gcc_assert (!VOID_TYPE_P (type));
9877 /* Note that COND_EXPRs whose type is a structure or union
9878 are required to be constructed to contain assignments of
9879 a temporary variable, so that we can evaluate them here
9880 for side effect only. If type is void, we must do likewise. */
9882 gcc_assert (!TREE_ADDRESSABLE (type)
9883 && !ignore
9884 && TREE_TYPE (treeop1) != void_type_node
9885 && TREE_TYPE (treeop2) != void_type_node);
9887 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9888 if (temp)
9889 return temp;
9891 /* If we are not to produce a result, we have no target. Otherwise,
9892 if a target was specified use it; it will not be used as an
9893 intermediate target unless it is safe. If no target, use a
9894 temporary. */
9896 if (modifier != EXPAND_STACK_PARM
9897 && original_target
9898 && safe_from_p (original_target, treeop0, 1)
9899 && GET_MODE (original_target) == mode
9900 && !MEM_P (original_target))
9901 temp = original_target;
9902 else
9903 temp = assign_temp (type, 0, 1);
9905 do_pending_stack_adjust ();
9906 NO_DEFER_POP;
9907 rtx_code_label *lab0 = gen_label_rtx ();
9908 rtx_code_label *lab1 = gen_label_rtx ();
9909 jumpifnot (treeop0, lab0,
9910 profile_probability::uninitialized ());
9911 store_expr (treeop1, temp,
9912 modifier == EXPAND_STACK_PARM,
9913 false, false);
9915 emit_jump_insn (targetm.gen_jump (lab1));
9916 emit_barrier ();
9917 emit_label (lab0);
9918 store_expr (treeop2, temp,
9919 modifier == EXPAND_STACK_PARM,
9920 false, false);
9922 emit_label (lab1);
9923 OK_DEFER_POP;
9924 return temp;
9927 case VEC_DUPLICATE_EXPR:
9928 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9929 target = expand_vector_broadcast (mode, op0);
9930 gcc_assert (target);
9931 return target;
9933 case VEC_SERIES_EXPR:
9934 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
9935 return expand_vec_series_expr (mode, op0, op1, target);
9937 case BIT_INSERT_EXPR:
9939 unsigned bitpos = tree_to_uhwi (treeop2);
9940 unsigned bitsize;
9941 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9942 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9943 else
9944 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9945 op0 = expand_normal (treeop0);
9946 op1 = expand_normal (treeop1);
9947 rtx dst = gen_reg_rtx (mode);
9948 emit_move_insn (dst, op0);
9949 store_bit_field (dst, bitsize, bitpos, 0, 0,
9950 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9951 return dst;
9954 default:
9955 gcc_unreachable ();
9958 /* Here to do an ordinary binary operator. */
9959 binop:
9960 expand_operands (treeop0, treeop1,
9961 subtarget, &op0, &op1, EXPAND_NORMAL);
9962 binop2:
9963 this_optab = optab_for_tree_code (code, type, optab_default);
9964 binop3:
9965 if (modifier == EXPAND_STACK_PARM)
9966 target = 0;
9967 temp = expand_binop (mode, this_optab, op0, op1, target,
9968 unsignedp, OPTAB_LIB_WIDEN);
9969 gcc_assert (temp);
9970 /* Bitwise operations do not need bitfield reduction as we expect their
9971 operands being properly truncated. */
9972 if (code == BIT_XOR_EXPR
9973 || code == BIT_AND_EXPR
9974 || code == BIT_IOR_EXPR)
9975 return temp;
9976 return REDUCE_BIT_FIELD (temp);
9978 #undef REDUCE_BIT_FIELD
9981 /* Return TRUE if expression STMT is suitable for replacement.
9982 Never consider memory loads as replaceable, because those don't ever lead
9983 into constant expressions. */
9985 static bool
9986 stmt_is_replaceable_p (gimple *stmt)
9988 if (ssa_is_replaceable_p (stmt))
9990 /* Don't move around loads. */
9991 if (!gimple_assign_single_p (stmt)
9992 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9993 return true;
9995 return false;
9999 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10000 enum expand_modifier modifier, rtx *alt_rtl,
10001 bool inner_reference_p)
10003 rtx op0, op1, temp, decl_rtl;
10004 tree type;
10005 int unsignedp;
10006 machine_mode mode, dmode;
10007 enum tree_code code = TREE_CODE (exp);
10008 rtx subtarget, original_target;
10009 int ignore;
10010 tree context;
10011 bool reduce_bit_field;
10012 location_t loc = EXPR_LOCATION (exp);
10013 struct separate_ops ops;
10014 tree treeop0, treeop1, treeop2;
10015 tree ssa_name = NULL_TREE;
10016 gimple *g;
10018 type = TREE_TYPE (exp);
10019 mode = TYPE_MODE (type);
10020 unsignedp = TYPE_UNSIGNED (type);
10022 treeop0 = treeop1 = treeop2 = NULL_TREE;
10023 if (!VL_EXP_CLASS_P (exp))
10024 switch (TREE_CODE_LENGTH (code))
10026 default:
10027 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10028 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10029 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10030 case 0: break;
10032 ops.code = code;
10033 ops.type = type;
10034 ops.op0 = treeop0;
10035 ops.op1 = treeop1;
10036 ops.op2 = treeop2;
10037 ops.location = loc;
10039 ignore = (target == const0_rtx
10040 || ((CONVERT_EXPR_CODE_P (code)
10041 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10042 && TREE_CODE (type) == VOID_TYPE));
10044 /* An operation in what may be a bit-field type needs the
10045 result to be reduced to the precision of the bit-field type,
10046 which is narrower than that of the type's mode. */
10047 reduce_bit_field = (!ignore
10048 && INTEGRAL_TYPE_P (type)
10049 && !type_has_mode_precision_p (type));
10051 /* If we are going to ignore this result, we need only do something
10052 if there is a side-effect somewhere in the expression. If there
10053 is, short-circuit the most common cases here. Note that we must
10054 not call expand_expr with anything but const0_rtx in case this
10055 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10057 if (ignore)
10059 if (! TREE_SIDE_EFFECTS (exp))
10060 return const0_rtx;
10062 /* Ensure we reference a volatile object even if value is ignored, but
10063 don't do this if all we are doing is taking its address. */
10064 if (TREE_THIS_VOLATILE (exp)
10065 && TREE_CODE (exp) != FUNCTION_DECL
10066 && mode != VOIDmode && mode != BLKmode
10067 && modifier != EXPAND_CONST_ADDRESS)
10069 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10070 if (MEM_P (temp))
10071 copy_to_reg (temp);
10072 return const0_rtx;
10075 if (TREE_CODE_CLASS (code) == tcc_unary
10076 || code == BIT_FIELD_REF
10077 || code == COMPONENT_REF
10078 || code == INDIRECT_REF)
10079 return expand_expr (treeop0, const0_rtx, VOIDmode,
10080 modifier);
10082 else if (TREE_CODE_CLASS (code) == tcc_binary
10083 || TREE_CODE_CLASS (code) == tcc_comparison
10084 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10086 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10087 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10088 return const0_rtx;
10091 target = 0;
10094 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10095 target = 0;
10097 /* Use subtarget as the target for operand 0 of a binary operation. */
10098 subtarget = get_subtarget (target);
10099 original_target = target;
10101 switch (code)
10103 case LABEL_DECL:
10105 tree function = decl_function_context (exp);
10107 temp = label_rtx (exp);
10108 temp = gen_rtx_LABEL_REF (Pmode, temp);
10110 if (function != current_function_decl
10111 && function != 0)
10112 LABEL_REF_NONLOCAL_P (temp) = 1;
10114 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10115 return temp;
10118 case SSA_NAME:
10119 /* ??? ivopts calls expander, without any preparation from
10120 out-of-ssa. So fake instructions as if this was an access to the
10121 base variable. This unnecessarily allocates a pseudo, see how we can
10122 reuse it, if partition base vars have it set already. */
10123 if (!currently_expanding_to_rtl)
10125 tree var = SSA_NAME_VAR (exp);
10126 if (var && DECL_RTL_SET_P (var))
10127 return DECL_RTL (var);
10128 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10129 LAST_VIRTUAL_REGISTER + 1);
10132 g = get_gimple_for_ssa_name (exp);
10133 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10134 if (g == NULL
10135 && modifier == EXPAND_INITIALIZER
10136 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10137 && (optimize || !SSA_NAME_VAR (exp)
10138 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10139 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10140 g = SSA_NAME_DEF_STMT (exp);
10141 if (g)
10143 rtx r;
10144 location_t saved_loc = curr_insn_location ();
10145 loc = gimple_location (g);
10146 if (loc != UNKNOWN_LOCATION)
10147 set_curr_insn_location (loc);
10148 ops.code = gimple_assign_rhs_code (g);
10149 switch (get_gimple_rhs_class (ops.code))
10151 case GIMPLE_TERNARY_RHS:
10152 ops.op2 = gimple_assign_rhs3 (g);
10153 /* Fallthru */
10154 case GIMPLE_BINARY_RHS:
10155 ops.op1 = gimple_assign_rhs2 (g);
10157 /* Try to expand conditonal compare. */
10158 if (targetm.gen_ccmp_first)
10160 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10161 r = expand_ccmp_expr (g, mode);
10162 if (r)
10163 break;
10165 /* Fallthru */
10166 case GIMPLE_UNARY_RHS:
10167 ops.op0 = gimple_assign_rhs1 (g);
10168 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10169 ops.location = loc;
10170 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10171 break;
10172 case GIMPLE_SINGLE_RHS:
10174 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10175 tmode, modifier, alt_rtl,
10176 inner_reference_p);
10177 break;
10179 default:
10180 gcc_unreachable ();
10182 set_curr_insn_location (saved_loc);
10183 if (REG_P (r) && !REG_EXPR (r))
10184 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10185 return r;
10188 ssa_name = exp;
10189 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10190 exp = SSA_NAME_VAR (ssa_name);
10191 goto expand_decl_rtl;
10193 case PARM_DECL:
10194 case VAR_DECL:
10195 /* If a static var's type was incomplete when the decl was written,
10196 but the type is complete now, lay out the decl now. */
10197 if (DECL_SIZE (exp) == 0
10198 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10199 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10200 layout_decl (exp, 0);
10202 /* fall through */
10204 case FUNCTION_DECL:
10205 case RESULT_DECL:
10206 decl_rtl = DECL_RTL (exp);
10207 expand_decl_rtl:
10208 gcc_assert (decl_rtl);
10210 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10211 settings for VECTOR_TYPE_P that might switch for the function. */
10212 if (currently_expanding_to_rtl
10213 && code == VAR_DECL && MEM_P (decl_rtl)
10214 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10215 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10216 else
10217 decl_rtl = copy_rtx (decl_rtl);
10219 /* Record writes to register variables. */
10220 if (modifier == EXPAND_WRITE
10221 && REG_P (decl_rtl)
10222 && HARD_REGISTER_P (decl_rtl))
10223 add_to_hard_reg_set (&crtl->asm_clobbers,
10224 GET_MODE (decl_rtl), REGNO (decl_rtl));
10226 /* Ensure variable marked as used even if it doesn't go through
10227 a parser. If it hasn't be used yet, write out an external
10228 definition. */
10229 if (exp)
10230 TREE_USED (exp) = 1;
10232 /* Show we haven't gotten RTL for this yet. */
10233 temp = 0;
10235 /* Variables inherited from containing functions should have
10236 been lowered by this point. */
10237 if (exp)
10238 context = decl_function_context (exp);
10239 gcc_assert (!exp
10240 || SCOPE_FILE_SCOPE_P (context)
10241 || context == current_function_decl
10242 || TREE_STATIC (exp)
10243 || DECL_EXTERNAL (exp)
10244 /* ??? C++ creates functions that are not TREE_STATIC. */
10245 || TREE_CODE (exp) == FUNCTION_DECL);
10247 /* This is the case of an array whose size is to be determined
10248 from its initializer, while the initializer is still being parsed.
10249 ??? We aren't parsing while expanding anymore. */
10251 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10252 temp = validize_mem (decl_rtl);
10254 /* If DECL_RTL is memory, we are in the normal case and the
10255 address is not valid, get the address into a register. */
10257 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10259 if (alt_rtl)
10260 *alt_rtl = decl_rtl;
10261 decl_rtl = use_anchored_address (decl_rtl);
10262 if (modifier != EXPAND_CONST_ADDRESS
10263 && modifier != EXPAND_SUM
10264 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10265 : GET_MODE (decl_rtl),
10266 XEXP (decl_rtl, 0),
10267 MEM_ADDR_SPACE (decl_rtl)))
10268 temp = replace_equiv_address (decl_rtl,
10269 copy_rtx (XEXP (decl_rtl, 0)));
10272 /* If we got something, return it. But first, set the alignment
10273 if the address is a register. */
10274 if (temp != 0)
10276 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10277 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10279 else if (MEM_P (decl_rtl))
10280 temp = decl_rtl;
10282 if (temp != 0)
10284 if (MEM_P (temp)
10285 && modifier != EXPAND_WRITE
10286 && modifier != EXPAND_MEMORY
10287 && modifier != EXPAND_INITIALIZER
10288 && modifier != EXPAND_CONST_ADDRESS
10289 && modifier != EXPAND_SUM
10290 && !inner_reference_p
10291 && mode != BLKmode
10292 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10293 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10294 MEM_ALIGN (temp), NULL_RTX, NULL);
10296 return temp;
10299 if (exp)
10300 dmode = DECL_MODE (exp);
10301 else
10302 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10304 /* If the mode of DECL_RTL does not match that of the decl,
10305 there are two cases: we are dealing with a BLKmode value
10306 that is returned in a register, or we are dealing with
10307 a promoted value. In the latter case, return a SUBREG
10308 of the wanted mode, but mark it so that we know that it
10309 was already extended. */
10310 if (REG_P (decl_rtl)
10311 && dmode != BLKmode
10312 && GET_MODE (decl_rtl) != dmode)
10314 machine_mode pmode;
10316 /* Get the signedness to be used for this variable. Ensure we get
10317 the same mode we got when the variable was declared. */
10318 if (code != SSA_NAME)
10319 pmode = promote_decl_mode (exp, &unsignedp);
10320 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10321 && gimple_code (g) == GIMPLE_CALL
10322 && !gimple_call_internal_p (g))
10323 pmode = promote_function_mode (type, mode, &unsignedp,
10324 gimple_call_fntype (g),
10326 else
10327 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10328 gcc_assert (GET_MODE (decl_rtl) == pmode);
10330 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10331 SUBREG_PROMOTED_VAR_P (temp) = 1;
10332 SUBREG_PROMOTED_SET (temp, unsignedp);
10333 return temp;
10336 return decl_rtl;
10338 case INTEGER_CST:
10340 /* Given that TYPE_PRECISION (type) is not always equal to
10341 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10342 the former to the latter according to the signedness of the
10343 type. */
10344 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
10345 temp = immed_wide_int_const
10346 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
10347 return temp;
10350 case VECTOR_CST:
10352 tree tmp = NULL_TREE;
10353 if (VECTOR_MODE_P (mode))
10354 return const_vector_from_tree (exp);
10355 scalar_int_mode int_mode;
10356 if (is_int_mode (mode, &int_mode))
10358 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
10359 if (type_for_mode)
10360 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10361 type_for_mode, exp);
10363 if (!tmp)
10365 vec<constructor_elt, va_gc> *v;
10366 /* Constructors need to be fixed-length. FIXME. */
10367 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10368 vec_alloc (v, nunits);
10369 for (unsigned int i = 0; i < nunits; ++i)
10370 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10371 tmp = build_constructor (type, v);
10373 return expand_expr (tmp, ignore ? const0_rtx : target,
10374 tmode, modifier);
10377 case CONST_DECL:
10378 if (modifier == EXPAND_WRITE)
10380 /* Writing into CONST_DECL is always invalid, but handle it
10381 gracefully. */
10382 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10383 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10384 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10385 EXPAND_NORMAL, as);
10386 op0 = memory_address_addr_space (mode, op0, as);
10387 temp = gen_rtx_MEM (mode, op0);
10388 set_mem_addr_space (temp, as);
10389 return temp;
10391 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10393 case REAL_CST:
10394 /* If optimized, generate immediate CONST_DOUBLE
10395 which will be turned into memory by reload if necessary.
10397 We used to force a register so that loop.c could see it. But
10398 this does not allow gen_* patterns to perform optimizations with
10399 the constants. It also produces two insns in cases like "x = 1.0;".
10400 On most machines, floating-point constants are not permitted in
10401 many insns, so we'd end up copying it to a register in any case.
10403 Now, we do the copying in expand_binop, if appropriate. */
10404 return const_double_from_real_value (TREE_REAL_CST (exp),
10405 TYPE_MODE (TREE_TYPE (exp)));
10407 case FIXED_CST:
10408 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10409 TYPE_MODE (TREE_TYPE (exp)));
10411 case COMPLEX_CST:
10412 /* Handle evaluating a complex constant in a CONCAT target. */
10413 if (original_target && GET_CODE (original_target) == CONCAT)
10415 rtx rtarg, itarg;
10417 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10418 rtarg = XEXP (original_target, 0);
10419 itarg = XEXP (original_target, 1);
10421 /* Move the real and imaginary parts separately. */
10422 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10423 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10425 if (op0 != rtarg)
10426 emit_move_insn (rtarg, op0);
10427 if (op1 != itarg)
10428 emit_move_insn (itarg, op1);
10430 return original_target;
10433 /* fall through */
10435 case STRING_CST:
10436 temp = expand_expr_constant (exp, 1, modifier);
10438 /* temp contains a constant address.
10439 On RISC machines where a constant address isn't valid,
10440 make some insns to get that address into a register. */
10441 if (modifier != EXPAND_CONST_ADDRESS
10442 && modifier != EXPAND_INITIALIZER
10443 && modifier != EXPAND_SUM
10444 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10445 MEM_ADDR_SPACE (temp)))
10446 return replace_equiv_address (temp,
10447 copy_rtx (XEXP (temp, 0)));
10448 return temp;
10450 case POLY_INT_CST:
10451 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10453 case SAVE_EXPR:
10455 tree val = treeop0;
10456 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10457 inner_reference_p);
10459 if (!SAVE_EXPR_RESOLVED_P (exp))
10461 /* We can indeed still hit this case, typically via builtin
10462 expanders calling save_expr immediately before expanding
10463 something. Assume this means that we only have to deal
10464 with non-BLKmode values. */
10465 gcc_assert (GET_MODE (ret) != BLKmode);
10467 val = build_decl (curr_insn_location (),
10468 VAR_DECL, NULL, TREE_TYPE (exp));
10469 DECL_ARTIFICIAL (val) = 1;
10470 DECL_IGNORED_P (val) = 1;
10471 treeop0 = val;
10472 TREE_OPERAND (exp, 0) = treeop0;
10473 SAVE_EXPR_RESOLVED_P (exp) = 1;
10475 if (!CONSTANT_P (ret))
10476 ret = copy_to_reg (ret);
10477 SET_DECL_RTL (val, ret);
10480 return ret;
10484 case CONSTRUCTOR:
10485 /* If we don't need the result, just ensure we evaluate any
10486 subexpressions. */
10487 if (ignore)
10489 unsigned HOST_WIDE_INT idx;
10490 tree value;
10492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10493 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10495 return const0_rtx;
10498 return expand_constructor (exp, target, modifier, false);
10500 case TARGET_MEM_REF:
10502 addr_space_t as
10503 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10504 unsigned int align;
10506 op0 = addr_for_mem_ref (exp, as, true);
10507 op0 = memory_address_addr_space (mode, op0, as);
10508 temp = gen_rtx_MEM (mode, op0);
10509 set_mem_attributes (temp, exp, 0);
10510 set_mem_addr_space (temp, as);
10511 align = get_object_alignment (exp);
10512 if (modifier != EXPAND_WRITE
10513 && modifier != EXPAND_MEMORY
10514 && mode != BLKmode
10515 && align < GET_MODE_ALIGNMENT (mode))
10516 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10517 align, NULL_RTX, NULL);
10518 return temp;
10521 case MEM_REF:
10523 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10524 addr_space_t as
10525 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10526 machine_mode address_mode;
10527 tree base = TREE_OPERAND (exp, 0);
10528 gimple *def_stmt;
10529 unsigned align;
10530 /* Handle expansion of non-aliased memory with non-BLKmode. That
10531 might end up in a register. */
10532 if (mem_ref_refers_to_non_mem_p (exp))
10534 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10535 base = TREE_OPERAND (base, 0);
10536 poly_uint64 type_size;
10537 if (known_eq (offset, 0)
10538 && !reverse
10539 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10540 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10541 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10542 target, tmode, modifier);
10543 if (TYPE_MODE (type) == BLKmode)
10545 temp = assign_stack_temp (DECL_MODE (base),
10546 GET_MODE_SIZE (DECL_MODE (base)));
10547 store_expr (base, temp, 0, false, false);
10548 temp = adjust_address (temp, BLKmode, offset);
10549 set_mem_size (temp, int_size_in_bytes (type));
10550 return temp;
10552 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10553 bitsize_int (offset * BITS_PER_UNIT));
10554 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10555 return expand_expr (exp, target, tmode, modifier);
10557 address_mode = targetm.addr_space.address_mode (as);
10558 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10560 tree mask = gimple_assign_rhs2 (def_stmt);
10561 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10562 gimple_assign_rhs1 (def_stmt), mask);
10563 TREE_OPERAND (exp, 0) = base;
10565 align = get_object_alignment (exp);
10566 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10567 op0 = memory_address_addr_space (mode, op0, as);
10568 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10570 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10571 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10572 op0 = memory_address_addr_space (mode, op0, as);
10574 temp = gen_rtx_MEM (mode, op0);
10575 set_mem_attributes (temp, exp, 0);
10576 set_mem_addr_space (temp, as);
10577 if (TREE_THIS_VOLATILE (exp))
10578 MEM_VOLATILE_P (temp) = 1;
10579 if (modifier != EXPAND_WRITE
10580 && modifier != EXPAND_MEMORY
10581 && !inner_reference_p
10582 && mode != BLKmode
10583 && align < GET_MODE_ALIGNMENT (mode))
10584 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
10585 modifier == EXPAND_STACK_PARM
10586 ? NULL_RTX : target, alt_rtl);
10587 if (reverse
10588 && modifier != EXPAND_MEMORY
10589 && modifier != EXPAND_WRITE)
10590 temp = flip_storage_order (mode, temp);
10591 return temp;
10594 case ARRAY_REF:
10597 tree array = treeop0;
10598 tree index = treeop1;
10599 tree init;
10601 /* Fold an expression like: "foo"[2].
10602 This is not done in fold so it won't happen inside &.
10603 Don't fold if this is for wide characters since it's too
10604 difficult to do correctly and this is a very rare case. */
10606 if (modifier != EXPAND_CONST_ADDRESS
10607 && modifier != EXPAND_INITIALIZER
10608 && modifier != EXPAND_MEMORY)
10610 tree t = fold_read_from_constant_string (exp);
10612 if (t)
10613 return expand_expr (t, target, tmode, modifier);
10616 /* If this is a constant index into a constant array,
10617 just get the value from the array. Handle both the cases when
10618 we have an explicit constructor and when our operand is a variable
10619 that was declared const. */
10621 if (modifier != EXPAND_CONST_ADDRESS
10622 && modifier != EXPAND_INITIALIZER
10623 && modifier != EXPAND_MEMORY
10624 && TREE_CODE (array) == CONSTRUCTOR
10625 && ! TREE_SIDE_EFFECTS (array)
10626 && TREE_CODE (index) == INTEGER_CST)
10628 unsigned HOST_WIDE_INT ix;
10629 tree field, value;
10631 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10632 field, value)
10633 if (tree_int_cst_equal (field, index))
10635 if (!TREE_SIDE_EFFECTS (value))
10636 return expand_expr (fold (value), target, tmode, modifier);
10637 break;
10641 else if (optimize >= 1
10642 && modifier != EXPAND_CONST_ADDRESS
10643 && modifier != EXPAND_INITIALIZER
10644 && modifier != EXPAND_MEMORY
10645 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10646 && TREE_CODE (index) == INTEGER_CST
10647 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10648 && (init = ctor_for_folding (array)) != error_mark_node)
10650 if (init == NULL_TREE)
10652 tree value = build_zero_cst (type);
10653 if (TREE_CODE (value) == CONSTRUCTOR)
10655 /* If VALUE is a CONSTRUCTOR, this optimization is only
10656 useful if this doesn't store the CONSTRUCTOR into
10657 memory. If it does, it is more efficient to just
10658 load the data from the array directly. */
10659 rtx ret = expand_constructor (value, target,
10660 modifier, true);
10661 if (ret == NULL_RTX)
10662 value = NULL_TREE;
10665 if (value)
10666 return expand_expr (value, target, tmode, modifier);
10668 else if (TREE_CODE (init) == CONSTRUCTOR)
10670 unsigned HOST_WIDE_INT ix;
10671 tree field, value;
10673 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10674 field, value)
10675 if (tree_int_cst_equal (field, index))
10677 if (TREE_SIDE_EFFECTS (value))
10678 break;
10680 if (TREE_CODE (value) == CONSTRUCTOR)
10682 /* If VALUE is a CONSTRUCTOR, this
10683 optimization is only useful if
10684 this doesn't store the CONSTRUCTOR
10685 into memory. If it does, it is more
10686 efficient to just load the data from
10687 the array directly. */
10688 rtx ret = expand_constructor (value, target,
10689 modifier, true);
10690 if (ret == NULL_RTX)
10691 break;
10694 return
10695 expand_expr (fold (value), target, tmode, modifier);
10698 else if (TREE_CODE (init) == STRING_CST)
10700 tree low_bound = array_ref_low_bound (exp);
10701 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10703 /* Optimize the special case of a zero lower bound.
10705 We convert the lower bound to sizetype to avoid problems
10706 with constant folding. E.g. suppose the lower bound is
10707 1 and its mode is QI. Without the conversion
10708 (ARRAY + (INDEX - (unsigned char)1))
10709 becomes
10710 (ARRAY + (-(unsigned char)1) + INDEX)
10711 which becomes
10712 (ARRAY + 255 + INDEX). Oops! */
10713 if (!integer_zerop (low_bound))
10714 index1 = size_diffop_loc (loc, index1,
10715 fold_convert_loc (loc, sizetype,
10716 low_bound));
10718 if (tree_fits_uhwi_p (index1)
10719 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10721 tree char_type = TREE_TYPE (TREE_TYPE (init));
10722 scalar_int_mode char_mode;
10724 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
10725 && GET_MODE_SIZE (char_mode) == 1)
10726 return gen_int_mode (TREE_STRING_POINTER (init)
10727 [TREE_INT_CST_LOW (index1)],
10728 char_mode);
10733 goto normal_inner_ref;
10735 case COMPONENT_REF:
10736 /* If the operand is a CONSTRUCTOR, we can just extract the
10737 appropriate field if it is present. */
10738 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10740 unsigned HOST_WIDE_INT idx;
10741 tree field, value;
10742 scalar_int_mode field_mode;
10744 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10745 idx, field, value)
10746 if (field == treeop1
10747 /* We can normally use the value of the field in the
10748 CONSTRUCTOR. However, if this is a bitfield in
10749 an integral mode that we can fit in a HOST_WIDE_INT,
10750 we must mask only the number of bits in the bitfield,
10751 since this is done implicitly by the constructor. If
10752 the bitfield does not meet either of those conditions,
10753 we can't do this optimization. */
10754 && (! DECL_BIT_FIELD (field)
10755 || (is_int_mode (DECL_MODE (field), &field_mode)
10756 && (GET_MODE_PRECISION (field_mode)
10757 <= HOST_BITS_PER_WIDE_INT))))
10759 if (DECL_BIT_FIELD (field)
10760 && modifier == EXPAND_STACK_PARM)
10761 target = 0;
10762 op0 = expand_expr (value, target, tmode, modifier);
10763 if (DECL_BIT_FIELD (field))
10765 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10766 scalar_int_mode imode
10767 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10769 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10771 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10772 imode);
10773 op0 = expand_and (imode, op0, op1, target);
10775 else
10777 int count = GET_MODE_PRECISION (imode) - bitsize;
10779 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10780 target, 0);
10781 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10782 target, 0);
10786 return op0;
10789 goto normal_inner_ref;
10791 case BIT_FIELD_REF:
10792 case ARRAY_RANGE_REF:
10793 normal_inner_ref:
10795 machine_mode mode1, mode2;
10796 poly_int64 bitsize, bitpos, bytepos;
10797 tree offset;
10798 int reversep, volatilep = 0, must_force_mem;
10799 tree tem
10800 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10801 &unsignedp, &reversep, &volatilep);
10802 rtx orig_op0, memloc;
10803 bool clear_mem_expr = false;
10805 /* If we got back the original object, something is wrong. Perhaps
10806 we are evaluating an expression too early. In any event, don't
10807 infinitely recurse. */
10808 gcc_assert (tem != exp);
10810 /* If TEM's type is a union of variable size, pass TARGET to the inner
10811 computation, since it will need a temporary and TARGET is known
10812 to have to do. This occurs in unchecked conversion in Ada. */
10813 orig_op0 = op0
10814 = expand_expr_real (tem,
10815 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10816 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10817 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10818 != INTEGER_CST)
10819 && modifier != EXPAND_STACK_PARM
10820 ? target : NULL_RTX),
10821 VOIDmode,
10822 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10823 NULL, true);
10825 /* If the field has a mode, we want to access it in the
10826 field's mode, not the computed mode.
10827 If a MEM has VOIDmode (external with incomplete type),
10828 use BLKmode for it instead. */
10829 if (MEM_P (op0))
10831 if (mode1 != VOIDmode)
10832 op0 = adjust_address (op0, mode1, 0);
10833 else if (GET_MODE (op0) == VOIDmode)
10834 op0 = adjust_address (op0, BLKmode, 0);
10837 mode2
10838 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10840 /* Make sure bitpos is not negative, it can wreak havoc later. */
10841 if (maybe_lt (bitpos, 0))
10843 gcc_checking_assert (offset == NULL_TREE);
10844 offset = size_int (bits_to_bytes_round_down (bitpos));
10845 bitpos = num_trailing_bits (bitpos);
10848 /* If we have either an offset, a BLKmode result, or a reference
10849 outside the underlying object, we must force it to memory.
10850 Such a case can occur in Ada if we have unchecked conversion
10851 of an expression from a scalar type to an aggregate type or
10852 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10853 passed a partially uninitialized object or a view-conversion
10854 to a larger size. */
10855 must_force_mem = (offset
10856 || mode1 == BLKmode
10857 || (mode == BLKmode
10858 && !int_mode_for_size (bitsize, 1).exists ())
10859 || maybe_gt (bitpos + bitsize,
10860 GET_MODE_BITSIZE (mode2)));
10862 /* Handle CONCAT first. */
10863 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10865 if (known_eq (bitpos, 0)
10866 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
10867 && COMPLEX_MODE_P (mode1)
10868 && COMPLEX_MODE_P (GET_MODE (op0))
10869 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10870 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10872 if (reversep)
10873 op0 = flip_storage_order (GET_MODE (op0), op0);
10874 if (mode1 != GET_MODE (op0))
10876 rtx parts[2];
10877 for (int i = 0; i < 2; i++)
10879 rtx op = read_complex_part (op0, i != 0);
10880 if (GET_CODE (op) == SUBREG)
10881 op = force_reg (GET_MODE (op), op);
10882 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
10883 if (temp)
10884 op = temp;
10885 else
10887 if (!REG_P (op) && !MEM_P (op))
10888 op = force_reg (GET_MODE (op), op);
10889 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10891 parts[i] = op;
10893 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10895 return op0;
10897 if (known_eq (bitpos, 0)
10898 && known_eq (bitsize,
10899 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10900 && maybe_ne (bitsize, 0))
10902 op0 = XEXP (op0, 0);
10903 mode2 = GET_MODE (op0);
10905 else if (known_eq (bitpos,
10906 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10907 && known_eq (bitsize,
10908 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
10909 && maybe_ne (bitpos, 0)
10910 && maybe_ne (bitsize, 0))
10912 op0 = XEXP (op0, 1);
10913 bitpos = 0;
10914 mode2 = GET_MODE (op0);
10916 else
10917 /* Otherwise force into memory. */
10918 must_force_mem = 1;
10921 /* If this is a constant, put it in a register if it is a legitimate
10922 constant and we don't need a memory reference. */
10923 if (CONSTANT_P (op0)
10924 && mode2 != BLKmode
10925 && targetm.legitimate_constant_p (mode2, op0)
10926 && !must_force_mem)
10927 op0 = force_reg (mode2, op0);
10929 /* Otherwise, if this is a constant, try to force it to the constant
10930 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10931 is a legitimate constant. */
10932 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10933 op0 = validize_mem (memloc);
10935 /* Otherwise, if this is a constant or the object is not in memory
10936 and need be, put it there. */
10937 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10939 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10940 emit_move_insn (memloc, op0);
10941 op0 = memloc;
10942 clear_mem_expr = true;
10945 if (offset)
10947 machine_mode address_mode;
10948 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10949 EXPAND_SUM);
10951 gcc_assert (MEM_P (op0));
10953 address_mode = get_address_mode (op0);
10954 if (GET_MODE (offset_rtx) != address_mode)
10956 /* We cannot be sure that the RTL in offset_rtx is valid outside
10957 of a memory address context, so force it into a register
10958 before attempting to convert it to the desired mode. */
10959 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10960 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10963 /* See the comment in expand_assignment for the rationale. */
10964 if (mode1 != VOIDmode
10965 && maybe_ne (bitpos, 0)
10966 && maybe_gt (bitsize, 0)
10967 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10968 && multiple_p (bitpos, bitsize)
10969 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
10970 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10972 op0 = adjust_address (op0, mode1, bytepos);
10973 bitpos = 0;
10976 op0 = offset_address (op0, offset_rtx,
10977 highest_pow2_factor (offset));
10980 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10981 record its alignment as BIGGEST_ALIGNMENT. */
10982 if (MEM_P (op0)
10983 && known_eq (bitpos, 0)
10984 && offset != 0
10985 && is_aligning_offset (offset, tem))
10986 set_mem_align (op0, BIGGEST_ALIGNMENT);
10988 /* Don't forget about volatility even if this is a bitfield. */
10989 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10991 if (op0 == orig_op0)
10992 op0 = copy_rtx (op0);
10994 MEM_VOLATILE_P (op0) = 1;
10997 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
10999 if (op0 == orig_op0)
11000 op0 = copy_rtx (op0);
11002 set_mem_align (op0, BITS_PER_UNIT);
11005 /* In cases where an aligned union has an unaligned object
11006 as a field, we might be extracting a BLKmode value from
11007 an integer-mode (e.g., SImode) object. Handle this case
11008 by doing the extract into an object as wide as the field
11009 (which we know to be the width of a basic mode), then
11010 storing into memory, and changing the mode to BLKmode. */
11011 if (mode1 == VOIDmode
11012 || REG_P (op0) || GET_CODE (op0) == SUBREG
11013 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11014 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11015 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11016 && modifier != EXPAND_CONST_ADDRESS
11017 && modifier != EXPAND_INITIALIZER
11018 && modifier != EXPAND_MEMORY)
11019 /* If the bitfield is volatile and the bitsize
11020 is narrower than the access size of the bitfield,
11021 we need to extract bitfields from the access. */
11022 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11023 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11024 && mode1 != BLKmode
11025 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11026 /* If the field isn't aligned enough to fetch as a memref,
11027 fetch it as a bit field. */
11028 || (mode1 != BLKmode
11029 && (((MEM_P (op0)
11030 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11031 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11032 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11033 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11034 && modifier != EXPAND_MEMORY
11035 && ((modifier == EXPAND_CONST_ADDRESS
11036 || modifier == EXPAND_INITIALIZER)
11037 ? STRICT_ALIGNMENT
11038 : targetm.slow_unaligned_access (mode1,
11039 MEM_ALIGN (op0))))
11040 || !multiple_p (bitpos, BITS_PER_UNIT)))
11041 /* If the type and the field are a constant size and the
11042 size of the type isn't the same size as the bitfield,
11043 we must use bitfield operations. */
11044 || (known_size_p (bitsize)
11045 && TYPE_SIZE (TREE_TYPE (exp))
11046 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11047 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11048 bitsize)))
11050 machine_mode ext_mode = mode;
11052 if (ext_mode == BLKmode
11053 && ! (target != 0 && MEM_P (op0)
11054 && MEM_P (target)
11055 && multiple_p (bitpos, BITS_PER_UNIT)))
11056 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11058 if (ext_mode == BLKmode)
11060 if (target == 0)
11061 target = assign_temp (type, 1, 1);
11063 /* ??? Unlike the similar test a few lines below, this one is
11064 very likely obsolete. */
11065 if (known_eq (bitsize, 0))
11066 return target;
11068 /* In this case, BITPOS must start at a byte boundary and
11069 TARGET, if specified, must be a MEM. */
11070 gcc_assert (MEM_P (op0)
11071 && (!target || MEM_P (target)));
11073 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11074 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11075 emit_block_move (target,
11076 adjust_address (op0, VOIDmode, bytepos),
11077 gen_int_mode (bytesize, Pmode),
11078 (modifier == EXPAND_STACK_PARM
11079 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11081 return target;
11084 /* If we have nothing to extract, the result will be 0 for targets
11085 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11086 return 0 for the sake of consistency, as reading a zero-sized
11087 bitfield is valid in Ada and the value is fully specified. */
11088 if (known_eq (bitsize, 0))
11089 return const0_rtx;
11091 op0 = validize_mem (op0);
11093 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11094 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11096 /* If the result has aggregate type and the extraction is done in
11097 an integral mode, then the field may be not aligned on a byte
11098 boundary; in this case, if it has reverse storage order, it
11099 needs to be extracted as a scalar field with reverse storage
11100 order and put back into memory order afterwards. */
11101 if (AGGREGATE_TYPE_P (type)
11102 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11103 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11105 gcc_checking_assert (known_ge (bitpos, 0));
11106 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11107 (modifier == EXPAND_STACK_PARM
11108 ? NULL_RTX : target),
11109 ext_mode, ext_mode, reversep, alt_rtl);
11111 /* If the result has aggregate type and the mode of OP0 is an
11112 integral mode then, if BITSIZE is narrower than this mode
11113 and this is for big-endian data, we must put the field
11114 into the high-order bits. And we must also put it back
11115 into memory order if it has been previously reversed. */
11116 scalar_int_mode op0_mode;
11117 if (AGGREGATE_TYPE_P (type)
11118 && is_int_mode (GET_MODE (op0), &op0_mode))
11120 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11122 gcc_checking_assert (known_le (bitsize, size));
11123 if (maybe_lt (bitsize, size)
11124 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11125 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11126 size - bitsize, op0, 1);
11128 if (reversep)
11129 op0 = flip_storage_order (op0_mode, op0);
11132 /* If the result type is BLKmode, store the data into a temporary
11133 of the appropriate type, but with the mode corresponding to the
11134 mode for the data we have (op0's mode). */
11135 if (mode == BLKmode)
11137 rtx new_rtx
11138 = assign_stack_temp_for_type (ext_mode,
11139 GET_MODE_BITSIZE (ext_mode),
11140 type);
11141 emit_move_insn (new_rtx, op0);
11142 op0 = copy_rtx (new_rtx);
11143 PUT_MODE (op0, BLKmode);
11146 return op0;
11149 /* If the result is BLKmode, use that to access the object
11150 now as well. */
11151 if (mode == BLKmode)
11152 mode1 = BLKmode;
11154 /* Get a reference to just this component. */
11155 bytepos = bits_to_bytes_round_down (bitpos);
11156 if (modifier == EXPAND_CONST_ADDRESS
11157 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11158 op0 = adjust_address_nv (op0, mode1, bytepos);
11159 else
11160 op0 = adjust_address (op0, mode1, bytepos);
11162 if (op0 == orig_op0)
11163 op0 = copy_rtx (op0);
11165 /* Don't set memory attributes if the base expression is
11166 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11167 we should just honor its original memory attributes. */
11168 if (!(TREE_CODE (tem) == SSA_NAME
11169 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11170 set_mem_attributes (op0, exp, 0);
11172 if (REG_P (XEXP (op0, 0)))
11173 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11175 /* If op0 is a temporary because the original expressions was forced
11176 to memory, clear MEM_EXPR so that the original expression cannot
11177 be marked as addressable through MEM_EXPR of the temporary. */
11178 if (clear_mem_expr)
11179 set_mem_expr (op0, NULL_TREE);
11181 MEM_VOLATILE_P (op0) |= volatilep;
11183 if (reversep
11184 && modifier != EXPAND_MEMORY
11185 && modifier != EXPAND_WRITE)
11186 op0 = flip_storage_order (mode1, op0);
11188 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11189 || modifier == EXPAND_CONST_ADDRESS
11190 || modifier == EXPAND_INITIALIZER)
11191 return op0;
11193 if (target == 0)
11194 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11196 convert_move (target, op0, unsignedp);
11197 return target;
11200 case OBJ_TYPE_REF:
11201 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11203 case CALL_EXPR:
11204 /* All valid uses of __builtin_va_arg_pack () are removed during
11205 inlining. */
11206 if (CALL_EXPR_VA_ARG_PACK (exp))
11207 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
11209 tree fndecl = get_callee_fndecl (exp), attr;
11211 if (fndecl
11212 /* Don't diagnose the error attribute in thunks, those are
11213 artificially created. */
11214 && !CALL_FROM_THUNK_P (exp)
11215 && (attr = lookup_attribute ("error",
11216 DECL_ATTRIBUTES (fndecl))) != NULL)
11218 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11219 error ("%Kcall to %qs declared with attribute error: %s", exp,
11220 identifier_to_locale (ident),
11221 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11223 if (fndecl
11224 /* Don't diagnose the warning attribute in thunks, those are
11225 artificially created. */
11226 && !CALL_FROM_THUNK_P (exp)
11227 && (attr = lookup_attribute ("warning",
11228 DECL_ATTRIBUTES (fndecl))) != NULL)
11230 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11231 warning_at (tree_nonartificial_location (exp),
11232 OPT_Wattribute_warning,
11233 "%Kcall to %qs declared with attribute warning: %s",
11234 exp, identifier_to_locale (ident),
11235 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11238 /* Check for a built-in function. */
11239 if (fndecl && fndecl_built_in_p (fndecl))
11241 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11242 return expand_builtin (exp, target, subtarget, tmode, ignore);
11245 return expand_call (exp, target, ignore);
11247 case VIEW_CONVERT_EXPR:
11248 op0 = NULL_RTX;
11250 /* If we are converting to BLKmode, try to avoid an intermediate
11251 temporary by fetching an inner memory reference. */
11252 if (mode == BLKmode
11253 && poly_int_tree_p (TYPE_SIZE (type))
11254 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11255 && handled_component_p (treeop0))
11257 machine_mode mode1;
11258 poly_int64 bitsize, bitpos, bytepos;
11259 tree offset;
11260 int reversep, volatilep = 0;
11261 tree tem
11262 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11263 &unsignedp, &reversep, &volatilep);
11265 /* ??? We should work harder and deal with non-zero offsets. */
11266 if (!offset
11267 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11268 && !reversep
11269 && known_size_p (bitsize)
11270 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11272 /* See the normal_inner_ref case for the rationale. */
11273 rtx orig_op0
11274 = expand_expr_real (tem,
11275 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11276 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11277 != INTEGER_CST)
11278 && modifier != EXPAND_STACK_PARM
11279 ? target : NULL_RTX),
11280 VOIDmode,
11281 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11282 NULL, true);
11284 if (MEM_P (orig_op0))
11286 op0 = orig_op0;
11288 /* Get a reference to just this component. */
11289 if (modifier == EXPAND_CONST_ADDRESS
11290 || modifier == EXPAND_SUM
11291 || modifier == EXPAND_INITIALIZER)
11292 op0 = adjust_address_nv (op0, mode, bytepos);
11293 else
11294 op0 = adjust_address (op0, mode, bytepos);
11296 if (op0 == orig_op0)
11297 op0 = copy_rtx (op0);
11299 set_mem_attributes (op0, treeop0, 0);
11300 if (REG_P (XEXP (op0, 0)))
11301 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11303 MEM_VOLATILE_P (op0) |= volatilep;
11308 if (!op0)
11309 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11310 NULL, inner_reference_p);
11312 /* If the input and output modes are both the same, we are done. */
11313 if (mode == GET_MODE (op0))
11315 /* If neither mode is BLKmode, and both modes are the same size
11316 then we can use gen_lowpart. */
11317 else if (mode != BLKmode
11318 && GET_MODE (op0) != BLKmode
11319 && known_eq (GET_MODE_PRECISION (mode),
11320 GET_MODE_PRECISION (GET_MODE (op0)))
11321 && !COMPLEX_MODE_P (GET_MODE (op0)))
11323 if (GET_CODE (op0) == SUBREG)
11324 op0 = force_reg (GET_MODE (op0), op0);
11325 temp = gen_lowpart_common (mode, op0);
11326 if (temp)
11327 op0 = temp;
11328 else
11330 if (!REG_P (op0) && !MEM_P (op0))
11331 op0 = force_reg (GET_MODE (op0), op0);
11332 op0 = gen_lowpart (mode, op0);
11335 /* If both types are integral, convert from one mode to the other. */
11336 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11337 op0 = convert_modes (mode, GET_MODE (op0), op0,
11338 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11339 /* If the output type is a bit-field type, do an extraction. */
11340 else if (reduce_bit_field)
11341 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11342 TYPE_UNSIGNED (type), NULL_RTX,
11343 mode, mode, false, NULL);
11344 /* As a last resort, spill op0 to memory, and reload it in a
11345 different mode. */
11346 else if (!MEM_P (op0))
11348 /* If the operand is not a MEM, force it into memory. Since we
11349 are going to be changing the mode of the MEM, don't call
11350 force_const_mem for constants because we don't allow pool
11351 constants to change mode. */
11352 tree inner_type = TREE_TYPE (treeop0);
11354 gcc_assert (!TREE_ADDRESSABLE (exp));
11356 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11357 target
11358 = assign_stack_temp_for_type
11359 (TYPE_MODE (inner_type),
11360 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11362 emit_move_insn (target, op0);
11363 op0 = target;
11366 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11367 output type is such that the operand is known to be aligned, indicate
11368 that it is. Otherwise, we need only be concerned about alignment for
11369 non-BLKmode results. */
11370 if (MEM_P (op0))
11372 enum insn_code icode;
11374 if (modifier != EXPAND_WRITE
11375 && modifier != EXPAND_MEMORY
11376 && !inner_reference_p
11377 && mode != BLKmode
11378 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11380 /* If the target does have special handling for unaligned
11381 loads of mode then use them. */
11382 if ((icode = optab_handler (movmisalign_optab, mode))
11383 != CODE_FOR_nothing)
11385 rtx reg;
11387 op0 = adjust_address (op0, mode, 0);
11388 /* We've already validated the memory, and we're creating a
11389 new pseudo destination. The predicates really can't
11390 fail. */
11391 reg = gen_reg_rtx (mode);
11393 /* Nor can the insn generator. */
11394 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11395 emit_insn (insn);
11396 return reg;
11398 else if (STRICT_ALIGNMENT)
11400 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11401 poly_uint64 temp_size = mode_size;
11402 if (GET_MODE (op0) != BLKmode)
11403 temp_size = upper_bound (temp_size,
11404 GET_MODE_SIZE (GET_MODE (op0)));
11405 rtx new_rtx
11406 = assign_stack_temp_for_type (mode, temp_size, type);
11407 rtx new_with_op0_mode
11408 = adjust_address (new_rtx, GET_MODE (op0), 0);
11410 gcc_assert (!TREE_ADDRESSABLE (exp));
11412 if (GET_MODE (op0) == BLKmode)
11414 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11415 emit_block_move (new_with_op0_mode, op0, size_rtx,
11416 (modifier == EXPAND_STACK_PARM
11417 ? BLOCK_OP_CALL_PARM
11418 : BLOCK_OP_NORMAL));
11420 else
11421 emit_move_insn (new_with_op0_mode, op0);
11423 op0 = new_rtx;
11427 op0 = adjust_address (op0, mode, 0);
11430 return op0;
11432 case MODIFY_EXPR:
11434 tree lhs = treeop0;
11435 tree rhs = treeop1;
11436 gcc_assert (ignore);
11438 /* Check for |= or &= of a bitfield of size one into another bitfield
11439 of size 1. In this case, (unless we need the result of the
11440 assignment) we can do this more efficiently with a
11441 test followed by an assignment, if necessary.
11443 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11444 things change so we do, this code should be enhanced to
11445 support it. */
11446 if (TREE_CODE (lhs) == COMPONENT_REF
11447 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11448 || TREE_CODE (rhs) == BIT_AND_EXPR)
11449 && TREE_OPERAND (rhs, 0) == lhs
11450 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11451 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11452 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11454 rtx_code_label *label = gen_label_rtx ();
11455 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11456 profile_probability prob = profile_probability::uninitialized ();
11457 if (value)
11458 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
11459 else
11460 jumpif (TREE_OPERAND (rhs, 1), label, prob);
11461 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11462 false);
11463 do_pending_stack_adjust ();
11464 emit_label (label);
11465 return const0_rtx;
11468 expand_assignment (lhs, rhs, false);
11469 return const0_rtx;
11472 case ADDR_EXPR:
11473 return expand_expr_addr_expr (exp, target, tmode, modifier);
11475 case REALPART_EXPR:
11476 op0 = expand_normal (treeop0);
11477 return read_complex_part (op0, false);
11479 case IMAGPART_EXPR:
11480 op0 = expand_normal (treeop0);
11481 return read_complex_part (op0, true);
11483 case RETURN_EXPR:
11484 case LABEL_EXPR:
11485 case GOTO_EXPR:
11486 case SWITCH_EXPR:
11487 case ASM_EXPR:
11488 /* Expanded in cfgexpand.c. */
11489 gcc_unreachable ();
11491 case TRY_CATCH_EXPR:
11492 case CATCH_EXPR:
11493 case EH_FILTER_EXPR:
11494 case TRY_FINALLY_EXPR:
11495 case EH_ELSE_EXPR:
11496 /* Lowered by tree-eh.c. */
11497 gcc_unreachable ();
11499 case WITH_CLEANUP_EXPR:
11500 case CLEANUP_POINT_EXPR:
11501 case TARGET_EXPR:
11502 case CASE_LABEL_EXPR:
11503 case VA_ARG_EXPR:
11504 case BIND_EXPR:
11505 case INIT_EXPR:
11506 case CONJ_EXPR:
11507 case COMPOUND_EXPR:
11508 case PREINCREMENT_EXPR:
11509 case PREDECREMENT_EXPR:
11510 case POSTINCREMENT_EXPR:
11511 case POSTDECREMENT_EXPR:
11512 case LOOP_EXPR:
11513 case EXIT_EXPR:
11514 case COMPOUND_LITERAL_EXPR:
11515 /* Lowered by gimplify.c. */
11516 gcc_unreachable ();
11518 case FDESC_EXPR:
11519 /* Function descriptors are not valid except for as
11520 initialization constants, and should not be expanded. */
11521 gcc_unreachable ();
11523 case WITH_SIZE_EXPR:
11524 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11525 have pulled out the size to use in whatever context it needed. */
11526 return expand_expr_real (treeop0, original_target, tmode,
11527 modifier, alt_rtl, inner_reference_p);
11529 default:
11530 return expand_expr_real_2 (&ops, target, tmode, modifier);
11534 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11535 signedness of TYPE), possibly returning the result in TARGET.
11536 TYPE is known to be a partial integer type. */
11537 static rtx
11538 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11540 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
11541 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11542 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
11543 && (!target || GET_MODE (target) == mode));
11545 /* For constant values, reduce using wide_int_to_tree. */
11546 if (poly_int_rtx_p (exp))
11548 auto value = wi::to_poly_wide (exp, mode);
11549 tree t = wide_int_to_tree (type, value);
11550 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11552 else if (TYPE_UNSIGNED (type))
11554 rtx mask = immed_wide_int_const
11555 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11556 return expand_and (mode, exp, mask, target);
11558 else
11560 int count = GET_MODE_PRECISION (mode) - prec;
11561 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11562 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11566 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11567 when applied to the address of EXP produces an address known to be
11568 aligned more than BIGGEST_ALIGNMENT. */
11570 static int
11571 is_aligning_offset (const_tree offset, const_tree exp)
11573 /* Strip off any conversions. */
11574 while (CONVERT_EXPR_P (offset))
11575 offset = TREE_OPERAND (offset, 0);
11577 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11578 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11579 if (TREE_CODE (offset) != BIT_AND_EXPR
11580 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11581 || compare_tree_int (TREE_OPERAND (offset, 1),
11582 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11583 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11584 return 0;
11586 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11587 It must be NEGATE_EXPR. Then strip any more conversions. */
11588 offset = TREE_OPERAND (offset, 0);
11589 while (CONVERT_EXPR_P (offset))
11590 offset = TREE_OPERAND (offset, 0);
11592 if (TREE_CODE (offset) != NEGATE_EXPR)
11593 return 0;
11595 offset = TREE_OPERAND (offset, 0);
11596 while (CONVERT_EXPR_P (offset))
11597 offset = TREE_OPERAND (offset, 0);
11599 /* This must now be the address of EXP. */
11600 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11603 /* If EXPR is a constant initializer (either an expression or CONSTRUCTOR),
11604 attempt to obtain its native representation as an array of nonzero BYTES.
11605 Return true on success and false on failure (the latter without modifying
11606 BYTES). */
11608 static bool
11609 convert_to_bytes (tree type, tree expr, vec<unsigned char> *bytes)
11611 if (TREE_CODE (expr) == CONSTRUCTOR)
11613 /* Set to the size of the CONSTRUCTOR elements. */
11614 unsigned HOST_WIDE_INT ctor_size = bytes->length ();
11616 if (TREE_CODE (type) == ARRAY_TYPE)
11618 tree val, idx;
11619 tree eltype = TREE_TYPE (type);
11620 unsigned HOST_WIDE_INT elsize =
11621 tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
11623 /* Jump through hoops to determine the lower bound for languages
11624 like Ada that can set it to an (almost) arbitrary value. */
11625 tree dom = TYPE_DOMAIN (type);
11626 if (!dom)
11627 return false;
11628 tree min = TYPE_MIN_VALUE (dom);
11629 if (!min || !tree_fits_uhwi_p (min))
11630 return false;
11631 unsigned HOST_WIDE_INT i, last_idx = tree_to_uhwi (min) - 1;
11632 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, idx, val)
11634 /* Append zeros for elements with no initializers. */
11635 if (!tree_fits_uhwi_p (idx))
11636 return false;
11637 unsigned HOST_WIDE_INT cur_idx = tree_to_uhwi (idx);
11638 if (unsigned HOST_WIDE_INT size = cur_idx - (last_idx + 1))
11640 size = size * elsize + bytes->length ();
11641 bytes->safe_grow_cleared (size, true);
11644 if (!convert_to_bytes (eltype, val, bytes))
11645 return false;
11647 last_idx = cur_idx;
11650 else if (TREE_CODE (type) == RECORD_TYPE)
11652 tree val, fld;
11653 unsigned HOST_WIDE_INT i;
11654 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (expr), i, fld, val)
11656 /* Append zeros for members with no initializers and
11657 any padding. */
11658 unsigned HOST_WIDE_INT cur_off = int_byte_position (fld);
11659 if (bytes->length () < cur_off)
11660 bytes->safe_grow_cleared (cur_off, true);
11662 if (!convert_to_bytes (TREE_TYPE (val), val, bytes))
11663 return false;
11666 else
11667 return false;
11669 /* Compute the size of the COSNTRUCTOR elements. */
11670 ctor_size = bytes->length () - ctor_size;
11672 /* Append zeros to the byte vector to the full size of the type.
11673 The type size can be less than the size of the CONSTRUCTOR
11674 if the latter contains initializers for a flexible array
11675 member. */
11676 tree size = TYPE_SIZE_UNIT (type);
11677 unsigned HOST_WIDE_INT type_size = tree_to_uhwi (size);
11678 if (ctor_size < type_size)
11679 if (unsigned HOST_WIDE_INT size_grow = type_size - ctor_size)
11680 bytes->safe_grow_cleared (bytes->length () + size_grow, true);
11682 return true;
11685 /* Except for RECORD_TYPE which may have an initialized flexible array
11686 member, the size of a type is the same as the size of the initializer
11687 (including any implicitly zeroed out members and padding). Allocate
11688 just enough for that many bytes. */
11689 tree expr_size = TYPE_SIZE_UNIT (TREE_TYPE (expr));
11690 if (!expr_size || !tree_fits_uhwi_p (expr_size))
11691 return false;
11692 const unsigned HOST_WIDE_INT expr_bytes = tree_to_uhwi (expr_size);
11693 const unsigned bytes_sofar = bytes->length ();
11694 /* native_encode_expr can convert at most INT_MAX bytes. vec is limited
11695 to at most UINT_MAX. */
11696 if (bytes_sofar + expr_bytes > INT_MAX)
11697 return false;
11699 /* Unlike for RECORD_TYPE, there is no need to clear the memory since
11700 it's completely overwritten by native_encode_expr. */
11701 bytes->safe_grow (bytes_sofar + expr_bytes, true);
11702 unsigned char *pnext = bytes->begin () + bytes_sofar;
11703 int nbytes = native_encode_expr (expr, pnext, expr_bytes, 0);
11704 /* NBYTES is zero on failure. Otherwise it should equal EXPR_BYTES. */
11705 return (unsigned HOST_WIDE_INT) nbytes == expr_bytes;
11708 /* Return a STRING_CST corresponding to ARG's constant initializer either
11709 if it's a string constant, or, when VALREP is set, any other constant,
11710 or null otherwise.
11711 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
11712 within the byte string that ARG is references. If nonnull set *MEM_SIZE
11713 to the size of the byte string. If nonnull, set *DECL to the constant
11714 declaration ARG refers to. */
11716 static tree
11717 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
11718 bool valrep = false)
11720 tree dummy = NULL_TREE;;
11721 if (!mem_size)
11722 mem_size = &dummy;
11724 /* Store the type of the original expression before conversions
11725 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
11726 removed. */
11727 tree argtype = TREE_TYPE (arg);
11729 tree array;
11730 STRIP_NOPS (arg);
11732 /* Non-constant index into the character array in an ARRAY_REF
11733 expression or null. */
11734 tree varidx = NULL_TREE;
11736 poly_int64 base_off = 0;
11738 if (TREE_CODE (arg) == ADDR_EXPR)
11740 arg = TREE_OPERAND (arg, 0);
11741 tree ref = arg;
11742 if (TREE_CODE (arg) == ARRAY_REF)
11744 tree idx = TREE_OPERAND (arg, 1);
11745 if (TREE_CODE (idx) != INTEGER_CST)
11747 /* From a pointer (but not array) argument extract the variable
11748 index to prevent get_addr_base_and_unit_offset() from failing
11749 due to it. Use it later to compute the non-constant offset
11750 into the string and return it to the caller. */
11751 varidx = idx;
11752 ref = TREE_OPERAND (arg, 0);
11754 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
11755 return NULL_TREE;
11757 if (!integer_zerop (array_ref_low_bound (arg)))
11758 return NULL_TREE;
11760 if (!integer_onep (array_ref_element_size (arg)))
11761 return NULL_TREE;
11764 array = get_addr_base_and_unit_offset (ref, &base_off);
11765 if (!array
11766 || (TREE_CODE (array) != VAR_DECL
11767 && TREE_CODE (array) != CONST_DECL
11768 && TREE_CODE (array) != STRING_CST))
11769 return NULL_TREE;
11771 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11773 tree arg0 = TREE_OPERAND (arg, 0);
11774 tree arg1 = TREE_OPERAND (arg, 1);
11776 tree offset;
11777 tree str = string_constant (arg0, &offset, mem_size, decl);
11778 if (!str)
11780 str = string_constant (arg1, &offset, mem_size, decl);
11781 arg1 = arg0;
11784 if (str)
11786 /* Avoid pointers to arrays (see bug 86622). */
11787 if (POINTER_TYPE_P (TREE_TYPE (arg))
11788 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
11789 && !(decl && !*decl)
11790 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11791 && tree_fits_uhwi_p (*mem_size)
11792 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11793 return NULL_TREE;
11795 tree type = TREE_TYPE (offset);
11796 arg1 = fold_convert (type, arg1);
11797 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
11798 return str;
11800 return NULL_TREE;
11802 else if (TREE_CODE (arg) == SSA_NAME)
11804 gimple *stmt = SSA_NAME_DEF_STMT (arg);
11805 if (!is_gimple_assign (stmt))
11806 return NULL_TREE;
11808 tree rhs1 = gimple_assign_rhs1 (stmt);
11809 tree_code code = gimple_assign_rhs_code (stmt);
11810 if (code == ADDR_EXPR)
11811 return string_constant (rhs1, ptr_offset, mem_size, decl);
11812 else if (code != POINTER_PLUS_EXPR)
11813 return NULL_TREE;
11815 tree offset;
11816 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
11818 /* Avoid pointers to arrays (see bug 86622). */
11819 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
11820 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
11821 && !(decl && !*decl)
11822 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
11823 && tree_fits_uhwi_p (*mem_size)
11824 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
11825 return NULL_TREE;
11827 tree rhs2 = gimple_assign_rhs2 (stmt);
11828 tree type = TREE_TYPE (offset);
11829 rhs2 = fold_convert (type, rhs2);
11830 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
11831 return str;
11833 return NULL_TREE;
11835 else if (DECL_P (arg))
11836 array = arg;
11837 else
11838 return NULL_TREE;
11840 tree offset = wide_int_to_tree (sizetype, base_off);
11841 if (varidx)
11843 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
11844 return NULL_TREE;
11846 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
11847 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
11848 if (TREE_CODE (chartype) != INTEGER_TYPE)
11849 return NULL;
11851 offset = fold_convert (sizetype, varidx);
11854 if (TREE_CODE (array) == STRING_CST)
11856 *ptr_offset = fold_convert (sizetype, offset);
11857 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
11858 if (decl)
11859 *decl = NULL_TREE;
11860 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
11861 >= TREE_STRING_LENGTH (array));
11862 return array;
11865 tree init = ctor_for_folding (array);
11866 if (!init || init == error_mark_node)
11867 return NULL_TREE;
11869 if (valrep)
11871 HOST_WIDE_INT cstoff;
11872 if (!base_off.is_constant (&cstoff))
11873 return NULL_TREE;
11875 /* If value representation was requested convert the initializer
11876 for the whole array or object into a string of bytes forming
11877 its value representation and return it. */
11878 auto_vec<unsigned char> bytes;
11879 if (!convert_to_bytes (TREE_TYPE (init), init, &bytes))
11880 return NULL_TREE;
11882 unsigned n = bytes.length ();
11883 const char *p = reinterpret_cast<const char *>(bytes.address ());
11884 init = build_string_literal (n, p, char_type_node);
11885 init = TREE_OPERAND (init, 0);
11886 init = TREE_OPERAND (init, 0);
11888 *mem_size = size_int (TREE_STRING_LENGTH (init));
11889 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
11891 if (decl)
11892 *decl = array;
11894 return init;
11897 if (TREE_CODE (init) == CONSTRUCTOR)
11899 /* Convert the 64-bit constant offset to a wider type to avoid
11900 overflow and use it to obtain the initializer for the subobject
11901 it points into. */
11902 offset_int wioff;
11903 if (!base_off.is_constant (&wioff))
11904 return NULL_TREE;
11906 wioff *= BITS_PER_UNIT;
11907 if (!wi::fits_uhwi_p (wioff))
11908 return NULL_TREE;
11910 base_off = wioff.to_uhwi ();
11911 unsigned HOST_WIDE_INT fieldoff = 0;
11912 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
11913 &fieldoff);
11914 if (!init || init == error_mark_node)
11915 return NULL_TREE;
11917 HOST_WIDE_INT cstoff;
11918 if (!base_off.is_constant (&cstoff))
11919 return NULL_TREE;
11921 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
11922 tree off = build_int_cst (sizetype, cstoff);
11923 if (varidx)
11924 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
11925 else
11926 offset = off;
11929 *ptr_offset = offset;
11931 tree inittype = TREE_TYPE (init);
11933 if (TREE_CODE (init) == INTEGER_CST
11934 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
11935 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
11937 /* For a reference to (address of) a single constant character,
11938 store the native representation of the character in CHARBUF.
11939 If the reference is to an element of an array or a member
11940 of a struct, only consider narrow characters until ctors
11941 for wide character arrays are transformed to STRING_CSTs
11942 like those for narrow arrays. */
11943 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11944 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
11945 if (len > 0)
11947 /* Construct a string literal with elements of INITTYPE and
11948 the representation above. Then strip
11949 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
11950 init = build_string_literal (len, (char *)charbuf, inittype);
11951 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
11955 tree initsize = TYPE_SIZE_UNIT (inittype);
11957 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
11959 /* Fold an empty/zero constructor for an implicitly initialized
11960 object or subobject into the empty string. */
11962 /* Determine the character type from that of the original
11963 expression. */
11964 tree chartype = argtype;
11965 if (POINTER_TYPE_P (chartype))
11966 chartype = TREE_TYPE (chartype);
11967 while (TREE_CODE (chartype) == ARRAY_TYPE)
11968 chartype = TREE_TYPE (chartype);
11970 if (INTEGRAL_TYPE_P (chartype)
11971 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
11973 /* Convert a char array to an empty STRING_CST having an array
11974 of the expected type and size. */
11975 if (!initsize)
11976 initsize = integer_zero_node;
11978 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
11979 init = build_string_literal (size, NULL, chartype, size);
11980 init = TREE_OPERAND (init, 0);
11981 init = TREE_OPERAND (init, 0);
11983 *ptr_offset = integer_zero_node;
11987 if (decl)
11988 *decl = array;
11990 if (TREE_CODE (init) != STRING_CST)
11991 return NULL_TREE;
11993 *mem_size = initsize;
11995 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
11997 return init;
12000 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12001 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12002 non-constant) offset in bytes within the string that ARG is accessing.
12003 If MEM_SIZE is non-zero the storage size of the memory is returned.
12004 If DECL is non-zero the constant declaration is returned if available. */
12006 tree
12007 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12009 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12012 /* Similar to string_constant, return a STRING_CST corresponding
12013 to the value representation of the first argument if it's
12014 a constant. */
12016 tree
12017 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12019 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12022 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12023 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12024 for C2 > 0 to x & C3 == C2
12025 for C2 < 0 to x & C3 == (C2 & C3). */
12026 enum tree_code
12027 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12029 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12030 tree treeop0 = gimple_assign_rhs1 (stmt);
12031 tree treeop1 = gimple_assign_rhs2 (stmt);
12032 tree type = TREE_TYPE (*arg0);
12033 scalar_int_mode mode;
12034 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12035 return code;
12036 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12037 || TYPE_PRECISION (type) <= 1
12038 || TYPE_UNSIGNED (type)
12039 /* Signed x % c == 0 should have been optimized into unsigned modulo
12040 earlier. */
12041 || integer_zerop (*arg1)
12042 /* If c is known to be non-negative, modulo will be expanded as unsigned
12043 modulo. */
12044 || get_range_pos_neg (treeop0) == 1)
12045 return code;
12047 /* x % c == d where d < 0 && d <= -c should be always false. */
12048 if (tree_int_cst_sgn (*arg1) == -1
12049 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12050 return code;
12052 int prec = TYPE_PRECISION (type);
12053 wide_int w = wi::to_wide (treeop1) - 1;
12054 w |= wi::shifted_mask (0, prec - 1, true, prec);
12055 tree c3 = wide_int_to_tree (type, w);
12056 tree c4 = *arg1;
12057 if (tree_int_cst_sgn (*arg1) == -1)
12058 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12060 rtx op0 = expand_normal (treeop0);
12061 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12063 bool speed_p = optimize_insn_for_speed_p ();
12065 do_pending_stack_adjust ();
12067 location_t loc = gimple_location (stmt);
12068 struct separate_ops ops;
12069 ops.code = TRUNC_MOD_EXPR;
12070 ops.location = loc;
12071 ops.type = TREE_TYPE (treeop0);
12072 ops.op0 = treeop0;
12073 ops.op1 = treeop1;
12074 ops.op2 = NULL_TREE;
12075 start_sequence ();
12076 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12077 EXPAND_NORMAL);
12078 rtx_insn *moinsns = get_insns ();
12079 end_sequence ();
12081 unsigned mocost = seq_cost (moinsns, speed_p);
12082 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12083 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12085 ops.code = BIT_AND_EXPR;
12086 ops.location = loc;
12087 ops.type = TREE_TYPE (treeop0);
12088 ops.op0 = treeop0;
12089 ops.op1 = c3;
12090 ops.op2 = NULL_TREE;
12091 start_sequence ();
12092 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12093 EXPAND_NORMAL);
12094 rtx_insn *muinsns = get_insns ();
12095 end_sequence ();
12097 unsigned mucost = seq_cost (muinsns, speed_p);
12098 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12099 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12101 if (mocost <= mucost)
12103 emit_insn (moinsns);
12104 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12105 return code;
12108 emit_insn (muinsns);
12109 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12110 *arg1 = c4;
12111 return code;
12114 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12115 If C1 is odd to:
12116 (X - C2) * C3 <= C4 (or >), where
12117 C3 is modular multiplicative inverse of C1 and 1<<prec and
12118 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12119 if C2 > ((1<<prec) - 1) % C1).
12120 If C1 is even, S = ctz (C1) and C2 is 0, use
12121 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12122 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12124 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12125 unsigned):
12126 (X * C3) + C4 <= 2 * C4, where
12127 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12128 C4 is ((1<<(prec - 1) - 1) / C1).
12129 If C1 is even, S = ctz(C1), use
12130 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12131 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12132 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12134 See the Hacker's Delight book, section 10-17. */
12135 enum tree_code
12136 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12138 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12139 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12141 if (optimize < 2)
12142 return code;
12144 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12145 if (stmt == NULL)
12146 return code;
12148 tree treeop0 = gimple_assign_rhs1 (stmt);
12149 tree treeop1 = gimple_assign_rhs2 (stmt);
12150 if (TREE_CODE (treeop0) != SSA_NAME
12151 || TREE_CODE (treeop1) != INTEGER_CST
12152 /* Don't optimize the undefined behavior case x % 0;
12153 x % 1 should have been optimized into zero, punt if
12154 it makes it here for whatever reason;
12155 x % -c should have been optimized into x % c. */
12156 || compare_tree_int (treeop1, 2) <= 0
12157 /* Likewise x % c == d where d >= c should be always false. */
12158 || tree_int_cst_le (treeop1, *arg1))
12159 return code;
12161 /* Unsigned x % pow2 is handled right already, for signed
12162 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12163 if (integer_pow2p (treeop1))
12164 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12166 tree type = TREE_TYPE (*arg0);
12167 scalar_int_mode mode;
12168 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12169 return code;
12170 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12171 || TYPE_PRECISION (type) <= 1)
12172 return code;
12174 signop sgn = UNSIGNED;
12175 /* If both operands are known to have the sign bit clear, handle
12176 even the signed modulo case as unsigned. treeop1 is always
12177 positive >= 2, checked above. */
12178 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12179 sgn = SIGNED;
12181 if (!TYPE_UNSIGNED (type))
12183 if (tree_int_cst_sgn (*arg1) == -1)
12184 return code;
12185 type = unsigned_type_for (type);
12186 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12187 return code;
12190 int prec = TYPE_PRECISION (type);
12191 wide_int w = wi::to_wide (treeop1);
12192 int shift = wi::ctz (w);
12193 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12194 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12195 If C1 is odd, we can handle all cases by subtracting
12196 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12197 e.g. by testing for overflow on the subtraction, punt on that for now
12198 though. */
12199 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12201 if (sgn == SIGNED)
12202 return code;
12203 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12204 if (wi::gtu_p (wi::to_wide (*arg1), x))
12205 return code;
12208 imm_use_iterator imm_iter;
12209 use_operand_p use_p;
12210 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12212 gimple *use_stmt = USE_STMT (use_p);
12213 /* Punt if treeop0 is used in the same bb in a division
12214 or another modulo with the same divisor. We should expect
12215 the division and modulo combined together. */
12216 if (use_stmt == stmt
12217 || gimple_bb (use_stmt) != gimple_bb (stmt))
12218 continue;
12219 if (!is_gimple_assign (use_stmt)
12220 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12221 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12222 continue;
12223 if (gimple_assign_rhs1 (use_stmt) != treeop0
12224 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12225 continue;
12226 return code;
12229 w = wi::lrshift (w, shift);
12230 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12231 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12232 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12233 tree c3 = wide_int_to_tree (type, m);
12234 tree c5 = NULL_TREE;
12235 wide_int d, e;
12236 if (sgn == UNSIGNED)
12238 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12239 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12240 otherwise use < or subtract one from C4. E.g. for
12241 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12242 x % 3U == 1 already needs to be
12243 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12244 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12245 d -= 1;
12246 if (shift)
12247 d = wi::lrshift (d, shift);
12249 else
12251 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12252 if (!shift)
12253 d = wi::lshift (e, 1);
12254 else
12256 e = wi::bit_and (e, wi::mask (shift, true, prec));
12257 d = wi::lrshift (e, shift - 1);
12259 c5 = wide_int_to_tree (type, e);
12261 tree c4 = wide_int_to_tree (type, d);
12263 rtx op0 = expand_normal (treeop0);
12264 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12266 bool speed_p = optimize_insn_for_speed_p ();
12268 do_pending_stack_adjust ();
12270 location_t loc = gimple_location (stmt);
12271 struct separate_ops ops;
12272 ops.code = TRUNC_MOD_EXPR;
12273 ops.location = loc;
12274 ops.type = TREE_TYPE (treeop0);
12275 ops.op0 = treeop0;
12276 ops.op1 = treeop1;
12277 ops.op2 = NULL_TREE;
12278 start_sequence ();
12279 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12280 EXPAND_NORMAL);
12281 rtx_insn *moinsns = get_insns ();
12282 end_sequence ();
12284 unsigned mocost = seq_cost (moinsns, speed_p);
12285 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12286 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12288 tree t = fold_convert_loc (loc, type, treeop0);
12289 if (!integer_zerop (*arg1))
12290 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12291 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12292 if (sgn == SIGNED)
12293 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12294 if (shift)
12296 tree s = build_int_cst (NULL_TREE, shift);
12297 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12300 start_sequence ();
12301 rtx mur = expand_normal (t);
12302 rtx_insn *muinsns = get_insns ();
12303 end_sequence ();
12305 unsigned mucost = seq_cost (muinsns, speed_p);
12306 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12307 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12309 if (mocost <= mucost)
12311 emit_insn (moinsns);
12312 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12313 return code;
12316 emit_insn (muinsns);
12317 *arg0 = make_tree (type, mur);
12318 *arg1 = c4;
12319 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12322 /* Generate code to calculate OPS, and exploded expression
12323 using a store-flag instruction and return an rtx for the result.
12324 OPS reflects a comparison.
12326 If TARGET is nonzero, store the result there if convenient.
12328 Return zero if there is no suitable set-flag instruction
12329 available on this machine.
12331 Once expand_expr has been called on the arguments of the comparison,
12332 we are committed to doing the store flag, since it is not safe to
12333 re-evaluate the expression. We emit the store-flag insn by calling
12334 emit_store_flag, but only expand the arguments if we have a reason
12335 to believe that emit_store_flag will be successful. If we think that
12336 it will, but it isn't, we have to simulate the store-flag with a
12337 set/jump/set sequence. */
12339 static rtx
12340 do_store_flag (sepops ops, rtx target, machine_mode mode)
12342 enum rtx_code code;
12343 tree arg0, arg1, type;
12344 machine_mode operand_mode;
12345 int unsignedp;
12346 rtx op0, op1;
12347 rtx subtarget = target;
12348 location_t loc = ops->location;
12350 arg0 = ops->op0;
12351 arg1 = ops->op1;
12353 /* Don't crash if the comparison was erroneous. */
12354 if (arg0 == error_mark_node || arg1 == error_mark_node)
12355 return const0_rtx;
12357 type = TREE_TYPE (arg0);
12358 operand_mode = TYPE_MODE (type);
12359 unsignedp = TYPE_UNSIGNED (type);
12361 /* We won't bother with BLKmode store-flag operations because it would mean
12362 passing a lot of information to emit_store_flag. */
12363 if (operand_mode == BLKmode)
12364 return 0;
12366 /* We won't bother with store-flag operations involving function pointers
12367 when function pointers must be canonicalized before comparisons. */
12368 if (targetm.have_canonicalize_funcptr_for_compare ()
12369 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12370 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12371 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12372 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12373 return 0;
12375 STRIP_NOPS (arg0);
12376 STRIP_NOPS (arg1);
12378 /* For vector typed comparisons emit code to generate the desired
12379 all-ones or all-zeros mask. */
12380 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12382 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12383 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12384 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12385 return expand_vec_cmp_expr (ops->type, ifexp, target);
12386 else
12387 gcc_unreachable ();
12390 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12391 into (x - C2) * C3 < C4. */
12392 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12393 && TREE_CODE (arg0) == SSA_NAME
12394 && TREE_CODE (arg1) == INTEGER_CST)
12396 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12397 &arg0, &arg1);
12398 if (new_code != ops->code)
12400 struct separate_ops nops = *ops;
12401 nops.code = ops->code = new_code;
12402 nops.op0 = arg0;
12403 nops.op1 = arg1;
12404 nops.type = TREE_TYPE (arg0);
12405 return do_store_flag (&nops, target, mode);
12409 /* Get the rtx comparison code to use. We know that EXP is a comparison
12410 operation of some type. Some comparisons against 1 and -1 can be
12411 converted to comparisons with zero. Do so here so that the tests
12412 below will be aware that we have a comparison with zero. These
12413 tests will not catch constants in the first operand, but constants
12414 are rarely passed as the first operand. */
12416 switch (ops->code)
12418 case EQ_EXPR:
12419 code = EQ;
12420 break;
12421 case NE_EXPR:
12422 code = NE;
12423 break;
12424 case LT_EXPR:
12425 if (integer_onep (arg1))
12426 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
12427 else
12428 code = unsignedp ? LTU : LT;
12429 break;
12430 case LE_EXPR:
12431 if (! unsignedp && integer_all_onesp (arg1))
12432 arg1 = integer_zero_node, code = LT;
12433 else
12434 code = unsignedp ? LEU : LE;
12435 break;
12436 case GT_EXPR:
12437 if (! unsignedp && integer_all_onesp (arg1))
12438 arg1 = integer_zero_node, code = GE;
12439 else
12440 code = unsignedp ? GTU : GT;
12441 break;
12442 case GE_EXPR:
12443 if (integer_onep (arg1))
12444 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
12445 else
12446 code = unsignedp ? GEU : GE;
12447 break;
12449 case UNORDERED_EXPR:
12450 code = UNORDERED;
12451 break;
12452 case ORDERED_EXPR:
12453 code = ORDERED;
12454 break;
12455 case UNLT_EXPR:
12456 code = UNLT;
12457 break;
12458 case UNLE_EXPR:
12459 code = UNLE;
12460 break;
12461 case UNGT_EXPR:
12462 code = UNGT;
12463 break;
12464 case UNGE_EXPR:
12465 code = UNGE;
12466 break;
12467 case UNEQ_EXPR:
12468 code = UNEQ;
12469 break;
12470 case LTGT_EXPR:
12471 code = LTGT;
12472 break;
12474 default:
12475 gcc_unreachable ();
12478 /* Put a constant second. */
12479 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
12480 || TREE_CODE (arg0) == FIXED_CST)
12482 std::swap (arg0, arg1);
12483 code = swap_condition (code);
12486 /* If this is an equality or inequality test of a single bit, we can
12487 do this by shifting the bit being tested to the low-order bit and
12488 masking the result with the constant 1. If the condition was EQ,
12489 we xor it with 1. This does not require an scc insn and is faster
12490 than an scc insn even if we have it.
12492 The code to make this transformation was moved into fold_single_bit_test,
12493 so we just call into the folder and expand its result. */
12495 if ((code == NE || code == EQ)
12496 && integer_zerop (arg1)
12497 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
12499 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
12500 if (srcstmt
12501 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
12503 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
12504 type = lang_hooks.types.type_for_mode (mode, unsignedp);
12505 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
12506 gimple_assign_rhs1 (srcstmt),
12507 gimple_assign_rhs2 (srcstmt));
12508 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
12509 if (temp)
12510 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
12514 if (! get_subtarget (target)
12515 || GET_MODE (subtarget) != operand_mode)
12516 subtarget = 0;
12518 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
12520 if (target == 0)
12521 target = gen_reg_rtx (mode);
12523 /* Try a cstore if possible. */
12524 return emit_store_flag_force (target, code, op0, op1,
12525 operand_mode, unsignedp,
12526 (TYPE_PRECISION (ops->type) == 1
12527 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
12530 /* Attempt to generate a casesi instruction. Returns 1 if successful,
12531 0 otherwise (i.e. if there is no casesi instruction).
12533 DEFAULT_PROBABILITY is the probability of jumping to the default
12534 label. */
12536 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
12537 rtx table_label, rtx default_label, rtx fallback_label,
12538 profile_probability default_probability)
12540 class expand_operand ops[5];
12541 scalar_int_mode index_mode = SImode;
12542 rtx op1, op2, index;
12544 if (! targetm.have_casesi ())
12545 return 0;
12547 /* The index must be some form of integer. Convert it to SImode. */
12548 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
12549 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
12551 rtx rangertx = expand_normal (range);
12553 /* We must handle the endpoints in the original mode. */
12554 index_expr = build2 (MINUS_EXPR, index_type,
12555 index_expr, minval);
12556 minval = integer_zero_node;
12557 index = expand_normal (index_expr);
12558 if (default_label)
12559 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
12560 omode, 1, default_label,
12561 default_probability);
12562 /* Now we can safely truncate. */
12563 index = convert_to_mode (index_mode, index, 0);
12565 else
12567 if (omode != index_mode)
12569 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
12570 index_expr = fold_convert (index_type, index_expr);
12573 index = expand_normal (index_expr);
12576 do_pending_stack_adjust ();
12578 op1 = expand_normal (minval);
12579 op2 = expand_normal (range);
12581 create_input_operand (&ops[0], index, index_mode);
12582 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
12583 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
12584 create_fixed_operand (&ops[3], table_label);
12585 create_fixed_operand (&ops[4], (default_label
12586 ? default_label
12587 : fallback_label));
12588 expand_jump_insn (targetm.code_for_casesi, 5, ops);
12589 return 1;
12592 /* Attempt to generate a tablejump instruction; same concept. */
12593 /* Subroutine of the next function.
12595 INDEX is the value being switched on, with the lowest value
12596 in the table already subtracted.
12597 MODE is its expected mode (needed if INDEX is constant).
12598 RANGE is the length of the jump table.
12599 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
12601 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
12602 index value is out of range.
12603 DEFAULT_PROBABILITY is the probability of jumping to
12604 the default label. */
12606 static void
12607 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
12608 rtx default_label, profile_probability default_probability)
12610 rtx temp, vector;
12612 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
12613 cfun->cfg->max_jumptable_ents = INTVAL (range);
12615 /* Do an unsigned comparison (in the proper mode) between the index
12616 expression and the value which represents the length of the range.
12617 Since we just finished subtracting the lower bound of the range
12618 from the index expression, this comparison allows us to simultaneously
12619 check that the original index expression value is both greater than
12620 or equal to the minimum value of the range and less than or equal to
12621 the maximum value of the range. */
12623 if (default_label)
12624 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
12625 default_label, default_probability);
12627 /* If index is in range, it must fit in Pmode.
12628 Convert to Pmode so we can index with it. */
12629 if (mode != Pmode)
12631 unsigned int width;
12633 /* We know the value of INDEX is between 0 and RANGE. If we have a
12634 sign-extended subreg, and RANGE does not have the sign bit set, then
12635 we have a value that is valid for both sign and zero extension. In
12636 this case, we get better code if we sign extend. */
12637 if (GET_CODE (index) == SUBREG
12638 && SUBREG_PROMOTED_VAR_P (index)
12639 && SUBREG_PROMOTED_SIGNED_P (index)
12640 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
12641 <= HOST_BITS_PER_WIDE_INT)
12642 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
12643 index = convert_to_mode (Pmode, index, 0);
12644 else
12645 index = convert_to_mode (Pmode, index, 1);
12648 /* Don't let a MEM slip through, because then INDEX that comes
12649 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
12650 and break_out_memory_refs will go to work on it and mess it up. */
12651 #ifdef PIC_CASE_VECTOR_ADDRESS
12652 if (flag_pic && !REG_P (index))
12653 index = copy_to_mode_reg (Pmode, index);
12654 #endif
12656 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
12657 GET_MODE_SIZE, because this indicates how large insns are. The other
12658 uses should all be Pmode, because they are addresses. This code
12659 could fail if addresses and insns are not the same size. */
12660 index = simplify_gen_binary (MULT, Pmode, index,
12661 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
12662 Pmode));
12663 index = simplify_gen_binary (PLUS, Pmode, index,
12664 gen_rtx_LABEL_REF (Pmode, table_label));
12666 #ifdef PIC_CASE_VECTOR_ADDRESS
12667 if (flag_pic)
12668 index = PIC_CASE_VECTOR_ADDRESS (index);
12669 else
12670 #endif
12671 index = memory_address (CASE_VECTOR_MODE, index);
12672 temp = gen_reg_rtx (CASE_VECTOR_MODE);
12673 vector = gen_const_mem (CASE_VECTOR_MODE, index);
12674 convert_move (temp, vector, 0);
12676 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
12678 /* If we are generating PIC code or if the table is PC-relative, the
12679 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
12680 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
12681 emit_barrier ();
12685 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
12686 rtx table_label, rtx default_label,
12687 profile_probability default_probability)
12689 rtx index;
12691 if (! targetm.have_tablejump ())
12692 return 0;
12694 index_expr = fold_build2 (MINUS_EXPR, index_type,
12695 fold_convert (index_type, index_expr),
12696 fold_convert (index_type, minval));
12697 index = expand_normal (index_expr);
12698 do_pending_stack_adjust ();
12700 do_tablejump (index, TYPE_MODE (index_type),
12701 convert_modes (TYPE_MODE (index_type),
12702 TYPE_MODE (TREE_TYPE (range)),
12703 expand_normal (range),
12704 TYPE_UNSIGNED (TREE_TYPE (range))),
12705 table_label, default_label, default_probability);
12706 return 1;
12709 /* Return a CONST_VECTOR rtx representing vector mask for
12710 a VECTOR_CST of booleans. */
12711 static rtx
12712 const_vector_mask_from_tree (tree exp)
12714 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12715 machine_mode inner = GET_MODE_INNER (mode);
12717 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12718 VECTOR_CST_NELTS_PER_PATTERN (exp));
12719 unsigned int count = builder.encoded_nelts ();
12720 for (unsigned int i = 0; i < count; ++i)
12722 tree elt = VECTOR_CST_ELT (exp, i);
12723 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
12724 if (integer_zerop (elt))
12725 builder.quick_push (CONST0_RTX (inner));
12726 else if (integer_onep (elt)
12727 || integer_minus_onep (elt))
12728 builder.quick_push (CONSTM1_RTX (inner));
12729 else
12730 gcc_unreachable ();
12732 return builder.build ();
12735 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
12736 static rtx
12737 const_vector_from_tree (tree exp)
12739 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
12741 if (initializer_zerop (exp))
12742 return CONST0_RTX (mode);
12744 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
12745 return const_vector_mask_from_tree (exp);
12747 machine_mode inner = GET_MODE_INNER (mode);
12749 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
12750 VECTOR_CST_NELTS_PER_PATTERN (exp));
12751 unsigned int count = builder.encoded_nelts ();
12752 for (unsigned int i = 0; i < count; ++i)
12754 tree elt = VECTOR_CST_ELT (exp, i);
12755 if (TREE_CODE (elt) == REAL_CST)
12756 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
12757 inner));
12758 else if (TREE_CODE (elt) == FIXED_CST)
12759 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
12760 inner));
12761 else
12762 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
12763 inner));
12765 return builder.build ();
12768 /* Build a decl for a personality function given a language prefix. */
12770 tree
12771 build_personality_function (const char *lang)
12773 const char *unwind_and_version;
12774 tree decl, type;
12775 char *name;
12777 switch (targetm_common.except_unwind_info (&global_options))
12779 case UI_NONE:
12780 return NULL;
12781 case UI_SJLJ:
12782 unwind_and_version = "_sj0";
12783 break;
12784 case UI_DWARF2:
12785 case UI_TARGET:
12786 unwind_and_version = "_v0";
12787 break;
12788 case UI_SEH:
12789 unwind_and_version = "_seh0";
12790 break;
12791 default:
12792 gcc_unreachable ();
12795 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
12797 type = build_function_type_list (unsigned_type_node,
12798 integer_type_node, integer_type_node,
12799 long_long_unsigned_type_node,
12800 ptr_type_node, ptr_type_node, NULL_TREE);
12801 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
12802 get_identifier (name), type);
12803 DECL_ARTIFICIAL (decl) = 1;
12804 DECL_EXTERNAL (decl) = 1;
12805 TREE_PUBLIC (decl) = 1;
12807 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
12808 are the flags assigned by targetm.encode_section_info. */
12809 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
12811 return decl;
12814 /* Extracts the personality function of DECL and returns the corresponding
12815 libfunc. */
12818 get_personality_function (tree decl)
12820 tree personality = DECL_FUNCTION_PERSONALITY (decl);
12821 enum eh_personality_kind pk;
12823 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
12824 if (pk == eh_personality_none)
12825 return NULL;
12827 if (!personality
12828 && pk == eh_personality_any)
12829 personality = lang_hooks.eh_personality ();
12831 if (pk == eh_personality_lang)
12832 gcc_assert (personality != NULL_TREE);
12834 return XEXP (DECL_RTL (personality), 0);
12837 /* Returns a tree for the size of EXP in bytes. */
12839 static tree
12840 tree_expr_size (const_tree exp)
12842 if (DECL_P (exp)
12843 && DECL_SIZE_UNIT (exp) != 0)
12844 return DECL_SIZE_UNIT (exp);
12845 else
12846 return size_in_bytes (TREE_TYPE (exp));
12849 /* Return an rtx for the size in bytes of the value of EXP. */
12852 expr_size (tree exp)
12854 tree size;
12856 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
12857 size = TREE_OPERAND (exp, 1);
12858 else
12860 size = tree_expr_size (exp);
12861 gcc_assert (size);
12862 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
12865 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
12868 /* Return a wide integer for the size in bytes of the value of EXP, or -1
12869 if the size can vary or is larger than an integer. */
12871 static HOST_WIDE_INT
12872 int_expr_size (tree exp)
12874 tree size;
12876 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
12877 size = TREE_OPERAND (exp, 1);
12878 else
12880 size = tree_expr_size (exp);
12881 gcc_assert (size);
12884 if (size == 0 || !tree_fits_shwi_p (size))
12885 return -1;
12887 return tree_to_shwi (size);