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