* gcc-interface/trans.c (process_freeze_entity): Be prepared for a
[official-gcc.git] / gcc / expr.c
blob80116381f1b7d075d0619e4fc7821db133730a7d
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 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-ssa-live.h"
58 #include "tree-outof-ssa.h"
59 #include "tree-ssa-address.h"
60 #include "builtins.h"
61 #include "tree-chkp.h"
62 #include "rtl-chkp.h"
63 #include "ccmp.h"
66 /* If this is nonzero, we do not bother generating VOLATILE
67 around volatile memory references, and we are willing to
68 output indirect addresses. If cse is to follow, we reject
69 indirect addresses so a useful potential cse is generated;
70 if it is used only once, instruction combination will produce
71 the same indirect address eventually. */
72 int cse_not_expected;
74 static bool block_move_libcall_safe_for_call_parm (void);
75 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
76 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT);
78 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
79 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
80 static rtx_insn *compress_float_constant (rtx, rtx);
81 static rtx get_subtarget (rtx);
82 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
83 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
84 unsigned HOST_WIDE_INT, machine_mode,
85 tree, int, alias_set_type, bool);
86 static void store_constructor (tree, rtx, int, HOST_WIDE_INT, bool);
87 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
88 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
89 machine_mode, tree, alias_set_type, bool, bool);
91 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
93 static int is_aligning_offset (const_tree, const_tree);
94 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
95 static rtx do_store_flag (sepops, rtx, machine_mode);
96 #ifdef PUSH_ROUNDING
97 static void emit_single_push_insn (machine_mode, rtx, tree);
98 #endif
99 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
100 profile_probability);
101 static rtx const_vector_from_tree (tree);
102 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
103 static tree tree_expr_size (const_tree);
104 static HOST_WIDE_INT int_expr_size (tree);
105 static void convert_mode_scalar (rtx, rtx, int);
108 /* This is run to set up which modes can be used
109 directly in memory and to initialize the block move optab. It is run
110 at the beginning of compilation and when the target is reinitialized. */
112 void
113 init_expr_target (void)
115 rtx pat;
116 int num_clobbers;
117 rtx mem, mem1;
118 rtx reg;
120 /* Try indexing by frame ptr and try by stack ptr.
121 It is known that on the Convex the stack ptr isn't a valid index.
122 With luck, one or the other is valid on any machine. */
123 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
124 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
126 /* A scratch register we can modify in-place below to avoid
127 useless RTL allocations. */
128 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
130 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
131 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
132 PATTERN (insn) = pat;
134 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
135 mode = (machine_mode) ((int) mode + 1))
137 int regno;
139 direct_load[(int) mode] = direct_store[(int) mode] = 0;
140 PUT_MODE (mem, mode);
141 PUT_MODE (mem1, mode);
143 /* See if there is some register that can be used in this mode and
144 directly loaded or stored from memory. */
146 if (mode != VOIDmode && mode != BLKmode)
147 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
148 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
149 regno++)
151 if (!targetm.hard_regno_mode_ok (regno, mode))
152 continue;
154 set_mode_and_regno (reg, mode, regno);
156 SET_SRC (pat) = mem;
157 SET_DEST (pat) = reg;
158 if (recog (pat, insn, &num_clobbers) >= 0)
159 direct_load[(int) mode] = 1;
161 SET_SRC (pat) = mem1;
162 SET_DEST (pat) = reg;
163 if (recog (pat, insn, &num_clobbers) >= 0)
164 direct_load[(int) mode] = 1;
166 SET_SRC (pat) = reg;
167 SET_DEST (pat) = mem;
168 if (recog (pat, insn, &num_clobbers) >= 0)
169 direct_store[(int) mode] = 1;
171 SET_SRC (pat) = reg;
172 SET_DEST (pat) = mem1;
173 if (recog (pat, insn, &num_clobbers) >= 0)
174 direct_store[(int) mode] = 1;
178 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
180 opt_scalar_float_mode mode_iter;
181 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
183 scalar_float_mode mode = mode_iter.require ();
184 scalar_float_mode srcmode;
185 FOR_EACH_MODE_UNTIL (srcmode, mode)
187 enum insn_code ic;
189 ic = can_extend_p (mode, srcmode, 0);
190 if (ic == CODE_FOR_nothing)
191 continue;
193 PUT_MODE (mem, srcmode);
195 if (insn_operand_matches (ic, 1, mem))
196 float_extend_from_mem[mode][srcmode] = true;
201 /* This is run at the start of compiling a function. */
203 void
204 init_expr (void)
206 memset (&crtl->expr, 0, sizeof (crtl->expr));
209 /* Copy data from FROM to TO, where the machine modes are not the same.
210 Both modes may be integer, or both may be floating, or both may be
211 fixed-point.
212 UNSIGNEDP should be nonzero if FROM is an unsigned type.
213 This causes zero-extension instead of sign-extension. */
215 void
216 convert_move (rtx to, rtx from, int unsignedp)
218 machine_mode to_mode = GET_MODE (to);
219 machine_mode from_mode = GET_MODE (from);
221 gcc_assert (to_mode != BLKmode);
222 gcc_assert (from_mode != BLKmode);
224 /* If the source and destination are already the same, then there's
225 nothing to do. */
226 if (to == from)
227 return;
229 /* If FROM is a SUBREG that indicates that we have already done at least
230 the required extension, strip it. We don't handle such SUBREGs as
231 TO here. */
233 scalar_int_mode to_int_mode;
234 if (GET_CODE (from) == SUBREG
235 && SUBREG_PROMOTED_VAR_P (from)
236 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
237 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
238 >= GET_MODE_PRECISION (to_int_mode))
239 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
240 from = gen_lowpart (to_int_mode, from), from_mode = to_int_mode;
242 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
244 if (to_mode == from_mode
245 || (from_mode == VOIDmode && CONSTANT_P (from)))
247 emit_move_insn (to, from);
248 return;
251 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
253 gcc_assert (GET_MODE_BITSIZE (from_mode) == 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 || (REG_P (x)
696 && (!HARD_REGISTER_P (x)
697 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
698 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
699 return gen_lowpart (int_mode, x);
701 /* Converting from integer constant into mode is always equivalent to an
702 subreg operation. */
703 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
705 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
706 return simplify_gen_subreg (mode, x, oldmode, 0);
709 temp = gen_reg_rtx (mode);
710 convert_move (temp, x, unsignedp);
711 return temp;
714 /* Return the largest alignment we can use for doing a move (or store)
715 of MAX_PIECES. ALIGN is the largest alignment we could use. */
717 static unsigned int
718 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
720 scalar_int_mode tmode
721 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
723 if (align >= GET_MODE_ALIGNMENT (tmode))
724 align = GET_MODE_ALIGNMENT (tmode);
725 else
727 scalar_int_mode xmode = NARROWEST_INT_MODE;
728 opt_scalar_int_mode mode_iter;
729 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
731 tmode = mode_iter.require ();
732 if (GET_MODE_SIZE (tmode) > max_pieces
733 || targetm.slow_unaligned_access (tmode, align))
734 break;
735 xmode = tmode;
738 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
741 return align;
744 /* Return the widest integer mode that is narrower than SIZE bytes. */
746 static scalar_int_mode
747 widest_int_mode_for_size (unsigned int size)
749 scalar_int_mode result = NARROWEST_INT_MODE;
751 gcc_checking_assert (size > 1);
753 opt_scalar_int_mode tmode;
754 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
755 if (GET_MODE_SIZE (tmode.require ()) < size)
756 result = tmode.require ();
758 return result;
761 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
762 and should be performed piecewise. */
764 static bool
765 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
766 enum by_pieces_operation op)
768 return targetm.use_by_pieces_infrastructure_p (len, align, op,
769 optimize_insn_for_speed_p ());
772 /* Determine whether the LEN bytes can be moved by using several move
773 instructions. Return nonzero if a call to move_by_pieces should
774 succeed. */
776 bool
777 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
779 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
782 /* Return number of insns required to perform operation OP by pieces
783 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
785 unsigned HOST_WIDE_INT
786 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
787 unsigned int max_size, by_pieces_operation op)
789 unsigned HOST_WIDE_INT n_insns = 0;
791 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
793 while (max_size > 1 && l > 0)
795 scalar_int_mode mode = widest_int_mode_for_size (max_size);
796 enum insn_code icode;
798 unsigned int modesize = GET_MODE_SIZE (mode);
800 icode = optab_handler (mov_optab, mode);
801 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
803 unsigned HOST_WIDE_INT n_pieces = l / modesize;
804 l %= modesize;
805 switch (op)
807 default:
808 n_insns += n_pieces;
809 break;
811 case COMPARE_BY_PIECES:
812 int batch = targetm.compare_by_pieces_branch_ratio (mode);
813 int batch_ops = 4 * batch - 1;
814 unsigned HOST_WIDE_INT full = n_pieces / batch;
815 n_insns += full * batch_ops;
816 if (n_pieces % batch != 0)
817 n_insns++;
818 break;
822 max_size = modesize;
825 gcc_assert (!l);
826 return n_insns;
829 /* Used when performing piecewise block operations, holds information
830 about one of the memory objects involved. The member functions
831 can be used to generate code for loading from the object and
832 updating the address when iterating. */
834 class pieces_addr
836 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
837 stack pushes. */
838 rtx m_obj;
839 /* The address of the object. Can differ from that seen in the
840 MEM rtx if we copied the address to a register. */
841 rtx m_addr;
842 /* Nonzero if the address on the object has an autoincrement already,
843 signifies whether that was an increment or decrement. */
844 signed char m_addr_inc;
845 /* Nonzero if we intend to use autoinc without the address already
846 having autoinc form. We will insert add insns around each memory
847 reference, expecting later passes to form autoinc addressing modes.
848 The only supported options are predecrement and postincrement. */
849 signed char m_explicit_inc;
850 /* True if we have either of the two possible cases of using
851 autoincrement. */
852 bool m_auto;
853 /* True if this is an address to be used for load operations rather
854 than stores. */
855 bool m_is_load;
857 /* Optionally, a function to obtain constants for any given offset into
858 the objects, and data associated with it. */
859 by_pieces_constfn m_constfn;
860 void *m_cfndata;
861 public:
862 pieces_addr (rtx, bool, by_pieces_constfn, void *);
863 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
864 void increment_address (HOST_WIDE_INT);
865 void maybe_predec (HOST_WIDE_INT);
866 void maybe_postinc (HOST_WIDE_INT);
867 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
868 int get_addr_inc ()
870 return m_addr_inc;
874 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
875 true if the operation to be performed on this object is a load
876 rather than a store. For stores, OBJ can be NULL, in which case we
877 assume the operation is a stack push. For loads, the optional
878 CONSTFN and its associated CFNDATA can be used in place of the
879 memory load. */
881 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
882 void *cfndata)
883 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
885 m_addr_inc = 0;
886 m_auto = false;
887 if (obj)
889 rtx addr = XEXP (obj, 0);
890 rtx_code code = GET_CODE (addr);
891 m_addr = addr;
892 bool dec = code == PRE_DEC || code == POST_DEC;
893 bool inc = code == PRE_INC || code == POST_INC;
894 m_auto = inc || dec;
895 if (m_auto)
896 m_addr_inc = dec ? -1 : 1;
898 /* While we have always looked for these codes here, the code
899 implementing the memory operation has never handled them.
900 Support could be added later if necessary or beneficial. */
901 gcc_assert (code != PRE_INC && code != POST_DEC);
903 else
905 m_addr = NULL_RTX;
906 if (!is_load)
908 m_auto = true;
909 if (STACK_GROWS_DOWNWARD)
910 m_addr_inc = -1;
911 else
912 m_addr_inc = 1;
914 else
915 gcc_assert (constfn != NULL);
917 m_explicit_inc = 0;
918 if (constfn)
919 gcc_assert (is_load);
922 /* Decide whether to use autoinc for an address involved in a memory op.
923 MODE is the mode of the accesses, REVERSE is true if we've decided to
924 perform the operation starting from the end, and LEN is the length of
925 the operation. Don't override an earlier decision to set m_auto. */
927 void
928 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
929 HOST_WIDE_INT len)
931 if (m_auto || m_obj == NULL_RTX)
932 return;
934 bool use_predec = (m_is_load
935 ? USE_LOAD_PRE_DECREMENT (mode)
936 : USE_STORE_PRE_DECREMENT (mode));
937 bool use_postinc = (m_is_load
938 ? USE_LOAD_POST_INCREMENT (mode)
939 : USE_STORE_POST_INCREMENT (mode));
940 machine_mode addr_mode = get_address_mode (m_obj);
942 if (use_predec && reverse)
944 m_addr = copy_to_mode_reg (addr_mode,
945 plus_constant (addr_mode,
946 m_addr, len));
947 m_auto = true;
948 m_explicit_inc = -1;
950 else if (use_postinc && !reverse)
952 m_addr = copy_to_mode_reg (addr_mode, m_addr);
953 m_auto = true;
954 m_explicit_inc = 1;
956 else if (CONSTANT_P (m_addr))
957 m_addr = copy_to_mode_reg (addr_mode, m_addr);
960 /* Adjust the address to refer to the data at OFFSET in MODE. If we
961 are using autoincrement for this address, we don't add the offset,
962 but we still modify the MEM's properties. */
965 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
967 if (m_constfn)
968 return m_constfn (m_cfndata, offset, mode);
969 if (m_obj == NULL_RTX)
970 return NULL_RTX;
971 if (m_auto)
972 return adjust_automodify_address (m_obj, mode, m_addr, offset);
973 else
974 return adjust_address (m_obj, mode, offset);
977 /* Emit an add instruction to increment the address by SIZE. */
979 void
980 pieces_addr::increment_address (HOST_WIDE_INT size)
982 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
983 emit_insn (gen_add2_insn (m_addr, amount));
986 /* If we are supposed to decrement the address after each access, emit code
987 to do so now. Increment by SIZE (which has should have the correct sign
988 already). */
990 void
991 pieces_addr::maybe_predec (HOST_WIDE_INT size)
993 if (m_explicit_inc >= 0)
994 return;
995 gcc_assert (HAVE_PRE_DECREMENT);
996 increment_address (size);
999 /* If we are supposed to decrement the address after each access, emit code
1000 to do so now. Increment by SIZE. */
1002 void
1003 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1005 if (m_explicit_inc <= 0)
1006 return;
1007 gcc_assert (HAVE_POST_INCREMENT);
1008 increment_address (size);
1011 /* This structure is used by do_op_by_pieces to describe the operation
1012 to be performed. */
1014 class op_by_pieces_d
1016 protected:
1017 pieces_addr m_to, m_from;
1018 unsigned HOST_WIDE_INT m_len;
1019 HOST_WIDE_INT m_offset;
1020 unsigned int m_align;
1021 unsigned int m_max_size;
1022 bool m_reverse;
1024 /* Virtual functions, overriden by derived classes for the specific
1025 operation. */
1026 virtual void generate (rtx, rtx, machine_mode) = 0;
1027 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1028 virtual void finish_mode (machine_mode)
1032 public:
1033 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1034 unsigned HOST_WIDE_INT, unsigned int);
1035 void run ();
1038 /* The constructor for an op_by_pieces_d structure. We require two
1039 objects named TO and FROM, which are identified as loads or stores
1040 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1041 and its associated FROM_CFN_DATA can be used to replace loads with
1042 constant values. LEN describes the length of the operation. */
1044 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1045 rtx from, bool from_load,
1046 by_pieces_constfn from_cfn,
1047 void *from_cfn_data,
1048 unsigned HOST_WIDE_INT len,
1049 unsigned int align)
1050 : m_to (to, to_load, NULL, NULL),
1051 m_from (from, from_load, from_cfn, from_cfn_data),
1052 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1054 int toi = m_to.get_addr_inc ();
1055 int fromi = m_from.get_addr_inc ();
1056 if (toi >= 0 && fromi >= 0)
1057 m_reverse = false;
1058 else if (toi <= 0 && fromi <= 0)
1059 m_reverse = true;
1060 else
1061 gcc_unreachable ();
1063 m_offset = m_reverse ? len : 0;
1064 align = MIN (to ? MEM_ALIGN (to) : align,
1065 from ? MEM_ALIGN (from) : align);
1067 /* If copying requires more than two move insns,
1068 copy addresses to registers (to make displacements shorter)
1069 and use post-increment if available. */
1070 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1072 /* Find the mode of the largest comparison. */
1073 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1075 m_from.decide_autoinc (mode, m_reverse, len);
1076 m_to.decide_autoinc (mode, m_reverse, len);
1079 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1080 m_align = align;
1083 /* This function contains the main loop used for expanding a block
1084 operation. First move what we can in the largest integer mode,
1085 then go to successively smaller modes. For every access, call
1086 GENFUN with the two operands and the EXTRA_DATA. */
1088 void
1089 op_by_pieces_d::run ()
1091 while (m_max_size > 1 && m_len > 0)
1093 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1095 if (prepare_mode (mode, m_align))
1097 unsigned int size = GET_MODE_SIZE (mode);
1098 rtx to1 = NULL_RTX, from1;
1100 while (m_len >= size)
1102 if (m_reverse)
1103 m_offset -= size;
1105 to1 = m_to.adjust (mode, m_offset);
1106 from1 = m_from.adjust (mode, m_offset);
1108 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1109 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1111 generate (to1, from1, mode);
1113 m_to.maybe_postinc (size);
1114 m_from.maybe_postinc (size);
1116 if (!m_reverse)
1117 m_offset += size;
1119 m_len -= size;
1122 finish_mode (mode);
1125 m_max_size = GET_MODE_SIZE (mode);
1128 /* The code above should have handled everything. */
1129 gcc_assert (!m_len);
1132 /* Derived class from op_by_pieces_d, providing support for block move
1133 operations. */
1135 class move_by_pieces_d : public op_by_pieces_d
1137 insn_gen_fn m_gen_fun;
1138 void generate (rtx, rtx, machine_mode);
1139 bool prepare_mode (machine_mode, unsigned int);
1141 public:
1142 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1143 unsigned int align)
1144 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1147 rtx finish_endp (int);
1150 /* Return true if MODE can be used for a set of copies, given an
1151 alignment ALIGN. Prepare whatever data is necessary for later
1152 calls to generate. */
1154 bool
1155 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1157 insn_code icode = optab_handler (mov_optab, mode);
1158 m_gen_fun = GEN_FCN (icode);
1159 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1162 /* A callback used when iterating for a compare_by_pieces_operation.
1163 OP0 and OP1 are the values that have been loaded and should be
1164 compared in MODE. If OP0 is NULL, this means we should generate a
1165 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1166 gen function that should be used to generate the mode. */
1168 void
1169 move_by_pieces_d::generate (rtx op0, rtx op1,
1170 machine_mode mode ATTRIBUTE_UNUSED)
1172 #ifdef PUSH_ROUNDING
1173 if (op0 == NULL_RTX)
1175 emit_single_push_insn (mode, op1, NULL);
1176 return;
1178 #endif
1179 emit_insn (m_gen_fun (op0, op1));
1182 /* Perform the final adjustment at the end of a string to obtain the
1183 correct return value for the block operation. If ENDP is 1 return
1184 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1185 end minus one byte ala stpcpy. */
1188 move_by_pieces_d::finish_endp (int endp)
1190 gcc_assert (!m_reverse);
1191 if (endp == 2)
1193 m_to.maybe_postinc (-1);
1194 --m_offset;
1196 return m_to.adjust (QImode, m_offset);
1199 /* Generate several move instructions to copy LEN bytes from block FROM to
1200 block TO. (These are MEM rtx's with BLKmode).
1202 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1203 used to push FROM to the stack.
1205 ALIGN is maximum stack alignment we can assume.
1207 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1208 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1209 stpcpy. */
1212 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1213 unsigned int align, int endp)
1215 #ifndef PUSH_ROUNDING
1216 if (to == NULL)
1217 gcc_unreachable ();
1218 #endif
1220 move_by_pieces_d data (to, from, len, align);
1222 data.run ();
1224 if (endp)
1225 return data.finish_endp (endp);
1226 else
1227 return to;
1230 /* Derived class from op_by_pieces_d, providing support for block move
1231 operations. */
1233 class store_by_pieces_d : public op_by_pieces_d
1235 insn_gen_fn m_gen_fun;
1236 void generate (rtx, rtx, machine_mode);
1237 bool prepare_mode (machine_mode, unsigned int);
1239 public:
1240 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1241 unsigned HOST_WIDE_INT len, unsigned int align)
1242 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1245 rtx finish_endp (int);
1248 /* Return true if MODE can be used for a set of stores, given an
1249 alignment ALIGN. Prepare whatever data is necessary for later
1250 calls to generate. */
1252 bool
1253 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1255 insn_code icode = optab_handler (mov_optab, mode);
1256 m_gen_fun = GEN_FCN (icode);
1257 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1260 /* A callback used when iterating for a store_by_pieces_operation.
1261 OP0 and OP1 are the values that have been loaded and should be
1262 compared in MODE. If OP0 is NULL, this means we should generate a
1263 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1264 gen function that should be used to generate the mode. */
1266 void
1267 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1269 emit_insn (m_gen_fun (op0, op1));
1272 /* Perform the final adjustment at the end of a string to obtain the
1273 correct return value for the block operation. If ENDP is 1 return
1274 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1275 end minus one byte ala stpcpy. */
1278 store_by_pieces_d::finish_endp (int endp)
1280 gcc_assert (!m_reverse);
1281 if (endp == 2)
1283 m_to.maybe_postinc (-1);
1284 --m_offset;
1286 return m_to.adjust (QImode, m_offset);
1289 /* Determine whether the LEN bytes generated by CONSTFUN can be
1290 stored to memory using several move instructions. CONSTFUNDATA is
1291 a pointer which will be passed as argument in every CONSTFUN call.
1292 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1293 a memset operation and false if it's a copy of a constant string.
1294 Return nonzero if a call to store_by_pieces should succeed. */
1297 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1298 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1299 void *constfundata, unsigned int align, bool memsetp)
1301 unsigned HOST_WIDE_INT l;
1302 unsigned int max_size;
1303 HOST_WIDE_INT offset = 0;
1304 enum insn_code icode;
1305 int reverse;
1306 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1307 rtx cst ATTRIBUTE_UNUSED;
1309 if (len == 0)
1310 return 1;
1312 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1313 memsetp
1314 ? SET_BY_PIECES
1315 : STORE_BY_PIECES,
1316 optimize_insn_for_speed_p ()))
1317 return 0;
1319 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1321 /* We would first store what we can in the largest integer mode, then go to
1322 successively smaller modes. */
1324 for (reverse = 0;
1325 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1326 reverse++)
1328 l = len;
1329 max_size = STORE_MAX_PIECES + 1;
1330 while (max_size > 1 && l > 0)
1332 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1334 icode = optab_handler (mov_optab, mode);
1335 if (icode != CODE_FOR_nothing
1336 && align >= GET_MODE_ALIGNMENT (mode))
1338 unsigned int size = GET_MODE_SIZE (mode);
1340 while (l >= size)
1342 if (reverse)
1343 offset -= size;
1345 cst = (*constfun) (constfundata, offset, mode);
1346 if (!targetm.legitimate_constant_p (mode, cst))
1347 return 0;
1349 if (!reverse)
1350 offset += size;
1352 l -= size;
1356 max_size = GET_MODE_SIZE (mode);
1359 /* The code above should have handled everything. */
1360 gcc_assert (!l);
1363 return 1;
1366 /* Generate several move instructions to store LEN bytes generated by
1367 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1368 pointer which will be passed as argument in every CONSTFUN call.
1369 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1370 a memset operation and false if it's a copy of a constant string.
1371 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1372 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1373 stpcpy. */
1376 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1377 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1378 void *constfundata, unsigned int align, bool memsetp, int endp)
1380 if (len == 0)
1382 gcc_assert (endp != 2);
1383 return to;
1386 gcc_assert (targetm.use_by_pieces_infrastructure_p
1387 (len, align,
1388 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1389 optimize_insn_for_speed_p ()));
1391 store_by_pieces_d data (to, constfun, constfundata, len, align);
1392 data.run ();
1394 if (endp)
1395 return data.finish_endp (endp);
1396 else
1397 return to;
1400 /* Callback routine for clear_by_pieces.
1401 Return const0_rtx unconditionally. */
1403 static rtx
1404 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1406 return const0_rtx;
1409 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1410 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1412 static void
1413 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1415 if (len == 0)
1416 return;
1418 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1419 data.run ();
1422 /* Context used by compare_by_pieces_genfn. It stores the fail label
1423 to jump to in case of miscomparison, and for branch ratios greater than 1,
1424 it stores an accumulator and the current and maximum counts before
1425 emitting another branch. */
1427 class compare_by_pieces_d : public op_by_pieces_d
1429 rtx_code_label *m_fail_label;
1430 rtx m_accumulator;
1431 int m_count, m_batch;
1433 void generate (rtx, rtx, machine_mode);
1434 bool prepare_mode (machine_mode, unsigned int);
1435 void finish_mode (machine_mode);
1436 public:
1437 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1438 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1439 rtx_code_label *fail_label)
1440 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1442 m_fail_label = fail_label;
1446 /* A callback used when iterating for a compare_by_pieces_operation.
1447 OP0 and OP1 are the values that have been loaded and should be
1448 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1449 context structure. */
1451 void
1452 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1454 if (m_batch > 1)
1456 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1457 true, OPTAB_LIB_WIDEN);
1458 if (m_count != 0)
1459 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1460 true, OPTAB_LIB_WIDEN);
1461 m_accumulator = temp;
1463 if (++m_count < m_batch)
1464 return;
1466 m_count = 0;
1467 op0 = m_accumulator;
1468 op1 = const0_rtx;
1469 m_accumulator = NULL_RTX;
1471 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1472 m_fail_label, profile_probability::uninitialized ());
1475 /* Return true if MODE can be used for a set of moves and comparisons,
1476 given an alignment ALIGN. Prepare whatever data is necessary for
1477 later calls to generate. */
1479 bool
1480 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1482 insn_code icode = optab_handler (mov_optab, mode);
1483 if (icode == CODE_FOR_nothing
1484 || align < GET_MODE_ALIGNMENT (mode)
1485 || !can_compare_p (EQ, mode, ccp_jump))
1486 return false;
1487 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1488 if (m_batch < 0)
1489 return false;
1490 m_accumulator = NULL_RTX;
1491 m_count = 0;
1492 return true;
1495 /* Called after expanding a series of comparisons in MODE. If we have
1496 accumulated results for which we haven't emitted a branch yet, do
1497 so now. */
1499 void
1500 compare_by_pieces_d::finish_mode (machine_mode mode)
1502 if (m_accumulator != NULL_RTX)
1503 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1504 NULL_RTX, NULL, m_fail_label,
1505 profile_probability::uninitialized ());
1508 /* Generate several move instructions to compare LEN bytes from blocks
1509 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1511 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1512 used to push FROM to the stack.
1514 ALIGN is maximum stack alignment we can assume.
1516 Optionally, the caller can pass a constfn and associated data in A1_CFN
1517 and A1_CFN_DATA. describing that the second operand being compared is a
1518 known constant and how to obtain its data. */
1520 static rtx
1521 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1522 rtx target, unsigned int align,
1523 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1525 rtx_code_label *fail_label = gen_label_rtx ();
1526 rtx_code_label *end_label = gen_label_rtx ();
1528 if (target == NULL_RTX
1529 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1530 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1532 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1533 fail_label);
1535 data.run ();
1537 emit_move_insn (target, const0_rtx);
1538 emit_jump (end_label);
1539 emit_barrier ();
1540 emit_label (fail_label);
1541 emit_move_insn (target, const1_rtx);
1542 emit_label (end_label);
1544 return target;
1547 /* Emit code to move a block Y to a block X. This may be done with
1548 string-move instructions, with multiple scalar move instructions,
1549 or with a library call.
1551 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1552 SIZE is an rtx that says how long they are.
1553 ALIGN is the maximum alignment we can assume they have.
1554 METHOD describes what kind of copy this is, and what mechanisms may be used.
1555 MIN_SIZE is the minimal size of block to move
1556 MAX_SIZE is the maximal size of block to move, if it can not be represented
1557 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1559 Return the address of the new block, if memcpy is called and returns it,
1560 0 otherwise. */
1563 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1564 unsigned int expected_align, HOST_WIDE_INT expected_size,
1565 unsigned HOST_WIDE_INT min_size,
1566 unsigned HOST_WIDE_INT max_size,
1567 unsigned HOST_WIDE_INT probable_max_size)
1569 bool may_use_call;
1570 rtx retval = 0;
1571 unsigned int align;
1573 gcc_assert (size);
1574 if (CONST_INT_P (size) && INTVAL (size) == 0)
1575 return 0;
1577 switch (method)
1579 case BLOCK_OP_NORMAL:
1580 case BLOCK_OP_TAILCALL:
1581 may_use_call = true;
1582 break;
1584 case BLOCK_OP_CALL_PARM:
1585 may_use_call = block_move_libcall_safe_for_call_parm ();
1587 /* Make inhibit_defer_pop nonzero around the library call
1588 to force it to pop the arguments right away. */
1589 NO_DEFER_POP;
1590 break;
1592 case BLOCK_OP_NO_LIBCALL:
1593 may_use_call = false;
1594 break;
1596 default:
1597 gcc_unreachable ();
1600 gcc_assert (MEM_P (x) && MEM_P (y));
1601 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1602 gcc_assert (align >= BITS_PER_UNIT);
1604 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1605 block copy is more efficient for other large modes, e.g. DCmode. */
1606 x = adjust_address (x, BLKmode, 0);
1607 y = adjust_address (y, BLKmode, 0);
1609 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1610 can be incorrect is coming from __builtin_memcpy. */
1611 if (CONST_INT_P (size))
1613 x = shallow_copy_rtx (x);
1614 y = shallow_copy_rtx (y);
1615 set_mem_size (x, INTVAL (size));
1616 set_mem_size (y, INTVAL (size));
1619 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1620 move_by_pieces (x, y, INTVAL (size), align, 0);
1621 else if (emit_block_move_via_movmem (x, y, size, align,
1622 expected_align, expected_size,
1623 min_size, max_size, probable_max_size))
1625 else if (may_use_call
1626 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1627 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1629 /* Since x and y are passed to a libcall, mark the corresponding
1630 tree EXPR as addressable. */
1631 tree y_expr = MEM_EXPR (y);
1632 tree x_expr = MEM_EXPR (x);
1633 if (y_expr)
1634 mark_addressable (y_expr);
1635 if (x_expr)
1636 mark_addressable (x_expr);
1637 retval = emit_block_copy_via_libcall (x, y, size,
1638 method == BLOCK_OP_TAILCALL);
1641 else
1642 emit_block_move_via_loop (x, y, size, align);
1644 if (method == BLOCK_OP_CALL_PARM)
1645 OK_DEFER_POP;
1647 return retval;
1651 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1653 unsigned HOST_WIDE_INT max, min = 0;
1654 if (GET_CODE (size) == CONST_INT)
1655 min = max = UINTVAL (size);
1656 else
1657 max = GET_MODE_MASK (GET_MODE (size));
1658 return emit_block_move_hints (x, y, size, method, 0, -1,
1659 min, max, max);
1662 /* A subroutine of emit_block_move. Returns true if calling the
1663 block move libcall will not clobber any parameters which may have
1664 already been placed on the stack. */
1666 static bool
1667 block_move_libcall_safe_for_call_parm (void)
1669 #if defined (REG_PARM_STACK_SPACE)
1670 tree fn;
1671 #endif
1673 /* If arguments are pushed on the stack, then they're safe. */
1674 if (PUSH_ARGS)
1675 return true;
1677 /* If registers go on the stack anyway, any argument is sure to clobber
1678 an outgoing argument. */
1679 #if defined (REG_PARM_STACK_SPACE)
1680 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1681 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1682 depend on its argument. */
1683 (void) fn;
1684 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1685 && REG_PARM_STACK_SPACE (fn) != 0)
1686 return false;
1687 #endif
1689 /* If any argument goes in memory, then it might clobber an outgoing
1690 argument. */
1692 CUMULATIVE_ARGS args_so_far_v;
1693 cumulative_args_t args_so_far;
1694 tree fn, arg;
1696 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1697 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1698 args_so_far = pack_cumulative_args (&args_so_far_v);
1700 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1701 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1703 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1704 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1705 NULL_TREE, true);
1706 if (!tmp || !REG_P (tmp))
1707 return false;
1708 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1709 return false;
1710 targetm.calls.function_arg_advance (args_so_far, mode,
1711 NULL_TREE, true);
1714 return true;
1717 /* A subroutine of emit_block_move. Expand a movmem pattern;
1718 return true if successful. */
1720 static bool
1721 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1722 unsigned int expected_align, HOST_WIDE_INT expected_size,
1723 unsigned HOST_WIDE_INT min_size,
1724 unsigned HOST_WIDE_INT max_size,
1725 unsigned HOST_WIDE_INT probable_max_size)
1727 int save_volatile_ok = volatile_ok;
1729 if (expected_align < align)
1730 expected_align = align;
1731 if (expected_size != -1)
1733 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1734 expected_size = probable_max_size;
1735 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1736 expected_size = min_size;
1739 /* Since this is a move insn, we don't care about volatility. */
1740 volatile_ok = 1;
1742 /* Try the most limited insn first, because there's no point
1743 including more than one in the machine description unless
1744 the more limited one has some advantage. */
1746 opt_scalar_int_mode mode_iter;
1747 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1749 scalar_int_mode mode = mode_iter.require ();
1750 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1752 if (code != CODE_FOR_nothing
1753 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1754 here because if SIZE is less than the mode mask, as it is
1755 returned by the macro, it will definitely be less than the
1756 actual mode mask. Since SIZE is within the Pmode address
1757 space, we limit MODE to Pmode. */
1758 && ((CONST_INT_P (size)
1759 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1760 <= (GET_MODE_MASK (mode) >> 1)))
1761 || max_size <= (GET_MODE_MASK (mode) >> 1)
1762 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1764 struct expand_operand ops[9];
1765 unsigned int nops;
1767 /* ??? When called via emit_block_move_for_call, it'd be
1768 nice if there were some way to inform the backend, so
1769 that it doesn't fail the expansion because it thinks
1770 emitting the libcall would be more efficient. */
1771 nops = insn_data[(int) code].n_generator_args;
1772 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1774 create_fixed_operand (&ops[0], x);
1775 create_fixed_operand (&ops[1], y);
1776 /* The check above guarantees that this size conversion is valid. */
1777 create_convert_operand_to (&ops[2], size, mode, true);
1778 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1779 if (nops >= 6)
1781 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1782 create_integer_operand (&ops[5], expected_size);
1784 if (nops >= 8)
1786 create_integer_operand (&ops[6], min_size);
1787 /* If we can not represent the maximal size,
1788 make parameter NULL. */
1789 if ((HOST_WIDE_INT) max_size != -1)
1790 create_integer_operand (&ops[7], max_size);
1791 else
1792 create_fixed_operand (&ops[7], NULL);
1794 if (nops == 9)
1796 /* If we can not represent the maximal size,
1797 make parameter NULL. */
1798 if ((HOST_WIDE_INT) probable_max_size != -1)
1799 create_integer_operand (&ops[8], probable_max_size);
1800 else
1801 create_fixed_operand (&ops[8], NULL);
1803 if (maybe_expand_insn (code, nops, ops))
1805 volatile_ok = save_volatile_ok;
1806 return true;
1811 volatile_ok = save_volatile_ok;
1812 return false;
1815 /* A subroutine of emit_block_move. Copy the data via an explicit
1816 loop. This is used only when libcalls are forbidden. */
1817 /* ??? It'd be nice to copy in hunks larger than QImode. */
1819 static void
1820 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1821 unsigned int align ATTRIBUTE_UNUSED)
1823 rtx_code_label *cmp_label, *top_label;
1824 rtx iter, x_addr, y_addr, tmp;
1825 machine_mode x_addr_mode = get_address_mode (x);
1826 machine_mode y_addr_mode = get_address_mode (y);
1827 machine_mode iter_mode;
1829 iter_mode = GET_MODE (size);
1830 if (iter_mode == VOIDmode)
1831 iter_mode = word_mode;
1833 top_label = gen_label_rtx ();
1834 cmp_label = gen_label_rtx ();
1835 iter = gen_reg_rtx (iter_mode);
1837 emit_move_insn (iter, const0_rtx);
1839 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1840 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1841 do_pending_stack_adjust ();
1843 emit_jump (cmp_label);
1844 emit_label (top_label);
1846 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1847 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1849 if (x_addr_mode != y_addr_mode)
1850 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1851 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1853 x = change_address (x, QImode, x_addr);
1854 y = change_address (y, QImode, y_addr);
1856 emit_move_insn (x, y);
1858 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1859 true, OPTAB_LIB_WIDEN);
1860 if (tmp != iter)
1861 emit_move_insn (iter, tmp);
1863 emit_label (cmp_label);
1865 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1866 true, top_label,
1867 profile_probability::guessed_always ()
1868 .apply_scale (9, 10));
1871 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1872 TAILCALL is true if this is a tail call. */
1875 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1876 rtx size, bool tailcall)
1878 rtx dst_addr, src_addr;
1879 tree call_expr, dst_tree, src_tree, size_tree;
1880 machine_mode size_mode;
1882 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1883 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1884 dst_tree = make_tree (ptr_type_node, dst_addr);
1886 src_addr = copy_addr_to_reg (XEXP (src, 0));
1887 src_addr = convert_memory_address (ptr_mode, src_addr);
1888 src_tree = make_tree (ptr_type_node, src_addr);
1890 size_mode = TYPE_MODE (sizetype);
1891 size = convert_to_mode (size_mode, size, 1);
1892 size = copy_to_mode_reg (size_mode, size);
1893 size_tree = make_tree (sizetype, size);
1895 /* It is incorrect to use the libcall calling conventions for calls to
1896 memcpy/memmove/memcmp because they can be provided by the user. */
1897 tree fn = builtin_decl_implicit (fncode);
1898 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1899 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1901 return expand_call (call_expr, NULL_RTX, false);
1904 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1905 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1906 otherwise return null. */
1909 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1910 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1911 HOST_WIDE_INT align)
1913 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1915 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1916 target = NULL_RTX;
1918 struct expand_operand ops[5];
1919 create_output_operand (&ops[0], target, insn_mode);
1920 create_fixed_operand (&ops[1], arg1_rtx);
1921 create_fixed_operand (&ops[2], arg2_rtx);
1922 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1923 TYPE_UNSIGNED (arg3_type));
1924 create_integer_operand (&ops[4], align);
1925 if (maybe_expand_insn (icode, 5, ops))
1926 return ops[0].value;
1927 return NULL_RTX;
1930 /* Expand a block compare between X and Y with length LEN using the
1931 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1932 of the expression that was used to calculate the length. ALIGN
1933 gives the known minimum common alignment. */
1935 static rtx
1936 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1937 unsigned align)
1939 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1940 implementing memcmp because it will stop if it encounters two
1941 zero bytes. */
1942 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1944 if (icode == CODE_FOR_nothing)
1945 return NULL_RTX;
1947 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1950 /* Emit code to compare a block Y to a block X. This may be done with
1951 string-compare instructions, with multiple scalar instructions,
1952 or with a library call.
1954 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1955 they are. LEN_TYPE is the type of the expression that was used to
1956 calculate it.
1958 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1959 value of a normal memcmp call, instead we can just compare for equality.
1960 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1961 returning NULL_RTX.
1963 Optionally, the caller can pass a constfn and associated data in Y_CFN
1964 and Y_CFN_DATA. describing that the second operand being compared is a
1965 known constant and how to obtain its data.
1966 Return the result of the comparison, or NULL_RTX if we failed to
1967 perform the operation. */
1970 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1971 bool equality_only, by_pieces_constfn y_cfn,
1972 void *y_cfndata)
1974 rtx result = 0;
1976 if (CONST_INT_P (len) && INTVAL (len) == 0)
1977 return const0_rtx;
1979 gcc_assert (MEM_P (x) && MEM_P (y));
1980 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1981 gcc_assert (align >= BITS_PER_UNIT);
1983 x = adjust_address (x, BLKmode, 0);
1984 y = adjust_address (y, BLKmode, 0);
1986 if (equality_only
1987 && CONST_INT_P (len)
1988 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1989 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1990 y_cfn, y_cfndata);
1991 else
1992 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1994 return result;
1997 /* Copy all or part of a value X into registers starting at REGNO.
1998 The number of registers to be filled is NREGS. */
2000 void
2001 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2003 if (nregs == 0)
2004 return;
2006 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2007 x = validize_mem (force_const_mem (mode, x));
2009 /* See if the machine can do this with a load multiple insn. */
2010 if (targetm.have_load_multiple ())
2012 rtx_insn *last = get_last_insn ();
2013 rtx first = gen_rtx_REG (word_mode, regno);
2014 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2015 GEN_INT (nregs)))
2017 emit_insn (pat);
2018 return;
2020 else
2021 delete_insns_since (last);
2024 for (int i = 0; i < nregs; i++)
2025 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2026 operand_subword_force (x, i, mode));
2029 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2030 The number of registers to be filled is NREGS. */
2032 void
2033 move_block_from_reg (int regno, rtx x, int nregs)
2035 if (nregs == 0)
2036 return;
2038 /* See if the machine can do this with a store multiple insn. */
2039 if (targetm.have_store_multiple ())
2041 rtx_insn *last = get_last_insn ();
2042 rtx first = gen_rtx_REG (word_mode, regno);
2043 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2044 GEN_INT (nregs)))
2046 emit_insn (pat);
2047 return;
2049 else
2050 delete_insns_since (last);
2053 for (int i = 0; i < nregs; i++)
2055 rtx tem = operand_subword (x, i, 1, BLKmode);
2057 gcc_assert (tem);
2059 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2063 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2064 ORIG, where ORIG is a non-consecutive group of registers represented by
2065 a PARALLEL. The clone is identical to the original except in that the
2066 original set of registers is replaced by a new set of pseudo registers.
2067 The new set has the same modes as the original set. */
2070 gen_group_rtx (rtx orig)
2072 int i, length;
2073 rtx *tmps;
2075 gcc_assert (GET_CODE (orig) == PARALLEL);
2077 length = XVECLEN (orig, 0);
2078 tmps = XALLOCAVEC (rtx, length);
2080 /* Skip a NULL entry in first slot. */
2081 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2083 if (i)
2084 tmps[0] = 0;
2086 for (; i < length; i++)
2088 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2089 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2091 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2094 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2097 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2098 except that values are placed in TMPS[i], and must later be moved
2099 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2101 static void
2102 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2104 rtx src;
2105 int start, i;
2106 machine_mode m = GET_MODE (orig_src);
2108 gcc_assert (GET_CODE (dst) == PARALLEL);
2110 if (m != VOIDmode
2111 && !SCALAR_INT_MODE_P (m)
2112 && !MEM_P (orig_src)
2113 && GET_CODE (orig_src) != CONCAT)
2115 scalar_int_mode imode;
2116 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2118 src = gen_reg_rtx (imode);
2119 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2121 else
2123 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2124 emit_move_insn (src, orig_src);
2126 emit_group_load_1 (tmps, dst, src, type, ssize);
2127 return;
2130 /* Check for a NULL entry, used to indicate that the parameter goes
2131 both on the stack and in registers. */
2132 if (XEXP (XVECEXP (dst, 0, 0), 0))
2133 start = 0;
2134 else
2135 start = 1;
2137 /* Process the pieces. */
2138 for (i = start; i < XVECLEN (dst, 0); i++)
2140 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2141 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2142 unsigned int bytelen = GET_MODE_SIZE (mode);
2143 int shift = 0;
2145 /* Handle trailing fragments that run over the size of the struct. */
2146 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2148 /* Arrange to shift the fragment to where it belongs.
2149 extract_bit_field loads to the lsb of the reg. */
2150 if (
2151 #ifdef BLOCK_REG_PADDING
2152 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2153 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2154 #else
2155 BYTES_BIG_ENDIAN
2156 #endif
2158 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2159 bytelen = ssize - bytepos;
2160 gcc_assert (bytelen > 0);
2163 /* If we won't be loading directly from memory, protect the real source
2164 from strange tricks we might play; but make sure that the source can
2165 be loaded directly into the destination. */
2166 src = orig_src;
2167 if (!MEM_P (orig_src)
2168 && (!CONSTANT_P (orig_src)
2169 || (GET_MODE (orig_src) != mode
2170 && GET_MODE (orig_src) != VOIDmode)))
2172 if (GET_MODE (orig_src) == VOIDmode)
2173 src = gen_reg_rtx (mode);
2174 else
2175 src = gen_reg_rtx (GET_MODE (orig_src));
2177 emit_move_insn (src, orig_src);
2180 /* Optimize the access just a bit. */
2181 if (MEM_P (src)
2182 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2183 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2184 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2185 && bytelen == GET_MODE_SIZE (mode))
2187 tmps[i] = gen_reg_rtx (mode);
2188 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2190 else if (COMPLEX_MODE_P (mode)
2191 && GET_MODE (src) == mode
2192 && bytelen == GET_MODE_SIZE (mode))
2193 /* Let emit_move_complex do the bulk of the work. */
2194 tmps[i] = src;
2195 else if (GET_CODE (src) == CONCAT)
2197 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2198 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2199 unsigned int elt = bytepos / slen0;
2200 unsigned int subpos = bytepos % slen0;
2202 if (subpos + bytelen <= slen0)
2204 /* The following assumes that the concatenated objects all
2205 have the same size. In this case, a simple calculation
2206 can be used to determine the object and the bit field
2207 to be extracted. */
2208 tmps[i] = XEXP (src, elt);
2209 if (subpos != 0
2210 || subpos + bytelen != slen0
2211 || (!CONSTANT_P (tmps[i])
2212 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2213 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2214 subpos * BITS_PER_UNIT,
2215 1, NULL_RTX, mode, mode, false,
2216 NULL);
2218 else
2220 rtx mem;
2222 gcc_assert (!bytepos);
2223 mem = assign_stack_temp (GET_MODE (src), slen);
2224 emit_move_insn (mem, src);
2225 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2226 0, 1, NULL_RTX, mode, mode, false,
2227 NULL);
2230 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2231 SIMD register, which is currently broken. While we get GCC
2232 to emit proper RTL for these cases, let's dump to memory. */
2233 else if (VECTOR_MODE_P (GET_MODE (dst))
2234 && REG_P (src))
2236 int slen = GET_MODE_SIZE (GET_MODE (src));
2237 rtx mem;
2239 mem = assign_stack_temp (GET_MODE (src), slen);
2240 emit_move_insn (mem, src);
2241 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2243 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2244 && XVECLEN (dst, 0) > 1)
2245 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2246 else if (CONSTANT_P (src))
2248 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2250 if (len == ssize)
2251 tmps[i] = src;
2252 else
2254 rtx first, second;
2256 /* TODO: const_wide_int can have sizes other than this... */
2257 gcc_assert (2 * len == ssize);
2258 split_double (src, &first, &second);
2259 if (i)
2260 tmps[i] = second;
2261 else
2262 tmps[i] = first;
2265 else if (REG_P (src) && GET_MODE (src) == mode)
2266 tmps[i] = src;
2267 else
2268 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2269 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2270 mode, mode, false, NULL);
2272 if (shift)
2273 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2274 shift, tmps[i], 0);
2278 /* Emit code to move a block SRC of type TYPE to a block DST,
2279 where DST is non-consecutive registers represented by a PARALLEL.
2280 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2281 if not known. */
2283 void
2284 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2286 rtx *tmps;
2287 int i;
2289 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2290 emit_group_load_1 (tmps, dst, src, type, ssize);
2292 /* Copy the extracted pieces into the proper (probable) hard regs. */
2293 for (i = 0; i < XVECLEN (dst, 0); i++)
2295 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2296 if (d == NULL)
2297 continue;
2298 emit_move_insn (d, tmps[i]);
2302 /* Similar, but load SRC into new pseudos in a format that looks like
2303 PARALLEL. This can later be fed to emit_group_move to get things
2304 in the right place. */
2307 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2309 rtvec vec;
2310 int i;
2312 vec = rtvec_alloc (XVECLEN (parallel, 0));
2313 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2315 /* Convert the vector to look just like the original PARALLEL, except
2316 with the computed values. */
2317 for (i = 0; i < XVECLEN (parallel, 0); i++)
2319 rtx e = XVECEXP (parallel, 0, i);
2320 rtx d = XEXP (e, 0);
2322 if (d)
2324 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2325 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2327 RTVEC_ELT (vec, i) = e;
2330 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2333 /* Emit code to move a block SRC to block DST, where SRC and DST are
2334 non-consecutive groups of registers, each represented by a PARALLEL. */
2336 void
2337 emit_group_move (rtx dst, rtx src)
2339 int i;
2341 gcc_assert (GET_CODE (src) == PARALLEL
2342 && GET_CODE (dst) == PARALLEL
2343 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2345 /* Skip first entry if NULL. */
2346 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2347 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2348 XEXP (XVECEXP (src, 0, i), 0));
2351 /* Move a group of registers represented by a PARALLEL into pseudos. */
2354 emit_group_move_into_temps (rtx src)
2356 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2357 int i;
2359 for (i = 0; i < XVECLEN (src, 0); i++)
2361 rtx e = XVECEXP (src, 0, i);
2362 rtx d = XEXP (e, 0);
2364 if (d)
2365 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2366 RTVEC_ELT (vec, i) = e;
2369 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2372 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2373 where SRC is non-consecutive registers represented by a PARALLEL.
2374 SSIZE represents the total size of block ORIG_DST, or -1 if not
2375 known. */
2377 void
2378 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2380 rtx *tmps, dst;
2381 int start, finish, i;
2382 machine_mode m = GET_MODE (orig_dst);
2384 gcc_assert (GET_CODE (src) == PARALLEL);
2386 if (!SCALAR_INT_MODE_P (m)
2387 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2389 scalar_int_mode imode;
2390 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2392 dst = gen_reg_rtx (imode);
2393 emit_group_store (dst, src, type, ssize);
2394 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2396 else
2398 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2399 emit_group_store (dst, src, type, ssize);
2401 emit_move_insn (orig_dst, dst);
2402 return;
2405 /* Check for a NULL entry, used to indicate that the parameter goes
2406 both on the stack and in registers. */
2407 if (XEXP (XVECEXP (src, 0, 0), 0))
2408 start = 0;
2409 else
2410 start = 1;
2411 finish = XVECLEN (src, 0);
2413 tmps = XALLOCAVEC (rtx, finish);
2415 /* Copy the (probable) hard regs into pseudos. */
2416 for (i = start; i < finish; i++)
2418 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2419 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2421 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2422 emit_move_insn (tmps[i], reg);
2424 else
2425 tmps[i] = reg;
2428 /* If we won't be storing directly into memory, protect the real destination
2429 from strange tricks we might play. */
2430 dst = orig_dst;
2431 if (GET_CODE (dst) == PARALLEL)
2433 rtx temp;
2435 /* We can get a PARALLEL dst if there is a conditional expression in
2436 a return statement. In that case, the dst and src are the same,
2437 so no action is necessary. */
2438 if (rtx_equal_p (dst, src))
2439 return;
2441 /* It is unclear if we can ever reach here, but we may as well handle
2442 it. Allocate a temporary, and split this into a store/load to/from
2443 the temporary. */
2444 temp = assign_stack_temp (GET_MODE (dst), ssize);
2445 emit_group_store (temp, src, type, ssize);
2446 emit_group_load (dst, temp, type, ssize);
2447 return;
2449 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2451 machine_mode outer = GET_MODE (dst);
2452 machine_mode inner;
2453 HOST_WIDE_INT bytepos;
2454 bool done = false;
2455 rtx temp;
2457 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2458 dst = gen_reg_rtx (outer);
2460 /* Make life a bit easier for combine. */
2461 /* If the first element of the vector is the low part
2462 of the destination mode, use a paradoxical subreg to
2463 initialize the destination. */
2464 if (start < finish)
2466 inner = GET_MODE (tmps[start]);
2467 bytepos = subreg_lowpart_offset (inner, outer);
2468 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2470 temp = simplify_gen_subreg (outer, tmps[start],
2471 inner, 0);
2472 if (temp)
2474 emit_move_insn (dst, temp);
2475 done = true;
2476 start++;
2481 /* If the first element wasn't the low part, try the last. */
2482 if (!done
2483 && start < finish - 1)
2485 inner = GET_MODE (tmps[finish - 1]);
2486 bytepos = subreg_lowpart_offset (inner, outer);
2487 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2489 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2490 inner, 0);
2491 if (temp)
2493 emit_move_insn (dst, temp);
2494 done = true;
2495 finish--;
2500 /* Otherwise, simply initialize the result to zero. */
2501 if (!done)
2502 emit_move_insn (dst, CONST0_RTX (outer));
2505 /* Process the pieces. */
2506 for (i = start; i < finish; i++)
2508 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2509 machine_mode mode = GET_MODE (tmps[i]);
2510 unsigned int bytelen = GET_MODE_SIZE (mode);
2511 unsigned int adj_bytelen;
2512 rtx dest = dst;
2514 /* Handle trailing fragments that run over the size of the struct. */
2515 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2516 adj_bytelen = ssize - bytepos;
2517 else
2518 adj_bytelen = bytelen;
2520 if (GET_CODE (dst) == CONCAT)
2522 if (bytepos + adj_bytelen
2523 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2524 dest = XEXP (dst, 0);
2525 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2527 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2528 dest = XEXP (dst, 1);
2530 else
2532 machine_mode dest_mode = GET_MODE (dest);
2533 machine_mode tmp_mode = GET_MODE (tmps[i]);
2535 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2537 if (GET_MODE_ALIGNMENT (dest_mode)
2538 >= GET_MODE_ALIGNMENT (tmp_mode))
2540 dest = assign_stack_temp (dest_mode,
2541 GET_MODE_SIZE (dest_mode));
2542 emit_move_insn (adjust_address (dest,
2543 tmp_mode,
2544 bytepos),
2545 tmps[i]);
2546 dst = dest;
2548 else
2550 dest = assign_stack_temp (tmp_mode,
2551 GET_MODE_SIZE (tmp_mode));
2552 emit_move_insn (dest, tmps[i]);
2553 dst = adjust_address (dest, dest_mode, bytepos);
2555 break;
2559 /* Handle trailing fragments that run over the size of the struct. */
2560 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2562 /* store_bit_field always takes its value from the lsb.
2563 Move the fragment to the lsb if it's not already there. */
2564 if (
2565 #ifdef BLOCK_REG_PADDING
2566 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2567 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2568 #else
2569 BYTES_BIG_ENDIAN
2570 #endif
2573 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2574 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2575 shift, tmps[i], 0);
2578 /* Make sure not to write past the end of the struct. */
2579 store_bit_field (dest,
2580 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2581 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2582 VOIDmode, tmps[i], false);
2585 /* Optimize the access just a bit. */
2586 else if (MEM_P (dest)
2587 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2588 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2589 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2590 && bytelen == GET_MODE_SIZE (mode))
2591 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2593 else
2594 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2595 0, 0, mode, tmps[i], false);
2598 /* Copy from the pseudo into the (probable) hard reg. */
2599 if (orig_dst != dst)
2600 emit_move_insn (orig_dst, dst);
2603 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2604 of the value stored in X. */
2607 maybe_emit_group_store (rtx x, tree type)
2609 machine_mode mode = TYPE_MODE (type);
2610 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2611 if (GET_CODE (x) == PARALLEL)
2613 rtx result = gen_reg_rtx (mode);
2614 emit_group_store (result, x, type, int_size_in_bytes (type));
2615 return result;
2617 return x;
2620 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2622 This is used on targets that return BLKmode values in registers. */
2624 static void
2625 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2627 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2628 rtx src = NULL, dst = NULL;
2629 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2630 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2631 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2632 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2633 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2634 fixed_size_mode copy_mode;
2636 /* BLKmode registers created in the back-end shouldn't have survived. */
2637 gcc_assert (mode != BLKmode);
2639 /* If the structure doesn't take up a whole number of words, see whether
2640 SRCREG is padded on the left or on the right. If it's on the left,
2641 set PADDING_CORRECTION to the number of bits to skip.
2643 In most ABIs, the structure will be returned at the least end of
2644 the register, which translates to right padding on little-endian
2645 targets and left padding on big-endian targets. The opposite
2646 holds if the structure is returned at the most significant
2647 end of the register. */
2648 if (bytes % UNITS_PER_WORD != 0
2649 && (targetm.calls.return_in_msb (type)
2650 ? !BYTES_BIG_ENDIAN
2651 : BYTES_BIG_ENDIAN))
2652 padding_correction
2653 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2655 /* We can use a single move if we have an exact mode for the size. */
2656 else if (MEM_P (target)
2657 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2658 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2659 && bytes == GET_MODE_SIZE (mode))
2661 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2662 return;
2665 /* And if we additionally have the same mode for a register. */
2666 else if (REG_P (target)
2667 && GET_MODE (target) == mode
2668 && bytes == GET_MODE_SIZE (mode))
2670 emit_move_insn (target, srcreg);
2671 return;
2674 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2675 into a new pseudo which is a full word. */
2676 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2678 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2679 mode = word_mode;
2682 /* Copy the structure BITSIZE bits at a time. If the target lives in
2683 memory, take care of not reading/writing past its end by selecting
2684 a copy mode suited to BITSIZE. This should always be possible given
2685 how it is computed.
2687 If the target lives in register, make sure not to select a copy mode
2688 larger than the mode of the register.
2690 We could probably emit more efficient code for machines which do not use
2691 strict alignment, but it doesn't seem worth the effort at the current
2692 time. */
2694 copy_mode = word_mode;
2695 if (MEM_P (target))
2697 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2698 if (mem_mode.exists ())
2699 copy_mode = mem_mode.require ();
2701 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2702 copy_mode = tmode;
2704 for (bitpos = 0, xbitpos = padding_correction;
2705 bitpos < bytes * BITS_PER_UNIT;
2706 bitpos += bitsize, xbitpos += bitsize)
2708 /* We need a new source operand each time xbitpos is on a
2709 word boundary and when xbitpos == padding_correction
2710 (the first time through). */
2711 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2712 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2714 /* We need a new destination operand each time bitpos is on
2715 a word boundary. */
2716 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2717 dst = target;
2718 else if (bitpos % BITS_PER_WORD == 0)
2719 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2721 /* Use xbitpos for the source extraction (right justified) and
2722 bitpos for the destination store (left justified). */
2723 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2724 extract_bit_field (src, bitsize,
2725 xbitpos % BITS_PER_WORD, 1,
2726 NULL_RTX, copy_mode, copy_mode,
2727 false, NULL),
2728 false);
2732 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2733 register if it contains any data, otherwise return null.
2735 This is used on targets that return BLKmode values in registers. */
2738 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2740 int i, n_regs;
2741 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2742 unsigned int bitsize;
2743 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2744 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2745 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2746 fixed_size_mode dst_mode;
2748 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2750 x = expand_normal (src);
2752 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2753 if (bytes == 0)
2754 return NULL_RTX;
2756 /* If the structure doesn't take up a whole number of words, see
2757 whether the register value should be padded on the left or on
2758 the right. Set PADDING_CORRECTION to the number of padding
2759 bits needed on the left side.
2761 In most ABIs, the structure will be returned at the least end of
2762 the register, which translates to right padding on little-endian
2763 targets and left padding on big-endian targets. The opposite
2764 holds if the structure is returned at the most significant
2765 end of the register. */
2766 if (bytes % UNITS_PER_WORD != 0
2767 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2768 ? !BYTES_BIG_ENDIAN
2769 : BYTES_BIG_ENDIAN))
2770 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2771 * BITS_PER_UNIT));
2773 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2774 dst_words = XALLOCAVEC (rtx, n_regs);
2775 bitsize = BITS_PER_WORD;
2776 if (targetm.slow_unaligned_access (word_mode, TYPE_ALIGN (TREE_TYPE (src))))
2777 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2779 /* Copy the structure BITSIZE bits at a time. */
2780 for (bitpos = 0, xbitpos = padding_correction;
2781 bitpos < bytes * BITS_PER_UNIT;
2782 bitpos += bitsize, xbitpos += bitsize)
2784 /* We need a new destination pseudo each time xbitpos is
2785 on a word boundary and when xbitpos == padding_correction
2786 (the first time through). */
2787 if (xbitpos % BITS_PER_WORD == 0
2788 || xbitpos == padding_correction)
2790 /* Generate an appropriate register. */
2791 dst_word = gen_reg_rtx (word_mode);
2792 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2794 /* Clear the destination before we move anything into it. */
2795 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2798 /* We need a new source operand each time bitpos is on a word
2799 boundary. */
2800 if (bitpos % BITS_PER_WORD == 0)
2801 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2803 /* Use bitpos for the source extraction (left justified) and
2804 xbitpos for the destination store (right justified). */
2805 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2806 0, 0, word_mode,
2807 extract_bit_field (src_word, bitsize,
2808 bitpos % BITS_PER_WORD, 1,
2809 NULL_RTX, word_mode, word_mode,
2810 false, NULL),
2811 false);
2814 if (mode == BLKmode)
2816 /* Find the smallest integer mode large enough to hold the
2817 entire structure. */
2818 opt_scalar_int_mode mode_iter;
2819 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2820 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2821 break;
2823 /* A suitable mode should have been found. */
2824 mode = mode_iter.require ();
2827 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2828 dst_mode = word_mode;
2829 else
2830 dst_mode = mode;
2831 dst = gen_reg_rtx (dst_mode);
2833 for (i = 0; i < n_regs; i++)
2834 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2836 if (mode != dst_mode)
2837 dst = gen_lowpart (mode, dst);
2839 return dst;
2842 /* Add a USE expression for REG to the (possibly empty) list pointed
2843 to by CALL_FUSAGE. REG must denote a hard register. */
2845 void
2846 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2848 gcc_assert (REG_P (reg));
2850 if (!HARD_REGISTER_P (reg))
2851 return;
2853 *call_fusage
2854 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2857 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2858 to by CALL_FUSAGE. REG must denote a hard register. */
2860 void
2861 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2863 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2865 *call_fusage
2866 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2869 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2870 starting at REGNO. All of these registers must be hard registers. */
2872 void
2873 use_regs (rtx *call_fusage, int regno, int nregs)
2875 int i;
2877 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2879 for (i = 0; i < nregs; i++)
2880 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2883 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2884 PARALLEL REGS. This is for calls that pass values in multiple
2885 non-contiguous locations. The Irix 6 ABI has examples of this. */
2887 void
2888 use_group_regs (rtx *call_fusage, rtx regs)
2890 int i;
2892 for (i = 0; i < XVECLEN (regs, 0); i++)
2894 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2896 /* A NULL entry means the parameter goes both on the stack and in
2897 registers. This can also be a MEM for targets that pass values
2898 partially on the stack and partially in registers. */
2899 if (reg != 0 && REG_P (reg))
2900 use_reg (call_fusage, reg);
2904 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2905 assigment and the code of the expresion on the RHS is CODE. Return
2906 NULL otherwise. */
2908 static gimple *
2909 get_def_for_expr (tree name, enum tree_code code)
2911 gimple *def_stmt;
2913 if (TREE_CODE (name) != SSA_NAME)
2914 return NULL;
2916 def_stmt = get_gimple_for_ssa_name (name);
2917 if (!def_stmt
2918 || gimple_assign_rhs_code (def_stmt) != code)
2919 return NULL;
2921 return def_stmt;
2924 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2925 assigment and the class of the expresion on the RHS is CLASS. Return
2926 NULL otherwise. */
2928 static gimple *
2929 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2931 gimple *def_stmt;
2933 if (TREE_CODE (name) != SSA_NAME)
2934 return NULL;
2936 def_stmt = get_gimple_for_ssa_name (name);
2937 if (!def_stmt
2938 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2939 return NULL;
2941 return def_stmt;
2944 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2945 its length in bytes. */
2948 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2949 unsigned int expected_align, HOST_WIDE_INT expected_size,
2950 unsigned HOST_WIDE_INT min_size,
2951 unsigned HOST_WIDE_INT max_size,
2952 unsigned HOST_WIDE_INT probable_max_size)
2954 machine_mode mode = GET_MODE (object);
2955 unsigned int align;
2957 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2959 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2960 just move a zero. Otherwise, do this a piece at a time. */
2961 if (mode != BLKmode
2962 && CONST_INT_P (size)
2963 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2965 rtx zero = CONST0_RTX (mode);
2966 if (zero != NULL)
2968 emit_move_insn (object, zero);
2969 return NULL;
2972 if (COMPLEX_MODE_P (mode))
2974 zero = CONST0_RTX (GET_MODE_INNER (mode));
2975 if (zero != NULL)
2977 write_complex_part (object, zero, 0);
2978 write_complex_part (object, zero, 1);
2979 return NULL;
2984 if (size == const0_rtx)
2985 return NULL;
2987 align = MEM_ALIGN (object);
2989 if (CONST_INT_P (size)
2990 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2991 CLEAR_BY_PIECES,
2992 optimize_insn_for_speed_p ()))
2993 clear_by_pieces (object, INTVAL (size), align);
2994 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2995 expected_align, expected_size,
2996 min_size, max_size, probable_max_size))
2998 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2999 return set_storage_via_libcall (object, size, const0_rtx,
3000 method == BLOCK_OP_TAILCALL);
3001 else
3002 gcc_unreachable ();
3004 return NULL;
3008 clear_storage (rtx object, rtx size, enum block_op_methods method)
3010 unsigned HOST_WIDE_INT max, min = 0;
3011 if (GET_CODE (size) == CONST_INT)
3012 min = max = UINTVAL (size);
3013 else
3014 max = GET_MODE_MASK (GET_MODE (size));
3015 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3019 /* A subroutine of clear_storage. Expand a call to memset.
3020 Return the return value of memset, 0 otherwise. */
3023 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3025 tree call_expr, fn, object_tree, size_tree, val_tree;
3026 machine_mode size_mode;
3028 object = copy_addr_to_reg (XEXP (object, 0));
3029 object_tree = make_tree (ptr_type_node, object);
3031 if (!CONST_INT_P (val))
3032 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3033 val_tree = make_tree (integer_type_node, val);
3035 size_mode = TYPE_MODE (sizetype);
3036 size = convert_to_mode (size_mode, size, 1);
3037 size = copy_to_mode_reg (size_mode, size);
3038 size_tree = make_tree (sizetype, size);
3040 /* It is incorrect to use the libcall calling conventions for calls to
3041 memset because it can be provided by the user. */
3042 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3043 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3044 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3046 return expand_call (call_expr, NULL_RTX, false);
3049 /* Expand a setmem pattern; return true if successful. */
3051 bool
3052 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3053 unsigned int expected_align, HOST_WIDE_INT expected_size,
3054 unsigned HOST_WIDE_INT min_size,
3055 unsigned HOST_WIDE_INT max_size,
3056 unsigned HOST_WIDE_INT probable_max_size)
3058 /* Try the most limited insn first, because there's no point
3059 including more than one in the machine description unless
3060 the more limited one has some advantage. */
3062 if (expected_align < align)
3063 expected_align = align;
3064 if (expected_size != -1)
3066 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3067 expected_size = max_size;
3068 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3069 expected_size = min_size;
3072 opt_scalar_int_mode mode_iter;
3073 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3075 scalar_int_mode mode = mode_iter.require ();
3076 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3078 if (code != CODE_FOR_nothing
3079 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3080 here because if SIZE is less than the mode mask, as it is
3081 returned by the macro, it will definitely be less than the
3082 actual mode mask. Since SIZE is within the Pmode address
3083 space, we limit MODE to Pmode. */
3084 && ((CONST_INT_P (size)
3085 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3086 <= (GET_MODE_MASK (mode) >> 1)))
3087 || max_size <= (GET_MODE_MASK (mode) >> 1)
3088 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3090 struct expand_operand ops[9];
3091 unsigned int nops;
3093 nops = insn_data[(int) code].n_generator_args;
3094 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3096 create_fixed_operand (&ops[0], object);
3097 /* The check above guarantees that this size conversion is valid. */
3098 create_convert_operand_to (&ops[1], size, mode, true);
3099 create_convert_operand_from (&ops[2], val, byte_mode, true);
3100 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3101 if (nops >= 6)
3103 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3104 create_integer_operand (&ops[5], expected_size);
3106 if (nops >= 8)
3108 create_integer_operand (&ops[6], min_size);
3109 /* If we can not represent the maximal size,
3110 make parameter NULL. */
3111 if ((HOST_WIDE_INT) max_size != -1)
3112 create_integer_operand (&ops[7], max_size);
3113 else
3114 create_fixed_operand (&ops[7], NULL);
3116 if (nops == 9)
3118 /* If we can not represent the maximal size,
3119 make parameter NULL. */
3120 if ((HOST_WIDE_INT) probable_max_size != -1)
3121 create_integer_operand (&ops[8], probable_max_size);
3122 else
3123 create_fixed_operand (&ops[8], NULL);
3125 if (maybe_expand_insn (code, nops, ops))
3126 return true;
3130 return false;
3134 /* Write to one of the components of the complex value CPLX. Write VAL to
3135 the real part if IMAG_P is false, and the imaginary part if its true. */
3137 void
3138 write_complex_part (rtx cplx, rtx val, bool imag_p)
3140 machine_mode cmode;
3141 scalar_mode imode;
3142 unsigned ibitsize;
3144 if (GET_CODE (cplx) == CONCAT)
3146 emit_move_insn (XEXP (cplx, imag_p), val);
3147 return;
3150 cmode = GET_MODE (cplx);
3151 imode = GET_MODE_INNER (cmode);
3152 ibitsize = GET_MODE_BITSIZE (imode);
3154 /* For MEMs simplify_gen_subreg may generate an invalid new address
3155 because, e.g., the original address is considered mode-dependent
3156 by the target, which restricts simplify_subreg from invoking
3157 adjust_address_nv. Instead of preparing fallback support for an
3158 invalid address, we call adjust_address_nv directly. */
3159 if (MEM_P (cplx))
3161 emit_move_insn (adjust_address_nv (cplx, imode,
3162 imag_p ? GET_MODE_SIZE (imode) : 0),
3163 val);
3164 return;
3167 /* If the sub-object is at least word sized, then we know that subregging
3168 will work. This special case is important, since store_bit_field
3169 wants to operate on integer modes, and there's rarely an OImode to
3170 correspond to TCmode. */
3171 if (ibitsize >= BITS_PER_WORD
3172 /* For hard regs we have exact predicates. Assume we can split
3173 the original object if it spans an even number of hard regs.
3174 This special case is important for SCmode on 64-bit platforms
3175 where the natural size of floating-point regs is 32-bit. */
3176 || (REG_P (cplx)
3177 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3178 && REG_NREGS (cplx) % 2 == 0))
3180 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3181 imag_p ? GET_MODE_SIZE (imode) : 0);
3182 if (part)
3184 emit_move_insn (part, val);
3185 return;
3187 else
3188 /* simplify_gen_subreg may fail for sub-word MEMs. */
3189 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3192 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3193 false);
3196 /* Extract one of the components of the complex value CPLX. Extract the
3197 real part if IMAG_P is false, and the imaginary part if it's true. */
3200 read_complex_part (rtx cplx, bool imag_p)
3202 machine_mode cmode;
3203 scalar_mode imode;
3204 unsigned ibitsize;
3206 if (GET_CODE (cplx) == CONCAT)
3207 return XEXP (cplx, imag_p);
3209 cmode = GET_MODE (cplx);
3210 imode = GET_MODE_INNER (cmode);
3211 ibitsize = GET_MODE_BITSIZE (imode);
3213 /* Special case reads from complex constants that got spilled to memory. */
3214 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3216 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3217 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3219 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3220 if (CONSTANT_CLASS_P (part))
3221 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3225 /* For MEMs simplify_gen_subreg may generate an invalid new address
3226 because, e.g., the original address is considered mode-dependent
3227 by the target, which restricts simplify_subreg from invoking
3228 adjust_address_nv. Instead of preparing fallback support for an
3229 invalid address, we call adjust_address_nv directly. */
3230 if (MEM_P (cplx))
3231 return adjust_address_nv (cplx, imode,
3232 imag_p ? GET_MODE_SIZE (imode) : 0);
3234 /* If the sub-object is at least word sized, then we know that subregging
3235 will work. This special case is important, since extract_bit_field
3236 wants to operate on integer modes, and there's rarely an OImode to
3237 correspond to TCmode. */
3238 if (ibitsize >= BITS_PER_WORD
3239 /* For hard regs we have exact predicates. Assume we can split
3240 the original object if it spans an even number of hard regs.
3241 This special case is important for SCmode on 64-bit platforms
3242 where the natural size of floating-point regs is 32-bit. */
3243 || (REG_P (cplx)
3244 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3245 && REG_NREGS (cplx) % 2 == 0))
3247 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3248 imag_p ? GET_MODE_SIZE (imode) : 0);
3249 if (ret)
3250 return ret;
3251 else
3252 /* simplify_gen_subreg may fail for sub-word MEMs. */
3253 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3256 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3257 true, NULL_RTX, imode, imode, false, NULL);
3260 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3261 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3262 represented in NEW_MODE. If FORCE is true, this will never happen, as
3263 we'll force-create a SUBREG if needed. */
3265 static rtx
3266 emit_move_change_mode (machine_mode new_mode,
3267 machine_mode old_mode, rtx x, bool force)
3269 rtx ret;
3271 if (push_operand (x, GET_MODE (x)))
3273 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3274 MEM_COPY_ATTRIBUTES (ret, x);
3276 else if (MEM_P (x))
3278 /* We don't have to worry about changing the address since the
3279 size in bytes is supposed to be the same. */
3280 if (reload_in_progress)
3282 /* Copy the MEM to change the mode and move any
3283 substitutions from the old MEM to the new one. */
3284 ret = adjust_address_nv (x, new_mode, 0);
3285 copy_replacements (x, ret);
3287 else
3288 ret = adjust_address (x, new_mode, 0);
3290 else
3292 /* Note that we do want simplify_subreg's behavior of validating
3293 that the new mode is ok for a hard register. If we were to use
3294 simplify_gen_subreg, we would create the subreg, but would
3295 probably run into the target not being able to implement it. */
3296 /* Except, of course, when FORCE is true, when this is exactly what
3297 we want. Which is needed for CCmodes on some targets. */
3298 if (force)
3299 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3300 else
3301 ret = simplify_subreg (new_mode, x, old_mode, 0);
3304 return ret;
3307 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3308 an integer mode of the same size as MODE. Returns the instruction
3309 emitted, or NULL if such a move could not be generated. */
3311 static rtx_insn *
3312 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3314 scalar_int_mode imode;
3315 enum insn_code code;
3317 /* There must exist a mode of the exact size we require. */
3318 if (!int_mode_for_mode (mode).exists (&imode))
3319 return NULL;
3321 /* The target must support moves in this mode. */
3322 code = optab_handler (mov_optab, imode);
3323 if (code == CODE_FOR_nothing)
3324 return NULL;
3326 x = emit_move_change_mode (imode, mode, x, force);
3327 if (x == NULL_RTX)
3328 return NULL;
3329 y = emit_move_change_mode (imode, mode, y, force);
3330 if (y == NULL_RTX)
3331 return NULL;
3332 return emit_insn (GEN_FCN (code) (x, y));
3335 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3336 Return an equivalent MEM that does not use an auto-increment. */
3339 emit_move_resolve_push (machine_mode mode, rtx x)
3341 enum rtx_code code = GET_CODE (XEXP (x, 0));
3342 HOST_WIDE_INT adjust;
3343 rtx temp;
3345 adjust = GET_MODE_SIZE (mode);
3346 #ifdef PUSH_ROUNDING
3347 adjust = PUSH_ROUNDING (adjust);
3348 #endif
3349 if (code == PRE_DEC || code == POST_DEC)
3350 adjust = -adjust;
3351 else if (code == PRE_MODIFY || code == POST_MODIFY)
3353 rtx expr = XEXP (XEXP (x, 0), 1);
3354 HOST_WIDE_INT val;
3356 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3357 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3358 val = INTVAL (XEXP (expr, 1));
3359 if (GET_CODE (expr) == MINUS)
3360 val = -val;
3361 gcc_assert (adjust == val || adjust == -val);
3362 adjust = val;
3365 /* Do not use anti_adjust_stack, since we don't want to update
3366 stack_pointer_delta. */
3367 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3368 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3369 0, OPTAB_LIB_WIDEN);
3370 if (temp != stack_pointer_rtx)
3371 emit_move_insn (stack_pointer_rtx, temp);
3373 switch (code)
3375 case PRE_INC:
3376 case PRE_DEC:
3377 case PRE_MODIFY:
3378 temp = stack_pointer_rtx;
3379 break;
3380 case POST_INC:
3381 case POST_DEC:
3382 case POST_MODIFY:
3383 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3384 break;
3385 default:
3386 gcc_unreachable ();
3389 return replace_equiv_address (x, temp);
3392 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3393 X is known to satisfy push_operand, and MODE is known to be complex.
3394 Returns the last instruction emitted. */
3396 rtx_insn *
3397 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3399 scalar_mode submode = GET_MODE_INNER (mode);
3400 bool imag_first;
3402 #ifdef PUSH_ROUNDING
3403 unsigned int submodesize = GET_MODE_SIZE (submode);
3405 /* In case we output to the stack, but the size is smaller than the
3406 machine can push exactly, we need to use move instructions. */
3407 if (PUSH_ROUNDING (submodesize) != submodesize)
3409 x = emit_move_resolve_push (mode, x);
3410 return emit_move_insn (x, y);
3412 #endif
3414 /* Note that the real part always precedes the imag part in memory
3415 regardless of machine's endianness. */
3416 switch (GET_CODE (XEXP (x, 0)))
3418 case PRE_DEC:
3419 case POST_DEC:
3420 imag_first = true;
3421 break;
3422 case PRE_INC:
3423 case POST_INC:
3424 imag_first = false;
3425 break;
3426 default:
3427 gcc_unreachable ();
3430 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3431 read_complex_part (y, imag_first));
3432 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3433 read_complex_part (y, !imag_first));
3436 /* A subroutine of emit_move_complex. Perform the move from Y to X
3437 via two moves of the parts. Returns the last instruction emitted. */
3439 rtx_insn *
3440 emit_move_complex_parts (rtx x, rtx y)
3442 /* Show the output dies here. This is necessary for SUBREGs
3443 of pseudos since we cannot track their lifetimes correctly;
3444 hard regs shouldn't appear here except as return values. */
3445 if (!reload_completed && !reload_in_progress
3446 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3447 emit_clobber (x);
3449 write_complex_part (x, read_complex_part (y, false), false);
3450 write_complex_part (x, read_complex_part (y, true), true);
3452 return get_last_insn ();
3455 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3456 MODE is known to be complex. Returns the last instruction emitted. */
3458 static rtx_insn *
3459 emit_move_complex (machine_mode mode, rtx x, rtx y)
3461 bool try_int;
3463 /* Need to take special care for pushes, to maintain proper ordering
3464 of the data, and possibly extra padding. */
3465 if (push_operand (x, mode))
3466 return emit_move_complex_push (mode, x, y);
3468 /* See if we can coerce the target into moving both values at once, except
3469 for floating point where we favor moving as parts if this is easy. */
3470 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3471 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3472 && !(REG_P (x)
3473 && HARD_REGISTER_P (x)
3474 && REG_NREGS (x) == 1)
3475 && !(REG_P (y)
3476 && HARD_REGISTER_P (y)
3477 && REG_NREGS (y) == 1))
3478 try_int = false;
3479 /* Not possible if the values are inherently not adjacent. */
3480 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3481 try_int = false;
3482 /* Is possible if both are registers (or subregs of registers). */
3483 else if (register_operand (x, mode) && register_operand (y, mode))
3484 try_int = true;
3485 /* If one of the operands is a memory, and alignment constraints
3486 are friendly enough, we may be able to do combined memory operations.
3487 We do not attempt this if Y is a constant because that combination is
3488 usually better with the by-parts thing below. */
3489 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3490 && (!STRICT_ALIGNMENT
3491 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3492 try_int = true;
3493 else
3494 try_int = false;
3496 if (try_int)
3498 rtx_insn *ret;
3500 /* For memory to memory moves, optimal behavior can be had with the
3501 existing block move logic. */
3502 if (MEM_P (x) && MEM_P (y))
3504 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3505 BLOCK_OP_NO_LIBCALL);
3506 return get_last_insn ();
3509 ret = emit_move_via_integer (mode, x, y, true);
3510 if (ret)
3511 return ret;
3514 return emit_move_complex_parts (x, y);
3517 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3518 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3520 static rtx_insn *
3521 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3523 rtx_insn *ret;
3525 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3526 if (mode != CCmode)
3528 enum insn_code code = optab_handler (mov_optab, CCmode);
3529 if (code != CODE_FOR_nothing)
3531 x = emit_move_change_mode (CCmode, mode, x, true);
3532 y = emit_move_change_mode (CCmode, mode, y, true);
3533 return emit_insn (GEN_FCN (code) (x, y));
3537 /* Otherwise, find the MODE_INT mode of the same width. */
3538 ret = emit_move_via_integer (mode, x, y, false);
3539 gcc_assert (ret != NULL);
3540 return ret;
3543 /* Return true if word I of OP lies entirely in the
3544 undefined bits of a paradoxical subreg. */
3546 static bool
3547 undefined_operand_subword_p (const_rtx op, int i)
3549 if (GET_CODE (op) != SUBREG)
3550 return false;
3551 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3552 HOST_WIDE_INT offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3553 return (offset >= GET_MODE_SIZE (innermostmode)
3554 || offset <= -UNITS_PER_WORD);
3557 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3558 MODE is any multi-word or full-word mode that lacks a move_insn
3559 pattern. Note that you will get better code if you define such
3560 patterns, even if they must turn into multiple assembler instructions. */
3562 static rtx_insn *
3563 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3565 rtx_insn *last_insn = 0;
3566 rtx_insn *seq;
3567 rtx inner;
3568 bool need_clobber;
3569 int i;
3571 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3573 /* If X is a push on the stack, do the push now and replace
3574 X with a reference to the stack pointer. */
3575 if (push_operand (x, mode))
3576 x = emit_move_resolve_push (mode, x);
3578 /* If we are in reload, see if either operand is a MEM whose address
3579 is scheduled for replacement. */
3580 if (reload_in_progress && MEM_P (x)
3581 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3582 x = replace_equiv_address_nv (x, inner);
3583 if (reload_in_progress && MEM_P (y)
3584 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3585 y = replace_equiv_address_nv (y, inner);
3587 start_sequence ();
3589 need_clobber = false;
3590 for (i = 0;
3591 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3592 i++)
3594 rtx xpart = operand_subword (x, i, 1, mode);
3595 rtx ypart;
3597 /* Do not generate code for a move if it would come entirely
3598 from the undefined bits of a paradoxical subreg. */
3599 if (undefined_operand_subword_p (y, i))
3600 continue;
3602 ypart = operand_subword (y, i, 1, mode);
3604 /* If we can't get a part of Y, put Y into memory if it is a
3605 constant. Otherwise, force it into a register. Then we must
3606 be able to get a part of Y. */
3607 if (ypart == 0 && CONSTANT_P (y))
3609 y = use_anchored_address (force_const_mem (mode, y));
3610 ypart = operand_subword (y, i, 1, mode);
3612 else if (ypart == 0)
3613 ypart = operand_subword_force (y, i, mode);
3615 gcc_assert (xpart && ypart);
3617 need_clobber |= (GET_CODE (xpart) == SUBREG);
3619 last_insn = emit_move_insn (xpart, ypart);
3622 seq = get_insns ();
3623 end_sequence ();
3625 /* Show the output dies here. This is necessary for SUBREGs
3626 of pseudos since we cannot track their lifetimes correctly;
3627 hard regs shouldn't appear here except as return values.
3628 We never want to emit such a clobber after reload. */
3629 if (x != y
3630 && ! (reload_in_progress || reload_completed)
3631 && need_clobber != 0)
3632 emit_clobber (x);
3634 emit_insn (seq);
3636 return last_insn;
3639 /* Low level part of emit_move_insn.
3640 Called just like emit_move_insn, but assumes X and Y
3641 are basically valid. */
3643 rtx_insn *
3644 emit_move_insn_1 (rtx x, rtx y)
3646 machine_mode mode = GET_MODE (x);
3647 enum insn_code code;
3649 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3651 code = optab_handler (mov_optab, mode);
3652 if (code != CODE_FOR_nothing)
3653 return emit_insn (GEN_FCN (code) (x, y));
3655 /* Expand complex moves by moving real part and imag part. */
3656 if (COMPLEX_MODE_P (mode))
3657 return emit_move_complex (mode, x, y);
3659 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3660 || ALL_FIXED_POINT_MODE_P (mode))
3662 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3664 /* If we can't find an integer mode, use multi words. */
3665 if (result)
3666 return result;
3667 else
3668 return emit_move_multi_word (mode, x, y);
3671 if (GET_MODE_CLASS (mode) == MODE_CC)
3672 return emit_move_ccmode (mode, x, y);
3674 /* Try using a move pattern for the corresponding integer mode. This is
3675 only safe when simplify_subreg can convert MODE constants into integer
3676 constants. At present, it can only do this reliably if the value
3677 fits within a HOST_WIDE_INT. */
3678 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3680 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3682 if (ret)
3684 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3685 return ret;
3689 return emit_move_multi_word (mode, x, y);
3692 /* Generate code to copy Y into X.
3693 Both Y and X must have the same mode, except that
3694 Y can be a constant with VOIDmode.
3695 This mode cannot be BLKmode; use emit_block_move for that.
3697 Return the last instruction emitted. */
3699 rtx_insn *
3700 emit_move_insn (rtx x, rtx y)
3702 machine_mode mode = GET_MODE (x);
3703 rtx y_cst = NULL_RTX;
3704 rtx_insn *last_insn;
3705 rtx set;
3707 gcc_assert (mode != BLKmode
3708 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3710 if (CONSTANT_P (y))
3712 if (optimize
3713 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3714 && (last_insn = compress_float_constant (x, y)))
3715 return last_insn;
3717 y_cst = y;
3719 if (!targetm.legitimate_constant_p (mode, y))
3721 y = force_const_mem (mode, y);
3723 /* If the target's cannot_force_const_mem prevented the spill,
3724 assume that the target's move expanders will also take care
3725 of the non-legitimate constant. */
3726 if (!y)
3727 y = y_cst;
3728 else
3729 y = use_anchored_address (y);
3733 /* If X or Y are memory references, verify that their addresses are valid
3734 for the machine. */
3735 if (MEM_P (x)
3736 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3737 MEM_ADDR_SPACE (x))
3738 && ! push_operand (x, GET_MODE (x))))
3739 x = validize_mem (x);
3741 if (MEM_P (y)
3742 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3743 MEM_ADDR_SPACE (y)))
3744 y = validize_mem (y);
3746 gcc_assert (mode != BLKmode);
3748 last_insn = emit_move_insn_1 (x, y);
3750 if (y_cst && REG_P (x)
3751 && (set = single_set (last_insn)) != NULL_RTX
3752 && SET_DEST (set) == x
3753 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3754 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3756 return last_insn;
3759 /* Generate the body of an instruction to copy Y into X.
3760 It may be a list of insns, if one insn isn't enough. */
3762 rtx_insn *
3763 gen_move_insn (rtx x, rtx y)
3765 rtx_insn *seq;
3767 start_sequence ();
3768 emit_move_insn_1 (x, y);
3769 seq = get_insns ();
3770 end_sequence ();
3771 return seq;
3774 /* If Y is representable exactly in a narrower mode, and the target can
3775 perform the extension directly from constant or memory, then emit the
3776 move as an extension. */
3778 static rtx_insn *
3779 compress_float_constant (rtx x, rtx y)
3781 machine_mode dstmode = GET_MODE (x);
3782 machine_mode orig_srcmode = GET_MODE (y);
3783 machine_mode srcmode;
3784 const REAL_VALUE_TYPE *r;
3785 int oldcost, newcost;
3786 bool speed = optimize_insn_for_speed_p ();
3788 r = CONST_DOUBLE_REAL_VALUE (y);
3790 if (targetm.legitimate_constant_p (dstmode, y))
3791 oldcost = set_src_cost (y, orig_srcmode, speed);
3792 else
3793 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3795 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3797 enum insn_code ic;
3798 rtx trunc_y;
3799 rtx_insn *last_insn;
3801 /* Skip if the target can't extend this way. */
3802 ic = can_extend_p (dstmode, srcmode, 0);
3803 if (ic == CODE_FOR_nothing)
3804 continue;
3806 /* Skip if the narrowed value isn't exact. */
3807 if (! exact_real_truncate (srcmode, r))
3808 continue;
3810 trunc_y = const_double_from_real_value (*r, srcmode);
3812 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3814 /* Skip if the target needs extra instructions to perform
3815 the extension. */
3816 if (!insn_operand_matches (ic, 1, trunc_y))
3817 continue;
3818 /* This is valid, but may not be cheaper than the original. */
3819 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3820 dstmode, speed);
3821 if (oldcost < newcost)
3822 continue;
3824 else if (float_extend_from_mem[dstmode][srcmode])
3826 trunc_y = force_const_mem (srcmode, trunc_y);
3827 /* This is valid, but may not be cheaper than the original. */
3828 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3829 dstmode, speed);
3830 if (oldcost < newcost)
3831 continue;
3832 trunc_y = validize_mem (trunc_y);
3834 else
3835 continue;
3837 /* For CSE's benefit, force the compressed constant pool entry
3838 into a new pseudo. This constant may be used in different modes,
3839 and if not, combine will put things back together for us. */
3840 trunc_y = force_reg (srcmode, trunc_y);
3842 /* If x is a hard register, perform the extension into a pseudo,
3843 so that e.g. stack realignment code is aware of it. */
3844 rtx target = x;
3845 if (REG_P (x) && HARD_REGISTER_P (x))
3846 target = gen_reg_rtx (dstmode);
3848 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3849 last_insn = get_last_insn ();
3851 if (REG_P (target))
3852 set_unique_reg_note (last_insn, REG_EQUAL, y);
3854 if (target != x)
3855 return emit_move_insn (x, target);
3856 return last_insn;
3859 return NULL;
3862 /* Pushing data onto the stack. */
3864 /* Push a block of length SIZE (perhaps variable)
3865 and return an rtx to address the beginning of the block.
3866 The value may be virtual_outgoing_args_rtx.
3868 EXTRA is the number of bytes of padding to push in addition to SIZE.
3869 BELOW nonzero means this padding comes at low addresses;
3870 otherwise, the padding comes at high addresses. */
3873 push_block (rtx size, int extra, int below)
3875 rtx temp;
3877 size = convert_modes (Pmode, ptr_mode, size, 1);
3878 if (CONSTANT_P (size))
3879 anti_adjust_stack (plus_constant (Pmode, size, extra));
3880 else if (REG_P (size) && extra == 0)
3881 anti_adjust_stack (size);
3882 else
3884 temp = copy_to_mode_reg (Pmode, size);
3885 if (extra != 0)
3886 temp = expand_binop (Pmode, add_optab, temp,
3887 gen_int_mode (extra, Pmode),
3888 temp, 0, OPTAB_LIB_WIDEN);
3889 anti_adjust_stack (temp);
3892 if (STACK_GROWS_DOWNWARD)
3894 temp = virtual_outgoing_args_rtx;
3895 if (extra != 0 && below)
3896 temp = plus_constant (Pmode, temp, extra);
3898 else
3900 if (CONST_INT_P (size))
3901 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3902 -INTVAL (size) - (below ? 0 : extra));
3903 else if (extra != 0 && !below)
3904 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3905 negate_rtx (Pmode, plus_constant (Pmode, size,
3906 extra)));
3907 else
3908 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3909 negate_rtx (Pmode, size));
3912 return memory_address (NARROWEST_INT_MODE, temp);
3915 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3917 static rtx
3918 mem_autoinc_base (rtx mem)
3920 if (MEM_P (mem))
3922 rtx addr = XEXP (mem, 0);
3923 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3924 return XEXP (addr, 0);
3926 return NULL;
3929 /* A utility routine used here, in reload, and in try_split. The insns
3930 after PREV up to and including LAST are known to adjust the stack,
3931 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3932 placing notes as appropriate. PREV may be NULL, indicating the
3933 entire insn sequence prior to LAST should be scanned.
3935 The set of allowed stack pointer modifications is small:
3936 (1) One or more auto-inc style memory references (aka pushes),
3937 (2) One or more addition/subtraction with the SP as destination,
3938 (3) A single move insn with the SP as destination,
3939 (4) A call_pop insn,
3940 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3942 Insns in the sequence that do not modify the SP are ignored,
3943 except for noreturn calls.
3945 The return value is the amount of adjustment that can be trivially
3946 verified, via immediate operand or auto-inc. If the adjustment
3947 cannot be trivially extracted, the return value is INT_MIN. */
3949 HOST_WIDE_INT
3950 find_args_size_adjust (rtx_insn *insn)
3952 rtx dest, set, pat;
3953 int i;
3955 pat = PATTERN (insn);
3956 set = NULL;
3958 /* Look for a call_pop pattern. */
3959 if (CALL_P (insn))
3961 /* We have to allow non-call_pop patterns for the case
3962 of emit_single_push_insn of a TLS address. */
3963 if (GET_CODE (pat) != PARALLEL)
3964 return 0;
3966 /* All call_pop have a stack pointer adjust in the parallel.
3967 The call itself is always first, and the stack adjust is
3968 usually last, so search from the end. */
3969 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3971 set = XVECEXP (pat, 0, i);
3972 if (GET_CODE (set) != SET)
3973 continue;
3974 dest = SET_DEST (set);
3975 if (dest == stack_pointer_rtx)
3976 break;
3978 /* We'd better have found the stack pointer adjust. */
3979 if (i == 0)
3980 return 0;
3981 /* Fall through to process the extracted SET and DEST
3982 as if it was a standalone insn. */
3984 else if (GET_CODE (pat) == SET)
3985 set = pat;
3986 else if ((set = single_set (insn)) != NULL)
3988 else if (GET_CODE (pat) == PARALLEL)
3990 /* ??? Some older ports use a parallel with a stack adjust
3991 and a store for a PUSH_ROUNDING pattern, rather than a
3992 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3993 /* ??? See h8300 and m68k, pushqi1. */
3994 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3996 set = XVECEXP (pat, 0, i);
3997 if (GET_CODE (set) != SET)
3998 continue;
3999 dest = SET_DEST (set);
4000 if (dest == stack_pointer_rtx)
4001 break;
4003 /* We do not expect an auto-inc of the sp in the parallel. */
4004 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4005 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4006 != stack_pointer_rtx);
4008 if (i < 0)
4009 return 0;
4011 else
4012 return 0;
4014 dest = SET_DEST (set);
4016 /* Look for direct modifications of the stack pointer. */
4017 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4019 /* Look for a trivial adjustment, otherwise assume nothing. */
4020 /* Note that the SPU restore_stack_block pattern refers to
4021 the stack pointer in V4SImode. Consider that non-trivial. */
4022 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4023 && GET_CODE (SET_SRC (set)) == PLUS
4024 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4025 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4026 return INTVAL (XEXP (SET_SRC (set), 1));
4027 /* ??? Reload can generate no-op moves, which will be cleaned
4028 up later. Recognize it and continue searching. */
4029 else if (rtx_equal_p (dest, SET_SRC (set)))
4030 return 0;
4031 else
4032 return HOST_WIDE_INT_MIN;
4034 else
4036 rtx mem, addr;
4038 /* Otherwise only think about autoinc patterns. */
4039 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4041 mem = dest;
4042 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4043 != stack_pointer_rtx);
4045 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4046 mem = SET_SRC (set);
4047 else
4048 return 0;
4050 addr = XEXP (mem, 0);
4051 switch (GET_CODE (addr))
4053 case PRE_INC:
4054 case POST_INC:
4055 return GET_MODE_SIZE (GET_MODE (mem));
4056 case PRE_DEC:
4057 case POST_DEC:
4058 return -GET_MODE_SIZE (GET_MODE (mem));
4059 case PRE_MODIFY:
4060 case POST_MODIFY:
4061 addr = XEXP (addr, 1);
4062 gcc_assert (GET_CODE (addr) == PLUS);
4063 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4064 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4065 return INTVAL (XEXP (addr, 1));
4066 default:
4067 gcc_unreachable ();
4073 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4075 int args_size = end_args_size;
4076 bool saw_unknown = false;
4077 rtx_insn *insn;
4079 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4081 HOST_WIDE_INT this_delta;
4083 if (!NONDEBUG_INSN_P (insn))
4084 continue;
4086 this_delta = find_args_size_adjust (insn);
4087 if (this_delta == 0)
4089 if (!CALL_P (insn)
4090 || ACCUMULATE_OUTGOING_ARGS
4091 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4092 continue;
4095 gcc_assert (!saw_unknown);
4096 if (this_delta == HOST_WIDE_INT_MIN)
4097 saw_unknown = true;
4099 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4100 if (STACK_GROWS_DOWNWARD)
4101 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4103 if (saw_unknown)
4104 args_size = INT_MIN;
4105 else
4106 args_size -= this_delta;
4109 return args_size;
4112 #ifdef PUSH_ROUNDING
4113 /* Emit single push insn. */
4115 static void
4116 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4118 rtx dest_addr;
4119 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4120 rtx dest;
4121 enum insn_code icode;
4123 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4124 /* If there is push pattern, use it. Otherwise try old way of throwing
4125 MEM representing push operation to move expander. */
4126 icode = optab_handler (push_optab, mode);
4127 if (icode != CODE_FOR_nothing)
4129 struct expand_operand ops[1];
4131 create_input_operand (&ops[0], x, mode);
4132 if (maybe_expand_insn (icode, 1, ops))
4133 return;
4135 if (GET_MODE_SIZE (mode) == rounded_size)
4136 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4137 /* If we are to pad downward, adjust the stack pointer first and
4138 then store X into the stack location using an offset. This is
4139 because emit_move_insn does not know how to pad; it does not have
4140 access to type. */
4141 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4143 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4144 HOST_WIDE_INT offset;
4146 emit_move_insn (stack_pointer_rtx,
4147 expand_binop (Pmode,
4148 STACK_GROWS_DOWNWARD ? sub_optab
4149 : add_optab,
4150 stack_pointer_rtx,
4151 gen_int_mode (rounded_size, Pmode),
4152 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4154 offset = (HOST_WIDE_INT) padding_size;
4155 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4156 /* We have already decremented the stack pointer, so get the
4157 previous value. */
4158 offset += (HOST_WIDE_INT) rounded_size;
4160 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4161 /* We have already incremented the stack pointer, so get the
4162 previous value. */
4163 offset -= (HOST_WIDE_INT) rounded_size;
4165 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4166 gen_int_mode (offset, Pmode));
4168 else
4170 if (STACK_GROWS_DOWNWARD)
4171 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4172 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4173 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4174 Pmode));
4175 else
4176 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4177 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4178 gen_int_mode (rounded_size, Pmode));
4180 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4183 dest = gen_rtx_MEM (mode, dest_addr);
4185 if (type != 0)
4187 set_mem_attributes (dest, type, 1);
4189 if (cfun->tail_call_marked)
4190 /* Function incoming arguments may overlap with sibling call
4191 outgoing arguments and we cannot allow reordering of reads
4192 from function arguments with stores to outgoing arguments
4193 of sibling calls. */
4194 set_mem_alias_set (dest, 0);
4196 emit_move_insn (dest, x);
4199 /* Emit and annotate a single push insn. */
4201 static void
4202 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4204 int delta, old_delta = stack_pointer_delta;
4205 rtx_insn *prev = get_last_insn ();
4206 rtx_insn *last;
4208 emit_single_push_insn_1 (mode, x, type);
4210 last = get_last_insn ();
4212 /* Notice the common case where we emitted exactly one insn. */
4213 if (PREV_INSN (last) == prev)
4215 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4216 return;
4219 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4220 gcc_assert (delta == INT_MIN || delta == old_delta);
4222 #endif
4224 /* If reading SIZE bytes from X will end up reading from
4225 Y return the number of bytes that overlap. Return -1
4226 if there is no overlap or -2 if we can't determine
4227 (for example when X and Y have different base registers). */
4229 static int
4230 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4232 rtx tmp = plus_constant (Pmode, x, size);
4233 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4235 if (!CONST_INT_P (sub))
4236 return -2;
4238 HOST_WIDE_INT val = INTVAL (sub);
4240 return IN_RANGE (val, 1, size) ? val : -1;
4243 /* Generate code to push X onto the stack, assuming it has mode MODE and
4244 type TYPE.
4245 MODE is redundant except when X is a CONST_INT (since they don't
4246 carry mode info).
4247 SIZE is an rtx for the size of data to be copied (in bytes),
4248 needed only if X is BLKmode.
4249 Return true if successful. May return false if asked to push a
4250 partial argument during a sibcall optimization (as specified by
4251 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4252 to not overlap.
4254 ALIGN (in bits) is maximum alignment we can assume.
4256 If PARTIAL and REG are both nonzero, then copy that many of the first
4257 bytes of X into registers starting with REG, and push the rest of X.
4258 The amount of space pushed is decreased by PARTIAL bytes.
4259 REG must be a hard register in this case.
4260 If REG is zero but PARTIAL is not, take any all others actions for an
4261 argument partially in registers, but do not actually load any
4262 registers.
4264 EXTRA is the amount in bytes of extra space to leave next to this arg.
4265 This is ignored if an argument block has already been allocated.
4267 On a machine that lacks real push insns, ARGS_ADDR is the address of
4268 the bottom of the argument block for this call. We use indexing off there
4269 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4270 argument block has not been preallocated.
4272 ARGS_SO_FAR is the size of args previously pushed for this call.
4274 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4275 for arguments passed in registers. If nonzero, it will be the number
4276 of bytes required. */
4278 bool
4279 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4280 unsigned int align, int partial, rtx reg, int extra,
4281 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4282 rtx alignment_pad, bool sibcall_p)
4284 rtx xinner;
4285 pad_direction stack_direction
4286 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4288 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4289 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4290 Default is below for small data on big-endian machines; else above. */
4291 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4293 /* Invert direction if stack is post-decrement.
4294 FIXME: why? */
4295 if (STACK_PUSH_CODE == POST_DEC)
4296 if (where_pad != PAD_NONE)
4297 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4299 xinner = x;
4301 int nregs = partial / UNITS_PER_WORD;
4302 rtx *tmp_regs = NULL;
4303 int overlapping = 0;
4305 if (mode == BLKmode
4306 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4308 /* Copy a block into the stack, entirely or partially. */
4310 rtx temp;
4311 int used;
4312 int offset;
4313 int skip;
4315 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4316 used = partial - offset;
4318 if (mode != BLKmode)
4320 /* A value is to be stored in an insufficiently aligned
4321 stack slot; copy via a suitably aligned slot if
4322 necessary. */
4323 size = GEN_INT (GET_MODE_SIZE (mode));
4324 if (!MEM_P (xinner))
4326 temp = assign_temp (type, 1, 1);
4327 emit_move_insn (temp, xinner);
4328 xinner = temp;
4332 gcc_assert (size);
4334 /* USED is now the # of bytes we need not copy to the stack
4335 because registers will take care of them. */
4337 if (partial != 0)
4338 xinner = adjust_address (xinner, BLKmode, used);
4340 /* If the partial register-part of the arg counts in its stack size,
4341 skip the part of stack space corresponding to the registers.
4342 Otherwise, start copying to the beginning of the stack space,
4343 by setting SKIP to 0. */
4344 skip = (reg_parm_stack_space == 0) ? 0 : used;
4346 #ifdef PUSH_ROUNDING
4347 /* Do it with several push insns if that doesn't take lots of insns
4348 and if there is no difficulty with push insns that skip bytes
4349 on the stack for alignment purposes. */
4350 if (args_addr == 0
4351 && PUSH_ARGS
4352 && CONST_INT_P (size)
4353 && skip == 0
4354 && MEM_ALIGN (xinner) >= align
4355 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4356 /* Here we avoid the case of a structure whose weak alignment
4357 forces many pushes of a small amount of data,
4358 and such small pushes do rounding that causes trouble. */
4359 && ((!targetm.slow_unaligned_access (word_mode, align))
4360 || align >= BIGGEST_ALIGNMENT
4361 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4362 == (align / BITS_PER_UNIT)))
4363 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4365 /* Push padding now if padding above and stack grows down,
4366 or if padding below and stack grows up.
4367 But if space already allocated, this has already been done. */
4368 if (extra && args_addr == 0
4369 && where_pad != PAD_NONE && where_pad != stack_direction)
4370 anti_adjust_stack (GEN_INT (extra));
4372 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4374 else
4375 #endif /* PUSH_ROUNDING */
4377 rtx target;
4379 /* Otherwise make space on the stack and copy the data
4380 to the address of that space. */
4382 /* Deduct words put into registers from the size we must copy. */
4383 if (partial != 0)
4385 if (CONST_INT_P (size))
4386 size = GEN_INT (INTVAL (size) - used);
4387 else
4388 size = expand_binop (GET_MODE (size), sub_optab, size,
4389 gen_int_mode (used, GET_MODE (size)),
4390 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4393 /* Get the address of the stack space.
4394 In this case, we do not deal with EXTRA separately.
4395 A single stack adjust will do. */
4396 if (! args_addr)
4398 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4399 extra = 0;
4401 else if (CONST_INT_P (args_so_far))
4402 temp = memory_address (BLKmode,
4403 plus_constant (Pmode, args_addr,
4404 skip + INTVAL (args_so_far)));
4405 else
4406 temp = memory_address (BLKmode,
4407 plus_constant (Pmode,
4408 gen_rtx_PLUS (Pmode,
4409 args_addr,
4410 args_so_far),
4411 skip));
4413 if (!ACCUMULATE_OUTGOING_ARGS)
4415 /* If the source is referenced relative to the stack pointer,
4416 copy it to another register to stabilize it. We do not need
4417 to do this if we know that we won't be changing sp. */
4419 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4420 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4421 temp = copy_to_reg (temp);
4424 target = gen_rtx_MEM (BLKmode, temp);
4426 /* We do *not* set_mem_attributes here, because incoming arguments
4427 may overlap with sibling call outgoing arguments and we cannot
4428 allow reordering of reads from function arguments with stores
4429 to outgoing arguments of sibling calls. We do, however, want
4430 to record the alignment of the stack slot. */
4431 /* ALIGN may well be better aligned than TYPE, e.g. due to
4432 PARM_BOUNDARY. Assume the caller isn't lying. */
4433 set_mem_align (target, align);
4435 /* If part should go in registers and pushing to that part would
4436 overwrite some of the values that need to go into regs, load the
4437 overlapping values into temporary pseudos to be moved into the hard
4438 regs at the end after the stack pushing has completed.
4439 We cannot load them directly into the hard regs here because
4440 they can be clobbered by the block move expansions.
4441 See PR 65358. */
4443 if (partial > 0 && reg != 0 && mode == BLKmode
4444 && GET_CODE (reg) != PARALLEL)
4446 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4447 if (overlapping > 0)
4449 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4450 overlapping /= UNITS_PER_WORD;
4452 tmp_regs = XALLOCAVEC (rtx, overlapping);
4454 for (int i = 0; i < overlapping; i++)
4455 tmp_regs[i] = gen_reg_rtx (word_mode);
4457 for (int i = 0; i < overlapping; i++)
4458 emit_move_insn (tmp_regs[i],
4459 operand_subword_force (target, i, mode));
4461 else if (overlapping == -1)
4462 overlapping = 0;
4463 /* Could not determine whether there is overlap.
4464 Fail the sibcall. */
4465 else
4467 overlapping = 0;
4468 if (sibcall_p)
4469 return false;
4472 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4475 else if (partial > 0)
4477 /* Scalar partly in registers. */
4479 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4480 int i;
4481 int not_stack;
4482 /* # bytes of start of argument
4483 that we must make space for but need not store. */
4484 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4485 int args_offset = INTVAL (args_so_far);
4486 int skip;
4488 /* Push padding now if padding above and stack grows down,
4489 or if padding below and stack grows up.
4490 But if space already allocated, this has already been done. */
4491 if (extra && args_addr == 0
4492 && where_pad != PAD_NONE && where_pad != stack_direction)
4493 anti_adjust_stack (GEN_INT (extra));
4495 /* If we make space by pushing it, we might as well push
4496 the real data. Otherwise, we can leave OFFSET nonzero
4497 and leave the space uninitialized. */
4498 if (args_addr == 0)
4499 offset = 0;
4501 /* Now NOT_STACK gets the number of words that we don't need to
4502 allocate on the stack. Convert OFFSET to words too. */
4503 not_stack = (partial - offset) / UNITS_PER_WORD;
4504 offset /= UNITS_PER_WORD;
4506 /* If the partial register-part of the arg counts in its stack size,
4507 skip the part of stack space corresponding to the registers.
4508 Otherwise, start copying to the beginning of the stack space,
4509 by setting SKIP to 0. */
4510 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4512 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4513 x = validize_mem (force_const_mem (mode, x));
4515 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4516 SUBREGs of such registers are not allowed. */
4517 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4518 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4519 x = copy_to_reg (x);
4521 /* Loop over all the words allocated on the stack for this arg. */
4522 /* We can do it by words, because any scalar bigger than a word
4523 has a size a multiple of a word. */
4524 for (i = size - 1; i >= not_stack; i--)
4525 if (i >= not_stack + offset)
4526 if (!emit_push_insn (operand_subword_force (x, i, mode),
4527 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4528 0, args_addr,
4529 GEN_INT (args_offset + ((i - not_stack + skip)
4530 * UNITS_PER_WORD)),
4531 reg_parm_stack_space, alignment_pad, sibcall_p))
4532 return false;
4534 else
4536 rtx addr;
4537 rtx dest;
4539 /* Push padding now if padding above and stack grows down,
4540 or if padding below and stack grows up.
4541 But if space already allocated, this has already been done. */
4542 if (extra && args_addr == 0
4543 && where_pad != PAD_NONE && where_pad != stack_direction)
4544 anti_adjust_stack (GEN_INT (extra));
4546 #ifdef PUSH_ROUNDING
4547 if (args_addr == 0 && PUSH_ARGS)
4548 emit_single_push_insn (mode, x, type);
4549 else
4550 #endif
4552 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4553 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4555 /* We do *not* set_mem_attributes here, because incoming arguments
4556 may overlap with sibling call outgoing arguments and we cannot
4557 allow reordering of reads from function arguments with stores
4558 to outgoing arguments of sibling calls. We do, however, want
4559 to record the alignment of the stack slot. */
4560 /* ALIGN may well be better aligned than TYPE, e.g. due to
4561 PARM_BOUNDARY. Assume the caller isn't lying. */
4562 set_mem_align (dest, align);
4564 emit_move_insn (dest, x);
4568 /* Move the partial arguments into the registers and any overlapping
4569 values that we moved into the pseudos in tmp_regs. */
4570 if (partial > 0 && reg != 0)
4572 /* Handle calls that pass values in multiple non-contiguous locations.
4573 The Irix 6 ABI has examples of this. */
4574 if (GET_CODE (reg) == PARALLEL)
4575 emit_group_load (reg, x, type, -1);
4576 else
4578 gcc_assert (partial % UNITS_PER_WORD == 0);
4579 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4581 for (int i = 0; i < overlapping; i++)
4582 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4583 + nregs - overlapping + i),
4584 tmp_regs[i]);
4589 if (extra && args_addr == 0 && where_pad == stack_direction)
4590 anti_adjust_stack (GEN_INT (extra));
4592 if (alignment_pad && args_addr == 0)
4593 anti_adjust_stack (alignment_pad);
4595 return true;
4598 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4599 operations. */
4601 static rtx
4602 get_subtarget (rtx x)
4604 return (optimize
4605 || x == 0
4606 /* Only registers can be subtargets. */
4607 || !REG_P (x)
4608 /* Don't use hard regs to avoid extending their life. */
4609 || REGNO (x) < FIRST_PSEUDO_REGISTER
4610 ? 0 : x);
4613 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4614 FIELD is a bitfield. Returns true if the optimization was successful,
4615 and there's nothing else to do. */
4617 static bool
4618 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4619 unsigned HOST_WIDE_INT bitpos,
4620 unsigned HOST_WIDE_INT bitregion_start,
4621 unsigned HOST_WIDE_INT bitregion_end,
4622 machine_mode mode1, rtx str_rtx,
4623 tree to, tree src, bool reverse)
4625 machine_mode str_mode = GET_MODE (str_rtx);
4626 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4627 tree op0, op1;
4628 rtx value, result;
4629 optab binop;
4630 gimple *srcstmt;
4631 enum tree_code code;
4633 if (mode1 != VOIDmode
4634 || bitsize >= BITS_PER_WORD
4635 || str_bitsize > BITS_PER_WORD
4636 || TREE_SIDE_EFFECTS (to)
4637 || TREE_THIS_VOLATILE (to))
4638 return false;
4640 STRIP_NOPS (src);
4641 if (TREE_CODE (src) != SSA_NAME)
4642 return false;
4643 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4644 return false;
4646 srcstmt = get_gimple_for_ssa_name (src);
4647 if (!srcstmt
4648 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4649 return false;
4651 code = gimple_assign_rhs_code (srcstmt);
4653 op0 = gimple_assign_rhs1 (srcstmt);
4655 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4656 to find its initialization. Hopefully the initialization will
4657 be from a bitfield load. */
4658 if (TREE_CODE (op0) == SSA_NAME)
4660 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4662 /* We want to eventually have OP0 be the same as TO, which
4663 should be a bitfield. */
4664 if (!op0stmt
4665 || !is_gimple_assign (op0stmt)
4666 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4667 return false;
4668 op0 = gimple_assign_rhs1 (op0stmt);
4671 op1 = gimple_assign_rhs2 (srcstmt);
4673 if (!operand_equal_p (to, op0, 0))
4674 return false;
4676 if (MEM_P (str_rtx))
4678 unsigned HOST_WIDE_INT offset1;
4680 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4681 str_bitsize = BITS_PER_WORD;
4683 scalar_int_mode best_mode;
4684 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4685 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4686 return false;
4687 str_mode = best_mode;
4688 str_bitsize = GET_MODE_BITSIZE (best_mode);
4690 offset1 = bitpos;
4691 bitpos %= str_bitsize;
4692 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4693 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4695 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4696 return false;
4697 else
4698 gcc_assert (!reverse);
4700 /* If the bit field covers the whole REG/MEM, store_field
4701 will likely generate better code. */
4702 if (bitsize >= str_bitsize)
4703 return false;
4705 /* We can't handle fields split across multiple entities. */
4706 if (bitpos + bitsize > str_bitsize)
4707 return false;
4709 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4710 bitpos = str_bitsize - bitpos - bitsize;
4712 switch (code)
4714 case PLUS_EXPR:
4715 case MINUS_EXPR:
4716 /* For now, just optimize the case of the topmost bitfield
4717 where we don't need to do any masking and also
4718 1 bit bitfields where xor can be used.
4719 We might win by one instruction for the other bitfields
4720 too if insv/extv instructions aren't used, so that
4721 can be added later. */
4722 if ((reverse || bitpos + bitsize != str_bitsize)
4723 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4724 break;
4726 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4727 value = convert_modes (str_mode,
4728 TYPE_MODE (TREE_TYPE (op1)), value,
4729 TYPE_UNSIGNED (TREE_TYPE (op1)));
4731 /* We may be accessing data outside the field, which means
4732 we can alias adjacent data. */
4733 if (MEM_P (str_rtx))
4735 str_rtx = shallow_copy_rtx (str_rtx);
4736 set_mem_alias_set (str_rtx, 0);
4737 set_mem_expr (str_rtx, 0);
4740 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4742 value = expand_and (str_mode, value, const1_rtx, NULL);
4743 binop = xor_optab;
4745 else
4746 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4748 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4749 if (reverse)
4750 value = flip_storage_order (str_mode, value);
4751 result = expand_binop (str_mode, binop, str_rtx,
4752 value, str_rtx, 1, OPTAB_WIDEN);
4753 if (result != str_rtx)
4754 emit_move_insn (str_rtx, result);
4755 return true;
4757 case BIT_IOR_EXPR:
4758 case BIT_XOR_EXPR:
4759 if (TREE_CODE (op1) != INTEGER_CST)
4760 break;
4761 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4762 value = convert_modes (str_mode,
4763 TYPE_MODE (TREE_TYPE (op1)), value,
4764 TYPE_UNSIGNED (TREE_TYPE (op1)));
4766 /* We may be accessing data outside the field, which means
4767 we can alias adjacent data. */
4768 if (MEM_P (str_rtx))
4770 str_rtx = shallow_copy_rtx (str_rtx);
4771 set_mem_alias_set (str_rtx, 0);
4772 set_mem_expr (str_rtx, 0);
4775 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4776 if (bitpos + bitsize != str_bitsize)
4778 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4779 str_mode);
4780 value = expand_and (str_mode, value, mask, NULL_RTX);
4782 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4783 if (reverse)
4784 value = flip_storage_order (str_mode, value);
4785 result = expand_binop (str_mode, binop, str_rtx,
4786 value, str_rtx, 1, OPTAB_WIDEN);
4787 if (result != str_rtx)
4788 emit_move_insn (str_rtx, result);
4789 return true;
4791 default:
4792 break;
4795 return false;
4798 /* In the C++ memory model, consecutive bit fields in a structure are
4799 considered one memory location.
4801 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4802 returns the bit range of consecutive bits in which this COMPONENT_REF
4803 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4804 and *OFFSET may be adjusted in the process.
4806 If the access does not need to be restricted, 0 is returned in both
4807 *BITSTART and *BITEND. */
4809 void
4810 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4811 unsigned HOST_WIDE_INT *bitend,
4812 tree exp,
4813 HOST_WIDE_INT *bitpos,
4814 tree *offset)
4816 HOST_WIDE_INT bitoffset;
4817 tree field, repr;
4819 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4821 field = TREE_OPERAND (exp, 1);
4822 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4823 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4824 need to limit the range we can access. */
4825 if (!repr)
4827 *bitstart = *bitend = 0;
4828 return;
4831 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4832 part of a larger bit field, then the representative does not serve any
4833 useful purpose. This can occur in Ada. */
4834 if (handled_component_p (TREE_OPERAND (exp, 0)))
4836 machine_mode rmode;
4837 HOST_WIDE_INT rbitsize, rbitpos;
4838 tree roffset;
4839 int unsignedp, reversep, volatilep = 0;
4840 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4841 &roffset, &rmode, &unsignedp, &reversep,
4842 &volatilep);
4843 if ((rbitpos % BITS_PER_UNIT) != 0)
4845 *bitstart = *bitend = 0;
4846 return;
4850 /* Compute the adjustment to bitpos from the offset of the field
4851 relative to the representative. DECL_FIELD_OFFSET of field and
4852 repr are the same by construction if they are not constants,
4853 see finish_bitfield_layout. */
4854 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4855 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4856 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4857 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4858 else
4859 bitoffset = 0;
4860 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4861 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4863 /* If the adjustment is larger than bitpos, we would have a negative bit
4864 position for the lower bound and this may wreak havoc later. Adjust
4865 offset and bitpos to make the lower bound non-negative in that case. */
4866 if (bitoffset > *bitpos)
4868 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4869 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4871 *bitpos += adjust;
4872 if (*offset == NULL_TREE)
4873 *offset = size_int (-adjust / BITS_PER_UNIT);
4874 else
4875 *offset
4876 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4877 *bitstart = 0;
4879 else
4880 *bitstart = *bitpos - bitoffset;
4882 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4885 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4886 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4887 DECL_RTL was not set yet, return NORTL. */
4889 static inline bool
4890 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4892 if (TREE_CODE (addr) != ADDR_EXPR)
4893 return false;
4895 tree base = TREE_OPERAND (addr, 0);
4897 if (!DECL_P (base)
4898 || TREE_ADDRESSABLE (base)
4899 || DECL_MODE (base) == BLKmode)
4900 return false;
4902 if (!DECL_RTL_SET_P (base))
4903 return nortl;
4905 return (!MEM_P (DECL_RTL (base)));
4908 /* Returns true if the MEM_REF REF refers to an object that does not
4909 reside in memory and has non-BLKmode. */
4911 static inline bool
4912 mem_ref_refers_to_non_mem_p (tree ref)
4914 tree base = TREE_OPERAND (ref, 0);
4915 return addr_expr_of_non_mem_decl_p_1 (base, false);
4918 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4919 is true, try generating a nontemporal store. */
4921 void
4922 expand_assignment (tree to, tree from, bool nontemporal)
4924 rtx to_rtx = 0;
4925 rtx result;
4926 machine_mode mode;
4927 unsigned int align;
4928 enum insn_code icode;
4930 /* Don't crash if the lhs of the assignment was erroneous. */
4931 if (TREE_CODE (to) == ERROR_MARK)
4933 expand_normal (from);
4934 return;
4937 /* Optimize away no-op moves without side-effects. */
4938 if (operand_equal_p (to, from, 0))
4939 return;
4941 /* Handle misaligned stores. */
4942 mode = TYPE_MODE (TREE_TYPE (to));
4943 if ((TREE_CODE (to) == MEM_REF
4944 || TREE_CODE (to) == TARGET_MEM_REF)
4945 && mode != BLKmode
4946 && !mem_ref_refers_to_non_mem_p (to)
4947 && ((align = get_object_alignment (to))
4948 < GET_MODE_ALIGNMENT (mode))
4949 && (((icode = optab_handler (movmisalign_optab, mode))
4950 != CODE_FOR_nothing)
4951 || targetm.slow_unaligned_access (mode, align)))
4953 rtx reg, mem;
4955 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4956 reg = force_not_mem (reg);
4957 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4958 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4959 reg = flip_storage_order (mode, reg);
4961 if (icode != CODE_FOR_nothing)
4963 struct expand_operand ops[2];
4965 create_fixed_operand (&ops[0], mem);
4966 create_input_operand (&ops[1], reg, mode);
4967 /* The movmisalign<mode> pattern cannot fail, else the assignment
4968 would silently be omitted. */
4969 expand_insn (icode, 2, ops);
4971 else
4972 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4973 false);
4974 return;
4977 /* Assignment of a structure component needs special treatment
4978 if the structure component's rtx is not simply a MEM.
4979 Assignment of an array element at a constant index, and assignment of
4980 an array element in an unaligned packed structure field, has the same
4981 problem. Same for (partially) storing into a non-memory object. */
4982 if (handled_component_p (to)
4983 || (TREE_CODE (to) == MEM_REF
4984 && (REF_REVERSE_STORAGE_ORDER (to)
4985 || mem_ref_refers_to_non_mem_p (to)))
4986 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4988 machine_mode mode1;
4989 HOST_WIDE_INT bitsize, bitpos;
4990 unsigned HOST_WIDE_INT bitregion_start = 0;
4991 unsigned HOST_WIDE_INT bitregion_end = 0;
4992 tree offset;
4993 int unsignedp, reversep, volatilep = 0;
4994 tree tem;
4996 push_temp_slots ();
4997 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4998 &unsignedp, &reversep, &volatilep);
5000 /* Make sure bitpos is not negative, it can wreak havoc later. */
5001 if (bitpos < 0)
5003 gcc_assert (offset == NULL_TREE);
5004 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
5005 bitpos &= BITS_PER_UNIT - 1;
5008 if (TREE_CODE (to) == COMPONENT_REF
5009 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5010 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5011 /* The C++ memory model naturally applies to byte-aligned fields.
5012 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5013 BITSIZE are not byte-aligned, there is no need to limit the range
5014 we can access. This can occur with packed structures in Ada. */
5015 else if (bitsize > 0
5016 && bitsize % BITS_PER_UNIT == 0
5017 && bitpos % BITS_PER_UNIT == 0)
5019 bitregion_start = bitpos;
5020 bitregion_end = bitpos + bitsize - 1;
5023 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5025 /* If the field has a mode, we want to access it in the
5026 field's mode, not the computed mode.
5027 If a MEM has VOIDmode (external with incomplete type),
5028 use BLKmode for it instead. */
5029 if (MEM_P (to_rtx))
5031 if (mode1 != VOIDmode)
5032 to_rtx = adjust_address (to_rtx, mode1, 0);
5033 else if (GET_MODE (to_rtx) == VOIDmode)
5034 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5037 if (offset != 0)
5039 machine_mode address_mode;
5040 rtx offset_rtx;
5042 if (!MEM_P (to_rtx))
5044 /* We can get constant negative offsets into arrays with broken
5045 user code. Translate this to a trap instead of ICEing. */
5046 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5047 expand_builtin_trap ();
5048 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5051 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5052 address_mode = get_address_mode (to_rtx);
5053 if (GET_MODE (offset_rtx) != address_mode)
5055 /* We cannot be sure that the RTL in offset_rtx is valid outside
5056 of a memory address context, so force it into a register
5057 before attempting to convert it to the desired mode. */
5058 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5059 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5062 /* If we have an expression in OFFSET_RTX and a non-zero
5063 byte offset in BITPOS, adding the byte offset before the
5064 OFFSET_RTX results in better intermediate code, which makes
5065 later rtl optimization passes perform better.
5067 We prefer intermediate code like this:
5069 r124:DI=r123:DI+0x18
5070 [r124:DI]=r121:DI
5072 ... instead of ...
5074 r124:DI=r123:DI+0x10
5075 [r124:DI+0x8]=r121:DI
5077 This is only done for aligned data values, as these can
5078 be expected to result in single move instructions. */
5079 if (mode1 != VOIDmode
5080 && bitpos != 0
5081 && bitsize > 0
5082 && (bitpos % bitsize) == 0
5083 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5084 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5086 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5087 bitregion_start = 0;
5088 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5089 bitregion_end -= bitpos;
5090 bitpos = 0;
5093 to_rtx = offset_address (to_rtx, offset_rtx,
5094 highest_pow2_factor_for_target (to,
5095 offset));
5098 /* No action is needed if the target is not a memory and the field
5099 lies completely outside that target. This can occur if the source
5100 code contains an out-of-bounds access to a small array. */
5101 if (!MEM_P (to_rtx)
5102 && GET_MODE (to_rtx) != BLKmode
5103 && (unsigned HOST_WIDE_INT) bitpos
5104 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5106 expand_normal (from);
5107 result = NULL;
5109 /* Handle expand_expr of a complex value returning a CONCAT. */
5110 else if (GET_CODE (to_rtx) == CONCAT)
5112 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5113 if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE (to_rtx)
5114 && COMPLEX_MODE_P (GET_MODE (to_rtx))
5115 && bitpos == 0
5116 && bitsize == mode_bitsize)
5117 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5118 else if (bitsize == mode_bitsize / 2
5119 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5120 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5121 nontemporal, reversep);
5122 else if (bitpos + bitsize <= mode_bitsize / 2)
5123 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5124 bitregion_start, bitregion_end,
5125 mode1, from, get_alias_set (to),
5126 nontemporal, reversep);
5127 else if (bitpos >= mode_bitsize / 2)
5128 result = store_field (XEXP (to_rtx, 1), bitsize,
5129 bitpos - mode_bitsize / 2,
5130 bitregion_start, bitregion_end,
5131 mode1, from, get_alias_set (to),
5132 nontemporal, reversep);
5133 else if (bitpos == 0 && bitsize == mode_bitsize)
5135 result = expand_normal (from);
5136 if (GET_CODE (result) == CONCAT)
5138 machine_mode to_mode = GET_MODE_INNER (GET_MODE (to_rtx));
5139 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5140 rtx from_real
5141 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5142 from_mode, 0);
5143 rtx from_imag
5144 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5145 from_mode, 1);
5146 emit_move_insn (XEXP (to_rtx, 0), from_real);
5147 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5149 else
5151 rtx from_rtx
5152 = simplify_gen_subreg (GET_MODE (to_rtx), result,
5153 TYPE_MODE (TREE_TYPE (from)), 0);
5154 emit_move_insn (XEXP (to_rtx, 0),
5155 read_complex_part (from_rtx, false));
5156 emit_move_insn (XEXP (to_rtx, 1),
5157 read_complex_part (from_rtx, true));
5160 else
5162 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5163 GET_MODE_SIZE (GET_MODE (to_rtx)));
5164 write_complex_part (temp, XEXP (to_rtx, 0), false);
5165 write_complex_part (temp, XEXP (to_rtx, 1), true);
5166 result = store_field (temp, bitsize, bitpos,
5167 bitregion_start, bitregion_end,
5168 mode1, from, get_alias_set (to),
5169 nontemporal, reversep);
5170 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5171 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5174 else
5176 if (MEM_P (to_rtx))
5178 /* If the field is at offset zero, we could have been given the
5179 DECL_RTX of the parent struct. Don't munge it. */
5180 to_rtx = shallow_copy_rtx (to_rtx);
5181 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5182 if (volatilep)
5183 MEM_VOLATILE_P (to_rtx) = 1;
5186 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5187 bitregion_start, bitregion_end,
5188 mode1, to_rtx, to, from,
5189 reversep))
5190 result = NULL;
5191 else
5192 result = store_field (to_rtx, bitsize, bitpos,
5193 bitregion_start, bitregion_end,
5194 mode1, from, get_alias_set (to),
5195 nontemporal, reversep);
5198 if (result)
5199 preserve_temp_slots (result);
5200 pop_temp_slots ();
5201 return;
5204 /* If the rhs is a function call and its value is not an aggregate,
5205 call the function before we start to compute the lhs.
5206 This is needed for correct code for cases such as
5207 val = setjmp (buf) on machines where reference to val
5208 requires loading up part of an address in a separate insn.
5210 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5211 since it might be a promoted variable where the zero- or sign- extension
5212 needs to be done. Handling this in the normal way is safe because no
5213 computation is done before the call. The same is true for SSA names. */
5214 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5215 && COMPLETE_TYPE_P (TREE_TYPE (from))
5216 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5217 && ! (((VAR_P (to)
5218 || TREE_CODE (to) == PARM_DECL
5219 || TREE_CODE (to) == RESULT_DECL)
5220 && REG_P (DECL_RTL (to)))
5221 || TREE_CODE (to) == SSA_NAME))
5223 rtx value;
5224 rtx bounds;
5226 push_temp_slots ();
5227 value = expand_normal (from);
5229 /* Split value and bounds to store them separately. */
5230 chkp_split_slot (value, &value, &bounds);
5232 if (to_rtx == 0)
5233 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5235 /* Handle calls that return values in multiple non-contiguous locations.
5236 The Irix 6 ABI has examples of this. */
5237 if (GET_CODE (to_rtx) == PARALLEL)
5239 if (GET_CODE (value) == PARALLEL)
5240 emit_group_move (to_rtx, value);
5241 else
5242 emit_group_load (to_rtx, value, TREE_TYPE (from),
5243 int_size_in_bytes (TREE_TYPE (from)));
5245 else if (GET_CODE (value) == PARALLEL)
5246 emit_group_store (to_rtx, value, TREE_TYPE (from),
5247 int_size_in_bytes (TREE_TYPE (from)));
5248 else if (GET_MODE (to_rtx) == BLKmode)
5250 /* Handle calls that return BLKmode values in registers. */
5251 if (REG_P (value))
5252 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5253 else
5254 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5256 else
5258 if (POINTER_TYPE_P (TREE_TYPE (to)))
5259 value = convert_memory_address_addr_space
5260 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5261 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5263 emit_move_insn (to_rtx, value);
5266 /* Store bounds if required. */
5267 if (bounds
5268 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5270 gcc_assert (MEM_P (to_rtx));
5271 chkp_emit_bounds_store (bounds, value, to_rtx);
5274 preserve_temp_slots (to_rtx);
5275 pop_temp_slots ();
5276 return;
5279 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5280 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5282 /* Don't move directly into a return register. */
5283 if (TREE_CODE (to) == RESULT_DECL
5284 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5286 rtx temp;
5288 push_temp_slots ();
5290 /* If the source is itself a return value, it still is in a pseudo at
5291 this point so we can move it back to the return register directly. */
5292 if (REG_P (to_rtx)
5293 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5294 && TREE_CODE (from) != CALL_EXPR)
5295 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5296 else
5297 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5299 /* Handle calls that return values in multiple non-contiguous locations.
5300 The Irix 6 ABI has examples of this. */
5301 if (GET_CODE (to_rtx) == PARALLEL)
5303 if (GET_CODE (temp) == PARALLEL)
5304 emit_group_move (to_rtx, temp);
5305 else
5306 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5307 int_size_in_bytes (TREE_TYPE (from)));
5309 else if (temp)
5310 emit_move_insn (to_rtx, temp);
5312 preserve_temp_slots (to_rtx);
5313 pop_temp_slots ();
5314 return;
5317 /* In case we are returning the contents of an object which overlaps
5318 the place the value is being stored, use a safe function when copying
5319 a value through a pointer into a structure value return block. */
5320 if (TREE_CODE (to) == RESULT_DECL
5321 && TREE_CODE (from) == INDIRECT_REF
5322 && ADDR_SPACE_GENERIC_P
5323 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5324 && refs_may_alias_p (to, from)
5325 && cfun->returns_struct
5326 && !cfun->returns_pcc_struct)
5328 rtx from_rtx, size;
5330 push_temp_slots ();
5331 size = expr_size (from);
5332 from_rtx = expand_normal (from);
5334 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5336 preserve_temp_slots (to_rtx);
5337 pop_temp_slots ();
5338 return;
5341 /* Compute FROM and store the value in the rtx we got. */
5343 push_temp_slots ();
5344 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5345 preserve_temp_slots (result);
5346 pop_temp_slots ();
5347 return;
5350 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5351 succeeded, false otherwise. */
5353 bool
5354 emit_storent_insn (rtx to, rtx from)
5356 struct expand_operand ops[2];
5357 machine_mode mode = GET_MODE (to);
5358 enum insn_code code = optab_handler (storent_optab, mode);
5360 if (code == CODE_FOR_nothing)
5361 return false;
5363 create_fixed_operand (&ops[0], to);
5364 create_input_operand (&ops[1], from, mode);
5365 return maybe_expand_insn (code, 2, ops);
5368 /* Generate code for computing expression EXP,
5369 and storing the value into TARGET.
5371 If the mode is BLKmode then we may return TARGET itself.
5372 It turns out that in BLKmode it doesn't cause a problem.
5373 because C has no operators that could combine two different
5374 assignments into the same BLKmode object with different values
5375 with no sequence point. Will other languages need this to
5376 be more thorough?
5378 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5379 stack, and block moves may need to be treated specially.
5381 If NONTEMPORAL is true, try using a nontemporal store instruction.
5383 If REVERSE is true, the store is to be done in reverse order.
5385 If BTARGET is not NULL then computed bounds of EXP are
5386 associated with BTARGET. */
5389 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5390 bool nontemporal, bool reverse, tree btarget)
5392 rtx temp;
5393 rtx alt_rtl = NULL_RTX;
5394 location_t loc = curr_insn_location ();
5396 if (VOID_TYPE_P (TREE_TYPE (exp)))
5398 /* C++ can generate ?: expressions with a throw expression in one
5399 branch and an rvalue in the other. Here, we resolve attempts to
5400 store the throw expression's nonexistent result. */
5401 gcc_assert (!call_param_p);
5402 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5403 return NULL_RTX;
5405 if (TREE_CODE (exp) == COMPOUND_EXPR)
5407 /* Perform first part of compound expression, then assign from second
5408 part. */
5409 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5410 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5411 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5412 call_param_p, nontemporal, reverse,
5413 btarget);
5415 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5417 /* For conditional expression, get safe form of the target. Then
5418 test the condition, doing the appropriate assignment on either
5419 side. This avoids the creation of unnecessary temporaries.
5420 For non-BLKmode, it is more efficient not to do this. */
5422 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5424 do_pending_stack_adjust ();
5425 NO_DEFER_POP;
5426 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5427 profile_probability::uninitialized ());
5428 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5429 nontemporal, reverse, btarget);
5430 emit_jump_insn (targetm.gen_jump (lab2));
5431 emit_barrier ();
5432 emit_label (lab1);
5433 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5434 nontemporal, reverse, btarget);
5435 emit_label (lab2);
5436 OK_DEFER_POP;
5438 return NULL_RTX;
5440 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5441 /* If this is a scalar in a register that is stored in a wider mode
5442 than the declared mode, compute the result into its declared mode
5443 and then convert to the wider mode. Our value is the computed
5444 expression. */
5446 rtx inner_target = 0;
5447 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5448 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5450 /* We can do the conversion inside EXP, which will often result
5451 in some optimizations. Do the conversion in two steps: first
5452 change the signedness, if needed, then the extend. But don't
5453 do this if the type of EXP is a subtype of something else
5454 since then the conversion might involve more than just
5455 converting modes. */
5456 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5457 && TREE_TYPE (TREE_TYPE (exp)) == 0
5458 && GET_MODE_PRECISION (outer_mode)
5459 == TYPE_PRECISION (TREE_TYPE (exp)))
5461 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5462 TYPE_UNSIGNED (TREE_TYPE (exp))))
5464 /* Some types, e.g. Fortran's logical*4, won't have a signed
5465 version, so use the mode instead. */
5466 tree ntype
5467 = (signed_or_unsigned_type_for
5468 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5469 if (ntype == NULL)
5470 ntype = lang_hooks.types.type_for_mode
5471 (TYPE_MODE (TREE_TYPE (exp)),
5472 SUBREG_PROMOTED_SIGN (target));
5474 exp = fold_convert_loc (loc, ntype, exp);
5477 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5478 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5479 exp);
5481 inner_target = SUBREG_REG (target);
5484 temp = expand_expr (exp, inner_target, VOIDmode,
5485 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5487 /* Handle bounds returned by call. */
5488 if (TREE_CODE (exp) == CALL_EXPR)
5490 rtx bounds;
5491 chkp_split_slot (temp, &temp, &bounds);
5492 if (bounds && btarget)
5494 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5495 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5496 chkp_set_rtl_bounds (btarget, tmp);
5500 /* If TEMP is a VOIDmode constant, use convert_modes to make
5501 sure that we properly convert it. */
5502 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5504 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5505 temp, SUBREG_PROMOTED_SIGN (target));
5506 temp = convert_modes (inner_mode, outer_mode, temp,
5507 SUBREG_PROMOTED_SIGN (target));
5510 convert_move (SUBREG_REG (target), temp,
5511 SUBREG_PROMOTED_SIGN (target));
5513 return NULL_RTX;
5515 else if ((TREE_CODE (exp) == STRING_CST
5516 || (TREE_CODE (exp) == MEM_REF
5517 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5518 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5519 == STRING_CST
5520 && integer_zerop (TREE_OPERAND (exp, 1))))
5521 && !nontemporal && !call_param_p
5522 && MEM_P (target))
5524 /* Optimize initialization of an array with a STRING_CST. */
5525 HOST_WIDE_INT exp_len, str_copy_len;
5526 rtx dest_mem;
5527 tree str = TREE_CODE (exp) == STRING_CST
5528 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5530 exp_len = int_expr_size (exp);
5531 if (exp_len <= 0)
5532 goto normal_expr;
5534 if (TREE_STRING_LENGTH (str) <= 0)
5535 goto normal_expr;
5537 str_copy_len = strlen (TREE_STRING_POINTER (str));
5538 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5539 goto normal_expr;
5541 str_copy_len = TREE_STRING_LENGTH (str);
5542 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5543 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5545 str_copy_len += STORE_MAX_PIECES - 1;
5546 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5548 str_copy_len = MIN (str_copy_len, exp_len);
5549 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5550 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5551 MEM_ALIGN (target), false))
5552 goto normal_expr;
5554 dest_mem = target;
5556 dest_mem = store_by_pieces (dest_mem,
5557 str_copy_len, builtin_strncpy_read_str,
5558 CONST_CAST (char *,
5559 TREE_STRING_POINTER (str)),
5560 MEM_ALIGN (target), false,
5561 exp_len > str_copy_len ? 1 : 0);
5562 if (exp_len > str_copy_len)
5563 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5564 GEN_INT (exp_len - str_copy_len),
5565 BLOCK_OP_NORMAL);
5566 return NULL_RTX;
5568 else
5570 rtx tmp_target;
5572 normal_expr:
5573 /* If we want to use a nontemporal or a reverse order store, force the
5574 value into a register first. */
5575 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5576 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5577 (call_param_p
5578 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5579 &alt_rtl, false);
5581 /* Handle bounds returned by call. */
5582 if (TREE_CODE (exp) == CALL_EXPR)
5584 rtx bounds;
5585 chkp_split_slot (temp, &temp, &bounds);
5586 if (bounds && btarget)
5588 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5589 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5590 chkp_set_rtl_bounds (btarget, tmp);
5595 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5596 the same as that of TARGET, adjust the constant. This is needed, for
5597 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5598 only a word-sized value. */
5599 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5600 && TREE_CODE (exp) != ERROR_MARK
5601 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5602 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5603 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5605 /* If value was not generated in the target, store it there.
5606 Convert the value to TARGET's type first if necessary and emit the
5607 pending incrementations that have been queued when expanding EXP.
5608 Note that we cannot emit the whole queue blindly because this will
5609 effectively disable the POST_INC optimization later.
5611 If TEMP and TARGET compare equal according to rtx_equal_p, but
5612 one or both of them are volatile memory refs, we have to distinguish
5613 two cases:
5614 - expand_expr has used TARGET. In this case, we must not generate
5615 another copy. This can be detected by TARGET being equal according
5616 to == .
5617 - expand_expr has not used TARGET - that means that the source just
5618 happens to have the same RTX form. Since temp will have been created
5619 by expand_expr, it will compare unequal according to == .
5620 We must generate a copy in this case, to reach the correct number
5621 of volatile memory references. */
5623 if ((! rtx_equal_p (temp, target)
5624 || (temp != target && (side_effects_p (temp)
5625 || side_effects_p (target))))
5626 && TREE_CODE (exp) != ERROR_MARK
5627 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5628 but TARGET is not valid memory reference, TEMP will differ
5629 from TARGET although it is really the same location. */
5630 && !(alt_rtl
5631 && rtx_equal_p (alt_rtl, target)
5632 && !side_effects_p (alt_rtl)
5633 && !side_effects_p (target))
5634 /* If there's nothing to copy, don't bother. Don't call
5635 expr_size unless necessary, because some front-ends (C++)
5636 expr_size-hook must not be given objects that are not
5637 supposed to be bit-copied or bit-initialized. */
5638 && expr_size (exp) != const0_rtx)
5640 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5642 if (GET_MODE (target) == BLKmode)
5644 /* Handle calls that return BLKmode values in registers. */
5645 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5646 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5647 else
5648 store_bit_field (target,
5649 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5650 0, 0, 0, GET_MODE (temp), temp, reverse);
5652 else
5653 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5656 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5658 /* Handle copying a string constant into an array. The string
5659 constant may be shorter than the array. So copy just the string's
5660 actual length, and clear the rest. First get the size of the data
5661 type of the string, which is actually the size of the target. */
5662 rtx size = expr_size (exp);
5664 if (CONST_INT_P (size)
5665 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5666 emit_block_move (target, temp, size,
5667 (call_param_p
5668 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5669 else
5671 machine_mode pointer_mode
5672 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5673 machine_mode address_mode = get_address_mode (target);
5675 /* Compute the size of the data to copy from the string. */
5676 tree copy_size
5677 = size_binop_loc (loc, MIN_EXPR,
5678 make_tree (sizetype, size),
5679 size_int (TREE_STRING_LENGTH (exp)));
5680 rtx copy_size_rtx
5681 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5682 (call_param_p
5683 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5684 rtx_code_label *label = 0;
5686 /* Copy that much. */
5687 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5688 TYPE_UNSIGNED (sizetype));
5689 emit_block_move (target, temp, copy_size_rtx,
5690 (call_param_p
5691 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5693 /* Figure out how much is left in TARGET that we have to clear.
5694 Do all calculations in pointer_mode. */
5695 if (CONST_INT_P (copy_size_rtx))
5697 size = plus_constant (address_mode, size,
5698 -INTVAL (copy_size_rtx));
5699 target = adjust_address (target, BLKmode,
5700 INTVAL (copy_size_rtx));
5702 else
5704 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5705 copy_size_rtx, NULL_RTX, 0,
5706 OPTAB_LIB_WIDEN);
5708 if (GET_MODE (copy_size_rtx) != address_mode)
5709 copy_size_rtx = convert_to_mode (address_mode,
5710 copy_size_rtx,
5711 TYPE_UNSIGNED (sizetype));
5713 target = offset_address (target, copy_size_rtx,
5714 highest_pow2_factor (copy_size));
5715 label = gen_label_rtx ();
5716 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5717 GET_MODE (size), 0, label);
5720 if (size != const0_rtx)
5721 clear_storage (target, size, BLOCK_OP_NORMAL);
5723 if (label)
5724 emit_label (label);
5727 /* Handle calls that return values in multiple non-contiguous locations.
5728 The Irix 6 ABI has examples of this. */
5729 else if (GET_CODE (target) == PARALLEL)
5731 if (GET_CODE (temp) == PARALLEL)
5732 emit_group_move (target, temp);
5733 else
5734 emit_group_load (target, temp, TREE_TYPE (exp),
5735 int_size_in_bytes (TREE_TYPE (exp)));
5737 else if (GET_CODE (temp) == PARALLEL)
5738 emit_group_store (target, temp, TREE_TYPE (exp),
5739 int_size_in_bytes (TREE_TYPE (exp)));
5740 else if (GET_MODE (temp) == BLKmode)
5741 emit_block_move (target, temp, expr_size (exp),
5742 (call_param_p
5743 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5744 /* If we emit a nontemporal store, there is nothing else to do. */
5745 else if (nontemporal && emit_storent_insn (target, temp))
5747 else
5749 if (reverse)
5750 temp = flip_storage_order (GET_MODE (target), temp);
5751 temp = force_operand (temp, target);
5752 if (temp != target)
5753 emit_move_insn (target, temp);
5757 return NULL_RTX;
5760 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5762 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5763 bool reverse)
5765 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5766 reverse, NULL);
5769 /* Return true if field F of structure TYPE is a flexible array. */
5771 static bool
5772 flexible_array_member_p (const_tree f, const_tree type)
5774 const_tree tf;
5776 tf = TREE_TYPE (f);
5777 return (DECL_CHAIN (f) == NULL
5778 && TREE_CODE (tf) == ARRAY_TYPE
5779 && TYPE_DOMAIN (tf)
5780 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5781 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5782 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5783 && int_size_in_bytes (type) >= 0);
5786 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5787 must have in order for it to completely initialize a value of type TYPE.
5788 Return -1 if the number isn't known.
5790 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5792 static HOST_WIDE_INT
5793 count_type_elements (const_tree type, bool for_ctor_p)
5795 switch (TREE_CODE (type))
5797 case ARRAY_TYPE:
5799 tree nelts;
5801 nelts = array_type_nelts (type);
5802 if (nelts && tree_fits_uhwi_p (nelts))
5804 unsigned HOST_WIDE_INT n;
5806 n = tree_to_uhwi (nelts) + 1;
5807 if (n == 0 || for_ctor_p)
5808 return n;
5809 else
5810 return n * count_type_elements (TREE_TYPE (type), false);
5812 return for_ctor_p ? -1 : 1;
5815 case RECORD_TYPE:
5817 unsigned HOST_WIDE_INT n;
5818 tree f;
5820 n = 0;
5821 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5822 if (TREE_CODE (f) == FIELD_DECL)
5824 if (!for_ctor_p)
5825 n += count_type_elements (TREE_TYPE (f), false);
5826 else if (!flexible_array_member_p (f, type))
5827 /* Don't count flexible arrays, which are not supposed
5828 to be initialized. */
5829 n += 1;
5832 return n;
5835 case UNION_TYPE:
5836 case QUAL_UNION_TYPE:
5838 tree f;
5839 HOST_WIDE_INT n, m;
5841 gcc_assert (!for_ctor_p);
5842 /* Estimate the number of scalars in each field and pick the
5843 maximum. Other estimates would do instead; the idea is simply
5844 to make sure that the estimate is not sensitive to the ordering
5845 of the fields. */
5846 n = 1;
5847 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5848 if (TREE_CODE (f) == FIELD_DECL)
5850 m = count_type_elements (TREE_TYPE (f), false);
5851 /* If the field doesn't span the whole union, add an extra
5852 scalar for the rest. */
5853 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5854 TYPE_SIZE (type)) != 1)
5855 m++;
5856 if (n < m)
5857 n = m;
5859 return n;
5862 case COMPLEX_TYPE:
5863 return 2;
5865 case VECTOR_TYPE:
5866 return TYPE_VECTOR_SUBPARTS (type);
5868 case INTEGER_TYPE:
5869 case REAL_TYPE:
5870 case FIXED_POINT_TYPE:
5871 case ENUMERAL_TYPE:
5872 case BOOLEAN_TYPE:
5873 case POINTER_TYPE:
5874 case OFFSET_TYPE:
5875 case REFERENCE_TYPE:
5876 case NULLPTR_TYPE:
5877 return 1;
5879 case ERROR_MARK:
5880 return 0;
5882 case VOID_TYPE:
5883 case METHOD_TYPE:
5884 case FUNCTION_TYPE:
5885 case LANG_TYPE:
5886 default:
5887 gcc_unreachable ();
5891 /* Helper for categorize_ctor_elements. Identical interface. */
5893 static bool
5894 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5895 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5897 unsigned HOST_WIDE_INT idx;
5898 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5899 tree value, purpose, elt_type;
5901 /* Whether CTOR is a valid constant initializer, in accordance with what
5902 initializer_constant_valid_p does. If inferred from the constructor
5903 elements, true until proven otherwise. */
5904 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5905 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5907 nz_elts = 0;
5908 init_elts = 0;
5909 num_fields = 0;
5910 elt_type = NULL_TREE;
5912 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5914 HOST_WIDE_INT mult = 1;
5916 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5918 tree lo_index = TREE_OPERAND (purpose, 0);
5919 tree hi_index = TREE_OPERAND (purpose, 1);
5921 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5922 mult = (tree_to_uhwi (hi_index)
5923 - tree_to_uhwi (lo_index) + 1);
5925 num_fields += mult;
5926 elt_type = TREE_TYPE (value);
5928 switch (TREE_CODE (value))
5930 case CONSTRUCTOR:
5932 HOST_WIDE_INT nz = 0, ic = 0;
5934 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5935 p_complete);
5937 nz_elts += mult * nz;
5938 init_elts += mult * ic;
5940 if (const_from_elts_p && const_p)
5941 const_p = const_elt_p;
5943 break;
5945 case INTEGER_CST:
5946 case REAL_CST:
5947 case FIXED_CST:
5948 if (!initializer_zerop (value))
5949 nz_elts += mult;
5950 init_elts += mult;
5951 break;
5953 case STRING_CST:
5954 nz_elts += mult * TREE_STRING_LENGTH (value);
5955 init_elts += mult * TREE_STRING_LENGTH (value);
5956 break;
5958 case COMPLEX_CST:
5959 if (!initializer_zerop (TREE_REALPART (value)))
5960 nz_elts += mult;
5961 if (!initializer_zerop (TREE_IMAGPART (value)))
5962 nz_elts += mult;
5963 init_elts += mult;
5964 break;
5966 case VECTOR_CST:
5968 unsigned i;
5969 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5971 tree v = VECTOR_CST_ELT (value, i);
5972 if (!initializer_zerop (v))
5973 nz_elts += mult;
5974 init_elts += mult;
5977 break;
5979 default:
5981 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5982 nz_elts += mult * tc;
5983 init_elts += mult * tc;
5985 if (const_from_elts_p && const_p)
5986 const_p
5987 = initializer_constant_valid_p (value,
5988 elt_type,
5989 TYPE_REVERSE_STORAGE_ORDER
5990 (TREE_TYPE (ctor)))
5991 != NULL_TREE;
5993 break;
5997 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5998 num_fields, elt_type))
5999 *p_complete = false;
6001 *p_nz_elts += nz_elts;
6002 *p_init_elts += init_elts;
6004 return const_p;
6007 /* Examine CTOR to discover:
6008 * how many scalar fields are set to nonzero values,
6009 and place it in *P_NZ_ELTS;
6010 * how many scalar fields in total are in CTOR,
6011 and place it in *P_ELT_COUNT.
6012 * whether the constructor is complete -- in the sense that every
6013 meaningful byte is explicitly given a value --
6014 and place it in *P_COMPLETE.
6016 Return whether or not CTOR is a valid static constant initializer, the same
6017 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6019 bool
6020 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6021 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6023 *p_nz_elts = 0;
6024 *p_init_elts = 0;
6025 *p_complete = true;
6027 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6030 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6031 of which had type LAST_TYPE. Each element was itself a complete
6032 initializer, in the sense that every meaningful byte was explicitly
6033 given a value. Return true if the same is true for the constructor
6034 as a whole. */
6036 bool
6037 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6038 const_tree last_type)
6040 if (TREE_CODE (type) == UNION_TYPE
6041 || TREE_CODE (type) == QUAL_UNION_TYPE)
6043 if (num_elts == 0)
6044 return false;
6046 gcc_assert (num_elts == 1 && last_type);
6048 /* ??? We could look at each element of the union, and find the
6049 largest element. Which would avoid comparing the size of the
6050 initialized element against any tail padding in the union.
6051 Doesn't seem worth the effort... */
6052 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6055 return count_type_elements (type, true) == num_elts;
6058 /* Return 1 if EXP contains mostly (3/4) zeros. */
6060 static int
6061 mostly_zeros_p (const_tree exp)
6063 if (TREE_CODE (exp) == CONSTRUCTOR)
6065 HOST_WIDE_INT nz_elts, init_elts;
6066 bool complete_p;
6068 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6069 return !complete_p || nz_elts < init_elts / 4;
6072 return initializer_zerop (exp);
6075 /* Return 1 if EXP contains all zeros. */
6077 static int
6078 all_zeros_p (const_tree exp)
6080 if (TREE_CODE (exp) == CONSTRUCTOR)
6082 HOST_WIDE_INT nz_elts, init_elts;
6083 bool complete_p;
6085 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6086 return nz_elts == 0;
6089 return initializer_zerop (exp);
6092 /* Helper function for store_constructor.
6093 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6094 CLEARED is as for store_constructor.
6095 ALIAS_SET is the alias set to use for any stores.
6096 If REVERSE is true, the store is to be done in reverse order.
6098 This provides a recursive shortcut back to store_constructor when it isn't
6099 necessary to go through store_field. This is so that we can pass through
6100 the cleared field to let store_constructor know that we may not have to
6101 clear a substructure if the outer structure has already been cleared. */
6103 static void
6104 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6105 HOST_WIDE_INT bitpos,
6106 unsigned HOST_WIDE_INT bitregion_start,
6107 unsigned HOST_WIDE_INT bitregion_end,
6108 machine_mode mode,
6109 tree exp, int cleared,
6110 alias_set_type alias_set, bool reverse)
6112 if (TREE_CODE (exp) == CONSTRUCTOR
6113 /* We can only call store_constructor recursively if the size and
6114 bit position are on a byte boundary. */
6115 && bitpos % BITS_PER_UNIT == 0
6116 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6117 /* If we have a nonzero bitpos for a register target, then we just
6118 let store_field do the bitfield handling. This is unlikely to
6119 generate unnecessary clear instructions anyways. */
6120 && (bitpos == 0 || MEM_P (target)))
6122 if (MEM_P (target))
6123 target
6124 = adjust_address (target,
6125 GET_MODE (target) == BLKmode
6126 || 0 != (bitpos
6127 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6128 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6131 /* Update the alias set, if required. */
6132 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6133 && MEM_ALIAS_SET (target) != 0)
6135 target = copy_rtx (target);
6136 set_mem_alias_set (target, alias_set);
6139 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6140 reverse);
6142 else
6143 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6144 exp, alias_set, false, reverse);
6148 /* Returns the number of FIELD_DECLs in TYPE. */
6150 static int
6151 fields_length (const_tree type)
6153 tree t = TYPE_FIELDS (type);
6154 int count = 0;
6156 for (; t; t = DECL_CHAIN (t))
6157 if (TREE_CODE (t) == FIELD_DECL)
6158 ++count;
6160 return count;
6164 /* Store the value of constructor EXP into the rtx TARGET.
6165 TARGET is either a REG or a MEM; we know it cannot conflict, since
6166 safe_from_p has been called.
6167 CLEARED is true if TARGET is known to have been zero'd.
6168 SIZE is the number of bytes of TARGET we are allowed to modify: this
6169 may not be the same as the size of EXP if we are assigning to a field
6170 which has been packed to exclude padding bits.
6171 If REVERSE is true, the store is to be done in reverse order. */
6173 static void
6174 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6175 bool reverse)
6177 tree type = TREE_TYPE (exp);
6178 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6179 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6181 switch (TREE_CODE (type))
6183 case RECORD_TYPE:
6184 case UNION_TYPE:
6185 case QUAL_UNION_TYPE:
6187 unsigned HOST_WIDE_INT idx;
6188 tree field, value;
6190 /* The storage order is specified for every aggregate type. */
6191 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6193 /* If size is zero or the target is already cleared, do nothing. */
6194 if (size == 0 || cleared)
6195 cleared = 1;
6196 /* We either clear the aggregate or indicate the value is dead. */
6197 else if ((TREE_CODE (type) == UNION_TYPE
6198 || TREE_CODE (type) == QUAL_UNION_TYPE)
6199 && ! CONSTRUCTOR_ELTS (exp))
6200 /* If the constructor is empty, clear the union. */
6202 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6203 cleared = 1;
6206 /* If we are building a static constructor into a register,
6207 set the initial value as zero so we can fold the value into
6208 a constant. But if more than one register is involved,
6209 this probably loses. */
6210 else if (REG_P (target) && TREE_STATIC (exp)
6211 && (GET_MODE_SIZE (GET_MODE (target))
6212 <= REGMODE_NATURAL_SIZE (GET_MODE (target))))
6214 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6215 cleared = 1;
6218 /* If the constructor has fewer fields than the structure or
6219 if we are initializing the structure to mostly zeros, clear
6220 the whole structure first. Don't do this if TARGET is a
6221 register whose mode size isn't equal to SIZE since
6222 clear_storage can't handle this case. */
6223 else if (size > 0
6224 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6225 || mostly_zeros_p (exp))
6226 && (!REG_P (target)
6227 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6228 == size)))
6230 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6231 cleared = 1;
6234 if (REG_P (target) && !cleared)
6235 emit_clobber (target);
6237 /* Store each element of the constructor into the
6238 corresponding field of TARGET. */
6239 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6241 machine_mode mode;
6242 HOST_WIDE_INT bitsize;
6243 HOST_WIDE_INT bitpos = 0;
6244 tree offset;
6245 rtx to_rtx = target;
6247 /* Just ignore missing fields. We cleared the whole
6248 structure, above, if any fields are missing. */
6249 if (field == 0)
6250 continue;
6252 if (cleared && initializer_zerop (value))
6253 continue;
6255 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6256 bitsize = tree_to_uhwi (DECL_SIZE (field));
6257 else
6258 gcc_unreachable ();
6260 mode = DECL_MODE (field);
6261 if (DECL_BIT_FIELD (field))
6262 mode = VOIDmode;
6264 offset = DECL_FIELD_OFFSET (field);
6265 if (tree_fits_shwi_p (offset)
6266 && tree_fits_shwi_p (bit_position (field)))
6268 bitpos = int_bit_position (field);
6269 offset = NULL_TREE;
6271 else
6272 gcc_unreachable ();
6274 /* If this initializes a field that is smaller than a
6275 word, at the start of a word, try to widen it to a full
6276 word. This special case allows us to output C++ member
6277 function initializations in a form that the optimizers
6278 can understand. */
6279 if (WORD_REGISTER_OPERATIONS
6280 && REG_P (target)
6281 && bitsize < BITS_PER_WORD
6282 && bitpos % BITS_PER_WORD == 0
6283 && GET_MODE_CLASS (mode) == MODE_INT
6284 && TREE_CODE (value) == INTEGER_CST
6285 && exp_size >= 0
6286 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6288 tree type = TREE_TYPE (value);
6290 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6292 type = lang_hooks.types.type_for_mode
6293 (word_mode, TYPE_UNSIGNED (type));
6294 value = fold_convert (type, value);
6295 /* Make sure the bits beyond the original bitsize are zero
6296 so that we can correctly avoid extra zeroing stores in
6297 later constructor elements. */
6298 tree bitsize_mask
6299 = wide_int_to_tree (type, wi::mask (bitsize, false,
6300 BITS_PER_WORD));
6301 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6304 if (BYTES_BIG_ENDIAN)
6305 value
6306 = fold_build2 (LSHIFT_EXPR, type, value,
6307 build_int_cst (type,
6308 BITS_PER_WORD - bitsize));
6309 bitsize = BITS_PER_WORD;
6310 mode = word_mode;
6313 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6314 && DECL_NONADDRESSABLE_P (field))
6316 to_rtx = copy_rtx (to_rtx);
6317 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6320 store_constructor_field (to_rtx, bitsize, bitpos,
6321 0, bitregion_end, mode,
6322 value, cleared,
6323 get_alias_set (TREE_TYPE (field)),
6324 reverse);
6326 break;
6328 case ARRAY_TYPE:
6330 tree value, index;
6331 unsigned HOST_WIDE_INT i;
6332 int need_to_clear;
6333 tree domain;
6334 tree elttype = TREE_TYPE (type);
6335 int const_bounds_p;
6336 HOST_WIDE_INT minelt = 0;
6337 HOST_WIDE_INT maxelt = 0;
6339 /* The storage order is specified for every aggregate type. */
6340 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6342 domain = TYPE_DOMAIN (type);
6343 const_bounds_p = (TYPE_MIN_VALUE (domain)
6344 && TYPE_MAX_VALUE (domain)
6345 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6346 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6348 /* If we have constant bounds for the range of the type, get them. */
6349 if (const_bounds_p)
6351 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6352 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6355 /* If the constructor has fewer elements than the array, clear
6356 the whole array first. Similarly if this is static
6357 constructor of a non-BLKmode object. */
6358 if (cleared)
6359 need_to_clear = 0;
6360 else if (REG_P (target) && TREE_STATIC (exp))
6361 need_to_clear = 1;
6362 else
6364 unsigned HOST_WIDE_INT idx;
6365 tree index, value;
6366 HOST_WIDE_INT count = 0, zero_count = 0;
6367 need_to_clear = ! const_bounds_p;
6369 /* This loop is a more accurate version of the loop in
6370 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6371 is also needed to check for missing elements. */
6372 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6374 HOST_WIDE_INT this_node_count;
6376 if (need_to_clear)
6377 break;
6379 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6381 tree lo_index = TREE_OPERAND (index, 0);
6382 tree hi_index = TREE_OPERAND (index, 1);
6384 if (! tree_fits_uhwi_p (lo_index)
6385 || ! tree_fits_uhwi_p (hi_index))
6387 need_to_clear = 1;
6388 break;
6391 this_node_count = (tree_to_uhwi (hi_index)
6392 - tree_to_uhwi (lo_index) + 1);
6394 else
6395 this_node_count = 1;
6397 count += this_node_count;
6398 if (mostly_zeros_p (value))
6399 zero_count += this_node_count;
6402 /* Clear the entire array first if there are any missing
6403 elements, or if the incidence of zero elements is >=
6404 75%. */
6405 if (! need_to_clear
6406 && (count < maxelt - minelt + 1
6407 || 4 * zero_count >= 3 * count))
6408 need_to_clear = 1;
6411 if (need_to_clear && size > 0)
6413 if (REG_P (target))
6414 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6415 else
6416 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6417 cleared = 1;
6420 if (!cleared && REG_P (target))
6421 /* Inform later passes that the old value is dead. */
6422 emit_clobber (target);
6424 /* Store each element of the constructor into the
6425 corresponding element of TARGET, determined by counting the
6426 elements. */
6427 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6429 machine_mode mode;
6430 HOST_WIDE_INT bitsize;
6431 HOST_WIDE_INT bitpos;
6432 rtx xtarget = target;
6434 if (cleared && initializer_zerop (value))
6435 continue;
6437 mode = TYPE_MODE (elttype);
6438 if (mode == BLKmode)
6439 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6440 ? tree_to_uhwi (TYPE_SIZE (elttype))
6441 : -1);
6442 else
6443 bitsize = GET_MODE_BITSIZE (mode);
6445 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6447 tree lo_index = TREE_OPERAND (index, 0);
6448 tree hi_index = TREE_OPERAND (index, 1);
6449 rtx index_r, pos_rtx;
6450 HOST_WIDE_INT lo, hi, count;
6451 tree position;
6453 /* If the range is constant and "small", unroll the loop. */
6454 if (const_bounds_p
6455 && tree_fits_shwi_p (lo_index)
6456 && tree_fits_shwi_p (hi_index)
6457 && (lo = tree_to_shwi (lo_index),
6458 hi = tree_to_shwi (hi_index),
6459 count = hi - lo + 1,
6460 (!MEM_P (target)
6461 || count <= 2
6462 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6463 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6464 <= 40 * 8)))))
6466 lo -= minelt; hi -= minelt;
6467 for (; lo <= hi; lo++)
6469 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6471 if (MEM_P (target)
6472 && !MEM_KEEP_ALIAS_SET_P (target)
6473 && TREE_CODE (type) == ARRAY_TYPE
6474 && TYPE_NONALIASED_COMPONENT (type))
6476 target = copy_rtx (target);
6477 MEM_KEEP_ALIAS_SET_P (target) = 1;
6480 store_constructor_field
6481 (target, bitsize, bitpos, 0, bitregion_end,
6482 mode, value, cleared,
6483 get_alias_set (elttype), reverse);
6486 else
6488 rtx_code_label *loop_start = gen_label_rtx ();
6489 rtx_code_label *loop_end = gen_label_rtx ();
6490 tree exit_cond;
6492 expand_normal (hi_index);
6494 index = build_decl (EXPR_LOCATION (exp),
6495 VAR_DECL, NULL_TREE, domain);
6496 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6497 SET_DECL_RTL (index, index_r);
6498 store_expr (lo_index, index_r, 0, false, reverse);
6500 /* Build the head of the loop. */
6501 do_pending_stack_adjust ();
6502 emit_label (loop_start);
6504 /* Assign value to element index. */
6505 position =
6506 fold_convert (ssizetype,
6507 fold_build2 (MINUS_EXPR,
6508 TREE_TYPE (index),
6509 index,
6510 TYPE_MIN_VALUE (domain)));
6512 position =
6513 size_binop (MULT_EXPR, position,
6514 fold_convert (ssizetype,
6515 TYPE_SIZE_UNIT (elttype)));
6517 pos_rtx = expand_normal (position);
6518 xtarget = offset_address (target, pos_rtx,
6519 highest_pow2_factor (position));
6520 xtarget = adjust_address (xtarget, mode, 0);
6521 if (TREE_CODE (value) == CONSTRUCTOR)
6522 store_constructor (value, xtarget, cleared,
6523 bitsize / BITS_PER_UNIT, reverse);
6524 else
6525 store_expr (value, xtarget, 0, false, reverse);
6527 /* Generate a conditional jump to exit the loop. */
6528 exit_cond = build2 (LT_EXPR, integer_type_node,
6529 index, hi_index);
6530 jumpif (exit_cond, loop_end,
6531 profile_probability::uninitialized ());
6533 /* Update the loop counter, and jump to the head of
6534 the loop. */
6535 expand_assignment (index,
6536 build2 (PLUS_EXPR, TREE_TYPE (index),
6537 index, integer_one_node),
6538 false);
6540 emit_jump (loop_start);
6542 /* Build the end of the loop. */
6543 emit_label (loop_end);
6546 else if ((index != 0 && ! tree_fits_shwi_p (index))
6547 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6549 tree position;
6551 if (index == 0)
6552 index = ssize_int (1);
6554 if (minelt)
6555 index = fold_convert (ssizetype,
6556 fold_build2 (MINUS_EXPR,
6557 TREE_TYPE (index),
6558 index,
6559 TYPE_MIN_VALUE (domain)));
6561 position =
6562 size_binop (MULT_EXPR, index,
6563 fold_convert (ssizetype,
6564 TYPE_SIZE_UNIT (elttype)));
6565 xtarget = offset_address (target,
6566 expand_normal (position),
6567 highest_pow2_factor (position));
6568 xtarget = adjust_address (xtarget, mode, 0);
6569 store_expr (value, xtarget, 0, false, reverse);
6571 else
6573 if (index != 0)
6574 bitpos = ((tree_to_shwi (index) - minelt)
6575 * tree_to_uhwi (TYPE_SIZE (elttype)));
6576 else
6577 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6579 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6580 && TREE_CODE (type) == ARRAY_TYPE
6581 && TYPE_NONALIASED_COMPONENT (type))
6583 target = copy_rtx (target);
6584 MEM_KEEP_ALIAS_SET_P (target) = 1;
6586 store_constructor_field (target, bitsize, bitpos, 0,
6587 bitregion_end, mode, value,
6588 cleared, get_alias_set (elttype),
6589 reverse);
6592 break;
6595 case VECTOR_TYPE:
6597 unsigned HOST_WIDE_INT idx;
6598 constructor_elt *ce;
6599 int i;
6600 int need_to_clear;
6601 int icode = CODE_FOR_nothing;
6602 tree elttype = TREE_TYPE (type);
6603 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6604 machine_mode eltmode = TYPE_MODE (elttype);
6605 HOST_WIDE_INT bitsize;
6606 HOST_WIDE_INT bitpos;
6607 rtvec vector = NULL;
6608 unsigned n_elts;
6609 alias_set_type alias;
6610 bool vec_vec_init_p = false;
6612 gcc_assert (eltmode != BLKmode);
6614 n_elts = TYPE_VECTOR_SUBPARTS (type);
6615 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6617 machine_mode mode = GET_MODE (target);
6618 machine_mode emode = eltmode;
6620 if (CONSTRUCTOR_NELTS (exp)
6621 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6622 == VECTOR_TYPE))
6624 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6625 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6626 == n_elts);
6627 emode = TYPE_MODE (etype);
6629 icode = (int) convert_optab_handler (vec_init_optab, mode, emode);
6630 if (icode != CODE_FOR_nothing)
6632 unsigned int i, n = n_elts;
6634 if (emode != eltmode)
6636 n = CONSTRUCTOR_NELTS (exp);
6637 vec_vec_init_p = true;
6639 vector = rtvec_alloc (n);
6640 for (i = 0; i < n; i++)
6641 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6645 /* If the constructor has fewer elements than the vector,
6646 clear the whole array first. Similarly if this is static
6647 constructor of a non-BLKmode object. */
6648 if (cleared)
6649 need_to_clear = 0;
6650 else if (REG_P (target) && TREE_STATIC (exp))
6651 need_to_clear = 1;
6652 else
6654 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6655 tree value;
6657 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6659 tree sz = TYPE_SIZE (TREE_TYPE (value));
6660 int n_elts_here
6661 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6662 TYPE_SIZE (elttype)));
6664 count += n_elts_here;
6665 if (mostly_zeros_p (value))
6666 zero_count += n_elts_here;
6669 /* Clear the entire vector first if there are any missing elements,
6670 or if the incidence of zero elements is >= 75%. */
6671 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6674 if (need_to_clear && size > 0 && !vector)
6676 if (REG_P (target))
6677 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6678 else
6679 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6680 cleared = 1;
6683 /* Inform later passes that the old value is dead. */
6684 if (!cleared && !vector && REG_P (target))
6685 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6687 if (MEM_P (target))
6688 alias = MEM_ALIAS_SET (target);
6689 else
6690 alias = get_alias_set (elttype);
6692 /* Store each element of the constructor into the corresponding
6693 element of TARGET, determined by counting the elements. */
6694 for (idx = 0, i = 0;
6695 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6696 idx++, i += bitsize / elt_size)
6698 HOST_WIDE_INT eltpos;
6699 tree value = ce->value;
6701 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6702 if (cleared && initializer_zerop (value))
6703 continue;
6705 if (ce->index)
6706 eltpos = tree_to_uhwi (ce->index);
6707 else
6708 eltpos = i;
6710 if (vector)
6712 if (vec_vec_init_p)
6714 gcc_assert (ce->index == NULL_TREE);
6715 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6716 eltpos = idx;
6718 else
6719 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6720 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6722 else
6724 machine_mode value_mode
6725 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6726 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6727 bitpos = eltpos * elt_size;
6728 store_constructor_field (target, bitsize, bitpos, 0,
6729 bitregion_end, value_mode,
6730 value, cleared, alias, reverse);
6734 if (vector)
6735 emit_insn (GEN_FCN (icode) (target,
6736 gen_rtx_PARALLEL (GET_MODE (target),
6737 vector)));
6738 break;
6741 default:
6742 gcc_unreachable ();
6746 /* Store the value of EXP (an expression tree)
6747 into a subfield of TARGET which has mode MODE and occupies
6748 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6749 If MODE is VOIDmode, it means that we are storing into a bit-field.
6751 BITREGION_START is bitpos of the first bitfield in this region.
6752 BITREGION_END is the bitpos of the ending bitfield in this region.
6753 These two fields are 0, if the C++ memory model does not apply,
6754 or we are not interested in keeping track of bitfield regions.
6756 Always return const0_rtx unless we have something particular to
6757 return.
6759 ALIAS_SET is the alias set for the destination. This value will
6760 (in general) be different from that for TARGET, since TARGET is a
6761 reference to the containing structure.
6763 If NONTEMPORAL is true, try generating a nontemporal store.
6765 If REVERSE is true, the store is to be done in reverse order. */
6767 static rtx
6768 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6769 unsigned HOST_WIDE_INT bitregion_start,
6770 unsigned HOST_WIDE_INT bitregion_end,
6771 machine_mode mode, tree exp,
6772 alias_set_type alias_set, bool nontemporal, bool reverse)
6774 if (TREE_CODE (exp) == ERROR_MARK)
6775 return const0_rtx;
6777 /* If we have nothing to store, do nothing unless the expression has
6778 side-effects. Don't do that for zero sized addressable lhs of
6779 calls. */
6780 if (bitsize == 0
6781 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6782 || TREE_CODE (exp) != CALL_EXPR))
6783 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6785 if (GET_CODE (target) == CONCAT)
6787 /* We're storing into a struct containing a single __complex. */
6789 gcc_assert (!bitpos);
6790 return store_expr (exp, target, 0, nontemporal, reverse);
6793 /* If the structure is in a register or if the component
6794 is a bit field, we cannot use addressing to access it.
6795 Use bit-field techniques or SUBREG to store in it. */
6797 if (mode == VOIDmode
6798 || (mode != BLKmode && ! direct_store[(int) mode]
6799 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6800 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6801 || REG_P (target)
6802 || GET_CODE (target) == SUBREG
6803 /* If the field isn't aligned enough to store as an ordinary memref,
6804 store it as a bit field. */
6805 || (mode != BLKmode
6806 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6807 || bitpos % GET_MODE_ALIGNMENT (mode))
6808 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6809 || (bitpos % BITS_PER_UNIT != 0)))
6810 || (bitsize >= 0 && mode != BLKmode
6811 && GET_MODE_BITSIZE (mode) > bitsize)
6812 /* If the RHS and field are a constant size and the size of the
6813 RHS isn't the same size as the bitfield, we must use bitfield
6814 operations. */
6815 || (bitsize >= 0
6816 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6817 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6818 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6819 we will handle specially below. */
6820 && !(TREE_CODE (exp) == CONSTRUCTOR
6821 && bitsize % BITS_PER_UNIT == 0)
6822 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6823 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6824 includes some extra padding. store_expr / expand_expr will in
6825 that case call get_inner_reference that will have the bitsize
6826 we check here and thus the block move will not clobber the
6827 padding that shouldn't be clobbered. In the future we could
6828 replace the TREE_ADDRESSABLE check with a check that
6829 get_base_address needs to live in memory. */
6830 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6831 || TREE_CODE (exp) != COMPONENT_REF
6832 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6833 || (bitsize % BITS_PER_UNIT != 0)
6834 || (bitpos % BITS_PER_UNIT != 0)
6835 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6836 != 0)))
6837 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6838 decl we must use bitfield operations. */
6839 || (bitsize >= 0
6840 && TREE_CODE (exp) == MEM_REF
6841 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6842 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6843 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6844 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6846 rtx temp;
6847 gimple *nop_def;
6849 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6850 implies a mask operation. If the precision is the same size as
6851 the field we're storing into, that mask is redundant. This is
6852 particularly common with bit field assignments generated by the
6853 C front end. */
6854 nop_def = get_def_for_expr (exp, NOP_EXPR);
6855 if (nop_def)
6857 tree type = TREE_TYPE (exp);
6858 if (INTEGRAL_TYPE_P (type)
6859 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6860 && bitsize == TYPE_PRECISION (type))
6862 tree op = gimple_assign_rhs1 (nop_def);
6863 type = TREE_TYPE (op);
6864 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6865 exp = op;
6869 temp = expand_normal (exp);
6871 /* Handle calls that return values in multiple non-contiguous locations.
6872 The Irix 6 ABI has examples of this. */
6873 if (GET_CODE (temp) == PARALLEL)
6875 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6876 scalar_int_mode temp_mode
6877 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6878 rtx temp_target = gen_reg_rtx (temp_mode);
6879 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6880 temp = temp_target;
6883 /* Handle calls that return BLKmode values in registers. */
6884 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6886 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6887 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6888 temp = temp_target;
6891 /* If the value has aggregate type and an integral mode then, if BITSIZE
6892 is narrower than this mode and this is for big-endian data, we first
6893 need to put the value into the low-order bits for store_bit_field,
6894 except when MODE is BLKmode and BITSIZE larger than the word size
6895 (see the handling of fields larger than a word in store_bit_field).
6896 Moreover, the field may be not aligned on a byte boundary; in this
6897 case, if it has reverse storage order, it needs to be accessed as a
6898 scalar field with reverse storage order and we must first put the
6899 value into target order. */
6900 scalar_int_mode temp_mode;
6901 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6902 && is_int_mode (GET_MODE (temp), &temp_mode))
6904 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6906 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6908 if (reverse)
6909 temp = flip_storage_order (temp_mode, temp);
6911 if (bitsize < size
6912 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6913 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6914 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6915 size - bitsize, NULL_RTX, 1);
6918 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6919 if (mode != VOIDmode && mode != BLKmode
6920 && mode != TYPE_MODE (TREE_TYPE (exp)))
6921 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6923 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6924 and BITPOS must be aligned on a byte boundary. If so, we simply do
6925 a block copy. Likewise for a BLKmode-like TARGET. */
6926 if (GET_MODE (temp) == BLKmode
6927 && (GET_MODE (target) == BLKmode
6928 || (MEM_P (target)
6929 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6930 && (bitpos % BITS_PER_UNIT) == 0
6931 && (bitsize % BITS_PER_UNIT) == 0)))
6933 gcc_assert (MEM_P (target) && MEM_P (temp)
6934 && (bitpos % BITS_PER_UNIT) == 0);
6936 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6937 emit_block_move (target, temp,
6938 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6939 / BITS_PER_UNIT),
6940 BLOCK_OP_NORMAL);
6942 return const0_rtx;
6945 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6946 word size, we need to load the value (see again store_bit_field). */
6947 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6949 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
6950 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6951 temp_mode, false, NULL);
6954 /* Store the value in the bitfield. */
6955 store_bit_field (target, bitsize, bitpos,
6956 bitregion_start, bitregion_end,
6957 mode, temp, reverse);
6959 return const0_rtx;
6961 else
6963 /* Now build a reference to just the desired component. */
6964 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6966 if (to_rtx == target)
6967 to_rtx = copy_rtx (to_rtx);
6969 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6970 set_mem_alias_set (to_rtx, alias_set);
6972 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6973 into a target smaller than its type; handle that case now. */
6974 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6976 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6977 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6978 return to_rtx;
6981 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6985 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6986 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6987 codes and find the ultimate containing object, which we return.
6989 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6990 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6991 storage order of the field.
6992 If the position of the field is variable, we store a tree
6993 giving the variable offset (in units) in *POFFSET.
6994 This offset is in addition to the bit position.
6995 If the position is not variable, we store 0 in *POFFSET.
6997 If any of the extraction expressions is volatile,
6998 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7000 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7001 Otherwise, it is a mode that can be used to access the field.
7003 If the field describes a variable-sized object, *PMODE is set to
7004 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7005 this case, but the address of the object can be found. */
7007 tree
7008 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
7009 HOST_WIDE_INT *pbitpos, tree *poffset,
7010 machine_mode *pmode, int *punsignedp,
7011 int *preversep, int *pvolatilep)
7013 tree size_tree = 0;
7014 machine_mode mode = VOIDmode;
7015 bool blkmode_bitfield = false;
7016 tree offset = size_zero_node;
7017 offset_int bit_offset = 0;
7019 /* First get the mode, signedness, storage order and size. We do this from
7020 just the outermost expression. */
7021 *pbitsize = -1;
7022 if (TREE_CODE (exp) == COMPONENT_REF)
7024 tree field = TREE_OPERAND (exp, 1);
7025 size_tree = DECL_SIZE (field);
7026 if (flag_strict_volatile_bitfields > 0
7027 && TREE_THIS_VOLATILE (exp)
7028 && DECL_BIT_FIELD_TYPE (field)
7029 && DECL_MODE (field) != BLKmode)
7030 /* Volatile bitfields should be accessed in the mode of the
7031 field's type, not the mode computed based on the bit
7032 size. */
7033 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7034 else if (!DECL_BIT_FIELD (field))
7036 mode = DECL_MODE (field);
7037 /* For vector fields re-check the target flags, as DECL_MODE
7038 could have been set with different target flags than
7039 the current function has. */
7040 if (mode == BLKmode
7041 && VECTOR_TYPE_P (TREE_TYPE (field))
7042 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7043 mode = TYPE_MODE (TREE_TYPE (field));
7045 else if (DECL_MODE (field) == BLKmode)
7046 blkmode_bitfield = true;
7048 *punsignedp = DECL_UNSIGNED (field);
7050 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7052 size_tree = TREE_OPERAND (exp, 1);
7053 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7054 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7056 /* For vector types, with the correct size of access, use the mode of
7057 inner type. */
7058 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7059 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7060 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7061 mode = TYPE_MODE (TREE_TYPE (exp));
7063 else
7065 mode = TYPE_MODE (TREE_TYPE (exp));
7066 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7068 if (mode == BLKmode)
7069 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7070 else
7071 *pbitsize = GET_MODE_BITSIZE (mode);
7074 if (size_tree != 0)
7076 if (! tree_fits_uhwi_p (size_tree))
7077 mode = BLKmode, *pbitsize = -1;
7078 else
7079 *pbitsize = tree_to_uhwi (size_tree);
7082 *preversep = reverse_storage_order_for_component_p (exp);
7084 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7085 and find the ultimate containing object. */
7086 while (1)
7088 switch (TREE_CODE (exp))
7090 case BIT_FIELD_REF:
7091 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7092 break;
7094 case COMPONENT_REF:
7096 tree field = TREE_OPERAND (exp, 1);
7097 tree this_offset = component_ref_field_offset (exp);
7099 /* If this field hasn't been filled in yet, don't go past it.
7100 This should only happen when folding expressions made during
7101 type construction. */
7102 if (this_offset == 0)
7103 break;
7105 offset = size_binop (PLUS_EXPR, offset, this_offset);
7106 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7108 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7110 break;
7112 case ARRAY_REF:
7113 case ARRAY_RANGE_REF:
7115 tree index = TREE_OPERAND (exp, 1);
7116 tree low_bound = array_ref_low_bound (exp);
7117 tree unit_size = array_ref_element_size (exp);
7119 /* We assume all arrays have sizes that are a multiple of a byte.
7120 First subtract the lower bound, if any, in the type of the
7121 index, then convert to sizetype and multiply by the size of
7122 the array element. */
7123 if (! integer_zerop (low_bound))
7124 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7125 index, low_bound);
7127 offset = size_binop (PLUS_EXPR, offset,
7128 size_binop (MULT_EXPR,
7129 fold_convert (sizetype, index),
7130 unit_size));
7132 break;
7134 case REALPART_EXPR:
7135 break;
7137 case IMAGPART_EXPR:
7138 bit_offset += *pbitsize;
7139 break;
7141 case VIEW_CONVERT_EXPR:
7142 break;
7144 case MEM_REF:
7145 /* Hand back the decl for MEM[&decl, off]. */
7146 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7148 tree off = TREE_OPERAND (exp, 1);
7149 if (!integer_zerop (off))
7151 offset_int boff, coff = mem_ref_offset (exp);
7152 boff = coff << LOG2_BITS_PER_UNIT;
7153 bit_offset += boff;
7155 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7157 goto done;
7159 default:
7160 goto done;
7163 /* If any reference in the chain is volatile, the effect is volatile. */
7164 if (TREE_THIS_VOLATILE (exp))
7165 *pvolatilep = 1;
7167 exp = TREE_OPERAND (exp, 0);
7169 done:
7171 /* If OFFSET is constant, see if we can return the whole thing as a
7172 constant bit position. Make sure to handle overflow during
7173 this conversion. */
7174 if (TREE_CODE (offset) == INTEGER_CST)
7176 offset_int tem = wi::sext (wi::to_offset (offset),
7177 TYPE_PRECISION (sizetype));
7178 tem <<= LOG2_BITS_PER_UNIT;
7179 tem += bit_offset;
7180 if (wi::fits_shwi_p (tem))
7182 *pbitpos = tem.to_shwi ();
7183 *poffset = offset = NULL_TREE;
7187 /* Otherwise, split it up. */
7188 if (offset)
7190 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7191 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7193 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7194 offset_int tem = wi::bit_and_not (bit_offset, mask);
7195 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7196 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7197 bit_offset -= tem;
7198 tem >>= LOG2_BITS_PER_UNIT;
7199 offset = size_binop (PLUS_EXPR, offset,
7200 wide_int_to_tree (sizetype, tem));
7203 *pbitpos = bit_offset.to_shwi ();
7204 *poffset = offset;
7207 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7208 if (mode == VOIDmode
7209 && blkmode_bitfield
7210 && (*pbitpos % BITS_PER_UNIT) == 0
7211 && (*pbitsize % BITS_PER_UNIT) == 0)
7212 *pmode = BLKmode;
7213 else
7214 *pmode = mode;
7216 return exp;
7219 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7221 static unsigned HOST_WIDE_INT
7222 target_align (const_tree target)
7224 /* We might have a chain of nested references with intermediate misaligning
7225 bitfields components, so need to recurse to find out. */
7227 unsigned HOST_WIDE_INT this_align, outer_align;
7229 switch (TREE_CODE (target))
7231 case BIT_FIELD_REF:
7232 return 1;
7234 case COMPONENT_REF:
7235 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7236 outer_align = target_align (TREE_OPERAND (target, 0));
7237 return MIN (this_align, outer_align);
7239 case ARRAY_REF:
7240 case ARRAY_RANGE_REF:
7241 this_align = TYPE_ALIGN (TREE_TYPE (target));
7242 outer_align = target_align (TREE_OPERAND (target, 0));
7243 return MIN (this_align, outer_align);
7245 CASE_CONVERT:
7246 case NON_LVALUE_EXPR:
7247 case VIEW_CONVERT_EXPR:
7248 this_align = TYPE_ALIGN (TREE_TYPE (target));
7249 outer_align = target_align (TREE_OPERAND (target, 0));
7250 return MAX (this_align, outer_align);
7252 default:
7253 return TYPE_ALIGN (TREE_TYPE (target));
7258 /* Given an rtx VALUE that may contain additions and multiplications, return
7259 an equivalent value that just refers to a register, memory, or constant.
7260 This is done by generating instructions to perform the arithmetic and
7261 returning a pseudo-register containing the value.
7263 The returned value may be a REG, SUBREG, MEM or constant. */
7266 force_operand (rtx value, rtx target)
7268 rtx op1, op2;
7269 /* Use subtarget as the target for operand 0 of a binary operation. */
7270 rtx subtarget = get_subtarget (target);
7271 enum rtx_code code = GET_CODE (value);
7273 /* Check for subreg applied to an expression produced by loop optimizer. */
7274 if (code == SUBREG
7275 && !REG_P (SUBREG_REG (value))
7276 && !MEM_P (SUBREG_REG (value)))
7278 value
7279 = simplify_gen_subreg (GET_MODE (value),
7280 force_reg (GET_MODE (SUBREG_REG (value)),
7281 force_operand (SUBREG_REG (value),
7282 NULL_RTX)),
7283 GET_MODE (SUBREG_REG (value)),
7284 SUBREG_BYTE (value));
7285 code = GET_CODE (value);
7288 /* Check for a PIC address load. */
7289 if ((code == PLUS || code == MINUS)
7290 && XEXP (value, 0) == pic_offset_table_rtx
7291 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7292 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7293 || GET_CODE (XEXP (value, 1)) == CONST))
7295 if (!subtarget)
7296 subtarget = gen_reg_rtx (GET_MODE (value));
7297 emit_move_insn (subtarget, value);
7298 return subtarget;
7301 if (ARITHMETIC_P (value))
7303 op2 = XEXP (value, 1);
7304 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7305 subtarget = 0;
7306 if (code == MINUS && CONST_INT_P (op2))
7308 code = PLUS;
7309 op2 = negate_rtx (GET_MODE (value), op2);
7312 /* Check for an addition with OP2 a constant integer and our first
7313 operand a PLUS of a virtual register and something else. In that
7314 case, we want to emit the sum of the virtual register and the
7315 constant first and then add the other value. This allows virtual
7316 register instantiation to simply modify the constant rather than
7317 creating another one around this addition. */
7318 if (code == PLUS && CONST_INT_P (op2)
7319 && GET_CODE (XEXP (value, 0)) == PLUS
7320 && REG_P (XEXP (XEXP (value, 0), 0))
7321 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7322 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7324 rtx temp = expand_simple_binop (GET_MODE (value), code,
7325 XEXP (XEXP (value, 0), 0), op2,
7326 subtarget, 0, OPTAB_LIB_WIDEN);
7327 return expand_simple_binop (GET_MODE (value), code, temp,
7328 force_operand (XEXP (XEXP (value,
7329 0), 1), 0),
7330 target, 0, OPTAB_LIB_WIDEN);
7333 op1 = force_operand (XEXP (value, 0), subtarget);
7334 op2 = force_operand (op2, NULL_RTX);
7335 switch (code)
7337 case MULT:
7338 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7339 case DIV:
7340 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7341 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7342 target, 1, OPTAB_LIB_WIDEN);
7343 else
7344 return expand_divmod (0,
7345 FLOAT_MODE_P (GET_MODE (value))
7346 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7347 GET_MODE (value), op1, op2, target, 0);
7348 case MOD:
7349 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7350 target, 0);
7351 case UDIV:
7352 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7353 target, 1);
7354 case UMOD:
7355 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7356 target, 1);
7357 case ASHIFTRT:
7358 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7359 target, 0, OPTAB_LIB_WIDEN);
7360 default:
7361 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7362 target, 1, OPTAB_LIB_WIDEN);
7365 if (UNARY_P (value))
7367 if (!target)
7368 target = gen_reg_rtx (GET_MODE (value));
7369 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7370 switch (code)
7372 case ZERO_EXTEND:
7373 case SIGN_EXTEND:
7374 case TRUNCATE:
7375 case FLOAT_EXTEND:
7376 case FLOAT_TRUNCATE:
7377 convert_move (target, op1, code == ZERO_EXTEND);
7378 return target;
7380 case FIX:
7381 case UNSIGNED_FIX:
7382 expand_fix (target, op1, code == UNSIGNED_FIX);
7383 return target;
7385 case FLOAT:
7386 case UNSIGNED_FLOAT:
7387 expand_float (target, op1, code == UNSIGNED_FLOAT);
7388 return target;
7390 default:
7391 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7395 #ifdef INSN_SCHEDULING
7396 /* On machines that have insn scheduling, we want all memory reference to be
7397 explicit, so we need to deal with such paradoxical SUBREGs. */
7398 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7399 value
7400 = simplify_gen_subreg (GET_MODE (value),
7401 force_reg (GET_MODE (SUBREG_REG (value)),
7402 force_operand (SUBREG_REG (value),
7403 NULL_RTX)),
7404 GET_MODE (SUBREG_REG (value)),
7405 SUBREG_BYTE (value));
7406 #endif
7408 return value;
7411 /* Subroutine of expand_expr: return nonzero iff there is no way that
7412 EXP can reference X, which is being modified. TOP_P is nonzero if this
7413 call is going to be used to determine whether we need a temporary
7414 for EXP, as opposed to a recursive call to this function.
7416 It is always safe for this routine to return zero since it merely
7417 searches for optimization opportunities. */
7420 safe_from_p (const_rtx x, tree exp, int top_p)
7422 rtx exp_rtl = 0;
7423 int i, nops;
7425 if (x == 0
7426 /* If EXP has varying size, we MUST use a target since we currently
7427 have no way of allocating temporaries of variable size
7428 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7429 So we assume here that something at a higher level has prevented a
7430 clash. This is somewhat bogus, but the best we can do. Only
7431 do this when X is BLKmode and when we are at the top level. */
7432 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7433 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7434 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7435 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7436 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7437 != INTEGER_CST)
7438 && GET_MODE (x) == BLKmode)
7439 /* If X is in the outgoing argument area, it is always safe. */
7440 || (MEM_P (x)
7441 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7442 || (GET_CODE (XEXP (x, 0)) == PLUS
7443 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7444 return 1;
7446 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7447 find the underlying pseudo. */
7448 if (GET_CODE (x) == SUBREG)
7450 x = SUBREG_REG (x);
7451 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7452 return 0;
7455 /* Now look at our tree code and possibly recurse. */
7456 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7458 case tcc_declaration:
7459 exp_rtl = DECL_RTL_IF_SET (exp);
7460 break;
7462 case tcc_constant:
7463 return 1;
7465 case tcc_exceptional:
7466 if (TREE_CODE (exp) == TREE_LIST)
7468 while (1)
7470 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7471 return 0;
7472 exp = TREE_CHAIN (exp);
7473 if (!exp)
7474 return 1;
7475 if (TREE_CODE (exp) != TREE_LIST)
7476 return safe_from_p (x, exp, 0);
7479 else if (TREE_CODE (exp) == CONSTRUCTOR)
7481 constructor_elt *ce;
7482 unsigned HOST_WIDE_INT idx;
7484 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7485 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7486 || !safe_from_p (x, ce->value, 0))
7487 return 0;
7488 return 1;
7490 else if (TREE_CODE (exp) == ERROR_MARK)
7491 return 1; /* An already-visited SAVE_EXPR? */
7492 else
7493 return 0;
7495 case tcc_statement:
7496 /* The only case we look at here is the DECL_INITIAL inside a
7497 DECL_EXPR. */
7498 return (TREE_CODE (exp) != DECL_EXPR
7499 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7500 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7501 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7503 case tcc_binary:
7504 case tcc_comparison:
7505 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7506 return 0;
7507 /* Fall through. */
7509 case tcc_unary:
7510 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7512 case tcc_expression:
7513 case tcc_reference:
7514 case tcc_vl_exp:
7515 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7516 the expression. If it is set, we conflict iff we are that rtx or
7517 both are in memory. Otherwise, we check all operands of the
7518 expression recursively. */
7520 switch (TREE_CODE (exp))
7522 case ADDR_EXPR:
7523 /* If the operand is static or we are static, we can't conflict.
7524 Likewise if we don't conflict with the operand at all. */
7525 if (staticp (TREE_OPERAND (exp, 0))
7526 || TREE_STATIC (exp)
7527 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7528 return 1;
7530 /* Otherwise, the only way this can conflict is if we are taking
7531 the address of a DECL a that address if part of X, which is
7532 very rare. */
7533 exp = TREE_OPERAND (exp, 0);
7534 if (DECL_P (exp))
7536 if (!DECL_RTL_SET_P (exp)
7537 || !MEM_P (DECL_RTL (exp)))
7538 return 0;
7539 else
7540 exp_rtl = XEXP (DECL_RTL (exp), 0);
7542 break;
7544 case MEM_REF:
7545 if (MEM_P (x)
7546 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7547 get_alias_set (exp)))
7548 return 0;
7549 break;
7551 case CALL_EXPR:
7552 /* Assume that the call will clobber all hard registers and
7553 all of memory. */
7554 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7555 || MEM_P (x))
7556 return 0;
7557 break;
7559 case WITH_CLEANUP_EXPR:
7560 case CLEANUP_POINT_EXPR:
7561 /* Lowered by gimplify.c. */
7562 gcc_unreachable ();
7564 case SAVE_EXPR:
7565 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7567 default:
7568 break;
7571 /* If we have an rtx, we do not need to scan our operands. */
7572 if (exp_rtl)
7573 break;
7575 nops = TREE_OPERAND_LENGTH (exp);
7576 for (i = 0; i < nops; i++)
7577 if (TREE_OPERAND (exp, i) != 0
7578 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7579 return 0;
7581 break;
7583 case tcc_type:
7584 /* Should never get a type here. */
7585 gcc_unreachable ();
7588 /* If we have an rtl, find any enclosed object. Then see if we conflict
7589 with it. */
7590 if (exp_rtl)
7592 if (GET_CODE (exp_rtl) == SUBREG)
7594 exp_rtl = SUBREG_REG (exp_rtl);
7595 if (REG_P (exp_rtl)
7596 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7597 return 0;
7600 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7601 are memory and they conflict. */
7602 return ! (rtx_equal_p (x, exp_rtl)
7603 || (MEM_P (x) && MEM_P (exp_rtl)
7604 && true_dependence (exp_rtl, VOIDmode, x)));
7607 /* If we reach here, it is safe. */
7608 return 1;
7612 /* Return the highest power of two that EXP is known to be a multiple of.
7613 This is used in updating alignment of MEMs in array references. */
7615 unsigned HOST_WIDE_INT
7616 highest_pow2_factor (const_tree exp)
7618 unsigned HOST_WIDE_INT ret;
7619 int trailing_zeros = tree_ctz (exp);
7620 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7621 return BIGGEST_ALIGNMENT;
7622 ret = HOST_WIDE_INT_1U << trailing_zeros;
7623 if (ret > BIGGEST_ALIGNMENT)
7624 return BIGGEST_ALIGNMENT;
7625 return ret;
7628 /* Similar, except that the alignment requirements of TARGET are
7629 taken into account. Assume it is at least as aligned as its
7630 type, unless it is a COMPONENT_REF in which case the layout of
7631 the structure gives the alignment. */
7633 static unsigned HOST_WIDE_INT
7634 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7636 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7637 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7639 return MAX (factor, talign);
7642 /* Convert the tree comparison code TCODE to the rtl one where the
7643 signedness is UNSIGNEDP. */
7645 static enum rtx_code
7646 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7648 enum rtx_code code;
7649 switch (tcode)
7651 case EQ_EXPR:
7652 code = EQ;
7653 break;
7654 case NE_EXPR:
7655 code = NE;
7656 break;
7657 case LT_EXPR:
7658 code = unsignedp ? LTU : LT;
7659 break;
7660 case LE_EXPR:
7661 code = unsignedp ? LEU : LE;
7662 break;
7663 case GT_EXPR:
7664 code = unsignedp ? GTU : GT;
7665 break;
7666 case GE_EXPR:
7667 code = unsignedp ? GEU : GE;
7668 break;
7669 case UNORDERED_EXPR:
7670 code = UNORDERED;
7671 break;
7672 case ORDERED_EXPR:
7673 code = ORDERED;
7674 break;
7675 case UNLT_EXPR:
7676 code = UNLT;
7677 break;
7678 case UNLE_EXPR:
7679 code = UNLE;
7680 break;
7681 case UNGT_EXPR:
7682 code = UNGT;
7683 break;
7684 case UNGE_EXPR:
7685 code = UNGE;
7686 break;
7687 case UNEQ_EXPR:
7688 code = UNEQ;
7689 break;
7690 case LTGT_EXPR:
7691 code = LTGT;
7692 break;
7694 default:
7695 gcc_unreachable ();
7697 return code;
7700 /* Subroutine of expand_expr. Expand the two operands of a binary
7701 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7702 The value may be stored in TARGET if TARGET is nonzero. The
7703 MODIFIER argument is as documented by expand_expr. */
7705 void
7706 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7707 enum expand_modifier modifier)
7709 if (! safe_from_p (target, exp1, 1))
7710 target = 0;
7711 if (operand_equal_p (exp0, exp1, 0))
7713 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7714 *op1 = copy_rtx (*op0);
7716 else
7718 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7719 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7724 /* Return a MEM that contains constant EXP. DEFER is as for
7725 output_constant_def and MODIFIER is as for expand_expr. */
7727 static rtx
7728 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7730 rtx mem;
7732 mem = output_constant_def (exp, defer);
7733 if (modifier != EXPAND_INITIALIZER)
7734 mem = use_anchored_address (mem);
7735 return mem;
7738 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7739 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7741 static rtx
7742 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7743 enum expand_modifier modifier, addr_space_t as)
7745 rtx result, subtarget;
7746 tree inner, offset;
7747 HOST_WIDE_INT bitsize, bitpos;
7748 int unsignedp, reversep, volatilep = 0;
7749 machine_mode mode1;
7751 /* If we are taking the address of a constant and are at the top level,
7752 we have to use output_constant_def since we can't call force_const_mem
7753 at top level. */
7754 /* ??? This should be considered a front-end bug. We should not be
7755 generating ADDR_EXPR of something that isn't an LVALUE. The only
7756 exception here is STRING_CST. */
7757 if (CONSTANT_CLASS_P (exp))
7759 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7760 if (modifier < EXPAND_SUM)
7761 result = force_operand (result, target);
7762 return result;
7765 /* Everything must be something allowed by is_gimple_addressable. */
7766 switch (TREE_CODE (exp))
7768 case INDIRECT_REF:
7769 /* This case will happen via recursion for &a->b. */
7770 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7772 case MEM_REF:
7774 tree tem = TREE_OPERAND (exp, 0);
7775 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7776 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7777 return expand_expr (tem, target, tmode, modifier);
7780 case CONST_DECL:
7781 /* Expand the initializer like constants above. */
7782 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7783 0, modifier), 0);
7784 if (modifier < EXPAND_SUM)
7785 result = force_operand (result, target);
7786 return result;
7788 case REALPART_EXPR:
7789 /* The real part of the complex number is always first, therefore
7790 the address is the same as the address of the parent object. */
7791 offset = 0;
7792 bitpos = 0;
7793 inner = TREE_OPERAND (exp, 0);
7794 break;
7796 case IMAGPART_EXPR:
7797 /* The imaginary part of the complex number is always second.
7798 The expression is therefore always offset by the size of the
7799 scalar type. */
7800 offset = 0;
7801 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7802 inner = TREE_OPERAND (exp, 0);
7803 break;
7805 case COMPOUND_LITERAL_EXPR:
7806 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7807 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7808 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7809 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7810 the initializers aren't gimplified. */
7811 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7812 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7813 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7814 target, tmode, modifier, as);
7815 /* FALLTHRU */
7816 default:
7817 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7818 expand_expr, as that can have various side effects; LABEL_DECLs for
7819 example, may not have their DECL_RTL set yet. Expand the rtl of
7820 CONSTRUCTORs too, which should yield a memory reference for the
7821 constructor's contents. Assume language specific tree nodes can
7822 be expanded in some interesting way. */
7823 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7824 if (DECL_P (exp)
7825 || TREE_CODE (exp) == CONSTRUCTOR
7826 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7828 result = expand_expr (exp, target, tmode,
7829 modifier == EXPAND_INITIALIZER
7830 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7832 /* If the DECL isn't in memory, then the DECL wasn't properly
7833 marked TREE_ADDRESSABLE, which will be either a front-end
7834 or a tree optimizer bug. */
7836 gcc_assert (MEM_P (result));
7837 result = XEXP (result, 0);
7839 /* ??? Is this needed anymore? */
7840 if (DECL_P (exp))
7841 TREE_USED (exp) = 1;
7843 if (modifier != EXPAND_INITIALIZER
7844 && modifier != EXPAND_CONST_ADDRESS
7845 && modifier != EXPAND_SUM)
7846 result = force_operand (result, target);
7847 return result;
7850 /* Pass FALSE as the last argument to get_inner_reference although
7851 we are expanding to RTL. The rationale is that we know how to
7852 handle "aligning nodes" here: we can just bypass them because
7853 they won't change the final object whose address will be returned
7854 (they actually exist only for that purpose). */
7855 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7856 &unsignedp, &reversep, &volatilep);
7857 break;
7860 /* We must have made progress. */
7861 gcc_assert (inner != exp);
7863 subtarget = offset || bitpos ? NULL_RTX : target;
7864 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7865 inner alignment, force the inner to be sufficiently aligned. */
7866 if (CONSTANT_CLASS_P (inner)
7867 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7869 inner = copy_node (inner);
7870 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7871 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7872 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7874 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7876 if (offset)
7878 rtx tmp;
7880 if (modifier != EXPAND_NORMAL)
7881 result = force_operand (result, NULL);
7882 tmp = expand_expr (offset, NULL_RTX, tmode,
7883 modifier == EXPAND_INITIALIZER
7884 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7886 /* expand_expr is allowed to return an object in a mode other
7887 than TMODE. If it did, we need to convert. */
7888 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7889 tmp = convert_modes (tmode, GET_MODE (tmp),
7890 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7891 result = convert_memory_address_addr_space (tmode, result, as);
7892 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7894 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7895 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7896 else
7898 subtarget = bitpos ? NULL_RTX : target;
7899 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7900 1, OPTAB_LIB_WIDEN);
7904 if (bitpos)
7906 /* Someone beforehand should have rejected taking the address
7907 of such an object. */
7908 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7910 result = convert_memory_address_addr_space (tmode, result, as);
7911 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7912 if (modifier < EXPAND_SUM)
7913 result = force_operand (result, target);
7916 return result;
7919 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7920 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7922 static rtx
7923 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7924 enum expand_modifier modifier)
7926 addr_space_t as = ADDR_SPACE_GENERIC;
7927 scalar_int_mode address_mode = Pmode;
7928 scalar_int_mode pointer_mode = ptr_mode;
7929 machine_mode rmode;
7930 rtx result;
7932 /* Target mode of VOIDmode says "whatever's natural". */
7933 if (tmode == VOIDmode)
7934 tmode = TYPE_MODE (TREE_TYPE (exp));
7936 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7938 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7939 address_mode = targetm.addr_space.address_mode (as);
7940 pointer_mode = targetm.addr_space.pointer_mode (as);
7943 /* We can get called with some Weird Things if the user does silliness
7944 like "(short) &a". In that case, convert_memory_address won't do
7945 the right thing, so ignore the given target mode. */
7946 scalar_int_mode new_tmode = (tmode == pointer_mode
7947 ? pointer_mode
7948 : address_mode);
7950 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7951 new_tmode, modifier, as);
7953 /* Despite expand_expr claims concerning ignoring TMODE when not
7954 strictly convenient, stuff breaks if we don't honor it. Note
7955 that combined with the above, we only do this for pointer modes. */
7956 rmode = GET_MODE (result);
7957 if (rmode == VOIDmode)
7958 rmode = new_tmode;
7959 if (rmode != new_tmode)
7960 result = convert_memory_address_addr_space (new_tmode, result, as);
7962 return result;
7965 /* Generate code for computing CONSTRUCTOR EXP.
7966 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7967 is TRUE, instead of creating a temporary variable in memory
7968 NULL is returned and the caller needs to handle it differently. */
7970 static rtx
7971 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7972 bool avoid_temp_mem)
7974 tree type = TREE_TYPE (exp);
7975 machine_mode mode = TYPE_MODE (type);
7977 /* Try to avoid creating a temporary at all. This is possible
7978 if all of the initializer is zero.
7979 FIXME: try to handle all [0..255] initializers we can handle
7980 with memset. */
7981 if (TREE_STATIC (exp)
7982 && !TREE_ADDRESSABLE (exp)
7983 && target != 0 && mode == BLKmode
7984 && all_zeros_p (exp))
7986 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7987 return target;
7990 /* All elts simple constants => refer to a constant in memory. But
7991 if this is a non-BLKmode mode, let it store a field at a time
7992 since that should make a CONST_INT, CONST_WIDE_INT or
7993 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7994 use, it is best to store directly into the target unless the type
7995 is large enough that memcpy will be used. If we are making an
7996 initializer and all operands are constant, put it in memory as
7997 well.
7999 FIXME: Avoid trying to fill vector constructors piece-meal.
8000 Output them with output_constant_def below unless we're sure
8001 they're zeros. This should go away when vector initializers
8002 are treated like VECTOR_CST instead of arrays. */
8003 if ((TREE_STATIC (exp)
8004 && ((mode == BLKmode
8005 && ! (target != 0 && safe_from_p (target, exp, 1)))
8006 || TREE_ADDRESSABLE (exp)
8007 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8008 && (! can_move_by_pieces
8009 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8010 TYPE_ALIGN (type)))
8011 && ! mostly_zeros_p (exp))))
8012 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8013 && TREE_CONSTANT (exp)))
8015 rtx constructor;
8017 if (avoid_temp_mem)
8018 return NULL_RTX;
8020 constructor = expand_expr_constant (exp, 1, modifier);
8022 if (modifier != EXPAND_CONST_ADDRESS
8023 && modifier != EXPAND_INITIALIZER
8024 && modifier != EXPAND_SUM)
8025 constructor = validize_mem (constructor);
8027 return constructor;
8030 /* Handle calls that pass values in multiple non-contiguous
8031 locations. The Irix 6 ABI has examples of this. */
8032 if (target == 0 || ! safe_from_p (target, exp, 1)
8033 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8035 if (avoid_temp_mem)
8036 return NULL_RTX;
8038 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8041 store_constructor (exp, target, 0, int_expr_size (exp), false);
8042 return target;
8046 /* expand_expr: generate code for computing expression EXP.
8047 An rtx for the computed value is returned. The value is never null.
8048 In the case of a void EXP, const0_rtx is returned.
8050 The value may be stored in TARGET if TARGET is nonzero.
8051 TARGET is just a suggestion; callers must assume that
8052 the rtx returned may not be the same as TARGET.
8054 If TARGET is CONST0_RTX, it means that the value will be ignored.
8056 If TMODE is not VOIDmode, it suggests generating the
8057 result in mode TMODE. But this is done only when convenient.
8058 Otherwise, TMODE is ignored and the value generated in its natural mode.
8059 TMODE is just a suggestion; callers must assume that
8060 the rtx returned may not have mode TMODE.
8062 Note that TARGET may have neither TMODE nor MODE. In that case, it
8063 probably will not be used.
8065 If MODIFIER is EXPAND_SUM then when EXP is an addition
8066 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8067 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8068 products as above, or REG or MEM, or constant.
8069 Ordinarily in such cases we would output mul or add instructions
8070 and then return a pseudo reg containing the sum.
8072 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8073 it also marks a label as absolutely required (it can't be dead).
8074 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8075 This is used for outputting expressions used in initializers.
8077 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8078 with a constant address even if that address is not normally legitimate.
8079 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8081 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8082 a call parameter. Such targets require special care as we haven't yet
8083 marked TARGET so that it's safe from being trashed by libcalls. We
8084 don't want to use TARGET for anything but the final result;
8085 Intermediate values must go elsewhere. Additionally, calls to
8086 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8088 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8089 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8090 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8091 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8092 recursively.
8094 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8095 In this case, we don't adjust a returned MEM rtx that wouldn't be
8096 sufficiently aligned for its mode; instead, it's up to the caller
8097 to deal with it afterwards. This is used to make sure that unaligned
8098 base objects for which out-of-bounds accesses are supported, for
8099 example record types with trailing arrays, aren't realigned behind
8100 the back of the caller.
8101 The normal operating mode is to pass FALSE for this parameter. */
8104 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8105 enum expand_modifier modifier, rtx *alt_rtl,
8106 bool inner_reference_p)
8108 rtx ret;
8110 /* Handle ERROR_MARK before anybody tries to access its type. */
8111 if (TREE_CODE (exp) == ERROR_MARK
8112 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8114 ret = CONST0_RTX (tmode);
8115 return ret ? ret : const0_rtx;
8118 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8119 inner_reference_p);
8120 return ret;
8123 /* Try to expand the conditional expression which is represented by
8124 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8125 return the rtl reg which represents the result. Otherwise return
8126 NULL_RTX. */
8128 static rtx
8129 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8130 tree treeop1 ATTRIBUTE_UNUSED,
8131 tree treeop2 ATTRIBUTE_UNUSED)
8133 rtx insn;
8134 rtx op00, op01, op1, op2;
8135 enum rtx_code comparison_code;
8136 machine_mode comparison_mode;
8137 gimple *srcstmt;
8138 rtx temp;
8139 tree type = TREE_TYPE (treeop1);
8140 int unsignedp = TYPE_UNSIGNED (type);
8141 machine_mode mode = TYPE_MODE (type);
8142 machine_mode orig_mode = mode;
8143 static bool expanding_cond_expr_using_cmove = false;
8145 /* Conditional move expansion can end up TERing two operands which,
8146 when recursively hitting conditional expressions can result in
8147 exponential behavior if the cmove expansion ultimatively fails.
8148 It's hardly profitable to TER a cmove into a cmove so avoid doing
8149 that by failing early if we end up recursing. */
8150 if (expanding_cond_expr_using_cmove)
8151 return NULL_RTX;
8153 /* If we cannot do a conditional move on the mode, try doing it
8154 with the promoted mode. */
8155 if (!can_conditionally_move_p (mode))
8157 mode = promote_mode (type, mode, &unsignedp);
8158 if (!can_conditionally_move_p (mode))
8159 return NULL_RTX;
8160 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8162 else
8163 temp = assign_temp (type, 0, 1);
8165 expanding_cond_expr_using_cmove = true;
8166 start_sequence ();
8167 expand_operands (treeop1, treeop2,
8168 temp, &op1, &op2, EXPAND_NORMAL);
8170 if (TREE_CODE (treeop0) == SSA_NAME
8171 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8173 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8174 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8175 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8176 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8177 comparison_mode = TYPE_MODE (type);
8178 unsignedp = TYPE_UNSIGNED (type);
8179 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8181 else if (COMPARISON_CLASS_P (treeop0))
8183 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8184 enum tree_code cmpcode = TREE_CODE (treeop0);
8185 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8186 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8187 unsignedp = TYPE_UNSIGNED (type);
8188 comparison_mode = TYPE_MODE (type);
8189 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8191 else
8193 op00 = expand_normal (treeop0);
8194 op01 = const0_rtx;
8195 comparison_code = NE;
8196 comparison_mode = GET_MODE (op00);
8197 if (comparison_mode == VOIDmode)
8198 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8200 expanding_cond_expr_using_cmove = false;
8202 if (GET_MODE (op1) != mode)
8203 op1 = gen_lowpart (mode, op1);
8205 if (GET_MODE (op2) != mode)
8206 op2 = gen_lowpart (mode, op2);
8208 /* Try to emit the conditional move. */
8209 insn = emit_conditional_move (temp, comparison_code,
8210 op00, op01, comparison_mode,
8211 op1, op2, mode,
8212 unsignedp);
8214 /* If we could do the conditional move, emit the sequence,
8215 and return. */
8216 if (insn)
8218 rtx_insn *seq = get_insns ();
8219 end_sequence ();
8220 emit_insn (seq);
8221 return convert_modes (orig_mode, mode, temp, 0);
8224 /* Otherwise discard the sequence and fall back to code with
8225 branches. */
8226 end_sequence ();
8227 return NULL_RTX;
8231 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8232 enum expand_modifier modifier)
8234 rtx op0, op1, op2, temp;
8235 rtx_code_label *lab;
8236 tree type;
8237 int unsignedp;
8238 machine_mode mode;
8239 scalar_int_mode int_mode;
8240 enum tree_code code = ops->code;
8241 optab this_optab;
8242 rtx subtarget, original_target;
8243 int ignore;
8244 bool reduce_bit_field;
8245 location_t loc = ops->location;
8246 tree treeop0, treeop1, treeop2;
8247 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8248 ? reduce_to_bit_field_precision ((expr), \
8249 target, \
8250 type) \
8251 : (expr))
8253 type = ops->type;
8254 mode = TYPE_MODE (type);
8255 unsignedp = TYPE_UNSIGNED (type);
8257 treeop0 = ops->op0;
8258 treeop1 = ops->op1;
8259 treeop2 = ops->op2;
8261 /* We should be called only on simple (binary or unary) expressions,
8262 exactly those that are valid in gimple expressions that aren't
8263 GIMPLE_SINGLE_RHS (or invalid). */
8264 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8265 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8266 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8268 ignore = (target == const0_rtx
8269 || ((CONVERT_EXPR_CODE_P (code)
8270 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8271 && TREE_CODE (type) == VOID_TYPE));
8273 /* We should be called only if we need the result. */
8274 gcc_assert (!ignore);
8276 /* An operation in what may be a bit-field type needs the
8277 result to be reduced to the precision of the bit-field type,
8278 which is narrower than that of the type's mode. */
8279 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8280 && !type_has_mode_precision_p (type));
8282 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8283 target = 0;
8285 /* Use subtarget as the target for operand 0 of a binary operation. */
8286 subtarget = get_subtarget (target);
8287 original_target = target;
8289 switch (code)
8291 case NON_LVALUE_EXPR:
8292 case PAREN_EXPR:
8293 CASE_CONVERT:
8294 if (treeop0 == error_mark_node)
8295 return const0_rtx;
8297 if (TREE_CODE (type) == UNION_TYPE)
8299 tree valtype = TREE_TYPE (treeop0);
8301 /* If both input and output are BLKmode, this conversion isn't doing
8302 anything except possibly changing memory attribute. */
8303 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8305 rtx result = expand_expr (treeop0, target, tmode,
8306 modifier);
8308 result = copy_rtx (result);
8309 set_mem_attributes (result, type, 0);
8310 return result;
8313 if (target == 0)
8315 if (TYPE_MODE (type) != BLKmode)
8316 target = gen_reg_rtx (TYPE_MODE (type));
8317 else
8318 target = assign_temp (type, 1, 1);
8321 if (MEM_P (target))
8322 /* Store data into beginning of memory target. */
8323 store_expr (treeop0,
8324 adjust_address (target, TYPE_MODE (valtype), 0),
8325 modifier == EXPAND_STACK_PARM,
8326 false, TYPE_REVERSE_STORAGE_ORDER (type));
8328 else
8330 gcc_assert (REG_P (target)
8331 && !TYPE_REVERSE_STORAGE_ORDER (type));
8333 /* Store this field into a union of the proper type. */
8334 store_field (target,
8335 MIN ((int_size_in_bytes (TREE_TYPE
8336 (treeop0))
8337 * BITS_PER_UNIT),
8338 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8339 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8340 false, false);
8343 /* Return the entire union. */
8344 return target;
8347 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8349 op0 = expand_expr (treeop0, target, VOIDmode,
8350 modifier);
8352 /* If the signedness of the conversion differs and OP0 is
8353 a promoted SUBREG, clear that indication since we now
8354 have to do the proper extension. */
8355 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8356 && GET_CODE (op0) == SUBREG)
8357 SUBREG_PROMOTED_VAR_P (op0) = 0;
8359 return REDUCE_BIT_FIELD (op0);
8362 op0 = expand_expr (treeop0, NULL_RTX, mode,
8363 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8364 if (GET_MODE (op0) == mode)
8367 /* If OP0 is a constant, just convert it into the proper mode. */
8368 else if (CONSTANT_P (op0))
8370 tree inner_type = TREE_TYPE (treeop0);
8371 machine_mode inner_mode = GET_MODE (op0);
8373 if (inner_mode == VOIDmode)
8374 inner_mode = TYPE_MODE (inner_type);
8376 if (modifier == EXPAND_INITIALIZER)
8377 op0 = lowpart_subreg (mode, op0, inner_mode);
8378 else
8379 op0= convert_modes (mode, inner_mode, op0,
8380 TYPE_UNSIGNED (inner_type));
8383 else if (modifier == EXPAND_INITIALIZER)
8384 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8385 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8387 else if (target == 0)
8388 op0 = convert_to_mode (mode, op0,
8389 TYPE_UNSIGNED (TREE_TYPE
8390 (treeop0)));
8391 else
8393 convert_move (target, op0,
8394 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8395 op0 = target;
8398 return REDUCE_BIT_FIELD (op0);
8400 case ADDR_SPACE_CONVERT_EXPR:
8402 tree treeop0_type = TREE_TYPE (treeop0);
8404 gcc_assert (POINTER_TYPE_P (type));
8405 gcc_assert (POINTER_TYPE_P (treeop0_type));
8407 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8408 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8410 /* Conversions between pointers to the same address space should
8411 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8412 gcc_assert (as_to != as_from);
8414 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8416 /* Ask target code to handle conversion between pointers
8417 to overlapping address spaces. */
8418 if (targetm.addr_space.subset_p (as_to, as_from)
8419 || targetm.addr_space.subset_p (as_from, as_to))
8421 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8423 else
8425 /* For disjoint address spaces, converting anything but a null
8426 pointer invokes undefined behavior. We truncate or extend the
8427 value as if we'd converted via integers, which handles 0 as
8428 required, and all others as the programmer likely expects. */
8429 #ifndef POINTERS_EXTEND_UNSIGNED
8430 const int POINTERS_EXTEND_UNSIGNED = 1;
8431 #endif
8432 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8433 op0, POINTERS_EXTEND_UNSIGNED);
8435 gcc_assert (op0);
8436 return op0;
8439 case POINTER_PLUS_EXPR:
8440 /* Even though the sizetype mode and the pointer's mode can be different
8441 expand is able to handle this correctly and get the correct result out
8442 of the PLUS_EXPR code. */
8443 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8444 if sizetype precision is smaller than pointer precision. */
8445 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8446 treeop1 = fold_convert_loc (loc, type,
8447 fold_convert_loc (loc, ssizetype,
8448 treeop1));
8449 /* If sizetype precision is larger than pointer precision, truncate the
8450 offset to have matching modes. */
8451 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8452 treeop1 = fold_convert_loc (loc, type, treeop1);
8453 /* FALLTHRU */
8455 case PLUS_EXPR:
8456 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8457 something else, make sure we add the register to the constant and
8458 then to the other thing. This case can occur during strength
8459 reduction and doing it this way will produce better code if the
8460 frame pointer or argument pointer is eliminated.
8462 fold-const.c will ensure that the constant is always in the inner
8463 PLUS_EXPR, so the only case we need to do anything about is if
8464 sp, ap, or fp is our second argument, in which case we must swap
8465 the innermost first argument and our second argument. */
8467 if (TREE_CODE (treeop0) == PLUS_EXPR
8468 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8469 && VAR_P (treeop1)
8470 && (DECL_RTL (treeop1) == frame_pointer_rtx
8471 || DECL_RTL (treeop1) == stack_pointer_rtx
8472 || DECL_RTL (treeop1) == arg_pointer_rtx))
8474 gcc_unreachable ();
8477 /* If the result is to be ptr_mode and we are adding an integer to
8478 something, we might be forming a constant. So try to use
8479 plus_constant. If it produces a sum and we can't accept it,
8480 use force_operand. This allows P = &ARR[const] to generate
8481 efficient code on machines where a SYMBOL_REF is not a valid
8482 address.
8484 If this is an EXPAND_SUM call, always return the sum. */
8485 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8486 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8488 if (modifier == EXPAND_STACK_PARM)
8489 target = 0;
8490 if (TREE_CODE (treeop0) == INTEGER_CST
8491 && HWI_COMPUTABLE_MODE_P (mode)
8492 && TREE_CONSTANT (treeop1))
8494 rtx constant_part;
8495 HOST_WIDE_INT wc;
8496 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8498 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8499 EXPAND_SUM);
8500 /* Use wi::shwi to ensure that the constant is
8501 truncated according to the mode of OP1, then sign extended
8502 to a HOST_WIDE_INT. Using the constant directly can result
8503 in non-canonical RTL in a 64x32 cross compile. */
8504 wc = TREE_INT_CST_LOW (treeop0);
8505 constant_part =
8506 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8507 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8508 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8509 op1 = force_operand (op1, target);
8510 return REDUCE_BIT_FIELD (op1);
8513 else if (TREE_CODE (treeop1) == INTEGER_CST
8514 && HWI_COMPUTABLE_MODE_P (mode)
8515 && TREE_CONSTANT (treeop0))
8517 rtx constant_part;
8518 HOST_WIDE_INT wc;
8519 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8521 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8522 (modifier == EXPAND_INITIALIZER
8523 ? EXPAND_INITIALIZER : EXPAND_SUM));
8524 if (! CONSTANT_P (op0))
8526 op1 = expand_expr (treeop1, NULL_RTX,
8527 VOIDmode, modifier);
8528 /* Return a PLUS if modifier says it's OK. */
8529 if (modifier == EXPAND_SUM
8530 || modifier == EXPAND_INITIALIZER)
8531 return simplify_gen_binary (PLUS, mode, op0, op1);
8532 goto binop2;
8534 /* Use wi::shwi to ensure that the constant is
8535 truncated according to the mode of OP1, then sign extended
8536 to a HOST_WIDE_INT. Using the constant directly can result
8537 in non-canonical RTL in a 64x32 cross compile. */
8538 wc = TREE_INT_CST_LOW (treeop1);
8539 constant_part
8540 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8541 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8542 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8543 op0 = force_operand (op0, target);
8544 return REDUCE_BIT_FIELD (op0);
8548 /* Use TER to expand pointer addition of a negated value
8549 as pointer subtraction. */
8550 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8551 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8552 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8553 && TREE_CODE (treeop1) == SSA_NAME
8554 && TYPE_MODE (TREE_TYPE (treeop0))
8555 == TYPE_MODE (TREE_TYPE (treeop1)))
8557 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8558 if (def)
8560 treeop1 = gimple_assign_rhs1 (def);
8561 code = MINUS_EXPR;
8562 goto do_minus;
8566 /* No sense saving up arithmetic to be done
8567 if it's all in the wrong mode to form part of an address.
8568 And force_operand won't know whether to sign-extend or
8569 zero-extend. */
8570 if (modifier != EXPAND_INITIALIZER
8571 && (modifier != EXPAND_SUM || mode != ptr_mode))
8573 expand_operands (treeop0, treeop1,
8574 subtarget, &op0, &op1, modifier);
8575 if (op0 == const0_rtx)
8576 return op1;
8577 if (op1 == const0_rtx)
8578 return op0;
8579 goto binop2;
8582 expand_operands (treeop0, treeop1,
8583 subtarget, &op0, &op1, modifier);
8584 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8586 case MINUS_EXPR:
8587 case POINTER_DIFF_EXPR:
8588 do_minus:
8589 /* For initializers, we are allowed to return a MINUS of two
8590 symbolic constants. Here we handle all cases when both operands
8591 are constant. */
8592 /* Handle difference of two symbolic constants,
8593 for the sake of an initializer. */
8594 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8595 && really_constant_p (treeop0)
8596 && really_constant_p (treeop1))
8598 expand_operands (treeop0, treeop1,
8599 NULL_RTX, &op0, &op1, modifier);
8600 return simplify_gen_binary (MINUS, mode, op0, op1);
8603 /* No sense saving up arithmetic to be done
8604 if it's all in the wrong mode to form part of an address.
8605 And force_operand won't know whether to sign-extend or
8606 zero-extend. */
8607 if (modifier != EXPAND_INITIALIZER
8608 && (modifier != EXPAND_SUM || mode != ptr_mode))
8609 goto binop;
8611 expand_operands (treeop0, treeop1,
8612 subtarget, &op0, &op1, modifier);
8614 /* Convert A - const to A + (-const). */
8615 if (CONST_INT_P (op1))
8617 op1 = negate_rtx (mode, op1);
8618 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8621 goto binop2;
8623 case WIDEN_MULT_PLUS_EXPR:
8624 case WIDEN_MULT_MINUS_EXPR:
8625 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8626 op2 = expand_normal (treeop2);
8627 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8628 target, unsignedp);
8629 return target;
8631 case WIDEN_MULT_EXPR:
8632 /* If first operand is constant, swap them.
8633 Thus the following special case checks need only
8634 check the second operand. */
8635 if (TREE_CODE (treeop0) == INTEGER_CST)
8636 std::swap (treeop0, treeop1);
8638 /* First, check if we have a multiplication of one signed and one
8639 unsigned operand. */
8640 if (TREE_CODE (treeop1) != INTEGER_CST
8641 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8642 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8644 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8645 this_optab = usmul_widen_optab;
8646 if (find_widening_optab_handler (this_optab, mode, innermode)
8647 != CODE_FOR_nothing)
8649 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8650 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8651 EXPAND_NORMAL);
8652 else
8653 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8654 EXPAND_NORMAL);
8655 /* op0 and op1 might still be constant, despite the above
8656 != INTEGER_CST check. Handle it. */
8657 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8659 op0 = convert_modes (innermode, mode, op0, true);
8660 op1 = convert_modes (innermode, mode, op1, false);
8661 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8662 target, unsignedp));
8664 goto binop3;
8667 /* Check for a multiplication with matching signedness. */
8668 else if ((TREE_CODE (treeop1) == INTEGER_CST
8669 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8670 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8671 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8673 tree op0type = TREE_TYPE (treeop0);
8674 machine_mode innermode = TYPE_MODE (op0type);
8675 bool zextend_p = TYPE_UNSIGNED (op0type);
8676 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8677 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8679 if (TREE_CODE (treeop0) != INTEGER_CST)
8681 if (find_widening_optab_handler (this_optab, mode, innermode)
8682 != CODE_FOR_nothing)
8684 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8685 EXPAND_NORMAL);
8686 /* op0 and op1 might still be constant, despite the above
8687 != INTEGER_CST check. Handle it. */
8688 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8690 widen_mult_const:
8691 op0 = convert_modes (innermode, mode, op0, zextend_p);
8693 = convert_modes (innermode, mode, op1,
8694 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8695 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8696 target,
8697 unsignedp));
8699 temp = expand_widening_mult (mode, op0, op1, target,
8700 unsignedp, this_optab);
8701 return REDUCE_BIT_FIELD (temp);
8703 if (find_widening_optab_handler (other_optab, mode, innermode)
8704 != CODE_FOR_nothing
8705 && innermode == word_mode)
8707 rtx htem, hipart;
8708 op0 = expand_normal (treeop0);
8709 if (TREE_CODE (treeop1) == INTEGER_CST)
8710 op1 = convert_modes (word_mode, mode,
8711 expand_normal (treeop1),
8712 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8713 else
8714 op1 = expand_normal (treeop1);
8715 /* op0 and op1 might still be constant, despite the above
8716 != INTEGER_CST check. Handle it. */
8717 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8718 goto widen_mult_const;
8719 temp = expand_binop (mode, other_optab, op0, op1, target,
8720 unsignedp, OPTAB_LIB_WIDEN);
8721 hipart = gen_highpart (word_mode, temp);
8722 htem = expand_mult_highpart_adjust (word_mode, hipart,
8723 op0, op1, hipart,
8724 zextend_p);
8725 if (htem != hipart)
8726 emit_move_insn (hipart, htem);
8727 return REDUCE_BIT_FIELD (temp);
8731 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8732 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8733 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8734 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8736 case FMA_EXPR:
8738 optab opt = fma_optab;
8739 gimple *def0, *def2;
8741 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8742 call. */
8743 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8745 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8746 tree call_expr;
8748 gcc_assert (fn != NULL_TREE);
8749 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8750 return expand_builtin (call_expr, target, subtarget, mode, false);
8753 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8754 /* The multiplication is commutative - look at its 2nd operand
8755 if the first isn't fed by a negate. */
8756 if (!def0)
8758 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8759 /* Swap operands if the 2nd operand is fed by a negate. */
8760 if (def0)
8761 std::swap (treeop0, treeop1);
8763 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8765 op0 = op2 = NULL;
8767 if (def0 && def2
8768 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8770 opt = fnms_optab;
8771 op0 = expand_normal (gimple_assign_rhs1 (def0));
8772 op2 = expand_normal (gimple_assign_rhs1 (def2));
8774 else if (def0
8775 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8777 opt = fnma_optab;
8778 op0 = expand_normal (gimple_assign_rhs1 (def0));
8780 else if (def2
8781 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8783 opt = fms_optab;
8784 op2 = expand_normal (gimple_assign_rhs1 (def2));
8787 if (op0 == NULL)
8788 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8789 if (op2 == NULL)
8790 op2 = expand_normal (treeop2);
8791 op1 = expand_normal (treeop1);
8793 return expand_ternary_op (TYPE_MODE (type), opt,
8794 op0, op1, op2, target, 0);
8797 case MULT_EXPR:
8798 /* If this is a fixed-point operation, then we cannot use the code
8799 below because "expand_mult" doesn't support sat/no-sat fixed-point
8800 multiplications. */
8801 if (ALL_FIXED_POINT_MODE_P (mode))
8802 goto binop;
8804 /* If first operand is constant, swap them.
8805 Thus the following special case checks need only
8806 check the second operand. */
8807 if (TREE_CODE (treeop0) == INTEGER_CST)
8808 std::swap (treeop0, treeop1);
8810 /* Attempt to return something suitable for generating an
8811 indexed address, for machines that support that. */
8813 if (modifier == EXPAND_SUM && mode == ptr_mode
8814 && tree_fits_shwi_p (treeop1))
8816 tree exp1 = treeop1;
8818 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8819 EXPAND_SUM);
8821 if (!REG_P (op0))
8822 op0 = force_operand (op0, NULL_RTX);
8823 if (!REG_P (op0))
8824 op0 = copy_to_mode_reg (mode, op0);
8826 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8827 gen_int_mode (tree_to_shwi (exp1),
8828 TYPE_MODE (TREE_TYPE (exp1)))));
8831 if (modifier == EXPAND_STACK_PARM)
8832 target = 0;
8834 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8835 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8837 case TRUNC_MOD_EXPR:
8838 case FLOOR_MOD_EXPR:
8839 case CEIL_MOD_EXPR:
8840 case ROUND_MOD_EXPR:
8842 case TRUNC_DIV_EXPR:
8843 case FLOOR_DIV_EXPR:
8844 case CEIL_DIV_EXPR:
8845 case ROUND_DIV_EXPR:
8846 case EXACT_DIV_EXPR:
8848 /* If this is a fixed-point operation, then we cannot use the code
8849 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8850 divisions. */
8851 if (ALL_FIXED_POINT_MODE_P (mode))
8852 goto binop;
8854 if (modifier == EXPAND_STACK_PARM)
8855 target = 0;
8856 /* Possible optimization: compute the dividend with EXPAND_SUM
8857 then if the divisor is constant can optimize the case
8858 where some terms of the dividend have coeffs divisible by it. */
8859 expand_operands (treeop0, treeop1,
8860 subtarget, &op0, &op1, EXPAND_NORMAL);
8861 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8862 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8863 if (SCALAR_INT_MODE_P (mode)
8864 && optimize >= 2
8865 && get_range_pos_neg (treeop0) == 1
8866 && get_range_pos_neg (treeop1) == 1)
8868 /* If both arguments are known to be positive when interpreted
8869 as signed, we can expand it as both signed and unsigned
8870 division or modulo. Choose the cheaper sequence in that case. */
8871 bool speed_p = optimize_insn_for_speed_p ();
8872 do_pending_stack_adjust ();
8873 start_sequence ();
8874 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8875 rtx_insn *uns_insns = get_insns ();
8876 end_sequence ();
8877 start_sequence ();
8878 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8879 rtx_insn *sgn_insns = get_insns ();
8880 end_sequence ();
8881 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8882 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8884 /* If costs are the same then use as tie breaker the other
8885 other factor. */
8886 if (uns_cost == sgn_cost)
8888 uns_cost = seq_cost (uns_insns, !speed_p);
8889 sgn_cost = seq_cost (sgn_insns, !speed_p);
8892 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8894 emit_insn (uns_insns);
8895 return uns_ret;
8897 emit_insn (sgn_insns);
8898 return sgn_ret;
8900 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8902 case RDIV_EXPR:
8903 goto binop;
8905 case MULT_HIGHPART_EXPR:
8906 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8907 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8908 gcc_assert (temp);
8909 return temp;
8911 case FIXED_CONVERT_EXPR:
8912 op0 = expand_normal (treeop0);
8913 if (target == 0 || modifier == EXPAND_STACK_PARM)
8914 target = gen_reg_rtx (mode);
8916 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8917 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8918 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8919 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8920 else
8921 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8922 return target;
8924 case FIX_TRUNC_EXPR:
8925 op0 = expand_normal (treeop0);
8926 if (target == 0 || modifier == EXPAND_STACK_PARM)
8927 target = gen_reg_rtx (mode);
8928 expand_fix (target, op0, unsignedp);
8929 return target;
8931 case FLOAT_EXPR:
8932 op0 = expand_normal (treeop0);
8933 if (target == 0 || modifier == EXPAND_STACK_PARM)
8934 target = gen_reg_rtx (mode);
8935 /* expand_float can't figure out what to do if FROM has VOIDmode.
8936 So give it the correct mode. With -O, cse will optimize this. */
8937 if (GET_MODE (op0) == VOIDmode)
8938 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8939 op0);
8940 expand_float (target, op0,
8941 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8942 return target;
8944 case NEGATE_EXPR:
8945 op0 = expand_expr (treeop0, subtarget,
8946 VOIDmode, EXPAND_NORMAL);
8947 if (modifier == EXPAND_STACK_PARM)
8948 target = 0;
8949 temp = expand_unop (mode,
8950 optab_for_tree_code (NEGATE_EXPR, type,
8951 optab_default),
8952 op0, target, 0);
8953 gcc_assert (temp);
8954 return REDUCE_BIT_FIELD (temp);
8956 case ABS_EXPR:
8957 op0 = expand_expr (treeop0, subtarget,
8958 VOIDmode, EXPAND_NORMAL);
8959 if (modifier == EXPAND_STACK_PARM)
8960 target = 0;
8962 /* ABS_EXPR is not valid for complex arguments. */
8963 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8964 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8966 /* Unsigned abs is simply the operand. Testing here means we don't
8967 risk generating incorrect code below. */
8968 if (TYPE_UNSIGNED (type))
8969 return op0;
8971 return expand_abs (mode, op0, target, unsignedp,
8972 safe_from_p (target, treeop0, 1));
8974 case MAX_EXPR:
8975 case MIN_EXPR:
8976 target = original_target;
8977 if (target == 0
8978 || modifier == EXPAND_STACK_PARM
8979 || (MEM_P (target) && MEM_VOLATILE_P (target))
8980 || GET_MODE (target) != mode
8981 || (REG_P (target)
8982 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8983 target = gen_reg_rtx (mode);
8984 expand_operands (treeop0, treeop1,
8985 target, &op0, &op1, EXPAND_NORMAL);
8987 /* First try to do it with a special MIN or MAX instruction.
8988 If that does not win, use a conditional jump to select the proper
8989 value. */
8990 this_optab = optab_for_tree_code (code, type, optab_default);
8991 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8992 OPTAB_WIDEN);
8993 if (temp != 0)
8994 return temp;
8996 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8997 and similarly for MAX <x, y>. */
8998 if (VECTOR_TYPE_P (type))
9000 tree t0 = make_tree (type, op0);
9001 tree t1 = make_tree (type, op1);
9002 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
9003 type, t0, t1);
9004 return expand_vec_cond_expr (type, comparison, t0, t1,
9005 original_target);
9008 /* At this point, a MEM target is no longer useful; we will get better
9009 code without it. */
9011 if (! REG_P (target))
9012 target = gen_reg_rtx (mode);
9014 /* If op1 was placed in target, swap op0 and op1. */
9015 if (target != op0 && target == op1)
9016 std::swap (op0, op1);
9018 /* We generate better code and avoid problems with op1 mentioning
9019 target by forcing op1 into a pseudo if it isn't a constant. */
9020 if (! CONSTANT_P (op1))
9021 op1 = force_reg (mode, op1);
9024 enum rtx_code comparison_code;
9025 rtx cmpop1 = op1;
9027 if (code == MAX_EXPR)
9028 comparison_code = unsignedp ? GEU : GE;
9029 else
9030 comparison_code = unsignedp ? LEU : LE;
9032 /* Canonicalize to comparisons against 0. */
9033 if (op1 == const1_rtx)
9035 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9036 or (a != 0 ? a : 1) for unsigned.
9037 For MIN we are safe converting (a <= 1 ? a : 1)
9038 into (a <= 0 ? a : 1) */
9039 cmpop1 = const0_rtx;
9040 if (code == MAX_EXPR)
9041 comparison_code = unsignedp ? NE : GT;
9043 if (op1 == constm1_rtx && !unsignedp)
9045 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9046 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9047 cmpop1 = const0_rtx;
9048 if (code == MIN_EXPR)
9049 comparison_code = LT;
9052 /* Use a conditional move if possible. */
9053 if (can_conditionally_move_p (mode))
9055 rtx insn;
9057 start_sequence ();
9059 /* Try to emit the conditional move. */
9060 insn = emit_conditional_move (target, comparison_code,
9061 op0, cmpop1, mode,
9062 op0, op1, mode,
9063 unsignedp);
9065 /* If we could do the conditional move, emit the sequence,
9066 and return. */
9067 if (insn)
9069 rtx_insn *seq = get_insns ();
9070 end_sequence ();
9071 emit_insn (seq);
9072 return target;
9075 /* Otherwise discard the sequence and fall back to code with
9076 branches. */
9077 end_sequence ();
9080 if (target != op0)
9081 emit_move_insn (target, op0);
9083 lab = gen_label_rtx ();
9084 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9085 unsignedp, mode, NULL_RTX, NULL, lab,
9086 profile_probability::uninitialized ());
9088 emit_move_insn (target, op1);
9089 emit_label (lab);
9090 return target;
9092 case BIT_NOT_EXPR:
9093 op0 = expand_expr (treeop0, subtarget,
9094 VOIDmode, EXPAND_NORMAL);
9095 if (modifier == EXPAND_STACK_PARM)
9096 target = 0;
9097 /* In case we have to reduce the result to bitfield precision
9098 for unsigned bitfield expand this as XOR with a proper constant
9099 instead. */
9100 if (reduce_bit_field && TYPE_UNSIGNED (type))
9102 int_mode = SCALAR_INT_TYPE_MODE (type);
9103 wide_int mask = wi::mask (TYPE_PRECISION (type),
9104 false, GET_MODE_PRECISION (int_mode));
9106 temp = expand_binop (int_mode, xor_optab, op0,
9107 immed_wide_int_const (mask, int_mode),
9108 target, 1, OPTAB_LIB_WIDEN);
9110 else
9111 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9112 gcc_assert (temp);
9113 return temp;
9115 /* ??? Can optimize bitwise operations with one arg constant.
9116 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9117 and (a bitwise1 b) bitwise2 b (etc)
9118 but that is probably not worth while. */
9120 case BIT_AND_EXPR:
9121 case BIT_IOR_EXPR:
9122 case BIT_XOR_EXPR:
9123 goto binop;
9125 case LROTATE_EXPR:
9126 case RROTATE_EXPR:
9127 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9128 || type_has_mode_precision_p (type));
9129 /* fall through */
9131 case LSHIFT_EXPR:
9132 case RSHIFT_EXPR:
9134 /* If this is a fixed-point operation, then we cannot use the code
9135 below because "expand_shift" doesn't support sat/no-sat fixed-point
9136 shifts. */
9137 if (ALL_FIXED_POINT_MODE_P (mode))
9138 goto binop;
9140 if (! safe_from_p (subtarget, treeop1, 1))
9141 subtarget = 0;
9142 if (modifier == EXPAND_STACK_PARM)
9143 target = 0;
9144 op0 = expand_expr (treeop0, subtarget,
9145 VOIDmode, EXPAND_NORMAL);
9147 /* Left shift optimization when shifting across word_size boundary.
9149 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9150 there isn't native instruction to support this wide mode
9151 left shift. Given below scenario:
9153 Type A = (Type) B << C
9155 |< T >|
9156 | dest_high | dest_low |
9158 | word_size |
9160 If the shift amount C caused we shift B to across the word
9161 size boundary, i.e part of B shifted into high half of
9162 destination register, and part of B remains in the low
9163 half, then GCC will use the following left shift expand
9164 logic:
9166 1. Initialize dest_low to B.
9167 2. Initialize every bit of dest_high to the sign bit of B.
9168 3. Logic left shift dest_low by C bit to finalize dest_low.
9169 The value of dest_low before this shift is kept in a temp D.
9170 4. Logic left shift dest_high by C.
9171 5. Logic right shift D by (word_size - C).
9172 6. Or the result of 4 and 5 to finalize dest_high.
9174 While, by checking gimple statements, if operand B is
9175 coming from signed extension, then we can simplify above
9176 expand logic into:
9178 1. dest_high = src_low >> (word_size - C).
9179 2. dest_low = src_low << C.
9181 We can use one arithmetic right shift to finish all the
9182 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9183 needed from 6 into 2.
9185 The case is similar for zero extension, except that we
9186 initialize dest_high to zero rather than copies of the sign
9187 bit from B. Furthermore, we need to use a logical right shift
9188 in this case.
9190 The choice of sign-extension versus zero-extension is
9191 determined entirely by whether or not B is signed and is
9192 independent of the current setting of unsignedp. */
9194 temp = NULL_RTX;
9195 if (code == LSHIFT_EXPR
9196 && target
9197 && REG_P (target)
9198 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9199 && mode == int_mode
9200 && TREE_CONSTANT (treeop1)
9201 && TREE_CODE (treeop0) == SSA_NAME)
9203 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9204 if (is_gimple_assign (def)
9205 && gimple_assign_rhs_code (def) == NOP_EXPR)
9207 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9208 (TREE_TYPE (gimple_assign_rhs1 (def)));
9210 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9211 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9212 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9213 >= GET_MODE_BITSIZE (word_mode)))
9215 rtx_insn *seq, *seq_old;
9216 unsigned int high_off = subreg_highpart_offset (word_mode,
9217 int_mode);
9218 bool extend_unsigned
9219 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9220 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9221 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9222 rtx dest_high = simplify_gen_subreg (word_mode, target,
9223 int_mode, high_off);
9224 HOST_WIDE_INT ramount = (BITS_PER_WORD
9225 - TREE_INT_CST_LOW (treeop1));
9226 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9228 start_sequence ();
9229 /* dest_high = src_low >> (word_size - C). */
9230 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9231 rshift, dest_high,
9232 extend_unsigned);
9233 if (temp != dest_high)
9234 emit_move_insn (dest_high, temp);
9236 /* dest_low = src_low << C. */
9237 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9238 treeop1, dest_low, unsignedp);
9239 if (temp != dest_low)
9240 emit_move_insn (dest_low, temp);
9242 seq = get_insns ();
9243 end_sequence ();
9244 temp = target ;
9246 if (have_insn_for (ASHIFT, int_mode))
9248 bool speed_p = optimize_insn_for_speed_p ();
9249 start_sequence ();
9250 rtx ret_old = expand_variable_shift (code, int_mode,
9251 op0, treeop1,
9252 target,
9253 unsignedp);
9255 seq_old = get_insns ();
9256 end_sequence ();
9257 if (seq_cost (seq, speed_p)
9258 >= seq_cost (seq_old, speed_p))
9260 seq = seq_old;
9261 temp = ret_old;
9264 emit_insn (seq);
9269 if (temp == NULL_RTX)
9270 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9271 unsignedp);
9272 if (code == LSHIFT_EXPR)
9273 temp = REDUCE_BIT_FIELD (temp);
9274 return temp;
9277 /* Could determine the answer when only additive constants differ. Also,
9278 the addition of one can be handled by changing the condition. */
9279 case LT_EXPR:
9280 case LE_EXPR:
9281 case GT_EXPR:
9282 case GE_EXPR:
9283 case EQ_EXPR:
9284 case NE_EXPR:
9285 case UNORDERED_EXPR:
9286 case ORDERED_EXPR:
9287 case UNLT_EXPR:
9288 case UNLE_EXPR:
9289 case UNGT_EXPR:
9290 case UNGE_EXPR:
9291 case UNEQ_EXPR:
9292 case LTGT_EXPR:
9294 temp = do_store_flag (ops,
9295 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9296 tmode != VOIDmode ? tmode : mode);
9297 if (temp)
9298 return temp;
9300 /* Use a compare and a jump for BLKmode comparisons, or for function
9301 type comparisons is have_canonicalize_funcptr_for_compare. */
9303 if ((target == 0
9304 || modifier == EXPAND_STACK_PARM
9305 || ! safe_from_p (target, treeop0, 1)
9306 || ! safe_from_p (target, treeop1, 1)
9307 /* Make sure we don't have a hard reg (such as function's return
9308 value) live across basic blocks, if not optimizing. */
9309 || (!optimize && REG_P (target)
9310 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9311 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9313 emit_move_insn (target, const0_rtx);
9315 rtx_code_label *lab1 = gen_label_rtx ();
9316 jumpifnot_1 (code, treeop0, treeop1, lab1,
9317 profile_probability::uninitialized ());
9319 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9320 emit_move_insn (target, constm1_rtx);
9321 else
9322 emit_move_insn (target, const1_rtx);
9324 emit_label (lab1);
9325 return target;
9327 case COMPLEX_EXPR:
9328 /* Get the rtx code of the operands. */
9329 op0 = expand_normal (treeop0);
9330 op1 = expand_normal (treeop1);
9332 if (!target)
9333 target = gen_reg_rtx (TYPE_MODE (type));
9334 else
9335 /* If target overlaps with op1, then either we need to force
9336 op1 into a pseudo (if target also overlaps with op0),
9337 or write the complex parts in reverse order. */
9338 switch (GET_CODE (target))
9340 case CONCAT:
9341 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9343 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9345 complex_expr_force_op1:
9346 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9347 emit_move_insn (temp, op1);
9348 op1 = temp;
9349 break;
9351 complex_expr_swap_order:
9352 /* Move the imaginary (op1) and real (op0) parts to their
9353 location. */
9354 write_complex_part (target, op1, true);
9355 write_complex_part (target, op0, false);
9357 return target;
9359 break;
9360 case MEM:
9361 temp = adjust_address_nv (target,
9362 GET_MODE_INNER (GET_MODE (target)), 0);
9363 if (reg_overlap_mentioned_p (temp, op1))
9365 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9366 temp = adjust_address_nv (target, imode,
9367 GET_MODE_SIZE (imode));
9368 if (reg_overlap_mentioned_p (temp, op0))
9369 goto complex_expr_force_op1;
9370 goto complex_expr_swap_order;
9372 break;
9373 default:
9374 if (reg_overlap_mentioned_p (target, op1))
9376 if (reg_overlap_mentioned_p (target, op0))
9377 goto complex_expr_force_op1;
9378 goto complex_expr_swap_order;
9380 break;
9383 /* Move the real (op0) and imaginary (op1) parts to their location. */
9384 write_complex_part (target, op0, false);
9385 write_complex_part (target, op1, true);
9387 return target;
9389 case WIDEN_SUM_EXPR:
9391 tree oprnd0 = treeop0;
9392 tree oprnd1 = treeop1;
9394 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9395 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9396 target, unsignedp);
9397 return target;
9400 case VEC_UNPACK_HI_EXPR:
9401 case VEC_UNPACK_LO_EXPR:
9403 op0 = expand_normal (treeop0);
9404 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9405 target, unsignedp);
9406 gcc_assert (temp);
9407 return temp;
9410 case VEC_UNPACK_FLOAT_HI_EXPR:
9411 case VEC_UNPACK_FLOAT_LO_EXPR:
9413 op0 = expand_normal (treeop0);
9414 /* The signedness is determined from input operand. */
9415 temp = expand_widen_pattern_expr
9416 (ops, op0, NULL_RTX, NULL_RTX,
9417 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9419 gcc_assert (temp);
9420 return temp;
9423 case VEC_WIDEN_MULT_HI_EXPR:
9424 case VEC_WIDEN_MULT_LO_EXPR:
9425 case VEC_WIDEN_MULT_EVEN_EXPR:
9426 case VEC_WIDEN_MULT_ODD_EXPR:
9427 case VEC_WIDEN_LSHIFT_HI_EXPR:
9428 case VEC_WIDEN_LSHIFT_LO_EXPR:
9429 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9430 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9431 target, unsignedp);
9432 gcc_assert (target);
9433 return target;
9435 case VEC_PACK_TRUNC_EXPR:
9436 case VEC_PACK_SAT_EXPR:
9437 case VEC_PACK_FIX_TRUNC_EXPR:
9438 mode = TYPE_MODE (TREE_TYPE (treeop0));
9439 goto binop;
9441 case VEC_PERM_EXPR:
9442 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9443 op2 = expand_normal (treeop2);
9445 /* Careful here: if the target doesn't support integral vector modes,
9446 a constant selection vector could wind up smooshed into a normal
9447 integral constant. */
9448 if (CONSTANT_P (op2) && !VECTOR_MODE_P (GET_MODE (op2)))
9450 tree sel_type = TREE_TYPE (treeop2);
9451 machine_mode vmode
9452 = mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (sel_type)),
9453 TYPE_VECTOR_SUBPARTS (sel_type)).require ();
9454 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9455 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9456 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9458 else
9459 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9461 temp = expand_vec_perm (mode, op0, op1, op2, target);
9462 gcc_assert (temp);
9463 return temp;
9465 case DOT_PROD_EXPR:
9467 tree oprnd0 = treeop0;
9468 tree oprnd1 = treeop1;
9469 tree oprnd2 = treeop2;
9470 rtx op2;
9472 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9473 op2 = expand_normal (oprnd2);
9474 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9475 target, unsignedp);
9476 return target;
9479 case SAD_EXPR:
9481 tree oprnd0 = treeop0;
9482 tree oprnd1 = treeop1;
9483 tree oprnd2 = treeop2;
9484 rtx op2;
9486 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9487 op2 = expand_normal (oprnd2);
9488 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9489 target, unsignedp);
9490 return target;
9493 case REALIGN_LOAD_EXPR:
9495 tree oprnd0 = treeop0;
9496 tree oprnd1 = treeop1;
9497 tree oprnd2 = treeop2;
9498 rtx op2;
9500 this_optab = optab_for_tree_code (code, type, optab_default);
9501 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9502 op2 = expand_normal (oprnd2);
9503 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9504 target, unsignedp);
9505 gcc_assert (temp);
9506 return temp;
9509 case COND_EXPR:
9511 /* A COND_EXPR with its type being VOID_TYPE represents a
9512 conditional jump and is handled in
9513 expand_gimple_cond_expr. */
9514 gcc_assert (!VOID_TYPE_P (type));
9516 /* Note that COND_EXPRs whose type is a structure or union
9517 are required to be constructed to contain assignments of
9518 a temporary variable, so that we can evaluate them here
9519 for side effect only. If type is void, we must do likewise. */
9521 gcc_assert (!TREE_ADDRESSABLE (type)
9522 && !ignore
9523 && TREE_TYPE (treeop1) != void_type_node
9524 && TREE_TYPE (treeop2) != void_type_node);
9526 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9527 if (temp)
9528 return temp;
9530 /* If we are not to produce a result, we have no target. Otherwise,
9531 if a target was specified use it; it will not be used as an
9532 intermediate target unless it is safe. If no target, use a
9533 temporary. */
9535 if (modifier != EXPAND_STACK_PARM
9536 && original_target
9537 && safe_from_p (original_target, treeop0, 1)
9538 && GET_MODE (original_target) == mode
9539 && !MEM_P (original_target))
9540 temp = original_target;
9541 else
9542 temp = assign_temp (type, 0, 1);
9544 do_pending_stack_adjust ();
9545 NO_DEFER_POP;
9546 rtx_code_label *lab0 = gen_label_rtx ();
9547 rtx_code_label *lab1 = gen_label_rtx ();
9548 jumpifnot (treeop0, lab0,
9549 profile_probability::uninitialized ());
9550 store_expr (treeop1, temp,
9551 modifier == EXPAND_STACK_PARM,
9552 false, false);
9554 emit_jump_insn (targetm.gen_jump (lab1));
9555 emit_barrier ();
9556 emit_label (lab0);
9557 store_expr (treeop2, temp,
9558 modifier == EXPAND_STACK_PARM,
9559 false, false);
9561 emit_label (lab1);
9562 OK_DEFER_POP;
9563 return temp;
9566 case VEC_COND_EXPR:
9567 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9568 return target;
9570 case BIT_INSERT_EXPR:
9572 unsigned bitpos = tree_to_uhwi (treeop2);
9573 unsigned bitsize;
9574 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9575 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9576 else
9577 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9578 rtx op0 = expand_normal (treeop0);
9579 rtx op1 = expand_normal (treeop1);
9580 rtx dst = gen_reg_rtx (mode);
9581 emit_move_insn (dst, op0);
9582 store_bit_field (dst, bitsize, bitpos, 0, 0,
9583 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9584 return dst;
9587 default:
9588 gcc_unreachable ();
9591 /* Here to do an ordinary binary operator. */
9592 binop:
9593 expand_operands (treeop0, treeop1,
9594 subtarget, &op0, &op1, EXPAND_NORMAL);
9595 binop2:
9596 this_optab = optab_for_tree_code (code, type, optab_default);
9597 binop3:
9598 if (modifier == EXPAND_STACK_PARM)
9599 target = 0;
9600 temp = expand_binop (mode, this_optab, op0, op1, target,
9601 unsignedp, OPTAB_LIB_WIDEN);
9602 gcc_assert (temp);
9603 /* Bitwise operations do not need bitfield reduction as we expect their
9604 operands being properly truncated. */
9605 if (code == BIT_XOR_EXPR
9606 || code == BIT_AND_EXPR
9607 || code == BIT_IOR_EXPR)
9608 return temp;
9609 return REDUCE_BIT_FIELD (temp);
9611 #undef REDUCE_BIT_FIELD
9614 /* Return TRUE if expression STMT is suitable for replacement.
9615 Never consider memory loads as replaceable, because those don't ever lead
9616 into constant expressions. */
9618 static bool
9619 stmt_is_replaceable_p (gimple *stmt)
9621 if (ssa_is_replaceable_p (stmt))
9623 /* Don't move around loads. */
9624 if (!gimple_assign_single_p (stmt)
9625 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9626 return true;
9628 return false;
9632 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9633 enum expand_modifier modifier, rtx *alt_rtl,
9634 bool inner_reference_p)
9636 rtx op0, op1, temp, decl_rtl;
9637 tree type;
9638 int unsignedp;
9639 machine_mode mode, dmode;
9640 enum tree_code code = TREE_CODE (exp);
9641 rtx subtarget, original_target;
9642 int ignore;
9643 tree context;
9644 bool reduce_bit_field;
9645 location_t loc = EXPR_LOCATION (exp);
9646 struct separate_ops ops;
9647 tree treeop0, treeop1, treeop2;
9648 tree ssa_name = NULL_TREE;
9649 gimple *g;
9651 type = TREE_TYPE (exp);
9652 mode = TYPE_MODE (type);
9653 unsignedp = TYPE_UNSIGNED (type);
9655 treeop0 = treeop1 = treeop2 = NULL_TREE;
9656 if (!VL_EXP_CLASS_P (exp))
9657 switch (TREE_CODE_LENGTH (code))
9659 default:
9660 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9661 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9662 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9663 case 0: break;
9665 ops.code = code;
9666 ops.type = type;
9667 ops.op0 = treeop0;
9668 ops.op1 = treeop1;
9669 ops.op2 = treeop2;
9670 ops.location = loc;
9672 ignore = (target == const0_rtx
9673 || ((CONVERT_EXPR_CODE_P (code)
9674 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9675 && TREE_CODE (type) == VOID_TYPE));
9677 /* An operation in what may be a bit-field type needs the
9678 result to be reduced to the precision of the bit-field type,
9679 which is narrower than that of the type's mode. */
9680 reduce_bit_field = (!ignore
9681 && INTEGRAL_TYPE_P (type)
9682 && !type_has_mode_precision_p (type));
9684 /* If we are going to ignore this result, we need only do something
9685 if there is a side-effect somewhere in the expression. If there
9686 is, short-circuit the most common cases here. Note that we must
9687 not call expand_expr with anything but const0_rtx in case this
9688 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9690 if (ignore)
9692 if (! TREE_SIDE_EFFECTS (exp))
9693 return const0_rtx;
9695 /* Ensure we reference a volatile object even if value is ignored, but
9696 don't do this if all we are doing is taking its address. */
9697 if (TREE_THIS_VOLATILE (exp)
9698 && TREE_CODE (exp) != FUNCTION_DECL
9699 && mode != VOIDmode && mode != BLKmode
9700 && modifier != EXPAND_CONST_ADDRESS)
9702 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9703 if (MEM_P (temp))
9704 copy_to_reg (temp);
9705 return const0_rtx;
9708 if (TREE_CODE_CLASS (code) == tcc_unary
9709 || code == BIT_FIELD_REF
9710 || code == COMPONENT_REF
9711 || code == INDIRECT_REF)
9712 return expand_expr (treeop0, const0_rtx, VOIDmode,
9713 modifier);
9715 else if (TREE_CODE_CLASS (code) == tcc_binary
9716 || TREE_CODE_CLASS (code) == tcc_comparison
9717 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9719 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9720 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9721 return const0_rtx;
9724 target = 0;
9727 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9728 target = 0;
9730 /* Use subtarget as the target for operand 0 of a binary operation. */
9731 subtarget = get_subtarget (target);
9732 original_target = target;
9734 switch (code)
9736 case LABEL_DECL:
9738 tree function = decl_function_context (exp);
9740 temp = label_rtx (exp);
9741 temp = gen_rtx_LABEL_REF (Pmode, temp);
9743 if (function != current_function_decl
9744 && function != 0)
9745 LABEL_REF_NONLOCAL_P (temp) = 1;
9747 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9748 return temp;
9751 case SSA_NAME:
9752 /* ??? ivopts calls expander, without any preparation from
9753 out-of-ssa. So fake instructions as if this was an access to the
9754 base variable. This unnecessarily allocates a pseudo, see how we can
9755 reuse it, if partition base vars have it set already. */
9756 if (!currently_expanding_to_rtl)
9758 tree var = SSA_NAME_VAR (exp);
9759 if (var && DECL_RTL_SET_P (var))
9760 return DECL_RTL (var);
9761 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9762 LAST_VIRTUAL_REGISTER + 1);
9765 g = get_gimple_for_ssa_name (exp);
9766 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9767 if (g == NULL
9768 && modifier == EXPAND_INITIALIZER
9769 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9770 && (optimize || !SSA_NAME_VAR (exp)
9771 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9772 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9773 g = SSA_NAME_DEF_STMT (exp);
9774 if (g)
9776 rtx r;
9777 location_t saved_loc = curr_insn_location ();
9778 location_t loc = gimple_location (g);
9779 if (loc != UNKNOWN_LOCATION)
9780 set_curr_insn_location (loc);
9781 ops.code = gimple_assign_rhs_code (g);
9782 switch (get_gimple_rhs_class (ops.code))
9784 case GIMPLE_TERNARY_RHS:
9785 ops.op2 = gimple_assign_rhs3 (g);
9786 /* Fallthru */
9787 case GIMPLE_BINARY_RHS:
9788 ops.op1 = gimple_assign_rhs2 (g);
9790 /* Try to expand conditonal compare. */
9791 if (targetm.gen_ccmp_first)
9793 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9794 r = expand_ccmp_expr (g, mode);
9795 if (r)
9796 break;
9798 /* Fallthru */
9799 case GIMPLE_UNARY_RHS:
9800 ops.op0 = gimple_assign_rhs1 (g);
9801 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9802 ops.location = loc;
9803 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9804 break;
9805 case GIMPLE_SINGLE_RHS:
9807 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9808 tmode, modifier, alt_rtl,
9809 inner_reference_p);
9810 break;
9812 default:
9813 gcc_unreachable ();
9815 set_curr_insn_location (saved_loc);
9816 if (REG_P (r) && !REG_EXPR (r))
9817 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9818 return r;
9821 ssa_name = exp;
9822 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9823 exp = SSA_NAME_VAR (ssa_name);
9824 goto expand_decl_rtl;
9826 case PARM_DECL:
9827 case VAR_DECL:
9828 /* If a static var's type was incomplete when the decl was written,
9829 but the type is complete now, lay out the decl now. */
9830 if (DECL_SIZE (exp) == 0
9831 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9832 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9833 layout_decl (exp, 0);
9835 /* fall through */
9837 case FUNCTION_DECL:
9838 case RESULT_DECL:
9839 decl_rtl = DECL_RTL (exp);
9840 expand_decl_rtl:
9841 gcc_assert (decl_rtl);
9843 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9844 settings for VECTOR_TYPE_P that might switch for the function. */
9845 if (currently_expanding_to_rtl
9846 && code == VAR_DECL && MEM_P (decl_rtl)
9847 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9848 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9849 else
9850 decl_rtl = copy_rtx (decl_rtl);
9852 /* Record writes to register variables. */
9853 if (modifier == EXPAND_WRITE
9854 && REG_P (decl_rtl)
9855 && HARD_REGISTER_P (decl_rtl))
9856 add_to_hard_reg_set (&crtl->asm_clobbers,
9857 GET_MODE (decl_rtl), REGNO (decl_rtl));
9859 /* Ensure variable marked as used even if it doesn't go through
9860 a parser. If it hasn't be used yet, write out an external
9861 definition. */
9862 if (exp)
9863 TREE_USED (exp) = 1;
9865 /* Show we haven't gotten RTL for this yet. */
9866 temp = 0;
9868 /* Variables inherited from containing functions should have
9869 been lowered by this point. */
9870 if (exp)
9871 context = decl_function_context (exp);
9872 gcc_assert (!exp
9873 || SCOPE_FILE_SCOPE_P (context)
9874 || context == current_function_decl
9875 || TREE_STATIC (exp)
9876 || DECL_EXTERNAL (exp)
9877 /* ??? C++ creates functions that are not TREE_STATIC. */
9878 || TREE_CODE (exp) == FUNCTION_DECL);
9880 /* This is the case of an array whose size is to be determined
9881 from its initializer, while the initializer is still being parsed.
9882 ??? We aren't parsing while expanding anymore. */
9884 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9885 temp = validize_mem (decl_rtl);
9887 /* If DECL_RTL is memory, we are in the normal case and the
9888 address is not valid, get the address into a register. */
9890 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9892 if (alt_rtl)
9893 *alt_rtl = decl_rtl;
9894 decl_rtl = use_anchored_address (decl_rtl);
9895 if (modifier != EXPAND_CONST_ADDRESS
9896 && modifier != EXPAND_SUM
9897 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9898 : GET_MODE (decl_rtl),
9899 XEXP (decl_rtl, 0),
9900 MEM_ADDR_SPACE (decl_rtl)))
9901 temp = replace_equiv_address (decl_rtl,
9902 copy_rtx (XEXP (decl_rtl, 0)));
9905 /* If we got something, return it. But first, set the alignment
9906 if the address is a register. */
9907 if (temp != 0)
9909 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9910 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9912 return temp;
9915 if (exp)
9916 dmode = DECL_MODE (exp);
9917 else
9918 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9920 /* If the mode of DECL_RTL does not match that of the decl,
9921 there are two cases: we are dealing with a BLKmode value
9922 that is returned in a register, or we are dealing with
9923 a promoted value. In the latter case, return a SUBREG
9924 of the wanted mode, but mark it so that we know that it
9925 was already extended. */
9926 if (REG_P (decl_rtl)
9927 && dmode != BLKmode
9928 && GET_MODE (decl_rtl) != dmode)
9930 machine_mode pmode;
9932 /* Get the signedness to be used for this variable. Ensure we get
9933 the same mode we got when the variable was declared. */
9934 if (code != SSA_NAME)
9935 pmode = promote_decl_mode (exp, &unsignedp);
9936 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9937 && gimple_code (g) == GIMPLE_CALL
9938 && !gimple_call_internal_p (g))
9939 pmode = promote_function_mode (type, mode, &unsignedp,
9940 gimple_call_fntype (g),
9942 else
9943 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9944 gcc_assert (GET_MODE (decl_rtl) == pmode);
9946 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9947 SUBREG_PROMOTED_VAR_P (temp) = 1;
9948 SUBREG_PROMOTED_SET (temp, unsignedp);
9949 return temp;
9952 return decl_rtl;
9954 case INTEGER_CST:
9956 /* Given that TYPE_PRECISION (type) is not always equal to
9957 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9958 the former to the latter according to the signedness of the
9959 type. */
9960 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
9961 temp = immed_wide_int_const
9962 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
9963 return temp;
9966 case VECTOR_CST:
9968 tree tmp = NULL_TREE;
9969 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9970 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9971 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9972 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9973 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9974 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9975 return const_vector_from_tree (exp);
9976 scalar_int_mode int_mode;
9977 if (is_int_mode (mode, &int_mode))
9979 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9980 return const_scalar_mask_from_tree (int_mode, exp);
9981 else
9983 tree type_for_mode
9984 = lang_hooks.types.type_for_mode (int_mode, 1);
9985 if (type_for_mode)
9986 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9987 type_for_mode, exp);
9990 if (!tmp)
9992 vec<constructor_elt, va_gc> *v;
9993 unsigned i;
9994 vec_alloc (v, VECTOR_CST_NELTS (exp));
9995 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9996 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9997 tmp = build_constructor (type, v);
9999 return expand_expr (tmp, ignore ? const0_rtx : target,
10000 tmode, modifier);
10003 case CONST_DECL:
10004 if (modifier == EXPAND_WRITE)
10006 /* Writing into CONST_DECL is always invalid, but handle it
10007 gracefully. */
10008 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10009 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10010 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10011 EXPAND_NORMAL, as);
10012 op0 = memory_address_addr_space (mode, op0, as);
10013 temp = gen_rtx_MEM (mode, op0);
10014 set_mem_addr_space (temp, as);
10015 return temp;
10017 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10019 case REAL_CST:
10020 /* If optimized, generate immediate CONST_DOUBLE
10021 which will be turned into memory by reload if necessary.
10023 We used to force a register so that loop.c could see it. But
10024 this does not allow gen_* patterns to perform optimizations with
10025 the constants. It also produces two insns in cases like "x = 1.0;".
10026 On most machines, floating-point constants are not permitted in
10027 many insns, so we'd end up copying it to a register in any case.
10029 Now, we do the copying in expand_binop, if appropriate. */
10030 return const_double_from_real_value (TREE_REAL_CST (exp),
10031 TYPE_MODE (TREE_TYPE (exp)));
10033 case FIXED_CST:
10034 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10035 TYPE_MODE (TREE_TYPE (exp)));
10037 case COMPLEX_CST:
10038 /* Handle evaluating a complex constant in a CONCAT target. */
10039 if (original_target && GET_CODE (original_target) == CONCAT)
10041 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10042 rtx rtarg, itarg;
10044 rtarg = XEXP (original_target, 0);
10045 itarg = XEXP (original_target, 1);
10047 /* Move the real and imaginary parts separately. */
10048 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10049 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10051 if (op0 != rtarg)
10052 emit_move_insn (rtarg, op0);
10053 if (op1 != itarg)
10054 emit_move_insn (itarg, op1);
10056 return original_target;
10059 /* fall through */
10061 case STRING_CST:
10062 temp = expand_expr_constant (exp, 1, modifier);
10064 /* temp contains a constant address.
10065 On RISC machines where a constant address isn't valid,
10066 make some insns to get that address into a register. */
10067 if (modifier != EXPAND_CONST_ADDRESS
10068 && modifier != EXPAND_INITIALIZER
10069 && modifier != EXPAND_SUM
10070 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10071 MEM_ADDR_SPACE (temp)))
10072 return replace_equiv_address (temp,
10073 copy_rtx (XEXP (temp, 0)));
10074 return temp;
10076 case SAVE_EXPR:
10078 tree val = treeop0;
10079 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10080 inner_reference_p);
10082 if (!SAVE_EXPR_RESOLVED_P (exp))
10084 /* We can indeed still hit this case, typically via builtin
10085 expanders calling save_expr immediately before expanding
10086 something. Assume this means that we only have to deal
10087 with non-BLKmode values. */
10088 gcc_assert (GET_MODE (ret) != BLKmode);
10090 val = build_decl (curr_insn_location (),
10091 VAR_DECL, NULL, TREE_TYPE (exp));
10092 DECL_ARTIFICIAL (val) = 1;
10093 DECL_IGNORED_P (val) = 1;
10094 treeop0 = val;
10095 TREE_OPERAND (exp, 0) = treeop0;
10096 SAVE_EXPR_RESOLVED_P (exp) = 1;
10098 if (!CONSTANT_P (ret))
10099 ret = copy_to_reg (ret);
10100 SET_DECL_RTL (val, ret);
10103 return ret;
10107 case CONSTRUCTOR:
10108 /* If we don't need the result, just ensure we evaluate any
10109 subexpressions. */
10110 if (ignore)
10112 unsigned HOST_WIDE_INT idx;
10113 tree value;
10115 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10116 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10118 return const0_rtx;
10121 return expand_constructor (exp, target, modifier, false);
10123 case TARGET_MEM_REF:
10125 addr_space_t as
10126 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10127 enum insn_code icode;
10128 unsigned int align;
10130 op0 = addr_for_mem_ref (exp, as, true);
10131 op0 = memory_address_addr_space (mode, op0, as);
10132 temp = gen_rtx_MEM (mode, op0);
10133 set_mem_attributes (temp, exp, 0);
10134 set_mem_addr_space (temp, as);
10135 align = get_object_alignment (exp);
10136 if (modifier != EXPAND_WRITE
10137 && modifier != EXPAND_MEMORY
10138 && mode != BLKmode
10139 && align < GET_MODE_ALIGNMENT (mode)
10140 /* If the target does not have special handling for unaligned
10141 loads of mode then it can use regular moves for them. */
10142 && ((icode = optab_handler (movmisalign_optab, mode))
10143 != CODE_FOR_nothing))
10145 struct expand_operand ops[2];
10147 /* We've already validated the memory, and we're creating a
10148 new pseudo destination. The predicates really can't fail,
10149 nor can the generator. */
10150 create_output_operand (&ops[0], NULL_RTX, mode);
10151 create_fixed_operand (&ops[1], temp);
10152 expand_insn (icode, 2, ops);
10153 temp = ops[0].value;
10155 return temp;
10158 case MEM_REF:
10160 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10161 addr_space_t as
10162 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10163 machine_mode address_mode;
10164 tree base = TREE_OPERAND (exp, 0);
10165 gimple *def_stmt;
10166 enum insn_code icode;
10167 unsigned align;
10168 /* Handle expansion of non-aliased memory with non-BLKmode. That
10169 might end up in a register. */
10170 if (mem_ref_refers_to_non_mem_p (exp))
10172 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10173 base = TREE_OPERAND (base, 0);
10174 if (offset == 0
10175 && !reverse
10176 && tree_fits_uhwi_p (TYPE_SIZE (type))
10177 && (GET_MODE_BITSIZE (DECL_MODE (base))
10178 == tree_to_uhwi (TYPE_SIZE (type))))
10179 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10180 target, tmode, modifier);
10181 if (TYPE_MODE (type) == BLKmode)
10183 temp = assign_stack_temp (DECL_MODE (base),
10184 GET_MODE_SIZE (DECL_MODE (base)));
10185 store_expr (base, temp, 0, false, false);
10186 temp = adjust_address (temp, BLKmode, offset);
10187 set_mem_size (temp, int_size_in_bytes (type));
10188 return temp;
10190 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10191 bitsize_int (offset * BITS_PER_UNIT));
10192 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10193 return expand_expr (exp, target, tmode, modifier);
10195 address_mode = targetm.addr_space.address_mode (as);
10196 base = TREE_OPERAND (exp, 0);
10197 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10199 tree mask = gimple_assign_rhs2 (def_stmt);
10200 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10201 gimple_assign_rhs1 (def_stmt), mask);
10202 TREE_OPERAND (exp, 0) = base;
10204 align = get_object_alignment (exp);
10205 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10206 op0 = memory_address_addr_space (mode, op0, as);
10207 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10209 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10210 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10211 op0 = memory_address_addr_space (mode, op0, as);
10213 temp = gen_rtx_MEM (mode, op0);
10214 set_mem_attributes (temp, exp, 0);
10215 set_mem_addr_space (temp, as);
10216 if (TREE_THIS_VOLATILE (exp))
10217 MEM_VOLATILE_P (temp) = 1;
10218 if (modifier != EXPAND_WRITE
10219 && modifier != EXPAND_MEMORY
10220 && !inner_reference_p
10221 && mode != BLKmode
10222 && align < GET_MODE_ALIGNMENT (mode))
10224 if ((icode = optab_handler (movmisalign_optab, mode))
10225 != CODE_FOR_nothing)
10227 struct expand_operand ops[2];
10229 /* We've already validated the memory, and we're creating a
10230 new pseudo destination. The predicates really can't fail,
10231 nor can the generator. */
10232 create_output_operand (&ops[0], NULL_RTX, mode);
10233 create_fixed_operand (&ops[1], temp);
10234 expand_insn (icode, 2, ops);
10235 temp = ops[0].value;
10237 else if (targetm.slow_unaligned_access (mode, align))
10238 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10239 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10240 (modifier == EXPAND_STACK_PARM
10241 ? NULL_RTX : target),
10242 mode, mode, false, alt_rtl);
10244 if (reverse
10245 && modifier != EXPAND_MEMORY
10246 && modifier != EXPAND_WRITE)
10247 temp = flip_storage_order (mode, temp);
10248 return temp;
10251 case ARRAY_REF:
10254 tree array = treeop0;
10255 tree index = treeop1;
10256 tree init;
10258 /* Fold an expression like: "foo"[2].
10259 This is not done in fold so it won't happen inside &.
10260 Don't fold if this is for wide characters since it's too
10261 difficult to do correctly and this is a very rare case. */
10263 if (modifier != EXPAND_CONST_ADDRESS
10264 && modifier != EXPAND_INITIALIZER
10265 && modifier != EXPAND_MEMORY)
10267 tree t = fold_read_from_constant_string (exp);
10269 if (t)
10270 return expand_expr (t, target, tmode, modifier);
10273 /* If this is a constant index into a constant array,
10274 just get the value from the array. Handle both the cases when
10275 we have an explicit constructor and when our operand is a variable
10276 that was declared const. */
10278 if (modifier != EXPAND_CONST_ADDRESS
10279 && modifier != EXPAND_INITIALIZER
10280 && modifier != EXPAND_MEMORY
10281 && TREE_CODE (array) == CONSTRUCTOR
10282 && ! TREE_SIDE_EFFECTS (array)
10283 && TREE_CODE (index) == INTEGER_CST)
10285 unsigned HOST_WIDE_INT ix;
10286 tree field, value;
10288 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10289 field, value)
10290 if (tree_int_cst_equal (field, index))
10292 if (!TREE_SIDE_EFFECTS (value))
10293 return expand_expr (fold (value), target, tmode, modifier);
10294 break;
10298 else if (optimize >= 1
10299 && modifier != EXPAND_CONST_ADDRESS
10300 && modifier != EXPAND_INITIALIZER
10301 && modifier != EXPAND_MEMORY
10302 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10303 && TREE_CODE (index) == INTEGER_CST
10304 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10305 && (init = ctor_for_folding (array)) != error_mark_node)
10307 if (init == NULL_TREE)
10309 tree value = build_zero_cst (type);
10310 if (TREE_CODE (value) == CONSTRUCTOR)
10312 /* If VALUE is a CONSTRUCTOR, this optimization is only
10313 useful if this doesn't store the CONSTRUCTOR into
10314 memory. If it does, it is more efficient to just
10315 load the data from the array directly. */
10316 rtx ret = expand_constructor (value, target,
10317 modifier, true);
10318 if (ret == NULL_RTX)
10319 value = NULL_TREE;
10322 if (value)
10323 return expand_expr (value, target, tmode, modifier);
10325 else if (TREE_CODE (init) == CONSTRUCTOR)
10327 unsigned HOST_WIDE_INT ix;
10328 tree field, value;
10330 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10331 field, value)
10332 if (tree_int_cst_equal (field, index))
10334 if (TREE_SIDE_EFFECTS (value))
10335 break;
10337 if (TREE_CODE (value) == CONSTRUCTOR)
10339 /* If VALUE is a CONSTRUCTOR, this
10340 optimization is only useful if
10341 this doesn't store the CONSTRUCTOR
10342 into memory. If it does, it is more
10343 efficient to just load the data from
10344 the array directly. */
10345 rtx ret = expand_constructor (value, target,
10346 modifier, true);
10347 if (ret == NULL_RTX)
10348 break;
10351 return
10352 expand_expr (fold (value), target, tmode, modifier);
10355 else if (TREE_CODE (init) == STRING_CST)
10357 tree low_bound = array_ref_low_bound (exp);
10358 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10360 /* Optimize the special case of a zero lower bound.
10362 We convert the lower bound to sizetype to avoid problems
10363 with constant folding. E.g. suppose the lower bound is
10364 1 and its mode is QI. Without the conversion
10365 (ARRAY + (INDEX - (unsigned char)1))
10366 becomes
10367 (ARRAY + (-(unsigned char)1) + INDEX)
10368 which becomes
10369 (ARRAY + 255 + INDEX). Oops! */
10370 if (!integer_zerop (low_bound))
10371 index1 = size_diffop_loc (loc, index1,
10372 fold_convert_loc (loc, sizetype,
10373 low_bound));
10375 if (tree_fits_uhwi_p (index1)
10376 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10378 tree type = TREE_TYPE (TREE_TYPE (init));
10379 scalar_int_mode mode;
10381 if (is_int_mode (TYPE_MODE (type), &mode)
10382 && GET_MODE_SIZE (mode) == 1)
10383 return gen_int_mode (TREE_STRING_POINTER (init)
10384 [TREE_INT_CST_LOW (index1)],
10385 mode);
10390 goto normal_inner_ref;
10392 case COMPONENT_REF:
10393 /* If the operand is a CONSTRUCTOR, we can just extract the
10394 appropriate field if it is present. */
10395 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10397 unsigned HOST_WIDE_INT idx;
10398 tree field, value;
10399 scalar_int_mode field_mode;
10401 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10402 idx, field, value)
10403 if (field == treeop1
10404 /* We can normally use the value of the field in the
10405 CONSTRUCTOR. However, if this is a bitfield in
10406 an integral mode that we can fit in a HOST_WIDE_INT,
10407 we must mask only the number of bits in the bitfield,
10408 since this is done implicitly by the constructor. If
10409 the bitfield does not meet either of those conditions,
10410 we can't do this optimization. */
10411 && (! DECL_BIT_FIELD (field)
10412 || (is_int_mode (DECL_MODE (field), &field_mode)
10413 && (GET_MODE_PRECISION (field_mode)
10414 <= HOST_BITS_PER_WIDE_INT))))
10416 if (DECL_BIT_FIELD (field)
10417 && modifier == EXPAND_STACK_PARM)
10418 target = 0;
10419 op0 = expand_expr (value, target, tmode, modifier);
10420 if (DECL_BIT_FIELD (field))
10422 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10423 scalar_int_mode imode
10424 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10426 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10428 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10429 imode);
10430 op0 = expand_and (imode, op0, op1, target);
10432 else
10434 int count = GET_MODE_PRECISION (imode) - bitsize;
10436 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10437 target, 0);
10438 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10439 target, 0);
10443 return op0;
10446 goto normal_inner_ref;
10448 case BIT_FIELD_REF:
10449 case ARRAY_RANGE_REF:
10450 normal_inner_ref:
10452 machine_mode mode1, mode2;
10453 HOST_WIDE_INT bitsize, bitpos;
10454 tree offset;
10455 int reversep, volatilep = 0, must_force_mem;
10456 tree tem
10457 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10458 &unsignedp, &reversep, &volatilep);
10459 rtx orig_op0, memloc;
10460 bool clear_mem_expr = false;
10462 /* If we got back the original object, something is wrong. Perhaps
10463 we are evaluating an expression too early. In any event, don't
10464 infinitely recurse. */
10465 gcc_assert (tem != exp);
10467 /* If TEM's type is a union of variable size, pass TARGET to the inner
10468 computation, since it will need a temporary and TARGET is known
10469 to have to do. This occurs in unchecked conversion in Ada. */
10470 orig_op0 = op0
10471 = expand_expr_real (tem,
10472 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10473 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10474 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10475 != INTEGER_CST)
10476 && modifier != EXPAND_STACK_PARM
10477 ? target : NULL_RTX),
10478 VOIDmode,
10479 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10480 NULL, true);
10482 /* If the field has a mode, we want to access it in the
10483 field's mode, not the computed mode.
10484 If a MEM has VOIDmode (external with incomplete type),
10485 use BLKmode for it instead. */
10486 if (MEM_P (op0))
10488 if (mode1 != VOIDmode)
10489 op0 = adjust_address (op0, mode1, 0);
10490 else if (GET_MODE (op0) == VOIDmode)
10491 op0 = adjust_address (op0, BLKmode, 0);
10494 mode2
10495 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10497 /* If we have either an offset, a BLKmode result, or a reference
10498 outside the underlying object, we must force it to memory.
10499 Such a case can occur in Ada if we have unchecked conversion
10500 of an expression from a scalar type to an aggregate type or
10501 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10502 passed a partially uninitialized object or a view-conversion
10503 to a larger size. */
10504 must_force_mem = (offset
10505 || mode1 == BLKmode
10506 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10508 /* Handle CONCAT first. */
10509 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10511 if (bitpos == 0
10512 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10513 && COMPLEX_MODE_P (mode1)
10514 && COMPLEX_MODE_P (GET_MODE (op0))
10515 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10516 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10518 if (reversep)
10519 op0 = flip_storage_order (GET_MODE (op0), op0);
10520 if (mode1 != GET_MODE (op0))
10522 rtx parts[2];
10523 for (int i = 0; i < 2; i++)
10525 rtx op = read_complex_part (op0, i != 0);
10526 if (GET_CODE (op) == SUBREG)
10527 op = force_reg (GET_MODE (op), op);
10528 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10529 op);
10530 if (temp)
10531 op = temp;
10532 else
10534 if (!REG_P (op) && !MEM_P (op))
10535 op = force_reg (GET_MODE (op), op);
10536 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10538 parts[i] = op;
10540 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10542 return op0;
10544 if (bitpos == 0
10545 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10546 && bitsize)
10548 op0 = XEXP (op0, 0);
10549 mode2 = GET_MODE (op0);
10551 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10552 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10553 && bitpos
10554 && bitsize)
10556 op0 = XEXP (op0, 1);
10557 bitpos = 0;
10558 mode2 = GET_MODE (op0);
10560 else
10561 /* Otherwise force into memory. */
10562 must_force_mem = 1;
10565 /* If this is a constant, put it in a register if it is a legitimate
10566 constant and we don't need a memory reference. */
10567 if (CONSTANT_P (op0)
10568 && mode2 != BLKmode
10569 && targetm.legitimate_constant_p (mode2, op0)
10570 && !must_force_mem)
10571 op0 = force_reg (mode2, op0);
10573 /* Otherwise, if this is a constant, try to force it to the constant
10574 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10575 is a legitimate constant. */
10576 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10577 op0 = validize_mem (memloc);
10579 /* Otherwise, if this is a constant or the object is not in memory
10580 and need be, put it there. */
10581 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10583 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10584 emit_move_insn (memloc, op0);
10585 op0 = memloc;
10586 clear_mem_expr = true;
10589 if (offset)
10591 machine_mode address_mode;
10592 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10593 EXPAND_SUM);
10595 gcc_assert (MEM_P (op0));
10597 address_mode = get_address_mode (op0);
10598 if (GET_MODE (offset_rtx) != address_mode)
10600 /* We cannot be sure that the RTL in offset_rtx is valid outside
10601 of a memory address context, so force it into a register
10602 before attempting to convert it to the desired mode. */
10603 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10604 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10607 /* See the comment in expand_assignment for the rationale. */
10608 if (mode1 != VOIDmode
10609 && bitpos != 0
10610 && bitsize > 0
10611 && (bitpos % bitsize) == 0
10612 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10613 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10615 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10616 bitpos = 0;
10619 op0 = offset_address (op0, offset_rtx,
10620 highest_pow2_factor (offset));
10623 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10624 record its alignment as BIGGEST_ALIGNMENT. */
10625 if (MEM_P (op0) && bitpos == 0 && offset != 0
10626 && is_aligning_offset (offset, tem))
10627 set_mem_align (op0, BIGGEST_ALIGNMENT);
10629 /* Don't forget about volatility even if this is a bitfield. */
10630 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10632 if (op0 == orig_op0)
10633 op0 = copy_rtx (op0);
10635 MEM_VOLATILE_P (op0) = 1;
10638 /* In cases where an aligned union has an unaligned object
10639 as a field, we might be extracting a BLKmode value from
10640 an integer-mode (e.g., SImode) object. Handle this case
10641 by doing the extract into an object as wide as the field
10642 (which we know to be the width of a basic mode), then
10643 storing into memory, and changing the mode to BLKmode. */
10644 if (mode1 == VOIDmode
10645 || REG_P (op0) || GET_CODE (op0) == SUBREG
10646 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10647 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10648 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10649 && modifier != EXPAND_CONST_ADDRESS
10650 && modifier != EXPAND_INITIALIZER
10651 && modifier != EXPAND_MEMORY)
10652 /* If the bitfield is volatile and the bitsize
10653 is narrower than the access size of the bitfield,
10654 we need to extract bitfields from the access. */
10655 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10656 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10657 && mode1 != BLKmode
10658 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10659 /* If the field isn't aligned enough to fetch as a memref,
10660 fetch it as a bit field. */
10661 || (mode1 != BLKmode
10662 && (((MEM_P (op0)
10663 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10664 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10665 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10666 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10667 && modifier != EXPAND_MEMORY
10668 && ((modifier == EXPAND_CONST_ADDRESS
10669 || modifier == EXPAND_INITIALIZER)
10670 ? STRICT_ALIGNMENT
10671 : targetm.slow_unaligned_access (mode1,
10672 MEM_ALIGN (op0))))
10673 || (bitpos % BITS_PER_UNIT != 0)))
10674 /* If the type and the field are a constant size and the
10675 size of the type isn't the same size as the bitfield,
10676 we must use bitfield operations. */
10677 || (bitsize >= 0
10678 && TYPE_SIZE (TREE_TYPE (exp))
10679 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10680 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10681 bitsize)))
10683 machine_mode ext_mode = mode;
10685 if (ext_mode == BLKmode
10686 && ! (target != 0 && MEM_P (op0)
10687 && MEM_P (target)
10688 && bitpos % BITS_PER_UNIT == 0))
10689 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10691 if (ext_mode == BLKmode)
10693 if (target == 0)
10694 target = assign_temp (type, 1, 1);
10696 /* ??? Unlike the similar test a few lines below, this one is
10697 very likely obsolete. */
10698 if (bitsize == 0)
10699 return target;
10701 /* In this case, BITPOS must start at a byte boundary and
10702 TARGET, if specified, must be a MEM. */
10703 gcc_assert (MEM_P (op0)
10704 && (!target || MEM_P (target))
10705 && !(bitpos % BITS_PER_UNIT));
10707 emit_block_move (target,
10708 adjust_address (op0, VOIDmode,
10709 bitpos / BITS_PER_UNIT),
10710 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10711 / BITS_PER_UNIT),
10712 (modifier == EXPAND_STACK_PARM
10713 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10715 return target;
10718 /* If we have nothing to extract, the result will be 0 for targets
10719 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10720 return 0 for the sake of consistency, as reading a zero-sized
10721 bitfield is valid in Ada and the value is fully specified. */
10722 if (bitsize == 0)
10723 return const0_rtx;
10725 op0 = validize_mem (op0);
10727 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10728 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10730 /* If the result has a record type and the extraction is done in
10731 an integral mode, then the field may be not aligned on a byte
10732 boundary; in this case, if it has reverse storage order, it
10733 needs to be extracted as a scalar field with reverse storage
10734 order and put back into memory order afterwards. */
10735 if (TREE_CODE (type) == RECORD_TYPE
10736 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10737 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10739 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10740 (modifier == EXPAND_STACK_PARM
10741 ? NULL_RTX : target),
10742 ext_mode, ext_mode, reversep, alt_rtl);
10744 /* If the result has a record type and the mode of OP0 is an
10745 integral mode then, if BITSIZE is narrower than this mode
10746 and this is for big-endian data, we must put the field
10747 into the high-order bits. And we must also put it back
10748 into memory order if it has been previously reversed. */
10749 scalar_int_mode op0_mode;
10750 if (TREE_CODE (type) == RECORD_TYPE
10751 && is_int_mode (GET_MODE (op0), &op0_mode))
10753 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10755 if (bitsize < size
10756 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10757 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10758 size - bitsize, op0, 1);
10760 if (reversep)
10761 op0 = flip_storage_order (op0_mode, op0);
10764 /* If the result type is BLKmode, store the data into a temporary
10765 of the appropriate type, but with the mode corresponding to the
10766 mode for the data we have (op0's mode). */
10767 if (mode == BLKmode)
10769 rtx new_rtx
10770 = assign_stack_temp_for_type (ext_mode,
10771 GET_MODE_BITSIZE (ext_mode),
10772 type);
10773 emit_move_insn (new_rtx, op0);
10774 op0 = copy_rtx (new_rtx);
10775 PUT_MODE (op0, BLKmode);
10778 return op0;
10781 /* If the result is BLKmode, use that to access the object
10782 now as well. */
10783 if (mode == BLKmode)
10784 mode1 = BLKmode;
10786 /* Get a reference to just this component. */
10787 if (modifier == EXPAND_CONST_ADDRESS
10788 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10789 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10790 else
10791 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10793 if (op0 == orig_op0)
10794 op0 = copy_rtx (op0);
10796 /* Don't set memory attributes if the base expression is
10797 SSA_NAME that got expanded as a MEM. In that case, we should
10798 just honor its original memory attributes. */
10799 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10800 set_mem_attributes (op0, exp, 0);
10802 if (REG_P (XEXP (op0, 0)))
10803 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10805 /* If op0 is a temporary because the original expressions was forced
10806 to memory, clear MEM_EXPR so that the original expression cannot
10807 be marked as addressable through MEM_EXPR of the temporary. */
10808 if (clear_mem_expr)
10809 set_mem_expr (op0, NULL_TREE);
10811 MEM_VOLATILE_P (op0) |= volatilep;
10813 if (reversep
10814 && modifier != EXPAND_MEMORY
10815 && modifier != EXPAND_WRITE)
10816 op0 = flip_storage_order (mode1, op0);
10818 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10819 || modifier == EXPAND_CONST_ADDRESS
10820 || modifier == EXPAND_INITIALIZER)
10821 return op0;
10823 if (target == 0)
10824 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10826 convert_move (target, op0, unsignedp);
10827 return target;
10830 case OBJ_TYPE_REF:
10831 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10833 case CALL_EXPR:
10834 /* All valid uses of __builtin_va_arg_pack () are removed during
10835 inlining. */
10836 if (CALL_EXPR_VA_ARG_PACK (exp))
10837 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10839 tree fndecl = get_callee_fndecl (exp), attr;
10841 if (fndecl
10842 && (attr = lookup_attribute ("error",
10843 DECL_ATTRIBUTES (fndecl))) != NULL)
10844 error ("%Kcall to %qs declared with attribute error: %s",
10845 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10846 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10847 if (fndecl
10848 && (attr = lookup_attribute ("warning",
10849 DECL_ATTRIBUTES (fndecl))) != NULL)
10850 warning_at (tree_nonartificial_location (exp),
10851 0, "%Kcall to %qs declared with attribute warning: %s",
10852 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10853 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10855 /* Check for a built-in function. */
10856 if (fndecl && DECL_BUILT_IN (fndecl))
10858 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10859 if (CALL_WITH_BOUNDS_P (exp))
10860 return expand_builtin_with_bounds (exp, target, subtarget,
10861 tmode, ignore);
10862 else
10863 return expand_builtin (exp, target, subtarget, tmode, ignore);
10866 return expand_call (exp, target, ignore);
10868 case VIEW_CONVERT_EXPR:
10869 op0 = NULL_RTX;
10871 /* If we are converting to BLKmode, try to avoid an intermediate
10872 temporary by fetching an inner memory reference. */
10873 if (mode == BLKmode
10874 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10875 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10876 && handled_component_p (treeop0))
10878 machine_mode mode1;
10879 HOST_WIDE_INT bitsize, bitpos;
10880 tree offset;
10881 int unsignedp, reversep, volatilep = 0;
10882 tree tem
10883 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10884 &unsignedp, &reversep, &volatilep);
10885 rtx orig_op0;
10887 /* ??? We should work harder and deal with non-zero offsets. */
10888 if (!offset
10889 && (bitpos % BITS_PER_UNIT) == 0
10890 && !reversep
10891 && bitsize >= 0
10892 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10894 /* See the normal_inner_ref case for the rationale. */
10895 orig_op0
10896 = expand_expr_real (tem,
10897 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10898 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10899 != INTEGER_CST)
10900 && modifier != EXPAND_STACK_PARM
10901 ? target : NULL_RTX),
10902 VOIDmode,
10903 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10904 NULL, true);
10906 if (MEM_P (orig_op0))
10908 op0 = orig_op0;
10910 /* Get a reference to just this component. */
10911 if (modifier == EXPAND_CONST_ADDRESS
10912 || modifier == EXPAND_SUM
10913 || modifier == EXPAND_INITIALIZER)
10914 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10915 else
10916 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10918 if (op0 == orig_op0)
10919 op0 = copy_rtx (op0);
10921 set_mem_attributes (op0, treeop0, 0);
10922 if (REG_P (XEXP (op0, 0)))
10923 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10925 MEM_VOLATILE_P (op0) |= volatilep;
10930 if (!op0)
10931 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10932 NULL, inner_reference_p);
10934 /* If the input and output modes are both the same, we are done. */
10935 if (mode == GET_MODE (op0))
10937 /* If neither mode is BLKmode, and both modes are the same size
10938 then we can use gen_lowpart. */
10939 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10940 && (GET_MODE_PRECISION (mode)
10941 == GET_MODE_PRECISION (GET_MODE (op0)))
10942 && !COMPLEX_MODE_P (GET_MODE (op0)))
10944 if (GET_CODE (op0) == SUBREG)
10945 op0 = force_reg (GET_MODE (op0), op0);
10946 temp = gen_lowpart_common (mode, op0);
10947 if (temp)
10948 op0 = temp;
10949 else
10951 if (!REG_P (op0) && !MEM_P (op0))
10952 op0 = force_reg (GET_MODE (op0), op0);
10953 op0 = gen_lowpart (mode, op0);
10956 /* If both types are integral, convert from one mode to the other. */
10957 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10958 op0 = convert_modes (mode, GET_MODE (op0), op0,
10959 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10960 /* If the output type is a bit-field type, do an extraction. */
10961 else if (reduce_bit_field)
10962 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10963 TYPE_UNSIGNED (type), NULL_RTX,
10964 mode, mode, false, NULL);
10965 /* As a last resort, spill op0 to memory, and reload it in a
10966 different mode. */
10967 else if (!MEM_P (op0))
10969 /* If the operand is not a MEM, force it into memory. Since we
10970 are going to be changing the mode of the MEM, don't call
10971 force_const_mem for constants because we don't allow pool
10972 constants to change mode. */
10973 tree inner_type = TREE_TYPE (treeop0);
10975 gcc_assert (!TREE_ADDRESSABLE (exp));
10977 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10978 target
10979 = assign_stack_temp_for_type
10980 (TYPE_MODE (inner_type),
10981 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10983 emit_move_insn (target, op0);
10984 op0 = target;
10987 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10988 output type is such that the operand is known to be aligned, indicate
10989 that it is. Otherwise, we need only be concerned about alignment for
10990 non-BLKmode results. */
10991 if (MEM_P (op0))
10993 enum insn_code icode;
10995 if (modifier != EXPAND_WRITE
10996 && modifier != EXPAND_MEMORY
10997 && !inner_reference_p
10998 && mode != BLKmode
10999 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11001 /* If the target does have special handling for unaligned
11002 loads of mode then use them. */
11003 if ((icode = optab_handler (movmisalign_optab, mode))
11004 != CODE_FOR_nothing)
11006 rtx reg;
11008 op0 = adjust_address (op0, mode, 0);
11009 /* We've already validated the memory, and we're creating a
11010 new pseudo destination. The predicates really can't
11011 fail. */
11012 reg = gen_reg_rtx (mode);
11014 /* Nor can the insn generator. */
11015 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11016 emit_insn (insn);
11017 return reg;
11019 else if (STRICT_ALIGNMENT)
11021 tree inner_type = TREE_TYPE (treeop0);
11022 HOST_WIDE_INT temp_size
11023 = MAX (int_size_in_bytes (inner_type),
11024 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11025 rtx new_rtx
11026 = assign_stack_temp_for_type (mode, temp_size, type);
11027 rtx new_with_op0_mode
11028 = adjust_address (new_rtx, GET_MODE (op0), 0);
11030 gcc_assert (!TREE_ADDRESSABLE (exp));
11032 if (GET_MODE (op0) == BLKmode)
11033 emit_block_move (new_with_op0_mode, op0,
11034 GEN_INT (GET_MODE_SIZE (mode)),
11035 (modifier == EXPAND_STACK_PARM
11036 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11037 else
11038 emit_move_insn (new_with_op0_mode, op0);
11040 op0 = new_rtx;
11044 op0 = adjust_address (op0, mode, 0);
11047 return op0;
11049 case MODIFY_EXPR:
11051 tree lhs = treeop0;
11052 tree rhs = treeop1;
11053 gcc_assert (ignore);
11055 /* Check for |= or &= of a bitfield of size one into another bitfield
11056 of size 1. In this case, (unless we need the result of the
11057 assignment) we can do this more efficiently with a
11058 test followed by an assignment, if necessary.
11060 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11061 things change so we do, this code should be enhanced to
11062 support it. */
11063 if (TREE_CODE (lhs) == COMPONENT_REF
11064 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11065 || TREE_CODE (rhs) == BIT_AND_EXPR)
11066 && TREE_OPERAND (rhs, 0) == lhs
11067 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11068 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11069 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11071 rtx_code_label *label = gen_label_rtx ();
11072 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11073 do_jump (TREE_OPERAND (rhs, 1),
11074 value ? label : 0,
11075 value ? 0 : label,
11076 profile_probability::uninitialized ());
11077 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11078 false);
11079 do_pending_stack_adjust ();
11080 emit_label (label);
11081 return const0_rtx;
11084 expand_assignment (lhs, rhs, false);
11085 return const0_rtx;
11088 case ADDR_EXPR:
11089 return expand_expr_addr_expr (exp, target, tmode, modifier);
11091 case REALPART_EXPR:
11092 op0 = expand_normal (treeop0);
11093 return read_complex_part (op0, false);
11095 case IMAGPART_EXPR:
11096 op0 = expand_normal (treeop0);
11097 return read_complex_part (op0, true);
11099 case RETURN_EXPR:
11100 case LABEL_EXPR:
11101 case GOTO_EXPR:
11102 case SWITCH_EXPR:
11103 case ASM_EXPR:
11104 /* Expanded in cfgexpand.c. */
11105 gcc_unreachable ();
11107 case TRY_CATCH_EXPR:
11108 case CATCH_EXPR:
11109 case EH_FILTER_EXPR:
11110 case TRY_FINALLY_EXPR:
11111 /* Lowered by tree-eh.c. */
11112 gcc_unreachable ();
11114 case WITH_CLEANUP_EXPR:
11115 case CLEANUP_POINT_EXPR:
11116 case TARGET_EXPR:
11117 case CASE_LABEL_EXPR:
11118 case VA_ARG_EXPR:
11119 case BIND_EXPR:
11120 case INIT_EXPR:
11121 case CONJ_EXPR:
11122 case COMPOUND_EXPR:
11123 case PREINCREMENT_EXPR:
11124 case PREDECREMENT_EXPR:
11125 case POSTINCREMENT_EXPR:
11126 case POSTDECREMENT_EXPR:
11127 case LOOP_EXPR:
11128 case EXIT_EXPR:
11129 case COMPOUND_LITERAL_EXPR:
11130 /* Lowered by gimplify.c. */
11131 gcc_unreachable ();
11133 case FDESC_EXPR:
11134 /* Function descriptors are not valid except for as
11135 initialization constants, and should not be expanded. */
11136 gcc_unreachable ();
11138 case WITH_SIZE_EXPR:
11139 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11140 have pulled out the size to use in whatever context it needed. */
11141 return expand_expr_real (treeop0, original_target, tmode,
11142 modifier, alt_rtl, inner_reference_p);
11144 default:
11145 return expand_expr_real_2 (&ops, target, tmode, modifier);
11149 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11150 signedness of TYPE), possibly returning the result in TARGET.
11151 TYPE is known to be a partial integer type. */
11152 static rtx
11153 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11155 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11156 if (target && GET_MODE (target) != GET_MODE (exp))
11157 target = 0;
11158 /* For constant values, reduce using build_int_cst_type. */
11159 if (CONST_INT_P (exp))
11161 HOST_WIDE_INT value = INTVAL (exp);
11162 tree t = build_int_cst_type (type, value);
11163 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11165 else if (TYPE_UNSIGNED (type))
11167 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11168 rtx mask = immed_wide_int_const
11169 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11170 return expand_and (mode, exp, mask, target);
11172 else
11174 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11175 int count = GET_MODE_PRECISION (mode) - prec;
11176 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11177 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11181 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11182 when applied to the address of EXP produces an address known to be
11183 aligned more than BIGGEST_ALIGNMENT. */
11185 static int
11186 is_aligning_offset (const_tree offset, const_tree exp)
11188 /* Strip off any conversions. */
11189 while (CONVERT_EXPR_P (offset))
11190 offset = TREE_OPERAND (offset, 0);
11192 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11193 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11194 if (TREE_CODE (offset) != BIT_AND_EXPR
11195 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11196 || compare_tree_int (TREE_OPERAND (offset, 1),
11197 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11198 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11199 return 0;
11201 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11202 It must be NEGATE_EXPR. Then strip any more conversions. */
11203 offset = TREE_OPERAND (offset, 0);
11204 while (CONVERT_EXPR_P (offset))
11205 offset = TREE_OPERAND (offset, 0);
11207 if (TREE_CODE (offset) != NEGATE_EXPR)
11208 return 0;
11210 offset = TREE_OPERAND (offset, 0);
11211 while (CONVERT_EXPR_P (offset))
11212 offset = TREE_OPERAND (offset, 0);
11214 /* This must now be the address of EXP. */
11215 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11218 /* Return the tree node if an ARG corresponds to a string constant or zero
11219 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11220 in bytes within the string that ARG is accessing. The type of the
11221 offset will be `sizetype'. */
11223 tree
11224 string_constant (tree arg, tree *ptr_offset)
11226 tree array, offset, lower_bound;
11227 STRIP_NOPS (arg);
11229 if (TREE_CODE (arg) == ADDR_EXPR)
11231 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11233 *ptr_offset = size_zero_node;
11234 return TREE_OPERAND (arg, 0);
11236 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11238 array = TREE_OPERAND (arg, 0);
11239 offset = size_zero_node;
11241 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11243 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11244 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11245 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11246 return 0;
11248 /* Check if the array has a nonzero lower bound. */
11249 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11250 if (!integer_zerop (lower_bound))
11252 /* If the offset and base aren't both constants, return 0. */
11253 if (TREE_CODE (lower_bound) != INTEGER_CST)
11254 return 0;
11255 if (TREE_CODE (offset) != INTEGER_CST)
11256 return 0;
11257 /* Adjust offset by the lower bound. */
11258 offset = size_diffop (fold_convert (sizetype, offset),
11259 fold_convert (sizetype, lower_bound));
11262 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11264 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11265 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11266 if (TREE_CODE (array) != ADDR_EXPR)
11267 return 0;
11268 array = TREE_OPERAND (array, 0);
11269 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11270 return 0;
11272 else
11273 return 0;
11275 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11277 tree arg0 = TREE_OPERAND (arg, 0);
11278 tree arg1 = TREE_OPERAND (arg, 1);
11280 STRIP_NOPS (arg0);
11281 STRIP_NOPS (arg1);
11283 if (TREE_CODE (arg0) == ADDR_EXPR
11284 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11285 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11287 array = TREE_OPERAND (arg0, 0);
11288 offset = arg1;
11290 else if (TREE_CODE (arg1) == ADDR_EXPR
11291 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11292 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11294 array = TREE_OPERAND (arg1, 0);
11295 offset = arg0;
11297 else
11298 return 0;
11300 else
11301 return 0;
11303 if (TREE_CODE (array) == STRING_CST)
11305 *ptr_offset = fold_convert (sizetype, offset);
11306 return array;
11308 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11310 int length;
11311 tree init = ctor_for_folding (array);
11313 /* Variables initialized to string literals can be handled too. */
11314 if (init == error_mark_node
11315 || !init
11316 || TREE_CODE (init) != STRING_CST)
11317 return 0;
11319 /* Avoid const char foo[4] = "abcde"; */
11320 if (DECL_SIZE_UNIT (array) == NULL_TREE
11321 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11322 || (length = TREE_STRING_LENGTH (init)) <= 0
11323 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11324 return 0;
11326 /* If variable is bigger than the string literal, OFFSET must be constant
11327 and inside of the bounds of the string literal. */
11328 offset = fold_convert (sizetype, offset);
11329 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11330 && (! tree_fits_uhwi_p (offset)
11331 || compare_tree_int (offset, length) >= 0))
11332 return 0;
11334 *ptr_offset = offset;
11335 return init;
11338 return 0;
11341 /* Generate code to calculate OPS, and exploded expression
11342 using a store-flag instruction and return an rtx for the result.
11343 OPS reflects a comparison.
11345 If TARGET is nonzero, store the result there if convenient.
11347 Return zero if there is no suitable set-flag instruction
11348 available on this machine.
11350 Once expand_expr has been called on the arguments of the comparison,
11351 we are committed to doing the store flag, since it is not safe to
11352 re-evaluate the expression. We emit the store-flag insn by calling
11353 emit_store_flag, but only expand the arguments if we have a reason
11354 to believe that emit_store_flag will be successful. If we think that
11355 it will, but it isn't, we have to simulate the store-flag with a
11356 set/jump/set sequence. */
11358 static rtx
11359 do_store_flag (sepops ops, rtx target, machine_mode mode)
11361 enum rtx_code code;
11362 tree arg0, arg1, type;
11363 machine_mode operand_mode;
11364 int unsignedp;
11365 rtx op0, op1;
11366 rtx subtarget = target;
11367 location_t loc = ops->location;
11369 arg0 = ops->op0;
11370 arg1 = ops->op1;
11372 /* Don't crash if the comparison was erroneous. */
11373 if (arg0 == error_mark_node || arg1 == error_mark_node)
11374 return const0_rtx;
11376 type = TREE_TYPE (arg0);
11377 operand_mode = TYPE_MODE (type);
11378 unsignedp = TYPE_UNSIGNED (type);
11380 /* We won't bother with BLKmode store-flag operations because it would mean
11381 passing a lot of information to emit_store_flag. */
11382 if (operand_mode == BLKmode)
11383 return 0;
11385 /* We won't bother with store-flag operations involving function pointers
11386 when function pointers must be canonicalized before comparisons. */
11387 if (targetm.have_canonicalize_funcptr_for_compare ()
11388 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11389 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11390 == FUNCTION_TYPE))
11391 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11392 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11393 == FUNCTION_TYPE))))
11394 return 0;
11396 STRIP_NOPS (arg0);
11397 STRIP_NOPS (arg1);
11399 /* For vector typed comparisons emit code to generate the desired
11400 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11401 expander for this. */
11402 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11404 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11405 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11406 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11407 return expand_vec_cmp_expr (ops->type, ifexp, target);
11408 else
11410 tree if_true = constant_boolean_node (true, ops->type);
11411 tree if_false = constant_boolean_node (false, ops->type);
11412 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11413 if_false, target);
11417 /* Get the rtx comparison code to use. We know that EXP is a comparison
11418 operation of some type. Some comparisons against 1 and -1 can be
11419 converted to comparisons with zero. Do so here so that the tests
11420 below will be aware that we have a comparison with zero. These
11421 tests will not catch constants in the first operand, but constants
11422 are rarely passed as the first operand. */
11424 switch (ops->code)
11426 case EQ_EXPR:
11427 code = EQ;
11428 break;
11429 case NE_EXPR:
11430 code = NE;
11431 break;
11432 case LT_EXPR:
11433 if (integer_onep (arg1))
11434 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11435 else
11436 code = unsignedp ? LTU : LT;
11437 break;
11438 case LE_EXPR:
11439 if (! unsignedp && integer_all_onesp (arg1))
11440 arg1 = integer_zero_node, code = LT;
11441 else
11442 code = unsignedp ? LEU : LE;
11443 break;
11444 case GT_EXPR:
11445 if (! unsignedp && integer_all_onesp (arg1))
11446 arg1 = integer_zero_node, code = GE;
11447 else
11448 code = unsignedp ? GTU : GT;
11449 break;
11450 case GE_EXPR:
11451 if (integer_onep (arg1))
11452 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11453 else
11454 code = unsignedp ? GEU : GE;
11455 break;
11457 case UNORDERED_EXPR:
11458 code = UNORDERED;
11459 break;
11460 case ORDERED_EXPR:
11461 code = ORDERED;
11462 break;
11463 case UNLT_EXPR:
11464 code = UNLT;
11465 break;
11466 case UNLE_EXPR:
11467 code = UNLE;
11468 break;
11469 case UNGT_EXPR:
11470 code = UNGT;
11471 break;
11472 case UNGE_EXPR:
11473 code = UNGE;
11474 break;
11475 case UNEQ_EXPR:
11476 code = UNEQ;
11477 break;
11478 case LTGT_EXPR:
11479 code = LTGT;
11480 break;
11482 default:
11483 gcc_unreachable ();
11486 /* Put a constant second. */
11487 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11488 || TREE_CODE (arg0) == FIXED_CST)
11490 std::swap (arg0, arg1);
11491 code = swap_condition (code);
11494 /* If this is an equality or inequality test of a single bit, we can
11495 do this by shifting the bit being tested to the low-order bit and
11496 masking the result with the constant 1. If the condition was EQ,
11497 we xor it with 1. This does not require an scc insn and is faster
11498 than an scc insn even if we have it.
11500 The code to make this transformation was moved into fold_single_bit_test,
11501 so we just call into the folder and expand its result. */
11503 if ((code == NE || code == EQ)
11504 && integer_zerop (arg1)
11505 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11507 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11508 if (srcstmt
11509 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11511 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11512 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11513 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11514 gimple_assign_rhs1 (srcstmt),
11515 gimple_assign_rhs2 (srcstmt));
11516 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11517 if (temp)
11518 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11522 if (! get_subtarget (target)
11523 || GET_MODE (subtarget) != operand_mode)
11524 subtarget = 0;
11526 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11528 if (target == 0)
11529 target = gen_reg_rtx (mode);
11531 /* Try a cstore if possible. */
11532 return emit_store_flag_force (target, code, op0, op1,
11533 operand_mode, unsignedp,
11534 (TYPE_PRECISION (ops->type) == 1
11535 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11538 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11539 0 otherwise (i.e. if there is no casesi instruction).
11541 DEFAULT_PROBABILITY is the probability of jumping to the default
11542 label. */
11544 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11545 rtx table_label, rtx default_label, rtx fallback_label,
11546 profile_probability default_probability)
11548 struct expand_operand ops[5];
11549 scalar_int_mode index_mode = SImode;
11550 rtx op1, op2, index;
11552 if (! targetm.have_casesi ())
11553 return 0;
11555 /* The index must be some form of integer. Convert it to SImode. */
11556 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11557 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11559 rtx rangertx = expand_normal (range);
11561 /* We must handle the endpoints in the original mode. */
11562 index_expr = build2 (MINUS_EXPR, index_type,
11563 index_expr, minval);
11564 minval = integer_zero_node;
11565 index = expand_normal (index_expr);
11566 if (default_label)
11567 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11568 omode, 1, default_label,
11569 default_probability);
11570 /* Now we can safely truncate. */
11571 index = convert_to_mode (index_mode, index, 0);
11573 else
11575 if (omode != index_mode)
11577 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11578 index_expr = fold_convert (index_type, index_expr);
11581 index = expand_normal (index_expr);
11584 do_pending_stack_adjust ();
11586 op1 = expand_normal (minval);
11587 op2 = expand_normal (range);
11589 create_input_operand (&ops[0], index, index_mode);
11590 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11591 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11592 create_fixed_operand (&ops[3], table_label);
11593 create_fixed_operand (&ops[4], (default_label
11594 ? default_label
11595 : fallback_label));
11596 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11597 return 1;
11600 /* Attempt to generate a tablejump instruction; same concept. */
11601 /* Subroutine of the next function.
11603 INDEX is the value being switched on, with the lowest value
11604 in the table already subtracted.
11605 MODE is its expected mode (needed if INDEX is constant).
11606 RANGE is the length of the jump table.
11607 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11609 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11610 index value is out of range.
11611 DEFAULT_PROBABILITY is the probability of jumping to
11612 the default label. */
11614 static void
11615 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11616 rtx default_label, profile_probability default_probability)
11618 rtx temp, vector;
11620 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11621 cfun->cfg->max_jumptable_ents = INTVAL (range);
11623 /* Do an unsigned comparison (in the proper mode) between the index
11624 expression and the value which represents the length of the range.
11625 Since we just finished subtracting the lower bound of the range
11626 from the index expression, this comparison allows us to simultaneously
11627 check that the original index expression value is both greater than
11628 or equal to the minimum value of the range and less than or equal to
11629 the maximum value of the range. */
11631 if (default_label)
11632 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11633 default_label, default_probability);
11636 /* If index is in range, it must fit in Pmode.
11637 Convert to Pmode so we can index with it. */
11638 if (mode != Pmode)
11639 index = convert_to_mode (Pmode, index, 1);
11641 /* Don't let a MEM slip through, because then INDEX that comes
11642 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11643 and break_out_memory_refs will go to work on it and mess it up. */
11644 #ifdef PIC_CASE_VECTOR_ADDRESS
11645 if (flag_pic && !REG_P (index))
11646 index = copy_to_mode_reg (Pmode, index);
11647 #endif
11649 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11650 GET_MODE_SIZE, because this indicates how large insns are. The other
11651 uses should all be Pmode, because they are addresses. This code
11652 could fail if addresses and insns are not the same size. */
11653 index = simplify_gen_binary (MULT, Pmode, index,
11654 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11655 Pmode));
11656 index = simplify_gen_binary (PLUS, Pmode, index,
11657 gen_rtx_LABEL_REF (Pmode, table_label));
11659 #ifdef PIC_CASE_VECTOR_ADDRESS
11660 if (flag_pic)
11661 index = PIC_CASE_VECTOR_ADDRESS (index);
11662 else
11663 #endif
11664 index = memory_address (CASE_VECTOR_MODE, index);
11665 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11666 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11667 convert_move (temp, vector, 0);
11669 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11671 /* If we are generating PIC code or if the table is PC-relative, the
11672 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11673 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11674 emit_barrier ();
11678 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11679 rtx table_label, rtx default_label,
11680 profile_probability default_probability)
11682 rtx index;
11684 if (! targetm.have_tablejump ())
11685 return 0;
11687 index_expr = fold_build2 (MINUS_EXPR, index_type,
11688 fold_convert (index_type, index_expr),
11689 fold_convert (index_type, minval));
11690 index = expand_normal (index_expr);
11691 do_pending_stack_adjust ();
11693 do_tablejump (index, TYPE_MODE (index_type),
11694 convert_modes (TYPE_MODE (index_type),
11695 TYPE_MODE (TREE_TYPE (range)),
11696 expand_normal (range),
11697 TYPE_UNSIGNED (TREE_TYPE (range))),
11698 table_label, default_label, default_probability);
11699 return 1;
11702 /* Return a CONST_VECTOR rtx representing vector mask for
11703 a VECTOR_CST of booleans. */
11704 static rtx
11705 const_vector_mask_from_tree (tree exp)
11707 rtvec v;
11708 unsigned i, units;
11709 tree elt;
11710 machine_mode inner, mode;
11712 mode = TYPE_MODE (TREE_TYPE (exp));
11713 units = VECTOR_CST_NELTS (exp);
11714 inner = GET_MODE_INNER (mode);
11716 v = rtvec_alloc (units);
11718 for (i = 0; i < units; ++i)
11720 elt = VECTOR_CST_ELT (exp, i);
11722 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11723 if (integer_zerop (elt))
11724 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11725 else if (integer_onep (elt)
11726 || integer_minus_onep (elt))
11727 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11728 else
11729 gcc_unreachable ();
11732 return gen_rtx_CONST_VECTOR (mode, v);
11735 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11736 Return a constant scalar rtx of mode MODE in which bit X is set if element
11737 X of EXP is nonzero. */
11738 static rtx
11739 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11741 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11742 tree elt;
11743 unsigned i;
11745 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11747 elt = VECTOR_CST_ELT (exp, i);
11748 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11749 if (integer_all_onesp (elt))
11750 res = wi::set_bit (res, i);
11751 else
11752 gcc_assert (integer_zerop (elt));
11755 return immed_wide_int_const (res, mode);
11758 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11759 static rtx
11760 const_vector_from_tree (tree exp)
11762 rtvec v;
11763 unsigned i, units;
11764 tree elt;
11765 machine_mode inner, mode;
11767 mode = TYPE_MODE (TREE_TYPE (exp));
11769 if (initializer_zerop (exp))
11770 return CONST0_RTX (mode);
11772 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11773 return const_vector_mask_from_tree (exp);
11775 units = VECTOR_CST_NELTS (exp);
11776 inner = GET_MODE_INNER (mode);
11778 v = rtvec_alloc (units);
11780 for (i = 0; i < units; ++i)
11782 elt = VECTOR_CST_ELT (exp, i);
11784 if (TREE_CODE (elt) == REAL_CST)
11785 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11786 inner);
11787 else if (TREE_CODE (elt) == FIXED_CST)
11788 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11789 inner);
11790 else
11791 RTVEC_ELT (v, i) = immed_wide_int_const (wi::to_wide (elt), inner);
11794 return gen_rtx_CONST_VECTOR (mode, v);
11797 /* Build a decl for a personality function given a language prefix. */
11799 tree
11800 build_personality_function (const char *lang)
11802 const char *unwind_and_version;
11803 tree decl, type;
11804 char *name;
11806 switch (targetm_common.except_unwind_info (&global_options))
11808 case UI_NONE:
11809 return NULL;
11810 case UI_SJLJ:
11811 unwind_and_version = "_sj0";
11812 break;
11813 case UI_DWARF2:
11814 case UI_TARGET:
11815 unwind_and_version = "_v0";
11816 break;
11817 case UI_SEH:
11818 unwind_and_version = "_seh0";
11819 break;
11820 default:
11821 gcc_unreachable ();
11824 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11826 type = build_function_type_list (integer_type_node, integer_type_node,
11827 long_long_unsigned_type_node,
11828 ptr_type_node, ptr_type_node, NULL_TREE);
11829 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11830 get_identifier (name), type);
11831 DECL_ARTIFICIAL (decl) = 1;
11832 DECL_EXTERNAL (decl) = 1;
11833 TREE_PUBLIC (decl) = 1;
11835 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11836 are the flags assigned by targetm.encode_section_info. */
11837 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11839 return decl;
11842 /* Extracts the personality function of DECL and returns the corresponding
11843 libfunc. */
11846 get_personality_function (tree decl)
11848 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11849 enum eh_personality_kind pk;
11851 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11852 if (pk == eh_personality_none)
11853 return NULL;
11855 if (!personality
11856 && pk == eh_personality_any)
11857 personality = lang_hooks.eh_personality ();
11859 if (pk == eh_personality_lang)
11860 gcc_assert (personality != NULL_TREE);
11862 return XEXP (DECL_RTL (personality), 0);
11865 /* Returns a tree for the size of EXP in bytes. */
11867 static tree
11868 tree_expr_size (const_tree exp)
11870 if (DECL_P (exp)
11871 && DECL_SIZE_UNIT (exp) != 0)
11872 return DECL_SIZE_UNIT (exp);
11873 else
11874 return size_in_bytes (TREE_TYPE (exp));
11877 /* Return an rtx for the size in bytes of the value of EXP. */
11880 expr_size (tree exp)
11882 tree size;
11884 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11885 size = TREE_OPERAND (exp, 1);
11886 else
11888 size = tree_expr_size (exp);
11889 gcc_assert (size);
11890 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11893 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11896 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11897 if the size can vary or is larger than an integer. */
11899 static HOST_WIDE_INT
11900 int_expr_size (tree exp)
11902 tree size;
11904 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11905 size = TREE_OPERAND (exp, 1);
11906 else
11908 size = tree_expr_size (exp);
11909 gcc_assert (size);
11912 if (size == 0 || !tree_fits_shwi_p (size))
11913 return -1;
11915 return tree_to_shwi (size);