[11/77] Add a float_mode_for_size helper function
[official-gcc.git] / gcc / expr.c
blob0219045f9ff793db77b6dcf71401b518affde9ba
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 (tree);
103 static tree tree_expr_size (const_tree);
104 static HOST_WIDE_INT int_expr_size (tree);
107 /* This is run to set up which modes can be used
108 directly in memory and to initialize the block move optab. It is run
109 at the beginning of compilation and when the target is reinitialized. */
111 void
112 init_expr_target (void)
114 rtx pat;
115 machine_mode mode;
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 (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 (! 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 FOR_EACH_MODE_IN_CLASS (mode, MODE_FLOAT)
182 machine_mode srcmode;
183 FOR_EACH_MODE_UNTIL (srcmode, mode)
185 enum insn_code ic;
187 ic = can_extend_p (mode, srcmode, 0);
188 if (ic == CODE_FOR_nothing)
189 continue;
191 PUT_MODE (mem, srcmode);
193 if (insn_operand_matches (ic, 1, mem))
194 float_extend_from_mem[mode][srcmode] = true;
199 /* This is run at the start of compiling a function. */
201 void
202 init_expr (void)
204 memset (&crtl->expr, 0, sizeof (crtl->expr));
207 /* Copy data from FROM to TO, where the machine modes are not the same.
208 Both modes may be integer, or both may be floating, or both may be
209 fixed-point.
210 UNSIGNEDP should be nonzero if FROM is an unsigned type.
211 This causes zero-extension instead of sign-extension. */
213 void
214 convert_move (rtx to, rtx from, int unsignedp)
216 machine_mode to_mode = GET_MODE (to);
217 machine_mode from_mode = GET_MODE (from);
218 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
219 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
220 enum insn_code code;
221 rtx libcall;
223 /* rtx code for making an equivalent value. */
224 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
225 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
228 gcc_assert (to_real == from_real);
229 gcc_assert (to_mode != BLKmode);
230 gcc_assert (from_mode != BLKmode);
232 /* If the source and destination are already the same, then there's
233 nothing to do. */
234 if (to == from)
235 return;
237 /* If FROM is a SUBREG that indicates that we have already done at least
238 the required extension, strip it. We don't handle such SUBREGs as
239 TO here. */
241 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
242 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
243 >= GET_MODE_PRECISION (to_mode))
244 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
245 from = gen_lowpart (to_mode, from), from_mode = to_mode;
247 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
249 if (to_mode == from_mode
250 || (from_mode == VOIDmode && CONSTANT_P (from)))
252 emit_move_insn (to, from);
253 return;
256 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
258 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
260 if (VECTOR_MODE_P (to_mode))
261 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
262 else
263 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
265 emit_move_insn (to, from);
266 return;
269 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
271 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
272 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
273 return;
276 if (to_real)
278 rtx value;
279 rtx_insn *insns;
280 convert_optab tab;
282 gcc_assert ((GET_MODE_PRECISION (from_mode)
283 != GET_MODE_PRECISION (to_mode))
284 || (DECIMAL_FLOAT_MODE_P (from_mode)
285 != DECIMAL_FLOAT_MODE_P (to_mode)));
287 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
288 /* Conversion between decimal float and binary float, same size. */
289 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
290 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
291 tab = sext_optab;
292 else
293 tab = trunc_optab;
295 /* Try converting directly if the insn is supported. */
297 code = convert_optab_handler (tab, to_mode, from_mode);
298 if (code != CODE_FOR_nothing)
300 emit_unop_insn (code, to, from,
301 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
302 return;
305 /* Otherwise use a libcall. */
306 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
308 /* Is this conversion implemented yet? */
309 gcc_assert (libcall);
311 start_sequence ();
312 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
313 1, from, from_mode);
314 insns = get_insns ();
315 end_sequence ();
316 emit_libcall_block (insns, to, value,
317 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
318 from)
319 : gen_rtx_FLOAT_EXTEND (to_mode, from));
320 return;
323 /* Handle pointer conversion. */ /* SPEE 900220. */
324 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
326 convert_optab ctab;
328 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
329 ctab = trunc_optab;
330 else if (unsignedp)
331 ctab = zext_optab;
332 else
333 ctab = sext_optab;
335 if (convert_optab_handler (ctab, to_mode, from_mode)
336 != CODE_FOR_nothing)
338 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
339 to, from, UNKNOWN);
340 return;
344 /* Targets are expected to provide conversion insns between PxImode and
345 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
346 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
348 machine_mode full_mode
349 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
351 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
352 != CODE_FOR_nothing);
354 if (full_mode != from_mode)
355 from = convert_to_mode (full_mode, from, unsignedp);
356 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
357 to, from, UNKNOWN);
358 return;
360 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
362 rtx new_from;
363 machine_mode full_mode
364 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
365 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
366 enum insn_code icode;
368 icode = convert_optab_handler (ctab, full_mode, from_mode);
369 gcc_assert (icode != CODE_FOR_nothing);
371 if (to_mode == full_mode)
373 emit_unop_insn (icode, to, from, UNKNOWN);
374 return;
377 new_from = gen_reg_rtx (full_mode);
378 emit_unop_insn (icode, new_from, from, UNKNOWN);
380 /* else proceed to integer conversions below. */
381 from_mode = full_mode;
382 from = new_from;
385 /* Make sure both are fixed-point modes or both are not. */
386 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
387 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
388 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
390 /* If we widen from_mode to to_mode and they are in the same class,
391 we won't saturate the result.
392 Otherwise, always saturate the result to play safe. */
393 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
394 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
395 expand_fixed_convert (to, from, 0, 0);
396 else
397 expand_fixed_convert (to, from, 0, 1);
398 return;
401 /* Now both modes are integers. */
403 /* Handle expanding beyond a word. */
404 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
405 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
407 rtx_insn *insns;
408 rtx lowpart;
409 rtx fill_value;
410 rtx lowfrom;
411 int i;
412 machine_mode lowpart_mode;
413 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
415 /* Try converting directly if the insn is supported. */
416 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
417 != CODE_FOR_nothing)
419 /* If FROM is a SUBREG, put it into a register. Do this
420 so that we always generate the same set of insns for
421 better cse'ing; if an intermediate assignment occurred,
422 we won't be doing the operation directly on the SUBREG. */
423 if (optimize > 0 && GET_CODE (from) == SUBREG)
424 from = force_reg (from_mode, from);
425 emit_unop_insn (code, to, from, equiv_code);
426 return;
428 /* Next, try converting via full word. */
429 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
430 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
431 != CODE_FOR_nothing))
433 rtx word_to = gen_reg_rtx (word_mode);
434 if (REG_P (to))
436 if (reg_overlap_mentioned_p (to, from))
437 from = force_reg (from_mode, from);
438 emit_clobber (to);
440 convert_move (word_to, from, unsignedp);
441 emit_unop_insn (code, to, word_to, equiv_code);
442 return;
445 /* No special multiword conversion insn; do it by hand. */
446 start_sequence ();
448 /* Since we will turn this into a no conflict block, we must ensure
449 the source does not overlap the target so force it into an isolated
450 register when maybe so. Likewise for any MEM input, since the
451 conversion sequence might require several references to it and we
452 must ensure we're getting the same value every time. */
454 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
455 from = force_reg (from_mode, from);
457 /* Get a copy of FROM widened to a word, if necessary. */
458 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
459 lowpart_mode = word_mode;
460 else
461 lowpart_mode = from_mode;
463 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
465 lowpart = gen_lowpart (lowpart_mode, to);
466 emit_move_insn (lowpart, lowfrom);
468 /* Compute the value to put in each remaining word. */
469 if (unsignedp)
470 fill_value = const0_rtx;
471 else
472 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
473 LT, lowfrom, const0_rtx,
474 lowpart_mode, 0, -1);
476 /* Fill the remaining words. */
477 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
479 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
480 rtx subword = operand_subword (to, index, 1, to_mode);
482 gcc_assert (subword);
484 if (fill_value != subword)
485 emit_move_insn (subword, fill_value);
488 insns = get_insns ();
489 end_sequence ();
491 emit_insn (insns);
492 return;
495 /* Truncating multi-word to a word or less. */
496 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
497 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
499 if (!((MEM_P (from)
500 && ! MEM_VOLATILE_P (from)
501 && direct_load[(int) to_mode]
502 && ! mode_dependent_address_p (XEXP (from, 0),
503 MEM_ADDR_SPACE (from)))
504 || REG_P (from)
505 || GET_CODE (from) == SUBREG))
506 from = force_reg (from_mode, from);
507 convert_move (to, gen_lowpart (word_mode, from), 0);
508 return;
511 /* Now follow all the conversions between integers
512 no more than a word long. */
514 /* For truncation, usually we can just refer to FROM in a narrower mode. */
515 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
516 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
518 if (!((MEM_P (from)
519 && ! MEM_VOLATILE_P (from)
520 && direct_load[(int) to_mode]
521 && ! mode_dependent_address_p (XEXP (from, 0),
522 MEM_ADDR_SPACE (from)))
523 || REG_P (from)
524 || GET_CODE (from) == SUBREG))
525 from = force_reg (from_mode, from);
526 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
527 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
528 from = copy_to_reg (from);
529 emit_move_insn (to, gen_lowpart (to_mode, from));
530 return;
533 /* Handle extension. */
534 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
536 /* Convert directly if that works. */
537 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
538 != CODE_FOR_nothing)
540 emit_unop_insn (code, to, from, equiv_code);
541 return;
543 else
545 machine_mode intermediate;
546 rtx tmp;
547 int shift_amount;
549 /* Search for a mode to convert via. */
550 FOR_EACH_MODE_FROM (intermediate, from_mode)
551 if (((can_extend_p (to_mode, intermediate, unsignedp)
552 != CODE_FOR_nothing)
553 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
554 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
555 && (can_extend_p (intermediate, from_mode, unsignedp)
556 != CODE_FOR_nothing))
558 convert_move (to, convert_to_mode (intermediate, from,
559 unsignedp), unsignedp);
560 return;
563 /* No suitable intermediate mode.
564 Generate what we need with shifts. */
565 shift_amount = (GET_MODE_PRECISION (to_mode)
566 - GET_MODE_PRECISION (from_mode));
567 from = gen_lowpart (to_mode, force_reg (from_mode, from));
568 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
569 to, unsignedp);
570 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
571 to, unsignedp);
572 if (tmp != to)
573 emit_move_insn (to, tmp);
574 return;
578 /* Support special truncate insns for certain modes. */
579 if (convert_optab_handler (trunc_optab, to_mode,
580 from_mode) != CODE_FOR_nothing)
582 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
583 to, from, UNKNOWN);
584 return;
587 /* Handle truncation of volatile memrefs, and so on;
588 the things that couldn't be truncated directly,
589 and for which there was no special instruction.
591 ??? Code above formerly short-circuited this, for most integer
592 mode pairs, with a force_reg in from_mode followed by a recursive
593 call to this routine. Appears always to have been wrong. */
594 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
596 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
597 emit_move_insn (to, temp);
598 return;
601 /* Mode combination is not recognized. */
602 gcc_unreachable ();
605 /* Return an rtx for a value that would result
606 from converting X to mode MODE.
607 Both X and MODE may be floating, or both integer.
608 UNSIGNEDP is nonzero if X is an unsigned value.
609 This can be done by referring to a part of X in place
610 or by copying to a new temporary with conversion. */
613 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
615 return convert_modes (mode, VOIDmode, x, unsignedp);
618 /* Return an rtx for a value that would result
619 from converting X from mode OLDMODE to mode MODE.
620 Both modes may be floating, or both integer.
621 UNSIGNEDP is nonzero if X is an unsigned value.
623 This can be done by referring to a part of X in place
624 or by copying to a new temporary with conversion.
626 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
629 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
631 rtx temp;
633 /* If FROM is a SUBREG that indicates that we have already done at least
634 the required extension, strip it. */
636 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
637 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
638 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
639 x = gen_lowpart (mode, SUBREG_REG (x));
641 if (GET_MODE (x) != VOIDmode)
642 oldmode = GET_MODE (x);
644 if (mode == oldmode)
645 return x;
647 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
649 /* If the caller did not tell us the old mode, then there is not
650 much to do with respect to canonicalization. We have to
651 assume that all the bits are significant. */
652 if (GET_MODE_CLASS (oldmode) != MODE_INT)
653 oldmode = MAX_MODE_INT;
654 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
655 GET_MODE_PRECISION (mode),
656 unsignedp ? UNSIGNED : SIGNED);
657 return immed_wide_int_const (w, mode);
660 /* We can do this with a gen_lowpart if both desired and current modes
661 are integer, and this is either a constant integer, a register, or a
662 non-volatile MEM. */
663 if (GET_MODE_CLASS (mode) == MODE_INT
664 && GET_MODE_CLASS (oldmode) == MODE_INT
665 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
666 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
667 || (REG_P (x)
668 && (!HARD_REGISTER_P (x)
669 || HARD_REGNO_MODE_OK (REGNO (x), mode))
670 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
672 return gen_lowpart (mode, x);
674 /* Converting from integer constant into mode is always equivalent to an
675 subreg operation. */
676 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
678 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
679 return simplify_gen_subreg (mode, x, oldmode, 0);
682 temp = gen_reg_rtx (mode);
683 convert_move (temp, x, unsignedp);
684 return temp;
687 /* Return the largest alignment we can use for doing a move (or store)
688 of MAX_PIECES. ALIGN is the largest alignment we could use. */
690 static unsigned int
691 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
693 machine_mode tmode;
695 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
696 if (align >= GET_MODE_ALIGNMENT (tmode))
697 align = GET_MODE_ALIGNMENT (tmode);
698 else
700 machine_mode tmode, xmode;
702 xmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
703 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
705 if (GET_MODE_SIZE (tmode) > max_pieces
706 || SLOW_UNALIGNED_ACCESS (tmode, align))
707 break;
708 xmode = tmode;
711 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
714 return align;
717 /* Return the widest integer mode no wider than SIZE. If no such mode
718 can be found, return VOIDmode. */
720 static machine_mode
721 widest_int_mode_for_size (unsigned int size)
723 machine_mode tmode, mode = VOIDmode;
725 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
726 if (GET_MODE_SIZE (tmode) < size)
727 mode = tmode;
729 return mode;
732 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
733 and should be performed piecewise. */
735 static bool
736 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
737 enum by_pieces_operation op)
739 return targetm.use_by_pieces_infrastructure_p (len, align, op,
740 optimize_insn_for_speed_p ());
743 /* Determine whether the LEN bytes can be moved by using several move
744 instructions. Return nonzero if a call to move_by_pieces should
745 succeed. */
747 bool
748 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
750 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
753 /* Return number of insns required to perform operation OP by pieces
754 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
756 unsigned HOST_WIDE_INT
757 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
758 unsigned int max_size, by_pieces_operation op)
760 unsigned HOST_WIDE_INT n_insns = 0;
762 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
764 while (max_size > 1 && l > 0)
766 machine_mode mode;
767 enum insn_code icode;
769 mode = widest_int_mode_for_size (max_size);
771 if (mode == VOIDmode)
772 break;
773 unsigned int modesize = GET_MODE_SIZE (mode);
775 icode = optab_handler (mov_optab, mode);
776 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
778 unsigned HOST_WIDE_INT n_pieces = l / modesize;
779 l %= modesize;
780 switch (op)
782 default:
783 n_insns += n_pieces;
784 break;
786 case COMPARE_BY_PIECES:
787 int batch = targetm.compare_by_pieces_branch_ratio (mode);
788 int batch_ops = 4 * batch - 1;
789 unsigned HOST_WIDE_INT full = n_pieces / batch;
790 n_insns += full * batch_ops;
791 if (n_pieces % batch != 0)
792 n_insns++;
793 break;
797 max_size = modesize;
800 gcc_assert (!l);
801 return n_insns;
804 /* Used when performing piecewise block operations, holds information
805 about one of the memory objects involved. The member functions
806 can be used to generate code for loading from the object and
807 updating the address when iterating. */
809 class pieces_addr
811 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
812 stack pushes. */
813 rtx m_obj;
814 /* The address of the object. Can differ from that seen in the
815 MEM rtx if we copied the address to a register. */
816 rtx m_addr;
817 /* Nonzero if the address on the object has an autoincrement already,
818 signifies whether that was an increment or decrement. */
819 signed char m_addr_inc;
820 /* Nonzero if we intend to use autoinc without the address already
821 having autoinc form. We will insert add insns around each memory
822 reference, expecting later passes to form autoinc addressing modes.
823 The only supported options are predecrement and postincrement. */
824 signed char m_explicit_inc;
825 /* True if we have either of the two possible cases of using
826 autoincrement. */
827 bool m_auto;
828 /* True if this is an address to be used for load operations rather
829 than stores. */
830 bool m_is_load;
832 /* Optionally, a function to obtain constants for any given offset into
833 the objects, and data associated with it. */
834 by_pieces_constfn m_constfn;
835 void *m_cfndata;
836 public:
837 pieces_addr (rtx, bool, by_pieces_constfn, void *);
838 rtx adjust (machine_mode, HOST_WIDE_INT);
839 void increment_address (HOST_WIDE_INT);
840 void maybe_predec (HOST_WIDE_INT);
841 void maybe_postinc (HOST_WIDE_INT);
842 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
843 int get_addr_inc ()
845 return m_addr_inc;
849 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
850 true if the operation to be performed on this object is a load
851 rather than a store. For stores, OBJ can be NULL, in which case we
852 assume the operation is a stack push. For loads, the optional
853 CONSTFN and its associated CFNDATA can be used in place of the
854 memory load. */
856 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
857 void *cfndata)
858 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
860 m_addr_inc = 0;
861 m_auto = false;
862 if (obj)
864 rtx addr = XEXP (obj, 0);
865 rtx_code code = GET_CODE (addr);
866 m_addr = addr;
867 bool dec = code == PRE_DEC || code == POST_DEC;
868 bool inc = code == PRE_INC || code == POST_INC;
869 m_auto = inc || dec;
870 if (m_auto)
871 m_addr_inc = dec ? -1 : 1;
873 /* While we have always looked for these codes here, the code
874 implementing the memory operation has never handled them.
875 Support could be added later if necessary or beneficial. */
876 gcc_assert (code != PRE_INC && code != POST_DEC);
878 else
880 m_addr = NULL_RTX;
881 if (!is_load)
883 m_auto = true;
884 if (STACK_GROWS_DOWNWARD)
885 m_addr_inc = -1;
886 else
887 m_addr_inc = 1;
889 else
890 gcc_assert (constfn != NULL);
892 m_explicit_inc = 0;
893 if (constfn)
894 gcc_assert (is_load);
897 /* Decide whether to use autoinc for an address involved in a memory op.
898 MODE is the mode of the accesses, REVERSE is true if we've decided to
899 perform the operation starting from the end, and LEN is the length of
900 the operation. Don't override an earlier decision to set m_auto. */
902 void
903 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
904 HOST_WIDE_INT len)
906 if (m_auto || m_obj == NULL_RTX)
907 return;
909 bool use_predec = (m_is_load
910 ? USE_LOAD_PRE_DECREMENT (mode)
911 : USE_STORE_PRE_DECREMENT (mode));
912 bool use_postinc = (m_is_load
913 ? USE_LOAD_POST_INCREMENT (mode)
914 : USE_STORE_POST_INCREMENT (mode));
915 machine_mode addr_mode = get_address_mode (m_obj);
917 if (use_predec && reverse)
919 m_addr = copy_to_mode_reg (addr_mode,
920 plus_constant (addr_mode,
921 m_addr, len));
922 m_auto = true;
923 m_explicit_inc = -1;
925 else if (use_postinc && !reverse)
927 m_addr = copy_to_mode_reg (addr_mode, m_addr);
928 m_auto = true;
929 m_explicit_inc = 1;
931 else if (CONSTANT_P (m_addr))
932 m_addr = copy_to_mode_reg (addr_mode, m_addr);
935 /* Adjust the address to refer to the data at OFFSET in MODE. If we
936 are using autoincrement for this address, we don't add the offset,
937 but we still modify the MEM's properties. */
940 pieces_addr::adjust (machine_mode mode, HOST_WIDE_INT offset)
942 if (m_constfn)
943 return m_constfn (m_cfndata, offset, mode);
944 if (m_obj == NULL_RTX)
945 return NULL_RTX;
946 if (m_auto)
947 return adjust_automodify_address (m_obj, mode, m_addr, offset);
948 else
949 return adjust_address (m_obj, mode, offset);
952 /* Emit an add instruction to increment the address by SIZE. */
954 void
955 pieces_addr::increment_address (HOST_WIDE_INT size)
957 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
958 emit_insn (gen_add2_insn (m_addr, amount));
961 /* If we are supposed to decrement the address after each access, emit code
962 to do so now. Increment by SIZE (which has should have the correct sign
963 already). */
965 void
966 pieces_addr::maybe_predec (HOST_WIDE_INT size)
968 if (m_explicit_inc >= 0)
969 return;
970 gcc_assert (HAVE_PRE_DECREMENT);
971 increment_address (size);
974 /* If we are supposed to decrement the address after each access, emit code
975 to do so now. Increment by SIZE. */
977 void
978 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
980 if (m_explicit_inc <= 0)
981 return;
982 gcc_assert (HAVE_POST_INCREMENT);
983 increment_address (size);
986 /* This structure is used by do_op_by_pieces to describe the operation
987 to be performed. */
989 class op_by_pieces_d
991 protected:
992 pieces_addr m_to, m_from;
993 unsigned HOST_WIDE_INT m_len;
994 HOST_WIDE_INT m_offset;
995 unsigned int m_align;
996 unsigned int m_max_size;
997 bool m_reverse;
999 /* Virtual functions, overriden by derived classes for the specific
1000 operation. */
1001 virtual void generate (rtx, rtx, machine_mode) = 0;
1002 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1003 virtual void finish_mode (machine_mode)
1007 public:
1008 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1009 unsigned HOST_WIDE_INT, unsigned int);
1010 void run ();
1013 /* The constructor for an op_by_pieces_d structure. We require two
1014 objects named TO and FROM, which are identified as loads or stores
1015 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1016 and its associated FROM_CFN_DATA can be used to replace loads with
1017 constant values. LEN describes the length of the operation. */
1019 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1020 rtx from, bool from_load,
1021 by_pieces_constfn from_cfn,
1022 void *from_cfn_data,
1023 unsigned HOST_WIDE_INT len,
1024 unsigned int align)
1025 : m_to (to, to_load, NULL, NULL),
1026 m_from (from, from_load, from_cfn, from_cfn_data),
1027 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1029 int toi = m_to.get_addr_inc ();
1030 int fromi = m_from.get_addr_inc ();
1031 if (toi >= 0 && fromi >= 0)
1032 m_reverse = false;
1033 else if (toi <= 0 && fromi <= 0)
1034 m_reverse = true;
1035 else
1036 gcc_unreachable ();
1038 m_offset = m_reverse ? len : 0;
1039 align = MIN (to ? MEM_ALIGN (to) : align,
1040 from ? MEM_ALIGN (from) : align);
1042 /* If copying requires more than two move insns,
1043 copy addresses to registers (to make displacements shorter)
1044 and use post-increment if available. */
1045 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1047 /* Find the mode of the largest comparison. */
1048 machine_mode mode = widest_int_mode_for_size (m_max_size);
1050 m_from.decide_autoinc (mode, m_reverse, len);
1051 m_to.decide_autoinc (mode, m_reverse, len);
1054 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1055 m_align = align;
1058 /* This function contains the main loop used for expanding a block
1059 operation. First move what we can in the largest integer mode,
1060 then go to successively smaller modes. For every access, call
1061 GENFUN with the two operands and the EXTRA_DATA. */
1063 void
1064 op_by_pieces_d::run ()
1066 while (m_max_size > 1 && m_len > 0)
1068 machine_mode mode = widest_int_mode_for_size (m_max_size);
1070 if (mode == VOIDmode)
1071 break;
1073 if (prepare_mode (mode, m_align))
1075 unsigned int size = GET_MODE_SIZE (mode);
1076 rtx to1 = NULL_RTX, from1;
1078 while (m_len >= size)
1080 if (m_reverse)
1081 m_offset -= size;
1083 to1 = m_to.adjust (mode, m_offset);
1084 from1 = m_from.adjust (mode, m_offset);
1086 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1087 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1089 generate (to1, from1, mode);
1091 m_to.maybe_postinc (size);
1092 m_from.maybe_postinc (size);
1094 if (!m_reverse)
1095 m_offset += size;
1097 m_len -= size;
1100 finish_mode (mode);
1103 m_max_size = GET_MODE_SIZE (mode);
1106 /* The code above should have handled everything. */
1107 gcc_assert (!m_len);
1110 /* Derived class from op_by_pieces_d, providing support for block move
1111 operations. */
1113 class move_by_pieces_d : public op_by_pieces_d
1115 insn_gen_fn m_gen_fun;
1116 void generate (rtx, rtx, machine_mode);
1117 bool prepare_mode (machine_mode, unsigned int);
1119 public:
1120 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1121 unsigned int align)
1122 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1125 rtx finish_endp (int);
1128 /* Return true if MODE can be used for a set of copies, given an
1129 alignment ALIGN. Prepare whatever data is necessary for later
1130 calls to generate. */
1132 bool
1133 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1135 insn_code icode = optab_handler (mov_optab, mode);
1136 m_gen_fun = GEN_FCN (icode);
1137 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1140 /* A callback used when iterating for a compare_by_pieces_operation.
1141 OP0 and OP1 are the values that have been loaded and should be
1142 compared in MODE. If OP0 is NULL, this means we should generate a
1143 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1144 gen function that should be used to generate the mode. */
1146 void
1147 move_by_pieces_d::generate (rtx op0, rtx op1,
1148 machine_mode mode ATTRIBUTE_UNUSED)
1150 #ifdef PUSH_ROUNDING
1151 if (op0 == NULL_RTX)
1153 emit_single_push_insn (mode, op1, NULL);
1154 return;
1156 #endif
1157 emit_insn (m_gen_fun (op0, op1));
1160 /* Perform the final adjustment at the end of a string to obtain the
1161 correct return value for the block operation. If ENDP is 1 return
1162 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1163 end minus one byte ala stpcpy. */
1166 move_by_pieces_d::finish_endp (int endp)
1168 gcc_assert (!m_reverse);
1169 if (endp == 2)
1171 m_to.maybe_postinc (-1);
1172 --m_offset;
1174 return m_to.adjust (QImode, m_offset);
1177 /* Generate several move instructions to copy LEN bytes from block FROM to
1178 block TO. (These are MEM rtx's with BLKmode).
1180 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1181 used to push FROM to the stack.
1183 ALIGN is maximum stack alignment we can assume.
1185 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1186 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1187 stpcpy. */
1190 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1191 unsigned int align, int endp)
1193 #ifndef PUSH_ROUNDING
1194 if (to == NULL)
1195 gcc_unreachable ();
1196 #endif
1198 move_by_pieces_d data (to, from, len, align);
1200 data.run ();
1202 if (endp)
1203 return data.finish_endp (endp);
1204 else
1205 return to;
1208 /* Derived class from op_by_pieces_d, providing support for block move
1209 operations. */
1211 class store_by_pieces_d : public op_by_pieces_d
1213 insn_gen_fn m_gen_fun;
1214 void generate (rtx, rtx, machine_mode);
1215 bool prepare_mode (machine_mode, unsigned int);
1217 public:
1218 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1219 unsigned HOST_WIDE_INT len, unsigned int align)
1220 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1223 rtx finish_endp (int);
1226 /* Return true if MODE can be used for a set of stores, given an
1227 alignment ALIGN. Prepare whatever data is necessary for later
1228 calls to generate. */
1230 bool
1231 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1233 insn_code icode = optab_handler (mov_optab, mode);
1234 m_gen_fun = GEN_FCN (icode);
1235 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1238 /* A callback used when iterating for a store_by_pieces_operation.
1239 OP0 and OP1 are the values that have been loaded and should be
1240 compared in MODE. If OP0 is NULL, this means we should generate a
1241 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1242 gen function that should be used to generate the mode. */
1244 void
1245 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1247 emit_insn (m_gen_fun (op0, op1));
1250 /* Perform the final adjustment at the end of a string to obtain the
1251 correct return value for the block operation. If ENDP is 1 return
1252 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1253 end minus one byte ala stpcpy. */
1256 store_by_pieces_d::finish_endp (int endp)
1258 gcc_assert (!m_reverse);
1259 if (endp == 2)
1261 m_to.maybe_postinc (-1);
1262 --m_offset;
1264 return m_to.adjust (QImode, m_offset);
1267 /* Determine whether the LEN bytes generated by CONSTFUN can be
1268 stored to memory using several move instructions. CONSTFUNDATA is
1269 a pointer which will be passed as argument in every CONSTFUN call.
1270 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1271 a memset operation and false if it's a copy of a constant string.
1272 Return nonzero if a call to store_by_pieces should succeed. */
1275 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1276 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1277 void *constfundata, unsigned int align, bool memsetp)
1279 unsigned HOST_WIDE_INT l;
1280 unsigned int max_size;
1281 HOST_WIDE_INT offset = 0;
1282 machine_mode mode;
1283 enum insn_code icode;
1284 int reverse;
1285 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1286 rtx cst ATTRIBUTE_UNUSED;
1288 if (len == 0)
1289 return 1;
1291 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1292 memsetp
1293 ? SET_BY_PIECES
1294 : STORE_BY_PIECES,
1295 optimize_insn_for_speed_p ()))
1296 return 0;
1298 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1300 /* We would first store what we can in the largest integer mode, then go to
1301 successively smaller modes. */
1303 for (reverse = 0;
1304 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1305 reverse++)
1307 l = len;
1308 max_size = STORE_MAX_PIECES + 1;
1309 while (max_size > 1 && l > 0)
1311 mode = widest_int_mode_for_size (max_size);
1313 if (mode == VOIDmode)
1314 break;
1316 icode = optab_handler (mov_optab, mode);
1317 if (icode != CODE_FOR_nothing
1318 && align >= GET_MODE_ALIGNMENT (mode))
1320 unsigned int size = GET_MODE_SIZE (mode);
1322 while (l >= size)
1324 if (reverse)
1325 offset -= size;
1327 cst = (*constfun) (constfundata, offset, mode);
1328 if (!targetm.legitimate_constant_p (mode, cst))
1329 return 0;
1331 if (!reverse)
1332 offset += size;
1334 l -= size;
1338 max_size = GET_MODE_SIZE (mode);
1341 /* The code above should have handled everything. */
1342 gcc_assert (!l);
1345 return 1;
1348 /* Generate several move instructions to store LEN bytes generated by
1349 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1350 pointer which will be passed as argument in every CONSTFUN call.
1351 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1352 a memset operation and false if it's a copy of a constant string.
1353 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1354 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1355 stpcpy. */
1358 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1359 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1360 void *constfundata, unsigned int align, bool memsetp, int endp)
1362 if (len == 0)
1364 gcc_assert (endp != 2);
1365 return to;
1368 gcc_assert (targetm.use_by_pieces_infrastructure_p
1369 (len, align,
1370 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1371 optimize_insn_for_speed_p ()));
1373 store_by_pieces_d data (to, constfun, constfundata, len, align);
1374 data.run ();
1376 if (endp)
1377 return data.finish_endp (endp);
1378 else
1379 return to;
1382 /* Callback routine for clear_by_pieces.
1383 Return const0_rtx unconditionally. */
1385 static rtx
1386 clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode)
1388 return const0_rtx;
1391 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1392 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1394 static void
1395 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1397 if (len == 0)
1398 return;
1400 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1401 data.run ();
1404 /* Context used by compare_by_pieces_genfn. It stores the fail label
1405 to jump to in case of miscomparison, and for branch ratios greater than 1,
1406 it stores an accumulator and the current and maximum counts before
1407 emitting another branch. */
1409 class compare_by_pieces_d : public op_by_pieces_d
1411 rtx_code_label *m_fail_label;
1412 rtx m_accumulator;
1413 int m_count, m_batch;
1415 void generate (rtx, rtx, machine_mode);
1416 bool prepare_mode (machine_mode, unsigned int);
1417 void finish_mode (machine_mode);
1418 public:
1419 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1420 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1421 rtx_code_label *fail_label)
1422 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1424 m_fail_label = fail_label;
1428 /* A callback used when iterating for a compare_by_pieces_operation.
1429 OP0 and OP1 are the values that have been loaded and should be
1430 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1431 context structure. */
1433 void
1434 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1436 if (m_batch > 1)
1438 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1439 true, OPTAB_LIB_WIDEN);
1440 if (m_count != 0)
1441 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1442 true, OPTAB_LIB_WIDEN);
1443 m_accumulator = temp;
1445 if (++m_count < m_batch)
1446 return;
1448 m_count = 0;
1449 op0 = m_accumulator;
1450 op1 = const0_rtx;
1451 m_accumulator = NULL_RTX;
1453 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1454 m_fail_label, profile_probability::uninitialized ());
1457 /* Return true if MODE can be used for a set of moves and comparisons,
1458 given an alignment ALIGN. Prepare whatever data is necessary for
1459 later calls to generate. */
1461 bool
1462 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1464 insn_code icode = optab_handler (mov_optab, mode);
1465 if (icode == CODE_FOR_nothing
1466 || align < GET_MODE_ALIGNMENT (mode)
1467 || !can_compare_p (EQ, mode, ccp_jump))
1468 return false;
1469 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1470 if (m_batch < 0)
1471 return false;
1472 m_accumulator = NULL_RTX;
1473 m_count = 0;
1474 return true;
1477 /* Called after expanding a series of comparisons in MODE. If we have
1478 accumulated results for which we haven't emitted a branch yet, do
1479 so now. */
1481 void
1482 compare_by_pieces_d::finish_mode (machine_mode mode)
1484 if (m_accumulator != NULL_RTX)
1485 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1486 NULL_RTX, NULL, m_fail_label,
1487 profile_probability::uninitialized ());
1490 /* Generate several move instructions to compare LEN bytes from blocks
1491 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1493 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1494 used to push FROM to the stack.
1496 ALIGN is maximum stack alignment we can assume.
1498 Optionally, the caller can pass a constfn and associated data in A1_CFN
1499 and A1_CFN_DATA. describing that the second operand being compared is a
1500 known constant and how to obtain its data. */
1502 static rtx
1503 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1504 rtx target, unsigned int align,
1505 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1507 rtx_code_label *fail_label = gen_label_rtx ();
1508 rtx_code_label *end_label = gen_label_rtx ();
1510 if (target == NULL_RTX
1511 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1512 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1514 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1515 fail_label);
1517 data.run ();
1519 emit_move_insn (target, const0_rtx);
1520 emit_jump (end_label);
1521 emit_barrier ();
1522 emit_label (fail_label);
1523 emit_move_insn (target, const1_rtx);
1524 emit_label (end_label);
1526 return target;
1529 /* Emit code to move a block Y to a block X. This may be done with
1530 string-move instructions, with multiple scalar move instructions,
1531 or with a library call.
1533 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1534 SIZE is an rtx that says how long they are.
1535 ALIGN is the maximum alignment we can assume they have.
1536 METHOD describes what kind of copy this is, and what mechanisms may be used.
1537 MIN_SIZE is the minimal size of block to move
1538 MAX_SIZE is the maximal size of block to move, if it can not be represented
1539 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1541 Return the address of the new block, if memcpy is called and returns it,
1542 0 otherwise. */
1545 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1546 unsigned int expected_align, HOST_WIDE_INT expected_size,
1547 unsigned HOST_WIDE_INT min_size,
1548 unsigned HOST_WIDE_INT max_size,
1549 unsigned HOST_WIDE_INT probable_max_size)
1551 bool may_use_call;
1552 rtx retval = 0;
1553 unsigned int align;
1555 gcc_assert (size);
1556 if (CONST_INT_P (size) && INTVAL (size) == 0)
1557 return 0;
1559 switch (method)
1561 case BLOCK_OP_NORMAL:
1562 case BLOCK_OP_TAILCALL:
1563 may_use_call = true;
1564 break;
1566 case BLOCK_OP_CALL_PARM:
1567 may_use_call = block_move_libcall_safe_for_call_parm ();
1569 /* Make inhibit_defer_pop nonzero around the library call
1570 to force it to pop the arguments right away. */
1571 NO_DEFER_POP;
1572 break;
1574 case BLOCK_OP_NO_LIBCALL:
1575 may_use_call = false;
1576 break;
1578 default:
1579 gcc_unreachable ();
1582 gcc_assert (MEM_P (x) && MEM_P (y));
1583 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1584 gcc_assert (align >= BITS_PER_UNIT);
1586 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1587 block copy is more efficient for other large modes, e.g. DCmode. */
1588 x = adjust_address (x, BLKmode, 0);
1589 y = adjust_address (y, BLKmode, 0);
1591 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1592 can be incorrect is coming from __builtin_memcpy. */
1593 if (CONST_INT_P (size))
1595 x = shallow_copy_rtx (x);
1596 y = shallow_copy_rtx (y);
1597 set_mem_size (x, INTVAL (size));
1598 set_mem_size (y, INTVAL (size));
1601 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1602 move_by_pieces (x, y, INTVAL (size), align, 0);
1603 else if (emit_block_move_via_movmem (x, y, size, align,
1604 expected_align, expected_size,
1605 min_size, max_size, probable_max_size))
1607 else if (may_use_call
1608 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1609 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1611 /* Since x and y are passed to a libcall, mark the corresponding
1612 tree EXPR as addressable. */
1613 tree y_expr = MEM_EXPR (y);
1614 tree x_expr = MEM_EXPR (x);
1615 if (y_expr)
1616 mark_addressable (y_expr);
1617 if (x_expr)
1618 mark_addressable (x_expr);
1619 retval = emit_block_copy_via_libcall (x, y, size,
1620 method == BLOCK_OP_TAILCALL);
1623 else
1624 emit_block_move_via_loop (x, y, size, align);
1626 if (method == BLOCK_OP_CALL_PARM)
1627 OK_DEFER_POP;
1629 return retval;
1633 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1635 unsigned HOST_WIDE_INT max, min = 0;
1636 if (GET_CODE (size) == CONST_INT)
1637 min = max = UINTVAL (size);
1638 else
1639 max = GET_MODE_MASK (GET_MODE (size));
1640 return emit_block_move_hints (x, y, size, method, 0, -1,
1641 min, max, max);
1644 /* A subroutine of emit_block_move. Returns true if calling the
1645 block move libcall will not clobber any parameters which may have
1646 already been placed on the stack. */
1648 static bool
1649 block_move_libcall_safe_for_call_parm (void)
1651 #if defined (REG_PARM_STACK_SPACE)
1652 tree fn;
1653 #endif
1655 /* If arguments are pushed on the stack, then they're safe. */
1656 if (PUSH_ARGS)
1657 return true;
1659 /* If registers go on the stack anyway, any argument is sure to clobber
1660 an outgoing argument. */
1661 #if defined (REG_PARM_STACK_SPACE)
1662 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1663 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1664 depend on its argument. */
1665 (void) fn;
1666 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1667 && REG_PARM_STACK_SPACE (fn) != 0)
1668 return false;
1669 #endif
1671 /* If any argument goes in memory, then it might clobber an outgoing
1672 argument. */
1674 CUMULATIVE_ARGS args_so_far_v;
1675 cumulative_args_t args_so_far;
1676 tree fn, arg;
1678 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1679 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1680 args_so_far = pack_cumulative_args (&args_so_far_v);
1682 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1683 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1685 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1686 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1687 NULL_TREE, true);
1688 if (!tmp || !REG_P (tmp))
1689 return false;
1690 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1691 return false;
1692 targetm.calls.function_arg_advance (args_so_far, mode,
1693 NULL_TREE, true);
1696 return true;
1699 /* A subroutine of emit_block_move. Expand a movmem pattern;
1700 return true if successful. */
1702 static bool
1703 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1704 unsigned int expected_align, HOST_WIDE_INT expected_size,
1705 unsigned HOST_WIDE_INT min_size,
1706 unsigned HOST_WIDE_INT max_size,
1707 unsigned HOST_WIDE_INT probable_max_size)
1709 int save_volatile_ok = volatile_ok;
1710 machine_mode mode;
1712 if (expected_align < align)
1713 expected_align = align;
1714 if (expected_size != -1)
1716 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1717 expected_size = probable_max_size;
1718 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1719 expected_size = min_size;
1722 /* Since this is a move insn, we don't care about volatility. */
1723 volatile_ok = 1;
1725 /* Try the most limited insn first, because there's no point
1726 including more than one in the machine description unless
1727 the more limited one has some advantage. */
1729 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
1731 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1733 if (code != CODE_FOR_nothing
1734 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1735 here because if SIZE is less than the mode mask, as it is
1736 returned by the macro, it will definitely be less than the
1737 actual mode mask. Since SIZE is within the Pmode address
1738 space, we limit MODE to Pmode. */
1739 && ((CONST_INT_P (size)
1740 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1741 <= (GET_MODE_MASK (mode) >> 1)))
1742 || max_size <= (GET_MODE_MASK (mode) >> 1)
1743 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1745 struct expand_operand ops[9];
1746 unsigned int nops;
1748 /* ??? When called via emit_block_move_for_call, it'd be
1749 nice if there were some way to inform the backend, so
1750 that it doesn't fail the expansion because it thinks
1751 emitting the libcall would be more efficient. */
1752 nops = insn_data[(int) code].n_generator_args;
1753 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1755 create_fixed_operand (&ops[0], x);
1756 create_fixed_operand (&ops[1], y);
1757 /* The check above guarantees that this size conversion is valid. */
1758 create_convert_operand_to (&ops[2], size, mode, true);
1759 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1760 if (nops >= 6)
1762 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1763 create_integer_operand (&ops[5], expected_size);
1765 if (nops >= 8)
1767 create_integer_operand (&ops[6], min_size);
1768 /* If we can not represent the maximal size,
1769 make parameter NULL. */
1770 if ((HOST_WIDE_INT) max_size != -1)
1771 create_integer_operand (&ops[7], max_size);
1772 else
1773 create_fixed_operand (&ops[7], NULL);
1775 if (nops == 9)
1777 /* If we can not represent the maximal size,
1778 make parameter NULL. */
1779 if ((HOST_WIDE_INT) probable_max_size != -1)
1780 create_integer_operand (&ops[8], probable_max_size);
1781 else
1782 create_fixed_operand (&ops[8], NULL);
1784 if (maybe_expand_insn (code, nops, ops))
1786 volatile_ok = save_volatile_ok;
1787 return true;
1792 volatile_ok = save_volatile_ok;
1793 return false;
1796 /* A subroutine of emit_block_move. Copy the data via an explicit
1797 loop. This is used only when libcalls are forbidden. */
1798 /* ??? It'd be nice to copy in hunks larger than QImode. */
1800 static void
1801 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1802 unsigned int align ATTRIBUTE_UNUSED)
1804 rtx_code_label *cmp_label, *top_label;
1805 rtx iter, x_addr, y_addr, tmp;
1806 machine_mode x_addr_mode = get_address_mode (x);
1807 machine_mode y_addr_mode = get_address_mode (y);
1808 machine_mode iter_mode;
1810 iter_mode = GET_MODE (size);
1811 if (iter_mode == VOIDmode)
1812 iter_mode = word_mode;
1814 top_label = gen_label_rtx ();
1815 cmp_label = gen_label_rtx ();
1816 iter = gen_reg_rtx (iter_mode);
1818 emit_move_insn (iter, const0_rtx);
1820 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1821 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1822 do_pending_stack_adjust ();
1824 emit_jump (cmp_label);
1825 emit_label (top_label);
1827 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1828 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1830 if (x_addr_mode != y_addr_mode)
1831 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1832 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1834 x = change_address (x, QImode, x_addr);
1835 y = change_address (y, QImode, y_addr);
1837 emit_move_insn (x, y);
1839 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1840 true, OPTAB_LIB_WIDEN);
1841 if (tmp != iter)
1842 emit_move_insn (iter, tmp);
1844 emit_label (cmp_label);
1846 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1847 true, top_label,
1848 profile_probability::guessed_always ()
1849 .apply_scale (9, 10));
1852 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1853 TAILCALL is true if this is a tail call. */
1856 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1857 rtx size, bool tailcall)
1859 rtx dst_addr, src_addr;
1860 tree call_expr, dst_tree, src_tree, size_tree;
1861 machine_mode size_mode;
1863 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1864 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1865 dst_tree = make_tree (ptr_type_node, dst_addr);
1867 src_addr = copy_addr_to_reg (XEXP (src, 0));
1868 src_addr = convert_memory_address (ptr_mode, src_addr);
1869 src_tree = make_tree (ptr_type_node, src_addr);
1871 size_mode = TYPE_MODE (sizetype);
1872 size = convert_to_mode (size_mode, size, 1);
1873 size = copy_to_mode_reg (size_mode, size);
1874 size_tree = make_tree (sizetype, size);
1876 /* It is incorrect to use the libcall calling conventions for calls to
1877 memcpy/memmove/memcmp because they can be provided by the user. */
1878 tree fn = builtin_decl_implicit (fncode);
1879 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1880 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1882 return expand_call (call_expr, NULL_RTX, false);
1885 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1886 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1887 otherwise return null. */
1890 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1891 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1892 HOST_WIDE_INT align)
1894 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1896 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1897 target = NULL_RTX;
1899 struct expand_operand ops[5];
1900 create_output_operand (&ops[0], target, insn_mode);
1901 create_fixed_operand (&ops[1], arg1_rtx);
1902 create_fixed_operand (&ops[2], arg2_rtx);
1903 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1904 TYPE_UNSIGNED (arg3_type));
1905 create_integer_operand (&ops[4], align);
1906 if (maybe_expand_insn (icode, 5, ops))
1907 return ops[0].value;
1908 return NULL_RTX;
1911 /* Expand a block compare between X and Y with length LEN using the
1912 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1913 of the expression that was used to calculate the length. ALIGN
1914 gives the known minimum common alignment. */
1916 static rtx
1917 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1918 unsigned align)
1920 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1921 implementing memcmp because it will stop if it encounters two
1922 zero bytes. */
1923 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1925 if (icode == CODE_FOR_nothing)
1926 return NULL_RTX;
1928 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1931 /* Emit code to compare a block Y to a block X. This may be done with
1932 string-compare instructions, with multiple scalar instructions,
1933 or with a library call.
1935 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1936 they are. LEN_TYPE is the type of the expression that was used to
1937 calculate it.
1939 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1940 value of a normal memcmp call, instead we can just compare for equality.
1941 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1942 returning NULL_RTX.
1944 Optionally, the caller can pass a constfn and associated data in Y_CFN
1945 and Y_CFN_DATA. describing that the second operand being compared is a
1946 known constant and how to obtain its data.
1947 Return the result of the comparison, or NULL_RTX if we failed to
1948 perform the operation. */
1951 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1952 bool equality_only, by_pieces_constfn y_cfn,
1953 void *y_cfndata)
1955 rtx result = 0;
1957 if (CONST_INT_P (len) && INTVAL (len) == 0)
1958 return const0_rtx;
1960 gcc_assert (MEM_P (x) && MEM_P (y));
1961 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1962 gcc_assert (align >= BITS_PER_UNIT);
1964 x = adjust_address (x, BLKmode, 0);
1965 y = adjust_address (y, BLKmode, 0);
1967 if (equality_only
1968 && CONST_INT_P (len)
1969 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1970 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1971 y_cfn, y_cfndata);
1972 else
1973 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1975 return result;
1978 /* Copy all or part of a value X into registers starting at REGNO.
1979 The number of registers to be filled is NREGS. */
1981 void
1982 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1984 if (nregs == 0)
1985 return;
1987 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1988 x = validize_mem (force_const_mem (mode, x));
1990 /* See if the machine can do this with a load multiple insn. */
1991 if (targetm.have_load_multiple ())
1993 rtx_insn *last = get_last_insn ();
1994 rtx first = gen_rtx_REG (word_mode, regno);
1995 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1996 GEN_INT (nregs)))
1998 emit_insn (pat);
1999 return;
2001 else
2002 delete_insns_since (last);
2005 for (int i = 0; i < nregs; i++)
2006 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2007 operand_subword_force (x, i, mode));
2010 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2011 The number of registers to be filled is NREGS. */
2013 void
2014 move_block_from_reg (int regno, rtx x, int nregs)
2016 if (nregs == 0)
2017 return;
2019 /* See if the machine can do this with a store multiple insn. */
2020 if (targetm.have_store_multiple ())
2022 rtx_insn *last = get_last_insn ();
2023 rtx first = gen_rtx_REG (word_mode, regno);
2024 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2025 GEN_INT (nregs)))
2027 emit_insn (pat);
2028 return;
2030 else
2031 delete_insns_since (last);
2034 for (int i = 0; i < nregs; i++)
2036 rtx tem = operand_subword (x, i, 1, BLKmode);
2038 gcc_assert (tem);
2040 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2044 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2045 ORIG, where ORIG is a non-consecutive group of registers represented by
2046 a PARALLEL. The clone is identical to the original except in that the
2047 original set of registers is replaced by a new set of pseudo registers.
2048 The new set has the same modes as the original set. */
2051 gen_group_rtx (rtx orig)
2053 int i, length;
2054 rtx *tmps;
2056 gcc_assert (GET_CODE (orig) == PARALLEL);
2058 length = XVECLEN (orig, 0);
2059 tmps = XALLOCAVEC (rtx, length);
2061 /* Skip a NULL entry in first slot. */
2062 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2064 if (i)
2065 tmps[0] = 0;
2067 for (; i < length; i++)
2069 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2070 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2072 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2075 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2078 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2079 except that values are placed in TMPS[i], and must later be moved
2080 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2082 static void
2083 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2085 rtx src;
2086 int start, i;
2087 machine_mode m = GET_MODE (orig_src);
2089 gcc_assert (GET_CODE (dst) == PARALLEL);
2091 if (m != VOIDmode
2092 && !SCALAR_INT_MODE_P (m)
2093 && !MEM_P (orig_src)
2094 && GET_CODE (orig_src) != CONCAT)
2096 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
2097 if (imode == BLKmode)
2098 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2099 else
2100 src = gen_reg_rtx (imode);
2101 if (imode != BLKmode)
2102 src = gen_lowpart (GET_MODE (orig_src), src);
2103 emit_move_insn (src, orig_src);
2104 /* ...and back again. */
2105 if (imode != BLKmode)
2106 src = gen_lowpart (imode, src);
2107 emit_group_load_1 (tmps, dst, src, type, ssize);
2108 return;
2111 /* Check for a NULL entry, used to indicate that the parameter goes
2112 both on the stack and in registers. */
2113 if (XEXP (XVECEXP (dst, 0, 0), 0))
2114 start = 0;
2115 else
2116 start = 1;
2118 /* Process the pieces. */
2119 for (i = start; i < XVECLEN (dst, 0); i++)
2121 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2122 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2123 unsigned int bytelen = GET_MODE_SIZE (mode);
2124 int shift = 0;
2126 /* Handle trailing fragments that run over the size of the struct. */
2127 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2129 /* Arrange to shift the fragment to where it belongs.
2130 extract_bit_field loads to the lsb of the reg. */
2131 if (
2132 #ifdef BLOCK_REG_PADDING
2133 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2134 == (BYTES_BIG_ENDIAN ? upward : downward)
2135 #else
2136 BYTES_BIG_ENDIAN
2137 #endif
2139 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2140 bytelen = ssize - bytepos;
2141 gcc_assert (bytelen > 0);
2144 /* If we won't be loading directly from memory, protect the real source
2145 from strange tricks we might play; but make sure that the source can
2146 be loaded directly into the destination. */
2147 src = orig_src;
2148 if (!MEM_P (orig_src)
2149 && (!CONSTANT_P (orig_src)
2150 || (GET_MODE (orig_src) != mode
2151 && GET_MODE (orig_src) != VOIDmode)))
2153 if (GET_MODE (orig_src) == VOIDmode)
2154 src = gen_reg_rtx (mode);
2155 else
2156 src = gen_reg_rtx (GET_MODE (orig_src));
2158 emit_move_insn (src, orig_src);
2161 /* Optimize the access just a bit. */
2162 if (MEM_P (src)
2163 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2164 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2165 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2166 && bytelen == GET_MODE_SIZE (mode))
2168 tmps[i] = gen_reg_rtx (mode);
2169 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2171 else if (COMPLEX_MODE_P (mode)
2172 && GET_MODE (src) == mode
2173 && bytelen == GET_MODE_SIZE (mode))
2174 /* Let emit_move_complex do the bulk of the work. */
2175 tmps[i] = src;
2176 else if (GET_CODE (src) == CONCAT)
2178 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2179 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2180 unsigned int elt = bytepos / slen0;
2181 unsigned int subpos = bytepos % slen0;
2183 if (subpos + bytelen <= slen0)
2185 /* The following assumes that the concatenated objects all
2186 have the same size. In this case, a simple calculation
2187 can be used to determine the object and the bit field
2188 to be extracted. */
2189 tmps[i] = XEXP (src, elt);
2190 if (subpos != 0
2191 || subpos + bytelen != slen0
2192 || (!CONSTANT_P (tmps[i])
2193 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2194 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2195 subpos * BITS_PER_UNIT,
2196 1, NULL_RTX, mode, mode, false,
2197 NULL);
2199 else
2201 rtx mem;
2203 gcc_assert (!bytepos);
2204 mem = assign_stack_temp (GET_MODE (src), slen);
2205 emit_move_insn (mem, src);
2206 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2207 0, 1, NULL_RTX, mode, mode, false,
2208 NULL);
2211 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2212 SIMD register, which is currently broken. While we get GCC
2213 to emit proper RTL for these cases, let's dump to memory. */
2214 else if (VECTOR_MODE_P (GET_MODE (dst))
2215 && REG_P (src))
2217 int slen = GET_MODE_SIZE (GET_MODE (src));
2218 rtx mem;
2220 mem = assign_stack_temp (GET_MODE (src), slen);
2221 emit_move_insn (mem, src);
2222 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2224 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2225 && XVECLEN (dst, 0) > 1)
2226 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2227 else if (CONSTANT_P (src))
2229 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2231 if (len == ssize)
2232 tmps[i] = src;
2233 else
2235 rtx first, second;
2237 /* TODO: const_wide_int can have sizes other than this... */
2238 gcc_assert (2 * len == ssize);
2239 split_double (src, &first, &second);
2240 if (i)
2241 tmps[i] = second;
2242 else
2243 tmps[i] = first;
2246 else if (REG_P (src) && GET_MODE (src) == mode)
2247 tmps[i] = src;
2248 else
2249 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2250 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2251 mode, mode, false, NULL);
2253 if (shift)
2254 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2255 shift, tmps[i], 0);
2259 /* Emit code to move a block SRC of type TYPE to a block DST,
2260 where DST is non-consecutive registers represented by a PARALLEL.
2261 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2262 if not known. */
2264 void
2265 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2267 rtx *tmps;
2268 int i;
2270 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2271 emit_group_load_1 (tmps, dst, src, type, ssize);
2273 /* Copy the extracted pieces into the proper (probable) hard regs. */
2274 for (i = 0; i < XVECLEN (dst, 0); i++)
2276 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2277 if (d == NULL)
2278 continue;
2279 emit_move_insn (d, tmps[i]);
2283 /* Similar, but load SRC into new pseudos in a format that looks like
2284 PARALLEL. This can later be fed to emit_group_move to get things
2285 in the right place. */
2288 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2290 rtvec vec;
2291 int i;
2293 vec = rtvec_alloc (XVECLEN (parallel, 0));
2294 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2296 /* Convert the vector to look just like the original PARALLEL, except
2297 with the computed values. */
2298 for (i = 0; i < XVECLEN (parallel, 0); i++)
2300 rtx e = XVECEXP (parallel, 0, i);
2301 rtx d = XEXP (e, 0);
2303 if (d)
2305 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2306 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2308 RTVEC_ELT (vec, i) = e;
2311 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2314 /* Emit code to move a block SRC to block DST, where SRC and DST are
2315 non-consecutive groups of registers, each represented by a PARALLEL. */
2317 void
2318 emit_group_move (rtx dst, rtx src)
2320 int i;
2322 gcc_assert (GET_CODE (src) == PARALLEL
2323 && GET_CODE (dst) == PARALLEL
2324 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2326 /* Skip first entry if NULL. */
2327 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2328 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2329 XEXP (XVECEXP (src, 0, i), 0));
2332 /* Move a group of registers represented by a PARALLEL into pseudos. */
2335 emit_group_move_into_temps (rtx src)
2337 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2338 int i;
2340 for (i = 0; i < XVECLEN (src, 0); i++)
2342 rtx e = XVECEXP (src, 0, i);
2343 rtx d = XEXP (e, 0);
2345 if (d)
2346 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2347 RTVEC_ELT (vec, i) = e;
2350 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2353 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2354 where SRC is non-consecutive registers represented by a PARALLEL.
2355 SSIZE represents the total size of block ORIG_DST, or -1 if not
2356 known. */
2358 void
2359 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2361 rtx *tmps, dst;
2362 int start, finish, i;
2363 machine_mode m = GET_MODE (orig_dst);
2365 gcc_assert (GET_CODE (src) == PARALLEL);
2367 if (!SCALAR_INT_MODE_P (m)
2368 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2370 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2371 if (imode == BLKmode)
2372 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2373 else
2374 dst = gen_reg_rtx (imode);
2375 emit_group_store (dst, src, type, ssize);
2376 if (imode != BLKmode)
2377 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2378 emit_move_insn (orig_dst, dst);
2379 return;
2382 /* Check for a NULL entry, used to indicate that the parameter goes
2383 both on the stack and in registers. */
2384 if (XEXP (XVECEXP (src, 0, 0), 0))
2385 start = 0;
2386 else
2387 start = 1;
2388 finish = XVECLEN (src, 0);
2390 tmps = XALLOCAVEC (rtx, finish);
2392 /* Copy the (probable) hard regs into pseudos. */
2393 for (i = start; i < finish; i++)
2395 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2396 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2398 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2399 emit_move_insn (tmps[i], reg);
2401 else
2402 tmps[i] = reg;
2405 /* If we won't be storing directly into memory, protect the real destination
2406 from strange tricks we might play. */
2407 dst = orig_dst;
2408 if (GET_CODE (dst) == PARALLEL)
2410 rtx temp;
2412 /* We can get a PARALLEL dst if there is a conditional expression in
2413 a return statement. In that case, the dst and src are the same,
2414 so no action is necessary. */
2415 if (rtx_equal_p (dst, src))
2416 return;
2418 /* It is unclear if we can ever reach here, but we may as well handle
2419 it. Allocate a temporary, and split this into a store/load to/from
2420 the temporary. */
2421 temp = assign_stack_temp (GET_MODE (dst), ssize);
2422 emit_group_store (temp, src, type, ssize);
2423 emit_group_load (dst, temp, type, ssize);
2424 return;
2426 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2428 machine_mode outer = GET_MODE (dst);
2429 machine_mode inner;
2430 HOST_WIDE_INT bytepos;
2431 bool done = false;
2432 rtx temp;
2434 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2435 dst = gen_reg_rtx (outer);
2437 /* Make life a bit easier for combine. */
2438 /* If the first element of the vector is the low part
2439 of the destination mode, use a paradoxical subreg to
2440 initialize the destination. */
2441 if (start < finish)
2443 inner = GET_MODE (tmps[start]);
2444 bytepos = subreg_lowpart_offset (inner, outer);
2445 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2447 temp = simplify_gen_subreg (outer, tmps[start],
2448 inner, 0);
2449 if (temp)
2451 emit_move_insn (dst, temp);
2452 done = true;
2453 start++;
2458 /* If the first element wasn't the low part, try the last. */
2459 if (!done
2460 && start < finish - 1)
2462 inner = GET_MODE (tmps[finish - 1]);
2463 bytepos = subreg_lowpart_offset (inner, outer);
2464 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2466 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2467 inner, 0);
2468 if (temp)
2470 emit_move_insn (dst, temp);
2471 done = true;
2472 finish--;
2477 /* Otherwise, simply initialize the result to zero. */
2478 if (!done)
2479 emit_move_insn (dst, CONST0_RTX (outer));
2482 /* Process the pieces. */
2483 for (i = start; i < finish; i++)
2485 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2486 machine_mode mode = GET_MODE (tmps[i]);
2487 unsigned int bytelen = GET_MODE_SIZE (mode);
2488 unsigned int adj_bytelen;
2489 rtx dest = dst;
2491 /* Handle trailing fragments that run over the size of the struct. */
2492 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2493 adj_bytelen = ssize - bytepos;
2494 else
2495 adj_bytelen = bytelen;
2497 if (GET_CODE (dst) == CONCAT)
2499 if (bytepos + adj_bytelen
2500 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2501 dest = XEXP (dst, 0);
2502 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2504 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2505 dest = XEXP (dst, 1);
2507 else
2509 machine_mode dest_mode = GET_MODE (dest);
2510 machine_mode tmp_mode = GET_MODE (tmps[i]);
2512 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2514 if (GET_MODE_ALIGNMENT (dest_mode)
2515 >= GET_MODE_ALIGNMENT (tmp_mode))
2517 dest = assign_stack_temp (dest_mode,
2518 GET_MODE_SIZE (dest_mode));
2519 emit_move_insn (adjust_address (dest,
2520 tmp_mode,
2521 bytepos),
2522 tmps[i]);
2523 dst = dest;
2525 else
2527 dest = assign_stack_temp (tmp_mode,
2528 GET_MODE_SIZE (tmp_mode));
2529 emit_move_insn (dest, tmps[i]);
2530 dst = adjust_address (dest, dest_mode, bytepos);
2532 break;
2536 /* Handle trailing fragments that run over the size of the struct. */
2537 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2539 /* store_bit_field always takes its value from the lsb.
2540 Move the fragment to the lsb if it's not already there. */
2541 if (
2542 #ifdef BLOCK_REG_PADDING
2543 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2544 == (BYTES_BIG_ENDIAN ? upward : downward)
2545 #else
2546 BYTES_BIG_ENDIAN
2547 #endif
2550 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2551 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2552 shift, tmps[i], 0);
2555 /* Make sure not to write past the end of the struct. */
2556 store_bit_field (dest,
2557 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2558 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2559 VOIDmode, tmps[i], false);
2562 /* Optimize the access just a bit. */
2563 else if (MEM_P (dest)
2564 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2565 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2566 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2567 && bytelen == GET_MODE_SIZE (mode))
2568 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2570 else
2571 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2572 0, 0, mode, tmps[i], false);
2575 /* Copy from the pseudo into the (probable) hard reg. */
2576 if (orig_dst != dst)
2577 emit_move_insn (orig_dst, dst);
2580 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2581 of the value stored in X. */
2584 maybe_emit_group_store (rtx x, tree type)
2586 machine_mode mode = TYPE_MODE (type);
2587 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2588 if (GET_CODE (x) == PARALLEL)
2590 rtx result = gen_reg_rtx (mode);
2591 emit_group_store (result, x, type, int_size_in_bytes (type));
2592 return result;
2594 return x;
2597 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2599 This is used on targets that return BLKmode values in registers. */
2601 static void
2602 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2604 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2605 rtx src = NULL, dst = NULL;
2606 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2607 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2608 machine_mode mode = GET_MODE (srcreg);
2609 machine_mode tmode = GET_MODE (target);
2610 machine_mode copy_mode;
2612 /* BLKmode registers created in the back-end shouldn't have survived. */
2613 gcc_assert (mode != BLKmode);
2615 /* If the structure doesn't take up a whole number of words, see whether
2616 SRCREG is padded on the left or on the right. If it's on the left,
2617 set PADDING_CORRECTION to the number of bits to skip.
2619 In most ABIs, the structure will be returned at the least end of
2620 the register, which translates to right padding on little-endian
2621 targets and left padding on big-endian targets. The opposite
2622 holds if the structure is returned at the most significant
2623 end of the register. */
2624 if (bytes % UNITS_PER_WORD != 0
2625 && (targetm.calls.return_in_msb (type)
2626 ? !BYTES_BIG_ENDIAN
2627 : BYTES_BIG_ENDIAN))
2628 padding_correction
2629 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2631 /* We can use a single move if we have an exact mode for the size. */
2632 else if (MEM_P (target)
2633 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2634 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2635 && bytes == GET_MODE_SIZE (mode))
2637 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2638 return;
2641 /* And if we additionally have the same mode for a register. */
2642 else if (REG_P (target)
2643 && GET_MODE (target) == mode
2644 && bytes == GET_MODE_SIZE (mode))
2646 emit_move_insn (target, srcreg);
2647 return;
2650 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2651 into a new pseudo which is a full word. */
2652 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2654 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2655 mode = word_mode;
2658 /* Copy the structure BITSIZE bits at a time. If the target lives in
2659 memory, take care of not reading/writing past its end by selecting
2660 a copy mode suited to BITSIZE. This should always be possible given
2661 how it is computed.
2663 If the target lives in register, make sure not to select a copy mode
2664 larger than the mode of the register.
2666 We could probably emit more efficient code for machines which do not use
2667 strict alignment, but it doesn't seem worth the effort at the current
2668 time. */
2670 copy_mode = word_mode;
2671 if (MEM_P (target))
2673 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2674 if (mem_mode != BLKmode)
2675 copy_mode = mem_mode;
2677 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2678 copy_mode = tmode;
2680 for (bitpos = 0, xbitpos = padding_correction;
2681 bitpos < bytes * BITS_PER_UNIT;
2682 bitpos += bitsize, xbitpos += bitsize)
2684 /* We need a new source operand each time xbitpos is on a
2685 word boundary and when xbitpos == padding_correction
2686 (the first time through). */
2687 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2688 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2690 /* We need a new destination operand each time bitpos is on
2691 a word boundary. */
2692 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2693 dst = target;
2694 else if (bitpos % BITS_PER_WORD == 0)
2695 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2697 /* Use xbitpos for the source extraction (right justified) and
2698 bitpos for the destination store (left justified). */
2699 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2700 extract_bit_field (src, bitsize,
2701 xbitpos % BITS_PER_WORD, 1,
2702 NULL_RTX, copy_mode, copy_mode,
2703 false, NULL),
2704 false);
2708 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2709 register if it contains any data, otherwise return null.
2711 This is used on targets that return BLKmode values in registers. */
2714 copy_blkmode_to_reg (machine_mode mode, tree src)
2716 int i, n_regs;
2717 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2718 unsigned int bitsize;
2719 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2720 machine_mode dst_mode;
2722 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2724 x = expand_normal (src);
2726 bytes = int_size_in_bytes (TREE_TYPE (src));
2727 if (bytes == 0)
2728 return NULL_RTX;
2730 /* If the structure doesn't take up a whole number of words, see
2731 whether the register value should be padded on the left or on
2732 the right. Set PADDING_CORRECTION to the number of padding
2733 bits needed on the left side.
2735 In most ABIs, the structure will be returned at the least end of
2736 the register, which translates to right padding on little-endian
2737 targets and left padding on big-endian targets. The opposite
2738 holds if the structure is returned at the most significant
2739 end of the register. */
2740 if (bytes % UNITS_PER_WORD != 0
2741 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2742 ? !BYTES_BIG_ENDIAN
2743 : BYTES_BIG_ENDIAN))
2744 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2745 * BITS_PER_UNIT));
2747 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2748 dst_words = XALLOCAVEC (rtx, n_regs);
2749 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2751 /* Copy the structure BITSIZE bits at a time. */
2752 for (bitpos = 0, xbitpos = padding_correction;
2753 bitpos < bytes * BITS_PER_UNIT;
2754 bitpos += bitsize, xbitpos += bitsize)
2756 /* We need a new destination pseudo each time xbitpos is
2757 on a word boundary and when xbitpos == padding_correction
2758 (the first time through). */
2759 if (xbitpos % BITS_PER_WORD == 0
2760 || xbitpos == padding_correction)
2762 /* Generate an appropriate register. */
2763 dst_word = gen_reg_rtx (word_mode);
2764 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2766 /* Clear the destination before we move anything into it. */
2767 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2770 /* We need a new source operand each time bitpos is on a word
2771 boundary. */
2772 if (bitpos % BITS_PER_WORD == 0)
2773 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2775 /* Use bitpos for the source extraction (left justified) and
2776 xbitpos for the destination store (right justified). */
2777 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2778 0, 0, word_mode,
2779 extract_bit_field (src_word, bitsize,
2780 bitpos % BITS_PER_WORD, 1,
2781 NULL_RTX, word_mode, word_mode,
2782 false, NULL),
2783 false);
2786 if (mode == BLKmode)
2788 /* Find the smallest integer mode large enough to hold the
2789 entire structure. */
2790 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
2791 /* Have we found a large enough mode? */
2792 if (GET_MODE_SIZE (mode) >= bytes)
2793 break;
2795 /* A suitable mode should have been found. */
2796 gcc_assert (mode != VOIDmode);
2799 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2800 dst_mode = word_mode;
2801 else
2802 dst_mode = mode;
2803 dst = gen_reg_rtx (dst_mode);
2805 for (i = 0; i < n_regs; i++)
2806 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2808 if (mode != dst_mode)
2809 dst = gen_lowpart (mode, dst);
2811 return dst;
2814 /* Add a USE expression for REG to the (possibly empty) list pointed
2815 to by CALL_FUSAGE. REG must denote a hard register. */
2817 void
2818 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2820 gcc_assert (REG_P (reg));
2822 if (!HARD_REGISTER_P (reg))
2823 return;
2825 *call_fusage
2826 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2829 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2830 to by CALL_FUSAGE. REG must denote a hard register. */
2832 void
2833 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2835 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2837 *call_fusage
2838 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2841 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2842 starting at REGNO. All of these registers must be hard registers. */
2844 void
2845 use_regs (rtx *call_fusage, int regno, int nregs)
2847 int i;
2849 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2851 for (i = 0; i < nregs; i++)
2852 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2855 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2856 PARALLEL REGS. This is for calls that pass values in multiple
2857 non-contiguous locations. The Irix 6 ABI has examples of this. */
2859 void
2860 use_group_regs (rtx *call_fusage, rtx regs)
2862 int i;
2864 for (i = 0; i < XVECLEN (regs, 0); i++)
2866 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2868 /* A NULL entry means the parameter goes both on the stack and in
2869 registers. This can also be a MEM for targets that pass values
2870 partially on the stack and partially in registers. */
2871 if (reg != 0 && REG_P (reg))
2872 use_reg (call_fusage, reg);
2876 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2877 assigment and the code of the expresion on the RHS is CODE. Return
2878 NULL otherwise. */
2880 static gimple *
2881 get_def_for_expr (tree name, enum tree_code code)
2883 gimple *def_stmt;
2885 if (TREE_CODE (name) != SSA_NAME)
2886 return NULL;
2888 def_stmt = get_gimple_for_ssa_name (name);
2889 if (!def_stmt
2890 || gimple_assign_rhs_code (def_stmt) != code)
2891 return NULL;
2893 return def_stmt;
2896 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2897 assigment and the class of the expresion on the RHS is CLASS. Return
2898 NULL otherwise. */
2900 static gimple *
2901 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2903 gimple *def_stmt;
2905 if (TREE_CODE (name) != SSA_NAME)
2906 return NULL;
2908 def_stmt = get_gimple_for_ssa_name (name);
2909 if (!def_stmt
2910 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2911 return NULL;
2913 return def_stmt;
2916 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2917 its length in bytes. */
2920 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2921 unsigned int expected_align, HOST_WIDE_INT expected_size,
2922 unsigned HOST_WIDE_INT min_size,
2923 unsigned HOST_WIDE_INT max_size,
2924 unsigned HOST_WIDE_INT probable_max_size)
2926 machine_mode mode = GET_MODE (object);
2927 unsigned int align;
2929 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2931 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2932 just move a zero. Otherwise, do this a piece at a time. */
2933 if (mode != BLKmode
2934 && CONST_INT_P (size)
2935 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2937 rtx zero = CONST0_RTX (mode);
2938 if (zero != NULL)
2940 emit_move_insn (object, zero);
2941 return NULL;
2944 if (COMPLEX_MODE_P (mode))
2946 zero = CONST0_RTX (GET_MODE_INNER (mode));
2947 if (zero != NULL)
2949 write_complex_part (object, zero, 0);
2950 write_complex_part (object, zero, 1);
2951 return NULL;
2956 if (size == const0_rtx)
2957 return NULL;
2959 align = MEM_ALIGN (object);
2961 if (CONST_INT_P (size)
2962 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2963 CLEAR_BY_PIECES,
2964 optimize_insn_for_speed_p ()))
2965 clear_by_pieces (object, INTVAL (size), align);
2966 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2967 expected_align, expected_size,
2968 min_size, max_size, probable_max_size))
2970 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2971 return set_storage_via_libcall (object, size, const0_rtx,
2972 method == BLOCK_OP_TAILCALL);
2973 else
2974 gcc_unreachable ();
2976 return NULL;
2980 clear_storage (rtx object, rtx size, enum block_op_methods method)
2982 unsigned HOST_WIDE_INT max, min = 0;
2983 if (GET_CODE (size) == CONST_INT)
2984 min = max = UINTVAL (size);
2985 else
2986 max = GET_MODE_MASK (GET_MODE (size));
2987 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2991 /* A subroutine of clear_storage. Expand a call to memset.
2992 Return the return value of memset, 0 otherwise. */
2995 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2997 tree call_expr, fn, object_tree, size_tree, val_tree;
2998 machine_mode size_mode;
3000 object = copy_addr_to_reg (XEXP (object, 0));
3001 object_tree = make_tree (ptr_type_node, object);
3003 if (!CONST_INT_P (val))
3004 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3005 val_tree = make_tree (integer_type_node, val);
3007 size_mode = TYPE_MODE (sizetype);
3008 size = convert_to_mode (size_mode, size, 1);
3009 size = copy_to_mode_reg (size_mode, size);
3010 size_tree = make_tree (sizetype, size);
3012 /* It is incorrect to use the libcall calling conventions for calls to
3013 memset because it can be provided by the user. */
3014 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3015 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3016 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3018 return expand_call (call_expr, NULL_RTX, false);
3021 /* Expand a setmem pattern; return true if successful. */
3023 bool
3024 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3025 unsigned int expected_align, HOST_WIDE_INT expected_size,
3026 unsigned HOST_WIDE_INT min_size,
3027 unsigned HOST_WIDE_INT max_size,
3028 unsigned HOST_WIDE_INT probable_max_size)
3030 /* Try the most limited insn first, because there's no point
3031 including more than one in the machine description unless
3032 the more limited one has some advantage. */
3034 machine_mode mode;
3036 if (expected_align < align)
3037 expected_align = align;
3038 if (expected_size != -1)
3040 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3041 expected_size = max_size;
3042 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3043 expected_size = min_size;
3046 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
3048 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3050 if (code != CODE_FOR_nothing
3051 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3052 here because if SIZE is less than the mode mask, as it is
3053 returned by the macro, it will definitely be less than the
3054 actual mode mask. Since SIZE is within the Pmode address
3055 space, we limit MODE to Pmode. */
3056 && ((CONST_INT_P (size)
3057 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3058 <= (GET_MODE_MASK (mode) >> 1)))
3059 || max_size <= (GET_MODE_MASK (mode) >> 1)
3060 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3062 struct expand_operand ops[9];
3063 unsigned int nops;
3065 nops = insn_data[(int) code].n_generator_args;
3066 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3068 create_fixed_operand (&ops[0], object);
3069 /* The check above guarantees that this size conversion is valid. */
3070 create_convert_operand_to (&ops[1], size, mode, true);
3071 create_convert_operand_from (&ops[2], val, byte_mode, true);
3072 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3073 if (nops >= 6)
3075 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3076 create_integer_operand (&ops[5], expected_size);
3078 if (nops >= 8)
3080 create_integer_operand (&ops[6], min_size);
3081 /* If we can not represent the maximal size,
3082 make parameter NULL. */
3083 if ((HOST_WIDE_INT) max_size != -1)
3084 create_integer_operand (&ops[7], max_size);
3085 else
3086 create_fixed_operand (&ops[7], NULL);
3088 if (nops == 9)
3090 /* If we can not represent the maximal size,
3091 make parameter NULL. */
3092 if ((HOST_WIDE_INT) probable_max_size != -1)
3093 create_integer_operand (&ops[8], probable_max_size);
3094 else
3095 create_fixed_operand (&ops[8], NULL);
3097 if (maybe_expand_insn (code, nops, ops))
3098 return true;
3102 return false;
3106 /* Write to one of the components of the complex value CPLX. Write VAL to
3107 the real part if IMAG_P is false, and the imaginary part if its true. */
3109 void
3110 write_complex_part (rtx cplx, rtx val, bool imag_p)
3112 machine_mode cmode;
3113 machine_mode imode;
3114 unsigned ibitsize;
3116 if (GET_CODE (cplx) == CONCAT)
3118 emit_move_insn (XEXP (cplx, imag_p), val);
3119 return;
3122 cmode = GET_MODE (cplx);
3123 imode = GET_MODE_INNER (cmode);
3124 ibitsize = GET_MODE_BITSIZE (imode);
3126 /* For MEMs simplify_gen_subreg may generate an invalid new address
3127 because, e.g., the original address is considered mode-dependent
3128 by the target, which restricts simplify_subreg from invoking
3129 adjust_address_nv. Instead of preparing fallback support for an
3130 invalid address, we call adjust_address_nv directly. */
3131 if (MEM_P (cplx))
3133 emit_move_insn (adjust_address_nv (cplx, imode,
3134 imag_p ? GET_MODE_SIZE (imode) : 0),
3135 val);
3136 return;
3139 /* If the sub-object is at least word sized, then we know that subregging
3140 will work. This special case is important, since store_bit_field
3141 wants to operate on integer modes, and there's rarely an OImode to
3142 correspond to TCmode. */
3143 if (ibitsize >= BITS_PER_WORD
3144 /* For hard regs we have exact predicates. Assume we can split
3145 the original object if it spans an even number of hard regs.
3146 This special case is important for SCmode on 64-bit platforms
3147 where the natural size of floating-point regs is 32-bit. */
3148 || (REG_P (cplx)
3149 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3150 && REG_NREGS (cplx) % 2 == 0))
3152 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3153 imag_p ? GET_MODE_SIZE (imode) : 0);
3154 if (part)
3156 emit_move_insn (part, val);
3157 return;
3159 else
3160 /* simplify_gen_subreg may fail for sub-word MEMs. */
3161 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3164 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3165 false);
3168 /* Extract one of the components of the complex value CPLX. Extract the
3169 real part if IMAG_P is false, and the imaginary part if it's true. */
3172 read_complex_part (rtx cplx, bool imag_p)
3174 machine_mode cmode, imode;
3175 unsigned ibitsize;
3177 if (GET_CODE (cplx) == CONCAT)
3178 return XEXP (cplx, imag_p);
3180 cmode = GET_MODE (cplx);
3181 imode = GET_MODE_INNER (cmode);
3182 ibitsize = GET_MODE_BITSIZE (imode);
3184 /* Special case reads from complex constants that got spilled to memory. */
3185 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3187 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3188 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3190 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3191 if (CONSTANT_CLASS_P (part))
3192 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3196 /* For MEMs simplify_gen_subreg may generate an invalid new address
3197 because, e.g., the original address is considered mode-dependent
3198 by the target, which restricts simplify_subreg from invoking
3199 adjust_address_nv. Instead of preparing fallback support for an
3200 invalid address, we call adjust_address_nv directly. */
3201 if (MEM_P (cplx))
3202 return adjust_address_nv (cplx, imode,
3203 imag_p ? GET_MODE_SIZE (imode) : 0);
3205 /* If the sub-object is at least word sized, then we know that subregging
3206 will work. This special case is important, since extract_bit_field
3207 wants to operate on integer modes, and there's rarely an OImode to
3208 correspond to TCmode. */
3209 if (ibitsize >= BITS_PER_WORD
3210 /* For hard regs we have exact predicates. Assume we can split
3211 the original object if it spans an even number of hard regs.
3212 This special case is important for SCmode on 64-bit platforms
3213 where the natural size of floating-point regs is 32-bit. */
3214 || (REG_P (cplx)
3215 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3216 && REG_NREGS (cplx) % 2 == 0))
3218 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3219 imag_p ? GET_MODE_SIZE (imode) : 0);
3220 if (ret)
3221 return ret;
3222 else
3223 /* simplify_gen_subreg may fail for sub-word MEMs. */
3224 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3227 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3228 true, NULL_RTX, imode, imode, false, NULL);
3231 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3232 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3233 represented in NEW_MODE. If FORCE is true, this will never happen, as
3234 we'll force-create a SUBREG if needed. */
3236 static rtx
3237 emit_move_change_mode (machine_mode new_mode,
3238 machine_mode old_mode, rtx x, bool force)
3240 rtx ret;
3242 if (push_operand (x, GET_MODE (x)))
3244 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3245 MEM_COPY_ATTRIBUTES (ret, x);
3247 else if (MEM_P (x))
3249 /* We don't have to worry about changing the address since the
3250 size in bytes is supposed to be the same. */
3251 if (reload_in_progress)
3253 /* Copy the MEM to change the mode and move any
3254 substitutions from the old MEM to the new one. */
3255 ret = adjust_address_nv (x, new_mode, 0);
3256 copy_replacements (x, ret);
3258 else
3259 ret = adjust_address (x, new_mode, 0);
3261 else
3263 /* Note that we do want simplify_subreg's behavior of validating
3264 that the new mode is ok for a hard register. If we were to use
3265 simplify_gen_subreg, we would create the subreg, but would
3266 probably run into the target not being able to implement it. */
3267 /* Except, of course, when FORCE is true, when this is exactly what
3268 we want. Which is needed for CCmodes on some targets. */
3269 if (force)
3270 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3271 else
3272 ret = simplify_subreg (new_mode, x, old_mode, 0);
3275 return ret;
3278 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3279 an integer mode of the same size as MODE. Returns the instruction
3280 emitted, or NULL if such a move could not be generated. */
3282 static rtx_insn *
3283 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3285 machine_mode imode;
3286 enum insn_code code;
3288 /* There must exist a mode of the exact size we require. */
3289 imode = int_mode_for_mode (mode);
3290 if (imode == BLKmode)
3291 return NULL;
3293 /* The target must support moves in this mode. */
3294 code = optab_handler (mov_optab, imode);
3295 if (code == CODE_FOR_nothing)
3296 return NULL;
3298 x = emit_move_change_mode (imode, mode, x, force);
3299 if (x == NULL_RTX)
3300 return NULL;
3301 y = emit_move_change_mode (imode, mode, y, force);
3302 if (y == NULL_RTX)
3303 return NULL;
3304 return emit_insn (GEN_FCN (code) (x, y));
3307 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3308 Return an equivalent MEM that does not use an auto-increment. */
3311 emit_move_resolve_push (machine_mode mode, rtx x)
3313 enum rtx_code code = GET_CODE (XEXP (x, 0));
3314 HOST_WIDE_INT adjust;
3315 rtx temp;
3317 adjust = GET_MODE_SIZE (mode);
3318 #ifdef PUSH_ROUNDING
3319 adjust = PUSH_ROUNDING (adjust);
3320 #endif
3321 if (code == PRE_DEC || code == POST_DEC)
3322 adjust = -adjust;
3323 else if (code == PRE_MODIFY || code == POST_MODIFY)
3325 rtx expr = XEXP (XEXP (x, 0), 1);
3326 HOST_WIDE_INT val;
3328 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3329 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3330 val = INTVAL (XEXP (expr, 1));
3331 if (GET_CODE (expr) == MINUS)
3332 val = -val;
3333 gcc_assert (adjust == val || adjust == -val);
3334 adjust = val;
3337 /* Do not use anti_adjust_stack, since we don't want to update
3338 stack_pointer_delta. */
3339 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3340 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3341 0, OPTAB_LIB_WIDEN);
3342 if (temp != stack_pointer_rtx)
3343 emit_move_insn (stack_pointer_rtx, temp);
3345 switch (code)
3347 case PRE_INC:
3348 case PRE_DEC:
3349 case PRE_MODIFY:
3350 temp = stack_pointer_rtx;
3351 break;
3352 case POST_INC:
3353 case POST_DEC:
3354 case POST_MODIFY:
3355 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3356 break;
3357 default:
3358 gcc_unreachable ();
3361 return replace_equiv_address (x, temp);
3364 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3365 X is known to satisfy push_operand, and MODE is known to be complex.
3366 Returns the last instruction emitted. */
3368 rtx_insn *
3369 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3371 machine_mode submode = GET_MODE_INNER (mode);
3372 bool imag_first;
3374 #ifdef PUSH_ROUNDING
3375 unsigned int submodesize = GET_MODE_SIZE (submode);
3377 /* In case we output to the stack, but the size is smaller than the
3378 machine can push exactly, we need to use move instructions. */
3379 if (PUSH_ROUNDING (submodesize) != submodesize)
3381 x = emit_move_resolve_push (mode, x);
3382 return emit_move_insn (x, y);
3384 #endif
3386 /* Note that the real part always precedes the imag part in memory
3387 regardless of machine's endianness. */
3388 switch (GET_CODE (XEXP (x, 0)))
3390 case PRE_DEC:
3391 case POST_DEC:
3392 imag_first = true;
3393 break;
3394 case PRE_INC:
3395 case POST_INC:
3396 imag_first = false;
3397 break;
3398 default:
3399 gcc_unreachable ();
3402 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3403 read_complex_part (y, imag_first));
3404 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3405 read_complex_part (y, !imag_first));
3408 /* A subroutine of emit_move_complex. Perform the move from Y to X
3409 via two moves of the parts. Returns the last instruction emitted. */
3411 rtx_insn *
3412 emit_move_complex_parts (rtx x, rtx y)
3414 /* Show the output dies here. This is necessary for SUBREGs
3415 of pseudos since we cannot track their lifetimes correctly;
3416 hard regs shouldn't appear here except as return values. */
3417 if (!reload_completed && !reload_in_progress
3418 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3419 emit_clobber (x);
3421 write_complex_part (x, read_complex_part (y, false), false);
3422 write_complex_part (x, read_complex_part (y, true), true);
3424 return get_last_insn ();
3427 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3428 MODE is known to be complex. Returns the last instruction emitted. */
3430 static rtx_insn *
3431 emit_move_complex (machine_mode mode, rtx x, rtx y)
3433 bool try_int;
3435 /* Need to take special care for pushes, to maintain proper ordering
3436 of the data, and possibly extra padding. */
3437 if (push_operand (x, mode))
3438 return emit_move_complex_push (mode, x, y);
3440 /* See if we can coerce the target into moving both values at once, except
3441 for floating point where we favor moving as parts if this is easy. */
3442 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3443 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3444 && !(REG_P (x)
3445 && HARD_REGISTER_P (x)
3446 && REG_NREGS (x) == 1)
3447 && !(REG_P (y)
3448 && HARD_REGISTER_P (y)
3449 && REG_NREGS (y) == 1))
3450 try_int = false;
3451 /* Not possible if the values are inherently not adjacent. */
3452 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3453 try_int = false;
3454 /* Is possible if both are registers (or subregs of registers). */
3455 else if (register_operand (x, mode) && register_operand (y, mode))
3456 try_int = true;
3457 /* If one of the operands is a memory, and alignment constraints
3458 are friendly enough, we may be able to do combined memory operations.
3459 We do not attempt this if Y is a constant because that combination is
3460 usually better with the by-parts thing below. */
3461 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3462 && (!STRICT_ALIGNMENT
3463 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3464 try_int = true;
3465 else
3466 try_int = false;
3468 if (try_int)
3470 rtx_insn *ret;
3472 /* For memory to memory moves, optimal behavior can be had with the
3473 existing block move logic. */
3474 if (MEM_P (x) && MEM_P (y))
3476 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3477 BLOCK_OP_NO_LIBCALL);
3478 return get_last_insn ();
3481 ret = emit_move_via_integer (mode, x, y, true);
3482 if (ret)
3483 return ret;
3486 return emit_move_complex_parts (x, y);
3489 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3490 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3492 static rtx_insn *
3493 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3495 rtx_insn *ret;
3497 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3498 if (mode != CCmode)
3500 enum insn_code code = optab_handler (mov_optab, CCmode);
3501 if (code != CODE_FOR_nothing)
3503 x = emit_move_change_mode (CCmode, mode, x, true);
3504 y = emit_move_change_mode (CCmode, mode, y, true);
3505 return emit_insn (GEN_FCN (code) (x, y));
3509 /* Otherwise, find the MODE_INT mode of the same width. */
3510 ret = emit_move_via_integer (mode, x, y, false);
3511 gcc_assert (ret != NULL);
3512 return ret;
3515 /* Return true if word I of OP lies entirely in the
3516 undefined bits of a paradoxical subreg. */
3518 static bool
3519 undefined_operand_subword_p (const_rtx op, int i)
3521 machine_mode innermode, innermostmode;
3522 int offset;
3523 if (GET_CODE (op) != SUBREG)
3524 return false;
3525 innermode = GET_MODE (op);
3526 innermostmode = GET_MODE (SUBREG_REG (op));
3527 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3528 /* The SUBREG_BYTE represents offset, as if the value were stored in
3529 memory, except for a paradoxical subreg where we define
3530 SUBREG_BYTE to be 0; undo this exception as in
3531 simplify_subreg. */
3532 if (SUBREG_BYTE (op) == 0
3533 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3535 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3536 if (WORDS_BIG_ENDIAN)
3537 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3538 if (BYTES_BIG_ENDIAN)
3539 offset += difference % UNITS_PER_WORD;
3541 if (offset >= GET_MODE_SIZE (innermostmode)
3542 || offset <= -GET_MODE_SIZE (word_mode))
3543 return true;
3544 return false;
3547 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3548 MODE is any multi-word or full-word mode that lacks a move_insn
3549 pattern. Note that you will get better code if you define such
3550 patterns, even if they must turn into multiple assembler instructions. */
3552 static rtx_insn *
3553 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3555 rtx_insn *last_insn = 0;
3556 rtx_insn *seq;
3557 rtx inner;
3558 bool need_clobber;
3559 int i;
3561 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3563 /* If X is a push on the stack, do the push now and replace
3564 X with a reference to the stack pointer. */
3565 if (push_operand (x, mode))
3566 x = emit_move_resolve_push (mode, x);
3568 /* If we are in reload, see if either operand is a MEM whose address
3569 is scheduled for replacement. */
3570 if (reload_in_progress && MEM_P (x)
3571 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3572 x = replace_equiv_address_nv (x, inner);
3573 if (reload_in_progress && MEM_P (y)
3574 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3575 y = replace_equiv_address_nv (y, inner);
3577 start_sequence ();
3579 need_clobber = false;
3580 for (i = 0;
3581 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3582 i++)
3584 rtx xpart = operand_subword (x, i, 1, mode);
3585 rtx ypart;
3587 /* Do not generate code for a move if it would come entirely
3588 from the undefined bits of a paradoxical subreg. */
3589 if (undefined_operand_subword_p (y, i))
3590 continue;
3592 ypart = operand_subword (y, i, 1, mode);
3594 /* If we can't get a part of Y, put Y into memory if it is a
3595 constant. Otherwise, force it into a register. Then we must
3596 be able to get a part of Y. */
3597 if (ypart == 0 && CONSTANT_P (y))
3599 y = use_anchored_address (force_const_mem (mode, y));
3600 ypart = operand_subword (y, i, 1, mode);
3602 else if (ypart == 0)
3603 ypart = operand_subword_force (y, i, mode);
3605 gcc_assert (xpart && ypart);
3607 need_clobber |= (GET_CODE (xpart) == SUBREG);
3609 last_insn = emit_move_insn (xpart, ypart);
3612 seq = get_insns ();
3613 end_sequence ();
3615 /* Show the output dies here. This is necessary for SUBREGs
3616 of pseudos since we cannot track their lifetimes correctly;
3617 hard regs shouldn't appear here except as return values.
3618 We never want to emit such a clobber after reload. */
3619 if (x != y
3620 && ! (reload_in_progress || reload_completed)
3621 && need_clobber != 0)
3622 emit_clobber (x);
3624 emit_insn (seq);
3626 return last_insn;
3629 /* Low level part of emit_move_insn.
3630 Called just like emit_move_insn, but assumes X and Y
3631 are basically valid. */
3633 rtx_insn *
3634 emit_move_insn_1 (rtx x, rtx y)
3636 machine_mode mode = GET_MODE (x);
3637 enum insn_code code;
3639 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3641 code = optab_handler (mov_optab, mode);
3642 if (code != CODE_FOR_nothing)
3643 return emit_insn (GEN_FCN (code) (x, y));
3645 /* Expand complex moves by moving real part and imag part. */
3646 if (COMPLEX_MODE_P (mode))
3647 return emit_move_complex (mode, x, y);
3649 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3650 || ALL_FIXED_POINT_MODE_P (mode))
3652 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3654 /* If we can't find an integer mode, use multi words. */
3655 if (result)
3656 return result;
3657 else
3658 return emit_move_multi_word (mode, x, y);
3661 if (GET_MODE_CLASS (mode) == MODE_CC)
3662 return emit_move_ccmode (mode, x, y);
3664 /* Try using a move pattern for the corresponding integer mode. This is
3665 only safe when simplify_subreg can convert MODE constants into integer
3666 constants. At present, it can only do this reliably if the value
3667 fits within a HOST_WIDE_INT. */
3668 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3670 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3672 if (ret)
3674 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3675 return ret;
3679 return emit_move_multi_word (mode, x, y);
3682 /* Generate code to copy Y into X.
3683 Both Y and X must have the same mode, except that
3684 Y can be a constant with VOIDmode.
3685 This mode cannot be BLKmode; use emit_block_move for that.
3687 Return the last instruction emitted. */
3689 rtx_insn *
3690 emit_move_insn (rtx x, rtx y)
3692 machine_mode mode = GET_MODE (x);
3693 rtx y_cst = NULL_RTX;
3694 rtx_insn *last_insn;
3695 rtx set;
3697 gcc_assert (mode != BLKmode
3698 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3700 if (CONSTANT_P (y))
3702 if (optimize
3703 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3704 && (last_insn = compress_float_constant (x, y)))
3705 return last_insn;
3707 y_cst = y;
3709 if (!targetm.legitimate_constant_p (mode, y))
3711 y = force_const_mem (mode, y);
3713 /* If the target's cannot_force_const_mem prevented the spill,
3714 assume that the target's move expanders will also take care
3715 of the non-legitimate constant. */
3716 if (!y)
3717 y = y_cst;
3718 else
3719 y = use_anchored_address (y);
3723 /* If X or Y are memory references, verify that their addresses are valid
3724 for the machine. */
3725 if (MEM_P (x)
3726 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3727 MEM_ADDR_SPACE (x))
3728 && ! push_operand (x, GET_MODE (x))))
3729 x = validize_mem (x);
3731 if (MEM_P (y)
3732 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3733 MEM_ADDR_SPACE (y)))
3734 y = validize_mem (y);
3736 gcc_assert (mode != BLKmode);
3738 last_insn = emit_move_insn_1 (x, y);
3740 if (y_cst && REG_P (x)
3741 && (set = single_set (last_insn)) != NULL_RTX
3742 && SET_DEST (set) == x
3743 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3744 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3746 return last_insn;
3749 /* Generate the body of an instruction to copy Y into X.
3750 It may be a list of insns, if one insn isn't enough. */
3752 rtx_insn *
3753 gen_move_insn (rtx x, rtx y)
3755 rtx_insn *seq;
3757 start_sequence ();
3758 emit_move_insn_1 (x, y);
3759 seq = get_insns ();
3760 end_sequence ();
3761 return seq;
3764 /* If Y is representable exactly in a narrower mode, and the target can
3765 perform the extension directly from constant or memory, then emit the
3766 move as an extension. */
3768 static rtx_insn *
3769 compress_float_constant (rtx x, rtx y)
3771 machine_mode dstmode = GET_MODE (x);
3772 machine_mode orig_srcmode = GET_MODE (y);
3773 machine_mode srcmode;
3774 const REAL_VALUE_TYPE *r;
3775 int oldcost, newcost;
3776 bool speed = optimize_insn_for_speed_p ();
3778 r = CONST_DOUBLE_REAL_VALUE (y);
3780 if (targetm.legitimate_constant_p (dstmode, y))
3781 oldcost = set_src_cost (y, orig_srcmode, speed);
3782 else
3783 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3785 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3787 enum insn_code ic;
3788 rtx trunc_y;
3789 rtx_insn *last_insn;
3791 /* Skip if the target can't extend this way. */
3792 ic = can_extend_p (dstmode, srcmode, 0);
3793 if (ic == CODE_FOR_nothing)
3794 continue;
3796 /* Skip if the narrowed value isn't exact. */
3797 if (! exact_real_truncate (srcmode, r))
3798 continue;
3800 trunc_y = const_double_from_real_value (*r, srcmode);
3802 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3804 /* Skip if the target needs extra instructions to perform
3805 the extension. */
3806 if (!insn_operand_matches (ic, 1, trunc_y))
3807 continue;
3808 /* This is valid, but may not be cheaper than the original. */
3809 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3810 dstmode, speed);
3811 if (oldcost < newcost)
3812 continue;
3814 else if (float_extend_from_mem[dstmode][srcmode])
3816 trunc_y = force_const_mem (srcmode, trunc_y);
3817 /* This is valid, but may not be cheaper than the original. */
3818 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3819 dstmode, speed);
3820 if (oldcost < newcost)
3821 continue;
3822 trunc_y = validize_mem (trunc_y);
3824 else
3825 continue;
3827 /* For CSE's benefit, force the compressed constant pool entry
3828 into a new pseudo. This constant may be used in different modes,
3829 and if not, combine will put things back together for us. */
3830 trunc_y = force_reg (srcmode, trunc_y);
3832 /* If x is a hard register, perform the extension into a pseudo,
3833 so that e.g. stack realignment code is aware of it. */
3834 rtx target = x;
3835 if (REG_P (x) && HARD_REGISTER_P (x))
3836 target = gen_reg_rtx (dstmode);
3838 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3839 last_insn = get_last_insn ();
3841 if (REG_P (target))
3842 set_unique_reg_note (last_insn, REG_EQUAL, y);
3844 if (target != x)
3845 return emit_move_insn (x, target);
3846 return last_insn;
3849 return NULL;
3852 /* Pushing data onto the stack. */
3854 /* Push a block of length SIZE (perhaps variable)
3855 and return an rtx to address the beginning of the block.
3856 The value may be virtual_outgoing_args_rtx.
3858 EXTRA is the number of bytes of padding to push in addition to SIZE.
3859 BELOW nonzero means this padding comes at low addresses;
3860 otherwise, the padding comes at high addresses. */
3863 push_block (rtx size, int extra, int below)
3865 rtx temp;
3867 size = convert_modes (Pmode, ptr_mode, size, 1);
3868 if (CONSTANT_P (size))
3869 anti_adjust_stack (plus_constant (Pmode, size, extra));
3870 else if (REG_P (size) && extra == 0)
3871 anti_adjust_stack (size);
3872 else
3874 temp = copy_to_mode_reg (Pmode, size);
3875 if (extra != 0)
3876 temp = expand_binop (Pmode, add_optab, temp,
3877 gen_int_mode (extra, Pmode),
3878 temp, 0, OPTAB_LIB_WIDEN);
3879 anti_adjust_stack (temp);
3882 if (STACK_GROWS_DOWNWARD)
3884 temp = virtual_outgoing_args_rtx;
3885 if (extra != 0 && below)
3886 temp = plus_constant (Pmode, temp, extra);
3888 else
3890 if (CONST_INT_P (size))
3891 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3892 -INTVAL (size) - (below ? 0 : extra));
3893 else if (extra != 0 && !below)
3894 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3895 negate_rtx (Pmode, plus_constant (Pmode, size,
3896 extra)));
3897 else
3898 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3899 negate_rtx (Pmode, size));
3902 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3905 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3907 static rtx
3908 mem_autoinc_base (rtx mem)
3910 if (MEM_P (mem))
3912 rtx addr = XEXP (mem, 0);
3913 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3914 return XEXP (addr, 0);
3916 return NULL;
3919 /* A utility routine used here, in reload, and in try_split. The insns
3920 after PREV up to and including LAST are known to adjust the stack,
3921 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3922 placing notes as appropriate. PREV may be NULL, indicating the
3923 entire insn sequence prior to LAST should be scanned.
3925 The set of allowed stack pointer modifications is small:
3926 (1) One or more auto-inc style memory references (aka pushes),
3927 (2) One or more addition/subtraction with the SP as destination,
3928 (3) A single move insn with the SP as destination,
3929 (4) A call_pop insn,
3930 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3932 Insns in the sequence that do not modify the SP are ignored,
3933 except for noreturn calls.
3935 The return value is the amount of adjustment that can be trivially
3936 verified, via immediate operand or auto-inc. If the adjustment
3937 cannot be trivially extracted, the return value is INT_MIN. */
3939 HOST_WIDE_INT
3940 find_args_size_adjust (rtx_insn *insn)
3942 rtx dest, set, pat;
3943 int i;
3945 pat = PATTERN (insn);
3946 set = NULL;
3948 /* Look for a call_pop pattern. */
3949 if (CALL_P (insn))
3951 /* We have to allow non-call_pop patterns for the case
3952 of emit_single_push_insn of a TLS address. */
3953 if (GET_CODE (pat) != PARALLEL)
3954 return 0;
3956 /* All call_pop have a stack pointer adjust in the parallel.
3957 The call itself is always first, and the stack adjust is
3958 usually last, so search from the end. */
3959 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3961 set = XVECEXP (pat, 0, i);
3962 if (GET_CODE (set) != SET)
3963 continue;
3964 dest = SET_DEST (set);
3965 if (dest == stack_pointer_rtx)
3966 break;
3968 /* We'd better have found the stack pointer adjust. */
3969 if (i == 0)
3970 return 0;
3971 /* Fall through to process the extracted SET and DEST
3972 as if it was a standalone insn. */
3974 else if (GET_CODE (pat) == SET)
3975 set = pat;
3976 else if ((set = single_set (insn)) != NULL)
3978 else if (GET_CODE (pat) == PARALLEL)
3980 /* ??? Some older ports use a parallel with a stack adjust
3981 and a store for a PUSH_ROUNDING pattern, rather than a
3982 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3983 /* ??? See h8300 and m68k, pushqi1. */
3984 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3986 set = XVECEXP (pat, 0, i);
3987 if (GET_CODE (set) != SET)
3988 continue;
3989 dest = SET_DEST (set);
3990 if (dest == stack_pointer_rtx)
3991 break;
3993 /* We do not expect an auto-inc of the sp in the parallel. */
3994 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3995 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3996 != stack_pointer_rtx);
3998 if (i < 0)
3999 return 0;
4001 else
4002 return 0;
4004 dest = SET_DEST (set);
4006 /* Look for direct modifications of the stack pointer. */
4007 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4009 /* Look for a trivial adjustment, otherwise assume nothing. */
4010 /* Note that the SPU restore_stack_block pattern refers to
4011 the stack pointer in V4SImode. Consider that non-trivial. */
4012 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4013 && GET_CODE (SET_SRC (set)) == PLUS
4014 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4015 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4016 return INTVAL (XEXP (SET_SRC (set), 1));
4017 /* ??? Reload can generate no-op moves, which will be cleaned
4018 up later. Recognize it and continue searching. */
4019 else if (rtx_equal_p (dest, SET_SRC (set)))
4020 return 0;
4021 else
4022 return HOST_WIDE_INT_MIN;
4024 else
4026 rtx mem, addr;
4028 /* Otherwise only think about autoinc patterns. */
4029 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4031 mem = dest;
4032 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4033 != stack_pointer_rtx);
4035 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4036 mem = SET_SRC (set);
4037 else
4038 return 0;
4040 addr = XEXP (mem, 0);
4041 switch (GET_CODE (addr))
4043 case PRE_INC:
4044 case POST_INC:
4045 return GET_MODE_SIZE (GET_MODE (mem));
4046 case PRE_DEC:
4047 case POST_DEC:
4048 return -GET_MODE_SIZE (GET_MODE (mem));
4049 case PRE_MODIFY:
4050 case POST_MODIFY:
4051 addr = XEXP (addr, 1);
4052 gcc_assert (GET_CODE (addr) == PLUS);
4053 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4054 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4055 return INTVAL (XEXP (addr, 1));
4056 default:
4057 gcc_unreachable ();
4063 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4065 int args_size = end_args_size;
4066 bool saw_unknown = false;
4067 rtx_insn *insn;
4069 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4071 HOST_WIDE_INT this_delta;
4073 if (!NONDEBUG_INSN_P (insn))
4074 continue;
4076 this_delta = find_args_size_adjust (insn);
4077 if (this_delta == 0)
4079 if (!CALL_P (insn)
4080 || ACCUMULATE_OUTGOING_ARGS
4081 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4082 continue;
4085 gcc_assert (!saw_unknown);
4086 if (this_delta == HOST_WIDE_INT_MIN)
4087 saw_unknown = true;
4089 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4090 if (STACK_GROWS_DOWNWARD)
4091 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4093 args_size -= this_delta;
4096 return saw_unknown ? INT_MIN : args_size;
4099 #ifdef PUSH_ROUNDING
4100 /* Emit single push insn. */
4102 static void
4103 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4105 rtx dest_addr;
4106 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4107 rtx dest;
4108 enum insn_code icode;
4110 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4111 /* If there is push pattern, use it. Otherwise try old way of throwing
4112 MEM representing push operation to move expander. */
4113 icode = optab_handler (push_optab, mode);
4114 if (icode != CODE_FOR_nothing)
4116 struct expand_operand ops[1];
4118 create_input_operand (&ops[0], x, mode);
4119 if (maybe_expand_insn (icode, 1, ops))
4120 return;
4122 if (GET_MODE_SIZE (mode) == rounded_size)
4123 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4124 /* If we are to pad downward, adjust the stack pointer first and
4125 then store X into the stack location using an offset. This is
4126 because emit_move_insn does not know how to pad; it does not have
4127 access to type. */
4128 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4130 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4131 HOST_WIDE_INT offset;
4133 emit_move_insn (stack_pointer_rtx,
4134 expand_binop (Pmode,
4135 STACK_GROWS_DOWNWARD ? sub_optab
4136 : add_optab,
4137 stack_pointer_rtx,
4138 gen_int_mode (rounded_size, Pmode),
4139 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4141 offset = (HOST_WIDE_INT) padding_size;
4142 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4143 /* We have already decremented the stack pointer, so get the
4144 previous value. */
4145 offset += (HOST_WIDE_INT) rounded_size;
4147 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4148 /* We have already incremented the stack pointer, so get the
4149 previous value. */
4150 offset -= (HOST_WIDE_INT) rounded_size;
4152 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4153 gen_int_mode (offset, Pmode));
4155 else
4157 if (STACK_GROWS_DOWNWARD)
4158 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4159 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4160 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4161 Pmode));
4162 else
4163 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4164 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4165 gen_int_mode (rounded_size, Pmode));
4167 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4170 dest = gen_rtx_MEM (mode, dest_addr);
4172 if (type != 0)
4174 set_mem_attributes (dest, type, 1);
4176 if (cfun->tail_call_marked)
4177 /* Function incoming arguments may overlap with sibling call
4178 outgoing arguments and we cannot allow reordering of reads
4179 from function arguments with stores to outgoing arguments
4180 of sibling calls. */
4181 set_mem_alias_set (dest, 0);
4183 emit_move_insn (dest, x);
4186 /* Emit and annotate a single push insn. */
4188 static void
4189 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4191 int delta, old_delta = stack_pointer_delta;
4192 rtx_insn *prev = get_last_insn ();
4193 rtx_insn *last;
4195 emit_single_push_insn_1 (mode, x, type);
4197 last = get_last_insn ();
4199 /* Notice the common case where we emitted exactly one insn. */
4200 if (PREV_INSN (last) == prev)
4202 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4203 return;
4206 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4207 gcc_assert (delta == INT_MIN || delta == old_delta);
4209 #endif
4211 /* If reading SIZE bytes from X will end up reading from
4212 Y return the number of bytes that overlap. Return -1
4213 if there is no overlap or -2 if we can't determine
4214 (for example when X and Y have different base registers). */
4216 static int
4217 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4219 rtx tmp = plus_constant (Pmode, x, size);
4220 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4222 if (!CONST_INT_P (sub))
4223 return -2;
4225 HOST_WIDE_INT val = INTVAL (sub);
4227 return IN_RANGE (val, 1, size) ? val : -1;
4230 /* Generate code to push X onto the stack, assuming it has mode MODE and
4231 type TYPE.
4232 MODE is redundant except when X is a CONST_INT (since they don't
4233 carry mode info).
4234 SIZE is an rtx for the size of data to be copied (in bytes),
4235 needed only if X is BLKmode.
4236 Return true if successful. May return false if asked to push a
4237 partial argument during a sibcall optimization (as specified by
4238 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4239 to not overlap.
4241 ALIGN (in bits) is maximum alignment we can assume.
4243 If PARTIAL and REG are both nonzero, then copy that many of the first
4244 bytes of X into registers starting with REG, and push the rest of X.
4245 The amount of space pushed is decreased by PARTIAL bytes.
4246 REG must be a hard register in this case.
4247 If REG is zero but PARTIAL is not, take any all others actions for an
4248 argument partially in registers, but do not actually load any
4249 registers.
4251 EXTRA is the amount in bytes of extra space to leave next to this arg.
4252 This is ignored if an argument block has already been allocated.
4254 On a machine that lacks real push insns, ARGS_ADDR is the address of
4255 the bottom of the argument block for this call. We use indexing off there
4256 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4257 argument block has not been preallocated.
4259 ARGS_SO_FAR is the size of args previously pushed for this call.
4261 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4262 for arguments passed in registers. If nonzero, it will be the number
4263 of bytes required. */
4265 bool
4266 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4267 unsigned int align, int partial, rtx reg, int extra,
4268 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4269 rtx alignment_pad, bool sibcall_p)
4271 rtx xinner;
4272 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4274 /* Decide where to pad the argument: `downward' for below,
4275 `upward' for above, or `none' for don't pad it.
4276 Default is below for small data on big-endian machines; else above. */
4277 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4279 /* Invert direction if stack is post-decrement.
4280 FIXME: why? */
4281 if (STACK_PUSH_CODE == POST_DEC)
4282 if (where_pad != none)
4283 where_pad = (where_pad == downward ? upward : downward);
4285 xinner = x;
4287 int nregs = partial / UNITS_PER_WORD;
4288 rtx *tmp_regs = NULL;
4289 int overlapping = 0;
4291 if (mode == BLKmode
4292 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4294 /* Copy a block into the stack, entirely or partially. */
4296 rtx temp;
4297 int used;
4298 int offset;
4299 int skip;
4301 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4302 used = partial - offset;
4304 if (mode != BLKmode)
4306 /* A value is to be stored in an insufficiently aligned
4307 stack slot; copy via a suitably aligned slot if
4308 necessary. */
4309 size = GEN_INT (GET_MODE_SIZE (mode));
4310 if (!MEM_P (xinner))
4312 temp = assign_temp (type, 1, 1);
4313 emit_move_insn (temp, xinner);
4314 xinner = temp;
4318 gcc_assert (size);
4320 /* USED is now the # of bytes we need not copy to the stack
4321 because registers will take care of them. */
4323 if (partial != 0)
4324 xinner = adjust_address (xinner, BLKmode, used);
4326 /* If the partial register-part of the arg counts in its stack size,
4327 skip the part of stack space corresponding to the registers.
4328 Otherwise, start copying to the beginning of the stack space,
4329 by setting SKIP to 0. */
4330 skip = (reg_parm_stack_space == 0) ? 0 : used;
4332 #ifdef PUSH_ROUNDING
4333 /* Do it with several push insns if that doesn't take lots of insns
4334 and if there is no difficulty with push insns that skip bytes
4335 on the stack for alignment purposes. */
4336 if (args_addr == 0
4337 && PUSH_ARGS
4338 && CONST_INT_P (size)
4339 && skip == 0
4340 && MEM_ALIGN (xinner) >= align
4341 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4342 /* Here we avoid the case of a structure whose weak alignment
4343 forces many pushes of a small amount of data,
4344 and such small pushes do rounding that causes trouble. */
4345 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4346 || align >= BIGGEST_ALIGNMENT
4347 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4348 == (align / BITS_PER_UNIT)))
4349 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4351 /* Push padding now if padding above and stack grows down,
4352 or if padding below and stack grows up.
4353 But if space already allocated, this has already been done. */
4354 if (extra && args_addr == 0
4355 && where_pad != none && where_pad != stack_direction)
4356 anti_adjust_stack (GEN_INT (extra));
4358 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4360 else
4361 #endif /* PUSH_ROUNDING */
4363 rtx target;
4365 /* Otherwise make space on the stack and copy the data
4366 to the address of that space. */
4368 /* Deduct words put into registers from the size we must copy. */
4369 if (partial != 0)
4371 if (CONST_INT_P (size))
4372 size = GEN_INT (INTVAL (size) - used);
4373 else
4374 size = expand_binop (GET_MODE (size), sub_optab, size,
4375 gen_int_mode (used, GET_MODE (size)),
4376 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4379 /* Get the address of the stack space.
4380 In this case, we do not deal with EXTRA separately.
4381 A single stack adjust will do. */
4382 if (! args_addr)
4384 temp = push_block (size, extra, where_pad == downward);
4385 extra = 0;
4387 else if (CONST_INT_P (args_so_far))
4388 temp = memory_address (BLKmode,
4389 plus_constant (Pmode, args_addr,
4390 skip + INTVAL (args_so_far)));
4391 else
4392 temp = memory_address (BLKmode,
4393 plus_constant (Pmode,
4394 gen_rtx_PLUS (Pmode,
4395 args_addr,
4396 args_so_far),
4397 skip));
4399 if (!ACCUMULATE_OUTGOING_ARGS)
4401 /* If the source is referenced relative to the stack pointer,
4402 copy it to another register to stabilize it. We do not need
4403 to do this if we know that we won't be changing sp. */
4405 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4406 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4407 temp = copy_to_reg (temp);
4410 target = gen_rtx_MEM (BLKmode, temp);
4412 /* We do *not* set_mem_attributes here, because incoming arguments
4413 may overlap with sibling call outgoing arguments and we cannot
4414 allow reordering of reads from function arguments with stores
4415 to outgoing arguments of sibling calls. We do, however, want
4416 to record the alignment of the stack slot. */
4417 /* ALIGN may well be better aligned than TYPE, e.g. due to
4418 PARM_BOUNDARY. Assume the caller isn't lying. */
4419 set_mem_align (target, align);
4421 /* If part should go in registers and pushing to that part would
4422 overwrite some of the values that need to go into regs, load the
4423 overlapping values into temporary pseudos to be moved into the hard
4424 regs at the end after the stack pushing has completed.
4425 We cannot load them directly into the hard regs here because
4426 they can be clobbered by the block move expansions.
4427 See PR 65358. */
4429 if (partial > 0 && reg != 0 && mode == BLKmode
4430 && GET_CODE (reg) != PARALLEL)
4432 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4433 if (overlapping > 0)
4435 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4436 overlapping /= UNITS_PER_WORD;
4438 tmp_regs = XALLOCAVEC (rtx, overlapping);
4440 for (int i = 0; i < overlapping; i++)
4441 tmp_regs[i] = gen_reg_rtx (word_mode);
4443 for (int i = 0; i < overlapping; i++)
4444 emit_move_insn (tmp_regs[i],
4445 operand_subword_force (target, i, mode));
4447 else if (overlapping == -1)
4448 overlapping = 0;
4449 /* Could not determine whether there is overlap.
4450 Fail the sibcall. */
4451 else
4453 overlapping = 0;
4454 if (sibcall_p)
4455 return false;
4458 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4461 else if (partial > 0)
4463 /* Scalar partly in registers. */
4465 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4466 int i;
4467 int not_stack;
4468 /* # bytes of start of argument
4469 that we must make space for but need not store. */
4470 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4471 int args_offset = INTVAL (args_so_far);
4472 int skip;
4474 /* Push padding now if padding above and stack grows down,
4475 or if padding below and stack grows up.
4476 But if space already allocated, this has already been done. */
4477 if (extra && args_addr == 0
4478 && where_pad != none && where_pad != stack_direction)
4479 anti_adjust_stack (GEN_INT (extra));
4481 /* If we make space by pushing it, we might as well push
4482 the real data. Otherwise, we can leave OFFSET nonzero
4483 and leave the space uninitialized. */
4484 if (args_addr == 0)
4485 offset = 0;
4487 /* Now NOT_STACK gets the number of words that we don't need to
4488 allocate on the stack. Convert OFFSET to words too. */
4489 not_stack = (partial - offset) / UNITS_PER_WORD;
4490 offset /= UNITS_PER_WORD;
4492 /* If the partial register-part of the arg counts in its stack size,
4493 skip the part of stack space corresponding to the registers.
4494 Otherwise, start copying to the beginning of the stack space,
4495 by setting SKIP to 0. */
4496 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4498 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4499 x = validize_mem (force_const_mem (mode, x));
4501 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4502 SUBREGs of such registers are not allowed. */
4503 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4504 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4505 x = copy_to_reg (x);
4507 /* Loop over all the words allocated on the stack for this arg. */
4508 /* We can do it by words, because any scalar bigger than a word
4509 has a size a multiple of a word. */
4510 for (i = size - 1; i >= not_stack; i--)
4511 if (i >= not_stack + offset)
4512 if (!emit_push_insn (operand_subword_force (x, i, mode),
4513 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4514 0, args_addr,
4515 GEN_INT (args_offset + ((i - not_stack + skip)
4516 * UNITS_PER_WORD)),
4517 reg_parm_stack_space, alignment_pad, sibcall_p))
4518 return false;
4520 else
4522 rtx addr;
4523 rtx dest;
4525 /* Push padding now if padding above and stack grows down,
4526 or if padding below and stack grows up.
4527 But if space already allocated, this has already been done. */
4528 if (extra && args_addr == 0
4529 && where_pad != none && where_pad != stack_direction)
4530 anti_adjust_stack (GEN_INT (extra));
4532 #ifdef PUSH_ROUNDING
4533 if (args_addr == 0 && PUSH_ARGS)
4534 emit_single_push_insn (mode, x, type);
4535 else
4536 #endif
4538 if (CONST_INT_P (args_so_far))
4539 addr
4540 = memory_address (mode,
4541 plus_constant (Pmode, args_addr,
4542 INTVAL (args_so_far)));
4543 else
4544 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4545 args_so_far));
4546 dest = gen_rtx_MEM (mode, addr);
4548 /* We do *not* set_mem_attributes here, because incoming arguments
4549 may overlap with sibling call outgoing arguments and we cannot
4550 allow reordering of reads from function arguments with stores
4551 to outgoing arguments of sibling calls. We do, however, want
4552 to record the alignment of the stack slot. */
4553 /* ALIGN may well be better aligned than TYPE, e.g. due to
4554 PARM_BOUNDARY. Assume the caller isn't lying. */
4555 set_mem_align (dest, align);
4557 emit_move_insn (dest, x);
4561 /* Move the partial arguments into the registers and any overlapping
4562 values that we moved into the pseudos in tmp_regs. */
4563 if (partial > 0 && reg != 0)
4565 /* Handle calls that pass values in multiple non-contiguous locations.
4566 The Irix 6 ABI has examples of this. */
4567 if (GET_CODE (reg) == PARALLEL)
4568 emit_group_load (reg, x, type, -1);
4569 else
4571 gcc_assert (partial % UNITS_PER_WORD == 0);
4572 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4574 for (int i = 0; i < overlapping; i++)
4575 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4576 + nregs - overlapping + i),
4577 tmp_regs[i]);
4582 if (extra && args_addr == 0 && where_pad == stack_direction)
4583 anti_adjust_stack (GEN_INT (extra));
4585 if (alignment_pad && args_addr == 0)
4586 anti_adjust_stack (alignment_pad);
4588 return true;
4591 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4592 operations. */
4594 static rtx
4595 get_subtarget (rtx x)
4597 return (optimize
4598 || x == 0
4599 /* Only registers can be subtargets. */
4600 || !REG_P (x)
4601 /* Don't use hard regs to avoid extending their life. */
4602 || REGNO (x) < FIRST_PSEUDO_REGISTER
4603 ? 0 : x);
4606 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4607 FIELD is a bitfield. Returns true if the optimization was successful,
4608 and there's nothing else to do. */
4610 static bool
4611 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4612 unsigned HOST_WIDE_INT bitpos,
4613 unsigned HOST_WIDE_INT bitregion_start,
4614 unsigned HOST_WIDE_INT bitregion_end,
4615 machine_mode mode1, rtx str_rtx,
4616 tree to, tree src, bool reverse)
4618 machine_mode str_mode = GET_MODE (str_rtx);
4619 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4620 tree op0, op1;
4621 rtx value, result;
4622 optab binop;
4623 gimple *srcstmt;
4624 enum tree_code code;
4626 if (mode1 != VOIDmode
4627 || bitsize >= BITS_PER_WORD
4628 || str_bitsize > BITS_PER_WORD
4629 || TREE_SIDE_EFFECTS (to)
4630 || TREE_THIS_VOLATILE (to))
4631 return false;
4633 STRIP_NOPS (src);
4634 if (TREE_CODE (src) != SSA_NAME)
4635 return false;
4636 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4637 return false;
4639 srcstmt = get_gimple_for_ssa_name (src);
4640 if (!srcstmt
4641 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4642 return false;
4644 code = gimple_assign_rhs_code (srcstmt);
4646 op0 = gimple_assign_rhs1 (srcstmt);
4648 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4649 to find its initialization. Hopefully the initialization will
4650 be from a bitfield load. */
4651 if (TREE_CODE (op0) == SSA_NAME)
4653 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4655 /* We want to eventually have OP0 be the same as TO, which
4656 should be a bitfield. */
4657 if (!op0stmt
4658 || !is_gimple_assign (op0stmt)
4659 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4660 return false;
4661 op0 = gimple_assign_rhs1 (op0stmt);
4664 op1 = gimple_assign_rhs2 (srcstmt);
4666 if (!operand_equal_p (to, op0, 0))
4667 return false;
4669 if (MEM_P (str_rtx))
4671 unsigned HOST_WIDE_INT offset1;
4673 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4674 str_mode = word_mode;
4675 str_mode = get_best_mode (bitsize, bitpos,
4676 bitregion_start, bitregion_end,
4677 MEM_ALIGN (str_rtx), str_mode, 0);
4678 if (str_mode == VOIDmode)
4679 return false;
4680 str_bitsize = GET_MODE_BITSIZE (str_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 || 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 (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;
5423 /* We can do the conversion inside EXP, which will often result
5424 in some optimizations. Do the conversion in two steps: first
5425 change the signedness, if needed, then the extend. But don't
5426 do this if the type of EXP is a subtype of something else
5427 since then the conversion might involve more than just
5428 converting modes. */
5429 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5430 && TREE_TYPE (TREE_TYPE (exp)) == 0
5431 && GET_MODE_PRECISION (GET_MODE (target))
5432 == TYPE_PRECISION (TREE_TYPE (exp)))
5434 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5435 TYPE_UNSIGNED (TREE_TYPE (exp))))
5437 /* Some types, e.g. Fortran's logical*4, won't have a signed
5438 version, so use the mode instead. */
5439 tree ntype
5440 = (signed_or_unsigned_type_for
5441 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5442 if (ntype == NULL)
5443 ntype = lang_hooks.types.type_for_mode
5444 (TYPE_MODE (TREE_TYPE (exp)),
5445 SUBREG_PROMOTED_SIGN (target));
5447 exp = fold_convert_loc (loc, ntype, exp);
5450 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5451 (GET_MODE (SUBREG_REG (target)),
5452 SUBREG_PROMOTED_SIGN (target)),
5453 exp);
5455 inner_target = SUBREG_REG (target);
5458 temp = expand_expr (exp, inner_target, VOIDmode,
5459 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5461 /* Handle bounds returned by call. */
5462 if (TREE_CODE (exp) == CALL_EXPR)
5464 rtx bounds;
5465 chkp_split_slot (temp, &temp, &bounds);
5466 if (bounds && btarget)
5468 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5469 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5470 chkp_set_rtl_bounds (btarget, tmp);
5474 /* If TEMP is a VOIDmode constant, use convert_modes to make
5475 sure that we properly convert it. */
5476 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5478 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5479 temp, SUBREG_PROMOTED_SIGN (target));
5480 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5481 GET_MODE (target), 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. */
6753 if (bitsize == 0)
6754 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6756 if (GET_CODE (target) == CONCAT)
6758 /* We're storing into a struct containing a single __complex. */
6760 gcc_assert (!bitpos);
6761 return store_expr (exp, target, 0, nontemporal, reverse);
6764 /* If the structure is in a register or if the component
6765 is a bit field, we cannot use addressing to access it.
6766 Use bit-field techniques or SUBREG to store in it. */
6768 if (mode == VOIDmode
6769 || (mode != BLKmode && ! direct_store[(int) mode]
6770 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6771 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6772 || REG_P (target)
6773 || GET_CODE (target) == SUBREG
6774 /* If the field isn't aligned enough to store as an ordinary memref,
6775 store it as a bit field. */
6776 || (mode != BLKmode
6777 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6778 || bitpos % GET_MODE_ALIGNMENT (mode))
6779 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6780 || (bitpos % BITS_PER_UNIT != 0)))
6781 || (bitsize >= 0 && mode != BLKmode
6782 && GET_MODE_BITSIZE (mode) > bitsize)
6783 /* If the RHS and field are a constant size and the size of the
6784 RHS isn't the same size as the bitfield, we must use bitfield
6785 operations. */
6786 || (bitsize >= 0
6787 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6788 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6789 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6790 we will handle specially below. */
6791 && !(TREE_CODE (exp) == CONSTRUCTOR
6792 && bitsize % BITS_PER_UNIT == 0)
6793 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6794 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6795 includes some extra padding. store_expr / expand_expr will in
6796 that case call get_inner_reference that will have the bitsize
6797 we check here and thus the block move will not clobber the
6798 padding that shouldn't be clobbered. In the future we could
6799 replace the TREE_ADDRESSABLE check with a check that
6800 get_base_address needs to live in memory. */
6801 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6802 || TREE_CODE (exp) != COMPONENT_REF
6803 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6804 || (bitsize % BITS_PER_UNIT != 0)
6805 || (bitpos % BITS_PER_UNIT != 0)
6806 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6807 != 0)))
6808 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6809 decl we must use bitfield operations. */
6810 || (bitsize >= 0
6811 && TREE_CODE (exp) == MEM_REF
6812 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6813 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6814 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6815 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6817 rtx temp;
6818 gimple *nop_def;
6820 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6821 implies a mask operation. If the precision is the same size as
6822 the field we're storing into, that mask is redundant. This is
6823 particularly common with bit field assignments generated by the
6824 C front end. */
6825 nop_def = get_def_for_expr (exp, NOP_EXPR);
6826 if (nop_def)
6828 tree type = TREE_TYPE (exp);
6829 if (INTEGRAL_TYPE_P (type)
6830 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6831 && bitsize == TYPE_PRECISION (type))
6833 tree op = gimple_assign_rhs1 (nop_def);
6834 type = TREE_TYPE (op);
6835 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6836 exp = op;
6840 temp = expand_normal (exp);
6842 /* Handle calls that return values in multiple non-contiguous locations.
6843 The Irix 6 ABI has examples of this. */
6844 if (GET_CODE (temp) == PARALLEL)
6846 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6847 machine_mode temp_mode
6848 = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6849 rtx temp_target = gen_reg_rtx (temp_mode);
6850 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6851 temp = temp_target;
6854 /* Handle calls that return BLKmode values in registers. */
6855 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6857 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6858 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6859 temp = temp_target;
6862 /* If the value has aggregate type and an integral mode then, if BITSIZE
6863 is narrower than this mode and this is for big-endian data, we first
6864 need to put the value into the low-order bits for store_bit_field,
6865 except when MODE is BLKmode and BITSIZE larger than the word size
6866 (see the handling of fields larger than a word in store_bit_field).
6867 Moreover, the field may be not aligned on a byte boundary; in this
6868 case, if it has reverse storage order, it needs to be accessed as a
6869 scalar field with reverse storage order and we must first put the
6870 value into target order. */
6871 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6872 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6874 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6876 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6878 if (reverse)
6879 temp = flip_storage_order (GET_MODE (temp), temp);
6881 if (bitsize < size
6882 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6883 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6884 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6885 size - bitsize, NULL_RTX, 1);
6888 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6889 if (mode != VOIDmode && mode != BLKmode
6890 && mode != TYPE_MODE (TREE_TYPE (exp)))
6891 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6893 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6894 and BITPOS must be aligned on a byte boundary. If so, we simply do
6895 a block copy. Likewise for a BLKmode-like TARGET. */
6896 if (GET_MODE (temp) == BLKmode
6897 && (GET_MODE (target) == BLKmode
6898 || (MEM_P (target)
6899 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6900 && (bitpos % BITS_PER_UNIT) == 0
6901 && (bitsize % BITS_PER_UNIT) == 0)))
6903 gcc_assert (MEM_P (target) && MEM_P (temp)
6904 && (bitpos % BITS_PER_UNIT) == 0);
6906 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6907 emit_block_move (target, temp,
6908 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6909 / BITS_PER_UNIT),
6910 BLOCK_OP_NORMAL);
6912 return const0_rtx;
6915 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6916 word size, we need to load the value (see again store_bit_field). */
6917 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6919 machine_mode temp_mode = smallest_mode_for_size (bitsize, MODE_INT);
6920 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6921 temp_mode, false, NULL);
6924 /* Store the value in the bitfield. */
6925 store_bit_field (target, bitsize, bitpos,
6926 bitregion_start, bitregion_end,
6927 mode, temp, reverse);
6929 return const0_rtx;
6931 else
6933 /* Now build a reference to just the desired component. */
6934 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6936 if (to_rtx == target)
6937 to_rtx = copy_rtx (to_rtx);
6939 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6940 set_mem_alias_set (to_rtx, alias_set);
6942 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6943 into a target smaller than its type; handle that case now. */
6944 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6946 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6947 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6948 return to_rtx;
6951 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6955 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6956 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6957 codes and find the ultimate containing object, which we return.
6959 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6960 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6961 storage order of the field.
6962 If the position of the field is variable, we store a tree
6963 giving the variable offset (in units) in *POFFSET.
6964 This offset is in addition to the bit position.
6965 If the position is not variable, we store 0 in *POFFSET.
6967 If any of the extraction expressions is volatile,
6968 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6970 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6971 Otherwise, it is a mode that can be used to access the field.
6973 If the field describes a variable-sized object, *PMODE is set to
6974 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6975 this case, but the address of the object can be found. */
6977 tree
6978 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6979 HOST_WIDE_INT *pbitpos, tree *poffset,
6980 machine_mode *pmode, int *punsignedp,
6981 int *preversep, int *pvolatilep)
6983 tree size_tree = 0;
6984 machine_mode mode = VOIDmode;
6985 bool blkmode_bitfield = false;
6986 tree offset = size_zero_node;
6987 offset_int bit_offset = 0;
6989 /* First get the mode, signedness, storage order and size. We do this from
6990 just the outermost expression. */
6991 *pbitsize = -1;
6992 if (TREE_CODE (exp) == COMPONENT_REF)
6994 tree field = TREE_OPERAND (exp, 1);
6995 size_tree = DECL_SIZE (field);
6996 if (flag_strict_volatile_bitfields > 0
6997 && TREE_THIS_VOLATILE (exp)
6998 && DECL_BIT_FIELD_TYPE (field)
6999 && DECL_MODE (field) != BLKmode)
7000 /* Volatile bitfields should be accessed in the mode of the
7001 field's type, not the mode computed based on the bit
7002 size. */
7003 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7004 else if (!DECL_BIT_FIELD (field))
7005 mode = DECL_MODE (field);
7006 else if (DECL_MODE (field) == BLKmode)
7007 blkmode_bitfield = true;
7009 *punsignedp = DECL_UNSIGNED (field);
7011 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7013 size_tree = TREE_OPERAND (exp, 1);
7014 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7015 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7017 /* For vector types, with the correct size of access, use the mode of
7018 inner type. */
7019 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7020 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7021 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7022 mode = TYPE_MODE (TREE_TYPE (exp));
7024 else
7026 mode = TYPE_MODE (TREE_TYPE (exp));
7027 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7029 if (mode == BLKmode)
7030 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7031 else
7032 *pbitsize = GET_MODE_BITSIZE (mode);
7035 if (size_tree != 0)
7037 if (! tree_fits_uhwi_p (size_tree))
7038 mode = BLKmode, *pbitsize = -1;
7039 else
7040 *pbitsize = tree_to_uhwi (size_tree);
7043 *preversep = reverse_storage_order_for_component_p (exp);
7045 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7046 and find the ultimate containing object. */
7047 while (1)
7049 switch (TREE_CODE (exp))
7051 case BIT_FIELD_REF:
7052 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7053 break;
7055 case COMPONENT_REF:
7057 tree field = TREE_OPERAND (exp, 1);
7058 tree this_offset = component_ref_field_offset (exp);
7060 /* If this field hasn't been filled in yet, don't go past it.
7061 This should only happen when folding expressions made during
7062 type construction. */
7063 if (this_offset == 0)
7064 break;
7066 offset = size_binop (PLUS_EXPR, offset, this_offset);
7067 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7069 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7071 break;
7073 case ARRAY_REF:
7074 case ARRAY_RANGE_REF:
7076 tree index = TREE_OPERAND (exp, 1);
7077 tree low_bound = array_ref_low_bound (exp);
7078 tree unit_size = array_ref_element_size (exp);
7080 /* We assume all arrays have sizes that are a multiple of a byte.
7081 First subtract the lower bound, if any, in the type of the
7082 index, then convert to sizetype and multiply by the size of
7083 the array element. */
7084 if (! integer_zerop (low_bound))
7085 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7086 index, low_bound);
7088 offset = size_binop (PLUS_EXPR, offset,
7089 size_binop (MULT_EXPR,
7090 fold_convert (sizetype, index),
7091 unit_size));
7093 break;
7095 case REALPART_EXPR:
7096 break;
7098 case IMAGPART_EXPR:
7099 bit_offset += *pbitsize;
7100 break;
7102 case VIEW_CONVERT_EXPR:
7103 break;
7105 case MEM_REF:
7106 /* Hand back the decl for MEM[&decl, off]. */
7107 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7109 tree off = TREE_OPERAND (exp, 1);
7110 if (!integer_zerop (off))
7112 offset_int boff, coff = mem_ref_offset (exp);
7113 boff = coff << LOG2_BITS_PER_UNIT;
7114 bit_offset += boff;
7116 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7118 goto done;
7120 default:
7121 goto done;
7124 /* If any reference in the chain is volatile, the effect is volatile. */
7125 if (TREE_THIS_VOLATILE (exp))
7126 *pvolatilep = 1;
7128 exp = TREE_OPERAND (exp, 0);
7130 done:
7132 /* If OFFSET is constant, see if we can return the whole thing as a
7133 constant bit position. Make sure to handle overflow during
7134 this conversion. */
7135 if (TREE_CODE (offset) == INTEGER_CST)
7137 offset_int tem = wi::sext (wi::to_offset (offset),
7138 TYPE_PRECISION (sizetype));
7139 tem <<= LOG2_BITS_PER_UNIT;
7140 tem += bit_offset;
7141 if (wi::fits_shwi_p (tem))
7143 *pbitpos = tem.to_shwi ();
7144 *poffset = offset = NULL_TREE;
7148 /* Otherwise, split it up. */
7149 if (offset)
7151 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7152 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7154 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7155 offset_int tem = bit_offset.and_not (mask);
7156 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7157 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7158 bit_offset -= tem;
7159 tem >>= LOG2_BITS_PER_UNIT;
7160 offset = size_binop (PLUS_EXPR, offset,
7161 wide_int_to_tree (sizetype, tem));
7164 *pbitpos = bit_offset.to_shwi ();
7165 *poffset = offset;
7168 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7169 if (mode == VOIDmode
7170 && blkmode_bitfield
7171 && (*pbitpos % BITS_PER_UNIT) == 0
7172 && (*pbitsize % BITS_PER_UNIT) == 0)
7173 *pmode = BLKmode;
7174 else
7175 *pmode = mode;
7177 return exp;
7180 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7182 static unsigned HOST_WIDE_INT
7183 target_align (const_tree target)
7185 /* We might have a chain of nested references with intermediate misaligning
7186 bitfields components, so need to recurse to find out. */
7188 unsigned HOST_WIDE_INT this_align, outer_align;
7190 switch (TREE_CODE (target))
7192 case BIT_FIELD_REF:
7193 return 1;
7195 case COMPONENT_REF:
7196 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7197 outer_align = target_align (TREE_OPERAND (target, 0));
7198 return MIN (this_align, outer_align);
7200 case ARRAY_REF:
7201 case ARRAY_RANGE_REF:
7202 this_align = TYPE_ALIGN (TREE_TYPE (target));
7203 outer_align = target_align (TREE_OPERAND (target, 0));
7204 return MIN (this_align, outer_align);
7206 CASE_CONVERT:
7207 case NON_LVALUE_EXPR:
7208 case VIEW_CONVERT_EXPR:
7209 this_align = TYPE_ALIGN (TREE_TYPE (target));
7210 outer_align = target_align (TREE_OPERAND (target, 0));
7211 return MAX (this_align, outer_align);
7213 default:
7214 return TYPE_ALIGN (TREE_TYPE (target));
7219 /* Given an rtx VALUE that may contain additions and multiplications, return
7220 an equivalent value that just refers to a register, memory, or constant.
7221 This is done by generating instructions to perform the arithmetic and
7222 returning a pseudo-register containing the value.
7224 The returned value may be a REG, SUBREG, MEM or constant. */
7227 force_operand (rtx value, rtx target)
7229 rtx op1, op2;
7230 /* Use subtarget as the target for operand 0 of a binary operation. */
7231 rtx subtarget = get_subtarget (target);
7232 enum rtx_code code = GET_CODE (value);
7234 /* Check for subreg applied to an expression produced by loop optimizer. */
7235 if (code == SUBREG
7236 && !REG_P (SUBREG_REG (value))
7237 && !MEM_P (SUBREG_REG (value)))
7239 value
7240 = simplify_gen_subreg (GET_MODE (value),
7241 force_reg (GET_MODE (SUBREG_REG (value)),
7242 force_operand (SUBREG_REG (value),
7243 NULL_RTX)),
7244 GET_MODE (SUBREG_REG (value)),
7245 SUBREG_BYTE (value));
7246 code = GET_CODE (value);
7249 /* Check for a PIC address load. */
7250 if ((code == PLUS || code == MINUS)
7251 && XEXP (value, 0) == pic_offset_table_rtx
7252 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7253 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7254 || GET_CODE (XEXP (value, 1)) == CONST))
7256 if (!subtarget)
7257 subtarget = gen_reg_rtx (GET_MODE (value));
7258 emit_move_insn (subtarget, value);
7259 return subtarget;
7262 if (ARITHMETIC_P (value))
7264 op2 = XEXP (value, 1);
7265 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7266 subtarget = 0;
7267 if (code == MINUS && CONST_INT_P (op2))
7269 code = PLUS;
7270 op2 = negate_rtx (GET_MODE (value), op2);
7273 /* Check for an addition with OP2 a constant integer and our first
7274 operand a PLUS of a virtual register and something else. In that
7275 case, we want to emit the sum of the virtual register and the
7276 constant first and then add the other value. This allows virtual
7277 register instantiation to simply modify the constant rather than
7278 creating another one around this addition. */
7279 if (code == PLUS && CONST_INT_P (op2)
7280 && GET_CODE (XEXP (value, 0)) == PLUS
7281 && REG_P (XEXP (XEXP (value, 0), 0))
7282 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7283 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7285 rtx temp = expand_simple_binop (GET_MODE (value), code,
7286 XEXP (XEXP (value, 0), 0), op2,
7287 subtarget, 0, OPTAB_LIB_WIDEN);
7288 return expand_simple_binop (GET_MODE (value), code, temp,
7289 force_operand (XEXP (XEXP (value,
7290 0), 1), 0),
7291 target, 0, OPTAB_LIB_WIDEN);
7294 op1 = force_operand (XEXP (value, 0), subtarget);
7295 op2 = force_operand (op2, NULL_RTX);
7296 switch (code)
7298 case MULT:
7299 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7300 case DIV:
7301 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7302 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7303 target, 1, OPTAB_LIB_WIDEN);
7304 else
7305 return expand_divmod (0,
7306 FLOAT_MODE_P (GET_MODE (value))
7307 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7308 GET_MODE (value), op1, op2, target, 0);
7309 case MOD:
7310 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7311 target, 0);
7312 case UDIV:
7313 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7314 target, 1);
7315 case UMOD:
7316 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7317 target, 1);
7318 case ASHIFTRT:
7319 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7320 target, 0, OPTAB_LIB_WIDEN);
7321 default:
7322 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7323 target, 1, OPTAB_LIB_WIDEN);
7326 if (UNARY_P (value))
7328 if (!target)
7329 target = gen_reg_rtx (GET_MODE (value));
7330 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7331 switch (code)
7333 case ZERO_EXTEND:
7334 case SIGN_EXTEND:
7335 case TRUNCATE:
7336 case FLOAT_EXTEND:
7337 case FLOAT_TRUNCATE:
7338 convert_move (target, op1, code == ZERO_EXTEND);
7339 return target;
7341 case FIX:
7342 case UNSIGNED_FIX:
7343 expand_fix (target, op1, code == UNSIGNED_FIX);
7344 return target;
7346 case FLOAT:
7347 case UNSIGNED_FLOAT:
7348 expand_float (target, op1, code == UNSIGNED_FLOAT);
7349 return target;
7351 default:
7352 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7356 #ifdef INSN_SCHEDULING
7357 /* On machines that have insn scheduling, we want all memory reference to be
7358 explicit, so we need to deal with such paradoxical SUBREGs. */
7359 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7360 value
7361 = simplify_gen_subreg (GET_MODE (value),
7362 force_reg (GET_MODE (SUBREG_REG (value)),
7363 force_operand (SUBREG_REG (value),
7364 NULL_RTX)),
7365 GET_MODE (SUBREG_REG (value)),
7366 SUBREG_BYTE (value));
7367 #endif
7369 return value;
7372 /* Subroutine of expand_expr: return nonzero iff there is no way that
7373 EXP can reference X, which is being modified. TOP_P is nonzero if this
7374 call is going to be used to determine whether we need a temporary
7375 for EXP, as opposed to a recursive call to this function.
7377 It is always safe for this routine to return zero since it merely
7378 searches for optimization opportunities. */
7381 safe_from_p (const_rtx x, tree exp, int top_p)
7383 rtx exp_rtl = 0;
7384 int i, nops;
7386 if (x == 0
7387 /* If EXP has varying size, we MUST use a target since we currently
7388 have no way of allocating temporaries of variable size
7389 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7390 So we assume here that something at a higher level has prevented a
7391 clash. This is somewhat bogus, but the best we can do. Only
7392 do this when X is BLKmode and when we are at the top level. */
7393 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7394 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7395 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7396 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7397 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7398 != INTEGER_CST)
7399 && GET_MODE (x) == BLKmode)
7400 /* If X is in the outgoing argument area, it is always safe. */
7401 || (MEM_P (x)
7402 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7403 || (GET_CODE (XEXP (x, 0)) == PLUS
7404 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7405 return 1;
7407 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7408 find the underlying pseudo. */
7409 if (GET_CODE (x) == SUBREG)
7411 x = SUBREG_REG (x);
7412 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7413 return 0;
7416 /* Now look at our tree code and possibly recurse. */
7417 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7419 case tcc_declaration:
7420 exp_rtl = DECL_RTL_IF_SET (exp);
7421 break;
7423 case tcc_constant:
7424 return 1;
7426 case tcc_exceptional:
7427 if (TREE_CODE (exp) == TREE_LIST)
7429 while (1)
7431 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7432 return 0;
7433 exp = TREE_CHAIN (exp);
7434 if (!exp)
7435 return 1;
7436 if (TREE_CODE (exp) != TREE_LIST)
7437 return safe_from_p (x, exp, 0);
7440 else if (TREE_CODE (exp) == CONSTRUCTOR)
7442 constructor_elt *ce;
7443 unsigned HOST_WIDE_INT idx;
7445 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7446 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7447 || !safe_from_p (x, ce->value, 0))
7448 return 0;
7449 return 1;
7451 else if (TREE_CODE (exp) == ERROR_MARK)
7452 return 1; /* An already-visited SAVE_EXPR? */
7453 else
7454 return 0;
7456 case tcc_statement:
7457 /* The only case we look at here is the DECL_INITIAL inside a
7458 DECL_EXPR. */
7459 return (TREE_CODE (exp) != DECL_EXPR
7460 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7461 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7462 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7464 case tcc_binary:
7465 case tcc_comparison:
7466 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7467 return 0;
7468 /* Fall through. */
7470 case tcc_unary:
7471 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7473 case tcc_expression:
7474 case tcc_reference:
7475 case tcc_vl_exp:
7476 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7477 the expression. If it is set, we conflict iff we are that rtx or
7478 both are in memory. Otherwise, we check all operands of the
7479 expression recursively. */
7481 switch (TREE_CODE (exp))
7483 case ADDR_EXPR:
7484 /* If the operand is static or we are static, we can't conflict.
7485 Likewise if we don't conflict with the operand at all. */
7486 if (staticp (TREE_OPERAND (exp, 0))
7487 || TREE_STATIC (exp)
7488 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7489 return 1;
7491 /* Otherwise, the only way this can conflict is if we are taking
7492 the address of a DECL a that address if part of X, which is
7493 very rare. */
7494 exp = TREE_OPERAND (exp, 0);
7495 if (DECL_P (exp))
7497 if (!DECL_RTL_SET_P (exp)
7498 || !MEM_P (DECL_RTL (exp)))
7499 return 0;
7500 else
7501 exp_rtl = XEXP (DECL_RTL (exp), 0);
7503 break;
7505 case MEM_REF:
7506 if (MEM_P (x)
7507 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7508 get_alias_set (exp)))
7509 return 0;
7510 break;
7512 case CALL_EXPR:
7513 /* Assume that the call will clobber all hard registers and
7514 all of memory. */
7515 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7516 || MEM_P (x))
7517 return 0;
7518 break;
7520 case WITH_CLEANUP_EXPR:
7521 case CLEANUP_POINT_EXPR:
7522 /* Lowered by gimplify.c. */
7523 gcc_unreachable ();
7525 case SAVE_EXPR:
7526 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7528 default:
7529 break;
7532 /* If we have an rtx, we do not need to scan our operands. */
7533 if (exp_rtl)
7534 break;
7536 nops = TREE_OPERAND_LENGTH (exp);
7537 for (i = 0; i < nops; i++)
7538 if (TREE_OPERAND (exp, i) != 0
7539 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7540 return 0;
7542 break;
7544 case tcc_type:
7545 /* Should never get a type here. */
7546 gcc_unreachable ();
7549 /* If we have an rtl, find any enclosed object. Then see if we conflict
7550 with it. */
7551 if (exp_rtl)
7553 if (GET_CODE (exp_rtl) == SUBREG)
7555 exp_rtl = SUBREG_REG (exp_rtl);
7556 if (REG_P (exp_rtl)
7557 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7558 return 0;
7561 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7562 are memory and they conflict. */
7563 return ! (rtx_equal_p (x, exp_rtl)
7564 || (MEM_P (x) && MEM_P (exp_rtl)
7565 && true_dependence (exp_rtl, VOIDmode, x)));
7568 /* If we reach here, it is safe. */
7569 return 1;
7573 /* Return the highest power of two that EXP is known to be a multiple of.
7574 This is used in updating alignment of MEMs in array references. */
7576 unsigned HOST_WIDE_INT
7577 highest_pow2_factor (const_tree exp)
7579 unsigned HOST_WIDE_INT ret;
7580 int trailing_zeros = tree_ctz (exp);
7581 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7582 return BIGGEST_ALIGNMENT;
7583 ret = HOST_WIDE_INT_1U << trailing_zeros;
7584 if (ret > BIGGEST_ALIGNMENT)
7585 return BIGGEST_ALIGNMENT;
7586 return ret;
7589 /* Similar, except that the alignment requirements of TARGET are
7590 taken into account. Assume it is at least as aligned as its
7591 type, unless it is a COMPONENT_REF in which case the layout of
7592 the structure gives the alignment. */
7594 static unsigned HOST_WIDE_INT
7595 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7597 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7598 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7600 return MAX (factor, talign);
7603 /* Convert the tree comparison code TCODE to the rtl one where the
7604 signedness is UNSIGNEDP. */
7606 static enum rtx_code
7607 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7609 enum rtx_code code;
7610 switch (tcode)
7612 case EQ_EXPR:
7613 code = EQ;
7614 break;
7615 case NE_EXPR:
7616 code = NE;
7617 break;
7618 case LT_EXPR:
7619 code = unsignedp ? LTU : LT;
7620 break;
7621 case LE_EXPR:
7622 code = unsignedp ? LEU : LE;
7623 break;
7624 case GT_EXPR:
7625 code = unsignedp ? GTU : GT;
7626 break;
7627 case GE_EXPR:
7628 code = unsignedp ? GEU : GE;
7629 break;
7630 case UNORDERED_EXPR:
7631 code = UNORDERED;
7632 break;
7633 case ORDERED_EXPR:
7634 code = ORDERED;
7635 break;
7636 case UNLT_EXPR:
7637 code = UNLT;
7638 break;
7639 case UNLE_EXPR:
7640 code = UNLE;
7641 break;
7642 case UNGT_EXPR:
7643 code = UNGT;
7644 break;
7645 case UNGE_EXPR:
7646 code = UNGE;
7647 break;
7648 case UNEQ_EXPR:
7649 code = UNEQ;
7650 break;
7651 case LTGT_EXPR:
7652 code = LTGT;
7653 break;
7655 default:
7656 gcc_unreachable ();
7658 return code;
7661 /* Subroutine of expand_expr. Expand the two operands of a binary
7662 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7663 The value may be stored in TARGET if TARGET is nonzero. The
7664 MODIFIER argument is as documented by expand_expr. */
7666 void
7667 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7668 enum expand_modifier modifier)
7670 if (! safe_from_p (target, exp1, 1))
7671 target = 0;
7672 if (operand_equal_p (exp0, exp1, 0))
7674 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7675 *op1 = copy_rtx (*op0);
7677 else
7679 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7680 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7685 /* Return a MEM that contains constant EXP. DEFER is as for
7686 output_constant_def and MODIFIER is as for expand_expr. */
7688 static rtx
7689 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7691 rtx mem;
7693 mem = output_constant_def (exp, defer);
7694 if (modifier != EXPAND_INITIALIZER)
7695 mem = use_anchored_address (mem);
7696 return mem;
7699 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7700 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7702 static rtx
7703 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7704 enum expand_modifier modifier, addr_space_t as)
7706 rtx result, subtarget;
7707 tree inner, offset;
7708 HOST_WIDE_INT bitsize, bitpos;
7709 int unsignedp, reversep, volatilep = 0;
7710 machine_mode mode1;
7712 /* If we are taking the address of a constant and are at the top level,
7713 we have to use output_constant_def since we can't call force_const_mem
7714 at top level. */
7715 /* ??? This should be considered a front-end bug. We should not be
7716 generating ADDR_EXPR of something that isn't an LVALUE. The only
7717 exception here is STRING_CST. */
7718 if (CONSTANT_CLASS_P (exp))
7720 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7721 if (modifier < EXPAND_SUM)
7722 result = force_operand (result, target);
7723 return result;
7726 /* Everything must be something allowed by is_gimple_addressable. */
7727 switch (TREE_CODE (exp))
7729 case INDIRECT_REF:
7730 /* This case will happen via recursion for &a->b. */
7731 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7733 case MEM_REF:
7735 tree tem = TREE_OPERAND (exp, 0);
7736 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7737 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7738 return expand_expr (tem, target, tmode, modifier);
7741 case CONST_DECL:
7742 /* Expand the initializer like constants above. */
7743 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7744 0, modifier), 0);
7745 if (modifier < EXPAND_SUM)
7746 result = force_operand (result, target);
7747 return result;
7749 case REALPART_EXPR:
7750 /* The real part of the complex number is always first, therefore
7751 the address is the same as the address of the parent object. */
7752 offset = 0;
7753 bitpos = 0;
7754 inner = TREE_OPERAND (exp, 0);
7755 break;
7757 case IMAGPART_EXPR:
7758 /* The imaginary part of the complex number is always second.
7759 The expression is therefore always offset by the size of the
7760 scalar type. */
7761 offset = 0;
7762 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7763 inner = TREE_OPERAND (exp, 0);
7764 break;
7766 case COMPOUND_LITERAL_EXPR:
7767 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7768 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7769 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7770 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7771 the initializers aren't gimplified. */
7772 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7773 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7774 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7775 target, tmode, modifier, as);
7776 /* FALLTHRU */
7777 default:
7778 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7779 expand_expr, as that can have various side effects; LABEL_DECLs for
7780 example, may not have their DECL_RTL set yet. Expand the rtl of
7781 CONSTRUCTORs too, which should yield a memory reference for the
7782 constructor's contents. Assume language specific tree nodes can
7783 be expanded in some interesting way. */
7784 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7785 if (DECL_P (exp)
7786 || TREE_CODE (exp) == CONSTRUCTOR
7787 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7789 result = expand_expr (exp, target, tmode,
7790 modifier == EXPAND_INITIALIZER
7791 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7793 /* If the DECL isn't in memory, then the DECL wasn't properly
7794 marked TREE_ADDRESSABLE, which will be either a front-end
7795 or a tree optimizer bug. */
7797 gcc_assert (MEM_P (result));
7798 result = XEXP (result, 0);
7800 /* ??? Is this needed anymore? */
7801 if (DECL_P (exp))
7802 TREE_USED (exp) = 1;
7804 if (modifier != EXPAND_INITIALIZER
7805 && modifier != EXPAND_CONST_ADDRESS
7806 && modifier != EXPAND_SUM)
7807 result = force_operand (result, target);
7808 return result;
7811 /* Pass FALSE as the last argument to get_inner_reference although
7812 we are expanding to RTL. The rationale is that we know how to
7813 handle "aligning nodes" here: we can just bypass them because
7814 they won't change the final object whose address will be returned
7815 (they actually exist only for that purpose). */
7816 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7817 &unsignedp, &reversep, &volatilep);
7818 break;
7821 /* We must have made progress. */
7822 gcc_assert (inner != exp);
7824 subtarget = offset || bitpos ? NULL_RTX : target;
7825 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7826 inner alignment, force the inner to be sufficiently aligned. */
7827 if (CONSTANT_CLASS_P (inner)
7828 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7830 inner = copy_node (inner);
7831 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7832 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7833 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7835 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7837 if (offset)
7839 rtx tmp;
7841 if (modifier != EXPAND_NORMAL)
7842 result = force_operand (result, NULL);
7843 tmp = expand_expr (offset, NULL_RTX, tmode,
7844 modifier == EXPAND_INITIALIZER
7845 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7847 /* expand_expr is allowed to return an object in a mode other
7848 than TMODE. If it did, we need to convert. */
7849 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7850 tmp = convert_modes (tmode, GET_MODE (tmp),
7851 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7852 result = convert_memory_address_addr_space (tmode, result, as);
7853 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7855 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7856 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7857 else
7859 subtarget = bitpos ? NULL_RTX : target;
7860 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7861 1, OPTAB_LIB_WIDEN);
7865 if (bitpos)
7867 /* Someone beforehand should have rejected taking the address
7868 of such an object. */
7869 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7871 result = convert_memory_address_addr_space (tmode, result, as);
7872 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7873 if (modifier < EXPAND_SUM)
7874 result = force_operand (result, target);
7877 return result;
7880 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7881 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7883 static rtx
7884 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7885 enum expand_modifier modifier)
7887 addr_space_t as = ADDR_SPACE_GENERIC;
7888 machine_mode address_mode = Pmode;
7889 machine_mode pointer_mode = ptr_mode;
7890 machine_mode rmode;
7891 rtx result;
7893 /* Target mode of VOIDmode says "whatever's natural". */
7894 if (tmode == VOIDmode)
7895 tmode = TYPE_MODE (TREE_TYPE (exp));
7897 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7899 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7900 address_mode = targetm.addr_space.address_mode (as);
7901 pointer_mode = targetm.addr_space.pointer_mode (as);
7904 /* We can get called with some Weird Things if the user does silliness
7905 like "(short) &a". In that case, convert_memory_address won't do
7906 the right thing, so ignore the given target mode. */
7907 if (tmode != address_mode && tmode != pointer_mode)
7908 tmode = address_mode;
7910 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7911 tmode, modifier, as);
7913 /* Despite expand_expr claims concerning ignoring TMODE when not
7914 strictly convenient, stuff breaks if we don't honor it. Note
7915 that combined with the above, we only do this for pointer modes. */
7916 rmode = GET_MODE (result);
7917 if (rmode == VOIDmode)
7918 rmode = tmode;
7919 if (rmode != tmode)
7920 result = convert_memory_address_addr_space (tmode, result, as);
7922 return result;
7925 /* Generate code for computing CONSTRUCTOR EXP.
7926 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7927 is TRUE, instead of creating a temporary variable in memory
7928 NULL is returned and the caller needs to handle it differently. */
7930 static rtx
7931 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7932 bool avoid_temp_mem)
7934 tree type = TREE_TYPE (exp);
7935 machine_mode mode = TYPE_MODE (type);
7937 /* Try to avoid creating a temporary at all. This is possible
7938 if all of the initializer is zero.
7939 FIXME: try to handle all [0..255] initializers we can handle
7940 with memset. */
7941 if (TREE_STATIC (exp)
7942 && !TREE_ADDRESSABLE (exp)
7943 && target != 0 && mode == BLKmode
7944 && all_zeros_p (exp))
7946 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7947 return target;
7950 /* All elts simple constants => refer to a constant in memory. But
7951 if this is a non-BLKmode mode, let it store a field at a time
7952 since that should make a CONST_INT, CONST_WIDE_INT or
7953 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7954 use, it is best to store directly into the target unless the type
7955 is large enough that memcpy will be used. If we are making an
7956 initializer and all operands are constant, put it in memory as
7957 well.
7959 FIXME: Avoid trying to fill vector constructors piece-meal.
7960 Output them with output_constant_def below unless we're sure
7961 they're zeros. This should go away when vector initializers
7962 are treated like VECTOR_CST instead of arrays. */
7963 if ((TREE_STATIC (exp)
7964 && ((mode == BLKmode
7965 && ! (target != 0 && safe_from_p (target, exp, 1)))
7966 || TREE_ADDRESSABLE (exp)
7967 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7968 && (! can_move_by_pieces
7969 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7970 TYPE_ALIGN (type)))
7971 && ! mostly_zeros_p (exp))))
7972 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7973 && TREE_CONSTANT (exp)))
7975 rtx constructor;
7977 if (avoid_temp_mem)
7978 return NULL_RTX;
7980 constructor = expand_expr_constant (exp, 1, modifier);
7982 if (modifier != EXPAND_CONST_ADDRESS
7983 && modifier != EXPAND_INITIALIZER
7984 && modifier != EXPAND_SUM)
7985 constructor = validize_mem (constructor);
7987 return constructor;
7990 /* Handle calls that pass values in multiple non-contiguous
7991 locations. The Irix 6 ABI has examples of this. */
7992 if (target == 0 || ! safe_from_p (target, exp, 1)
7993 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7995 if (avoid_temp_mem)
7996 return NULL_RTX;
7998 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8001 store_constructor (exp, target, 0, int_expr_size (exp), false);
8002 return target;
8006 /* expand_expr: generate code for computing expression EXP.
8007 An rtx for the computed value is returned. The value is never null.
8008 In the case of a void EXP, const0_rtx is returned.
8010 The value may be stored in TARGET if TARGET is nonzero.
8011 TARGET is just a suggestion; callers must assume that
8012 the rtx returned may not be the same as TARGET.
8014 If TARGET is CONST0_RTX, it means that the value will be ignored.
8016 If TMODE is not VOIDmode, it suggests generating the
8017 result in mode TMODE. But this is done only when convenient.
8018 Otherwise, TMODE is ignored and the value generated in its natural mode.
8019 TMODE is just a suggestion; callers must assume that
8020 the rtx returned may not have mode TMODE.
8022 Note that TARGET may have neither TMODE nor MODE. In that case, it
8023 probably will not be used.
8025 If MODIFIER is EXPAND_SUM then when EXP is an addition
8026 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8027 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8028 products as above, or REG or MEM, or constant.
8029 Ordinarily in such cases we would output mul or add instructions
8030 and then return a pseudo reg containing the sum.
8032 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8033 it also marks a label as absolutely required (it can't be dead).
8034 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8035 This is used for outputting expressions used in initializers.
8037 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8038 with a constant address even if that address is not normally legitimate.
8039 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8041 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8042 a call parameter. Such targets require special care as we haven't yet
8043 marked TARGET so that it's safe from being trashed by libcalls. We
8044 don't want to use TARGET for anything but the final result;
8045 Intermediate values must go elsewhere. Additionally, calls to
8046 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8048 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8049 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8050 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8051 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8052 recursively.
8054 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8055 In this case, we don't adjust a returned MEM rtx that wouldn't be
8056 sufficiently aligned for its mode; instead, it's up to the caller
8057 to deal with it afterwards. This is used to make sure that unaligned
8058 base objects for which out-of-bounds accesses are supported, for
8059 example record types with trailing arrays, aren't realigned behind
8060 the back of the caller.
8061 The normal operating mode is to pass FALSE for this parameter. */
8064 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8065 enum expand_modifier modifier, rtx *alt_rtl,
8066 bool inner_reference_p)
8068 rtx ret;
8070 /* Handle ERROR_MARK before anybody tries to access its type. */
8071 if (TREE_CODE (exp) == ERROR_MARK
8072 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8074 ret = CONST0_RTX (tmode);
8075 return ret ? ret : const0_rtx;
8078 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8079 inner_reference_p);
8080 return ret;
8083 /* Try to expand the conditional expression which is represented by
8084 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8085 return the rtl reg which represents the result. Otherwise return
8086 NULL_RTX. */
8088 static rtx
8089 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8090 tree treeop1 ATTRIBUTE_UNUSED,
8091 tree treeop2 ATTRIBUTE_UNUSED)
8093 rtx insn;
8094 rtx op00, op01, op1, op2;
8095 enum rtx_code comparison_code;
8096 machine_mode comparison_mode;
8097 gimple *srcstmt;
8098 rtx temp;
8099 tree type = TREE_TYPE (treeop1);
8100 int unsignedp = TYPE_UNSIGNED (type);
8101 machine_mode mode = TYPE_MODE (type);
8102 machine_mode orig_mode = mode;
8103 static bool expanding_cond_expr_using_cmove = false;
8105 /* Conditional move expansion can end up TERing two operands which,
8106 when recursively hitting conditional expressions can result in
8107 exponential behavior if the cmove expansion ultimatively fails.
8108 It's hardly profitable to TER a cmove into a cmove so avoid doing
8109 that by failing early if we end up recursing. */
8110 if (expanding_cond_expr_using_cmove)
8111 return NULL_RTX;
8113 /* If we cannot do a conditional move on the mode, try doing it
8114 with the promoted mode. */
8115 if (!can_conditionally_move_p (mode))
8117 mode = promote_mode (type, mode, &unsignedp);
8118 if (!can_conditionally_move_p (mode))
8119 return NULL_RTX;
8120 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8122 else
8123 temp = assign_temp (type, 0, 1);
8125 expanding_cond_expr_using_cmove = true;
8126 start_sequence ();
8127 expand_operands (treeop1, treeop2,
8128 temp, &op1, &op2, EXPAND_NORMAL);
8130 if (TREE_CODE (treeop0) == SSA_NAME
8131 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8133 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8134 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8135 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8136 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8137 comparison_mode = TYPE_MODE (type);
8138 unsignedp = TYPE_UNSIGNED (type);
8139 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8141 else if (COMPARISON_CLASS_P (treeop0))
8143 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8144 enum tree_code cmpcode = TREE_CODE (treeop0);
8145 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8146 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8147 unsignedp = TYPE_UNSIGNED (type);
8148 comparison_mode = TYPE_MODE (type);
8149 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8151 else
8153 op00 = expand_normal (treeop0);
8154 op01 = const0_rtx;
8155 comparison_code = NE;
8156 comparison_mode = GET_MODE (op00);
8157 if (comparison_mode == VOIDmode)
8158 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8160 expanding_cond_expr_using_cmove = false;
8162 if (GET_MODE (op1) != mode)
8163 op1 = gen_lowpart (mode, op1);
8165 if (GET_MODE (op2) != mode)
8166 op2 = gen_lowpart (mode, op2);
8168 /* Try to emit the conditional move. */
8169 insn = emit_conditional_move (temp, comparison_code,
8170 op00, op01, comparison_mode,
8171 op1, op2, mode,
8172 unsignedp);
8174 /* If we could do the conditional move, emit the sequence,
8175 and return. */
8176 if (insn)
8178 rtx_insn *seq = get_insns ();
8179 end_sequence ();
8180 emit_insn (seq);
8181 return convert_modes (orig_mode, mode, temp, 0);
8184 /* Otherwise discard the sequence and fall back to code with
8185 branches. */
8186 end_sequence ();
8187 return NULL_RTX;
8191 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8192 enum expand_modifier modifier)
8194 rtx op0, op1, op2, temp;
8195 rtx_code_label *lab;
8196 tree type;
8197 int unsignedp;
8198 machine_mode mode;
8199 enum tree_code code = ops->code;
8200 optab this_optab;
8201 rtx subtarget, original_target;
8202 int ignore;
8203 bool reduce_bit_field;
8204 location_t loc = ops->location;
8205 tree treeop0, treeop1, treeop2;
8206 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8207 ? reduce_to_bit_field_precision ((expr), \
8208 target, \
8209 type) \
8210 : (expr))
8212 type = ops->type;
8213 mode = TYPE_MODE (type);
8214 unsignedp = TYPE_UNSIGNED (type);
8216 treeop0 = ops->op0;
8217 treeop1 = ops->op1;
8218 treeop2 = ops->op2;
8220 /* We should be called only on simple (binary or unary) expressions,
8221 exactly those that are valid in gimple expressions that aren't
8222 GIMPLE_SINGLE_RHS (or invalid). */
8223 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8224 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8225 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8227 ignore = (target == const0_rtx
8228 || ((CONVERT_EXPR_CODE_P (code)
8229 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8230 && TREE_CODE (type) == VOID_TYPE));
8232 /* We should be called only if we need the result. */
8233 gcc_assert (!ignore);
8235 /* An operation in what may be a bit-field type needs the
8236 result to be reduced to the precision of the bit-field type,
8237 which is narrower than that of the type's mode. */
8238 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8239 && !type_has_mode_precision_p (type));
8241 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8242 target = 0;
8244 /* Use subtarget as the target for operand 0 of a binary operation. */
8245 subtarget = get_subtarget (target);
8246 original_target = target;
8248 switch (code)
8250 case NON_LVALUE_EXPR:
8251 case PAREN_EXPR:
8252 CASE_CONVERT:
8253 if (treeop0 == error_mark_node)
8254 return const0_rtx;
8256 if (TREE_CODE (type) == UNION_TYPE)
8258 tree valtype = TREE_TYPE (treeop0);
8260 /* If both input and output are BLKmode, this conversion isn't doing
8261 anything except possibly changing memory attribute. */
8262 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8264 rtx result = expand_expr (treeop0, target, tmode,
8265 modifier);
8267 result = copy_rtx (result);
8268 set_mem_attributes (result, type, 0);
8269 return result;
8272 if (target == 0)
8274 if (TYPE_MODE (type) != BLKmode)
8275 target = gen_reg_rtx (TYPE_MODE (type));
8276 else
8277 target = assign_temp (type, 1, 1);
8280 if (MEM_P (target))
8281 /* Store data into beginning of memory target. */
8282 store_expr (treeop0,
8283 adjust_address (target, TYPE_MODE (valtype), 0),
8284 modifier == EXPAND_STACK_PARM,
8285 false, TYPE_REVERSE_STORAGE_ORDER (type));
8287 else
8289 gcc_assert (REG_P (target)
8290 && !TYPE_REVERSE_STORAGE_ORDER (type));
8292 /* Store this field into a union of the proper type. */
8293 store_field (target,
8294 MIN ((int_size_in_bytes (TREE_TYPE
8295 (treeop0))
8296 * BITS_PER_UNIT),
8297 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8298 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8299 false, false);
8302 /* Return the entire union. */
8303 return target;
8306 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8308 op0 = expand_expr (treeop0, target, VOIDmode,
8309 modifier);
8311 /* If the signedness of the conversion differs and OP0 is
8312 a promoted SUBREG, clear that indication since we now
8313 have to do the proper extension. */
8314 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8315 && GET_CODE (op0) == SUBREG)
8316 SUBREG_PROMOTED_VAR_P (op0) = 0;
8318 return REDUCE_BIT_FIELD (op0);
8321 op0 = expand_expr (treeop0, NULL_RTX, mode,
8322 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8323 if (GET_MODE (op0) == mode)
8326 /* If OP0 is a constant, just convert it into the proper mode. */
8327 else if (CONSTANT_P (op0))
8329 tree inner_type = TREE_TYPE (treeop0);
8330 machine_mode inner_mode = GET_MODE (op0);
8332 if (inner_mode == VOIDmode)
8333 inner_mode = TYPE_MODE (inner_type);
8335 if (modifier == EXPAND_INITIALIZER)
8336 op0 = lowpart_subreg (mode, op0, inner_mode);
8337 else
8338 op0= convert_modes (mode, inner_mode, op0,
8339 TYPE_UNSIGNED (inner_type));
8342 else if (modifier == EXPAND_INITIALIZER)
8343 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8344 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8346 else if (target == 0)
8347 op0 = convert_to_mode (mode, op0,
8348 TYPE_UNSIGNED (TREE_TYPE
8349 (treeop0)));
8350 else
8352 convert_move (target, op0,
8353 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8354 op0 = target;
8357 return REDUCE_BIT_FIELD (op0);
8359 case ADDR_SPACE_CONVERT_EXPR:
8361 tree treeop0_type = TREE_TYPE (treeop0);
8363 gcc_assert (POINTER_TYPE_P (type));
8364 gcc_assert (POINTER_TYPE_P (treeop0_type));
8366 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8367 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8369 /* Conversions between pointers to the same address space should
8370 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8371 gcc_assert (as_to != as_from);
8373 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8375 /* Ask target code to handle conversion between pointers
8376 to overlapping address spaces. */
8377 if (targetm.addr_space.subset_p (as_to, as_from)
8378 || targetm.addr_space.subset_p (as_from, as_to))
8380 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8382 else
8384 /* For disjoint address spaces, converting anything but a null
8385 pointer invokes undefined behavior. We truncate or extend the
8386 value as if we'd converted via integers, which handles 0 as
8387 required, and all others as the programmer likely expects. */
8388 #ifndef POINTERS_EXTEND_UNSIGNED
8389 const int POINTERS_EXTEND_UNSIGNED = 1;
8390 #endif
8391 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8392 op0, POINTERS_EXTEND_UNSIGNED);
8394 gcc_assert (op0);
8395 return op0;
8398 case POINTER_PLUS_EXPR:
8399 /* Even though the sizetype mode and the pointer's mode can be different
8400 expand is able to handle this correctly and get the correct result out
8401 of the PLUS_EXPR code. */
8402 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8403 if sizetype precision is smaller than pointer precision. */
8404 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8405 treeop1 = fold_convert_loc (loc, type,
8406 fold_convert_loc (loc, ssizetype,
8407 treeop1));
8408 /* If sizetype precision is larger than pointer precision, truncate the
8409 offset to have matching modes. */
8410 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8411 treeop1 = fold_convert_loc (loc, type, treeop1);
8412 /* FALLTHRU */
8414 case PLUS_EXPR:
8415 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8416 something else, make sure we add the register to the constant and
8417 then to the other thing. This case can occur during strength
8418 reduction and doing it this way will produce better code if the
8419 frame pointer or argument pointer is eliminated.
8421 fold-const.c will ensure that the constant is always in the inner
8422 PLUS_EXPR, so the only case we need to do anything about is if
8423 sp, ap, or fp is our second argument, in which case we must swap
8424 the innermost first argument and our second argument. */
8426 if (TREE_CODE (treeop0) == PLUS_EXPR
8427 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8428 && VAR_P (treeop1)
8429 && (DECL_RTL (treeop1) == frame_pointer_rtx
8430 || DECL_RTL (treeop1) == stack_pointer_rtx
8431 || DECL_RTL (treeop1) == arg_pointer_rtx))
8433 gcc_unreachable ();
8436 /* If the result is to be ptr_mode and we are adding an integer to
8437 something, we might be forming a constant. So try to use
8438 plus_constant. If it produces a sum and we can't accept it,
8439 use force_operand. This allows P = &ARR[const] to generate
8440 efficient code on machines where a SYMBOL_REF is not a valid
8441 address.
8443 If this is an EXPAND_SUM call, always return the sum. */
8444 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8445 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8447 if (modifier == EXPAND_STACK_PARM)
8448 target = 0;
8449 if (TREE_CODE (treeop0) == INTEGER_CST
8450 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8451 && TREE_CONSTANT (treeop1))
8453 rtx constant_part;
8454 HOST_WIDE_INT wc;
8455 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8457 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8458 EXPAND_SUM);
8459 /* Use wi::shwi to ensure that the constant is
8460 truncated according to the mode of OP1, then sign extended
8461 to a HOST_WIDE_INT. Using the constant directly can result
8462 in non-canonical RTL in a 64x32 cross compile. */
8463 wc = TREE_INT_CST_LOW (treeop0);
8464 constant_part =
8465 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8466 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8467 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8468 op1 = force_operand (op1, target);
8469 return REDUCE_BIT_FIELD (op1);
8472 else if (TREE_CODE (treeop1) == INTEGER_CST
8473 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8474 && TREE_CONSTANT (treeop0))
8476 rtx constant_part;
8477 HOST_WIDE_INT wc;
8478 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8480 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8481 (modifier == EXPAND_INITIALIZER
8482 ? EXPAND_INITIALIZER : EXPAND_SUM));
8483 if (! CONSTANT_P (op0))
8485 op1 = expand_expr (treeop1, NULL_RTX,
8486 VOIDmode, modifier);
8487 /* Return a PLUS if modifier says it's OK. */
8488 if (modifier == EXPAND_SUM
8489 || modifier == EXPAND_INITIALIZER)
8490 return simplify_gen_binary (PLUS, mode, op0, op1);
8491 goto binop2;
8493 /* Use wi::shwi to ensure that the constant is
8494 truncated according to the mode of OP1, then sign extended
8495 to a HOST_WIDE_INT. Using the constant directly can result
8496 in non-canonical RTL in a 64x32 cross compile. */
8497 wc = TREE_INT_CST_LOW (treeop1);
8498 constant_part
8499 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8500 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8501 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8502 op0 = force_operand (op0, target);
8503 return REDUCE_BIT_FIELD (op0);
8507 /* Use TER to expand pointer addition of a negated value
8508 as pointer subtraction. */
8509 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8510 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8511 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8512 && TREE_CODE (treeop1) == SSA_NAME
8513 && TYPE_MODE (TREE_TYPE (treeop0))
8514 == TYPE_MODE (TREE_TYPE (treeop1)))
8516 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8517 if (def)
8519 treeop1 = gimple_assign_rhs1 (def);
8520 code = MINUS_EXPR;
8521 goto do_minus;
8525 /* No sense saving up arithmetic to be done
8526 if it's all in the wrong mode to form part of an address.
8527 And force_operand won't know whether to sign-extend or
8528 zero-extend. */
8529 if (modifier != EXPAND_INITIALIZER
8530 && (modifier != EXPAND_SUM || mode != ptr_mode))
8532 expand_operands (treeop0, treeop1,
8533 subtarget, &op0, &op1, modifier);
8534 if (op0 == const0_rtx)
8535 return op1;
8536 if (op1 == const0_rtx)
8537 return op0;
8538 goto binop2;
8541 expand_operands (treeop0, treeop1,
8542 subtarget, &op0, &op1, modifier);
8543 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8545 case MINUS_EXPR:
8546 do_minus:
8547 /* For initializers, we are allowed to return a MINUS of two
8548 symbolic constants. Here we handle all cases when both operands
8549 are constant. */
8550 /* Handle difference of two symbolic constants,
8551 for the sake of an initializer. */
8552 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8553 && really_constant_p (treeop0)
8554 && really_constant_p (treeop1))
8556 expand_operands (treeop0, treeop1,
8557 NULL_RTX, &op0, &op1, modifier);
8559 /* If the last operand is a CONST_INT, use plus_constant of
8560 the negated constant. Else make the MINUS. */
8561 if (CONST_INT_P (op1))
8562 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8563 -INTVAL (op1)));
8564 else
8565 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8568 /* No sense saving up arithmetic to be done
8569 if it's all in the wrong mode to form part of an address.
8570 And force_operand won't know whether to sign-extend or
8571 zero-extend. */
8572 if (modifier != EXPAND_INITIALIZER
8573 && (modifier != EXPAND_SUM || mode != ptr_mode))
8574 goto binop;
8576 expand_operands (treeop0, treeop1,
8577 subtarget, &op0, &op1, modifier);
8579 /* Convert A - const to A + (-const). */
8580 if (CONST_INT_P (op1))
8582 op1 = negate_rtx (mode, op1);
8583 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8586 goto binop2;
8588 case WIDEN_MULT_PLUS_EXPR:
8589 case WIDEN_MULT_MINUS_EXPR:
8590 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8591 op2 = expand_normal (treeop2);
8592 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8593 target, unsignedp);
8594 return target;
8596 case WIDEN_MULT_EXPR:
8597 /* If first operand is constant, swap them.
8598 Thus the following special case checks need only
8599 check the second operand. */
8600 if (TREE_CODE (treeop0) == INTEGER_CST)
8601 std::swap (treeop0, treeop1);
8603 /* First, check if we have a multiplication of one signed and one
8604 unsigned operand. */
8605 if (TREE_CODE (treeop1) != INTEGER_CST
8606 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8607 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8609 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8610 this_optab = usmul_widen_optab;
8611 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8612 != CODE_FOR_nothing)
8614 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8615 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8616 EXPAND_NORMAL);
8617 else
8618 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8619 EXPAND_NORMAL);
8620 /* op0 and op1 might still be constant, despite the above
8621 != INTEGER_CST check. Handle it. */
8622 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8624 op0 = convert_modes (innermode, mode, op0, true);
8625 op1 = convert_modes (innermode, mode, op1, false);
8626 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8627 target, unsignedp));
8629 goto binop3;
8632 /* Check for a multiplication with matching signedness. */
8633 else if ((TREE_CODE (treeop1) == INTEGER_CST
8634 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8635 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8636 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8638 tree op0type = TREE_TYPE (treeop0);
8639 machine_mode innermode = TYPE_MODE (op0type);
8640 bool zextend_p = TYPE_UNSIGNED (op0type);
8641 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8642 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8644 if (TREE_CODE (treeop0) != INTEGER_CST)
8646 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8647 != CODE_FOR_nothing)
8649 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8650 EXPAND_NORMAL);
8651 /* op0 and op1 might still be constant, despite the above
8652 != INTEGER_CST check. Handle it. */
8653 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8655 widen_mult_const:
8656 op0 = convert_modes (innermode, mode, op0, zextend_p);
8658 = convert_modes (innermode, mode, op1,
8659 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8660 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8661 target,
8662 unsignedp));
8664 temp = expand_widening_mult (mode, op0, op1, target,
8665 unsignedp, this_optab);
8666 return REDUCE_BIT_FIELD (temp);
8668 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8669 != CODE_FOR_nothing
8670 && innermode == word_mode)
8672 rtx htem, hipart;
8673 op0 = expand_normal (treeop0);
8674 if (TREE_CODE (treeop1) == INTEGER_CST)
8675 op1 = convert_modes (innermode, mode,
8676 expand_normal (treeop1),
8677 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8678 else
8679 op1 = expand_normal (treeop1);
8680 /* op0 and op1 might still be constant, despite the above
8681 != INTEGER_CST check. Handle it. */
8682 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8683 goto widen_mult_const;
8684 temp = expand_binop (mode, other_optab, op0, op1, target,
8685 unsignedp, OPTAB_LIB_WIDEN);
8686 hipart = gen_highpart (innermode, temp);
8687 htem = expand_mult_highpart_adjust (innermode, hipart,
8688 op0, op1, hipart,
8689 zextend_p);
8690 if (htem != hipart)
8691 emit_move_insn (hipart, htem);
8692 return REDUCE_BIT_FIELD (temp);
8696 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8697 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8698 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8699 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8701 case FMA_EXPR:
8703 optab opt = fma_optab;
8704 gimple *def0, *def2;
8706 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8707 call. */
8708 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8710 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8711 tree call_expr;
8713 gcc_assert (fn != NULL_TREE);
8714 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8715 return expand_builtin (call_expr, target, subtarget, mode, false);
8718 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8719 /* The multiplication is commutative - look at its 2nd operand
8720 if the first isn't fed by a negate. */
8721 if (!def0)
8723 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8724 /* Swap operands if the 2nd operand is fed by a negate. */
8725 if (def0)
8726 std::swap (treeop0, treeop1);
8728 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8730 op0 = op2 = NULL;
8732 if (def0 && def2
8733 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8735 opt = fnms_optab;
8736 op0 = expand_normal (gimple_assign_rhs1 (def0));
8737 op2 = expand_normal (gimple_assign_rhs1 (def2));
8739 else if (def0
8740 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8742 opt = fnma_optab;
8743 op0 = expand_normal (gimple_assign_rhs1 (def0));
8745 else if (def2
8746 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8748 opt = fms_optab;
8749 op2 = expand_normal (gimple_assign_rhs1 (def2));
8752 if (op0 == NULL)
8753 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8754 if (op2 == NULL)
8755 op2 = expand_normal (treeop2);
8756 op1 = expand_normal (treeop1);
8758 return expand_ternary_op (TYPE_MODE (type), opt,
8759 op0, op1, op2, target, 0);
8762 case MULT_EXPR:
8763 /* If this is a fixed-point operation, then we cannot use the code
8764 below because "expand_mult" doesn't support sat/no-sat fixed-point
8765 multiplications. */
8766 if (ALL_FIXED_POINT_MODE_P (mode))
8767 goto binop;
8769 /* If first operand is constant, swap them.
8770 Thus the following special case checks need only
8771 check the second operand. */
8772 if (TREE_CODE (treeop0) == INTEGER_CST)
8773 std::swap (treeop0, treeop1);
8775 /* Attempt to return something suitable for generating an
8776 indexed address, for machines that support that. */
8778 if (modifier == EXPAND_SUM && mode == ptr_mode
8779 && tree_fits_shwi_p (treeop1))
8781 tree exp1 = treeop1;
8783 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8784 EXPAND_SUM);
8786 if (!REG_P (op0))
8787 op0 = force_operand (op0, NULL_RTX);
8788 if (!REG_P (op0))
8789 op0 = copy_to_mode_reg (mode, op0);
8791 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8792 gen_int_mode (tree_to_shwi (exp1),
8793 TYPE_MODE (TREE_TYPE (exp1)))));
8796 if (modifier == EXPAND_STACK_PARM)
8797 target = 0;
8799 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8800 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8802 case TRUNC_MOD_EXPR:
8803 case FLOOR_MOD_EXPR:
8804 case CEIL_MOD_EXPR:
8805 case ROUND_MOD_EXPR:
8807 case TRUNC_DIV_EXPR:
8808 case FLOOR_DIV_EXPR:
8809 case CEIL_DIV_EXPR:
8810 case ROUND_DIV_EXPR:
8811 case EXACT_DIV_EXPR:
8813 /* If this is a fixed-point operation, then we cannot use the code
8814 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8815 divisions. */
8816 if (ALL_FIXED_POINT_MODE_P (mode))
8817 goto binop;
8819 if (modifier == EXPAND_STACK_PARM)
8820 target = 0;
8821 /* Possible optimization: compute the dividend with EXPAND_SUM
8822 then if the divisor is constant can optimize the case
8823 where some terms of the dividend have coeffs divisible by it. */
8824 expand_operands (treeop0, treeop1,
8825 subtarget, &op0, &op1, EXPAND_NORMAL);
8826 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8827 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8828 if (SCALAR_INT_MODE_P (mode)
8829 && optimize >= 2
8830 && get_range_pos_neg (treeop0) == 1
8831 && get_range_pos_neg (treeop1) == 1)
8833 /* If both arguments are known to be positive when interpreted
8834 as signed, we can expand it as both signed and unsigned
8835 division or modulo. Choose the cheaper sequence in that case. */
8836 bool speed_p = optimize_insn_for_speed_p ();
8837 do_pending_stack_adjust ();
8838 start_sequence ();
8839 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8840 rtx_insn *uns_insns = get_insns ();
8841 end_sequence ();
8842 start_sequence ();
8843 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8844 rtx_insn *sgn_insns = get_insns ();
8845 end_sequence ();
8846 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8847 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8849 /* If costs are the same then use as tie breaker the other
8850 other factor. */
8851 if (uns_cost == sgn_cost)
8853 uns_cost = seq_cost (uns_insns, !speed_p);
8854 sgn_cost = seq_cost (sgn_insns, !speed_p);
8857 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8859 emit_insn (uns_insns);
8860 return uns_ret;
8862 emit_insn (sgn_insns);
8863 return sgn_ret;
8865 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8867 case RDIV_EXPR:
8868 goto binop;
8870 case MULT_HIGHPART_EXPR:
8871 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8872 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8873 gcc_assert (temp);
8874 return temp;
8876 case FIXED_CONVERT_EXPR:
8877 op0 = expand_normal (treeop0);
8878 if (target == 0 || modifier == EXPAND_STACK_PARM)
8879 target = gen_reg_rtx (mode);
8881 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8882 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8883 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8884 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8885 else
8886 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8887 return target;
8889 case FIX_TRUNC_EXPR:
8890 op0 = expand_normal (treeop0);
8891 if (target == 0 || modifier == EXPAND_STACK_PARM)
8892 target = gen_reg_rtx (mode);
8893 expand_fix (target, op0, unsignedp);
8894 return target;
8896 case FLOAT_EXPR:
8897 op0 = expand_normal (treeop0);
8898 if (target == 0 || modifier == EXPAND_STACK_PARM)
8899 target = gen_reg_rtx (mode);
8900 /* expand_float can't figure out what to do if FROM has VOIDmode.
8901 So give it the correct mode. With -O, cse will optimize this. */
8902 if (GET_MODE (op0) == VOIDmode)
8903 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8904 op0);
8905 expand_float (target, op0,
8906 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8907 return target;
8909 case NEGATE_EXPR:
8910 op0 = expand_expr (treeop0, subtarget,
8911 VOIDmode, EXPAND_NORMAL);
8912 if (modifier == EXPAND_STACK_PARM)
8913 target = 0;
8914 temp = expand_unop (mode,
8915 optab_for_tree_code (NEGATE_EXPR, type,
8916 optab_default),
8917 op0, target, 0);
8918 gcc_assert (temp);
8919 return REDUCE_BIT_FIELD (temp);
8921 case ABS_EXPR:
8922 op0 = expand_expr (treeop0, subtarget,
8923 VOIDmode, EXPAND_NORMAL);
8924 if (modifier == EXPAND_STACK_PARM)
8925 target = 0;
8927 /* ABS_EXPR is not valid for complex arguments. */
8928 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8929 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8931 /* Unsigned abs is simply the operand. Testing here means we don't
8932 risk generating incorrect code below. */
8933 if (TYPE_UNSIGNED (type))
8934 return op0;
8936 return expand_abs (mode, op0, target, unsignedp,
8937 safe_from_p (target, treeop0, 1));
8939 case MAX_EXPR:
8940 case MIN_EXPR:
8941 target = original_target;
8942 if (target == 0
8943 || modifier == EXPAND_STACK_PARM
8944 || (MEM_P (target) && MEM_VOLATILE_P (target))
8945 || GET_MODE (target) != mode
8946 || (REG_P (target)
8947 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8948 target = gen_reg_rtx (mode);
8949 expand_operands (treeop0, treeop1,
8950 target, &op0, &op1, EXPAND_NORMAL);
8952 /* First try to do it with a special MIN or MAX instruction.
8953 If that does not win, use a conditional jump to select the proper
8954 value. */
8955 this_optab = optab_for_tree_code (code, type, optab_default);
8956 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8957 OPTAB_WIDEN);
8958 if (temp != 0)
8959 return temp;
8961 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8962 and similarly for MAX <x, y>. */
8963 if (VECTOR_TYPE_P (type))
8965 tree t0 = make_tree (type, op0);
8966 tree t1 = make_tree (type, op1);
8967 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8968 type, t0, t1);
8969 return expand_vec_cond_expr (type, comparison, t0, t1,
8970 original_target);
8973 /* At this point, a MEM target is no longer useful; we will get better
8974 code without it. */
8976 if (! REG_P (target))
8977 target = gen_reg_rtx (mode);
8979 /* If op1 was placed in target, swap op0 and op1. */
8980 if (target != op0 && target == op1)
8981 std::swap (op0, op1);
8983 /* We generate better code and avoid problems with op1 mentioning
8984 target by forcing op1 into a pseudo if it isn't a constant. */
8985 if (! CONSTANT_P (op1))
8986 op1 = force_reg (mode, op1);
8989 enum rtx_code comparison_code;
8990 rtx cmpop1 = op1;
8992 if (code == MAX_EXPR)
8993 comparison_code = unsignedp ? GEU : GE;
8994 else
8995 comparison_code = unsignedp ? LEU : LE;
8997 /* Canonicalize to comparisons against 0. */
8998 if (op1 == const1_rtx)
9000 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9001 or (a != 0 ? a : 1) for unsigned.
9002 For MIN we are safe converting (a <= 1 ? a : 1)
9003 into (a <= 0 ? a : 1) */
9004 cmpop1 = const0_rtx;
9005 if (code == MAX_EXPR)
9006 comparison_code = unsignedp ? NE : GT;
9008 if (op1 == constm1_rtx && !unsignedp)
9010 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9011 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9012 cmpop1 = const0_rtx;
9013 if (code == MIN_EXPR)
9014 comparison_code = LT;
9017 /* Use a conditional move if possible. */
9018 if (can_conditionally_move_p (mode))
9020 rtx insn;
9022 start_sequence ();
9024 /* Try to emit the conditional move. */
9025 insn = emit_conditional_move (target, comparison_code,
9026 op0, cmpop1, mode,
9027 op0, op1, mode,
9028 unsignedp);
9030 /* If we could do the conditional move, emit the sequence,
9031 and return. */
9032 if (insn)
9034 rtx_insn *seq = get_insns ();
9035 end_sequence ();
9036 emit_insn (seq);
9037 return target;
9040 /* Otherwise discard the sequence and fall back to code with
9041 branches. */
9042 end_sequence ();
9045 if (target != op0)
9046 emit_move_insn (target, op0);
9048 lab = gen_label_rtx ();
9049 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9050 unsignedp, mode, NULL_RTX, NULL, lab,
9051 profile_probability::uninitialized ());
9053 emit_move_insn (target, op1);
9054 emit_label (lab);
9055 return target;
9057 case BIT_NOT_EXPR:
9058 op0 = expand_expr (treeop0, subtarget,
9059 VOIDmode, EXPAND_NORMAL);
9060 if (modifier == EXPAND_STACK_PARM)
9061 target = 0;
9062 /* In case we have to reduce the result to bitfield precision
9063 for unsigned bitfield expand this as XOR with a proper constant
9064 instead. */
9065 if (reduce_bit_field && TYPE_UNSIGNED (type))
9067 wide_int mask = wi::mask (TYPE_PRECISION (type),
9068 false, GET_MODE_PRECISION (mode));
9070 temp = expand_binop (mode, xor_optab, op0,
9071 immed_wide_int_const (mask, 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 && mode == GET_MODE_WIDER_MODE (word_mode).else_void ()
9163 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_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 machine_mode rmode = TYPE_MODE
9172 (TREE_TYPE (gimple_assign_rhs1 (def)));
9174 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (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 mode);
9182 bool extend_unsigned
9183 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9184 rtx low = lowpart_subreg (word_mode, op0, mode);
9185 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9186 rtx dest_high = simplify_gen_subreg (word_mode, target,
9187 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, mode))
9212 bool speed_p = optimize_insn_for_speed_p ();
9213 start_sequence ();
9214 rtx ret_old = expand_variable_shift (code, mode, op0,
9215 treeop1, target,
9216 unsignedp);
9218 seq_old = get_insns ();
9219 end_sequence ();
9220 if (seq_cost (seq, speed_p)
9221 >= seq_cost (seq_old, speed_p))
9223 seq = seq_old;
9224 temp = ret_old;
9227 emit_insn (seq);
9232 if (temp == NULL_RTX)
9233 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9234 unsignedp);
9235 if (code == LSHIFT_EXPR)
9236 temp = REDUCE_BIT_FIELD (temp);
9237 return temp;
9240 /* Could determine the answer when only additive constants differ. Also,
9241 the addition of one can be handled by changing the condition. */
9242 case LT_EXPR:
9243 case LE_EXPR:
9244 case GT_EXPR:
9245 case GE_EXPR:
9246 case EQ_EXPR:
9247 case NE_EXPR:
9248 case UNORDERED_EXPR:
9249 case ORDERED_EXPR:
9250 case UNLT_EXPR:
9251 case UNLE_EXPR:
9252 case UNGT_EXPR:
9253 case UNGE_EXPR:
9254 case UNEQ_EXPR:
9255 case LTGT_EXPR:
9257 temp = do_store_flag (ops,
9258 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9259 tmode != VOIDmode ? tmode : mode);
9260 if (temp)
9261 return temp;
9263 /* Use a compare and a jump for BLKmode comparisons, or for function
9264 type comparisons is have_canonicalize_funcptr_for_compare. */
9266 if ((target == 0
9267 || modifier == EXPAND_STACK_PARM
9268 || ! safe_from_p (target, treeop0, 1)
9269 || ! safe_from_p (target, treeop1, 1)
9270 /* Make sure we don't have a hard reg (such as function's return
9271 value) live across basic blocks, if not optimizing. */
9272 || (!optimize && REG_P (target)
9273 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9274 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9276 emit_move_insn (target, const0_rtx);
9278 rtx_code_label *lab1 = gen_label_rtx ();
9279 jumpifnot_1 (code, treeop0, treeop1, lab1,
9280 profile_probability::uninitialized ());
9282 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9283 emit_move_insn (target, constm1_rtx);
9284 else
9285 emit_move_insn (target, const1_rtx);
9287 emit_label (lab1);
9288 return target;
9290 case COMPLEX_EXPR:
9291 /* Get the rtx code of the operands. */
9292 op0 = expand_normal (treeop0);
9293 op1 = expand_normal (treeop1);
9295 if (!target)
9296 target = gen_reg_rtx (TYPE_MODE (type));
9297 else
9298 /* If target overlaps with op1, then either we need to force
9299 op1 into a pseudo (if target also overlaps with op0),
9300 or write the complex parts in reverse order. */
9301 switch (GET_CODE (target))
9303 case CONCAT:
9304 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9306 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9308 complex_expr_force_op1:
9309 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9310 emit_move_insn (temp, op1);
9311 op1 = temp;
9312 break;
9314 complex_expr_swap_order:
9315 /* Move the imaginary (op1) and real (op0) parts to their
9316 location. */
9317 write_complex_part (target, op1, true);
9318 write_complex_part (target, op0, false);
9320 return target;
9322 break;
9323 case MEM:
9324 temp = adjust_address_nv (target,
9325 GET_MODE_INNER (GET_MODE (target)), 0);
9326 if (reg_overlap_mentioned_p (temp, op1))
9328 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9329 temp = adjust_address_nv (target, imode,
9330 GET_MODE_SIZE (imode));
9331 if (reg_overlap_mentioned_p (temp, op0))
9332 goto complex_expr_force_op1;
9333 goto complex_expr_swap_order;
9335 break;
9336 default:
9337 if (reg_overlap_mentioned_p (target, op1))
9339 if (reg_overlap_mentioned_p (target, op0))
9340 goto complex_expr_force_op1;
9341 goto complex_expr_swap_order;
9343 break;
9346 /* Move the real (op0) and imaginary (op1) parts to their location. */
9347 write_complex_part (target, op0, false);
9348 write_complex_part (target, op1, true);
9350 return target;
9352 case WIDEN_SUM_EXPR:
9354 tree oprnd0 = treeop0;
9355 tree oprnd1 = treeop1;
9357 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9358 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9359 target, unsignedp);
9360 return target;
9363 case REDUC_MAX_EXPR:
9364 case REDUC_MIN_EXPR:
9365 case REDUC_PLUS_EXPR:
9367 op0 = expand_normal (treeop0);
9368 this_optab = optab_for_tree_code (code, type, optab_default);
9369 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9371 struct expand_operand ops[2];
9372 enum insn_code icode = optab_handler (this_optab, vec_mode);
9374 create_output_operand (&ops[0], target, mode);
9375 create_input_operand (&ops[1], op0, vec_mode);
9376 expand_insn (icode, 2, ops);
9377 target = ops[0].value;
9378 if (GET_MODE (target) != mode)
9379 return gen_lowpart (tmode, target);
9380 return target;
9383 case VEC_UNPACK_HI_EXPR:
9384 case VEC_UNPACK_LO_EXPR:
9386 op0 = expand_normal (treeop0);
9387 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9388 target, unsignedp);
9389 gcc_assert (temp);
9390 return temp;
9393 case VEC_UNPACK_FLOAT_HI_EXPR:
9394 case VEC_UNPACK_FLOAT_LO_EXPR:
9396 op0 = expand_normal (treeop0);
9397 /* The signedness is determined from input operand. */
9398 temp = expand_widen_pattern_expr
9399 (ops, op0, NULL_RTX, NULL_RTX,
9400 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9402 gcc_assert (temp);
9403 return temp;
9406 case VEC_WIDEN_MULT_HI_EXPR:
9407 case VEC_WIDEN_MULT_LO_EXPR:
9408 case VEC_WIDEN_MULT_EVEN_EXPR:
9409 case VEC_WIDEN_MULT_ODD_EXPR:
9410 case VEC_WIDEN_LSHIFT_HI_EXPR:
9411 case VEC_WIDEN_LSHIFT_LO_EXPR:
9412 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9413 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9414 target, unsignedp);
9415 gcc_assert (target);
9416 return target;
9418 case VEC_PACK_TRUNC_EXPR:
9419 case VEC_PACK_SAT_EXPR:
9420 case VEC_PACK_FIX_TRUNC_EXPR:
9421 mode = TYPE_MODE (TREE_TYPE (treeop0));
9422 goto binop;
9424 case VEC_PERM_EXPR:
9425 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9426 op2 = expand_normal (treeop2);
9428 /* Careful here: if the target doesn't support integral vector modes,
9429 a constant selection vector could wind up smooshed into a normal
9430 integral constant. */
9431 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9433 tree sel_type = TREE_TYPE (treeop2);
9434 machine_mode vmode
9435 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9436 TYPE_VECTOR_SUBPARTS (sel_type));
9437 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9438 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9439 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9441 else
9442 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9444 temp = expand_vec_perm (mode, op0, op1, op2, target);
9445 gcc_assert (temp);
9446 return temp;
9448 case DOT_PROD_EXPR:
9450 tree oprnd0 = treeop0;
9451 tree oprnd1 = treeop1;
9452 tree oprnd2 = treeop2;
9453 rtx op2;
9455 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9456 op2 = expand_normal (oprnd2);
9457 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9458 target, unsignedp);
9459 return target;
9462 case SAD_EXPR:
9464 tree oprnd0 = treeop0;
9465 tree oprnd1 = treeop1;
9466 tree oprnd2 = treeop2;
9467 rtx op2;
9469 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9470 op2 = expand_normal (oprnd2);
9471 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9472 target, unsignedp);
9473 return target;
9476 case REALIGN_LOAD_EXPR:
9478 tree oprnd0 = treeop0;
9479 tree oprnd1 = treeop1;
9480 tree oprnd2 = treeop2;
9481 rtx op2;
9483 this_optab = optab_for_tree_code (code, type, optab_default);
9484 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9485 op2 = expand_normal (oprnd2);
9486 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9487 target, unsignedp);
9488 gcc_assert (temp);
9489 return temp;
9492 case COND_EXPR:
9494 /* A COND_EXPR with its type being VOID_TYPE represents a
9495 conditional jump and is handled in
9496 expand_gimple_cond_expr. */
9497 gcc_assert (!VOID_TYPE_P (type));
9499 /* Note that COND_EXPRs whose type is a structure or union
9500 are required to be constructed to contain assignments of
9501 a temporary variable, so that we can evaluate them here
9502 for side effect only. If type is void, we must do likewise. */
9504 gcc_assert (!TREE_ADDRESSABLE (type)
9505 && !ignore
9506 && TREE_TYPE (treeop1) != void_type_node
9507 && TREE_TYPE (treeop2) != void_type_node);
9509 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9510 if (temp)
9511 return temp;
9513 /* If we are not to produce a result, we have no target. Otherwise,
9514 if a target was specified use it; it will not be used as an
9515 intermediate target unless it is safe. If no target, use a
9516 temporary. */
9518 if (modifier != EXPAND_STACK_PARM
9519 && original_target
9520 && safe_from_p (original_target, treeop0, 1)
9521 && GET_MODE (original_target) == mode
9522 && !MEM_P (original_target))
9523 temp = original_target;
9524 else
9525 temp = assign_temp (type, 0, 1);
9527 do_pending_stack_adjust ();
9528 NO_DEFER_POP;
9529 rtx_code_label *lab0 = gen_label_rtx ();
9530 rtx_code_label *lab1 = gen_label_rtx ();
9531 jumpifnot (treeop0, lab0,
9532 profile_probability::uninitialized ());
9533 store_expr (treeop1, temp,
9534 modifier == EXPAND_STACK_PARM,
9535 false, false);
9537 emit_jump_insn (targetm.gen_jump (lab1));
9538 emit_barrier ();
9539 emit_label (lab0);
9540 store_expr (treeop2, temp,
9541 modifier == EXPAND_STACK_PARM,
9542 false, false);
9544 emit_label (lab1);
9545 OK_DEFER_POP;
9546 return temp;
9549 case VEC_COND_EXPR:
9550 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9551 return target;
9553 case BIT_INSERT_EXPR:
9555 unsigned bitpos = tree_to_uhwi (treeop2);
9556 unsigned bitsize;
9557 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9558 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9559 else
9560 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9561 rtx op0 = expand_normal (treeop0);
9562 rtx op1 = expand_normal (treeop1);
9563 rtx dst = gen_reg_rtx (mode);
9564 emit_move_insn (dst, op0);
9565 store_bit_field (dst, bitsize, bitpos, 0, 0,
9566 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9567 return dst;
9570 default:
9571 gcc_unreachable ();
9574 /* Here to do an ordinary binary operator. */
9575 binop:
9576 expand_operands (treeop0, treeop1,
9577 subtarget, &op0, &op1, EXPAND_NORMAL);
9578 binop2:
9579 this_optab = optab_for_tree_code (code, type, optab_default);
9580 binop3:
9581 if (modifier == EXPAND_STACK_PARM)
9582 target = 0;
9583 temp = expand_binop (mode, this_optab, op0, op1, target,
9584 unsignedp, OPTAB_LIB_WIDEN);
9585 gcc_assert (temp);
9586 /* Bitwise operations do not need bitfield reduction as we expect their
9587 operands being properly truncated. */
9588 if (code == BIT_XOR_EXPR
9589 || code == BIT_AND_EXPR
9590 || code == BIT_IOR_EXPR)
9591 return temp;
9592 return REDUCE_BIT_FIELD (temp);
9594 #undef REDUCE_BIT_FIELD
9597 /* Return TRUE if expression STMT is suitable for replacement.
9598 Never consider memory loads as replaceable, because those don't ever lead
9599 into constant expressions. */
9601 static bool
9602 stmt_is_replaceable_p (gimple *stmt)
9604 if (ssa_is_replaceable_p (stmt))
9606 /* Don't move around loads. */
9607 if (!gimple_assign_single_p (stmt)
9608 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9609 return true;
9611 return false;
9615 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9616 enum expand_modifier modifier, rtx *alt_rtl,
9617 bool inner_reference_p)
9619 rtx op0, op1, temp, decl_rtl;
9620 tree type;
9621 int unsignedp;
9622 machine_mode mode, dmode;
9623 enum tree_code code = TREE_CODE (exp);
9624 rtx subtarget, original_target;
9625 int ignore;
9626 tree context;
9627 bool reduce_bit_field;
9628 location_t loc = EXPR_LOCATION (exp);
9629 struct separate_ops ops;
9630 tree treeop0, treeop1, treeop2;
9631 tree ssa_name = NULL_TREE;
9632 gimple *g;
9634 type = TREE_TYPE (exp);
9635 mode = TYPE_MODE (type);
9636 unsignedp = TYPE_UNSIGNED (type);
9638 treeop0 = treeop1 = treeop2 = NULL_TREE;
9639 if (!VL_EXP_CLASS_P (exp))
9640 switch (TREE_CODE_LENGTH (code))
9642 default:
9643 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9644 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9645 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9646 case 0: break;
9648 ops.code = code;
9649 ops.type = type;
9650 ops.op0 = treeop0;
9651 ops.op1 = treeop1;
9652 ops.op2 = treeop2;
9653 ops.location = loc;
9655 ignore = (target == const0_rtx
9656 || ((CONVERT_EXPR_CODE_P (code)
9657 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9658 && TREE_CODE (type) == VOID_TYPE));
9660 /* An operation in what may be a bit-field type needs the
9661 result to be reduced to the precision of the bit-field type,
9662 which is narrower than that of the type's mode. */
9663 reduce_bit_field = (!ignore
9664 && INTEGRAL_TYPE_P (type)
9665 && !type_has_mode_precision_p (type));
9667 /* If we are going to ignore this result, we need only do something
9668 if there is a side-effect somewhere in the expression. If there
9669 is, short-circuit the most common cases here. Note that we must
9670 not call expand_expr with anything but const0_rtx in case this
9671 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9673 if (ignore)
9675 if (! TREE_SIDE_EFFECTS (exp))
9676 return const0_rtx;
9678 /* Ensure we reference a volatile object even if value is ignored, but
9679 don't do this if all we are doing is taking its address. */
9680 if (TREE_THIS_VOLATILE (exp)
9681 && TREE_CODE (exp) != FUNCTION_DECL
9682 && mode != VOIDmode && mode != BLKmode
9683 && modifier != EXPAND_CONST_ADDRESS)
9685 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9686 if (MEM_P (temp))
9687 copy_to_reg (temp);
9688 return const0_rtx;
9691 if (TREE_CODE_CLASS (code) == tcc_unary
9692 || code == BIT_FIELD_REF
9693 || code == COMPONENT_REF
9694 || code == INDIRECT_REF)
9695 return expand_expr (treeop0, const0_rtx, VOIDmode,
9696 modifier);
9698 else if (TREE_CODE_CLASS (code) == tcc_binary
9699 || TREE_CODE_CLASS (code) == tcc_comparison
9700 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9702 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9703 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9704 return const0_rtx;
9707 target = 0;
9710 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9711 target = 0;
9713 /* Use subtarget as the target for operand 0 of a binary operation. */
9714 subtarget = get_subtarget (target);
9715 original_target = target;
9717 switch (code)
9719 case LABEL_DECL:
9721 tree function = decl_function_context (exp);
9723 temp = label_rtx (exp);
9724 temp = gen_rtx_LABEL_REF (Pmode, temp);
9726 if (function != current_function_decl
9727 && function != 0)
9728 LABEL_REF_NONLOCAL_P (temp) = 1;
9730 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9731 return temp;
9734 case SSA_NAME:
9735 /* ??? ivopts calls expander, without any preparation from
9736 out-of-ssa. So fake instructions as if this was an access to the
9737 base variable. This unnecessarily allocates a pseudo, see how we can
9738 reuse it, if partition base vars have it set already. */
9739 if (!currently_expanding_to_rtl)
9741 tree var = SSA_NAME_VAR (exp);
9742 if (var && DECL_RTL_SET_P (var))
9743 return DECL_RTL (var);
9744 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9745 LAST_VIRTUAL_REGISTER + 1);
9748 g = get_gimple_for_ssa_name (exp);
9749 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9750 if (g == NULL
9751 && modifier == EXPAND_INITIALIZER
9752 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9753 && (optimize || !SSA_NAME_VAR (exp)
9754 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9755 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9756 g = SSA_NAME_DEF_STMT (exp);
9757 if (g)
9759 rtx r;
9760 location_t saved_loc = curr_insn_location ();
9761 location_t loc = gimple_location (g);
9762 if (loc != UNKNOWN_LOCATION)
9763 set_curr_insn_location (loc);
9764 ops.code = gimple_assign_rhs_code (g);
9765 switch (get_gimple_rhs_class (ops.code))
9767 case GIMPLE_TERNARY_RHS:
9768 ops.op2 = gimple_assign_rhs3 (g);
9769 /* Fallthru */
9770 case GIMPLE_BINARY_RHS:
9771 ops.op1 = gimple_assign_rhs2 (g);
9773 /* Try to expand conditonal compare. */
9774 if (targetm.gen_ccmp_first)
9776 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9777 r = expand_ccmp_expr (g, mode);
9778 if (r)
9779 break;
9781 /* Fallthru */
9782 case GIMPLE_UNARY_RHS:
9783 ops.op0 = gimple_assign_rhs1 (g);
9784 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9785 ops.location = loc;
9786 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9787 break;
9788 case GIMPLE_SINGLE_RHS:
9790 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9791 tmode, modifier, alt_rtl,
9792 inner_reference_p);
9793 break;
9795 default:
9796 gcc_unreachable ();
9798 set_curr_insn_location (saved_loc);
9799 if (REG_P (r) && !REG_EXPR (r))
9800 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9801 return r;
9804 ssa_name = exp;
9805 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9806 exp = SSA_NAME_VAR (ssa_name);
9807 goto expand_decl_rtl;
9809 case PARM_DECL:
9810 case VAR_DECL:
9811 /* If a static var's type was incomplete when the decl was written,
9812 but the type is complete now, lay out the decl now. */
9813 if (DECL_SIZE (exp) == 0
9814 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9815 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9816 layout_decl (exp, 0);
9818 /* fall through */
9820 case FUNCTION_DECL:
9821 case RESULT_DECL:
9822 decl_rtl = DECL_RTL (exp);
9823 expand_decl_rtl:
9824 gcc_assert (decl_rtl);
9826 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9827 settings for VECTOR_TYPE_P that might switch for the function. */
9828 if (currently_expanding_to_rtl
9829 && code == VAR_DECL && MEM_P (decl_rtl)
9830 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9831 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9832 else
9833 decl_rtl = copy_rtx (decl_rtl);
9835 /* Record writes to register variables. */
9836 if (modifier == EXPAND_WRITE
9837 && REG_P (decl_rtl)
9838 && HARD_REGISTER_P (decl_rtl))
9839 add_to_hard_reg_set (&crtl->asm_clobbers,
9840 GET_MODE (decl_rtl), REGNO (decl_rtl));
9842 /* Ensure variable marked as used even if it doesn't go through
9843 a parser. If it hasn't be used yet, write out an external
9844 definition. */
9845 if (exp)
9846 TREE_USED (exp) = 1;
9848 /* Show we haven't gotten RTL for this yet. */
9849 temp = 0;
9851 /* Variables inherited from containing functions should have
9852 been lowered by this point. */
9853 if (exp)
9854 context = decl_function_context (exp);
9855 gcc_assert (!exp
9856 || SCOPE_FILE_SCOPE_P (context)
9857 || context == current_function_decl
9858 || TREE_STATIC (exp)
9859 || DECL_EXTERNAL (exp)
9860 /* ??? C++ creates functions that are not TREE_STATIC. */
9861 || TREE_CODE (exp) == FUNCTION_DECL);
9863 /* This is the case of an array whose size is to be determined
9864 from its initializer, while the initializer is still being parsed.
9865 ??? We aren't parsing while expanding anymore. */
9867 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9868 temp = validize_mem (decl_rtl);
9870 /* If DECL_RTL is memory, we are in the normal case and the
9871 address is not valid, get the address into a register. */
9873 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9875 if (alt_rtl)
9876 *alt_rtl = decl_rtl;
9877 decl_rtl = use_anchored_address (decl_rtl);
9878 if (modifier != EXPAND_CONST_ADDRESS
9879 && modifier != EXPAND_SUM
9880 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9881 : GET_MODE (decl_rtl),
9882 XEXP (decl_rtl, 0),
9883 MEM_ADDR_SPACE (decl_rtl)))
9884 temp = replace_equiv_address (decl_rtl,
9885 copy_rtx (XEXP (decl_rtl, 0)));
9888 /* If we got something, return it. But first, set the alignment
9889 if the address is a register. */
9890 if (temp != 0)
9892 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9893 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9895 return temp;
9898 if (exp)
9899 dmode = DECL_MODE (exp);
9900 else
9901 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9903 /* If the mode of DECL_RTL does not match that of the decl,
9904 there are two cases: we are dealing with a BLKmode value
9905 that is returned in a register, or we are dealing with
9906 a promoted value. In the latter case, return a SUBREG
9907 of the wanted mode, but mark it so that we know that it
9908 was already extended. */
9909 if (REG_P (decl_rtl)
9910 && dmode != BLKmode
9911 && GET_MODE (decl_rtl) != dmode)
9913 machine_mode pmode;
9915 /* Get the signedness to be used for this variable. Ensure we get
9916 the same mode we got when the variable was declared. */
9917 if (code != SSA_NAME)
9918 pmode = promote_decl_mode (exp, &unsignedp);
9919 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9920 && gimple_code (g) == GIMPLE_CALL
9921 && !gimple_call_internal_p (g))
9922 pmode = promote_function_mode (type, mode, &unsignedp,
9923 gimple_call_fntype (g),
9925 else
9926 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9927 gcc_assert (GET_MODE (decl_rtl) == pmode);
9929 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9930 SUBREG_PROMOTED_VAR_P (temp) = 1;
9931 SUBREG_PROMOTED_SET (temp, unsignedp);
9932 return temp;
9935 return decl_rtl;
9937 case INTEGER_CST:
9938 /* Given that TYPE_PRECISION (type) is not always equal to
9939 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9940 the former to the latter according to the signedness of the
9941 type. */
9942 temp = immed_wide_int_const (wi::to_wide
9943 (exp,
9944 GET_MODE_PRECISION (TYPE_MODE (type))),
9945 TYPE_MODE (type));
9946 return temp;
9948 case VECTOR_CST:
9950 tree tmp = NULL_TREE;
9951 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9952 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9953 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9954 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9955 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9956 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9957 return const_vector_from_tree (exp);
9958 if (GET_MODE_CLASS (mode) == MODE_INT)
9960 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9961 return const_scalar_mask_from_tree (exp);
9962 else
9964 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9965 if (type_for_mode)
9966 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9967 type_for_mode, exp);
9970 if (!tmp)
9972 vec<constructor_elt, va_gc> *v;
9973 unsigned i;
9974 vec_alloc (v, VECTOR_CST_NELTS (exp));
9975 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9976 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9977 tmp = build_constructor (type, v);
9979 return expand_expr (tmp, ignore ? const0_rtx : target,
9980 tmode, modifier);
9983 case CONST_DECL:
9984 if (modifier == EXPAND_WRITE)
9986 /* Writing into CONST_DECL is always invalid, but handle it
9987 gracefully. */
9988 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9989 machine_mode address_mode = targetm.addr_space.address_mode (as);
9990 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9991 EXPAND_NORMAL, as);
9992 op0 = memory_address_addr_space (mode, op0, as);
9993 temp = gen_rtx_MEM (mode, op0);
9994 set_mem_addr_space (temp, as);
9995 return temp;
9997 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9999 case REAL_CST:
10000 /* If optimized, generate immediate CONST_DOUBLE
10001 which will be turned into memory by reload if necessary.
10003 We used to force a register so that loop.c could see it. But
10004 this does not allow gen_* patterns to perform optimizations with
10005 the constants. It also produces two insns in cases like "x = 1.0;".
10006 On most machines, floating-point constants are not permitted in
10007 many insns, so we'd end up copying it to a register in any case.
10009 Now, we do the copying in expand_binop, if appropriate. */
10010 return const_double_from_real_value (TREE_REAL_CST (exp),
10011 TYPE_MODE (TREE_TYPE (exp)));
10013 case FIXED_CST:
10014 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10015 TYPE_MODE (TREE_TYPE (exp)));
10017 case COMPLEX_CST:
10018 /* Handle evaluating a complex constant in a CONCAT target. */
10019 if (original_target && GET_CODE (original_target) == CONCAT)
10021 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10022 rtx rtarg, itarg;
10024 rtarg = XEXP (original_target, 0);
10025 itarg = XEXP (original_target, 1);
10027 /* Move the real and imaginary parts separately. */
10028 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10029 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10031 if (op0 != rtarg)
10032 emit_move_insn (rtarg, op0);
10033 if (op1 != itarg)
10034 emit_move_insn (itarg, op1);
10036 return original_target;
10039 /* fall through */
10041 case STRING_CST:
10042 temp = expand_expr_constant (exp, 1, modifier);
10044 /* temp contains a constant address.
10045 On RISC machines where a constant address isn't valid,
10046 make some insns to get that address into a register. */
10047 if (modifier != EXPAND_CONST_ADDRESS
10048 && modifier != EXPAND_INITIALIZER
10049 && modifier != EXPAND_SUM
10050 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10051 MEM_ADDR_SPACE (temp)))
10052 return replace_equiv_address (temp,
10053 copy_rtx (XEXP (temp, 0)));
10054 return temp;
10056 case SAVE_EXPR:
10058 tree val = treeop0;
10059 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10060 inner_reference_p);
10062 if (!SAVE_EXPR_RESOLVED_P (exp))
10064 /* We can indeed still hit this case, typically via builtin
10065 expanders calling save_expr immediately before expanding
10066 something. Assume this means that we only have to deal
10067 with non-BLKmode values. */
10068 gcc_assert (GET_MODE (ret) != BLKmode);
10070 val = build_decl (curr_insn_location (),
10071 VAR_DECL, NULL, TREE_TYPE (exp));
10072 DECL_ARTIFICIAL (val) = 1;
10073 DECL_IGNORED_P (val) = 1;
10074 treeop0 = val;
10075 TREE_OPERAND (exp, 0) = treeop0;
10076 SAVE_EXPR_RESOLVED_P (exp) = 1;
10078 if (!CONSTANT_P (ret))
10079 ret = copy_to_reg (ret);
10080 SET_DECL_RTL (val, ret);
10083 return ret;
10087 case CONSTRUCTOR:
10088 /* If we don't need the result, just ensure we evaluate any
10089 subexpressions. */
10090 if (ignore)
10092 unsigned HOST_WIDE_INT idx;
10093 tree value;
10095 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10096 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10098 return const0_rtx;
10101 return expand_constructor (exp, target, modifier, false);
10103 case TARGET_MEM_REF:
10105 addr_space_t as
10106 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10107 enum insn_code icode;
10108 unsigned int align;
10110 op0 = addr_for_mem_ref (exp, as, true);
10111 op0 = memory_address_addr_space (mode, op0, as);
10112 temp = gen_rtx_MEM (mode, op0);
10113 set_mem_attributes (temp, exp, 0);
10114 set_mem_addr_space (temp, as);
10115 align = get_object_alignment (exp);
10116 if (modifier != EXPAND_WRITE
10117 && modifier != EXPAND_MEMORY
10118 && mode != BLKmode
10119 && align < GET_MODE_ALIGNMENT (mode)
10120 /* If the target does not have special handling for unaligned
10121 loads of mode then it can use regular moves for them. */
10122 && ((icode = optab_handler (movmisalign_optab, mode))
10123 != CODE_FOR_nothing))
10125 struct expand_operand ops[2];
10127 /* We've already validated the memory, and we're creating a
10128 new pseudo destination. The predicates really can't fail,
10129 nor can the generator. */
10130 create_output_operand (&ops[0], NULL_RTX, mode);
10131 create_fixed_operand (&ops[1], temp);
10132 expand_insn (icode, 2, ops);
10133 temp = ops[0].value;
10135 return temp;
10138 case MEM_REF:
10140 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10141 addr_space_t as
10142 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10143 machine_mode address_mode;
10144 tree base = TREE_OPERAND (exp, 0);
10145 gimple *def_stmt;
10146 enum insn_code icode;
10147 unsigned align;
10148 /* Handle expansion of non-aliased memory with non-BLKmode. That
10149 might end up in a register. */
10150 if (mem_ref_refers_to_non_mem_p (exp))
10152 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10153 base = TREE_OPERAND (base, 0);
10154 if (offset == 0
10155 && !reverse
10156 && tree_fits_uhwi_p (TYPE_SIZE (type))
10157 && (GET_MODE_BITSIZE (DECL_MODE (base))
10158 == tree_to_uhwi (TYPE_SIZE (type))))
10159 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10160 target, tmode, modifier);
10161 if (TYPE_MODE (type) == BLKmode)
10163 temp = assign_stack_temp (DECL_MODE (base),
10164 GET_MODE_SIZE (DECL_MODE (base)));
10165 store_expr (base, temp, 0, false, false);
10166 temp = adjust_address (temp, BLKmode, offset);
10167 set_mem_size (temp, int_size_in_bytes (type));
10168 return temp;
10170 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10171 bitsize_int (offset * BITS_PER_UNIT));
10172 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10173 return expand_expr (exp, target, tmode, modifier);
10175 address_mode = targetm.addr_space.address_mode (as);
10176 base = TREE_OPERAND (exp, 0);
10177 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10179 tree mask = gimple_assign_rhs2 (def_stmt);
10180 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10181 gimple_assign_rhs1 (def_stmt), mask);
10182 TREE_OPERAND (exp, 0) = base;
10184 align = get_object_alignment (exp);
10185 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10186 op0 = memory_address_addr_space (mode, op0, as);
10187 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10189 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10190 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10191 op0 = memory_address_addr_space (mode, op0, as);
10193 temp = gen_rtx_MEM (mode, op0);
10194 set_mem_attributes (temp, exp, 0);
10195 set_mem_addr_space (temp, as);
10196 if (TREE_THIS_VOLATILE (exp))
10197 MEM_VOLATILE_P (temp) = 1;
10198 if (modifier != EXPAND_WRITE
10199 && modifier != EXPAND_MEMORY
10200 && !inner_reference_p
10201 && mode != BLKmode
10202 && align < GET_MODE_ALIGNMENT (mode))
10204 if ((icode = optab_handler (movmisalign_optab, mode))
10205 != CODE_FOR_nothing)
10207 struct expand_operand ops[2];
10209 /* We've already validated the memory, and we're creating a
10210 new pseudo destination. The predicates really can't fail,
10211 nor can the generator. */
10212 create_output_operand (&ops[0], NULL_RTX, mode);
10213 create_fixed_operand (&ops[1], temp);
10214 expand_insn (icode, 2, ops);
10215 temp = ops[0].value;
10217 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10218 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10219 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10220 (modifier == EXPAND_STACK_PARM
10221 ? NULL_RTX : target),
10222 mode, mode, false, alt_rtl);
10224 if (reverse
10225 && modifier != EXPAND_MEMORY
10226 && modifier != EXPAND_WRITE)
10227 temp = flip_storage_order (mode, temp);
10228 return temp;
10231 case ARRAY_REF:
10234 tree array = treeop0;
10235 tree index = treeop1;
10236 tree init;
10238 /* Fold an expression like: "foo"[2].
10239 This is not done in fold so it won't happen inside &.
10240 Don't fold if this is for wide characters since it's too
10241 difficult to do correctly and this is a very rare case. */
10243 if (modifier != EXPAND_CONST_ADDRESS
10244 && modifier != EXPAND_INITIALIZER
10245 && modifier != EXPAND_MEMORY)
10247 tree t = fold_read_from_constant_string (exp);
10249 if (t)
10250 return expand_expr (t, target, tmode, modifier);
10253 /* If this is a constant index into a constant array,
10254 just get the value from the array. Handle both the cases when
10255 we have an explicit constructor and when our operand is a variable
10256 that was declared const. */
10258 if (modifier != EXPAND_CONST_ADDRESS
10259 && modifier != EXPAND_INITIALIZER
10260 && modifier != EXPAND_MEMORY
10261 && TREE_CODE (array) == CONSTRUCTOR
10262 && ! TREE_SIDE_EFFECTS (array)
10263 && TREE_CODE (index) == INTEGER_CST)
10265 unsigned HOST_WIDE_INT ix;
10266 tree field, value;
10268 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10269 field, value)
10270 if (tree_int_cst_equal (field, index))
10272 if (!TREE_SIDE_EFFECTS (value))
10273 return expand_expr (fold (value), target, tmode, modifier);
10274 break;
10278 else if (optimize >= 1
10279 && modifier != EXPAND_CONST_ADDRESS
10280 && modifier != EXPAND_INITIALIZER
10281 && modifier != EXPAND_MEMORY
10282 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10283 && TREE_CODE (index) == INTEGER_CST
10284 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10285 && (init = ctor_for_folding (array)) != error_mark_node)
10287 if (init == NULL_TREE)
10289 tree value = build_zero_cst (type);
10290 if (TREE_CODE (value) == CONSTRUCTOR)
10292 /* If VALUE is a CONSTRUCTOR, this optimization is only
10293 useful if this doesn't store the CONSTRUCTOR into
10294 memory. If it does, it is more efficient to just
10295 load the data from the array directly. */
10296 rtx ret = expand_constructor (value, target,
10297 modifier, true);
10298 if (ret == NULL_RTX)
10299 value = NULL_TREE;
10302 if (value)
10303 return expand_expr (value, target, tmode, modifier);
10305 else if (TREE_CODE (init) == CONSTRUCTOR)
10307 unsigned HOST_WIDE_INT ix;
10308 tree field, value;
10310 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10311 field, value)
10312 if (tree_int_cst_equal (field, index))
10314 if (TREE_SIDE_EFFECTS (value))
10315 break;
10317 if (TREE_CODE (value) == CONSTRUCTOR)
10319 /* If VALUE is a CONSTRUCTOR, this
10320 optimization is only useful if
10321 this doesn't store the CONSTRUCTOR
10322 into memory. If it does, it is more
10323 efficient to just load the data from
10324 the array directly. */
10325 rtx ret = expand_constructor (value, target,
10326 modifier, true);
10327 if (ret == NULL_RTX)
10328 break;
10331 return
10332 expand_expr (fold (value), target, tmode, modifier);
10335 else if (TREE_CODE (init) == STRING_CST)
10337 tree low_bound = array_ref_low_bound (exp);
10338 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10340 /* Optimize the special case of a zero lower bound.
10342 We convert the lower bound to sizetype to avoid problems
10343 with constant folding. E.g. suppose the lower bound is
10344 1 and its mode is QI. Without the conversion
10345 (ARRAY + (INDEX - (unsigned char)1))
10346 becomes
10347 (ARRAY + (-(unsigned char)1) + INDEX)
10348 which becomes
10349 (ARRAY + 255 + INDEX). Oops! */
10350 if (!integer_zerop (low_bound))
10351 index1 = size_diffop_loc (loc, index1,
10352 fold_convert_loc (loc, sizetype,
10353 low_bound));
10355 if (tree_fits_uhwi_p (index1)
10356 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10358 tree type = TREE_TYPE (TREE_TYPE (init));
10359 machine_mode mode = TYPE_MODE (type);
10361 if (GET_MODE_CLASS (mode) == MODE_INT
10362 && GET_MODE_SIZE (mode) == 1)
10363 return gen_int_mode (TREE_STRING_POINTER (init)
10364 [TREE_INT_CST_LOW (index1)],
10365 mode);
10370 goto normal_inner_ref;
10372 case COMPONENT_REF:
10373 /* If the operand is a CONSTRUCTOR, we can just extract the
10374 appropriate field if it is present. */
10375 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10377 unsigned HOST_WIDE_INT idx;
10378 tree field, value;
10380 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10381 idx, field, value)
10382 if (field == treeop1
10383 /* We can normally use the value of the field in the
10384 CONSTRUCTOR. However, if this is a bitfield in
10385 an integral mode that we can fit in a HOST_WIDE_INT,
10386 we must mask only the number of bits in the bitfield,
10387 since this is done implicitly by the constructor. If
10388 the bitfield does not meet either of those conditions,
10389 we can't do this optimization. */
10390 && (! DECL_BIT_FIELD (field)
10391 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10392 && (GET_MODE_PRECISION (DECL_MODE (field))
10393 <= HOST_BITS_PER_WIDE_INT))))
10395 if (DECL_BIT_FIELD (field)
10396 && modifier == EXPAND_STACK_PARM)
10397 target = 0;
10398 op0 = expand_expr (value, target, tmode, modifier);
10399 if (DECL_BIT_FIELD (field))
10401 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10402 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10404 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10406 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10407 imode);
10408 op0 = expand_and (imode, op0, op1, target);
10410 else
10412 int count = GET_MODE_PRECISION (imode) - bitsize;
10414 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10415 target, 0);
10416 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10417 target, 0);
10421 return op0;
10424 goto normal_inner_ref;
10426 case BIT_FIELD_REF:
10427 case ARRAY_RANGE_REF:
10428 normal_inner_ref:
10430 machine_mode mode1, mode2;
10431 HOST_WIDE_INT bitsize, bitpos;
10432 tree offset;
10433 int reversep, volatilep = 0, must_force_mem;
10434 tree tem
10435 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10436 &unsignedp, &reversep, &volatilep);
10437 rtx orig_op0, memloc;
10438 bool clear_mem_expr = false;
10440 /* If we got back the original object, something is wrong. Perhaps
10441 we are evaluating an expression too early. In any event, don't
10442 infinitely recurse. */
10443 gcc_assert (tem != exp);
10445 /* If TEM's type is a union of variable size, pass TARGET to the inner
10446 computation, since it will need a temporary and TARGET is known
10447 to have to do. This occurs in unchecked conversion in Ada. */
10448 orig_op0 = op0
10449 = expand_expr_real (tem,
10450 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10451 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10452 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10453 != INTEGER_CST)
10454 && modifier != EXPAND_STACK_PARM
10455 ? target : NULL_RTX),
10456 VOIDmode,
10457 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10458 NULL, true);
10460 /* If the field has a mode, we want to access it in the
10461 field's mode, not the computed mode.
10462 If a MEM has VOIDmode (external with incomplete type),
10463 use BLKmode for it instead. */
10464 if (MEM_P (op0))
10466 if (mode1 != VOIDmode)
10467 op0 = adjust_address (op0, mode1, 0);
10468 else if (GET_MODE (op0) == VOIDmode)
10469 op0 = adjust_address (op0, BLKmode, 0);
10472 mode2
10473 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10475 /* If we have either an offset, a BLKmode result, or a reference
10476 outside the underlying object, we must force it to memory.
10477 Such a case can occur in Ada if we have unchecked conversion
10478 of an expression from a scalar type to an aggregate type or
10479 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10480 passed a partially uninitialized object or a view-conversion
10481 to a larger size. */
10482 must_force_mem = (offset
10483 || mode1 == BLKmode
10484 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10486 /* Handle CONCAT first. */
10487 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10489 if (bitpos == 0
10490 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10491 && COMPLEX_MODE_P (mode1)
10492 && COMPLEX_MODE_P (GET_MODE (op0))
10493 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10494 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10496 if (reversep)
10497 op0 = flip_storage_order (GET_MODE (op0), op0);
10498 if (mode1 != GET_MODE (op0))
10500 rtx parts[2];
10501 for (int i = 0; i < 2; i++)
10503 rtx op = read_complex_part (op0, i != 0);
10504 if (GET_CODE (op) == SUBREG)
10505 op = force_reg (GET_MODE (op), op);
10506 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10507 op);
10508 if (temp)
10509 op = temp;
10510 else
10512 if (!REG_P (op) && !MEM_P (op))
10513 op = force_reg (GET_MODE (op), op);
10514 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10516 parts[i] = op;
10518 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10520 return op0;
10522 if (bitpos == 0
10523 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10524 && bitsize)
10526 op0 = XEXP (op0, 0);
10527 mode2 = GET_MODE (op0);
10529 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10530 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10531 && bitpos
10532 && bitsize)
10534 op0 = XEXP (op0, 1);
10535 bitpos = 0;
10536 mode2 = GET_MODE (op0);
10538 else
10539 /* Otherwise force into memory. */
10540 must_force_mem = 1;
10543 /* If this is a constant, put it in a register if it is a legitimate
10544 constant and we don't need a memory reference. */
10545 if (CONSTANT_P (op0)
10546 && mode2 != BLKmode
10547 && targetm.legitimate_constant_p (mode2, op0)
10548 && !must_force_mem)
10549 op0 = force_reg (mode2, op0);
10551 /* Otherwise, if this is a constant, try to force it to the constant
10552 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10553 is a legitimate constant. */
10554 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10555 op0 = validize_mem (memloc);
10557 /* Otherwise, if this is a constant or the object is not in memory
10558 and need be, put it there. */
10559 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10561 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10562 emit_move_insn (memloc, op0);
10563 op0 = memloc;
10564 clear_mem_expr = true;
10567 if (offset)
10569 machine_mode address_mode;
10570 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10571 EXPAND_SUM);
10573 gcc_assert (MEM_P (op0));
10575 address_mode = get_address_mode (op0);
10576 if (GET_MODE (offset_rtx) != address_mode)
10578 /* We cannot be sure that the RTL in offset_rtx is valid outside
10579 of a memory address context, so force it into a register
10580 before attempting to convert it to the desired mode. */
10581 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10582 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10585 /* See the comment in expand_assignment for the rationale. */
10586 if (mode1 != VOIDmode
10587 && bitpos != 0
10588 && bitsize > 0
10589 && (bitpos % bitsize) == 0
10590 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10591 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10593 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10594 bitpos = 0;
10597 op0 = offset_address (op0, offset_rtx,
10598 highest_pow2_factor (offset));
10601 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10602 record its alignment as BIGGEST_ALIGNMENT. */
10603 if (MEM_P (op0) && bitpos == 0 && offset != 0
10604 && is_aligning_offset (offset, tem))
10605 set_mem_align (op0, BIGGEST_ALIGNMENT);
10607 /* Don't forget about volatility even if this is a bitfield. */
10608 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10610 if (op0 == orig_op0)
10611 op0 = copy_rtx (op0);
10613 MEM_VOLATILE_P (op0) = 1;
10616 /* In cases where an aligned union has an unaligned object
10617 as a field, we might be extracting a BLKmode value from
10618 an integer-mode (e.g., SImode) object. Handle this case
10619 by doing the extract into an object as wide as the field
10620 (which we know to be the width of a basic mode), then
10621 storing into memory, and changing the mode to BLKmode. */
10622 if (mode1 == VOIDmode
10623 || REG_P (op0) || GET_CODE (op0) == SUBREG
10624 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10625 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10626 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10627 && modifier != EXPAND_CONST_ADDRESS
10628 && modifier != EXPAND_INITIALIZER
10629 && modifier != EXPAND_MEMORY)
10630 /* If the bitfield is volatile and the bitsize
10631 is narrower than the access size of the bitfield,
10632 we need to extract bitfields from the access. */
10633 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10634 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10635 && mode1 != BLKmode
10636 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10637 /* If the field isn't aligned enough to fetch as a memref,
10638 fetch it as a bit field. */
10639 || (mode1 != BLKmode
10640 && (((MEM_P (op0)
10641 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10642 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10643 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10644 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10645 && modifier != EXPAND_MEMORY
10646 && ((modifier == EXPAND_CONST_ADDRESS
10647 || modifier == EXPAND_INITIALIZER)
10648 ? STRICT_ALIGNMENT
10649 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10650 || (bitpos % BITS_PER_UNIT != 0)))
10651 /* If the type and the field are a constant size and the
10652 size of the type isn't the same size as the bitfield,
10653 we must use bitfield operations. */
10654 || (bitsize >= 0
10655 && TYPE_SIZE (TREE_TYPE (exp))
10656 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10657 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10658 bitsize)))
10660 machine_mode ext_mode = mode;
10662 if (ext_mode == BLKmode
10663 && ! (target != 0 && MEM_P (op0)
10664 && MEM_P (target)
10665 && bitpos % BITS_PER_UNIT == 0))
10666 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10668 if (ext_mode == BLKmode)
10670 if (target == 0)
10671 target = assign_temp (type, 1, 1);
10673 /* ??? Unlike the similar test a few lines below, this one is
10674 very likely obsolete. */
10675 if (bitsize == 0)
10676 return target;
10678 /* In this case, BITPOS must start at a byte boundary and
10679 TARGET, if specified, must be a MEM. */
10680 gcc_assert (MEM_P (op0)
10681 && (!target || MEM_P (target))
10682 && !(bitpos % BITS_PER_UNIT));
10684 emit_block_move (target,
10685 adjust_address (op0, VOIDmode,
10686 bitpos / BITS_PER_UNIT),
10687 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10688 / BITS_PER_UNIT),
10689 (modifier == EXPAND_STACK_PARM
10690 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10692 return target;
10695 /* If we have nothing to extract, the result will be 0 for targets
10696 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10697 return 0 for the sake of consistency, as reading a zero-sized
10698 bitfield is valid in Ada and the value is fully specified. */
10699 if (bitsize == 0)
10700 return const0_rtx;
10702 op0 = validize_mem (op0);
10704 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10705 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10707 /* If the result has a record type and the extraction is done in
10708 an integral mode, then the field may be not aligned on a byte
10709 boundary; in this case, if it has reverse storage order, it
10710 needs to be extracted as a scalar field with reverse storage
10711 order and put back into memory order afterwards. */
10712 if (TREE_CODE (type) == RECORD_TYPE
10713 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10714 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10716 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10717 (modifier == EXPAND_STACK_PARM
10718 ? NULL_RTX : target),
10719 ext_mode, ext_mode, reversep, alt_rtl);
10721 /* If the result has a record type and the mode of OP0 is an
10722 integral mode then, if BITSIZE is narrower than this mode
10723 and this is for big-endian data, we must put the field
10724 into the high-order bits. And we must also put it back
10725 into memory order if it has been previously reversed. */
10726 if (TREE_CODE (type) == RECORD_TYPE
10727 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10729 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10731 if (bitsize < size
10732 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10733 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10734 size - bitsize, op0, 1);
10736 if (reversep)
10737 op0 = flip_storage_order (GET_MODE (op0), op0);
10740 /* If the result type is BLKmode, store the data into a temporary
10741 of the appropriate type, but with the mode corresponding to the
10742 mode for the data we have (op0's mode). */
10743 if (mode == BLKmode)
10745 rtx new_rtx
10746 = assign_stack_temp_for_type (ext_mode,
10747 GET_MODE_BITSIZE (ext_mode),
10748 type);
10749 emit_move_insn (new_rtx, op0);
10750 op0 = copy_rtx (new_rtx);
10751 PUT_MODE (op0, BLKmode);
10754 return op0;
10757 /* If the result is BLKmode, use that to access the object
10758 now as well. */
10759 if (mode == BLKmode)
10760 mode1 = BLKmode;
10762 /* Get a reference to just this component. */
10763 if (modifier == EXPAND_CONST_ADDRESS
10764 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10765 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10766 else
10767 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10769 if (op0 == orig_op0)
10770 op0 = copy_rtx (op0);
10772 /* Don't set memory attributes if the base expression is
10773 SSA_NAME that got expanded as a MEM. In that case, we should
10774 just honor its original memory attributes. */
10775 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10776 set_mem_attributes (op0, exp, 0);
10778 if (REG_P (XEXP (op0, 0)))
10779 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10781 /* If op0 is a temporary because the original expressions was forced
10782 to memory, clear MEM_EXPR so that the original expression cannot
10783 be marked as addressable through MEM_EXPR of the temporary. */
10784 if (clear_mem_expr)
10785 set_mem_expr (op0, NULL_TREE);
10787 MEM_VOLATILE_P (op0) |= volatilep;
10789 if (reversep
10790 && modifier != EXPAND_MEMORY
10791 && modifier != EXPAND_WRITE)
10792 op0 = flip_storage_order (mode1, op0);
10794 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10795 || modifier == EXPAND_CONST_ADDRESS
10796 || modifier == EXPAND_INITIALIZER)
10797 return op0;
10799 if (target == 0)
10800 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10802 convert_move (target, op0, unsignedp);
10803 return target;
10806 case OBJ_TYPE_REF:
10807 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10809 case CALL_EXPR:
10810 /* All valid uses of __builtin_va_arg_pack () are removed during
10811 inlining. */
10812 if (CALL_EXPR_VA_ARG_PACK (exp))
10813 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10815 tree fndecl = get_callee_fndecl (exp), attr;
10817 if (fndecl
10818 && (attr = lookup_attribute ("error",
10819 DECL_ATTRIBUTES (fndecl))) != NULL)
10820 error ("%Kcall to %qs declared with attribute error: %s",
10821 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10822 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10823 if (fndecl
10824 && (attr = lookup_attribute ("warning",
10825 DECL_ATTRIBUTES (fndecl))) != NULL)
10826 warning_at (tree_nonartificial_location (exp),
10827 0, "%Kcall to %qs declared with attribute warning: %s",
10828 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10829 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10831 /* Check for a built-in function. */
10832 if (fndecl && DECL_BUILT_IN (fndecl))
10834 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10835 if (CALL_WITH_BOUNDS_P (exp))
10836 return expand_builtin_with_bounds (exp, target, subtarget,
10837 tmode, ignore);
10838 else
10839 return expand_builtin (exp, target, subtarget, tmode, ignore);
10842 return expand_call (exp, target, ignore);
10844 case VIEW_CONVERT_EXPR:
10845 op0 = NULL_RTX;
10847 /* If we are converting to BLKmode, try to avoid an intermediate
10848 temporary by fetching an inner memory reference. */
10849 if (mode == BLKmode
10850 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10851 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10852 && handled_component_p (treeop0))
10854 machine_mode mode1;
10855 HOST_WIDE_INT bitsize, bitpos;
10856 tree offset;
10857 int unsignedp, reversep, volatilep = 0;
10858 tree tem
10859 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10860 &unsignedp, &reversep, &volatilep);
10861 rtx orig_op0;
10863 /* ??? We should work harder and deal with non-zero offsets. */
10864 if (!offset
10865 && (bitpos % BITS_PER_UNIT) == 0
10866 && !reversep
10867 && bitsize >= 0
10868 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10870 /* See the normal_inner_ref case for the rationale. */
10871 orig_op0
10872 = expand_expr_real (tem,
10873 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10874 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10875 != INTEGER_CST)
10876 && modifier != EXPAND_STACK_PARM
10877 ? target : NULL_RTX),
10878 VOIDmode,
10879 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10880 NULL, true);
10882 if (MEM_P (orig_op0))
10884 op0 = orig_op0;
10886 /* Get a reference to just this component. */
10887 if (modifier == EXPAND_CONST_ADDRESS
10888 || modifier == EXPAND_SUM
10889 || modifier == EXPAND_INITIALIZER)
10890 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10891 else
10892 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10894 if (op0 == orig_op0)
10895 op0 = copy_rtx (op0);
10897 set_mem_attributes (op0, treeop0, 0);
10898 if (REG_P (XEXP (op0, 0)))
10899 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10901 MEM_VOLATILE_P (op0) |= volatilep;
10906 if (!op0)
10907 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10908 NULL, inner_reference_p);
10910 /* If the input and output modes are both the same, we are done. */
10911 if (mode == GET_MODE (op0))
10913 /* If neither mode is BLKmode, and both modes are the same size
10914 then we can use gen_lowpart. */
10915 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10916 && (GET_MODE_PRECISION (mode)
10917 == GET_MODE_PRECISION (GET_MODE (op0)))
10918 && !COMPLEX_MODE_P (GET_MODE (op0)))
10920 if (GET_CODE (op0) == SUBREG)
10921 op0 = force_reg (GET_MODE (op0), op0);
10922 temp = gen_lowpart_common (mode, op0);
10923 if (temp)
10924 op0 = temp;
10925 else
10927 if (!REG_P (op0) && !MEM_P (op0))
10928 op0 = force_reg (GET_MODE (op0), op0);
10929 op0 = gen_lowpart (mode, op0);
10932 /* If both types are integral, convert from one mode to the other. */
10933 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10934 op0 = convert_modes (mode, GET_MODE (op0), op0,
10935 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10936 /* If the output type is a bit-field type, do an extraction. */
10937 else if (reduce_bit_field)
10938 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10939 TYPE_UNSIGNED (type), NULL_RTX,
10940 mode, mode, false, NULL);
10941 /* As a last resort, spill op0 to memory, and reload it in a
10942 different mode. */
10943 else if (!MEM_P (op0))
10945 /* If the operand is not a MEM, force it into memory. Since we
10946 are going to be changing the mode of the MEM, don't call
10947 force_const_mem for constants because we don't allow pool
10948 constants to change mode. */
10949 tree inner_type = TREE_TYPE (treeop0);
10951 gcc_assert (!TREE_ADDRESSABLE (exp));
10953 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10954 target
10955 = assign_stack_temp_for_type
10956 (TYPE_MODE (inner_type),
10957 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10959 emit_move_insn (target, op0);
10960 op0 = target;
10963 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10964 output type is such that the operand is known to be aligned, indicate
10965 that it is. Otherwise, we need only be concerned about alignment for
10966 non-BLKmode results. */
10967 if (MEM_P (op0))
10969 enum insn_code icode;
10971 if (modifier != EXPAND_WRITE
10972 && modifier != EXPAND_MEMORY
10973 && !inner_reference_p
10974 && mode != BLKmode
10975 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10977 /* If the target does have special handling for unaligned
10978 loads of mode then use them. */
10979 if ((icode = optab_handler (movmisalign_optab, mode))
10980 != CODE_FOR_nothing)
10982 rtx reg;
10984 op0 = adjust_address (op0, mode, 0);
10985 /* We've already validated the memory, and we're creating a
10986 new pseudo destination. The predicates really can't
10987 fail. */
10988 reg = gen_reg_rtx (mode);
10990 /* Nor can the insn generator. */
10991 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10992 emit_insn (insn);
10993 return reg;
10995 else if (STRICT_ALIGNMENT)
10997 tree inner_type = TREE_TYPE (treeop0);
10998 HOST_WIDE_INT temp_size
10999 = MAX (int_size_in_bytes (inner_type),
11000 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11001 rtx new_rtx
11002 = assign_stack_temp_for_type (mode, temp_size, type);
11003 rtx new_with_op0_mode
11004 = adjust_address (new_rtx, GET_MODE (op0), 0);
11006 gcc_assert (!TREE_ADDRESSABLE (exp));
11008 if (GET_MODE (op0) == BLKmode)
11009 emit_block_move (new_with_op0_mode, op0,
11010 GEN_INT (GET_MODE_SIZE (mode)),
11011 (modifier == EXPAND_STACK_PARM
11012 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11013 else
11014 emit_move_insn (new_with_op0_mode, op0);
11016 op0 = new_rtx;
11020 op0 = adjust_address (op0, mode, 0);
11023 return op0;
11025 case MODIFY_EXPR:
11027 tree lhs = treeop0;
11028 tree rhs = treeop1;
11029 gcc_assert (ignore);
11031 /* Check for |= or &= of a bitfield of size one into another bitfield
11032 of size 1. In this case, (unless we need the result of the
11033 assignment) we can do this more efficiently with a
11034 test followed by an assignment, if necessary.
11036 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11037 things change so we do, this code should be enhanced to
11038 support it. */
11039 if (TREE_CODE (lhs) == COMPONENT_REF
11040 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11041 || TREE_CODE (rhs) == BIT_AND_EXPR)
11042 && TREE_OPERAND (rhs, 0) == lhs
11043 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11044 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11045 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11047 rtx_code_label *label = gen_label_rtx ();
11048 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11049 do_jump (TREE_OPERAND (rhs, 1),
11050 value ? label : 0,
11051 value ? 0 : label,
11052 profile_probability::uninitialized ());
11053 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11054 false);
11055 do_pending_stack_adjust ();
11056 emit_label (label);
11057 return const0_rtx;
11060 expand_assignment (lhs, rhs, false);
11061 return const0_rtx;
11064 case ADDR_EXPR:
11065 return expand_expr_addr_expr (exp, target, tmode, modifier);
11067 case REALPART_EXPR:
11068 op0 = expand_normal (treeop0);
11069 return read_complex_part (op0, false);
11071 case IMAGPART_EXPR:
11072 op0 = expand_normal (treeop0);
11073 return read_complex_part (op0, true);
11075 case RETURN_EXPR:
11076 case LABEL_EXPR:
11077 case GOTO_EXPR:
11078 case SWITCH_EXPR:
11079 case ASM_EXPR:
11080 /* Expanded in cfgexpand.c. */
11081 gcc_unreachable ();
11083 case TRY_CATCH_EXPR:
11084 case CATCH_EXPR:
11085 case EH_FILTER_EXPR:
11086 case TRY_FINALLY_EXPR:
11087 /* Lowered by tree-eh.c. */
11088 gcc_unreachable ();
11090 case WITH_CLEANUP_EXPR:
11091 case CLEANUP_POINT_EXPR:
11092 case TARGET_EXPR:
11093 case CASE_LABEL_EXPR:
11094 case VA_ARG_EXPR:
11095 case BIND_EXPR:
11096 case INIT_EXPR:
11097 case CONJ_EXPR:
11098 case COMPOUND_EXPR:
11099 case PREINCREMENT_EXPR:
11100 case PREDECREMENT_EXPR:
11101 case POSTINCREMENT_EXPR:
11102 case POSTDECREMENT_EXPR:
11103 case LOOP_EXPR:
11104 case EXIT_EXPR:
11105 case COMPOUND_LITERAL_EXPR:
11106 /* Lowered by gimplify.c. */
11107 gcc_unreachable ();
11109 case FDESC_EXPR:
11110 /* Function descriptors are not valid except for as
11111 initialization constants, and should not be expanded. */
11112 gcc_unreachable ();
11114 case WITH_SIZE_EXPR:
11115 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11116 have pulled out the size to use in whatever context it needed. */
11117 return expand_expr_real (treeop0, original_target, tmode,
11118 modifier, alt_rtl, inner_reference_p);
11120 default:
11121 return expand_expr_real_2 (&ops, target, tmode, modifier);
11125 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11126 signedness of TYPE), possibly returning the result in TARGET. */
11127 static rtx
11128 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11130 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11131 if (target && GET_MODE (target) != GET_MODE (exp))
11132 target = 0;
11133 /* For constant values, reduce using build_int_cst_type. */
11134 if (CONST_INT_P (exp))
11136 HOST_WIDE_INT value = INTVAL (exp);
11137 tree t = build_int_cst_type (type, value);
11138 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11140 else if (TYPE_UNSIGNED (type))
11142 machine_mode mode = GET_MODE (exp);
11143 rtx mask = immed_wide_int_const
11144 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11145 return expand_and (mode, exp, mask, target);
11147 else
11149 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11150 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11151 exp, count, target, 0);
11152 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11153 exp, count, target, 0);
11157 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11158 when applied to the address of EXP produces an address known to be
11159 aligned more than BIGGEST_ALIGNMENT. */
11161 static int
11162 is_aligning_offset (const_tree offset, const_tree exp)
11164 /* Strip off any conversions. */
11165 while (CONVERT_EXPR_P (offset))
11166 offset = TREE_OPERAND (offset, 0);
11168 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11169 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11170 if (TREE_CODE (offset) != BIT_AND_EXPR
11171 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11172 || compare_tree_int (TREE_OPERAND (offset, 1),
11173 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11174 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11175 return 0;
11177 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11178 It must be NEGATE_EXPR. Then strip any more conversions. */
11179 offset = TREE_OPERAND (offset, 0);
11180 while (CONVERT_EXPR_P (offset))
11181 offset = TREE_OPERAND (offset, 0);
11183 if (TREE_CODE (offset) != NEGATE_EXPR)
11184 return 0;
11186 offset = TREE_OPERAND (offset, 0);
11187 while (CONVERT_EXPR_P (offset))
11188 offset = TREE_OPERAND (offset, 0);
11190 /* This must now be the address of EXP. */
11191 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11194 /* Return the tree node if an ARG corresponds to a string constant or zero
11195 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11196 in bytes within the string that ARG is accessing. The type of the
11197 offset will be `sizetype'. */
11199 tree
11200 string_constant (tree arg, tree *ptr_offset)
11202 tree array, offset, lower_bound;
11203 STRIP_NOPS (arg);
11205 if (TREE_CODE (arg) == ADDR_EXPR)
11207 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11209 *ptr_offset = size_zero_node;
11210 return TREE_OPERAND (arg, 0);
11212 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11214 array = TREE_OPERAND (arg, 0);
11215 offset = size_zero_node;
11217 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11219 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11220 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11221 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11222 return 0;
11224 /* Check if the array has a nonzero lower bound. */
11225 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11226 if (!integer_zerop (lower_bound))
11228 /* If the offset and base aren't both constants, return 0. */
11229 if (TREE_CODE (lower_bound) != INTEGER_CST)
11230 return 0;
11231 if (TREE_CODE (offset) != INTEGER_CST)
11232 return 0;
11233 /* Adjust offset by the lower bound. */
11234 offset = size_diffop (fold_convert (sizetype, offset),
11235 fold_convert (sizetype, lower_bound));
11238 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11240 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11241 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11242 if (TREE_CODE (array) != ADDR_EXPR)
11243 return 0;
11244 array = TREE_OPERAND (array, 0);
11245 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11246 return 0;
11248 else
11249 return 0;
11251 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11253 tree arg0 = TREE_OPERAND (arg, 0);
11254 tree arg1 = TREE_OPERAND (arg, 1);
11256 STRIP_NOPS (arg0);
11257 STRIP_NOPS (arg1);
11259 if (TREE_CODE (arg0) == ADDR_EXPR
11260 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11261 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11263 array = TREE_OPERAND (arg0, 0);
11264 offset = arg1;
11266 else if (TREE_CODE (arg1) == ADDR_EXPR
11267 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11268 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11270 array = TREE_OPERAND (arg1, 0);
11271 offset = arg0;
11273 else
11274 return 0;
11276 else
11277 return 0;
11279 if (TREE_CODE (array) == STRING_CST)
11281 *ptr_offset = fold_convert (sizetype, offset);
11282 return array;
11284 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11286 int length;
11287 tree init = ctor_for_folding (array);
11289 /* Variables initialized to string literals can be handled too. */
11290 if (init == error_mark_node
11291 || !init
11292 || TREE_CODE (init) != STRING_CST)
11293 return 0;
11295 /* Avoid const char foo[4] = "abcde"; */
11296 if (DECL_SIZE_UNIT (array) == NULL_TREE
11297 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11298 || (length = TREE_STRING_LENGTH (init)) <= 0
11299 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11300 return 0;
11302 /* If variable is bigger than the string literal, OFFSET must be constant
11303 and inside of the bounds of the string literal. */
11304 offset = fold_convert (sizetype, offset);
11305 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11306 && (! tree_fits_uhwi_p (offset)
11307 || compare_tree_int (offset, length) >= 0))
11308 return 0;
11310 *ptr_offset = offset;
11311 return init;
11314 return 0;
11317 /* Generate code to calculate OPS, and exploded expression
11318 using a store-flag instruction and return an rtx for the result.
11319 OPS reflects a comparison.
11321 If TARGET is nonzero, store the result there if convenient.
11323 Return zero if there is no suitable set-flag instruction
11324 available on this machine.
11326 Once expand_expr has been called on the arguments of the comparison,
11327 we are committed to doing the store flag, since it is not safe to
11328 re-evaluate the expression. We emit the store-flag insn by calling
11329 emit_store_flag, but only expand the arguments if we have a reason
11330 to believe that emit_store_flag will be successful. If we think that
11331 it will, but it isn't, we have to simulate the store-flag with a
11332 set/jump/set sequence. */
11334 static rtx
11335 do_store_flag (sepops ops, rtx target, machine_mode mode)
11337 enum rtx_code code;
11338 tree arg0, arg1, type;
11339 machine_mode operand_mode;
11340 int unsignedp;
11341 rtx op0, op1;
11342 rtx subtarget = target;
11343 location_t loc = ops->location;
11345 arg0 = ops->op0;
11346 arg1 = ops->op1;
11348 /* Don't crash if the comparison was erroneous. */
11349 if (arg0 == error_mark_node || arg1 == error_mark_node)
11350 return const0_rtx;
11352 type = TREE_TYPE (arg0);
11353 operand_mode = TYPE_MODE (type);
11354 unsignedp = TYPE_UNSIGNED (type);
11356 /* We won't bother with BLKmode store-flag operations because it would mean
11357 passing a lot of information to emit_store_flag. */
11358 if (operand_mode == BLKmode)
11359 return 0;
11361 /* We won't bother with store-flag operations involving function pointers
11362 when function pointers must be canonicalized before comparisons. */
11363 if (targetm.have_canonicalize_funcptr_for_compare ()
11364 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11365 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11366 == FUNCTION_TYPE))
11367 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11368 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11369 == FUNCTION_TYPE))))
11370 return 0;
11372 STRIP_NOPS (arg0);
11373 STRIP_NOPS (arg1);
11375 /* For vector typed comparisons emit code to generate the desired
11376 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11377 expander for this. */
11378 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11380 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11381 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11382 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11383 return expand_vec_cmp_expr (ops->type, ifexp, target);
11384 else
11386 tree if_true = constant_boolean_node (true, ops->type);
11387 tree if_false = constant_boolean_node (false, ops->type);
11388 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11389 if_false, target);
11393 /* Get the rtx comparison code to use. We know that EXP is a comparison
11394 operation of some type. Some comparisons against 1 and -1 can be
11395 converted to comparisons with zero. Do so here so that the tests
11396 below will be aware that we have a comparison with zero. These
11397 tests will not catch constants in the first operand, but constants
11398 are rarely passed as the first operand. */
11400 switch (ops->code)
11402 case EQ_EXPR:
11403 code = EQ;
11404 break;
11405 case NE_EXPR:
11406 code = NE;
11407 break;
11408 case LT_EXPR:
11409 if (integer_onep (arg1))
11410 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11411 else
11412 code = unsignedp ? LTU : LT;
11413 break;
11414 case LE_EXPR:
11415 if (! unsignedp && integer_all_onesp (arg1))
11416 arg1 = integer_zero_node, code = LT;
11417 else
11418 code = unsignedp ? LEU : LE;
11419 break;
11420 case GT_EXPR:
11421 if (! unsignedp && integer_all_onesp (arg1))
11422 arg1 = integer_zero_node, code = GE;
11423 else
11424 code = unsignedp ? GTU : GT;
11425 break;
11426 case GE_EXPR:
11427 if (integer_onep (arg1))
11428 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11429 else
11430 code = unsignedp ? GEU : GE;
11431 break;
11433 case UNORDERED_EXPR:
11434 code = UNORDERED;
11435 break;
11436 case ORDERED_EXPR:
11437 code = ORDERED;
11438 break;
11439 case UNLT_EXPR:
11440 code = UNLT;
11441 break;
11442 case UNLE_EXPR:
11443 code = UNLE;
11444 break;
11445 case UNGT_EXPR:
11446 code = UNGT;
11447 break;
11448 case UNGE_EXPR:
11449 code = UNGE;
11450 break;
11451 case UNEQ_EXPR:
11452 code = UNEQ;
11453 break;
11454 case LTGT_EXPR:
11455 code = LTGT;
11456 break;
11458 default:
11459 gcc_unreachable ();
11462 /* Put a constant second. */
11463 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11464 || TREE_CODE (arg0) == FIXED_CST)
11466 std::swap (arg0, arg1);
11467 code = swap_condition (code);
11470 /* If this is an equality or inequality test of a single bit, we can
11471 do this by shifting the bit being tested to the low-order bit and
11472 masking the result with the constant 1. If the condition was EQ,
11473 we xor it with 1. This does not require an scc insn and is faster
11474 than an scc insn even if we have it.
11476 The code to make this transformation was moved into fold_single_bit_test,
11477 so we just call into the folder and expand its result. */
11479 if ((code == NE || code == EQ)
11480 && integer_zerop (arg1)
11481 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11483 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11484 if (srcstmt
11485 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11487 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11488 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11489 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11490 gimple_assign_rhs1 (srcstmt),
11491 gimple_assign_rhs2 (srcstmt));
11492 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11493 if (temp)
11494 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11498 if (! get_subtarget (target)
11499 || GET_MODE (subtarget) != operand_mode)
11500 subtarget = 0;
11502 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11504 if (target == 0)
11505 target = gen_reg_rtx (mode);
11507 /* Try a cstore if possible. */
11508 return emit_store_flag_force (target, code, op0, op1,
11509 operand_mode, unsignedp,
11510 (TYPE_PRECISION (ops->type) == 1
11511 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11514 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11515 0 otherwise (i.e. if there is no casesi instruction).
11517 DEFAULT_PROBABILITY is the probability of jumping to the default
11518 label. */
11520 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11521 rtx table_label, rtx default_label, rtx fallback_label,
11522 profile_probability default_probability)
11524 struct expand_operand ops[5];
11525 machine_mode index_mode = SImode;
11526 rtx op1, op2, index;
11528 if (! targetm.have_casesi ())
11529 return 0;
11531 /* Convert the index to SImode. */
11532 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11534 machine_mode omode = TYPE_MODE (index_type);
11535 rtx rangertx = expand_normal (range);
11537 /* We must handle the endpoints in the original mode. */
11538 index_expr = build2 (MINUS_EXPR, index_type,
11539 index_expr, minval);
11540 minval = integer_zero_node;
11541 index = expand_normal (index_expr);
11542 if (default_label)
11543 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11544 omode, 1, default_label,
11545 default_probability);
11546 /* Now we can safely truncate. */
11547 index = convert_to_mode (index_mode, index, 0);
11549 else
11551 if (TYPE_MODE (index_type) != index_mode)
11553 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11554 index_expr = fold_convert (index_type, index_expr);
11557 index = expand_normal (index_expr);
11560 do_pending_stack_adjust ();
11562 op1 = expand_normal (minval);
11563 op2 = expand_normal (range);
11565 create_input_operand (&ops[0], index, index_mode);
11566 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11567 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11568 create_fixed_operand (&ops[3], table_label);
11569 create_fixed_operand (&ops[4], (default_label
11570 ? default_label
11571 : fallback_label));
11572 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11573 return 1;
11576 /* Attempt to generate a tablejump instruction; same concept. */
11577 /* Subroutine of the next function.
11579 INDEX is the value being switched on, with the lowest value
11580 in the table already subtracted.
11581 MODE is its expected mode (needed if INDEX is constant).
11582 RANGE is the length of the jump table.
11583 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11585 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11586 index value is out of range.
11587 DEFAULT_PROBABILITY is the probability of jumping to
11588 the default label. */
11590 static void
11591 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11592 rtx default_label, profile_probability default_probability)
11594 rtx temp, vector;
11596 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11597 cfun->cfg->max_jumptable_ents = INTVAL (range);
11599 /* Do an unsigned comparison (in the proper mode) between the index
11600 expression and the value which represents the length of the range.
11601 Since we just finished subtracting the lower bound of the range
11602 from the index expression, this comparison allows us to simultaneously
11603 check that the original index expression value is both greater than
11604 or equal to the minimum value of the range and less than or equal to
11605 the maximum value of the range. */
11607 if (default_label)
11608 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11609 default_label, default_probability);
11612 /* If index is in range, it must fit in Pmode.
11613 Convert to Pmode so we can index with it. */
11614 if (mode != Pmode)
11615 index = convert_to_mode (Pmode, index, 1);
11617 /* Don't let a MEM slip through, because then INDEX that comes
11618 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11619 and break_out_memory_refs will go to work on it and mess it up. */
11620 #ifdef PIC_CASE_VECTOR_ADDRESS
11621 if (flag_pic && !REG_P (index))
11622 index = copy_to_mode_reg (Pmode, index);
11623 #endif
11625 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11626 GET_MODE_SIZE, because this indicates how large insns are. The other
11627 uses should all be Pmode, because they are addresses. This code
11628 could fail if addresses and insns are not the same size. */
11629 index = simplify_gen_binary (MULT, Pmode, index,
11630 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11631 Pmode));
11632 index = simplify_gen_binary (PLUS, Pmode, index,
11633 gen_rtx_LABEL_REF (Pmode, table_label));
11635 #ifdef PIC_CASE_VECTOR_ADDRESS
11636 if (flag_pic)
11637 index = PIC_CASE_VECTOR_ADDRESS (index);
11638 else
11639 #endif
11640 index = memory_address (CASE_VECTOR_MODE, index);
11641 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11642 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11643 convert_move (temp, vector, 0);
11645 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11647 /* If we are generating PIC code or if the table is PC-relative, the
11648 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11649 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11650 emit_barrier ();
11654 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11655 rtx table_label, rtx default_label,
11656 profile_probability default_probability)
11658 rtx index;
11660 if (! targetm.have_tablejump ())
11661 return 0;
11663 index_expr = fold_build2 (MINUS_EXPR, index_type,
11664 fold_convert (index_type, index_expr),
11665 fold_convert (index_type, minval));
11666 index = expand_normal (index_expr);
11667 do_pending_stack_adjust ();
11669 do_tablejump (index, TYPE_MODE (index_type),
11670 convert_modes (TYPE_MODE (index_type),
11671 TYPE_MODE (TREE_TYPE (range)),
11672 expand_normal (range),
11673 TYPE_UNSIGNED (TREE_TYPE (range))),
11674 table_label, default_label, default_probability);
11675 return 1;
11678 /* Return a CONST_VECTOR rtx representing vector mask for
11679 a VECTOR_CST of booleans. */
11680 static rtx
11681 const_vector_mask_from_tree (tree exp)
11683 rtvec v;
11684 unsigned i;
11685 int units;
11686 tree elt;
11687 machine_mode inner, mode;
11689 mode = TYPE_MODE (TREE_TYPE (exp));
11690 units = GET_MODE_NUNITS (mode);
11691 inner = GET_MODE_INNER (mode);
11693 v = rtvec_alloc (units);
11695 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11697 elt = VECTOR_CST_ELT (exp, i);
11699 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11700 if (integer_zerop (elt))
11701 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11702 else if (integer_onep (elt)
11703 || integer_minus_onep (elt))
11704 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11705 else
11706 gcc_unreachable ();
11709 return gen_rtx_CONST_VECTOR (mode, v);
11712 /* Return a CONST_INT rtx representing vector mask for
11713 a VECTOR_CST of booleans. */
11714 static rtx
11715 const_scalar_mask_from_tree (tree exp)
11717 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11718 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11719 tree elt;
11720 unsigned i;
11722 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11724 elt = VECTOR_CST_ELT (exp, i);
11725 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11726 if (integer_all_onesp (elt))
11727 res = wi::set_bit (res, i);
11728 else
11729 gcc_assert (integer_zerop (elt));
11732 return immed_wide_int_const (res, mode);
11735 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11736 static rtx
11737 const_vector_from_tree (tree exp)
11739 rtvec v;
11740 unsigned i;
11741 int units;
11742 tree elt;
11743 machine_mode inner, mode;
11745 mode = TYPE_MODE (TREE_TYPE (exp));
11747 if (initializer_zerop (exp))
11748 return CONST0_RTX (mode);
11750 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11751 return const_vector_mask_from_tree (exp);
11753 units = GET_MODE_NUNITS (mode);
11754 inner = GET_MODE_INNER (mode);
11756 v = rtvec_alloc (units);
11758 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11760 elt = VECTOR_CST_ELT (exp, i);
11762 if (TREE_CODE (elt) == REAL_CST)
11763 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11764 inner);
11765 else if (TREE_CODE (elt) == FIXED_CST)
11766 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11767 inner);
11768 else
11769 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11772 return gen_rtx_CONST_VECTOR (mode, v);
11775 /* Build a decl for a personality function given a language prefix. */
11777 tree
11778 build_personality_function (const char *lang)
11780 const char *unwind_and_version;
11781 tree decl, type;
11782 char *name;
11784 switch (targetm_common.except_unwind_info (&global_options))
11786 case UI_NONE:
11787 return NULL;
11788 case UI_SJLJ:
11789 unwind_and_version = "_sj0";
11790 break;
11791 case UI_DWARF2:
11792 case UI_TARGET:
11793 unwind_and_version = "_v0";
11794 break;
11795 case UI_SEH:
11796 unwind_and_version = "_seh0";
11797 break;
11798 default:
11799 gcc_unreachable ();
11802 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11804 type = build_function_type_list (integer_type_node, integer_type_node,
11805 long_long_unsigned_type_node,
11806 ptr_type_node, ptr_type_node, NULL_TREE);
11807 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11808 get_identifier (name), type);
11809 DECL_ARTIFICIAL (decl) = 1;
11810 DECL_EXTERNAL (decl) = 1;
11811 TREE_PUBLIC (decl) = 1;
11813 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11814 are the flags assigned by targetm.encode_section_info. */
11815 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11817 return decl;
11820 /* Extracts the personality function of DECL and returns the corresponding
11821 libfunc. */
11824 get_personality_function (tree decl)
11826 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11827 enum eh_personality_kind pk;
11829 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11830 if (pk == eh_personality_none)
11831 return NULL;
11833 if (!personality
11834 && pk == eh_personality_any)
11835 personality = lang_hooks.eh_personality ();
11837 if (pk == eh_personality_lang)
11838 gcc_assert (personality != NULL_TREE);
11840 return XEXP (DECL_RTL (personality), 0);
11843 /* Returns a tree for the size of EXP in bytes. */
11845 static tree
11846 tree_expr_size (const_tree exp)
11848 if (DECL_P (exp)
11849 && DECL_SIZE_UNIT (exp) != 0)
11850 return DECL_SIZE_UNIT (exp);
11851 else
11852 return size_in_bytes (TREE_TYPE (exp));
11855 /* Return an rtx for the size in bytes of the value of EXP. */
11858 expr_size (tree exp)
11860 tree size;
11862 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11863 size = TREE_OPERAND (exp, 1);
11864 else
11866 size = tree_expr_size (exp);
11867 gcc_assert (size);
11868 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11871 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11874 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11875 if the size can vary or is larger than an integer. */
11877 static HOST_WIDE_INT
11878 int_expr_size (tree exp)
11880 tree size;
11882 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11883 size = TREE_OPERAND (exp, 1);
11884 else
11886 size = tree_expr_size (exp);
11887 gcc_assert (size);
11890 if (size == 0 || !tree_fits_shwi_p (size))
11891 return -1;
11893 return tree_to_shwi (size);