backport: unnecessary duplication and repeating bugs like PR78439 due to changes...
[official-gcc.git] / gcc / expr.c
blob496d492c9fa167ebb0b0e918144e777cfad1612b
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 machine_mode mode = GET_MODE (srcreg);
2632 machine_mode tmode = GET_MODE (target);
2633 machine_mode copy_mode;
2635 /* BLKmode registers created in the back-end shouldn't have survived. */
2636 gcc_assert (mode != BLKmode);
2638 /* If the structure doesn't take up a whole number of words, see whether
2639 SRCREG is padded on the left or on the right. If it's on the left,
2640 set PADDING_CORRECTION to the number of bits to skip.
2642 In most ABIs, the structure will be returned at the least end of
2643 the register, which translates to right padding on little-endian
2644 targets and left padding on big-endian targets. The opposite
2645 holds if the structure is returned at the most significant
2646 end of the register. */
2647 if (bytes % UNITS_PER_WORD != 0
2648 && (targetm.calls.return_in_msb (type)
2649 ? !BYTES_BIG_ENDIAN
2650 : BYTES_BIG_ENDIAN))
2651 padding_correction
2652 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2654 /* We can use a single move if we have an exact mode for the size. */
2655 else if (MEM_P (target)
2656 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2657 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2658 && bytes == GET_MODE_SIZE (mode))
2660 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2661 return;
2664 /* And if we additionally have the same mode for a register. */
2665 else if (REG_P (target)
2666 && GET_MODE (target) == mode
2667 && bytes == GET_MODE_SIZE (mode))
2669 emit_move_insn (target, srcreg);
2670 return;
2673 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2674 into a new pseudo which is a full word. */
2675 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2677 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2678 mode = word_mode;
2681 /* Copy the structure BITSIZE bits at a time. If the target lives in
2682 memory, take care of not reading/writing past its end by selecting
2683 a copy mode suited to BITSIZE. This should always be possible given
2684 how it is computed.
2686 If the target lives in register, make sure not to select a copy mode
2687 larger than the mode of the register.
2689 We could probably emit more efficient code for machines which do not use
2690 strict alignment, but it doesn't seem worth the effort at the current
2691 time. */
2693 copy_mode = word_mode;
2694 if (MEM_P (target))
2696 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2697 if (mem_mode.exists ())
2698 copy_mode = mem_mode.require ();
2700 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2701 copy_mode = tmode;
2703 for (bitpos = 0, xbitpos = padding_correction;
2704 bitpos < bytes * BITS_PER_UNIT;
2705 bitpos += bitsize, xbitpos += bitsize)
2707 /* We need a new source operand each time xbitpos is on a
2708 word boundary and when xbitpos == padding_correction
2709 (the first time through). */
2710 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2711 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2713 /* We need a new destination operand each time bitpos is on
2714 a word boundary. */
2715 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2716 dst = target;
2717 else if (bitpos % BITS_PER_WORD == 0)
2718 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2720 /* Use xbitpos for the source extraction (right justified) and
2721 bitpos for the destination store (left justified). */
2722 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2723 extract_bit_field (src, bitsize,
2724 xbitpos % BITS_PER_WORD, 1,
2725 NULL_RTX, copy_mode, copy_mode,
2726 false, NULL),
2727 false);
2731 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2732 register if it contains any data, otherwise return null.
2734 This is used on targets that return BLKmode values in registers. */
2737 copy_blkmode_to_reg (machine_mode mode, tree src)
2739 int i, n_regs;
2740 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2741 unsigned int bitsize;
2742 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2743 machine_mode dst_mode;
2745 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2747 x = expand_normal (src);
2749 bytes = int_size_in_bytes (TREE_TYPE (src));
2750 if (bytes == 0)
2751 return NULL_RTX;
2753 /* If the structure doesn't take up a whole number of words, see
2754 whether the register value should be padded on the left or on
2755 the right. Set PADDING_CORRECTION to the number of padding
2756 bits needed on the left side.
2758 In most ABIs, the structure will be returned at the least end of
2759 the register, which translates to right padding on little-endian
2760 targets and left padding on big-endian targets. The opposite
2761 holds if the structure is returned at the most significant
2762 end of the register. */
2763 if (bytes % UNITS_PER_WORD != 0
2764 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2765 ? !BYTES_BIG_ENDIAN
2766 : BYTES_BIG_ENDIAN))
2767 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2768 * BITS_PER_UNIT));
2770 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2771 dst_words = XALLOCAVEC (rtx, n_regs);
2772 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2774 /* Copy the structure BITSIZE bits at a time. */
2775 for (bitpos = 0, xbitpos = padding_correction;
2776 bitpos < bytes * BITS_PER_UNIT;
2777 bitpos += bitsize, xbitpos += bitsize)
2779 /* We need a new destination pseudo each time xbitpos is
2780 on a word boundary and when xbitpos == padding_correction
2781 (the first time through). */
2782 if (xbitpos % BITS_PER_WORD == 0
2783 || xbitpos == padding_correction)
2785 /* Generate an appropriate register. */
2786 dst_word = gen_reg_rtx (word_mode);
2787 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2789 /* Clear the destination before we move anything into it. */
2790 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2793 /* We need a new source operand each time bitpos is on a word
2794 boundary. */
2795 if (bitpos % BITS_PER_WORD == 0)
2796 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2798 /* Use bitpos for the source extraction (left justified) and
2799 xbitpos for the destination store (right justified). */
2800 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2801 0, 0, word_mode,
2802 extract_bit_field (src_word, bitsize,
2803 bitpos % BITS_PER_WORD, 1,
2804 NULL_RTX, word_mode, word_mode,
2805 false, NULL),
2806 false);
2809 if (mode == BLKmode)
2811 /* Find the smallest integer mode large enough to hold the
2812 entire structure. */
2813 opt_scalar_int_mode mode_iter;
2814 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2815 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2816 break;
2818 /* A suitable mode should have been found. */
2819 mode = mode_iter.require ();
2822 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2823 dst_mode = word_mode;
2824 else
2825 dst_mode = mode;
2826 dst = gen_reg_rtx (dst_mode);
2828 for (i = 0; i < n_regs; i++)
2829 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2831 if (mode != dst_mode)
2832 dst = gen_lowpart (mode, dst);
2834 return dst;
2837 /* Add a USE expression for REG to the (possibly empty) list pointed
2838 to by CALL_FUSAGE. REG must denote a hard register. */
2840 void
2841 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2843 gcc_assert (REG_P (reg));
2845 if (!HARD_REGISTER_P (reg))
2846 return;
2848 *call_fusage
2849 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2852 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2853 to by CALL_FUSAGE. REG must denote a hard register. */
2855 void
2856 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2858 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2860 *call_fusage
2861 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2864 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2865 starting at REGNO. All of these registers must be hard registers. */
2867 void
2868 use_regs (rtx *call_fusage, int regno, int nregs)
2870 int i;
2872 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2874 for (i = 0; i < nregs; i++)
2875 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2878 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2879 PARALLEL REGS. This is for calls that pass values in multiple
2880 non-contiguous locations. The Irix 6 ABI has examples of this. */
2882 void
2883 use_group_regs (rtx *call_fusage, rtx regs)
2885 int i;
2887 for (i = 0; i < XVECLEN (regs, 0); i++)
2889 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2891 /* A NULL entry means the parameter goes both on the stack and in
2892 registers. This can also be a MEM for targets that pass values
2893 partially on the stack and partially in registers. */
2894 if (reg != 0 && REG_P (reg))
2895 use_reg (call_fusage, reg);
2899 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2900 assigment and the code of the expresion on the RHS is CODE. Return
2901 NULL otherwise. */
2903 static gimple *
2904 get_def_for_expr (tree name, enum tree_code code)
2906 gimple *def_stmt;
2908 if (TREE_CODE (name) != SSA_NAME)
2909 return NULL;
2911 def_stmt = get_gimple_for_ssa_name (name);
2912 if (!def_stmt
2913 || gimple_assign_rhs_code (def_stmt) != code)
2914 return NULL;
2916 return def_stmt;
2919 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2920 assigment and the class of the expresion on the RHS is CLASS. Return
2921 NULL otherwise. */
2923 static gimple *
2924 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2926 gimple *def_stmt;
2928 if (TREE_CODE (name) != SSA_NAME)
2929 return NULL;
2931 def_stmt = get_gimple_for_ssa_name (name);
2932 if (!def_stmt
2933 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2934 return NULL;
2936 return def_stmt;
2939 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2940 its length in bytes. */
2943 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2944 unsigned int expected_align, HOST_WIDE_INT expected_size,
2945 unsigned HOST_WIDE_INT min_size,
2946 unsigned HOST_WIDE_INT max_size,
2947 unsigned HOST_WIDE_INT probable_max_size)
2949 machine_mode mode = GET_MODE (object);
2950 unsigned int align;
2952 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2954 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2955 just move a zero. Otherwise, do this a piece at a time. */
2956 if (mode != BLKmode
2957 && CONST_INT_P (size)
2958 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2960 rtx zero = CONST0_RTX (mode);
2961 if (zero != NULL)
2963 emit_move_insn (object, zero);
2964 return NULL;
2967 if (COMPLEX_MODE_P (mode))
2969 zero = CONST0_RTX (GET_MODE_INNER (mode));
2970 if (zero != NULL)
2972 write_complex_part (object, zero, 0);
2973 write_complex_part (object, zero, 1);
2974 return NULL;
2979 if (size == const0_rtx)
2980 return NULL;
2982 align = MEM_ALIGN (object);
2984 if (CONST_INT_P (size)
2985 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2986 CLEAR_BY_PIECES,
2987 optimize_insn_for_speed_p ()))
2988 clear_by_pieces (object, INTVAL (size), align);
2989 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2990 expected_align, expected_size,
2991 min_size, max_size, probable_max_size))
2993 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2994 return set_storage_via_libcall (object, size, const0_rtx,
2995 method == BLOCK_OP_TAILCALL);
2996 else
2997 gcc_unreachable ();
2999 return NULL;
3003 clear_storage (rtx object, rtx size, enum block_op_methods method)
3005 unsigned HOST_WIDE_INT max, min = 0;
3006 if (GET_CODE (size) == CONST_INT)
3007 min = max = UINTVAL (size);
3008 else
3009 max = GET_MODE_MASK (GET_MODE (size));
3010 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3014 /* A subroutine of clear_storage. Expand a call to memset.
3015 Return the return value of memset, 0 otherwise. */
3018 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3020 tree call_expr, fn, object_tree, size_tree, val_tree;
3021 machine_mode size_mode;
3023 object = copy_addr_to_reg (XEXP (object, 0));
3024 object_tree = make_tree (ptr_type_node, object);
3026 if (!CONST_INT_P (val))
3027 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3028 val_tree = make_tree (integer_type_node, val);
3030 size_mode = TYPE_MODE (sizetype);
3031 size = convert_to_mode (size_mode, size, 1);
3032 size = copy_to_mode_reg (size_mode, size);
3033 size_tree = make_tree (sizetype, size);
3035 /* It is incorrect to use the libcall calling conventions for calls to
3036 memset because it can be provided by the user. */
3037 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3038 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3039 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3041 return expand_call (call_expr, NULL_RTX, false);
3044 /* Expand a setmem pattern; return true if successful. */
3046 bool
3047 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3048 unsigned int expected_align, HOST_WIDE_INT expected_size,
3049 unsigned HOST_WIDE_INT min_size,
3050 unsigned HOST_WIDE_INT max_size,
3051 unsigned HOST_WIDE_INT probable_max_size)
3053 /* Try the most limited insn first, because there's no point
3054 including more than one in the machine description unless
3055 the more limited one has some advantage. */
3057 if (expected_align < align)
3058 expected_align = align;
3059 if (expected_size != -1)
3061 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3062 expected_size = max_size;
3063 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3064 expected_size = min_size;
3067 opt_scalar_int_mode mode_iter;
3068 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3070 scalar_int_mode mode = mode_iter.require ();
3071 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3073 if (code != CODE_FOR_nothing
3074 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3075 here because if SIZE is less than the mode mask, as it is
3076 returned by the macro, it will definitely be less than the
3077 actual mode mask. Since SIZE is within the Pmode address
3078 space, we limit MODE to Pmode. */
3079 && ((CONST_INT_P (size)
3080 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3081 <= (GET_MODE_MASK (mode) >> 1)))
3082 || max_size <= (GET_MODE_MASK (mode) >> 1)
3083 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3085 struct expand_operand ops[9];
3086 unsigned int nops;
3088 nops = insn_data[(int) code].n_generator_args;
3089 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3091 create_fixed_operand (&ops[0], object);
3092 /* The check above guarantees that this size conversion is valid. */
3093 create_convert_operand_to (&ops[1], size, mode, true);
3094 create_convert_operand_from (&ops[2], val, byte_mode, true);
3095 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3096 if (nops >= 6)
3098 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3099 create_integer_operand (&ops[5], expected_size);
3101 if (nops >= 8)
3103 create_integer_operand (&ops[6], min_size);
3104 /* If we can not represent the maximal size,
3105 make parameter NULL. */
3106 if ((HOST_WIDE_INT) max_size != -1)
3107 create_integer_operand (&ops[7], max_size);
3108 else
3109 create_fixed_operand (&ops[7], NULL);
3111 if (nops == 9)
3113 /* If we can not represent the maximal size,
3114 make parameter NULL. */
3115 if ((HOST_WIDE_INT) probable_max_size != -1)
3116 create_integer_operand (&ops[8], probable_max_size);
3117 else
3118 create_fixed_operand (&ops[8], NULL);
3120 if (maybe_expand_insn (code, nops, ops))
3121 return true;
3125 return false;
3129 /* Write to one of the components of the complex value CPLX. Write VAL to
3130 the real part if IMAG_P is false, and the imaginary part if its true. */
3132 void
3133 write_complex_part (rtx cplx, rtx val, bool imag_p)
3135 machine_mode cmode;
3136 scalar_mode imode;
3137 unsigned ibitsize;
3139 if (GET_CODE (cplx) == CONCAT)
3141 emit_move_insn (XEXP (cplx, imag_p), val);
3142 return;
3145 cmode = GET_MODE (cplx);
3146 imode = GET_MODE_INNER (cmode);
3147 ibitsize = GET_MODE_BITSIZE (imode);
3149 /* For MEMs simplify_gen_subreg may generate an invalid new address
3150 because, e.g., the original address is considered mode-dependent
3151 by the target, which restricts simplify_subreg from invoking
3152 adjust_address_nv. Instead of preparing fallback support for an
3153 invalid address, we call adjust_address_nv directly. */
3154 if (MEM_P (cplx))
3156 emit_move_insn (adjust_address_nv (cplx, imode,
3157 imag_p ? GET_MODE_SIZE (imode) : 0),
3158 val);
3159 return;
3162 /* If the sub-object is at least word sized, then we know that subregging
3163 will work. This special case is important, since store_bit_field
3164 wants to operate on integer modes, and there's rarely an OImode to
3165 correspond to TCmode. */
3166 if (ibitsize >= BITS_PER_WORD
3167 /* For hard regs we have exact predicates. Assume we can split
3168 the original object if it spans an even number of hard regs.
3169 This special case is important for SCmode on 64-bit platforms
3170 where the natural size of floating-point regs is 32-bit. */
3171 || (REG_P (cplx)
3172 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3173 && REG_NREGS (cplx) % 2 == 0))
3175 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3176 imag_p ? GET_MODE_SIZE (imode) : 0);
3177 if (part)
3179 emit_move_insn (part, val);
3180 return;
3182 else
3183 /* simplify_gen_subreg may fail for sub-word MEMs. */
3184 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3187 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3188 false);
3191 /* Extract one of the components of the complex value CPLX. Extract the
3192 real part if IMAG_P is false, and the imaginary part if it's true. */
3195 read_complex_part (rtx cplx, bool imag_p)
3197 machine_mode cmode;
3198 scalar_mode imode;
3199 unsigned ibitsize;
3201 if (GET_CODE (cplx) == CONCAT)
3202 return XEXP (cplx, imag_p);
3204 cmode = GET_MODE (cplx);
3205 imode = GET_MODE_INNER (cmode);
3206 ibitsize = GET_MODE_BITSIZE (imode);
3208 /* Special case reads from complex constants that got spilled to memory. */
3209 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3211 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3212 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3214 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3215 if (CONSTANT_CLASS_P (part))
3216 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3220 /* For MEMs simplify_gen_subreg may generate an invalid new address
3221 because, e.g., the original address is considered mode-dependent
3222 by the target, which restricts simplify_subreg from invoking
3223 adjust_address_nv. Instead of preparing fallback support for an
3224 invalid address, we call adjust_address_nv directly. */
3225 if (MEM_P (cplx))
3226 return adjust_address_nv (cplx, imode,
3227 imag_p ? GET_MODE_SIZE (imode) : 0);
3229 /* If the sub-object is at least word sized, then we know that subregging
3230 will work. This special case is important, since extract_bit_field
3231 wants to operate on integer modes, and there's rarely an OImode to
3232 correspond to TCmode. */
3233 if (ibitsize >= BITS_PER_WORD
3234 /* For hard regs we have exact predicates. Assume we can split
3235 the original object if it spans an even number of hard regs.
3236 This special case is important for SCmode on 64-bit platforms
3237 where the natural size of floating-point regs is 32-bit. */
3238 || (REG_P (cplx)
3239 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3240 && REG_NREGS (cplx) % 2 == 0))
3242 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3243 imag_p ? GET_MODE_SIZE (imode) : 0);
3244 if (ret)
3245 return ret;
3246 else
3247 /* simplify_gen_subreg may fail for sub-word MEMs. */
3248 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3251 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3252 true, NULL_RTX, imode, imode, false, NULL);
3255 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3256 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3257 represented in NEW_MODE. If FORCE is true, this will never happen, as
3258 we'll force-create a SUBREG if needed. */
3260 static rtx
3261 emit_move_change_mode (machine_mode new_mode,
3262 machine_mode old_mode, rtx x, bool force)
3264 rtx ret;
3266 if (push_operand (x, GET_MODE (x)))
3268 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3269 MEM_COPY_ATTRIBUTES (ret, x);
3271 else if (MEM_P (x))
3273 /* We don't have to worry about changing the address since the
3274 size in bytes is supposed to be the same. */
3275 if (reload_in_progress)
3277 /* Copy the MEM to change the mode and move any
3278 substitutions from the old MEM to the new one. */
3279 ret = adjust_address_nv (x, new_mode, 0);
3280 copy_replacements (x, ret);
3282 else
3283 ret = adjust_address (x, new_mode, 0);
3285 else
3287 /* Note that we do want simplify_subreg's behavior of validating
3288 that the new mode is ok for a hard register. If we were to use
3289 simplify_gen_subreg, we would create the subreg, but would
3290 probably run into the target not being able to implement it. */
3291 /* Except, of course, when FORCE is true, when this is exactly what
3292 we want. Which is needed for CCmodes on some targets. */
3293 if (force)
3294 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3295 else
3296 ret = simplify_subreg (new_mode, x, old_mode, 0);
3299 return ret;
3302 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3303 an integer mode of the same size as MODE. Returns the instruction
3304 emitted, or NULL if such a move could not be generated. */
3306 static rtx_insn *
3307 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3309 scalar_int_mode imode;
3310 enum insn_code code;
3312 /* There must exist a mode of the exact size we require. */
3313 if (!int_mode_for_mode (mode).exists (&imode))
3314 return NULL;
3316 /* The target must support moves in this mode. */
3317 code = optab_handler (mov_optab, imode);
3318 if (code == CODE_FOR_nothing)
3319 return NULL;
3321 x = emit_move_change_mode (imode, mode, x, force);
3322 if (x == NULL_RTX)
3323 return NULL;
3324 y = emit_move_change_mode (imode, mode, y, force);
3325 if (y == NULL_RTX)
3326 return NULL;
3327 return emit_insn (GEN_FCN (code) (x, y));
3330 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3331 Return an equivalent MEM that does not use an auto-increment. */
3334 emit_move_resolve_push (machine_mode mode, rtx x)
3336 enum rtx_code code = GET_CODE (XEXP (x, 0));
3337 HOST_WIDE_INT adjust;
3338 rtx temp;
3340 adjust = GET_MODE_SIZE (mode);
3341 #ifdef PUSH_ROUNDING
3342 adjust = PUSH_ROUNDING (adjust);
3343 #endif
3344 if (code == PRE_DEC || code == POST_DEC)
3345 adjust = -adjust;
3346 else if (code == PRE_MODIFY || code == POST_MODIFY)
3348 rtx expr = XEXP (XEXP (x, 0), 1);
3349 HOST_WIDE_INT val;
3351 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3352 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3353 val = INTVAL (XEXP (expr, 1));
3354 if (GET_CODE (expr) == MINUS)
3355 val = -val;
3356 gcc_assert (adjust == val || adjust == -val);
3357 adjust = val;
3360 /* Do not use anti_adjust_stack, since we don't want to update
3361 stack_pointer_delta. */
3362 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3363 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3364 0, OPTAB_LIB_WIDEN);
3365 if (temp != stack_pointer_rtx)
3366 emit_move_insn (stack_pointer_rtx, temp);
3368 switch (code)
3370 case PRE_INC:
3371 case PRE_DEC:
3372 case PRE_MODIFY:
3373 temp = stack_pointer_rtx;
3374 break;
3375 case POST_INC:
3376 case POST_DEC:
3377 case POST_MODIFY:
3378 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3379 break;
3380 default:
3381 gcc_unreachable ();
3384 return replace_equiv_address (x, temp);
3387 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3388 X is known to satisfy push_operand, and MODE is known to be complex.
3389 Returns the last instruction emitted. */
3391 rtx_insn *
3392 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3394 scalar_mode submode = GET_MODE_INNER (mode);
3395 bool imag_first;
3397 #ifdef PUSH_ROUNDING
3398 unsigned int submodesize = GET_MODE_SIZE (submode);
3400 /* In case we output to the stack, but the size is smaller than the
3401 machine can push exactly, we need to use move instructions. */
3402 if (PUSH_ROUNDING (submodesize) != submodesize)
3404 x = emit_move_resolve_push (mode, x);
3405 return emit_move_insn (x, y);
3407 #endif
3409 /* Note that the real part always precedes the imag part in memory
3410 regardless of machine's endianness. */
3411 switch (GET_CODE (XEXP (x, 0)))
3413 case PRE_DEC:
3414 case POST_DEC:
3415 imag_first = true;
3416 break;
3417 case PRE_INC:
3418 case POST_INC:
3419 imag_first = false;
3420 break;
3421 default:
3422 gcc_unreachable ();
3425 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3426 read_complex_part (y, imag_first));
3427 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3428 read_complex_part (y, !imag_first));
3431 /* A subroutine of emit_move_complex. Perform the move from Y to X
3432 via two moves of the parts. Returns the last instruction emitted. */
3434 rtx_insn *
3435 emit_move_complex_parts (rtx x, rtx y)
3437 /* Show the output dies here. This is necessary for SUBREGs
3438 of pseudos since we cannot track their lifetimes correctly;
3439 hard regs shouldn't appear here except as return values. */
3440 if (!reload_completed && !reload_in_progress
3441 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3442 emit_clobber (x);
3444 write_complex_part (x, read_complex_part (y, false), false);
3445 write_complex_part (x, read_complex_part (y, true), true);
3447 return get_last_insn ();
3450 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3451 MODE is known to be complex. Returns the last instruction emitted. */
3453 static rtx_insn *
3454 emit_move_complex (machine_mode mode, rtx x, rtx y)
3456 bool try_int;
3458 /* Need to take special care for pushes, to maintain proper ordering
3459 of the data, and possibly extra padding. */
3460 if (push_operand (x, mode))
3461 return emit_move_complex_push (mode, x, y);
3463 /* See if we can coerce the target into moving both values at once, except
3464 for floating point where we favor moving as parts if this is easy. */
3465 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3466 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3467 && !(REG_P (x)
3468 && HARD_REGISTER_P (x)
3469 && REG_NREGS (x) == 1)
3470 && !(REG_P (y)
3471 && HARD_REGISTER_P (y)
3472 && REG_NREGS (y) == 1))
3473 try_int = false;
3474 /* Not possible if the values are inherently not adjacent. */
3475 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3476 try_int = false;
3477 /* Is possible if both are registers (or subregs of registers). */
3478 else if (register_operand (x, mode) && register_operand (y, mode))
3479 try_int = true;
3480 /* If one of the operands is a memory, and alignment constraints
3481 are friendly enough, we may be able to do combined memory operations.
3482 We do not attempt this if Y is a constant because that combination is
3483 usually better with the by-parts thing below. */
3484 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3485 && (!STRICT_ALIGNMENT
3486 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3487 try_int = true;
3488 else
3489 try_int = false;
3491 if (try_int)
3493 rtx_insn *ret;
3495 /* For memory to memory moves, optimal behavior can be had with the
3496 existing block move logic. */
3497 if (MEM_P (x) && MEM_P (y))
3499 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3500 BLOCK_OP_NO_LIBCALL);
3501 return get_last_insn ();
3504 ret = emit_move_via_integer (mode, x, y, true);
3505 if (ret)
3506 return ret;
3509 return emit_move_complex_parts (x, y);
3512 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3513 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3515 static rtx_insn *
3516 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3518 rtx_insn *ret;
3520 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3521 if (mode != CCmode)
3523 enum insn_code code = optab_handler (mov_optab, CCmode);
3524 if (code != CODE_FOR_nothing)
3526 x = emit_move_change_mode (CCmode, mode, x, true);
3527 y = emit_move_change_mode (CCmode, mode, y, true);
3528 return emit_insn (GEN_FCN (code) (x, y));
3532 /* Otherwise, find the MODE_INT mode of the same width. */
3533 ret = emit_move_via_integer (mode, x, y, false);
3534 gcc_assert (ret != NULL);
3535 return ret;
3538 /* Return true if word I of OP lies entirely in the
3539 undefined bits of a paradoxical subreg. */
3541 static bool
3542 undefined_operand_subword_p (const_rtx op, int i)
3544 if (GET_CODE (op) != SUBREG)
3545 return false;
3546 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3547 HOST_WIDE_INT offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3548 return (offset >= GET_MODE_SIZE (innermostmode)
3549 || offset <= -UNITS_PER_WORD);
3552 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3553 MODE is any multi-word or full-word mode that lacks a move_insn
3554 pattern. Note that you will get better code if you define such
3555 patterns, even if they must turn into multiple assembler instructions. */
3557 static rtx_insn *
3558 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3560 rtx_insn *last_insn = 0;
3561 rtx_insn *seq;
3562 rtx inner;
3563 bool need_clobber;
3564 int i;
3566 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3568 /* If X is a push on the stack, do the push now and replace
3569 X with a reference to the stack pointer. */
3570 if (push_operand (x, mode))
3571 x = emit_move_resolve_push (mode, x);
3573 /* If we are in reload, see if either operand is a MEM whose address
3574 is scheduled for replacement. */
3575 if (reload_in_progress && MEM_P (x)
3576 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3577 x = replace_equiv_address_nv (x, inner);
3578 if (reload_in_progress && MEM_P (y)
3579 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3580 y = replace_equiv_address_nv (y, inner);
3582 start_sequence ();
3584 need_clobber = false;
3585 for (i = 0;
3586 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3587 i++)
3589 rtx xpart = operand_subword (x, i, 1, mode);
3590 rtx ypart;
3592 /* Do not generate code for a move if it would come entirely
3593 from the undefined bits of a paradoxical subreg. */
3594 if (undefined_operand_subword_p (y, i))
3595 continue;
3597 ypart = operand_subword (y, i, 1, mode);
3599 /* If we can't get a part of Y, put Y into memory if it is a
3600 constant. Otherwise, force it into a register. Then we must
3601 be able to get a part of Y. */
3602 if (ypart == 0 && CONSTANT_P (y))
3604 y = use_anchored_address (force_const_mem (mode, y));
3605 ypart = operand_subword (y, i, 1, mode);
3607 else if (ypart == 0)
3608 ypart = operand_subword_force (y, i, mode);
3610 gcc_assert (xpart && ypart);
3612 need_clobber |= (GET_CODE (xpart) == SUBREG);
3614 last_insn = emit_move_insn (xpart, ypart);
3617 seq = get_insns ();
3618 end_sequence ();
3620 /* Show the output dies here. This is necessary for SUBREGs
3621 of pseudos since we cannot track their lifetimes correctly;
3622 hard regs shouldn't appear here except as return values.
3623 We never want to emit such a clobber after reload. */
3624 if (x != y
3625 && ! (reload_in_progress || reload_completed)
3626 && need_clobber != 0)
3627 emit_clobber (x);
3629 emit_insn (seq);
3631 return last_insn;
3634 /* Low level part of emit_move_insn.
3635 Called just like emit_move_insn, but assumes X and Y
3636 are basically valid. */
3638 rtx_insn *
3639 emit_move_insn_1 (rtx x, rtx y)
3641 machine_mode mode = GET_MODE (x);
3642 enum insn_code code;
3644 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3646 code = optab_handler (mov_optab, mode);
3647 if (code != CODE_FOR_nothing)
3648 return emit_insn (GEN_FCN (code) (x, y));
3650 /* Expand complex moves by moving real part and imag part. */
3651 if (COMPLEX_MODE_P (mode))
3652 return emit_move_complex (mode, x, y);
3654 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3655 || ALL_FIXED_POINT_MODE_P (mode))
3657 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3659 /* If we can't find an integer mode, use multi words. */
3660 if (result)
3661 return result;
3662 else
3663 return emit_move_multi_word (mode, x, y);
3666 if (GET_MODE_CLASS (mode) == MODE_CC)
3667 return emit_move_ccmode (mode, x, y);
3669 /* Try using a move pattern for the corresponding integer mode. This is
3670 only safe when simplify_subreg can convert MODE constants into integer
3671 constants. At present, it can only do this reliably if the value
3672 fits within a HOST_WIDE_INT. */
3673 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3675 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3677 if (ret)
3679 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3680 return ret;
3684 return emit_move_multi_word (mode, x, y);
3687 /* Generate code to copy Y into X.
3688 Both Y and X must have the same mode, except that
3689 Y can be a constant with VOIDmode.
3690 This mode cannot be BLKmode; use emit_block_move for that.
3692 Return the last instruction emitted. */
3694 rtx_insn *
3695 emit_move_insn (rtx x, rtx y)
3697 machine_mode mode = GET_MODE (x);
3698 rtx y_cst = NULL_RTX;
3699 rtx_insn *last_insn;
3700 rtx set;
3702 gcc_assert (mode != BLKmode
3703 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3705 if (CONSTANT_P (y))
3707 if (optimize
3708 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3709 && (last_insn = compress_float_constant (x, y)))
3710 return last_insn;
3712 y_cst = y;
3714 if (!targetm.legitimate_constant_p (mode, y))
3716 y = force_const_mem (mode, y);
3718 /* If the target's cannot_force_const_mem prevented the spill,
3719 assume that the target's move expanders will also take care
3720 of the non-legitimate constant. */
3721 if (!y)
3722 y = y_cst;
3723 else
3724 y = use_anchored_address (y);
3728 /* If X or Y are memory references, verify that their addresses are valid
3729 for the machine. */
3730 if (MEM_P (x)
3731 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3732 MEM_ADDR_SPACE (x))
3733 && ! push_operand (x, GET_MODE (x))))
3734 x = validize_mem (x);
3736 if (MEM_P (y)
3737 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3738 MEM_ADDR_SPACE (y)))
3739 y = validize_mem (y);
3741 gcc_assert (mode != BLKmode);
3743 last_insn = emit_move_insn_1 (x, y);
3745 if (y_cst && REG_P (x)
3746 && (set = single_set (last_insn)) != NULL_RTX
3747 && SET_DEST (set) == x
3748 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3749 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3751 return last_insn;
3754 /* Generate the body of an instruction to copy Y into X.
3755 It may be a list of insns, if one insn isn't enough. */
3757 rtx_insn *
3758 gen_move_insn (rtx x, rtx y)
3760 rtx_insn *seq;
3762 start_sequence ();
3763 emit_move_insn_1 (x, y);
3764 seq = get_insns ();
3765 end_sequence ();
3766 return seq;
3769 /* If Y is representable exactly in a narrower mode, and the target can
3770 perform the extension directly from constant or memory, then emit the
3771 move as an extension. */
3773 static rtx_insn *
3774 compress_float_constant (rtx x, rtx y)
3776 machine_mode dstmode = GET_MODE (x);
3777 machine_mode orig_srcmode = GET_MODE (y);
3778 machine_mode srcmode;
3779 const REAL_VALUE_TYPE *r;
3780 int oldcost, newcost;
3781 bool speed = optimize_insn_for_speed_p ();
3783 r = CONST_DOUBLE_REAL_VALUE (y);
3785 if (targetm.legitimate_constant_p (dstmode, y))
3786 oldcost = set_src_cost (y, orig_srcmode, speed);
3787 else
3788 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3790 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3792 enum insn_code ic;
3793 rtx trunc_y;
3794 rtx_insn *last_insn;
3796 /* Skip if the target can't extend this way. */
3797 ic = can_extend_p (dstmode, srcmode, 0);
3798 if (ic == CODE_FOR_nothing)
3799 continue;
3801 /* Skip if the narrowed value isn't exact. */
3802 if (! exact_real_truncate (srcmode, r))
3803 continue;
3805 trunc_y = const_double_from_real_value (*r, srcmode);
3807 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3809 /* Skip if the target needs extra instructions to perform
3810 the extension. */
3811 if (!insn_operand_matches (ic, 1, trunc_y))
3812 continue;
3813 /* This is valid, but may not be cheaper than the original. */
3814 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3815 dstmode, speed);
3816 if (oldcost < newcost)
3817 continue;
3819 else if (float_extend_from_mem[dstmode][srcmode])
3821 trunc_y = force_const_mem (srcmode, trunc_y);
3822 /* This is valid, but may not be cheaper than the original. */
3823 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3824 dstmode, speed);
3825 if (oldcost < newcost)
3826 continue;
3827 trunc_y = validize_mem (trunc_y);
3829 else
3830 continue;
3832 /* For CSE's benefit, force the compressed constant pool entry
3833 into a new pseudo. This constant may be used in different modes,
3834 and if not, combine will put things back together for us. */
3835 trunc_y = force_reg (srcmode, trunc_y);
3837 /* If x is a hard register, perform the extension into a pseudo,
3838 so that e.g. stack realignment code is aware of it. */
3839 rtx target = x;
3840 if (REG_P (x) && HARD_REGISTER_P (x))
3841 target = gen_reg_rtx (dstmode);
3843 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3844 last_insn = get_last_insn ();
3846 if (REG_P (target))
3847 set_unique_reg_note (last_insn, REG_EQUAL, y);
3849 if (target != x)
3850 return emit_move_insn (x, target);
3851 return last_insn;
3854 return NULL;
3857 /* Pushing data onto the stack. */
3859 /* Push a block of length SIZE (perhaps variable)
3860 and return an rtx to address the beginning of the block.
3861 The value may be virtual_outgoing_args_rtx.
3863 EXTRA is the number of bytes of padding to push in addition to SIZE.
3864 BELOW nonzero means this padding comes at low addresses;
3865 otherwise, the padding comes at high addresses. */
3868 push_block (rtx size, int extra, int below)
3870 rtx temp;
3872 size = convert_modes (Pmode, ptr_mode, size, 1);
3873 if (CONSTANT_P (size))
3874 anti_adjust_stack (plus_constant (Pmode, size, extra));
3875 else if (REG_P (size) && extra == 0)
3876 anti_adjust_stack (size);
3877 else
3879 temp = copy_to_mode_reg (Pmode, size);
3880 if (extra != 0)
3881 temp = expand_binop (Pmode, add_optab, temp,
3882 gen_int_mode (extra, Pmode),
3883 temp, 0, OPTAB_LIB_WIDEN);
3884 anti_adjust_stack (temp);
3887 if (STACK_GROWS_DOWNWARD)
3889 temp = virtual_outgoing_args_rtx;
3890 if (extra != 0 && below)
3891 temp = plus_constant (Pmode, temp, extra);
3893 else
3895 if (CONST_INT_P (size))
3896 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3897 -INTVAL (size) - (below ? 0 : extra));
3898 else if (extra != 0 && !below)
3899 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3900 negate_rtx (Pmode, plus_constant (Pmode, size,
3901 extra)));
3902 else
3903 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3904 negate_rtx (Pmode, size));
3907 return memory_address (NARROWEST_INT_MODE, temp);
3910 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3912 static rtx
3913 mem_autoinc_base (rtx mem)
3915 if (MEM_P (mem))
3917 rtx addr = XEXP (mem, 0);
3918 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3919 return XEXP (addr, 0);
3921 return NULL;
3924 /* A utility routine used here, in reload, and in try_split. The insns
3925 after PREV up to and including LAST are known to adjust the stack,
3926 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3927 placing notes as appropriate. PREV may be NULL, indicating the
3928 entire insn sequence prior to LAST should be scanned.
3930 The set of allowed stack pointer modifications is small:
3931 (1) One or more auto-inc style memory references (aka pushes),
3932 (2) One or more addition/subtraction with the SP as destination,
3933 (3) A single move insn with the SP as destination,
3934 (4) A call_pop insn,
3935 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3937 Insns in the sequence that do not modify the SP are ignored,
3938 except for noreturn calls.
3940 The return value is the amount of adjustment that can be trivially
3941 verified, via immediate operand or auto-inc. If the adjustment
3942 cannot be trivially extracted, the return value is INT_MIN. */
3944 HOST_WIDE_INT
3945 find_args_size_adjust (rtx_insn *insn)
3947 rtx dest, set, pat;
3948 int i;
3950 pat = PATTERN (insn);
3951 set = NULL;
3953 /* Look for a call_pop pattern. */
3954 if (CALL_P (insn))
3956 /* We have to allow non-call_pop patterns for the case
3957 of emit_single_push_insn of a TLS address. */
3958 if (GET_CODE (pat) != PARALLEL)
3959 return 0;
3961 /* All call_pop have a stack pointer adjust in the parallel.
3962 The call itself is always first, and the stack adjust is
3963 usually last, so search from the end. */
3964 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3966 set = XVECEXP (pat, 0, i);
3967 if (GET_CODE (set) != SET)
3968 continue;
3969 dest = SET_DEST (set);
3970 if (dest == stack_pointer_rtx)
3971 break;
3973 /* We'd better have found the stack pointer adjust. */
3974 if (i == 0)
3975 return 0;
3976 /* Fall through to process the extracted SET and DEST
3977 as if it was a standalone insn. */
3979 else if (GET_CODE (pat) == SET)
3980 set = pat;
3981 else if ((set = single_set (insn)) != NULL)
3983 else if (GET_CODE (pat) == PARALLEL)
3985 /* ??? Some older ports use a parallel with a stack adjust
3986 and a store for a PUSH_ROUNDING pattern, rather than a
3987 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3988 /* ??? See h8300 and m68k, pushqi1. */
3989 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3991 set = XVECEXP (pat, 0, i);
3992 if (GET_CODE (set) != SET)
3993 continue;
3994 dest = SET_DEST (set);
3995 if (dest == stack_pointer_rtx)
3996 break;
3998 /* We do not expect an auto-inc of the sp in the parallel. */
3999 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4000 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4001 != stack_pointer_rtx);
4003 if (i < 0)
4004 return 0;
4006 else
4007 return 0;
4009 dest = SET_DEST (set);
4011 /* Look for direct modifications of the stack pointer. */
4012 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4014 /* Look for a trivial adjustment, otherwise assume nothing. */
4015 /* Note that the SPU restore_stack_block pattern refers to
4016 the stack pointer in V4SImode. Consider that non-trivial. */
4017 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4018 && GET_CODE (SET_SRC (set)) == PLUS
4019 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4020 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4021 return INTVAL (XEXP (SET_SRC (set), 1));
4022 /* ??? Reload can generate no-op moves, which will be cleaned
4023 up later. Recognize it and continue searching. */
4024 else if (rtx_equal_p (dest, SET_SRC (set)))
4025 return 0;
4026 else
4027 return HOST_WIDE_INT_MIN;
4029 else
4031 rtx mem, addr;
4033 /* Otherwise only think about autoinc patterns. */
4034 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4036 mem = dest;
4037 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4038 != stack_pointer_rtx);
4040 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4041 mem = SET_SRC (set);
4042 else
4043 return 0;
4045 addr = XEXP (mem, 0);
4046 switch (GET_CODE (addr))
4048 case PRE_INC:
4049 case POST_INC:
4050 return GET_MODE_SIZE (GET_MODE (mem));
4051 case PRE_DEC:
4052 case POST_DEC:
4053 return -GET_MODE_SIZE (GET_MODE (mem));
4054 case PRE_MODIFY:
4055 case POST_MODIFY:
4056 addr = XEXP (addr, 1);
4057 gcc_assert (GET_CODE (addr) == PLUS);
4058 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4059 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4060 return INTVAL (XEXP (addr, 1));
4061 default:
4062 gcc_unreachable ();
4068 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4070 int args_size = end_args_size;
4071 bool saw_unknown = false;
4072 rtx_insn *insn;
4074 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4076 HOST_WIDE_INT this_delta;
4078 if (!NONDEBUG_INSN_P (insn))
4079 continue;
4081 this_delta = find_args_size_adjust (insn);
4082 if (this_delta == 0)
4084 if (!CALL_P (insn)
4085 || ACCUMULATE_OUTGOING_ARGS
4086 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4087 continue;
4090 gcc_assert (!saw_unknown);
4091 if (this_delta == HOST_WIDE_INT_MIN)
4092 saw_unknown = true;
4094 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4095 if (STACK_GROWS_DOWNWARD)
4096 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4098 args_size -= this_delta;
4101 return saw_unknown ? INT_MIN : args_size;
4104 #ifdef PUSH_ROUNDING
4105 /* Emit single push insn. */
4107 static void
4108 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4110 rtx dest_addr;
4111 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4112 rtx dest;
4113 enum insn_code icode;
4115 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4116 /* If there is push pattern, use it. Otherwise try old way of throwing
4117 MEM representing push operation to move expander. */
4118 icode = optab_handler (push_optab, mode);
4119 if (icode != CODE_FOR_nothing)
4121 struct expand_operand ops[1];
4123 create_input_operand (&ops[0], x, mode);
4124 if (maybe_expand_insn (icode, 1, ops))
4125 return;
4127 if (GET_MODE_SIZE (mode) == rounded_size)
4128 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4129 /* If we are to pad downward, adjust the stack pointer first and
4130 then store X into the stack location using an offset. This is
4131 because emit_move_insn does not know how to pad; it does not have
4132 access to type. */
4133 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4135 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4136 HOST_WIDE_INT offset;
4138 emit_move_insn (stack_pointer_rtx,
4139 expand_binop (Pmode,
4140 STACK_GROWS_DOWNWARD ? sub_optab
4141 : add_optab,
4142 stack_pointer_rtx,
4143 gen_int_mode (rounded_size, Pmode),
4144 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4146 offset = (HOST_WIDE_INT) padding_size;
4147 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4148 /* We have already decremented the stack pointer, so get the
4149 previous value. */
4150 offset += (HOST_WIDE_INT) rounded_size;
4152 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4153 /* We have already incremented the stack pointer, so get the
4154 previous value. */
4155 offset -= (HOST_WIDE_INT) rounded_size;
4157 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4158 gen_int_mode (offset, Pmode));
4160 else
4162 if (STACK_GROWS_DOWNWARD)
4163 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4164 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4165 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4166 Pmode));
4167 else
4168 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4169 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4170 gen_int_mode (rounded_size, Pmode));
4172 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4175 dest = gen_rtx_MEM (mode, dest_addr);
4177 if (type != 0)
4179 set_mem_attributes (dest, type, 1);
4181 if (cfun->tail_call_marked)
4182 /* Function incoming arguments may overlap with sibling call
4183 outgoing arguments and we cannot allow reordering of reads
4184 from function arguments with stores to outgoing arguments
4185 of sibling calls. */
4186 set_mem_alias_set (dest, 0);
4188 emit_move_insn (dest, x);
4191 /* Emit and annotate a single push insn. */
4193 static void
4194 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4196 int delta, old_delta = stack_pointer_delta;
4197 rtx_insn *prev = get_last_insn ();
4198 rtx_insn *last;
4200 emit_single_push_insn_1 (mode, x, type);
4202 last = get_last_insn ();
4204 /* Notice the common case where we emitted exactly one insn. */
4205 if (PREV_INSN (last) == prev)
4207 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4208 return;
4211 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4212 gcc_assert (delta == INT_MIN || delta == old_delta);
4214 #endif
4216 /* If reading SIZE bytes from X will end up reading from
4217 Y return the number of bytes that overlap. Return -1
4218 if there is no overlap or -2 if we can't determine
4219 (for example when X and Y have different base registers). */
4221 static int
4222 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4224 rtx tmp = plus_constant (Pmode, x, size);
4225 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4227 if (!CONST_INT_P (sub))
4228 return -2;
4230 HOST_WIDE_INT val = INTVAL (sub);
4232 return IN_RANGE (val, 1, size) ? val : -1;
4235 /* Generate code to push X onto the stack, assuming it has mode MODE and
4236 type TYPE.
4237 MODE is redundant except when X is a CONST_INT (since they don't
4238 carry mode info).
4239 SIZE is an rtx for the size of data to be copied (in bytes),
4240 needed only if X is BLKmode.
4241 Return true if successful. May return false if asked to push a
4242 partial argument during a sibcall optimization (as specified by
4243 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4244 to not overlap.
4246 ALIGN (in bits) is maximum alignment we can assume.
4248 If PARTIAL and REG are both nonzero, then copy that many of the first
4249 bytes of X into registers starting with REG, and push the rest of X.
4250 The amount of space pushed is decreased by PARTIAL bytes.
4251 REG must be a hard register in this case.
4252 If REG is zero but PARTIAL is not, take any all others actions for an
4253 argument partially in registers, but do not actually load any
4254 registers.
4256 EXTRA is the amount in bytes of extra space to leave next to this arg.
4257 This is ignored if an argument block has already been allocated.
4259 On a machine that lacks real push insns, ARGS_ADDR is the address of
4260 the bottom of the argument block for this call. We use indexing off there
4261 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4262 argument block has not been preallocated.
4264 ARGS_SO_FAR is the size of args previously pushed for this call.
4266 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4267 for arguments passed in registers. If nonzero, it will be the number
4268 of bytes required. */
4270 bool
4271 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4272 unsigned int align, int partial, rtx reg, int extra,
4273 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4274 rtx alignment_pad, bool sibcall_p)
4276 rtx xinner;
4277 pad_direction stack_direction
4278 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4280 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4281 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4282 Default is below for small data on big-endian machines; else above. */
4283 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4285 /* Invert direction if stack is post-decrement.
4286 FIXME: why? */
4287 if (STACK_PUSH_CODE == POST_DEC)
4288 if (where_pad != PAD_NONE)
4289 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4291 xinner = x;
4293 int nregs = partial / UNITS_PER_WORD;
4294 rtx *tmp_regs = NULL;
4295 int overlapping = 0;
4297 if (mode == BLKmode
4298 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4300 /* Copy a block into the stack, entirely or partially. */
4302 rtx temp;
4303 int used;
4304 int offset;
4305 int skip;
4307 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4308 used = partial - offset;
4310 if (mode != BLKmode)
4312 /* A value is to be stored in an insufficiently aligned
4313 stack slot; copy via a suitably aligned slot if
4314 necessary. */
4315 size = GEN_INT (GET_MODE_SIZE (mode));
4316 if (!MEM_P (xinner))
4318 temp = assign_temp (type, 1, 1);
4319 emit_move_insn (temp, xinner);
4320 xinner = temp;
4324 gcc_assert (size);
4326 /* USED is now the # of bytes we need not copy to the stack
4327 because registers will take care of them. */
4329 if (partial != 0)
4330 xinner = adjust_address (xinner, BLKmode, used);
4332 /* If the partial register-part of the arg counts in its stack size,
4333 skip the part of stack space corresponding to the registers.
4334 Otherwise, start copying to the beginning of the stack space,
4335 by setting SKIP to 0. */
4336 skip = (reg_parm_stack_space == 0) ? 0 : used;
4338 #ifdef PUSH_ROUNDING
4339 /* Do it with several push insns if that doesn't take lots of insns
4340 and if there is no difficulty with push insns that skip bytes
4341 on the stack for alignment purposes. */
4342 if (args_addr == 0
4343 && PUSH_ARGS
4344 && CONST_INT_P (size)
4345 && skip == 0
4346 && MEM_ALIGN (xinner) >= align
4347 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4348 /* Here we avoid the case of a structure whose weak alignment
4349 forces many pushes of a small amount of data,
4350 and such small pushes do rounding that causes trouble. */
4351 && ((!targetm.slow_unaligned_access (word_mode, align))
4352 || align >= BIGGEST_ALIGNMENT
4353 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4354 == (align / BITS_PER_UNIT)))
4355 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4357 /* Push padding now if padding above and stack grows down,
4358 or if padding below and stack grows up.
4359 But if space already allocated, this has already been done. */
4360 if (extra && args_addr == 0
4361 && where_pad != PAD_NONE && where_pad != stack_direction)
4362 anti_adjust_stack (GEN_INT (extra));
4364 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4366 else
4367 #endif /* PUSH_ROUNDING */
4369 rtx target;
4371 /* Otherwise make space on the stack and copy the data
4372 to the address of that space. */
4374 /* Deduct words put into registers from the size we must copy. */
4375 if (partial != 0)
4377 if (CONST_INT_P (size))
4378 size = GEN_INT (INTVAL (size) - used);
4379 else
4380 size = expand_binop (GET_MODE (size), sub_optab, size,
4381 gen_int_mode (used, GET_MODE (size)),
4382 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4385 /* Get the address of the stack space.
4386 In this case, we do not deal with EXTRA separately.
4387 A single stack adjust will do. */
4388 if (! args_addr)
4390 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4391 extra = 0;
4393 else if (CONST_INT_P (args_so_far))
4394 temp = memory_address (BLKmode,
4395 plus_constant (Pmode, args_addr,
4396 skip + INTVAL (args_so_far)));
4397 else
4398 temp = memory_address (BLKmode,
4399 plus_constant (Pmode,
4400 gen_rtx_PLUS (Pmode,
4401 args_addr,
4402 args_so_far),
4403 skip));
4405 if (!ACCUMULATE_OUTGOING_ARGS)
4407 /* If the source is referenced relative to the stack pointer,
4408 copy it to another register to stabilize it. We do not need
4409 to do this if we know that we won't be changing sp. */
4411 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4412 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4413 temp = copy_to_reg (temp);
4416 target = gen_rtx_MEM (BLKmode, temp);
4418 /* We do *not* set_mem_attributes here, because incoming arguments
4419 may overlap with sibling call outgoing arguments and we cannot
4420 allow reordering of reads from function arguments with stores
4421 to outgoing arguments of sibling calls. We do, however, want
4422 to record the alignment of the stack slot. */
4423 /* ALIGN may well be better aligned than TYPE, e.g. due to
4424 PARM_BOUNDARY. Assume the caller isn't lying. */
4425 set_mem_align (target, align);
4427 /* If part should go in registers and pushing to that part would
4428 overwrite some of the values that need to go into regs, load the
4429 overlapping values into temporary pseudos to be moved into the hard
4430 regs at the end after the stack pushing has completed.
4431 We cannot load them directly into the hard regs here because
4432 they can be clobbered by the block move expansions.
4433 See PR 65358. */
4435 if (partial > 0 && reg != 0 && mode == BLKmode
4436 && GET_CODE (reg) != PARALLEL)
4438 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4439 if (overlapping > 0)
4441 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4442 overlapping /= UNITS_PER_WORD;
4444 tmp_regs = XALLOCAVEC (rtx, overlapping);
4446 for (int i = 0; i < overlapping; i++)
4447 tmp_regs[i] = gen_reg_rtx (word_mode);
4449 for (int i = 0; i < overlapping; i++)
4450 emit_move_insn (tmp_regs[i],
4451 operand_subword_force (target, i, mode));
4453 else if (overlapping == -1)
4454 overlapping = 0;
4455 /* Could not determine whether there is overlap.
4456 Fail the sibcall. */
4457 else
4459 overlapping = 0;
4460 if (sibcall_p)
4461 return false;
4464 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4467 else if (partial > 0)
4469 /* Scalar partly in registers. */
4471 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4472 int i;
4473 int not_stack;
4474 /* # bytes of start of argument
4475 that we must make space for but need not store. */
4476 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4477 int args_offset = INTVAL (args_so_far);
4478 int skip;
4480 /* Push padding now if padding above and stack grows down,
4481 or if padding below and stack grows up.
4482 But if space already allocated, this has already been done. */
4483 if (extra && args_addr == 0
4484 && where_pad != PAD_NONE && where_pad != stack_direction)
4485 anti_adjust_stack (GEN_INT (extra));
4487 /* If we make space by pushing it, we might as well push
4488 the real data. Otherwise, we can leave OFFSET nonzero
4489 and leave the space uninitialized. */
4490 if (args_addr == 0)
4491 offset = 0;
4493 /* Now NOT_STACK gets the number of words that we don't need to
4494 allocate on the stack. Convert OFFSET to words too. */
4495 not_stack = (partial - offset) / UNITS_PER_WORD;
4496 offset /= UNITS_PER_WORD;
4498 /* If the partial register-part of the arg counts in its stack size,
4499 skip the part of stack space corresponding to the registers.
4500 Otherwise, start copying to the beginning of the stack space,
4501 by setting SKIP to 0. */
4502 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4504 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4505 x = validize_mem (force_const_mem (mode, x));
4507 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4508 SUBREGs of such registers are not allowed. */
4509 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4510 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4511 x = copy_to_reg (x);
4513 /* Loop over all the words allocated on the stack for this arg. */
4514 /* We can do it by words, because any scalar bigger than a word
4515 has a size a multiple of a word. */
4516 for (i = size - 1; i >= not_stack; i--)
4517 if (i >= not_stack + offset)
4518 if (!emit_push_insn (operand_subword_force (x, i, mode),
4519 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4520 0, args_addr,
4521 GEN_INT (args_offset + ((i - not_stack + skip)
4522 * UNITS_PER_WORD)),
4523 reg_parm_stack_space, alignment_pad, sibcall_p))
4524 return false;
4526 else
4528 rtx addr;
4529 rtx dest;
4531 /* Push padding now if padding above and stack grows down,
4532 or if padding below and stack grows up.
4533 But if space already allocated, this has already been done. */
4534 if (extra && args_addr == 0
4535 && where_pad != PAD_NONE && where_pad != stack_direction)
4536 anti_adjust_stack (GEN_INT (extra));
4538 #ifdef PUSH_ROUNDING
4539 if (args_addr == 0 && PUSH_ARGS)
4540 emit_single_push_insn (mode, x, type);
4541 else
4542 #endif
4544 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4545 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4547 /* We do *not* set_mem_attributes here, because incoming arguments
4548 may overlap with sibling call outgoing arguments and we cannot
4549 allow reordering of reads from function arguments with stores
4550 to outgoing arguments of sibling calls. We do, however, want
4551 to record the alignment of the stack slot. */
4552 /* ALIGN may well be better aligned than TYPE, e.g. due to
4553 PARM_BOUNDARY. Assume the caller isn't lying. */
4554 set_mem_align (dest, align);
4556 emit_move_insn (dest, x);
4560 /* Move the partial arguments into the registers and any overlapping
4561 values that we moved into the pseudos in tmp_regs. */
4562 if (partial > 0 && reg != 0)
4564 /* Handle calls that pass values in multiple non-contiguous locations.
4565 The Irix 6 ABI has examples of this. */
4566 if (GET_CODE (reg) == PARALLEL)
4567 emit_group_load (reg, x, type, -1);
4568 else
4570 gcc_assert (partial % UNITS_PER_WORD == 0);
4571 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4573 for (int i = 0; i < overlapping; i++)
4574 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4575 + nregs - overlapping + i),
4576 tmp_regs[i]);
4581 if (extra && args_addr == 0 && where_pad == stack_direction)
4582 anti_adjust_stack (GEN_INT (extra));
4584 if (alignment_pad && args_addr == 0)
4585 anti_adjust_stack (alignment_pad);
4587 return true;
4590 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4591 operations. */
4593 static rtx
4594 get_subtarget (rtx x)
4596 return (optimize
4597 || x == 0
4598 /* Only registers can be subtargets. */
4599 || !REG_P (x)
4600 /* Don't use hard regs to avoid extending their life. */
4601 || REGNO (x) < FIRST_PSEUDO_REGISTER
4602 ? 0 : x);
4605 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4606 FIELD is a bitfield. Returns true if the optimization was successful,
4607 and there's nothing else to do. */
4609 static bool
4610 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4611 unsigned HOST_WIDE_INT bitpos,
4612 unsigned HOST_WIDE_INT bitregion_start,
4613 unsigned HOST_WIDE_INT bitregion_end,
4614 machine_mode mode1, rtx str_rtx,
4615 tree to, tree src, bool reverse)
4617 machine_mode str_mode = GET_MODE (str_rtx);
4618 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4619 tree op0, op1;
4620 rtx value, result;
4621 optab binop;
4622 gimple *srcstmt;
4623 enum tree_code code;
4625 if (mode1 != VOIDmode
4626 || bitsize >= BITS_PER_WORD
4627 || str_bitsize > BITS_PER_WORD
4628 || TREE_SIDE_EFFECTS (to)
4629 || TREE_THIS_VOLATILE (to))
4630 return false;
4632 STRIP_NOPS (src);
4633 if (TREE_CODE (src) != SSA_NAME)
4634 return false;
4635 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4636 return false;
4638 srcstmt = get_gimple_for_ssa_name (src);
4639 if (!srcstmt
4640 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4641 return false;
4643 code = gimple_assign_rhs_code (srcstmt);
4645 op0 = gimple_assign_rhs1 (srcstmt);
4647 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4648 to find its initialization. Hopefully the initialization will
4649 be from a bitfield load. */
4650 if (TREE_CODE (op0) == SSA_NAME)
4652 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4654 /* We want to eventually have OP0 be the same as TO, which
4655 should be a bitfield. */
4656 if (!op0stmt
4657 || !is_gimple_assign (op0stmt)
4658 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4659 return false;
4660 op0 = gimple_assign_rhs1 (op0stmt);
4663 op1 = gimple_assign_rhs2 (srcstmt);
4665 if (!operand_equal_p (to, op0, 0))
4666 return false;
4668 if (MEM_P (str_rtx))
4670 unsigned HOST_WIDE_INT offset1;
4672 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4673 str_bitsize = BITS_PER_WORD;
4675 scalar_int_mode best_mode;
4676 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4677 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4678 return false;
4679 str_mode = best_mode;
4680 str_bitsize = GET_MODE_BITSIZE (best_mode);
4682 offset1 = bitpos;
4683 bitpos %= str_bitsize;
4684 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4685 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4687 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4688 return false;
4689 else
4690 gcc_assert (!reverse);
4692 /* If the bit field covers the whole REG/MEM, store_field
4693 will likely generate better code. */
4694 if (bitsize >= str_bitsize)
4695 return false;
4697 /* We can't handle fields split across multiple entities. */
4698 if (bitpos + bitsize > str_bitsize)
4699 return false;
4701 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4702 bitpos = str_bitsize - bitpos - bitsize;
4704 switch (code)
4706 case PLUS_EXPR:
4707 case MINUS_EXPR:
4708 /* For now, just optimize the case of the topmost bitfield
4709 where we don't need to do any masking and also
4710 1 bit bitfields where xor can be used.
4711 We might win by one instruction for the other bitfields
4712 too if insv/extv instructions aren't used, so that
4713 can be added later. */
4714 if ((reverse || bitpos + bitsize != str_bitsize)
4715 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4716 break;
4718 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4719 value = convert_modes (str_mode,
4720 TYPE_MODE (TREE_TYPE (op1)), value,
4721 TYPE_UNSIGNED (TREE_TYPE (op1)));
4723 /* We may be accessing data outside the field, which means
4724 we can alias adjacent data. */
4725 if (MEM_P (str_rtx))
4727 str_rtx = shallow_copy_rtx (str_rtx);
4728 set_mem_alias_set (str_rtx, 0);
4729 set_mem_expr (str_rtx, 0);
4732 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4734 value = expand_and (str_mode, value, const1_rtx, NULL);
4735 binop = xor_optab;
4737 else
4738 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4740 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4741 if (reverse)
4742 value = flip_storage_order (str_mode, value);
4743 result = expand_binop (str_mode, binop, str_rtx,
4744 value, str_rtx, 1, OPTAB_WIDEN);
4745 if (result != str_rtx)
4746 emit_move_insn (str_rtx, result);
4747 return true;
4749 case BIT_IOR_EXPR:
4750 case BIT_XOR_EXPR:
4751 if (TREE_CODE (op1) != INTEGER_CST)
4752 break;
4753 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4754 value = convert_modes (str_mode,
4755 TYPE_MODE (TREE_TYPE (op1)), value,
4756 TYPE_UNSIGNED (TREE_TYPE (op1)));
4758 /* We may be accessing data outside the field, which means
4759 we can alias adjacent data. */
4760 if (MEM_P (str_rtx))
4762 str_rtx = shallow_copy_rtx (str_rtx);
4763 set_mem_alias_set (str_rtx, 0);
4764 set_mem_expr (str_rtx, 0);
4767 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4768 if (bitpos + bitsize != str_bitsize)
4770 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4771 str_mode);
4772 value = expand_and (str_mode, value, mask, NULL_RTX);
4774 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4775 if (reverse)
4776 value = flip_storage_order (str_mode, value);
4777 result = expand_binop (str_mode, binop, str_rtx,
4778 value, str_rtx, 1, OPTAB_WIDEN);
4779 if (result != str_rtx)
4780 emit_move_insn (str_rtx, result);
4781 return true;
4783 default:
4784 break;
4787 return false;
4790 /* In the C++ memory model, consecutive bit fields in a structure are
4791 considered one memory location.
4793 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4794 returns the bit range of consecutive bits in which this COMPONENT_REF
4795 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4796 and *OFFSET may be adjusted in the process.
4798 If the access does not need to be restricted, 0 is returned in both
4799 *BITSTART and *BITEND. */
4801 void
4802 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4803 unsigned HOST_WIDE_INT *bitend,
4804 tree exp,
4805 HOST_WIDE_INT *bitpos,
4806 tree *offset)
4808 HOST_WIDE_INT bitoffset;
4809 tree field, repr;
4811 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4813 field = TREE_OPERAND (exp, 1);
4814 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4815 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4816 need to limit the range we can access. */
4817 if (!repr)
4819 *bitstart = *bitend = 0;
4820 return;
4823 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4824 part of a larger bit field, then the representative does not serve any
4825 useful purpose. This can occur in Ada. */
4826 if (handled_component_p (TREE_OPERAND (exp, 0)))
4828 machine_mode rmode;
4829 HOST_WIDE_INT rbitsize, rbitpos;
4830 tree roffset;
4831 int unsignedp, reversep, volatilep = 0;
4832 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4833 &roffset, &rmode, &unsignedp, &reversep,
4834 &volatilep);
4835 if ((rbitpos % BITS_PER_UNIT) != 0)
4837 *bitstart = *bitend = 0;
4838 return;
4842 /* Compute the adjustment to bitpos from the offset of the field
4843 relative to the representative. DECL_FIELD_OFFSET of field and
4844 repr are the same by construction if they are not constants,
4845 see finish_bitfield_layout. */
4846 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4847 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4848 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4849 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4850 else
4851 bitoffset = 0;
4852 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4853 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4855 /* If the adjustment is larger than bitpos, we would have a negative bit
4856 position for the lower bound and this may wreak havoc later. Adjust
4857 offset and bitpos to make the lower bound non-negative in that case. */
4858 if (bitoffset > *bitpos)
4860 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4861 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4863 *bitpos += adjust;
4864 if (*offset == NULL_TREE)
4865 *offset = size_int (-adjust / BITS_PER_UNIT);
4866 else
4867 *offset
4868 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4869 *bitstart = 0;
4871 else
4872 *bitstart = *bitpos - bitoffset;
4874 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4877 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4878 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4879 DECL_RTL was not set yet, return NORTL. */
4881 static inline bool
4882 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4884 if (TREE_CODE (addr) != ADDR_EXPR)
4885 return false;
4887 tree base = TREE_OPERAND (addr, 0);
4889 if (!DECL_P (base)
4890 || TREE_ADDRESSABLE (base)
4891 || DECL_MODE (base) == BLKmode)
4892 return false;
4894 if (!DECL_RTL_SET_P (base))
4895 return nortl;
4897 return (!MEM_P (DECL_RTL (base)));
4900 /* Returns true if the MEM_REF REF refers to an object that does not
4901 reside in memory and has non-BLKmode. */
4903 static inline bool
4904 mem_ref_refers_to_non_mem_p (tree ref)
4906 tree base = TREE_OPERAND (ref, 0);
4907 return addr_expr_of_non_mem_decl_p_1 (base, false);
4910 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4911 is true, try generating a nontemporal store. */
4913 void
4914 expand_assignment (tree to, tree from, bool nontemporal)
4916 rtx to_rtx = 0;
4917 rtx result;
4918 machine_mode mode;
4919 unsigned int align;
4920 enum insn_code icode;
4922 /* Don't crash if the lhs of the assignment was erroneous. */
4923 if (TREE_CODE (to) == ERROR_MARK)
4925 expand_normal (from);
4926 return;
4929 /* Optimize away no-op moves without side-effects. */
4930 if (operand_equal_p (to, from, 0))
4931 return;
4933 /* Handle misaligned stores. */
4934 mode = TYPE_MODE (TREE_TYPE (to));
4935 if ((TREE_CODE (to) == MEM_REF
4936 || TREE_CODE (to) == TARGET_MEM_REF)
4937 && mode != BLKmode
4938 && !mem_ref_refers_to_non_mem_p (to)
4939 && ((align = get_object_alignment (to))
4940 < GET_MODE_ALIGNMENT (mode))
4941 && (((icode = optab_handler (movmisalign_optab, mode))
4942 != CODE_FOR_nothing)
4943 || targetm.slow_unaligned_access (mode, align)))
4945 rtx reg, mem;
4947 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4948 reg = force_not_mem (reg);
4949 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4950 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4951 reg = flip_storage_order (mode, reg);
4953 if (icode != CODE_FOR_nothing)
4955 struct expand_operand ops[2];
4957 create_fixed_operand (&ops[0], mem);
4958 create_input_operand (&ops[1], reg, mode);
4959 /* The movmisalign<mode> pattern cannot fail, else the assignment
4960 would silently be omitted. */
4961 expand_insn (icode, 2, ops);
4963 else
4964 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4965 false);
4966 return;
4969 /* Assignment of a structure component needs special treatment
4970 if the structure component's rtx is not simply a MEM.
4971 Assignment of an array element at a constant index, and assignment of
4972 an array element in an unaligned packed structure field, has the same
4973 problem. Same for (partially) storing into a non-memory object. */
4974 if (handled_component_p (to)
4975 || (TREE_CODE (to) == MEM_REF
4976 && (REF_REVERSE_STORAGE_ORDER (to)
4977 || mem_ref_refers_to_non_mem_p (to)))
4978 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4980 machine_mode mode1;
4981 HOST_WIDE_INT bitsize, bitpos;
4982 unsigned HOST_WIDE_INT bitregion_start = 0;
4983 unsigned HOST_WIDE_INT bitregion_end = 0;
4984 tree offset;
4985 int unsignedp, reversep, volatilep = 0;
4986 tree tem;
4988 push_temp_slots ();
4989 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4990 &unsignedp, &reversep, &volatilep);
4992 /* Make sure bitpos is not negative, it can wreak havoc later. */
4993 if (bitpos < 0)
4995 gcc_assert (offset == NULL_TREE);
4996 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
4997 bitpos &= BITS_PER_UNIT - 1;
5000 if (TREE_CODE (to) == COMPONENT_REF
5001 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5002 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5003 /* The C++ memory model naturally applies to byte-aligned fields.
5004 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5005 BITSIZE are not byte-aligned, there is no need to limit the range
5006 we can access. This can occur with packed structures in Ada. */
5007 else if (bitsize > 0
5008 && bitsize % BITS_PER_UNIT == 0
5009 && bitpos % BITS_PER_UNIT == 0)
5011 bitregion_start = bitpos;
5012 bitregion_end = bitpos + bitsize - 1;
5015 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5017 /* If the field has a mode, we want to access it in the
5018 field's mode, not the computed mode.
5019 If a MEM has VOIDmode (external with incomplete type),
5020 use BLKmode for it instead. */
5021 if (MEM_P (to_rtx))
5023 if (mode1 != VOIDmode)
5024 to_rtx = adjust_address (to_rtx, mode1, 0);
5025 else if (GET_MODE (to_rtx) == VOIDmode)
5026 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5029 if (offset != 0)
5031 machine_mode address_mode;
5032 rtx offset_rtx;
5034 if (!MEM_P (to_rtx))
5036 /* We can get constant negative offsets into arrays with broken
5037 user code. Translate this to a trap instead of ICEing. */
5038 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5039 expand_builtin_trap ();
5040 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5043 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5044 address_mode = get_address_mode (to_rtx);
5045 if (GET_MODE (offset_rtx) != address_mode)
5047 /* We cannot be sure that the RTL in offset_rtx is valid outside
5048 of a memory address context, so force it into a register
5049 before attempting to convert it to the desired mode. */
5050 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5051 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5054 /* If we have an expression in OFFSET_RTX and a non-zero
5055 byte offset in BITPOS, adding the byte offset before the
5056 OFFSET_RTX results in better intermediate code, which makes
5057 later rtl optimization passes perform better.
5059 We prefer intermediate code like this:
5061 r124:DI=r123:DI+0x18
5062 [r124:DI]=r121:DI
5064 ... instead of ...
5066 r124:DI=r123:DI+0x10
5067 [r124:DI+0x8]=r121:DI
5069 This is only done for aligned data values, as these can
5070 be expected to result in single move instructions. */
5071 if (mode1 != VOIDmode
5072 && bitpos != 0
5073 && bitsize > 0
5074 && (bitpos % bitsize) == 0
5075 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5076 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5078 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5079 bitregion_start = 0;
5080 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5081 bitregion_end -= bitpos;
5082 bitpos = 0;
5085 to_rtx = offset_address (to_rtx, offset_rtx,
5086 highest_pow2_factor_for_target (to,
5087 offset));
5090 /* No action is needed if the target is not a memory and the field
5091 lies completely outside that target. This can occur if the source
5092 code contains an out-of-bounds access to a small array. */
5093 if (!MEM_P (to_rtx)
5094 && GET_MODE (to_rtx) != BLKmode
5095 && (unsigned HOST_WIDE_INT) bitpos
5096 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5098 expand_normal (from);
5099 result = NULL;
5101 /* Handle expand_expr of a complex value returning a CONCAT. */
5102 else if (GET_CODE (to_rtx) == CONCAT)
5104 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5105 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5106 && bitpos == 0
5107 && bitsize == mode_bitsize)
5108 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5109 else if (bitsize == mode_bitsize / 2
5110 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5111 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5112 nontemporal, reversep);
5113 else if (bitpos + bitsize <= mode_bitsize / 2)
5114 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5115 bitregion_start, bitregion_end,
5116 mode1, from, get_alias_set (to),
5117 nontemporal, reversep);
5118 else if (bitpos >= mode_bitsize / 2)
5119 result = store_field (XEXP (to_rtx, 1), bitsize,
5120 bitpos - mode_bitsize / 2,
5121 bitregion_start, bitregion_end,
5122 mode1, from, get_alias_set (to),
5123 nontemporal, reversep);
5124 else if (bitpos == 0 && bitsize == mode_bitsize)
5126 rtx from_rtx;
5127 result = expand_normal (from);
5128 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5129 TYPE_MODE (TREE_TYPE (from)), 0);
5130 emit_move_insn (XEXP (to_rtx, 0),
5131 read_complex_part (from_rtx, false));
5132 emit_move_insn (XEXP (to_rtx, 1),
5133 read_complex_part (from_rtx, true));
5135 else
5137 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5138 GET_MODE_SIZE (GET_MODE (to_rtx)));
5139 write_complex_part (temp, XEXP (to_rtx, 0), false);
5140 write_complex_part (temp, XEXP (to_rtx, 1), true);
5141 result = store_field (temp, bitsize, bitpos,
5142 bitregion_start, bitregion_end,
5143 mode1, from, get_alias_set (to),
5144 nontemporal, reversep);
5145 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5146 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5149 else
5151 if (MEM_P (to_rtx))
5153 /* If the field is at offset zero, we could have been given the
5154 DECL_RTX of the parent struct. Don't munge it. */
5155 to_rtx = shallow_copy_rtx (to_rtx);
5156 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5157 if (volatilep)
5158 MEM_VOLATILE_P (to_rtx) = 1;
5161 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5162 bitregion_start, bitregion_end,
5163 mode1, to_rtx, to, from,
5164 reversep))
5165 result = NULL;
5166 else
5167 result = store_field (to_rtx, bitsize, bitpos,
5168 bitregion_start, bitregion_end,
5169 mode1, from, get_alias_set (to),
5170 nontemporal, reversep);
5173 if (result)
5174 preserve_temp_slots (result);
5175 pop_temp_slots ();
5176 return;
5179 /* If the rhs is a function call and its value is not an aggregate,
5180 call the function before we start to compute the lhs.
5181 This is needed for correct code for cases such as
5182 val = setjmp (buf) on machines where reference to val
5183 requires loading up part of an address in a separate insn.
5185 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5186 since it might be a promoted variable where the zero- or sign- extension
5187 needs to be done. Handling this in the normal way is safe because no
5188 computation is done before the call. The same is true for SSA names. */
5189 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5190 && COMPLETE_TYPE_P (TREE_TYPE (from))
5191 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5192 && ! (((VAR_P (to)
5193 || TREE_CODE (to) == PARM_DECL
5194 || TREE_CODE (to) == RESULT_DECL)
5195 && REG_P (DECL_RTL (to)))
5196 || TREE_CODE (to) == SSA_NAME))
5198 rtx value;
5199 rtx bounds;
5201 push_temp_slots ();
5202 value = expand_normal (from);
5204 /* Split value and bounds to store them separately. */
5205 chkp_split_slot (value, &value, &bounds);
5207 if (to_rtx == 0)
5208 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5210 /* Handle calls that return values in multiple non-contiguous locations.
5211 The Irix 6 ABI has examples of this. */
5212 if (GET_CODE (to_rtx) == PARALLEL)
5214 if (GET_CODE (value) == PARALLEL)
5215 emit_group_move (to_rtx, value);
5216 else
5217 emit_group_load (to_rtx, value, TREE_TYPE (from),
5218 int_size_in_bytes (TREE_TYPE (from)));
5220 else if (GET_CODE (value) == PARALLEL)
5221 emit_group_store (to_rtx, value, TREE_TYPE (from),
5222 int_size_in_bytes (TREE_TYPE (from)));
5223 else if (GET_MODE (to_rtx) == BLKmode)
5225 /* Handle calls that return BLKmode values in registers. */
5226 if (REG_P (value))
5227 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5228 else
5229 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5231 else
5233 if (POINTER_TYPE_P (TREE_TYPE (to)))
5234 value = convert_memory_address_addr_space
5235 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5236 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5238 emit_move_insn (to_rtx, value);
5241 /* Store bounds if required. */
5242 if (bounds
5243 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5245 gcc_assert (MEM_P (to_rtx));
5246 chkp_emit_bounds_store (bounds, value, to_rtx);
5249 preserve_temp_slots (to_rtx);
5250 pop_temp_slots ();
5251 return;
5254 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5255 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5257 /* Don't move directly into a return register. */
5258 if (TREE_CODE (to) == RESULT_DECL
5259 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5261 rtx temp;
5263 push_temp_slots ();
5265 /* If the source is itself a return value, it still is in a pseudo at
5266 this point so we can move it back to the return register directly. */
5267 if (REG_P (to_rtx)
5268 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5269 && TREE_CODE (from) != CALL_EXPR)
5270 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5271 else
5272 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5274 /* Handle calls that return values in multiple non-contiguous locations.
5275 The Irix 6 ABI has examples of this. */
5276 if (GET_CODE (to_rtx) == PARALLEL)
5278 if (GET_CODE (temp) == PARALLEL)
5279 emit_group_move (to_rtx, temp);
5280 else
5281 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5282 int_size_in_bytes (TREE_TYPE (from)));
5284 else if (temp)
5285 emit_move_insn (to_rtx, temp);
5287 preserve_temp_slots (to_rtx);
5288 pop_temp_slots ();
5289 return;
5292 /* In case we are returning the contents of an object which overlaps
5293 the place the value is being stored, use a safe function when copying
5294 a value through a pointer into a structure value return block. */
5295 if (TREE_CODE (to) == RESULT_DECL
5296 && TREE_CODE (from) == INDIRECT_REF
5297 && ADDR_SPACE_GENERIC_P
5298 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5299 && refs_may_alias_p (to, from)
5300 && cfun->returns_struct
5301 && !cfun->returns_pcc_struct)
5303 rtx from_rtx, size;
5305 push_temp_slots ();
5306 size = expr_size (from);
5307 from_rtx = expand_normal (from);
5309 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5311 preserve_temp_slots (to_rtx);
5312 pop_temp_slots ();
5313 return;
5316 /* Compute FROM and store the value in the rtx we got. */
5318 push_temp_slots ();
5319 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5320 preserve_temp_slots (result);
5321 pop_temp_slots ();
5322 return;
5325 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5326 succeeded, false otherwise. */
5328 bool
5329 emit_storent_insn (rtx to, rtx from)
5331 struct expand_operand ops[2];
5332 machine_mode mode = GET_MODE (to);
5333 enum insn_code code = optab_handler (storent_optab, mode);
5335 if (code == CODE_FOR_nothing)
5336 return false;
5338 create_fixed_operand (&ops[0], to);
5339 create_input_operand (&ops[1], from, mode);
5340 return maybe_expand_insn (code, 2, ops);
5343 /* Generate code for computing expression EXP,
5344 and storing the value into TARGET.
5346 If the mode is BLKmode then we may return TARGET itself.
5347 It turns out that in BLKmode it doesn't cause a problem.
5348 because C has no operators that could combine two different
5349 assignments into the same BLKmode object with different values
5350 with no sequence point. Will other languages need this to
5351 be more thorough?
5353 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5354 stack, and block moves may need to be treated specially.
5356 If NONTEMPORAL is true, try using a nontemporal store instruction.
5358 If REVERSE is true, the store is to be done in reverse order.
5360 If BTARGET is not NULL then computed bounds of EXP are
5361 associated with BTARGET. */
5364 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5365 bool nontemporal, bool reverse, tree btarget)
5367 rtx temp;
5368 rtx alt_rtl = NULL_RTX;
5369 location_t loc = curr_insn_location ();
5371 if (VOID_TYPE_P (TREE_TYPE (exp)))
5373 /* C++ can generate ?: expressions with a throw expression in one
5374 branch and an rvalue in the other. Here, we resolve attempts to
5375 store the throw expression's nonexistent result. */
5376 gcc_assert (!call_param_p);
5377 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5378 return NULL_RTX;
5380 if (TREE_CODE (exp) == COMPOUND_EXPR)
5382 /* Perform first part of compound expression, then assign from second
5383 part. */
5384 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5385 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5386 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5387 call_param_p, nontemporal, reverse,
5388 btarget);
5390 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5392 /* For conditional expression, get safe form of the target. Then
5393 test the condition, doing the appropriate assignment on either
5394 side. This avoids the creation of unnecessary temporaries.
5395 For non-BLKmode, it is more efficient not to do this. */
5397 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5399 do_pending_stack_adjust ();
5400 NO_DEFER_POP;
5401 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5402 profile_probability::uninitialized ());
5403 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5404 nontemporal, reverse, btarget);
5405 emit_jump_insn (targetm.gen_jump (lab2));
5406 emit_barrier ();
5407 emit_label (lab1);
5408 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5409 nontemporal, reverse, btarget);
5410 emit_label (lab2);
5411 OK_DEFER_POP;
5413 return NULL_RTX;
5415 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5416 /* If this is a scalar in a register that is stored in a wider mode
5417 than the declared mode, compute the result into its declared mode
5418 and then convert to the wider mode. Our value is the computed
5419 expression. */
5421 rtx inner_target = 0;
5422 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5423 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5425 /* We can do the conversion inside EXP, which will often result
5426 in some optimizations. Do the conversion in two steps: first
5427 change the signedness, if needed, then the extend. But don't
5428 do this if the type of EXP is a subtype of something else
5429 since then the conversion might involve more than just
5430 converting modes. */
5431 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5432 && TREE_TYPE (TREE_TYPE (exp)) == 0
5433 && GET_MODE_PRECISION (outer_mode)
5434 == TYPE_PRECISION (TREE_TYPE (exp)))
5436 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5437 TYPE_UNSIGNED (TREE_TYPE (exp))))
5439 /* Some types, e.g. Fortran's logical*4, won't have a signed
5440 version, so use the mode instead. */
5441 tree ntype
5442 = (signed_or_unsigned_type_for
5443 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5444 if (ntype == NULL)
5445 ntype = lang_hooks.types.type_for_mode
5446 (TYPE_MODE (TREE_TYPE (exp)),
5447 SUBREG_PROMOTED_SIGN (target));
5449 exp = fold_convert_loc (loc, ntype, exp);
5452 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5453 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5454 exp);
5456 inner_target = SUBREG_REG (target);
5459 temp = expand_expr (exp, inner_target, VOIDmode,
5460 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5462 /* Handle bounds returned by call. */
5463 if (TREE_CODE (exp) == CALL_EXPR)
5465 rtx bounds;
5466 chkp_split_slot (temp, &temp, &bounds);
5467 if (bounds && btarget)
5469 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5470 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5471 chkp_set_rtl_bounds (btarget, tmp);
5475 /* If TEMP is a VOIDmode constant, use convert_modes to make
5476 sure that we properly convert it. */
5477 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5479 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5480 temp, SUBREG_PROMOTED_SIGN (target));
5481 temp = convert_modes (inner_mode, outer_mode, temp,
5482 SUBREG_PROMOTED_SIGN (target));
5485 convert_move (SUBREG_REG (target), temp,
5486 SUBREG_PROMOTED_SIGN (target));
5488 return NULL_RTX;
5490 else if ((TREE_CODE (exp) == STRING_CST
5491 || (TREE_CODE (exp) == MEM_REF
5492 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5493 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5494 == STRING_CST
5495 && integer_zerop (TREE_OPERAND (exp, 1))))
5496 && !nontemporal && !call_param_p
5497 && MEM_P (target))
5499 /* Optimize initialization of an array with a STRING_CST. */
5500 HOST_WIDE_INT exp_len, str_copy_len;
5501 rtx dest_mem;
5502 tree str = TREE_CODE (exp) == STRING_CST
5503 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5505 exp_len = int_expr_size (exp);
5506 if (exp_len <= 0)
5507 goto normal_expr;
5509 if (TREE_STRING_LENGTH (str) <= 0)
5510 goto normal_expr;
5512 str_copy_len = strlen (TREE_STRING_POINTER (str));
5513 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5514 goto normal_expr;
5516 str_copy_len = TREE_STRING_LENGTH (str);
5517 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5518 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5520 str_copy_len += STORE_MAX_PIECES - 1;
5521 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5523 str_copy_len = MIN (str_copy_len, exp_len);
5524 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5525 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5526 MEM_ALIGN (target), false))
5527 goto normal_expr;
5529 dest_mem = target;
5531 dest_mem = store_by_pieces (dest_mem,
5532 str_copy_len, builtin_strncpy_read_str,
5533 CONST_CAST (char *,
5534 TREE_STRING_POINTER (str)),
5535 MEM_ALIGN (target), false,
5536 exp_len > str_copy_len ? 1 : 0);
5537 if (exp_len > str_copy_len)
5538 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5539 GEN_INT (exp_len - str_copy_len),
5540 BLOCK_OP_NORMAL);
5541 return NULL_RTX;
5543 else
5545 rtx tmp_target;
5547 normal_expr:
5548 /* If we want to use a nontemporal or a reverse order store, force the
5549 value into a register first. */
5550 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5551 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5552 (call_param_p
5553 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5554 &alt_rtl, false);
5556 /* Handle bounds returned by call. */
5557 if (TREE_CODE (exp) == CALL_EXPR)
5559 rtx bounds;
5560 chkp_split_slot (temp, &temp, &bounds);
5561 if (bounds && btarget)
5563 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5564 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5565 chkp_set_rtl_bounds (btarget, tmp);
5570 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5571 the same as that of TARGET, adjust the constant. This is needed, for
5572 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5573 only a word-sized value. */
5574 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5575 && TREE_CODE (exp) != ERROR_MARK
5576 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5577 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5578 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5580 /* If value was not generated in the target, store it there.
5581 Convert the value to TARGET's type first if necessary and emit the
5582 pending incrementations that have been queued when expanding EXP.
5583 Note that we cannot emit the whole queue blindly because this will
5584 effectively disable the POST_INC optimization later.
5586 If TEMP and TARGET compare equal according to rtx_equal_p, but
5587 one or both of them are volatile memory refs, we have to distinguish
5588 two cases:
5589 - expand_expr has used TARGET. In this case, we must not generate
5590 another copy. This can be detected by TARGET being equal according
5591 to == .
5592 - expand_expr has not used TARGET - that means that the source just
5593 happens to have the same RTX form. Since temp will have been created
5594 by expand_expr, it will compare unequal according to == .
5595 We must generate a copy in this case, to reach the correct number
5596 of volatile memory references. */
5598 if ((! rtx_equal_p (temp, target)
5599 || (temp != target && (side_effects_p (temp)
5600 || side_effects_p (target))))
5601 && TREE_CODE (exp) != ERROR_MARK
5602 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5603 but TARGET is not valid memory reference, TEMP will differ
5604 from TARGET although it is really the same location. */
5605 && !(alt_rtl
5606 && rtx_equal_p (alt_rtl, target)
5607 && !side_effects_p (alt_rtl)
5608 && !side_effects_p (target))
5609 /* If there's nothing to copy, don't bother. Don't call
5610 expr_size unless necessary, because some front-ends (C++)
5611 expr_size-hook must not be given objects that are not
5612 supposed to be bit-copied or bit-initialized. */
5613 && expr_size (exp) != const0_rtx)
5615 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5617 if (GET_MODE (target) == BLKmode)
5619 /* Handle calls that return BLKmode values in registers. */
5620 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5621 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5622 else
5623 store_bit_field (target,
5624 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5625 0, 0, 0, GET_MODE (temp), temp, reverse);
5627 else
5628 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5631 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5633 /* Handle copying a string constant into an array. The string
5634 constant may be shorter than the array. So copy just the string's
5635 actual length, and clear the rest. First get the size of the data
5636 type of the string, which is actually the size of the target. */
5637 rtx size = expr_size (exp);
5639 if (CONST_INT_P (size)
5640 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5641 emit_block_move (target, temp, size,
5642 (call_param_p
5643 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5644 else
5646 machine_mode pointer_mode
5647 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5648 machine_mode address_mode = get_address_mode (target);
5650 /* Compute the size of the data to copy from the string. */
5651 tree copy_size
5652 = size_binop_loc (loc, MIN_EXPR,
5653 make_tree (sizetype, size),
5654 size_int (TREE_STRING_LENGTH (exp)));
5655 rtx copy_size_rtx
5656 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5657 (call_param_p
5658 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5659 rtx_code_label *label = 0;
5661 /* Copy that much. */
5662 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5663 TYPE_UNSIGNED (sizetype));
5664 emit_block_move (target, temp, copy_size_rtx,
5665 (call_param_p
5666 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5668 /* Figure out how much is left in TARGET that we have to clear.
5669 Do all calculations in pointer_mode. */
5670 if (CONST_INT_P (copy_size_rtx))
5672 size = plus_constant (address_mode, size,
5673 -INTVAL (copy_size_rtx));
5674 target = adjust_address (target, BLKmode,
5675 INTVAL (copy_size_rtx));
5677 else
5679 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5680 copy_size_rtx, NULL_RTX, 0,
5681 OPTAB_LIB_WIDEN);
5683 if (GET_MODE (copy_size_rtx) != address_mode)
5684 copy_size_rtx = convert_to_mode (address_mode,
5685 copy_size_rtx,
5686 TYPE_UNSIGNED (sizetype));
5688 target = offset_address (target, copy_size_rtx,
5689 highest_pow2_factor (copy_size));
5690 label = gen_label_rtx ();
5691 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5692 GET_MODE (size), 0, label);
5695 if (size != const0_rtx)
5696 clear_storage (target, size, BLOCK_OP_NORMAL);
5698 if (label)
5699 emit_label (label);
5702 /* Handle calls that return values in multiple non-contiguous locations.
5703 The Irix 6 ABI has examples of this. */
5704 else if (GET_CODE (target) == PARALLEL)
5706 if (GET_CODE (temp) == PARALLEL)
5707 emit_group_move (target, temp);
5708 else
5709 emit_group_load (target, temp, TREE_TYPE (exp),
5710 int_size_in_bytes (TREE_TYPE (exp)));
5712 else if (GET_CODE (temp) == PARALLEL)
5713 emit_group_store (target, temp, TREE_TYPE (exp),
5714 int_size_in_bytes (TREE_TYPE (exp)));
5715 else if (GET_MODE (temp) == BLKmode)
5716 emit_block_move (target, temp, expr_size (exp),
5717 (call_param_p
5718 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5719 /* If we emit a nontemporal store, there is nothing else to do. */
5720 else if (nontemporal && emit_storent_insn (target, temp))
5722 else
5724 if (reverse)
5725 temp = flip_storage_order (GET_MODE (target), temp);
5726 temp = force_operand (temp, target);
5727 if (temp != target)
5728 emit_move_insn (target, temp);
5732 return NULL_RTX;
5735 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5737 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5738 bool reverse)
5740 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5741 reverse, NULL);
5744 /* Return true if field F of structure TYPE is a flexible array. */
5746 static bool
5747 flexible_array_member_p (const_tree f, const_tree type)
5749 const_tree tf;
5751 tf = TREE_TYPE (f);
5752 return (DECL_CHAIN (f) == NULL
5753 && TREE_CODE (tf) == ARRAY_TYPE
5754 && TYPE_DOMAIN (tf)
5755 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5756 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5757 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5758 && int_size_in_bytes (type) >= 0);
5761 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5762 must have in order for it to completely initialize a value of type TYPE.
5763 Return -1 if the number isn't known.
5765 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5767 static HOST_WIDE_INT
5768 count_type_elements (const_tree type, bool for_ctor_p)
5770 switch (TREE_CODE (type))
5772 case ARRAY_TYPE:
5774 tree nelts;
5776 nelts = array_type_nelts (type);
5777 if (nelts && tree_fits_uhwi_p (nelts))
5779 unsigned HOST_WIDE_INT n;
5781 n = tree_to_uhwi (nelts) + 1;
5782 if (n == 0 || for_ctor_p)
5783 return n;
5784 else
5785 return n * count_type_elements (TREE_TYPE (type), false);
5787 return for_ctor_p ? -1 : 1;
5790 case RECORD_TYPE:
5792 unsigned HOST_WIDE_INT n;
5793 tree f;
5795 n = 0;
5796 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5797 if (TREE_CODE (f) == FIELD_DECL)
5799 if (!for_ctor_p)
5800 n += count_type_elements (TREE_TYPE (f), false);
5801 else if (!flexible_array_member_p (f, type))
5802 /* Don't count flexible arrays, which are not supposed
5803 to be initialized. */
5804 n += 1;
5807 return n;
5810 case UNION_TYPE:
5811 case QUAL_UNION_TYPE:
5813 tree f;
5814 HOST_WIDE_INT n, m;
5816 gcc_assert (!for_ctor_p);
5817 /* Estimate the number of scalars in each field and pick the
5818 maximum. Other estimates would do instead; the idea is simply
5819 to make sure that the estimate is not sensitive to the ordering
5820 of the fields. */
5821 n = 1;
5822 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5823 if (TREE_CODE (f) == FIELD_DECL)
5825 m = count_type_elements (TREE_TYPE (f), false);
5826 /* If the field doesn't span the whole union, add an extra
5827 scalar for the rest. */
5828 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5829 TYPE_SIZE (type)) != 1)
5830 m++;
5831 if (n < m)
5832 n = m;
5834 return n;
5837 case COMPLEX_TYPE:
5838 return 2;
5840 case VECTOR_TYPE:
5841 return TYPE_VECTOR_SUBPARTS (type);
5843 case INTEGER_TYPE:
5844 case REAL_TYPE:
5845 case FIXED_POINT_TYPE:
5846 case ENUMERAL_TYPE:
5847 case BOOLEAN_TYPE:
5848 case POINTER_TYPE:
5849 case OFFSET_TYPE:
5850 case REFERENCE_TYPE:
5851 case NULLPTR_TYPE:
5852 return 1;
5854 case ERROR_MARK:
5855 return 0;
5857 case VOID_TYPE:
5858 case METHOD_TYPE:
5859 case FUNCTION_TYPE:
5860 case LANG_TYPE:
5861 default:
5862 gcc_unreachable ();
5866 /* Helper for categorize_ctor_elements. Identical interface. */
5868 static bool
5869 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5870 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5872 unsigned HOST_WIDE_INT idx;
5873 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5874 tree value, purpose, elt_type;
5876 /* Whether CTOR is a valid constant initializer, in accordance with what
5877 initializer_constant_valid_p does. If inferred from the constructor
5878 elements, true until proven otherwise. */
5879 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5880 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5882 nz_elts = 0;
5883 init_elts = 0;
5884 num_fields = 0;
5885 elt_type = NULL_TREE;
5887 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5889 HOST_WIDE_INT mult = 1;
5891 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5893 tree lo_index = TREE_OPERAND (purpose, 0);
5894 tree hi_index = TREE_OPERAND (purpose, 1);
5896 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5897 mult = (tree_to_uhwi (hi_index)
5898 - tree_to_uhwi (lo_index) + 1);
5900 num_fields += mult;
5901 elt_type = TREE_TYPE (value);
5903 switch (TREE_CODE (value))
5905 case CONSTRUCTOR:
5907 HOST_WIDE_INT nz = 0, ic = 0;
5909 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5910 p_complete);
5912 nz_elts += mult * nz;
5913 init_elts += mult * ic;
5915 if (const_from_elts_p && const_p)
5916 const_p = const_elt_p;
5918 break;
5920 case INTEGER_CST:
5921 case REAL_CST:
5922 case FIXED_CST:
5923 if (!initializer_zerop (value))
5924 nz_elts += mult;
5925 init_elts += mult;
5926 break;
5928 case STRING_CST:
5929 nz_elts += mult * TREE_STRING_LENGTH (value);
5930 init_elts += mult * TREE_STRING_LENGTH (value);
5931 break;
5933 case COMPLEX_CST:
5934 if (!initializer_zerop (TREE_REALPART (value)))
5935 nz_elts += mult;
5936 if (!initializer_zerop (TREE_IMAGPART (value)))
5937 nz_elts += mult;
5938 init_elts += mult;
5939 break;
5941 case VECTOR_CST:
5943 unsigned i;
5944 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5946 tree v = VECTOR_CST_ELT (value, i);
5947 if (!initializer_zerop (v))
5948 nz_elts += mult;
5949 init_elts += mult;
5952 break;
5954 default:
5956 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5957 nz_elts += mult * tc;
5958 init_elts += mult * tc;
5960 if (const_from_elts_p && const_p)
5961 const_p
5962 = initializer_constant_valid_p (value,
5963 elt_type,
5964 TYPE_REVERSE_STORAGE_ORDER
5965 (TREE_TYPE (ctor)))
5966 != NULL_TREE;
5968 break;
5972 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5973 num_fields, elt_type))
5974 *p_complete = false;
5976 *p_nz_elts += nz_elts;
5977 *p_init_elts += init_elts;
5979 return const_p;
5982 /* Examine CTOR to discover:
5983 * how many scalar fields are set to nonzero values,
5984 and place it in *P_NZ_ELTS;
5985 * how many scalar fields in total are in CTOR,
5986 and place it in *P_ELT_COUNT.
5987 * whether the constructor is complete -- in the sense that every
5988 meaningful byte is explicitly given a value --
5989 and place it in *P_COMPLETE.
5991 Return whether or not CTOR is a valid static constant initializer, the same
5992 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5994 bool
5995 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5996 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5998 *p_nz_elts = 0;
5999 *p_init_elts = 0;
6000 *p_complete = true;
6002 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6005 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6006 of which had type LAST_TYPE. Each element was itself a complete
6007 initializer, in the sense that every meaningful byte was explicitly
6008 given a value. Return true if the same is true for the constructor
6009 as a whole. */
6011 bool
6012 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6013 const_tree last_type)
6015 if (TREE_CODE (type) == UNION_TYPE
6016 || TREE_CODE (type) == QUAL_UNION_TYPE)
6018 if (num_elts == 0)
6019 return false;
6021 gcc_assert (num_elts == 1 && last_type);
6023 /* ??? We could look at each element of the union, and find the
6024 largest element. Which would avoid comparing the size of the
6025 initialized element against any tail padding in the union.
6026 Doesn't seem worth the effort... */
6027 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6030 return count_type_elements (type, true) == num_elts;
6033 /* Return 1 if EXP contains mostly (3/4) zeros. */
6035 static int
6036 mostly_zeros_p (const_tree exp)
6038 if (TREE_CODE (exp) == CONSTRUCTOR)
6040 HOST_WIDE_INT nz_elts, init_elts;
6041 bool complete_p;
6043 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6044 return !complete_p || nz_elts < init_elts / 4;
6047 return initializer_zerop (exp);
6050 /* Return 1 if EXP contains all zeros. */
6052 static int
6053 all_zeros_p (const_tree exp)
6055 if (TREE_CODE (exp) == CONSTRUCTOR)
6057 HOST_WIDE_INT nz_elts, init_elts;
6058 bool complete_p;
6060 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6061 return nz_elts == 0;
6064 return initializer_zerop (exp);
6067 /* Helper function for store_constructor.
6068 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6069 CLEARED is as for store_constructor.
6070 ALIAS_SET is the alias set to use for any stores.
6071 If REVERSE is true, the store is to be done in reverse order.
6073 This provides a recursive shortcut back to store_constructor when it isn't
6074 necessary to go through store_field. This is so that we can pass through
6075 the cleared field to let store_constructor know that we may not have to
6076 clear a substructure if the outer structure has already been cleared. */
6078 static void
6079 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6080 HOST_WIDE_INT bitpos,
6081 unsigned HOST_WIDE_INT bitregion_start,
6082 unsigned HOST_WIDE_INT bitregion_end,
6083 machine_mode mode,
6084 tree exp, int cleared,
6085 alias_set_type alias_set, bool reverse)
6087 if (TREE_CODE (exp) == CONSTRUCTOR
6088 /* We can only call store_constructor recursively if the size and
6089 bit position are on a byte boundary. */
6090 && bitpos % BITS_PER_UNIT == 0
6091 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6092 /* If we have a nonzero bitpos for a register target, then we just
6093 let store_field do the bitfield handling. This is unlikely to
6094 generate unnecessary clear instructions anyways. */
6095 && (bitpos == 0 || MEM_P (target)))
6097 if (MEM_P (target))
6098 target
6099 = adjust_address (target,
6100 GET_MODE (target) == BLKmode
6101 || 0 != (bitpos
6102 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6103 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6106 /* Update the alias set, if required. */
6107 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6108 && MEM_ALIAS_SET (target) != 0)
6110 target = copy_rtx (target);
6111 set_mem_alias_set (target, alias_set);
6114 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6115 reverse);
6117 else
6118 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6119 exp, alias_set, false, reverse);
6123 /* Returns the number of FIELD_DECLs in TYPE. */
6125 static int
6126 fields_length (const_tree type)
6128 tree t = TYPE_FIELDS (type);
6129 int count = 0;
6131 for (; t; t = DECL_CHAIN (t))
6132 if (TREE_CODE (t) == FIELD_DECL)
6133 ++count;
6135 return count;
6139 /* Store the value of constructor EXP into the rtx TARGET.
6140 TARGET is either a REG or a MEM; we know it cannot conflict, since
6141 safe_from_p has been called.
6142 CLEARED is true if TARGET is known to have been zero'd.
6143 SIZE is the number of bytes of TARGET we are allowed to modify: this
6144 may not be the same as the size of EXP if we are assigning to a field
6145 which has been packed to exclude padding bits.
6146 If REVERSE is true, the store is to be done in reverse order. */
6148 static void
6149 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6150 bool reverse)
6152 tree type = TREE_TYPE (exp);
6153 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6154 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6156 switch (TREE_CODE (type))
6158 case RECORD_TYPE:
6159 case UNION_TYPE:
6160 case QUAL_UNION_TYPE:
6162 unsigned HOST_WIDE_INT idx;
6163 tree field, value;
6165 /* The storage order is specified for every aggregate type. */
6166 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6168 /* If size is zero or the target is already cleared, do nothing. */
6169 if (size == 0 || cleared)
6170 cleared = 1;
6171 /* We either clear the aggregate or indicate the value is dead. */
6172 else if ((TREE_CODE (type) == UNION_TYPE
6173 || TREE_CODE (type) == QUAL_UNION_TYPE)
6174 && ! CONSTRUCTOR_ELTS (exp))
6175 /* If the constructor is empty, clear the union. */
6177 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6178 cleared = 1;
6181 /* If we are building a static constructor into a register,
6182 set the initial value as zero so we can fold the value into
6183 a constant. But if more than one register is involved,
6184 this probably loses. */
6185 else if (REG_P (target) && TREE_STATIC (exp)
6186 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6188 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6189 cleared = 1;
6192 /* If the constructor has fewer fields than the structure or
6193 if we are initializing the structure to mostly zeros, clear
6194 the whole structure first. Don't do this if TARGET is a
6195 register whose mode size isn't equal to SIZE since
6196 clear_storage can't handle this case. */
6197 else if (size > 0
6198 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6199 || mostly_zeros_p (exp))
6200 && (!REG_P (target)
6201 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6202 == size)))
6204 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6205 cleared = 1;
6208 if (REG_P (target) && !cleared)
6209 emit_clobber (target);
6211 /* Store each element of the constructor into the
6212 corresponding field of TARGET. */
6213 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6215 machine_mode mode;
6216 HOST_WIDE_INT bitsize;
6217 HOST_WIDE_INT bitpos = 0;
6218 tree offset;
6219 rtx to_rtx = target;
6221 /* Just ignore missing fields. We cleared the whole
6222 structure, above, if any fields are missing. */
6223 if (field == 0)
6224 continue;
6226 if (cleared && initializer_zerop (value))
6227 continue;
6229 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6230 bitsize = tree_to_uhwi (DECL_SIZE (field));
6231 else
6232 gcc_unreachable ();
6234 mode = DECL_MODE (field);
6235 if (DECL_BIT_FIELD (field))
6236 mode = VOIDmode;
6238 offset = DECL_FIELD_OFFSET (field);
6239 if (tree_fits_shwi_p (offset)
6240 && tree_fits_shwi_p (bit_position (field)))
6242 bitpos = int_bit_position (field);
6243 offset = NULL_TREE;
6245 else
6246 gcc_unreachable ();
6248 /* If this initializes a field that is smaller than a
6249 word, at the start of a word, try to widen it to a full
6250 word. This special case allows us to output C++ member
6251 function initializations in a form that the optimizers
6252 can understand. */
6253 if (WORD_REGISTER_OPERATIONS
6254 && REG_P (target)
6255 && bitsize < BITS_PER_WORD
6256 && bitpos % BITS_PER_WORD == 0
6257 && GET_MODE_CLASS (mode) == MODE_INT
6258 && TREE_CODE (value) == INTEGER_CST
6259 && exp_size >= 0
6260 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6262 tree type = TREE_TYPE (value);
6264 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6266 type = lang_hooks.types.type_for_mode
6267 (word_mode, TYPE_UNSIGNED (type));
6268 value = fold_convert (type, value);
6269 /* Make sure the bits beyond the original bitsize are zero
6270 so that we can correctly avoid extra zeroing stores in
6271 later constructor elements. */
6272 tree bitsize_mask
6273 = wide_int_to_tree (type, wi::mask (bitsize, false,
6274 BITS_PER_WORD));
6275 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6278 if (BYTES_BIG_ENDIAN)
6279 value
6280 = fold_build2 (LSHIFT_EXPR, type, value,
6281 build_int_cst (type,
6282 BITS_PER_WORD - bitsize));
6283 bitsize = BITS_PER_WORD;
6284 mode = word_mode;
6287 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6288 && DECL_NONADDRESSABLE_P (field))
6290 to_rtx = copy_rtx (to_rtx);
6291 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6294 store_constructor_field (to_rtx, bitsize, bitpos,
6295 0, bitregion_end, mode,
6296 value, cleared,
6297 get_alias_set (TREE_TYPE (field)),
6298 reverse);
6300 break;
6302 case ARRAY_TYPE:
6304 tree value, index;
6305 unsigned HOST_WIDE_INT i;
6306 int need_to_clear;
6307 tree domain;
6308 tree elttype = TREE_TYPE (type);
6309 int const_bounds_p;
6310 HOST_WIDE_INT minelt = 0;
6311 HOST_WIDE_INT maxelt = 0;
6313 /* The storage order is specified for every aggregate type. */
6314 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6316 domain = TYPE_DOMAIN (type);
6317 const_bounds_p = (TYPE_MIN_VALUE (domain)
6318 && TYPE_MAX_VALUE (domain)
6319 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6320 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6322 /* If we have constant bounds for the range of the type, get them. */
6323 if (const_bounds_p)
6325 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6326 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6329 /* If the constructor has fewer elements than the array, clear
6330 the whole array first. Similarly if this is static
6331 constructor of a non-BLKmode object. */
6332 if (cleared)
6333 need_to_clear = 0;
6334 else if (REG_P (target) && TREE_STATIC (exp))
6335 need_to_clear = 1;
6336 else
6338 unsigned HOST_WIDE_INT idx;
6339 tree index, value;
6340 HOST_WIDE_INT count = 0, zero_count = 0;
6341 need_to_clear = ! const_bounds_p;
6343 /* This loop is a more accurate version of the loop in
6344 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6345 is also needed to check for missing elements. */
6346 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6348 HOST_WIDE_INT this_node_count;
6350 if (need_to_clear)
6351 break;
6353 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6355 tree lo_index = TREE_OPERAND (index, 0);
6356 tree hi_index = TREE_OPERAND (index, 1);
6358 if (! tree_fits_uhwi_p (lo_index)
6359 || ! tree_fits_uhwi_p (hi_index))
6361 need_to_clear = 1;
6362 break;
6365 this_node_count = (tree_to_uhwi (hi_index)
6366 - tree_to_uhwi (lo_index) + 1);
6368 else
6369 this_node_count = 1;
6371 count += this_node_count;
6372 if (mostly_zeros_p (value))
6373 zero_count += this_node_count;
6376 /* Clear the entire array first if there are any missing
6377 elements, or if the incidence of zero elements is >=
6378 75%. */
6379 if (! need_to_clear
6380 && (count < maxelt - minelt + 1
6381 || 4 * zero_count >= 3 * count))
6382 need_to_clear = 1;
6385 if (need_to_clear && size > 0)
6387 if (REG_P (target))
6388 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6389 else
6390 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6391 cleared = 1;
6394 if (!cleared && REG_P (target))
6395 /* Inform later passes that the old value is dead. */
6396 emit_clobber (target);
6398 /* Store each element of the constructor into the
6399 corresponding element of TARGET, determined by counting the
6400 elements. */
6401 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6403 machine_mode mode;
6404 HOST_WIDE_INT bitsize;
6405 HOST_WIDE_INT bitpos;
6406 rtx xtarget = target;
6408 if (cleared && initializer_zerop (value))
6409 continue;
6411 mode = TYPE_MODE (elttype);
6412 if (mode == BLKmode)
6413 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6414 ? tree_to_uhwi (TYPE_SIZE (elttype))
6415 : -1);
6416 else
6417 bitsize = GET_MODE_BITSIZE (mode);
6419 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6421 tree lo_index = TREE_OPERAND (index, 0);
6422 tree hi_index = TREE_OPERAND (index, 1);
6423 rtx index_r, pos_rtx;
6424 HOST_WIDE_INT lo, hi, count;
6425 tree position;
6427 /* If the range is constant and "small", unroll the loop. */
6428 if (const_bounds_p
6429 && tree_fits_shwi_p (lo_index)
6430 && tree_fits_shwi_p (hi_index)
6431 && (lo = tree_to_shwi (lo_index),
6432 hi = tree_to_shwi (hi_index),
6433 count = hi - lo + 1,
6434 (!MEM_P (target)
6435 || count <= 2
6436 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6437 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6438 <= 40 * 8)))))
6440 lo -= minelt; hi -= minelt;
6441 for (; lo <= hi; lo++)
6443 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6445 if (MEM_P (target)
6446 && !MEM_KEEP_ALIAS_SET_P (target)
6447 && TREE_CODE (type) == ARRAY_TYPE
6448 && TYPE_NONALIASED_COMPONENT (type))
6450 target = copy_rtx (target);
6451 MEM_KEEP_ALIAS_SET_P (target) = 1;
6454 store_constructor_field
6455 (target, bitsize, bitpos, 0, bitregion_end,
6456 mode, value, cleared,
6457 get_alias_set (elttype), reverse);
6460 else
6462 rtx_code_label *loop_start = gen_label_rtx ();
6463 rtx_code_label *loop_end = gen_label_rtx ();
6464 tree exit_cond;
6466 expand_normal (hi_index);
6468 index = build_decl (EXPR_LOCATION (exp),
6469 VAR_DECL, NULL_TREE, domain);
6470 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6471 SET_DECL_RTL (index, index_r);
6472 store_expr (lo_index, index_r, 0, false, reverse);
6474 /* Build the head of the loop. */
6475 do_pending_stack_adjust ();
6476 emit_label (loop_start);
6478 /* Assign value to element index. */
6479 position =
6480 fold_convert (ssizetype,
6481 fold_build2 (MINUS_EXPR,
6482 TREE_TYPE (index),
6483 index,
6484 TYPE_MIN_VALUE (domain)));
6486 position =
6487 size_binop (MULT_EXPR, position,
6488 fold_convert (ssizetype,
6489 TYPE_SIZE_UNIT (elttype)));
6491 pos_rtx = expand_normal (position);
6492 xtarget = offset_address (target, pos_rtx,
6493 highest_pow2_factor (position));
6494 xtarget = adjust_address (xtarget, mode, 0);
6495 if (TREE_CODE (value) == CONSTRUCTOR)
6496 store_constructor (value, xtarget, cleared,
6497 bitsize / BITS_PER_UNIT, reverse);
6498 else
6499 store_expr (value, xtarget, 0, false, reverse);
6501 /* Generate a conditional jump to exit the loop. */
6502 exit_cond = build2 (LT_EXPR, integer_type_node,
6503 index, hi_index);
6504 jumpif (exit_cond, loop_end,
6505 profile_probability::uninitialized ());
6507 /* Update the loop counter, and jump to the head of
6508 the loop. */
6509 expand_assignment (index,
6510 build2 (PLUS_EXPR, TREE_TYPE (index),
6511 index, integer_one_node),
6512 false);
6514 emit_jump (loop_start);
6516 /* Build the end of the loop. */
6517 emit_label (loop_end);
6520 else if ((index != 0 && ! tree_fits_shwi_p (index))
6521 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6523 tree position;
6525 if (index == 0)
6526 index = ssize_int (1);
6528 if (minelt)
6529 index = fold_convert (ssizetype,
6530 fold_build2 (MINUS_EXPR,
6531 TREE_TYPE (index),
6532 index,
6533 TYPE_MIN_VALUE (domain)));
6535 position =
6536 size_binop (MULT_EXPR, index,
6537 fold_convert (ssizetype,
6538 TYPE_SIZE_UNIT (elttype)));
6539 xtarget = offset_address (target,
6540 expand_normal (position),
6541 highest_pow2_factor (position));
6542 xtarget = adjust_address (xtarget, mode, 0);
6543 store_expr (value, xtarget, 0, false, reverse);
6545 else
6547 if (index != 0)
6548 bitpos = ((tree_to_shwi (index) - minelt)
6549 * tree_to_uhwi (TYPE_SIZE (elttype)));
6550 else
6551 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6553 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6554 && TREE_CODE (type) == ARRAY_TYPE
6555 && TYPE_NONALIASED_COMPONENT (type))
6557 target = copy_rtx (target);
6558 MEM_KEEP_ALIAS_SET_P (target) = 1;
6560 store_constructor_field (target, bitsize, bitpos, 0,
6561 bitregion_end, mode, value,
6562 cleared, get_alias_set (elttype),
6563 reverse);
6566 break;
6569 case VECTOR_TYPE:
6571 unsigned HOST_WIDE_INT idx;
6572 constructor_elt *ce;
6573 int i;
6574 int need_to_clear;
6575 int icode = CODE_FOR_nothing;
6576 tree elttype = TREE_TYPE (type);
6577 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6578 machine_mode eltmode = TYPE_MODE (elttype);
6579 HOST_WIDE_INT bitsize;
6580 HOST_WIDE_INT bitpos;
6581 rtvec vector = NULL;
6582 unsigned n_elts;
6583 alias_set_type alias;
6584 bool vec_vec_init_p = false;
6586 gcc_assert (eltmode != BLKmode);
6588 n_elts = TYPE_VECTOR_SUBPARTS (type);
6589 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6591 machine_mode mode = GET_MODE (target);
6592 machine_mode emode = eltmode;
6594 if (CONSTRUCTOR_NELTS (exp)
6595 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6596 == VECTOR_TYPE))
6598 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6599 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6600 == n_elts);
6601 emode = TYPE_MODE (etype);
6603 icode = (int) convert_optab_handler (vec_init_optab, mode, emode);
6604 if (icode != CODE_FOR_nothing)
6606 unsigned int i, n = n_elts;
6608 if (emode != eltmode)
6610 n = CONSTRUCTOR_NELTS (exp);
6611 vec_vec_init_p = true;
6613 vector = rtvec_alloc (n);
6614 for (i = 0; i < n; i++)
6615 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6619 /* If the constructor has fewer elements than the vector,
6620 clear the whole array first. Similarly if this is static
6621 constructor of a non-BLKmode object. */
6622 if (cleared)
6623 need_to_clear = 0;
6624 else if (REG_P (target) && TREE_STATIC (exp))
6625 need_to_clear = 1;
6626 else
6628 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6629 tree value;
6631 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6633 tree sz = TYPE_SIZE (TREE_TYPE (value));
6634 int n_elts_here
6635 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6636 TYPE_SIZE (elttype)));
6638 count += n_elts_here;
6639 if (mostly_zeros_p (value))
6640 zero_count += n_elts_here;
6643 /* Clear the entire vector first if there are any missing elements,
6644 or if the incidence of zero elements is >= 75%. */
6645 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6648 if (need_to_clear && size > 0 && !vector)
6650 if (REG_P (target))
6651 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6652 else
6653 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6654 cleared = 1;
6657 /* Inform later passes that the old value is dead. */
6658 if (!cleared && !vector && REG_P (target))
6659 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6661 if (MEM_P (target))
6662 alias = MEM_ALIAS_SET (target);
6663 else
6664 alias = get_alias_set (elttype);
6666 /* Store each element of the constructor into the corresponding
6667 element of TARGET, determined by counting the elements. */
6668 for (idx = 0, i = 0;
6669 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6670 idx++, i += bitsize / elt_size)
6672 HOST_WIDE_INT eltpos;
6673 tree value = ce->value;
6675 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6676 if (cleared && initializer_zerop (value))
6677 continue;
6679 if (ce->index)
6680 eltpos = tree_to_uhwi (ce->index);
6681 else
6682 eltpos = i;
6684 if (vector)
6686 if (vec_vec_init_p)
6688 gcc_assert (ce->index == NULL_TREE);
6689 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6690 eltpos = idx;
6692 else
6693 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6694 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6696 else
6698 machine_mode value_mode
6699 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6700 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6701 bitpos = eltpos * elt_size;
6702 store_constructor_field (target, bitsize, bitpos, 0,
6703 bitregion_end, value_mode,
6704 value, cleared, alias, reverse);
6708 if (vector)
6709 emit_insn (GEN_FCN (icode) (target,
6710 gen_rtx_PARALLEL (GET_MODE (target),
6711 vector)));
6712 break;
6715 default:
6716 gcc_unreachable ();
6720 /* Store the value of EXP (an expression tree)
6721 into a subfield of TARGET which has mode MODE and occupies
6722 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6723 If MODE is VOIDmode, it means that we are storing into a bit-field.
6725 BITREGION_START is bitpos of the first bitfield in this region.
6726 BITREGION_END is the bitpos of the ending bitfield in this region.
6727 These two fields are 0, if the C++ memory model does not apply,
6728 or we are not interested in keeping track of bitfield regions.
6730 Always return const0_rtx unless we have something particular to
6731 return.
6733 ALIAS_SET is the alias set for the destination. This value will
6734 (in general) be different from that for TARGET, since TARGET is a
6735 reference to the containing structure.
6737 If NONTEMPORAL is true, try generating a nontemporal store.
6739 If REVERSE is true, the store is to be done in reverse order. */
6741 static rtx
6742 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6743 unsigned HOST_WIDE_INT bitregion_start,
6744 unsigned HOST_WIDE_INT bitregion_end,
6745 machine_mode mode, tree exp,
6746 alias_set_type alias_set, bool nontemporal, bool reverse)
6748 if (TREE_CODE (exp) == ERROR_MARK)
6749 return const0_rtx;
6751 /* If we have nothing to store, do nothing unless the expression has
6752 side-effects. Don't do that for zero sized addressable lhs of
6753 calls. */
6754 if (bitsize == 0
6755 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6756 || TREE_CODE (exp) != CALL_EXPR))
6757 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6759 if (GET_CODE (target) == CONCAT)
6761 /* We're storing into a struct containing a single __complex. */
6763 gcc_assert (!bitpos);
6764 return store_expr (exp, target, 0, nontemporal, reverse);
6767 /* If the structure is in a register or if the component
6768 is a bit field, we cannot use addressing to access it.
6769 Use bit-field techniques or SUBREG to store in it. */
6771 if (mode == VOIDmode
6772 || (mode != BLKmode && ! direct_store[(int) mode]
6773 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6774 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6775 || REG_P (target)
6776 || GET_CODE (target) == SUBREG
6777 /* If the field isn't aligned enough to store as an ordinary memref,
6778 store it as a bit field. */
6779 || (mode != BLKmode
6780 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6781 || bitpos % GET_MODE_ALIGNMENT (mode))
6782 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6783 || (bitpos % BITS_PER_UNIT != 0)))
6784 || (bitsize >= 0 && mode != BLKmode
6785 && GET_MODE_BITSIZE (mode) > bitsize)
6786 /* If the RHS and field are a constant size and the size of the
6787 RHS isn't the same size as the bitfield, we must use bitfield
6788 operations. */
6789 || (bitsize >= 0
6790 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6791 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6792 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6793 we will handle specially below. */
6794 && !(TREE_CODE (exp) == CONSTRUCTOR
6795 && bitsize % BITS_PER_UNIT == 0)
6796 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6797 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6798 includes some extra padding. store_expr / expand_expr will in
6799 that case call get_inner_reference that will have the bitsize
6800 we check here and thus the block move will not clobber the
6801 padding that shouldn't be clobbered. In the future we could
6802 replace the TREE_ADDRESSABLE check with a check that
6803 get_base_address needs to live in memory. */
6804 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6805 || TREE_CODE (exp) != COMPONENT_REF
6806 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6807 || (bitsize % BITS_PER_UNIT != 0)
6808 || (bitpos % BITS_PER_UNIT != 0)
6809 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6810 != 0)))
6811 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6812 decl we must use bitfield operations. */
6813 || (bitsize >= 0
6814 && TREE_CODE (exp) == MEM_REF
6815 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6816 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6817 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6818 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6820 rtx temp;
6821 gimple *nop_def;
6823 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6824 implies a mask operation. If the precision is the same size as
6825 the field we're storing into, that mask is redundant. This is
6826 particularly common with bit field assignments generated by the
6827 C front end. */
6828 nop_def = get_def_for_expr (exp, NOP_EXPR);
6829 if (nop_def)
6831 tree type = TREE_TYPE (exp);
6832 if (INTEGRAL_TYPE_P (type)
6833 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6834 && bitsize == TYPE_PRECISION (type))
6836 tree op = gimple_assign_rhs1 (nop_def);
6837 type = TREE_TYPE (op);
6838 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6839 exp = op;
6843 temp = expand_normal (exp);
6845 /* Handle calls that return values in multiple non-contiguous locations.
6846 The Irix 6 ABI has examples of this. */
6847 if (GET_CODE (temp) == PARALLEL)
6849 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6850 scalar_int_mode temp_mode
6851 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6852 rtx temp_target = gen_reg_rtx (temp_mode);
6853 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6854 temp = temp_target;
6857 /* Handle calls that return BLKmode values in registers. */
6858 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6860 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6861 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6862 temp = temp_target;
6865 /* If the value has aggregate type and an integral mode then, if BITSIZE
6866 is narrower than this mode and this is for big-endian data, we first
6867 need to put the value into the low-order bits for store_bit_field,
6868 except when MODE is BLKmode and BITSIZE larger than the word size
6869 (see the handling of fields larger than a word in store_bit_field).
6870 Moreover, the field may be not aligned on a byte boundary; in this
6871 case, if it has reverse storage order, it needs to be accessed as a
6872 scalar field with reverse storage order and we must first put the
6873 value into target order. */
6874 scalar_int_mode temp_mode;
6875 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6876 && is_int_mode (GET_MODE (temp), &temp_mode))
6878 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6880 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6882 if (reverse)
6883 temp = flip_storage_order (temp_mode, temp);
6885 if (bitsize < size
6886 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6887 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6888 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6889 size - bitsize, NULL_RTX, 1);
6892 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6893 if (mode != VOIDmode && mode != BLKmode
6894 && mode != TYPE_MODE (TREE_TYPE (exp)))
6895 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6897 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6898 and BITPOS must be aligned on a byte boundary. If so, we simply do
6899 a block copy. Likewise for a BLKmode-like TARGET. */
6900 if (GET_MODE (temp) == BLKmode
6901 && (GET_MODE (target) == BLKmode
6902 || (MEM_P (target)
6903 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6904 && (bitpos % BITS_PER_UNIT) == 0
6905 && (bitsize % BITS_PER_UNIT) == 0)))
6907 gcc_assert (MEM_P (target) && MEM_P (temp)
6908 && (bitpos % BITS_PER_UNIT) == 0);
6910 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6911 emit_block_move (target, temp,
6912 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6913 / BITS_PER_UNIT),
6914 BLOCK_OP_NORMAL);
6916 return const0_rtx;
6919 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6920 word size, we need to load the value (see again store_bit_field). */
6921 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6923 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
6924 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6925 temp_mode, false, NULL);
6928 /* Store the value in the bitfield. */
6929 store_bit_field (target, bitsize, bitpos,
6930 bitregion_start, bitregion_end,
6931 mode, temp, reverse);
6933 return const0_rtx;
6935 else
6937 /* Now build a reference to just the desired component. */
6938 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6940 if (to_rtx == target)
6941 to_rtx = copy_rtx (to_rtx);
6943 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6944 set_mem_alias_set (to_rtx, alias_set);
6946 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6947 into a target smaller than its type; handle that case now. */
6948 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6950 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6951 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6952 return to_rtx;
6955 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6959 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6960 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6961 codes and find the ultimate containing object, which we return.
6963 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6964 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6965 storage order of the field.
6966 If the position of the field is variable, we store a tree
6967 giving the variable offset (in units) in *POFFSET.
6968 This offset is in addition to the bit position.
6969 If the position is not variable, we store 0 in *POFFSET.
6971 If any of the extraction expressions is volatile,
6972 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6974 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6975 Otherwise, it is a mode that can be used to access the field.
6977 If the field describes a variable-sized object, *PMODE is set to
6978 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6979 this case, but the address of the object can be found. */
6981 tree
6982 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6983 HOST_WIDE_INT *pbitpos, tree *poffset,
6984 machine_mode *pmode, int *punsignedp,
6985 int *preversep, int *pvolatilep)
6987 tree size_tree = 0;
6988 machine_mode mode = VOIDmode;
6989 bool blkmode_bitfield = false;
6990 tree offset = size_zero_node;
6991 offset_int bit_offset = 0;
6993 /* First get the mode, signedness, storage order and size. We do this from
6994 just the outermost expression. */
6995 *pbitsize = -1;
6996 if (TREE_CODE (exp) == COMPONENT_REF)
6998 tree field = TREE_OPERAND (exp, 1);
6999 size_tree = DECL_SIZE (field);
7000 if (flag_strict_volatile_bitfields > 0
7001 && TREE_THIS_VOLATILE (exp)
7002 && DECL_BIT_FIELD_TYPE (field)
7003 && DECL_MODE (field) != BLKmode)
7004 /* Volatile bitfields should be accessed in the mode of the
7005 field's type, not the mode computed based on the bit
7006 size. */
7007 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7008 else if (!DECL_BIT_FIELD (field))
7009 mode = DECL_MODE (field);
7010 else if (DECL_MODE (field) == BLKmode)
7011 blkmode_bitfield = true;
7013 *punsignedp = DECL_UNSIGNED (field);
7015 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7017 size_tree = TREE_OPERAND (exp, 1);
7018 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7019 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7021 /* For vector types, with the correct size of access, use the mode of
7022 inner type. */
7023 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7024 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7025 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7026 mode = TYPE_MODE (TREE_TYPE (exp));
7028 else
7030 mode = TYPE_MODE (TREE_TYPE (exp));
7031 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7033 if (mode == BLKmode)
7034 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7035 else
7036 *pbitsize = GET_MODE_BITSIZE (mode);
7039 if (size_tree != 0)
7041 if (! tree_fits_uhwi_p (size_tree))
7042 mode = BLKmode, *pbitsize = -1;
7043 else
7044 *pbitsize = tree_to_uhwi (size_tree);
7047 *preversep = reverse_storage_order_for_component_p (exp);
7049 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7050 and find the ultimate containing object. */
7051 while (1)
7053 switch (TREE_CODE (exp))
7055 case BIT_FIELD_REF:
7056 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7057 break;
7059 case COMPONENT_REF:
7061 tree field = TREE_OPERAND (exp, 1);
7062 tree this_offset = component_ref_field_offset (exp);
7064 /* If this field hasn't been filled in yet, don't go past it.
7065 This should only happen when folding expressions made during
7066 type construction. */
7067 if (this_offset == 0)
7068 break;
7070 offset = size_binop (PLUS_EXPR, offset, this_offset);
7071 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7073 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7075 break;
7077 case ARRAY_REF:
7078 case ARRAY_RANGE_REF:
7080 tree index = TREE_OPERAND (exp, 1);
7081 tree low_bound = array_ref_low_bound (exp);
7082 tree unit_size = array_ref_element_size (exp);
7084 /* We assume all arrays have sizes that are a multiple of a byte.
7085 First subtract the lower bound, if any, in the type of the
7086 index, then convert to sizetype and multiply by the size of
7087 the array element. */
7088 if (! integer_zerop (low_bound))
7089 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7090 index, low_bound);
7092 offset = size_binop (PLUS_EXPR, offset,
7093 size_binop (MULT_EXPR,
7094 fold_convert (sizetype, index),
7095 unit_size));
7097 break;
7099 case REALPART_EXPR:
7100 break;
7102 case IMAGPART_EXPR:
7103 bit_offset += *pbitsize;
7104 break;
7106 case VIEW_CONVERT_EXPR:
7107 break;
7109 case MEM_REF:
7110 /* Hand back the decl for MEM[&decl, off]. */
7111 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7113 tree off = TREE_OPERAND (exp, 1);
7114 if (!integer_zerop (off))
7116 offset_int boff, coff = mem_ref_offset (exp);
7117 boff = coff << LOG2_BITS_PER_UNIT;
7118 bit_offset += boff;
7120 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7122 goto done;
7124 default:
7125 goto done;
7128 /* If any reference in the chain is volatile, the effect is volatile. */
7129 if (TREE_THIS_VOLATILE (exp))
7130 *pvolatilep = 1;
7132 exp = TREE_OPERAND (exp, 0);
7134 done:
7136 /* If OFFSET is constant, see if we can return the whole thing as a
7137 constant bit position. Make sure to handle overflow during
7138 this conversion. */
7139 if (TREE_CODE (offset) == INTEGER_CST)
7141 offset_int tem = wi::sext (wi::to_offset (offset),
7142 TYPE_PRECISION (sizetype));
7143 tem <<= LOG2_BITS_PER_UNIT;
7144 tem += bit_offset;
7145 if (wi::fits_shwi_p (tem))
7147 *pbitpos = tem.to_shwi ();
7148 *poffset = offset = NULL_TREE;
7152 /* Otherwise, split it up. */
7153 if (offset)
7155 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7156 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7158 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7159 offset_int tem = wi::bit_and_not (bit_offset, mask);
7160 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7161 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7162 bit_offset -= tem;
7163 tem >>= LOG2_BITS_PER_UNIT;
7164 offset = size_binop (PLUS_EXPR, offset,
7165 wide_int_to_tree (sizetype, tem));
7168 *pbitpos = bit_offset.to_shwi ();
7169 *poffset = offset;
7172 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7173 if (mode == VOIDmode
7174 && blkmode_bitfield
7175 && (*pbitpos % BITS_PER_UNIT) == 0
7176 && (*pbitsize % BITS_PER_UNIT) == 0)
7177 *pmode = BLKmode;
7178 else
7179 *pmode = mode;
7181 return exp;
7184 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7186 static unsigned HOST_WIDE_INT
7187 target_align (const_tree target)
7189 /* We might have a chain of nested references with intermediate misaligning
7190 bitfields components, so need to recurse to find out. */
7192 unsigned HOST_WIDE_INT this_align, outer_align;
7194 switch (TREE_CODE (target))
7196 case BIT_FIELD_REF:
7197 return 1;
7199 case COMPONENT_REF:
7200 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7201 outer_align = target_align (TREE_OPERAND (target, 0));
7202 return MIN (this_align, outer_align);
7204 case ARRAY_REF:
7205 case ARRAY_RANGE_REF:
7206 this_align = TYPE_ALIGN (TREE_TYPE (target));
7207 outer_align = target_align (TREE_OPERAND (target, 0));
7208 return MIN (this_align, outer_align);
7210 CASE_CONVERT:
7211 case NON_LVALUE_EXPR:
7212 case VIEW_CONVERT_EXPR:
7213 this_align = TYPE_ALIGN (TREE_TYPE (target));
7214 outer_align = target_align (TREE_OPERAND (target, 0));
7215 return MAX (this_align, outer_align);
7217 default:
7218 return TYPE_ALIGN (TREE_TYPE (target));
7223 /* Given an rtx VALUE that may contain additions and multiplications, return
7224 an equivalent value that just refers to a register, memory, or constant.
7225 This is done by generating instructions to perform the arithmetic and
7226 returning a pseudo-register containing the value.
7228 The returned value may be a REG, SUBREG, MEM or constant. */
7231 force_operand (rtx value, rtx target)
7233 rtx op1, op2;
7234 /* Use subtarget as the target for operand 0 of a binary operation. */
7235 rtx subtarget = get_subtarget (target);
7236 enum rtx_code code = GET_CODE (value);
7238 /* Check for subreg applied to an expression produced by loop optimizer. */
7239 if (code == SUBREG
7240 && !REG_P (SUBREG_REG (value))
7241 && !MEM_P (SUBREG_REG (value)))
7243 value
7244 = simplify_gen_subreg (GET_MODE (value),
7245 force_reg (GET_MODE (SUBREG_REG (value)),
7246 force_operand (SUBREG_REG (value),
7247 NULL_RTX)),
7248 GET_MODE (SUBREG_REG (value)),
7249 SUBREG_BYTE (value));
7250 code = GET_CODE (value);
7253 /* Check for a PIC address load. */
7254 if ((code == PLUS || code == MINUS)
7255 && XEXP (value, 0) == pic_offset_table_rtx
7256 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7257 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7258 || GET_CODE (XEXP (value, 1)) == CONST))
7260 if (!subtarget)
7261 subtarget = gen_reg_rtx (GET_MODE (value));
7262 emit_move_insn (subtarget, value);
7263 return subtarget;
7266 if (ARITHMETIC_P (value))
7268 op2 = XEXP (value, 1);
7269 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7270 subtarget = 0;
7271 if (code == MINUS && CONST_INT_P (op2))
7273 code = PLUS;
7274 op2 = negate_rtx (GET_MODE (value), op2);
7277 /* Check for an addition with OP2 a constant integer and our first
7278 operand a PLUS of a virtual register and something else. In that
7279 case, we want to emit the sum of the virtual register and the
7280 constant first and then add the other value. This allows virtual
7281 register instantiation to simply modify the constant rather than
7282 creating another one around this addition. */
7283 if (code == PLUS && CONST_INT_P (op2)
7284 && GET_CODE (XEXP (value, 0)) == PLUS
7285 && REG_P (XEXP (XEXP (value, 0), 0))
7286 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7287 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7289 rtx temp = expand_simple_binop (GET_MODE (value), code,
7290 XEXP (XEXP (value, 0), 0), op2,
7291 subtarget, 0, OPTAB_LIB_WIDEN);
7292 return expand_simple_binop (GET_MODE (value), code, temp,
7293 force_operand (XEXP (XEXP (value,
7294 0), 1), 0),
7295 target, 0, OPTAB_LIB_WIDEN);
7298 op1 = force_operand (XEXP (value, 0), subtarget);
7299 op2 = force_operand (op2, NULL_RTX);
7300 switch (code)
7302 case MULT:
7303 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7304 case DIV:
7305 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7306 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7307 target, 1, OPTAB_LIB_WIDEN);
7308 else
7309 return expand_divmod (0,
7310 FLOAT_MODE_P (GET_MODE (value))
7311 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7312 GET_MODE (value), op1, op2, target, 0);
7313 case MOD:
7314 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7315 target, 0);
7316 case UDIV:
7317 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7318 target, 1);
7319 case UMOD:
7320 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7321 target, 1);
7322 case ASHIFTRT:
7323 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7324 target, 0, OPTAB_LIB_WIDEN);
7325 default:
7326 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7327 target, 1, OPTAB_LIB_WIDEN);
7330 if (UNARY_P (value))
7332 if (!target)
7333 target = gen_reg_rtx (GET_MODE (value));
7334 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7335 switch (code)
7337 case ZERO_EXTEND:
7338 case SIGN_EXTEND:
7339 case TRUNCATE:
7340 case FLOAT_EXTEND:
7341 case FLOAT_TRUNCATE:
7342 convert_move (target, op1, code == ZERO_EXTEND);
7343 return target;
7345 case FIX:
7346 case UNSIGNED_FIX:
7347 expand_fix (target, op1, code == UNSIGNED_FIX);
7348 return target;
7350 case FLOAT:
7351 case UNSIGNED_FLOAT:
7352 expand_float (target, op1, code == UNSIGNED_FLOAT);
7353 return target;
7355 default:
7356 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7360 #ifdef INSN_SCHEDULING
7361 /* On machines that have insn scheduling, we want all memory reference to be
7362 explicit, so we need to deal with such paradoxical SUBREGs. */
7363 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7364 value
7365 = simplify_gen_subreg (GET_MODE (value),
7366 force_reg (GET_MODE (SUBREG_REG (value)),
7367 force_operand (SUBREG_REG (value),
7368 NULL_RTX)),
7369 GET_MODE (SUBREG_REG (value)),
7370 SUBREG_BYTE (value));
7371 #endif
7373 return value;
7376 /* Subroutine of expand_expr: return nonzero iff there is no way that
7377 EXP can reference X, which is being modified. TOP_P is nonzero if this
7378 call is going to be used to determine whether we need a temporary
7379 for EXP, as opposed to a recursive call to this function.
7381 It is always safe for this routine to return zero since it merely
7382 searches for optimization opportunities. */
7385 safe_from_p (const_rtx x, tree exp, int top_p)
7387 rtx exp_rtl = 0;
7388 int i, nops;
7390 if (x == 0
7391 /* If EXP has varying size, we MUST use a target since we currently
7392 have no way of allocating temporaries of variable size
7393 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7394 So we assume here that something at a higher level has prevented a
7395 clash. This is somewhat bogus, but the best we can do. Only
7396 do this when X is BLKmode and when we are at the top level. */
7397 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7398 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7399 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7400 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7401 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7402 != INTEGER_CST)
7403 && GET_MODE (x) == BLKmode)
7404 /* If X is in the outgoing argument area, it is always safe. */
7405 || (MEM_P (x)
7406 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7407 || (GET_CODE (XEXP (x, 0)) == PLUS
7408 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7409 return 1;
7411 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7412 find the underlying pseudo. */
7413 if (GET_CODE (x) == SUBREG)
7415 x = SUBREG_REG (x);
7416 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7417 return 0;
7420 /* Now look at our tree code and possibly recurse. */
7421 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7423 case tcc_declaration:
7424 exp_rtl = DECL_RTL_IF_SET (exp);
7425 break;
7427 case tcc_constant:
7428 return 1;
7430 case tcc_exceptional:
7431 if (TREE_CODE (exp) == TREE_LIST)
7433 while (1)
7435 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7436 return 0;
7437 exp = TREE_CHAIN (exp);
7438 if (!exp)
7439 return 1;
7440 if (TREE_CODE (exp) != TREE_LIST)
7441 return safe_from_p (x, exp, 0);
7444 else if (TREE_CODE (exp) == CONSTRUCTOR)
7446 constructor_elt *ce;
7447 unsigned HOST_WIDE_INT idx;
7449 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7450 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7451 || !safe_from_p (x, ce->value, 0))
7452 return 0;
7453 return 1;
7455 else if (TREE_CODE (exp) == ERROR_MARK)
7456 return 1; /* An already-visited SAVE_EXPR? */
7457 else
7458 return 0;
7460 case tcc_statement:
7461 /* The only case we look at here is the DECL_INITIAL inside a
7462 DECL_EXPR. */
7463 return (TREE_CODE (exp) != DECL_EXPR
7464 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7465 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7466 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7468 case tcc_binary:
7469 case tcc_comparison:
7470 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7471 return 0;
7472 /* Fall through. */
7474 case tcc_unary:
7475 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7477 case tcc_expression:
7478 case tcc_reference:
7479 case tcc_vl_exp:
7480 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7481 the expression. If it is set, we conflict iff we are that rtx or
7482 both are in memory. Otherwise, we check all operands of the
7483 expression recursively. */
7485 switch (TREE_CODE (exp))
7487 case ADDR_EXPR:
7488 /* If the operand is static or we are static, we can't conflict.
7489 Likewise if we don't conflict with the operand at all. */
7490 if (staticp (TREE_OPERAND (exp, 0))
7491 || TREE_STATIC (exp)
7492 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7493 return 1;
7495 /* Otherwise, the only way this can conflict is if we are taking
7496 the address of a DECL a that address if part of X, which is
7497 very rare. */
7498 exp = TREE_OPERAND (exp, 0);
7499 if (DECL_P (exp))
7501 if (!DECL_RTL_SET_P (exp)
7502 || !MEM_P (DECL_RTL (exp)))
7503 return 0;
7504 else
7505 exp_rtl = XEXP (DECL_RTL (exp), 0);
7507 break;
7509 case MEM_REF:
7510 if (MEM_P (x)
7511 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7512 get_alias_set (exp)))
7513 return 0;
7514 break;
7516 case CALL_EXPR:
7517 /* Assume that the call will clobber all hard registers and
7518 all of memory. */
7519 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7520 || MEM_P (x))
7521 return 0;
7522 break;
7524 case WITH_CLEANUP_EXPR:
7525 case CLEANUP_POINT_EXPR:
7526 /* Lowered by gimplify.c. */
7527 gcc_unreachable ();
7529 case SAVE_EXPR:
7530 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7532 default:
7533 break;
7536 /* If we have an rtx, we do not need to scan our operands. */
7537 if (exp_rtl)
7538 break;
7540 nops = TREE_OPERAND_LENGTH (exp);
7541 for (i = 0; i < nops; i++)
7542 if (TREE_OPERAND (exp, i) != 0
7543 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7544 return 0;
7546 break;
7548 case tcc_type:
7549 /* Should never get a type here. */
7550 gcc_unreachable ();
7553 /* If we have an rtl, find any enclosed object. Then see if we conflict
7554 with it. */
7555 if (exp_rtl)
7557 if (GET_CODE (exp_rtl) == SUBREG)
7559 exp_rtl = SUBREG_REG (exp_rtl);
7560 if (REG_P (exp_rtl)
7561 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7562 return 0;
7565 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7566 are memory and they conflict. */
7567 return ! (rtx_equal_p (x, exp_rtl)
7568 || (MEM_P (x) && MEM_P (exp_rtl)
7569 && true_dependence (exp_rtl, VOIDmode, x)));
7572 /* If we reach here, it is safe. */
7573 return 1;
7577 /* Return the highest power of two that EXP is known to be a multiple of.
7578 This is used in updating alignment of MEMs in array references. */
7580 unsigned HOST_WIDE_INT
7581 highest_pow2_factor (const_tree exp)
7583 unsigned HOST_WIDE_INT ret;
7584 int trailing_zeros = tree_ctz (exp);
7585 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7586 return BIGGEST_ALIGNMENT;
7587 ret = HOST_WIDE_INT_1U << trailing_zeros;
7588 if (ret > BIGGEST_ALIGNMENT)
7589 return BIGGEST_ALIGNMENT;
7590 return ret;
7593 /* Similar, except that the alignment requirements of TARGET are
7594 taken into account. Assume it is at least as aligned as its
7595 type, unless it is a COMPONENT_REF in which case the layout of
7596 the structure gives the alignment. */
7598 static unsigned HOST_WIDE_INT
7599 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7601 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7602 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7604 return MAX (factor, talign);
7607 /* Convert the tree comparison code TCODE to the rtl one where the
7608 signedness is UNSIGNEDP. */
7610 static enum rtx_code
7611 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7613 enum rtx_code code;
7614 switch (tcode)
7616 case EQ_EXPR:
7617 code = EQ;
7618 break;
7619 case NE_EXPR:
7620 code = NE;
7621 break;
7622 case LT_EXPR:
7623 code = unsignedp ? LTU : LT;
7624 break;
7625 case LE_EXPR:
7626 code = unsignedp ? LEU : LE;
7627 break;
7628 case GT_EXPR:
7629 code = unsignedp ? GTU : GT;
7630 break;
7631 case GE_EXPR:
7632 code = unsignedp ? GEU : GE;
7633 break;
7634 case UNORDERED_EXPR:
7635 code = UNORDERED;
7636 break;
7637 case ORDERED_EXPR:
7638 code = ORDERED;
7639 break;
7640 case UNLT_EXPR:
7641 code = UNLT;
7642 break;
7643 case UNLE_EXPR:
7644 code = UNLE;
7645 break;
7646 case UNGT_EXPR:
7647 code = UNGT;
7648 break;
7649 case UNGE_EXPR:
7650 code = UNGE;
7651 break;
7652 case UNEQ_EXPR:
7653 code = UNEQ;
7654 break;
7655 case LTGT_EXPR:
7656 code = LTGT;
7657 break;
7659 default:
7660 gcc_unreachable ();
7662 return code;
7665 /* Subroutine of expand_expr. Expand the two operands of a binary
7666 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7667 The value may be stored in TARGET if TARGET is nonzero. The
7668 MODIFIER argument is as documented by expand_expr. */
7670 void
7671 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7672 enum expand_modifier modifier)
7674 if (! safe_from_p (target, exp1, 1))
7675 target = 0;
7676 if (operand_equal_p (exp0, exp1, 0))
7678 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7679 *op1 = copy_rtx (*op0);
7681 else
7683 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7684 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7689 /* Return a MEM that contains constant EXP. DEFER is as for
7690 output_constant_def and MODIFIER is as for expand_expr. */
7692 static rtx
7693 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7695 rtx mem;
7697 mem = output_constant_def (exp, defer);
7698 if (modifier != EXPAND_INITIALIZER)
7699 mem = use_anchored_address (mem);
7700 return mem;
7703 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7704 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7706 static rtx
7707 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7708 enum expand_modifier modifier, addr_space_t as)
7710 rtx result, subtarget;
7711 tree inner, offset;
7712 HOST_WIDE_INT bitsize, bitpos;
7713 int unsignedp, reversep, volatilep = 0;
7714 machine_mode mode1;
7716 /* If we are taking the address of a constant and are at the top level,
7717 we have to use output_constant_def since we can't call force_const_mem
7718 at top level. */
7719 /* ??? This should be considered a front-end bug. We should not be
7720 generating ADDR_EXPR of something that isn't an LVALUE. The only
7721 exception here is STRING_CST. */
7722 if (CONSTANT_CLASS_P (exp))
7724 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7725 if (modifier < EXPAND_SUM)
7726 result = force_operand (result, target);
7727 return result;
7730 /* Everything must be something allowed by is_gimple_addressable. */
7731 switch (TREE_CODE (exp))
7733 case INDIRECT_REF:
7734 /* This case will happen via recursion for &a->b. */
7735 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7737 case MEM_REF:
7739 tree tem = TREE_OPERAND (exp, 0);
7740 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7741 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7742 return expand_expr (tem, target, tmode, modifier);
7745 case CONST_DECL:
7746 /* Expand the initializer like constants above. */
7747 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7748 0, modifier), 0);
7749 if (modifier < EXPAND_SUM)
7750 result = force_operand (result, target);
7751 return result;
7753 case REALPART_EXPR:
7754 /* The real part of the complex number is always first, therefore
7755 the address is the same as the address of the parent object. */
7756 offset = 0;
7757 bitpos = 0;
7758 inner = TREE_OPERAND (exp, 0);
7759 break;
7761 case IMAGPART_EXPR:
7762 /* The imaginary part of the complex number is always second.
7763 The expression is therefore always offset by the size of the
7764 scalar type. */
7765 offset = 0;
7766 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7767 inner = TREE_OPERAND (exp, 0);
7768 break;
7770 case COMPOUND_LITERAL_EXPR:
7771 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7772 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7773 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7774 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7775 the initializers aren't gimplified. */
7776 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7777 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7778 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7779 target, tmode, modifier, as);
7780 /* FALLTHRU */
7781 default:
7782 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7783 expand_expr, as that can have various side effects; LABEL_DECLs for
7784 example, may not have their DECL_RTL set yet. Expand the rtl of
7785 CONSTRUCTORs too, which should yield a memory reference for the
7786 constructor's contents. Assume language specific tree nodes can
7787 be expanded in some interesting way. */
7788 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7789 if (DECL_P (exp)
7790 || TREE_CODE (exp) == CONSTRUCTOR
7791 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7793 result = expand_expr (exp, target, tmode,
7794 modifier == EXPAND_INITIALIZER
7795 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7797 /* If the DECL isn't in memory, then the DECL wasn't properly
7798 marked TREE_ADDRESSABLE, which will be either a front-end
7799 or a tree optimizer bug. */
7801 gcc_assert (MEM_P (result));
7802 result = XEXP (result, 0);
7804 /* ??? Is this needed anymore? */
7805 if (DECL_P (exp))
7806 TREE_USED (exp) = 1;
7808 if (modifier != EXPAND_INITIALIZER
7809 && modifier != EXPAND_CONST_ADDRESS
7810 && modifier != EXPAND_SUM)
7811 result = force_operand (result, target);
7812 return result;
7815 /* Pass FALSE as the last argument to get_inner_reference although
7816 we are expanding to RTL. The rationale is that we know how to
7817 handle "aligning nodes" here: we can just bypass them because
7818 they won't change the final object whose address will be returned
7819 (they actually exist only for that purpose). */
7820 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7821 &unsignedp, &reversep, &volatilep);
7822 break;
7825 /* We must have made progress. */
7826 gcc_assert (inner != exp);
7828 subtarget = offset || bitpos ? NULL_RTX : target;
7829 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7830 inner alignment, force the inner to be sufficiently aligned. */
7831 if (CONSTANT_CLASS_P (inner)
7832 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7834 inner = copy_node (inner);
7835 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7836 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7837 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7839 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7841 if (offset)
7843 rtx tmp;
7845 if (modifier != EXPAND_NORMAL)
7846 result = force_operand (result, NULL);
7847 tmp = expand_expr (offset, NULL_RTX, tmode,
7848 modifier == EXPAND_INITIALIZER
7849 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7851 /* expand_expr is allowed to return an object in a mode other
7852 than TMODE. If it did, we need to convert. */
7853 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7854 tmp = convert_modes (tmode, GET_MODE (tmp),
7855 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7856 result = convert_memory_address_addr_space (tmode, result, as);
7857 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7859 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7860 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7861 else
7863 subtarget = bitpos ? NULL_RTX : target;
7864 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7865 1, OPTAB_LIB_WIDEN);
7869 if (bitpos)
7871 /* Someone beforehand should have rejected taking the address
7872 of such an object. */
7873 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7875 result = convert_memory_address_addr_space (tmode, result, as);
7876 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7877 if (modifier < EXPAND_SUM)
7878 result = force_operand (result, target);
7881 return result;
7884 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7885 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7887 static rtx
7888 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7889 enum expand_modifier modifier)
7891 addr_space_t as = ADDR_SPACE_GENERIC;
7892 scalar_int_mode address_mode = Pmode;
7893 scalar_int_mode pointer_mode = ptr_mode;
7894 machine_mode rmode;
7895 rtx result;
7897 /* Target mode of VOIDmode says "whatever's natural". */
7898 if (tmode == VOIDmode)
7899 tmode = TYPE_MODE (TREE_TYPE (exp));
7901 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7903 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7904 address_mode = targetm.addr_space.address_mode (as);
7905 pointer_mode = targetm.addr_space.pointer_mode (as);
7908 /* We can get called with some Weird Things if the user does silliness
7909 like "(short) &a". In that case, convert_memory_address won't do
7910 the right thing, so ignore the given target mode. */
7911 scalar_int_mode new_tmode = (tmode == pointer_mode
7912 ? pointer_mode
7913 : address_mode);
7915 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7916 new_tmode, modifier, as);
7918 /* Despite expand_expr claims concerning ignoring TMODE when not
7919 strictly convenient, stuff breaks if we don't honor it. Note
7920 that combined with the above, we only do this for pointer modes. */
7921 rmode = GET_MODE (result);
7922 if (rmode == VOIDmode)
7923 rmode = new_tmode;
7924 if (rmode != new_tmode)
7925 result = convert_memory_address_addr_space (new_tmode, result, as);
7927 return result;
7930 /* Generate code for computing CONSTRUCTOR EXP.
7931 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7932 is TRUE, instead of creating a temporary variable in memory
7933 NULL is returned and the caller needs to handle it differently. */
7935 static rtx
7936 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7937 bool avoid_temp_mem)
7939 tree type = TREE_TYPE (exp);
7940 machine_mode mode = TYPE_MODE (type);
7942 /* Try to avoid creating a temporary at all. This is possible
7943 if all of the initializer is zero.
7944 FIXME: try to handle all [0..255] initializers we can handle
7945 with memset. */
7946 if (TREE_STATIC (exp)
7947 && !TREE_ADDRESSABLE (exp)
7948 && target != 0 && mode == BLKmode
7949 && all_zeros_p (exp))
7951 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7952 return target;
7955 /* All elts simple constants => refer to a constant in memory. But
7956 if this is a non-BLKmode mode, let it store a field at a time
7957 since that should make a CONST_INT, CONST_WIDE_INT or
7958 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7959 use, it is best to store directly into the target unless the type
7960 is large enough that memcpy will be used. If we are making an
7961 initializer and all operands are constant, put it in memory as
7962 well.
7964 FIXME: Avoid trying to fill vector constructors piece-meal.
7965 Output them with output_constant_def below unless we're sure
7966 they're zeros. This should go away when vector initializers
7967 are treated like VECTOR_CST instead of arrays. */
7968 if ((TREE_STATIC (exp)
7969 && ((mode == BLKmode
7970 && ! (target != 0 && safe_from_p (target, exp, 1)))
7971 || TREE_ADDRESSABLE (exp)
7972 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7973 && (! can_move_by_pieces
7974 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7975 TYPE_ALIGN (type)))
7976 && ! mostly_zeros_p (exp))))
7977 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7978 && TREE_CONSTANT (exp)))
7980 rtx constructor;
7982 if (avoid_temp_mem)
7983 return NULL_RTX;
7985 constructor = expand_expr_constant (exp, 1, modifier);
7987 if (modifier != EXPAND_CONST_ADDRESS
7988 && modifier != EXPAND_INITIALIZER
7989 && modifier != EXPAND_SUM)
7990 constructor = validize_mem (constructor);
7992 return constructor;
7995 /* Handle calls that pass values in multiple non-contiguous
7996 locations. The Irix 6 ABI has examples of this. */
7997 if (target == 0 || ! safe_from_p (target, exp, 1)
7998 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8000 if (avoid_temp_mem)
8001 return NULL_RTX;
8003 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8006 store_constructor (exp, target, 0, int_expr_size (exp), false);
8007 return target;
8011 /* expand_expr: generate code for computing expression EXP.
8012 An rtx for the computed value is returned. The value is never null.
8013 In the case of a void EXP, const0_rtx is returned.
8015 The value may be stored in TARGET if TARGET is nonzero.
8016 TARGET is just a suggestion; callers must assume that
8017 the rtx returned may not be the same as TARGET.
8019 If TARGET is CONST0_RTX, it means that the value will be ignored.
8021 If TMODE is not VOIDmode, it suggests generating the
8022 result in mode TMODE. But this is done only when convenient.
8023 Otherwise, TMODE is ignored and the value generated in its natural mode.
8024 TMODE is just a suggestion; callers must assume that
8025 the rtx returned may not have mode TMODE.
8027 Note that TARGET may have neither TMODE nor MODE. In that case, it
8028 probably will not be used.
8030 If MODIFIER is EXPAND_SUM then when EXP is an addition
8031 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8032 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8033 products as above, or REG or MEM, or constant.
8034 Ordinarily in such cases we would output mul or add instructions
8035 and then return a pseudo reg containing the sum.
8037 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8038 it also marks a label as absolutely required (it can't be dead).
8039 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8040 This is used for outputting expressions used in initializers.
8042 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8043 with a constant address even if that address is not normally legitimate.
8044 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8046 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8047 a call parameter. Such targets require special care as we haven't yet
8048 marked TARGET so that it's safe from being trashed by libcalls. We
8049 don't want to use TARGET for anything but the final result;
8050 Intermediate values must go elsewhere. Additionally, calls to
8051 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8053 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8054 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8055 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8056 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8057 recursively.
8059 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8060 In this case, we don't adjust a returned MEM rtx that wouldn't be
8061 sufficiently aligned for its mode; instead, it's up to the caller
8062 to deal with it afterwards. This is used to make sure that unaligned
8063 base objects for which out-of-bounds accesses are supported, for
8064 example record types with trailing arrays, aren't realigned behind
8065 the back of the caller.
8066 The normal operating mode is to pass FALSE for this parameter. */
8069 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8070 enum expand_modifier modifier, rtx *alt_rtl,
8071 bool inner_reference_p)
8073 rtx ret;
8075 /* Handle ERROR_MARK before anybody tries to access its type. */
8076 if (TREE_CODE (exp) == ERROR_MARK
8077 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8079 ret = CONST0_RTX (tmode);
8080 return ret ? ret : const0_rtx;
8083 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8084 inner_reference_p);
8085 return ret;
8088 /* Try to expand the conditional expression which is represented by
8089 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8090 return the rtl reg which represents the result. Otherwise return
8091 NULL_RTX. */
8093 static rtx
8094 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8095 tree treeop1 ATTRIBUTE_UNUSED,
8096 tree treeop2 ATTRIBUTE_UNUSED)
8098 rtx insn;
8099 rtx op00, op01, op1, op2;
8100 enum rtx_code comparison_code;
8101 machine_mode comparison_mode;
8102 gimple *srcstmt;
8103 rtx temp;
8104 tree type = TREE_TYPE (treeop1);
8105 int unsignedp = TYPE_UNSIGNED (type);
8106 machine_mode mode = TYPE_MODE (type);
8107 machine_mode orig_mode = mode;
8108 static bool expanding_cond_expr_using_cmove = false;
8110 /* Conditional move expansion can end up TERing two operands which,
8111 when recursively hitting conditional expressions can result in
8112 exponential behavior if the cmove expansion ultimatively fails.
8113 It's hardly profitable to TER a cmove into a cmove so avoid doing
8114 that by failing early if we end up recursing. */
8115 if (expanding_cond_expr_using_cmove)
8116 return NULL_RTX;
8118 /* If we cannot do a conditional move on the mode, try doing it
8119 with the promoted mode. */
8120 if (!can_conditionally_move_p (mode))
8122 mode = promote_mode (type, mode, &unsignedp);
8123 if (!can_conditionally_move_p (mode))
8124 return NULL_RTX;
8125 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8127 else
8128 temp = assign_temp (type, 0, 1);
8130 expanding_cond_expr_using_cmove = true;
8131 start_sequence ();
8132 expand_operands (treeop1, treeop2,
8133 temp, &op1, &op2, EXPAND_NORMAL);
8135 if (TREE_CODE (treeop0) == SSA_NAME
8136 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8138 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8139 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8140 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8141 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8142 comparison_mode = TYPE_MODE (type);
8143 unsignedp = TYPE_UNSIGNED (type);
8144 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8146 else if (COMPARISON_CLASS_P (treeop0))
8148 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8149 enum tree_code cmpcode = TREE_CODE (treeop0);
8150 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8151 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8152 unsignedp = TYPE_UNSIGNED (type);
8153 comparison_mode = TYPE_MODE (type);
8154 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8156 else
8158 op00 = expand_normal (treeop0);
8159 op01 = const0_rtx;
8160 comparison_code = NE;
8161 comparison_mode = GET_MODE (op00);
8162 if (comparison_mode == VOIDmode)
8163 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8165 expanding_cond_expr_using_cmove = false;
8167 if (GET_MODE (op1) != mode)
8168 op1 = gen_lowpart (mode, op1);
8170 if (GET_MODE (op2) != mode)
8171 op2 = gen_lowpart (mode, op2);
8173 /* Try to emit the conditional move. */
8174 insn = emit_conditional_move (temp, comparison_code,
8175 op00, op01, comparison_mode,
8176 op1, op2, mode,
8177 unsignedp);
8179 /* If we could do the conditional move, emit the sequence,
8180 and return. */
8181 if (insn)
8183 rtx_insn *seq = get_insns ();
8184 end_sequence ();
8185 emit_insn (seq);
8186 return convert_modes (orig_mode, mode, temp, 0);
8189 /* Otherwise discard the sequence and fall back to code with
8190 branches. */
8191 end_sequence ();
8192 return NULL_RTX;
8196 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8197 enum expand_modifier modifier)
8199 rtx op0, op1, op2, temp;
8200 rtx_code_label *lab;
8201 tree type;
8202 int unsignedp;
8203 machine_mode mode;
8204 scalar_int_mode int_mode;
8205 enum tree_code code = ops->code;
8206 optab this_optab;
8207 rtx subtarget, original_target;
8208 int ignore;
8209 bool reduce_bit_field;
8210 location_t loc = ops->location;
8211 tree treeop0, treeop1, treeop2;
8212 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8213 ? reduce_to_bit_field_precision ((expr), \
8214 target, \
8215 type) \
8216 : (expr))
8218 type = ops->type;
8219 mode = TYPE_MODE (type);
8220 unsignedp = TYPE_UNSIGNED (type);
8222 treeop0 = ops->op0;
8223 treeop1 = ops->op1;
8224 treeop2 = ops->op2;
8226 /* We should be called only on simple (binary or unary) expressions,
8227 exactly those that are valid in gimple expressions that aren't
8228 GIMPLE_SINGLE_RHS (or invalid). */
8229 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8230 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8231 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8233 ignore = (target == const0_rtx
8234 || ((CONVERT_EXPR_CODE_P (code)
8235 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8236 && TREE_CODE (type) == VOID_TYPE));
8238 /* We should be called only if we need the result. */
8239 gcc_assert (!ignore);
8241 /* An operation in what may be a bit-field type needs the
8242 result to be reduced to the precision of the bit-field type,
8243 which is narrower than that of the type's mode. */
8244 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8245 && !type_has_mode_precision_p (type));
8247 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8248 target = 0;
8250 /* Use subtarget as the target for operand 0 of a binary operation. */
8251 subtarget = get_subtarget (target);
8252 original_target = target;
8254 switch (code)
8256 case NON_LVALUE_EXPR:
8257 case PAREN_EXPR:
8258 CASE_CONVERT:
8259 if (treeop0 == error_mark_node)
8260 return const0_rtx;
8262 if (TREE_CODE (type) == UNION_TYPE)
8264 tree valtype = TREE_TYPE (treeop0);
8266 /* If both input and output are BLKmode, this conversion isn't doing
8267 anything except possibly changing memory attribute. */
8268 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8270 rtx result = expand_expr (treeop0, target, tmode,
8271 modifier);
8273 result = copy_rtx (result);
8274 set_mem_attributes (result, type, 0);
8275 return result;
8278 if (target == 0)
8280 if (TYPE_MODE (type) != BLKmode)
8281 target = gen_reg_rtx (TYPE_MODE (type));
8282 else
8283 target = assign_temp (type, 1, 1);
8286 if (MEM_P (target))
8287 /* Store data into beginning of memory target. */
8288 store_expr (treeop0,
8289 adjust_address (target, TYPE_MODE (valtype), 0),
8290 modifier == EXPAND_STACK_PARM,
8291 false, TYPE_REVERSE_STORAGE_ORDER (type));
8293 else
8295 gcc_assert (REG_P (target)
8296 && !TYPE_REVERSE_STORAGE_ORDER (type));
8298 /* Store this field into a union of the proper type. */
8299 store_field (target,
8300 MIN ((int_size_in_bytes (TREE_TYPE
8301 (treeop0))
8302 * BITS_PER_UNIT),
8303 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8304 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8305 false, false);
8308 /* Return the entire union. */
8309 return target;
8312 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8314 op0 = expand_expr (treeop0, target, VOIDmode,
8315 modifier);
8317 /* If the signedness of the conversion differs and OP0 is
8318 a promoted SUBREG, clear that indication since we now
8319 have to do the proper extension. */
8320 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8321 && GET_CODE (op0) == SUBREG)
8322 SUBREG_PROMOTED_VAR_P (op0) = 0;
8324 return REDUCE_BIT_FIELD (op0);
8327 op0 = expand_expr (treeop0, NULL_RTX, mode,
8328 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8329 if (GET_MODE (op0) == mode)
8332 /* If OP0 is a constant, just convert it into the proper mode. */
8333 else if (CONSTANT_P (op0))
8335 tree inner_type = TREE_TYPE (treeop0);
8336 machine_mode inner_mode = GET_MODE (op0);
8338 if (inner_mode == VOIDmode)
8339 inner_mode = TYPE_MODE (inner_type);
8341 if (modifier == EXPAND_INITIALIZER)
8342 op0 = lowpart_subreg (mode, op0, inner_mode);
8343 else
8344 op0= convert_modes (mode, inner_mode, op0,
8345 TYPE_UNSIGNED (inner_type));
8348 else if (modifier == EXPAND_INITIALIZER)
8349 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8350 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8352 else if (target == 0)
8353 op0 = convert_to_mode (mode, op0,
8354 TYPE_UNSIGNED (TREE_TYPE
8355 (treeop0)));
8356 else
8358 convert_move (target, op0,
8359 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8360 op0 = target;
8363 return REDUCE_BIT_FIELD (op0);
8365 case ADDR_SPACE_CONVERT_EXPR:
8367 tree treeop0_type = TREE_TYPE (treeop0);
8369 gcc_assert (POINTER_TYPE_P (type));
8370 gcc_assert (POINTER_TYPE_P (treeop0_type));
8372 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8373 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8375 /* Conversions between pointers to the same address space should
8376 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8377 gcc_assert (as_to != as_from);
8379 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8381 /* Ask target code to handle conversion between pointers
8382 to overlapping address spaces. */
8383 if (targetm.addr_space.subset_p (as_to, as_from)
8384 || targetm.addr_space.subset_p (as_from, as_to))
8386 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8388 else
8390 /* For disjoint address spaces, converting anything but a null
8391 pointer invokes undefined behavior. We truncate or extend the
8392 value as if we'd converted via integers, which handles 0 as
8393 required, and all others as the programmer likely expects. */
8394 #ifndef POINTERS_EXTEND_UNSIGNED
8395 const int POINTERS_EXTEND_UNSIGNED = 1;
8396 #endif
8397 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8398 op0, POINTERS_EXTEND_UNSIGNED);
8400 gcc_assert (op0);
8401 return op0;
8404 case POINTER_PLUS_EXPR:
8405 /* Even though the sizetype mode and the pointer's mode can be different
8406 expand is able to handle this correctly and get the correct result out
8407 of the PLUS_EXPR code. */
8408 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8409 if sizetype precision is smaller than pointer precision. */
8410 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8411 treeop1 = fold_convert_loc (loc, type,
8412 fold_convert_loc (loc, ssizetype,
8413 treeop1));
8414 /* If sizetype precision is larger than pointer precision, truncate the
8415 offset to have matching modes. */
8416 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8417 treeop1 = fold_convert_loc (loc, type, treeop1);
8418 /* FALLTHRU */
8420 case PLUS_EXPR:
8421 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8422 something else, make sure we add the register to the constant and
8423 then to the other thing. This case can occur during strength
8424 reduction and doing it this way will produce better code if the
8425 frame pointer or argument pointer is eliminated.
8427 fold-const.c will ensure that the constant is always in the inner
8428 PLUS_EXPR, so the only case we need to do anything about is if
8429 sp, ap, or fp is our second argument, in which case we must swap
8430 the innermost first argument and our second argument. */
8432 if (TREE_CODE (treeop0) == PLUS_EXPR
8433 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8434 && VAR_P (treeop1)
8435 && (DECL_RTL (treeop1) == frame_pointer_rtx
8436 || DECL_RTL (treeop1) == stack_pointer_rtx
8437 || DECL_RTL (treeop1) == arg_pointer_rtx))
8439 gcc_unreachable ();
8442 /* If the result is to be ptr_mode and we are adding an integer to
8443 something, we might be forming a constant. So try to use
8444 plus_constant. If it produces a sum and we can't accept it,
8445 use force_operand. This allows P = &ARR[const] to generate
8446 efficient code on machines where a SYMBOL_REF is not a valid
8447 address.
8449 If this is an EXPAND_SUM call, always return the sum. */
8450 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8451 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8453 if (modifier == EXPAND_STACK_PARM)
8454 target = 0;
8455 if (TREE_CODE (treeop0) == INTEGER_CST
8456 && HWI_COMPUTABLE_MODE_P (mode)
8457 && TREE_CONSTANT (treeop1))
8459 rtx constant_part;
8460 HOST_WIDE_INT wc;
8461 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8463 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8464 EXPAND_SUM);
8465 /* Use wi::shwi to ensure that the constant is
8466 truncated according to the mode of OP1, then sign extended
8467 to a HOST_WIDE_INT. Using the constant directly can result
8468 in non-canonical RTL in a 64x32 cross compile. */
8469 wc = TREE_INT_CST_LOW (treeop0);
8470 constant_part =
8471 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8472 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8473 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8474 op1 = force_operand (op1, target);
8475 return REDUCE_BIT_FIELD (op1);
8478 else if (TREE_CODE (treeop1) == INTEGER_CST
8479 && HWI_COMPUTABLE_MODE_P (mode)
8480 && TREE_CONSTANT (treeop0))
8482 rtx constant_part;
8483 HOST_WIDE_INT wc;
8484 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8486 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8487 (modifier == EXPAND_INITIALIZER
8488 ? EXPAND_INITIALIZER : EXPAND_SUM));
8489 if (! CONSTANT_P (op0))
8491 op1 = expand_expr (treeop1, NULL_RTX,
8492 VOIDmode, modifier);
8493 /* Return a PLUS if modifier says it's OK. */
8494 if (modifier == EXPAND_SUM
8495 || modifier == EXPAND_INITIALIZER)
8496 return simplify_gen_binary (PLUS, mode, op0, op1);
8497 goto binop2;
8499 /* Use wi::shwi to ensure that the constant is
8500 truncated according to the mode of OP1, then sign extended
8501 to a HOST_WIDE_INT. Using the constant directly can result
8502 in non-canonical RTL in a 64x32 cross compile. */
8503 wc = TREE_INT_CST_LOW (treeop1);
8504 constant_part
8505 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8506 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8507 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8508 op0 = force_operand (op0, target);
8509 return REDUCE_BIT_FIELD (op0);
8513 /* Use TER to expand pointer addition of a negated value
8514 as pointer subtraction. */
8515 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8516 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8517 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8518 && TREE_CODE (treeop1) == SSA_NAME
8519 && TYPE_MODE (TREE_TYPE (treeop0))
8520 == TYPE_MODE (TREE_TYPE (treeop1)))
8522 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8523 if (def)
8525 treeop1 = gimple_assign_rhs1 (def);
8526 code = MINUS_EXPR;
8527 goto do_minus;
8531 /* No sense saving up arithmetic to be done
8532 if it's all in the wrong mode to form part of an address.
8533 And force_operand won't know whether to sign-extend or
8534 zero-extend. */
8535 if (modifier != EXPAND_INITIALIZER
8536 && (modifier != EXPAND_SUM || mode != ptr_mode))
8538 expand_operands (treeop0, treeop1,
8539 subtarget, &op0, &op1, modifier);
8540 if (op0 == const0_rtx)
8541 return op1;
8542 if (op1 == const0_rtx)
8543 return op0;
8544 goto binop2;
8547 expand_operands (treeop0, treeop1,
8548 subtarget, &op0, &op1, modifier);
8549 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8551 case MINUS_EXPR:
8552 do_minus:
8553 /* For initializers, we are allowed to return a MINUS of two
8554 symbolic constants. Here we handle all cases when both operands
8555 are constant. */
8556 /* Handle difference of two symbolic constants,
8557 for the sake of an initializer. */
8558 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8559 && really_constant_p (treeop0)
8560 && really_constant_p (treeop1))
8562 expand_operands (treeop0, treeop1,
8563 NULL_RTX, &op0, &op1, modifier);
8564 return simplify_gen_binary (MINUS, mode, op0, op1);
8567 /* No sense saving up arithmetic to be done
8568 if it's all in the wrong mode to form part of an address.
8569 And force_operand won't know whether to sign-extend or
8570 zero-extend. */
8571 if (modifier != EXPAND_INITIALIZER
8572 && (modifier != EXPAND_SUM || mode != ptr_mode))
8573 goto binop;
8575 expand_operands (treeop0, treeop1,
8576 subtarget, &op0, &op1, modifier);
8578 /* Convert A - const to A + (-const). */
8579 if (CONST_INT_P (op1))
8581 op1 = negate_rtx (mode, op1);
8582 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8585 goto binop2;
8587 case WIDEN_MULT_PLUS_EXPR:
8588 case WIDEN_MULT_MINUS_EXPR:
8589 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8590 op2 = expand_normal (treeop2);
8591 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8592 target, unsignedp);
8593 return target;
8595 case WIDEN_MULT_EXPR:
8596 /* If first operand is constant, swap them.
8597 Thus the following special case checks need only
8598 check the second operand. */
8599 if (TREE_CODE (treeop0) == INTEGER_CST)
8600 std::swap (treeop0, treeop1);
8602 /* First, check if we have a multiplication of one signed and one
8603 unsigned operand. */
8604 if (TREE_CODE (treeop1) != INTEGER_CST
8605 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8606 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8608 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8609 this_optab = usmul_widen_optab;
8610 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8611 != CODE_FOR_nothing)
8613 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8614 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8615 EXPAND_NORMAL);
8616 else
8617 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8618 EXPAND_NORMAL);
8619 /* op0 and op1 might still be constant, despite the above
8620 != INTEGER_CST check. Handle it. */
8621 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8623 op0 = convert_modes (innermode, mode, op0, true);
8624 op1 = convert_modes (innermode, mode, op1, false);
8625 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8626 target, unsignedp));
8628 goto binop3;
8631 /* Check for a multiplication with matching signedness. */
8632 else if ((TREE_CODE (treeop1) == INTEGER_CST
8633 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8634 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8635 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8637 tree op0type = TREE_TYPE (treeop0);
8638 machine_mode innermode = TYPE_MODE (op0type);
8639 bool zextend_p = TYPE_UNSIGNED (op0type);
8640 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8641 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8643 if (TREE_CODE (treeop0) != INTEGER_CST)
8645 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8646 != CODE_FOR_nothing)
8648 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8649 EXPAND_NORMAL);
8650 /* op0 and op1 might still be constant, despite the above
8651 != INTEGER_CST check. Handle it. */
8652 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8654 widen_mult_const:
8655 op0 = convert_modes (innermode, mode, op0, zextend_p);
8657 = convert_modes (innermode, mode, op1,
8658 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8659 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8660 target,
8661 unsignedp));
8663 temp = expand_widening_mult (mode, op0, op1, target,
8664 unsignedp, this_optab);
8665 return REDUCE_BIT_FIELD (temp);
8667 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8668 != CODE_FOR_nothing
8669 && innermode == word_mode)
8671 rtx htem, hipart;
8672 op0 = expand_normal (treeop0);
8673 if (TREE_CODE (treeop1) == INTEGER_CST)
8674 op1 = convert_modes (word_mode, mode,
8675 expand_normal (treeop1),
8676 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8677 else
8678 op1 = expand_normal (treeop1);
8679 /* op0 and op1 might still be constant, despite the above
8680 != INTEGER_CST check. Handle it. */
8681 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8682 goto widen_mult_const;
8683 temp = expand_binop (mode, other_optab, op0, op1, target,
8684 unsignedp, OPTAB_LIB_WIDEN);
8685 hipart = gen_highpart (word_mode, temp);
8686 htem = expand_mult_highpart_adjust (word_mode, hipart,
8687 op0, op1, hipart,
8688 zextend_p);
8689 if (htem != hipart)
8690 emit_move_insn (hipart, htem);
8691 return REDUCE_BIT_FIELD (temp);
8695 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8696 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8697 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8698 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8700 case FMA_EXPR:
8702 optab opt = fma_optab;
8703 gimple *def0, *def2;
8705 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8706 call. */
8707 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8709 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8710 tree call_expr;
8712 gcc_assert (fn != NULL_TREE);
8713 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8714 return expand_builtin (call_expr, target, subtarget, mode, false);
8717 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8718 /* The multiplication is commutative - look at its 2nd operand
8719 if the first isn't fed by a negate. */
8720 if (!def0)
8722 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8723 /* Swap operands if the 2nd operand is fed by a negate. */
8724 if (def0)
8725 std::swap (treeop0, treeop1);
8727 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8729 op0 = op2 = NULL;
8731 if (def0 && def2
8732 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8734 opt = fnms_optab;
8735 op0 = expand_normal (gimple_assign_rhs1 (def0));
8736 op2 = expand_normal (gimple_assign_rhs1 (def2));
8738 else if (def0
8739 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8741 opt = fnma_optab;
8742 op0 = expand_normal (gimple_assign_rhs1 (def0));
8744 else if (def2
8745 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8747 opt = fms_optab;
8748 op2 = expand_normal (gimple_assign_rhs1 (def2));
8751 if (op0 == NULL)
8752 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8753 if (op2 == NULL)
8754 op2 = expand_normal (treeop2);
8755 op1 = expand_normal (treeop1);
8757 return expand_ternary_op (TYPE_MODE (type), opt,
8758 op0, op1, op2, target, 0);
8761 case MULT_EXPR:
8762 /* If this is a fixed-point operation, then we cannot use the code
8763 below because "expand_mult" doesn't support sat/no-sat fixed-point
8764 multiplications. */
8765 if (ALL_FIXED_POINT_MODE_P (mode))
8766 goto binop;
8768 /* If first operand is constant, swap them.
8769 Thus the following special case checks need only
8770 check the second operand. */
8771 if (TREE_CODE (treeop0) == INTEGER_CST)
8772 std::swap (treeop0, treeop1);
8774 /* Attempt to return something suitable for generating an
8775 indexed address, for machines that support that. */
8777 if (modifier == EXPAND_SUM && mode == ptr_mode
8778 && tree_fits_shwi_p (treeop1))
8780 tree exp1 = treeop1;
8782 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8783 EXPAND_SUM);
8785 if (!REG_P (op0))
8786 op0 = force_operand (op0, NULL_RTX);
8787 if (!REG_P (op0))
8788 op0 = copy_to_mode_reg (mode, op0);
8790 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8791 gen_int_mode (tree_to_shwi (exp1),
8792 TYPE_MODE (TREE_TYPE (exp1)))));
8795 if (modifier == EXPAND_STACK_PARM)
8796 target = 0;
8798 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8799 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8801 case TRUNC_MOD_EXPR:
8802 case FLOOR_MOD_EXPR:
8803 case CEIL_MOD_EXPR:
8804 case ROUND_MOD_EXPR:
8806 case TRUNC_DIV_EXPR:
8807 case FLOOR_DIV_EXPR:
8808 case CEIL_DIV_EXPR:
8809 case ROUND_DIV_EXPR:
8810 case EXACT_DIV_EXPR:
8812 /* If this is a fixed-point operation, then we cannot use the code
8813 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8814 divisions. */
8815 if (ALL_FIXED_POINT_MODE_P (mode))
8816 goto binop;
8818 if (modifier == EXPAND_STACK_PARM)
8819 target = 0;
8820 /* Possible optimization: compute the dividend with EXPAND_SUM
8821 then if the divisor is constant can optimize the case
8822 where some terms of the dividend have coeffs divisible by it. */
8823 expand_operands (treeop0, treeop1,
8824 subtarget, &op0, &op1, EXPAND_NORMAL);
8825 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8826 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8827 if (SCALAR_INT_MODE_P (mode)
8828 && optimize >= 2
8829 && get_range_pos_neg (treeop0) == 1
8830 && get_range_pos_neg (treeop1) == 1)
8832 /* If both arguments are known to be positive when interpreted
8833 as signed, we can expand it as both signed and unsigned
8834 division or modulo. Choose the cheaper sequence in that case. */
8835 bool speed_p = optimize_insn_for_speed_p ();
8836 do_pending_stack_adjust ();
8837 start_sequence ();
8838 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8839 rtx_insn *uns_insns = get_insns ();
8840 end_sequence ();
8841 start_sequence ();
8842 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8843 rtx_insn *sgn_insns = get_insns ();
8844 end_sequence ();
8845 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8846 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8848 /* If costs are the same then use as tie breaker the other
8849 other factor. */
8850 if (uns_cost == sgn_cost)
8852 uns_cost = seq_cost (uns_insns, !speed_p);
8853 sgn_cost = seq_cost (sgn_insns, !speed_p);
8856 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8858 emit_insn (uns_insns);
8859 return uns_ret;
8861 emit_insn (sgn_insns);
8862 return sgn_ret;
8864 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8866 case RDIV_EXPR:
8867 goto binop;
8869 case MULT_HIGHPART_EXPR:
8870 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8871 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8872 gcc_assert (temp);
8873 return temp;
8875 case FIXED_CONVERT_EXPR:
8876 op0 = expand_normal (treeop0);
8877 if (target == 0 || modifier == EXPAND_STACK_PARM)
8878 target = gen_reg_rtx (mode);
8880 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8881 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8882 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8883 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8884 else
8885 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8886 return target;
8888 case FIX_TRUNC_EXPR:
8889 op0 = expand_normal (treeop0);
8890 if (target == 0 || modifier == EXPAND_STACK_PARM)
8891 target = gen_reg_rtx (mode);
8892 expand_fix (target, op0, unsignedp);
8893 return target;
8895 case FLOAT_EXPR:
8896 op0 = expand_normal (treeop0);
8897 if (target == 0 || modifier == EXPAND_STACK_PARM)
8898 target = gen_reg_rtx (mode);
8899 /* expand_float can't figure out what to do if FROM has VOIDmode.
8900 So give it the correct mode. With -O, cse will optimize this. */
8901 if (GET_MODE (op0) == VOIDmode)
8902 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8903 op0);
8904 expand_float (target, op0,
8905 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8906 return target;
8908 case NEGATE_EXPR:
8909 op0 = expand_expr (treeop0, subtarget,
8910 VOIDmode, EXPAND_NORMAL);
8911 if (modifier == EXPAND_STACK_PARM)
8912 target = 0;
8913 temp = expand_unop (mode,
8914 optab_for_tree_code (NEGATE_EXPR, type,
8915 optab_default),
8916 op0, target, 0);
8917 gcc_assert (temp);
8918 return REDUCE_BIT_FIELD (temp);
8920 case ABS_EXPR:
8921 op0 = expand_expr (treeop0, subtarget,
8922 VOIDmode, EXPAND_NORMAL);
8923 if (modifier == EXPAND_STACK_PARM)
8924 target = 0;
8926 /* ABS_EXPR is not valid for complex arguments. */
8927 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8928 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8930 /* Unsigned abs is simply the operand. Testing here means we don't
8931 risk generating incorrect code below. */
8932 if (TYPE_UNSIGNED (type))
8933 return op0;
8935 return expand_abs (mode, op0, target, unsignedp,
8936 safe_from_p (target, treeop0, 1));
8938 case MAX_EXPR:
8939 case MIN_EXPR:
8940 target = original_target;
8941 if (target == 0
8942 || modifier == EXPAND_STACK_PARM
8943 || (MEM_P (target) && MEM_VOLATILE_P (target))
8944 || GET_MODE (target) != mode
8945 || (REG_P (target)
8946 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8947 target = gen_reg_rtx (mode);
8948 expand_operands (treeop0, treeop1,
8949 target, &op0, &op1, EXPAND_NORMAL);
8951 /* First try to do it with a special MIN or MAX instruction.
8952 If that does not win, use a conditional jump to select the proper
8953 value. */
8954 this_optab = optab_for_tree_code (code, type, optab_default);
8955 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8956 OPTAB_WIDEN);
8957 if (temp != 0)
8958 return temp;
8960 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8961 and similarly for MAX <x, y>. */
8962 if (VECTOR_TYPE_P (type))
8964 tree t0 = make_tree (type, op0);
8965 tree t1 = make_tree (type, op1);
8966 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8967 type, t0, t1);
8968 return expand_vec_cond_expr (type, comparison, t0, t1,
8969 original_target);
8972 /* At this point, a MEM target is no longer useful; we will get better
8973 code without it. */
8975 if (! REG_P (target))
8976 target = gen_reg_rtx (mode);
8978 /* If op1 was placed in target, swap op0 and op1. */
8979 if (target != op0 && target == op1)
8980 std::swap (op0, op1);
8982 /* We generate better code and avoid problems with op1 mentioning
8983 target by forcing op1 into a pseudo if it isn't a constant. */
8984 if (! CONSTANT_P (op1))
8985 op1 = force_reg (mode, op1);
8988 enum rtx_code comparison_code;
8989 rtx cmpop1 = op1;
8991 if (code == MAX_EXPR)
8992 comparison_code = unsignedp ? GEU : GE;
8993 else
8994 comparison_code = unsignedp ? LEU : LE;
8996 /* Canonicalize to comparisons against 0. */
8997 if (op1 == const1_rtx)
8999 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9000 or (a != 0 ? a : 1) for unsigned.
9001 For MIN we are safe converting (a <= 1 ? a : 1)
9002 into (a <= 0 ? a : 1) */
9003 cmpop1 = const0_rtx;
9004 if (code == MAX_EXPR)
9005 comparison_code = unsignedp ? NE : GT;
9007 if (op1 == constm1_rtx && !unsignedp)
9009 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9010 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9011 cmpop1 = const0_rtx;
9012 if (code == MIN_EXPR)
9013 comparison_code = LT;
9016 /* Use a conditional move if possible. */
9017 if (can_conditionally_move_p (mode))
9019 rtx insn;
9021 start_sequence ();
9023 /* Try to emit the conditional move. */
9024 insn = emit_conditional_move (target, comparison_code,
9025 op0, cmpop1, mode,
9026 op0, op1, mode,
9027 unsignedp);
9029 /* If we could do the conditional move, emit the sequence,
9030 and return. */
9031 if (insn)
9033 rtx_insn *seq = get_insns ();
9034 end_sequence ();
9035 emit_insn (seq);
9036 return target;
9039 /* Otherwise discard the sequence and fall back to code with
9040 branches. */
9041 end_sequence ();
9044 if (target != op0)
9045 emit_move_insn (target, op0);
9047 lab = gen_label_rtx ();
9048 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9049 unsignedp, mode, NULL_RTX, NULL, lab,
9050 profile_probability::uninitialized ());
9052 emit_move_insn (target, op1);
9053 emit_label (lab);
9054 return target;
9056 case BIT_NOT_EXPR:
9057 op0 = expand_expr (treeop0, subtarget,
9058 VOIDmode, EXPAND_NORMAL);
9059 if (modifier == EXPAND_STACK_PARM)
9060 target = 0;
9061 /* In case we have to reduce the result to bitfield precision
9062 for unsigned bitfield expand this as XOR with a proper constant
9063 instead. */
9064 if (reduce_bit_field && TYPE_UNSIGNED (type))
9066 int_mode = SCALAR_INT_TYPE_MODE (type);
9067 wide_int mask = wi::mask (TYPE_PRECISION (type),
9068 false, GET_MODE_PRECISION (int_mode));
9070 temp = expand_binop (int_mode, xor_optab, op0,
9071 immed_wide_int_const (mask, int_mode),
9072 target, 1, OPTAB_LIB_WIDEN);
9074 else
9075 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9076 gcc_assert (temp);
9077 return temp;
9079 /* ??? Can optimize bitwise operations with one arg constant.
9080 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9081 and (a bitwise1 b) bitwise2 b (etc)
9082 but that is probably not worth while. */
9084 case BIT_AND_EXPR:
9085 case BIT_IOR_EXPR:
9086 case BIT_XOR_EXPR:
9087 goto binop;
9089 case LROTATE_EXPR:
9090 case RROTATE_EXPR:
9091 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9092 || type_has_mode_precision_p (type));
9093 /* fall through */
9095 case LSHIFT_EXPR:
9096 case RSHIFT_EXPR:
9098 /* If this is a fixed-point operation, then we cannot use the code
9099 below because "expand_shift" doesn't support sat/no-sat fixed-point
9100 shifts. */
9101 if (ALL_FIXED_POINT_MODE_P (mode))
9102 goto binop;
9104 if (! safe_from_p (subtarget, treeop1, 1))
9105 subtarget = 0;
9106 if (modifier == EXPAND_STACK_PARM)
9107 target = 0;
9108 op0 = expand_expr (treeop0, subtarget,
9109 VOIDmode, EXPAND_NORMAL);
9111 /* Left shift optimization when shifting across word_size boundary.
9113 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9114 there isn't native instruction to support this wide mode
9115 left shift. Given below scenario:
9117 Type A = (Type) B << C
9119 |< T >|
9120 | dest_high | dest_low |
9122 | word_size |
9124 If the shift amount C caused we shift B to across the word
9125 size boundary, i.e part of B shifted into high half of
9126 destination register, and part of B remains in the low
9127 half, then GCC will use the following left shift expand
9128 logic:
9130 1. Initialize dest_low to B.
9131 2. Initialize every bit of dest_high to the sign bit of B.
9132 3. Logic left shift dest_low by C bit to finalize dest_low.
9133 The value of dest_low before this shift is kept in a temp D.
9134 4. Logic left shift dest_high by C.
9135 5. Logic right shift D by (word_size - C).
9136 6. Or the result of 4 and 5 to finalize dest_high.
9138 While, by checking gimple statements, if operand B is
9139 coming from signed extension, then we can simplify above
9140 expand logic into:
9142 1. dest_high = src_low >> (word_size - C).
9143 2. dest_low = src_low << C.
9145 We can use one arithmetic right shift to finish all the
9146 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9147 needed from 6 into 2.
9149 The case is similar for zero extension, except that we
9150 initialize dest_high to zero rather than copies of the sign
9151 bit from B. Furthermore, we need to use a logical right shift
9152 in this case.
9154 The choice of sign-extension versus zero-extension is
9155 determined entirely by whether or not B is signed and is
9156 independent of the current setting of unsignedp. */
9158 temp = NULL_RTX;
9159 if (code == LSHIFT_EXPR
9160 && target
9161 && REG_P (target)
9162 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9163 && mode == int_mode
9164 && TREE_CONSTANT (treeop1)
9165 && TREE_CODE (treeop0) == SSA_NAME)
9167 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9168 if (is_gimple_assign (def)
9169 && gimple_assign_rhs_code (def) == NOP_EXPR)
9171 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9172 (TREE_TYPE (gimple_assign_rhs1 (def)));
9174 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9175 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9176 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9177 >= GET_MODE_BITSIZE (word_mode)))
9179 rtx_insn *seq, *seq_old;
9180 unsigned int high_off = subreg_highpart_offset (word_mode,
9181 int_mode);
9182 bool extend_unsigned
9183 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9184 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9185 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9186 rtx dest_high = simplify_gen_subreg (word_mode, target,
9187 int_mode, high_off);
9188 HOST_WIDE_INT ramount = (BITS_PER_WORD
9189 - TREE_INT_CST_LOW (treeop1));
9190 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9192 start_sequence ();
9193 /* dest_high = src_low >> (word_size - C). */
9194 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9195 rshift, dest_high,
9196 extend_unsigned);
9197 if (temp != dest_high)
9198 emit_move_insn (dest_high, temp);
9200 /* dest_low = src_low << C. */
9201 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9202 treeop1, dest_low, unsignedp);
9203 if (temp != dest_low)
9204 emit_move_insn (dest_low, temp);
9206 seq = get_insns ();
9207 end_sequence ();
9208 temp = target ;
9210 if (have_insn_for (ASHIFT, int_mode))
9212 bool speed_p = optimize_insn_for_speed_p ();
9213 start_sequence ();
9214 rtx ret_old = expand_variable_shift (code, int_mode,
9215 op0, treeop1,
9216 target,
9217 unsignedp);
9219 seq_old = get_insns ();
9220 end_sequence ();
9221 if (seq_cost (seq, speed_p)
9222 >= seq_cost (seq_old, speed_p))
9224 seq = seq_old;
9225 temp = ret_old;
9228 emit_insn (seq);
9233 if (temp == NULL_RTX)
9234 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9235 unsignedp);
9236 if (code == LSHIFT_EXPR)
9237 temp = REDUCE_BIT_FIELD (temp);
9238 return temp;
9241 /* Could determine the answer when only additive constants differ. Also,
9242 the addition of one can be handled by changing the condition. */
9243 case LT_EXPR:
9244 case LE_EXPR:
9245 case GT_EXPR:
9246 case GE_EXPR:
9247 case EQ_EXPR:
9248 case NE_EXPR:
9249 case UNORDERED_EXPR:
9250 case ORDERED_EXPR:
9251 case UNLT_EXPR:
9252 case UNLE_EXPR:
9253 case UNGT_EXPR:
9254 case UNGE_EXPR:
9255 case UNEQ_EXPR:
9256 case LTGT_EXPR:
9258 temp = do_store_flag (ops,
9259 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9260 tmode != VOIDmode ? tmode : mode);
9261 if (temp)
9262 return temp;
9264 /* Use a compare and a jump for BLKmode comparisons, or for function
9265 type comparisons is have_canonicalize_funcptr_for_compare. */
9267 if ((target == 0
9268 || modifier == EXPAND_STACK_PARM
9269 || ! safe_from_p (target, treeop0, 1)
9270 || ! safe_from_p (target, treeop1, 1)
9271 /* Make sure we don't have a hard reg (such as function's return
9272 value) live across basic blocks, if not optimizing. */
9273 || (!optimize && REG_P (target)
9274 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9275 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9277 emit_move_insn (target, const0_rtx);
9279 rtx_code_label *lab1 = gen_label_rtx ();
9280 jumpifnot_1 (code, treeop0, treeop1, lab1,
9281 profile_probability::uninitialized ());
9283 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9284 emit_move_insn (target, constm1_rtx);
9285 else
9286 emit_move_insn (target, const1_rtx);
9288 emit_label (lab1);
9289 return target;
9291 case COMPLEX_EXPR:
9292 /* Get the rtx code of the operands. */
9293 op0 = expand_normal (treeop0);
9294 op1 = expand_normal (treeop1);
9296 if (!target)
9297 target = gen_reg_rtx (TYPE_MODE (type));
9298 else
9299 /* If target overlaps with op1, then either we need to force
9300 op1 into a pseudo (if target also overlaps with op0),
9301 or write the complex parts in reverse order. */
9302 switch (GET_CODE (target))
9304 case CONCAT:
9305 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9307 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9309 complex_expr_force_op1:
9310 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9311 emit_move_insn (temp, op1);
9312 op1 = temp;
9313 break;
9315 complex_expr_swap_order:
9316 /* Move the imaginary (op1) and real (op0) parts to their
9317 location. */
9318 write_complex_part (target, op1, true);
9319 write_complex_part (target, op0, false);
9321 return target;
9323 break;
9324 case MEM:
9325 temp = adjust_address_nv (target,
9326 GET_MODE_INNER (GET_MODE (target)), 0);
9327 if (reg_overlap_mentioned_p (temp, op1))
9329 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9330 temp = adjust_address_nv (target, imode,
9331 GET_MODE_SIZE (imode));
9332 if (reg_overlap_mentioned_p (temp, op0))
9333 goto complex_expr_force_op1;
9334 goto complex_expr_swap_order;
9336 break;
9337 default:
9338 if (reg_overlap_mentioned_p (target, op1))
9340 if (reg_overlap_mentioned_p (target, op0))
9341 goto complex_expr_force_op1;
9342 goto complex_expr_swap_order;
9344 break;
9347 /* Move the real (op0) and imaginary (op1) parts to their location. */
9348 write_complex_part (target, op0, false);
9349 write_complex_part (target, op1, true);
9351 return target;
9353 case WIDEN_SUM_EXPR:
9355 tree oprnd0 = treeop0;
9356 tree oprnd1 = treeop1;
9358 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9359 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9360 target, unsignedp);
9361 return target;
9364 case REDUC_MAX_EXPR:
9365 case REDUC_MIN_EXPR:
9366 case REDUC_PLUS_EXPR:
9368 op0 = expand_normal (treeop0);
9369 this_optab = optab_for_tree_code (code, type, optab_default);
9370 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9372 struct expand_operand ops[2];
9373 enum insn_code icode = optab_handler (this_optab, vec_mode);
9375 create_output_operand (&ops[0], target, mode);
9376 create_input_operand (&ops[1], op0, vec_mode);
9377 expand_insn (icode, 2, ops);
9378 target = ops[0].value;
9379 if (GET_MODE (target) != mode)
9380 return gen_lowpart (tmode, target);
9381 return target;
9384 case VEC_UNPACK_HI_EXPR:
9385 case VEC_UNPACK_LO_EXPR:
9387 op0 = expand_normal (treeop0);
9388 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9389 target, unsignedp);
9390 gcc_assert (temp);
9391 return temp;
9394 case VEC_UNPACK_FLOAT_HI_EXPR:
9395 case VEC_UNPACK_FLOAT_LO_EXPR:
9397 op0 = expand_normal (treeop0);
9398 /* The signedness is determined from input operand. */
9399 temp = expand_widen_pattern_expr
9400 (ops, op0, NULL_RTX, NULL_RTX,
9401 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9403 gcc_assert (temp);
9404 return temp;
9407 case VEC_WIDEN_MULT_HI_EXPR:
9408 case VEC_WIDEN_MULT_LO_EXPR:
9409 case VEC_WIDEN_MULT_EVEN_EXPR:
9410 case VEC_WIDEN_MULT_ODD_EXPR:
9411 case VEC_WIDEN_LSHIFT_HI_EXPR:
9412 case VEC_WIDEN_LSHIFT_LO_EXPR:
9413 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9414 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9415 target, unsignedp);
9416 gcc_assert (target);
9417 return target;
9419 case VEC_PACK_TRUNC_EXPR:
9420 case VEC_PACK_SAT_EXPR:
9421 case VEC_PACK_FIX_TRUNC_EXPR:
9422 mode = TYPE_MODE (TREE_TYPE (treeop0));
9423 goto binop;
9425 case VEC_PERM_EXPR:
9426 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9427 op2 = expand_normal (treeop2);
9429 /* Careful here: if the target doesn't support integral vector modes,
9430 a constant selection vector could wind up smooshed into a normal
9431 integral constant. */
9432 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9434 tree sel_type = TREE_TYPE (treeop2);
9435 machine_mode vmode
9436 = mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (sel_type)),
9437 TYPE_VECTOR_SUBPARTS (sel_type)).require ();
9438 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9439 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9440 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9442 else
9443 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9445 temp = expand_vec_perm (mode, op0, op1, op2, target);
9446 gcc_assert (temp);
9447 return temp;
9449 case DOT_PROD_EXPR:
9451 tree oprnd0 = treeop0;
9452 tree oprnd1 = treeop1;
9453 tree oprnd2 = treeop2;
9454 rtx op2;
9456 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9457 op2 = expand_normal (oprnd2);
9458 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9459 target, unsignedp);
9460 return target;
9463 case SAD_EXPR:
9465 tree oprnd0 = treeop0;
9466 tree oprnd1 = treeop1;
9467 tree oprnd2 = treeop2;
9468 rtx op2;
9470 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9471 op2 = expand_normal (oprnd2);
9472 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9473 target, unsignedp);
9474 return target;
9477 case REALIGN_LOAD_EXPR:
9479 tree oprnd0 = treeop0;
9480 tree oprnd1 = treeop1;
9481 tree oprnd2 = treeop2;
9482 rtx op2;
9484 this_optab = optab_for_tree_code (code, type, optab_default);
9485 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9486 op2 = expand_normal (oprnd2);
9487 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9488 target, unsignedp);
9489 gcc_assert (temp);
9490 return temp;
9493 case COND_EXPR:
9495 /* A COND_EXPR with its type being VOID_TYPE represents a
9496 conditional jump and is handled in
9497 expand_gimple_cond_expr. */
9498 gcc_assert (!VOID_TYPE_P (type));
9500 /* Note that COND_EXPRs whose type is a structure or union
9501 are required to be constructed to contain assignments of
9502 a temporary variable, so that we can evaluate them here
9503 for side effect only. If type is void, we must do likewise. */
9505 gcc_assert (!TREE_ADDRESSABLE (type)
9506 && !ignore
9507 && TREE_TYPE (treeop1) != void_type_node
9508 && TREE_TYPE (treeop2) != void_type_node);
9510 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9511 if (temp)
9512 return temp;
9514 /* If we are not to produce a result, we have no target. Otherwise,
9515 if a target was specified use it; it will not be used as an
9516 intermediate target unless it is safe. If no target, use a
9517 temporary. */
9519 if (modifier != EXPAND_STACK_PARM
9520 && original_target
9521 && safe_from_p (original_target, treeop0, 1)
9522 && GET_MODE (original_target) == mode
9523 && !MEM_P (original_target))
9524 temp = original_target;
9525 else
9526 temp = assign_temp (type, 0, 1);
9528 do_pending_stack_adjust ();
9529 NO_DEFER_POP;
9530 rtx_code_label *lab0 = gen_label_rtx ();
9531 rtx_code_label *lab1 = gen_label_rtx ();
9532 jumpifnot (treeop0, lab0,
9533 profile_probability::uninitialized ());
9534 store_expr (treeop1, temp,
9535 modifier == EXPAND_STACK_PARM,
9536 false, false);
9538 emit_jump_insn (targetm.gen_jump (lab1));
9539 emit_barrier ();
9540 emit_label (lab0);
9541 store_expr (treeop2, temp,
9542 modifier == EXPAND_STACK_PARM,
9543 false, false);
9545 emit_label (lab1);
9546 OK_DEFER_POP;
9547 return temp;
9550 case VEC_COND_EXPR:
9551 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9552 return target;
9554 case BIT_INSERT_EXPR:
9556 unsigned bitpos = tree_to_uhwi (treeop2);
9557 unsigned bitsize;
9558 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9559 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9560 else
9561 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9562 rtx op0 = expand_normal (treeop0);
9563 rtx op1 = expand_normal (treeop1);
9564 rtx dst = gen_reg_rtx (mode);
9565 emit_move_insn (dst, op0);
9566 store_bit_field (dst, bitsize, bitpos, 0, 0,
9567 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9568 return dst;
9571 default:
9572 gcc_unreachable ();
9575 /* Here to do an ordinary binary operator. */
9576 binop:
9577 expand_operands (treeop0, treeop1,
9578 subtarget, &op0, &op1, EXPAND_NORMAL);
9579 binop2:
9580 this_optab = optab_for_tree_code (code, type, optab_default);
9581 binop3:
9582 if (modifier == EXPAND_STACK_PARM)
9583 target = 0;
9584 temp = expand_binop (mode, this_optab, op0, op1, target,
9585 unsignedp, OPTAB_LIB_WIDEN);
9586 gcc_assert (temp);
9587 /* Bitwise operations do not need bitfield reduction as we expect their
9588 operands being properly truncated. */
9589 if (code == BIT_XOR_EXPR
9590 || code == BIT_AND_EXPR
9591 || code == BIT_IOR_EXPR)
9592 return temp;
9593 return REDUCE_BIT_FIELD (temp);
9595 #undef REDUCE_BIT_FIELD
9598 /* Return TRUE if expression STMT is suitable for replacement.
9599 Never consider memory loads as replaceable, because those don't ever lead
9600 into constant expressions. */
9602 static bool
9603 stmt_is_replaceable_p (gimple *stmt)
9605 if (ssa_is_replaceable_p (stmt))
9607 /* Don't move around loads. */
9608 if (!gimple_assign_single_p (stmt)
9609 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9610 return true;
9612 return false;
9616 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9617 enum expand_modifier modifier, rtx *alt_rtl,
9618 bool inner_reference_p)
9620 rtx op0, op1, temp, decl_rtl;
9621 tree type;
9622 int unsignedp;
9623 machine_mode mode, dmode;
9624 enum tree_code code = TREE_CODE (exp);
9625 rtx subtarget, original_target;
9626 int ignore;
9627 tree context;
9628 bool reduce_bit_field;
9629 location_t loc = EXPR_LOCATION (exp);
9630 struct separate_ops ops;
9631 tree treeop0, treeop1, treeop2;
9632 tree ssa_name = NULL_TREE;
9633 gimple *g;
9635 type = TREE_TYPE (exp);
9636 mode = TYPE_MODE (type);
9637 unsignedp = TYPE_UNSIGNED (type);
9639 treeop0 = treeop1 = treeop2 = NULL_TREE;
9640 if (!VL_EXP_CLASS_P (exp))
9641 switch (TREE_CODE_LENGTH (code))
9643 default:
9644 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9645 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9646 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9647 case 0: break;
9649 ops.code = code;
9650 ops.type = type;
9651 ops.op0 = treeop0;
9652 ops.op1 = treeop1;
9653 ops.op2 = treeop2;
9654 ops.location = loc;
9656 ignore = (target == const0_rtx
9657 || ((CONVERT_EXPR_CODE_P (code)
9658 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9659 && TREE_CODE (type) == VOID_TYPE));
9661 /* An operation in what may be a bit-field type needs the
9662 result to be reduced to the precision of the bit-field type,
9663 which is narrower than that of the type's mode. */
9664 reduce_bit_field = (!ignore
9665 && INTEGRAL_TYPE_P (type)
9666 && !type_has_mode_precision_p (type));
9668 /* If we are going to ignore this result, we need only do something
9669 if there is a side-effect somewhere in the expression. If there
9670 is, short-circuit the most common cases here. Note that we must
9671 not call expand_expr with anything but const0_rtx in case this
9672 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9674 if (ignore)
9676 if (! TREE_SIDE_EFFECTS (exp))
9677 return const0_rtx;
9679 /* Ensure we reference a volatile object even if value is ignored, but
9680 don't do this if all we are doing is taking its address. */
9681 if (TREE_THIS_VOLATILE (exp)
9682 && TREE_CODE (exp) != FUNCTION_DECL
9683 && mode != VOIDmode && mode != BLKmode
9684 && modifier != EXPAND_CONST_ADDRESS)
9686 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9687 if (MEM_P (temp))
9688 copy_to_reg (temp);
9689 return const0_rtx;
9692 if (TREE_CODE_CLASS (code) == tcc_unary
9693 || code == BIT_FIELD_REF
9694 || code == COMPONENT_REF
9695 || code == INDIRECT_REF)
9696 return expand_expr (treeop0, const0_rtx, VOIDmode,
9697 modifier);
9699 else if (TREE_CODE_CLASS (code) == tcc_binary
9700 || TREE_CODE_CLASS (code) == tcc_comparison
9701 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9703 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9704 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9705 return const0_rtx;
9708 target = 0;
9711 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9712 target = 0;
9714 /* Use subtarget as the target for operand 0 of a binary operation. */
9715 subtarget = get_subtarget (target);
9716 original_target = target;
9718 switch (code)
9720 case LABEL_DECL:
9722 tree function = decl_function_context (exp);
9724 temp = label_rtx (exp);
9725 temp = gen_rtx_LABEL_REF (Pmode, temp);
9727 if (function != current_function_decl
9728 && function != 0)
9729 LABEL_REF_NONLOCAL_P (temp) = 1;
9731 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9732 return temp;
9735 case SSA_NAME:
9736 /* ??? ivopts calls expander, without any preparation from
9737 out-of-ssa. So fake instructions as if this was an access to the
9738 base variable. This unnecessarily allocates a pseudo, see how we can
9739 reuse it, if partition base vars have it set already. */
9740 if (!currently_expanding_to_rtl)
9742 tree var = SSA_NAME_VAR (exp);
9743 if (var && DECL_RTL_SET_P (var))
9744 return DECL_RTL (var);
9745 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9746 LAST_VIRTUAL_REGISTER + 1);
9749 g = get_gimple_for_ssa_name (exp);
9750 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9751 if (g == NULL
9752 && modifier == EXPAND_INITIALIZER
9753 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9754 && (optimize || !SSA_NAME_VAR (exp)
9755 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9756 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9757 g = SSA_NAME_DEF_STMT (exp);
9758 if (g)
9760 rtx r;
9761 location_t saved_loc = curr_insn_location ();
9762 location_t loc = gimple_location (g);
9763 if (loc != UNKNOWN_LOCATION)
9764 set_curr_insn_location (loc);
9765 ops.code = gimple_assign_rhs_code (g);
9766 switch (get_gimple_rhs_class (ops.code))
9768 case GIMPLE_TERNARY_RHS:
9769 ops.op2 = gimple_assign_rhs3 (g);
9770 /* Fallthru */
9771 case GIMPLE_BINARY_RHS:
9772 ops.op1 = gimple_assign_rhs2 (g);
9774 /* Try to expand conditonal compare. */
9775 if (targetm.gen_ccmp_first)
9777 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9778 r = expand_ccmp_expr (g, mode);
9779 if (r)
9780 break;
9782 /* Fallthru */
9783 case GIMPLE_UNARY_RHS:
9784 ops.op0 = gimple_assign_rhs1 (g);
9785 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9786 ops.location = loc;
9787 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9788 break;
9789 case GIMPLE_SINGLE_RHS:
9791 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9792 tmode, modifier, alt_rtl,
9793 inner_reference_p);
9794 break;
9796 default:
9797 gcc_unreachable ();
9799 set_curr_insn_location (saved_loc);
9800 if (REG_P (r) && !REG_EXPR (r))
9801 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9802 return r;
9805 ssa_name = exp;
9806 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9807 exp = SSA_NAME_VAR (ssa_name);
9808 goto expand_decl_rtl;
9810 case PARM_DECL:
9811 case VAR_DECL:
9812 /* If a static var's type was incomplete when the decl was written,
9813 but the type is complete now, lay out the decl now. */
9814 if (DECL_SIZE (exp) == 0
9815 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9816 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9817 layout_decl (exp, 0);
9819 /* fall through */
9821 case FUNCTION_DECL:
9822 case RESULT_DECL:
9823 decl_rtl = DECL_RTL (exp);
9824 expand_decl_rtl:
9825 gcc_assert (decl_rtl);
9827 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9828 settings for VECTOR_TYPE_P that might switch for the function. */
9829 if (currently_expanding_to_rtl
9830 && code == VAR_DECL && MEM_P (decl_rtl)
9831 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9832 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9833 else
9834 decl_rtl = copy_rtx (decl_rtl);
9836 /* Record writes to register variables. */
9837 if (modifier == EXPAND_WRITE
9838 && REG_P (decl_rtl)
9839 && HARD_REGISTER_P (decl_rtl))
9840 add_to_hard_reg_set (&crtl->asm_clobbers,
9841 GET_MODE (decl_rtl), REGNO (decl_rtl));
9843 /* Ensure variable marked as used even if it doesn't go through
9844 a parser. If it hasn't be used yet, write out an external
9845 definition. */
9846 if (exp)
9847 TREE_USED (exp) = 1;
9849 /* Show we haven't gotten RTL for this yet. */
9850 temp = 0;
9852 /* Variables inherited from containing functions should have
9853 been lowered by this point. */
9854 if (exp)
9855 context = decl_function_context (exp);
9856 gcc_assert (!exp
9857 || SCOPE_FILE_SCOPE_P (context)
9858 || context == current_function_decl
9859 || TREE_STATIC (exp)
9860 || DECL_EXTERNAL (exp)
9861 /* ??? C++ creates functions that are not TREE_STATIC. */
9862 || TREE_CODE (exp) == FUNCTION_DECL);
9864 /* This is the case of an array whose size is to be determined
9865 from its initializer, while the initializer is still being parsed.
9866 ??? We aren't parsing while expanding anymore. */
9868 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9869 temp = validize_mem (decl_rtl);
9871 /* If DECL_RTL is memory, we are in the normal case and the
9872 address is not valid, get the address into a register. */
9874 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9876 if (alt_rtl)
9877 *alt_rtl = decl_rtl;
9878 decl_rtl = use_anchored_address (decl_rtl);
9879 if (modifier != EXPAND_CONST_ADDRESS
9880 && modifier != EXPAND_SUM
9881 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9882 : GET_MODE (decl_rtl),
9883 XEXP (decl_rtl, 0),
9884 MEM_ADDR_SPACE (decl_rtl)))
9885 temp = replace_equiv_address (decl_rtl,
9886 copy_rtx (XEXP (decl_rtl, 0)));
9889 /* If we got something, return it. But first, set the alignment
9890 if the address is a register. */
9891 if (temp != 0)
9893 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9894 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9896 return temp;
9899 if (exp)
9900 dmode = DECL_MODE (exp);
9901 else
9902 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9904 /* If the mode of DECL_RTL does not match that of the decl,
9905 there are two cases: we are dealing with a BLKmode value
9906 that is returned in a register, or we are dealing with
9907 a promoted value. In the latter case, return a SUBREG
9908 of the wanted mode, but mark it so that we know that it
9909 was already extended. */
9910 if (REG_P (decl_rtl)
9911 && dmode != BLKmode
9912 && GET_MODE (decl_rtl) != dmode)
9914 machine_mode pmode;
9916 /* Get the signedness to be used for this variable. Ensure we get
9917 the same mode we got when the variable was declared. */
9918 if (code != SSA_NAME)
9919 pmode = promote_decl_mode (exp, &unsignedp);
9920 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9921 && gimple_code (g) == GIMPLE_CALL
9922 && !gimple_call_internal_p (g))
9923 pmode = promote_function_mode (type, mode, &unsignedp,
9924 gimple_call_fntype (g),
9926 else
9927 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9928 gcc_assert (GET_MODE (decl_rtl) == pmode);
9930 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9931 SUBREG_PROMOTED_VAR_P (temp) = 1;
9932 SUBREG_PROMOTED_SET (temp, unsignedp);
9933 return temp;
9936 return decl_rtl;
9938 case INTEGER_CST:
9940 /* Given that TYPE_PRECISION (type) is not always equal to
9941 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9942 the former to the latter according to the signedness of the
9943 type. */
9944 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
9945 temp = immed_wide_int_const
9946 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
9947 return temp;
9950 case VECTOR_CST:
9952 tree tmp = NULL_TREE;
9953 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9954 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9955 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9956 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9957 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9958 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9959 return const_vector_from_tree (exp);
9960 scalar_int_mode int_mode;
9961 if (is_int_mode (mode, &int_mode))
9963 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9964 return const_scalar_mask_from_tree (int_mode, exp);
9965 else
9967 tree type_for_mode
9968 = lang_hooks.types.type_for_mode (int_mode, 1);
9969 if (type_for_mode)
9970 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9971 type_for_mode, exp);
9974 if (!tmp)
9976 vec<constructor_elt, va_gc> *v;
9977 unsigned i;
9978 vec_alloc (v, VECTOR_CST_NELTS (exp));
9979 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9980 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9981 tmp = build_constructor (type, v);
9983 return expand_expr (tmp, ignore ? const0_rtx : target,
9984 tmode, modifier);
9987 case CONST_DECL:
9988 if (modifier == EXPAND_WRITE)
9990 /* Writing into CONST_DECL is always invalid, but handle it
9991 gracefully. */
9992 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9993 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
9994 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9995 EXPAND_NORMAL, as);
9996 op0 = memory_address_addr_space (mode, op0, as);
9997 temp = gen_rtx_MEM (mode, op0);
9998 set_mem_addr_space (temp, as);
9999 return temp;
10001 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10003 case REAL_CST:
10004 /* If optimized, generate immediate CONST_DOUBLE
10005 which will be turned into memory by reload if necessary.
10007 We used to force a register so that loop.c could see it. But
10008 this does not allow gen_* patterns to perform optimizations with
10009 the constants. It also produces two insns in cases like "x = 1.0;".
10010 On most machines, floating-point constants are not permitted in
10011 many insns, so we'd end up copying it to a register in any case.
10013 Now, we do the copying in expand_binop, if appropriate. */
10014 return const_double_from_real_value (TREE_REAL_CST (exp),
10015 TYPE_MODE (TREE_TYPE (exp)));
10017 case FIXED_CST:
10018 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10019 TYPE_MODE (TREE_TYPE (exp)));
10021 case COMPLEX_CST:
10022 /* Handle evaluating a complex constant in a CONCAT target. */
10023 if (original_target && GET_CODE (original_target) == CONCAT)
10025 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10026 rtx rtarg, itarg;
10028 rtarg = XEXP (original_target, 0);
10029 itarg = XEXP (original_target, 1);
10031 /* Move the real and imaginary parts separately. */
10032 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10033 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10035 if (op0 != rtarg)
10036 emit_move_insn (rtarg, op0);
10037 if (op1 != itarg)
10038 emit_move_insn (itarg, op1);
10040 return original_target;
10043 /* fall through */
10045 case STRING_CST:
10046 temp = expand_expr_constant (exp, 1, modifier);
10048 /* temp contains a constant address.
10049 On RISC machines where a constant address isn't valid,
10050 make some insns to get that address into a register. */
10051 if (modifier != EXPAND_CONST_ADDRESS
10052 && modifier != EXPAND_INITIALIZER
10053 && modifier != EXPAND_SUM
10054 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10055 MEM_ADDR_SPACE (temp)))
10056 return replace_equiv_address (temp,
10057 copy_rtx (XEXP (temp, 0)));
10058 return temp;
10060 case SAVE_EXPR:
10062 tree val = treeop0;
10063 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10064 inner_reference_p);
10066 if (!SAVE_EXPR_RESOLVED_P (exp))
10068 /* We can indeed still hit this case, typically via builtin
10069 expanders calling save_expr immediately before expanding
10070 something. Assume this means that we only have to deal
10071 with non-BLKmode values. */
10072 gcc_assert (GET_MODE (ret) != BLKmode);
10074 val = build_decl (curr_insn_location (),
10075 VAR_DECL, NULL, TREE_TYPE (exp));
10076 DECL_ARTIFICIAL (val) = 1;
10077 DECL_IGNORED_P (val) = 1;
10078 treeop0 = val;
10079 TREE_OPERAND (exp, 0) = treeop0;
10080 SAVE_EXPR_RESOLVED_P (exp) = 1;
10082 if (!CONSTANT_P (ret))
10083 ret = copy_to_reg (ret);
10084 SET_DECL_RTL (val, ret);
10087 return ret;
10091 case CONSTRUCTOR:
10092 /* If we don't need the result, just ensure we evaluate any
10093 subexpressions. */
10094 if (ignore)
10096 unsigned HOST_WIDE_INT idx;
10097 tree value;
10099 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10100 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10102 return const0_rtx;
10105 return expand_constructor (exp, target, modifier, false);
10107 case TARGET_MEM_REF:
10109 addr_space_t as
10110 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10111 enum insn_code icode;
10112 unsigned int align;
10114 op0 = addr_for_mem_ref (exp, as, true);
10115 op0 = memory_address_addr_space (mode, op0, as);
10116 temp = gen_rtx_MEM (mode, op0);
10117 set_mem_attributes (temp, exp, 0);
10118 set_mem_addr_space (temp, as);
10119 align = get_object_alignment (exp);
10120 if (modifier != EXPAND_WRITE
10121 && modifier != EXPAND_MEMORY
10122 && mode != BLKmode
10123 && align < GET_MODE_ALIGNMENT (mode)
10124 /* If the target does not have special handling for unaligned
10125 loads of mode then it can use regular moves for them. */
10126 && ((icode = optab_handler (movmisalign_optab, mode))
10127 != CODE_FOR_nothing))
10129 struct expand_operand ops[2];
10131 /* We've already validated the memory, and we're creating a
10132 new pseudo destination. The predicates really can't fail,
10133 nor can the generator. */
10134 create_output_operand (&ops[0], NULL_RTX, mode);
10135 create_fixed_operand (&ops[1], temp);
10136 expand_insn (icode, 2, ops);
10137 temp = ops[0].value;
10139 return temp;
10142 case MEM_REF:
10144 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10145 addr_space_t as
10146 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10147 machine_mode address_mode;
10148 tree base = TREE_OPERAND (exp, 0);
10149 gimple *def_stmt;
10150 enum insn_code icode;
10151 unsigned align;
10152 /* Handle expansion of non-aliased memory with non-BLKmode. That
10153 might end up in a register. */
10154 if (mem_ref_refers_to_non_mem_p (exp))
10156 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10157 base = TREE_OPERAND (base, 0);
10158 if (offset == 0
10159 && !reverse
10160 && tree_fits_uhwi_p (TYPE_SIZE (type))
10161 && (GET_MODE_BITSIZE (DECL_MODE (base))
10162 == tree_to_uhwi (TYPE_SIZE (type))))
10163 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10164 target, tmode, modifier);
10165 if (TYPE_MODE (type) == BLKmode)
10167 temp = assign_stack_temp (DECL_MODE (base),
10168 GET_MODE_SIZE (DECL_MODE (base)));
10169 store_expr (base, temp, 0, false, false);
10170 temp = adjust_address (temp, BLKmode, offset);
10171 set_mem_size (temp, int_size_in_bytes (type));
10172 return temp;
10174 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10175 bitsize_int (offset * BITS_PER_UNIT));
10176 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10177 return expand_expr (exp, target, tmode, modifier);
10179 address_mode = targetm.addr_space.address_mode (as);
10180 base = TREE_OPERAND (exp, 0);
10181 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10183 tree mask = gimple_assign_rhs2 (def_stmt);
10184 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10185 gimple_assign_rhs1 (def_stmt), mask);
10186 TREE_OPERAND (exp, 0) = base;
10188 align = get_object_alignment (exp);
10189 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10190 op0 = memory_address_addr_space (mode, op0, as);
10191 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10193 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10194 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10195 op0 = memory_address_addr_space (mode, op0, as);
10197 temp = gen_rtx_MEM (mode, op0);
10198 set_mem_attributes (temp, exp, 0);
10199 set_mem_addr_space (temp, as);
10200 if (TREE_THIS_VOLATILE (exp))
10201 MEM_VOLATILE_P (temp) = 1;
10202 if (modifier != EXPAND_WRITE
10203 && modifier != EXPAND_MEMORY
10204 && !inner_reference_p
10205 && mode != BLKmode
10206 && align < GET_MODE_ALIGNMENT (mode))
10208 if ((icode = optab_handler (movmisalign_optab, mode))
10209 != CODE_FOR_nothing)
10211 struct expand_operand ops[2];
10213 /* We've already validated the memory, and we're creating a
10214 new pseudo destination. The predicates really can't fail,
10215 nor can the generator. */
10216 create_output_operand (&ops[0], NULL_RTX, mode);
10217 create_fixed_operand (&ops[1], temp);
10218 expand_insn (icode, 2, ops);
10219 temp = ops[0].value;
10221 else if (targetm.slow_unaligned_access (mode, align))
10222 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10223 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10224 (modifier == EXPAND_STACK_PARM
10225 ? NULL_RTX : target),
10226 mode, mode, false, alt_rtl);
10228 if (reverse
10229 && modifier != EXPAND_MEMORY
10230 && modifier != EXPAND_WRITE)
10231 temp = flip_storage_order (mode, temp);
10232 return temp;
10235 case ARRAY_REF:
10238 tree array = treeop0;
10239 tree index = treeop1;
10240 tree init;
10242 /* Fold an expression like: "foo"[2].
10243 This is not done in fold so it won't happen inside &.
10244 Don't fold if this is for wide characters since it's too
10245 difficult to do correctly and this is a very rare case. */
10247 if (modifier != EXPAND_CONST_ADDRESS
10248 && modifier != EXPAND_INITIALIZER
10249 && modifier != EXPAND_MEMORY)
10251 tree t = fold_read_from_constant_string (exp);
10253 if (t)
10254 return expand_expr (t, target, tmode, modifier);
10257 /* If this is a constant index into a constant array,
10258 just get the value from the array. Handle both the cases when
10259 we have an explicit constructor and when our operand is a variable
10260 that was declared const. */
10262 if (modifier != EXPAND_CONST_ADDRESS
10263 && modifier != EXPAND_INITIALIZER
10264 && modifier != EXPAND_MEMORY
10265 && TREE_CODE (array) == CONSTRUCTOR
10266 && ! TREE_SIDE_EFFECTS (array)
10267 && TREE_CODE (index) == INTEGER_CST)
10269 unsigned HOST_WIDE_INT ix;
10270 tree field, value;
10272 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10273 field, value)
10274 if (tree_int_cst_equal (field, index))
10276 if (!TREE_SIDE_EFFECTS (value))
10277 return expand_expr (fold (value), target, tmode, modifier);
10278 break;
10282 else if (optimize >= 1
10283 && modifier != EXPAND_CONST_ADDRESS
10284 && modifier != EXPAND_INITIALIZER
10285 && modifier != EXPAND_MEMORY
10286 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10287 && TREE_CODE (index) == INTEGER_CST
10288 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10289 && (init = ctor_for_folding (array)) != error_mark_node)
10291 if (init == NULL_TREE)
10293 tree value = build_zero_cst (type);
10294 if (TREE_CODE (value) == CONSTRUCTOR)
10296 /* If VALUE is a CONSTRUCTOR, this optimization is only
10297 useful if this doesn't store the CONSTRUCTOR into
10298 memory. If it does, it is more efficient to just
10299 load the data from the array directly. */
10300 rtx ret = expand_constructor (value, target,
10301 modifier, true);
10302 if (ret == NULL_RTX)
10303 value = NULL_TREE;
10306 if (value)
10307 return expand_expr (value, target, tmode, modifier);
10309 else if (TREE_CODE (init) == CONSTRUCTOR)
10311 unsigned HOST_WIDE_INT ix;
10312 tree field, value;
10314 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10315 field, value)
10316 if (tree_int_cst_equal (field, index))
10318 if (TREE_SIDE_EFFECTS (value))
10319 break;
10321 if (TREE_CODE (value) == CONSTRUCTOR)
10323 /* If VALUE is a CONSTRUCTOR, this
10324 optimization is only useful if
10325 this doesn't store the CONSTRUCTOR
10326 into memory. If it does, it is more
10327 efficient to just load the data from
10328 the array directly. */
10329 rtx ret = expand_constructor (value, target,
10330 modifier, true);
10331 if (ret == NULL_RTX)
10332 break;
10335 return
10336 expand_expr (fold (value), target, tmode, modifier);
10339 else if (TREE_CODE (init) == STRING_CST)
10341 tree low_bound = array_ref_low_bound (exp);
10342 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10344 /* Optimize the special case of a zero lower bound.
10346 We convert the lower bound to sizetype to avoid problems
10347 with constant folding. E.g. suppose the lower bound is
10348 1 and its mode is QI. Without the conversion
10349 (ARRAY + (INDEX - (unsigned char)1))
10350 becomes
10351 (ARRAY + (-(unsigned char)1) + INDEX)
10352 which becomes
10353 (ARRAY + 255 + INDEX). Oops! */
10354 if (!integer_zerop (low_bound))
10355 index1 = size_diffop_loc (loc, index1,
10356 fold_convert_loc (loc, sizetype,
10357 low_bound));
10359 if (tree_fits_uhwi_p (index1)
10360 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10362 tree type = TREE_TYPE (TREE_TYPE (init));
10363 scalar_int_mode mode;
10365 if (is_int_mode (TYPE_MODE (type), &mode)
10366 && GET_MODE_SIZE (mode) == 1)
10367 return gen_int_mode (TREE_STRING_POINTER (init)
10368 [TREE_INT_CST_LOW (index1)],
10369 mode);
10374 goto normal_inner_ref;
10376 case COMPONENT_REF:
10377 /* If the operand is a CONSTRUCTOR, we can just extract the
10378 appropriate field if it is present. */
10379 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10381 unsigned HOST_WIDE_INT idx;
10382 tree field, value;
10383 scalar_int_mode field_mode;
10385 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10386 idx, field, value)
10387 if (field == treeop1
10388 /* We can normally use the value of the field in the
10389 CONSTRUCTOR. However, if this is a bitfield in
10390 an integral mode that we can fit in a HOST_WIDE_INT,
10391 we must mask only the number of bits in the bitfield,
10392 since this is done implicitly by the constructor. If
10393 the bitfield does not meet either of those conditions,
10394 we can't do this optimization. */
10395 && (! DECL_BIT_FIELD (field)
10396 || (is_int_mode (DECL_MODE (field), &field_mode)
10397 && (GET_MODE_PRECISION (field_mode)
10398 <= HOST_BITS_PER_WIDE_INT))))
10400 if (DECL_BIT_FIELD (field)
10401 && modifier == EXPAND_STACK_PARM)
10402 target = 0;
10403 op0 = expand_expr (value, target, tmode, modifier);
10404 if (DECL_BIT_FIELD (field))
10406 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10407 scalar_int_mode imode
10408 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10410 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10412 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10413 imode);
10414 op0 = expand_and (imode, op0, op1, target);
10416 else
10418 int count = GET_MODE_PRECISION (imode) - bitsize;
10420 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10421 target, 0);
10422 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10423 target, 0);
10427 return op0;
10430 goto normal_inner_ref;
10432 case BIT_FIELD_REF:
10433 case ARRAY_RANGE_REF:
10434 normal_inner_ref:
10436 machine_mode mode1, mode2;
10437 HOST_WIDE_INT bitsize, bitpos;
10438 tree offset;
10439 int reversep, volatilep = 0, must_force_mem;
10440 tree tem
10441 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10442 &unsignedp, &reversep, &volatilep);
10443 rtx orig_op0, memloc;
10444 bool clear_mem_expr = false;
10446 /* If we got back the original object, something is wrong. Perhaps
10447 we are evaluating an expression too early. In any event, don't
10448 infinitely recurse. */
10449 gcc_assert (tem != exp);
10451 /* If TEM's type is a union of variable size, pass TARGET to the inner
10452 computation, since it will need a temporary and TARGET is known
10453 to have to do. This occurs in unchecked conversion in Ada. */
10454 orig_op0 = op0
10455 = expand_expr_real (tem,
10456 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10457 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10458 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10459 != INTEGER_CST)
10460 && modifier != EXPAND_STACK_PARM
10461 ? target : NULL_RTX),
10462 VOIDmode,
10463 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10464 NULL, true);
10466 /* If the field has a mode, we want to access it in the
10467 field's mode, not the computed mode.
10468 If a MEM has VOIDmode (external with incomplete type),
10469 use BLKmode for it instead. */
10470 if (MEM_P (op0))
10472 if (mode1 != VOIDmode)
10473 op0 = adjust_address (op0, mode1, 0);
10474 else if (GET_MODE (op0) == VOIDmode)
10475 op0 = adjust_address (op0, BLKmode, 0);
10478 mode2
10479 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10481 /* If we have either an offset, a BLKmode result, or a reference
10482 outside the underlying object, we must force it to memory.
10483 Such a case can occur in Ada if we have unchecked conversion
10484 of an expression from a scalar type to an aggregate type or
10485 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10486 passed a partially uninitialized object or a view-conversion
10487 to a larger size. */
10488 must_force_mem = (offset
10489 || mode1 == BLKmode
10490 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10492 /* Handle CONCAT first. */
10493 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10495 if (bitpos == 0
10496 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10497 && COMPLEX_MODE_P (mode1)
10498 && COMPLEX_MODE_P (GET_MODE (op0))
10499 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10500 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10502 if (reversep)
10503 op0 = flip_storage_order (GET_MODE (op0), op0);
10504 if (mode1 != GET_MODE (op0))
10506 rtx parts[2];
10507 for (int i = 0; i < 2; i++)
10509 rtx op = read_complex_part (op0, i != 0);
10510 if (GET_CODE (op) == SUBREG)
10511 op = force_reg (GET_MODE (op), op);
10512 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10513 op);
10514 if (temp)
10515 op = temp;
10516 else
10518 if (!REG_P (op) && !MEM_P (op))
10519 op = force_reg (GET_MODE (op), op);
10520 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10522 parts[i] = op;
10524 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10526 return op0;
10528 if (bitpos == 0
10529 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10530 && bitsize)
10532 op0 = XEXP (op0, 0);
10533 mode2 = GET_MODE (op0);
10535 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10536 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10537 && bitpos
10538 && bitsize)
10540 op0 = XEXP (op0, 1);
10541 bitpos = 0;
10542 mode2 = GET_MODE (op0);
10544 else
10545 /* Otherwise force into memory. */
10546 must_force_mem = 1;
10549 /* If this is a constant, put it in a register if it is a legitimate
10550 constant and we don't need a memory reference. */
10551 if (CONSTANT_P (op0)
10552 && mode2 != BLKmode
10553 && targetm.legitimate_constant_p (mode2, op0)
10554 && !must_force_mem)
10555 op0 = force_reg (mode2, op0);
10557 /* Otherwise, if this is a constant, try to force it to the constant
10558 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10559 is a legitimate constant. */
10560 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10561 op0 = validize_mem (memloc);
10563 /* Otherwise, if this is a constant or the object is not in memory
10564 and need be, put it there. */
10565 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10567 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10568 emit_move_insn (memloc, op0);
10569 op0 = memloc;
10570 clear_mem_expr = true;
10573 if (offset)
10575 machine_mode address_mode;
10576 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10577 EXPAND_SUM);
10579 gcc_assert (MEM_P (op0));
10581 address_mode = get_address_mode (op0);
10582 if (GET_MODE (offset_rtx) != address_mode)
10584 /* We cannot be sure that the RTL in offset_rtx is valid outside
10585 of a memory address context, so force it into a register
10586 before attempting to convert it to the desired mode. */
10587 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10588 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10591 /* See the comment in expand_assignment for the rationale. */
10592 if (mode1 != VOIDmode
10593 && bitpos != 0
10594 && bitsize > 0
10595 && (bitpos % bitsize) == 0
10596 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10597 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10599 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10600 bitpos = 0;
10603 op0 = offset_address (op0, offset_rtx,
10604 highest_pow2_factor (offset));
10607 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10608 record its alignment as BIGGEST_ALIGNMENT. */
10609 if (MEM_P (op0) && bitpos == 0 && offset != 0
10610 && is_aligning_offset (offset, tem))
10611 set_mem_align (op0, BIGGEST_ALIGNMENT);
10613 /* Don't forget about volatility even if this is a bitfield. */
10614 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10616 if (op0 == orig_op0)
10617 op0 = copy_rtx (op0);
10619 MEM_VOLATILE_P (op0) = 1;
10622 /* In cases where an aligned union has an unaligned object
10623 as a field, we might be extracting a BLKmode value from
10624 an integer-mode (e.g., SImode) object. Handle this case
10625 by doing the extract into an object as wide as the field
10626 (which we know to be the width of a basic mode), then
10627 storing into memory, and changing the mode to BLKmode. */
10628 if (mode1 == VOIDmode
10629 || REG_P (op0) || GET_CODE (op0) == SUBREG
10630 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10631 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10632 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10633 && modifier != EXPAND_CONST_ADDRESS
10634 && modifier != EXPAND_INITIALIZER
10635 && modifier != EXPAND_MEMORY)
10636 /* If the bitfield is volatile and the bitsize
10637 is narrower than the access size of the bitfield,
10638 we need to extract bitfields from the access. */
10639 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10640 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10641 && mode1 != BLKmode
10642 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10643 /* If the field isn't aligned enough to fetch as a memref,
10644 fetch it as a bit field. */
10645 || (mode1 != BLKmode
10646 && (((MEM_P (op0)
10647 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10648 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10649 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10650 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10651 && modifier != EXPAND_MEMORY
10652 && ((modifier == EXPAND_CONST_ADDRESS
10653 || modifier == EXPAND_INITIALIZER)
10654 ? STRICT_ALIGNMENT
10655 : targetm.slow_unaligned_access (mode1,
10656 MEM_ALIGN (op0))))
10657 || (bitpos % BITS_PER_UNIT != 0)))
10658 /* If the type and the field are a constant size and the
10659 size of the type isn't the same size as the bitfield,
10660 we must use bitfield operations. */
10661 || (bitsize >= 0
10662 && TYPE_SIZE (TREE_TYPE (exp))
10663 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10664 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10665 bitsize)))
10667 machine_mode ext_mode = mode;
10669 if (ext_mode == BLKmode
10670 && ! (target != 0 && MEM_P (op0)
10671 && MEM_P (target)
10672 && bitpos % BITS_PER_UNIT == 0))
10673 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10675 if (ext_mode == BLKmode)
10677 if (target == 0)
10678 target = assign_temp (type, 1, 1);
10680 /* ??? Unlike the similar test a few lines below, this one is
10681 very likely obsolete. */
10682 if (bitsize == 0)
10683 return target;
10685 /* In this case, BITPOS must start at a byte boundary and
10686 TARGET, if specified, must be a MEM. */
10687 gcc_assert (MEM_P (op0)
10688 && (!target || MEM_P (target))
10689 && !(bitpos % BITS_PER_UNIT));
10691 emit_block_move (target,
10692 adjust_address (op0, VOIDmode,
10693 bitpos / BITS_PER_UNIT),
10694 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10695 / BITS_PER_UNIT),
10696 (modifier == EXPAND_STACK_PARM
10697 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10699 return target;
10702 /* If we have nothing to extract, the result will be 0 for targets
10703 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10704 return 0 for the sake of consistency, as reading a zero-sized
10705 bitfield is valid in Ada and the value is fully specified. */
10706 if (bitsize == 0)
10707 return const0_rtx;
10709 op0 = validize_mem (op0);
10711 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10712 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10714 /* If the result has a record type and the extraction is done in
10715 an integral mode, then the field may be not aligned on a byte
10716 boundary; in this case, if it has reverse storage order, it
10717 needs to be extracted as a scalar field with reverse storage
10718 order and put back into memory order afterwards. */
10719 if (TREE_CODE (type) == RECORD_TYPE
10720 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10721 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10723 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10724 (modifier == EXPAND_STACK_PARM
10725 ? NULL_RTX : target),
10726 ext_mode, ext_mode, reversep, alt_rtl);
10728 /* If the result has a record type and the mode of OP0 is an
10729 integral mode then, if BITSIZE is narrower than this mode
10730 and this is for big-endian data, we must put the field
10731 into the high-order bits. And we must also put it back
10732 into memory order if it has been previously reversed. */
10733 scalar_int_mode op0_mode;
10734 if (TREE_CODE (type) == RECORD_TYPE
10735 && is_int_mode (GET_MODE (op0), &op0_mode))
10737 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10739 if (bitsize < size
10740 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10741 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10742 size - bitsize, op0, 1);
10744 if (reversep)
10745 op0 = flip_storage_order (op0_mode, op0);
10748 /* If the result type is BLKmode, store the data into a temporary
10749 of the appropriate type, but with the mode corresponding to the
10750 mode for the data we have (op0's mode). */
10751 if (mode == BLKmode)
10753 rtx new_rtx
10754 = assign_stack_temp_for_type (ext_mode,
10755 GET_MODE_BITSIZE (ext_mode),
10756 type);
10757 emit_move_insn (new_rtx, op0);
10758 op0 = copy_rtx (new_rtx);
10759 PUT_MODE (op0, BLKmode);
10762 return op0;
10765 /* If the result is BLKmode, use that to access the object
10766 now as well. */
10767 if (mode == BLKmode)
10768 mode1 = BLKmode;
10770 /* Get a reference to just this component. */
10771 if (modifier == EXPAND_CONST_ADDRESS
10772 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10773 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10774 else
10775 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10777 if (op0 == orig_op0)
10778 op0 = copy_rtx (op0);
10780 /* Don't set memory attributes if the base expression is
10781 SSA_NAME that got expanded as a MEM. In that case, we should
10782 just honor its original memory attributes. */
10783 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10784 set_mem_attributes (op0, exp, 0);
10786 if (REG_P (XEXP (op0, 0)))
10787 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10789 /* If op0 is a temporary because the original expressions was forced
10790 to memory, clear MEM_EXPR so that the original expression cannot
10791 be marked as addressable through MEM_EXPR of the temporary. */
10792 if (clear_mem_expr)
10793 set_mem_expr (op0, NULL_TREE);
10795 MEM_VOLATILE_P (op0) |= volatilep;
10797 if (reversep
10798 && modifier != EXPAND_MEMORY
10799 && modifier != EXPAND_WRITE)
10800 op0 = flip_storage_order (mode1, op0);
10802 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10803 || modifier == EXPAND_CONST_ADDRESS
10804 || modifier == EXPAND_INITIALIZER)
10805 return op0;
10807 if (target == 0)
10808 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10810 convert_move (target, op0, unsignedp);
10811 return target;
10814 case OBJ_TYPE_REF:
10815 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10817 case CALL_EXPR:
10818 /* All valid uses of __builtin_va_arg_pack () are removed during
10819 inlining. */
10820 if (CALL_EXPR_VA_ARG_PACK (exp))
10821 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10823 tree fndecl = get_callee_fndecl (exp), attr;
10825 if (fndecl
10826 && (attr = lookup_attribute ("error",
10827 DECL_ATTRIBUTES (fndecl))) != NULL)
10828 error ("%Kcall to %qs declared with attribute error: %s",
10829 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10830 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10831 if (fndecl
10832 && (attr = lookup_attribute ("warning",
10833 DECL_ATTRIBUTES (fndecl))) != NULL)
10834 warning_at (tree_nonartificial_location (exp),
10835 0, "%Kcall to %qs declared with attribute warning: %s",
10836 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10837 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10839 /* Check for a built-in function. */
10840 if (fndecl && DECL_BUILT_IN (fndecl))
10842 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10843 if (CALL_WITH_BOUNDS_P (exp))
10844 return expand_builtin_with_bounds (exp, target, subtarget,
10845 tmode, ignore);
10846 else
10847 return expand_builtin (exp, target, subtarget, tmode, ignore);
10850 return expand_call (exp, target, ignore);
10852 case VIEW_CONVERT_EXPR:
10853 op0 = NULL_RTX;
10855 /* If we are converting to BLKmode, try to avoid an intermediate
10856 temporary by fetching an inner memory reference. */
10857 if (mode == BLKmode
10858 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10859 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10860 && handled_component_p (treeop0))
10862 machine_mode mode1;
10863 HOST_WIDE_INT bitsize, bitpos;
10864 tree offset;
10865 int unsignedp, reversep, volatilep = 0;
10866 tree tem
10867 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10868 &unsignedp, &reversep, &volatilep);
10869 rtx orig_op0;
10871 /* ??? We should work harder and deal with non-zero offsets. */
10872 if (!offset
10873 && (bitpos % BITS_PER_UNIT) == 0
10874 && !reversep
10875 && bitsize >= 0
10876 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10878 /* See the normal_inner_ref case for the rationale. */
10879 orig_op0
10880 = expand_expr_real (tem,
10881 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10882 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10883 != INTEGER_CST)
10884 && modifier != EXPAND_STACK_PARM
10885 ? target : NULL_RTX),
10886 VOIDmode,
10887 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10888 NULL, true);
10890 if (MEM_P (orig_op0))
10892 op0 = orig_op0;
10894 /* Get a reference to just this component. */
10895 if (modifier == EXPAND_CONST_ADDRESS
10896 || modifier == EXPAND_SUM
10897 || modifier == EXPAND_INITIALIZER)
10898 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10899 else
10900 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10902 if (op0 == orig_op0)
10903 op0 = copy_rtx (op0);
10905 set_mem_attributes (op0, treeop0, 0);
10906 if (REG_P (XEXP (op0, 0)))
10907 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10909 MEM_VOLATILE_P (op0) |= volatilep;
10914 if (!op0)
10915 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10916 NULL, inner_reference_p);
10918 /* If the input and output modes are both the same, we are done. */
10919 if (mode == GET_MODE (op0))
10921 /* If neither mode is BLKmode, and both modes are the same size
10922 then we can use gen_lowpart. */
10923 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10924 && (GET_MODE_PRECISION (mode)
10925 == GET_MODE_PRECISION (GET_MODE (op0)))
10926 && !COMPLEX_MODE_P (GET_MODE (op0)))
10928 if (GET_CODE (op0) == SUBREG)
10929 op0 = force_reg (GET_MODE (op0), op0);
10930 temp = gen_lowpart_common (mode, op0);
10931 if (temp)
10932 op0 = temp;
10933 else
10935 if (!REG_P (op0) && !MEM_P (op0))
10936 op0 = force_reg (GET_MODE (op0), op0);
10937 op0 = gen_lowpart (mode, op0);
10940 /* If both types are integral, convert from one mode to the other. */
10941 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10942 op0 = convert_modes (mode, GET_MODE (op0), op0,
10943 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10944 /* If the output type is a bit-field type, do an extraction. */
10945 else if (reduce_bit_field)
10946 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10947 TYPE_UNSIGNED (type), NULL_RTX,
10948 mode, mode, false, NULL);
10949 /* As a last resort, spill op0 to memory, and reload it in a
10950 different mode. */
10951 else if (!MEM_P (op0))
10953 /* If the operand is not a MEM, force it into memory. Since we
10954 are going to be changing the mode of the MEM, don't call
10955 force_const_mem for constants because we don't allow pool
10956 constants to change mode. */
10957 tree inner_type = TREE_TYPE (treeop0);
10959 gcc_assert (!TREE_ADDRESSABLE (exp));
10961 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10962 target
10963 = assign_stack_temp_for_type
10964 (TYPE_MODE (inner_type),
10965 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10967 emit_move_insn (target, op0);
10968 op0 = target;
10971 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10972 output type is such that the operand is known to be aligned, indicate
10973 that it is. Otherwise, we need only be concerned about alignment for
10974 non-BLKmode results. */
10975 if (MEM_P (op0))
10977 enum insn_code icode;
10979 if (modifier != EXPAND_WRITE
10980 && modifier != EXPAND_MEMORY
10981 && !inner_reference_p
10982 && mode != BLKmode
10983 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10985 /* If the target does have special handling for unaligned
10986 loads of mode then use them. */
10987 if ((icode = optab_handler (movmisalign_optab, mode))
10988 != CODE_FOR_nothing)
10990 rtx reg;
10992 op0 = adjust_address (op0, mode, 0);
10993 /* We've already validated the memory, and we're creating a
10994 new pseudo destination. The predicates really can't
10995 fail. */
10996 reg = gen_reg_rtx (mode);
10998 /* Nor can the insn generator. */
10999 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11000 emit_insn (insn);
11001 return reg;
11003 else if (STRICT_ALIGNMENT)
11005 tree inner_type = TREE_TYPE (treeop0);
11006 HOST_WIDE_INT temp_size
11007 = MAX (int_size_in_bytes (inner_type),
11008 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11009 rtx new_rtx
11010 = assign_stack_temp_for_type (mode, temp_size, type);
11011 rtx new_with_op0_mode
11012 = adjust_address (new_rtx, GET_MODE (op0), 0);
11014 gcc_assert (!TREE_ADDRESSABLE (exp));
11016 if (GET_MODE (op0) == BLKmode)
11017 emit_block_move (new_with_op0_mode, op0,
11018 GEN_INT (GET_MODE_SIZE (mode)),
11019 (modifier == EXPAND_STACK_PARM
11020 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11021 else
11022 emit_move_insn (new_with_op0_mode, op0);
11024 op0 = new_rtx;
11028 op0 = adjust_address (op0, mode, 0);
11031 return op0;
11033 case MODIFY_EXPR:
11035 tree lhs = treeop0;
11036 tree rhs = treeop1;
11037 gcc_assert (ignore);
11039 /* Check for |= or &= of a bitfield of size one into another bitfield
11040 of size 1. In this case, (unless we need the result of the
11041 assignment) we can do this more efficiently with a
11042 test followed by an assignment, if necessary.
11044 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11045 things change so we do, this code should be enhanced to
11046 support it. */
11047 if (TREE_CODE (lhs) == COMPONENT_REF
11048 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11049 || TREE_CODE (rhs) == BIT_AND_EXPR)
11050 && TREE_OPERAND (rhs, 0) == lhs
11051 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11052 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11053 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11055 rtx_code_label *label = gen_label_rtx ();
11056 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11057 do_jump (TREE_OPERAND (rhs, 1),
11058 value ? label : 0,
11059 value ? 0 : label,
11060 profile_probability::uninitialized ());
11061 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11062 false);
11063 do_pending_stack_adjust ();
11064 emit_label (label);
11065 return const0_rtx;
11068 expand_assignment (lhs, rhs, false);
11069 return const0_rtx;
11072 case ADDR_EXPR:
11073 return expand_expr_addr_expr (exp, target, tmode, modifier);
11075 case REALPART_EXPR:
11076 op0 = expand_normal (treeop0);
11077 return read_complex_part (op0, false);
11079 case IMAGPART_EXPR:
11080 op0 = expand_normal (treeop0);
11081 return read_complex_part (op0, true);
11083 case RETURN_EXPR:
11084 case LABEL_EXPR:
11085 case GOTO_EXPR:
11086 case SWITCH_EXPR:
11087 case ASM_EXPR:
11088 /* Expanded in cfgexpand.c. */
11089 gcc_unreachable ();
11091 case TRY_CATCH_EXPR:
11092 case CATCH_EXPR:
11093 case EH_FILTER_EXPR:
11094 case TRY_FINALLY_EXPR:
11095 /* Lowered by tree-eh.c. */
11096 gcc_unreachable ();
11098 case WITH_CLEANUP_EXPR:
11099 case CLEANUP_POINT_EXPR:
11100 case TARGET_EXPR:
11101 case CASE_LABEL_EXPR:
11102 case VA_ARG_EXPR:
11103 case BIND_EXPR:
11104 case INIT_EXPR:
11105 case CONJ_EXPR:
11106 case COMPOUND_EXPR:
11107 case PREINCREMENT_EXPR:
11108 case PREDECREMENT_EXPR:
11109 case POSTINCREMENT_EXPR:
11110 case POSTDECREMENT_EXPR:
11111 case LOOP_EXPR:
11112 case EXIT_EXPR:
11113 case COMPOUND_LITERAL_EXPR:
11114 /* Lowered by gimplify.c. */
11115 gcc_unreachable ();
11117 case FDESC_EXPR:
11118 /* Function descriptors are not valid except for as
11119 initialization constants, and should not be expanded. */
11120 gcc_unreachable ();
11122 case WITH_SIZE_EXPR:
11123 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11124 have pulled out the size to use in whatever context it needed. */
11125 return expand_expr_real (treeop0, original_target, tmode,
11126 modifier, alt_rtl, inner_reference_p);
11128 default:
11129 return expand_expr_real_2 (&ops, target, tmode, modifier);
11133 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11134 signedness of TYPE), possibly returning the result in TARGET.
11135 TYPE is known to be a partial integer type. */
11136 static rtx
11137 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11139 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11140 if (target && GET_MODE (target) != GET_MODE (exp))
11141 target = 0;
11142 /* For constant values, reduce using build_int_cst_type. */
11143 if (CONST_INT_P (exp))
11145 HOST_WIDE_INT value = INTVAL (exp);
11146 tree t = build_int_cst_type (type, value);
11147 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11149 else if (TYPE_UNSIGNED (type))
11151 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11152 rtx mask = immed_wide_int_const
11153 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11154 return expand_and (mode, exp, mask, target);
11156 else
11158 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11159 int count = GET_MODE_PRECISION (mode) - prec;
11160 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11161 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11165 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11166 when applied to the address of EXP produces an address known to be
11167 aligned more than BIGGEST_ALIGNMENT. */
11169 static int
11170 is_aligning_offset (const_tree offset, const_tree exp)
11172 /* Strip off any conversions. */
11173 while (CONVERT_EXPR_P (offset))
11174 offset = TREE_OPERAND (offset, 0);
11176 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11177 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11178 if (TREE_CODE (offset) != BIT_AND_EXPR
11179 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11180 || compare_tree_int (TREE_OPERAND (offset, 1),
11181 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11182 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11183 return 0;
11185 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11186 It must be NEGATE_EXPR. Then strip any more conversions. */
11187 offset = TREE_OPERAND (offset, 0);
11188 while (CONVERT_EXPR_P (offset))
11189 offset = TREE_OPERAND (offset, 0);
11191 if (TREE_CODE (offset) != NEGATE_EXPR)
11192 return 0;
11194 offset = TREE_OPERAND (offset, 0);
11195 while (CONVERT_EXPR_P (offset))
11196 offset = TREE_OPERAND (offset, 0);
11198 /* This must now be the address of EXP. */
11199 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11202 /* Return the tree node if an ARG corresponds to a string constant or zero
11203 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11204 in bytes within the string that ARG is accessing. The type of the
11205 offset will be `sizetype'. */
11207 tree
11208 string_constant (tree arg, tree *ptr_offset)
11210 tree array, offset, lower_bound;
11211 STRIP_NOPS (arg);
11213 if (TREE_CODE (arg) == ADDR_EXPR)
11215 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11217 *ptr_offset = size_zero_node;
11218 return TREE_OPERAND (arg, 0);
11220 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11222 array = TREE_OPERAND (arg, 0);
11223 offset = size_zero_node;
11225 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11227 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11228 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11229 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11230 return 0;
11232 /* Check if the array has a nonzero lower bound. */
11233 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11234 if (!integer_zerop (lower_bound))
11236 /* If the offset and base aren't both constants, return 0. */
11237 if (TREE_CODE (lower_bound) != INTEGER_CST)
11238 return 0;
11239 if (TREE_CODE (offset) != INTEGER_CST)
11240 return 0;
11241 /* Adjust offset by the lower bound. */
11242 offset = size_diffop (fold_convert (sizetype, offset),
11243 fold_convert (sizetype, lower_bound));
11246 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11248 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11249 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11250 if (TREE_CODE (array) != ADDR_EXPR)
11251 return 0;
11252 array = TREE_OPERAND (array, 0);
11253 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11254 return 0;
11256 else
11257 return 0;
11259 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11261 tree arg0 = TREE_OPERAND (arg, 0);
11262 tree arg1 = TREE_OPERAND (arg, 1);
11264 STRIP_NOPS (arg0);
11265 STRIP_NOPS (arg1);
11267 if (TREE_CODE (arg0) == ADDR_EXPR
11268 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11269 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11271 array = TREE_OPERAND (arg0, 0);
11272 offset = arg1;
11274 else if (TREE_CODE (arg1) == ADDR_EXPR
11275 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11276 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11278 array = TREE_OPERAND (arg1, 0);
11279 offset = arg0;
11281 else
11282 return 0;
11284 else
11285 return 0;
11287 if (TREE_CODE (array) == STRING_CST)
11289 *ptr_offset = fold_convert (sizetype, offset);
11290 return array;
11292 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11294 int length;
11295 tree init = ctor_for_folding (array);
11297 /* Variables initialized to string literals can be handled too. */
11298 if (init == error_mark_node
11299 || !init
11300 || TREE_CODE (init) != STRING_CST)
11301 return 0;
11303 /* Avoid const char foo[4] = "abcde"; */
11304 if (DECL_SIZE_UNIT (array) == NULL_TREE
11305 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11306 || (length = TREE_STRING_LENGTH (init)) <= 0
11307 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11308 return 0;
11310 /* If variable is bigger than the string literal, OFFSET must be constant
11311 and inside of the bounds of the string literal. */
11312 offset = fold_convert (sizetype, offset);
11313 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11314 && (! tree_fits_uhwi_p (offset)
11315 || compare_tree_int (offset, length) >= 0))
11316 return 0;
11318 *ptr_offset = offset;
11319 return init;
11322 return 0;
11325 /* Generate code to calculate OPS, and exploded expression
11326 using a store-flag instruction and return an rtx for the result.
11327 OPS reflects a comparison.
11329 If TARGET is nonzero, store the result there if convenient.
11331 Return zero if there is no suitable set-flag instruction
11332 available on this machine.
11334 Once expand_expr has been called on the arguments of the comparison,
11335 we are committed to doing the store flag, since it is not safe to
11336 re-evaluate the expression. We emit the store-flag insn by calling
11337 emit_store_flag, but only expand the arguments if we have a reason
11338 to believe that emit_store_flag will be successful. If we think that
11339 it will, but it isn't, we have to simulate the store-flag with a
11340 set/jump/set sequence. */
11342 static rtx
11343 do_store_flag (sepops ops, rtx target, machine_mode mode)
11345 enum rtx_code code;
11346 tree arg0, arg1, type;
11347 machine_mode operand_mode;
11348 int unsignedp;
11349 rtx op0, op1;
11350 rtx subtarget = target;
11351 location_t loc = ops->location;
11353 arg0 = ops->op0;
11354 arg1 = ops->op1;
11356 /* Don't crash if the comparison was erroneous. */
11357 if (arg0 == error_mark_node || arg1 == error_mark_node)
11358 return const0_rtx;
11360 type = TREE_TYPE (arg0);
11361 operand_mode = TYPE_MODE (type);
11362 unsignedp = TYPE_UNSIGNED (type);
11364 /* We won't bother with BLKmode store-flag operations because it would mean
11365 passing a lot of information to emit_store_flag. */
11366 if (operand_mode == BLKmode)
11367 return 0;
11369 /* We won't bother with store-flag operations involving function pointers
11370 when function pointers must be canonicalized before comparisons. */
11371 if (targetm.have_canonicalize_funcptr_for_compare ()
11372 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11373 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11374 == FUNCTION_TYPE))
11375 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11376 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11377 == FUNCTION_TYPE))))
11378 return 0;
11380 STRIP_NOPS (arg0);
11381 STRIP_NOPS (arg1);
11383 /* For vector typed comparisons emit code to generate the desired
11384 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11385 expander for this. */
11386 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11388 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11389 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11390 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11391 return expand_vec_cmp_expr (ops->type, ifexp, target);
11392 else
11394 tree if_true = constant_boolean_node (true, ops->type);
11395 tree if_false = constant_boolean_node (false, ops->type);
11396 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11397 if_false, target);
11401 /* Get the rtx comparison code to use. We know that EXP is a comparison
11402 operation of some type. Some comparisons against 1 and -1 can be
11403 converted to comparisons with zero. Do so here so that the tests
11404 below will be aware that we have a comparison with zero. These
11405 tests will not catch constants in the first operand, but constants
11406 are rarely passed as the first operand. */
11408 switch (ops->code)
11410 case EQ_EXPR:
11411 code = EQ;
11412 break;
11413 case NE_EXPR:
11414 code = NE;
11415 break;
11416 case LT_EXPR:
11417 if (integer_onep (arg1))
11418 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11419 else
11420 code = unsignedp ? LTU : LT;
11421 break;
11422 case LE_EXPR:
11423 if (! unsignedp && integer_all_onesp (arg1))
11424 arg1 = integer_zero_node, code = LT;
11425 else
11426 code = unsignedp ? LEU : LE;
11427 break;
11428 case GT_EXPR:
11429 if (! unsignedp && integer_all_onesp (arg1))
11430 arg1 = integer_zero_node, code = GE;
11431 else
11432 code = unsignedp ? GTU : GT;
11433 break;
11434 case GE_EXPR:
11435 if (integer_onep (arg1))
11436 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11437 else
11438 code = unsignedp ? GEU : GE;
11439 break;
11441 case UNORDERED_EXPR:
11442 code = UNORDERED;
11443 break;
11444 case ORDERED_EXPR:
11445 code = ORDERED;
11446 break;
11447 case UNLT_EXPR:
11448 code = UNLT;
11449 break;
11450 case UNLE_EXPR:
11451 code = UNLE;
11452 break;
11453 case UNGT_EXPR:
11454 code = UNGT;
11455 break;
11456 case UNGE_EXPR:
11457 code = UNGE;
11458 break;
11459 case UNEQ_EXPR:
11460 code = UNEQ;
11461 break;
11462 case LTGT_EXPR:
11463 code = LTGT;
11464 break;
11466 default:
11467 gcc_unreachable ();
11470 /* Put a constant second. */
11471 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11472 || TREE_CODE (arg0) == FIXED_CST)
11474 std::swap (arg0, arg1);
11475 code = swap_condition (code);
11478 /* If this is an equality or inequality test of a single bit, we can
11479 do this by shifting the bit being tested to the low-order bit and
11480 masking the result with the constant 1. If the condition was EQ,
11481 we xor it with 1. This does not require an scc insn and is faster
11482 than an scc insn even if we have it.
11484 The code to make this transformation was moved into fold_single_bit_test,
11485 so we just call into the folder and expand its result. */
11487 if ((code == NE || code == EQ)
11488 && integer_zerop (arg1)
11489 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11491 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11492 if (srcstmt
11493 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11495 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11496 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11497 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11498 gimple_assign_rhs1 (srcstmt),
11499 gimple_assign_rhs2 (srcstmt));
11500 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11501 if (temp)
11502 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11506 if (! get_subtarget (target)
11507 || GET_MODE (subtarget) != operand_mode)
11508 subtarget = 0;
11510 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11512 if (target == 0)
11513 target = gen_reg_rtx (mode);
11515 /* Try a cstore if possible. */
11516 return emit_store_flag_force (target, code, op0, op1,
11517 operand_mode, unsignedp,
11518 (TYPE_PRECISION (ops->type) == 1
11519 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11522 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11523 0 otherwise (i.e. if there is no casesi instruction).
11525 DEFAULT_PROBABILITY is the probability of jumping to the default
11526 label. */
11528 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11529 rtx table_label, rtx default_label, rtx fallback_label,
11530 profile_probability default_probability)
11532 struct expand_operand ops[5];
11533 scalar_int_mode index_mode = SImode;
11534 rtx op1, op2, index;
11536 if (! targetm.have_casesi ())
11537 return 0;
11539 /* The index must be some form of integer. Convert it to SImode. */
11540 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11541 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11543 rtx rangertx = expand_normal (range);
11545 /* We must handle the endpoints in the original mode. */
11546 index_expr = build2 (MINUS_EXPR, index_type,
11547 index_expr, minval);
11548 minval = integer_zero_node;
11549 index = expand_normal (index_expr);
11550 if (default_label)
11551 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11552 omode, 1, default_label,
11553 default_probability);
11554 /* Now we can safely truncate. */
11555 index = convert_to_mode (index_mode, index, 0);
11557 else
11559 if (omode != index_mode)
11561 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11562 index_expr = fold_convert (index_type, index_expr);
11565 index = expand_normal (index_expr);
11568 do_pending_stack_adjust ();
11570 op1 = expand_normal (minval);
11571 op2 = expand_normal (range);
11573 create_input_operand (&ops[0], index, index_mode);
11574 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11575 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11576 create_fixed_operand (&ops[3], table_label);
11577 create_fixed_operand (&ops[4], (default_label
11578 ? default_label
11579 : fallback_label));
11580 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11581 return 1;
11584 /* Attempt to generate a tablejump instruction; same concept. */
11585 /* Subroutine of the next function.
11587 INDEX is the value being switched on, with the lowest value
11588 in the table already subtracted.
11589 MODE is its expected mode (needed if INDEX is constant).
11590 RANGE is the length of the jump table.
11591 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11593 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11594 index value is out of range.
11595 DEFAULT_PROBABILITY is the probability of jumping to
11596 the default label. */
11598 static void
11599 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11600 rtx default_label, profile_probability default_probability)
11602 rtx temp, vector;
11604 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11605 cfun->cfg->max_jumptable_ents = INTVAL (range);
11607 /* Do an unsigned comparison (in the proper mode) between the index
11608 expression and the value which represents the length of the range.
11609 Since we just finished subtracting the lower bound of the range
11610 from the index expression, this comparison allows us to simultaneously
11611 check that the original index expression value is both greater than
11612 or equal to the minimum value of the range and less than or equal to
11613 the maximum value of the range. */
11615 if (default_label)
11616 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11617 default_label, default_probability);
11620 /* If index is in range, it must fit in Pmode.
11621 Convert to Pmode so we can index with it. */
11622 if (mode != Pmode)
11623 index = convert_to_mode (Pmode, index, 1);
11625 /* Don't let a MEM slip through, because then INDEX that comes
11626 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11627 and break_out_memory_refs will go to work on it and mess it up. */
11628 #ifdef PIC_CASE_VECTOR_ADDRESS
11629 if (flag_pic && !REG_P (index))
11630 index = copy_to_mode_reg (Pmode, index);
11631 #endif
11633 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11634 GET_MODE_SIZE, because this indicates how large insns are. The other
11635 uses should all be Pmode, because they are addresses. This code
11636 could fail if addresses and insns are not the same size. */
11637 index = simplify_gen_binary (MULT, Pmode, index,
11638 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11639 Pmode));
11640 index = simplify_gen_binary (PLUS, Pmode, index,
11641 gen_rtx_LABEL_REF (Pmode, table_label));
11643 #ifdef PIC_CASE_VECTOR_ADDRESS
11644 if (flag_pic)
11645 index = PIC_CASE_VECTOR_ADDRESS (index);
11646 else
11647 #endif
11648 index = memory_address (CASE_VECTOR_MODE, index);
11649 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11650 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11651 convert_move (temp, vector, 0);
11653 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11655 /* If we are generating PIC code or if the table is PC-relative, the
11656 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11657 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11658 emit_barrier ();
11662 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11663 rtx table_label, rtx default_label,
11664 profile_probability default_probability)
11666 rtx index;
11668 if (! targetm.have_tablejump ())
11669 return 0;
11671 index_expr = fold_build2 (MINUS_EXPR, index_type,
11672 fold_convert (index_type, index_expr),
11673 fold_convert (index_type, minval));
11674 index = expand_normal (index_expr);
11675 do_pending_stack_adjust ();
11677 do_tablejump (index, TYPE_MODE (index_type),
11678 convert_modes (TYPE_MODE (index_type),
11679 TYPE_MODE (TREE_TYPE (range)),
11680 expand_normal (range),
11681 TYPE_UNSIGNED (TREE_TYPE (range))),
11682 table_label, default_label, default_probability);
11683 return 1;
11686 /* Return a CONST_VECTOR rtx representing vector mask for
11687 a VECTOR_CST of booleans. */
11688 static rtx
11689 const_vector_mask_from_tree (tree exp)
11691 rtvec v;
11692 unsigned i, units;
11693 tree elt;
11694 machine_mode inner, mode;
11696 mode = TYPE_MODE (TREE_TYPE (exp));
11697 units = VECTOR_CST_NELTS (exp);
11698 inner = GET_MODE_INNER (mode);
11700 v = rtvec_alloc (units);
11702 for (i = 0; i < units; ++i)
11704 elt = VECTOR_CST_ELT (exp, i);
11706 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11707 if (integer_zerop (elt))
11708 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11709 else if (integer_onep (elt)
11710 || integer_minus_onep (elt))
11711 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11712 else
11713 gcc_unreachable ();
11716 return gen_rtx_CONST_VECTOR (mode, v);
11719 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11720 Return a constant scalar rtx of mode MODE in which bit X is set if element
11721 X of EXP is nonzero. */
11722 static rtx
11723 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11725 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11726 tree elt;
11727 unsigned i;
11729 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11731 elt = VECTOR_CST_ELT (exp, i);
11732 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11733 if (integer_all_onesp (elt))
11734 res = wi::set_bit (res, i);
11735 else
11736 gcc_assert (integer_zerop (elt));
11739 return immed_wide_int_const (res, mode);
11742 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11743 static rtx
11744 const_vector_from_tree (tree exp)
11746 rtvec v;
11747 unsigned i, units;
11748 tree elt;
11749 machine_mode inner, mode;
11751 mode = TYPE_MODE (TREE_TYPE (exp));
11753 if (initializer_zerop (exp))
11754 return CONST0_RTX (mode);
11756 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11757 return const_vector_mask_from_tree (exp);
11759 units = VECTOR_CST_NELTS (exp);
11760 inner = GET_MODE_INNER (mode);
11762 v = rtvec_alloc (units);
11764 for (i = 0; i < units; ++i)
11766 elt = VECTOR_CST_ELT (exp, i);
11768 if (TREE_CODE (elt) == REAL_CST)
11769 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11770 inner);
11771 else if (TREE_CODE (elt) == FIXED_CST)
11772 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11773 inner);
11774 else
11775 RTVEC_ELT (v, i) = immed_wide_int_const (wi::to_wide (elt), inner);
11778 return gen_rtx_CONST_VECTOR (mode, v);
11781 /* Build a decl for a personality function given a language prefix. */
11783 tree
11784 build_personality_function (const char *lang)
11786 const char *unwind_and_version;
11787 tree decl, type;
11788 char *name;
11790 switch (targetm_common.except_unwind_info (&global_options))
11792 case UI_NONE:
11793 return NULL;
11794 case UI_SJLJ:
11795 unwind_and_version = "_sj0";
11796 break;
11797 case UI_DWARF2:
11798 case UI_TARGET:
11799 unwind_and_version = "_v0";
11800 break;
11801 case UI_SEH:
11802 unwind_and_version = "_seh0";
11803 break;
11804 default:
11805 gcc_unreachable ();
11808 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11810 type = build_function_type_list (integer_type_node, integer_type_node,
11811 long_long_unsigned_type_node,
11812 ptr_type_node, ptr_type_node, NULL_TREE);
11813 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11814 get_identifier (name), type);
11815 DECL_ARTIFICIAL (decl) = 1;
11816 DECL_EXTERNAL (decl) = 1;
11817 TREE_PUBLIC (decl) = 1;
11819 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11820 are the flags assigned by targetm.encode_section_info. */
11821 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11823 return decl;
11826 /* Extracts the personality function of DECL and returns the corresponding
11827 libfunc. */
11830 get_personality_function (tree decl)
11832 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11833 enum eh_personality_kind pk;
11835 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11836 if (pk == eh_personality_none)
11837 return NULL;
11839 if (!personality
11840 && pk == eh_personality_any)
11841 personality = lang_hooks.eh_personality ();
11843 if (pk == eh_personality_lang)
11844 gcc_assert (personality != NULL_TREE);
11846 return XEXP (DECL_RTL (personality), 0);
11849 /* Returns a tree for the size of EXP in bytes. */
11851 static tree
11852 tree_expr_size (const_tree exp)
11854 if (DECL_P (exp)
11855 && DECL_SIZE_UNIT (exp) != 0)
11856 return DECL_SIZE_UNIT (exp);
11857 else
11858 return size_in_bytes (TREE_TYPE (exp));
11861 /* Return an rtx for the size in bytes of the value of EXP. */
11864 expr_size (tree exp)
11866 tree size;
11868 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11869 size = TREE_OPERAND (exp, 1);
11870 else
11872 size = tree_expr_size (exp);
11873 gcc_assert (size);
11874 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11877 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11880 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11881 if the size can vary or is larger than an integer. */
11883 static HOST_WIDE_INT
11884 int_expr_size (tree exp)
11886 tree size;
11888 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11889 size = TREE_OPERAND (exp, 1);
11890 else
11892 size = tree_expr_size (exp);
11893 gcc_assert (size);
11896 if (size == 0 || !tree_fits_shwi_p (size))
11897 return -1;
11899 return tree_to_shwi (size);