PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / expr.c
blob797b1f5d794a2849b3bc3a18b0af1f732430a2fd
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "ssa.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "cgraph.h"
38 #include "diagnostic.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "attribs.h"
43 #include "varasm.h"
44 #include "except.h"
45 #include "insn-attr.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "stmt.h"
50 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
51 #include "expr.h"
52 #include "optabs-tree.h"
53 #include "libfuncs.h"
54 #include "reload.h"
55 #include "langhooks.h"
56 #include "common/common-target.h"
57 #include "tree-dfa.h"
58 #include "tree-ssa-live.h"
59 #include "tree-outof-ssa.h"
60 #include "tree-ssa-address.h"
61 #include "builtins.h"
62 #include "ccmp.h"
63 #include "gimple-fold.h"
64 #include "rtx-vector-builder.h"
67 /* If this is nonzero, we do not bother generating VOLATILE
68 around volatile memory references, and we are willing to
69 output indirect addresses. If cse is to follow, we reject
70 indirect addresses so a useful potential cse is generated;
71 if it is used only once, instruction combination will produce
72 the same indirect address eventually. */
73 int cse_not_expected;
75 static bool block_move_libcall_safe_for_call_parm (void);
76 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
78 unsigned HOST_WIDE_INT);
79 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
80 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
81 static rtx_insn *compress_float_constant (rtx, rtx);
82 static rtx get_subtarget (rtx);
83 static void store_constructor (tree, rtx, int, poly_int64, bool);
84 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
85 machine_mode, tree, alias_set_type, bool, bool);
87 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
89 static int is_aligning_offset (const_tree, const_tree);
90 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
91 static rtx do_store_flag (sepops, rtx, machine_mode);
92 #ifdef PUSH_ROUNDING
93 static void emit_single_push_insn (machine_mode, rtx, tree);
94 #endif
95 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
96 profile_probability);
97 static rtx const_vector_from_tree (tree);
98 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
99 static tree tree_expr_size (const_tree);
100 static HOST_WIDE_INT int_expr_size (tree);
101 static void convert_mode_scalar (rtx, rtx, int);
104 /* This is run to set up which modes can be used
105 directly in memory and to initialize the block move optab. It is run
106 at the beginning of compilation and when the target is reinitialized. */
108 void
109 init_expr_target (void)
111 rtx pat;
112 int num_clobbers;
113 rtx mem, mem1;
114 rtx reg;
116 /* Try indexing by frame ptr and try by stack ptr.
117 It is known that on the Convex the stack ptr isn't a valid index.
118 With luck, one or the other is valid on any machine. */
119 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
120 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
122 /* A scratch register we can modify in-place below to avoid
123 useless RTL allocations. */
124 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
126 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
127 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
128 PATTERN (insn) = pat;
130 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
131 mode = (machine_mode) ((int) mode + 1))
133 int regno;
135 direct_load[(int) mode] = direct_store[(int) mode] = 0;
136 PUT_MODE (mem, mode);
137 PUT_MODE (mem1, mode);
139 /* See if there is some register that can be used in this mode and
140 directly loaded or stored from memory. */
142 if (mode != VOIDmode && mode != BLKmode)
143 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
144 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
145 regno++)
147 if (!targetm.hard_regno_mode_ok (regno, mode))
148 continue;
150 set_mode_and_regno (reg, mode, regno);
152 SET_SRC (pat) = mem;
153 SET_DEST (pat) = reg;
154 if (recog (pat, insn, &num_clobbers) >= 0)
155 direct_load[(int) mode] = 1;
157 SET_SRC (pat) = mem1;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
162 SET_SRC (pat) = reg;
163 SET_DEST (pat) = mem;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_store[(int) mode] = 1;
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem1;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
174 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
176 opt_scalar_float_mode mode_iter;
177 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
179 scalar_float_mode mode = mode_iter.require ();
180 scalar_float_mode srcmode;
181 FOR_EACH_MODE_UNTIL (srcmode, mode)
183 enum insn_code ic;
185 ic = can_extend_p (mode, srcmode, 0);
186 if (ic == CODE_FOR_nothing)
187 continue;
189 PUT_MODE (mem, srcmode);
191 if (insn_operand_matches (ic, 1, mem))
192 float_extend_from_mem[mode][srcmode] = true;
197 /* This is run at the start of compiling a function. */
199 void
200 init_expr (void)
202 memset (&crtl->expr, 0, sizeof (crtl->expr));
205 /* Copy data from FROM to TO, where the machine modes are not the same.
206 Both modes may be integer, or both may be floating, or both may be
207 fixed-point.
208 UNSIGNEDP should be nonzero if FROM is an unsigned type.
209 This causes zero-extension instead of sign-extension. */
211 void
212 convert_move (rtx to, rtx from, int unsignedp)
214 machine_mode to_mode = GET_MODE (to);
215 machine_mode from_mode = GET_MODE (from);
217 gcc_assert (to_mode != BLKmode);
218 gcc_assert (from_mode != BLKmode);
220 /* If the source and destination are already the same, then there's
221 nothing to do. */
222 if (to == from)
223 return;
225 /* If FROM is a SUBREG that indicates that we have already done at least
226 the required extension, strip it. We don't handle such SUBREGs as
227 TO here. */
229 scalar_int_mode to_int_mode;
230 if (GET_CODE (from) == SUBREG
231 && SUBREG_PROMOTED_VAR_P (from)
232 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
233 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
234 >= GET_MODE_PRECISION (to_int_mode))
235 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
237 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
238 from_mode = to_int_mode;
241 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
243 if (to_mode == from_mode
244 || (from_mode == VOIDmode && CONSTANT_P (from)))
246 emit_move_insn (to, from);
247 return;
250 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
252 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
253 GET_MODE_BITSIZE (to_mode)));
255 if (VECTOR_MODE_P (to_mode))
256 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
257 else
258 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
260 emit_move_insn (to, from);
261 return;
264 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
266 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
267 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
268 return;
271 convert_mode_scalar (to, from, unsignedp);
274 /* Like convert_move, but deals only with scalar modes. */
276 static void
277 convert_mode_scalar (rtx to, rtx from, int unsignedp)
279 /* Both modes should be scalar types. */
280 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
281 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
282 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
283 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
284 enum insn_code code;
285 rtx libcall;
287 gcc_assert (to_real == from_real);
289 /* rtx code for making an equivalent value. */
290 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
291 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
293 if (to_real)
295 rtx value;
296 rtx_insn *insns;
297 convert_optab tab;
299 gcc_assert ((GET_MODE_PRECISION (from_mode)
300 != GET_MODE_PRECISION (to_mode))
301 || (DECIMAL_FLOAT_MODE_P (from_mode)
302 != DECIMAL_FLOAT_MODE_P (to_mode)));
304 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
305 /* Conversion between decimal float and binary float, same size. */
306 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
307 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
308 tab = sext_optab;
309 else
310 tab = trunc_optab;
312 /* Try converting directly if the insn is supported. */
314 code = convert_optab_handler (tab, to_mode, from_mode);
315 if (code != CODE_FOR_nothing)
317 emit_unop_insn (code, to, from,
318 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
319 return;
322 /* Otherwise use a libcall. */
323 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
325 /* Is this conversion implemented yet? */
326 gcc_assert (libcall);
328 start_sequence ();
329 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
330 from, from_mode);
331 insns = get_insns ();
332 end_sequence ();
333 emit_libcall_block (insns, to, value,
334 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
335 from)
336 : gen_rtx_FLOAT_EXTEND (to_mode, from));
337 return;
340 /* Handle pointer conversion. */ /* SPEE 900220. */
341 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
343 convert_optab ctab;
345 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
346 ctab = trunc_optab;
347 else if (unsignedp)
348 ctab = zext_optab;
349 else
350 ctab = sext_optab;
352 if (convert_optab_handler (ctab, to_mode, from_mode)
353 != CODE_FOR_nothing)
355 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
356 to, from, UNKNOWN);
357 return;
361 /* Targets are expected to provide conversion insns between PxImode and
362 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
363 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
365 scalar_int_mode full_mode
366 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
368 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
369 != CODE_FOR_nothing);
371 if (full_mode != from_mode)
372 from = convert_to_mode (full_mode, from, unsignedp);
373 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
374 to, from, UNKNOWN);
375 return;
377 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
379 rtx new_from;
380 scalar_int_mode full_mode
381 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
382 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
383 enum insn_code icode;
385 icode = convert_optab_handler (ctab, full_mode, from_mode);
386 gcc_assert (icode != CODE_FOR_nothing);
388 if (to_mode == full_mode)
390 emit_unop_insn (icode, to, from, UNKNOWN);
391 return;
394 new_from = gen_reg_rtx (full_mode);
395 emit_unop_insn (icode, new_from, from, UNKNOWN);
397 /* else proceed to integer conversions below. */
398 from_mode = full_mode;
399 from = new_from;
402 /* Make sure both are fixed-point modes or both are not. */
403 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
404 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
405 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
407 /* If we widen from_mode to to_mode and they are in the same class,
408 we won't saturate the result.
409 Otherwise, always saturate the result to play safe. */
410 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
411 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
412 expand_fixed_convert (to, from, 0, 0);
413 else
414 expand_fixed_convert (to, from, 0, 1);
415 return;
418 /* Now both modes are integers. */
420 /* Handle expanding beyond a word. */
421 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
422 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
424 rtx_insn *insns;
425 rtx lowpart;
426 rtx fill_value;
427 rtx lowfrom;
428 int i;
429 scalar_mode lowpart_mode;
430 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
432 /* Try converting directly if the insn is supported. */
433 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
434 != CODE_FOR_nothing)
436 /* If FROM is a SUBREG, put it into a register. Do this
437 so that we always generate the same set of insns for
438 better cse'ing; if an intermediate assignment occurred,
439 we won't be doing the operation directly on the SUBREG. */
440 if (optimize > 0 && GET_CODE (from) == SUBREG)
441 from = force_reg (from_mode, from);
442 emit_unop_insn (code, to, from, equiv_code);
443 return;
445 /* Next, try converting via full word. */
446 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
447 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
448 != CODE_FOR_nothing))
450 rtx word_to = gen_reg_rtx (word_mode);
451 if (REG_P (to))
453 if (reg_overlap_mentioned_p (to, from))
454 from = force_reg (from_mode, from);
455 emit_clobber (to);
457 convert_move (word_to, from, unsignedp);
458 emit_unop_insn (code, to, word_to, equiv_code);
459 return;
462 /* No special multiword conversion insn; do it by hand. */
463 start_sequence ();
465 /* Since we will turn this into a no conflict block, we must ensure
466 the source does not overlap the target so force it into an isolated
467 register when maybe so. Likewise for any MEM input, since the
468 conversion sequence might require several references to it and we
469 must ensure we're getting the same value every time. */
471 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
472 from = force_reg (from_mode, from);
474 /* Get a copy of FROM widened to a word, if necessary. */
475 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
476 lowpart_mode = word_mode;
477 else
478 lowpart_mode = from_mode;
480 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
482 lowpart = gen_lowpart (lowpart_mode, to);
483 emit_move_insn (lowpart, lowfrom);
485 /* Compute the value to put in each remaining word. */
486 if (unsignedp)
487 fill_value = const0_rtx;
488 else
489 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
490 LT, lowfrom, const0_rtx,
491 lowpart_mode, 0, -1);
493 /* Fill the remaining words. */
494 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
496 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
497 rtx subword = operand_subword (to, index, 1, to_mode);
499 gcc_assert (subword);
501 if (fill_value != subword)
502 emit_move_insn (subword, fill_value);
505 insns = get_insns ();
506 end_sequence ();
508 emit_insn (insns);
509 return;
512 /* Truncating multi-word to a word or less. */
513 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
514 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
516 if (!((MEM_P (from)
517 && ! MEM_VOLATILE_P (from)
518 && direct_load[(int) to_mode]
519 && ! mode_dependent_address_p (XEXP (from, 0),
520 MEM_ADDR_SPACE (from)))
521 || REG_P (from)
522 || GET_CODE (from) == SUBREG))
523 from = force_reg (from_mode, from);
524 convert_move (to, gen_lowpart (word_mode, from), 0);
525 return;
528 /* Now follow all the conversions between integers
529 no more than a word long. */
531 /* For truncation, usually we can just refer to FROM in a narrower mode. */
532 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
533 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
535 if (!((MEM_P (from)
536 && ! MEM_VOLATILE_P (from)
537 && direct_load[(int) to_mode]
538 && ! mode_dependent_address_p (XEXP (from, 0),
539 MEM_ADDR_SPACE (from)))
540 || REG_P (from)
541 || GET_CODE (from) == SUBREG))
542 from = force_reg (from_mode, from);
543 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
544 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
545 from = copy_to_reg (from);
546 emit_move_insn (to, gen_lowpart (to_mode, from));
547 return;
550 /* Handle extension. */
551 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
553 /* Convert directly if that works. */
554 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
555 != CODE_FOR_nothing)
557 emit_unop_insn (code, to, from, equiv_code);
558 return;
560 else
562 scalar_mode intermediate;
563 rtx tmp;
564 int shift_amount;
566 /* Search for a mode to convert via. */
567 opt_scalar_mode intermediate_iter;
568 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
570 scalar_mode intermediate = intermediate_iter.require ();
571 if (((can_extend_p (to_mode, intermediate, unsignedp)
572 != CODE_FOR_nothing)
573 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
574 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
575 intermediate)))
576 && (can_extend_p (intermediate, from_mode, unsignedp)
577 != CODE_FOR_nothing))
579 convert_move (to, convert_to_mode (intermediate, from,
580 unsignedp), unsignedp);
581 return;
585 /* No suitable intermediate mode.
586 Generate what we need with shifts. */
587 shift_amount = (GET_MODE_PRECISION (to_mode)
588 - GET_MODE_PRECISION (from_mode));
589 from = gen_lowpart (to_mode, force_reg (from_mode, from));
590 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
591 to, unsignedp);
592 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
593 to, unsignedp);
594 if (tmp != to)
595 emit_move_insn (to, tmp);
596 return;
600 /* Support special truncate insns for certain modes. */
601 if (convert_optab_handler (trunc_optab, to_mode,
602 from_mode) != CODE_FOR_nothing)
604 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
605 to, from, UNKNOWN);
606 return;
609 /* Handle truncation of volatile memrefs, and so on;
610 the things that couldn't be truncated directly,
611 and for which there was no special instruction.
613 ??? Code above formerly short-circuited this, for most integer
614 mode pairs, with a force_reg in from_mode followed by a recursive
615 call to this routine. Appears always to have been wrong. */
616 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
618 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
619 emit_move_insn (to, temp);
620 return;
623 /* Mode combination is not recognized. */
624 gcc_unreachable ();
627 /* Return an rtx for a value that would result
628 from converting X to mode MODE.
629 Both X and MODE may be floating, or both integer.
630 UNSIGNEDP is nonzero if X is an unsigned value.
631 This can be done by referring to a part of X in place
632 or by copying to a new temporary with conversion. */
635 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
637 return convert_modes (mode, VOIDmode, x, unsignedp);
640 /* Return an rtx for a value that would result
641 from converting X from mode OLDMODE to mode MODE.
642 Both modes may be floating, or both integer.
643 UNSIGNEDP is nonzero if X is an unsigned value.
645 This can be done by referring to a part of X in place
646 or by copying to a new temporary with conversion.
648 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
651 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
653 rtx temp;
654 scalar_int_mode int_mode;
656 /* If FROM is a SUBREG that indicates that we have already done at least
657 the required extension, strip it. */
659 if (GET_CODE (x) == SUBREG
660 && SUBREG_PROMOTED_VAR_P (x)
661 && is_a <scalar_int_mode> (mode, &int_mode)
662 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
663 >= GET_MODE_PRECISION (int_mode))
664 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
665 x = gen_lowpart (int_mode, SUBREG_REG (x));
667 if (GET_MODE (x) != VOIDmode)
668 oldmode = GET_MODE (x);
670 if (mode == oldmode)
671 return x;
673 if (CONST_SCALAR_INT_P (x)
674 && is_int_mode (mode, &int_mode))
676 /* If the caller did not tell us the old mode, then there is not
677 much to do with respect to canonicalization. We have to
678 assume that all the bits are significant. */
679 if (GET_MODE_CLASS (oldmode) != MODE_INT)
680 oldmode = MAX_MODE_INT;
681 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
682 GET_MODE_PRECISION (int_mode),
683 unsignedp ? UNSIGNED : SIGNED);
684 return immed_wide_int_const (w, int_mode);
687 /* We can do this with a gen_lowpart if both desired and current modes
688 are integer, and this is either a constant integer, a register, or a
689 non-volatile MEM. */
690 scalar_int_mode int_oldmode;
691 if (is_int_mode (mode, &int_mode)
692 && is_int_mode (oldmode, &int_oldmode)
693 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
694 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
695 || CONST_POLY_INT_P (x)
696 || (REG_P (x)
697 && (!HARD_REGISTER_P (x)
698 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
699 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
700 return gen_lowpart (int_mode, x);
702 /* Converting from integer constant into mode is always equivalent to an
703 subreg operation. */
704 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
706 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
707 GET_MODE_BITSIZE (oldmode)));
708 return simplify_gen_subreg (mode, x, oldmode, 0);
711 temp = gen_reg_rtx (mode);
712 convert_move (temp, x, unsignedp);
713 return temp;
716 /* Return the largest alignment we can use for doing a move (or store)
717 of MAX_PIECES. ALIGN is the largest alignment we could use. */
719 static unsigned int
720 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
722 scalar_int_mode tmode
723 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
725 if (align >= GET_MODE_ALIGNMENT (tmode))
726 align = GET_MODE_ALIGNMENT (tmode);
727 else
729 scalar_int_mode xmode = NARROWEST_INT_MODE;
730 opt_scalar_int_mode mode_iter;
731 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
733 tmode = mode_iter.require ();
734 if (GET_MODE_SIZE (tmode) > max_pieces
735 || targetm.slow_unaligned_access (tmode, align))
736 break;
737 xmode = tmode;
740 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
743 return align;
746 /* Return the widest integer mode that is narrower than SIZE bytes. */
748 static scalar_int_mode
749 widest_int_mode_for_size (unsigned int size)
751 scalar_int_mode result = NARROWEST_INT_MODE;
753 gcc_checking_assert (size > 1);
755 opt_scalar_int_mode tmode;
756 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
757 if (GET_MODE_SIZE (tmode.require ()) < size)
758 result = tmode.require ();
760 return result;
763 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
764 and should be performed piecewise. */
766 static bool
767 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
768 enum by_pieces_operation op)
770 return targetm.use_by_pieces_infrastructure_p (len, align, op,
771 optimize_insn_for_speed_p ());
774 /* Determine whether the LEN bytes can be moved by using several move
775 instructions. Return nonzero if a call to move_by_pieces should
776 succeed. */
778 bool
779 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
781 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
784 /* Return number of insns required to perform operation OP by pieces
785 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
787 unsigned HOST_WIDE_INT
788 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
789 unsigned int max_size, by_pieces_operation op)
791 unsigned HOST_WIDE_INT n_insns = 0;
793 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
795 while (max_size > 1 && l > 0)
797 scalar_int_mode mode = widest_int_mode_for_size (max_size);
798 enum insn_code icode;
800 unsigned int modesize = GET_MODE_SIZE (mode);
802 icode = optab_handler (mov_optab, mode);
803 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
805 unsigned HOST_WIDE_INT n_pieces = l / modesize;
806 l %= modesize;
807 switch (op)
809 default:
810 n_insns += n_pieces;
811 break;
813 case COMPARE_BY_PIECES:
814 int batch = targetm.compare_by_pieces_branch_ratio (mode);
815 int batch_ops = 4 * batch - 1;
816 unsigned HOST_WIDE_INT full = n_pieces / batch;
817 n_insns += full * batch_ops;
818 if (n_pieces % batch != 0)
819 n_insns++;
820 break;
824 max_size = modesize;
827 gcc_assert (!l);
828 return n_insns;
831 /* Used when performing piecewise block operations, holds information
832 about one of the memory objects involved. The member functions
833 can be used to generate code for loading from the object and
834 updating the address when iterating. */
836 class pieces_addr
838 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
839 stack pushes. */
840 rtx m_obj;
841 /* The address of the object. Can differ from that seen in the
842 MEM rtx if we copied the address to a register. */
843 rtx m_addr;
844 /* Nonzero if the address on the object has an autoincrement already,
845 signifies whether that was an increment or decrement. */
846 signed char m_addr_inc;
847 /* Nonzero if we intend to use autoinc without the address already
848 having autoinc form. We will insert add insns around each memory
849 reference, expecting later passes to form autoinc addressing modes.
850 The only supported options are predecrement and postincrement. */
851 signed char m_explicit_inc;
852 /* True if we have either of the two possible cases of using
853 autoincrement. */
854 bool m_auto;
855 /* True if this is an address to be used for load operations rather
856 than stores. */
857 bool m_is_load;
859 /* Optionally, a function to obtain constants for any given offset into
860 the objects, and data associated with it. */
861 by_pieces_constfn m_constfn;
862 void *m_cfndata;
863 public:
864 pieces_addr (rtx, bool, by_pieces_constfn, void *);
865 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
866 void increment_address (HOST_WIDE_INT);
867 void maybe_predec (HOST_WIDE_INT);
868 void maybe_postinc (HOST_WIDE_INT);
869 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
870 int get_addr_inc ()
872 return m_addr_inc;
876 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
877 true if the operation to be performed on this object is a load
878 rather than a store. For stores, OBJ can be NULL, in which case we
879 assume the operation is a stack push. For loads, the optional
880 CONSTFN and its associated CFNDATA can be used in place of the
881 memory load. */
883 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
884 void *cfndata)
885 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
887 m_addr_inc = 0;
888 m_auto = false;
889 if (obj)
891 rtx addr = XEXP (obj, 0);
892 rtx_code code = GET_CODE (addr);
893 m_addr = addr;
894 bool dec = code == PRE_DEC || code == POST_DEC;
895 bool inc = code == PRE_INC || code == POST_INC;
896 m_auto = inc || dec;
897 if (m_auto)
898 m_addr_inc = dec ? -1 : 1;
900 /* While we have always looked for these codes here, the code
901 implementing the memory operation has never handled them.
902 Support could be added later if necessary or beneficial. */
903 gcc_assert (code != PRE_INC && code != POST_DEC);
905 else
907 m_addr = NULL_RTX;
908 if (!is_load)
910 m_auto = true;
911 if (STACK_GROWS_DOWNWARD)
912 m_addr_inc = -1;
913 else
914 m_addr_inc = 1;
916 else
917 gcc_assert (constfn != NULL);
919 m_explicit_inc = 0;
920 if (constfn)
921 gcc_assert (is_load);
924 /* Decide whether to use autoinc for an address involved in a memory op.
925 MODE is the mode of the accesses, REVERSE is true if we've decided to
926 perform the operation starting from the end, and LEN is the length of
927 the operation. Don't override an earlier decision to set m_auto. */
929 void
930 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
931 HOST_WIDE_INT len)
933 if (m_auto || m_obj == NULL_RTX)
934 return;
936 bool use_predec = (m_is_load
937 ? USE_LOAD_PRE_DECREMENT (mode)
938 : USE_STORE_PRE_DECREMENT (mode));
939 bool use_postinc = (m_is_load
940 ? USE_LOAD_POST_INCREMENT (mode)
941 : USE_STORE_POST_INCREMENT (mode));
942 machine_mode addr_mode = get_address_mode (m_obj);
944 if (use_predec && reverse)
946 m_addr = copy_to_mode_reg (addr_mode,
947 plus_constant (addr_mode,
948 m_addr, len));
949 m_auto = true;
950 m_explicit_inc = -1;
952 else if (use_postinc && !reverse)
954 m_addr = copy_to_mode_reg (addr_mode, m_addr);
955 m_auto = true;
956 m_explicit_inc = 1;
958 else if (CONSTANT_P (m_addr))
959 m_addr = copy_to_mode_reg (addr_mode, m_addr);
962 /* Adjust the address to refer to the data at OFFSET in MODE. If we
963 are using autoincrement for this address, we don't add the offset,
964 but we still modify the MEM's properties. */
967 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
969 if (m_constfn)
970 return m_constfn (m_cfndata, offset, mode);
971 if (m_obj == NULL_RTX)
972 return NULL_RTX;
973 if (m_auto)
974 return adjust_automodify_address (m_obj, mode, m_addr, offset);
975 else
976 return adjust_address (m_obj, mode, offset);
979 /* Emit an add instruction to increment the address by SIZE. */
981 void
982 pieces_addr::increment_address (HOST_WIDE_INT size)
984 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
985 emit_insn (gen_add2_insn (m_addr, amount));
988 /* If we are supposed to decrement the address after each access, emit code
989 to do so now. Increment by SIZE (which has should have the correct sign
990 already). */
992 void
993 pieces_addr::maybe_predec (HOST_WIDE_INT size)
995 if (m_explicit_inc >= 0)
996 return;
997 gcc_assert (HAVE_PRE_DECREMENT);
998 increment_address (size);
1001 /* If we are supposed to decrement the address after each access, emit code
1002 to do so now. Increment by SIZE. */
1004 void
1005 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1007 if (m_explicit_inc <= 0)
1008 return;
1009 gcc_assert (HAVE_POST_INCREMENT);
1010 increment_address (size);
1013 /* This structure is used by do_op_by_pieces to describe the operation
1014 to be performed. */
1016 class op_by_pieces_d
1018 protected:
1019 pieces_addr m_to, m_from;
1020 unsigned HOST_WIDE_INT m_len;
1021 HOST_WIDE_INT m_offset;
1022 unsigned int m_align;
1023 unsigned int m_max_size;
1024 bool m_reverse;
1026 /* Virtual functions, overriden by derived classes for the specific
1027 operation. */
1028 virtual void generate (rtx, rtx, machine_mode) = 0;
1029 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1030 virtual void finish_mode (machine_mode)
1034 public:
1035 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1036 unsigned HOST_WIDE_INT, unsigned int);
1037 void run ();
1040 /* The constructor for an op_by_pieces_d structure. We require two
1041 objects named TO and FROM, which are identified as loads or stores
1042 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1043 and its associated FROM_CFN_DATA can be used to replace loads with
1044 constant values. LEN describes the length of the operation. */
1046 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1047 rtx from, bool from_load,
1048 by_pieces_constfn from_cfn,
1049 void *from_cfn_data,
1050 unsigned HOST_WIDE_INT len,
1051 unsigned int align)
1052 : m_to (to, to_load, NULL, NULL),
1053 m_from (from, from_load, from_cfn, from_cfn_data),
1054 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1056 int toi = m_to.get_addr_inc ();
1057 int fromi = m_from.get_addr_inc ();
1058 if (toi >= 0 && fromi >= 0)
1059 m_reverse = false;
1060 else if (toi <= 0 && fromi <= 0)
1061 m_reverse = true;
1062 else
1063 gcc_unreachable ();
1065 m_offset = m_reverse ? len : 0;
1066 align = MIN (to ? MEM_ALIGN (to) : align,
1067 from ? MEM_ALIGN (from) : align);
1069 /* If copying requires more than two move insns,
1070 copy addresses to registers (to make displacements shorter)
1071 and use post-increment if available. */
1072 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1074 /* Find the mode of the largest comparison. */
1075 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1077 m_from.decide_autoinc (mode, m_reverse, len);
1078 m_to.decide_autoinc (mode, m_reverse, len);
1081 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1082 m_align = align;
1085 /* This function contains the main loop used for expanding a block
1086 operation. First move what we can in the largest integer mode,
1087 then go to successively smaller modes. For every access, call
1088 GENFUN with the two operands and the EXTRA_DATA. */
1090 void
1091 op_by_pieces_d::run ()
1093 while (m_max_size > 1 && m_len > 0)
1095 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1097 if (prepare_mode (mode, m_align))
1099 unsigned int size = GET_MODE_SIZE (mode);
1100 rtx to1 = NULL_RTX, from1;
1102 while (m_len >= size)
1104 if (m_reverse)
1105 m_offset -= size;
1107 to1 = m_to.adjust (mode, m_offset);
1108 from1 = m_from.adjust (mode, m_offset);
1110 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1111 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1113 generate (to1, from1, mode);
1115 m_to.maybe_postinc (size);
1116 m_from.maybe_postinc (size);
1118 if (!m_reverse)
1119 m_offset += size;
1121 m_len -= size;
1124 finish_mode (mode);
1127 m_max_size = GET_MODE_SIZE (mode);
1130 /* The code above should have handled everything. */
1131 gcc_assert (!m_len);
1134 /* Derived class from op_by_pieces_d, providing support for block move
1135 operations. */
1137 class move_by_pieces_d : public op_by_pieces_d
1139 insn_gen_fn m_gen_fun;
1140 void generate (rtx, rtx, machine_mode);
1141 bool prepare_mode (machine_mode, unsigned int);
1143 public:
1144 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1145 unsigned int align)
1146 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1149 rtx finish_endp (int);
1152 /* Return true if MODE can be used for a set of copies, given an
1153 alignment ALIGN. Prepare whatever data is necessary for later
1154 calls to generate. */
1156 bool
1157 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1159 insn_code icode = optab_handler (mov_optab, mode);
1160 m_gen_fun = GEN_FCN (icode);
1161 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1164 /* A callback used when iterating for a compare_by_pieces_operation.
1165 OP0 and OP1 are the values that have been loaded and should be
1166 compared in MODE. If OP0 is NULL, this means we should generate a
1167 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1168 gen function that should be used to generate the mode. */
1170 void
1171 move_by_pieces_d::generate (rtx op0, rtx op1,
1172 machine_mode mode ATTRIBUTE_UNUSED)
1174 #ifdef PUSH_ROUNDING
1175 if (op0 == NULL_RTX)
1177 emit_single_push_insn (mode, op1, NULL);
1178 return;
1180 #endif
1181 emit_insn (m_gen_fun (op0, op1));
1184 /* Perform the final adjustment at the end of a string to obtain the
1185 correct return value for the block operation. If ENDP is 1 return
1186 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1187 end minus one byte ala stpcpy. */
1190 move_by_pieces_d::finish_endp (int endp)
1192 gcc_assert (!m_reverse);
1193 if (endp == 2)
1195 m_to.maybe_postinc (-1);
1196 --m_offset;
1198 return m_to.adjust (QImode, m_offset);
1201 /* Generate several move instructions to copy LEN bytes from block FROM to
1202 block TO. (These are MEM rtx's with BLKmode).
1204 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1205 used to push FROM to the stack.
1207 ALIGN is maximum stack alignment we can assume.
1209 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1210 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1211 stpcpy. */
1214 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1215 unsigned int align, int endp)
1217 #ifndef PUSH_ROUNDING
1218 if (to == NULL)
1219 gcc_unreachable ();
1220 #endif
1222 move_by_pieces_d data (to, from, len, align);
1224 data.run ();
1226 if (endp)
1227 return data.finish_endp (endp);
1228 else
1229 return to;
1232 /* Derived class from op_by_pieces_d, providing support for block move
1233 operations. */
1235 class store_by_pieces_d : public op_by_pieces_d
1237 insn_gen_fn m_gen_fun;
1238 void generate (rtx, rtx, machine_mode);
1239 bool prepare_mode (machine_mode, unsigned int);
1241 public:
1242 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1243 unsigned HOST_WIDE_INT len, unsigned int align)
1244 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1247 rtx finish_endp (int);
1250 /* Return true if MODE can be used for a set of stores, given an
1251 alignment ALIGN. Prepare whatever data is necessary for later
1252 calls to generate. */
1254 bool
1255 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1257 insn_code icode = optab_handler (mov_optab, mode);
1258 m_gen_fun = GEN_FCN (icode);
1259 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1262 /* A callback used when iterating for a store_by_pieces_operation.
1263 OP0 and OP1 are the values that have been loaded and should be
1264 compared in MODE. If OP0 is NULL, this means we should generate a
1265 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1266 gen function that should be used to generate the mode. */
1268 void
1269 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1271 emit_insn (m_gen_fun (op0, op1));
1274 /* Perform the final adjustment at the end of a string to obtain the
1275 correct return value for the block operation. If ENDP is 1 return
1276 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1277 end minus one byte ala stpcpy. */
1280 store_by_pieces_d::finish_endp (int endp)
1282 gcc_assert (!m_reverse);
1283 if (endp == 2)
1285 m_to.maybe_postinc (-1);
1286 --m_offset;
1288 return m_to.adjust (QImode, m_offset);
1291 /* Determine whether the LEN bytes generated by CONSTFUN can be
1292 stored to memory using several move instructions. CONSTFUNDATA is
1293 a pointer which will be passed as argument in every CONSTFUN call.
1294 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1295 a memset operation and false if it's a copy of a constant string.
1296 Return nonzero if a call to store_by_pieces should succeed. */
1299 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1300 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1301 void *constfundata, unsigned int align, bool memsetp)
1303 unsigned HOST_WIDE_INT l;
1304 unsigned int max_size;
1305 HOST_WIDE_INT offset = 0;
1306 enum insn_code icode;
1307 int reverse;
1308 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1309 rtx cst ATTRIBUTE_UNUSED;
1311 if (len == 0)
1312 return 1;
1314 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1315 memsetp
1316 ? SET_BY_PIECES
1317 : STORE_BY_PIECES,
1318 optimize_insn_for_speed_p ()))
1319 return 0;
1321 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1323 /* We would first store what we can in the largest integer mode, then go to
1324 successively smaller modes. */
1326 for (reverse = 0;
1327 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1328 reverse++)
1330 l = len;
1331 max_size = STORE_MAX_PIECES + 1;
1332 while (max_size > 1 && l > 0)
1334 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1336 icode = optab_handler (mov_optab, mode);
1337 if (icode != CODE_FOR_nothing
1338 && align >= GET_MODE_ALIGNMENT (mode))
1340 unsigned int size = GET_MODE_SIZE (mode);
1342 while (l >= size)
1344 if (reverse)
1345 offset -= size;
1347 cst = (*constfun) (constfundata, offset, mode);
1348 if (!targetm.legitimate_constant_p (mode, cst))
1349 return 0;
1351 if (!reverse)
1352 offset += size;
1354 l -= size;
1358 max_size = GET_MODE_SIZE (mode);
1361 /* The code above should have handled everything. */
1362 gcc_assert (!l);
1365 return 1;
1368 /* Generate several move instructions to store LEN bytes generated by
1369 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1370 pointer which will be passed as argument in every CONSTFUN call.
1371 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1372 a memset operation and false if it's a copy of a constant string.
1373 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1374 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1375 stpcpy. */
1378 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1379 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1380 void *constfundata, unsigned int align, bool memsetp, int endp)
1382 if (len == 0)
1384 gcc_assert (endp != 2);
1385 return to;
1388 gcc_assert (targetm.use_by_pieces_infrastructure_p
1389 (len, align,
1390 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1391 optimize_insn_for_speed_p ()));
1393 store_by_pieces_d data (to, constfun, constfundata, len, align);
1394 data.run ();
1396 if (endp)
1397 return data.finish_endp (endp);
1398 else
1399 return to;
1402 /* Callback routine for clear_by_pieces.
1403 Return const0_rtx unconditionally. */
1405 static rtx
1406 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1408 return const0_rtx;
1411 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1412 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1414 static void
1415 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1417 if (len == 0)
1418 return;
1420 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1421 data.run ();
1424 /* Context used by compare_by_pieces_genfn. It stores the fail label
1425 to jump to in case of miscomparison, and for branch ratios greater than 1,
1426 it stores an accumulator and the current and maximum counts before
1427 emitting another branch. */
1429 class compare_by_pieces_d : public op_by_pieces_d
1431 rtx_code_label *m_fail_label;
1432 rtx m_accumulator;
1433 int m_count, m_batch;
1435 void generate (rtx, rtx, machine_mode);
1436 bool prepare_mode (machine_mode, unsigned int);
1437 void finish_mode (machine_mode);
1438 public:
1439 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1440 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1441 rtx_code_label *fail_label)
1442 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1444 m_fail_label = fail_label;
1448 /* A callback used when iterating for a compare_by_pieces_operation.
1449 OP0 and OP1 are the values that have been loaded and should be
1450 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1451 context structure. */
1453 void
1454 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1456 if (m_batch > 1)
1458 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1459 true, OPTAB_LIB_WIDEN);
1460 if (m_count != 0)
1461 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1462 true, OPTAB_LIB_WIDEN);
1463 m_accumulator = temp;
1465 if (++m_count < m_batch)
1466 return;
1468 m_count = 0;
1469 op0 = m_accumulator;
1470 op1 = const0_rtx;
1471 m_accumulator = NULL_RTX;
1473 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1474 m_fail_label, profile_probability::uninitialized ());
1477 /* Return true if MODE can be used for a set of moves and comparisons,
1478 given an alignment ALIGN. Prepare whatever data is necessary for
1479 later calls to generate. */
1481 bool
1482 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1484 insn_code icode = optab_handler (mov_optab, mode);
1485 if (icode == CODE_FOR_nothing
1486 || align < GET_MODE_ALIGNMENT (mode)
1487 || !can_compare_p (EQ, mode, ccp_jump))
1488 return false;
1489 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1490 if (m_batch < 0)
1491 return false;
1492 m_accumulator = NULL_RTX;
1493 m_count = 0;
1494 return true;
1497 /* Called after expanding a series of comparisons in MODE. If we have
1498 accumulated results for which we haven't emitted a branch yet, do
1499 so now. */
1501 void
1502 compare_by_pieces_d::finish_mode (machine_mode mode)
1504 if (m_accumulator != NULL_RTX)
1505 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1506 NULL_RTX, NULL, m_fail_label,
1507 profile_probability::uninitialized ());
1510 /* Generate several move instructions to compare LEN bytes from blocks
1511 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1513 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1514 used to push FROM to the stack.
1516 ALIGN is maximum stack alignment we can assume.
1518 Optionally, the caller can pass a constfn and associated data in A1_CFN
1519 and A1_CFN_DATA. describing that the second operand being compared is a
1520 known constant and how to obtain its data. */
1522 static rtx
1523 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1524 rtx target, unsigned int align,
1525 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1527 rtx_code_label *fail_label = gen_label_rtx ();
1528 rtx_code_label *end_label = gen_label_rtx ();
1530 if (target == NULL_RTX
1531 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1532 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1534 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1535 fail_label);
1537 data.run ();
1539 emit_move_insn (target, const0_rtx);
1540 emit_jump (end_label);
1541 emit_barrier ();
1542 emit_label (fail_label);
1543 emit_move_insn (target, const1_rtx);
1544 emit_label (end_label);
1546 return target;
1549 /* Emit code to move a block Y to a block X. This may be done with
1550 string-move instructions, with multiple scalar move instructions,
1551 or with a library call.
1553 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1554 SIZE is an rtx that says how long they are.
1555 ALIGN is the maximum alignment we can assume they have.
1556 METHOD describes what kind of copy this is, and what mechanisms may be used.
1557 MIN_SIZE is the minimal size of block to move
1558 MAX_SIZE is the maximal size of block to move, if it can not be represented
1559 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1561 Return the address of the new block, if memcpy is called and returns it,
1562 0 otherwise. */
1565 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1566 unsigned int expected_align, HOST_WIDE_INT expected_size,
1567 unsigned HOST_WIDE_INT min_size,
1568 unsigned HOST_WIDE_INT max_size,
1569 unsigned HOST_WIDE_INT probable_max_size)
1571 int may_use_call;
1572 rtx retval = 0;
1573 unsigned int align;
1575 gcc_assert (size);
1576 if (CONST_INT_P (size) && INTVAL (size) == 0)
1577 return 0;
1579 switch (method)
1581 case BLOCK_OP_NORMAL:
1582 case BLOCK_OP_TAILCALL:
1583 may_use_call = 1;
1584 break;
1586 case BLOCK_OP_CALL_PARM:
1587 may_use_call = block_move_libcall_safe_for_call_parm ();
1589 /* Make inhibit_defer_pop nonzero around the library call
1590 to force it to pop the arguments right away. */
1591 NO_DEFER_POP;
1592 break;
1594 case BLOCK_OP_NO_LIBCALL:
1595 may_use_call = 0;
1596 break;
1598 case BLOCK_OP_NO_LIBCALL_RET:
1599 may_use_call = -1;
1600 break;
1602 default:
1603 gcc_unreachable ();
1606 gcc_assert (MEM_P (x) && MEM_P (y));
1607 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1608 gcc_assert (align >= BITS_PER_UNIT);
1610 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1611 block copy is more efficient for other large modes, e.g. DCmode. */
1612 x = adjust_address (x, BLKmode, 0);
1613 y = adjust_address (y, BLKmode, 0);
1615 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1616 can be incorrect is coming from __builtin_memcpy. */
1617 poly_int64 const_size;
1618 if (poly_int_rtx_p (size, &const_size))
1620 x = shallow_copy_rtx (x);
1621 y = shallow_copy_rtx (y);
1622 set_mem_size (x, const_size);
1623 set_mem_size (y, const_size);
1626 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1627 move_by_pieces (x, y, INTVAL (size), align, 0);
1628 else if (emit_block_move_via_movmem (x, y, size, align,
1629 expected_align, expected_size,
1630 min_size, max_size, probable_max_size))
1632 else if (may_use_call
1633 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1634 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1636 if (may_use_call < 0)
1637 return pc_rtx;
1639 /* Since x and y are passed to a libcall, mark the corresponding
1640 tree EXPR as addressable. */
1641 tree y_expr = MEM_EXPR (y);
1642 tree x_expr = MEM_EXPR (x);
1643 if (y_expr)
1644 mark_addressable (y_expr);
1645 if (x_expr)
1646 mark_addressable (x_expr);
1647 retval = emit_block_copy_via_libcall (x, y, size,
1648 method == BLOCK_OP_TAILCALL);
1651 else
1652 emit_block_move_via_loop (x, y, size, align);
1654 if (method == BLOCK_OP_CALL_PARM)
1655 OK_DEFER_POP;
1657 return retval;
1661 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1663 unsigned HOST_WIDE_INT max, min = 0;
1664 if (GET_CODE (size) == CONST_INT)
1665 min = max = UINTVAL (size);
1666 else
1667 max = GET_MODE_MASK (GET_MODE (size));
1668 return emit_block_move_hints (x, y, size, method, 0, -1,
1669 min, max, max);
1672 /* A subroutine of emit_block_move. Returns true if calling the
1673 block move libcall will not clobber any parameters which may have
1674 already been placed on the stack. */
1676 static bool
1677 block_move_libcall_safe_for_call_parm (void)
1679 #if defined (REG_PARM_STACK_SPACE)
1680 tree fn;
1681 #endif
1683 /* If arguments are pushed on the stack, then they're safe. */
1684 if (PUSH_ARGS)
1685 return true;
1687 /* If registers go on the stack anyway, any argument is sure to clobber
1688 an outgoing argument. */
1689 #if defined (REG_PARM_STACK_SPACE)
1690 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1691 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1692 depend on its argument. */
1693 (void) fn;
1694 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1695 && REG_PARM_STACK_SPACE (fn) != 0)
1696 return false;
1697 #endif
1699 /* If any argument goes in memory, then it might clobber an outgoing
1700 argument. */
1702 CUMULATIVE_ARGS args_so_far_v;
1703 cumulative_args_t args_so_far;
1704 tree fn, arg;
1706 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1707 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1708 args_so_far = pack_cumulative_args (&args_so_far_v);
1710 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1711 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1713 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1714 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1715 NULL_TREE, true);
1716 if (!tmp || !REG_P (tmp))
1717 return false;
1718 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1719 return false;
1720 targetm.calls.function_arg_advance (args_so_far, mode,
1721 NULL_TREE, true);
1724 return true;
1727 /* A subroutine of emit_block_move. Expand a movmem pattern;
1728 return true if successful. */
1730 static bool
1731 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1732 unsigned int expected_align, HOST_WIDE_INT expected_size,
1733 unsigned HOST_WIDE_INT min_size,
1734 unsigned HOST_WIDE_INT max_size,
1735 unsigned HOST_WIDE_INT probable_max_size)
1737 int save_volatile_ok = volatile_ok;
1739 if (expected_align < align)
1740 expected_align = align;
1741 if (expected_size != -1)
1743 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1744 expected_size = probable_max_size;
1745 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1746 expected_size = min_size;
1749 /* Since this is a move insn, we don't care about volatility. */
1750 volatile_ok = 1;
1752 /* Try the most limited insn first, because there's no point
1753 including more than one in the machine description unless
1754 the more limited one has some advantage. */
1756 opt_scalar_int_mode mode_iter;
1757 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1759 scalar_int_mode mode = mode_iter.require ();
1760 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1762 if (code != CODE_FOR_nothing
1763 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1764 here because if SIZE is less than the mode mask, as it is
1765 returned by the macro, it will definitely be less than the
1766 actual mode mask. Since SIZE is within the Pmode address
1767 space, we limit MODE to Pmode. */
1768 && ((CONST_INT_P (size)
1769 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1770 <= (GET_MODE_MASK (mode) >> 1)))
1771 || max_size <= (GET_MODE_MASK (mode) >> 1)
1772 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1774 struct expand_operand ops[9];
1775 unsigned int nops;
1777 /* ??? When called via emit_block_move_for_call, it'd be
1778 nice if there were some way to inform the backend, so
1779 that it doesn't fail the expansion because it thinks
1780 emitting the libcall would be more efficient. */
1781 nops = insn_data[(int) code].n_generator_args;
1782 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1784 create_fixed_operand (&ops[0], x);
1785 create_fixed_operand (&ops[1], y);
1786 /* The check above guarantees that this size conversion is valid. */
1787 create_convert_operand_to (&ops[2], size, mode, true);
1788 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1789 if (nops >= 6)
1791 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1792 create_integer_operand (&ops[5], expected_size);
1794 if (nops >= 8)
1796 create_integer_operand (&ops[6], min_size);
1797 /* If we can not represent the maximal size,
1798 make parameter NULL. */
1799 if ((HOST_WIDE_INT) max_size != -1)
1800 create_integer_operand (&ops[7], max_size);
1801 else
1802 create_fixed_operand (&ops[7], NULL);
1804 if (nops == 9)
1806 /* If we can not represent the maximal size,
1807 make parameter NULL. */
1808 if ((HOST_WIDE_INT) probable_max_size != -1)
1809 create_integer_operand (&ops[8], probable_max_size);
1810 else
1811 create_fixed_operand (&ops[8], NULL);
1813 if (maybe_expand_insn (code, nops, ops))
1815 volatile_ok = save_volatile_ok;
1816 return true;
1821 volatile_ok = save_volatile_ok;
1822 return false;
1825 /* A subroutine of emit_block_move. Copy the data via an explicit
1826 loop. This is used only when libcalls are forbidden. */
1827 /* ??? It'd be nice to copy in hunks larger than QImode. */
1829 static void
1830 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1831 unsigned int align ATTRIBUTE_UNUSED)
1833 rtx_code_label *cmp_label, *top_label;
1834 rtx iter, x_addr, y_addr, tmp;
1835 machine_mode x_addr_mode = get_address_mode (x);
1836 machine_mode y_addr_mode = get_address_mode (y);
1837 machine_mode iter_mode;
1839 iter_mode = GET_MODE (size);
1840 if (iter_mode == VOIDmode)
1841 iter_mode = word_mode;
1843 top_label = gen_label_rtx ();
1844 cmp_label = gen_label_rtx ();
1845 iter = gen_reg_rtx (iter_mode);
1847 emit_move_insn (iter, const0_rtx);
1849 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1850 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1851 do_pending_stack_adjust ();
1853 emit_jump (cmp_label);
1854 emit_label (top_label);
1856 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1857 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1859 if (x_addr_mode != y_addr_mode)
1860 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1861 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1863 x = change_address (x, QImode, x_addr);
1864 y = change_address (y, QImode, y_addr);
1866 emit_move_insn (x, y);
1868 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1869 true, OPTAB_LIB_WIDEN);
1870 if (tmp != iter)
1871 emit_move_insn (iter, tmp);
1873 emit_label (cmp_label);
1875 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1876 true, top_label,
1877 profile_probability::guessed_always ()
1878 .apply_scale (9, 10));
1881 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1882 TAILCALL is true if this is a tail call. */
1885 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1886 rtx size, bool tailcall)
1888 rtx dst_addr, src_addr;
1889 tree call_expr, dst_tree, src_tree, size_tree;
1890 machine_mode size_mode;
1892 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1893 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1894 dst_tree = make_tree (ptr_type_node, dst_addr);
1896 src_addr = copy_addr_to_reg (XEXP (src, 0));
1897 src_addr = convert_memory_address (ptr_mode, src_addr);
1898 src_tree = make_tree (ptr_type_node, src_addr);
1900 size_mode = TYPE_MODE (sizetype);
1901 size = convert_to_mode (size_mode, size, 1);
1902 size = copy_to_mode_reg (size_mode, size);
1903 size_tree = make_tree (sizetype, size);
1905 /* It is incorrect to use the libcall calling conventions for calls to
1906 memcpy/memmove/memcmp because they can be provided by the user. */
1907 tree fn = builtin_decl_implicit (fncode);
1908 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1909 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1911 return expand_call (call_expr, NULL_RTX, false);
1914 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1915 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1916 otherwise return null. */
1919 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1920 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1921 HOST_WIDE_INT align)
1923 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1925 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1926 target = NULL_RTX;
1928 struct expand_operand ops[5];
1929 create_output_operand (&ops[0], target, insn_mode);
1930 create_fixed_operand (&ops[1], arg1_rtx);
1931 create_fixed_operand (&ops[2], arg2_rtx);
1932 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1933 TYPE_UNSIGNED (arg3_type));
1934 create_integer_operand (&ops[4], align);
1935 if (maybe_expand_insn (icode, 5, ops))
1936 return ops[0].value;
1937 return NULL_RTX;
1940 /* Expand a block compare between X and Y with length LEN using the
1941 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1942 of the expression that was used to calculate the length. ALIGN
1943 gives the known minimum common alignment. */
1945 static rtx
1946 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1947 unsigned align)
1949 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1950 implementing memcmp because it will stop if it encounters two
1951 zero bytes. */
1952 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1954 if (icode == CODE_FOR_nothing)
1955 return NULL_RTX;
1957 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1960 /* Emit code to compare a block Y to a block X. This may be done with
1961 string-compare instructions, with multiple scalar instructions,
1962 or with a library call.
1964 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1965 they are. LEN_TYPE is the type of the expression that was used to
1966 calculate it.
1968 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1969 value of a normal memcmp call, instead we can just compare for equality.
1970 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1971 returning NULL_RTX.
1973 Optionally, the caller can pass a constfn and associated data in Y_CFN
1974 and Y_CFN_DATA. describing that the second operand being compared is a
1975 known constant and how to obtain its data.
1976 Return the result of the comparison, or NULL_RTX if we failed to
1977 perform the operation. */
1980 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1981 bool equality_only, by_pieces_constfn y_cfn,
1982 void *y_cfndata)
1984 rtx result = 0;
1986 if (CONST_INT_P (len) && INTVAL (len) == 0)
1987 return const0_rtx;
1989 gcc_assert (MEM_P (x) && MEM_P (y));
1990 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1991 gcc_assert (align >= BITS_PER_UNIT);
1993 x = adjust_address (x, BLKmode, 0);
1994 y = adjust_address (y, BLKmode, 0);
1996 if (equality_only
1997 && CONST_INT_P (len)
1998 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1999 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2000 y_cfn, y_cfndata);
2001 else
2002 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2004 return result;
2007 /* Copy all or part of a value X into registers starting at REGNO.
2008 The number of registers to be filled is NREGS. */
2010 void
2011 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2013 if (nregs == 0)
2014 return;
2016 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2017 x = validize_mem (force_const_mem (mode, x));
2019 /* See if the machine can do this with a load multiple insn. */
2020 if (targetm.have_load_multiple ())
2022 rtx_insn *last = get_last_insn ();
2023 rtx first = gen_rtx_REG (word_mode, regno);
2024 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2025 GEN_INT (nregs)))
2027 emit_insn (pat);
2028 return;
2030 else
2031 delete_insns_since (last);
2034 for (int i = 0; i < nregs; i++)
2035 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2036 operand_subword_force (x, i, mode));
2039 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2040 The number of registers to be filled is NREGS. */
2042 void
2043 move_block_from_reg (int regno, rtx x, int nregs)
2045 if (nregs == 0)
2046 return;
2048 /* See if the machine can do this with a store multiple insn. */
2049 if (targetm.have_store_multiple ())
2051 rtx_insn *last = get_last_insn ();
2052 rtx first = gen_rtx_REG (word_mode, regno);
2053 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2054 GEN_INT (nregs)))
2056 emit_insn (pat);
2057 return;
2059 else
2060 delete_insns_since (last);
2063 for (int i = 0; i < nregs; i++)
2065 rtx tem = operand_subword (x, i, 1, BLKmode);
2067 gcc_assert (tem);
2069 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2073 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2074 ORIG, where ORIG is a non-consecutive group of registers represented by
2075 a PARALLEL. The clone is identical to the original except in that the
2076 original set of registers is replaced by a new set of pseudo registers.
2077 The new set has the same modes as the original set. */
2080 gen_group_rtx (rtx orig)
2082 int i, length;
2083 rtx *tmps;
2085 gcc_assert (GET_CODE (orig) == PARALLEL);
2087 length = XVECLEN (orig, 0);
2088 tmps = XALLOCAVEC (rtx, length);
2090 /* Skip a NULL entry in first slot. */
2091 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2093 if (i)
2094 tmps[0] = 0;
2096 for (; i < length; i++)
2098 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2099 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2101 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2104 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2107 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2108 except that values are placed in TMPS[i], and must later be moved
2109 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2111 static void
2112 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2113 poly_int64 ssize)
2115 rtx src;
2116 int start, i;
2117 machine_mode m = GET_MODE (orig_src);
2119 gcc_assert (GET_CODE (dst) == PARALLEL);
2121 if (m != VOIDmode
2122 && !SCALAR_INT_MODE_P (m)
2123 && !MEM_P (orig_src)
2124 && GET_CODE (orig_src) != CONCAT)
2126 scalar_int_mode imode;
2127 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2129 src = gen_reg_rtx (imode);
2130 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2132 else
2134 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2135 emit_move_insn (src, orig_src);
2137 emit_group_load_1 (tmps, dst, src, type, ssize);
2138 return;
2141 /* Check for a NULL entry, used to indicate that the parameter goes
2142 both on the stack and in registers. */
2143 if (XEXP (XVECEXP (dst, 0, 0), 0))
2144 start = 0;
2145 else
2146 start = 1;
2148 /* Process the pieces. */
2149 for (i = start; i < XVECLEN (dst, 0); i++)
2151 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2152 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2153 poly_int64 bytelen = GET_MODE_SIZE (mode);
2154 poly_int64 shift = 0;
2156 /* Handle trailing fragments that run over the size of the struct.
2157 It's the target's responsibility to make sure that the fragment
2158 cannot be strictly smaller in some cases and strictly larger
2159 in others. */
2160 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2161 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2163 /* Arrange to shift the fragment to where it belongs.
2164 extract_bit_field loads to the lsb of the reg. */
2165 if (
2166 #ifdef BLOCK_REG_PADDING
2167 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2168 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2169 #else
2170 BYTES_BIG_ENDIAN
2171 #endif
2173 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2174 bytelen = ssize - bytepos;
2175 gcc_assert (maybe_gt (bytelen, 0));
2178 /* If we won't be loading directly from memory, protect the real source
2179 from strange tricks we might play; but make sure that the source can
2180 be loaded directly into the destination. */
2181 src = orig_src;
2182 if (!MEM_P (orig_src)
2183 && (!CONSTANT_P (orig_src)
2184 || (GET_MODE (orig_src) != mode
2185 && GET_MODE (orig_src) != VOIDmode)))
2187 if (GET_MODE (orig_src) == VOIDmode)
2188 src = gen_reg_rtx (mode);
2189 else
2190 src = gen_reg_rtx (GET_MODE (orig_src));
2192 emit_move_insn (src, orig_src);
2195 /* Optimize the access just a bit. */
2196 if (MEM_P (src)
2197 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2198 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2199 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2200 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2202 tmps[i] = gen_reg_rtx (mode);
2203 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2205 else if (COMPLEX_MODE_P (mode)
2206 && GET_MODE (src) == mode
2207 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2208 /* Let emit_move_complex do the bulk of the work. */
2209 tmps[i] = src;
2210 else if (GET_CODE (src) == CONCAT)
2212 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2213 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2214 unsigned int elt;
2215 poly_int64 subpos;
2217 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2218 && known_le (subpos + bytelen, slen0))
2220 /* The following assumes that the concatenated objects all
2221 have the same size. In this case, a simple calculation
2222 can be used to determine the object and the bit field
2223 to be extracted. */
2224 tmps[i] = XEXP (src, elt);
2225 if (maybe_ne (subpos, 0)
2226 || maybe_ne (subpos + bytelen, slen0)
2227 || (!CONSTANT_P (tmps[i])
2228 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2229 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2230 subpos * BITS_PER_UNIT,
2231 1, NULL_RTX, mode, mode, false,
2232 NULL);
2234 else
2236 rtx mem;
2238 gcc_assert (known_eq (bytepos, 0));
2239 mem = assign_stack_temp (GET_MODE (src), slen);
2240 emit_move_insn (mem, src);
2241 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2242 0, 1, NULL_RTX, mode, mode, false,
2243 NULL);
2246 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2247 SIMD register, which is currently broken. While we get GCC
2248 to emit proper RTL for these cases, let's dump to memory. */
2249 else if (VECTOR_MODE_P (GET_MODE (dst))
2250 && REG_P (src))
2252 poly_uint64 slen = GET_MODE_SIZE (GET_MODE (src));
2253 rtx mem;
2255 mem = assign_stack_temp (GET_MODE (src), slen);
2256 emit_move_insn (mem, src);
2257 tmps[i] = adjust_address (mem, mode, bytepos);
2259 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2260 && XVECLEN (dst, 0) > 1)
2261 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2262 else if (CONSTANT_P (src))
2264 if (known_eq (bytelen, ssize))
2265 tmps[i] = src;
2266 else
2268 rtx first, second;
2270 /* TODO: const_wide_int can have sizes other than this... */
2271 gcc_assert (known_eq (2 * bytelen, ssize));
2272 split_double (src, &first, &second);
2273 if (i)
2274 tmps[i] = second;
2275 else
2276 tmps[i] = first;
2279 else if (REG_P (src) && GET_MODE (src) == mode)
2280 tmps[i] = src;
2281 else
2282 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2283 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2284 mode, mode, false, NULL);
2286 if (maybe_ne (shift, 0))
2287 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2288 shift, tmps[i], 0);
2292 /* Emit code to move a block SRC of type TYPE to a block DST,
2293 where DST is non-consecutive registers represented by a PARALLEL.
2294 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2295 if not known. */
2297 void
2298 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2300 rtx *tmps;
2301 int i;
2303 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2304 emit_group_load_1 (tmps, dst, src, type, ssize);
2306 /* Copy the extracted pieces into the proper (probable) hard regs. */
2307 for (i = 0; i < XVECLEN (dst, 0); i++)
2309 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2310 if (d == NULL)
2311 continue;
2312 emit_move_insn (d, tmps[i]);
2316 /* Similar, but load SRC into new pseudos in a format that looks like
2317 PARALLEL. This can later be fed to emit_group_move to get things
2318 in the right place. */
2321 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2323 rtvec vec;
2324 int i;
2326 vec = rtvec_alloc (XVECLEN (parallel, 0));
2327 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2329 /* Convert the vector to look just like the original PARALLEL, except
2330 with the computed values. */
2331 for (i = 0; i < XVECLEN (parallel, 0); i++)
2333 rtx e = XVECEXP (parallel, 0, i);
2334 rtx d = XEXP (e, 0);
2336 if (d)
2338 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2339 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2341 RTVEC_ELT (vec, i) = e;
2344 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2347 /* Emit code to move a block SRC to block DST, where SRC and DST are
2348 non-consecutive groups of registers, each represented by a PARALLEL. */
2350 void
2351 emit_group_move (rtx dst, rtx src)
2353 int i;
2355 gcc_assert (GET_CODE (src) == PARALLEL
2356 && GET_CODE (dst) == PARALLEL
2357 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2359 /* Skip first entry if NULL. */
2360 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2361 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2362 XEXP (XVECEXP (src, 0, i), 0));
2365 /* Move a group of registers represented by a PARALLEL into pseudos. */
2368 emit_group_move_into_temps (rtx src)
2370 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2371 int i;
2373 for (i = 0; i < XVECLEN (src, 0); i++)
2375 rtx e = XVECEXP (src, 0, i);
2376 rtx d = XEXP (e, 0);
2378 if (d)
2379 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2380 RTVEC_ELT (vec, i) = e;
2383 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2386 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2387 where SRC is non-consecutive registers represented by a PARALLEL.
2388 SSIZE represents the total size of block ORIG_DST, or -1 if not
2389 known. */
2391 void
2392 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2393 poly_int64 ssize)
2395 rtx *tmps, dst;
2396 int start, finish, i;
2397 machine_mode m = GET_MODE (orig_dst);
2399 gcc_assert (GET_CODE (src) == PARALLEL);
2401 if (!SCALAR_INT_MODE_P (m)
2402 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2404 scalar_int_mode imode;
2405 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2407 dst = gen_reg_rtx (imode);
2408 emit_group_store (dst, src, type, ssize);
2409 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2411 else
2413 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2414 emit_group_store (dst, src, type, ssize);
2416 emit_move_insn (orig_dst, dst);
2417 return;
2420 /* Check for a NULL entry, used to indicate that the parameter goes
2421 both on the stack and in registers. */
2422 if (XEXP (XVECEXP (src, 0, 0), 0))
2423 start = 0;
2424 else
2425 start = 1;
2426 finish = XVECLEN (src, 0);
2428 tmps = XALLOCAVEC (rtx, finish);
2430 /* Copy the (probable) hard regs into pseudos. */
2431 for (i = start; i < finish; i++)
2433 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2434 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2436 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2437 emit_move_insn (tmps[i], reg);
2439 else
2440 tmps[i] = reg;
2443 /* If we won't be storing directly into memory, protect the real destination
2444 from strange tricks we might play. */
2445 dst = orig_dst;
2446 if (GET_CODE (dst) == PARALLEL)
2448 rtx temp;
2450 /* We can get a PARALLEL dst if there is a conditional expression in
2451 a return statement. In that case, the dst and src are the same,
2452 so no action is necessary. */
2453 if (rtx_equal_p (dst, src))
2454 return;
2456 /* It is unclear if we can ever reach here, but we may as well handle
2457 it. Allocate a temporary, and split this into a store/load to/from
2458 the temporary. */
2459 temp = assign_stack_temp (GET_MODE (dst), ssize);
2460 emit_group_store (temp, src, type, ssize);
2461 emit_group_load (dst, temp, type, ssize);
2462 return;
2464 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2466 machine_mode outer = GET_MODE (dst);
2467 machine_mode inner;
2468 poly_int64 bytepos;
2469 bool done = false;
2470 rtx temp;
2472 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2473 dst = gen_reg_rtx (outer);
2475 /* Make life a bit easier for combine. */
2476 /* If the first element of the vector is the low part
2477 of the destination mode, use a paradoxical subreg to
2478 initialize the destination. */
2479 if (start < finish)
2481 inner = GET_MODE (tmps[start]);
2482 bytepos = subreg_lowpart_offset (inner, outer);
2483 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
2484 bytepos))
2486 temp = simplify_gen_subreg (outer, tmps[start],
2487 inner, 0);
2488 if (temp)
2490 emit_move_insn (dst, temp);
2491 done = true;
2492 start++;
2497 /* If the first element wasn't the low part, try the last. */
2498 if (!done
2499 && start < finish - 1)
2501 inner = GET_MODE (tmps[finish - 1]);
2502 bytepos = subreg_lowpart_offset (inner, outer);
2503 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2504 finish - 1), 1)),
2505 bytepos))
2507 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2508 inner, 0);
2509 if (temp)
2511 emit_move_insn (dst, temp);
2512 done = true;
2513 finish--;
2518 /* Otherwise, simply initialize the result to zero. */
2519 if (!done)
2520 emit_move_insn (dst, CONST0_RTX (outer));
2523 /* Process the pieces. */
2524 for (i = start; i < finish; i++)
2526 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2527 machine_mode mode = GET_MODE (tmps[i]);
2528 poly_int64 bytelen = GET_MODE_SIZE (mode);
2529 poly_uint64 adj_bytelen;
2530 rtx dest = dst;
2532 /* Handle trailing fragments that run over the size of the struct.
2533 It's the target's responsibility to make sure that the fragment
2534 cannot be strictly smaller in some cases and strictly larger
2535 in others. */
2536 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2537 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2538 adj_bytelen = ssize - bytepos;
2539 else
2540 adj_bytelen = bytelen;
2542 if (GET_CODE (dst) == CONCAT)
2544 if (known_le (bytepos + adj_bytelen,
2545 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2546 dest = XEXP (dst, 0);
2547 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2549 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2550 dest = XEXP (dst, 1);
2552 else
2554 machine_mode dest_mode = GET_MODE (dest);
2555 machine_mode tmp_mode = GET_MODE (tmps[i]);
2557 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2559 if (GET_MODE_ALIGNMENT (dest_mode)
2560 >= GET_MODE_ALIGNMENT (tmp_mode))
2562 dest = assign_stack_temp (dest_mode,
2563 GET_MODE_SIZE (dest_mode));
2564 emit_move_insn (adjust_address (dest,
2565 tmp_mode,
2566 bytepos),
2567 tmps[i]);
2568 dst = dest;
2570 else
2572 dest = assign_stack_temp (tmp_mode,
2573 GET_MODE_SIZE (tmp_mode));
2574 emit_move_insn (dest, tmps[i]);
2575 dst = adjust_address (dest, dest_mode, bytepos);
2577 break;
2581 /* Handle trailing fragments that run over the size of the struct. */
2582 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2584 /* store_bit_field always takes its value from the lsb.
2585 Move the fragment to the lsb if it's not already there. */
2586 if (
2587 #ifdef BLOCK_REG_PADDING
2588 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2589 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2590 #else
2591 BYTES_BIG_ENDIAN
2592 #endif
2595 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2596 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2597 shift, tmps[i], 0);
2600 /* Make sure not to write past the end of the struct. */
2601 store_bit_field (dest,
2602 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2603 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2604 VOIDmode, tmps[i], false);
2607 /* Optimize the access just a bit. */
2608 else if (MEM_P (dest)
2609 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2610 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2611 && multiple_p (bytepos * BITS_PER_UNIT,
2612 GET_MODE_ALIGNMENT (mode))
2613 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2614 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2616 else
2617 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2618 0, 0, mode, tmps[i], false);
2621 /* Copy from the pseudo into the (probable) hard reg. */
2622 if (orig_dst != dst)
2623 emit_move_insn (orig_dst, dst);
2626 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2627 of the value stored in X. */
2630 maybe_emit_group_store (rtx x, tree type)
2632 machine_mode mode = TYPE_MODE (type);
2633 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2634 if (GET_CODE (x) == PARALLEL)
2636 rtx result = gen_reg_rtx (mode);
2637 emit_group_store (result, x, type, int_size_in_bytes (type));
2638 return result;
2640 return x;
2643 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2645 This is used on targets that return BLKmode values in registers. */
2647 static void
2648 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2650 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2651 rtx src = NULL, dst = NULL;
2652 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2653 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2654 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2655 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2656 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2657 fixed_size_mode copy_mode;
2659 /* BLKmode registers created in the back-end shouldn't have survived. */
2660 gcc_assert (mode != BLKmode);
2662 /* If the structure doesn't take up a whole number of words, see whether
2663 SRCREG is padded on the left or on the right. If it's on the left,
2664 set PADDING_CORRECTION to the number of bits to skip.
2666 In most ABIs, the structure will be returned at the least end of
2667 the register, which translates to right padding on little-endian
2668 targets and left padding on big-endian targets. The opposite
2669 holds if the structure is returned at the most significant
2670 end of the register. */
2671 if (bytes % UNITS_PER_WORD != 0
2672 && (targetm.calls.return_in_msb (type)
2673 ? !BYTES_BIG_ENDIAN
2674 : BYTES_BIG_ENDIAN))
2675 padding_correction
2676 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2678 /* We can use a single move if we have an exact mode for the size. */
2679 else if (MEM_P (target)
2680 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2681 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2682 && bytes == GET_MODE_SIZE (mode))
2684 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2685 return;
2688 /* And if we additionally have the same mode for a register. */
2689 else if (REG_P (target)
2690 && GET_MODE (target) == mode
2691 && bytes == GET_MODE_SIZE (mode))
2693 emit_move_insn (target, srcreg);
2694 return;
2697 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2698 into a new pseudo which is a full word. */
2699 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2701 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2702 mode = word_mode;
2705 /* Copy the structure BITSIZE bits at a time. If the target lives in
2706 memory, take care of not reading/writing past its end by selecting
2707 a copy mode suited to BITSIZE. This should always be possible given
2708 how it is computed.
2710 If the target lives in register, make sure not to select a copy mode
2711 larger than the mode of the register.
2713 We could probably emit more efficient code for machines which do not use
2714 strict alignment, but it doesn't seem worth the effort at the current
2715 time. */
2717 copy_mode = word_mode;
2718 if (MEM_P (target))
2720 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2721 if (mem_mode.exists ())
2722 copy_mode = mem_mode.require ();
2724 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2725 copy_mode = tmode;
2727 for (bitpos = 0, xbitpos = padding_correction;
2728 bitpos < bytes * BITS_PER_UNIT;
2729 bitpos += bitsize, xbitpos += bitsize)
2731 /* We need a new source operand each time xbitpos is on a
2732 word boundary and when xbitpos == padding_correction
2733 (the first time through). */
2734 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2735 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2737 /* We need a new destination operand each time bitpos is on
2738 a word boundary. */
2739 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2740 dst = target;
2741 else if (bitpos % BITS_PER_WORD == 0)
2742 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2744 /* Use xbitpos for the source extraction (right justified) and
2745 bitpos for the destination store (left justified). */
2746 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2747 extract_bit_field (src, bitsize,
2748 xbitpos % BITS_PER_WORD, 1,
2749 NULL_RTX, copy_mode, copy_mode,
2750 false, NULL),
2751 false);
2755 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2756 register if it contains any data, otherwise return null.
2758 This is used on targets that return BLKmode values in registers. */
2761 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2763 int i, n_regs;
2764 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2765 unsigned int bitsize;
2766 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2767 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2768 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2769 fixed_size_mode dst_mode;
2771 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2773 x = expand_normal (src);
2775 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2776 if (bytes == 0)
2777 return NULL_RTX;
2779 /* If the structure doesn't take up a whole number of words, see
2780 whether the register value should be padded on the left or on
2781 the right. Set PADDING_CORRECTION to the number of padding
2782 bits needed on the left side.
2784 In most ABIs, the structure will be returned at the least end of
2785 the register, which translates to right padding on little-endian
2786 targets and left padding on big-endian targets. The opposite
2787 holds if the structure is returned at the most significant
2788 end of the register. */
2789 if (bytes % UNITS_PER_WORD != 0
2790 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2791 ? !BYTES_BIG_ENDIAN
2792 : BYTES_BIG_ENDIAN))
2793 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2794 * BITS_PER_UNIT));
2796 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2797 dst_words = XALLOCAVEC (rtx, n_regs);
2798 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2800 /* Copy the structure BITSIZE bits at a time. */
2801 for (bitpos = 0, xbitpos = padding_correction;
2802 bitpos < bytes * BITS_PER_UNIT;
2803 bitpos += bitsize, xbitpos += bitsize)
2805 /* We need a new destination pseudo each time xbitpos is
2806 on a word boundary and when xbitpos == padding_correction
2807 (the first time through). */
2808 if (xbitpos % BITS_PER_WORD == 0
2809 || xbitpos == padding_correction)
2811 /* Generate an appropriate register. */
2812 dst_word = gen_reg_rtx (word_mode);
2813 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2815 /* Clear the destination before we move anything into it. */
2816 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2819 /* We need a new source operand each time bitpos is on a word
2820 boundary. */
2821 if (bitpos % BITS_PER_WORD == 0)
2822 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2824 /* Use bitpos for the source extraction (left justified) and
2825 xbitpos for the destination store (right justified). */
2826 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2827 0, 0, word_mode,
2828 extract_bit_field (src_word, bitsize,
2829 bitpos % BITS_PER_WORD, 1,
2830 NULL_RTX, word_mode, word_mode,
2831 false, NULL),
2832 false);
2835 if (mode == BLKmode)
2837 /* Find the smallest integer mode large enough to hold the
2838 entire structure. */
2839 opt_scalar_int_mode mode_iter;
2840 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2841 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2842 break;
2844 /* A suitable mode should have been found. */
2845 mode = mode_iter.require ();
2848 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2849 dst_mode = word_mode;
2850 else
2851 dst_mode = mode;
2852 dst = gen_reg_rtx (dst_mode);
2854 for (i = 0; i < n_regs; i++)
2855 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2857 if (mode != dst_mode)
2858 dst = gen_lowpart (mode, dst);
2860 return dst;
2863 /* Add a USE expression for REG to the (possibly empty) list pointed
2864 to by CALL_FUSAGE. REG must denote a hard register. */
2866 void
2867 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2869 gcc_assert (REG_P (reg));
2871 if (!HARD_REGISTER_P (reg))
2872 return;
2874 *call_fusage
2875 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2878 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2879 to by CALL_FUSAGE. REG must denote a hard register. */
2881 void
2882 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2884 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2886 *call_fusage
2887 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2890 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2891 starting at REGNO. All of these registers must be hard registers. */
2893 void
2894 use_regs (rtx *call_fusage, int regno, int nregs)
2896 int i;
2898 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2900 for (i = 0; i < nregs; i++)
2901 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2904 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2905 PARALLEL REGS. This is for calls that pass values in multiple
2906 non-contiguous locations. The Irix 6 ABI has examples of this. */
2908 void
2909 use_group_regs (rtx *call_fusage, rtx regs)
2911 int i;
2913 for (i = 0; i < XVECLEN (regs, 0); i++)
2915 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2917 /* A NULL entry means the parameter goes both on the stack and in
2918 registers. This can also be a MEM for targets that pass values
2919 partially on the stack and partially in registers. */
2920 if (reg != 0 && REG_P (reg))
2921 use_reg (call_fusage, reg);
2925 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2926 assigment and the code of the expresion on the RHS is CODE. Return
2927 NULL otherwise. */
2929 static gimple *
2930 get_def_for_expr (tree name, enum tree_code code)
2932 gimple *def_stmt;
2934 if (TREE_CODE (name) != SSA_NAME)
2935 return NULL;
2937 def_stmt = get_gimple_for_ssa_name (name);
2938 if (!def_stmt
2939 || gimple_assign_rhs_code (def_stmt) != code)
2940 return NULL;
2942 return def_stmt;
2945 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2946 assigment and the class of the expresion on the RHS is CLASS. Return
2947 NULL otherwise. */
2949 static gimple *
2950 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2952 gimple *def_stmt;
2954 if (TREE_CODE (name) != SSA_NAME)
2955 return NULL;
2957 def_stmt = get_gimple_for_ssa_name (name);
2958 if (!def_stmt
2959 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2960 return NULL;
2962 return def_stmt;
2965 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2966 its length in bytes. */
2969 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2970 unsigned int expected_align, HOST_WIDE_INT expected_size,
2971 unsigned HOST_WIDE_INT min_size,
2972 unsigned HOST_WIDE_INT max_size,
2973 unsigned HOST_WIDE_INT probable_max_size)
2975 machine_mode mode = GET_MODE (object);
2976 unsigned int align;
2978 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2980 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2981 just move a zero. Otherwise, do this a piece at a time. */
2982 poly_int64 size_val;
2983 if (mode != BLKmode
2984 && poly_int_rtx_p (size, &size_val)
2985 && known_eq (size_val, GET_MODE_SIZE (mode)))
2987 rtx zero = CONST0_RTX (mode);
2988 if (zero != NULL)
2990 emit_move_insn (object, zero);
2991 return NULL;
2994 if (COMPLEX_MODE_P (mode))
2996 zero = CONST0_RTX (GET_MODE_INNER (mode));
2997 if (zero != NULL)
2999 write_complex_part (object, zero, 0);
3000 write_complex_part (object, zero, 1);
3001 return NULL;
3006 if (size == const0_rtx)
3007 return NULL;
3009 align = MEM_ALIGN (object);
3011 if (CONST_INT_P (size)
3012 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3013 CLEAR_BY_PIECES,
3014 optimize_insn_for_speed_p ()))
3015 clear_by_pieces (object, INTVAL (size), align);
3016 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3017 expected_align, expected_size,
3018 min_size, max_size, probable_max_size))
3020 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3021 return set_storage_via_libcall (object, size, const0_rtx,
3022 method == BLOCK_OP_TAILCALL);
3023 else
3024 gcc_unreachable ();
3026 return NULL;
3030 clear_storage (rtx object, rtx size, enum block_op_methods method)
3032 unsigned HOST_WIDE_INT max, min = 0;
3033 if (GET_CODE (size) == CONST_INT)
3034 min = max = UINTVAL (size);
3035 else
3036 max = GET_MODE_MASK (GET_MODE (size));
3037 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3041 /* A subroutine of clear_storage. Expand a call to memset.
3042 Return the return value of memset, 0 otherwise. */
3045 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3047 tree call_expr, fn, object_tree, size_tree, val_tree;
3048 machine_mode size_mode;
3050 object = copy_addr_to_reg (XEXP (object, 0));
3051 object_tree = make_tree (ptr_type_node, object);
3053 if (!CONST_INT_P (val))
3054 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3055 val_tree = make_tree (integer_type_node, val);
3057 size_mode = TYPE_MODE (sizetype);
3058 size = convert_to_mode (size_mode, size, 1);
3059 size = copy_to_mode_reg (size_mode, size);
3060 size_tree = make_tree (sizetype, size);
3062 /* It is incorrect to use the libcall calling conventions for calls to
3063 memset because it can be provided by the user. */
3064 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3065 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3066 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3068 return expand_call (call_expr, NULL_RTX, false);
3071 /* Expand a setmem pattern; return true if successful. */
3073 bool
3074 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3075 unsigned int expected_align, HOST_WIDE_INT expected_size,
3076 unsigned HOST_WIDE_INT min_size,
3077 unsigned HOST_WIDE_INT max_size,
3078 unsigned HOST_WIDE_INT probable_max_size)
3080 /* Try the most limited insn first, because there's no point
3081 including more than one in the machine description unless
3082 the more limited one has some advantage. */
3084 if (expected_align < align)
3085 expected_align = align;
3086 if (expected_size != -1)
3088 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3089 expected_size = max_size;
3090 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3091 expected_size = min_size;
3094 opt_scalar_int_mode mode_iter;
3095 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3097 scalar_int_mode mode = mode_iter.require ();
3098 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3100 if (code != CODE_FOR_nothing
3101 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3102 here because if SIZE is less than the mode mask, as it is
3103 returned by the macro, it will definitely be less than the
3104 actual mode mask. Since SIZE is within the Pmode address
3105 space, we limit MODE to Pmode. */
3106 && ((CONST_INT_P (size)
3107 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3108 <= (GET_MODE_MASK (mode) >> 1)))
3109 || max_size <= (GET_MODE_MASK (mode) >> 1)
3110 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3112 struct expand_operand ops[9];
3113 unsigned int nops;
3115 nops = insn_data[(int) code].n_generator_args;
3116 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3118 create_fixed_operand (&ops[0], object);
3119 /* The check above guarantees that this size conversion is valid. */
3120 create_convert_operand_to (&ops[1], size, mode, true);
3121 create_convert_operand_from (&ops[2], val, byte_mode, true);
3122 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3123 if (nops >= 6)
3125 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3126 create_integer_operand (&ops[5], expected_size);
3128 if (nops >= 8)
3130 create_integer_operand (&ops[6], min_size);
3131 /* If we can not represent the maximal size,
3132 make parameter NULL. */
3133 if ((HOST_WIDE_INT) max_size != -1)
3134 create_integer_operand (&ops[7], max_size);
3135 else
3136 create_fixed_operand (&ops[7], NULL);
3138 if (nops == 9)
3140 /* If we can not represent the maximal size,
3141 make parameter NULL. */
3142 if ((HOST_WIDE_INT) probable_max_size != -1)
3143 create_integer_operand (&ops[8], probable_max_size);
3144 else
3145 create_fixed_operand (&ops[8], NULL);
3147 if (maybe_expand_insn (code, nops, ops))
3148 return true;
3152 return false;
3156 /* Write to one of the components of the complex value CPLX. Write VAL to
3157 the real part if IMAG_P is false, and the imaginary part if its true. */
3159 void
3160 write_complex_part (rtx cplx, rtx val, bool imag_p)
3162 machine_mode cmode;
3163 scalar_mode imode;
3164 unsigned ibitsize;
3166 if (GET_CODE (cplx) == CONCAT)
3168 emit_move_insn (XEXP (cplx, imag_p), val);
3169 return;
3172 cmode = GET_MODE (cplx);
3173 imode = GET_MODE_INNER (cmode);
3174 ibitsize = GET_MODE_BITSIZE (imode);
3176 /* For MEMs simplify_gen_subreg may generate an invalid new address
3177 because, e.g., the original address is considered mode-dependent
3178 by the target, which restricts simplify_subreg from invoking
3179 adjust_address_nv. Instead of preparing fallback support for an
3180 invalid address, we call adjust_address_nv directly. */
3181 if (MEM_P (cplx))
3183 emit_move_insn (adjust_address_nv (cplx, imode,
3184 imag_p ? GET_MODE_SIZE (imode) : 0),
3185 val);
3186 return;
3189 /* If the sub-object is at least word sized, then we know that subregging
3190 will work. This special case is important, since store_bit_field
3191 wants to operate on integer modes, and there's rarely an OImode to
3192 correspond to TCmode. */
3193 if (ibitsize >= BITS_PER_WORD
3194 /* For hard regs we have exact predicates. Assume we can split
3195 the original object if it spans an even number of hard regs.
3196 This special case is important for SCmode on 64-bit platforms
3197 where the natural size of floating-point regs is 32-bit. */
3198 || (REG_P (cplx)
3199 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3200 && REG_NREGS (cplx) % 2 == 0))
3202 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3203 imag_p ? GET_MODE_SIZE (imode) : 0);
3204 if (part)
3206 emit_move_insn (part, val);
3207 return;
3209 else
3210 /* simplify_gen_subreg may fail for sub-word MEMs. */
3211 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3214 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3215 false);
3218 /* Extract one of the components of the complex value CPLX. Extract the
3219 real part if IMAG_P is false, and the imaginary part if it's true. */
3222 read_complex_part (rtx cplx, bool imag_p)
3224 machine_mode cmode;
3225 scalar_mode imode;
3226 unsigned ibitsize;
3228 if (GET_CODE (cplx) == CONCAT)
3229 return XEXP (cplx, imag_p);
3231 cmode = GET_MODE (cplx);
3232 imode = GET_MODE_INNER (cmode);
3233 ibitsize = GET_MODE_BITSIZE (imode);
3235 /* Special case reads from complex constants that got spilled to memory. */
3236 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3238 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3239 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3241 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3242 if (CONSTANT_CLASS_P (part))
3243 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3247 /* For MEMs simplify_gen_subreg may generate an invalid new address
3248 because, e.g., the original address is considered mode-dependent
3249 by the target, which restricts simplify_subreg from invoking
3250 adjust_address_nv. Instead of preparing fallback support for an
3251 invalid address, we call adjust_address_nv directly. */
3252 if (MEM_P (cplx))
3253 return adjust_address_nv (cplx, imode,
3254 imag_p ? GET_MODE_SIZE (imode) : 0);
3256 /* If the sub-object is at least word sized, then we know that subregging
3257 will work. This special case is important, since extract_bit_field
3258 wants to operate on integer modes, and there's rarely an OImode to
3259 correspond to TCmode. */
3260 if (ibitsize >= BITS_PER_WORD
3261 /* For hard regs we have exact predicates. Assume we can split
3262 the original object if it spans an even number of hard regs.
3263 This special case is important for SCmode on 64-bit platforms
3264 where the natural size of floating-point regs is 32-bit. */
3265 || (REG_P (cplx)
3266 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3267 && REG_NREGS (cplx) % 2 == 0))
3269 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3270 imag_p ? GET_MODE_SIZE (imode) : 0);
3271 if (ret)
3272 return ret;
3273 else
3274 /* simplify_gen_subreg may fail for sub-word MEMs. */
3275 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3278 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3279 true, NULL_RTX, imode, imode, false, NULL);
3282 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3283 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3284 represented in NEW_MODE. If FORCE is true, this will never happen, as
3285 we'll force-create a SUBREG if needed. */
3287 static rtx
3288 emit_move_change_mode (machine_mode new_mode,
3289 machine_mode old_mode, rtx x, bool force)
3291 rtx ret;
3293 if (push_operand (x, GET_MODE (x)))
3295 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3296 MEM_COPY_ATTRIBUTES (ret, x);
3298 else if (MEM_P (x))
3300 /* We don't have to worry about changing the address since the
3301 size in bytes is supposed to be the same. */
3302 if (reload_in_progress)
3304 /* Copy the MEM to change the mode and move any
3305 substitutions from the old MEM to the new one. */
3306 ret = adjust_address_nv (x, new_mode, 0);
3307 copy_replacements (x, ret);
3309 else
3310 ret = adjust_address (x, new_mode, 0);
3312 else
3314 /* Note that we do want simplify_subreg's behavior of validating
3315 that the new mode is ok for a hard register. If we were to use
3316 simplify_gen_subreg, we would create the subreg, but would
3317 probably run into the target not being able to implement it. */
3318 /* Except, of course, when FORCE is true, when this is exactly what
3319 we want. Which is needed for CCmodes on some targets. */
3320 if (force)
3321 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3322 else
3323 ret = simplify_subreg (new_mode, x, old_mode, 0);
3326 return ret;
3329 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3330 an integer mode of the same size as MODE. Returns the instruction
3331 emitted, or NULL if such a move could not be generated. */
3333 static rtx_insn *
3334 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3336 scalar_int_mode imode;
3337 enum insn_code code;
3339 /* There must exist a mode of the exact size we require. */
3340 if (!int_mode_for_mode (mode).exists (&imode))
3341 return NULL;
3343 /* The target must support moves in this mode. */
3344 code = optab_handler (mov_optab, imode);
3345 if (code == CODE_FOR_nothing)
3346 return NULL;
3348 x = emit_move_change_mode (imode, mode, x, force);
3349 if (x == NULL_RTX)
3350 return NULL;
3351 y = emit_move_change_mode (imode, mode, y, force);
3352 if (y == NULL_RTX)
3353 return NULL;
3354 return emit_insn (GEN_FCN (code) (x, y));
3357 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3358 Return an equivalent MEM that does not use an auto-increment. */
3361 emit_move_resolve_push (machine_mode mode, rtx x)
3363 enum rtx_code code = GET_CODE (XEXP (x, 0));
3364 rtx temp;
3366 poly_int64 adjust = GET_MODE_SIZE (mode);
3367 #ifdef PUSH_ROUNDING
3368 adjust = PUSH_ROUNDING (adjust);
3369 #endif
3370 if (code == PRE_DEC || code == POST_DEC)
3371 adjust = -adjust;
3372 else if (code == PRE_MODIFY || code == POST_MODIFY)
3374 rtx expr = XEXP (XEXP (x, 0), 1);
3376 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3377 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3378 if (GET_CODE (expr) == MINUS)
3379 val = -val;
3380 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3381 adjust = val;
3384 /* Do not use anti_adjust_stack, since we don't want to update
3385 stack_pointer_delta. */
3386 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3387 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3388 0, OPTAB_LIB_WIDEN);
3389 if (temp != stack_pointer_rtx)
3390 emit_move_insn (stack_pointer_rtx, temp);
3392 switch (code)
3394 case PRE_INC:
3395 case PRE_DEC:
3396 case PRE_MODIFY:
3397 temp = stack_pointer_rtx;
3398 break;
3399 case POST_INC:
3400 case POST_DEC:
3401 case POST_MODIFY:
3402 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3403 break;
3404 default:
3405 gcc_unreachable ();
3408 return replace_equiv_address (x, temp);
3411 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3412 X is known to satisfy push_operand, and MODE is known to be complex.
3413 Returns the last instruction emitted. */
3415 rtx_insn *
3416 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3418 scalar_mode submode = GET_MODE_INNER (mode);
3419 bool imag_first;
3421 #ifdef PUSH_ROUNDING
3422 poly_int64 submodesize = GET_MODE_SIZE (submode);
3424 /* In case we output to the stack, but the size is smaller than the
3425 machine can push exactly, we need to use move instructions. */
3426 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3428 x = emit_move_resolve_push (mode, x);
3429 return emit_move_insn (x, y);
3431 #endif
3433 /* Note that the real part always precedes the imag part in memory
3434 regardless of machine's endianness. */
3435 switch (GET_CODE (XEXP (x, 0)))
3437 case PRE_DEC:
3438 case POST_DEC:
3439 imag_first = true;
3440 break;
3441 case PRE_INC:
3442 case POST_INC:
3443 imag_first = false;
3444 break;
3445 default:
3446 gcc_unreachable ();
3449 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3450 read_complex_part (y, imag_first));
3451 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3452 read_complex_part (y, !imag_first));
3455 /* A subroutine of emit_move_complex. Perform the move from Y to X
3456 via two moves of the parts. Returns the last instruction emitted. */
3458 rtx_insn *
3459 emit_move_complex_parts (rtx x, rtx y)
3461 /* Show the output dies here. This is necessary for SUBREGs
3462 of pseudos since we cannot track their lifetimes correctly;
3463 hard regs shouldn't appear here except as return values. */
3464 if (!reload_completed && !reload_in_progress
3465 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3466 emit_clobber (x);
3468 write_complex_part (x, read_complex_part (y, false), false);
3469 write_complex_part (x, read_complex_part (y, true), true);
3471 return get_last_insn ();
3474 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3475 MODE is known to be complex. Returns the last instruction emitted. */
3477 static rtx_insn *
3478 emit_move_complex (machine_mode mode, rtx x, rtx y)
3480 bool try_int;
3482 /* Need to take special care for pushes, to maintain proper ordering
3483 of the data, and possibly extra padding. */
3484 if (push_operand (x, mode))
3485 return emit_move_complex_push (mode, x, y);
3487 /* See if we can coerce the target into moving both values at once, except
3488 for floating point where we favor moving as parts if this is easy. */
3489 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3490 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3491 && !(REG_P (x)
3492 && HARD_REGISTER_P (x)
3493 && REG_NREGS (x) == 1)
3494 && !(REG_P (y)
3495 && HARD_REGISTER_P (y)
3496 && REG_NREGS (y) == 1))
3497 try_int = false;
3498 /* Not possible if the values are inherently not adjacent. */
3499 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3500 try_int = false;
3501 /* Is possible if both are registers (or subregs of registers). */
3502 else if (register_operand (x, mode) && register_operand (y, mode))
3503 try_int = true;
3504 /* If one of the operands is a memory, and alignment constraints
3505 are friendly enough, we may be able to do combined memory operations.
3506 We do not attempt this if Y is a constant because that combination is
3507 usually better with the by-parts thing below. */
3508 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3509 && (!STRICT_ALIGNMENT
3510 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3511 try_int = true;
3512 else
3513 try_int = false;
3515 if (try_int)
3517 rtx_insn *ret;
3519 /* For memory to memory moves, optimal behavior can be had with the
3520 existing block move logic. */
3521 if (MEM_P (x) && MEM_P (y))
3523 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
3524 BLOCK_OP_NO_LIBCALL);
3525 return get_last_insn ();
3528 ret = emit_move_via_integer (mode, x, y, true);
3529 if (ret)
3530 return ret;
3533 return emit_move_complex_parts (x, y);
3536 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3537 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3539 static rtx_insn *
3540 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3542 rtx_insn *ret;
3544 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3545 if (mode != CCmode)
3547 enum insn_code code = optab_handler (mov_optab, CCmode);
3548 if (code != CODE_FOR_nothing)
3550 x = emit_move_change_mode (CCmode, mode, x, true);
3551 y = emit_move_change_mode (CCmode, mode, y, true);
3552 return emit_insn (GEN_FCN (code) (x, y));
3556 /* Otherwise, find the MODE_INT mode of the same width. */
3557 ret = emit_move_via_integer (mode, x, y, false);
3558 gcc_assert (ret != NULL);
3559 return ret;
3562 /* Return true if word I of OP lies entirely in the
3563 undefined bits of a paradoxical subreg. */
3565 static bool
3566 undefined_operand_subword_p (const_rtx op, int i)
3568 if (GET_CODE (op) != SUBREG)
3569 return false;
3570 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3571 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3572 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3573 || known_le (offset, -UNITS_PER_WORD));
3576 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3577 MODE is any multi-word or full-word mode that lacks a move_insn
3578 pattern. Note that you will get better code if you define such
3579 patterns, even if they must turn into multiple assembler instructions. */
3581 static rtx_insn *
3582 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3584 rtx_insn *last_insn = 0;
3585 rtx_insn *seq;
3586 rtx inner;
3587 bool need_clobber;
3588 int i, mode_size;
3590 /* This function can only handle cases where the number of words is
3591 known at compile time. */
3592 mode_size = GET_MODE_SIZE (mode).to_constant ();
3593 gcc_assert (mode_size >= UNITS_PER_WORD);
3595 /* If X is a push on the stack, do the push now and replace
3596 X with a reference to the stack pointer. */
3597 if (push_operand (x, mode))
3598 x = emit_move_resolve_push (mode, x);
3600 /* If we are in reload, see if either operand is a MEM whose address
3601 is scheduled for replacement. */
3602 if (reload_in_progress && MEM_P (x)
3603 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3604 x = replace_equiv_address_nv (x, inner);
3605 if (reload_in_progress && MEM_P (y)
3606 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3607 y = replace_equiv_address_nv (y, inner);
3609 start_sequence ();
3611 need_clobber = false;
3612 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3614 rtx xpart = operand_subword (x, i, 1, mode);
3615 rtx ypart;
3617 /* Do not generate code for a move if it would come entirely
3618 from the undefined bits of a paradoxical subreg. */
3619 if (undefined_operand_subword_p (y, i))
3620 continue;
3622 ypart = operand_subword (y, i, 1, mode);
3624 /* If we can't get a part of Y, put Y into memory if it is a
3625 constant. Otherwise, force it into a register. Then we must
3626 be able to get a part of Y. */
3627 if (ypart == 0 && CONSTANT_P (y))
3629 y = use_anchored_address (force_const_mem (mode, y));
3630 ypart = operand_subword (y, i, 1, mode);
3632 else if (ypart == 0)
3633 ypart = operand_subword_force (y, i, mode);
3635 gcc_assert (xpart && ypart);
3637 need_clobber |= (GET_CODE (xpart) == SUBREG);
3639 last_insn = emit_move_insn (xpart, ypart);
3642 seq = get_insns ();
3643 end_sequence ();
3645 /* Show the output dies here. This is necessary for SUBREGs
3646 of pseudos since we cannot track their lifetimes correctly;
3647 hard regs shouldn't appear here except as return values.
3648 We never want to emit such a clobber after reload. */
3649 if (x != y
3650 && ! (reload_in_progress || reload_completed)
3651 && need_clobber != 0)
3652 emit_clobber (x);
3654 emit_insn (seq);
3656 return last_insn;
3659 /* Low level part of emit_move_insn.
3660 Called just like emit_move_insn, but assumes X and Y
3661 are basically valid. */
3663 rtx_insn *
3664 emit_move_insn_1 (rtx x, rtx y)
3666 machine_mode mode = GET_MODE (x);
3667 enum insn_code code;
3669 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3671 code = optab_handler (mov_optab, mode);
3672 if (code != CODE_FOR_nothing)
3673 return emit_insn (GEN_FCN (code) (x, y));
3675 /* Expand complex moves by moving real part and imag part. */
3676 if (COMPLEX_MODE_P (mode))
3677 return emit_move_complex (mode, x, y);
3679 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3680 || ALL_FIXED_POINT_MODE_P (mode))
3682 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3684 /* If we can't find an integer mode, use multi words. */
3685 if (result)
3686 return result;
3687 else
3688 return emit_move_multi_word (mode, x, y);
3691 if (GET_MODE_CLASS (mode) == MODE_CC)
3692 return emit_move_ccmode (mode, x, y);
3694 /* Try using a move pattern for the corresponding integer mode. This is
3695 only safe when simplify_subreg can convert MODE constants into integer
3696 constants. At present, it can only do this reliably if the value
3697 fits within a HOST_WIDE_INT. */
3698 if (!CONSTANT_P (y)
3699 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3701 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3703 if (ret)
3705 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3706 return ret;
3710 return emit_move_multi_word (mode, x, y);
3713 /* Generate code to copy Y into X.
3714 Both Y and X must have the same mode, except that
3715 Y can be a constant with VOIDmode.
3716 This mode cannot be BLKmode; use emit_block_move for that.
3718 Return the last instruction emitted. */
3720 rtx_insn *
3721 emit_move_insn (rtx x, rtx y)
3723 machine_mode mode = GET_MODE (x);
3724 rtx y_cst = NULL_RTX;
3725 rtx_insn *last_insn;
3726 rtx set;
3728 gcc_assert (mode != BLKmode
3729 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3731 if (CONSTANT_P (y))
3733 if (optimize
3734 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3735 && (last_insn = compress_float_constant (x, y)))
3736 return last_insn;
3738 y_cst = y;
3740 if (!targetm.legitimate_constant_p (mode, y))
3742 y = force_const_mem (mode, y);
3744 /* If the target's cannot_force_const_mem prevented the spill,
3745 assume that the target's move expanders will also take care
3746 of the non-legitimate constant. */
3747 if (!y)
3748 y = y_cst;
3749 else
3750 y = use_anchored_address (y);
3754 /* If X or Y are memory references, verify that their addresses are valid
3755 for the machine. */
3756 if (MEM_P (x)
3757 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3758 MEM_ADDR_SPACE (x))
3759 && ! push_operand (x, GET_MODE (x))))
3760 x = validize_mem (x);
3762 if (MEM_P (y)
3763 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3764 MEM_ADDR_SPACE (y)))
3765 y = validize_mem (y);
3767 gcc_assert (mode != BLKmode);
3769 last_insn = emit_move_insn_1 (x, y);
3771 if (y_cst && REG_P (x)
3772 && (set = single_set (last_insn)) != NULL_RTX
3773 && SET_DEST (set) == x
3774 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3775 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3777 return last_insn;
3780 /* Generate the body of an instruction to copy Y into X.
3781 It may be a list of insns, if one insn isn't enough. */
3783 rtx_insn *
3784 gen_move_insn (rtx x, rtx y)
3786 rtx_insn *seq;
3788 start_sequence ();
3789 emit_move_insn_1 (x, y);
3790 seq = get_insns ();
3791 end_sequence ();
3792 return seq;
3795 /* If Y is representable exactly in a narrower mode, and the target can
3796 perform the extension directly from constant or memory, then emit the
3797 move as an extension. */
3799 static rtx_insn *
3800 compress_float_constant (rtx x, rtx y)
3802 machine_mode dstmode = GET_MODE (x);
3803 machine_mode orig_srcmode = GET_MODE (y);
3804 machine_mode srcmode;
3805 const REAL_VALUE_TYPE *r;
3806 int oldcost, newcost;
3807 bool speed = optimize_insn_for_speed_p ();
3809 r = CONST_DOUBLE_REAL_VALUE (y);
3811 if (targetm.legitimate_constant_p (dstmode, y))
3812 oldcost = set_src_cost (y, orig_srcmode, speed);
3813 else
3814 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3816 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3818 enum insn_code ic;
3819 rtx trunc_y;
3820 rtx_insn *last_insn;
3822 /* Skip if the target can't extend this way. */
3823 ic = can_extend_p (dstmode, srcmode, 0);
3824 if (ic == CODE_FOR_nothing)
3825 continue;
3827 /* Skip if the narrowed value isn't exact. */
3828 if (! exact_real_truncate (srcmode, r))
3829 continue;
3831 trunc_y = const_double_from_real_value (*r, srcmode);
3833 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3835 /* Skip if the target needs extra instructions to perform
3836 the extension. */
3837 if (!insn_operand_matches (ic, 1, trunc_y))
3838 continue;
3839 /* This is valid, but may not be cheaper than the original. */
3840 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3841 dstmode, speed);
3842 if (oldcost < newcost)
3843 continue;
3845 else if (float_extend_from_mem[dstmode][srcmode])
3847 trunc_y = force_const_mem (srcmode, trunc_y);
3848 /* This is valid, but may not be cheaper than the original. */
3849 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3850 dstmode, speed);
3851 if (oldcost < newcost)
3852 continue;
3853 trunc_y = validize_mem (trunc_y);
3855 else
3856 continue;
3858 /* For CSE's benefit, force the compressed constant pool entry
3859 into a new pseudo. This constant may be used in different modes,
3860 and if not, combine will put things back together for us. */
3861 trunc_y = force_reg (srcmode, trunc_y);
3863 /* If x is a hard register, perform the extension into a pseudo,
3864 so that e.g. stack realignment code is aware of it. */
3865 rtx target = x;
3866 if (REG_P (x) && HARD_REGISTER_P (x))
3867 target = gen_reg_rtx (dstmode);
3869 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3870 last_insn = get_last_insn ();
3872 if (REG_P (target))
3873 set_unique_reg_note (last_insn, REG_EQUAL, y);
3875 if (target != x)
3876 return emit_move_insn (x, target);
3877 return last_insn;
3880 return NULL;
3883 /* Pushing data onto the stack. */
3885 /* Push a block of length SIZE (perhaps variable)
3886 and return an rtx to address the beginning of the block.
3887 The value may be virtual_outgoing_args_rtx.
3889 EXTRA is the number of bytes of padding to push in addition to SIZE.
3890 BELOW nonzero means this padding comes at low addresses;
3891 otherwise, the padding comes at high addresses. */
3894 push_block (rtx size, poly_int64 extra, int below)
3896 rtx temp;
3898 size = convert_modes (Pmode, ptr_mode, size, 1);
3899 if (CONSTANT_P (size))
3900 anti_adjust_stack (plus_constant (Pmode, size, extra));
3901 else if (REG_P (size) && known_eq (extra, 0))
3902 anti_adjust_stack (size);
3903 else
3905 temp = copy_to_mode_reg (Pmode, size);
3906 if (maybe_ne (extra, 0))
3907 temp = expand_binop (Pmode, add_optab, temp,
3908 gen_int_mode (extra, Pmode),
3909 temp, 0, OPTAB_LIB_WIDEN);
3910 anti_adjust_stack (temp);
3913 if (STACK_GROWS_DOWNWARD)
3915 temp = virtual_outgoing_args_rtx;
3916 if (maybe_ne (extra, 0) && below)
3917 temp = plus_constant (Pmode, temp, extra);
3919 else
3921 poly_int64 csize;
3922 if (poly_int_rtx_p (size, &csize))
3923 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3924 -csize - (below ? 0 : extra));
3925 else if (maybe_ne (extra, 0) && !below)
3926 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3927 negate_rtx (Pmode, plus_constant (Pmode, size,
3928 extra)));
3929 else
3930 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3931 negate_rtx (Pmode, size));
3934 return memory_address (NARROWEST_INT_MODE, temp);
3937 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3939 static rtx
3940 mem_autoinc_base (rtx mem)
3942 if (MEM_P (mem))
3944 rtx addr = XEXP (mem, 0);
3945 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3946 return XEXP (addr, 0);
3948 return NULL;
3951 /* A utility routine used here, in reload, and in try_split. The insns
3952 after PREV up to and including LAST are known to adjust the stack,
3953 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3954 placing notes as appropriate. PREV may be NULL, indicating the
3955 entire insn sequence prior to LAST should be scanned.
3957 The set of allowed stack pointer modifications is small:
3958 (1) One or more auto-inc style memory references (aka pushes),
3959 (2) One or more addition/subtraction with the SP as destination,
3960 (3) A single move insn with the SP as destination,
3961 (4) A call_pop insn,
3962 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3964 Insns in the sequence that do not modify the SP are ignored,
3965 except for noreturn calls.
3967 The return value is the amount of adjustment that can be trivially
3968 verified, via immediate operand or auto-inc. If the adjustment
3969 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
3971 poly_int64
3972 find_args_size_adjust (rtx_insn *insn)
3974 rtx dest, set, pat;
3975 int i;
3977 pat = PATTERN (insn);
3978 set = NULL;
3980 /* Look for a call_pop pattern. */
3981 if (CALL_P (insn))
3983 /* We have to allow non-call_pop patterns for the case
3984 of emit_single_push_insn of a TLS address. */
3985 if (GET_CODE (pat) != PARALLEL)
3986 return 0;
3988 /* All call_pop have a stack pointer adjust in the parallel.
3989 The call itself is always first, and the stack adjust is
3990 usually last, so search from the end. */
3991 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3993 set = XVECEXP (pat, 0, i);
3994 if (GET_CODE (set) != SET)
3995 continue;
3996 dest = SET_DEST (set);
3997 if (dest == stack_pointer_rtx)
3998 break;
4000 /* We'd better have found the stack pointer adjust. */
4001 if (i == 0)
4002 return 0;
4003 /* Fall through to process the extracted SET and DEST
4004 as if it was a standalone insn. */
4006 else if (GET_CODE (pat) == SET)
4007 set = pat;
4008 else if ((set = single_set (insn)) != NULL)
4010 else if (GET_CODE (pat) == PARALLEL)
4012 /* ??? Some older ports use a parallel with a stack adjust
4013 and a store for a PUSH_ROUNDING pattern, rather than a
4014 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4015 /* ??? See h8300 and m68k, pushqi1. */
4016 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4018 set = XVECEXP (pat, 0, i);
4019 if (GET_CODE (set) != SET)
4020 continue;
4021 dest = SET_DEST (set);
4022 if (dest == stack_pointer_rtx)
4023 break;
4025 /* We do not expect an auto-inc of the sp in the parallel. */
4026 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4027 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4028 != stack_pointer_rtx);
4030 if (i < 0)
4031 return 0;
4033 else
4034 return 0;
4036 dest = SET_DEST (set);
4038 /* Look for direct modifications of the stack pointer. */
4039 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4041 /* Look for a trivial adjustment, otherwise assume nothing. */
4042 /* Note that the SPU restore_stack_block pattern refers to
4043 the stack pointer in V4SImode. Consider that non-trivial. */
4044 poly_int64 offset;
4045 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4046 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4047 return offset;
4048 /* ??? Reload can generate no-op moves, which will be cleaned
4049 up later. Recognize it and continue searching. */
4050 else if (rtx_equal_p (dest, SET_SRC (set)))
4051 return 0;
4052 else
4053 return HOST_WIDE_INT_MIN;
4055 else
4057 rtx mem, addr;
4059 /* Otherwise only think about autoinc patterns. */
4060 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4062 mem = dest;
4063 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4064 != stack_pointer_rtx);
4066 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4067 mem = SET_SRC (set);
4068 else
4069 return 0;
4071 addr = XEXP (mem, 0);
4072 switch (GET_CODE (addr))
4074 case PRE_INC:
4075 case POST_INC:
4076 return GET_MODE_SIZE (GET_MODE (mem));
4077 case PRE_DEC:
4078 case POST_DEC:
4079 return -GET_MODE_SIZE (GET_MODE (mem));
4080 case PRE_MODIFY:
4081 case POST_MODIFY:
4082 addr = XEXP (addr, 1);
4083 gcc_assert (GET_CODE (addr) == PLUS);
4084 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4085 return rtx_to_poly_int64 (XEXP (addr, 1));
4086 default:
4087 gcc_unreachable ();
4092 poly_int64
4093 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4094 poly_int64 end_args_size)
4096 poly_int64 args_size = end_args_size;
4097 bool saw_unknown = false;
4098 rtx_insn *insn;
4100 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4102 if (!NONDEBUG_INSN_P (insn))
4103 continue;
4105 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4106 a call argument containing a TLS address that itself requires
4107 a call to __tls_get_addr. The handling of stack_pointer_delta
4108 in emit_single_push_insn is supposed to ensure that any such
4109 notes are already correct. */
4110 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4111 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4113 poly_int64 this_delta = find_args_size_adjust (insn);
4114 if (known_eq (this_delta, 0))
4116 if (!CALL_P (insn)
4117 || ACCUMULATE_OUTGOING_ARGS
4118 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4119 continue;
4122 gcc_assert (!saw_unknown);
4123 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4124 saw_unknown = true;
4126 if (!note)
4127 add_args_size_note (insn, args_size);
4128 if (STACK_GROWS_DOWNWARD)
4129 this_delta = -poly_uint64 (this_delta);
4131 if (saw_unknown)
4132 args_size = HOST_WIDE_INT_MIN;
4133 else
4134 args_size -= this_delta;
4137 return args_size;
4140 #ifdef PUSH_ROUNDING
4141 /* Emit single push insn. */
4143 static void
4144 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4146 rtx dest_addr;
4147 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4148 rtx dest;
4149 enum insn_code icode;
4151 /* If there is push pattern, use it. Otherwise try old way of throwing
4152 MEM representing push operation to move expander. */
4153 icode = optab_handler (push_optab, mode);
4154 if (icode != CODE_FOR_nothing)
4156 struct expand_operand ops[1];
4158 create_input_operand (&ops[0], x, mode);
4159 if (maybe_expand_insn (icode, 1, ops))
4160 return;
4162 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4163 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4164 /* If we are to pad downward, adjust the stack pointer first and
4165 then store X into the stack location using an offset. This is
4166 because emit_move_insn does not know how to pad; it does not have
4167 access to type. */
4168 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4170 emit_move_insn (stack_pointer_rtx,
4171 expand_binop (Pmode,
4172 STACK_GROWS_DOWNWARD ? sub_optab
4173 : add_optab,
4174 stack_pointer_rtx,
4175 gen_int_mode (rounded_size, Pmode),
4176 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4178 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4179 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4180 /* We have already decremented the stack pointer, so get the
4181 previous value. */
4182 offset += rounded_size;
4184 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4185 /* We have already incremented the stack pointer, so get the
4186 previous value. */
4187 offset -= rounded_size;
4189 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4191 else
4193 if (STACK_GROWS_DOWNWARD)
4194 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4195 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4196 else
4197 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4198 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4200 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4203 dest = gen_rtx_MEM (mode, dest_addr);
4205 if (type != 0)
4207 set_mem_attributes (dest, type, 1);
4209 if (cfun->tail_call_marked)
4210 /* Function incoming arguments may overlap with sibling call
4211 outgoing arguments and we cannot allow reordering of reads
4212 from function arguments with stores to outgoing arguments
4213 of sibling calls. */
4214 set_mem_alias_set (dest, 0);
4216 emit_move_insn (dest, x);
4219 /* Emit and annotate a single push insn. */
4221 static void
4222 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4224 poly_int64 delta, old_delta = stack_pointer_delta;
4225 rtx_insn *prev = get_last_insn ();
4226 rtx_insn *last;
4228 emit_single_push_insn_1 (mode, x, type);
4230 /* Adjust stack_pointer_delta to describe the situation after the push
4231 we just performed. Note that we must do this after the push rather
4232 than before the push in case calculating X needs pushes and pops of
4233 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4234 for such pushes and pops must not include the effect of the future
4235 push of X. */
4236 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4238 last = get_last_insn ();
4240 /* Notice the common case where we emitted exactly one insn. */
4241 if (PREV_INSN (last) == prev)
4243 add_args_size_note (last, stack_pointer_delta);
4244 return;
4247 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4248 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4249 || known_eq (delta, old_delta));
4251 #endif
4253 /* If reading SIZE bytes from X will end up reading from
4254 Y return the number of bytes that overlap. Return -1
4255 if there is no overlap or -2 if we can't determine
4256 (for example when X and Y have different base registers). */
4258 static int
4259 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4261 rtx tmp = plus_constant (Pmode, x, size);
4262 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4264 if (!CONST_INT_P (sub))
4265 return -2;
4267 HOST_WIDE_INT val = INTVAL (sub);
4269 return IN_RANGE (val, 1, size) ? val : -1;
4272 /* Generate code to push X onto the stack, assuming it has mode MODE and
4273 type TYPE.
4274 MODE is redundant except when X is a CONST_INT (since they don't
4275 carry mode info).
4276 SIZE is an rtx for the size of data to be copied (in bytes),
4277 needed only if X is BLKmode.
4278 Return true if successful. May return false if asked to push a
4279 partial argument during a sibcall optimization (as specified by
4280 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4281 to not overlap.
4283 ALIGN (in bits) is maximum alignment we can assume.
4285 If PARTIAL and REG are both nonzero, then copy that many of the first
4286 bytes of X into registers starting with REG, and push the rest of X.
4287 The amount of space pushed is decreased by PARTIAL bytes.
4288 REG must be a hard register in this case.
4289 If REG is zero but PARTIAL is not, take any all others actions for an
4290 argument partially in registers, but do not actually load any
4291 registers.
4293 EXTRA is the amount in bytes of extra space to leave next to this arg.
4294 This is ignored if an argument block has already been allocated.
4296 On a machine that lacks real push insns, ARGS_ADDR is the address of
4297 the bottom of the argument block for this call. We use indexing off there
4298 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4299 argument block has not been preallocated.
4301 ARGS_SO_FAR is the size of args previously pushed for this call.
4303 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4304 for arguments passed in registers. If nonzero, it will be the number
4305 of bytes required. */
4307 bool
4308 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4309 unsigned int align, int partial, rtx reg, poly_int64 extra,
4310 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4311 rtx alignment_pad, bool sibcall_p)
4313 rtx xinner;
4314 pad_direction stack_direction
4315 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4317 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4318 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4319 Default is below for small data on big-endian machines; else above. */
4320 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4322 /* Invert direction if stack is post-decrement.
4323 FIXME: why? */
4324 if (STACK_PUSH_CODE == POST_DEC)
4325 if (where_pad != PAD_NONE)
4326 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4328 xinner = x;
4330 int nregs = partial / UNITS_PER_WORD;
4331 rtx *tmp_regs = NULL;
4332 int overlapping = 0;
4334 if (mode == BLKmode
4335 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4337 /* Copy a block into the stack, entirely or partially. */
4339 rtx temp;
4340 int used;
4341 int offset;
4342 int skip;
4344 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4345 used = partial - offset;
4347 if (mode != BLKmode)
4349 /* A value is to be stored in an insufficiently aligned
4350 stack slot; copy via a suitably aligned slot if
4351 necessary. */
4352 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4353 if (!MEM_P (xinner))
4355 temp = assign_temp (type, 1, 1);
4356 emit_move_insn (temp, xinner);
4357 xinner = temp;
4361 gcc_assert (size);
4363 /* USED is now the # of bytes we need not copy to the stack
4364 because registers will take care of them. */
4366 if (partial != 0)
4367 xinner = adjust_address (xinner, BLKmode, used);
4369 /* If the partial register-part of the arg counts in its stack size,
4370 skip the part of stack space corresponding to the registers.
4371 Otherwise, start copying to the beginning of the stack space,
4372 by setting SKIP to 0. */
4373 skip = (reg_parm_stack_space == 0) ? 0 : used;
4375 #ifdef PUSH_ROUNDING
4376 /* Do it with several push insns if that doesn't take lots of insns
4377 and if there is no difficulty with push insns that skip bytes
4378 on the stack for alignment purposes. */
4379 if (args_addr == 0
4380 && PUSH_ARGS
4381 && CONST_INT_P (size)
4382 && skip == 0
4383 && MEM_ALIGN (xinner) >= align
4384 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4385 /* Here we avoid the case of a structure whose weak alignment
4386 forces many pushes of a small amount of data,
4387 and such small pushes do rounding that causes trouble. */
4388 && ((!targetm.slow_unaligned_access (word_mode, align))
4389 || align >= BIGGEST_ALIGNMENT
4390 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4391 align / BITS_PER_UNIT))
4392 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4394 /* Push padding now if padding above and stack grows down,
4395 or if padding below and stack grows up.
4396 But if space already allocated, this has already been done. */
4397 if (maybe_ne (extra, 0)
4398 && args_addr == 0
4399 && where_pad != PAD_NONE
4400 && where_pad != stack_direction)
4401 anti_adjust_stack (gen_int_mode (extra, Pmode));
4403 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4405 else
4406 #endif /* PUSH_ROUNDING */
4408 rtx target;
4410 /* Otherwise make space on the stack and copy the data
4411 to the address of that space. */
4413 /* Deduct words put into registers from the size we must copy. */
4414 if (partial != 0)
4416 if (CONST_INT_P (size))
4417 size = GEN_INT (INTVAL (size) - used);
4418 else
4419 size = expand_binop (GET_MODE (size), sub_optab, size,
4420 gen_int_mode (used, GET_MODE (size)),
4421 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4424 /* Get the address of the stack space.
4425 In this case, we do not deal with EXTRA separately.
4426 A single stack adjust will do. */
4427 poly_int64 offset;
4428 if (! args_addr)
4430 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4431 extra = 0;
4433 else if (poly_int_rtx_p (args_so_far, &offset))
4434 temp = memory_address (BLKmode,
4435 plus_constant (Pmode, args_addr,
4436 skip + offset));
4437 else
4438 temp = memory_address (BLKmode,
4439 plus_constant (Pmode,
4440 gen_rtx_PLUS (Pmode,
4441 args_addr,
4442 args_so_far),
4443 skip));
4445 if (!ACCUMULATE_OUTGOING_ARGS)
4447 /* If the source is referenced relative to the stack pointer,
4448 copy it to another register to stabilize it. We do not need
4449 to do this if we know that we won't be changing sp. */
4451 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4452 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4453 temp = copy_to_reg (temp);
4456 target = gen_rtx_MEM (BLKmode, temp);
4458 /* We do *not* set_mem_attributes here, because incoming arguments
4459 may overlap with sibling call outgoing arguments and we cannot
4460 allow reordering of reads from function arguments with stores
4461 to outgoing arguments of sibling calls. We do, however, want
4462 to record the alignment of the stack slot. */
4463 /* ALIGN may well be better aligned than TYPE, e.g. due to
4464 PARM_BOUNDARY. Assume the caller isn't lying. */
4465 set_mem_align (target, align);
4467 /* If part should go in registers and pushing to that part would
4468 overwrite some of the values that need to go into regs, load the
4469 overlapping values into temporary pseudos to be moved into the hard
4470 regs at the end after the stack pushing has completed.
4471 We cannot load them directly into the hard regs here because
4472 they can be clobbered by the block move expansions.
4473 See PR 65358. */
4475 if (partial > 0 && reg != 0 && mode == BLKmode
4476 && GET_CODE (reg) != PARALLEL)
4478 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4479 if (overlapping > 0)
4481 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4482 overlapping /= UNITS_PER_WORD;
4484 tmp_regs = XALLOCAVEC (rtx, overlapping);
4486 for (int i = 0; i < overlapping; i++)
4487 tmp_regs[i] = gen_reg_rtx (word_mode);
4489 for (int i = 0; i < overlapping; i++)
4490 emit_move_insn (tmp_regs[i],
4491 operand_subword_force (target, i, mode));
4493 else if (overlapping == -1)
4494 overlapping = 0;
4495 /* Could not determine whether there is overlap.
4496 Fail the sibcall. */
4497 else
4499 overlapping = 0;
4500 if (sibcall_p)
4501 return false;
4504 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4507 else if (partial > 0)
4509 /* Scalar partly in registers. This case is only supported
4510 for fixed-wdth modes. */
4511 int size = GET_MODE_SIZE (mode).to_constant ();
4512 size /= UNITS_PER_WORD;
4513 int i;
4514 int not_stack;
4515 /* # bytes of start of argument
4516 that we must make space for but need not store. */
4517 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4518 int args_offset = INTVAL (args_so_far);
4519 int skip;
4521 /* Push padding now if padding above and stack grows down,
4522 or if padding below and stack grows up.
4523 But if space already allocated, this has already been done. */
4524 if (maybe_ne (extra, 0)
4525 && args_addr == 0
4526 && where_pad != PAD_NONE
4527 && where_pad != stack_direction)
4528 anti_adjust_stack (gen_int_mode (extra, Pmode));
4530 /* If we make space by pushing it, we might as well push
4531 the real data. Otherwise, we can leave OFFSET nonzero
4532 and leave the space uninitialized. */
4533 if (args_addr == 0)
4534 offset = 0;
4536 /* Now NOT_STACK gets the number of words that we don't need to
4537 allocate on the stack. Convert OFFSET to words too. */
4538 not_stack = (partial - offset) / UNITS_PER_WORD;
4539 offset /= UNITS_PER_WORD;
4541 /* If the partial register-part of the arg counts in its stack size,
4542 skip the part of stack space corresponding to the registers.
4543 Otherwise, start copying to the beginning of the stack space,
4544 by setting SKIP to 0. */
4545 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4547 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4548 x = validize_mem (force_const_mem (mode, x));
4550 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4551 SUBREGs of such registers are not allowed. */
4552 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4553 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4554 x = copy_to_reg (x);
4556 /* Loop over all the words allocated on the stack for this arg. */
4557 /* We can do it by words, because any scalar bigger than a word
4558 has a size a multiple of a word. */
4559 for (i = size - 1; i >= not_stack; i--)
4560 if (i >= not_stack + offset)
4561 if (!emit_push_insn (operand_subword_force (x, i, mode),
4562 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4563 0, args_addr,
4564 GEN_INT (args_offset + ((i - not_stack + skip)
4565 * UNITS_PER_WORD)),
4566 reg_parm_stack_space, alignment_pad, sibcall_p))
4567 return false;
4569 else
4571 rtx addr;
4572 rtx dest;
4574 /* Push padding now if padding above and stack grows down,
4575 or if padding below and stack grows up.
4576 But if space already allocated, this has already been done. */
4577 if (maybe_ne (extra, 0)
4578 && args_addr == 0
4579 && where_pad != PAD_NONE
4580 && where_pad != stack_direction)
4581 anti_adjust_stack (gen_int_mode (extra, Pmode));
4583 #ifdef PUSH_ROUNDING
4584 if (args_addr == 0 && PUSH_ARGS)
4585 emit_single_push_insn (mode, x, type);
4586 else
4587 #endif
4589 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4590 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4592 /* We do *not* set_mem_attributes here, because incoming arguments
4593 may overlap with sibling call outgoing arguments and we cannot
4594 allow reordering of reads from function arguments with stores
4595 to outgoing arguments of sibling calls. We do, however, want
4596 to record the alignment of the stack slot. */
4597 /* ALIGN may well be better aligned than TYPE, e.g. due to
4598 PARM_BOUNDARY. Assume the caller isn't lying. */
4599 set_mem_align (dest, align);
4601 emit_move_insn (dest, x);
4605 /* Move the partial arguments into the registers and any overlapping
4606 values that we moved into the pseudos in tmp_regs. */
4607 if (partial > 0 && reg != 0)
4609 /* Handle calls that pass values in multiple non-contiguous locations.
4610 The Irix 6 ABI has examples of this. */
4611 if (GET_CODE (reg) == PARALLEL)
4612 emit_group_load (reg, x, type, -1);
4613 else
4615 gcc_assert (partial % UNITS_PER_WORD == 0);
4616 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4618 for (int i = 0; i < overlapping; i++)
4619 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4620 + nregs - overlapping + i),
4621 tmp_regs[i]);
4626 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4627 anti_adjust_stack (gen_int_mode (extra, Pmode));
4629 if (alignment_pad && args_addr == 0)
4630 anti_adjust_stack (alignment_pad);
4632 return true;
4635 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4636 operations. */
4638 static rtx
4639 get_subtarget (rtx x)
4641 return (optimize
4642 || x == 0
4643 /* Only registers can be subtargets. */
4644 || !REG_P (x)
4645 /* Don't use hard regs to avoid extending their life. */
4646 || REGNO (x) < FIRST_PSEUDO_REGISTER
4647 ? 0 : x);
4650 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4651 FIELD is a bitfield. Returns true if the optimization was successful,
4652 and there's nothing else to do. */
4654 static bool
4655 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4656 poly_uint64 pbitpos,
4657 poly_uint64 pbitregion_start,
4658 poly_uint64 pbitregion_end,
4659 machine_mode mode1, rtx str_rtx,
4660 tree to, tree src, bool reverse)
4662 /* str_mode is not guaranteed to be a scalar type. */
4663 machine_mode str_mode = GET_MODE (str_rtx);
4664 unsigned int str_bitsize;
4665 tree op0, op1;
4666 rtx value, result;
4667 optab binop;
4668 gimple *srcstmt;
4669 enum tree_code code;
4671 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4672 if (mode1 != VOIDmode
4673 || !pbitsize.is_constant (&bitsize)
4674 || !pbitpos.is_constant (&bitpos)
4675 || !pbitregion_start.is_constant (&bitregion_start)
4676 || !pbitregion_end.is_constant (&bitregion_end)
4677 || bitsize >= BITS_PER_WORD
4678 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
4679 || str_bitsize > BITS_PER_WORD
4680 || TREE_SIDE_EFFECTS (to)
4681 || TREE_THIS_VOLATILE (to))
4682 return false;
4684 STRIP_NOPS (src);
4685 if (TREE_CODE (src) != SSA_NAME)
4686 return false;
4687 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4688 return false;
4690 srcstmt = get_gimple_for_ssa_name (src);
4691 if (!srcstmt
4692 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4693 return false;
4695 code = gimple_assign_rhs_code (srcstmt);
4697 op0 = gimple_assign_rhs1 (srcstmt);
4699 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4700 to find its initialization. Hopefully the initialization will
4701 be from a bitfield load. */
4702 if (TREE_CODE (op0) == SSA_NAME)
4704 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4706 /* We want to eventually have OP0 be the same as TO, which
4707 should be a bitfield. */
4708 if (!op0stmt
4709 || !is_gimple_assign (op0stmt)
4710 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4711 return false;
4712 op0 = gimple_assign_rhs1 (op0stmt);
4715 op1 = gimple_assign_rhs2 (srcstmt);
4717 if (!operand_equal_p (to, op0, 0))
4718 return false;
4720 if (MEM_P (str_rtx))
4722 unsigned HOST_WIDE_INT offset1;
4724 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4725 str_bitsize = BITS_PER_WORD;
4727 scalar_int_mode best_mode;
4728 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4729 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4730 return false;
4731 str_mode = best_mode;
4732 str_bitsize = GET_MODE_BITSIZE (best_mode);
4734 offset1 = bitpos;
4735 bitpos %= str_bitsize;
4736 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4737 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4739 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4740 return false;
4742 /* If the bit field covers the whole REG/MEM, store_field
4743 will likely generate better code. */
4744 if (bitsize >= str_bitsize)
4745 return false;
4747 /* We can't handle fields split across multiple entities. */
4748 if (bitpos + bitsize > str_bitsize)
4749 return false;
4751 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4752 bitpos = str_bitsize - bitpos - bitsize;
4754 switch (code)
4756 case PLUS_EXPR:
4757 case MINUS_EXPR:
4758 /* For now, just optimize the case of the topmost bitfield
4759 where we don't need to do any masking and also
4760 1 bit bitfields where xor can be used.
4761 We might win by one instruction for the other bitfields
4762 too if insv/extv instructions aren't used, so that
4763 can be added later. */
4764 if ((reverse || bitpos + bitsize != str_bitsize)
4765 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4766 break;
4768 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4769 value = convert_modes (str_mode,
4770 TYPE_MODE (TREE_TYPE (op1)), value,
4771 TYPE_UNSIGNED (TREE_TYPE (op1)));
4773 /* We may be accessing data outside the field, which means
4774 we can alias adjacent data. */
4775 if (MEM_P (str_rtx))
4777 str_rtx = shallow_copy_rtx (str_rtx);
4778 set_mem_alias_set (str_rtx, 0);
4779 set_mem_expr (str_rtx, 0);
4782 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4784 value = expand_and (str_mode, value, const1_rtx, NULL);
4785 binop = xor_optab;
4787 else
4788 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4790 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4791 if (reverse)
4792 value = flip_storage_order (str_mode, value);
4793 result = expand_binop (str_mode, binop, str_rtx,
4794 value, str_rtx, 1, OPTAB_WIDEN);
4795 if (result != str_rtx)
4796 emit_move_insn (str_rtx, result);
4797 return true;
4799 case BIT_IOR_EXPR:
4800 case BIT_XOR_EXPR:
4801 if (TREE_CODE (op1) != INTEGER_CST)
4802 break;
4803 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4804 value = convert_modes (str_mode,
4805 TYPE_MODE (TREE_TYPE (op1)), value,
4806 TYPE_UNSIGNED (TREE_TYPE (op1)));
4808 /* We may be accessing data outside the field, which means
4809 we can alias adjacent data. */
4810 if (MEM_P (str_rtx))
4812 str_rtx = shallow_copy_rtx (str_rtx);
4813 set_mem_alias_set (str_rtx, 0);
4814 set_mem_expr (str_rtx, 0);
4817 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4818 if (bitpos + bitsize != str_bitsize)
4820 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4821 str_mode);
4822 value = expand_and (str_mode, value, mask, NULL_RTX);
4824 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4825 if (reverse)
4826 value = flip_storage_order (str_mode, value);
4827 result = expand_binop (str_mode, binop, str_rtx,
4828 value, str_rtx, 1, OPTAB_WIDEN);
4829 if (result != str_rtx)
4830 emit_move_insn (str_rtx, result);
4831 return true;
4833 default:
4834 break;
4837 return false;
4840 /* In the C++ memory model, consecutive bit fields in a structure are
4841 considered one memory location.
4843 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4844 returns the bit range of consecutive bits in which this COMPONENT_REF
4845 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4846 and *OFFSET may be adjusted in the process.
4848 If the access does not need to be restricted, 0 is returned in both
4849 *BITSTART and *BITEND. */
4851 void
4852 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
4853 poly_int64_pod *bitpos, tree *offset)
4855 poly_int64 bitoffset;
4856 tree field, repr;
4858 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4860 field = TREE_OPERAND (exp, 1);
4861 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4862 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4863 need to limit the range we can access. */
4864 if (!repr)
4866 *bitstart = *bitend = 0;
4867 return;
4870 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4871 part of a larger bit field, then the representative does not serve any
4872 useful purpose. This can occur in Ada. */
4873 if (handled_component_p (TREE_OPERAND (exp, 0)))
4875 machine_mode rmode;
4876 poly_int64 rbitsize, rbitpos;
4877 tree roffset;
4878 int unsignedp, reversep, volatilep = 0;
4879 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4880 &roffset, &rmode, &unsignedp, &reversep,
4881 &volatilep);
4882 if (!multiple_p (rbitpos, BITS_PER_UNIT))
4884 *bitstart = *bitend = 0;
4885 return;
4889 /* Compute the adjustment to bitpos from the offset of the field
4890 relative to the representative. DECL_FIELD_OFFSET of field and
4891 repr are the same by construction if they are not constants,
4892 see finish_bitfield_layout. */
4893 poly_uint64 field_offset, repr_offset;
4894 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
4895 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
4896 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
4897 else
4898 bitoffset = 0;
4899 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4900 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4902 /* If the adjustment is larger than bitpos, we would have a negative bit
4903 position for the lower bound and this may wreak havoc later. Adjust
4904 offset and bitpos to make the lower bound non-negative in that case. */
4905 if (maybe_gt (bitoffset, *bitpos))
4907 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
4908 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
4910 *bitpos += adjust_bits;
4911 if (*offset == NULL_TREE)
4912 *offset = size_int (-adjust_bytes);
4913 else
4914 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
4915 *bitstart = 0;
4917 else
4918 *bitstart = *bitpos - bitoffset;
4920 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
4923 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4924 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4925 DECL_RTL was not set yet, return NORTL. */
4927 static inline bool
4928 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4930 if (TREE_CODE (addr) != ADDR_EXPR)
4931 return false;
4933 tree base = TREE_OPERAND (addr, 0);
4935 if (!DECL_P (base)
4936 || TREE_ADDRESSABLE (base)
4937 || DECL_MODE (base) == BLKmode)
4938 return false;
4940 if (!DECL_RTL_SET_P (base))
4941 return nortl;
4943 return (!MEM_P (DECL_RTL (base)));
4946 /* Returns true if the MEM_REF REF refers to an object that does not
4947 reside in memory and has non-BLKmode. */
4949 static inline bool
4950 mem_ref_refers_to_non_mem_p (tree ref)
4952 tree base = TREE_OPERAND (ref, 0);
4953 return addr_expr_of_non_mem_decl_p_1 (base, false);
4956 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4957 is true, try generating a nontemporal store. */
4959 void
4960 expand_assignment (tree to, tree from, bool nontemporal)
4962 rtx to_rtx = 0;
4963 rtx result;
4964 machine_mode mode;
4965 unsigned int align;
4966 enum insn_code icode;
4968 /* Don't crash if the lhs of the assignment was erroneous. */
4969 if (TREE_CODE (to) == ERROR_MARK)
4971 expand_normal (from);
4972 return;
4975 /* Optimize away no-op moves without side-effects. */
4976 if (operand_equal_p (to, from, 0))
4977 return;
4979 /* Handle misaligned stores. */
4980 mode = TYPE_MODE (TREE_TYPE (to));
4981 if ((TREE_CODE (to) == MEM_REF
4982 || TREE_CODE (to) == TARGET_MEM_REF)
4983 && mode != BLKmode
4984 && !mem_ref_refers_to_non_mem_p (to)
4985 && ((align = get_object_alignment (to))
4986 < GET_MODE_ALIGNMENT (mode))
4987 && (((icode = optab_handler (movmisalign_optab, mode))
4988 != CODE_FOR_nothing)
4989 || targetm.slow_unaligned_access (mode, align)))
4991 rtx reg, mem;
4993 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4994 reg = force_not_mem (reg);
4995 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4996 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4997 reg = flip_storage_order (mode, reg);
4999 if (icode != CODE_FOR_nothing)
5001 struct expand_operand ops[2];
5003 create_fixed_operand (&ops[0], mem);
5004 create_input_operand (&ops[1], reg, mode);
5005 /* The movmisalign<mode> pattern cannot fail, else the assignment
5006 would silently be omitted. */
5007 expand_insn (icode, 2, ops);
5009 else
5010 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5011 false);
5012 return;
5015 /* Assignment of a structure component needs special treatment
5016 if the structure component's rtx is not simply a MEM.
5017 Assignment of an array element at a constant index, and assignment of
5018 an array element in an unaligned packed structure field, has the same
5019 problem. Same for (partially) storing into a non-memory object. */
5020 if (handled_component_p (to)
5021 || (TREE_CODE (to) == MEM_REF
5022 && (REF_REVERSE_STORAGE_ORDER (to)
5023 || mem_ref_refers_to_non_mem_p (to)))
5024 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5026 machine_mode mode1;
5027 poly_int64 bitsize, bitpos;
5028 poly_uint64 bitregion_start = 0;
5029 poly_uint64 bitregion_end = 0;
5030 tree offset;
5031 int unsignedp, reversep, volatilep = 0;
5032 tree tem;
5034 push_temp_slots ();
5035 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5036 &unsignedp, &reversep, &volatilep);
5038 /* Make sure bitpos is not negative, it can wreak havoc later. */
5039 if (maybe_lt (bitpos, 0))
5041 gcc_assert (offset == NULL_TREE);
5042 offset = size_int (bits_to_bytes_round_down (bitpos));
5043 bitpos = num_trailing_bits (bitpos);
5046 if (TREE_CODE (to) == COMPONENT_REF
5047 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5048 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5049 /* The C++ memory model naturally applies to byte-aligned fields.
5050 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5051 BITSIZE are not byte-aligned, there is no need to limit the range
5052 we can access. This can occur with packed structures in Ada. */
5053 else if (maybe_gt (bitsize, 0)
5054 && multiple_p (bitsize, BITS_PER_UNIT)
5055 && multiple_p (bitpos, BITS_PER_UNIT))
5057 bitregion_start = bitpos;
5058 bitregion_end = bitpos + bitsize - 1;
5061 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5063 /* If the field has a mode, we want to access it in the
5064 field's mode, not the computed mode.
5065 If a MEM has VOIDmode (external with incomplete type),
5066 use BLKmode for it instead. */
5067 if (MEM_P (to_rtx))
5069 if (mode1 != VOIDmode)
5070 to_rtx = adjust_address (to_rtx, mode1, 0);
5071 else if (GET_MODE (to_rtx) == VOIDmode)
5072 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5075 if (offset != 0)
5077 machine_mode address_mode;
5078 rtx offset_rtx;
5080 if (!MEM_P (to_rtx))
5082 /* We can get constant negative offsets into arrays with broken
5083 user code. Translate this to a trap instead of ICEing. */
5084 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5085 expand_builtin_trap ();
5086 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5089 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5090 address_mode = get_address_mode (to_rtx);
5091 if (GET_MODE (offset_rtx) != address_mode)
5093 /* We cannot be sure that the RTL in offset_rtx is valid outside
5094 of a memory address context, so force it into a register
5095 before attempting to convert it to the desired mode. */
5096 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5097 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5100 /* If we have an expression in OFFSET_RTX and a non-zero
5101 byte offset in BITPOS, adding the byte offset before the
5102 OFFSET_RTX results in better intermediate code, which makes
5103 later rtl optimization passes perform better.
5105 We prefer intermediate code like this:
5107 r124:DI=r123:DI+0x18
5108 [r124:DI]=r121:DI
5110 ... instead of ...
5112 r124:DI=r123:DI+0x10
5113 [r124:DI+0x8]=r121:DI
5115 This is only done for aligned data values, as these can
5116 be expected to result in single move instructions. */
5117 poly_int64 bytepos;
5118 if (mode1 != VOIDmode
5119 && maybe_ne (bitpos, 0)
5120 && maybe_gt (bitsize, 0)
5121 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5122 && multiple_p (bitpos, bitsize)
5123 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5124 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5126 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5127 bitregion_start = 0;
5128 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5129 bitregion_end -= bitpos;
5130 bitpos = 0;
5133 to_rtx = offset_address (to_rtx, offset_rtx,
5134 highest_pow2_factor_for_target (to,
5135 offset));
5138 /* No action is needed if the target is not a memory and the field
5139 lies completely outside that target. This can occur if the source
5140 code contains an out-of-bounds access to a small array. */
5141 if (!MEM_P (to_rtx)
5142 && GET_MODE (to_rtx) != BLKmode
5143 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5145 expand_normal (from);
5146 result = NULL;
5148 /* Handle expand_expr of a complex value returning a CONCAT. */
5149 else if (GET_CODE (to_rtx) == CONCAT)
5151 machine_mode to_mode = GET_MODE (to_rtx);
5152 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5153 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5154 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5155 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5156 && known_eq (bitpos, 0)
5157 && known_eq (bitsize, mode_bitsize))
5158 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5159 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5160 && known_eq (bitsize, inner_bitsize)
5161 && (known_eq (bitpos, 0)
5162 || known_eq (bitpos, inner_bitsize)))
5163 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5164 false, nontemporal, reversep);
5165 else if (known_le (bitpos + bitsize, inner_bitsize))
5166 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5167 bitregion_start, bitregion_end,
5168 mode1, from, get_alias_set (to),
5169 nontemporal, reversep);
5170 else if (known_ge (bitpos, inner_bitsize))
5171 result = store_field (XEXP (to_rtx, 1), bitsize,
5172 bitpos - inner_bitsize,
5173 bitregion_start, bitregion_end,
5174 mode1, from, get_alias_set (to),
5175 nontemporal, reversep);
5176 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5178 result = expand_normal (from);
5179 if (GET_CODE (result) == CONCAT)
5181 to_mode = GET_MODE_INNER (to_mode);
5182 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5183 rtx from_real
5184 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5185 from_mode, 0);
5186 rtx from_imag
5187 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5188 from_mode, 0);
5189 if (!from_real || !from_imag)
5190 goto concat_store_slow;
5191 emit_move_insn (XEXP (to_rtx, 0), from_real);
5192 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5194 else
5196 rtx from_rtx
5197 = simplify_gen_subreg (to_mode, result,
5198 TYPE_MODE (TREE_TYPE (from)), 0);
5199 if (from_rtx)
5201 emit_move_insn (XEXP (to_rtx, 0),
5202 read_complex_part (from_rtx, false));
5203 emit_move_insn (XEXP (to_rtx, 1),
5204 read_complex_part (from_rtx, true));
5206 else
5208 machine_mode to_mode
5209 = GET_MODE_INNER (GET_MODE (to_rtx));
5210 rtx from_real
5211 = simplify_gen_subreg (to_mode, result,
5212 TYPE_MODE (TREE_TYPE (from)),
5214 rtx from_imag
5215 = simplify_gen_subreg (to_mode, result,
5216 TYPE_MODE (TREE_TYPE (from)),
5217 GET_MODE_SIZE (to_mode));
5218 if (!from_real || !from_imag)
5219 goto concat_store_slow;
5220 emit_move_insn (XEXP (to_rtx, 0), from_real);
5221 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5225 else
5227 concat_store_slow:;
5228 rtx temp = assign_stack_temp (to_mode,
5229 GET_MODE_SIZE (GET_MODE (to_rtx)));
5230 write_complex_part (temp, XEXP (to_rtx, 0), false);
5231 write_complex_part (temp, XEXP (to_rtx, 1), true);
5232 result = store_field (temp, bitsize, bitpos,
5233 bitregion_start, bitregion_end,
5234 mode1, from, get_alias_set (to),
5235 nontemporal, reversep);
5236 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5237 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5240 else
5242 if (MEM_P (to_rtx))
5244 /* If the field is at offset zero, we could have been given the
5245 DECL_RTX of the parent struct. Don't munge it. */
5246 to_rtx = shallow_copy_rtx (to_rtx);
5247 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5248 if (volatilep)
5249 MEM_VOLATILE_P (to_rtx) = 1;
5252 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5253 bitregion_start, bitregion_end,
5254 mode1, to_rtx, to, from,
5255 reversep))
5256 result = NULL;
5257 else
5258 result = store_field (to_rtx, bitsize, bitpos,
5259 bitregion_start, bitregion_end,
5260 mode1, from, get_alias_set (to),
5261 nontemporal, reversep);
5264 if (result)
5265 preserve_temp_slots (result);
5266 pop_temp_slots ();
5267 return;
5270 /* If the rhs is a function call and its value is not an aggregate,
5271 call the function before we start to compute the lhs.
5272 This is needed for correct code for cases such as
5273 val = setjmp (buf) on machines where reference to val
5274 requires loading up part of an address in a separate insn.
5276 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5277 since it might be a promoted variable where the zero- or sign- extension
5278 needs to be done. Handling this in the normal way is safe because no
5279 computation is done before the call. The same is true for SSA names. */
5280 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5281 && COMPLETE_TYPE_P (TREE_TYPE (from))
5282 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5283 && ! (((VAR_P (to)
5284 || TREE_CODE (to) == PARM_DECL
5285 || TREE_CODE (to) == RESULT_DECL)
5286 && REG_P (DECL_RTL (to)))
5287 || TREE_CODE (to) == SSA_NAME))
5289 rtx value;
5291 push_temp_slots ();
5292 value = expand_normal (from);
5294 if (to_rtx == 0)
5295 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5297 /* Handle calls that return values in multiple non-contiguous locations.
5298 The Irix 6 ABI has examples of this. */
5299 if (GET_CODE (to_rtx) == PARALLEL)
5301 if (GET_CODE (value) == PARALLEL)
5302 emit_group_move (to_rtx, value);
5303 else
5304 emit_group_load (to_rtx, value, TREE_TYPE (from),
5305 int_size_in_bytes (TREE_TYPE (from)));
5307 else if (GET_CODE (value) == PARALLEL)
5308 emit_group_store (to_rtx, value, TREE_TYPE (from),
5309 int_size_in_bytes (TREE_TYPE (from)));
5310 else if (GET_MODE (to_rtx) == BLKmode)
5312 /* Handle calls that return BLKmode values in registers. */
5313 if (REG_P (value))
5314 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5315 else
5316 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5318 else
5320 if (POINTER_TYPE_P (TREE_TYPE (to)))
5321 value = convert_memory_address_addr_space
5322 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5323 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5325 emit_move_insn (to_rtx, value);
5328 preserve_temp_slots (to_rtx);
5329 pop_temp_slots ();
5330 return;
5333 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5334 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5336 /* Don't move directly into a return register. */
5337 if (TREE_CODE (to) == RESULT_DECL
5338 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5340 rtx temp;
5342 push_temp_slots ();
5344 /* If the source is itself a return value, it still is in a pseudo at
5345 this point so we can move it back to the return register directly. */
5346 if (REG_P (to_rtx)
5347 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5348 && TREE_CODE (from) != CALL_EXPR)
5349 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5350 else
5351 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5353 /* Handle calls that return values in multiple non-contiguous locations.
5354 The Irix 6 ABI has examples of this. */
5355 if (GET_CODE (to_rtx) == PARALLEL)
5357 if (GET_CODE (temp) == PARALLEL)
5358 emit_group_move (to_rtx, temp);
5359 else
5360 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5361 int_size_in_bytes (TREE_TYPE (from)));
5363 else if (temp)
5364 emit_move_insn (to_rtx, temp);
5366 preserve_temp_slots (to_rtx);
5367 pop_temp_slots ();
5368 return;
5371 /* In case we are returning the contents of an object which overlaps
5372 the place the value is being stored, use a safe function when copying
5373 a value through a pointer into a structure value return block. */
5374 if (TREE_CODE (to) == RESULT_DECL
5375 && TREE_CODE (from) == INDIRECT_REF
5376 && ADDR_SPACE_GENERIC_P
5377 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5378 && refs_may_alias_p (to, from)
5379 && cfun->returns_struct
5380 && !cfun->returns_pcc_struct)
5382 rtx from_rtx, size;
5384 push_temp_slots ();
5385 size = expr_size (from);
5386 from_rtx = expand_normal (from);
5388 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5390 preserve_temp_slots (to_rtx);
5391 pop_temp_slots ();
5392 return;
5395 /* Compute FROM and store the value in the rtx we got. */
5397 push_temp_slots ();
5398 result = store_expr (from, to_rtx, 0, nontemporal, false);
5399 preserve_temp_slots (result);
5400 pop_temp_slots ();
5401 return;
5404 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5405 succeeded, false otherwise. */
5407 bool
5408 emit_storent_insn (rtx to, rtx from)
5410 struct expand_operand ops[2];
5411 machine_mode mode = GET_MODE (to);
5412 enum insn_code code = optab_handler (storent_optab, mode);
5414 if (code == CODE_FOR_nothing)
5415 return false;
5417 create_fixed_operand (&ops[0], to);
5418 create_input_operand (&ops[1], from, mode);
5419 return maybe_expand_insn (code, 2, ops);
5422 /* Generate code for computing expression EXP,
5423 and storing the value into TARGET.
5425 If the mode is BLKmode then we may return TARGET itself.
5426 It turns out that in BLKmode it doesn't cause a problem.
5427 because C has no operators that could combine two different
5428 assignments into the same BLKmode object with different values
5429 with no sequence point. Will other languages need this to
5430 be more thorough?
5432 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5433 stack, and block moves may need to be treated specially.
5435 If NONTEMPORAL is true, try using a nontemporal store instruction.
5437 If REVERSE is true, the store is to be done in reverse order. */
5440 store_expr (tree exp, rtx target, int call_param_p,
5441 bool nontemporal, bool reverse)
5443 rtx temp;
5444 rtx alt_rtl = NULL_RTX;
5445 location_t loc = curr_insn_location ();
5447 if (VOID_TYPE_P (TREE_TYPE (exp)))
5449 /* C++ can generate ?: expressions with a throw expression in one
5450 branch and an rvalue in the other. Here, we resolve attempts to
5451 store the throw expression's nonexistent result. */
5452 gcc_assert (!call_param_p);
5453 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5454 return NULL_RTX;
5456 if (TREE_CODE (exp) == COMPOUND_EXPR)
5458 /* Perform first part of compound expression, then assign from second
5459 part. */
5460 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5461 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5462 return store_expr (TREE_OPERAND (exp, 1), target,
5463 call_param_p, nontemporal, reverse);
5465 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5467 /* For conditional expression, get safe form of the target. Then
5468 test the condition, doing the appropriate assignment on either
5469 side. This avoids the creation of unnecessary temporaries.
5470 For non-BLKmode, it is more efficient not to do this. */
5472 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5474 do_pending_stack_adjust ();
5475 NO_DEFER_POP;
5476 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5477 profile_probability::uninitialized ());
5478 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5479 nontemporal, reverse);
5480 emit_jump_insn (targetm.gen_jump (lab2));
5481 emit_barrier ();
5482 emit_label (lab1);
5483 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5484 nontemporal, reverse);
5485 emit_label (lab2);
5486 OK_DEFER_POP;
5488 return NULL_RTX;
5490 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5491 /* If this is a scalar in a register that is stored in a wider mode
5492 than the declared mode, compute the result into its declared mode
5493 and then convert to the wider mode. Our value is the computed
5494 expression. */
5496 rtx inner_target = 0;
5497 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5498 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5500 /* We can do the conversion inside EXP, which will often result
5501 in some optimizations. Do the conversion in two steps: first
5502 change the signedness, if needed, then the extend. But don't
5503 do this if the type of EXP is a subtype of something else
5504 since then the conversion might involve more than just
5505 converting modes. */
5506 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5507 && TREE_TYPE (TREE_TYPE (exp)) == 0
5508 && GET_MODE_PRECISION (outer_mode)
5509 == TYPE_PRECISION (TREE_TYPE (exp)))
5511 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5512 TYPE_UNSIGNED (TREE_TYPE (exp))))
5514 /* Some types, e.g. Fortran's logical*4, won't have a signed
5515 version, so use the mode instead. */
5516 tree ntype
5517 = (signed_or_unsigned_type_for
5518 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5519 if (ntype == NULL)
5520 ntype = lang_hooks.types.type_for_mode
5521 (TYPE_MODE (TREE_TYPE (exp)),
5522 SUBREG_PROMOTED_SIGN (target));
5524 exp = fold_convert_loc (loc, ntype, exp);
5527 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5528 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5529 exp);
5531 inner_target = SUBREG_REG (target);
5534 temp = expand_expr (exp, inner_target, VOIDmode,
5535 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5538 /* If TEMP is a VOIDmode constant, use convert_modes to make
5539 sure that we properly convert it. */
5540 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5542 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5543 temp, SUBREG_PROMOTED_SIGN (target));
5544 temp = convert_modes (inner_mode, outer_mode, temp,
5545 SUBREG_PROMOTED_SIGN (target));
5548 convert_move (SUBREG_REG (target), temp,
5549 SUBREG_PROMOTED_SIGN (target));
5551 return NULL_RTX;
5553 else if ((TREE_CODE (exp) == STRING_CST
5554 || (TREE_CODE (exp) == MEM_REF
5555 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5556 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5557 == STRING_CST
5558 && integer_zerop (TREE_OPERAND (exp, 1))))
5559 && !nontemporal && !call_param_p
5560 && MEM_P (target))
5562 /* Optimize initialization of an array with a STRING_CST. */
5563 HOST_WIDE_INT exp_len, str_copy_len;
5564 rtx dest_mem;
5565 tree str = TREE_CODE (exp) == STRING_CST
5566 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5568 exp_len = int_expr_size (exp);
5569 if (exp_len <= 0)
5570 goto normal_expr;
5572 if (TREE_STRING_LENGTH (str) <= 0)
5573 goto normal_expr;
5575 str_copy_len = strlen (TREE_STRING_POINTER (str));
5576 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5577 goto normal_expr;
5579 str_copy_len = TREE_STRING_LENGTH (str);
5580 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5581 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5583 str_copy_len += STORE_MAX_PIECES - 1;
5584 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5586 str_copy_len = MIN (str_copy_len, exp_len);
5587 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5588 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5589 MEM_ALIGN (target), false))
5590 goto normal_expr;
5592 dest_mem = target;
5594 dest_mem = store_by_pieces (dest_mem,
5595 str_copy_len, builtin_strncpy_read_str,
5596 CONST_CAST (char *,
5597 TREE_STRING_POINTER (str)),
5598 MEM_ALIGN (target), false,
5599 exp_len > str_copy_len ? 1 : 0);
5600 if (exp_len > str_copy_len)
5601 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5602 GEN_INT (exp_len - str_copy_len),
5603 BLOCK_OP_NORMAL);
5604 return NULL_RTX;
5606 else
5608 rtx tmp_target;
5610 normal_expr:
5611 /* If we want to use a nontemporal or a reverse order store, force the
5612 value into a register first. */
5613 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5614 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5615 (call_param_p
5616 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5617 &alt_rtl, false);
5620 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5621 the same as that of TARGET, adjust the constant. This is needed, for
5622 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5623 only a word-sized value. */
5624 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5625 && TREE_CODE (exp) != ERROR_MARK
5626 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5628 if (GET_MODE_CLASS (GET_MODE (target))
5629 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
5630 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
5631 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
5633 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
5634 TYPE_MODE (TREE_TYPE (exp)), 0);
5635 if (t)
5636 temp = t;
5638 if (GET_MODE (temp) == VOIDmode)
5639 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5640 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5643 /* If value was not generated in the target, store it there.
5644 Convert the value to TARGET's type first if necessary and emit the
5645 pending incrementations that have been queued when expanding EXP.
5646 Note that we cannot emit the whole queue blindly because this will
5647 effectively disable the POST_INC optimization later.
5649 If TEMP and TARGET compare equal according to rtx_equal_p, but
5650 one or both of them are volatile memory refs, we have to distinguish
5651 two cases:
5652 - expand_expr has used TARGET. In this case, we must not generate
5653 another copy. This can be detected by TARGET being equal according
5654 to == .
5655 - expand_expr has not used TARGET - that means that the source just
5656 happens to have the same RTX form. Since temp will have been created
5657 by expand_expr, it will compare unequal according to == .
5658 We must generate a copy in this case, to reach the correct number
5659 of volatile memory references. */
5661 if ((! rtx_equal_p (temp, target)
5662 || (temp != target && (side_effects_p (temp)
5663 || side_effects_p (target))))
5664 && TREE_CODE (exp) != ERROR_MARK
5665 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5666 but TARGET is not valid memory reference, TEMP will differ
5667 from TARGET although it is really the same location. */
5668 && !(alt_rtl
5669 && rtx_equal_p (alt_rtl, target)
5670 && !side_effects_p (alt_rtl)
5671 && !side_effects_p (target))
5672 /* If there's nothing to copy, don't bother. Don't call
5673 expr_size unless necessary, because some front-ends (C++)
5674 expr_size-hook must not be given objects that are not
5675 supposed to be bit-copied or bit-initialized. */
5676 && expr_size (exp) != const0_rtx)
5678 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5680 if (GET_MODE (target) == BLKmode)
5682 /* Handle calls that return BLKmode values in registers. */
5683 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5684 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5685 else
5686 store_bit_field (target,
5687 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5688 0, 0, 0, GET_MODE (temp), temp, reverse);
5690 else
5691 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5694 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5696 /* Handle copying a string constant into an array. The string
5697 constant may be shorter than the array. So copy just the string's
5698 actual length, and clear the rest. First get the size of the data
5699 type of the string, which is actually the size of the target. */
5700 rtx size = expr_size (exp);
5702 if (CONST_INT_P (size)
5703 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5704 emit_block_move (target, temp, size,
5705 (call_param_p
5706 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5707 else
5709 machine_mode pointer_mode
5710 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5711 machine_mode address_mode = get_address_mode (target);
5713 /* Compute the size of the data to copy from the string. */
5714 tree copy_size
5715 = size_binop_loc (loc, MIN_EXPR,
5716 make_tree (sizetype, size),
5717 size_int (TREE_STRING_LENGTH (exp)));
5718 rtx copy_size_rtx
5719 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5720 (call_param_p
5721 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5722 rtx_code_label *label = 0;
5724 /* Copy that much. */
5725 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5726 TYPE_UNSIGNED (sizetype));
5727 emit_block_move (target, temp, copy_size_rtx,
5728 (call_param_p
5729 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5731 /* Figure out how much is left in TARGET that we have to clear.
5732 Do all calculations in pointer_mode. */
5733 poly_int64 const_copy_size;
5734 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
5736 size = plus_constant (address_mode, size, -const_copy_size);
5737 target = adjust_address (target, BLKmode, const_copy_size);
5739 else
5741 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5742 copy_size_rtx, NULL_RTX, 0,
5743 OPTAB_LIB_WIDEN);
5745 if (GET_MODE (copy_size_rtx) != address_mode)
5746 copy_size_rtx = convert_to_mode (address_mode,
5747 copy_size_rtx,
5748 TYPE_UNSIGNED (sizetype));
5750 target = offset_address (target, copy_size_rtx,
5751 highest_pow2_factor (copy_size));
5752 label = gen_label_rtx ();
5753 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5754 GET_MODE (size), 0, label);
5757 if (size != const0_rtx)
5758 clear_storage (target, size, BLOCK_OP_NORMAL);
5760 if (label)
5761 emit_label (label);
5764 /* Handle calls that return values in multiple non-contiguous locations.
5765 The Irix 6 ABI has examples of this. */
5766 else if (GET_CODE (target) == PARALLEL)
5768 if (GET_CODE (temp) == PARALLEL)
5769 emit_group_move (target, temp);
5770 else
5771 emit_group_load (target, temp, TREE_TYPE (exp),
5772 int_size_in_bytes (TREE_TYPE (exp)));
5774 else if (GET_CODE (temp) == PARALLEL)
5775 emit_group_store (target, temp, TREE_TYPE (exp),
5776 int_size_in_bytes (TREE_TYPE (exp)));
5777 else if (GET_MODE (temp) == BLKmode)
5778 emit_block_move (target, temp, expr_size (exp),
5779 (call_param_p
5780 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5781 /* If we emit a nontemporal store, there is nothing else to do. */
5782 else if (nontemporal && emit_storent_insn (target, temp))
5784 else
5786 if (reverse)
5787 temp = flip_storage_order (GET_MODE (target), temp);
5788 temp = force_operand (temp, target);
5789 if (temp != target)
5790 emit_move_insn (target, temp);
5794 return NULL_RTX;
5797 /* Return true if field F of structure TYPE is a flexible array. */
5799 static bool
5800 flexible_array_member_p (const_tree f, const_tree type)
5802 const_tree tf;
5804 tf = TREE_TYPE (f);
5805 return (DECL_CHAIN (f) == NULL
5806 && TREE_CODE (tf) == ARRAY_TYPE
5807 && TYPE_DOMAIN (tf)
5808 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5809 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5810 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5811 && int_size_in_bytes (type) >= 0);
5814 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5815 must have in order for it to completely initialize a value of type TYPE.
5816 Return -1 if the number isn't known.
5818 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5820 static HOST_WIDE_INT
5821 count_type_elements (const_tree type, bool for_ctor_p)
5823 switch (TREE_CODE (type))
5825 case ARRAY_TYPE:
5827 tree nelts;
5829 nelts = array_type_nelts (type);
5830 if (nelts && tree_fits_uhwi_p (nelts))
5832 unsigned HOST_WIDE_INT n;
5834 n = tree_to_uhwi (nelts) + 1;
5835 if (n == 0 || for_ctor_p)
5836 return n;
5837 else
5838 return n * count_type_elements (TREE_TYPE (type), false);
5840 return for_ctor_p ? -1 : 1;
5843 case RECORD_TYPE:
5845 unsigned HOST_WIDE_INT n;
5846 tree f;
5848 n = 0;
5849 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5850 if (TREE_CODE (f) == FIELD_DECL)
5852 if (!for_ctor_p)
5853 n += count_type_elements (TREE_TYPE (f), false);
5854 else if (!flexible_array_member_p (f, type))
5855 /* Don't count flexible arrays, which are not supposed
5856 to be initialized. */
5857 n += 1;
5860 return n;
5863 case UNION_TYPE:
5864 case QUAL_UNION_TYPE:
5866 tree f;
5867 HOST_WIDE_INT n, m;
5869 gcc_assert (!for_ctor_p);
5870 /* Estimate the number of scalars in each field and pick the
5871 maximum. Other estimates would do instead; the idea is simply
5872 to make sure that the estimate is not sensitive to the ordering
5873 of the fields. */
5874 n = 1;
5875 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5876 if (TREE_CODE (f) == FIELD_DECL)
5878 m = count_type_elements (TREE_TYPE (f), false);
5879 /* If the field doesn't span the whole union, add an extra
5880 scalar for the rest. */
5881 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5882 TYPE_SIZE (type)) != 1)
5883 m++;
5884 if (n < m)
5885 n = m;
5887 return n;
5890 case COMPLEX_TYPE:
5891 return 2;
5893 case VECTOR_TYPE:
5895 unsigned HOST_WIDE_INT nelts;
5896 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
5897 return nelts;
5898 else
5899 return -1;
5902 case INTEGER_TYPE:
5903 case REAL_TYPE:
5904 case FIXED_POINT_TYPE:
5905 case ENUMERAL_TYPE:
5906 case BOOLEAN_TYPE:
5907 case POINTER_TYPE:
5908 case OFFSET_TYPE:
5909 case REFERENCE_TYPE:
5910 case NULLPTR_TYPE:
5911 return 1;
5913 case ERROR_MARK:
5914 return 0;
5916 case VOID_TYPE:
5917 case METHOD_TYPE:
5918 case FUNCTION_TYPE:
5919 case LANG_TYPE:
5920 default:
5921 gcc_unreachable ();
5925 /* Helper for categorize_ctor_elements. Identical interface. */
5927 static bool
5928 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5929 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5931 unsigned HOST_WIDE_INT idx;
5932 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5933 tree value, purpose, elt_type;
5935 /* Whether CTOR is a valid constant initializer, in accordance with what
5936 initializer_constant_valid_p does. If inferred from the constructor
5937 elements, true until proven otherwise. */
5938 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5939 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5941 nz_elts = 0;
5942 init_elts = 0;
5943 num_fields = 0;
5944 elt_type = NULL_TREE;
5946 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5948 HOST_WIDE_INT mult = 1;
5950 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5952 tree lo_index = TREE_OPERAND (purpose, 0);
5953 tree hi_index = TREE_OPERAND (purpose, 1);
5955 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5956 mult = (tree_to_uhwi (hi_index)
5957 - tree_to_uhwi (lo_index) + 1);
5959 num_fields += mult;
5960 elt_type = TREE_TYPE (value);
5962 switch (TREE_CODE (value))
5964 case CONSTRUCTOR:
5966 HOST_WIDE_INT nz = 0, ic = 0;
5968 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5969 p_complete);
5971 nz_elts += mult * nz;
5972 init_elts += mult * ic;
5974 if (const_from_elts_p && const_p)
5975 const_p = const_elt_p;
5977 break;
5979 case INTEGER_CST:
5980 case REAL_CST:
5981 case FIXED_CST:
5982 if (!initializer_zerop (value))
5983 nz_elts += mult;
5984 init_elts += mult;
5985 break;
5987 case STRING_CST:
5988 nz_elts += mult * TREE_STRING_LENGTH (value);
5989 init_elts += mult * TREE_STRING_LENGTH (value);
5990 break;
5992 case COMPLEX_CST:
5993 if (!initializer_zerop (TREE_REALPART (value)))
5994 nz_elts += mult;
5995 if (!initializer_zerop (TREE_IMAGPART (value)))
5996 nz_elts += mult;
5997 init_elts += mult;
5998 break;
6000 case VECTOR_CST:
6002 /* We can only construct constant-length vectors using
6003 CONSTRUCTOR. */
6004 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6005 for (unsigned int i = 0; i < nunits; ++i)
6007 tree v = VECTOR_CST_ELT (value, i);
6008 if (!initializer_zerop (v))
6009 nz_elts += mult;
6010 init_elts += mult;
6013 break;
6015 default:
6017 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6018 nz_elts += mult * tc;
6019 init_elts += mult * tc;
6021 if (const_from_elts_p && const_p)
6022 const_p
6023 = initializer_constant_valid_p (value,
6024 elt_type,
6025 TYPE_REVERSE_STORAGE_ORDER
6026 (TREE_TYPE (ctor)))
6027 != NULL_TREE;
6029 break;
6033 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6034 num_fields, elt_type))
6035 *p_complete = false;
6037 *p_nz_elts += nz_elts;
6038 *p_init_elts += init_elts;
6040 return const_p;
6043 /* Examine CTOR to discover:
6044 * how many scalar fields are set to nonzero values,
6045 and place it in *P_NZ_ELTS;
6046 * how many scalar fields in total are in CTOR,
6047 and place it in *P_ELT_COUNT.
6048 * whether the constructor is complete -- in the sense that every
6049 meaningful byte is explicitly given a value --
6050 and place it in *P_COMPLETE.
6052 Return whether or not CTOR is a valid static constant initializer, the same
6053 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6055 bool
6056 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6057 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6059 *p_nz_elts = 0;
6060 *p_init_elts = 0;
6061 *p_complete = true;
6063 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6066 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6067 of which had type LAST_TYPE. Each element was itself a complete
6068 initializer, in the sense that every meaningful byte was explicitly
6069 given a value. Return true if the same is true for the constructor
6070 as a whole. */
6072 bool
6073 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6074 const_tree last_type)
6076 if (TREE_CODE (type) == UNION_TYPE
6077 || TREE_CODE (type) == QUAL_UNION_TYPE)
6079 if (num_elts == 0)
6080 return false;
6082 gcc_assert (num_elts == 1 && last_type);
6084 /* ??? We could look at each element of the union, and find the
6085 largest element. Which would avoid comparing the size of the
6086 initialized element against any tail padding in the union.
6087 Doesn't seem worth the effort... */
6088 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6091 return count_type_elements (type, true) == num_elts;
6094 /* Return 1 if EXP contains mostly (3/4) zeros. */
6096 static int
6097 mostly_zeros_p (const_tree exp)
6099 if (TREE_CODE (exp) == CONSTRUCTOR)
6101 HOST_WIDE_INT nz_elts, init_elts;
6102 bool complete_p;
6104 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6105 return !complete_p || nz_elts < init_elts / 4;
6108 return initializer_zerop (exp);
6111 /* Return 1 if EXP contains all zeros. */
6113 static int
6114 all_zeros_p (const_tree exp)
6116 if (TREE_CODE (exp) == CONSTRUCTOR)
6118 HOST_WIDE_INT nz_elts, init_elts;
6119 bool complete_p;
6121 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6122 return nz_elts == 0;
6125 return initializer_zerop (exp);
6128 /* Helper function for store_constructor.
6129 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6130 CLEARED is as for store_constructor.
6131 ALIAS_SET is the alias set to use for any stores.
6132 If REVERSE is true, the store is to be done in reverse order.
6134 This provides a recursive shortcut back to store_constructor when it isn't
6135 necessary to go through store_field. This is so that we can pass through
6136 the cleared field to let store_constructor know that we may not have to
6137 clear a substructure if the outer structure has already been cleared. */
6139 static void
6140 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6141 poly_uint64 bitregion_start,
6142 poly_uint64 bitregion_end,
6143 machine_mode mode,
6144 tree exp, int cleared,
6145 alias_set_type alias_set, bool reverse)
6147 poly_int64 bytepos;
6148 poly_uint64 bytesize;
6149 if (TREE_CODE (exp) == CONSTRUCTOR
6150 /* We can only call store_constructor recursively if the size and
6151 bit position are on a byte boundary. */
6152 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6153 && maybe_ne (bitsize, 0U)
6154 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6155 /* If we have a nonzero bitpos for a register target, then we just
6156 let store_field do the bitfield handling. This is unlikely to
6157 generate unnecessary clear instructions anyways. */
6158 && (known_eq (bitpos, 0) || MEM_P (target)))
6160 if (MEM_P (target))
6162 machine_mode target_mode = GET_MODE (target);
6163 if (target_mode != BLKmode
6164 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6165 target_mode = BLKmode;
6166 target = adjust_address (target, target_mode, bytepos);
6170 /* Update the alias set, if required. */
6171 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6172 && MEM_ALIAS_SET (target) != 0)
6174 target = copy_rtx (target);
6175 set_mem_alias_set (target, alias_set);
6178 store_constructor (exp, target, cleared, bytesize, reverse);
6180 else
6181 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6182 exp, alias_set, false, reverse);
6186 /* Returns the number of FIELD_DECLs in TYPE. */
6188 static int
6189 fields_length (const_tree type)
6191 tree t = TYPE_FIELDS (type);
6192 int count = 0;
6194 for (; t; t = DECL_CHAIN (t))
6195 if (TREE_CODE (t) == FIELD_DECL)
6196 ++count;
6198 return count;
6202 /* Store the value of constructor EXP into the rtx TARGET.
6203 TARGET is either a REG or a MEM; we know it cannot conflict, since
6204 safe_from_p has been called.
6205 CLEARED is true if TARGET is known to have been zero'd.
6206 SIZE is the number of bytes of TARGET we are allowed to modify: this
6207 may not be the same as the size of EXP if we are assigning to a field
6208 which has been packed to exclude padding bits.
6209 If REVERSE is true, the store is to be done in reverse order. */
6211 static void
6212 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6213 bool reverse)
6215 tree type = TREE_TYPE (exp);
6216 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6217 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6219 switch (TREE_CODE (type))
6221 case RECORD_TYPE:
6222 case UNION_TYPE:
6223 case QUAL_UNION_TYPE:
6225 unsigned HOST_WIDE_INT idx;
6226 tree field, value;
6228 /* The storage order is specified for every aggregate type. */
6229 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6231 /* If size is zero or the target is already cleared, do nothing. */
6232 if (known_eq (size, 0) || cleared)
6233 cleared = 1;
6234 /* We either clear the aggregate or indicate the value is dead. */
6235 else if ((TREE_CODE (type) == UNION_TYPE
6236 || TREE_CODE (type) == QUAL_UNION_TYPE)
6237 && ! CONSTRUCTOR_ELTS (exp))
6238 /* If the constructor is empty, clear the union. */
6240 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6241 cleared = 1;
6244 /* If we are building a static constructor into a register,
6245 set the initial value as zero so we can fold the value into
6246 a constant. But if more than one register is involved,
6247 this probably loses. */
6248 else if (REG_P (target) && TREE_STATIC (exp)
6249 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6250 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6252 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6253 cleared = 1;
6256 /* If the constructor has fewer fields than the structure or
6257 if we are initializing the structure to mostly zeros, clear
6258 the whole structure first. Don't do this if TARGET is a
6259 register whose mode size isn't equal to SIZE since
6260 clear_storage can't handle this case. */
6261 else if (known_size_p (size)
6262 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6263 || mostly_zeros_p (exp))
6264 && (!REG_P (target)
6265 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6267 clear_storage (target, gen_int_mode (size, Pmode),
6268 BLOCK_OP_NORMAL);
6269 cleared = 1;
6272 if (REG_P (target) && !cleared)
6273 emit_clobber (target);
6275 /* Store each element of the constructor into the
6276 corresponding field of TARGET. */
6277 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6279 machine_mode mode;
6280 HOST_WIDE_INT bitsize;
6281 HOST_WIDE_INT bitpos = 0;
6282 tree offset;
6283 rtx to_rtx = target;
6285 /* Just ignore missing fields. We cleared the whole
6286 structure, above, if any fields are missing. */
6287 if (field == 0)
6288 continue;
6290 if (cleared && initializer_zerop (value))
6291 continue;
6293 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6294 bitsize = tree_to_uhwi (DECL_SIZE (field));
6295 else
6296 gcc_unreachable ();
6298 mode = DECL_MODE (field);
6299 if (DECL_BIT_FIELD (field))
6300 mode = VOIDmode;
6302 offset = DECL_FIELD_OFFSET (field);
6303 if (tree_fits_shwi_p (offset)
6304 && tree_fits_shwi_p (bit_position (field)))
6306 bitpos = int_bit_position (field);
6307 offset = NULL_TREE;
6309 else
6310 gcc_unreachable ();
6312 /* If this initializes a field that is smaller than a
6313 word, at the start of a word, try to widen it to a full
6314 word. This special case allows us to output C++ member
6315 function initializations in a form that the optimizers
6316 can understand. */
6317 if (WORD_REGISTER_OPERATIONS
6318 && REG_P (target)
6319 && bitsize < BITS_PER_WORD
6320 && bitpos % BITS_PER_WORD == 0
6321 && GET_MODE_CLASS (mode) == MODE_INT
6322 && TREE_CODE (value) == INTEGER_CST
6323 && exp_size >= 0
6324 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6326 tree type = TREE_TYPE (value);
6328 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6330 type = lang_hooks.types.type_for_mode
6331 (word_mode, TYPE_UNSIGNED (type));
6332 value = fold_convert (type, value);
6333 /* Make sure the bits beyond the original bitsize are zero
6334 so that we can correctly avoid extra zeroing stores in
6335 later constructor elements. */
6336 tree bitsize_mask
6337 = wide_int_to_tree (type, wi::mask (bitsize, false,
6338 BITS_PER_WORD));
6339 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6342 if (BYTES_BIG_ENDIAN)
6343 value
6344 = fold_build2 (LSHIFT_EXPR, type, value,
6345 build_int_cst (type,
6346 BITS_PER_WORD - bitsize));
6347 bitsize = BITS_PER_WORD;
6348 mode = word_mode;
6351 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6352 && DECL_NONADDRESSABLE_P (field))
6354 to_rtx = copy_rtx (to_rtx);
6355 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6358 store_constructor_field (to_rtx, bitsize, bitpos,
6359 0, bitregion_end, mode,
6360 value, cleared,
6361 get_alias_set (TREE_TYPE (field)),
6362 reverse);
6364 break;
6366 case ARRAY_TYPE:
6368 tree value, index;
6369 unsigned HOST_WIDE_INT i;
6370 int need_to_clear;
6371 tree domain;
6372 tree elttype = TREE_TYPE (type);
6373 int const_bounds_p;
6374 HOST_WIDE_INT minelt = 0;
6375 HOST_WIDE_INT maxelt = 0;
6377 /* The storage order is specified for every aggregate type. */
6378 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6380 domain = TYPE_DOMAIN (type);
6381 const_bounds_p = (TYPE_MIN_VALUE (domain)
6382 && TYPE_MAX_VALUE (domain)
6383 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6384 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6386 /* If we have constant bounds for the range of the type, get them. */
6387 if (const_bounds_p)
6389 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6390 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6393 /* If the constructor has fewer elements than the array, clear
6394 the whole array first. Similarly if this is static
6395 constructor of a non-BLKmode object. */
6396 if (cleared)
6397 need_to_clear = 0;
6398 else if (REG_P (target) && TREE_STATIC (exp))
6399 need_to_clear = 1;
6400 else
6402 unsigned HOST_WIDE_INT idx;
6403 tree index, value;
6404 HOST_WIDE_INT count = 0, zero_count = 0;
6405 need_to_clear = ! const_bounds_p;
6407 /* This loop is a more accurate version of the loop in
6408 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6409 is also needed to check for missing elements. */
6410 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6412 HOST_WIDE_INT this_node_count;
6414 if (need_to_clear)
6415 break;
6417 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6419 tree lo_index = TREE_OPERAND (index, 0);
6420 tree hi_index = TREE_OPERAND (index, 1);
6422 if (! tree_fits_uhwi_p (lo_index)
6423 || ! tree_fits_uhwi_p (hi_index))
6425 need_to_clear = 1;
6426 break;
6429 this_node_count = (tree_to_uhwi (hi_index)
6430 - tree_to_uhwi (lo_index) + 1);
6432 else
6433 this_node_count = 1;
6435 count += this_node_count;
6436 if (mostly_zeros_p (value))
6437 zero_count += this_node_count;
6440 /* Clear the entire array first if there are any missing
6441 elements, or if the incidence of zero elements is >=
6442 75%. */
6443 if (! need_to_clear
6444 && (count < maxelt - minelt + 1
6445 || 4 * zero_count >= 3 * count))
6446 need_to_clear = 1;
6449 if (need_to_clear && maybe_gt (size, 0))
6451 if (REG_P (target))
6452 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6453 else
6454 clear_storage (target, gen_int_mode (size, Pmode),
6455 BLOCK_OP_NORMAL);
6456 cleared = 1;
6459 if (!cleared && REG_P (target))
6460 /* Inform later passes that the old value is dead. */
6461 emit_clobber (target);
6463 /* Store each element of the constructor into the
6464 corresponding element of TARGET, determined by counting the
6465 elements. */
6466 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6468 machine_mode mode;
6469 poly_int64 bitsize;
6470 HOST_WIDE_INT bitpos;
6471 rtx xtarget = target;
6473 if (cleared && initializer_zerop (value))
6474 continue;
6476 mode = TYPE_MODE (elttype);
6477 if (mode != BLKmode)
6478 bitsize = GET_MODE_BITSIZE (mode);
6479 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
6480 bitsize = -1;
6482 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6484 tree lo_index = TREE_OPERAND (index, 0);
6485 tree hi_index = TREE_OPERAND (index, 1);
6486 rtx index_r, pos_rtx;
6487 HOST_WIDE_INT lo, hi, count;
6488 tree position;
6490 /* If the range is constant and "small", unroll the loop. */
6491 if (const_bounds_p
6492 && tree_fits_shwi_p (lo_index)
6493 && tree_fits_shwi_p (hi_index)
6494 && (lo = tree_to_shwi (lo_index),
6495 hi = tree_to_shwi (hi_index),
6496 count = hi - lo + 1,
6497 (!MEM_P (target)
6498 || count <= 2
6499 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6500 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6501 <= 40 * 8)))))
6503 lo -= minelt; hi -= minelt;
6504 for (; lo <= hi; lo++)
6506 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6508 if (MEM_P (target)
6509 && !MEM_KEEP_ALIAS_SET_P (target)
6510 && TREE_CODE (type) == ARRAY_TYPE
6511 && TYPE_NONALIASED_COMPONENT (type))
6513 target = copy_rtx (target);
6514 MEM_KEEP_ALIAS_SET_P (target) = 1;
6517 store_constructor_field
6518 (target, bitsize, bitpos, 0, bitregion_end,
6519 mode, value, cleared,
6520 get_alias_set (elttype), reverse);
6523 else
6525 rtx_code_label *loop_start = gen_label_rtx ();
6526 rtx_code_label *loop_end = gen_label_rtx ();
6527 tree exit_cond;
6529 expand_normal (hi_index);
6531 index = build_decl (EXPR_LOCATION (exp),
6532 VAR_DECL, NULL_TREE, domain);
6533 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6534 SET_DECL_RTL (index, index_r);
6535 store_expr (lo_index, index_r, 0, false, reverse);
6537 /* Build the head of the loop. */
6538 do_pending_stack_adjust ();
6539 emit_label (loop_start);
6541 /* Assign value to element index. */
6542 position =
6543 fold_convert (ssizetype,
6544 fold_build2 (MINUS_EXPR,
6545 TREE_TYPE (index),
6546 index,
6547 TYPE_MIN_VALUE (domain)));
6549 position =
6550 size_binop (MULT_EXPR, position,
6551 fold_convert (ssizetype,
6552 TYPE_SIZE_UNIT (elttype)));
6554 pos_rtx = expand_normal (position);
6555 xtarget = offset_address (target, pos_rtx,
6556 highest_pow2_factor (position));
6557 xtarget = adjust_address (xtarget, mode, 0);
6558 if (TREE_CODE (value) == CONSTRUCTOR)
6559 store_constructor (value, xtarget, cleared,
6560 exact_div (bitsize, BITS_PER_UNIT),
6561 reverse);
6562 else
6563 store_expr (value, xtarget, 0, false, reverse);
6565 /* Generate a conditional jump to exit the loop. */
6566 exit_cond = build2 (LT_EXPR, integer_type_node,
6567 index, hi_index);
6568 jumpif (exit_cond, loop_end,
6569 profile_probability::uninitialized ());
6571 /* Update the loop counter, and jump to the head of
6572 the loop. */
6573 expand_assignment (index,
6574 build2 (PLUS_EXPR, TREE_TYPE (index),
6575 index, integer_one_node),
6576 false);
6578 emit_jump (loop_start);
6580 /* Build the end of the loop. */
6581 emit_label (loop_end);
6584 else if ((index != 0 && ! tree_fits_shwi_p (index))
6585 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6587 tree position;
6589 if (index == 0)
6590 index = ssize_int (1);
6592 if (minelt)
6593 index = fold_convert (ssizetype,
6594 fold_build2 (MINUS_EXPR,
6595 TREE_TYPE (index),
6596 index,
6597 TYPE_MIN_VALUE (domain)));
6599 position =
6600 size_binop (MULT_EXPR, index,
6601 fold_convert (ssizetype,
6602 TYPE_SIZE_UNIT (elttype)));
6603 xtarget = offset_address (target,
6604 expand_normal (position),
6605 highest_pow2_factor (position));
6606 xtarget = adjust_address (xtarget, mode, 0);
6607 store_expr (value, xtarget, 0, false, reverse);
6609 else
6611 if (index != 0)
6612 bitpos = ((tree_to_shwi (index) - minelt)
6613 * tree_to_uhwi (TYPE_SIZE (elttype)));
6614 else
6615 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6617 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6618 && TREE_CODE (type) == ARRAY_TYPE
6619 && TYPE_NONALIASED_COMPONENT (type))
6621 target = copy_rtx (target);
6622 MEM_KEEP_ALIAS_SET_P (target) = 1;
6624 store_constructor_field (target, bitsize, bitpos, 0,
6625 bitregion_end, mode, value,
6626 cleared, get_alias_set (elttype),
6627 reverse);
6630 break;
6633 case VECTOR_TYPE:
6635 unsigned HOST_WIDE_INT idx;
6636 constructor_elt *ce;
6637 int i;
6638 int need_to_clear;
6639 insn_code icode = CODE_FOR_nothing;
6640 tree elt;
6641 tree elttype = TREE_TYPE (type);
6642 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6643 machine_mode eltmode = TYPE_MODE (elttype);
6644 HOST_WIDE_INT bitsize;
6645 HOST_WIDE_INT bitpos;
6646 rtvec vector = NULL;
6647 poly_uint64 n_elts;
6648 unsigned HOST_WIDE_INT const_n_elts;
6649 alias_set_type alias;
6650 bool vec_vec_init_p = false;
6651 machine_mode mode = GET_MODE (target);
6653 gcc_assert (eltmode != BLKmode);
6655 /* Try using vec_duplicate_optab for uniform vectors. */
6656 if (!TREE_SIDE_EFFECTS (exp)
6657 && VECTOR_MODE_P (mode)
6658 && eltmode == GET_MODE_INNER (mode)
6659 && ((icode = optab_handler (vec_duplicate_optab, mode))
6660 != CODE_FOR_nothing)
6661 && (elt = uniform_vector_p (exp)))
6663 struct expand_operand ops[2];
6664 create_output_operand (&ops[0], target, mode);
6665 create_input_operand (&ops[1], expand_normal (elt), eltmode);
6666 expand_insn (icode, 2, ops);
6667 if (!rtx_equal_p (target, ops[0].value))
6668 emit_move_insn (target, ops[0].value);
6669 break;
6672 n_elts = TYPE_VECTOR_SUBPARTS (type);
6673 if (REG_P (target)
6674 && VECTOR_MODE_P (mode)
6675 && n_elts.is_constant (&const_n_elts))
6677 machine_mode emode = eltmode;
6679 if (CONSTRUCTOR_NELTS (exp)
6680 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6681 == VECTOR_TYPE))
6683 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6684 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
6685 * TYPE_VECTOR_SUBPARTS (etype),
6686 n_elts));
6687 emode = TYPE_MODE (etype);
6689 icode = convert_optab_handler (vec_init_optab, mode, emode);
6690 if (icode != CODE_FOR_nothing)
6692 unsigned int i, n = const_n_elts;
6694 if (emode != eltmode)
6696 n = CONSTRUCTOR_NELTS (exp);
6697 vec_vec_init_p = true;
6699 vector = rtvec_alloc (n);
6700 for (i = 0; i < n; i++)
6701 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6705 /* If the constructor has fewer elements than the vector,
6706 clear the whole array first. Similarly if this is static
6707 constructor of a non-BLKmode object. */
6708 if (cleared)
6709 need_to_clear = 0;
6710 else if (REG_P (target) && TREE_STATIC (exp))
6711 need_to_clear = 1;
6712 else
6714 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6715 tree value;
6717 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6719 tree sz = TYPE_SIZE (TREE_TYPE (value));
6720 int n_elts_here
6721 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6722 TYPE_SIZE (elttype)));
6724 count += n_elts_here;
6725 if (mostly_zeros_p (value))
6726 zero_count += n_elts_here;
6729 /* Clear the entire vector first if there are any missing elements,
6730 or if the incidence of zero elements is >= 75%. */
6731 need_to_clear = (maybe_lt (count, n_elts)
6732 || 4 * zero_count >= 3 * count);
6735 if (need_to_clear && maybe_gt (size, 0) && !vector)
6737 if (REG_P (target))
6738 emit_move_insn (target, CONST0_RTX (mode));
6739 else
6740 clear_storage (target, gen_int_mode (size, Pmode),
6741 BLOCK_OP_NORMAL);
6742 cleared = 1;
6745 /* Inform later passes that the old value is dead. */
6746 if (!cleared && !vector && REG_P (target))
6747 emit_move_insn (target, CONST0_RTX (mode));
6749 if (MEM_P (target))
6750 alias = MEM_ALIAS_SET (target);
6751 else
6752 alias = get_alias_set (elttype);
6754 /* Store each element of the constructor into the corresponding
6755 element of TARGET, determined by counting the elements. */
6756 for (idx = 0, i = 0;
6757 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6758 idx++, i += bitsize / elt_size)
6760 HOST_WIDE_INT eltpos;
6761 tree value = ce->value;
6763 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6764 if (cleared && initializer_zerop (value))
6765 continue;
6767 if (ce->index)
6768 eltpos = tree_to_uhwi (ce->index);
6769 else
6770 eltpos = i;
6772 if (vector)
6774 if (vec_vec_init_p)
6776 gcc_assert (ce->index == NULL_TREE);
6777 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6778 eltpos = idx;
6780 else
6781 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6782 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6784 else
6786 machine_mode value_mode
6787 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6788 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6789 bitpos = eltpos * elt_size;
6790 store_constructor_field (target, bitsize, bitpos, 0,
6791 bitregion_end, value_mode,
6792 value, cleared, alias, reverse);
6796 if (vector)
6797 emit_insn (GEN_FCN (icode) (target,
6798 gen_rtx_PARALLEL (mode, vector)));
6799 break;
6802 default:
6803 gcc_unreachable ();
6807 /* Store the value of EXP (an expression tree)
6808 into a subfield of TARGET which has mode MODE and occupies
6809 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6810 If MODE is VOIDmode, it means that we are storing into a bit-field.
6812 BITREGION_START is bitpos of the first bitfield in this region.
6813 BITREGION_END is the bitpos of the ending bitfield in this region.
6814 These two fields are 0, if the C++ memory model does not apply,
6815 or we are not interested in keeping track of bitfield regions.
6817 Always return const0_rtx unless we have something particular to
6818 return.
6820 ALIAS_SET is the alias set for the destination. This value will
6821 (in general) be different from that for TARGET, since TARGET is a
6822 reference to the containing structure.
6824 If NONTEMPORAL is true, try generating a nontemporal store.
6826 If REVERSE is true, the store is to be done in reverse order. */
6828 static rtx
6829 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
6830 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
6831 machine_mode mode, tree exp,
6832 alias_set_type alias_set, bool nontemporal, bool reverse)
6834 if (TREE_CODE (exp) == ERROR_MARK)
6835 return const0_rtx;
6837 /* If we have nothing to store, do nothing unless the expression has
6838 side-effects. Don't do that for zero sized addressable lhs of
6839 calls. */
6840 if (known_eq (bitsize, 0)
6841 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6842 || TREE_CODE (exp) != CALL_EXPR))
6843 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6845 if (GET_CODE (target) == CONCAT)
6847 /* We're storing into a struct containing a single __complex. */
6849 gcc_assert (known_eq (bitpos, 0));
6850 return store_expr (exp, target, 0, nontemporal, reverse);
6853 /* If the structure is in a register or if the component
6854 is a bit field, we cannot use addressing to access it.
6855 Use bit-field techniques or SUBREG to store in it. */
6857 poly_int64 decl_bitsize;
6858 if (mode == VOIDmode
6859 || (mode != BLKmode && ! direct_store[(int) mode]
6860 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6861 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6862 || REG_P (target)
6863 || GET_CODE (target) == SUBREG
6864 /* If the field isn't aligned enough to store as an ordinary memref,
6865 store it as a bit field. */
6866 || (mode != BLKmode
6867 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6868 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
6869 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6870 || !multiple_p (bitpos, BITS_PER_UNIT)))
6871 || (known_size_p (bitsize)
6872 && mode != BLKmode
6873 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
6874 /* If the RHS and field are a constant size and the size of the
6875 RHS isn't the same size as the bitfield, we must use bitfield
6876 operations. */
6877 || (known_size_p (bitsize)
6878 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
6879 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
6880 bitsize)
6881 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6882 we will handle specially below. */
6883 && !(TREE_CODE (exp) == CONSTRUCTOR
6884 && multiple_p (bitsize, BITS_PER_UNIT))
6885 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6886 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6887 includes some extra padding. store_expr / expand_expr will in
6888 that case call get_inner_reference that will have the bitsize
6889 we check here and thus the block move will not clobber the
6890 padding that shouldn't be clobbered. In the future we could
6891 replace the TREE_ADDRESSABLE check with a check that
6892 get_base_address needs to live in memory. */
6893 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6894 || TREE_CODE (exp) != COMPONENT_REF
6895 || !multiple_p (bitsize, BITS_PER_UNIT)
6896 || !multiple_p (bitpos, BITS_PER_UNIT)
6897 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
6898 &decl_bitsize)
6899 || maybe_ne (decl_bitsize, bitsize)))
6900 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6901 decl we must use bitfield operations. */
6902 || (known_size_p (bitsize)
6903 && TREE_CODE (exp) == MEM_REF
6904 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6905 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6906 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6907 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6909 rtx temp;
6910 gimple *nop_def;
6912 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6913 implies a mask operation. If the precision is the same size as
6914 the field we're storing into, that mask is redundant. This is
6915 particularly common with bit field assignments generated by the
6916 C front end. */
6917 nop_def = get_def_for_expr (exp, NOP_EXPR);
6918 if (nop_def)
6920 tree type = TREE_TYPE (exp);
6921 if (INTEGRAL_TYPE_P (type)
6922 && maybe_ne (TYPE_PRECISION (type),
6923 GET_MODE_BITSIZE (TYPE_MODE (type)))
6924 && known_eq (bitsize, TYPE_PRECISION (type)))
6926 tree op = gimple_assign_rhs1 (nop_def);
6927 type = TREE_TYPE (op);
6928 if (INTEGRAL_TYPE_P (type)
6929 && known_ge (TYPE_PRECISION (type), bitsize))
6930 exp = op;
6934 temp = expand_normal (exp);
6936 /* We don't support variable-sized BLKmode bitfields, since our
6937 handling of BLKmode is bound up with the ability to break
6938 things into words. */
6939 gcc_assert (mode != BLKmode || bitsize.is_constant ());
6941 /* Handle calls that return values in multiple non-contiguous locations.
6942 The Irix 6 ABI has examples of this. */
6943 if (GET_CODE (temp) == PARALLEL)
6945 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6946 machine_mode temp_mode = GET_MODE (temp);
6947 if (temp_mode == BLKmode || temp_mode == VOIDmode)
6948 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6949 rtx temp_target = gen_reg_rtx (temp_mode);
6950 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6951 temp = temp_target;
6954 /* Handle calls that return BLKmode values in registers. */
6955 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6957 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6958 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6959 temp = temp_target;
6962 /* If the value has aggregate type and an integral mode then, if BITSIZE
6963 is narrower than this mode and this is for big-endian data, we first
6964 need to put the value into the low-order bits for store_bit_field,
6965 except when MODE is BLKmode and BITSIZE larger than the word size
6966 (see the handling of fields larger than a word in store_bit_field).
6967 Moreover, the field may be not aligned on a byte boundary; in this
6968 case, if it has reverse storage order, it needs to be accessed as a
6969 scalar field with reverse storage order and we must first put the
6970 value into target order. */
6971 scalar_int_mode temp_mode;
6972 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6973 && is_int_mode (GET_MODE (temp), &temp_mode))
6975 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6977 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6979 if (reverse)
6980 temp = flip_storage_order (temp_mode, temp);
6982 gcc_checking_assert (known_le (bitsize, size));
6983 if (maybe_lt (bitsize, size)
6984 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6985 /* Use of to_constant for BLKmode was checked above. */
6986 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
6987 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6988 size - bitsize, NULL_RTX, 1);
6991 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6992 if (mode != VOIDmode && mode != BLKmode
6993 && mode != TYPE_MODE (TREE_TYPE (exp)))
6994 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6996 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6997 and BITPOS must be aligned on a byte boundary. If so, we simply do
6998 a block copy. Likewise for a BLKmode-like TARGET. */
6999 if (GET_MODE (temp) == BLKmode
7000 && (GET_MODE (target) == BLKmode
7001 || (MEM_P (target)
7002 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7003 && multiple_p (bitpos, BITS_PER_UNIT)
7004 && multiple_p (bitsize, BITS_PER_UNIT))))
7006 gcc_assert (MEM_P (target) && MEM_P (temp));
7007 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7008 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7010 target = adjust_address (target, VOIDmode, bytepos);
7011 emit_block_move (target, temp,
7012 gen_int_mode (bytesize, Pmode),
7013 BLOCK_OP_NORMAL);
7015 return const0_rtx;
7018 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7019 word size, we need to load the value (see again store_bit_field). */
7020 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7022 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
7023 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7024 temp_mode, false, NULL);
7027 /* Store the value in the bitfield. */
7028 store_bit_field (target, bitsize, bitpos,
7029 bitregion_start, bitregion_end,
7030 mode, temp, reverse);
7032 return const0_rtx;
7034 else
7036 /* Now build a reference to just the desired component. */
7037 rtx to_rtx = adjust_address (target, mode,
7038 exact_div (bitpos, BITS_PER_UNIT));
7040 if (to_rtx == target)
7041 to_rtx = copy_rtx (to_rtx);
7043 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7044 set_mem_alias_set (to_rtx, alias_set);
7046 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7047 into a target smaller than its type; handle that case now. */
7048 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7050 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7051 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7052 return to_rtx;
7055 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7059 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7060 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7061 codes and find the ultimate containing object, which we return.
7063 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7064 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7065 storage order of the field.
7066 If the position of the field is variable, we store a tree
7067 giving the variable offset (in units) in *POFFSET.
7068 This offset is in addition to the bit position.
7069 If the position is not variable, we store 0 in *POFFSET.
7071 If any of the extraction expressions is volatile,
7072 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7074 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7075 Otherwise, it is a mode that can be used to access the field.
7077 If the field describes a variable-sized object, *PMODE is set to
7078 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7079 this case, but the address of the object can be found. */
7081 tree
7082 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7083 poly_int64_pod *pbitpos, tree *poffset,
7084 machine_mode *pmode, int *punsignedp,
7085 int *preversep, int *pvolatilep)
7087 tree size_tree = 0;
7088 machine_mode mode = VOIDmode;
7089 bool blkmode_bitfield = false;
7090 tree offset = size_zero_node;
7091 poly_offset_int bit_offset = 0;
7093 /* First get the mode, signedness, storage order and size. We do this from
7094 just the outermost expression. */
7095 *pbitsize = -1;
7096 if (TREE_CODE (exp) == COMPONENT_REF)
7098 tree field = TREE_OPERAND (exp, 1);
7099 size_tree = DECL_SIZE (field);
7100 if (flag_strict_volatile_bitfields > 0
7101 && TREE_THIS_VOLATILE (exp)
7102 && DECL_BIT_FIELD_TYPE (field)
7103 && DECL_MODE (field) != BLKmode)
7104 /* Volatile bitfields should be accessed in the mode of the
7105 field's type, not the mode computed based on the bit
7106 size. */
7107 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7108 else if (!DECL_BIT_FIELD (field))
7110 mode = DECL_MODE (field);
7111 /* For vector fields re-check the target flags, as DECL_MODE
7112 could have been set with different target flags than
7113 the current function has. */
7114 if (mode == BLKmode
7115 && VECTOR_TYPE_P (TREE_TYPE (field))
7116 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7117 mode = TYPE_MODE (TREE_TYPE (field));
7119 else if (DECL_MODE (field) == BLKmode)
7120 blkmode_bitfield = true;
7122 *punsignedp = DECL_UNSIGNED (field);
7124 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7126 size_tree = TREE_OPERAND (exp, 1);
7127 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7128 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7130 /* For vector types, with the correct size of access, use the mode of
7131 inner type. */
7132 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7133 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7134 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7135 mode = TYPE_MODE (TREE_TYPE (exp));
7137 else
7139 mode = TYPE_MODE (TREE_TYPE (exp));
7140 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7142 if (mode == BLKmode)
7143 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7144 else
7145 *pbitsize = GET_MODE_BITSIZE (mode);
7148 if (size_tree != 0)
7150 if (! tree_fits_uhwi_p (size_tree))
7151 mode = BLKmode, *pbitsize = -1;
7152 else
7153 *pbitsize = tree_to_uhwi (size_tree);
7156 *preversep = reverse_storage_order_for_component_p (exp);
7158 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7159 and find the ultimate containing object. */
7160 while (1)
7162 switch (TREE_CODE (exp))
7164 case BIT_FIELD_REF:
7165 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7166 break;
7168 case COMPONENT_REF:
7170 tree field = TREE_OPERAND (exp, 1);
7171 tree this_offset = component_ref_field_offset (exp);
7173 /* If this field hasn't been filled in yet, don't go past it.
7174 This should only happen when folding expressions made during
7175 type construction. */
7176 if (this_offset == 0)
7177 break;
7179 offset = size_binop (PLUS_EXPR, offset, this_offset);
7180 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7182 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7184 break;
7186 case ARRAY_REF:
7187 case ARRAY_RANGE_REF:
7189 tree index = TREE_OPERAND (exp, 1);
7190 tree low_bound = array_ref_low_bound (exp);
7191 tree unit_size = array_ref_element_size (exp);
7193 /* We assume all arrays have sizes that are a multiple of a byte.
7194 First subtract the lower bound, if any, in the type of the
7195 index, then convert to sizetype and multiply by the size of
7196 the array element. */
7197 if (! integer_zerop (low_bound))
7198 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7199 index, low_bound);
7201 offset = size_binop (PLUS_EXPR, offset,
7202 size_binop (MULT_EXPR,
7203 fold_convert (sizetype, index),
7204 unit_size));
7206 break;
7208 case REALPART_EXPR:
7209 break;
7211 case IMAGPART_EXPR:
7212 bit_offset += *pbitsize;
7213 break;
7215 case VIEW_CONVERT_EXPR:
7216 break;
7218 case MEM_REF:
7219 /* Hand back the decl for MEM[&decl, off]. */
7220 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7222 tree off = TREE_OPERAND (exp, 1);
7223 if (!integer_zerop (off))
7225 poly_offset_int boff = mem_ref_offset (exp);
7226 boff <<= LOG2_BITS_PER_UNIT;
7227 bit_offset += boff;
7229 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7231 goto done;
7233 default:
7234 goto done;
7237 /* If any reference in the chain is volatile, the effect is volatile. */
7238 if (TREE_THIS_VOLATILE (exp))
7239 *pvolatilep = 1;
7241 exp = TREE_OPERAND (exp, 0);
7243 done:
7245 /* If OFFSET is constant, see if we can return the whole thing as a
7246 constant bit position. Make sure to handle overflow during
7247 this conversion. */
7248 if (poly_int_tree_p (offset))
7250 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7251 TYPE_PRECISION (sizetype));
7252 tem <<= LOG2_BITS_PER_UNIT;
7253 tem += bit_offset;
7254 if (tem.to_shwi (pbitpos))
7255 *poffset = offset = NULL_TREE;
7258 /* Otherwise, split it up. */
7259 if (offset)
7261 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7262 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7264 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7265 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7266 offset = size_binop (PLUS_EXPR, offset,
7267 build_int_cst (sizetype, bytes.force_shwi ()));
7270 *poffset = offset;
7273 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7274 if (mode == VOIDmode
7275 && blkmode_bitfield
7276 && multiple_p (*pbitpos, BITS_PER_UNIT)
7277 && multiple_p (*pbitsize, BITS_PER_UNIT))
7278 *pmode = BLKmode;
7279 else
7280 *pmode = mode;
7282 return exp;
7285 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7287 static unsigned HOST_WIDE_INT
7288 target_align (const_tree target)
7290 /* We might have a chain of nested references with intermediate misaligning
7291 bitfields components, so need to recurse to find out. */
7293 unsigned HOST_WIDE_INT this_align, outer_align;
7295 switch (TREE_CODE (target))
7297 case BIT_FIELD_REF:
7298 return 1;
7300 case COMPONENT_REF:
7301 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7302 outer_align = target_align (TREE_OPERAND (target, 0));
7303 return MIN (this_align, outer_align);
7305 case ARRAY_REF:
7306 case ARRAY_RANGE_REF:
7307 this_align = TYPE_ALIGN (TREE_TYPE (target));
7308 outer_align = target_align (TREE_OPERAND (target, 0));
7309 return MIN (this_align, outer_align);
7311 CASE_CONVERT:
7312 case NON_LVALUE_EXPR:
7313 case VIEW_CONVERT_EXPR:
7314 this_align = TYPE_ALIGN (TREE_TYPE (target));
7315 outer_align = target_align (TREE_OPERAND (target, 0));
7316 return MAX (this_align, outer_align);
7318 default:
7319 return TYPE_ALIGN (TREE_TYPE (target));
7324 /* Given an rtx VALUE that may contain additions and multiplications, return
7325 an equivalent value that just refers to a register, memory, or constant.
7326 This is done by generating instructions to perform the arithmetic and
7327 returning a pseudo-register containing the value.
7329 The returned value may be a REG, SUBREG, MEM or constant. */
7332 force_operand (rtx value, rtx target)
7334 rtx op1, op2;
7335 /* Use subtarget as the target for operand 0 of a binary operation. */
7336 rtx subtarget = get_subtarget (target);
7337 enum rtx_code code = GET_CODE (value);
7339 /* Check for subreg applied to an expression produced by loop optimizer. */
7340 if (code == SUBREG
7341 && !REG_P (SUBREG_REG (value))
7342 && !MEM_P (SUBREG_REG (value)))
7344 value
7345 = simplify_gen_subreg (GET_MODE (value),
7346 force_reg (GET_MODE (SUBREG_REG (value)),
7347 force_operand (SUBREG_REG (value),
7348 NULL_RTX)),
7349 GET_MODE (SUBREG_REG (value)),
7350 SUBREG_BYTE (value));
7351 code = GET_CODE (value);
7354 /* Check for a PIC address load. */
7355 if ((code == PLUS || code == MINUS)
7356 && XEXP (value, 0) == pic_offset_table_rtx
7357 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7358 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7359 || GET_CODE (XEXP (value, 1)) == CONST))
7361 if (!subtarget)
7362 subtarget = gen_reg_rtx (GET_MODE (value));
7363 emit_move_insn (subtarget, value);
7364 return subtarget;
7367 if (ARITHMETIC_P (value))
7369 op2 = XEXP (value, 1);
7370 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7371 subtarget = 0;
7372 if (code == MINUS && CONST_INT_P (op2))
7374 code = PLUS;
7375 op2 = negate_rtx (GET_MODE (value), op2);
7378 /* Check for an addition with OP2 a constant integer and our first
7379 operand a PLUS of a virtual register and something else. In that
7380 case, we want to emit the sum of the virtual register and the
7381 constant first and then add the other value. This allows virtual
7382 register instantiation to simply modify the constant rather than
7383 creating another one around this addition. */
7384 if (code == PLUS && CONST_INT_P (op2)
7385 && GET_CODE (XEXP (value, 0)) == PLUS
7386 && REG_P (XEXP (XEXP (value, 0), 0))
7387 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7388 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7390 rtx temp = expand_simple_binop (GET_MODE (value), code,
7391 XEXP (XEXP (value, 0), 0), op2,
7392 subtarget, 0, OPTAB_LIB_WIDEN);
7393 return expand_simple_binop (GET_MODE (value), code, temp,
7394 force_operand (XEXP (XEXP (value,
7395 0), 1), 0),
7396 target, 0, OPTAB_LIB_WIDEN);
7399 op1 = force_operand (XEXP (value, 0), subtarget);
7400 op2 = force_operand (op2, NULL_RTX);
7401 switch (code)
7403 case MULT:
7404 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7405 case DIV:
7406 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7407 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7408 target, 1, OPTAB_LIB_WIDEN);
7409 else
7410 return expand_divmod (0,
7411 FLOAT_MODE_P (GET_MODE (value))
7412 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7413 GET_MODE (value), op1, op2, target, 0);
7414 case MOD:
7415 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7416 target, 0);
7417 case UDIV:
7418 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7419 target, 1);
7420 case UMOD:
7421 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7422 target, 1);
7423 case ASHIFTRT:
7424 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7425 target, 0, OPTAB_LIB_WIDEN);
7426 default:
7427 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7428 target, 1, OPTAB_LIB_WIDEN);
7431 if (UNARY_P (value))
7433 if (!target)
7434 target = gen_reg_rtx (GET_MODE (value));
7435 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7436 switch (code)
7438 case ZERO_EXTEND:
7439 case SIGN_EXTEND:
7440 case TRUNCATE:
7441 case FLOAT_EXTEND:
7442 case FLOAT_TRUNCATE:
7443 convert_move (target, op1, code == ZERO_EXTEND);
7444 return target;
7446 case FIX:
7447 case UNSIGNED_FIX:
7448 expand_fix (target, op1, code == UNSIGNED_FIX);
7449 return target;
7451 case FLOAT:
7452 case UNSIGNED_FLOAT:
7453 expand_float (target, op1, code == UNSIGNED_FLOAT);
7454 return target;
7456 default:
7457 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7461 #ifdef INSN_SCHEDULING
7462 /* On machines that have insn scheduling, we want all memory reference to be
7463 explicit, so we need to deal with such paradoxical SUBREGs. */
7464 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7465 value
7466 = simplify_gen_subreg (GET_MODE (value),
7467 force_reg (GET_MODE (SUBREG_REG (value)),
7468 force_operand (SUBREG_REG (value),
7469 NULL_RTX)),
7470 GET_MODE (SUBREG_REG (value)),
7471 SUBREG_BYTE (value));
7472 #endif
7474 return value;
7477 /* Subroutine of expand_expr: return nonzero iff there is no way that
7478 EXP can reference X, which is being modified. TOP_P is nonzero if this
7479 call is going to be used to determine whether we need a temporary
7480 for EXP, as opposed to a recursive call to this function.
7482 It is always safe for this routine to return zero since it merely
7483 searches for optimization opportunities. */
7486 safe_from_p (const_rtx x, tree exp, int top_p)
7488 rtx exp_rtl = 0;
7489 int i, nops;
7491 if (x == 0
7492 /* If EXP has varying size, we MUST use a target since we currently
7493 have no way of allocating temporaries of variable size
7494 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7495 So we assume here that something at a higher level has prevented a
7496 clash. This is somewhat bogus, but the best we can do. Only
7497 do this when X is BLKmode and when we are at the top level. */
7498 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7499 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7500 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7501 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7502 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7503 != INTEGER_CST)
7504 && GET_MODE (x) == BLKmode)
7505 /* If X is in the outgoing argument area, it is always safe. */
7506 || (MEM_P (x)
7507 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7508 || (GET_CODE (XEXP (x, 0)) == PLUS
7509 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7510 return 1;
7512 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7513 find the underlying pseudo. */
7514 if (GET_CODE (x) == SUBREG)
7516 x = SUBREG_REG (x);
7517 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7518 return 0;
7521 /* Now look at our tree code and possibly recurse. */
7522 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7524 case tcc_declaration:
7525 exp_rtl = DECL_RTL_IF_SET (exp);
7526 break;
7528 case tcc_constant:
7529 return 1;
7531 case tcc_exceptional:
7532 if (TREE_CODE (exp) == TREE_LIST)
7534 while (1)
7536 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7537 return 0;
7538 exp = TREE_CHAIN (exp);
7539 if (!exp)
7540 return 1;
7541 if (TREE_CODE (exp) != TREE_LIST)
7542 return safe_from_p (x, exp, 0);
7545 else if (TREE_CODE (exp) == CONSTRUCTOR)
7547 constructor_elt *ce;
7548 unsigned HOST_WIDE_INT idx;
7550 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7551 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7552 || !safe_from_p (x, ce->value, 0))
7553 return 0;
7554 return 1;
7556 else if (TREE_CODE (exp) == ERROR_MARK)
7557 return 1; /* An already-visited SAVE_EXPR? */
7558 else
7559 return 0;
7561 case tcc_statement:
7562 /* The only case we look at here is the DECL_INITIAL inside a
7563 DECL_EXPR. */
7564 return (TREE_CODE (exp) != DECL_EXPR
7565 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7566 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7567 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7569 case tcc_binary:
7570 case tcc_comparison:
7571 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7572 return 0;
7573 /* Fall through. */
7575 case tcc_unary:
7576 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7578 case tcc_expression:
7579 case tcc_reference:
7580 case tcc_vl_exp:
7581 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7582 the expression. If it is set, we conflict iff we are that rtx or
7583 both are in memory. Otherwise, we check all operands of the
7584 expression recursively. */
7586 switch (TREE_CODE (exp))
7588 case ADDR_EXPR:
7589 /* If the operand is static or we are static, we can't conflict.
7590 Likewise if we don't conflict with the operand at all. */
7591 if (staticp (TREE_OPERAND (exp, 0))
7592 || TREE_STATIC (exp)
7593 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7594 return 1;
7596 /* Otherwise, the only way this can conflict is if we are taking
7597 the address of a DECL a that address if part of X, which is
7598 very rare. */
7599 exp = TREE_OPERAND (exp, 0);
7600 if (DECL_P (exp))
7602 if (!DECL_RTL_SET_P (exp)
7603 || !MEM_P (DECL_RTL (exp)))
7604 return 0;
7605 else
7606 exp_rtl = XEXP (DECL_RTL (exp), 0);
7608 break;
7610 case MEM_REF:
7611 if (MEM_P (x)
7612 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7613 get_alias_set (exp)))
7614 return 0;
7615 break;
7617 case CALL_EXPR:
7618 /* Assume that the call will clobber all hard registers and
7619 all of memory. */
7620 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7621 || MEM_P (x))
7622 return 0;
7623 break;
7625 case WITH_CLEANUP_EXPR:
7626 case CLEANUP_POINT_EXPR:
7627 /* Lowered by gimplify.c. */
7628 gcc_unreachable ();
7630 case SAVE_EXPR:
7631 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7633 default:
7634 break;
7637 /* If we have an rtx, we do not need to scan our operands. */
7638 if (exp_rtl)
7639 break;
7641 nops = TREE_OPERAND_LENGTH (exp);
7642 for (i = 0; i < nops; i++)
7643 if (TREE_OPERAND (exp, i) != 0
7644 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7645 return 0;
7647 break;
7649 case tcc_type:
7650 /* Should never get a type here. */
7651 gcc_unreachable ();
7654 /* If we have an rtl, find any enclosed object. Then see if we conflict
7655 with it. */
7656 if (exp_rtl)
7658 if (GET_CODE (exp_rtl) == SUBREG)
7660 exp_rtl = SUBREG_REG (exp_rtl);
7661 if (REG_P (exp_rtl)
7662 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7663 return 0;
7666 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7667 are memory and they conflict. */
7668 return ! (rtx_equal_p (x, exp_rtl)
7669 || (MEM_P (x) && MEM_P (exp_rtl)
7670 && true_dependence (exp_rtl, VOIDmode, x)));
7673 /* If we reach here, it is safe. */
7674 return 1;
7678 /* Return the highest power of two that EXP is known to be a multiple of.
7679 This is used in updating alignment of MEMs in array references. */
7681 unsigned HOST_WIDE_INT
7682 highest_pow2_factor (const_tree exp)
7684 unsigned HOST_WIDE_INT ret;
7685 int trailing_zeros = tree_ctz (exp);
7686 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7687 return BIGGEST_ALIGNMENT;
7688 ret = HOST_WIDE_INT_1U << trailing_zeros;
7689 if (ret > BIGGEST_ALIGNMENT)
7690 return BIGGEST_ALIGNMENT;
7691 return ret;
7694 /* Similar, except that the alignment requirements of TARGET are
7695 taken into account. Assume it is at least as aligned as its
7696 type, unless it is a COMPONENT_REF in which case the layout of
7697 the structure gives the alignment. */
7699 static unsigned HOST_WIDE_INT
7700 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7702 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7703 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7705 return MAX (factor, talign);
7708 /* Convert the tree comparison code TCODE to the rtl one where the
7709 signedness is UNSIGNEDP. */
7711 static enum rtx_code
7712 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7714 enum rtx_code code;
7715 switch (tcode)
7717 case EQ_EXPR:
7718 code = EQ;
7719 break;
7720 case NE_EXPR:
7721 code = NE;
7722 break;
7723 case LT_EXPR:
7724 code = unsignedp ? LTU : LT;
7725 break;
7726 case LE_EXPR:
7727 code = unsignedp ? LEU : LE;
7728 break;
7729 case GT_EXPR:
7730 code = unsignedp ? GTU : GT;
7731 break;
7732 case GE_EXPR:
7733 code = unsignedp ? GEU : GE;
7734 break;
7735 case UNORDERED_EXPR:
7736 code = UNORDERED;
7737 break;
7738 case ORDERED_EXPR:
7739 code = ORDERED;
7740 break;
7741 case UNLT_EXPR:
7742 code = UNLT;
7743 break;
7744 case UNLE_EXPR:
7745 code = UNLE;
7746 break;
7747 case UNGT_EXPR:
7748 code = UNGT;
7749 break;
7750 case UNGE_EXPR:
7751 code = UNGE;
7752 break;
7753 case UNEQ_EXPR:
7754 code = UNEQ;
7755 break;
7756 case LTGT_EXPR:
7757 code = LTGT;
7758 break;
7760 default:
7761 gcc_unreachable ();
7763 return code;
7766 /* Subroutine of expand_expr. Expand the two operands of a binary
7767 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7768 The value may be stored in TARGET if TARGET is nonzero. The
7769 MODIFIER argument is as documented by expand_expr. */
7771 void
7772 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7773 enum expand_modifier modifier)
7775 if (! safe_from_p (target, exp1, 1))
7776 target = 0;
7777 if (operand_equal_p (exp0, exp1, 0))
7779 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7780 *op1 = copy_rtx (*op0);
7782 else
7784 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7785 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7790 /* Return a MEM that contains constant EXP. DEFER is as for
7791 output_constant_def and MODIFIER is as for expand_expr. */
7793 static rtx
7794 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7796 rtx mem;
7798 mem = output_constant_def (exp, defer);
7799 if (modifier != EXPAND_INITIALIZER)
7800 mem = use_anchored_address (mem);
7801 return mem;
7804 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7805 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7807 static rtx
7808 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7809 enum expand_modifier modifier, addr_space_t as)
7811 rtx result, subtarget;
7812 tree inner, offset;
7813 poly_int64 bitsize, bitpos;
7814 int unsignedp, reversep, volatilep = 0;
7815 machine_mode mode1;
7817 /* If we are taking the address of a constant and are at the top level,
7818 we have to use output_constant_def since we can't call force_const_mem
7819 at top level. */
7820 /* ??? This should be considered a front-end bug. We should not be
7821 generating ADDR_EXPR of something that isn't an LVALUE. The only
7822 exception here is STRING_CST. */
7823 if (CONSTANT_CLASS_P (exp))
7825 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7826 if (modifier < EXPAND_SUM)
7827 result = force_operand (result, target);
7828 return result;
7831 /* Everything must be something allowed by is_gimple_addressable. */
7832 switch (TREE_CODE (exp))
7834 case INDIRECT_REF:
7835 /* This case will happen via recursion for &a->b. */
7836 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7838 case MEM_REF:
7840 tree tem = TREE_OPERAND (exp, 0);
7841 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7842 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7843 return expand_expr (tem, target, tmode, modifier);
7846 case TARGET_MEM_REF:
7847 return addr_for_mem_ref (exp, as, true);
7849 case CONST_DECL:
7850 /* Expand the initializer like constants above. */
7851 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7852 0, modifier), 0);
7853 if (modifier < EXPAND_SUM)
7854 result = force_operand (result, target);
7855 return result;
7857 case REALPART_EXPR:
7858 /* The real part of the complex number is always first, therefore
7859 the address is the same as the address of the parent object. */
7860 offset = 0;
7861 bitpos = 0;
7862 inner = TREE_OPERAND (exp, 0);
7863 break;
7865 case IMAGPART_EXPR:
7866 /* The imaginary part of the complex number is always second.
7867 The expression is therefore always offset by the size of the
7868 scalar type. */
7869 offset = 0;
7870 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7871 inner = TREE_OPERAND (exp, 0);
7872 break;
7874 case COMPOUND_LITERAL_EXPR:
7875 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7876 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7877 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7878 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7879 the initializers aren't gimplified. */
7880 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7881 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7882 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7883 target, tmode, modifier, as);
7884 /* FALLTHRU */
7885 default:
7886 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7887 expand_expr, as that can have various side effects; LABEL_DECLs for
7888 example, may not have their DECL_RTL set yet. Expand the rtl of
7889 CONSTRUCTORs too, which should yield a memory reference for the
7890 constructor's contents. Assume language specific tree nodes can
7891 be expanded in some interesting way. */
7892 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7893 if (DECL_P (exp)
7894 || TREE_CODE (exp) == CONSTRUCTOR
7895 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7897 result = expand_expr (exp, target, tmode,
7898 modifier == EXPAND_INITIALIZER
7899 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7901 /* If the DECL isn't in memory, then the DECL wasn't properly
7902 marked TREE_ADDRESSABLE, which will be either a front-end
7903 or a tree optimizer bug. */
7905 gcc_assert (MEM_P (result));
7906 result = XEXP (result, 0);
7908 /* ??? Is this needed anymore? */
7909 if (DECL_P (exp))
7910 TREE_USED (exp) = 1;
7912 if (modifier != EXPAND_INITIALIZER
7913 && modifier != EXPAND_CONST_ADDRESS
7914 && modifier != EXPAND_SUM)
7915 result = force_operand (result, target);
7916 return result;
7919 /* Pass FALSE as the last argument to get_inner_reference although
7920 we are expanding to RTL. The rationale is that we know how to
7921 handle "aligning nodes" here: we can just bypass them because
7922 they won't change the final object whose address will be returned
7923 (they actually exist only for that purpose). */
7924 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7925 &unsignedp, &reversep, &volatilep);
7926 break;
7929 /* We must have made progress. */
7930 gcc_assert (inner != exp);
7932 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
7933 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7934 inner alignment, force the inner to be sufficiently aligned. */
7935 if (CONSTANT_CLASS_P (inner)
7936 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7938 inner = copy_node (inner);
7939 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7940 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7941 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7943 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7945 if (offset)
7947 rtx tmp;
7949 if (modifier != EXPAND_NORMAL)
7950 result = force_operand (result, NULL);
7951 tmp = expand_expr (offset, NULL_RTX, tmode,
7952 modifier == EXPAND_INITIALIZER
7953 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7955 /* expand_expr is allowed to return an object in a mode other
7956 than TMODE. If it did, we need to convert. */
7957 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7958 tmp = convert_modes (tmode, GET_MODE (tmp),
7959 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7960 result = convert_memory_address_addr_space (tmode, result, as);
7961 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7963 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7964 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7965 else
7967 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
7968 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7969 1, OPTAB_LIB_WIDEN);
7973 if (maybe_ne (bitpos, 0))
7975 /* Someone beforehand should have rejected taking the address
7976 of an object that isn't byte-aligned. */
7977 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7978 result = convert_memory_address_addr_space (tmode, result, as);
7979 result = plus_constant (tmode, result, bytepos);
7980 if (modifier < EXPAND_SUM)
7981 result = force_operand (result, target);
7984 return result;
7987 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7988 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7990 static rtx
7991 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7992 enum expand_modifier modifier)
7994 addr_space_t as = ADDR_SPACE_GENERIC;
7995 scalar_int_mode address_mode = Pmode;
7996 scalar_int_mode pointer_mode = ptr_mode;
7997 machine_mode rmode;
7998 rtx result;
8000 /* Target mode of VOIDmode says "whatever's natural". */
8001 if (tmode == VOIDmode)
8002 tmode = TYPE_MODE (TREE_TYPE (exp));
8004 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8006 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8007 address_mode = targetm.addr_space.address_mode (as);
8008 pointer_mode = targetm.addr_space.pointer_mode (as);
8011 /* We can get called with some Weird Things if the user does silliness
8012 like "(short) &a". In that case, convert_memory_address won't do
8013 the right thing, so ignore the given target mode. */
8014 scalar_int_mode new_tmode = (tmode == pointer_mode
8015 ? pointer_mode
8016 : address_mode);
8018 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8019 new_tmode, modifier, as);
8021 /* Despite expand_expr claims concerning ignoring TMODE when not
8022 strictly convenient, stuff breaks if we don't honor it. Note
8023 that combined with the above, we only do this for pointer modes. */
8024 rmode = GET_MODE (result);
8025 if (rmode == VOIDmode)
8026 rmode = new_tmode;
8027 if (rmode != new_tmode)
8028 result = convert_memory_address_addr_space (new_tmode, result, as);
8030 return result;
8033 /* Generate code for computing CONSTRUCTOR EXP.
8034 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8035 is TRUE, instead of creating a temporary variable in memory
8036 NULL is returned and the caller needs to handle it differently. */
8038 static rtx
8039 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8040 bool avoid_temp_mem)
8042 tree type = TREE_TYPE (exp);
8043 machine_mode mode = TYPE_MODE (type);
8045 /* Try to avoid creating a temporary at all. This is possible
8046 if all of the initializer is zero.
8047 FIXME: try to handle all [0..255] initializers we can handle
8048 with memset. */
8049 if (TREE_STATIC (exp)
8050 && !TREE_ADDRESSABLE (exp)
8051 && target != 0 && mode == BLKmode
8052 && all_zeros_p (exp))
8054 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8055 return target;
8058 /* All elts simple constants => refer to a constant in memory. But
8059 if this is a non-BLKmode mode, let it store a field at a time
8060 since that should make a CONST_INT, CONST_WIDE_INT or
8061 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8062 use, it is best to store directly into the target unless the type
8063 is large enough that memcpy will be used. If we are making an
8064 initializer and all operands are constant, put it in memory as
8065 well.
8067 FIXME: Avoid trying to fill vector constructors piece-meal.
8068 Output them with output_constant_def below unless we're sure
8069 they're zeros. This should go away when vector initializers
8070 are treated like VECTOR_CST instead of arrays. */
8071 if ((TREE_STATIC (exp)
8072 && ((mode == BLKmode
8073 && ! (target != 0 && safe_from_p (target, exp, 1)))
8074 || TREE_ADDRESSABLE (exp)
8075 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8076 && (! can_move_by_pieces
8077 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8078 TYPE_ALIGN (type)))
8079 && ! mostly_zeros_p (exp))))
8080 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8081 && TREE_CONSTANT (exp)))
8083 rtx constructor;
8085 if (avoid_temp_mem)
8086 return NULL_RTX;
8088 constructor = expand_expr_constant (exp, 1, modifier);
8090 if (modifier != EXPAND_CONST_ADDRESS
8091 && modifier != EXPAND_INITIALIZER
8092 && modifier != EXPAND_SUM)
8093 constructor = validize_mem (constructor);
8095 return constructor;
8098 /* Handle calls that pass values in multiple non-contiguous
8099 locations. The Irix 6 ABI has examples of this. */
8100 if (target == 0 || ! safe_from_p (target, exp, 1)
8101 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8103 if (avoid_temp_mem)
8104 return NULL_RTX;
8106 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8109 store_constructor (exp, target, 0, int_expr_size (exp), false);
8110 return target;
8114 /* expand_expr: generate code for computing expression EXP.
8115 An rtx for the computed value is returned. The value is never null.
8116 In the case of a void EXP, const0_rtx is returned.
8118 The value may be stored in TARGET if TARGET is nonzero.
8119 TARGET is just a suggestion; callers must assume that
8120 the rtx returned may not be the same as TARGET.
8122 If TARGET is CONST0_RTX, it means that the value will be ignored.
8124 If TMODE is not VOIDmode, it suggests generating the
8125 result in mode TMODE. But this is done only when convenient.
8126 Otherwise, TMODE is ignored and the value generated in its natural mode.
8127 TMODE is just a suggestion; callers must assume that
8128 the rtx returned may not have mode TMODE.
8130 Note that TARGET may have neither TMODE nor MODE. In that case, it
8131 probably will not be used.
8133 If MODIFIER is EXPAND_SUM then when EXP is an addition
8134 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8135 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8136 products as above, or REG or MEM, or constant.
8137 Ordinarily in such cases we would output mul or add instructions
8138 and then return a pseudo reg containing the sum.
8140 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8141 it also marks a label as absolutely required (it can't be dead).
8142 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8143 This is used for outputting expressions used in initializers.
8145 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8146 with a constant address even if that address is not normally legitimate.
8147 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8149 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8150 a call parameter. Such targets require special care as we haven't yet
8151 marked TARGET so that it's safe from being trashed by libcalls. We
8152 don't want to use TARGET for anything but the final result;
8153 Intermediate values must go elsewhere. Additionally, calls to
8154 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8156 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8157 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8158 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8159 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8160 recursively.
8162 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8163 In this case, we don't adjust a returned MEM rtx that wouldn't be
8164 sufficiently aligned for its mode; instead, it's up to the caller
8165 to deal with it afterwards. This is used to make sure that unaligned
8166 base objects for which out-of-bounds accesses are supported, for
8167 example record types with trailing arrays, aren't realigned behind
8168 the back of the caller.
8169 The normal operating mode is to pass FALSE for this parameter. */
8172 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8173 enum expand_modifier modifier, rtx *alt_rtl,
8174 bool inner_reference_p)
8176 rtx ret;
8178 /* Handle ERROR_MARK before anybody tries to access its type. */
8179 if (TREE_CODE (exp) == ERROR_MARK
8180 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8182 ret = CONST0_RTX (tmode);
8183 return ret ? ret : const0_rtx;
8186 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8187 inner_reference_p);
8188 return ret;
8191 /* Try to expand the conditional expression which is represented by
8192 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8193 return the rtl reg which represents the result. Otherwise return
8194 NULL_RTX. */
8196 static rtx
8197 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8198 tree treeop1 ATTRIBUTE_UNUSED,
8199 tree treeop2 ATTRIBUTE_UNUSED)
8201 rtx insn;
8202 rtx op00, op01, op1, op2;
8203 enum rtx_code comparison_code;
8204 machine_mode comparison_mode;
8205 gimple *srcstmt;
8206 rtx temp;
8207 tree type = TREE_TYPE (treeop1);
8208 int unsignedp = TYPE_UNSIGNED (type);
8209 machine_mode mode = TYPE_MODE (type);
8210 machine_mode orig_mode = mode;
8211 static bool expanding_cond_expr_using_cmove = false;
8213 /* Conditional move expansion can end up TERing two operands which,
8214 when recursively hitting conditional expressions can result in
8215 exponential behavior if the cmove expansion ultimatively fails.
8216 It's hardly profitable to TER a cmove into a cmove so avoid doing
8217 that by failing early if we end up recursing. */
8218 if (expanding_cond_expr_using_cmove)
8219 return NULL_RTX;
8221 /* If we cannot do a conditional move on the mode, try doing it
8222 with the promoted mode. */
8223 if (!can_conditionally_move_p (mode))
8225 mode = promote_mode (type, mode, &unsignedp);
8226 if (!can_conditionally_move_p (mode))
8227 return NULL_RTX;
8228 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8230 else
8231 temp = assign_temp (type, 0, 1);
8233 expanding_cond_expr_using_cmove = true;
8234 start_sequence ();
8235 expand_operands (treeop1, treeop2,
8236 temp, &op1, &op2, EXPAND_NORMAL);
8238 if (TREE_CODE (treeop0) == SSA_NAME
8239 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8241 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8242 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8243 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8244 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8245 comparison_mode = TYPE_MODE (type);
8246 unsignedp = TYPE_UNSIGNED (type);
8247 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8249 else if (COMPARISON_CLASS_P (treeop0))
8251 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8252 enum tree_code cmpcode = TREE_CODE (treeop0);
8253 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8254 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8255 unsignedp = TYPE_UNSIGNED (type);
8256 comparison_mode = TYPE_MODE (type);
8257 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8259 else
8261 op00 = expand_normal (treeop0);
8262 op01 = const0_rtx;
8263 comparison_code = NE;
8264 comparison_mode = GET_MODE (op00);
8265 if (comparison_mode == VOIDmode)
8266 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8268 expanding_cond_expr_using_cmove = false;
8270 if (GET_MODE (op1) != mode)
8271 op1 = gen_lowpart (mode, op1);
8273 if (GET_MODE (op2) != mode)
8274 op2 = gen_lowpart (mode, op2);
8276 /* Try to emit the conditional move. */
8277 insn = emit_conditional_move (temp, comparison_code,
8278 op00, op01, comparison_mode,
8279 op1, op2, mode,
8280 unsignedp);
8282 /* If we could do the conditional move, emit the sequence,
8283 and return. */
8284 if (insn)
8286 rtx_insn *seq = get_insns ();
8287 end_sequence ();
8288 emit_insn (seq);
8289 return convert_modes (orig_mode, mode, temp, 0);
8292 /* Otherwise discard the sequence and fall back to code with
8293 branches. */
8294 end_sequence ();
8295 return NULL_RTX;
8299 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8300 enum expand_modifier modifier)
8302 rtx op0, op1, op2, temp;
8303 rtx_code_label *lab;
8304 tree type;
8305 int unsignedp;
8306 machine_mode mode;
8307 scalar_int_mode int_mode;
8308 enum tree_code code = ops->code;
8309 optab this_optab;
8310 rtx subtarget, original_target;
8311 int ignore;
8312 bool reduce_bit_field;
8313 location_t loc = ops->location;
8314 tree treeop0, treeop1, treeop2;
8315 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8316 ? reduce_to_bit_field_precision ((expr), \
8317 target, \
8318 type) \
8319 : (expr))
8321 type = ops->type;
8322 mode = TYPE_MODE (type);
8323 unsignedp = TYPE_UNSIGNED (type);
8325 treeop0 = ops->op0;
8326 treeop1 = ops->op1;
8327 treeop2 = ops->op2;
8329 /* We should be called only on simple (binary or unary) expressions,
8330 exactly those that are valid in gimple expressions that aren't
8331 GIMPLE_SINGLE_RHS (or invalid). */
8332 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8333 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8334 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8336 ignore = (target == const0_rtx
8337 || ((CONVERT_EXPR_CODE_P (code)
8338 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8339 && TREE_CODE (type) == VOID_TYPE));
8341 /* We should be called only if we need the result. */
8342 gcc_assert (!ignore);
8344 /* An operation in what may be a bit-field type needs the
8345 result to be reduced to the precision of the bit-field type,
8346 which is narrower than that of the type's mode. */
8347 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8348 && !type_has_mode_precision_p (type));
8350 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8351 target = 0;
8353 /* Use subtarget as the target for operand 0 of a binary operation. */
8354 subtarget = get_subtarget (target);
8355 original_target = target;
8357 switch (code)
8359 case NON_LVALUE_EXPR:
8360 case PAREN_EXPR:
8361 CASE_CONVERT:
8362 if (treeop0 == error_mark_node)
8363 return const0_rtx;
8365 if (TREE_CODE (type) == UNION_TYPE)
8367 tree valtype = TREE_TYPE (treeop0);
8369 /* If both input and output are BLKmode, this conversion isn't doing
8370 anything except possibly changing memory attribute. */
8371 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8373 rtx result = expand_expr (treeop0, target, tmode,
8374 modifier);
8376 result = copy_rtx (result);
8377 set_mem_attributes (result, type, 0);
8378 return result;
8381 if (target == 0)
8383 if (TYPE_MODE (type) != BLKmode)
8384 target = gen_reg_rtx (TYPE_MODE (type));
8385 else
8386 target = assign_temp (type, 1, 1);
8389 if (MEM_P (target))
8390 /* Store data into beginning of memory target. */
8391 store_expr (treeop0,
8392 adjust_address (target, TYPE_MODE (valtype), 0),
8393 modifier == EXPAND_STACK_PARM,
8394 false, TYPE_REVERSE_STORAGE_ORDER (type));
8396 else
8398 gcc_assert (REG_P (target)
8399 && !TYPE_REVERSE_STORAGE_ORDER (type));
8401 /* Store this field into a union of the proper type. */
8402 poly_uint64 op0_size
8403 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
8404 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
8405 store_field (target,
8406 /* The conversion must be constructed so that
8407 we know at compile time how many bits
8408 to preserve. */
8409 ordered_min (op0_size, union_size),
8410 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8411 false, false);
8414 /* Return the entire union. */
8415 return target;
8418 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8420 op0 = expand_expr (treeop0, target, VOIDmode,
8421 modifier);
8423 /* If the signedness of the conversion differs and OP0 is
8424 a promoted SUBREG, clear that indication since we now
8425 have to do the proper extension. */
8426 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8427 && GET_CODE (op0) == SUBREG)
8428 SUBREG_PROMOTED_VAR_P (op0) = 0;
8430 return REDUCE_BIT_FIELD (op0);
8433 op0 = expand_expr (treeop0, NULL_RTX, mode,
8434 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8435 if (GET_MODE (op0) == mode)
8438 /* If OP0 is a constant, just convert it into the proper mode. */
8439 else if (CONSTANT_P (op0))
8441 tree inner_type = TREE_TYPE (treeop0);
8442 machine_mode inner_mode = GET_MODE (op0);
8444 if (inner_mode == VOIDmode)
8445 inner_mode = TYPE_MODE (inner_type);
8447 if (modifier == EXPAND_INITIALIZER)
8448 op0 = lowpart_subreg (mode, op0, inner_mode);
8449 else
8450 op0= convert_modes (mode, inner_mode, op0,
8451 TYPE_UNSIGNED (inner_type));
8454 else if (modifier == EXPAND_INITIALIZER)
8455 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8456 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8458 else if (target == 0)
8459 op0 = convert_to_mode (mode, op0,
8460 TYPE_UNSIGNED (TREE_TYPE
8461 (treeop0)));
8462 else
8464 convert_move (target, op0,
8465 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8466 op0 = target;
8469 return REDUCE_BIT_FIELD (op0);
8471 case ADDR_SPACE_CONVERT_EXPR:
8473 tree treeop0_type = TREE_TYPE (treeop0);
8475 gcc_assert (POINTER_TYPE_P (type));
8476 gcc_assert (POINTER_TYPE_P (treeop0_type));
8478 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8479 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8481 /* Conversions between pointers to the same address space should
8482 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8483 gcc_assert (as_to != as_from);
8485 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8487 /* Ask target code to handle conversion between pointers
8488 to overlapping address spaces. */
8489 if (targetm.addr_space.subset_p (as_to, as_from)
8490 || targetm.addr_space.subset_p (as_from, as_to))
8492 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8494 else
8496 /* For disjoint address spaces, converting anything but a null
8497 pointer invokes undefined behavior. We truncate or extend the
8498 value as if we'd converted via integers, which handles 0 as
8499 required, and all others as the programmer likely expects. */
8500 #ifndef POINTERS_EXTEND_UNSIGNED
8501 const int POINTERS_EXTEND_UNSIGNED = 1;
8502 #endif
8503 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8504 op0, POINTERS_EXTEND_UNSIGNED);
8506 gcc_assert (op0);
8507 return op0;
8510 case POINTER_PLUS_EXPR:
8511 /* Even though the sizetype mode and the pointer's mode can be different
8512 expand is able to handle this correctly and get the correct result out
8513 of the PLUS_EXPR code. */
8514 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8515 if sizetype precision is smaller than pointer precision. */
8516 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8517 treeop1 = fold_convert_loc (loc, type,
8518 fold_convert_loc (loc, ssizetype,
8519 treeop1));
8520 /* If sizetype precision is larger than pointer precision, truncate the
8521 offset to have matching modes. */
8522 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8523 treeop1 = fold_convert_loc (loc, type, treeop1);
8524 /* FALLTHRU */
8526 case PLUS_EXPR:
8527 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8528 something else, make sure we add the register to the constant and
8529 then to the other thing. This case can occur during strength
8530 reduction and doing it this way will produce better code if the
8531 frame pointer or argument pointer is eliminated.
8533 fold-const.c will ensure that the constant is always in the inner
8534 PLUS_EXPR, so the only case we need to do anything about is if
8535 sp, ap, or fp is our second argument, in which case we must swap
8536 the innermost first argument and our second argument. */
8538 if (TREE_CODE (treeop0) == PLUS_EXPR
8539 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8540 && VAR_P (treeop1)
8541 && (DECL_RTL (treeop1) == frame_pointer_rtx
8542 || DECL_RTL (treeop1) == stack_pointer_rtx
8543 || DECL_RTL (treeop1) == arg_pointer_rtx))
8545 gcc_unreachable ();
8548 /* If the result is to be ptr_mode and we are adding an integer to
8549 something, we might be forming a constant. So try to use
8550 plus_constant. If it produces a sum and we can't accept it,
8551 use force_operand. This allows P = &ARR[const] to generate
8552 efficient code on machines where a SYMBOL_REF is not a valid
8553 address.
8555 If this is an EXPAND_SUM call, always return the sum. */
8556 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8557 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8559 if (modifier == EXPAND_STACK_PARM)
8560 target = 0;
8561 if (TREE_CODE (treeop0) == INTEGER_CST
8562 && HWI_COMPUTABLE_MODE_P (mode)
8563 && TREE_CONSTANT (treeop1))
8565 rtx constant_part;
8566 HOST_WIDE_INT wc;
8567 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8569 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8570 EXPAND_SUM);
8571 /* Use wi::shwi to ensure that the constant is
8572 truncated according to the mode of OP1, then sign extended
8573 to a HOST_WIDE_INT. Using the constant directly can result
8574 in non-canonical RTL in a 64x32 cross compile. */
8575 wc = TREE_INT_CST_LOW (treeop0);
8576 constant_part =
8577 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8578 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8579 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8580 op1 = force_operand (op1, target);
8581 return REDUCE_BIT_FIELD (op1);
8584 else if (TREE_CODE (treeop1) == INTEGER_CST
8585 && HWI_COMPUTABLE_MODE_P (mode)
8586 && TREE_CONSTANT (treeop0))
8588 rtx constant_part;
8589 HOST_WIDE_INT wc;
8590 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8592 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8593 (modifier == EXPAND_INITIALIZER
8594 ? EXPAND_INITIALIZER : EXPAND_SUM));
8595 if (! CONSTANT_P (op0))
8597 op1 = expand_expr (treeop1, NULL_RTX,
8598 VOIDmode, modifier);
8599 /* Return a PLUS if modifier says it's OK. */
8600 if (modifier == EXPAND_SUM
8601 || modifier == EXPAND_INITIALIZER)
8602 return simplify_gen_binary (PLUS, mode, op0, op1);
8603 goto binop2;
8605 /* Use wi::shwi to ensure that the constant is
8606 truncated according to the mode of OP1, then sign extended
8607 to a HOST_WIDE_INT. Using the constant directly can result
8608 in non-canonical RTL in a 64x32 cross compile. */
8609 wc = TREE_INT_CST_LOW (treeop1);
8610 constant_part
8611 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8612 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8613 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8614 op0 = force_operand (op0, target);
8615 return REDUCE_BIT_FIELD (op0);
8619 /* Use TER to expand pointer addition of a negated value
8620 as pointer subtraction. */
8621 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8622 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8623 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8624 && TREE_CODE (treeop1) == SSA_NAME
8625 && TYPE_MODE (TREE_TYPE (treeop0))
8626 == TYPE_MODE (TREE_TYPE (treeop1)))
8628 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8629 if (def)
8631 treeop1 = gimple_assign_rhs1 (def);
8632 code = MINUS_EXPR;
8633 goto do_minus;
8637 /* No sense saving up arithmetic to be done
8638 if it's all in the wrong mode to form part of an address.
8639 And force_operand won't know whether to sign-extend or
8640 zero-extend. */
8641 if (modifier != EXPAND_INITIALIZER
8642 && (modifier != EXPAND_SUM || mode != ptr_mode))
8644 expand_operands (treeop0, treeop1,
8645 subtarget, &op0, &op1, modifier);
8646 if (op0 == const0_rtx)
8647 return op1;
8648 if (op1 == const0_rtx)
8649 return op0;
8650 goto binop2;
8653 expand_operands (treeop0, treeop1,
8654 subtarget, &op0, &op1, modifier);
8655 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8657 case MINUS_EXPR:
8658 case POINTER_DIFF_EXPR:
8659 do_minus:
8660 /* For initializers, we are allowed to return a MINUS of two
8661 symbolic constants. Here we handle all cases when both operands
8662 are constant. */
8663 /* Handle difference of two symbolic constants,
8664 for the sake of an initializer. */
8665 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8666 && really_constant_p (treeop0)
8667 && really_constant_p (treeop1))
8669 expand_operands (treeop0, treeop1,
8670 NULL_RTX, &op0, &op1, modifier);
8671 return simplify_gen_binary (MINUS, mode, op0, op1);
8674 /* No sense saving up arithmetic to be done
8675 if it's all in the wrong mode to form part of an address.
8676 And force_operand won't know whether to sign-extend or
8677 zero-extend. */
8678 if (modifier != EXPAND_INITIALIZER
8679 && (modifier != EXPAND_SUM || mode != ptr_mode))
8680 goto binop;
8682 expand_operands (treeop0, treeop1,
8683 subtarget, &op0, &op1, modifier);
8685 /* Convert A - const to A + (-const). */
8686 if (CONST_INT_P (op1))
8688 op1 = negate_rtx (mode, op1);
8689 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8692 goto binop2;
8694 case WIDEN_MULT_PLUS_EXPR:
8695 case WIDEN_MULT_MINUS_EXPR:
8696 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8697 op2 = expand_normal (treeop2);
8698 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8699 target, unsignedp);
8700 return target;
8702 case WIDEN_MULT_EXPR:
8703 /* If first operand is constant, swap them.
8704 Thus the following special case checks need only
8705 check the second operand. */
8706 if (TREE_CODE (treeop0) == INTEGER_CST)
8707 std::swap (treeop0, treeop1);
8709 /* First, check if we have a multiplication of one signed and one
8710 unsigned operand. */
8711 if (TREE_CODE (treeop1) != INTEGER_CST
8712 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8713 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8715 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8716 this_optab = usmul_widen_optab;
8717 if (find_widening_optab_handler (this_optab, mode, innermode)
8718 != CODE_FOR_nothing)
8720 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8721 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8722 EXPAND_NORMAL);
8723 else
8724 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8725 EXPAND_NORMAL);
8726 /* op0 and op1 might still be constant, despite the above
8727 != INTEGER_CST check. Handle it. */
8728 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8730 op0 = convert_modes (innermode, mode, op0, true);
8731 op1 = convert_modes (innermode, mode, op1, false);
8732 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8733 target, unsignedp));
8735 goto binop3;
8738 /* Check for a multiplication with matching signedness. */
8739 else if ((TREE_CODE (treeop1) == INTEGER_CST
8740 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8741 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8742 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8744 tree op0type = TREE_TYPE (treeop0);
8745 machine_mode innermode = TYPE_MODE (op0type);
8746 bool zextend_p = TYPE_UNSIGNED (op0type);
8747 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8748 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8750 if (TREE_CODE (treeop0) != INTEGER_CST)
8752 if (find_widening_optab_handler (this_optab, mode, innermode)
8753 != CODE_FOR_nothing)
8755 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8756 EXPAND_NORMAL);
8757 /* op0 and op1 might still be constant, despite the above
8758 != INTEGER_CST check. Handle it. */
8759 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8761 widen_mult_const:
8762 op0 = convert_modes (innermode, mode, op0, zextend_p);
8764 = convert_modes (innermode, mode, op1,
8765 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8766 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8767 target,
8768 unsignedp));
8770 temp = expand_widening_mult (mode, op0, op1, target,
8771 unsignedp, this_optab);
8772 return REDUCE_BIT_FIELD (temp);
8774 if (find_widening_optab_handler (other_optab, mode, innermode)
8775 != CODE_FOR_nothing
8776 && innermode == word_mode)
8778 rtx htem, hipart;
8779 op0 = expand_normal (treeop0);
8780 if (TREE_CODE (treeop1) == INTEGER_CST)
8781 op1 = convert_modes (word_mode, mode,
8782 expand_normal (treeop1),
8783 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8784 else
8785 op1 = expand_normal (treeop1);
8786 /* op0 and op1 might still be constant, despite the above
8787 != INTEGER_CST check. Handle it. */
8788 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8789 goto widen_mult_const;
8790 temp = expand_binop (mode, other_optab, op0, op1, target,
8791 unsignedp, OPTAB_LIB_WIDEN);
8792 hipart = gen_highpart (word_mode, temp);
8793 htem = expand_mult_highpart_adjust (word_mode, hipart,
8794 op0, op1, hipart,
8795 zextend_p);
8796 if (htem != hipart)
8797 emit_move_insn (hipart, htem);
8798 return REDUCE_BIT_FIELD (temp);
8802 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8803 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8804 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8805 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8807 case MULT_EXPR:
8808 /* If this is a fixed-point operation, then we cannot use the code
8809 below because "expand_mult" doesn't support sat/no-sat fixed-point
8810 multiplications. */
8811 if (ALL_FIXED_POINT_MODE_P (mode))
8812 goto binop;
8814 /* If first operand is constant, swap them.
8815 Thus the following special case checks need only
8816 check the second operand. */
8817 if (TREE_CODE (treeop0) == INTEGER_CST)
8818 std::swap (treeop0, treeop1);
8820 /* Attempt to return something suitable for generating an
8821 indexed address, for machines that support that. */
8823 if (modifier == EXPAND_SUM && mode == ptr_mode
8824 && tree_fits_shwi_p (treeop1))
8826 tree exp1 = treeop1;
8828 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8829 EXPAND_SUM);
8831 if (!REG_P (op0))
8832 op0 = force_operand (op0, NULL_RTX);
8833 if (!REG_P (op0))
8834 op0 = copy_to_mode_reg (mode, op0);
8836 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8837 gen_int_mode (tree_to_shwi (exp1),
8838 TYPE_MODE (TREE_TYPE (exp1)))));
8841 if (modifier == EXPAND_STACK_PARM)
8842 target = 0;
8844 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8845 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8847 case TRUNC_MOD_EXPR:
8848 case FLOOR_MOD_EXPR:
8849 case CEIL_MOD_EXPR:
8850 case ROUND_MOD_EXPR:
8852 case TRUNC_DIV_EXPR:
8853 case FLOOR_DIV_EXPR:
8854 case CEIL_DIV_EXPR:
8855 case ROUND_DIV_EXPR:
8856 case EXACT_DIV_EXPR:
8858 /* If this is a fixed-point operation, then we cannot use the code
8859 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8860 divisions. */
8861 if (ALL_FIXED_POINT_MODE_P (mode))
8862 goto binop;
8864 if (modifier == EXPAND_STACK_PARM)
8865 target = 0;
8866 /* Possible optimization: compute the dividend with EXPAND_SUM
8867 then if the divisor is constant can optimize the case
8868 where some terms of the dividend have coeffs divisible by it. */
8869 expand_operands (treeop0, treeop1,
8870 subtarget, &op0, &op1, EXPAND_NORMAL);
8871 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8872 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8873 if (SCALAR_INT_MODE_P (mode)
8874 && optimize >= 2
8875 && get_range_pos_neg (treeop0) == 1
8876 && get_range_pos_neg (treeop1) == 1)
8878 /* If both arguments are known to be positive when interpreted
8879 as signed, we can expand it as both signed and unsigned
8880 division or modulo. Choose the cheaper sequence in that case. */
8881 bool speed_p = optimize_insn_for_speed_p ();
8882 do_pending_stack_adjust ();
8883 start_sequence ();
8884 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8885 rtx_insn *uns_insns = get_insns ();
8886 end_sequence ();
8887 start_sequence ();
8888 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8889 rtx_insn *sgn_insns = get_insns ();
8890 end_sequence ();
8891 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8892 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8894 /* If costs are the same then use as tie breaker the other
8895 other factor. */
8896 if (uns_cost == sgn_cost)
8898 uns_cost = seq_cost (uns_insns, !speed_p);
8899 sgn_cost = seq_cost (sgn_insns, !speed_p);
8902 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8904 emit_insn (uns_insns);
8905 return uns_ret;
8907 emit_insn (sgn_insns);
8908 return sgn_ret;
8910 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8912 case RDIV_EXPR:
8913 goto binop;
8915 case MULT_HIGHPART_EXPR:
8916 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8917 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8918 gcc_assert (temp);
8919 return temp;
8921 case FIXED_CONVERT_EXPR:
8922 op0 = expand_normal (treeop0);
8923 if (target == 0 || modifier == EXPAND_STACK_PARM)
8924 target = gen_reg_rtx (mode);
8926 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8927 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8928 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8929 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8930 else
8931 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8932 return target;
8934 case FIX_TRUNC_EXPR:
8935 op0 = expand_normal (treeop0);
8936 if (target == 0 || modifier == EXPAND_STACK_PARM)
8937 target = gen_reg_rtx (mode);
8938 expand_fix (target, op0, unsignedp);
8939 return target;
8941 case FLOAT_EXPR:
8942 op0 = expand_normal (treeop0);
8943 if (target == 0 || modifier == EXPAND_STACK_PARM)
8944 target = gen_reg_rtx (mode);
8945 /* expand_float can't figure out what to do if FROM has VOIDmode.
8946 So give it the correct mode. With -O, cse will optimize this. */
8947 if (GET_MODE (op0) == VOIDmode)
8948 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8949 op0);
8950 expand_float (target, op0,
8951 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8952 return target;
8954 case NEGATE_EXPR:
8955 op0 = expand_expr (treeop0, subtarget,
8956 VOIDmode, EXPAND_NORMAL);
8957 if (modifier == EXPAND_STACK_PARM)
8958 target = 0;
8959 temp = expand_unop (mode,
8960 optab_for_tree_code (NEGATE_EXPR, type,
8961 optab_default),
8962 op0, target, 0);
8963 gcc_assert (temp);
8964 return REDUCE_BIT_FIELD (temp);
8966 case ABS_EXPR:
8967 case ABSU_EXPR:
8968 op0 = expand_expr (treeop0, subtarget,
8969 VOIDmode, EXPAND_NORMAL);
8970 if (modifier == EXPAND_STACK_PARM)
8971 target = 0;
8973 /* ABS_EXPR is not valid for complex arguments. */
8974 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8975 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8977 /* Unsigned abs is simply the operand. Testing here means we don't
8978 risk generating incorrect code below. */
8979 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8980 return op0;
8982 return expand_abs (mode, op0, target, unsignedp,
8983 safe_from_p (target, treeop0, 1));
8985 case MAX_EXPR:
8986 case MIN_EXPR:
8987 target = original_target;
8988 if (target == 0
8989 || modifier == EXPAND_STACK_PARM
8990 || (MEM_P (target) && MEM_VOLATILE_P (target))
8991 || GET_MODE (target) != mode
8992 || (REG_P (target)
8993 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8994 target = gen_reg_rtx (mode);
8995 expand_operands (treeop0, treeop1,
8996 target, &op0, &op1, EXPAND_NORMAL);
8998 /* First try to do it with a special MIN or MAX instruction.
8999 If that does not win, use a conditional jump to select the proper
9000 value. */
9001 this_optab = optab_for_tree_code (code, type, optab_default);
9002 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9003 OPTAB_WIDEN);
9004 if (temp != 0)
9005 return temp;
9007 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
9008 and similarly for MAX <x, y>. */
9009 if (VECTOR_TYPE_P (type))
9011 tree t0 = make_tree (type, op0);
9012 tree t1 = make_tree (type, op1);
9013 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
9014 type, t0, t1);
9015 return expand_vec_cond_expr (type, comparison, t0, t1,
9016 original_target);
9019 /* At this point, a MEM target is no longer useful; we will get better
9020 code without it. */
9022 if (! REG_P (target))
9023 target = gen_reg_rtx (mode);
9025 /* If op1 was placed in target, swap op0 and op1. */
9026 if (target != op0 && target == op1)
9027 std::swap (op0, op1);
9029 /* We generate better code and avoid problems with op1 mentioning
9030 target by forcing op1 into a pseudo if it isn't a constant. */
9031 if (! CONSTANT_P (op1))
9032 op1 = force_reg (mode, op1);
9035 enum rtx_code comparison_code;
9036 rtx cmpop1 = op1;
9038 if (code == MAX_EXPR)
9039 comparison_code = unsignedp ? GEU : GE;
9040 else
9041 comparison_code = unsignedp ? LEU : LE;
9043 /* Canonicalize to comparisons against 0. */
9044 if (op1 == const1_rtx)
9046 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9047 or (a != 0 ? a : 1) for unsigned.
9048 For MIN we are safe converting (a <= 1 ? a : 1)
9049 into (a <= 0 ? a : 1) */
9050 cmpop1 = const0_rtx;
9051 if (code == MAX_EXPR)
9052 comparison_code = unsignedp ? NE : GT;
9054 if (op1 == constm1_rtx && !unsignedp)
9056 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9057 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9058 cmpop1 = const0_rtx;
9059 if (code == MIN_EXPR)
9060 comparison_code = LT;
9063 /* Use a conditional move if possible. */
9064 if (can_conditionally_move_p (mode))
9066 rtx insn;
9068 start_sequence ();
9070 /* Try to emit the conditional move. */
9071 insn = emit_conditional_move (target, comparison_code,
9072 op0, cmpop1, mode,
9073 op0, op1, mode,
9074 unsignedp);
9076 /* If we could do the conditional move, emit the sequence,
9077 and return. */
9078 if (insn)
9080 rtx_insn *seq = get_insns ();
9081 end_sequence ();
9082 emit_insn (seq);
9083 return target;
9086 /* Otherwise discard the sequence and fall back to code with
9087 branches. */
9088 end_sequence ();
9091 if (target != op0)
9092 emit_move_insn (target, op0);
9094 lab = gen_label_rtx ();
9095 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9096 unsignedp, mode, NULL_RTX, NULL, lab,
9097 profile_probability::uninitialized ());
9099 emit_move_insn (target, op1);
9100 emit_label (lab);
9101 return target;
9103 case BIT_NOT_EXPR:
9104 op0 = expand_expr (treeop0, subtarget,
9105 VOIDmode, EXPAND_NORMAL);
9106 if (modifier == EXPAND_STACK_PARM)
9107 target = 0;
9108 /* In case we have to reduce the result to bitfield precision
9109 for unsigned bitfield expand this as XOR with a proper constant
9110 instead. */
9111 if (reduce_bit_field && TYPE_UNSIGNED (type))
9113 int_mode = SCALAR_INT_TYPE_MODE (type);
9114 wide_int mask = wi::mask (TYPE_PRECISION (type),
9115 false, GET_MODE_PRECISION (int_mode));
9117 temp = expand_binop (int_mode, xor_optab, op0,
9118 immed_wide_int_const (mask, int_mode),
9119 target, 1, OPTAB_LIB_WIDEN);
9121 else
9122 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9123 gcc_assert (temp);
9124 return temp;
9126 /* ??? Can optimize bitwise operations with one arg constant.
9127 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9128 and (a bitwise1 b) bitwise2 b (etc)
9129 but that is probably not worth while. */
9131 case BIT_AND_EXPR:
9132 case BIT_IOR_EXPR:
9133 case BIT_XOR_EXPR:
9134 goto binop;
9136 case LROTATE_EXPR:
9137 case RROTATE_EXPR:
9138 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9139 || type_has_mode_precision_p (type));
9140 /* fall through */
9142 case LSHIFT_EXPR:
9143 case RSHIFT_EXPR:
9145 /* If this is a fixed-point operation, then we cannot use the code
9146 below because "expand_shift" doesn't support sat/no-sat fixed-point
9147 shifts. */
9148 if (ALL_FIXED_POINT_MODE_P (mode))
9149 goto binop;
9151 if (! safe_from_p (subtarget, treeop1, 1))
9152 subtarget = 0;
9153 if (modifier == EXPAND_STACK_PARM)
9154 target = 0;
9155 op0 = expand_expr (treeop0, subtarget,
9156 VOIDmode, EXPAND_NORMAL);
9158 /* Left shift optimization when shifting across word_size boundary.
9160 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9161 there isn't native instruction to support this wide mode
9162 left shift. Given below scenario:
9164 Type A = (Type) B << C
9166 |< T >|
9167 | dest_high | dest_low |
9169 | word_size |
9171 If the shift amount C caused we shift B to across the word
9172 size boundary, i.e part of B shifted into high half of
9173 destination register, and part of B remains in the low
9174 half, then GCC will use the following left shift expand
9175 logic:
9177 1. Initialize dest_low to B.
9178 2. Initialize every bit of dest_high to the sign bit of B.
9179 3. Logic left shift dest_low by C bit to finalize dest_low.
9180 The value of dest_low before this shift is kept in a temp D.
9181 4. Logic left shift dest_high by C.
9182 5. Logic right shift D by (word_size - C).
9183 6. Or the result of 4 and 5 to finalize dest_high.
9185 While, by checking gimple statements, if operand B is
9186 coming from signed extension, then we can simplify above
9187 expand logic into:
9189 1. dest_high = src_low >> (word_size - C).
9190 2. dest_low = src_low << C.
9192 We can use one arithmetic right shift to finish all the
9193 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9194 needed from 6 into 2.
9196 The case is similar for zero extension, except that we
9197 initialize dest_high to zero rather than copies of the sign
9198 bit from B. Furthermore, we need to use a logical right shift
9199 in this case.
9201 The choice of sign-extension versus zero-extension is
9202 determined entirely by whether or not B is signed and is
9203 independent of the current setting of unsignedp. */
9205 temp = NULL_RTX;
9206 if (code == LSHIFT_EXPR
9207 && target
9208 && REG_P (target)
9209 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9210 && mode == int_mode
9211 && TREE_CONSTANT (treeop1)
9212 && TREE_CODE (treeop0) == SSA_NAME)
9214 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9215 if (is_gimple_assign (def)
9216 && gimple_assign_rhs_code (def) == NOP_EXPR)
9218 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9219 (TREE_TYPE (gimple_assign_rhs1 (def)));
9221 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9222 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9223 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9224 >= GET_MODE_BITSIZE (word_mode)))
9226 rtx_insn *seq, *seq_old;
9227 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9228 int_mode);
9229 bool extend_unsigned
9230 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9231 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9232 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9233 rtx dest_high = simplify_gen_subreg (word_mode, target,
9234 int_mode, high_off);
9235 HOST_WIDE_INT ramount = (BITS_PER_WORD
9236 - TREE_INT_CST_LOW (treeop1));
9237 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9239 start_sequence ();
9240 /* dest_high = src_low >> (word_size - C). */
9241 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9242 rshift, dest_high,
9243 extend_unsigned);
9244 if (temp != dest_high)
9245 emit_move_insn (dest_high, temp);
9247 /* dest_low = src_low << C. */
9248 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9249 treeop1, dest_low, unsignedp);
9250 if (temp != dest_low)
9251 emit_move_insn (dest_low, temp);
9253 seq = get_insns ();
9254 end_sequence ();
9255 temp = target ;
9257 if (have_insn_for (ASHIFT, int_mode))
9259 bool speed_p = optimize_insn_for_speed_p ();
9260 start_sequence ();
9261 rtx ret_old = expand_variable_shift (code, int_mode,
9262 op0, treeop1,
9263 target,
9264 unsignedp);
9266 seq_old = get_insns ();
9267 end_sequence ();
9268 if (seq_cost (seq, speed_p)
9269 >= seq_cost (seq_old, speed_p))
9271 seq = seq_old;
9272 temp = ret_old;
9275 emit_insn (seq);
9280 if (temp == NULL_RTX)
9281 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9282 unsignedp);
9283 if (code == LSHIFT_EXPR)
9284 temp = REDUCE_BIT_FIELD (temp);
9285 return temp;
9288 /* Could determine the answer when only additive constants differ. Also,
9289 the addition of one can be handled by changing the condition. */
9290 case LT_EXPR:
9291 case LE_EXPR:
9292 case GT_EXPR:
9293 case GE_EXPR:
9294 case EQ_EXPR:
9295 case NE_EXPR:
9296 case UNORDERED_EXPR:
9297 case ORDERED_EXPR:
9298 case UNLT_EXPR:
9299 case UNLE_EXPR:
9300 case UNGT_EXPR:
9301 case UNGE_EXPR:
9302 case UNEQ_EXPR:
9303 case LTGT_EXPR:
9305 temp = do_store_flag (ops,
9306 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9307 tmode != VOIDmode ? tmode : mode);
9308 if (temp)
9309 return temp;
9311 /* Use a compare and a jump for BLKmode comparisons, or for function
9312 type comparisons is have_canonicalize_funcptr_for_compare. */
9314 if ((target == 0
9315 || modifier == EXPAND_STACK_PARM
9316 || ! safe_from_p (target, treeop0, 1)
9317 || ! safe_from_p (target, treeop1, 1)
9318 /* Make sure we don't have a hard reg (such as function's return
9319 value) live across basic blocks, if not optimizing. */
9320 || (!optimize && REG_P (target)
9321 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9322 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9324 emit_move_insn (target, const0_rtx);
9326 rtx_code_label *lab1 = gen_label_rtx ();
9327 jumpifnot_1 (code, treeop0, treeop1, lab1,
9328 profile_probability::uninitialized ());
9330 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9331 emit_move_insn (target, constm1_rtx);
9332 else
9333 emit_move_insn (target, const1_rtx);
9335 emit_label (lab1);
9336 return target;
9338 case COMPLEX_EXPR:
9339 /* Get the rtx code of the operands. */
9340 op0 = expand_normal (treeop0);
9341 op1 = expand_normal (treeop1);
9343 if (!target)
9344 target = gen_reg_rtx (TYPE_MODE (type));
9345 else
9346 /* If target overlaps with op1, then either we need to force
9347 op1 into a pseudo (if target also overlaps with op0),
9348 or write the complex parts in reverse order. */
9349 switch (GET_CODE (target))
9351 case CONCAT:
9352 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9354 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9356 complex_expr_force_op1:
9357 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9358 emit_move_insn (temp, op1);
9359 op1 = temp;
9360 break;
9362 complex_expr_swap_order:
9363 /* Move the imaginary (op1) and real (op0) parts to their
9364 location. */
9365 write_complex_part (target, op1, true);
9366 write_complex_part (target, op0, false);
9368 return target;
9370 break;
9371 case MEM:
9372 temp = adjust_address_nv (target,
9373 GET_MODE_INNER (GET_MODE (target)), 0);
9374 if (reg_overlap_mentioned_p (temp, op1))
9376 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9377 temp = adjust_address_nv (target, imode,
9378 GET_MODE_SIZE (imode));
9379 if (reg_overlap_mentioned_p (temp, op0))
9380 goto complex_expr_force_op1;
9381 goto complex_expr_swap_order;
9383 break;
9384 default:
9385 if (reg_overlap_mentioned_p (target, op1))
9387 if (reg_overlap_mentioned_p (target, op0))
9388 goto complex_expr_force_op1;
9389 goto complex_expr_swap_order;
9391 break;
9394 /* Move the real (op0) and imaginary (op1) parts to their location. */
9395 write_complex_part (target, op0, false);
9396 write_complex_part (target, op1, true);
9398 return target;
9400 case WIDEN_SUM_EXPR:
9402 tree oprnd0 = treeop0;
9403 tree oprnd1 = treeop1;
9405 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9406 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9407 target, unsignedp);
9408 return target;
9411 case VEC_UNPACK_HI_EXPR:
9412 case VEC_UNPACK_LO_EXPR:
9413 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
9414 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
9416 op0 = expand_normal (treeop0);
9417 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9418 target, unsignedp);
9419 gcc_assert (temp);
9420 return temp;
9423 case VEC_UNPACK_FLOAT_HI_EXPR:
9424 case VEC_UNPACK_FLOAT_LO_EXPR:
9426 op0 = expand_normal (treeop0);
9427 /* The signedness is determined from input operand. */
9428 temp = expand_widen_pattern_expr
9429 (ops, op0, NULL_RTX, NULL_RTX,
9430 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9432 gcc_assert (temp);
9433 return temp;
9436 case VEC_WIDEN_MULT_HI_EXPR:
9437 case VEC_WIDEN_MULT_LO_EXPR:
9438 case VEC_WIDEN_MULT_EVEN_EXPR:
9439 case VEC_WIDEN_MULT_ODD_EXPR:
9440 case VEC_WIDEN_LSHIFT_HI_EXPR:
9441 case VEC_WIDEN_LSHIFT_LO_EXPR:
9442 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9443 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9444 target, unsignedp);
9445 gcc_assert (target);
9446 return target;
9448 case VEC_PACK_TRUNC_EXPR:
9449 case VEC_PACK_SAT_EXPR:
9450 case VEC_PACK_FIX_TRUNC_EXPR:
9451 mode = TYPE_MODE (TREE_TYPE (treeop0));
9452 goto binop;
9454 case VEC_PACK_FLOAT_EXPR:
9455 mode = TYPE_MODE (TREE_TYPE (treeop0));
9456 expand_operands (treeop0, treeop1,
9457 subtarget, &op0, &op1, EXPAND_NORMAL);
9458 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
9459 optab_default);
9460 target = expand_binop (mode, this_optab, op0, op1, target,
9461 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
9462 OPTAB_LIB_WIDEN);
9463 gcc_assert (target);
9464 return target;
9466 case VEC_PERM_EXPR:
9468 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9469 vec_perm_builder sel;
9470 if (TREE_CODE (treeop2) == VECTOR_CST
9471 && tree_to_vec_perm_builder (&sel, treeop2))
9473 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
9474 temp = expand_vec_perm_const (mode, op0, op1, sel,
9475 sel_mode, target);
9477 else
9479 op2 = expand_normal (treeop2);
9480 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
9482 gcc_assert (temp);
9483 return temp;
9486 case DOT_PROD_EXPR:
9488 tree oprnd0 = treeop0;
9489 tree oprnd1 = treeop1;
9490 tree oprnd2 = treeop2;
9491 rtx op2;
9493 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9494 op2 = expand_normal (oprnd2);
9495 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9496 target, unsignedp);
9497 return target;
9500 case SAD_EXPR:
9502 tree oprnd0 = treeop0;
9503 tree oprnd1 = treeop1;
9504 tree oprnd2 = treeop2;
9505 rtx op2;
9507 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9508 op2 = expand_normal (oprnd2);
9509 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9510 target, unsignedp);
9511 return target;
9514 case REALIGN_LOAD_EXPR:
9516 tree oprnd0 = treeop0;
9517 tree oprnd1 = treeop1;
9518 tree oprnd2 = treeop2;
9519 rtx op2;
9521 this_optab = optab_for_tree_code (code, type, optab_default);
9522 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9523 op2 = expand_normal (oprnd2);
9524 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9525 target, unsignedp);
9526 gcc_assert (temp);
9527 return temp;
9530 case COND_EXPR:
9532 /* A COND_EXPR with its type being VOID_TYPE represents a
9533 conditional jump and is handled in
9534 expand_gimple_cond_expr. */
9535 gcc_assert (!VOID_TYPE_P (type));
9537 /* Note that COND_EXPRs whose type is a structure or union
9538 are required to be constructed to contain assignments of
9539 a temporary variable, so that we can evaluate them here
9540 for side effect only. If type is void, we must do likewise. */
9542 gcc_assert (!TREE_ADDRESSABLE (type)
9543 && !ignore
9544 && TREE_TYPE (treeop1) != void_type_node
9545 && TREE_TYPE (treeop2) != void_type_node);
9547 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9548 if (temp)
9549 return temp;
9551 /* If we are not to produce a result, we have no target. Otherwise,
9552 if a target was specified use it; it will not be used as an
9553 intermediate target unless it is safe. If no target, use a
9554 temporary. */
9556 if (modifier != EXPAND_STACK_PARM
9557 && original_target
9558 && safe_from_p (original_target, treeop0, 1)
9559 && GET_MODE (original_target) == mode
9560 && !MEM_P (original_target))
9561 temp = original_target;
9562 else
9563 temp = assign_temp (type, 0, 1);
9565 do_pending_stack_adjust ();
9566 NO_DEFER_POP;
9567 rtx_code_label *lab0 = gen_label_rtx ();
9568 rtx_code_label *lab1 = gen_label_rtx ();
9569 jumpifnot (treeop0, lab0,
9570 profile_probability::uninitialized ());
9571 store_expr (treeop1, temp,
9572 modifier == EXPAND_STACK_PARM,
9573 false, false);
9575 emit_jump_insn (targetm.gen_jump (lab1));
9576 emit_barrier ();
9577 emit_label (lab0);
9578 store_expr (treeop2, temp,
9579 modifier == EXPAND_STACK_PARM,
9580 false, false);
9582 emit_label (lab1);
9583 OK_DEFER_POP;
9584 return temp;
9587 case VEC_COND_EXPR:
9588 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9589 return target;
9591 case VEC_DUPLICATE_EXPR:
9592 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9593 target = expand_vector_broadcast (mode, op0);
9594 gcc_assert (target);
9595 return target;
9597 case VEC_SERIES_EXPR:
9598 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
9599 return expand_vec_series_expr (mode, op0, op1, target);
9601 case BIT_INSERT_EXPR:
9603 unsigned bitpos = tree_to_uhwi (treeop2);
9604 unsigned bitsize;
9605 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9606 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9607 else
9608 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9609 rtx op0 = expand_normal (treeop0);
9610 rtx op1 = expand_normal (treeop1);
9611 rtx dst = gen_reg_rtx (mode);
9612 emit_move_insn (dst, op0);
9613 store_bit_field (dst, bitsize, bitpos, 0, 0,
9614 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9615 return dst;
9618 default:
9619 gcc_unreachable ();
9622 /* Here to do an ordinary binary operator. */
9623 binop:
9624 expand_operands (treeop0, treeop1,
9625 subtarget, &op0, &op1, EXPAND_NORMAL);
9626 binop2:
9627 this_optab = optab_for_tree_code (code, type, optab_default);
9628 binop3:
9629 if (modifier == EXPAND_STACK_PARM)
9630 target = 0;
9631 temp = expand_binop (mode, this_optab, op0, op1, target,
9632 unsignedp, OPTAB_LIB_WIDEN);
9633 gcc_assert (temp);
9634 /* Bitwise operations do not need bitfield reduction as we expect their
9635 operands being properly truncated. */
9636 if (code == BIT_XOR_EXPR
9637 || code == BIT_AND_EXPR
9638 || code == BIT_IOR_EXPR)
9639 return temp;
9640 return REDUCE_BIT_FIELD (temp);
9642 #undef REDUCE_BIT_FIELD
9645 /* Return TRUE if expression STMT is suitable for replacement.
9646 Never consider memory loads as replaceable, because those don't ever lead
9647 into constant expressions. */
9649 static bool
9650 stmt_is_replaceable_p (gimple *stmt)
9652 if (ssa_is_replaceable_p (stmt))
9654 /* Don't move around loads. */
9655 if (!gimple_assign_single_p (stmt)
9656 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9657 return true;
9659 return false;
9663 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9664 enum expand_modifier modifier, rtx *alt_rtl,
9665 bool inner_reference_p)
9667 rtx op0, op1, temp, decl_rtl;
9668 tree type;
9669 int unsignedp;
9670 machine_mode mode, dmode;
9671 enum tree_code code = TREE_CODE (exp);
9672 rtx subtarget, original_target;
9673 int ignore;
9674 tree context;
9675 bool reduce_bit_field;
9676 location_t loc = EXPR_LOCATION (exp);
9677 struct separate_ops ops;
9678 tree treeop0, treeop1, treeop2;
9679 tree ssa_name = NULL_TREE;
9680 gimple *g;
9682 type = TREE_TYPE (exp);
9683 mode = TYPE_MODE (type);
9684 unsignedp = TYPE_UNSIGNED (type);
9686 treeop0 = treeop1 = treeop2 = NULL_TREE;
9687 if (!VL_EXP_CLASS_P (exp))
9688 switch (TREE_CODE_LENGTH (code))
9690 default:
9691 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9692 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9693 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9694 case 0: break;
9696 ops.code = code;
9697 ops.type = type;
9698 ops.op0 = treeop0;
9699 ops.op1 = treeop1;
9700 ops.op2 = treeop2;
9701 ops.location = loc;
9703 ignore = (target == const0_rtx
9704 || ((CONVERT_EXPR_CODE_P (code)
9705 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9706 && TREE_CODE (type) == VOID_TYPE));
9708 /* An operation in what may be a bit-field type needs the
9709 result to be reduced to the precision of the bit-field type,
9710 which is narrower than that of the type's mode. */
9711 reduce_bit_field = (!ignore
9712 && INTEGRAL_TYPE_P (type)
9713 && !type_has_mode_precision_p (type));
9715 /* If we are going to ignore this result, we need only do something
9716 if there is a side-effect somewhere in the expression. If there
9717 is, short-circuit the most common cases here. Note that we must
9718 not call expand_expr with anything but const0_rtx in case this
9719 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9721 if (ignore)
9723 if (! TREE_SIDE_EFFECTS (exp))
9724 return const0_rtx;
9726 /* Ensure we reference a volatile object even if value is ignored, but
9727 don't do this if all we are doing is taking its address. */
9728 if (TREE_THIS_VOLATILE (exp)
9729 && TREE_CODE (exp) != FUNCTION_DECL
9730 && mode != VOIDmode && mode != BLKmode
9731 && modifier != EXPAND_CONST_ADDRESS)
9733 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9734 if (MEM_P (temp))
9735 copy_to_reg (temp);
9736 return const0_rtx;
9739 if (TREE_CODE_CLASS (code) == tcc_unary
9740 || code == BIT_FIELD_REF
9741 || code == COMPONENT_REF
9742 || code == INDIRECT_REF)
9743 return expand_expr (treeop0, const0_rtx, VOIDmode,
9744 modifier);
9746 else if (TREE_CODE_CLASS (code) == tcc_binary
9747 || TREE_CODE_CLASS (code) == tcc_comparison
9748 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9750 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9751 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9752 return const0_rtx;
9755 target = 0;
9758 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9759 target = 0;
9761 /* Use subtarget as the target for operand 0 of a binary operation. */
9762 subtarget = get_subtarget (target);
9763 original_target = target;
9765 switch (code)
9767 case LABEL_DECL:
9769 tree function = decl_function_context (exp);
9771 temp = label_rtx (exp);
9772 temp = gen_rtx_LABEL_REF (Pmode, temp);
9774 if (function != current_function_decl
9775 && function != 0)
9776 LABEL_REF_NONLOCAL_P (temp) = 1;
9778 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9779 return temp;
9782 case SSA_NAME:
9783 /* ??? ivopts calls expander, without any preparation from
9784 out-of-ssa. So fake instructions as if this was an access to the
9785 base variable. This unnecessarily allocates a pseudo, see how we can
9786 reuse it, if partition base vars have it set already. */
9787 if (!currently_expanding_to_rtl)
9789 tree var = SSA_NAME_VAR (exp);
9790 if (var && DECL_RTL_SET_P (var))
9791 return DECL_RTL (var);
9792 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9793 LAST_VIRTUAL_REGISTER + 1);
9796 g = get_gimple_for_ssa_name (exp);
9797 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9798 if (g == NULL
9799 && modifier == EXPAND_INITIALIZER
9800 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9801 && (optimize || !SSA_NAME_VAR (exp)
9802 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9803 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9804 g = SSA_NAME_DEF_STMT (exp);
9805 if (g)
9807 rtx r;
9808 location_t saved_loc = curr_insn_location ();
9809 location_t loc = gimple_location (g);
9810 if (loc != UNKNOWN_LOCATION)
9811 set_curr_insn_location (loc);
9812 ops.code = gimple_assign_rhs_code (g);
9813 switch (get_gimple_rhs_class (ops.code))
9815 case GIMPLE_TERNARY_RHS:
9816 ops.op2 = gimple_assign_rhs3 (g);
9817 /* Fallthru */
9818 case GIMPLE_BINARY_RHS:
9819 ops.op1 = gimple_assign_rhs2 (g);
9821 /* Try to expand conditonal compare. */
9822 if (targetm.gen_ccmp_first)
9824 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9825 r = expand_ccmp_expr (g, mode);
9826 if (r)
9827 break;
9829 /* Fallthru */
9830 case GIMPLE_UNARY_RHS:
9831 ops.op0 = gimple_assign_rhs1 (g);
9832 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9833 ops.location = loc;
9834 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9835 break;
9836 case GIMPLE_SINGLE_RHS:
9838 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9839 tmode, modifier, alt_rtl,
9840 inner_reference_p);
9841 break;
9843 default:
9844 gcc_unreachable ();
9846 set_curr_insn_location (saved_loc);
9847 if (REG_P (r) && !REG_EXPR (r))
9848 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9849 return r;
9852 ssa_name = exp;
9853 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9854 exp = SSA_NAME_VAR (ssa_name);
9855 goto expand_decl_rtl;
9857 case PARM_DECL:
9858 case VAR_DECL:
9859 /* If a static var's type was incomplete when the decl was written,
9860 but the type is complete now, lay out the decl now. */
9861 if (DECL_SIZE (exp) == 0
9862 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9863 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9864 layout_decl (exp, 0);
9866 /* fall through */
9868 case FUNCTION_DECL:
9869 case RESULT_DECL:
9870 decl_rtl = DECL_RTL (exp);
9871 expand_decl_rtl:
9872 gcc_assert (decl_rtl);
9874 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9875 settings for VECTOR_TYPE_P that might switch for the function. */
9876 if (currently_expanding_to_rtl
9877 && code == VAR_DECL && MEM_P (decl_rtl)
9878 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9879 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9880 else
9881 decl_rtl = copy_rtx (decl_rtl);
9883 /* Record writes to register variables. */
9884 if (modifier == EXPAND_WRITE
9885 && REG_P (decl_rtl)
9886 && HARD_REGISTER_P (decl_rtl))
9887 add_to_hard_reg_set (&crtl->asm_clobbers,
9888 GET_MODE (decl_rtl), REGNO (decl_rtl));
9890 /* Ensure variable marked as used even if it doesn't go through
9891 a parser. If it hasn't be used yet, write out an external
9892 definition. */
9893 if (exp)
9894 TREE_USED (exp) = 1;
9896 /* Show we haven't gotten RTL for this yet. */
9897 temp = 0;
9899 /* Variables inherited from containing functions should have
9900 been lowered by this point. */
9901 if (exp)
9902 context = decl_function_context (exp);
9903 gcc_assert (!exp
9904 || SCOPE_FILE_SCOPE_P (context)
9905 || context == current_function_decl
9906 || TREE_STATIC (exp)
9907 || DECL_EXTERNAL (exp)
9908 /* ??? C++ creates functions that are not TREE_STATIC. */
9909 || TREE_CODE (exp) == FUNCTION_DECL);
9911 /* This is the case of an array whose size is to be determined
9912 from its initializer, while the initializer is still being parsed.
9913 ??? We aren't parsing while expanding anymore. */
9915 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9916 temp = validize_mem (decl_rtl);
9918 /* If DECL_RTL is memory, we are in the normal case and the
9919 address is not valid, get the address into a register. */
9921 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9923 if (alt_rtl)
9924 *alt_rtl = decl_rtl;
9925 decl_rtl = use_anchored_address (decl_rtl);
9926 if (modifier != EXPAND_CONST_ADDRESS
9927 && modifier != EXPAND_SUM
9928 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9929 : GET_MODE (decl_rtl),
9930 XEXP (decl_rtl, 0),
9931 MEM_ADDR_SPACE (decl_rtl)))
9932 temp = replace_equiv_address (decl_rtl,
9933 copy_rtx (XEXP (decl_rtl, 0)));
9936 /* If we got something, return it. But first, set the alignment
9937 if the address is a register. */
9938 if (temp != 0)
9940 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9941 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9943 return temp;
9946 if (exp)
9947 dmode = DECL_MODE (exp);
9948 else
9949 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9951 /* If the mode of DECL_RTL does not match that of the decl,
9952 there are two cases: we are dealing with a BLKmode value
9953 that is returned in a register, or we are dealing with
9954 a promoted value. In the latter case, return a SUBREG
9955 of the wanted mode, but mark it so that we know that it
9956 was already extended. */
9957 if (REG_P (decl_rtl)
9958 && dmode != BLKmode
9959 && GET_MODE (decl_rtl) != dmode)
9961 machine_mode pmode;
9963 /* Get the signedness to be used for this variable. Ensure we get
9964 the same mode we got when the variable was declared. */
9965 if (code != SSA_NAME)
9966 pmode = promote_decl_mode (exp, &unsignedp);
9967 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9968 && gimple_code (g) == GIMPLE_CALL
9969 && !gimple_call_internal_p (g))
9970 pmode = promote_function_mode (type, mode, &unsignedp,
9971 gimple_call_fntype (g),
9973 else
9974 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9975 gcc_assert (GET_MODE (decl_rtl) == pmode);
9977 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9978 SUBREG_PROMOTED_VAR_P (temp) = 1;
9979 SUBREG_PROMOTED_SET (temp, unsignedp);
9980 return temp;
9983 return decl_rtl;
9985 case INTEGER_CST:
9987 /* Given that TYPE_PRECISION (type) is not always equal to
9988 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9989 the former to the latter according to the signedness of the
9990 type. */
9991 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
9992 temp = immed_wide_int_const
9993 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
9994 return temp;
9997 case VECTOR_CST:
9999 tree tmp = NULL_TREE;
10000 if (VECTOR_MODE_P (mode))
10001 return const_vector_from_tree (exp);
10002 scalar_int_mode int_mode;
10003 if (is_int_mode (mode, &int_mode))
10005 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
10006 return const_scalar_mask_from_tree (int_mode, exp);
10007 else
10009 tree type_for_mode
10010 = lang_hooks.types.type_for_mode (int_mode, 1);
10011 if (type_for_mode)
10012 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10013 type_for_mode, exp);
10016 if (!tmp)
10018 vec<constructor_elt, va_gc> *v;
10019 /* Constructors need to be fixed-length. FIXME. */
10020 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10021 vec_alloc (v, nunits);
10022 for (unsigned int i = 0; i < nunits; ++i)
10023 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10024 tmp = build_constructor (type, v);
10026 return expand_expr (tmp, ignore ? const0_rtx : target,
10027 tmode, modifier);
10030 case CONST_DECL:
10031 if (modifier == EXPAND_WRITE)
10033 /* Writing into CONST_DECL is always invalid, but handle it
10034 gracefully. */
10035 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10036 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10037 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10038 EXPAND_NORMAL, as);
10039 op0 = memory_address_addr_space (mode, op0, as);
10040 temp = gen_rtx_MEM (mode, op0);
10041 set_mem_addr_space (temp, as);
10042 return temp;
10044 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10046 case REAL_CST:
10047 /* If optimized, generate immediate CONST_DOUBLE
10048 which will be turned into memory by reload if necessary.
10050 We used to force a register so that loop.c could see it. But
10051 this does not allow gen_* patterns to perform optimizations with
10052 the constants. It also produces two insns in cases like "x = 1.0;".
10053 On most machines, floating-point constants are not permitted in
10054 many insns, so we'd end up copying it to a register in any case.
10056 Now, we do the copying in expand_binop, if appropriate. */
10057 return const_double_from_real_value (TREE_REAL_CST (exp),
10058 TYPE_MODE (TREE_TYPE (exp)));
10060 case FIXED_CST:
10061 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10062 TYPE_MODE (TREE_TYPE (exp)));
10064 case COMPLEX_CST:
10065 /* Handle evaluating a complex constant in a CONCAT target. */
10066 if (original_target && GET_CODE (original_target) == CONCAT)
10068 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10069 rtx rtarg, itarg;
10071 rtarg = XEXP (original_target, 0);
10072 itarg = XEXP (original_target, 1);
10074 /* Move the real and imaginary parts separately. */
10075 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10076 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10078 if (op0 != rtarg)
10079 emit_move_insn (rtarg, op0);
10080 if (op1 != itarg)
10081 emit_move_insn (itarg, op1);
10083 return original_target;
10086 /* fall through */
10088 case STRING_CST:
10089 temp = expand_expr_constant (exp, 1, modifier);
10091 /* temp contains a constant address.
10092 On RISC machines where a constant address isn't valid,
10093 make some insns to get that address into a register. */
10094 if (modifier != EXPAND_CONST_ADDRESS
10095 && modifier != EXPAND_INITIALIZER
10096 && modifier != EXPAND_SUM
10097 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10098 MEM_ADDR_SPACE (temp)))
10099 return replace_equiv_address (temp,
10100 copy_rtx (XEXP (temp, 0)));
10101 return temp;
10103 case POLY_INT_CST:
10104 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10106 case SAVE_EXPR:
10108 tree val = treeop0;
10109 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10110 inner_reference_p);
10112 if (!SAVE_EXPR_RESOLVED_P (exp))
10114 /* We can indeed still hit this case, typically via builtin
10115 expanders calling save_expr immediately before expanding
10116 something. Assume this means that we only have to deal
10117 with non-BLKmode values. */
10118 gcc_assert (GET_MODE (ret) != BLKmode);
10120 val = build_decl (curr_insn_location (),
10121 VAR_DECL, NULL, TREE_TYPE (exp));
10122 DECL_ARTIFICIAL (val) = 1;
10123 DECL_IGNORED_P (val) = 1;
10124 treeop0 = val;
10125 TREE_OPERAND (exp, 0) = treeop0;
10126 SAVE_EXPR_RESOLVED_P (exp) = 1;
10128 if (!CONSTANT_P (ret))
10129 ret = copy_to_reg (ret);
10130 SET_DECL_RTL (val, ret);
10133 return ret;
10137 case CONSTRUCTOR:
10138 /* If we don't need the result, just ensure we evaluate any
10139 subexpressions. */
10140 if (ignore)
10142 unsigned HOST_WIDE_INT idx;
10143 tree value;
10145 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10146 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10148 return const0_rtx;
10151 return expand_constructor (exp, target, modifier, false);
10153 case TARGET_MEM_REF:
10155 addr_space_t as
10156 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10157 enum insn_code icode;
10158 unsigned int align;
10160 op0 = addr_for_mem_ref (exp, as, true);
10161 op0 = memory_address_addr_space (mode, op0, as);
10162 temp = gen_rtx_MEM (mode, op0);
10163 set_mem_attributes (temp, exp, 0);
10164 set_mem_addr_space (temp, as);
10165 align = get_object_alignment (exp);
10166 if (modifier != EXPAND_WRITE
10167 && modifier != EXPAND_MEMORY
10168 && mode != BLKmode
10169 && align < GET_MODE_ALIGNMENT (mode)
10170 /* If the target does not have special handling for unaligned
10171 loads of mode then it can use regular moves for them. */
10172 && ((icode = optab_handler (movmisalign_optab, mode))
10173 != CODE_FOR_nothing))
10175 struct expand_operand ops[2];
10177 /* We've already validated the memory, and we're creating a
10178 new pseudo destination. The predicates really can't fail,
10179 nor can the generator. */
10180 create_output_operand (&ops[0], NULL_RTX, mode);
10181 create_fixed_operand (&ops[1], temp);
10182 expand_insn (icode, 2, ops);
10183 temp = ops[0].value;
10185 return temp;
10188 case MEM_REF:
10190 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10191 addr_space_t as
10192 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10193 machine_mode address_mode;
10194 tree base = TREE_OPERAND (exp, 0);
10195 gimple *def_stmt;
10196 enum insn_code icode;
10197 unsigned align;
10198 /* Handle expansion of non-aliased memory with non-BLKmode. That
10199 might end up in a register. */
10200 if (mem_ref_refers_to_non_mem_p (exp))
10202 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10203 base = TREE_OPERAND (base, 0);
10204 poly_uint64 type_size;
10205 if (known_eq (offset, 0)
10206 && !reverse
10207 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
10208 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
10209 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10210 target, tmode, modifier);
10211 if (TYPE_MODE (type) == BLKmode)
10213 temp = assign_stack_temp (DECL_MODE (base),
10214 GET_MODE_SIZE (DECL_MODE (base)));
10215 store_expr (base, temp, 0, false, false);
10216 temp = adjust_address (temp, BLKmode, offset);
10217 set_mem_size (temp, int_size_in_bytes (type));
10218 return temp;
10220 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10221 bitsize_int (offset * BITS_PER_UNIT));
10222 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10223 return expand_expr (exp, target, tmode, modifier);
10225 address_mode = targetm.addr_space.address_mode (as);
10226 base = TREE_OPERAND (exp, 0);
10227 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10229 tree mask = gimple_assign_rhs2 (def_stmt);
10230 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10231 gimple_assign_rhs1 (def_stmt), mask);
10232 TREE_OPERAND (exp, 0) = base;
10234 align = get_object_alignment (exp);
10235 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10236 op0 = memory_address_addr_space (mode, op0, as);
10237 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10239 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10240 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10241 op0 = memory_address_addr_space (mode, op0, as);
10243 temp = gen_rtx_MEM (mode, op0);
10244 set_mem_attributes (temp, exp, 0);
10245 set_mem_addr_space (temp, as);
10246 if (TREE_THIS_VOLATILE (exp))
10247 MEM_VOLATILE_P (temp) = 1;
10248 if (modifier != EXPAND_WRITE
10249 && modifier != EXPAND_MEMORY
10250 && !inner_reference_p
10251 && mode != BLKmode
10252 && align < GET_MODE_ALIGNMENT (mode))
10254 if ((icode = optab_handler (movmisalign_optab, mode))
10255 != CODE_FOR_nothing)
10257 struct expand_operand ops[2];
10259 /* We've already validated the memory, and we're creating a
10260 new pseudo destination. The predicates really can't fail,
10261 nor can the generator. */
10262 create_output_operand (&ops[0], NULL_RTX, mode);
10263 create_fixed_operand (&ops[1], temp);
10264 expand_insn (icode, 2, ops);
10265 temp = ops[0].value;
10267 else if (targetm.slow_unaligned_access (mode, align))
10268 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10269 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10270 (modifier == EXPAND_STACK_PARM
10271 ? NULL_RTX : target),
10272 mode, mode, false, alt_rtl);
10274 if (reverse
10275 && modifier != EXPAND_MEMORY
10276 && modifier != EXPAND_WRITE)
10277 temp = flip_storage_order (mode, temp);
10278 return temp;
10281 case ARRAY_REF:
10284 tree array = treeop0;
10285 tree index = treeop1;
10286 tree init;
10288 /* Fold an expression like: "foo"[2].
10289 This is not done in fold so it won't happen inside &.
10290 Don't fold if this is for wide characters since it's too
10291 difficult to do correctly and this is a very rare case. */
10293 if (modifier != EXPAND_CONST_ADDRESS
10294 && modifier != EXPAND_INITIALIZER
10295 && modifier != EXPAND_MEMORY)
10297 tree t = fold_read_from_constant_string (exp);
10299 if (t)
10300 return expand_expr (t, target, tmode, modifier);
10303 /* If this is a constant index into a constant array,
10304 just get the value from the array. Handle both the cases when
10305 we have an explicit constructor and when our operand is a variable
10306 that was declared const. */
10308 if (modifier != EXPAND_CONST_ADDRESS
10309 && modifier != EXPAND_INITIALIZER
10310 && modifier != EXPAND_MEMORY
10311 && TREE_CODE (array) == CONSTRUCTOR
10312 && ! TREE_SIDE_EFFECTS (array)
10313 && TREE_CODE (index) == INTEGER_CST)
10315 unsigned HOST_WIDE_INT ix;
10316 tree field, value;
10318 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10319 field, value)
10320 if (tree_int_cst_equal (field, index))
10322 if (!TREE_SIDE_EFFECTS (value))
10323 return expand_expr (fold (value), target, tmode, modifier);
10324 break;
10328 else if (optimize >= 1
10329 && modifier != EXPAND_CONST_ADDRESS
10330 && modifier != EXPAND_INITIALIZER
10331 && modifier != EXPAND_MEMORY
10332 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10333 && TREE_CODE (index) == INTEGER_CST
10334 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10335 && (init = ctor_for_folding (array)) != error_mark_node)
10337 if (init == NULL_TREE)
10339 tree value = build_zero_cst (type);
10340 if (TREE_CODE (value) == CONSTRUCTOR)
10342 /* If VALUE is a CONSTRUCTOR, this optimization is only
10343 useful if this doesn't store the CONSTRUCTOR into
10344 memory. If it does, it is more efficient to just
10345 load the data from the array directly. */
10346 rtx ret = expand_constructor (value, target,
10347 modifier, true);
10348 if (ret == NULL_RTX)
10349 value = NULL_TREE;
10352 if (value)
10353 return expand_expr (value, target, tmode, modifier);
10355 else if (TREE_CODE (init) == CONSTRUCTOR)
10357 unsigned HOST_WIDE_INT ix;
10358 tree field, value;
10360 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10361 field, value)
10362 if (tree_int_cst_equal (field, index))
10364 if (TREE_SIDE_EFFECTS (value))
10365 break;
10367 if (TREE_CODE (value) == CONSTRUCTOR)
10369 /* If VALUE is a CONSTRUCTOR, this
10370 optimization is only useful if
10371 this doesn't store the CONSTRUCTOR
10372 into memory. If it does, it is more
10373 efficient to just load the data from
10374 the array directly. */
10375 rtx ret = expand_constructor (value, target,
10376 modifier, true);
10377 if (ret == NULL_RTX)
10378 break;
10381 return
10382 expand_expr (fold (value), target, tmode, modifier);
10385 else if (TREE_CODE (init) == STRING_CST)
10387 tree low_bound = array_ref_low_bound (exp);
10388 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10390 /* Optimize the special case of a zero lower bound.
10392 We convert the lower bound to sizetype to avoid problems
10393 with constant folding. E.g. suppose the lower bound is
10394 1 and its mode is QI. Without the conversion
10395 (ARRAY + (INDEX - (unsigned char)1))
10396 becomes
10397 (ARRAY + (-(unsigned char)1) + INDEX)
10398 which becomes
10399 (ARRAY + 255 + INDEX). Oops! */
10400 if (!integer_zerop (low_bound))
10401 index1 = size_diffop_loc (loc, index1,
10402 fold_convert_loc (loc, sizetype,
10403 low_bound));
10405 if (tree_fits_uhwi_p (index1)
10406 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10408 tree type = TREE_TYPE (TREE_TYPE (init));
10409 scalar_int_mode mode;
10411 if (is_int_mode (TYPE_MODE (type), &mode)
10412 && GET_MODE_SIZE (mode) == 1)
10413 return gen_int_mode (TREE_STRING_POINTER (init)
10414 [TREE_INT_CST_LOW (index1)],
10415 mode);
10420 goto normal_inner_ref;
10422 case COMPONENT_REF:
10423 /* If the operand is a CONSTRUCTOR, we can just extract the
10424 appropriate field if it is present. */
10425 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10427 unsigned HOST_WIDE_INT idx;
10428 tree field, value;
10429 scalar_int_mode field_mode;
10431 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10432 idx, field, value)
10433 if (field == treeop1
10434 /* We can normally use the value of the field in the
10435 CONSTRUCTOR. However, if this is a bitfield in
10436 an integral mode that we can fit in a HOST_WIDE_INT,
10437 we must mask only the number of bits in the bitfield,
10438 since this is done implicitly by the constructor. If
10439 the bitfield does not meet either of those conditions,
10440 we can't do this optimization. */
10441 && (! DECL_BIT_FIELD (field)
10442 || (is_int_mode (DECL_MODE (field), &field_mode)
10443 && (GET_MODE_PRECISION (field_mode)
10444 <= HOST_BITS_PER_WIDE_INT))))
10446 if (DECL_BIT_FIELD (field)
10447 && modifier == EXPAND_STACK_PARM)
10448 target = 0;
10449 op0 = expand_expr (value, target, tmode, modifier);
10450 if (DECL_BIT_FIELD (field))
10452 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10453 scalar_int_mode imode
10454 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10456 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10458 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10459 imode);
10460 op0 = expand_and (imode, op0, op1, target);
10462 else
10464 int count = GET_MODE_PRECISION (imode) - bitsize;
10466 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10467 target, 0);
10468 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10469 target, 0);
10473 return op0;
10476 goto normal_inner_ref;
10478 case BIT_FIELD_REF:
10479 case ARRAY_RANGE_REF:
10480 normal_inner_ref:
10482 machine_mode mode1, mode2;
10483 poly_int64 bitsize, bitpos, bytepos;
10484 tree offset;
10485 int reversep, volatilep = 0, must_force_mem;
10486 tree tem
10487 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10488 &unsignedp, &reversep, &volatilep);
10489 rtx orig_op0, memloc;
10490 bool clear_mem_expr = false;
10492 /* If we got back the original object, something is wrong. Perhaps
10493 we are evaluating an expression too early. In any event, don't
10494 infinitely recurse. */
10495 gcc_assert (tem != exp);
10497 /* If TEM's type is a union of variable size, pass TARGET to the inner
10498 computation, since it will need a temporary and TARGET is known
10499 to have to do. This occurs in unchecked conversion in Ada. */
10500 orig_op0 = op0
10501 = expand_expr_real (tem,
10502 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10503 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10504 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10505 != INTEGER_CST)
10506 && modifier != EXPAND_STACK_PARM
10507 ? target : NULL_RTX),
10508 VOIDmode,
10509 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10510 NULL, true);
10512 /* If the field has a mode, we want to access it in the
10513 field's mode, not the computed mode.
10514 If a MEM has VOIDmode (external with incomplete type),
10515 use BLKmode for it instead. */
10516 if (MEM_P (op0))
10518 if (mode1 != VOIDmode)
10519 op0 = adjust_address (op0, mode1, 0);
10520 else if (GET_MODE (op0) == VOIDmode)
10521 op0 = adjust_address (op0, BLKmode, 0);
10524 mode2
10525 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10527 /* If we have either an offset, a BLKmode result, or a reference
10528 outside the underlying object, we must force it to memory.
10529 Such a case can occur in Ada if we have unchecked conversion
10530 of an expression from a scalar type to an aggregate type or
10531 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10532 passed a partially uninitialized object or a view-conversion
10533 to a larger size. */
10534 must_force_mem = (offset
10535 || mode1 == BLKmode
10536 || (mode == BLKmode
10537 && !int_mode_for_size (bitsize, 1).exists ())
10538 || maybe_gt (bitpos + bitsize,
10539 GET_MODE_BITSIZE (mode2)));
10541 /* Handle CONCAT first. */
10542 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10544 if (known_eq (bitpos, 0)
10545 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
10546 && COMPLEX_MODE_P (mode1)
10547 && COMPLEX_MODE_P (GET_MODE (op0))
10548 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10549 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10551 if (reversep)
10552 op0 = flip_storage_order (GET_MODE (op0), op0);
10553 if (mode1 != GET_MODE (op0))
10555 rtx parts[2];
10556 for (int i = 0; i < 2; i++)
10558 rtx op = read_complex_part (op0, i != 0);
10559 if (GET_CODE (op) == SUBREG)
10560 op = force_reg (GET_MODE (op), op);
10561 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10562 op);
10563 if (temp)
10564 op = temp;
10565 else
10567 if (!REG_P (op) && !MEM_P (op))
10568 op = force_reg (GET_MODE (op), op);
10569 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10571 parts[i] = op;
10573 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10575 return op0;
10577 if (known_eq (bitpos, 0)
10578 && known_eq (bitsize,
10579 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10580 && maybe_ne (bitsize, 0))
10582 op0 = XEXP (op0, 0);
10583 mode2 = GET_MODE (op0);
10585 else if (known_eq (bitpos,
10586 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10587 && known_eq (bitsize,
10588 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
10589 && maybe_ne (bitpos, 0)
10590 && maybe_ne (bitsize, 0))
10592 op0 = XEXP (op0, 1);
10593 bitpos = 0;
10594 mode2 = GET_MODE (op0);
10596 else
10597 /* Otherwise force into memory. */
10598 must_force_mem = 1;
10601 /* If this is a constant, put it in a register if it is a legitimate
10602 constant and we don't need a memory reference. */
10603 if (CONSTANT_P (op0)
10604 && mode2 != BLKmode
10605 && targetm.legitimate_constant_p (mode2, op0)
10606 && !must_force_mem)
10607 op0 = force_reg (mode2, op0);
10609 /* Otherwise, if this is a constant, try to force it to the constant
10610 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10611 is a legitimate constant. */
10612 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10613 op0 = validize_mem (memloc);
10615 /* Otherwise, if this is a constant or the object is not in memory
10616 and need be, put it there. */
10617 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10619 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10620 emit_move_insn (memloc, op0);
10621 op0 = memloc;
10622 clear_mem_expr = true;
10625 if (offset)
10627 machine_mode address_mode;
10628 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10629 EXPAND_SUM);
10631 gcc_assert (MEM_P (op0));
10633 address_mode = get_address_mode (op0);
10634 if (GET_MODE (offset_rtx) != address_mode)
10636 /* We cannot be sure that the RTL in offset_rtx is valid outside
10637 of a memory address context, so force it into a register
10638 before attempting to convert it to the desired mode. */
10639 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10640 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10643 /* See the comment in expand_assignment for the rationale. */
10644 if (mode1 != VOIDmode
10645 && maybe_ne (bitpos, 0)
10646 && maybe_gt (bitsize, 0)
10647 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10648 && multiple_p (bitpos, bitsize)
10649 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
10650 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10652 op0 = adjust_address (op0, mode1, bytepos);
10653 bitpos = 0;
10656 op0 = offset_address (op0, offset_rtx,
10657 highest_pow2_factor (offset));
10660 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10661 record its alignment as BIGGEST_ALIGNMENT. */
10662 if (MEM_P (op0)
10663 && known_eq (bitpos, 0)
10664 && offset != 0
10665 && is_aligning_offset (offset, tem))
10666 set_mem_align (op0, BIGGEST_ALIGNMENT);
10668 /* Don't forget about volatility even if this is a bitfield. */
10669 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10671 if (op0 == orig_op0)
10672 op0 = copy_rtx (op0);
10674 MEM_VOLATILE_P (op0) = 1;
10677 /* In cases where an aligned union has an unaligned object
10678 as a field, we might be extracting a BLKmode value from
10679 an integer-mode (e.g., SImode) object. Handle this case
10680 by doing the extract into an object as wide as the field
10681 (which we know to be the width of a basic mode), then
10682 storing into memory, and changing the mode to BLKmode. */
10683 if (mode1 == VOIDmode
10684 || REG_P (op0) || GET_CODE (op0) == SUBREG
10685 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10686 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10687 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10688 && modifier != EXPAND_CONST_ADDRESS
10689 && modifier != EXPAND_INITIALIZER
10690 && modifier != EXPAND_MEMORY)
10691 /* If the bitfield is volatile and the bitsize
10692 is narrower than the access size of the bitfield,
10693 we need to extract bitfields from the access. */
10694 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10695 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10696 && mode1 != BLKmode
10697 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
10698 /* If the field isn't aligned enough to fetch as a memref,
10699 fetch it as a bit field. */
10700 || (mode1 != BLKmode
10701 && (((MEM_P (op0)
10702 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10703 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
10704 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10705 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
10706 && modifier != EXPAND_MEMORY
10707 && ((modifier == EXPAND_CONST_ADDRESS
10708 || modifier == EXPAND_INITIALIZER)
10709 ? STRICT_ALIGNMENT
10710 : targetm.slow_unaligned_access (mode1,
10711 MEM_ALIGN (op0))))
10712 || !multiple_p (bitpos, BITS_PER_UNIT)))
10713 /* If the type and the field are a constant size and the
10714 size of the type isn't the same size as the bitfield,
10715 we must use bitfield operations. */
10716 || (known_size_p (bitsize)
10717 && TYPE_SIZE (TREE_TYPE (exp))
10718 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
10719 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
10720 bitsize)))
10722 machine_mode ext_mode = mode;
10724 if (ext_mode == BLKmode
10725 && ! (target != 0 && MEM_P (op0)
10726 && MEM_P (target)
10727 && multiple_p (bitpos, BITS_PER_UNIT)))
10728 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10730 if (ext_mode == BLKmode)
10732 if (target == 0)
10733 target = assign_temp (type, 1, 1);
10735 /* ??? Unlike the similar test a few lines below, this one is
10736 very likely obsolete. */
10737 if (known_eq (bitsize, 0))
10738 return target;
10740 /* In this case, BITPOS must start at a byte boundary and
10741 TARGET, if specified, must be a MEM. */
10742 gcc_assert (MEM_P (op0)
10743 && (!target || MEM_P (target)));
10745 bytepos = exact_div (bitpos, BITS_PER_UNIT);
10746 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
10747 emit_block_move (target,
10748 adjust_address (op0, VOIDmode, bytepos),
10749 gen_int_mode (bytesize, Pmode),
10750 (modifier == EXPAND_STACK_PARM
10751 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10753 return target;
10756 /* If we have nothing to extract, the result will be 0 for targets
10757 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10758 return 0 for the sake of consistency, as reading a zero-sized
10759 bitfield is valid in Ada and the value is fully specified. */
10760 if (known_eq (bitsize, 0))
10761 return const0_rtx;
10763 op0 = validize_mem (op0);
10765 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10766 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10768 /* If the result has a record type and the extraction is done in
10769 an integral mode, then the field may be not aligned on a byte
10770 boundary; in this case, if it has reverse storage order, it
10771 needs to be extracted as a scalar field with reverse storage
10772 order and put back into memory order afterwards. */
10773 if (TREE_CODE (type) == RECORD_TYPE
10774 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10775 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10777 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10778 (modifier == EXPAND_STACK_PARM
10779 ? NULL_RTX : target),
10780 ext_mode, ext_mode, reversep, alt_rtl);
10782 /* If the result has a record type and the mode of OP0 is an
10783 integral mode then, if BITSIZE is narrower than this mode
10784 and this is for big-endian data, we must put the field
10785 into the high-order bits. And we must also put it back
10786 into memory order if it has been previously reversed. */
10787 scalar_int_mode op0_mode;
10788 if (TREE_CODE (type) == RECORD_TYPE
10789 && is_int_mode (GET_MODE (op0), &op0_mode))
10791 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10793 gcc_checking_assert (known_le (bitsize, size));
10794 if (maybe_lt (bitsize, size)
10795 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10796 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10797 size - bitsize, op0, 1);
10799 if (reversep)
10800 op0 = flip_storage_order (op0_mode, op0);
10803 /* If the result type is BLKmode, store the data into a temporary
10804 of the appropriate type, but with the mode corresponding to the
10805 mode for the data we have (op0's mode). */
10806 if (mode == BLKmode)
10808 rtx new_rtx
10809 = assign_stack_temp_for_type (ext_mode,
10810 GET_MODE_BITSIZE (ext_mode),
10811 type);
10812 emit_move_insn (new_rtx, op0);
10813 op0 = copy_rtx (new_rtx);
10814 PUT_MODE (op0, BLKmode);
10817 return op0;
10820 /* If the result is BLKmode, use that to access the object
10821 now as well. */
10822 if (mode == BLKmode)
10823 mode1 = BLKmode;
10825 /* Get a reference to just this component. */
10826 bytepos = bits_to_bytes_round_down (bitpos);
10827 if (modifier == EXPAND_CONST_ADDRESS
10828 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10829 op0 = adjust_address_nv (op0, mode1, bytepos);
10830 else
10831 op0 = adjust_address (op0, mode1, bytepos);
10833 if (op0 == orig_op0)
10834 op0 = copy_rtx (op0);
10836 /* Don't set memory attributes if the base expression is
10837 SSA_NAME that got expanded as a MEM. In that case, we should
10838 just honor its original memory attributes. */
10839 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10840 set_mem_attributes (op0, exp, 0);
10842 if (REG_P (XEXP (op0, 0)))
10843 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10845 /* If op0 is a temporary because the original expressions was forced
10846 to memory, clear MEM_EXPR so that the original expression cannot
10847 be marked as addressable through MEM_EXPR of the temporary. */
10848 if (clear_mem_expr)
10849 set_mem_expr (op0, NULL_TREE);
10851 MEM_VOLATILE_P (op0) |= volatilep;
10853 if (reversep
10854 && modifier != EXPAND_MEMORY
10855 && modifier != EXPAND_WRITE)
10856 op0 = flip_storage_order (mode1, op0);
10858 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10859 || modifier == EXPAND_CONST_ADDRESS
10860 || modifier == EXPAND_INITIALIZER)
10861 return op0;
10863 if (target == 0)
10864 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10866 convert_move (target, op0, unsignedp);
10867 return target;
10870 case OBJ_TYPE_REF:
10871 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10873 case CALL_EXPR:
10874 /* All valid uses of __builtin_va_arg_pack () are removed during
10875 inlining. */
10876 if (CALL_EXPR_VA_ARG_PACK (exp))
10877 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10879 tree fndecl = get_callee_fndecl (exp), attr;
10881 if (fndecl
10882 /* Don't diagnose the error attribute in thunks, those are
10883 artificially created. */
10884 && !CALL_FROM_THUNK_P (exp)
10885 && (attr = lookup_attribute ("error",
10886 DECL_ATTRIBUTES (fndecl))) != NULL)
10888 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
10889 error ("%Kcall to %qs declared with attribute error: %s", exp,
10890 identifier_to_locale (ident),
10891 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10893 if (fndecl
10894 /* Don't diagnose the warning attribute in thunks, those are
10895 artificially created. */
10896 && !CALL_FROM_THUNK_P (exp)
10897 && (attr = lookup_attribute ("warning",
10898 DECL_ATTRIBUTES (fndecl))) != NULL)
10900 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
10901 warning_at (tree_nonartificial_location (exp), 0,
10902 "%Kcall to %qs declared with attribute warning: %s",
10903 exp, identifier_to_locale (ident),
10904 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10907 /* Check for a built-in function. */
10908 if (fndecl && DECL_BUILT_IN (fndecl))
10910 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10911 return expand_builtin (exp, target, subtarget, tmode, ignore);
10914 return expand_call (exp, target, ignore);
10916 case VIEW_CONVERT_EXPR:
10917 op0 = NULL_RTX;
10919 /* If we are converting to BLKmode, try to avoid an intermediate
10920 temporary by fetching an inner memory reference. */
10921 if (mode == BLKmode
10922 && poly_int_tree_p (TYPE_SIZE (type))
10923 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10924 && handled_component_p (treeop0))
10926 machine_mode mode1;
10927 poly_int64 bitsize, bitpos, bytepos;
10928 tree offset;
10929 int unsignedp, reversep, volatilep = 0;
10930 tree tem
10931 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10932 &unsignedp, &reversep, &volatilep);
10933 rtx orig_op0;
10935 /* ??? We should work harder and deal with non-zero offsets. */
10936 if (!offset
10937 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10938 && !reversep
10939 && known_size_p (bitsize)
10940 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
10942 /* See the normal_inner_ref case for the rationale. */
10943 orig_op0
10944 = expand_expr_real (tem,
10945 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10946 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10947 != INTEGER_CST)
10948 && modifier != EXPAND_STACK_PARM
10949 ? target : NULL_RTX),
10950 VOIDmode,
10951 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10952 NULL, true);
10954 if (MEM_P (orig_op0))
10956 op0 = orig_op0;
10958 /* Get a reference to just this component. */
10959 if (modifier == EXPAND_CONST_ADDRESS
10960 || modifier == EXPAND_SUM
10961 || modifier == EXPAND_INITIALIZER)
10962 op0 = adjust_address_nv (op0, mode, bytepos);
10963 else
10964 op0 = adjust_address (op0, mode, bytepos);
10966 if (op0 == orig_op0)
10967 op0 = copy_rtx (op0);
10969 set_mem_attributes (op0, treeop0, 0);
10970 if (REG_P (XEXP (op0, 0)))
10971 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10973 MEM_VOLATILE_P (op0) |= volatilep;
10978 if (!op0)
10979 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10980 NULL, inner_reference_p);
10982 /* If the input and output modes are both the same, we are done. */
10983 if (mode == GET_MODE (op0))
10985 /* If neither mode is BLKmode, and both modes are the same size
10986 then we can use gen_lowpart. */
10987 else if (mode != BLKmode
10988 && GET_MODE (op0) != BLKmode
10989 && known_eq (GET_MODE_PRECISION (mode),
10990 GET_MODE_PRECISION (GET_MODE (op0)))
10991 && !COMPLEX_MODE_P (GET_MODE (op0)))
10993 if (GET_CODE (op0) == SUBREG)
10994 op0 = force_reg (GET_MODE (op0), op0);
10995 temp = gen_lowpart_common (mode, op0);
10996 if (temp)
10997 op0 = temp;
10998 else
11000 if (!REG_P (op0) && !MEM_P (op0))
11001 op0 = force_reg (GET_MODE (op0), op0);
11002 op0 = gen_lowpart (mode, op0);
11005 /* If both types are integral, convert from one mode to the other. */
11006 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11007 op0 = convert_modes (mode, GET_MODE (op0), op0,
11008 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11009 /* If the output type is a bit-field type, do an extraction. */
11010 else if (reduce_bit_field)
11011 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11012 TYPE_UNSIGNED (type), NULL_RTX,
11013 mode, mode, false, NULL);
11014 /* As a last resort, spill op0 to memory, and reload it in a
11015 different mode. */
11016 else if (!MEM_P (op0))
11018 /* If the operand is not a MEM, force it into memory. Since we
11019 are going to be changing the mode of the MEM, don't call
11020 force_const_mem for constants because we don't allow pool
11021 constants to change mode. */
11022 tree inner_type = TREE_TYPE (treeop0);
11024 gcc_assert (!TREE_ADDRESSABLE (exp));
11026 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11027 target
11028 = assign_stack_temp_for_type
11029 (TYPE_MODE (inner_type),
11030 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11032 emit_move_insn (target, op0);
11033 op0 = target;
11036 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11037 output type is such that the operand is known to be aligned, indicate
11038 that it is. Otherwise, we need only be concerned about alignment for
11039 non-BLKmode results. */
11040 if (MEM_P (op0))
11042 enum insn_code icode;
11044 if (modifier != EXPAND_WRITE
11045 && modifier != EXPAND_MEMORY
11046 && !inner_reference_p
11047 && mode != BLKmode
11048 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11050 /* If the target does have special handling for unaligned
11051 loads of mode then use them. */
11052 if ((icode = optab_handler (movmisalign_optab, mode))
11053 != CODE_FOR_nothing)
11055 rtx reg;
11057 op0 = adjust_address (op0, mode, 0);
11058 /* We've already validated the memory, and we're creating a
11059 new pseudo destination. The predicates really can't
11060 fail. */
11061 reg = gen_reg_rtx (mode);
11063 /* Nor can the insn generator. */
11064 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11065 emit_insn (insn);
11066 return reg;
11068 else if (STRICT_ALIGNMENT)
11070 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11071 poly_uint64 temp_size = mode_size;
11072 if (GET_MODE (op0) != BLKmode)
11073 temp_size = upper_bound (temp_size,
11074 GET_MODE_SIZE (GET_MODE (op0)));
11075 rtx new_rtx
11076 = assign_stack_temp_for_type (mode, temp_size, type);
11077 rtx new_with_op0_mode
11078 = adjust_address (new_rtx, GET_MODE (op0), 0);
11080 gcc_assert (!TREE_ADDRESSABLE (exp));
11082 if (GET_MODE (op0) == BLKmode)
11084 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11085 emit_block_move (new_with_op0_mode, op0, size_rtx,
11086 (modifier == EXPAND_STACK_PARM
11087 ? BLOCK_OP_CALL_PARM
11088 : BLOCK_OP_NORMAL));
11090 else
11091 emit_move_insn (new_with_op0_mode, op0);
11093 op0 = new_rtx;
11097 op0 = adjust_address (op0, mode, 0);
11100 return op0;
11102 case MODIFY_EXPR:
11104 tree lhs = treeop0;
11105 tree rhs = treeop1;
11106 gcc_assert (ignore);
11108 /* Check for |= or &= of a bitfield of size one into another bitfield
11109 of size 1. In this case, (unless we need the result of the
11110 assignment) we can do this more efficiently with a
11111 test followed by an assignment, if necessary.
11113 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11114 things change so we do, this code should be enhanced to
11115 support it. */
11116 if (TREE_CODE (lhs) == COMPONENT_REF
11117 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11118 || TREE_CODE (rhs) == BIT_AND_EXPR)
11119 && TREE_OPERAND (rhs, 0) == lhs
11120 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11121 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11122 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11124 rtx_code_label *label = gen_label_rtx ();
11125 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11126 do_jump (TREE_OPERAND (rhs, 1),
11127 value ? label : 0,
11128 value ? 0 : label,
11129 profile_probability::uninitialized ());
11130 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11131 false);
11132 do_pending_stack_adjust ();
11133 emit_label (label);
11134 return const0_rtx;
11137 expand_assignment (lhs, rhs, false);
11138 return const0_rtx;
11141 case ADDR_EXPR:
11142 return expand_expr_addr_expr (exp, target, tmode, modifier);
11144 case REALPART_EXPR:
11145 op0 = expand_normal (treeop0);
11146 return read_complex_part (op0, false);
11148 case IMAGPART_EXPR:
11149 op0 = expand_normal (treeop0);
11150 return read_complex_part (op0, true);
11152 case RETURN_EXPR:
11153 case LABEL_EXPR:
11154 case GOTO_EXPR:
11155 case SWITCH_EXPR:
11156 case ASM_EXPR:
11157 /* Expanded in cfgexpand.c. */
11158 gcc_unreachable ();
11160 case TRY_CATCH_EXPR:
11161 case CATCH_EXPR:
11162 case EH_FILTER_EXPR:
11163 case TRY_FINALLY_EXPR:
11164 /* Lowered by tree-eh.c. */
11165 gcc_unreachable ();
11167 case WITH_CLEANUP_EXPR:
11168 case CLEANUP_POINT_EXPR:
11169 case TARGET_EXPR:
11170 case CASE_LABEL_EXPR:
11171 case VA_ARG_EXPR:
11172 case BIND_EXPR:
11173 case INIT_EXPR:
11174 case CONJ_EXPR:
11175 case COMPOUND_EXPR:
11176 case PREINCREMENT_EXPR:
11177 case PREDECREMENT_EXPR:
11178 case POSTINCREMENT_EXPR:
11179 case POSTDECREMENT_EXPR:
11180 case LOOP_EXPR:
11181 case EXIT_EXPR:
11182 case COMPOUND_LITERAL_EXPR:
11183 /* Lowered by gimplify.c. */
11184 gcc_unreachable ();
11186 case FDESC_EXPR:
11187 /* Function descriptors are not valid except for as
11188 initialization constants, and should not be expanded. */
11189 gcc_unreachable ();
11191 case WITH_SIZE_EXPR:
11192 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11193 have pulled out the size to use in whatever context it needed. */
11194 return expand_expr_real (treeop0, original_target, tmode,
11195 modifier, alt_rtl, inner_reference_p);
11197 default:
11198 return expand_expr_real_2 (&ops, target, tmode, modifier);
11202 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11203 signedness of TYPE), possibly returning the result in TARGET.
11204 TYPE is known to be a partial integer type. */
11205 static rtx
11206 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11208 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11209 if (target && GET_MODE (target) != GET_MODE (exp))
11210 target = 0;
11211 /* For constant values, reduce using build_int_cst_type. */
11212 poly_int64 const_exp;
11213 if (poly_int_rtx_p (exp, &const_exp))
11215 tree t = build_int_cst_type (type, const_exp);
11216 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11218 else if (TYPE_UNSIGNED (type))
11220 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11221 rtx mask = immed_wide_int_const
11222 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11223 return expand_and (mode, exp, mask, target);
11225 else
11227 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11228 int count = GET_MODE_PRECISION (mode) - prec;
11229 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11230 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11234 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11235 when applied to the address of EXP produces an address known to be
11236 aligned more than BIGGEST_ALIGNMENT. */
11238 static int
11239 is_aligning_offset (const_tree offset, const_tree exp)
11241 /* Strip off any conversions. */
11242 while (CONVERT_EXPR_P (offset))
11243 offset = TREE_OPERAND (offset, 0);
11245 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11246 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11247 if (TREE_CODE (offset) != BIT_AND_EXPR
11248 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11249 || compare_tree_int (TREE_OPERAND (offset, 1),
11250 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11251 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11252 return 0;
11254 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11255 It must be NEGATE_EXPR. Then strip any more conversions. */
11256 offset = TREE_OPERAND (offset, 0);
11257 while (CONVERT_EXPR_P (offset))
11258 offset = TREE_OPERAND (offset, 0);
11260 if (TREE_CODE (offset) != NEGATE_EXPR)
11261 return 0;
11263 offset = TREE_OPERAND (offset, 0);
11264 while (CONVERT_EXPR_P (offset))
11265 offset = TREE_OPERAND (offset, 0);
11267 /* This must now be the address of EXP. */
11268 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11271 /* Return the tree node if an ARG corresponds to a string constant or zero
11272 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
11273 non-constant) offset in bytes within the string that ARG is accessing.
11274 The type of the offset is sizetype. */
11276 tree
11277 string_constant (tree arg, tree *ptr_offset)
11279 tree array;
11280 STRIP_NOPS (arg);
11282 /* Non-constant index into the character array in an ARRAY_REF
11283 expression or null. */
11284 tree varidx = NULL_TREE;
11286 poly_int64 base_off = 0;
11288 if (TREE_CODE (arg) == ADDR_EXPR)
11290 arg = TREE_OPERAND (arg, 0);
11291 tree ref = arg;
11292 if (TREE_CODE (arg) == ARRAY_REF)
11294 tree idx = TREE_OPERAND (arg, 1);
11295 if (TREE_CODE (idx) != INTEGER_CST)
11297 /* Extract the variable index to prevent
11298 get_addr_base_and_unit_offset() from failing due to
11299 it. Use it later to compute the non-constant offset
11300 into the string and return it to the caller. */
11301 varidx = idx;
11302 ref = TREE_OPERAND (arg, 0);
11305 array = get_addr_base_and_unit_offset (ref, &base_off);
11306 if (!array
11307 || (TREE_CODE (array) != VAR_DECL
11308 && TREE_CODE (array) != CONST_DECL
11309 && TREE_CODE (array) != STRING_CST))
11310 return NULL_TREE;
11312 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11314 tree arg0 = TREE_OPERAND (arg, 0);
11315 tree arg1 = TREE_OPERAND (arg, 1);
11317 STRIP_NOPS (arg0);
11318 STRIP_NOPS (arg1);
11320 if (TREE_CODE (arg0) == ADDR_EXPR)
11321 ; /* Do nothing. */
11322 else if (TREE_CODE (arg1) == ADDR_EXPR)
11323 std::swap (arg0, arg1);
11324 else
11325 return NULL_TREE;
11327 tree offset;
11328 if (tree str = string_constant (arg0, &offset))
11330 tree type = TREE_TYPE (arg1);
11331 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
11332 return str;
11334 return NULL_TREE;
11336 else if (DECL_P (arg))
11337 array = arg;
11338 else
11339 return NULL_TREE;
11341 tree offset = wide_int_to_tree (sizetype, base_off);
11342 if (varidx)
11344 if (tree eltsize = TYPE_SIZE_UNIT (TREE_TYPE (array)))
11346 /* Add the scaled variable index to the constant offset. */
11347 tree eltoff = fold_build2 (MULT_EXPR, TREE_TYPE (offset),
11348 fold_convert (sizetype, varidx),
11349 eltsize);
11350 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, eltoff);
11352 else
11353 return NULL_TREE;
11356 if (TREE_CODE (array) == STRING_CST)
11358 *ptr_offset = fold_convert (sizetype, offset);
11359 return array;
11362 if (!VAR_P (array) && TREE_CODE (array) != CONST_DECL)
11363 return NULL_TREE;
11365 tree init = ctor_for_folding (array);
11367 /* Handle variables initialized with string literals. */
11368 if (!init || init == error_mark_node)
11369 return NULL_TREE;
11370 if (TREE_CODE (init) == CONSTRUCTOR)
11372 if (TREE_CODE (arg) != ARRAY_REF
11373 && TREE_CODE (arg) == COMPONENT_REF
11374 && TREE_CODE (arg) == MEM_REF)
11375 return NULL_TREE;
11377 /* Convert the 64-bit constant offset to a wider type to avoid
11378 overflow. */
11379 offset_int wioff;
11380 if (!base_off.is_constant (&wioff))
11381 return NULL_TREE;
11383 wioff *= BITS_PER_UNIT;
11384 if (!wi::fits_uhwi_p (wioff))
11385 return NULL_TREE;
11387 base_off = wioff.to_uhwi ();
11388 unsigned HOST_WIDE_INT fieldoff = 0;
11389 init = fold_ctor_reference (NULL_TREE, init, base_off, 0, array,
11390 &fieldoff);
11391 HOST_WIDE_INT cstoff;
11392 if (init && base_off.is_constant (&cstoff))
11394 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
11395 offset = build_int_cst (sizetype, cstoff);
11399 if (!init || TREE_CODE (init) != STRING_CST)
11400 return NULL_TREE;
11402 tree array_size = DECL_SIZE_UNIT (array);
11403 if (!array_size || TREE_CODE (array_size) != INTEGER_CST)
11404 return NULL_TREE;
11406 /* Avoid returning a string that doesn't fit in the array
11407 it is stored in, like
11408 const char a[4] = "abcde";
11409 but do handle those that fit even if they have excess
11410 initializers, such as in
11411 const char a[4] = "abc\000\000";
11412 The excess elements contribute to TREE_STRING_LENGTH()
11413 but not to strlen(). */
11414 unsigned HOST_WIDE_INT length
11415 = strnlen (TREE_STRING_POINTER (init), TREE_STRING_LENGTH (init));
11416 if (compare_tree_int (array_size, length + 1) < 0)
11417 return NULL_TREE;
11419 *ptr_offset = offset;
11420 return init;
11423 /* Generate code to calculate OPS, and exploded expression
11424 using a store-flag instruction and return an rtx for the result.
11425 OPS reflects a comparison.
11427 If TARGET is nonzero, store the result there if convenient.
11429 Return zero if there is no suitable set-flag instruction
11430 available on this machine.
11432 Once expand_expr has been called on the arguments of the comparison,
11433 we are committed to doing the store flag, since it is not safe to
11434 re-evaluate the expression. We emit the store-flag insn by calling
11435 emit_store_flag, but only expand the arguments if we have a reason
11436 to believe that emit_store_flag will be successful. If we think that
11437 it will, but it isn't, we have to simulate the store-flag with a
11438 set/jump/set sequence. */
11440 static rtx
11441 do_store_flag (sepops ops, rtx target, machine_mode mode)
11443 enum rtx_code code;
11444 tree arg0, arg1, type;
11445 machine_mode operand_mode;
11446 int unsignedp;
11447 rtx op0, op1;
11448 rtx subtarget = target;
11449 location_t loc = ops->location;
11451 arg0 = ops->op0;
11452 arg1 = ops->op1;
11454 /* Don't crash if the comparison was erroneous. */
11455 if (arg0 == error_mark_node || arg1 == error_mark_node)
11456 return const0_rtx;
11458 type = TREE_TYPE (arg0);
11459 operand_mode = TYPE_MODE (type);
11460 unsignedp = TYPE_UNSIGNED (type);
11462 /* We won't bother with BLKmode store-flag operations because it would mean
11463 passing a lot of information to emit_store_flag. */
11464 if (operand_mode == BLKmode)
11465 return 0;
11467 /* We won't bother with store-flag operations involving function pointers
11468 when function pointers must be canonicalized before comparisons. */
11469 if (targetm.have_canonicalize_funcptr_for_compare ()
11470 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11471 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11472 == FUNCTION_TYPE))
11473 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11474 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11475 == FUNCTION_TYPE))))
11476 return 0;
11478 STRIP_NOPS (arg0);
11479 STRIP_NOPS (arg1);
11481 /* For vector typed comparisons emit code to generate the desired
11482 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11483 expander for this. */
11484 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11486 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11487 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11488 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11489 return expand_vec_cmp_expr (ops->type, ifexp, target);
11490 else
11492 tree if_true = constant_boolean_node (true, ops->type);
11493 tree if_false = constant_boolean_node (false, ops->type);
11494 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11495 if_false, target);
11499 /* Get the rtx comparison code to use. We know that EXP is a comparison
11500 operation of some type. Some comparisons against 1 and -1 can be
11501 converted to comparisons with zero. Do so here so that the tests
11502 below will be aware that we have a comparison with zero. These
11503 tests will not catch constants in the first operand, but constants
11504 are rarely passed as the first operand. */
11506 switch (ops->code)
11508 case EQ_EXPR:
11509 code = EQ;
11510 break;
11511 case NE_EXPR:
11512 code = NE;
11513 break;
11514 case LT_EXPR:
11515 if (integer_onep (arg1))
11516 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11517 else
11518 code = unsignedp ? LTU : LT;
11519 break;
11520 case LE_EXPR:
11521 if (! unsignedp && integer_all_onesp (arg1))
11522 arg1 = integer_zero_node, code = LT;
11523 else
11524 code = unsignedp ? LEU : LE;
11525 break;
11526 case GT_EXPR:
11527 if (! unsignedp && integer_all_onesp (arg1))
11528 arg1 = integer_zero_node, code = GE;
11529 else
11530 code = unsignedp ? GTU : GT;
11531 break;
11532 case GE_EXPR:
11533 if (integer_onep (arg1))
11534 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11535 else
11536 code = unsignedp ? GEU : GE;
11537 break;
11539 case UNORDERED_EXPR:
11540 code = UNORDERED;
11541 break;
11542 case ORDERED_EXPR:
11543 code = ORDERED;
11544 break;
11545 case UNLT_EXPR:
11546 code = UNLT;
11547 break;
11548 case UNLE_EXPR:
11549 code = UNLE;
11550 break;
11551 case UNGT_EXPR:
11552 code = UNGT;
11553 break;
11554 case UNGE_EXPR:
11555 code = UNGE;
11556 break;
11557 case UNEQ_EXPR:
11558 code = UNEQ;
11559 break;
11560 case LTGT_EXPR:
11561 code = LTGT;
11562 break;
11564 default:
11565 gcc_unreachable ();
11568 /* Put a constant second. */
11569 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11570 || TREE_CODE (arg0) == FIXED_CST)
11572 std::swap (arg0, arg1);
11573 code = swap_condition (code);
11576 /* If this is an equality or inequality test of a single bit, we can
11577 do this by shifting the bit being tested to the low-order bit and
11578 masking the result with the constant 1. If the condition was EQ,
11579 we xor it with 1. This does not require an scc insn and is faster
11580 than an scc insn even if we have it.
11582 The code to make this transformation was moved into fold_single_bit_test,
11583 so we just call into the folder and expand its result. */
11585 if ((code == NE || code == EQ)
11586 && integer_zerop (arg1)
11587 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11589 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11590 if (srcstmt
11591 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11593 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11594 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11595 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11596 gimple_assign_rhs1 (srcstmt),
11597 gimple_assign_rhs2 (srcstmt));
11598 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11599 if (temp)
11600 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11604 if (! get_subtarget (target)
11605 || GET_MODE (subtarget) != operand_mode)
11606 subtarget = 0;
11608 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11610 if (target == 0)
11611 target = gen_reg_rtx (mode);
11613 /* Try a cstore if possible. */
11614 return emit_store_flag_force (target, code, op0, op1,
11615 operand_mode, unsignedp,
11616 (TYPE_PRECISION (ops->type) == 1
11617 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11620 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11621 0 otherwise (i.e. if there is no casesi instruction).
11623 DEFAULT_PROBABILITY is the probability of jumping to the default
11624 label. */
11626 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11627 rtx table_label, rtx default_label, rtx fallback_label,
11628 profile_probability default_probability)
11630 struct expand_operand ops[5];
11631 scalar_int_mode index_mode = SImode;
11632 rtx op1, op2, index;
11634 if (! targetm.have_casesi ())
11635 return 0;
11637 /* The index must be some form of integer. Convert it to SImode. */
11638 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11639 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11641 rtx rangertx = expand_normal (range);
11643 /* We must handle the endpoints in the original mode. */
11644 index_expr = build2 (MINUS_EXPR, index_type,
11645 index_expr, minval);
11646 minval = integer_zero_node;
11647 index = expand_normal (index_expr);
11648 if (default_label)
11649 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11650 omode, 1, default_label,
11651 default_probability);
11652 /* Now we can safely truncate. */
11653 index = convert_to_mode (index_mode, index, 0);
11655 else
11657 if (omode != index_mode)
11659 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11660 index_expr = fold_convert (index_type, index_expr);
11663 index = expand_normal (index_expr);
11666 do_pending_stack_adjust ();
11668 op1 = expand_normal (minval);
11669 op2 = expand_normal (range);
11671 create_input_operand (&ops[0], index, index_mode);
11672 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11673 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11674 create_fixed_operand (&ops[3], table_label);
11675 create_fixed_operand (&ops[4], (default_label
11676 ? default_label
11677 : fallback_label));
11678 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11679 return 1;
11682 /* Attempt to generate a tablejump instruction; same concept. */
11683 /* Subroutine of the next function.
11685 INDEX is the value being switched on, with the lowest value
11686 in the table already subtracted.
11687 MODE is its expected mode (needed if INDEX is constant).
11688 RANGE is the length of the jump table.
11689 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11691 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11692 index value is out of range.
11693 DEFAULT_PROBABILITY is the probability of jumping to
11694 the default label. */
11696 static void
11697 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11698 rtx default_label, profile_probability default_probability)
11700 rtx temp, vector;
11702 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11703 cfun->cfg->max_jumptable_ents = INTVAL (range);
11705 /* Do an unsigned comparison (in the proper mode) between the index
11706 expression and the value which represents the length of the range.
11707 Since we just finished subtracting the lower bound of the range
11708 from the index expression, this comparison allows us to simultaneously
11709 check that the original index expression value is both greater than
11710 or equal to the minimum value of the range and less than or equal to
11711 the maximum value of the range. */
11713 if (default_label)
11714 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11715 default_label, default_probability);
11717 /* If index is in range, it must fit in Pmode.
11718 Convert to Pmode so we can index with it. */
11719 if (mode != Pmode)
11721 unsigned int width;
11723 /* We know the value of INDEX is between 0 and RANGE. If we have a
11724 sign-extended subreg, and RANGE does not have the sign bit set, then
11725 we have a value that is valid for both sign and zero extension. In
11726 this case, we get better code if we sign extend. */
11727 if (GET_CODE (index) == SUBREG
11728 && SUBREG_PROMOTED_VAR_P (index)
11729 && SUBREG_PROMOTED_SIGNED_P (index)
11730 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
11731 <= HOST_BITS_PER_WIDE_INT)
11732 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
11733 index = convert_to_mode (Pmode, index, 0);
11734 else
11735 index = convert_to_mode (Pmode, index, 1);
11738 /* Don't let a MEM slip through, because then INDEX that comes
11739 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11740 and break_out_memory_refs will go to work on it and mess it up. */
11741 #ifdef PIC_CASE_VECTOR_ADDRESS
11742 if (flag_pic && !REG_P (index))
11743 index = copy_to_mode_reg (Pmode, index);
11744 #endif
11746 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11747 GET_MODE_SIZE, because this indicates how large insns are. The other
11748 uses should all be Pmode, because they are addresses. This code
11749 could fail if addresses and insns are not the same size. */
11750 index = simplify_gen_binary (MULT, Pmode, index,
11751 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11752 Pmode));
11753 index = simplify_gen_binary (PLUS, Pmode, index,
11754 gen_rtx_LABEL_REF (Pmode, table_label));
11756 #ifdef PIC_CASE_VECTOR_ADDRESS
11757 if (flag_pic)
11758 index = PIC_CASE_VECTOR_ADDRESS (index);
11759 else
11760 #endif
11761 index = memory_address (CASE_VECTOR_MODE, index);
11762 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11763 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11764 convert_move (temp, vector, 0);
11766 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11768 /* If we are generating PIC code or if the table is PC-relative, the
11769 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11770 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11771 emit_barrier ();
11775 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11776 rtx table_label, rtx default_label,
11777 profile_probability default_probability)
11779 rtx index;
11781 if (! targetm.have_tablejump ())
11782 return 0;
11784 index_expr = fold_build2 (MINUS_EXPR, index_type,
11785 fold_convert (index_type, index_expr),
11786 fold_convert (index_type, minval));
11787 index = expand_normal (index_expr);
11788 do_pending_stack_adjust ();
11790 do_tablejump (index, TYPE_MODE (index_type),
11791 convert_modes (TYPE_MODE (index_type),
11792 TYPE_MODE (TREE_TYPE (range)),
11793 expand_normal (range),
11794 TYPE_UNSIGNED (TREE_TYPE (range))),
11795 table_label, default_label, default_probability);
11796 return 1;
11799 /* Return a CONST_VECTOR rtx representing vector mask for
11800 a VECTOR_CST of booleans. */
11801 static rtx
11802 const_vector_mask_from_tree (tree exp)
11804 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11805 machine_mode inner = GET_MODE_INNER (mode);
11807 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11808 VECTOR_CST_NELTS_PER_PATTERN (exp));
11809 unsigned int count = builder.encoded_nelts ();
11810 for (unsigned int i = 0; i < count; ++i)
11812 tree elt = VECTOR_CST_ELT (exp, i);
11813 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11814 if (integer_zerop (elt))
11815 builder.quick_push (CONST0_RTX (inner));
11816 else if (integer_onep (elt)
11817 || integer_minus_onep (elt))
11818 builder.quick_push (CONSTM1_RTX (inner));
11819 else
11820 gcc_unreachable ();
11822 return builder.build ();
11825 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11826 Return a constant scalar rtx of mode MODE in which bit X is set if element
11827 X of EXP is nonzero. */
11828 static rtx
11829 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11831 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11832 tree elt;
11834 /* The result has a fixed number of bits so the input must too. */
11835 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11836 for (unsigned int i = 0; i < nunits; ++i)
11838 elt = VECTOR_CST_ELT (exp, i);
11839 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11840 if (integer_all_onesp (elt))
11841 res = wi::set_bit (res, i);
11842 else
11843 gcc_assert (integer_zerop (elt));
11846 return immed_wide_int_const (res, mode);
11849 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11850 static rtx
11851 const_vector_from_tree (tree exp)
11853 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11855 if (initializer_zerop (exp))
11856 return CONST0_RTX (mode);
11858 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11859 return const_vector_mask_from_tree (exp);
11861 machine_mode inner = GET_MODE_INNER (mode);
11863 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11864 VECTOR_CST_NELTS_PER_PATTERN (exp));
11865 unsigned int count = builder.encoded_nelts ();
11866 for (unsigned int i = 0; i < count; ++i)
11868 tree elt = VECTOR_CST_ELT (exp, i);
11869 if (TREE_CODE (elt) == REAL_CST)
11870 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
11871 inner));
11872 else if (TREE_CODE (elt) == FIXED_CST)
11873 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11874 inner));
11875 else
11876 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
11877 inner));
11879 return builder.build ();
11882 /* Build a decl for a personality function given a language prefix. */
11884 tree
11885 build_personality_function (const char *lang)
11887 const char *unwind_and_version;
11888 tree decl, type;
11889 char *name;
11891 switch (targetm_common.except_unwind_info (&global_options))
11893 case UI_NONE:
11894 return NULL;
11895 case UI_SJLJ:
11896 unwind_and_version = "_sj0";
11897 break;
11898 case UI_DWARF2:
11899 case UI_TARGET:
11900 unwind_and_version = "_v0";
11901 break;
11902 case UI_SEH:
11903 unwind_and_version = "_seh0";
11904 break;
11905 default:
11906 gcc_unreachable ();
11909 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11911 type = build_function_type_list (integer_type_node, integer_type_node,
11912 long_long_unsigned_type_node,
11913 ptr_type_node, ptr_type_node, NULL_TREE);
11914 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11915 get_identifier (name), type);
11916 DECL_ARTIFICIAL (decl) = 1;
11917 DECL_EXTERNAL (decl) = 1;
11918 TREE_PUBLIC (decl) = 1;
11920 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11921 are the flags assigned by targetm.encode_section_info. */
11922 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11924 return decl;
11927 /* Extracts the personality function of DECL and returns the corresponding
11928 libfunc. */
11931 get_personality_function (tree decl)
11933 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11934 enum eh_personality_kind pk;
11936 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11937 if (pk == eh_personality_none)
11938 return NULL;
11940 if (!personality
11941 && pk == eh_personality_any)
11942 personality = lang_hooks.eh_personality ();
11944 if (pk == eh_personality_lang)
11945 gcc_assert (personality != NULL_TREE);
11947 return XEXP (DECL_RTL (personality), 0);
11950 /* Returns a tree for the size of EXP in bytes. */
11952 static tree
11953 tree_expr_size (const_tree exp)
11955 if (DECL_P (exp)
11956 && DECL_SIZE_UNIT (exp) != 0)
11957 return DECL_SIZE_UNIT (exp);
11958 else
11959 return size_in_bytes (TREE_TYPE (exp));
11962 /* Return an rtx for the size in bytes of the value of EXP. */
11965 expr_size (tree exp)
11967 tree size;
11969 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11970 size = TREE_OPERAND (exp, 1);
11971 else
11973 size = tree_expr_size (exp);
11974 gcc_assert (size);
11975 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11978 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11981 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11982 if the size can vary or is larger than an integer. */
11984 static HOST_WIDE_INT
11985 int_expr_size (tree exp)
11987 tree size;
11989 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11990 size = TREE_OPERAND (exp, 1);
11991 else
11993 size = tree_expr_size (exp);
11994 gcc_assert (size);
11997 if (size == 0 || !tree_fits_shwi_p (size))
11998 return -1;
12000 return tree_to_shwi (size);