Define arm_arch_core_flags in a single file
[official-gcc.git] / gcc / expr.c
blobfe752fb8f2b3dc17b644da9d25c15e72cd3800b3
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2016 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, machine_mode,
84 tree, int, alias_set_type, bool);
85 static void store_constructor (tree, rtx, int, HOST_WIDE_INT, bool);
86 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
87 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
88 machine_mode, tree, alias_set_type, bool, bool);
90 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
92 static int is_aligning_offset (const_tree, const_tree);
93 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
94 static rtx do_store_flag (sepops, rtx, machine_mode);
95 #ifdef PUSH_ROUNDING
96 static void emit_single_push_insn (machine_mode, rtx, tree);
97 #endif
98 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
99 static rtx const_vector_from_tree (tree);
100 static rtx const_scalar_mask_from_tree (tree);
101 static tree tree_expr_size (const_tree);
102 static HOST_WIDE_INT int_expr_size (tree);
105 /* This is run to set up which modes can be used
106 directly in memory and to initialize the block move optab. It is run
107 at the beginning of compilation and when the target is reinitialized. */
109 void
110 init_expr_target (void)
112 rtx pat;
113 machine_mode mode;
114 int num_clobbers;
115 rtx mem, mem1;
116 rtx reg;
118 /* Try indexing by frame ptr and try by stack ptr.
119 It is known that on the Convex the stack ptr isn't a valid index.
120 With luck, one or the other is valid on any machine. */
121 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
122 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
124 /* A scratch register we can modify in-place below to avoid
125 useless RTL allocations. */
126 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
128 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
129 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
130 PATTERN (insn) = pat;
132 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
133 mode = (machine_mode) ((int) mode + 1))
135 int regno;
137 direct_load[(int) mode] = direct_store[(int) mode] = 0;
138 PUT_MODE (mem, mode);
139 PUT_MODE (mem1, mode);
141 /* See if there is some register that can be used in this mode and
142 directly loaded or stored from memory. */
144 if (mode != VOIDmode && mode != BLKmode)
145 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
146 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
147 regno++)
149 if (! HARD_REGNO_MODE_OK (regno, mode))
150 continue;
152 set_mode_and_regno (reg, mode, regno);
154 SET_SRC (pat) = mem;
155 SET_DEST (pat) = reg;
156 if (recog (pat, insn, &num_clobbers) >= 0)
157 direct_load[(int) mode] = 1;
159 SET_SRC (pat) = mem1;
160 SET_DEST (pat) = reg;
161 if (recog (pat, insn, &num_clobbers) >= 0)
162 direct_load[(int) mode] = 1;
164 SET_SRC (pat) = reg;
165 SET_DEST (pat) = mem;
166 if (recog (pat, insn, &num_clobbers) >= 0)
167 direct_store[(int) mode] = 1;
169 SET_SRC (pat) = reg;
170 SET_DEST (pat) = mem1;
171 if (recog (pat, insn, &num_clobbers) >= 0)
172 direct_store[(int) mode] = 1;
176 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
178 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
179 mode = GET_MODE_WIDER_MODE (mode))
181 machine_mode srcmode;
182 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
183 srcmode = GET_MODE_WIDER_MODE (srcmode))
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 (intermediate = from_mode; intermediate != VOIDmode;
551 intermediate = GET_MODE_WIDER_MODE (intermediate))
552 if (((can_extend_p (to_mode, intermediate, unsignedp)
553 != CODE_FOR_nothing)
554 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
555 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
556 && (can_extend_p (intermediate, from_mode, unsignedp)
557 != CODE_FOR_nothing))
559 convert_move (to, convert_to_mode (intermediate, from,
560 unsignedp), unsignedp);
561 return;
564 /* No suitable intermediate mode.
565 Generate what we need with shifts. */
566 shift_amount = (GET_MODE_PRECISION (to_mode)
567 - GET_MODE_PRECISION (from_mode));
568 from = gen_lowpart (to_mode, force_reg (from_mode, from));
569 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
570 to, unsignedp);
571 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
572 to, unsignedp);
573 if (tmp != to)
574 emit_move_insn (to, tmp);
575 return;
579 /* Support special truncate insns for certain modes. */
580 if (convert_optab_handler (trunc_optab, to_mode,
581 from_mode) != CODE_FOR_nothing)
583 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
584 to, from, UNKNOWN);
585 return;
588 /* Handle truncation of volatile memrefs, and so on;
589 the things that couldn't be truncated directly,
590 and for which there was no special instruction.
592 ??? Code above formerly short-circuited this, for most integer
593 mode pairs, with a force_reg in from_mode followed by a recursive
594 call to this routine. Appears always to have been wrong. */
595 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
597 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
598 emit_move_insn (to, temp);
599 return;
602 /* Mode combination is not recognized. */
603 gcc_unreachable ();
606 /* Return an rtx for a value that would result
607 from converting X to mode MODE.
608 Both X and MODE may be floating, or both integer.
609 UNSIGNEDP is nonzero if X is an unsigned value.
610 This can be done by referring to a part of X in place
611 or by copying to a new temporary with conversion. */
614 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
616 return convert_modes (mode, VOIDmode, x, unsignedp);
619 /* Return an rtx for a value that would result
620 from converting X from mode OLDMODE to mode MODE.
621 Both modes may be floating, or both integer.
622 UNSIGNEDP is nonzero if X is an unsigned value.
624 This can be done by referring to a part of X in place
625 or by copying to a new temporary with conversion.
627 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
630 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
632 rtx temp;
634 /* If FROM is a SUBREG that indicates that we have already done at least
635 the required extension, strip it. */
637 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
638 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
639 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
640 x = gen_lowpart (mode, SUBREG_REG (x));
642 if (GET_MODE (x) != VOIDmode)
643 oldmode = GET_MODE (x);
645 if (mode == oldmode)
646 return x;
648 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
650 /* If the caller did not tell us the old mode, then there is not
651 much to do with respect to canonicalization. We have to
652 assume that all the bits are significant. */
653 if (GET_MODE_CLASS (oldmode) != MODE_INT)
654 oldmode = MAX_MODE_INT;
655 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
656 GET_MODE_PRECISION (mode),
657 unsignedp ? UNSIGNED : SIGNED);
658 return immed_wide_int_const (w, mode);
661 /* We can do this with a gen_lowpart if both desired and current modes
662 are integer, and this is either a constant integer, a register, or a
663 non-volatile MEM. */
664 if (GET_MODE_CLASS (mode) == MODE_INT
665 && GET_MODE_CLASS (oldmode) == MODE_INT
666 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
667 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
668 || (REG_P (x)
669 && (!HARD_REGISTER_P (x)
670 || HARD_REGNO_MODE_OK (REGNO (x), mode))
671 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
673 return gen_lowpart (mode, x);
675 /* Converting from integer constant into mode is always equivalent to an
676 subreg operation. */
677 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
679 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
680 return simplify_gen_subreg (mode, x, oldmode, 0);
683 temp = gen_reg_rtx (mode);
684 convert_move (temp, x, unsignedp);
685 return temp;
688 /* Return the largest alignment we can use for doing a move (or store)
689 of MAX_PIECES. ALIGN is the largest alignment we could use. */
691 static unsigned int
692 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
694 machine_mode tmode;
696 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
697 if (align >= GET_MODE_ALIGNMENT (tmode))
698 align = GET_MODE_ALIGNMENT (tmode);
699 else
701 machine_mode tmode, xmode;
703 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
704 tmode != VOIDmode;
705 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
706 if (GET_MODE_SIZE (tmode) > max_pieces
707 || SLOW_UNALIGNED_ACCESS (tmode, align))
708 break;
710 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
713 return align;
716 /* Return the widest integer mode no wider than SIZE. If no such mode
717 can be found, return VOIDmode. */
719 static machine_mode
720 widest_int_mode_for_size (unsigned int size)
722 machine_mode tmode, mode = VOIDmode;
724 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
725 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
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, -1);
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, -1);
1489 /* Generate several move instructions to compare LEN bytes from blocks
1490 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1492 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1493 used to push FROM to the stack.
1495 ALIGN is maximum stack alignment we can assume.
1497 Optionally, the caller can pass a constfn and associated data in A1_CFN
1498 and A1_CFN_DATA. describing that the second operand being compared is a
1499 known constant and how to obtain its data. */
1501 static rtx
1502 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1503 rtx target, unsigned int align,
1504 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1506 rtx_code_label *fail_label = gen_label_rtx ();
1507 rtx_code_label *end_label = gen_label_rtx ();
1509 if (target == NULL_RTX
1510 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1511 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1513 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1514 fail_label);
1516 data.run ();
1518 emit_move_insn (target, const0_rtx);
1519 emit_jump (end_label);
1520 emit_barrier ();
1521 emit_label (fail_label);
1522 emit_move_insn (target, const1_rtx);
1523 emit_label (end_label);
1525 return target;
1528 /* Emit code to move a block Y to a block X. This may be done with
1529 string-move instructions, with multiple scalar move instructions,
1530 or with a library call.
1532 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1533 SIZE is an rtx that says how long they are.
1534 ALIGN is the maximum alignment we can assume they have.
1535 METHOD describes what kind of copy this is, and what mechanisms may be used.
1536 MIN_SIZE is the minimal size of block to move
1537 MAX_SIZE is the maximal size of block to move, if it can not be represented
1538 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1540 Return the address of the new block, if memcpy is called and returns it,
1541 0 otherwise. */
1544 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1545 unsigned int expected_align, HOST_WIDE_INT expected_size,
1546 unsigned HOST_WIDE_INT min_size,
1547 unsigned HOST_WIDE_INT max_size,
1548 unsigned HOST_WIDE_INT probable_max_size)
1550 bool may_use_call;
1551 rtx retval = 0;
1552 unsigned int align;
1554 gcc_assert (size);
1555 if (CONST_INT_P (size) && INTVAL (size) == 0)
1556 return 0;
1558 switch (method)
1560 case BLOCK_OP_NORMAL:
1561 case BLOCK_OP_TAILCALL:
1562 may_use_call = true;
1563 break;
1565 case BLOCK_OP_CALL_PARM:
1566 may_use_call = block_move_libcall_safe_for_call_parm ();
1568 /* Make inhibit_defer_pop nonzero around the library call
1569 to force it to pop the arguments right away. */
1570 NO_DEFER_POP;
1571 break;
1573 case BLOCK_OP_NO_LIBCALL:
1574 may_use_call = false;
1575 break;
1577 default:
1578 gcc_unreachable ();
1581 gcc_assert (MEM_P (x) && MEM_P (y));
1582 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1583 gcc_assert (align >= BITS_PER_UNIT);
1585 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1586 block copy is more efficient for other large modes, e.g. DCmode. */
1587 x = adjust_address (x, BLKmode, 0);
1588 y = adjust_address (y, BLKmode, 0);
1590 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1591 can be incorrect is coming from __builtin_memcpy. */
1592 if (CONST_INT_P (size))
1594 x = shallow_copy_rtx (x);
1595 y = shallow_copy_rtx (y);
1596 set_mem_size (x, INTVAL (size));
1597 set_mem_size (y, INTVAL (size));
1600 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1601 move_by_pieces (x, y, INTVAL (size), align, 0);
1602 else if (emit_block_move_via_movmem (x, y, size, align,
1603 expected_align, expected_size,
1604 min_size, max_size, probable_max_size))
1606 else if (may_use_call
1607 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1608 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1610 /* Since x and y are passed to a libcall, mark the corresponding
1611 tree EXPR as addressable. */
1612 tree y_expr = MEM_EXPR (y);
1613 tree x_expr = MEM_EXPR (x);
1614 if (y_expr)
1615 mark_addressable (y_expr);
1616 if (x_expr)
1617 mark_addressable (x_expr);
1618 retval = emit_block_copy_via_libcall (x, y, size,
1619 method == BLOCK_OP_TAILCALL);
1622 else
1623 emit_block_move_via_loop (x, y, size, align);
1625 if (method == BLOCK_OP_CALL_PARM)
1626 OK_DEFER_POP;
1628 return retval;
1632 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1634 unsigned HOST_WIDE_INT max, min = 0;
1635 if (GET_CODE (size) == CONST_INT)
1636 min = max = UINTVAL (size);
1637 else
1638 max = GET_MODE_MASK (GET_MODE (size));
1639 return emit_block_move_hints (x, y, size, method, 0, -1,
1640 min, max, max);
1643 /* A subroutine of emit_block_move. Returns true if calling the
1644 block move libcall will not clobber any parameters which may have
1645 already been placed on the stack. */
1647 static bool
1648 block_move_libcall_safe_for_call_parm (void)
1650 #if defined (REG_PARM_STACK_SPACE)
1651 tree fn;
1652 #endif
1654 /* If arguments are pushed on the stack, then they're safe. */
1655 if (PUSH_ARGS)
1656 return true;
1658 /* If registers go on the stack anyway, any argument is sure to clobber
1659 an outgoing argument. */
1660 #if defined (REG_PARM_STACK_SPACE)
1661 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1662 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1663 depend on its argument. */
1664 (void) fn;
1665 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1666 && REG_PARM_STACK_SPACE (fn) != 0)
1667 return false;
1668 #endif
1670 /* If any argument goes in memory, then it might clobber an outgoing
1671 argument. */
1673 CUMULATIVE_ARGS args_so_far_v;
1674 cumulative_args_t args_so_far;
1675 tree fn, arg;
1677 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1678 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1679 args_so_far = pack_cumulative_args (&args_so_far_v);
1681 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1682 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1684 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1685 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1686 NULL_TREE, true);
1687 if (!tmp || !REG_P (tmp))
1688 return false;
1689 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1690 return false;
1691 targetm.calls.function_arg_advance (args_so_far, mode,
1692 NULL_TREE, true);
1695 return true;
1698 /* A subroutine of emit_block_move. Expand a movmem pattern;
1699 return true if successful. */
1701 static bool
1702 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1703 unsigned int expected_align, HOST_WIDE_INT expected_size,
1704 unsigned HOST_WIDE_INT min_size,
1705 unsigned HOST_WIDE_INT max_size,
1706 unsigned HOST_WIDE_INT probable_max_size)
1708 int save_volatile_ok = volatile_ok;
1709 machine_mode mode;
1711 if (expected_align < align)
1712 expected_align = align;
1713 if (expected_size != -1)
1715 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1716 expected_size = probable_max_size;
1717 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1718 expected_size = min_size;
1721 /* Since this is a move insn, we don't care about volatility. */
1722 volatile_ok = 1;
1724 /* Try the most limited insn first, because there's no point
1725 including more than one in the machine description unless
1726 the more limited one has some advantage. */
1728 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1729 mode = GET_MODE_WIDER_MODE (mode))
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, REG_BR_PROB_BASE * 90 / 100);
1850 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1851 TAILCALL is true if this is a tail call. */
1854 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1855 rtx size, bool tailcall)
1857 rtx dst_addr, src_addr;
1858 tree call_expr, dst_tree, src_tree, size_tree;
1859 machine_mode size_mode;
1861 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1862 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1863 dst_tree = make_tree (ptr_type_node, dst_addr);
1865 src_addr = copy_addr_to_reg (XEXP (src, 0));
1866 src_addr = convert_memory_address (ptr_mode, src_addr);
1867 src_tree = make_tree (ptr_type_node, src_addr);
1869 size_mode = TYPE_MODE (sizetype);
1870 size = convert_to_mode (size_mode, size, 1);
1871 size = copy_to_mode_reg (size_mode, size);
1872 size_tree = make_tree (sizetype, size);
1874 /* It is incorrect to use the libcall calling conventions for calls to
1875 memcpy/memmove/memcmp because they can be provided by the user. */
1876 tree fn = builtin_decl_implicit (fncode);
1877 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1878 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1880 return expand_call (call_expr, NULL_RTX, false);
1883 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1884 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1885 otherwise return null. */
1888 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1889 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1890 HOST_WIDE_INT align)
1892 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1894 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1895 target = NULL_RTX;
1897 struct expand_operand ops[5];
1898 create_output_operand (&ops[0], target, insn_mode);
1899 create_fixed_operand (&ops[1], arg1_rtx);
1900 create_fixed_operand (&ops[2], arg2_rtx);
1901 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1902 TYPE_UNSIGNED (arg3_type));
1903 create_integer_operand (&ops[4], align);
1904 if (maybe_expand_insn (icode, 5, ops))
1905 return ops[0].value;
1906 return NULL_RTX;
1909 /* Expand a block compare between X and Y with length LEN using the
1910 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1911 of the expression that was used to calculate the length. ALIGN
1912 gives the known minimum common alignment. */
1914 static rtx
1915 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1916 unsigned align)
1918 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1919 implementing memcmp because it will stop if it encounters two
1920 zero bytes. */
1921 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1923 if (icode == CODE_FOR_nothing)
1924 return NULL_RTX;
1926 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1929 /* Emit code to compare a block Y to a block X. This may be done with
1930 string-compare instructions, with multiple scalar instructions,
1931 or with a library call.
1933 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1934 they are. LEN_TYPE is the type of the expression that was used to
1935 calculate it.
1937 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1938 value of a normal memcmp call, instead we can just compare for equality.
1939 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1940 returning NULL_RTX.
1942 Optionally, the caller can pass a constfn and associated data in Y_CFN
1943 and Y_CFN_DATA. describing that the second operand being compared is a
1944 known constant and how to obtain its data.
1945 Return the result of the comparison, or NULL_RTX if we failed to
1946 perform the operation. */
1949 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1950 bool equality_only, by_pieces_constfn y_cfn,
1951 void *y_cfndata)
1953 rtx result = 0;
1955 if (CONST_INT_P (len) && INTVAL (len) == 0)
1956 return const0_rtx;
1958 gcc_assert (MEM_P (x) && MEM_P (y));
1959 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1960 gcc_assert (align >= BITS_PER_UNIT);
1962 x = adjust_address (x, BLKmode, 0);
1963 y = adjust_address (y, BLKmode, 0);
1965 if (equality_only
1966 && CONST_INT_P (len)
1967 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1968 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1969 y_cfn, y_cfndata);
1970 else
1971 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1973 return result;
1976 /* Copy all or part of a value X into registers starting at REGNO.
1977 The number of registers to be filled is NREGS. */
1979 void
1980 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1982 if (nregs == 0)
1983 return;
1985 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1986 x = validize_mem (force_const_mem (mode, x));
1988 /* See if the machine can do this with a load multiple insn. */
1989 if (targetm.have_load_multiple ())
1991 rtx_insn *last = get_last_insn ();
1992 rtx first = gen_rtx_REG (word_mode, regno);
1993 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1994 GEN_INT (nregs)))
1996 emit_insn (pat);
1997 return;
1999 else
2000 delete_insns_since (last);
2003 for (int i = 0; i < nregs; i++)
2004 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2005 operand_subword_force (x, i, mode));
2008 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2009 The number of registers to be filled is NREGS. */
2011 void
2012 move_block_from_reg (int regno, rtx x, int nregs)
2014 if (nregs == 0)
2015 return;
2017 /* See if the machine can do this with a store multiple insn. */
2018 if (targetm.have_store_multiple ())
2020 rtx_insn *last = get_last_insn ();
2021 rtx first = gen_rtx_REG (word_mode, regno);
2022 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2023 GEN_INT (nregs)))
2025 emit_insn (pat);
2026 return;
2028 else
2029 delete_insns_since (last);
2032 for (int i = 0; i < nregs; i++)
2034 rtx tem = operand_subword (x, i, 1, BLKmode);
2036 gcc_assert (tem);
2038 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2042 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2043 ORIG, where ORIG is a non-consecutive group of registers represented by
2044 a PARALLEL. The clone is identical to the original except in that the
2045 original set of registers is replaced by a new set of pseudo registers.
2046 The new set has the same modes as the original set. */
2049 gen_group_rtx (rtx orig)
2051 int i, length;
2052 rtx *tmps;
2054 gcc_assert (GET_CODE (orig) == PARALLEL);
2056 length = XVECLEN (orig, 0);
2057 tmps = XALLOCAVEC (rtx, length);
2059 /* Skip a NULL entry in first slot. */
2060 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2062 if (i)
2063 tmps[0] = 0;
2065 for (; i < length; i++)
2067 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2068 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2070 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2073 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2076 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2077 except that values are placed in TMPS[i], and must later be moved
2078 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2080 static void
2081 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2083 rtx src;
2084 int start, i;
2085 machine_mode m = GET_MODE (orig_src);
2087 gcc_assert (GET_CODE (dst) == PARALLEL);
2089 if (m != VOIDmode
2090 && !SCALAR_INT_MODE_P (m)
2091 && !MEM_P (orig_src)
2092 && GET_CODE (orig_src) != CONCAT)
2094 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
2095 if (imode == BLKmode)
2096 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2097 else
2098 src = gen_reg_rtx (imode);
2099 if (imode != BLKmode)
2100 src = gen_lowpart (GET_MODE (orig_src), src);
2101 emit_move_insn (src, orig_src);
2102 /* ...and back again. */
2103 if (imode != BLKmode)
2104 src = gen_lowpart (imode, src);
2105 emit_group_load_1 (tmps, dst, src, type, ssize);
2106 return;
2109 /* Check for a NULL entry, used to indicate that the parameter goes
2110 both on the stack and in registers. */
2111 if (XEXP (XVECEXP (dst, 0, 0), 0))
2112 start = 0;
2113 else
2114 start = 1;
2116 /* Process the pieces. */
2117 for (i = start; i < XVECLEN (dst, 0); i++)
2119 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2120 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2121 unsigned int bytelen = GET_MODE_SIZE (mode);
2122 int shift = 0;
2124 /* Handle trailing fragments that run over the size of the struct. */
2125 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2127 /* Arrange to shift the fragment to where it belongs.
2128 extract_bit_field loads to the lsb of the reg. */
2129 if (
2130 #ifdef BLOCK_REG_PADDING
2131 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2132 == (BYTES_BIG_ENDIAN ? upward : downward)
2133 #else
2134 BYTES_BIG_ENDIAN
2135 #endif
2137 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2138 bytelen = ssize - bytepos;
2139 gcc_assert (bytelen > 0);
2142 /* If we won't be loading directly from memory, protect the real source
2143 from strange tricks we might play; but make sure that the source can
2144 be loaded directly into the destination. */
2145 src = orig_src;
2146 if (!MEM_P (orig_src)
2147 && (!CONSTANT_P (orig_src)
2148 || (GET_MODE (orig_src) != mode
2149 && GET_MODE (orig_src) != VOIDmode)))
2151 if (GET_MODE (orig_src) == VOIDmode)
2152 src = gen_reg_rtx (mode);
2153 else
2154 src = gen_reg_rtx (GET_MODE (orig_src));
2156 emit_move_insn (src, orig_src);
2159 /* Optimize the access just a bit. */
2160 if (MEM_P (src)
2161 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2162 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2163 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2164 && bytelen == GET_MODE_SIZE (mode))
2166 tmps[i] = gen_reg_rtx (mode);
2167 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2169 else if (COMPLEX_MODE_P (mode)
2170 && GET_MODE (src) == mode
2171 && bytelen == GET_MODE_SIZE (mode))
2172 /* Let emit_move_complex do the bulk of the work. */
2173 tmps[i] = src;
2174 else if (GET_CODE (src) == CONCAT)
2176 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2177 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2178 unsigned int elt = bytepos / slen0;
2179 unsigned int subpos = bytepos % slen0;
2181 if (subpos + bytelen <= slen0)
2183 /* The following assumes that the concatenated objects all
2184 have the same size. In this case, a simple calculation
2185 can be used to determine the object and the bit field
2186 to be extracted. */
2187 tmps[i] = XEXP (src, elt);
2188 if (subpos != 0
2189 || subpos + bytelen != slen0
2190 || (!CONSTANT_P (tmps[i])
2191 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2192 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2193 subpos * BITS_PER_UNIT,
2194 1, NULL_RTX, mode, mode, false);
2196 else
2198 rtx mem;
2200 gcc_assert (!bytepos);
2201 mem = assign_stack_temp (GET_MODE (src), slen);
2202 emit_move_insn (mem, src);
2203 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2204 0, 1, NULL_RTX, mode, mode, false);
2207 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2208 SIMD register, which is currently broken. While we get GCC
2209 to emit proper RTL for these cases, let's dump to memory. */
2210 else if (VECTOR_MODE_P (GET_MODE (dst))
2211 && REG_P (src))
2213 int slen = GET_MODE_SIZE (GET_MODE (src));
2214 rtx mem;
2216 mem = assign_stack_temp (GET_MODE (src), slen);
2217 emit_move_insn (mem, src);
2218 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2220 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2221 && XVECLEN (dst, 0) > 1)
2222 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2223 else if (CONSTANT_P (src))
2225 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2227 if (len == ssize)
2228 tmps[i] = src;
2229 else
2231 rtx first, second;
2233 /* TODO: const_wide_int can have sizes other than this... */
2234 gcc_assert (2 * len == ssize);
2235 split_double (src, &first, &second);
2236 if (i)
2237 tmps[i] = second;
2238 else
2239 tmps[i] = first;
2242 else if (REG_P (src) && GET_MODE (src) == mode)
2243 tmps[i] = src;
2244 else
2245 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2246 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2247 mode, mode, false);
2249 if (shift)
2250 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2251 shift, tmps[i], 0);
2255 /* Emit code to move a block SRC of type TYPE to a block DST,
2256 where DST is non-consecutive registers represented by a PARALLEL.
2257 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2258 if not known. */
2260 void
2261 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2263 rtx *tmps;
2264 int i;
2266 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2267 emit_group_load_1 (tmps, dst, src, type, ssize);
2269 /* Copy the extracted pieces into the proper (probable) hard regs. */
2270 for (i = 0; i < XVECLEN (dst, 0); i++)
2272 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2273 if (d == NULL)
2274 continue;
2275 emit_move_insn (d, tmps[i]);
2279 /* Similar, but load SRC into new pseudos in a format that looks like
2280 PARALLEL. This can later be fed to emit_group_move to get things
2281 in the right place. */
2284 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2286 rtvec vec;
2287 int i;
2289 vec = rtvec_alloc (XVECLEN (parallel, 0));
2290 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2292 /* Convert the vector to look just like the original PARALLEL, except
2293 with the computed values. */
2294 for (i = 0; i < XVECLEN (parallel, 0); i++)
2296 rtx e = XVECEXP (parallel, 0, i);
2297 rtx d = XEXP (e, 0);
2299 if (d)
2301 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2302 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2304 RTVEC_ELT (vec, i) = e;
2307 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2310 /* Emit code to move a block SRC to block DST, where SRC and DST are
2311 non-consecutive groups of registers, each represented by a PARALLEL. */
2313 void
2314 emit_group_move (rtx dst, rtx src)
2316 int i;
2318 gcc_assert (GET_CODE (src) == PARALLEL
2319 && GET_CODE (dst) == PARALLEL
2320 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2322 /* Skip first entry if NULL. */
2323 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2324 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2325 XEXP (XVECEXP (src, 0, i), 0));
2328 /* Move a group of registers represented by a PARALLEL into pseudos. */
2331 emit_group_move_into_temps (rtx src)
2333 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2334 int i;
2336 for (i = 0; i < XVECLEN (src, 0); i++)
2338 rtx e = XVECEXP (src, 0, i);
2339 rtx d = XEXP (e, 0);
2341 if (d)
2342 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2343 RTVEC_ELT (vec, i) = e;
2346 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2349 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2350 where SRC is non-consecutive registers represented by a PARALLEL.
2351 SSIZE represents the total size of block ORIG_DST, or -1 if not
2352 known. */
2354 void
2355 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2357 rtx *tmps, dst;
2358 int start, finish, i;
2359 machine_mode m = GET_MODE (orig_dst);
2361 gcc_assert (GET_CODE (src) == PARALLEL);
2363 if (!SCALAR_INT_MODE_P (m)
2364 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2366 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2367 if (imode == BLKmode)
2368 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2369 else
2370 dst = gen_reg_rtx (imode);
2371 emit_group_store (dst, src, type, ssize);
2372 if (imode != BLKmode)
2373 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2374 emit_move_insn (orig_dst, dst);
2375 return;
2378 /* Check for a NULL entry, used to indicate that the parameter goes
2379 both on the stack and in registers. */
2380 if (XEXP (XVECEXP (src, 0, 0), 0))
2381 start = 0;
2382 else
2383 start = 1;
2384 finish = XVECLEN (src, 0);
2386 tmps = XALLOCAVEC (rtx, finish);
2388 /* Copy the (probable) hard regs into pseudos. */
2389 for (i = start; i < finish; i++)
2391 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2392 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2394 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2395 emit_move_insn (tmps[i], reg);
2397 else
2398 tmps[i] = reg;
2401 /* If we won't be storing directly into memory, protect the real destination
2402 from strange tricks we might play. */
2403 dst = orig_dst;
2404 if (GET_CODE (dst) == PARALLEL)
2406 rtx temp;
2408 /* We can get a PARALLEL dst if there is a conditional expression in
2409 a return statement. In that case, the dst and src are the same,
2410 so no action is necessary. */
2411 if (rtx_equal_p (dst, src))
2412 return;
2414 /* It is unclear if we can ever reach here, but we may as well handle
2415 it. Allocate a temporary, and split this into a store/load to/from
2416 the temporary. */
2417 temp = assign_stack_temp (GET_MODE (dst), ssize);
2418 emit_group_store (temp, src, type, ssize);
2419 emit_group_load (dst, temp, type, ssize);
2420 return;
2422 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2424 machine_mode outer = GET_MODE (dst);
2425 machine_mode inner;
2426 HOST_WIDE_INT bytepos;
2427 bool done = false;
2428 rtx temp;
2430 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2431 dst = gen_reg_rtx (outer);
2433 /* Make life a bit easier for combine. */
2434 /* If the first element of the vector is the low part
2435 of the destination mode, use a paradoxical subreg to
2436 initialize the destination. */
2437 if (start < finish)
2439 inner = GET_MODE (tmps[start]);
2440 bytepos = subreg_lowpart_offset (inner, outer);
2441 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2443 temp = simplify_gen_subreg (outer, tmps[start],
2444 inner, 0);
2445 if (temp)
2447 emit_move_insn (dst, temp);
2448 done = true;
2449 start++;
2454 /* If the first element wasn't the low part, try the last. */
2455 if (!done
2456 && start < finish - 1)
2458 inner = GET_MODE (tmps[finish - 1]);
2459 bytepos = subreg_lowpart_offset (inner, outer);
2460 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2462 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2463 inner, 0);
2464 if (temp)
2466 emit_move_insn (dst, temp);
2467 done = true;
2468 finish--;
2473 /* Otherwise, simply initialize the result to zero. */
2474 if (!done)
2475 emit_move_insn (dst, CONST0_RTX (outer));
2478 /* Process the pieces. */
2479 for (i = start; i < finish; i++)
2481 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2482 machine_mode mode = GET_MODE (tmps[i]);
2483 unsigned int bytelen = GET_MODE_SIZE (mode);
2484 unsigned int adj_bytelen;
2485 rtx dest = dst;
2487 /* Handle trailing fragments that run over the size of the struct. */
2488 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2489 adj_bytelen = ssize - bytepos;
2490 else
2491 adj_bytelen = bytelen;
2493 if (GET_CODE (dst) == CONCAT)
2495 if (bytepos + adj_bytelen
2496 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2497 dest = XEXP (dst, 0);
2498 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2500 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2501 dest = XEXP (dst, 1);
2503 else
2505 machine_mode dest_mode = GET_MODE (dest);
2506 machine_mode tmp_mode = GET_MODE (tmps[i]);
2508 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2510 if (GET_MODE_ALIGNMENT (dest_mode)
2511 >= GET_MODE_ALIGNMENT (tmp_mode))
2513 dest = assign_stack_temp (dest_mode,
2514 GET_MODE_SIZE (dest_mode));
2515 emit_move_insn (adjust_address (dest,
2516 tmp_mode,
2517 bytepos),
2518 tmps[i]);
2519 dst = dest;
2521 else
2523 dest = assign_stack_temp (tmp_mode,
2524 GET_MODE_SIZE (tmp_mode));
2525 emit_move_insn (dest, tmps[i]);
2526 dst = adjust_address (dest, dest_mode, bytepos);
2528 break;
2532 /* Handle trailing fragments that run over the size of the struct. */
2533 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2535 /* store_bit_field always takes its value from the lsb.
2536 Move the fragment to the lsb if it's not already there. */
2537 if (
2538 #ifdef BLOCK_REG_PADDING
2539 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2540 == (BYTES_BIG_ENDIAN ? upward : downward)
2541 #else
2542 BYTES_BIG_ENDIAN
2543 #endif
2546 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2547 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2548 shift, tmps[i], 0);
2551 /* Make sure not to write past the end of the struct. */
2552 store_bit_field (dest,
2553 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2554 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2555 VOIDmode, tmps[i], false);
2558 /* Optimize the access just a bit. */
2559 else if (MEM_P (dest)
2560 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2561 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2562 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2563 && bytelen == GET_MODE_SIZE (mode))
2564 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2566 else
2567 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2568 0, 0, mode, tmps[i], false);
2571 /* Copy from the pseudo into the (probable) hard reg. */
2572 if (orig_dst != dst)
2573 emit_move_insn (orig_dst, dst);
2576 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2577 of the value stored in X. */
2580 maybe_emit_group_store (rtx x, tree type)
2582 machine_mode mode = TYPE_MODE (type);
2583 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2584 if (GET_CODE (x) == PARALLEL)
2586 rtx result = gen_reg_rtx (mode);
2587 emit_group_store (result, x, type, int_size_in_bytes (type));
2588 return result;
2590 return x;
2593 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2595 This is used on targets that return BLKmode values in registers. */
2597 static void
2598 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2600 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2601 rtx src = NULL, dst = NULL;
2602 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2603 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2604 machine_mode mode = GET_MODE (srcreg);
2605 machine_mode tmode = GET_MODE (target);
2606 machine_mode copy_mode;
2608 /* BLKmode registers created in the back-end shouldn't have survived. */
2609 gcc_assert (mode != BLKmode);
2611 /* If the structure doesn't take up a whole number of words, see whether
2612 SRCREG is padded on the left or on the right. If it's on the left,
2613 set PADDING_CORRECTION to the number of bits to skip.
2615 In most ABIs, the structure will be returned at the least end of
2616 the register, which translates to right padding on little-endian
2617 targets and left padding on big-endian targets. The opposite
2618 holds if the structure is returned at the most significant
2619 end of the register. */
2620 if (bytes % UNITS_PER_WORD != 0
2621 && (targetm.calls.return_in_msb (type)
2622 ? !BYTES_BIG_ENDIAN
2623 : BYTES_BIG_ENDIAN))
2624 padding_correction
2625 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2627 /* We can use a single move if we have an exact mode for the size. */
2628 else if (MEM_P (target)
2629 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2630 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2631 && bytes == GET_MODE_SIZE (mode))
2633 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2634 return;
2637 /* And if we additionally have the same mode for a register. */
2638 else if (REG_P (target)
2639 && GET_MODE (target) == mode
2640 && bytes == GET_MODE_SIZE (mode))
2642 emit_move_insn (target, srcreg);
2643 return;
2646 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2647 into a new pseudo which is a full word. */
2648 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2650 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2651 mode = word_mode;
2654 /* Copy the structure BITSIZE bits at a time. If the target lives in
2655 memory, take care of not reading/writing past its end by selecting
2656 a copy mode suited to BITSIZE. This should always be possible given
2657 how it is computed.
2659 If the target lives in register, make sure not to select a copy mode
2660 larger than the mode of the register.
2662 We could probably emit more efficient code for machines which do not use
2663 strict alignment, but it doesn't seem worth the effort at the current
2664 time. */
2666 copy_mode = word_mode;
2667 if (MEM_P (target))
2669 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2670 if (mem_mode != BLKmode)
2671 copy_mode = mem_mode;
2673 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2674 copy_mode = tmode;
2676 for (bitpos = 0, xbitpos = padding_correction;
2677 bitpos < bytes * BITS_PER_UNIT;
2678 bitpos += bitsize, xbitpos += bitsize)
2680 /* We need a new source operand each time xbitpos is on a
2681 word boundary and when xbitpos == padding_correction
2682 (the first time through). */
2683 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2684 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2686 /* We need a new destination operand each time bitpos is on
2687 a word boundary. */
2688 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2689 dst = target;
2690 else if (bitpos % BITS_PER_WORD == 0)
2691 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2693 /* Use xbitpos for the source extraction (right justified) and
2694 bitpos for the destination store (left justified). */
2695 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2696 extract_bit_field (src, bitsize,
2697 xbitpos % BITS_PER_WORD, 1,
2698 NULL_RTX, copy_mode, copy_mode,
2699 false),
2700 false);
2704 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2705 register if it contains any data, otherwise return null.
2707 This is used on targets that return BLKmode values in registers. */
2710 copy_blkmode_to_reg (machine_mode mode, tree src)
2712 int i, n_regs;
2713 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2714 unsigned int bitsize;
2715 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2716 machine_mode dst_mode;
2718 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2720 x = expand_normal (src);
2722 bytes = int_size_in_bytes (TREE_TYPE (src));
2723 if (bytes == 0)
2724 return NULL_RTX;
2726 /* If the structure doesn't take up a whole number of words, see
2727 whether the register value should be padded on the left or on
2728 the right. Set PADDING_CORRECTION to the number of padding
2729 bits needed on the left side.
2731 In most ABIs, the structure will be returned at the least end of
2732 the register, which translates to right padding on little-endian
2733 targets and left padding on big-endian targets. The opposite
2734 holds if the structure is returned at the most significant
2735 end of the register. */
2736 if (bytes % UNITS_PER_WORD != 0
2737 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2738 ? !BYTES_BIG_ENDIAN
2739 : BYTES_BIG_ENDIAN))
2740 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2741 * BITS_PER_UNIT));
2743 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2744 dst_words = XALLOCAVEC (rtx, n_regs);
2745 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2747 /* Copy the structure BITSIZE bits at a time. */
2748 for (bitpos = 0, xbitpos = padding_correction;
2749 bitpos < bytes * BITS_PER_UNIT;
2750 bitpos += bitsize, xbitpos += bitsize)
2752 /* We need a new destination pseudo each time xbitpos is
2753 on a word boundary and when xbitpos == padding_correction
2754 (the first time through). */
2755 if (xbitpos % BITS_PER_WORD == 0
2756 || xbitpos == padding_correction)
2758 /* Generate an appropriate register. */
2759 dst_word = gen_reg_rtx (word_mode);
2760 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2762 /* Clear the destination before we move anything into it. */
2763 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2766 /* We need a new source operand each time bitpos is on a word
2767 boundary. */
2768 if (bitpos % BITS_PER_WORD == 0)
2769 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2771 /* Use bitpos for the source extraction (left justified) and
2772 xbitpos for the destination store (right justified). */
2773 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2774 0, 0, word_mode,
2775 extract_bit_field (src_word, bitsize,
2776 bitpos % BITS_PER_WORD, 1,
2777 NULL_RTX, word_mode, word_mode,
2778 false),
2779 false);
2782 if (mode == BLKmode)
2784 /* Find the smallest integer mode large enough to hold the
2785 entire structure. */
2786 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2787 mode != VOIDmode;
2788 mode = GET_MODE_WIDER_MODE (mode))
2789 /* Have we found a large enough mode? */
2790 if (GET_MODE_SIZE (mode) >= bytes)
2791 break;
2793 /* A suitable mode should have been found. */
2794 gcc_assert (mode != VOIDmode);
2797 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2798 dst_mode = word_mode;
2799 else
2800 dst_mode = mode;
2801 dst = gen_reg_rtx (dst_mode);
2803 for (i = 0; i < n_regs; i++)
2804 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2806 if (mode != dst_mode)
2807 dst = gen_lowpart (mode, dst);
2809 return dst;
2812 /* Add a USE expression for REG to the (possibly empty) list pointed
2813 to by CALL_FUSAGE. REG must denote a hard register. */
2815 void
2816 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2818 gcc_assert (REG_P (reg));
2820 if (!HARD_REGISTER_P (reg))
2821 return;
2823 *call_fusage
2824 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2827 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2828 to by CALL_FUSAGE. REG must denote a hard register. */
2830 void
2831 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2833 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2835 *call_fusage
2836 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2839 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2840 starting at REGNO. All of these registers must be hard registers. */
2842 void
2843 use_regs (rtx *call_fusage, int regno, int nregs)
2845 int i;
2847 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2849 for (i = 0; i < nregs; i++)
2850 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2853 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2854 PARALLEL REGS. This is for calls that pass values in multiple
2855 non-contiguous locations. The Irix 6 ABI has examples of this. */
2857 void
2858 use_group_regs (rtx *call_fusage, rtx regs)
2860 int i;
2862 for (i = 0; i < XVECLEN (regs, 0); i++)
2864 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2866 /* A NULL entry means the parameter goes both on the stack and in
2867 registers. This can also be a MEM for targets that pass values
2868 partially on the stack and partially in registers. */
2869 if (reg != 0 && REG_P (reg))
2870 use_reg (call_fusage, reg);
2874 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2875 assigment and the code of the expresion on the RHS is CODE. Return
2876 NULL otherwise. */
2878 static gimple *
2879 get_def_for_expr (tree name, enum tree_code code)
2881 gimple *def_stmt;
2883 if (TREE_CODE (name) != SSA_NAME)
2884 return NULL;
2886 def_stmt = get_gimple_for_ssa_name (name);
2887 if (!def_stmt
2888 || gimple_assign_rhs_code (def_stmt) != code)
2889 return NULL;
2891 return def_stmt;
2894 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2895 assigment and the class of the expresion on the RHS is CLASS. Return
2896 NULL otherwise. */
2898 static gimple *
2899 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2901 gimple *def_stmt;
2903 if (TREE_CODE (name) != SSA_NAME)
2904 return NULL;
2906 def_stmt = get_gimple_for_ssa_name (name);
2907 if (!def_stmt
2908 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2909 return NULL;
2911 return def_stmt;
2914 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2915 its length in bytes. */
2918 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2919 unsigned int expected_align, HOST_WIDE_INT expected_size,
2920 unsigned HOST_WIDE_INT min_size,
2921 unsigned HOST_WIDE_INT max_size,
2922 unsigned HOST_WIDE_INT probable_max_size)
2924 machine_mode mode = GET_MODE (object);
2925 unsigned int align;
2927 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2929 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2930 just move a zero. Otherwise, do this a piece at a time. */
2931 if (mode != BLKmode
2932 && CONST_INT_P (size)
2933 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2935 rtx zero = CONST0_RTX (mode);
2936 if (zero != NULL)
2938 emit_move_insn (object, zero);
2939 return NULL;
2942 if (COMPLEX_MODE_P (mode))
2944 zero = CONST0_RTX (GET_MODE_INNER (mode));
2945 if (zero != NULL)
2947 write_complex_part (object, zero, 0);
2948 write_complex_part (object, zero, 1);
2949 return NULL;
2954 if (size == const0_rtx)
2955 return NULL;
2957 align = MEM_ALIGN (object);
2959 if (CONST_INT_P (size)
2960 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2961 CLEAR_BY_PIECES,
2962 optimize_insn_for_speed_p ()))
2963 clear_by_pieces (object, INTVAL (size), align);
2964 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2965 expected_align, expected_size,
2966 min_size, max_size, probable_max_size))
2968 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2969 return set_storage_via_libcall (object, size, const0_rtx,
2970 method == BLOCK_OP_TAILCALL);
2971 else
2972 gcc_unreachable ();
2974 return NULL;
2978 clear_storage (rtx object, rtx size, enum block_op_methods method)
2980 unsigned HOST_WIDE_INT max, min = 0;
2981 if (GET_CODE (size) == CONST_INT)
2982 min = max = UINTVAL (size);
2983 else
2984 max = GET_MODE_MASK (GET_MODE (size));
2985 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2989 /* A subroutine of clear_storage. Expand a call to memset.
2990 Return the return value of memset, 0 otherwise. */
2993 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2995 tree call_expr, fn, object_tree, size_tree, val_tree;
2996 machine_mode size_mode;
2998 object = copy_addr_to_reg (XEXP (object, 0));
2999 object_tree = make_tree (ptr_type_node, object);
3001 if (!CONST_INT_P (val))
3002 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3003 val_tree = make_tree (integer_type_node, val);
3005 size_mode = TYPE_MODE (sizetype);
3006 size = convert_to_mode (size_mode, size, 1);
3007 size = copy_to_mode_reg (size_mode, size);
3008 size_tree = make_tree (sizetype, size);
3010 /* It is incorrect to use the libcall calling conventions for calls to
3011 memset because it can be provided by the user. */
3012 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3013 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3014 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3016 return expand_call (call_expr, NULL_RTX, false);
3019 /* Expand a setmem pattern; return true if successful. */
3021 bool
3022 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3023 unsigned int expected_align, HOST_WIDE_INT expected_size,
3024 unsigned HOST_WIDE_INT min_size,
3025 unsigned HOST_WIDE_INT max_size,
3026 unsigned HOST_WIDE_INT probable_max_size)
3028 /* Try the most limited insn first, because there's no point
3029 including more than one in the machine description unless
3030 the more limited one has some advantage. */
3032 machine_mode mode;
3034 if (expected_align < align)
3035 expected_align = align;
3036 if (expected_size != -1)
3038 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3039 expected_size = max_size;
3040 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3041 expected_size = min_size;
3044 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3045 mode = GET_MODE_WIDER_MODE (mode))
3047 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3049 if (code != CODE_FOR_nothing
3050 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3051 here because if SIZE is less than the mode mask, as it is
3052 returned by the macro, it will definitely be less than the
3053 actual mode mask. Since SIZE is within the Pmode address
3054 space, we limit MODE to Pmode. */
3055 && ((CONST_INT_P (size)
3056 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3057 <= (GET_MODE_MASK (mode) >> 1)))
3058 || max_size <= (GET_MODE_MASK (mode) >> 1)
3059 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3061 struct expand_operand ops[9];
3062 unsigned int nops;
3064 nops = insn_data[(int) code].n_generator_args;
3065 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3067 create_fixed_operand (&ops[0], object);
3068 /* The check above guarantees that this size conversion is valid. */
3069 create_convert_operand_to (&ops[1], size, mode, true);
3070 create_convert_operand_from (&ops[2], val, byte_mode, true);
3071 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3072 if (nops >= 6)
3074 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3075 create_integer_operand (&ops[5], expected_size);
3077 if (nops >= 8)
3079 create_integer_operand (&ops[6], min_size);
3080 /* If we can not represent the maximal size,
3081 make parameter NULL. */
3082 if ((HOST_WIDE_INT) max_size != -1)
3083 create_integer_operand (&ops[7], max_size);
3084 else
3085 create_fixed_operand (&ops[7], NULL);
3087 if (nops == 9)
3089 /* If we can not represent the maximal size,
3090 make parameter NULL. */
3091 if ((HOST_WIDE_INT) probable_max_size != -1)
3092 create_integer_operand (&ops[8], probable_max_size);
3093 else
3094 create_fixed_operand (&ops[8], NULL);
3096 if (maybe_expand_insn (code, nops, ops))
3097 return true;
3101 return false;
3105 /* Write to one of the components of the complex value CPLX. Write VAL to
3106 the real part if IMAG_P is false, and the imaginary part if its true. */
3108 void
3109 write_complex_part (rtx cplx, rtx val, bool imag_p)
3111 machine_mode cmode;
3112 machine_mode imode;
3113 unsigned ibitsize;
3115 if (GET_CODE (cplx) == CONCAT)
3117 emit_move_insn (XEXP (cplx, imag_p), val);
3118 return;
3121 cmode = GET_MODE (cplx);
3122 imode = GET_MODE_INNER (cmode);
3123 ibitsize = GET_MODE_BITSIZE (imode);
3125 /* For MEMs simplify_gen_subreg may generate an invalid new address
3126 because, e.g., the original address is considered mode-dependent
3127 by the target, which restricts simplify_subreg from invoking
3128 adjust_address_nv. Instead of preparing fallback support for an
3129 invalid address, we call adjust_address_nv directly. */
3130 if (MEM_P (cplx))
3132 emit_move_insn (adjust_address_nv (cplx, imode,
3133 imag_p ? GET_MODE_SIZE (imode) : 0),
3134 val);
3135 return;
3138 /* If the sub-object is at least word sized, then we know that subregging
3139 will work. This special case is important, since store_bit_field
3140 wants to operate on integer modes, and there's rarely an OImode to
3141 correspond to TCmode. */
3142 if (ibitsize >= BITS_PER_WORD
3143 /* For hard regs we have exact predicates. Assume we can split
3144 the original object if it spans an even number of hard regs.
3145 This special case is important for SCmode on 64-bit platforms
3146 where the natural size of floating-point regs is 32-bit. */
3147 || (REG_P (cplx)
3148 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3149 && REG_NREGS (cplx) % 2 == 0))
3151 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3152 imag_p ? GET_MODE_SIZE (imode) : 0);
3153 if (part)
3155 emit_move_insn (part, val);
3156 return;
3158 else
3159 /* simplify_gen_subreg may fail for sub-word MEMs. */
3160 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3163 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3164 false);
3167 /* Extract one of the components of the complex value CPLX. Extract the
3168 real part if IMAG_P is false, and the imaginary part if it's true. */
3171 read_complex_part (rtx cplx, bool imag_p)
3173 machine_mode cmode, imode;
3174 unsigned ibitsize;
3176 if (GET_CODE (cplx) == CONCAT)
3177 return XEXP (cplx, imag_p);
3179 cmode = GET_MODE (cplx);
3180 imode = GET_MODE_INNER (cmode);
3181 ibitsize = GET_MODE_BITSIZE (imode);
3183 /* Special case reads from complex constants that got spilled to memory. */
3184 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3186 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3187 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3189 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3190 if (CONSTANT_CLASS_P (part))
3191 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3195 /* For MEMs simplify_gen_subreg may generate an invalid new address
3196 because, e.g., the original address is considered mode-dependent
3197 by the target, which restricts simplify_subreg from invoking
3198 adjust_address_nv. Instead of preparing fallback support for an
3199 invalid address, we call adjust_address_nv directly. */
3200 if (MEM_P (cplx))
3201 return adjust_address_nv (cplx, imode,
3202 imag_p ? GET_MODE_SIZE (imode) : 0);
3204 /* If the sub-object is at least word sized, then we know that subregging
3205 will work. This special case is important, since extract_bit_field
3206 wants to operate on integer modes, and there's rarely an OImode to
3207 correspond to TCmode. */
3208 if (ibitsize >= BITS_PER_WORD
3209 /* For hard regs we have exact predicates. Assume we can split
3210 the original object if it spans an even number of hard regs.
3211 This special case is important for SCmode on 64-bit platforms
3212 where the natural size of floating-point regs is 32-bit. */
3213 || (REG_P (cplx)
3214 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3215 && REG_NREGS (cplx) % 2 == 0))
3217 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3218 imag_p ? GET_MODE_SIZE (imode) : 0);
3219 if (ret)
3220 return ret;
3221 else
3222 /* simplify_gen_subreg may fail for sub-word MEMs. */
3223 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3226 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3227 true, NULL_RTX, imode, imode, false);
3230 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3231 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3232 represented in NEW_MODE. If FORCE is true, this will never happen, as
3233 we'll force-create a SUBREG if needed. */
3235 static rtx
3236 emit_move_change_mode (machine_mode new_mode,
3237 machine_mode old_mode, rtx x, bool force)
3239 rtx ret;
3241 if (push_operand (x, GET_MODE (x)))
3243 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3244 MEM_COPY_ATTRIBUTES (ret, x);
3246 else if (MEM_P (x))
3248 /* We don't have to worry about changing the address since the
3249 size in bytes is supposed to be the same. */
3250 if (reload_in_progress)
3252 /* Copy the MEM to change the mode and move any
3253 substitutions from the old MEM to the new one. */
3254 ret = adjust_address_nv (x, new_mode, 0);
3255 copy_replacements (x, ret);
3257 else
3258 ret = adjust_address (x, new_mode, 0);
3260 else
3262 /* Note that we do want simplify_subreg's behavior of validating
3263 that the new mode is ok for a hard register. If we were to use
3264 simplify_gen_subreg, we would create the subreg, but would
3265 probably run into the target not being able to implement it. */
3266 /* Except, of course, when FORCE is true, when this is exactly what
3267 we want. Which is needed for CCmodes on some targets. */
3268 if (force)
3269 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3270 else
3271 ret = simplify_subreg (new_mode, x, old_mode, 0);
3274 return ret;
3277 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3278 an integer mode of the same size as MODE. Returns the instruction
3279 emitted, or NULL if such a move could not be generated. */
3281 static rtx_insn *
3282 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3284 machine_mode imode;
3285 enum insn_code code;
3287 /* There must exist a mode of the exact size we require. */
3288 imode = int_mode_for_mode (mode);
3289 if (imode == BLKmode)
3290 return NULL;
3292 /* The target must support moves in this mode. */
3293 code = optab_handler (mov_optab, imode);
3294 if (code == CODE_FOR_nothing)
3295 return NULL;
3297 x = emit_move_change_mode (imode, mode, x, force);
3298 if (x == NULL_RTX)
3299 return NULL;
3300 y = emit_move_change_mode (imode, mode, y, force);
3301 if (y == NULL_RTX)
3302 return NULL;
3303 return emit_insn (GEN_FCN (code) (x, y));
3306 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3307 Return an equivalent MEM that does not use an auto-increment. */
3310 emit_move_resolve_push (machine_mode mode, rtx x)
3312 enum rtx_code code = GET_CODE (XEXP (x, 0));
3313 HOST_WIDE_INT adjust;
3314 rtx temp;
3316 adjust = GET_MODE_SIZE (mode);
3317 #ifdef PUSH_ROUNDING
3318 adjust = PUSH_ROUNDING (adjust);
3319 #endif
3320 if (code == PRE_DEC || code == POST_DEC)
3321 adjust = -adjust;
3322 else if (code == PRE_MODIFY || code == POST_MODIFY)
3324 rtx expr = XEXP (XEXP (x, 0), 1);
3325 HOST_WIDE_INT val;
3327 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3328 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3329 val = INTVAL (XEXP (expr, 1));
3330 if (GET_CODE (expr) == MINUS)
3331 val = -val;
3332 gcc_assert (adjust == val || adjust == -val);
3333 adjust = val;
3336 /* Do not use anti_adjust_stack, since we don't want to update
3337 stack_pointer_delta. */
3338 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3339 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3340 0, OPTAB_LIB_WIDEN);
3341 if (temp != stack_pointer_rtx)
3342 emit_move_insn (stack_pointer_rtx, temp);
3344 switch (code)
3346 case PRE_INC:
3347 case PRE_DEC:
3348 case PRE_MODIFY:
3349 temp = stack_pointer_rtx;
3350 break;
3351 case POST_INC:
3352 case POST_DEC:
3353 case POST_MODIFY:
3354 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3355 break;
3356 default:
3357 gcc_unreachable ();
3360 return replace_equiv_address (x, temp);
3363 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3364 X is known to satisfy push_operand, and MODE is known to be complex.
3365 Returns the last instruction emitted. */
3367 rtx_insn *
3368 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3370 machine_mode submode = GET_MODE_INNER (mode);
3371 bool imag_first;
3373 #ifdef PUSH_ROUNDING
3374 unsigned int submodesize = GET_MODE_SIZE (submode);
3376 /* In case we output to the stack, but the size is smaller than the
3377 machine can push exactly, we need to use move instructions. */
3378 if (PUSH_ROUNDING (submodesize) != submodesize)
3380 x = emit_move_resolve_push (mode, x);
3381 return emit_move_insn (x, y);
3383 #endif
3385 /* Note that the real part always precedes the imag part in memory
3386 regardless of machine's endianness. */
3387 switch (GET_CODE (XEXP (x, 0)))
3389 case PRE_DEC:
3390 case POST_DEC:
3391 imag_first = true;
3392 break;
3393 case PRE_INC:
3394 case POST_INC:
3395 imag_first = false;
3396 break;
3397 default:
3398 gcc_unreachable ();
3401 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3402 read_complex_part (y, imag_first));
3403 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3404 read_complex_part (y, !imag_first));
3407 /* A subroutine of emit_move_complex. Perform the move from Y to X
3408 via two moves of the parts. Returns the last instruction emitted. */
3410 rtx_insn *
3411 emit_move_complex_parts (rtx x, rtx y)
3413 /* Show the output dies here. This is necessary for SUBREGs
3414 of pseudos since we cannot track their lifetimes correctly;
3415 hard regs shouldn't appear here except as return values. */
3416 if (!reload_completed && !reload_in_progress
3417 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3418 emit_clobber (x);
3420 write_complex_part (x, read_complex_part (y, false), false);
3421 write_complex_part (x, read_complex_part (y, true), true);
3423 return get_last_insn ();
3426 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3427 MODE is known to be complex. Returns the last instruction emitted. */
3429 static rtx_insn *
3430 emit_move_complex (machine_mode mode, rtx x, rtx y)
3432 bool try_int;
3434 /* Need to take special care for pushes, to maintain proper ordering
3435 of the data, and possibly extra padding. */
3436 if (push_operand (x, mode))
3437 return emit_move_complex_push (mode, x, y);
3439 /* See if we can coerce the target into moving both values at once, except
3440 for floating point where we favor moving as parts if this is easy. */
3441 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3442 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3443 && !(REG_P (x)
3444 && HARD_REGISTER_P (x)
3445 && REG_NREGS (x) == 1)
3446 && !(REG_P (y)
3447 && HARD_REGISTER_P (y)
3448 && REG_NREGS (y) == 1))
3449 try_int = false;
3450 /* Not possible if the values are inherently not adjacent. */
3451 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3452 try_int = false;
3453 /* Is possible if both are registers (or subregs of registers). */
3454 else if (register_operand (x, mode) && register_operand (y, mode))
3455 try_int = true;
3456 /* If one of the operands is a memory, and alignment constraints
3457 are friendly enough, we may be able to do combined memory operations.
3458 We do not attempt this if Y is a constant because that combination is
3459 usually better with the by-parts thing below. */
3460 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3461 && (!STRICT_ALIGNMENT
3462 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3463 try_int = true;
3464 else
3465 try_int = false;
3467 if (try_int)
3469 rtx_insn *ret;
3471 /* For memory to memory moves, optimal behavior can be had with the
3472 existing block move logic. */
3473 if (MEM_P (x) && MEM_P (y))
3475 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3476 BLOCK_OP_NO_LIBCALL);
3477 return get_last_insn ();
3480 ret = emit_move_via_integer (mode, x, y, true);
3481 if (ret)
3482 return ret;
3485 return emit_move_complex_parts (x, y);
3488 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3489 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3491 static rtx_insn *
3492 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3494 rtx_insn *ret;
3496 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3497 if (mode != CCmode)
3499 enum insn_code code = optab_handler (mov_optab, CCmode);
3500 if (code != CODE_FOR_nothing)
3502 x = emit_move_change_mode (CCmode, mode, x, true);
3503 y = emit_move_change_mode (CCmode, mode, y, true);
3504 return emit_insn (GEN_FCN (code) (x, y));
3508 /* Otherwise, find the MODE_INT mode of the same width. */
3509 ret = emit_move_via_integer (mode, x, y, false);
3510 gcc_assert (ret != NULL);
3511 return ret;
3514 /* Return true if word I of OP lies entirely in the
3515 undefined bits of a paradoxical subreg. */
3517 static bool
3518 undefined_operand_subword_p (const_rtx op, int i)
3520 machine_mode innermode, innermostmode;
3521 int offset;
3522 if (GET_CODE (op) != SUBREG)
3523 return false;
3524 innermode = GET_MODE (op);
3525 innermostmode = GET_MODE (SUBREG_REG (op));
3526 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3527 /* The SUBREG_BYTE represents offset, as if the value were stored in
3528 memory, except for a paradoxical subreg where we define
3529 SUBREG_BYTE to be 0; undo this exception as in
3530 simplify_subreg. */
3531 if (SUBREG_BYTE (op) == 0
3532 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3534 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3535 if (WORDS_BIG_ENDIAN)
3536 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3537 if (BYTES_BIG_ENDIAN)
3538 offset += difference % UNITS_PER_WORD;
3540 if (offset >= GET_MODE_SIZE (innermostmode)
3541 || offset <= -GET_MODE_SIZE (word_mode))
3542 return true;
3543 return false;
3546 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3547 MODE is any multi-word or full-word mode that lacks a move_insn
3548 pattern. Note that you will get better code if you define such
3549 patterns, even if they must turn into multiple assembler instructions. */
3551 static rtx_insn *
3552 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3554 rtx_insn *last_insn = 0;
3555 rtx_insn *seq;
3556 rtx inner;
3557 bool need_clobber;
3558 int i;
3560 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3562 /* If X is a push on the stack, do the push now and replace
3563 X with a reference to the stack pointer. */
3564 if (push_operand (x, mode))
3565 x = emit_move_resolve_push (mode, x);
3567 /* If we are in reload, see if either operand is a MEM whose address
3568 is scheduled for replacement. */
3569 if (reload_in_progress && MEM_P (x)
3570 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3571 x = replace_equiv_address_nv (x, inner);
3572 if (reload_in_progress && MEM_P (y)
3573 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3574 y = replace_equiv_address_nv (y, inner);
3576 start_sequence ();
3578 need_clobber = false;
3579 for (i = 0;
3580 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3581 i++)
3583 rtx xpart = operand_subword (x, i, 1, mode);
3584 rtx ypart;
3586 /* Do not generate code for a move if it would come entirely
3587 from the undefined bits of a paradoxical subreg. */
3588 if (undefined_operand_subword_p (y, i))
3589 continue;
3591 ypart = operand_subword (y, i, 1, mode);
3593 /* If we can't get a part of Y, put Y into memory if it is a
3594 constant. Otherwise, force it into a register. Then we must
3595 be able to get a part of Y. */
3596 if (ypart == 0 && CONSTANT_P (y))
3598 y = use_anchored_address (force_const_mem (mode, y));
3599 ypart = operand_subword (y, i, 1, mode);
3601 else if (ypart == 0)
3602 ypart = operand_subword_force (y, i, mode);
3604 gcc_assert (xpart && ypart);
3606 need_clobber |= (GET_CODE (xpart) == SUBREG);
3608 last_insn = emit_move_insn (xpart, ypart);
3611 seq = get_insns ();
3612 end_sequence ();
3614 /* Show the output dies here. This is necessary for SUBREGs
3615 of pseudos since we cannot track their lifetimes correctly;
3616 hard regs shouldn't appear here except as return values.
3617 We never want to emit such a clobber after reload. */
3618 if (x != y
3619 && ! (reload_in_progress || reload_completed)
3620 && need_clobber != 0)
3621 emit_clobber (x);
3623 emit_insn (seq);
3625 return last_insn;
3628 /* Low level part of emit_move_insn.
3629 Called just like emit_move_insn, but assumes X and Y
3630 are basically valid. */
3632 rtx_insn *
3633 emit_move_insn_1 (rtx x, rtx y)
3635 machine_mode mode = GET_MODE (x);
3636 enum insn_code code;
3638 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3640 code = optab_handler (mov_optab, mode);
3641 if (code != CODE_FOR_nothing)
3642 return emit_insn (GEN_FCN (code) (x, y));
3644 /* Expand complex moves by moving real part and imag part. */
3645 if (COMPLEX_MODE_P (mode))
3646 return emit_move_complex (mode, x, y);
3648 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3649 || ALL_FIXED_POINT_MODE_P (mode))
3651 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3653 /* If we can't find an integer mode, use multi words. */
3654 if (result)
3655 return result;
3656 else
3657 return emit_move_multi_word (mode, x, y);
3660 if (GET_MODE_CLASS (mode) == MODE_CC)
3661 return emit_move_ccmode (mode, x, y);
3663 /* Try using a move pattern for the corresponding integer mode. This is
3664 only safe when simplify_subreg can convert MODE constants into integer
3665 constants. At present, it can only do this reliably if the value
3666 fits within a HOST_WIDE_INT. */
3667 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3669 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3671 if (ret)
3673 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3674 return ret;
3678 return emit_move_multi_word (mode, x, y);
3681 /* Generate code to copy Y into X.
3682 Both Y and X must have the same mode, except that
3683 Y can be a constant with VOIDmode.
3684 This mode cannot be BLKmode; use emit_block_move for that.
3686 Return the last instruction emitted. */
3688 rtx_insn *
3689 emit_move_insn (rtx x, rtx y)
3691 machine_mode mode = GET_MODE (x);
3692 rtx y_cst = NULL_RTX;
3693 rtx_insn *last_insn;
3694 rtx set;
3696 gcc_assert (mode != BLKmode
3697 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3699 if (CONSTANT_P (y))
3701 if (optimize
3702 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3703 && (last_insn = compress_float_constant (x, y)))
3704 return last_insn;
3706 y_cst = y;
3708 if (!targetm.legitimate_constant_p (mode, y))
3710 y = force_const_mem (mode, y);
3712 /* If the target's cannot_force_const_mem prevented the spill,
3713 assume that the target's move expanders will also take care
3714 of the non-legitimate constant. */
3715 if (!y)
3716 y = y_cst;
3717 else
3718 y = use_anchored_address (y);
3722 /* If X or Y are memory references, verify that their addresses are valid
3723 for the machine. */
3724 if (MEM_P (x)
3725 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3726 MEM_ADDR_SPACE (x))
3727 && ! push_operand (x, GET_MODE (x))))
3728 x = validize_mem (x);
3730 if (MEM_P (y)
3731 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3732 MEM_ADDR_SPACE (y)))
3733 y = validize_mem (y);
3735 gcc_assert (mode != BLKmode);
3737 last_insn = emit_move_insn_1 (x, y);
3739 if (y_cst && REG_P (x)
3740 && (set = single_set (last_insn)) != NULL_RTX
3741 && SET_DEST (set) == x
3742 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3743 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3745 return last_insn;
3748 /* Generate the body of an instruction to copy Y into X.
3749 It may be a list of insns, if one insn isn't enough. */
3751 rtx_insn *
3752 gen_move_insn (rtx x, rtx y)
3754 rtx_insn *seq;
3756 start_sequence ();
3757 emit_move_insn_1 (x, y);
3758 seq = get_insns ();
3759 end_sequence ();
3760 return seq;
3763 /* If Y is representable exactly in a narrower mode, and the target can
3764 perform the extension directly from constant or memory, then emit the
3765 move as an extension. */
3767 static rtx_insn *
3768 compress_float_constant (rtx x, rtx y)
3770 machine_mode dstmode = GET_MODE (x);
3771 machine_mode orig_srcmode = GET_MODE (y);
3772 machine_mode srcmode;
3773 const REAL_VALUE_TYPE *r;
3774 int oldcost, newcost;
3775 bool speed = optimize_insn_for_speed_p ();
3777 r = CONST_DOUBLE_REAL_VALUE (y);
3779 if (targetm.legitimate_constant_p (dstmode, y))
3780 oldcost = set_src_cost (y, orig_srcmode, speed);
3781 else
3782 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3784 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3785 srcmode != orig_srcmode;
3786 srcmode = GET_MODE_WIDER_MODE (srcmode))
3788 enum insn_code ic;
3789 rtx trunc_y;
3790 rtx_insn *last_insn;
3792 /* Skip if the target can't extend this way. */
3793 ic = can_extend_p (dstmode, srcmode, 0);
3794 if (ic == CODE_FOR_nothing)
3795 continue;
3797 /* Skip if the narrowed value isn't exact. */
3798 if (! exact_real_truncate (srcmode, r))
3799 continue;
3801 trunc_y = const_double_from_real_value (*r, srcmode);
3803 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3805 /* Skip if the target needs extra instructions to perform
3806 the extension. */
3807 if (!insn_operand_matches (ic, 1, trunc_y))
3808 continue;
3809 /* This is valid, but may not be cheaper than the original. */
3810 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3811 dstmode, speed);
3812 if (oldcost < newcost)
3813 continue;
3815 else if (float_extend_from_mem[dstmode][srcmode])
3817 trunc_y = force_const_mem (srcmode, trunc_y);
3818 /* This is valid, but may not be cheaper than the original. */
3819 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3820 dstmode, speed);
3821 if (oldcost < newcost)
3822 continue;
3823 trunc_y = validize_mem (trunc_y);
3825 else
3826 continue;
3828 /* For CSE's benefit, force the compressed constant pool entry
3829 into a new pseudo. This constant may be used in different modes,
3830 and if not, combine will put things back together for us. */
3831 trunc_y = force_reg (srcmode, trunc_y);
3833 /* If x is a hard register, perform the extension into a pseudo,
3834 so that e.g. stack realignment code is aware of it. */
3835 rtx target = x;
3836 if (REG_P (x) && HARD_REGISTER_P (x))
3837 target = gen_reg_rtx (dstmode);
3839 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3840 last_insn = get_last_insn ();
3842 if (REG_P (target))
3843 set_unique_reg_note (last_insn, REG_EQUAL, y);
3845 if (target != x)
3846 return emit_move_insn (x, target);
3847 return last_insn;
3850 return NULL;
3853 /* Pushing data onto the stack. */
3855 /* Push a block of length SIZE (perhaps variable)
3856 and return an rtx to address the beginning of the block.
3857 The value may be virtual_outgoing_args_rtx.
3859 EXTRA is the number of bytes of padding to push in addition to SIZE.
3860 BELOW nonzero means this padding comes at low addresses;
3861 otherwise, the padding comes at high addresses. */
3864 push_block (rtx size, int extra, int below)
3866 rtx temp;
3868 size = convert_modes (Pmode, ptr_mode, size, 1);
3869 if (CONSTANT_P (size))
3870 anti_adjust_stack (plus_constant (Pmode, size, extra));
3871 else if (REG_P (size) && extra == 0)
3872 anti_adjust_stack (size);
3873 else
3875 temp = copy_to_mode_reg (Pmode, size);
3876 if (extra != 0)
3877 temp = expand_binop (Pmode, add_optab, temp,
3878 gen_int_mode (extra, Pmode),
3879 temp, 0, OPTAB_LIB_WIDEN);
3880 anti_adjust_stack (temp);
3883 if (STACK_GROWS_DOWNWARD)
3885 temp = virtual_outgoing_args_rtx;
3886 if (extra != 0 && below)
3887 temp = plus_constant (Pmode, temp, extra);
3889 else
3891 if (CONST_INT_P (size))
3892 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3893 -INTVAL (size) - (below ? 0 : extra));
3894 else if (extra != 0 && !below)
3895 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3896 negate_rtx (Pmode, plus_constant (Pmode, size,
3897 extra)));
3898 else
3899 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3900 negate_rtx (Pmode, size));
3903 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3906 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3908 static rtx
3909 mem_autoinc_base (rtx mem)
3911 if (MEM_P (mem))
3913 rtx addr = XEXP (mem, 0);
3914 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3915 return XEXP (addr, 0);
3917 return NULL;
3920 /* A utility routine used here, in reload, and in try_split. The insns
3921 after PREV up to and including LAST are known to adjust the stack,
3922 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3923 placing notes as appropriate. PREV may be NULL, indicating the
3924 entire insn sequence prior to LAST should be scanned.
3926 The set of allowed stack pointer modifications is small:
3927 (1) One or more auto-inc style memory references (aka pushes),
3928 (2) One or more addition/subtraction with the SP as destination,
3929 (3) A single move insn with the SP as destination,
3930 (4) A call_pop insn,
3931 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3933 Insns in the sequence that do not modify the SP are ignored,
3934 except for noreturn calls.
3936 The return value is the amount of adjustment that can be trivially
3937 verified, via immediate operand or auto-inc. If the adjustment
3938 cannot be trivially extracted, the return value is INT_MIN. */
3940 HOST_WIDE_INT
3941 find_args_size_adjust (rtx_insn *insn)
3943 rtx dest, set, pat;
3944 int i;
3946 pat = PATTERN (insn);
3947 set = NULL;
3949 /* Look for a call_pop pattern. */
3950 if (CALL_P (insn))
3952 /* We have to allow non-call_pop patterns for the case
3953 of emit_single_push_insn of a TLS address. */
3954 if (GET_CODE (pat) != PARALLEL)
3955 return 0;
3957 /* All call_pop have a stack pointer adjust in the parallel.
3958 The call itself is always first, and the stack adjust is
3959 usually last, so search from the end. */
3960 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3962 set = XVECEXP (pat, 0, i);
3963 if (GET_CODE (set) != SET)
3964 continue;
3965 dest = SET_DEST (set);
3966 if (dest == stack_pointer_rtx)
3967 break;
3969 /* We'd better have found the stack pointer adjust. */
3970 if (i == 0)
3971 return 0;
3972 /* Fall through to process the extracted SET and DEST
3973 as if it was a standalone insn. */
3975 else if (GET_CODE (pat) == SET)
3976 set = pat;
3977 else if ((set = single_set (insn)) != NULL)
3979 else if (GET_CODE (pat) == PARALLEL)
3981 /* ??? Some older ports use a parallel with a stack adjust
3982 and a store for a PUSH_ROUNDING pattern, rather than a
3983 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3984 /* ??? See h8300 and m68k, pushqi1. */
3985 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3987 set = XVECEXP (pat, 0, i);
3988 if (GET_CODE (set) != SET)
3989 continue;
3990 dest = SET_DEST (set);
3991 if (dest == stack_pointer_rtx)
3992 break;
3994 /* We do not expect an auto-inc of the sp in the parallel. */
3995 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3996 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3997 != stack_pointer_rtx);
3999 if (i < 0)
4000 return 0;
4002 else
4003 return 0;
4005 dest = SET_DEST (set);
4007 /* Look for direct modifications of the stack pointer. */
4008 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4010 /* Look for a trivial adjustment, otherwise assume nothing. */
4011 /* Note that the SPU restore_stack_block pattern refers to
4012 the stack pointer in V4SImode. Consider that non-trivial. */
4013 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4014 && GET_CODE (SET_SRC (set)) == PLUS
4015 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4016 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4017 return INTVAL (XEXP (SET_SRC (set), 1));
4018 /* ??? Reload can generate no-op moves, which will be cleaned
4019 up later. Recognize it and continue searching. */
4020 else if (rtx_equal_p (dest, SET_SRC (set)))
4021 return 0;
4022 else
4023 return HOST_WIDE_INT_MIN;
4025 else
4027 rtx mem, addr;
4029 /* Otherwise only think about autoinc patterns. */
4030 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4032 mem = dest;
4033 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4034 != stack_pointer_rtx);
4036 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4037 mem = SET_SRC (set);
4038 else
4039 return 0;
4041 addr = XEXP (mem, 0);
4042 switch (GET_CODE (addr))
4044 case PRE_INC:
4045 case POST_INC:
4046 return GET_MODE_SIZE (GET_MODE (mem));
4047 case PRE_DEC:
4048 case POST_DEC:
4049 return -GET_MODE_SIZE (GET_MODE (mem));
4050 case PRE_MODIFY:
4051 case POST_MODIFY:
4052 addr = XEXP (addr, 1);
4053 gcc_assert (GET_CODE (addr) == PLUS);
4054 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4055 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4056 return INTVAL (XEXP (addr, 1));
4057 default:
4058 gcc_unreachable ();
4064 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4066 int args_size = end_args_size;
4067 bool saw_unknown = false;
4068 rtx_insn *insn;
4070 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4072 HOST_WIDE_INT this_delta;
4074 if (!NONDEBUG_INSN_P (insn))
4075 continue;
4077 this_delta = find_args_size_adjust (insn);
4078 if (this_delta == 0)
4080 if (!CALL_P (insn)
4081 || ACCUMULATE_OUTGOING_ARGS
4082 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4083 continue;
4086 gcc_assert (!saw_unknown);
4087 if (this_delta == HOST_WIDE_INT_MIN)
4088 saw_unknown = true;
4090 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4091 if (STACK_GROWS_DOWNWARD)
4092 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4094 args_size -= this_delta;
4097 return saw_unknown ? INT_MIN : args_size;
4100 #ifdef PUSH_ROUNDING
4101 /* Emit single push insn. */
4103 static void
4104 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4106 rtx dest_addr;
4107 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4108 rtx dest;
4109 enum insn_code icode;
4111 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4112 /* If there is push pattern, use it. Otherwise try old way of throwing
4113 MEM representing push operation to move expander. */
4114 icode = optab_handler (push_optab, mode);
4115 if (icode != CODE_FOR_nothing)
4117 struct expand_operand ops[1];
4119 create_input_operand (&ops[0], x, mode);
4120 if (maybe_expand_insn (icode, 1, ops))
4121 return;
4123 if (GET_MODE_SIZE (mode) == rounded_size)
4124 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4125 /* If we are to pad downward, adjust the stack pointer first and
4126 then store X into the stack location using an offset. This is
4127 because emit_move_insn does not know how to pad; it does not have
4128 access to type. */
4129 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4131 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4132 HOST_WIDE_INT offset;
4134 emit_move_insn (stack_pointer_rtx,
4135 expand_binop (Pmode,
4136 STACK_GROWS_DOWNWARD ? sub_optab
4137 : add_optab,
4138 stack_pointer_rtx,
4139 gen_int_mode (rounded_size, Pmode),
4140 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4142 offset = (HOST_WIDE_INT) padding_size;
4143 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4144 /* We have already decremented the stack pointer, so get the
4145 previous value. */
4146 offset += (HOST_WIDE_INT) rounded_size;
4148 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4149 /* We have already incremented the stack pointer, so get the
4150 previous value. */
4151 offset -= (HOST_WIDE_INT) rounded_size;
4153 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4154 gen_int_mode (offset, Pmode));
4156 else
4158 if (STACK_GROWS_DOWNWARD)
4159 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4160 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4161 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4162 Pmode));
4163 else
4164 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4165 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4166 gen_int_mode (rounded_size, Pmode));
4168 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4171 dest = gen_rtx_MEM (mode, dest_addr);
4173 if (type != 0)
4175 set_mem_attributes (dest, type, 1);
4177 if (cfun->tail_call_marked)
4178 /* Function incoming arguments may overlap with sibling call
4179 outgoing arguments and we cannot allow reordering of reads
4180 from function arguments with stores to outgoing arguments
4181 of sibling calls. */
4182 set_mem_alias_set (dest, 0);
4184 emit_move_insn (dest, x);
4187 /* Emit and annotate a single push insn. */
4189 static void
4190 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4192 int delta, old_delta = stack_pointer_delta;
4193 rtx_insn *prev = get_last_insn ();
4194 rtx_insn *last;
4196 emit_single_push_insn_1 (mode, x, type);
4198 last = get_last_insn ();
4200 /* Notice the common case where we emitted exactly one insn. */
4201 if (PREV_INSN (last) == prev)
4203 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4204 return;
4207 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4208 gcc_assert (delta == INT_MIN || delta == old_delta);
4210 #endif
4212 /* If reading SIZE bytes from X will end up reading from
4213 Y return the number of bytes that overlap. Return -1
4214 if there is no overlap or -2 if we can't determine
4215 (for example when X and Y have different base registers). */
4217 static int
4218 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4220 rtx tmp = plus_constant (Pmode, x, size);
4221 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4223 if (!CONST_INT_P (sub))
4224 return -2;
4226 HOST_WIDE_INT val = INTVAL (sub);
4228 return IN_RANGE (val, 1, size) ? val : -1;
4231 /* Generate code to push X onto the stack, assuming it has mode MODE and
4232 type TYPE.
4233 MODE is redundant except when X is a CONST_INT (since they don't
4234 carry mode info).
4235 SIZE is an rtx for the size of data to be copied (in bytes),
4236 needed only if X is BLKmode.
4237 Return true if successful. May return false if asked to push a
4238 partial argument during a sibcall optimization (as specified by
4239 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4240 to not overlap.
4242 ALIGN (in bits) is maximum alignment we can assume.
4244 If PARTIAL and REG are both nonzero, then copy that many of the first
4245 bytes of X into registers starting with REG, and push the rest of X.
4246 The amount of space pushed is decreased by PARTIAL bytes.
4247 REG must be a hard register in this case.
4248 If REG is zero but PARTIAL is not, take any all others actions for an
4249 argument partially in registers, but do not actually load any
4250 registers.
4252 EXTRA is the amount in bytes of extra space to leave next to this arg.
4253 This is ignored if an argument block has already been allocated.
4255 On a machine that lacks real push insns, ARGS_ADDR is the address of
4256 the bottom of the argument block for this call. We use indexing off there
4257 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4258 argument block has not been preallocated.
4260 ARGS_SO_FAR is the size of args previously pushed for this call.
4262 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4263 for arguments passed in registers. If nonzero, it will be the number
4264 of bytes required. */
4266 bool
4267 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4268 unsigned int align, int partial, rtx reg, int extra,
4269 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4270 rtx alignment_pad, bool sibcall_p)
4272 rtx xinner;
4273 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4275 /* Decide where to pad the argument: `downward' for below,
4276 `upward' for above, or `none' for don't pad it.
4277 Default is below for small data on big-endian machines; else above. */
4278 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4280 /* Invert direction if stack is post-decrement.
4281 FIXME: why? */
4282 if (STACK_PUSH_CODE == POST_DEC)
4283 if (where_pad != none)
4284 where_pad = (where_pad == downward ? upward : downward);
4286 xinner = x;
4288 int nregs = partial / UNITS_PER_WORD;
4289 rtx *tmp_regs = NULL;
4290 int overlapping = 0;
4292 if (mode == BLKmode
4293 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4295 /* Copy a block into the stack, entirely or partially. */
4297 rtx temp;
4298 int used;
4299 int offset;
4300 int skip;
4302 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4303 used = partial - offset;
4305 if (mode != BLKmode)
4307 /* A value is to be stored in an insufficiently aligned
4308 stack slot; copy via a suitably aligned slot if
4309 necessary. */
4310 size = GEN_INT (GET_MODE_SIZE (mode));
4311 if (!MEM_P (xinner))
4313 temp = assign_temp (type, 1, 1);
4314 emit_move_insn (temp, xinner);
4315 xinner = temp;
4319 gcc_assert (size);
4321 /* USED is now the # of bytes we need not copy to the stack
4322 because registers will take care of them. */
4324 if (partial != 0)
4325 xinner = adjust_address (xinner, BLKmode, used);
4327 /* If the partial register-part of the arg counts in its stack size,
4328 skip the part of stack space corresponding to the registers.
4329 Otherwise, start copying to the beginning of the stack space,
4330 by setting SKIP to 0. */
4331 skip = (reg_parm_stack_space == 0) ? 0 : used;
4333 #ifdef PUSH_ROUNDING
4334 /* Do it with several push insns if that doesn't take lots of insns
4335 and if there is no difficulty with push insns that skip bytes
4336 on the stack for alignment purposes. */
4337 if (args_addr == 0
4338 && PUSH_ARGS
4339 && CONST_INT_P (size)
4340 && skip == 0
4341 && MEM_ALIGN (xinner) >= align
4342 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4343 /* Here we avoid the case of a structure whose weak alignment
4344 forces many pushes of a small amount of data,
4345 and such small pushes do rounding that causes trouble. */
4346 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4347 || align >= BIGGEST_ALIGNMENT
4348 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4349 == (align / BITS_PER_UNIT)))
4350 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4352 /* Push padding now if padding above and stack grows down,
4353 or if padding below and stack grows up.
4354 But if space already allocated, this has already been done. */
4355 if (extra && args_addr == 0
4356 && where_pad != none && where_pad != stack_direction)
4357 anti_adjust_stack (GEN_INT (extra));
4359 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4361 else
4362 #endif /* PUSH_ROUNDING */
4364 rtx target;
4366 /* Otherwise make space on the stack and copy the data
4367 to the address of that space. */
4369 /* Deduct words put into registers from the size we must copy. */
4370 if (partial != 0)
4372 if (CONST_INT_P (size))
4373 size = GEN_INT (INTVAL (size) - used);
4374 else
4375 size = expand_binop (GET_MODE (size), sub_optab, size,
4376 gen_int_mode (used, GET_MODE (size)),
4377 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4380 /* Get the address of the stack space.
4381 In this case, we do not deal with EXTRA separately.
4382 A single stack adjust will do. */
4383 if (! args_addr)
4385 temp = push_block (size, extra, where_pad == downward);
4386 extra = 0;
4388 else if (CONST_INT_P (args_so_far))
4389 temp = memory_address (BLKmode,
4390 plus_constant (Pmode, args_addr,
4391 skip + INTVAL (args_so_far)));
4392 else
4393 temp = memory_address (BLKmode,
4394 plus_constant (Pmode,
4395 gen_rtx_PLUS (Pmode,
4396 args_addr,
4397 args_so_far),
4398 skip));
4400 if (!ACCUMULATE_OUTGOING_ARGS)
4402 /* If the source is referenced relative to the stack pointer,
4403 copy it to another register to stabilize it. We do not need
4404 to do this if we know that we won't be changing sp. */
4406 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4407 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4408 temp = copy_to_reg (temp);
4411 target = gen_rtx_MEM (BLKmode, temp);
4413 /* We do *not* set_mem_attributes here, because incoming arguments
4414 may overlap with sibling call outgoing arguments and we cannot
4415 allow reordering of reads from function arguments with stores
4416 to outgoing arguments of sibling calls. We do, however, want
4417 to record the alignment of the stack slot. */
4418 /* ALIGN may well be better aligned than TYPE, e.g. due to
4419 PARM_BOUNDARY. Assume the caller isn't lying. */
4420 set_mem_align (target, align);
4422 /* If part should go in registers and pushing to that part would
4423 overwrite some of the values that need to go into regs, load the
4424 overlapping values into temporary pseudos to be moved into the hard
4425 regs at the end after the stack pushing has completed.
4426 We cannot load them directly into the hard regs here because
4427 they can be clobbered by the block move expansions.
4428 See PR 65358. */
4430 if (partial > 0 && reg != 0 && mode == BLKmode
4431 && GET_CODE (reg) != PARALLEL)
4433 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4434 if (overlapping > 0)
4436 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4437 overlapping /= UNITS_PER_WORD;
4439 tmp_regs = XALLOCAVEC (rtx, overlapping);
4441 for (int i = 0; i < overlapping; i++)
4442 tmp_regs[i] = gen_reg_rtx (word_mode);
4444 for (int i = 0; i < overlapping; i++)
4445 emit_move_insn (tmp_regs[i],
4446 operand_subword_force (target, i, mode));
4448 else if (overlapping == -1)
4449 overlapping = 0;
4450 /* Could not determine whether there is overlap.
4451 Fail the sibcall. */
4452 else
4454 overlapping = 0;
4455 if (sibcall_p)
4456 return false;
4459 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4462 else if (partial > 0)
4464 /* Scalar partly in registers. */
4466 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4467 int i;
4468 int not_stack;
4469 /* # bytes of start of argument
4470 that we must make space for but need not store. */
4471 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4472 int args_offset = INTVAL (args_so_far);
4473 int skip;
4475 /* Push padding now if padding above and stack grows down,
4476 or if padding below and stack grows up.
4477 But if space already allocated, this has already been done. */
4478 if (extra && args_addr == 0
4479 && where_pad != none && where_pad != stack_direction)
4480 anti_adjust_stack (GEN_INT (extra));
4482 /* If we make space by pushing it, we might as well push
4483 the real data. Otherwise, we can leave OFFSET nonzero
4484 and leave the space uninitialized. */
4485 if (args_addr == 0)
4486 offset = 0;
4488 /* Now NOT_STACK gets the number of words that we don't need to
4489 allocate on the stack. Convert OFFSET to words too. */
4490 not_stack = (partial - offset) / UNITS_PER_WORD;
4491 offset /= UNITS_PER_WORD;
4493 /* If the partial register-part of the arg counts in its stack size,
4494 skip the part of stack space corresponding to the registers.
4495 Otherwise, start copying to the beginning of the stack space,
4496 by setting SKIP to 0. */
4497 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4499 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4500 x = validize_mem (force_const_mem (mode, x));
4502 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4503 SUBREGs of such registers are not allowed. */
4504 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4505 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4506 x = copy_to_reg (x);
4508 /* Loop over all the words allocated on the stack for this arg. */
4509 /* We can do it by words, because any scalar bigger than a word
4510 has a size a multiple of a word. */
4511 for (i = size - 1; i >= not_stack; i--)
4512 if (i >= not_stack + offset)
4513 if (!emit_push_insn (operand_subword_force (x, i, mode),
4514 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4515 0, args_addr,
4516 GEN_INT (args_offset + ((i - not_stack + skip)
4517 * UNITS_PER_WORD)),
4518 reg_parm_stack_space, alignment_pad, sibcall_p))
4519 return false;
4521 else
4523 rtx addr;
4524 rtx dest;
4526 /* Push padding now if padding above and stack grows down,
4527 or if padding below and stack grows up.
4528 But if space already allocated, this has already been done. */
4529 if (extra && args_addr == 0
4530 && where_pad != none && where_pad != stack_direction)
4531 anti_adjust_stack (GEN_INT (extra));
4533 #ifdef PUSH_ROUNDING
4534 if (args_addr == 0 && PUSH_ARGS)
4535 emit_single_push_insn (mode, x, type);
4536 else
4537 #endif
4539 if (CONST_INT_P (args_so_far))
4540 addr
4541 = memory_address (mode,
4542 plus_constant (Pmode, args_addr,
4543 INTVAL (args_so_far)));
4544 else
4545 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4546 args_so_far));
4547 dest = gen_rtx_MEM (mode, addr);
4549 /* We do *not* set_mem_attributes here, because incoming arguments
4550 may overlap with sibling call outgoing arguments and we cannot
4551 allow reordering of reads from function arguments with stores
4552 to outgoing arguments of sibling calls. We do, however, want
4553 to record the alignment of the stack slot. */
4554 /* ALIGN may well be better aligned than TYPE, e.g. due to
4555 PARM_BOUNDARY. Assume the caller isn't lying. */
4556 set_mem_align (dest, align);
4558 emit_move_insn (dest, x);
4562 /* Move the partial arguments into the registers and any overlapping
4563 values that we moved into the pseudos in tmp_regs. */
4564 if (partial > 0 && reg != 0)
4566 /* Handle calls that pass values in multiple non-contiguous locations.
4567 The Irix 6 ABI has examples of this. */
4568 if (GET_CODE (reg) == PARALLEL)
4569 emit_group_load (reg, x, type, -1);
4570 else
4572 gcc_assert (partial % UNITS_PER_WORD == 0);
4573 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4575 for (int i = 0; i < overlapping; i++)
4576 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4577 + nregs - overlapping + i),
4578 tmp_regs[i]);
4583 if (extra && args_addr == 0 && where_pad == stack_direction)
4584 anti_adjust_stack (GEN_INT (extra));
4586 if (alignment_pad && args_addr == 0)
4587 anti_adjust_stack (alignment_pad);
4589 return true;
4592 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4593 operations. */
4595 static rtx
4596 get_subtarget (rtx x)
4598 return (optimize
4599 || x == 0
4600 /* Only registers can be subtargets. */
4601 || !REG_P (x)
4602 /* Don't use hard regs to avoid extending their life. */
4603 || REGNO (x) < FIRST_PSEUDO_REGISTER
4604 ? 0 : x);
4607 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4608 FIELD is a bitfield. Returns true if the optimization was successful,
4609 and there's nothing else to do. */
4611 static bool
4612 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4613 unsigned HOST_WIDE_INT bitpos,
4614 unsigned HOST_WIDE_INT bitregion_start,
4615 unsigned HOST_WIDE_INT bitregion_end,
4616 machine_mode mode1, rtx str_rtx,
4617 tree to, tree src, bool reverse)
4619 machine_mode str_mode = GET_MODE (str_rtx);
4620 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4621 tree op0, op1;
4622 rtx value, result;
4623 optab binop;
4624 gimple *srcstmt;
4625 enum tree_code code;
4627 if (mode1 != VOIDmode
4628 || bitsize >= BITS_PER_WORD
4629 || str_bitsize > BITS_PER_WORD
4630 || TREE_SIDE_EFFECTS (to)
4631 || TREE_THIS_VOLATILE (to))
4632 return false;
4634 STRIP_NOPS (src);
4635 if (TREE_CODE (src) != SSA_NAME)
4636 return false;
4637 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4638 return false;
4640 srcstmt = get_gimple_for_ssa_name (src);
4641 if (!srcstmt
4642 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4643 return false;
4645 code = gimple_assign_rhs_code (srcstmt);
4647 op0 = gimple_assign_rhs1 (srcstmt);
4649 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4650 to find its initialization. Hopefully the initialization will
4651 be from a bitfield load. */
4652 if (TREE_CODE (op0) == SSA_NAME)
4654 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4656 /* We want to eventually have OP0 be the same as TO, which
4657 should be a bitfield. */
4658 if (!op0stmt
4659 || !is_gimple_assign (op0stmt)
4660 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4661 return false;
4662 op0 = gimple_assign_rhs1 (op0stmt);
4665 op1 = gimple_assign_rhs2 (srcstmt);
4667 if (!operand_equal_p (to, op0, 0))
4668 return false;
4670 if (MEM_P (str_rtx))
4672 unsigned HOST_WIDE_INT offset1;
4674 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4675 str_mode = word_mode;
4676 str_mode = get_best_mode (bitsize, bitpos,
4677 bitregion_start, bitregion_end,
4678 MEM_ALIGN (str_rtx), str_mode, 0);
4679 if (str_mode == VOIDmode)
4680 return false;
4681 str_bitsize = GET_MODE_BITSIZE (str_mode);
4683 offset1 = bitpos;
4684 bitpos %= str_bitsize;
4685 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4686 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4688 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4689 return false;
4690 else
4691 gcc_assert (!reverse);
4693 /* If the bit field covers the whole REG/MEM, store_field
4694 will likely generate better code. */
4695 if (bitsize >= str_bitsize)
4696 return false;
4698 /* We can't handle fields split across multiple entities. */
4699 if (bitpos + bitsize > str_bitsize)
4700 return false;
4702 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4703 bitpos = str_bitsize - bitpos - bitsize;
4705 switch (code)
4707 case PLUS_EXPR:
4708 case MINUS_EXPR:
4709 /* For now, just optimize the case of the topmost bitfield
4710 where we don't need to do any masking and also
4711 1 bit bitfields where xor can be used.
4712 We might win by one instruction for the other bitfields
4713 too if insv/extv instructions aren't used, so that
4714 can be added later. */
4715 if ((reverse || bitpos + bitsize != str_bitsize)
4716 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4717 break;
4719 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4720 value = convert_modes (str_mode,
4721 TYPE_MODE (TREE_TYPE (op1)), value,
4722 TYPE_UNSIGNED (TREE_TYPE (op1)));
4724 /* We may be accessing data outside the field, which means
4725 we can alias adjacent data. */
4726 if (MEM_P (str_rtx))
4728 str_rtx = shallow_copy_rtx (str_rtx);
4729 set_mem_alias_set (str_rtx, 0);
4730 set_mem_expr (str_rtx, 0);
4733 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4735 value = expand_and (str_mode, value, const1_rtx, NULL);
4736 binop = xor_optab;
4738 else
4739 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4741 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4742 if (reverse)
4743 value = flip_storage_order (str_mode, value);
4744 result = expand_binop (str_mode, binop, str_rtx,
4745 value, str_rtx, 1, OPTAB_WIDEN);
4746 if (result != str_rtx)
4747 emit_move_insn (str_rtx, result);
4748 return true;
4750 case BIT_IOR_EXPR:
4751 case BIT_XOR_EXPR:
4752 if (TREE_CODE (op1) != INTEGER_CST)
4753 break;
4754 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4755 value = convert_modes (str_mode,
4756 TYPE_MODE (TREE_TYPE (op1)), value,
4757 TYPE_UNSIGNED (TREE_TYPE (op1)));
4759 /* We may be accessing data outside the field, which means
4760 we can alias adjacent data. */
4761 if (MEM_P (str_rtx))
4763 str_rtx = shallow_copy_rtx (str_rtx);
4764 set_mem_alias_set (str_rtx, 0);
4765 set_mem_expr (str_rtx, 0);
4768 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4769 if (bitpos + bitsize != str_bitsize)
4771 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4772 str_mode);
4773 value = expand_and (str_mode, value, mask, NULL_RTX);
4775 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4776 if (reverse)
4777 value = flip_storage_order (str_mode, value);
4778 result = expand_binop (str_mode, binop, str_rtx,
4779 value, str_rtx, 1, OPTAB_WIDEN);
4780 if (result != str_rtx)
4781 emit_move_insn (str_rtx, result);
4782 return true;
4784 default:
4785 break;
4788 return false;
4791 /* In the C++ memory model, consecutive bit fields in a structure are
4792 considered one memory location.
4794 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4795 returns the bit range of consecutive bits in which this COMPONENT_REF
4796 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4797 and *OFFSET may be adjusted in the process.
4799 If the access does not need to be restricted, 0 is returned in both
4800 *BITSTART and *BITEND. */
4802 void
4803 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4804 unsigned HOST_WIDE_INT *bitend,
4805 tree exp,
4806 HOST_WIDE_INT *bitpos,
4807 tree *offset)
4809 HOST_WIDE_INT bitoffset;
4810 tree field, repr;
4812 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4814 field = TREE_OPERAND (exp, 1);
4815 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4816 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4817 need to limit the range we can access. */
4818 if (!repr)
4820 *bitstart = *bitend = 0;
4821 return;
4824 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4825 part of a larger bit field, then the representative does not serve any
4826 useful purpose. This can occur in Ada. */
4827 if (handled_component_p (TREE_OPERAND (exp, 0)))
4829 machine_mode rmode;
4830 HOST_WIDE_INT rbitsize, rbitpos;
4831 tree roffset;
4832 int unsignedp, reversep, volatilep = 0;
4833 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4834 &roffset, &rmode, &unsignedp, &reversep,
4835 &volatilep);
4836 if ((rbitpos % BITS_PER_UNIT) != 0)
4838 *bitstart = *bitend = 0;
4839 return;
4843 /* Compute the adjustment to bitpos from the offset of the field
4844 relative to the representative. DECL_FIELD_OFFSET of field and
4845 repr are the same by construction if they are not constants,
4846 see finish_bitfield_layout. */
4847 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4848 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4849 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4850 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4851 else
4852 bitoffset = 0;
4853 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4854 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4856 /* If the adjustment is larger than bitpos, we would have a negative bit
4857 position for the lower bound and this may wreak havoc later. Adjust
4858 offset and bitpos to make the lower bound non-negative in that case. */
4859 if (bitoffset > *bitpos)
4861 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4862 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4864 *bitpos += adjust;
4865 if (*offset == NULL_TREE)
4866 *offset = size_int (-adjust / BITS_PER_UNIT);
4867 else
4868 *offset
4869 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4870 *bitstart = 0;
4872 else
4873 *bitstart = *bitpos - bitoffset;
4875 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4878 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4879 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4880 DECL_RTL was not set yet, return NORTL. */
4882 static inline bool
4883 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4885 if (TREE_CODE (addr) != ADDR_EXPR)
4886 return false;
4888 tree base = TREE_OPERAND (addr, 0);
4890 if (!DECL_P (base)
4891 || TREE_ADDRESSABLE (base)
4892 || DECL_MODE (base) == BLKmode)
4893 return false;
4895 if (!DECL_RTL_SET_P (base))
4896 return nortl;
4898 return (!MEM_P (DECL_RTL (base)));
4901 /* Returns true if the MEM_REF REF refers to an object that does not
4902 reside in memory and has non-BLKmode. */
4904 static inline bool
4905 mem_ref_refers_to_non_mem_p (tree ref)
4907 tree base = TREE_OPERAND (ref, 0);
4908 return addr_expr_of_non_mem_decl_p_1 (base, false);
4911 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4912 is true, try generating a nontemporal store. */
4914 void
4915 expand_assignment (tree to, tree from, bool nontemporal)
4917 rtx to_rtx = 0;
4918 rtx result;
4919 machine_mode mode;
4920 unsigned int align;
4921 enum insn_code icode;
4923 /* Don't crash if the lhs of the assignment was erroneous. */
4924 if (TREE_CODE (to) == ERROR_MARK)
4926 expand_normal (from);
4927 return;
4930 /* Optimize away no-op moves without side-effects. */
4931 if (operand_equal_p (to, from, 0))
4932 return;
4934 /* Handle misaligned stores. */
4935 mode = TYPE_MODE (TREE_TYPE (to));
4936 if ((TREE_CODE (to) == MEM_REF
4937 || TREE_CODE (to) == TARGET_MEM_REF)
4938 && mode != BLKmode
4939 && !mem_ref_refers_to_non_mem_p (to)
4940 && ((align = get_object_alignment (to))
4941 < GET_MODE_ALIGNMENT (mode))
4942 && (((icode = optab_handler (movmisalign_optab, mode))
4943 != CODE_FOR_nothing)
4944 || SLOW_UNALIGNED_ACCESS (mode, align)))
4946 rtx reg, mem;
4948 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4949 reg = force_not_mem (reg);
4950 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4951 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4952 reg = flip_storage_order (mode, reg);
4954 if (icode != CODE_FOR_nothing)
4956 struct expand_operand ops[2];
4958 create_fixed_operand (&ops[0], mem);
4959 create_input_operand (&ops[1], reg, mode);
4960 /* The movmisalign<mode> pattern cannot fail, else the assignment
4961 would silently be omitted. */
4962 expand_insn (icode, 2, ops);
4964 else
4965 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4966 false);
4967 return;
4970 /* Assignment of a structure component needs special treatment
4971 if the structure component's rtx is not simply a MEM.
4972 Assignment of an array element at a constant index, and assignment of
4973 an array element in an unaligned packed structure field, has the same
4974 problem. Same for (partially) storing into a non-memory object. */
4975 if (handled_component_p (to)
4976 || (TREE_CODE (to) == MEM_REF
4977 && (REF_REVERSE_STORAGE_ORDER (to)
4978 || mem_ref_refers_to_non_mem_p (to)))
4979 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4981 machine_mode mode1;
4982 HOST_WIDE_INT bitsize, bitpos;
4983 unsigned HOST_WIDE_INT bitregion_start = 0;
4984 unsigned HOST_WIDE_INT bitregion_end = 0;
4985 tree offset;
4986 int unsignedp, reversep, volatilep = 0;
4987 tree tem;
4989 push_temp_slots ();
4990 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4991 &unsignedp, &reversep, &volatilep);
4993 /* Make sure bitpos is not negative, it can wreak havoc later. */
4994 if (bitpos < 0)
4996 gcc_assert (offset == NULL_TREE);
4997 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
4998 bitpos &= BITS_PER_UNIT - 1;
5001 if (TREE_CODE (to) == COMPONENT_REF
5002 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5003 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5004 /* The C++ memory model naturally applies to byte-aligned fields.
5005 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5006 BITSIZE are not byte-aligned, there is no need to limit the range
5007 we can access. This can occur with packed structures in Ada. */
5008 else if (bitsize > 0
5009 && bitsize % BITS_PER_UNIT == 0
5010 && bitpos % BITS_PER_UNIT == 0)
5012 bitregion_start = bitpos;
5013 bitregion_end = bitpos + bitsize - 1;
5016 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5018 /* If the field has a mode, we want to access it in the
5019 field's mode, not the computed mode.
5020 If a MEM has VOIDmode (external with incomplete type),
5021 use BLKmode for it instead. */
5022 if (MEM_P (to_rtx))
5024 if (mode1 != VOIDmode)
5025 to_rtx = adjust_address (to_rtx, mode1, 0);
5026 else if (GET_MODE (to_rtx) == VOIDmode)
5027 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5030 if (offset != 0)
5032 machine_mode address_mode;
5033 rtx offset_rtx;
5035 if (!MEM_P (to_rtx))
5037 /* We can get constant negative offsets into arrays with broken
5038 user code. Translate this to a trap instead of ICEing. */
5039 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5040 expand_builtin_trap ();
5041 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5044 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5045 address_mode = get_address_mode (to_rtx);
5046 if (GET_MODE (offset_rtx) != address_mode)
5048 /* We cannot be sure that the RTL in offset_rtx is valid outside
5049 of a memory address context, so force it into a register
5050 before attempting to convert it to the desired mode. */
5051 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5052 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5055 /* If we have an expression in OFFSET_RTX and a non-zero
5056 byte offset in BITPOS, adding the byte offset before the
5057 OFFSET_RTX results in better intermediate code, which makes
5058 later rtl optimization passes perform better.
5060 We prefer intermediate code like this:
5062 r124:DI=r123:DI+0x18
5063 [r124:DI]=r121:DI
5065 ... instead of ...
5067 r124:DI=r123:DI+0x10
5068 [r124:DI+0x8]=r121:DI
5070 This is only done for aligned data values, as these can
5071 be expected to result in single move instructions. */
5072 if (mode1 != VOIDmode
5073 && bitpos != 0
5074 && bitsize > 0
5075 && (bitpos % bitsize) == 0
5076 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5077 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5079 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5080 bitregion_start = 0;
5081 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5082 bitregion_end -= bitpos;
5083 bitpos = 0;
5086 to_rtx = offset_address (to_rtx, offset_rtx,
5087 highest_pow2_factor_for_target (to,
5088 offset));
5091 /* No action is needed if the target is not a memory and the field
5092 lies completely outside that target. This can occur if the source
5093 code contains an out-of-bounds access to a small array. */
5094 if (!MEM_P (to_rtx)
5095 && GET_MODE (to_rtx) != BLKmode
5096 && (unsigned HOST_WIDE_INT) bitpos
5097 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5099 expand_normal (from);
5100 result = NULL;
5102 /* Handle expand_expr of a complex value returning a CONCAT. */
5103 else if (GET_CODE (to_rtx) == CONCAT)
5105 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5106 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5107 && bitpos == 0
5108 && bitsize == mode_bitsize)
5109 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5110 else if (bitsize == mode_bitsize / 2
5111 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5112 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5113 nontemporal, reversep);
5114 else if (bitpos + bitsize <= mode_bitsize / 2)
5115 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5116 bitregion_start, bitregion_end,
5117 mode1, from, get_alias_set (to),
5118 nontemporal, reversep);
5119 else if (bitpos >= mode_bitsize / 2)
5120 result = store_field (XEXP (to_rtx, 1), bitsize,
5121 bitpos - mode_bitsize / 2,
5122 bitregion_start, bitregion_end,
5123 mode1, from, get_alias_set (to),
5124 nontemporal, reversep);
5125 else if (bitpos == 0 && bitsize == mode_bitsize)
5127 rtx from_rtx;
5128 result = expand_normal (from);
5129 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5130 TYPE_MODE (TREE_TYPE (from)), 0);
5131 emit_move_insn (XEXP (to_rtx, 0),
5132 read_complex_part (from_rtx, false));
5133 emit_move_insn (XEXP (to_rtx, 1),
5134 read_complex_part (from_rtx, true));
5136 else
5138 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5139 GET_MODE_SIZE (GET_MODE (to_rtx)));
5140 write_complex_part (temp, XEXP (to_rtx, 0), false);
5141 write_complex_part (temp, XEXP (to_rtx, 1), true);
5142 result = store_field (temp, bitsize, bitpos,
5143 bitregion_start, bitregion_end,
5144 mode1, from, get_alias_set (to),
5145 nontemporal, reversep);
5146 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5147 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5150 else
5152 if (MEM_P (to_rtx))
5154 /* If the field is at offset zero, we could have been given the
5155 DECL_RTX of the parent struct. Don't munge it. */
5156 to_rtx = shallow_copy_rtx (to_rtx);
5157 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5158 if (volatilep)
5159 MEM_VOLATILE_P (to_rtx) = 1;
5162 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5163 bitregion_start, bitregion_end,
5164 mode1, to_rtx, to, from,
5165 reversep))
5166 result = NULL;
5167 else
5168 result = store_field (to_rtx, bitsize, bitpos,
5169 bitregion_start, bitregion_end,
5170 mode1, from, get_alias_set (to),
5171 nontemporal, reversep);
5174 if (result)
5175 preserve_temp_slots (result);
5176 pop_temp_slots ();
5177 return;
5180 /* If the rhs is a function call and its value is not an aggregate,
5181 call the function before we start to compute the lhs.
5182 This is needed for correct code for cases such as
5183 val = setjmp (buf) on machines where reference to val
5184 requires loading up part of an address in a separate insn.
5186 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5187 since it might be a promoted variable where the zero- or sign- extension
5188 needs to be done. Handling this in the normal way is safe because no
5189 computation is done before the call. The same is true for SSA names. */
5190 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5191 && COMPLETE_TYPE_P (TREE_TYPE (from))
5192 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5193 && ! (((VAR_P (to)
5194 || TREE_CODE (to) == PARM_DECL
5195 || TREE_CODE (to) == RESULT_DECL)
5196 && REG_P (DECL_RTL (to)))
5197 || TREE_CODE (to) == SSA_NAME))
5199 rtx value;
5200 rtx bounds;
5202 push_temp_slots ();
5203 value = expand_normal (from);
5205 /* Split value and bounds to store them separately. */
5206 chkp_split_slot (value, &value, &bounds);
5208 if (to_rtx == 0)
5209 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5211 /* Handle calls that return values in multiple non-contiguous locations.
5212 The Irix 6 ABI has examples of this. */
5213 if (GET_CODE (to_rtx) == PARALLEL)
5215 if (GET_CODE (value) == PARALLEL)
5216 emit_group_move (to_rtx, value);
5217 else
5218 emit_group_load (to_rtx, value, TREE_TYPE (from),
5219 int_size_in_bytes (TREE_TYPE (from)));
5221 else if (GET_CODE (value) == PARALLEL)
5222 emit_group_store (to_rtx, value, TREE_TYPE (from),
5223 int_size_in_bytes (TREE_TYPE (from)));
5224 else if (GET_MODE (to_rtx) == BLKmode)
5226 /* Handle calls that return BLKmode values in registers. */
5227 if (REG_P (value))
5228 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5229 else
5230 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5232 else
5234 if (POINTER_TYPE_P (TREE_TYPE (to)))
5235 value = convert_memory_address_addr_space
5236 (GET_MODE (to_rtx), value,
5237 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5239 emit_move_insn (to_rtx, value);
5242 /* Store bounds if required. */
5243 if (bounds
5244 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5246 gcc_assert (MEM_P (to_rtx));
5247 chkp_emit_bounds_store (bounds, value, to_rtx);
5250 preserve_temp_slots (to_rtx);
5251 pop_temp_slots ();
5252 return;
5255 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5256 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5258 /* Don't move directly into a return register. */
5259 if (TREE_CODE (to) == RESULT_DECL
5260 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5262 rtx temp;
5264 push_temp_slots ();
5266 /* If the source is itself a return value, it still is in a pseudo at
5267 this point so we can move it back to the return register directly. */
5268 if (REG_P (to_rtx)
5269 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5270 && TREE_CODE (from) != CALL_EXPR)
5271 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5272 else
5273 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5275 /* Handle calls that return values in multiple non-contiguous locations.
5276 The Irix 6 ABI has examples of this. */
5277 if (GET_CODE (to_rtx) == PARALLEL)
5279 if (GET_CODE (temp) == PARALLEL)
5280 emit_group_move (to_rtx, temp);
5281 else
5282 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5283 int_size_in_bytes (TREE_TYPE (from)));
5285 else if (temp)
5286 emit_move_insn (to_rtx, temp);
5288 preserve_temp_slots (to_rtx);
5289 pop_temp_slots ();
5290 return;
5293 /* In case we are returning the contents of an object which overlaps
5294 the place the value is being stored, use a safe function when copying
5295 a value through a pointer into a structure value return block. */
5296 if (TREE_CODE (to) == RESULT_DECL
5297 && TREE_CODE (from) == INDIRECT_REF
5298 && ADDR_SPACE_GENERIC_P
5299 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5300 && refs_may_alias_p (to, from)
5301 && cfun->returns_struct
5302 && !cfun->returns_pcc_struct)
5304 rtx from_rtx, size;
5306 push_temp_slots ();
5307 size = expr_size (from);
5308 from_rtx = expand_normal (from);
5310 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5312 preserve_temp_slots (to_rtx);
5313 pop_temp_slots ();
5314 return;
5317 /* Compute FROM and store the value in the rtx we got. */
5319 push_temp_slots ();
5320 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5321 preserve_temp_slots (result);
5322 pop_temp_slots ();
5323 return;
5326 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5327 succeeded, false otherwise. */
5329 bool
5330 emit_storent_insn (rtx to, rtx from)
5332 struct expand_operand ops[2];
5333 machine_mode mode = GET_MODE (to);
5334 enum insn_code code = optab_handler (storent_optab, mode);
5336 if (code == CODE_FOR_nothing)
5337 return false;
5339 create_fixed_operand (&ops[0], to);
5340 create_input_operand (&ops[1], from, mode);
5341 return maybe_expand_insn (code, 2, ops);
5344 /* Generate code for computing expression EXP,
5345 and storing the value into TARGET.
5347 If the mode is BLKmode then we may return TARGET itself.
5348 It turns out that in BLKmode it doesn't cause a problem.
5349 because C has no operators that could combine two different
5350 assignments into the same BLKmode object with different values
5351 with no sequence point. Will other languages need this to
5352 be more thorough?
5354 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5355 stack, and block moves may need to be treated specially.
5357 If NONTEMPORAL is true, try using a nontemporal store instruction.
5359 If REVERSE is true, the store is to be done in reverse order.
5361 If BTARGET is not NULL then computed bounds of EXP are
5362 associated with BTARGET. */
5365 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5366 bool nontemporal, bool reverse, tree btarget)
5368 rtx temp;
5369 rtx alt_rtl = NULL_RTX;
5370 location_t loc = curr_insn_location ();
5372 if (VOID_TYPE_P (TREE_TYPE (exp)))
5374 /* C++ can generate ?: expressions with a throw expression in one
5375 branch and an rvalue in the other. Here, we resolve attempts to
5376 store the throw expression's nonexistent result. */
5377 gcc_assert (!call_param_p);
5378 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5379 return NULL_RTX;
5381 if (TREE_CODE (exp) == COMPOUND_EXPR)
5383 /* Perform first part of compound expression, then assign from second
5384 part. */
5385 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5386 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5387 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5388 call_param_p, nontemporal, reverse,
5389 btarget);
5391 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5393 /* For conditional expression, get safe form of the target. Then
5394 test the condition, doing the appropriate assignment on either
5395 side. This avoids the creation of unnecessary temporaries.
5396 For non-BLKmode, it is more efficient not to do this. */
5398 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5400 do_pending_stack_adjust ();
5401 NO_DEFER_POP;
5402 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
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, machine_mode mode,
6081 tree exp, int cleared,
6082 alias_set_type alias_set, bool reverse)
6084 if (TREE_CODE (exp) == CONSTRUCTOR
6085 /* We can only call store_constructor recursively if the size and
6086 bit position are on a byte boundary. */
6087 && bitpos % BITS_PER_UNIT == 0
6088 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6089 /* If we have a nonzero bitpos for a register target, then we just
6090 let store_field do the bitfield handling. This is unlikely to
6091 generate unnecessary clear instructions anyways. */
6092 && (bitpos == 0 || MEM_P (target)))
6094 if (MEM_P (target))
6095 target
6096 = adjust_address (target,
6097 GET_MODE (target) == BLKmode
6098 || 0 != (bitpos
6099 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6100 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6103 /* Update the alias set, if required. */
6104 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6105 && MEM_ALIAS_SET (target) != 0)
6107 target = copy_rtx (target);
6108 set_mem_alias_set (target, alias_set);
6111 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6112 reverse);
6114 else
6115 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false,
6116 reverse);
6120 /* Returns the number of FIELD_DECLs in TYPE. */
6122 static int
6123 fields_length (const_tree type)
6125 tree t = TYPE_FIELDS (type);
6126 int count = 0;
6128 for (; t; t = DECL_CHAIN (t))
6129 if (TREE_CODE (t) == FIELD_DECL)
6130 ++count;
6132 return count;
6136 /* Store the value of constructor EXP into the rtx TARGET.
6137 TARGET is either a REG or a MEM; we know it cannot conflict, since
6138 safe_from_p has been called.
6139 CLEARED is true if TARGET is known to have been zero'd.
6140 SIZE is the number of bytes of TARGET we are allowed to modify: this
6141 may not be the same as the size of EXP if we are assigning to a field
6142 which has been packed to exclude padding bits.
6143 If REVERSE is true, the store is to be done in reverse order. */
6145 static void
6146 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6147 bool reverse)
6149 tree type = TREE_TYPE (exp);
6150 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6152 switch (TREE_CODE (type))
6154 case RECORD_TYPE:
6155 case UNION_TYPE:
6156 case QUAL_UNION_TYPE:
6158 unsigned HOST_WIDE_INT idx;
6159 tree field, value;
6161 /* The storage order is specified for every aggregate type. */
6162 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6164 /* If size is zero or the target is already cleared, do nothing. */
6165 if (size == 0 || cleared)
6166 cleared = 1;
6167 /* We either clear the aggregate or indicate the value is dead. */
6168 else if ((TREE_CODE (type) == UNION_TYPE
6169 || TREE_CODE (type) == QUAL_UNION_TYPE)
6170 && ! CONSTRUCTOR_ELTS (exp))
6171 /* If the constructor is empty, clear the union. */
6173 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6174 cleared = 1;
6177 /* If we are building a static constructor into a register,
6178 set the initial value as zero so we can fold the value into
6179 a constant. But if more than one register is involved,
6180 this probably loses. */
6181 else if (REG_P (target) && TREE_STATIC (exp)
6182 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6184 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6185 cleared = 1;
6188 /* If the constructor has fewer fields than the structure or
6189 if we are initializing the structure to mostly zeros, clear
6190 the whole structure first. Don't do this if TARGET is a
6191 register whose mode size isn't equal to SIZE since
6192 clear_storage can't handle this case. */
6193 else if (size > 0
6194 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6195 || mostly_zeros_p (exp))
6196 && (!REG_P (target)
6197 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6198 == size)))
6200 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6201 cleared = 1;
6204 if (REG_P (target) && !cleared)
6205 emit_clobber (target);
6207 /* Store each element of the constructor into the
6208 corresponding field of TARGET. */
6209 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6211 machine_mode mode;
6212 HOST_WIDE_INT bitsize;
6213 HOST_WIDE_INT bitpos = 0;
6214 tree offset;
6215 rtx to_rtx = target;
6217 /* Just ignore missing fields. We cleared the whole
6218 structure, above, if any fields are missing. */
6219 if (field == 0)
6220 continue;
6222 if (cleared && initializer_zerop (value))
6223 continue;
6225 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6226 bitsize = tree_to_uhwi (DECL_SIZE (field));
6227 else
6228 bitsize = -1;
6230 mode = DECL_MODE (field);
6231 if (DECL_BIT_FIELD (field))
6232 mode = VOIDmode;
6234 offset = DECL_FIELD_OFFSET (field);
6235 if (tree_fits_shwi_p (offset)
6236 && tree_fits_shwi_p (bit_position (field)))
6238 bitpos = int_bit_position (field);
6239 offset = 0;
6241 else
6242 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6244 if (offset)
6246 machine_mode address_mode;
6247 rtx offset_rtx;
6249 offset
6250 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6251 make_tree (TREE_TYPE (exp),
6252 target));
6254 offset_rtx = expand_normal (offset);
6255 gcc_assert (MEM_P (to_rtx));
6257 address_mode = get_address_mode (to_rtx);
6258 if (GET_MODE (offset_rtx) != address_mode)
6259 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6261 to_rtx = offset_address (to_rtx, offset_rtx,
6262 highest_pow2_factor (offset));
6265 /* If this initializes a field that is smaller than a
6266 word, at the start of a word, try to widen it to a full
6267 word. This special case allows us to output C++ member
6268 function initializations in a form that the optimizers
6269 can understand. */
6270 if (WORD_REGISTER_OPERATIONS
6271 && REG_P (target)
6272 && bitsize < BITS_PER_WORD
6273 && bitpos % BITS_PER_WORD == 0
6274 && GET_MODE_CLASS (mode) == MODE_INT
6275 && TREE_CODE (value) == INTEGER_CST
6276 && exp_size >= 0
6277 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6279 tree type = TREE_TYPE (value);
6281 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6283 type = lang_hooks.types.type_for_mode
6284 (word_mode, TYPE_UNSIGNED (type));
6285 value = fold_convert (type, value);
6286 /* Make sure the bits beyond the original bitsize are zero
6287 so that we can correctly avoid extra zeroing stores in
6288 later constructor elements. */
6289 tree bitsize_mask
6290 = wide_int_to_tree (type, wi::mask (bitsize, false,
6291 BITS_PER_WORD));
6292 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6295 if (BYTES_BIG_ENDIAN)
6296 value
6297 = fold_build2 (LSHIFT_EXPR, type, value,
6298 build_int_cst (type,
6299 BITS_PER_WORD - bitsize));
6300 bitsize = BITS_PER_WORD;
6301 mode = word_mode;
6304 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6305 && DECL_NONADDRESSABLE_P (field))
6307 to_rtx = copy_rtx (to_rtx);
6308 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6311 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6312 value, cleared,
6313 get_alias_set (TREE_TYPE (field)),
6314 reverse);
6316 break;
6318 case ARRAY_TYPE:
6320 tree value, index;
6321 unsigned HOST_WIDE_INT i;
6322 int need_to_clear;
6323 tree domain;
6324 tree elttype = TREE_TYPE (type);
6325 int const_bounds_p;
6326 HOST_WIDE_INT minelt = 0;
6327 HOST_WIDE_INT maxelt = 0;
6329 /* The storage order is specified for every aggregate type. */
6330 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6332 domain = TYPE_DOMAIN (type);
6333 const_bounds_p = (TYPE_MIN_VALUE (domain)
6334 && TYPE_MAX_VALUE (domain)
6335 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6336 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6338 /* If we have constant bounds for the range of the type, get them. */
6339 if (const_bounds_p)
6341 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6342 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6345 /* If the constructor has fewer elements than the array, clear
6346 the whole array first. Similarly if this is static
6347 constructor of a non-BLKmode object. */
6348 if (cleared)
6349 need_to_clear = 0;
6350 else if (REG_P (target) && TREE_STATIC (exp))
6351 need_to_clear = 1;
6352 else
6354 unsigned HOST_WIDE_INT idx;
6355 tree index, value;
6356 HOST_WIDE_INT count = 0, zero_count = 0;
6357 need_to_clear = ! const_bounds_p;
6359 /* This loop is a more accurate version of the loop in
6360 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6361 is also needed to check for missing elements. */
6362 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6364 HOST_WIDE_INT this_node_count;
6366 if (need_to_clear)
6367 break;
6369 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6371 tree lo_index = TREE_OPERAND (index, 0);
6372 tree hi_index = TREE_OPERAND (index, 1);
6374 if (! tree_fits_uhwi_p (lo_index)
6375 || ! tree_fits_uhwi_p (hi_index))
6377 need_to_clear = 1;
6378 break;
6381 this_node_count = (tree_to_uhwi (hi_index)
6382 - tree_to_uhwi (lo_index) + 1);
6384 else
6385 this_node_count = 1;
6387 count += this_node_count;
6388 if (mostly_zeros_p (value))
6389 zero_count += this_node_count;
6392 /* Clear the entire array first if there are any missing
6393 elements, or if the incidence of zero elements is >=
6394 75%. */
6395 if (! need_to_clear
6396 && (count < maxelt - minelt + 1
6397 || 4 * zero_count >= 3 * count))
6398 need_to_clear = 1;
6401 if (need_to_clear && size > 0)
6403 if (REG_P (target))
6404 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6405 else
6406 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6407 cleared = 1;
6410 if (!cleared && REG_P (target))
6411 /* Inform later passes that the old value is dead. */
6412 emit_clobber (target);
6414 /* Store each element of the constructor into the
6415 corresponding element of TARGET, determined by counting the
6416 elements. */
6417 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6419 machine_mode mode;
6420 HOST_WIDE_INT bitsize;
6421 HOST_WIDE_INT bitpos;
6422 rtx xtarget = target;
6424 if (cleared && initializer_zerop (value))
6425 continue;
6427 mode = TYPE_MODE (elttype);
6428 if (mode == BLKmode)
6429 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6430 ? tree_to_uhwi (TYPE_SIZE (elttype))
6431 : -1);
6432 else
6433 bitsize = GET_MODE_BITSIZE (mode);
6435 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6437 tree lo_index = TREE_OPERAND (index, 0);
6438 tree hi_index = TREE_OPERAND (index, 1);
6439 rtx index_r, pos_rtx;
6440 HOST_WIDE_INT lo, hi, count;
6441 tree position;
6443 /* If the range is constant and "small", unroll the loop. */
6444 if (const_bounds_p
6445 && tree_fits_shwi_p (lo_index)
6446 && tree_fits_shwi_p (hi_index)
6447 && (lo = tree_to_shwi (lo_index),
6448 hi = tree_to_shwi (hi_index),
6449 count = hi - lo + 1,
6450 (!MEM_P (target)
6451 || count <= 2
6452 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6453 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6454 <= 40 * 8)))))
6456 lo -= minelt; hi -= minelt;
6457 for (; lo <= hi; lo++)
6459 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6461 if (MEM_P (target)
6462 && !MEM_KEEP_ALIAS_SET_P (target)
6463 && TREE_CODE (type) == ARRAY_TYPE
6464 && TYPE_NONALIASED_COMPONENT (type))
6466 target = copy_rtx (target);
6467 MEM_KEEP_ALIAS_SET_P (target) = 1;
6470 store_constructor_field
6471 (target, bitsize, bitpos, mode, value, cleared,
6472 get_alias_set (elttype), reverse);
6475 else
6477 rtx_code_label *loop_start = gen_label_rtx ();
6478 rtx_code_label *loop_end = gen_label_rtx ();
6479 tree exit_cond;
6481 expand_normal (hi_index);
6483 index = build_decl (EXPR_LOCATION (exp),
6484 VAR_DECL, NULL_TREE, domain);
6485 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6486 SET_DECL_RTL (index, index_r);
6487 store_expr (lo_index, index_r, 0, false, reverse);
6489 /* Build the head of the loop. */
6490 do_pending_stack_adjust ();
6491 emit_label (loop_start);
6493 /* Assign value to element index. */
6494 position =
6495 fold_convert (ssizetype,
6496 fold_build2 (MINUS_EXPR,
6497 TREE_TYPE (index),
6498 index,
6499 TYPE_MIN_VALUE (domain)));
6501 position =
6502 size_binop (MULT_EXPR, position,
6503 fold_convert (ssizetype,
6504 TYPE_SIZE_UNIT (elttype)));
6506 pos_rtx = expand_normal (position);
6507 xtarget = offset_address (target, pos_rtx,
6508 highest_pow2_factor (position));
6509 xtarget = adjust_address (xtarget, mode, 0);
6510 if (TREE_CODE (value) == CONSTRUCTOR)
6511 store_constructor (value, xtarget, cleared,
6512 bitsize / BITS_PER_UNIT, reverse);
6513 else
6514 store_expr (value, xtarget, 0, false, reverse);
6516 /* Generate a conditional jump to exit the loop. */
6517 exit_cond = build2 (LT_EXPR, integer_type_node,
6518 index, hi_index);
6519 jumpif (exit_cond, loop_end, -1);
6521 /* Update the loop counter, and jump to the head of
6522 the loop. */
6523 expand_assignment (index,
6524 build2 (PLUS_EXPR, TREE_TYPE (index),
6525 index, integer_one_node),
6526 false);
6528 emit_jump (loop_start);
6530 /* Build the end of the loop. */
6531 emit_label (loop_end);
6534 else if ((index != 0 && ! tree_fits_shwi_p (index))
6535 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6537 tree position;
6539 if (index == 0)
6540 index = ssize_int (1);
6542 if (minelt)
6543 index = fold_convert (ssizetype,
6544 fold_build2 (MINUS_EXPR,
6545 TREE_TYPE (index),
6546 index,
6547 TYPE_MIN_VALUE (domain)));
6549 position =
6550 size_binop (MULT_EXPR, index,
6551 fold_convert (ssizetype,
6552 TYPE_SIZE_UNIT (elttype)));
6553 xtarget = offset_address (target,
6554 expand_normal (position),
6555 highest_pow2_factor (position));
6556 xtarget = adjust_address (xtarget, mode, 0);
6557 store_expr (value, xtarget, 0, false, reverse);
6559 else
6561 if (index != 0)
6562 bitpos = ((tree_to_shwi (index) - minelt)
6563 * tree_to_uhwi (TYPE_SIZE (elttype)));
6564 else
6565 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6567 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6568 && TREE_CODE (type) == ARRAY_TYPE
6569 && TYPE_NONALIASED_COMPONENT (type))
6571 target = copy_rtx (target);
6572 MEM_KEEP_ALIAS_SET_P (target) = 1;
6574 store_constructor_field (target, bitsize, bitpos, mode, value,
6575 cleared, get_alias_set (elttype),
6576 reverse);
6579 break;
6582 case VECTOR_TYPE:
6584 unsigned HOST_WIDE_INT idx;
6585 constructor_elt *ce;
6586 int i;
6587 int need_to_clear;
6588 int icode = CODE_FOR_nothing;
6589 tree elttype = TREE_TYPE (type);
6590 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6591 machine_mode eltmode = TYPE_MODE (elttype);
6592 HOST_WIDE_INT bitsize;
6593 HOST_WIDE_INT bitpos;
6594 rtvec vector = NULL;
6595 unsigned n_elts;
6596 alias_set_type alias;
6598 gcc_assert (eltmode != BLKmode);
6600 n_elts = TYPE_VECTOR_SUBPARTS (type);
6601 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6603 machine_mode mode = GET_MODE (target);
6605 icode = (int) optab_handler (vec_init_optab, mode);
6606 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6607 if (icode != CODE_FOR_nothing)
6609 tree value;
6611 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6612 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6614 icode = CODE_FOR_nothing;
6615 break;
6618 if (icode != CODE_FOR_nothing)
6620 unsigned int i;
6622 vector = rtvec_alloc (n_elts);
6623 for (i = 0; i < n_elts; i++)
6624 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6628 /* If the constructor has fewer elements than the vector,
6629 clear the whole array first. Similarly if this is static
6630 constructor of a non-BLKmode object. */
6631 if (cleared)
6632 need_to_clear = 0;
6633 else if (REG_P (target) && TREE_STATIC (exp))
6634 need_to_clear = 1;
6635 else
6637 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6638 tree value;
6640 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6642 int n_elts_here = tree_to_uhwi
6643 (int_const_binop (TRUNC_DIV_EXPR,
6644 TYPE_SIZE (TREE_TYPE (value)),
6645 TYPE_SIZE (elttype)));
6647 count += n_elts_here;
6648 if (mostly_zeros_p (value))
6649 zero_count += n_elts_here;
6652 /* Clear the entire vector first if there are any missing elements,
6653 or if the incidence of zero elements is >= 75%. */
6654 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6657 if (need_to_clear && size > 0 && !vector)
6659 if (REG_P (target))
6660 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6661 else
6662 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6663 cleared = 1;
6666 /* Inform later passes that the old value is dead. */
6667 if (!cleared && !vector && REG_P (target))
6668 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6670 if (MEM_P (target))
6671 alias = MEM_ALIAS_SET (target);
6672 else
6673 alias = get_alias_set (elttype);
6675 /* Store each element of the constructor into the corresponding
6676 element of TARGET, determined by counting the elements. */
6677 for (idx = 0, i = 0;
6678 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6679 idx++, i += bitsize / elt_size)
6681 HOST_WIDE_INT eltpos;
6682 tree value = ce->value;
6684 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6685 if (cleared && initializer_zerop (value))
6686 continue;
6688 if (ce->index)
6689 eltpos = tree_to_uhwi (ce->index);
6690 else
6691 eltpos = i;
6693 if (vector)
6695 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6696 elements. */
6697 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6698 RTVEC_ELT (vector, eltpos)
6699 = expand_normal (value);
6701 else
6703 machine_mode value_mode =
6704 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6705 ? TYPE_MODE (TREE_TYPE (value))
6706 : eltmode;
6707 bitpos = eltpos * elt_size;
6708 store_constructor_field (target, bitsize, bitpos, value_mode,
6709 value, cleared, alias, reverse);
6713 if (vector)
6714 emit_insn (GEN_FCN (icode)
6715 (target,
6716 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6717 break;
6720 default:
6721 gcc_unreachable ();
6725 /* Store the value of EXP (an expression tree)
6726 into a subfield of TARGET which has mode MODE and occupies
6727 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6728 If MODE is VOIDmode, it means that we are storing into a bit-field.
6730 BITREGION_START is bitpos of the first bitfield in this region.
6731 BITREGION_END is the bitpos of the ending bitfield in this region.
6732 These two fields are 0, if the C++ memory model does not apply,
6733 or we are not interested in keeping track of bitfield regions.
6735 Always return const0_rtx unless we have something particular to
6736 return.
6738 ALIAS_SET is the alias set for the destination. This value will
6739 (in general) be different from that for TARGET, since TARGET is a
6740 reference to the containing structure.
6742 If NONTEMPORAL is true, try generating a nontemporal store.
6744 If REVERSE is true, the store is to be done in reverse order. */
6746 static rtx
6747 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6748 unsigned HOST_WIDE_INT bitregion_start,
6749 unsigned HOST_WIDE_INT bitregion_end,
6750 machine_mode mode, tree exp,
6751 alias_set_type alias_set, bool nontemporal, bool reverse)
6753 if (TREE_CODE (exp) == ERROR_MARK)
6754 return const0_rtx;
6756 /* If we have nothing to store, do nothing unless the expression has
6757 side-effects. */
6758 if (bitsize == 0)
6759 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6761 if (GET_CODE (target) == CONCAT)
6763 /* We're storing into a struct containing a single __complex. */
6765 gcc_assert (!bitpos);
6766 return store_expr (exp, target, 0, nontemporal, reverse);
6769 /* If the structure is in a register or if the component
6770 is a bit field, we cannot use addressing to access it.
6771 Use bit-field techniques or SUBREG to store in it. */
6773 if (mode == VOIDmode
6774 || (mode != BLKmode && ! direct_store[(int) mode]
6775 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6776 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6777 || REG_P (target)
6778 || GET_CODE (target) == SUBREG
6779 /* If the field isn't aligned enough to store as an ordinary memref,
6780 store it as a bit field. */
6781 || (mode != BLKmode
6782 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6783 || bitpos % GET_MODE_ALIGNMENT (mode))
6784 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6785 || (bitpos % BITS_PER_UNIT != 0)))
6786 || (bitsize >= 0 && mode != BLKmode
6787 && GET_MODE_BITSIZE (mode) > bitsize)
6788 /* If the RHS and field are a constant size and the size of the
6789 RHS isn't the same size as the bitfield, we must use bitfield
6790 operations. */
6791 || (bitsize >= 0
6792 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6793 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6794 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6795 we will handle specially below. */
6796 && !(TREE_CODE (exp) == CONSTRUCTOR
6797 && bitsize % BITS_PER_UNIT == 0)
6798 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6799 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6800 includes some extra padding. store_expr / expand_expr will in
6801 that case call get_inner_reference that will have the bitsize
6802 we check here and thus the block move will not clobber the
6803 padding that shouldn't be clobbered. In the future we could
6804 replace the TREE_ADDRESSABLE check with a check that
6805 get_base_address needs to live in memory. */
6806 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6807 || TREE_CODE (exp) != COMPONENT_REF
6808 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6809 || (bitsize % BITS_PER_UNIT != 0)
6810 || (bitpos % BITS_PER_UNIT != 0)
6811 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6812 != 0)))
6813 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6814 decl we must use bitfield operations. */
6815 || (bitsize >= 0
6816 && TREE_CODE (exp) == MEM_REF
6817 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6818 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6819 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6820 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6822 rtx temp;
6823 gimple *nop_def;
6825 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6826 implies a mask operation. If the precision is the same size as
6827 the field we're storing into, that mask is redundant. This is
6828 particularly common with bit field assignments generated by the
6829 C front end. */
6830 nop_def = get_def_for_expr (exp, NOP_EXPR);
6831 if (nop_def)
6833 tree type = TREE_TYPE (exp);
6834 if (INTEGRAL_TYPE_P (type)
6835 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6836 && bitsize == TYPE_PRECISION (type))
6838 tree op = gimple_assign_rhs1 (nop_def);
6839 type = TREE_TYPE (op);
6840 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6841 exp = op;
6845 temp = expand_normal (exp);
6847 /* If the value has a record type and an integral mode then, if BITSIZE
6848 is narrower than this mode and this is for big-endian data, we must
6849 first put the value into the low-order bits. Moreover, the field may
6850 be not aligned on a byte boundary; in this case, if it has reverse
6851 storage order, it needs to be accessed as a scalar field with reverse
6852 storage order and we must first put the value into target order. */
6853 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
6854 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6856 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6858 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6860 if (reverse)
6861 temp = flip_storage_order (GET_MODE (temp), temp);
6863 if (bitsize < size
6864 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6865 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6866 size - bitsize, NULL_RTX, 1);
6869 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6870 if (mode != VOIDmode && mode != BLKmode
6871 && mode != TYPE_MODE (TREE_TYPE (exp)))
6872 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6874 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6875 are both BLKmode, both must be in memory and BITPOS must be aligned
6876 on a byte boundary. If so, we simply do a block copy. Likewise for
6877 a BLKmode-like TARGET. */
6878 if (GET_CODE (temp) != PARALLEL
6879 && GET_MODE (temp) == BLKmode
6880 && (GET_MODE (target) == BLKmode
6881 || (MEM_P (target)
6882 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6883 && (bitpos % BITS_PER_UNIT) == 0
6884 && (bitsize % BITS_PER_UNIT) == 0)))
6886 gcc_assert (MEM_P (target) && MEM_P (temp)
6887 && (bitpos % BITS_PER_UNIT) == 0);
6889 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6890 emit_block_move (target, temp,
6891 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6892 / BITS_PER_UNIT),
6893 BLOCK_OP_NORMAL);
6895 return const0_rtx;
6898 /* Handle calls that return values in multiple non-contiguous locations.
6899 The Irix 6 ABI has examples of this. */
6900 if (GET_CODE (temp) == PARALLEL)
6902 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6903 rtx temp_target;
6904 if (mode == BLKmode || mode == VOIDmode)
6905 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6906 temp_target = gen_reg_rtx (mode);
6907 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6908 temp = temp_target;
6910 else if (mode == BLKmode)
6912 /* Handle calls that return BLKmode values in registers. */
6913 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6915 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6916 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6917 temp = temp_target;
6919 else
6921 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6922 rtx temp_target;
6923 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6924 temp_target = gen_reg_rtx (mode);
6925 temp_target
6926 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6927 temp_target, mode, mode, false);
6928 temp = temp_target;
6932 /* Store the value in the bitfield. */
6933 store_bit_field (target, bitsize, bitpos,
6934 bitregion_start, bitregion_end,
6935 mode, temp, reverse);
6937 return const0_rtx;
6939 else
6941 /* Now build a reference to just the desired component. */
6942 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6944 if (to_rtx == target)
6945 to_rtx = copy_rtx (to_rtx);
6947 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6948 set_mem_alias_set (to_rtx, alias_set);
6950 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6951 into a target smaller than its type; handle that case now. */
6952 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6954 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6955 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6956 return to_rtx;
6959 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6963 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6964 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6965 codes and find the ultimate containing object, which we return.
6967 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6968 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6969 storage order of the field.
6970 If the position of the field is variable, we store a tree
6971 giving the variable offset (in units) in *POFFSET.
6972 This offset is in addition to the bit position.
6973 If the position is not variable, we store 0 in *POFFSET.
6975 If any of the extraction expressions is volatile,
6976 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6978 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6979 Otherwise, it is a mode that can be used to access the field.
6981 If the field describes a variable-sized object, *PMODE is set to
6982 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6983 this case, but the address of the object can be found. */
6985 tree
6986 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6987 HOST_WIDE_INT *pbitpos, tree *poffset,
6988 machine_mode *pmode, int *punsignedp,
6989 int *preversep, int *pvolatilep)
6991 tree size_tree = 0;
6992 machine_mode mode = VOIDmode;
6993 bool blkmode_bitfield = false;
6994 tree offset = size_zero_node;
6995 offset_int bit_offset = 0;
6997 /* First get the mode, signedness, storage order and size. We do this from
6998 just the outermost expression. */
6999 *pbitsize = -1;
7000 if (TREE_CODE (exp) == COMPONENT_REF)
7002 tree field = TREE_OPERAND (exp, 1);
7003 size_tree = DECL_SIZE (field);
7004 if (flag_strict_volatile_bitfields > 0
7005 && TREE_THIS_VOLATILE (exp)
7006 && DECL_BIT_FIELD_TYPE (field)
7007 && DECL_MODE (field) != BLKmode)
7008 /* Volatile bitfields should be accessed in the mode of the
7009 field's type, not the mode computed based on the bit
7010 size. */
7011 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7012 else if (!DECL_BIT_FIELD (field))
7013 mode = DECL_MODE (field);
7014 else if (DECL_MODE (field) == BLKmode)
7015 blkmode_bitfield = true;
7017 *punsignedp = DECL_UNSIGNED (field);
7019 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7021 size_tree = TREE_OPERAND (exp, 1);
7022 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7023 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7025 /* For vector types, with the correct size of access, use the mode of
7026 inner type. */
7027 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7028 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7029 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7030 mode = TYPE_MODE (TREE_TYPE (exp));
7032 else
7034 mode = TYPE_MODE (TREE_TYPE (exp));
7035 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7037 if (mode == BLKmode)
7038 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7039 else
7040 *pbitsize = GET_MODE_BITSIZE (mode);
7043 if (size_tree != 0)
7045 if (! tree_fits_uhwi_p (size_tree))
7046 mode = BLKmode, *pbitsize = -1;
7047 else
7048 *pbitsize = tree_to_uhwi (size_tree);
7051 *preversep = reverse_storage_order_for_component_p (exp);
7053 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7054 and find the ultimate containing object. */
7055 while (1)
7057 switch (TREE_CODE (exp))
7059 case BIT_FIELD_REF:
7060 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7061 break;
7063 case COMPONENT_REF:
7065 tree field = TREE_OPERAND (exp, 1);
7066 tree this_offset = component_ref_field_offset (exp);
7068 /* If this field hasn't been filled in yet, don't go past it.
7069 This should only happen when folding expressions made during
7070 type construction. */
7071 if (this_offset == 0)
7072 break;
7074 offset = size_binop (PLUS_EXPR, offset, this_offset);
7075 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7077 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7079 break;
7081 case ARRAY_REF:
7082 case ARRAY_RANGE_REF:
7084 tree index = TREE_OPERAND (exp, 1);
7085 tree low_bound = array_ref_low_bound (exp);
7086 tree unit_size = array_ref_element_size (exp);
7088 /* We assume all arrays have sizes that are a multiple of a byte.
7089 First subtract the lower bound, if any, in the type of the
7090 index, then convert to sizetype and multiply by the size of
7091 the array element. */
7092 if (! integer_zerop (low_bound))
7093 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7094 index, low_bound);
7096 offset = size_binop (PLUS_EXPR, offset,
7097 size_binop (MULT_EXPR,
7098 fold_convert (sizetype, index),
7099 unit_size));
7101 break;
7103 case REALPART_EXPR:
7104 break;
7106 case IMAGPART_EXPR:
7107 bit_offset += *pbitsize;
7108 break;
7110 case VIEW_CONVERT_EXPR:
7111 break;
7113 case MEM_REF:
7114 /* Hand back the decl for MEM[&decl, off]. */
7115 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7117 tree off = TREE_OPERAND (exp, 1);
7118 if (!integer_zerop (off))
7120 offset_int boff, coff = mem_ref_offset (exp);
7121 boff = coff << LOG2_BITS_PER_UNIT;
7122 bit_offset += boff;
7124 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7126 goto done;
7128 default:
7129 goto done;
7132 /* If any reference in the chain is volatile, the effect is volatile. */
7133 if (TREE_THIS_VOLATILE (exp))
7134 *pvolatilep = 1;
7136 exp = TREE_OPERAND (exp, 0);
7138 done:
7140 /* If OFFSET is constant, see if we can return the whole thing as a
7141 constant bit position. Make sure to handle overflow during
7142 this conversion. */
7143 if (TREE_CODE (offset) == INTEGER_CST)
7145 offset_int tem = wi::sext (wi::to_offset (offset),
7146 TYPE_PRECISION (sizetype));
7147 tem <<= LOG2_BITS_PER_UNIT;
7148 tem += bit_offset;
7149 if (wi::fits_shwi_p (tem))
7151 *pbitpos = tem.to_shwi ();
7152 *poffset = offset = NULL_TREE;
7156 /* Otherwise, split it up. */
7157 if (offset)
7159 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7160 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7162 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7163 offset_int tem = bit_offset.and_not (mask);
7164 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7165 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7166 bit_offset -= tem;
7167 tem >>= LOG2_BITS_PER_UNIT;
7168 offset = size_binop (PLUS_EXPR, offset,
7169 wide_int_to_tree (sizetype, tem));
7172 *pbitpos = bit_offset.to_shwi ();
7173 *poffset = offset;
7176 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7177 if (mode == VOIDmode
7178 && blkmode_bitfield
7179 && (*pbitpos % BITS_PER_UNIT) == 0
7180 && (*pbitsize % BITS_PER_UNIT) == 0)
7181 *pmode = BLKmode;
7182 else
7183 *pmode = mode;
7185 return exp;
7188 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7190 static unsigned HOST_WIDE_INT
7191 target_align (const_tree target)
7193 /* We might have a chain of nested references with intermediate misaligning
7194 bitfields components, so need to recurse to find out. */
7196 unsigned HOST_WIDE_INT this_align, outer_align;
7198 switch (TREE_CODE (target))
7200 case BIT_FIELD_REF:
7201 return 1;
7203 case COMPONENT_REF:
7204 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7205 outer_align = target_align (TREE_OPERAND (target, 0));
7206 return MIN (this_align, outer_align);
7208 case ARRAY_REF:
7209 case ARRAY_RANGE_REF:
7210 this_align = TYPE_ALIGN (TREE_TYPE (target));
7211 outer_align = target_align (TREE_OPERAND (target, 0));
7212 return MIN (this_align, outer_align);
7214 CASE_CONVERT:
7215 case NON_LVALUE_EXPR:
7216 case VIEW_CONVERT_EXPR:
7217 this_align = TYPE_ALIGN (TREE_TYPE (target));
7218 outer_align = target_align (TREE_OPERAND (target, 0));
7219 return MAX (this_align, outer_align);
7221 default:
7222 return TYPE_ALIGN (TREE_TYPE (target));
7227 /* Given an rtx VALUE that may contain additions and multiplications, return
7228 an equivalent value that just refers to a register, memory, or constant.
7229 This is done by generating instructions to perform the arithmetic and
7230 returning a pseudo-register containing the value.
7232 The returned value may be a REG, SUBREG, MEM or constant. */
7235 force_operand (rtx value, rtx target)
7237 rtx op1, op2;
7238 /* Use subtarget as the target for operand 0 of a binary operation. */
7239 rtx subtarget = get_subtarget (target);
7240 enum rtx_code code = GET_CODE (value);
7242 /* Check for subreg applied to an expression produced by loop optimizer. */
7243 if (code == SUBREG
7244 && !REG_P (SUBREG_REG (value))
7245 && !MEM_P (SUBREG_REG (value)))
7247 value
7248 = simplify_gen_subreg (GET_MODE (value),
7249 force_reg (GET_MODE (SUBREG_REG (value)),
7250 force_operand (SUBREG_REG (value),
7251 NULL_RTX)),
7252 GET_MODE (SUBREG_REG (value)),
7253 SUBREG_BYTE (value));
7254 code = GET_CODE (value);
7257 /* Check for a PIC address load. */
7258 if ((code == PLUS || code == MINUS)
7259 && XEXP (value, 0) == pic_offset_table_rtx
7260 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7261 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7262 || GET_CODE (XEXP (value, 1)) == CONST))
7264 if (!subtarget)
7265 subtarget = gen_reg_rtx (GET_MODE (value));
7266 emit_move_insn (subtarget, value);
7267 return subtarget;
7270 if (ARITHMETIC_P (value))
7272 op2 = XEXP (value, 1);
7273 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7274 subtarget = 0;
7275 if (code == MINUS && CONST_INT_P (op2))
7277 code = PLUS;
7278 op2 = negate_rtx (GET_MODE (value), op2);
7281 /* Check for an addition with OP2 a constant integer and our first
7282 operand a PLUS of a virtual register and something else. In that
7283 case, we want to emit the sum of the virtual register and the
7284 constant first and then add the other value. This allows virtual
7285 register instantiation to simply modify the constant rather than
7286 creating another one around this addition. */
7287 if (code == PLUS && CONST_INT_P (op2)
7288 && GET_CODE (XEXP (value, 0)) == PLUS
7289 && REG_P (XEXP (XEXP (value, 0), 0))
7290 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7291 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7293 rtx temp = expand_simple_binop (GET_MODE (value), code,
7294 XEXP (XEXP (value, 0), 0), op2,
7295 subtarget, 0, OPTAB_LIB_WIDEN);
7296 return expand_simple_binop (GET_MODE (value), code, temp,
7297 force_operand (XEXP (XEXP (value,
7298 0), 1), 0),
7299 target, 0, OPTAB_LIB_WIDEN);
7302 op1 = force_operand (XEXP (value, 0), subtarget);
7303 op2 = force_operand (op2, NULL_RTX);
7304 switch (code)
7306 case MULT:
7307 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7308 case DIV:
7309 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7310 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7311 target, 1, OPTAB_LIB_WIDEN);
7312 else
7313 return expand_divmod (0,
7314 FLOAT_MODE_P (GET_MODE (value))
7315 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7316 GET_MODE (value), op1, op2, target, 0);
7317 case MOD:
7318 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7319 target, 0);
7320 case UDIV:
7321 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7322 target, 1);
7323 case UMOD:
7324 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7325 target, 1);
7326 case ASHIFTRT:
7327 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7328 target, 0, OPTAB_LIB_WIDEN);
7329 default:
7330 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7331 target, 1, OPTAB_LIB_WIDEN);
7334 if (UNARY_P (value))
7336 if (!target)
7337 target = gen_reg_rtx (GET_MODE (value));
7338 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7339 switch (code)
7341 case ZERO_EXTEND:
7342 case SIGN_EXTEND:
7343 case TRUNCATE:
7344 case FLOAT_EXTEND:
7345 case FLOAT_TRUNCATE:
7346 convert_move (target, op1, code == ZERO_EXTEND);
7347 return target;
7349 case FIX:
7350 case UNSIGNED_FIX:
7351 expand_fix (target, op1, code == UNSIGNED_FIX);
7352 return target;
7354 case FLOAT:
7355 case UNSIGNED_FLOAT:
7356 expand_float (target, op1, code == UNSIGNED_FLOAT);
7357 return target;
7359 default:
7360 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7364 #ifdef INSN_SCHEDULING
7365 /* On machines that have insn scheduling, we want all memory reference to be
7366 explicit, so we need to deal with such paradoxical SUBREGs. */
7367 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7368 value
7369 = simplify_gen_subreg (GET_MODE (value),
7370 force_reg (GET_MODE (SUBREG_REG (value)),
7371 force_operand (SUBREG_REG (value),
7372 NULL_RTX)),
7373 GET_MODE (SUBREG_REG (value)),
7374 SUBREG_BYTE (value));
7375 #endif
7377 return value;
7380 /* Subroutine of expand_expr: return nonzero iff there is no way that
7381 EXP can reference X, which is being modified. TOP_P is nonzero if this
7382 call is going to be used to determine whether we need a temporary
7383 for EXP, as opposed to a recursive call to this function.
7385 It is always safe for this routine to return zero since it merely
7386 searches for optimization opportunities. */
7389 safe_from_p (const_rtx x, tree exp, int top_p)
7391 rtx exp_rtl = 0;
7392 int i, nops;
7394 if (x == 0
7395 /* If EXP has varying size, we MUST use a target since we currently
7396 have no way of allocating temporaries of variable size
7397 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7398 So we assume here that something at a higher level has prevented a
7399 clash. This is somewhat bogus, but the best we can do. Only
7400 do this when X is BLKmode and when we are at the top level. */
7401 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7402 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7403 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7404 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7405 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7406 != INTEGER_CST)
7407 && GET_MODE (x) == BLKmode)
7408 /* If X is in the outgoing argument area, it is always safe. */
7409 || (MEM_P (x)
7410 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7411 || (GET_CODE (XEXP (x, 0)) == PLUS
7412 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7413 return 1;
7415 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7416 find the underlying pseudo. */
7417 if (GET_CODE (x) == SUBREG)
7419 x = SUBREG_REG (x);
7420 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7421 return 0;
7424 /* Now look at our tree code and possibly recurse. */
7425 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7427 case tcc_declaration:
7428 exp_rtl = DECL_RTL_IF_SET (exp);
7429 break;
7431 case tcc_constant:
7432 return 1;
7434 case tcc_exceptional:
7435 if (TREE_CODE (exp) == TREE_LIST)
7437 while (1)
7439 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7440 return 0;
7441 exp = TREE_CHAIN (exp);
7442 if (!exp)
7443 return 1;
7444 if (TREE_CODE (exp) != TREE_LIST)
7445 return safe_from_p (x, exp, 0);
7448 else if (TREE_CODE (exp) == CONSTRUCTOR)
7450 constructor_elt *ce;
7451 unsigned HOST_WIDE_INT idx;
7453 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7454 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7455 || !safe_from_p (x, ce->value, 0))
7456 return 0;
7457 return 1;
7459 else if (TREE_CODE (exp) == ERROR_MARK)
7460 return 1; /* An already-visited SAVE_EXPR? */
7461 else
7462 return 0;
7464 case tcc_statement:
7465 /* The only case we look at here is the DECL_INITIAL inside a
7466 DECL_EXPR. */
7467 return (TREE_CODE (exp) != DECL_EXPR
7468 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7469 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7470 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7472 case tcc_binary:
7473 case tcc_comparison:
7474 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7475 return 0;
7476 /* Fall through. */
7478 case tcc_unary:
7479 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7481 case tcc_expression:
7482 case tcc_reference:
7483 case tcc_vl_exp:
7484 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7485 the expression. If it is set, we conflict iff we are that rtx or
7486 both are in memory. Otherwise, we check all operands of the
7487 expression recursively. */
7489 switch (TREE_CODE (exp))
7491 case ADDR_EXPR:
7492 /* If the operand is static or we are static, we can't conflict.
7493 Likewise if we don't conflict with the operand at all. */
7494 if (staticp (TREE_OPERAND (exp, 0))
7495 || TREE_STATIC (exp)
7496 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7497 return 1;
7499 /* Otherwise, the only way this can conflict is if we are taking
7500 the address of a DECL a that address if part of X, which is
7501 very rare. */
7502 exp = TREE_OPERAND (exp, 0);
7503 if (DECL_P (exp))
7505 if (!DECL_RTL_SET_P (exp)
7506 || !MEM_P (DECL_RTL (exp)))
7507 return 0;
7508 else
7509 exp_rtl = XEXP (DECL_RTL (exp), 0);
7511 break;
7513 case MEM_REF:
7514 if (MEM_P (x)
7515 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7516 get_alias_set (exp)))
7517 return 0;
7518 break;
7520 case CALL_EXPR:
7521 /* Assume that the call will clobber all hard registers and
7522 all of memory. */
7523 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7524 || MEM_P (x))
7525 return 0;
7526 break;
7528 case WITH_CLEANUP_EXPR:
7529 case CLEANUP_POINT_EXPR:
7530 /* Lowered by gimplify.c. */
7531 gcc_unreachable ();
7533 case SAVE_EXPR:
7534 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7536 default:
7537 break;
7540 /* If we have an rtx, we do not need to scan our operands. */
7541 if (exp_rtl)
7542 break;
7544 nops = TREE_OPERAND_LENGTH (exp);
7545 for (i = 0; i < nops; i++)
7546 if (TREE_OPERAND (exp, i) != 0
7547 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7548 return 0;
7550 break;
7552 case tcc_type:
7553 /* Should never get a type here. */
7554 gcc_unreachable ();
7557 /* If we have an rtl, find any enclosed object. Then see if we conflict
7558 with it. */
7559 if (exp_rtl)
7561 if (GET_CODE (exp_rtl) == SUBREG)
7563 exp_rtl = SUBREG_REG (exp_rtl);
7564 if (REG_P (exp_rtl)
7565 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7566 return 0;
7569 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7570 are memory and they conflict. */
7571 return ! (rtx_equal_p (x, exp_rtl)
7572 || (MEM_P (x) && MEM_P (exp_rtl)
7573 && true_dependence (exp_rtl, VOIDmode, x)));
7576 /* If we reach here, it is safe. */
7577 return 1;
7581 /* Return the highest power of two that EXP is known to be a multiple of.
7582 This is used in updating alignment of MEMs in array references. */
7584 unsigned HOST_WIDE_INT
7585 highest_pow2_factor (const_tree exp)
7587 unsigned HOST_WIDE_INT ret;
7588 int trailing_zeros = tree_ctz (exp);
7589 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7590 return BIGGEST_ALIGNMENT;
7591 ret = HOST_WIDE_INT_1U << trailing_zeros;
7592 if (ret > BIGGEST_ALIGNMENT)
7593 return BIGGEST_ALIGNMENT;
7594 return ret;
7597 /* Similar, except that the alignment requirements of TARGET are
7598 taken into account. Assume it is at least as aligned as its
7599 type, unless it is a COMPONENT_REF in which case the layout of
7600 the structure gives the alignment. */
7602 static unsigned HOST_WIDE_INT
7603 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7605 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7606 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7608 return MAX (factor, talign);
7611 /* Convert the tree comparison code TCODE to the rtl one where the
7612 signedness is UNSIGNEDP. */
7614 static enum rtx_code
7615 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7617 enum rtx_code code;
7618 switch (tcode)
7620 case EQ_EXPR:
7621 code = EQ;
7622 break;
7623 case NE_EXPR:
7624 code = NE;
7625 break;
7626 case LT_EXPR:
7627 code = unsignedp ? LTU : LT;
7628 break;
7629 case LE_EXPR:
7630 code = unsignedp ? LEU : LE;
7631 break;
7632 case GT_EXPR:
7633 code = unsignedp ? GTU : GT;
7634 break;
7635 case GE_EXPR:
7636 code = unsignedp ? GEU : GE;
7637 break;
7638 case UNORDERED_EXPR:
7639 code = UNORDERED;
7640 break;
7641 case ORDERED_EXPR:
7642 code = ORDERED;
7643 break;
7644 case UNLT_EXPR:
7645 code = UNLT;
7646 break;
7647 case UNLE_EXPR:
7648 code = UNLE;
7649 break;
7650 case UNGT_EXPR:
7651 code = UNGT;
7652 break;
7653 case UNGE_EXPR:
7654 code = UNGE;
7655 break;
7656 case UNEQ_EXPR:
7657 code = UNEQ;
7658 break;
7659 case LTGT_EXPR:
7660 code = LTGT;
7661 break;
7663 default:
7664 gcc_unreachable ();
7666 return code;
7669 /* Subroutine of expand_expr. Expand the two operands of a binary
7670 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7671 The value may be stored in TARGET if TARGET is nonzero. The
7672 MODIFIER argument is as documented by expand_expr. */
7674 void
7675 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7676 enum expand_modifier modifier)
7678 if (! safe_from_p (target, exp1, 1))
7679 target = 0;
7680 if (operand_equal_p (exp0, exp1, 0))
7682 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7683 *op1 = copy_rtx (*op0);
7685 else
7687 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7688 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7693 /* Return a MEM that contains constant EXP. DEFER is as for
7694 output_constant_def and MODIFIER is as for expand_expr. */
7696 static rtx
7697 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7699 rtx mem;
7701 mem = output_constant_def (exp, defer);
7702 if (modifier != EXPAND_INITIALIZER)
7703 mem = use_anchored_address (mem);
7704 return mem;
7707 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7708 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7710 static rtx
7711 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7712 enum expand_modifier modifier, addr_space_t as)
7714 rtx result, subtarget;
7715 tree inner, offset;
7716 HOST_WIDE_INT bitsize, bitpos;
7717 int unsignedp, reversep, volatilep = 0;
7718 machine_mode mode1;
7720 /* If we are taking the address of a constant and are at the top level,
7721 we have to use output_constant_def since we can't call force_const_mem
7722 at top level. */
7723 /* ??? This should be considered a front-end bug. We should not be
7724 generating ADDR_EXPR of something that isn't an LVALUE. The only
7725 exception here is STRING_CST. */
7726 if (CONSTANT_CLASS_P (exp))
7728 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7729 if (modifier < EXPAND_SUM)
7730 result = force_operand (result, target);
7731 return result;
7734 /* Everything must be something allowed by is_gimple_addressable. */
7735 switch (TREE_CODE (exp))
7737 case INDIRECT_REF:
7738 /* This case will happen via recursion for &a->b. */
7739 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7741 case MEM_REF:
7743 tree tem = TREE_OPERAND (exp, 0);
7744 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7745 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7746 return expand_expr (tem, target, tmode, modifier);
7749 case CONST_DECL:
7750 /* Expand the initializer like constants above. */
7751 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7752 0, modifier), 0);
7753 if (modifier < EXPAND_SUM)
7754 result = force_operand (result, target);
7755 return result;
7757 case REALPART_EXPR:
7758 /* The real part of the complex number is always first, therefore
7759 the address is the same as the address of the parent object. */
7760 offset = 0;
7761 bitpos = 0;
7762 inner = TREE_OPERAND (exp, 0);
7763 break;
7765 case IMAGPART_EXPR:
7766 /* The imaginary part of the complex number is always second.
7767 The expression is therefore always offset by the size of the
7768 scalar type. */
7769 offset = 0;
7770 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7771 inner = TREE_OPERAND (exp, 0);
7772 break;
7774 case COMPOUND_LITERAL_EXPR:
7775 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7776 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7777 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7778 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7779 the initializers aren't gimplified. */
7780 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7781 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7782 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7783 target, tmode, modifier, as);
7784 /* FALLTHRU */
7785 default:
7786 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7787 expand_expr, as that can have various side effects; LABEL_DECLs for
7788 example, may not have their DECL_RTL set yet. Expand the rtl of
7789 CONSTRUCTORs too, which should yield a memory reference for the
7790 constructor's contents. Assume language specific tree nodes can
7791 be expanded in some interesting way. */
7792 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7793 if (DECL_P (exp)
7794 || TREE_CODE (exp) == CONSTRUCTOR
7795 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7797 result = expand_expr (exp, target, tmode,
7798 modifier == EXPAND_INITIALIZER
7799 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7801 /* If the DECL isn't in memory, then the DECL wasn't properly
7802 marked TREE_ADDRESSABLE, which will be either a front-end
7803 or a tree optimizer bug. */
7805 gcc_assert (MEM_P (result));
7806 result = XEXP (result, 0);
7808 /* ??? Is this needed anymore? */
7809 if (DECL_P (exp))
7810 TREE_USED (exp) = 1;
7812 if (modifier != EXPAND_INITIALIZER
7813 && modifier != EXPAND_CONST_ADDRESS
7814 && modifier != EXPAND_SUM)
7815 result = force_operand (result, target);
7816 return result;
7819 /* Pass FALSE as the last argument to get_inner_reference although
7820 we are expanding to RTL. The rationale is that we know how to
7821 handle "aligning nodes" here: we can just bypass them because
7822 they won't change the final object whose address will be returned
7823 (they actually exist only for that purpose). */
7824 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7825 &unsignedp, &reversep, &volatilep);
7826 break;
7829 /* We must have made progress. */
7830 gcc_assert (inner != exp);
7832 subtarget = offset || bitpos ? NULL_RTX : target;
7833 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7834 inner alignment, force the inner to be sufficiently aligned. */
7835 if (CONSTANT_CLASS_P (inner)
7836 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7838 inner = copy_node (inner);
7839 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7840 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7841 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7843 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7845 if (offset)
7847 rtx tmp;
7849 if (modifier != EXPAND_NORMAL)
7850 result = force_operand (result, NULL);
7851 tmp = expand_expr (offset, NULL_RTX, tmode,
7852 modifier == EXPAND_INITIALIZER
7853 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7855 /* expand_expr is allowed to return an object in a mode other
7856 than TMODE. If it did, we need to convert. */
7857 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7858 tmp = convert_modes (tmode, GET_MODE (tmp),
7859 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7860 result = convert_memory_address_addr_space (tmode, result, as);
7861 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7863 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7864 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7865 else
7867 subtarget = bitpos ? NULL_RTX : target;
7868 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7869 1, OPTAB_LIB_WIDEN);
7873 if (bitpos)
7875 /* Someone beforehand should have rejected taking the address
7876 of such an object. */
7877 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7879 result = convert_memory_address_addr_space (tmode, result, as);
7880 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7881 if (modifier < EXPAND_SUM)
7882 result = force_operand (result, target);
7885 return result;
7888 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7889 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7891 static rtx
7892 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7893 enum expand_modifier modifier)
7895 addr_space_t as = ADDR_SPACE_GENERIC;
7896 machine_mode address_mode = Pmode;
7897 machine_mode pointer_mode = ptr_mode;
7898 machine_mode rmode;
7899 rtx result;
7901 /* Target mode of VOIDmode says "whatever's natural". */
7902 if (tmode == VOIDmode)
7903 tmode = TYPE_MODE (TREE_TYPE (exp));
7905 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7907 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7908 address_mode = targetm.addr_space.address_mode (as);
7909 pointer_mode = targetm.addr_space.pointer_mode (as);
7912 /* We can get called with some Weird Things if the user does silliness
7913 like "(short) &a". In that case, convert_memory_address won't do
7914 the right thing, so ignore the given target mode. */
7915 if (tmode != address_mode && tmode != pointer_mode)
7916 tmode = address_mode;
7918 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7919 tmode, modifier, as);
7921 /* Despite expand_expr claims concerning ignoring TMODE when not
7922 strictly convenient, stuff breaks if we don't honor it. Note
7923 that combined with the above, we only do this for pointer modes. */
7924 rmode = GET_MODE (result);
7925 if (rmode == VOIDmode)
7926 rmode = tmode;
7927 if (rmode != tmode)
7928 result = convert_memory_address_addr_space (tmode, result, as);
7930 return result;
7933 /* Generate code for computing CONSTRUCTOR EXP.
7934 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7935 is TRUE, instead of creating a temporary variable in memory
7936 NULL is returned and the caller needs to handle it differently. */
7938 static rtx
7939 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7940 bool avoid_temp_mem)
7942 tree type = TREE_TYPE (exp);
7943 machine_mode mode = TYPE_MODE (type);
7945 /* Try to avoid creating a temporary at all. This is possible
7946 if all of the initializer is zero.
7947 FIXME: try to handle all [0..255] initializers we can handle
7948 with memset. */
7949 if (TREE_STATIC (exp)
7950 && !TREE_ADDRESSABLE (exp)
7951 && target != 0 && mode == BLKmode
7952 && all_zeros_p (exp))
7954 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7955 return target;
7958 /* All elts simple constants => refer to a constant in memory. But
7959 if this is a non-BLKmode mode, let it store a field at a time
7960 since that should make a CONST_INT, CONST_WIDE_INT or
7961 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7962 use, it is best to store directly into the target unless the type
7963 is large enough that memcpy will be used. If we are making an
7964 initializer and all operands are constant, put it in memory as
7965 well.
7967 FIXME: Avoid trying to fill vector constructors piece-meal.
7968 Output them with output_constant_def below unless we're sure
7969 they're zeros. This should go away when vector initializers
7970 are treated like VECTOR_CST instead of arrays. */
7971 if ((TREE_STATIC (exp)
7972 && ((mode == BLKmode
7973 && ! (target != 0 && safe_from_p (target, exp, 1)))
7974 || TREE_ADDRESSABLE (exp)
7975 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7976 && (! can_move_by_pieces
7977 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7978 TYPE_ALIGN (type)))
7979 && ! mostly_zeros_p (exp))))
7980 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7981 && TREE_CONSTANT (exp)))
7983 rtx constructor;
7985 if (avoid_temp_mem)
7986 return NULL_RTX;
7988 constructor = expand_expr_constant (exp, 1, modifier);
7990 if (modifier != EXPAND_CONST_ADDRESS
7991 && modifier != EXPAND_INITIALIZER
7992 && modifier != EXPAND_SUM)
7993 constructor = validize_mem (constructor);
7995 return constructor;
7998 /* Handle calls that pass values in multiple non-contiguous
7999 locations. The Irix 6 ABI has examples of this. */
8000 if (target == 0 || ! safe_from_p (target, exp, 1)
8001 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8003 if (avoid_temp_mem)
8004 return NULL_RTX;
8006 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8009 store_constructor (exp, target, 0, int_expr_size (exp), false);
8010 return target;
8014 /* expand_expr: generate code for computing expression EXP.
8015 An rtx for the computed value is returned. The value is never null.
8016 In the case of a void EXP, const0_rtx is returned.
8018 The value may be stored in TARGET if TARGET is nonzero.
8019 TARGET is just a suggestion; callers must assume that
8020 the rtx returned may not be the same as TARGET.
8022 If TARGET is CONST0_RTX, it means that the value will be ignored.
8024 If TMODE is not VOIDmode, it suggests generating the
8025 result in mode TMODE. But this is done only when convenient.
8026 Otherwise, TMODE is ignored and the value generated in its natural mode.
8027 TMODE is just a suggestion; callers must assume that
8028 the rtx returned may not have mode TMODE.
8030 Note that TARGET may have neither TMODE nor MODE. In that case, it
8031 probably will not be used.
8033 If MODIFIER is EXPAND_SUM then when EXP is an addition
8034 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8035 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8036 products as above, or REG or MEM, or constant.
8037 Ordinarily in such cases we would output mul or add instructions
8038 and then return a pseudo reg containing the sum.
8040 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8041 it also marks a label as absolutely required (it can't be dead).
8042 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8043 This is used for outputting expressions used in initializers.
8045 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8046 with a constant address even if that address is not normally legitimate.
8047 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8049 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8050 a call parameter. Such targets require special care as we haven't yet
8051 marked TARGET so that it's safe from being trashed by libcalls. We
8052 don't want to use TARGET for anything but the final result;
8053 Intermediate values must go elsewhere. Additionally, calls to
8054 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8056 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8057 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8058 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8059 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8060 recursively.
8062 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8063 In this case, we don't adjust a returned MEM rtx that wouldn't be
8064 sufficiently aligned for its mode; instead, it's up to the caller
8065 to deal with it afterwards. This is used to make sure that unaligned
8066 base objects for which out-of-bounds accesses are supported, for
8067 example record types with trailing arrays, aren't realigned behind
8068 the back of the caller.
8069 The normal operating mode is to pass FALSE for this parameter. */
8072 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8073 enum expand_modifier modifier, rtx *alt_rtl,
8074 bool inner_reference_p)
8076 rtx ret;
8078 /* Handle ERROR_MARK before anybody tries to access its type. */
8079 if (TREE_CODE (exp) == ERROR_MARK
8080 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8082 ret = CONST0_RTX (tmode);
8083 return ret ? ret : const0_rtx;
8086 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8087 inner_reference_p);
8088 return ret;
8091 /* Try to expand the conditional expression which is represented by
8092 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8093 return the rtl reg which represents the result. Otherwise return
8094 NULL_RTX. */
8096 static rtx
8097 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8098 tree treeop1 ATTRIBUTE_UNUSED,
8099 tree treeop2 ATTRIBUTE_UNUSED)
8101 rtx insn;
8102 rtx op00, op01, op1, op2;
8103 enum rtx_code comparison_code;
8104 machine_mode comparison_mode;
8105 gimple *srcstmt;
8106 rtx temp;
8107 tree type = TREE_TYPE (treeop1);
8108 int unsignedp = TYPE_UNSIGNED (type);
8109 machine_mode mode = TYPE_MODE (type);
8110 machine_mode orig_mode = mode;
8112 /* If we cannot do a conditional move on the mode, try doing it
8113 with the promoted mode. */
8114 if (!can_conditionally_move_p (mode))
8116 mode = promote_mode (type, mode, &unsignedp);
8117 if (!can_conditionally_move_p (mode))
8118 return NULL_RTX;
8119 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8121 else
8122 temp = assign_temp (type, 0, 1);
8124 start_sequence ();
8125 expand_operands (treeop1, treeop2,
8126 temp, &op1, &op2, EXPAND_NORMAL);
8128 if (TREE_CODE (treeop0) == SSA_NAME
8129 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8131 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8132 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8133 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8134 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8135 comparison_mode = TYPE_MODE (type);
8136 unsignedp = TYPE_UNSIGNED (type);
8137 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8139 else if (COMPARISON_CLASS_P (treeop0))
8141 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8142 enum tree_code cmpcode = TREE_CODE (treeop0);
8143 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8144 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8145 unsignedp = TYPE_UNSIGNED (type);
8146 comparison_mode = TYPE_MODE (type);
8147 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8149 else
8151 op00 = expand_normal (treeop0);
8152 op01 = const0_rtx;
8153 comparison_code = NE;
8154 comparison_mode = GET_MODE (op00);
8155 if (comparison_mode == VOIDmode)
8156 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8159 if (GET_MODE (op1) != mode)
8160 op1 = gen_lowpart (mode, op1);
8162 if (GET_MODE (op2) != mode)
8163 op2 = gen_lowpart (mode, op2);
8165 /* Try to emit the conditional move. */
8166 insn = emit_conditional_move (temp, comparison_code,
8167 op00, op01, comparison_mode,
8168 op1, op2, mode,
8169 unsignedp);
8171 /* If we could do the conditional move, emit the sequence,
8172 and return. */
8173 if (insn)
8175 rtx_insn *seq = get_insns ();
8176 end_sequence ();
8177 emit_insn (seq);
8178 return convert_modes (orig_mode, mode, temp, 0);
8181 /* Otherwise discard the sequence and fall back to code with
8182 branches. */
8183 end_sequence ();
8184 return NULL_RTX;
8188 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8189 enum expand_modifier modifier)
8191 rtx op0, op1, op2, temp;
8192 rtx_code_label *lab;
8193 tree type;
8194 int unsignedp;
8195 machine_mode mode;
8196 enum tree_code code = ops->code;
8197 optab this_optab;
8198 rtx subtarget, original_target;
8199 int ignore;
8200 bool reduce_bit_field;
8201 location_t loc = ops->location;
8202 tree treeop0, treeop1, treeop2;
8203 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8204 ? reduce_to_bit_field_precision ((expr), \
8205 target, \
8206 type) \
8207 : (expr))
8209 type = ops->type;
8210 mode = TYPE_MODE (type);
8211 unsignedp = TYPE_UNSIGNED (type);
8213 treeop0 = ops->op0;
8214 treeop1 = ops->op1;
8215 treeop2 = ops->op2;
8217 /* We should be called only on simple (binary or unary) expressions,
8218 exactly those that are valid in gimple expressions that aren't
8219 GIMPLE_SINGLE_RHS (or invalid). */
8220 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8221 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8222 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8224 ignore = (target == const0_rtx
8225 || ((CONVERT_EXPR_CODE_P (code)
8226 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8227 && TREE_CODE (type) == VOID_TYPE));
8229 /* We should be called only if we need the result. */
8230 gcc_assert (!ignore);
8232 /* An operation in what may be a bit-field type needs the
8233 result to be reduced to the precision of the bit-field type,
8234 which is narrower than that of the type's mode. */
8235 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8236 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8238 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8239 target = 0;
8241 /* Use subtarget as the target for operand 0 of a binary operation. */
8242 subtarget = get_subtarget (target);
8243 original_target = target;
8245 switch (code)
8247 case NON_LVALUE_EXPR:
8248 case PAREN_EXPR:
8249 CASE_CONVERT:
8250 if (treeop0 == error_mark_node)
8251 return const0_rtx;
8253 if (TREE_CODE (type) == UNION_TYPE)
8255 tree valtype = TREE_TYPE (treeop0);
8257 /* If both input and output are BLKmode, this conversion isn't doing
8258 anything except possibly changing memory attribute. */
8259 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8261 rtx result = expand_expr (treeop0, target, tmode,
8262 modifier);
8264 result = copy_rtx (result);
8265 set_mem_attributes (result, type, 0);
8266 return result;
8269 if (target == 0)
8271 if (TYPE_MODE (type) != BLKmode)
8272 target = gen_reg_rtx (TYPE_MODE (type));
8273 else
8274 target = assign_temp (type, 1, 1);
8277 if (MEM_P (target))
8278 /* Store data into beginning of memory target. */
8279 store_expr (treeop0,
8280 adjust_address (target, TYPE_MODE (valtype), 0),
8281 modifier == EXPAND_STACK_PARM,
8282 false, TYPE_REVERSE_STORAGE_ORDER (type));
8284 else
8286 gcc_assert (REG_P (target)
8287 && !TYPE_REVERSE_STORAGE_ORDER (type));
8289 /* Store this field into a union of the proper type. */
8290 store_field (target,
8291 MIN ((int_size_in_bytes (TREE_TYPE
8292 (treeop0))
8293 * BITS_PER_UNIT),
8294 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8295 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8296 false, false);
8299 /* Return the entire union. */
8300 return target;
8303 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8305 op0 = expand_expr (treeop0, target, VOIDmode,
8306 modifier);
8308 /* If the signedness of the conversion differs and OP0 is
8309 a promoted SUBREG, clear that indication since we now
8310 have to do the proper extension. */
8311 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8312 && GET_CODE (op0) == SUBREG)
8313 SUBREG_PROMOTED_VAR_P (op0) = 0;
8315 return REDUCE_BIT_FIELD (op0);
8318 op0 = expand_expr (treeop0, NULL_RTX, mode,
8319 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8320 if (GET_MODE (op0) == mode)
8323 /* If OP0 is a constant, just convert it into the proper mode. */
8324 else if (CONSTANT_P (op0))
8326 tree inner_type = TREE_TYPE (treeop0);
8327 machine_mode inner_mode = GET_MODE (op0);
8329 if (inner_mode == VOIDmode)
8330 inner_mode = TYPE_MODE (inner_type);
8332 if (modifier == EXPAND_INITIALIZER)
8333 op0 = lowpart_subreg (mode, op0, inner_mode);
8334 else
8335 op0= convert_modes (mode, inner_mode, op0,
8336 TYPE_UNSIGNED (inner_type));
8339 else if (modifier == EXPAND_INITIALIZER)
8340 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8342 else if (target == 0)
8343 op0 = convert_to_mode (mode, op0,
8344 TYPE_UNSIGNED (TREE_TYPE
8345 (treeop0)));
8346 else
8348 convert_move (target, op0,
8349 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8350 op0 = target;
8353 return REDUCE_BIT_FIELD (op0);
8355 case ADDR_SPACE_CONVERT_EXPR:
8357 tree treeop0_type = TREE_TYPE (treeop0);
8359 gcc_assert (POINTER_TYPE_P (type));
8360 gcc_assert (POINTER_TYPE_P (treeop0_type));
8362 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8363 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8365 /* Conversions between pointers to the same address space should
8366 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8367 gcc_assert (as_to != as_from);
8369 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8371 /* Ask target code to handle conversion between pointers
8372 to overlapping address spaces. */
8373 if (targetm.addr_space.subset_p (as_to, as_from)
8374 || targetm.addr_space.subset_p (as_from, as_to))
8376 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8378 else
8380 /* For disjoint address spaces, converting anything but a null
8381 pointer invokes undefined behavior. We truncate or extend the
8382 value as if we'd converted via integers, which handles 0 as
8383 required, and all others as the programmer likely expects. */
8384 #ifndef POINTERS_EXTEND_UNSIGNED
8385 const int POINTERS_EXTEND_UNSIGNED = 1;
8386 #endif
8387 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8388 op0, POINTERS_EXTEND_UNSIGNED);
8390 gcc_assert (op0);
8391 return op0;
8394 case POINTER_PLUS_EXPR:
8395 /* Even though the sizetype mode and the pointer's mode can be different
8396 expand is able to handle this correctly and get the correct result out
8397 of the PLUS_EXPR code. */
8398 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8399 if sizetype precision is smaller than pointer precision. */
8400 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8401 treeop1 = fold_convert_loc (loc, type,
8402 fold_convert_loc (loc, ssizetype,
8403 treeop1));
8404 /* If sizetype precision is larger than pointer precision, truncate the
8405 offset to have matching modes. */
8406 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8407 treeop1 = fold_convert_loc (loc, type, treeop1);
8408 /* FALLTHRU */
8410 case PLUS_EXPR:
8411 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8412 something else, make sure we add the register to the constant and
8413 then to the other thing. This case can occur during strength
8414 reduction and doing it this way will produce better code if the
8415 frame pointer or argument pointer is eliminated.
8417 fold-const.c will ensure that the constant is always in the inner
8418 PLUS_EXPR, so the only case we need to do anything about is if
8419 sp, ap, or fp is our second argument, in which case we must swap
8420 the innermost first argument and our second argument. */
8422 if (TREE_CODE (treeop0) == PLUS_EXPR
8423 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8424 && VAR_P (treeop1)
8425 && (DECL_RTL (treeop1) == frame_pointer_rtx
8426 || DECL_RTL (treeop1) == stack_pointer_rtx
8427 || DECL_RTL (treeop1) == arg_pointer_rtx))
8429 gcc_unreachable ();
8432 /* If the result is to be ptr_mode and we are adding an integer to
8433 something, we might be forming a constant. So try to use
8434 plus_constant. If it produces a sum and we can't accept it,
8435 use force_operand. This allows P = &ARR[const] to generate
8436 efficient code on machines where a SYMBOL_REF is not a valid
8437 address.
8439 If this is an EXPAND_SUM call, always return the sum. */
8440 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8441 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8443 if (modifier == EXPAND_STACK_PARM)
8444 target = 0;
8445 if (TREE_CODE (treeop0) == INTEGER_CST
8446 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8447 && TREE_CONSTANT (treeop1))
8449 rtx constant_part;
8450 HOST_WIDE_INT wc;
8451 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8453 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8454 EXPAND_SUM);
8455 /* Use wi::shwi to ensure that the constant is
8456 truncated according to the mode of OP1, then sign extended
8457 to a HOST_WIDE_INT. Using the constant directly can result
8458 in non-canonical RTL in a 64x32 cross compile. */
8459 wc = TREE_INT_CST_LOW (treeop0);
8460 constant_part =
8461 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8462 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8463 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8464 op1 = force_operand (op1, target);
8465 return REDUCE_BIT_FIELD (op1);
8468 else if (TREE_CODE (treeop1) == INTEGER_CST
8469 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8470 && TREE_CONSTANT (treeop0))
8472 rtx constant_part;
8473 HOST_WIDE_INT wc;
8474 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8476 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8477 (modifier == EXPAND_INITIALIZER
8478 ? EXPAND_INITIALIZER : EXPAND_SUM));
8479 if (! CONSTANT_P (op0))
8481 op1 = expand_expr (treeop1, NULL_RTX,
8482 VOIDmode, modifier);
8483 /* Return a PLUS if modifier says it's OK. */
8484 if (modifier == EXPAND_SUM
8485 || modifier == EXPAND_INITIALIZER)
8486 return simplify_gen_binary (PLUS, mode, op0, op1);
8487 goto binop2;
8489 /* Use wi::shwi to ensure that the constant is
8490 truncated according to the mode of OP1, then sign extended
8491 to a HOST_WIDE_INT. Using the constant directly can result
8492 in non-canonical RTL in a 64x32 cross compile. */
8493 wc = TREE_INT_CST_LOW (treeop1);
8494 constant_part
8495 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8496 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8497 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8498 op0 = force_operand (op0, target);
8499 return REDUCE_BIT_FIELD (op0);
8503 /* Use TER to expand pointer addition of a negated value
8504 as pointer subtraction. */
8505 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8506 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8507 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8508 && TREE_CODE (treeop1) == SSA_NAME
8509 && TYPE_MODE (TREE_TYPE (treeop0))
8510 == TYPE_MODE (TREE_TYPE (treeop1)))
8512 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8513 if (def)
8515 treeop1 = gimple_assign_rhs1 (def);
8516 code = MINUS_EXPR;
8517 goto do_minus;
8521 /* No sense saving up arithmetic to be done
8522 if it's all in the wrong mode to form part of an address.
8523 And force_operand won't know whether to sign-extend or
8524 zero-extend. */
8525 if (modifier != EXPAND_INITIALIZER
8526 && (modifier != EXPAND_SUM || mode != ptr_mode))
8528 expand_operands (treeop0, treeop1,
8529 subtarget, &op0, &op1, modifier);
8530 if (op0 == const0_rtx)
8531 return op1;
8532 if (op1 == const0_rtx)
8533 return op0;
8534 goto binop2;
8537 expand_operands (treeop0, treeop1,
8538 subtarget, &op0, &op1, modifier);
8539 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8541 case MINUS_EXPR:
8542 do_minus:
8543 /* For initializers, we are allowed to return a MINUS of two
8544 symbolic constants. Here we handle all cases when both operands
8545 are constant. */
8546 /* Handle difference of two symbolic constants,
8547 for the sake of an initializer. */
8548 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8549 && really_constant_p (treeop0)
8550 && really_constant_p (treeop1))
8552 expand_operands (treeop0, treeop1,
8553 NULL_RTX, &op0, &op1, modifier);
8555 /* If the last operand is a CONST_INT, use plus_constant of
8556 the negated constant. Else make the MINUS. */
8557 if (CONST_INT_P (op1))
8558 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8559 -INTVAL (op1)));
8560 else
8561 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8564 /* No sense saving up arithmetic to be done
8565 if it's all in the wrong mode to form part of an address.
8566 And force_operand won't know whether to sign-extend or
8567 zero-extend. */
8568 if (modifier != EXPAND_INITIALIZER
8569 && (modifier != EXPAND_SUM || mode != ptr_mode))
8570 goto binop;
8572 expand_operands (treeop0, treeop1,
8573 subtarget, &op0, &op1, modifier);
8575 /* Convert A - const to A + (-const). */
8576 if (CONST_INT_P (op1))
8578 op1 = negate_rtx (mode, op1);
8579 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8582 goto binop2;
8584 case WIDEN_MULT_PLUS_EXPR:
8585 case WIDEN_MULT_MINUS_EXPR:
8586 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8587 op2 = expand_normal (treeop2);
8588 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8589 target, unsignedp);
8590 return target;
8592 case WIDEN_MULT_EXPR:
8593 /* If first operand is constant, swap them.
8594 Thus the following special case checks need only
8595 check the second operand. */
8596 if (TREE_CODE (treeop0) == INTEGER_CST)
8597 std::swap (treeop0, treeop1);
8599 /* First, check if we have a multiplication of one signed and one
8600 unsigned operand. */
8601 if (TREE_CODE (treeop1) != INTEGER_CST
8602 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8603 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8605 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8606 this_optab = usmul_widen_optab;
8607 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8608 != CODE_FOR_nothing)
8610 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8611 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8612 EXPAND_NORMAL);
8613 else
8614 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8615 EXPAND_NORMAL);
8616 /* op0 and op1 might still be constant, despite the above
8617 != INTEGER_CST check. Handle it. */
8618 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8620 op0 = convert_modes (innermode, mode, op0, true);
8621 op1 = convert_modes (innermode, mode, op1, false);
8622 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8623 target, unsignedp));
8625 goto binop3;
8628 /* Check for a multiplication with matching signedness. */
8629 else if ((TREE_CODE (treeop1) == INTEGER_CST
8630 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8631 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8632 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8634 tree op0type = TREE_TYPE (treeop0);
8635 machine_mode innermode = TYPE_MODE (op0type);
8636 bool zextend_p = TYPE_UNSIGNED (op0type);
8637 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8638 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8640 if (TREE_CODE (treeop0) != INTEGER_CST)
8642 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8643 != CODE_FOR_nothing)
8645 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8646 EXPAND_NORMAL);
8647 /* op0 and op1 might still be constant, despite the above
8648 != INTEGER_CST check. Handle it. */
8649 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8651 widen_mult_const:
8652 op0 = convert_modes (innermode, mode, op0, zextend_p);
8654 = convert_modes (innermode, mode, op1,
8655 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8656 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8657 target,
8658 unsignedp));
8660 temp = expand_widening_mult (mode, op0, op1, target,
8661 unsignedp, this_optab);
8662 return REDUCE_BIT_FIELD (temp);
8664 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8665 != CODE_FOR_nothing
8666 && innermode == word_mode)
8668 rtx htem, hipart;
8669 op0 = expand_normal (treeop0);
8670 if (TREE_CODE (treeop1) == INTEGER_CST)
8671 op1 = convert_modes (innermode, mode,
8672 expand_normal (treeop1),
8673 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8674 else
8675 op1 = expand_normal (treeop1);
8676 /* op0 and op1 might still be constant, despite the above
8677 != INTEGER_CST check. Handle it. */
8678 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8679 goto widen_mult_const;
8680 temp = expand_binop (mode, other_optab, op0, op1, target,
8681 unsignedp, OPTAB_LIB_WIDEN);
8682 hipart = gen_highpart (innermode, temp);
8683 htem = expand_mult_highpart_adjust (innermode, hipart,
8684 op0, op1, hipart,
8685 zextend_p);
8686 if (htem != hipart)
8687 emit_move_insn (hipart, htem);
8688 return REDUCE_BIT_FIELD (temp);
8692 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8693 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8694 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8695 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8697 case FMA_EXPR:
8699 optab opt = fma_optab;
8700 gimple *def0, *def2;
8702 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8703 call. */
8704 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8706 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8707 tree call_expr;
8709 gcc_assert (fn != NULL_TREE);
8710 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8711 return expand_builtin (call_expr, target, subtarget, mode, false);
8714 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8715 /* The multiplication is commutative - look at its 2nd operand
8716 if the first isn't fed by a negate. */
8717 if (!def0)
8719 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8720 /* Swap operands if the 2nd operand is fed by a negate. */
8721 if (def0)
8722 std::swap (treeop0, treeop1);
8724 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8726 op0 = op2 = NULL;
8728 if (def0 && def2
8729 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8731 opt = fnms_optab;
8732 op0 = expand_normal (gimple_assign_rhs1 (def0));
8733 op2 = expand_normal (gimple_assign_rhs1 (def2));
8735 else if (def0
8736 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8738 opt = fnma_optab;
8739 op0 = expand_normal (gimple_assign_rhs1 (def0));
8741 else if (def2
8742 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8744 opt = fms_optab;
8745 op2 = expand_normal (gimple_assign_rhs1 (def2));
8748 if (op0 == NULL)
8749 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8750 if (op2 == NULL)
8751 op2 = expand_normal (treeop2);
8752 op1 = expand_normal (treeop1);
8754 return expand_ternary_op (TYPE_MODE (type), opt,
8755 op0, op1, op2, target, 0);
8758 case MULT_EXPR:
8759 /* If this is a fixed-point operation, then we cannot use the code
8760 below because "expand_mult" doesn't support sat/no-sat fixed-point
8761 multiplications. */
8762 if (ALL_FIXED_POINT_MODE_P (mode))
8763 goto binop;
8765 /* If first operand is constant, swap them.
8766 Thus the following special case checks need only
8767 check the second operand. */
8768 if (TREE_CODE (treeop0) == INTEGER_CST)
8769 std::swap (treeop0, treeop1);
8771 /* Attempt to return something suitable for generating an
8772 indexed address, for machines that support that. */
8774 if (modifier == EXPAND_SUM && mode == ptr_mode
8775 && tree_fits_shwi_p (treeop1))
8777 tree exp1 = treeop1;
8779 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8780 EXPAND_SUM);
8782 if (!REG_P (op0))
8783 op0 = force_operand (op0, NULL_RTX);
8784 if (!REG_P (op0))
8785 op0 = copy_to_mode_reg (mode, op0);
8787 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8788 gen_int_mode (tree_to_shwi (exp1),
8789 TYPE_MODE (TREE_TYPE (exp1)))));
8792 if (modifier == EXPAND_STACK_PARM)
8793 target = 0;
8795 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8796 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8798 case TRUNC_DIV_EXPR:
8799 case FLOOR_DIV_EXPR:
8800 case CEIL_DIV_EXPR:
8801 case ROUND_DIV_EXPR:
8802 case EXACT_DIV_EXPR:
8803 /* If this is a fixed-point operation, then we cannot use the code
8804 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8805 divisions. */
8806 if (ALL_FIXED_POINT_MODE_P (mode))
8807 goto binop;
8809 if (modifier == EXPAND_STACK_PARM)
8810 target = 0;
8811 /* Possible optimization: compute the dividend with EXPAND_SUM
8812 then if the divisor is constant can optimize the case
8813 where some terms of the dividend have coeffs divisible by it. */
8814 expand_operands (treeop0, treeop1,
8815 subtarget, &op0, &op1, EXPAND_NORMAL);
8816 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8818 case RDIV_EXPR:
8819 goto binop;
8821 case MULT_HIGHPART_EXPR:
8822 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8823 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8824 gcc_assert (temp);
8825 return temp;
8827 case TRUNC_MOD_EXPR:
8828 case FLOOR_MOD_EXPR:
8829 case CEIL_MOD_EXPR:
8830 case ROUND_MOD_EXPR:
8831 if (modifier == EXPAND_STACK_PARM)
8832 target = 0;
8833 expand_operands (treeop0, treeop1,
8834 subtarget, &op0, &op1, EXPAND_NORMAL);
8835 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8837 case FIXED_CONVERT_EXPR:
8838 op0 = expand_normal (treeop0);
8839 if (target == 0 || modifier == EXPAND_STACK_PARM)
8840 target = gen_reg_rtx (mode);
8842 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8843 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8844 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8845 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8846 else
8847 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8848 return target;
8850 case FIX_TRUNC_EXPR:
8851 op0 = expand_normal (treeop0);
8852 if (target == 0 || modifier == EXPAND_STACK_PARM)
8853 target = gen_reg_rtx (mode);
8854 expand_fix (target, op0, unsignedp);
8855 return target;
8857 case FLOAT_EXPR:
8858 op0 = expand_normal (treeop0);
8859 if (target == 0 || modifier == EXPAND_STACK_PARM)
8860 target = gen_reg_rtx (mode);
8861 /* expand_float can't figure out what to do if FROM has VOIDmode.
8862 So give it the correct mode. With -O, cse will optimize this. */
8863 if (GET_MODE (op0) == VOIDmode)
8864 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8865 op0);
8866 expand_float (target, op0,
8867 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8868 return target;
8870 case NEGATE_EXPR:
8871 op0 = expand_expr (treeop0, subtarget,
8872 VOIDmode, EXPAND_NORMAL);
8873 if (modifier == EXPAND_STACK_PARM)
8874 target = 0;
8875 temp = expand_unop (mode,
8876 optab_for_tree_code (NEGATE_EXPR, type,
8877 optab_default),
8878 op0, target, 0);
8879 gcc_assert (temp);
8880 return REDUCE_BIT_FIELD (temp);
8882 case ABS_EXPR:
8883 op0 = expand_expr (treeop0, subtarget,
8884 VOIDmode, EXPAND_NORMAL);
8885 if (modifier == EXPAND_STACK_PARM)
8886 target = 0;
8888 /* ABS_EXPR is not valid for complex arguments. */
8889 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8890 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8892 /* Unsigned abs is simply the operand. Testing here means we don't
8893 risk generating incorrect code below. */
8894 if (TYPE_UNSIGNED (type))
8895 return op0;
8897 return expand_abs (mode, op0, target, unsignedp,
8898 safe_from_p (target, treeop0, 1));
8900 case MAX_EXPR:
8901 case MIN_EXPR:
8902 target = original_target;
8903 if (target == 0
8904 || modifier == EXPAND_STACK_PARM
8905 || (MEM_P (target) && MEM_VOLATILE_P (target))
8906 || GET_MODE (target) != mode
8907 || (REG_P (target)
8908 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8909 target = gen_reg_rtx (mode);
8910 expand_operands (treeop0, treeop1,
8911 target, &op0, &op1, EXPAND_NORMAL);
8913 /* First try to do it with a special MIN or MAX instruction.
8914 If that does not win, use a conditional jump to select the proper
8915 value. */
8916 this_optab = optab_for_tree_code (code, type, optab_default);
8917 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8918 OPTAB_WIDEN);
8919 if (temp != 0)
8920 return temp;
8922 /* At this point, a MEM target is no longer useful; we will get better
8923 code without it. */
8925 if (! REG_P (target))
8926 target = gen_reg_rtx (mode);
8928 /* If op1 was placed in target, swap op0 and op1. */
8929 if (target != op0 && target == op1)
8930 std::swap (op0, op1);
8932 /* We generate better code and avoid problems with op1 mentioning
8933 target by forcing op1 into a pseudo if it isn't a constant. */
8934 if (! CONSTANT_P (op1))
8935 op1 = force_reg (mode, op1);
8938 enum rtx_code comparison_code;
8939 rtx cmpop1 = op1;
8941 if (code == MAX_EXPR)
8942 comparison_code = unsignedp ? GEU : GE;
8943 else
8944 comparison_code = unsignedp ? LEU : LE;
8946 /* Canonicalize to comparisons against 0. */
8947 if (op1 == const1_rtx)
8949 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8950 or (a != 0 ? a : 1) for unsigned.
8951 For MIN we are safe converting (a <= 1 ? a : 1)
8952 into (a <= 0 ? a : 1) */
8953 cmpop1 = const0_rtx;
8954 if (code == MAX_EXPR)
8955 comparison_code = unsignedp ? NE : GT;
8957 if (op1 == constm1_rtx && !unsignedp)
8959 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8960 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8961 cmpop1 = const0_rtx;
8962 if (code == MIN_EXPR)
8963 comparison_code = LT;
8966 /* Use a conditional move if possible. */
8967 if (can_conditionally_move_p (mode))
8969 rtx insn;
8971 start_sequence ();
8973 /* Try to emit the conditional move. */
8974 insn = emit_conditional_move (target, comparison_code,
8975 op0, cmpop1, mode,
8976 op0, op1, mode,
8977 unsignedp);
8979 /* If we could do the conditional move, emit the sequence,
8980 and return. */
8981 if (insn)
8983 rtx_insn *seq = get_insns ();
8984 end_sequence ();
8985 emit_insn (seq);
8986 return target;
8989 /* Otherwise discard the sequence and fall back to code with
8990 branches. */
8991 end_sequence ();
8994 if (target != op0)
8995 emit_move_insn (target, op0);
8997 lab = gen_label_rtx ();
8998 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8999 unsignedp, mode, NULL_RTX, NULL, lab,
9000 -1);
9002 emit_move_insn (target, op1);
9003 emit_label (lab);
9004 return target;
9006 case BIT_NOT_EXPR:
9007 op0 = expand_expr (treeop0, subtarget,
9008 VOIDmode, EXPAND_NORMAL);
9009 if (modifier == EXPAND_STACK_PARM)
9010 target = 0;
9011 /* In case we have to reduce the result to bitfield precision
9012 for unsigned bitfield expand this as XOR with a proper constant
9013 instead. */
9014 if (reduce_bit_field && TYPE_UNSIGNED (type))
9016 wide_int mask = wi::mask (TYPE_PRECISION (type),
9017 false, GET_MODE_PRECISION (mode));
9019 temp = expand_binop (mode, xor_optab, op0,
9020 immed_wide_int_const (mask, mode),
9021 target, 1, OPTAB_LIB_WIDEN);
9023 else
9024 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9025 gcc_assert (temp);
9026 return temp;
9028 /* ??? Can optimize bitwise operations with one arg constant.
9029 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9030 and (a bitwise1 b) bitwise2 b (etc)
9031 but that is probably not worth while. */
9033 case BIT_AND_EXPR:
9034 case BIT_IOR_EXPR:
9035 case BIT_XOR_EXPR:
9036 goto binop;
9038 case LROTATE_EXPR:
9039 case RROTATE_EXPR:
9040 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9041 || (GET_MODE_PRECISION (TYPE_MODE (type))
9042 == TYPE_PRECISION (type)));
9043 /* fall through */
9045 case LSHIFT_EXPR:
9046 case RSHIFT_EXPR:
9048 /* If this is a fixed-point operation, then we cannot use the code
9049 below because "expand_shift" doesn't support sat/no-sat fixed-point
9050 shifts. */
9051 if (ALL_FIXED_POINT_MODE_P (mode))
9052 goto binop;
9054 if (! safe_from_p (subtarget, treeop1, 1))
9055 subtarget = 0;
9056 if (modifier == EXPAND_STACK_PARM)
9057 target = 0;
9058 op0 = expand_expr (treeop0, subtarget,
9059 VOIDmode, EXPAND_NORMAL);
9061 /* Left shift optimization when shifting across word_size boundary.
9063 If mode == GET_MODE_WIDER_MODE (word_mode), then normally there isn't
9064 native instruction to support this wide mode left shift. Given below
9065 scenario:
9067 Type A = (Type) B << C
9069 |< T >|
9070 | dest_high | dest_low |
9072 | word_size |
9074 If the shift amount C caused we shift B to across the word size
9075 boundary, i.e part of B shifted into high half of destination
9076 register, and part of B remains in the low half, then GCC will use
9077 the following left shift expand logic:
9079 1. Initialize dest_low to B.
9080 2. Initialize every bit of dest_high to the sign bit of B.
9081 3. Logic left shift dest_low by C bit to finalize dest_low.
9082 The value of dest_low before this shift is kept in a temp D.
9083 4. Logic left shift dest_high by C.
9084 5. Logic right shift D by (word_size - C).
9085 6. Or the result of 4 and 5 to finalize dest_high.
9087 While, by checking gimple statements, if operand B is coming from
9088 signed extension, then we can simplify above expand logic into:
9090 1. dest_high = src_low >> (word_size - C).
9091 2. dest_low = src_low << C.
9093 We can use one arithmetic right shift to finish all the purpose of
9094 steps 2, 4, 5, 6, thus we reduce the steps needed from 6 into 2. */
9096 temp = NULL_RTX;
9097 if (code == LSHIFT_EXPR
9098 && target
9099 && REG_P (target)
9100 && ! unsignedp
9101 && mode == GET_MODE_WIDER_MODE (word_mode)
9102 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9103 && TREE_CONSTANT (treeop1)
9104 && TREE_CODE (treeop0) == SSA_NAME)
9106 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9107 if (is_gimple_assign (def)
9108 && gimple_assign_rhs_code (def) == NOP_EXPR)
9110 machine_mode rmode = TYPE_MODE
9111 (TREE_TYPE (gimple_assign_rhs1 (def)));
9113 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9114 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9115 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9116 >= GET_MODE_BITSIZE (word_mode)))
9118 rtx_insn *seq, *seq_old;
9119 unsigned int high_off = subreg_highpart_offset (word_mode,
9120 mode);
9121 rtx low = lowpart_subreg (word_mode, op0, mode);
9122 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9123 rtx dest_high = simplify_gen_subreg (word_mode, target,
9124 mode, high_off);
9125 HOST_WIDE_INT ramount = (BITS_PER_WORD
9126 - TREE_INT_CST_LOW (treeop1));
9127 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9129 start_sequence ();
9130 /* dest_high = src_low >> (word_size - C). */
9131 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9132 rshift, dest_high, unsignedp);
9133 if (temp != dest_high)
9134 emit_move_insn (dest_high, temp);
9136 /* dest_low = src_low << C. */
9137 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9138 treeop1, dest_low, unsignedp);
9139 if (temp != dest_low)
9140 emit_move_insn (dest_low, temp);
9142 seq = get_insns ();
9143 end_sequence ();
9144 temp = target ;
9146 if (have_insn_for (ASHIFT, mode))
9148 bool speed_p = optimize_insn_for_speed_p ();
9149 start_sequence ();
9150 rtx ret_old = expand_variable_shift (code, mode, op0,
9151 treeop1, target,
9152 unsignedp);
9154 seq_old = get_insns ();
9155 end_sequence ();
9156 if (seq_cost (seq, speed_p)
9157 >= seq_cost (seq_old, speed_p))
9159 seq = seq_old;
9160 temp = ret_old;
9163 emit_insn (seq);
9168 if (temp == NULL_RTX)
9169 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9170 unsignedp);
9171 if (code == LSHIFT_EXPR)
9172 temp = REDUCE_BIT_FIELD (temp);
9173 return temp;
9176 /* Could determine the answer when only additive constants differ. Also,
9177 the addition of one can be handled by changing the condition. */
9178 case LT_EXPR:
9179 case LE_EXPR:
9180 case GT_EXPR:
9181 case GE_EXPR:
9182 case EQ_EXPR:
9183 case NE_EXPR:
9184 case UNORDERED_EXPR:
9185 case ORDERED_EXPR:
9186 case UNLT_EXPR:
9187 case UNLE_EXPR:
9188 case UNGT_EXPR:
9189 case UNGE_EXPR:
9190 case UNEQ_EXPR:
9191 case LTGT_EXPR:
9193 temp = do_store_flag (ops,
9194 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9195 tmode != VOIDmode ? tmode : mode);
9196 if (temp)
9197 return temp;
9199 /* Use a compare and a jump for BLKmode comparisons, or for function
9200 type comparisons is have_canonicalize_funcptr_for_compare. */
9202 if ((target == 0
9203 || modifier == EXPAND_STACK_PARM
9204 || ! safe_from_p (target, treeop0, 1)
9205 || ! safe_from_p (target, treeop1, 1)
9206 /* Make sure we don't have a hard reg (such as function's return
9207 value) live across basic blocks, if not optimizing. */
9208 || (!optimize && REG_P (target)
9209 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9210 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9212 emit_move_insn (target, const0_rtx);
9214 rtx_code_label *lab1 = gen_label_rtx ();
9215 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9217 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9218 emit_move_insn (target, constm1_rtx);
9219 else
9220 emit_move_insn (target, const1_rtx);
9222 emit_label (lab1);
9223 return target;
9225 case COMPLEX_EXPR:
9226 /* Get the rtx code of the operands. */
9227 op0 = expand_normal (treeop0);
9228 op1 = expand_normal (treeop1);
9230 if (!target)
9231 target = gen_reg_rtx (TYPE_MODE (type));
9232 else
9233 /* If target overlaps with op1, then either we need to force
9234 op1 into a pseudo (if target also overlaps with op0),
9235 or write the complex parts in reverse order. */
9236 switch (GET_CODE (target))
9238 case CONCAT:
9239 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9241 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9243 complex_expr_force_op1:
9244 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9245 emit_move_insn (temp, op1);
9246 op1 = temp;
9247 break;
9249 complex_expr_swap_order:
9250 /* Move the imaginary (op1) and real (op0) parts to their
9251 location. */
9252 write_complex_part (target, op1, true);
9253 write_complex_part (target, op0, false);
9255 return target;
9257 break;
9258 case MEM:
9259 temp = adjust_address_nv (target,
9260 GET_MODE_INNER (GET_MODE (target)), 0);
9261 if (reg_overlap_mentioned_p (temp, op1))
9263 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9264 temp = adjust_address_nv (target, imode,
9265 GET_MODE_SIZE (imode));
9266 if (reg_overlap_mentioned_p (temp, op0))
9267 goto complex_expr_force_op1;
9268 goto complex_expr_swap_order;
9270 break;
9271 default:
9272 if (reg_overlap_mentioned_p (target, op1))
9274 if (reg_overlap_mentioned_p (target, op0))
9275 goto complex_expr_force_op1;
9276 goto complex_expr_swap_order;
9278 break;
9281 /* Move the real (op0) and imaginary (op1) parts to their location. */
9282 write_complex_part (target, op0, false);
9283 write_complex_part (target, op1, true);
9285 return target;
9287 case WIDEN_SUM_EXPR:
9289 tree oprnd0 = treeop0;
9290 tree oprnd1 = treeop1;
9292 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9293 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9294 target, unsignedp);
9295 return target;
9298 case REDUC_MAX_EXPR:
9299 case REDUC_MIN_EXPR:
9300 case REDUC_PLUS_EXPR:
9302 op0 = expand_normal (treeop0);
9303 this_optab = optab_for_tree_code (code, type, optab_default);
9304 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9306 struct expand_operand ops[2];
9307 enum insn_code icode = optab_handler (this_optab, vec_mode);
9309 create_output_operand (&ops[0], target, mode);
9310 create_input_operand (&ops[1], op0, vec_mode);
9311 expand_insn (icode, 2, ops);
9312 target = ops[0].value;
9313 if (GET_MODE (target) != mode)
9314 return gen_lowpart (tmode, target);
9315 return target;
9318 case VEC_UNPACK_HI_EXPR:
9319 case VEC_UNPACK_LO_EXPR:
9321 op0 = expand_normal (treeop0);
9322 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9323 target, unsignedp);
9324 gcc_assert (temp);
9325 return temp;
9328 case VEC_UNPACK_FLOAT_HI_EXPR:
9329 case VEC_UNPACK_FLOAT_LO_EXPR:
9331 op0 = expand_normal (treeop0);
9332 /* The signedness is determined from input operand. */
9333 temp = expand_widen_pattern_expr
9334 (ops, op0, NULL_RTX, NULL_RTX,
9335 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9337 gcc_assert (temp);
9338 return temp;
9341 case VEC_WIDEN_MULT_HI_EXPR:
9342 case VEC_WIDEN_MULT_LO_EXPR:
9343 case VEC_WIDEN_MULT_EVEN_EXPR:
9344 case VEC_WIDEN_MULT_ODD_EXPR:
9345 case VEC_WIDEN_LSHIFT_HI_EXPR:
9346 case VEC_WIDEN_LSHIFT_LO_EXPR:
9347 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9348 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9349 target, unsignedp);
9350 gcc_assert (target);
9351 return target;
9353 case VEC_PACK_TRUNC_EXPR:
9354 case VEC_PACK_SAT_EXPR:
9355 case VEC_PACK_FIX_TRUNC_EXPR:
9356 mode = TYPE_MODE (TREE_TYPE (treeop0));
9357 goto binop;
9359 case VEC_PERM_EXPR:
9360 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9361 op2 = expand_normal (treeop2);
9363 /* Careful here: if the target doesn't support integral vector modes,
9364 a constant selection vector could wind up smooshed into a normal
9365 integral constant. */
9366 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9368 tree sel_type = TREE_TYPE (treeop2);
9369 machine_mode vmode
9370 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9371 TYPE_VECTOR_SUBPARTS (sel_type));
9372 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9373 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9374 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9376 else
9377 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9379 temp = expand_vec_perm (mode, op0, op1, op2, target);
9380 gcc_assert (temp);
9381 return temp;
9383 case DOT_PROD_EXPR:
9385 tree oprnd0 = treeop0;
9386 tree oprnd1 = treeop1;
9387 tree oprnd2 = treeop2;
9388 rtx op2;
9390 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9391 op2 = expand_normal (oprnd2);
9392 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9393 target, unsignedp);
9394 return target;
9397 case SAD_EXPR:
9399 tree oprnd0 = treeop0;
9400 tree oprnd1 = treeop1;
9401 tree oprnd2 = treeop2;
9402 rtx op2;
9404 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9405 op2 = expand_normal (oprnd2);
9406 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9407 target, unsignedp);
9408 return target;
9411 case REALIGN_LOAD_EXPR:
9413 tree oprnd0 = treeop0;
9414 tree oprnd1 = treeop1;
9415 tree oprnd2 = treeop2;
9416 rtx op2;
9418 this_optab = optab_for_tree_code (code, type, optab_default);
9419 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9420 op2 = expand_normal (oprnd2);
9421 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9422 target, unsignedp);
9423 gcc_assert (temp);
9424 return temp;
9427 case COND_EXPR:
9429 /* A COND_EXPR with its type being VOID_TYPE represents a
9430 conditional jump and is handled in
9431 expand_gimple_cond_expr. */
9432 gcc_assert (!VOID_TYPE_P (type));
9434 /* Note that COND_EXPRs whose type is a structure or union
9435 are required to be constructed to contain assignments of
9436 a temporary variable, so that we can evaluate them here
9437 for side effect only. If type is void, we must do likewise. */
9439 gcc_assert (!TREE_ADDRESSABLE (type)
9440 && !ignore
9441 && TREE_TYPE (treeop1) != void_type_node
9442 && TREE_TYPE (treeop2) != void_type_node);
9444 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9445 if (temp)
9446 return temp;
9448 /* If we are not to produce a result, we have no target. Otherwise,
9449 if a target was specified use it; it will not be used as an
9450 intermediate target unless it is safe. If no target, use a
9451 temporary. */
9453 if (modifier != EXPAND_STACK_PARM
9454 && original_target
9455 && safe_from_p (original_target, treeop0, 1)
9456 && GET_MODE (original_target) == mode
9457 && !MEM_P (original_target))
9458 temp = original_target;
9459 else
9460 temp = assign_temp (type, 0, 1);
9462 do_pending_stack_adjust ();
9463 NO_DEFER_POP;
9464 rtx_code_label *lab0 = gen_label_rtx ();
9465 rtx_code_label *lab1 = gen_label_rtx ();
9466 jumpifnot (treeop0, lab0, -1);
9467 store_expr (treeop1, temp,
9468 modifier == EXPAND_STACK_PARM,
9469 false, false);
9471 emit_jump_insn (targetm.gen_jump (lab1));
9472 emit_barrier ();
9473 emit_label (lab0);
9474 store_expr (treeop2, temp,
9475 modifier == EXPAND_STACK_PARM,
9476 false, false);
9478 emit_label (lab1);
9479 OK_DEFER_POP;
9480 return temp;
9483 case VEC_COND_EXPR:
9484 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9485 return target;
9487 case BIT_INSERT_EXPR:
9489 unsigned bitpos = tree_to_uhwi (treeop2);
9490 unsigned bitsize;
9491 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9492 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9493 else
9494 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9495 rtx op0 = expand_normal (treeop0);
9496 rtx op1 = expand_normal (treeop1);
9497 rtx dst = gen_reg_rtx (mode);
9498 emit_move_insn (dst, op0);
9499 store_bit_field (dst, bitsize, bitpos, 0, 0,
9500 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9501 return dst;
9504 default:
9505 gcc_unreachable ();
9508 /* Here to do an ordinary binary operator. */
9509 binop:
9510 expand_operands (treeop0, treeop1,
9511 subtarget, &op0, &op1, EXPAND_NORMAL);
9512 binop2:
9513 this_optab = optab_for_tree_code (code, type, optab_default);
9514 binop3:
9515 if (modifier == EXPAND_STACK_PARM)
9516 target = 0;
9517 temp = expand_binop (mode, this_optab, op0, op1, target,
9518 unsignedp, OPTAB_LIB_WIDEN);
9519 gcc_assert (temp);
9520 /* Bitwise operations do not need bitfield reduction as we expect their
9521 operands being properly truncated. */
9522 if (code == BIT_XOR_EXPR
9523 || code == BIT_AND_EXPR
9524 || code == BIT_IOR_EXPR)
9525 return temp;
9526 return REDUCE_BIT_FIELD (temp);
9528 #undef REDUCE_BIT_FIELD
9531 /* Return TRUE if expression STMT is suitable for replacement.
9532 Never consider memory loads as replaceable, because those don't ever lead
9533 into constant expressions. */
9535 static bool
9536 stmt_is_replaceable_p (gimple *stmt)
9538 if (ssa_is_replaceable_p (stmt))
9540 /* Don't move around loads. */
9541 if (!gimple_assign_single_p (stmt)
9542 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9543 return true;
9545 return false;
9549 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9550 enum expand_modifier modifier, rtx *alt_rtl,
9551 bool inner_reference_p)
9553 rtx op0, op1, temp, decl_rtl;
9554 tree type;
9555 int unsignedp;
9556 machine_mode mode, dmode;
9557 enum tree_code code = TREE_CODE (exp);
9558 rtx subtarget, original_target;
9559 int ignore;
9560 tree context;
9561 bool reduce_bit_field;
9562 location_t loc = EXPR_LOCATION (exp);
9563 struct separate_ops ops;
9564 tree treeop0, treeop1, treeop2;
9565 tree ssa_name = NULL_TREE;
9566 gimple *g;
9568 type = TREE_TYPE (exp);
9569 mode = TYPE_MODE (type);
9570 unsignedp = TYPE_UNSIGNED (type);
9572 treeop0 = treeop1 = treeop2 = NULL_TREE;
9573 if (!VL_EXP_CLASS_P (exp))
9574 switch (TREE_CODE_LENGTH (code))
9576 default:
9577 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9578 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9579 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9580 case 0: break;
9582 ops.code = code;
9583 ops.type = type;
9584 ops.op0 = treeop0;
9585 ops.op1 = treeop1;
9586 ops.op2 = treeop2;
9587 ops.location = loc;
9589 ignore = (target == const0_rtx
9590 || ((CONVERT_EXPR_CODE_P (code)
9591 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9592 && TREE_CODE (type) == VOID_TYPE));
9594 /* An operation in what may be a bit-field type needs the
9595 result to be reduced to the precision of the bit-field type,
9596 which is narrower than that of the type's mode. */
9597 reduce_bit_field = (!ignore
9598 && INTEGRAL_TYPE_P (type)
9599 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9601 /* If we are going to ignore this result, we need only do something
9602 if there is a side-effect somewhere in the expression. If there
9603 is, short-circuit the most common cases here. Note that we must
9604 not call expand_expr with anything but const0_rtx in case this
9605 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9607 if (ignore)
9609 if (! TREE_SIDE_EFFECTS (exp))
9610 return const0_rtx;
9612 /* Ensure we reference a volatile object even if value is ignored, but
9613 don't do this if all we are doing is taking its address. */
9614 if (TREE_THIS_VOLATILE (exp)
9615 && TREE_CODE (exp) != FUNCTION_DECL
9616 && mode != VOIDmode && mode != BLKmode
9617 && modifier != EXPAND_CONST_ADDRESS)
9619 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9620 if (MEM_P (temp))
9621 copy_to_reg (temp);
9622 return const0_rtx;
9625 if (TREE_CODE_CLASS (code) == tcc_unary
9626 || code == BIT_FIELD_REF
9627 || code == COMPONENT_REF
9628 || code == INDIRECT_REF)
9629 return expand_expr (treeop0, const0_rtx, VOIDmode,
9630 modifier);
9632 else if (TREE_CODE_CLASS (code) == tcc_binary
9633 || TREE_CODE_CLASS (code) == tcc_comparison
9634 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9636 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9637 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9638 return const0_rtx;
9641 target = 0;
9644 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9645 target = 0;
9647 /* Use subtarget as the target for operand 0 of a binary operation. */
9648 subtarget = get_subtarget (target);
9649 original_target = target;
9651 switch (code)
9653 case LABEL_DECL:
9655 tree function = decl_function_context (exp);
9657 temp = label_rtx (exp);
9658 temp = gen_rtx_LABEL_REF (Pmode, temp);
9660 if (function != current_function_decl
9661 && function != 0)
9662 LABEL_REF_NONLOCAL_P (temp) = 1;
9664 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9665 return temp;
9668 case SSA_NAME:
9669 /* ??? ivopts calls expander, without any preparation from
9670 out-of-ssa. So fake instructions as if this was an access to the
9671 base variable. This unnecessarily allocates a pseudo, see how we can
9672 reuse it, if partition base vars have it set already. */
9673 if (!currently_expanding_to_rtl)
9675 tree var = SSA_NAME_VAR (exp);
9676 if (var && DECL_RTL_SET_P (var))
9677 return DECL_RTL (var);
9678 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9679 LAST_VIRTUAL_REGISTER + 1);
9682 g = get_gimple_for_ssa_name (exp);
9683 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9684 if (g == NULL
9685 && modifier == EXPAND_INITIALIZER
9686 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9687 && (optimize || !SSA_NAME_VAR (exp)
9688 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9689 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9690 g = SSA_NAME_DEF_STMT (exp);
9691 if (g)
9693 rtx r;
9694 location_t saved_loc = curr_insn_location ();
9695 location_t loc = gimple_location (g);
9696 if (loc != UNKNOWN_LOCATION)
9697 set_curr_insn_location (loc);
9698 ops.code = gimple_assign_rhs_code (g);
9699 switch (get_gimple_rhs_class (ops.code))
9701 case GIMPLE_TERNARY_RHS:
9702 ops.op2 = gimple_assign_rhs3 (g);
9703 /* Fallthru */
9704 case GIMPLE_BINARY_RHS:
9705 ops.op1 = gimple_assign_rhs2 (g);
9707 /* Try to expand conditonal compare. */
9708 if (targetm.gen_ccmp_first)
9710 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9711 r = expand_ccmp_expr (g);
9712 if (r)
9713 break;
9715 /* Fallthru */
9716 case GIMPLE_UNARY_RHS:
9717 ops.op0 = gimple_assign_rhs1 (g);
9718 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9719 ops.location = loc;
9720 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9721 break;
9722 case GIMPLE_SINGLE_RHS:
9724 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9725 tmode, modifier, NULL, inner_reference_p);
9726 break;
9728 default:
9729 gcc_unreachable ();
9731 set_curr_insn_location (saved_loc);
9732 if (REG_P (r) && !REG_EXPR (r))
9733 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9734 return r;
9737 ssa_name = exp;
9738 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9739 exp = SSA_NAME_VAR (ssa_name);
9740 goto expand_decl_rtl;
9742 case PARM_DECL:
9743 case VAR_DECL:
9744 /* If a static var's type was incomplete when the decl was written,
9745 but the type is complete now, lay out the decl now. */
9746 if (DECL_SIZE (exp) == 0
9747 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9748 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9749 layout_decl (exp, 0);
9751 /* fall through */
9753 case FUNCTION_DECL:
9754 case RESULT_DECL:
9755 decl_rtl = DECL_RTL (exp);
9756 expand_decl_rtl:
9757 gcc_assert (decl_rtl);
9759 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9760 settings for VECTOR_TYPE_P that might switch for the function. */
9761 if (currently_expanding_to_rtl
9762 && code == VAR_DECL && MEM_P (decl_rtl)
9763 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9764 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9765 else
9766 decl_rtl = copy_rtx (decl_rtl);
9768 /* Record writes to register variables. */
9769 if (modifier == EXPAND_WRITE
9770 && REG_P (decl_rtl)
9771 && HARD_REGISTER_P (decl_rtl))
9772 add_to_hard_reg_set (&crtl->asm_clobbers,
9773 GET_MODE (decl_rtl), REGNO (decl_rtl));
9775 /* Ensure variable marked as used even if it doesn't go through
9776 a parser. If it hasn't be used yet, write out an external
9777 definition. */
9778 if (exp)
9779 TREE_USED (exp) = 1;
9781 /* Show we haven't gotten RTL for this yet. */
9782 temp = 0;
9784 /* Variables inherited from containing functions should have
9785 been lowered by this point. */
9786 if (exp)
9787 context = decl_function_context (exp);
9788 gcc_assert (!exp
9789 || SCOPE_FILE_SCOPE_P (context)
9790 || context == current_function_decl
9791 || TREE_STATIC (exp)
9792 || DECL_EXTERNAL (exp)
9793 /* ??? C++ creates functions that are not TREE_STATIC. */
9794 || TREE_CODE (exp) == FUNCTION_DECL);
9796 /* This is the case of an array whose size is to be determined
9797 from its initializer, while the initializer is still being parsed.
9798 ??? We aren't parsing while expanding anymore. */
9800 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9801 temp = validize_mem (decl_rtl);
9803 /* If DECL_RTL is memory, we are in the normal case and the
9804 address is not valid, get the address into a register. */
9806 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9808 if (alt_rtl)
9809 *alt_rtl = decl_rtl;
9810 decl_rtl = use_anchored_address (decl_rtl);
9811 if (modifier != EXPAND_CONST_ADDRESS
9812 && modifier != EXPAND_SUM
9813 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9814 : GET_MODE (decl_rtl),
9815 XEXP (decl_rtl, 0),
9816 MEM_ADDR_SPACE (decl_rtl)))
9817 temp = replace_equiv_address (decl_rtl,
9818 copy_rtx (XEXP (decl_rtl, 0)));
9821 /* If we got something, return it. But first, set the alignment
9822 if the address is a register. */
9823 if (temp != 0)
9825 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9826 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9828 return temp;
9831 if (exp)
9832 dmode = DECL_MODE (exp);
9833 else
9834 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9836 /* If the mode of DECL_RTL does not match that of the decl,
9837 there are two cases: we are dealing with a BLKmode value
9838 that is returned in a register, or we are dealing with
9839 a promoted value. In the latter case, return a SUBREG
9840 of the wanted mode, but mark it so that we know that it
9841 was already extended. */
9842 if (REG_P (decl_rtl)
9843 && dmode != BLKmode
9844 && GET_MODE (decl_rtl) != dmode)
9846 machine_mode pmode;
9848 /* Get the signedness to be used for this variable. Ensure we get
9849 the same mode we got when the variable was declared. */
9850 if (code != SSA_NAME)
9851 pmode = promote_decl_mode (exp, &unsignedp);
9852 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9853 && gimple_code (g) == GIMPLE_CALL
9854 && !gimple_call_internal_p (g))
9855 pmode = promote_function_mode (type, mode, &unsignedp,
9856 gimple_call_fntype (g),
9858 else
9859 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9860 gcc_assert (GET_MODE (decl_rtl) == pmode);
9862 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9863 SUBREG_PROMOTED_VAR_P (temp) = 1;
9864 SUBREG_PROMOTED_SET (temp, unsignedp);
9865 return temp;
9868 return decl_rtl;
9870 case INTEGER_CST:
9871 /* Given that TYPE_PRECISION (type) is not always equal to
9872 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9873 the former to the latter according to the signedness of the
9874 type. */
9875 temp = immed_wide_int_const (wi::to_wide
9876 (exp,
9877 GET_MODE_PRECISION (TYPE_MODE (type))),
9878 TYPE_MODE (type));
9879 return temp;
9881 case VECTOR_CST:
9883 tree tmp = NULL_TREE;
9884 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9885 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9886 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9887 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9888 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9889 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9890 return const_vector_from_tree (exp);
9891 if (GET_MODE_CLASS (mode) == MODE_INT)
9893 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9894 return const_scalar_mask_from_tree (exp);
9895 else
9897 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9898 if (type_for_mode)
9899 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9900 type_for_mode, exp);
9903 if (!tmp)
9905 vec<constructor_elt, va_gc> *v;
9906 unsigned i;
9907 vec_alloc (v, VECTOR_CST_NELTS (exp));
9908 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9909 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9910 tmp = build_constructor (type, v);
9912 return expand_expr (tmp, ignore ? const0_rtx : target,
9913 tmode, modifier);
9916 case CONST_DECL:
9917 if (modifier == EXPAND_WRITE)
9919 /* Writing into CONST_DECL is always invalid, but handle it
9920 gracefully. */
9921 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9922 machine_mode address_mode = targetm.addr_space.address_mode (as);
9923 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9924 EXPAND_NORMAL, as);
9925 op0 = memory_address_addr_space (mode, op0, as);
9926 temp = gen_rtx_MEM (mode, op0);
9927 set_mem_addr_space (temp, as);
9928 return temp;
9930 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9932 case REAL_CST:
9933 /* If optimized, generate immediate CONST_DOUBLE
9934 which will be turned into memory by reload if necessary.
9936 We used to force a register so that loop.c could see it. But
9937 this does not allow gen_* patterns to perform optimizations with
9938 the constants. It also produces two insns in cases like "x = 1.0;".
9939 On most machines, floating-point constants are not permitted in
9940 many insns, so we'd end up copying it to a register in any case.
9942 Now, we do the copying in expand_binop, if appropriate. */
9943 return const_double_from_real_value (TREE_REAL_CST (exp),
9944 TYPE_MODE (TREE_TYPE (exp)));
9946 case FIXED_CST:
9947 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9948 TYPE_MODE (TREE_TYPE (exp)));
9950 case COMPLEX_CST:
9951 /* Handle evaluating a complex constant in a CONCAT target. */
9952 if (original_target && GET_CODE (original_target) == CONCAT)
9954 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9955 rtx rtarg, itarg;
9957 rtarg = XEXP (original_target, 0);
9958 itarg = XEXP (original_target, 1);
9960 /* Move the real and imaginary parts separately. */
9961 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9962 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9964 if (op0 != rtarg)
9965 emit_move_insn (rtarg, op0);
9966 if (op1 != itarg)
9967 emit_move_insn (itarg, op1);
9969 return original_target;
9972 /* fall through */
9974 case STRING_CST:
9975 temp = expand_expr_constant (exp, 1, modifier);
9977 /* temp contains a constant address.
9978 On RISC machines where a constant address isn't valid,
9979 make some insns to get that address into a register. */
9980 if (modifier != EXPAND_CONST_ADDRESS
9981 && modifier != EXPAND_INITIALIZER
9982 && modifier != EXPAND_SUM
9983 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9984 MEM_ADDR_SPACE (temp)))
9985 return replace_equiv_address (temp,
9986 copy_rtx (XEXP (temp, 0)));
9987 return temp;
9989 case SAVE_EXPR:
9991 tree val = treeop0;
9992 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9993 inner_reference_p);
9995 if (!SAVE_EXPR_RESOLVED_P (exp))
9997 /* We can indeed still hit this case, typically via builtin
9998 expanders calling save_expr immediately before expanding
9999 something. Assume this means that we only have to deal
10000 with non-BLKmode values. */
10001 gcc_assert (GET_MODE (ret) != BLKmode);
10003 val = build_decl (curr_insn_location (),
10004 VAR_DECL, NULL, TREE_TYPE (exp));
10005 DECL_ARTIFICIAL (val) = 1;
10006 DECL_IGNORED_P (val) = 1;
10007 treeop0 = val;
10008 TREE_OPERAND (exp, 0) = treeop0;
10009 SAVE_EXPR_RESOLVED_P (exp) = 1;
10011 if (!CONSTANT_P (ret))
10012 ret = copy_to_reg (ret);
10013 SET_DECL_RTL (val, ret);
10016 return ret;
10020 case CONSTRUCTOR:
10021 /* If we don't need the result, just ensure we evaluate any
10022 subexpressions. */
10023 if (ignore)
10025 unsigned HOST_WIDE_INT idx;
10026 tree value;
10028 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10029 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10031 return const0_rtx;
10034 return expand_constructor (exp, target, modifier, false);
10036 case TARGET_MEM_REF:
10038 addr_space_t as
10039 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10040 enum insn_code icode;
10041 unsigned int align;
10043 op0 = addr_for_mem_ref (exp, as, true);
10044 op0 = memory_address_addr_space (mode, op0, as);
10045 temp = gen_rtx_MEM (mode, op0);
10046 set_mem_attributes (temp, exp, 0);
10047 set_mem_addr_space (temp, as);
10048 align = get_object_alignment (exp);
10049 if (modifier != EXPAND_WRITE
10050 && modifier != EXPAND_MEMORY
10051 && mode != BLKmode
10052 && align < GET_MODE_ALIGNMENT (mode)
10053 /* If the target does not have special handling for unaligned
10054 loads of mode then it can use regular moves for them. */
10055 && ((icode = optab_handler (movmisalign_optab, mode))
10056 != CODE_FOR_nothing))
10058 struct expand_operand ops[2];
10060 /* We've already validated the memory, and we're creating a
10061 new pseudo destination. The predicates really can't fail,
10062 nor can the generator. */
10063 create_output_operand (&ops[0], NULL_RTX, mode);
10064 create_fixed_operand (&ops[1], temp);
10065 expand_insn (icode, 2, ops);
10066 temp = ops[0].value;
10068 return temp;
10071 case MEM_REF:
10073 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10074 addr_space_t as
10075 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10076 machine_mode address_mode;
10077 tree base = TREE_OPERAND (exp, 0);
10078 gimple *def_stmt;
10079 enum insn_code icode;
10080 unsigned align;
10081 /* Handle expansion of non-aliased memory with non-BLKmode. That
10082 might end up in a register. */
10083 if (mem_ref_refers_to_non_mem_p (exp))
10085 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10086 base = TREE_OPERAND (base, 0);
10087 if (offset == 0
10088 && !reverse
10089 && tree_fits_uhwi_p (TYPE_SIZE (type))
10090 && (GET_MODE_BITSIZE (DECL_MODE (base))
10091 == tree_to_uhwi (TYPE_SIZE (type))))
10092 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10093 target, tmode, modifier);
10094 if (TYPE_MODE (type) == BLKmode)
10096 temp = assign_stack_temp (DECL_MODE (base),
10097 GET_MODE_SIZE (DECL_MODE (base)));
10098 store_expr (base, temp, 0, false, false);
10099 temp = adjust_address (temp, BLKmode, offset);
10100 set_mem_size (temp, int_size_in_bytes (type));
10101 return temp;
10103 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10104 bitsize_int (offset * BITS_PER_UNIT));
10105 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10106 return expand_expr (exp, target, tmode, modifier);
10108 address_mode = targetm.addr_space.address_mode (as);
10109 base = TREE_OPERAND (exp, 0);
10110 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10112 tree mask = gimple_assign_rhs2 (def_stmt);
10113 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10114 gimple_assign_rhs1 (def_stmt), mask);
10115 TREE_OPERAND (exp, 0) = base;
10117 align = get_object_alignment (exp);
10118 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10119 op0 = memory_address_addr_space (mode, op0, as);
10120 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10122 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10123 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10124 op0 = memory_address_addr_space (mode, op0, as);
10126 temp = gen_rtx_MEM (mode, op0);
10127 set_mem_attributes (temp, exp, 0);
10128 set_mem_addr_space (temp, as);
10129 if (TREE_THIS_VOLATILE (exp))
10130 MEM_VOLATILE_P (temp) = 1;
10131 if (modifier != EXPAND_WRITE
10132 && modifier != EXPAND_MEMORY
10133 && !inner_reference_p
10134 && mode != BLKmode
10135 && align < GET_MODE_ALIGNMENT (mode))
10137 if ((icode = optab_handler (movmisalign_optab, mode))
10138 != CODE_FOR_nothing)
10140 struct expand_operand ops[2];
10142 /* We've already validated the memory, and we're creating a
10143 new pseudo destination. The predicates really can't fail,
10144 nor can the generator. */
10145 create_output_operand (&ops[0], NULL_RTX, mode);
10146 create_fixed_operand (&ops[1], temp);
10147 expand_insn (icode, 2, ops);
10148 temp = ops[0].value;
10150 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10151 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10152 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10153 (modifier == EXPAND_STACK_PARM
10154 ? NULL_RTX : target),
10155 mode, mode, false);
10157 if (reverse
10158 && modifier != EXPAND_MEMORY
10159 && modifier != EXPAND_WRITE)
10160 temp = flip_storage_order (mode, temp);
10161 return temp;
10164 case ARRAY_REF:
10167 tree array = treeop0;
10168 tree index = treeop1;
10169 tree init;
10171 /* Fold an expression like: "foo"[2].
10172 This is not done in fold so it won't happen inside &.
10173 Don't fold if this is for wide characters since it's too
10174 difficult to do correctly and this is a very rare case. */
10176 if (modifier != EXPAND_CONST_ADDRESS
10177 && modifier != EXPAND_INITIALIZER
10178 && modifier != EXPAND_MEMORY)
10180 tree t = fold_read_from_constant_string (exp);
10182 if (t)
10183 return expand_expr (t, target, tmode, modifier);
10186 /* If this is a constant index into a constant array,
10187 just get the value from the array. Handle both the cases when
10188 we have an explicit constructor and when our operand is a variable
10189 that was declared const. */
10191 if (modifier != EXPAND_CONST_ADDRESS
10192 && modifier != EXPAND_INITIALIZER
10193 && modifier != EXPAND_MEMORY
10194 && TREE_CODE (array) == CONSTRUCTOR
10195 && ! TREE_SIDE_EFFECTS (array)
10196 && TREE_CODE (index) == INTEGER_CST)
10198 unsigned HOST_WIDE_INT ix;
10199 tree field, value;
10201 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10202 field, value)
10203 if (tree_int_cst_equal (field, index))
10205 if (!TREE_SIDE_EFFECTS (value))
10206 return expand_expr (fold (value), target, tmode, modifier);
10207 break;
10211 else if (optimize >= 1
10212 && modifier != EXPAND_CONST_ADDRESS
10213 && modifier != EXPAND_INITIALIZER
10214 && modifier != EXPAND_MEMORY
10215 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10216 && TREE_CODE (index) == INTEGER_CST
10217 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10218 && (init = ctor_for_folding (array)) != error_mark_node)
10220 if (init == NULL_TREE)
10222 tree value = build_zero_cst (type);
10223 if (TREE_CODE (value) == CONSTRUCTOR)
10225 /* If VALUE is a CONSTRUCTOR, this optimization is only
10226 useful if this doesn't store the CONSTRUCTOR into
10227 memory. If it does, it is more efficient to just
10228 load the data from the array directly. */
10229 rtx ret = expand_constructor (value, target,
10230 modifier, true);
10231 if (ret == NULL_RTX)
10232 value = NULL_TREE;
10235 if (value)
10236 return expand_expr (value, target, tmode, modifier);
10238 else if (TREE_CODE (init) == CONSTRUCTOR)
10240 unsigned HOST_WIDE_INT ix;
10241 tree field, value;
10243 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10244 field, value)
10245 if (tree_int_cst_equal (field, index))
10247 if (TREE_SIDE_EFFECTS (value))
10248 break;
10250 if (TREE_CODE (value) == CONSTRUCTOR)
10252 /* If VALUE is a CONSTRUCTOR, this
10253 optimization is only useful if
10254 this doesn't store the CONSTRUCTOR
10255 into memory. If it does, it is more
10256 efficient to just load the data from
10257 the array directly. */
10258 rtx ret = expand_constructor (value, target,
10259 modifier, true);
10260 if (ret == NULL_RTX)
10261 break;
10264 return
10265 expand_expr (fold (value), target, tmode, modifier);
10268 else if (TREE_CODE (init) == STRING_CST)
10270 tree low_bound = array_ref_low_bound (exp);
10271 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10273 /* Optimize the special case of a zero lower bound.
10275 We convert the lower bound to sizetype to avoid problems
10276 with constant folding. E.g. suppose the lower bound is
10277 1 and its mode is QI. Without the conversion
10278 (ARRAY + (INDEX - (unsigned char)1))
10279 becomes
10280 (ARRAY + (-(unsigned char)1) + INDEX)
10281 which becomes
10282 (ARRAY + 255 + INDEX). Oops! */
10283 if (!integer_zerop (low_bound))
10284 index1 = size_diffop_loc (loc, index1,
10285 fold_convert_loc (loc, sizetype,
10286 low_bound));
10288 if (tree_fits_uhwi_p (index1)
10289 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10291 tree type = TREE_TYPE (TREE_TYPE (init));
10292 machine_mode mode = TYPE_MODE (type);
10294 if (GET_MODE_CLASS (mode) == MODE_INT
10295 && GET_MODE_SIZE (mode) == 1)
10296 return gen_int_mode (TREE_STRING_POINTER (init)
10297 [TREE_INT_CST_LOW (index1)],
10298 mode);
10303 goto normal_inner_ref;
10305 case COMPONENT_REF:
10306 /* If the operand is a CONSTRUCTOR, we can just extract the
10307 appropriate field if it is present. */
10308 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10310 unsigned HOST_WIDE_INT idx;
10311 tree field, value;
10313 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10314 idx, field, value)
10315 if (field == treeop1
10316 /* We can normally use the value of the field in the
10317 CONSTRUCTOR. However, if this is a bitfield in
10318 an integral mode that we can fit in a HOST_WIDE_INT,
10319 we must mask only the number of bits in the bitfield,
10320 since this is done implicitly by the constructor. If
10321 the bitfield does not meet either of those conditions,
10322 we can't do this optimization. */
10323 && (! DECL_BIT_FIELD (field)
10324 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10325 && (GET_MODE_PRECISION (DECL_MODE (field))
10326 <= HOST_BITS_PER_WIDE_INT))))
10328 if (DECL_BIT_FIELD (field)
10329 && modifier == EXPAND_STACK_PARM)
10330 target = 0;
10331 op0 = expand_expr (value, target, tmode, modifier);
10332 if (DECL_BIT_FIELD (field))
10334 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10335 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10337 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10339 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10340 imode);
10341 op0 = expand_and (imode, op0, op1, target);
10343 else
10345 int count = GET_MODE_PRECISION (imode) - bitsize;
10347 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10348 target, 0);
10349 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10350 target, 0);
10354 return op0;
10357 goto normal_inner_ref;
10359 case BIT_FIELD_REF:
10360 case ARRAY_RANGE_REF:
10361 normal_inner_ref:
10363 machine_mode mode1, mode2;
10364 HOST_WIDE_INT bitsize, bitpos;
10365 tree offset;
10366 int reversep, volatilep = 0, must_force_mem;
10367 tree tem
10368 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10369 &unsignedp, &reversep, &volatilep);
10370 rtx orig_op0, memloc;
10371 bool clear_mem_expr = false;
10373 /* If we got back the original object, something is wrong. Perhaps
10374 we are evaluating an expression too early. In any event, don't
10375 infinitely recurse. */
10376 gcc_assert (tem != exp);
10378 /* If TEM's type is a union of variable size, pass TARGET to the inner
10379 computation, since it will need a temporary and TARGET is known
10380 to have to do. This occurs in unchecked conversion in Ada. */
10381 orig_op0 = op0
10382 = expand_expr_real (tem,
10383 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10384 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10385 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10386 != INTEGER_CST)
10387 && modifier != EXPAND_STACK_PARM
10388 ? target : NULL_RTX),
10389 VOIDmode,
10390 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10391 NULL, true);
10393 /* If the field has a mode, we want to access it in the
10394 field's mode, not the computed mode.
10395 If a MEM has VOIDmode (external with incomplete type),
10396 use BLKmode for it instead. */
10397 if (MEM_P (op0))
10399 if (mode1 != VOIDmode)
10400 op0 = adjust_address (op0, mode1, 0);
10401 else if (GET_MODE (op0) == VOIDmode)
10402 op0 = adjust_address (op0, BLKmode, 0);
10405 mode2
10406 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10408 /* If we have either an offset, a BLKmode result, or a reference
10409 outside the underlying object, we must force it to memory.
10410 Such a case can occur in Ada if we have unchecked conversion
10411 of an expression from a scalar type to an aggregate type or
10412 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10413 passed a partially uninitialized object or a view-conversion
10414 to a larger size. */
10415 must_force_mem = (offset
10416 || mode1 == BLKmode
10417 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10419 /* Handle CONCAT first. */
10420 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10422 if (bitpos == 0
10423 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10424 && COMPLEX_MODE_P (mode1)
10425 && COMPLEX_MODE_P (GET_MODE (op0))
10426 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10427 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10429 if (reversep)
10430 op0 = flip_storage_order (GET_MODE (op0), op0);
10431 if (mode1 != GET_MODE (op0))
10433 rtx parts[2];
10434 for (int i = 0; i < 2; i++)
10436 rtx op = read_complex_part (op0, i != 0);
10437 if (GET_CODE (op) == SUBREG)
10438 op = force_reg (GET_MODE (op), op);
10439 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10440 op);
10441 if (temp)
10442 op = temp;
10443 else
10445 if (!REG_P (op) && !MEM_P (op))
10446 op = force_reg (GET_MODE (op), op);
10447 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10449 parts[i] = op;
10451 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10453 return op0;
10455 if (bitpos == 0
10456 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10457 && bitsize)
10459 op0 = XEXP (op0, 0);
10460 mode2 = GET_MODE (op0);
10462 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10463 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10464 && bitpos
10465 && bitsize)
10467 op0 = XEXP (op0, 1);
10468 bitpos = 0;
10469 mode2 = GET_MODE (op0);
10471 else
10472 /* Otherwise force into memory. */
10473 must_force_mem = 1;
10476 /* If this is a constant, put it in a register if it is a legitimate
10477 constant and we don't need a memory reference. */
10478 if (CONSTANT_P (op0)
10479 && mode2 != BLKmode
10480 && targetm.legitimate_constant_p (mode2, op0)
10481 && !must_force_mem)
10482 op0 = force_reg (mode2, op0);
10484 /* Otherwise, if this is a constant, try to force it to the constant
10485 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10486 is a legitimate constant. */
10487 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10488 op0 = validize_mem (memloc);
10490 /* Otherwise, if this is a constant or the object is not in memory
10491 and need be, put it there. */
10492 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10494 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10495 emit_move_insn (memloc, op0);
10496 op0 = memloc;
10497 clear_mem_expr = true;
10500 if (offset)
10502 machine_mode address_mode;
10503 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10504 EXPAND_SUM);
10506 gcc_assert (MEM_P (op0));
10508 address_mode = get_address_mode (op0);
10509 if (GET_MODE (offset_rtx) != address_mode)
10511 /* We cannot be sure that the RTL in offset_rtx is valid outside
10512 of a memory address context, so force it into a register
10513 before attempting to convert it to the desired mode. */
10514 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10515 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10518 /* See the comment in expand_assignment for the rationale. */
10519 if (mode1 != VOIDmode
10520 && bitpos != 0
10521 && bitsize > 0
10522 && (bitpos % bitsize) == 0
10523 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10524 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10526 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10527 bitpos = 0;
10530 op0 = offset_address (op0, offset_rtx,
10531 highest_pow2_factor (offset));
10534 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10535 record its alignment as BIGGEST_ALIGNMENT. */
10536 if (MEM_P (op0) && bitpos == 0 && offset != 0
10537 && is_aligning_offset (offset, tem))
10538 set_mem_align (op0, BIGGEST_ALIGNMENT);
10540 /* Don't forget about volatility even if this is a bitfield. */
10541 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10543 if (op0 == orig_op0)
10544 op0 = copy_rtx (op0);
10546 MEM_VOLATILE_P (op0) = 1;
10549 /* In cases where an aligned union has an unaligned object
10550 as a field, we might be extracting a BLKmode value from
10551 an integer-mode (e.g., SImode) object. Handle this case
10552 by doing the extract into an object as wide as the field
10553 (which we know to be the width of a basic mode), then
10554 storing into memory, and changing the mode to BLKmode. */
10555 if (mode1 == VOIDmode
10556 || REG_P (op0) || GET_CODE (op0) == SUBREG
10557 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10558 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10559 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10560 && modifier != EXPAND_CONST_ADDRESS
10561 && modifier != EXPAND_INITIALIZER
10562 && modifier != EXPAND_MEMORY)
10563 /* If the bitfield is volatile and the bitsize
10564 is narrower than the access size of the bitfield,
10565 we need to extract bitfields from the access. */
10566 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10567 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10568 && mode1 != BLKmode
10569 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10570 /* If the field isn't aligned enough to fetch as a memref,
10571 fetch it as a bit field. */
10572 || (mode1 != BLKmode
10573 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10574 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10575 || (MEM_P (op0)
10576 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10577 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10578 && modifier != EXPAND_MEMORY
10579 && ((modifier == EXPAND_CONST_ADDRESS
10580 || modifier == EXPAND_INITIALIZER)
10581 ? STRICT_ALIGNMENT
10582 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10583 || (bitpos % BITS_PER_UNIT != 0)))
10584 /* If the type and the field are a constant size and the
10585 size of the type isn't the same size as the bitfield,
10586 we must use bitfield operations. */
10587 || (bitsize >= 0
10588 && TYPE_SIZE (TREE_TYPE (exp))
10589 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10590 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10591 bitsize)))
10593 machine_mode ext_mode = mode;
10595 if (ext_mode == BLKmode
10596 && ! (target != 0 && MEM_P (op0)
10597 && MEM_P (target)
10598 && bitpos % BITS_PER_UNIT == 0))
10599 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10601 if (ext_mode == BLKmode)
10603 if (target == 0)
10604 target = assign_temp (type, 1, 1);
10606 /* ??? Unlike the similar test a few lines below, this one is
10607 very likely obsolete. */
10608 if (bitsize == 0)
10609 return target;
10611 /* In this case, BITPOS must start at a byte boundary and
10612 TARGET, if specified, must be a MEM. */
10613 gcc_assert (MEM_P (op0)
10614 && (!target || MEM_P (target))
10615 && !(bitpos % BITS_PER_UNIT));
10617 emit_block_move (target,
10618 adjust_address (op0, VOIDmode,
10619 bitpos / BITS_PER_UNIT),
10620 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10621 / BITS_PER_UNIT),
10622 (modifier == EXPAND_STACK_PARM
10623 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10625 return target;
10628 /* If we have nothing to extract, the result will be 0 for targets
10629 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10630 return 0 for the sake of consistency, as reading a zero-sized
10631 bitfield is valid in Ada and the value is fully specified. */
10632 if (bitsize == 0)
10633 return const0_rtx;
10635 op0 = validize_mem (op0);
10637 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10638 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10640 /* If the result has a record type and the extraction is done in
10641 an integral mode, then the field may be not aligned on a byte
10642 boundary; in this case, if it has reverse storage order, it
10643 needs to be extracted as a scalar field with reverse storage
10644 order and put back into memory order afterwards. */
10645 if (TREE_CODE (type) == RECORD_TYPE
10646 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10647 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10649 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10650 (modifier == EXPAND_STACK_PARM
10651 ? NULL_RTX : target),
10652 ext_mode, ext_mode, reversep);
10654 /* If the result has a record type and the mode of OP0 is an
10655 integral mode then, if BITSIZE is narrower than this mode
10656 and this is for big-endian data, we must put the field
10657 into the high-order bits. And we must also put it back
10658 into memory order if it has been previously reversed. */
10659 if (TREE_CODE (type) == RECORD_TYPE
10660 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10662 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10664 if (bitsize < size
10665 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10666 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10667 size - bitsize, op0, 1);
10669 if (reversep)
10670 op0 = flip_storage_order (GET_MODE (op0), op0);
10673 /* If the result type is BLKmode, store the data into a temporary
10674 of the appropriate type, but with the mode corresponding to the
10675 mode for the data we have (op0's mode). */
10676 if (mode == BLKmode)
10678 rtx new_rtx
10679 = assign_stack_temp_for_type (ext_mode,
10680 GET_MODE_BITSIZE (ext_mode),
10681 type);
10682 emit_move_insn (new_rtx, op0);
10683 op0 = copy_rtx (new_rtx);
10684 PUT_MODE (op0, BLKmode);
10687 return op0;
10690 /* If the result is BLKmode, use that to access the object
10691 now as well. */
10692 if (mode == BLKmode)
10693 mode1 = BLKmode;
10695 /* Get a reference to just this component. */
10696 if (modifier == EXPAND_CONST_ADDRESS
10697 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10698 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10699 else
10700 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10702 if (op0 == orig_op0)
10703 op0 = copy_rtx (op0);
10705 /* Don't set memory attributes if the base expression is
10706 SSA_NAME that got expanded as a MEM. In that case, we should
10707 just honor its original memory attributes. */
10708 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10709 set_mem_attributes (op0, exp, 0);
10711 if (REG_P (XEXP (op0, 0)))
10712 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10714 /* If op0 is a temporary because the original expressions was forced
10715 to memory, clear MEM_EXPR so that the original expression cannot
10716 be marked as addressable through MEM_EXPR of the temporary. */
10717 if (clear_mem_expr)
10718 set_mem_expr (op0, NULL_TREE);
10720 MEM_VOLATILE_P (op0) |= volatilep;
10722 if (reversep
10723 && modifier != EXPAND_MEMORY
10724 && modifier != EXPAND_WRITE)
10725 op0 = flip_storage_order (mode1, op0);
10727 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10728 || modifier == EXPAND_CONST_ADDRESS
10729 || modifier == EXPAND_INITIALIZER)
10730 return op0;
10732 if (target == 0)
10733 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10735 convert_move (target, op0, unsignedp);
10736 return target;
10739 case OBJ_TYPE_REF:
10740 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10742 case CALL_EXPR:
10743 /* All valid uses of __builtin_va_arg_pack () are removed during
10744 inlining. */
10745 if (CALL_EXPR_VA_ARG_PACK (exp))
10746 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10748 tree fndecl = get_callee_fndecl (exp), attr;
10750 if (fndecl
10751 && (attr = lookup_attribute ("error",
10752 DECL_ATTRIBUTES (fndecl))) != NULL)
10753 error ("%Kcall to %qs declared with attribute error: %s",
10754 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10755 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10756 if (fndecl
10757 && (attr = lookup_attribute ("warning",
10758 DECL_ATTRIBUTES (fndecl))) != NULL)
10759 warning_at (tree_nonartificial_location (exp),
10760 0, "%Kcall to %qs declared with attribute warning: %s",
10761 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10762 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10764 /* Check for a built-in function. */
10765 if (fndecl && DECL_BUILT_IN (fndecl))
10767 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10768 if (CALL_WITH_BOUNDS_P (exp))
10769 return expand_builtin_with_bounds (exp, target, subtarget,
10770 tmode, ignore);
10771 else
10772 return expand_builtin (exp, target, subtarget, tmode, ignore);
10775 return expand_call (exp, target, ignore);
10777 case VIEW_CONVERT_EXPR:
10778 op0 = NULL_RTX;
10780 /* If we are converting to BLKmode, try to avoid an intermediate
10781 temporary by fetching an inner memory reference. */
10782 if (mode == BLKmode
10783 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10784 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10785 && handled_component_p (treeop0))
10787 machine_mode mode1;
10788 HOST_WIDE_INT bitsize, bitpos;
10789 tree offset;
10790 int unsignedp, reversep, volatilep = 0;
10791 tree tem
10792 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10793 &unsignedp, &reversep, &volatilep);
10794 rtx orig_op0;
10796 /* ??? We should work harder and deal with non-zero offsets. */
10797 if (!offset
10798 && (bitpos % BITS_PER_UNIT) == 0
10799 && !reversep
10800 && bitsize >= 0
10801 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10803 /* See the normal_inner_ref case for the rationale. */
10804 orig_op0
10805 = expand_expr_real (tem,
10806 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10807 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10808 != INTEGER_CST)
10809 && modifier != EXPAND_STACK_PARM
10810 ? target : NULL_RTX),
10811 VOIDmode,
10812 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10813 NULL, true);
10815 if (MEM_P (orig_op0))
10817 op0 = orig_op0;
10819 /* Get a reference to just this component. */
10820 if (modifier == EXPAND_CONST_ADDRESS
10821 || modifier == EXPAND_SUM
10822 || modifier == EXPAND_INITIALIZER)
10823 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10824 else
10825 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10827 if (op0 == orig_op0)
10828 op0 = copy_rtx (op0);
10830 set_mem_attributes (op0, treeop0, 0);
10831 if (REG_P (XEXP (op0, 0)))
10832 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10834 MEM_VOLATILE_P (op0) |= volatilep;
10839 if (!op0)
10840 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10841 NULL, inner_reference_p);
10843 /* If the input and output modes are both the same, we are done. */
10844 if (mode == GET_MODE (op0))
10846 /* If neither mode is BLKmode, and both modes are the same size
10847 then we can use gen_lowpart. */
10848 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10849 && (GET_MODE_PRECISION (mode)
10850 == GET_MODE_PRECISION (GET_MODE (op0)))
10851 && !COMPLEX_MODE_P (GET_MODE (op0)))
10853 if (GET_CODE (op0) == SUBREG)
10854 op0 = force_reg (GET_MODE (op0), op0);
10855 temp = gen_lowpart_common (mode, op0);
10856 if (temp)
10857 op0 = temp;
10858 else
10860 if (!REG_P (op0) && !MEM_P (op0))
10861 op0 = force_reg (GET_MODE (op0), op0);
10862 op0 = gen_lowpart (mode, op0);
10865 /* If both types are integral, convert from one mode to the other. */
10866 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10867 op0 = convert_modes (mode, GET_MODE (op0), op0,
10868 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10869 /* If the output type is a bit-field type, do an extraction. */
10870 else if (reduce_bit_field)
10871 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10872 TYPE_UNSIGNED (type), NULL_RTX,
10873 mode, mode, false);
10874 /* As a last resort, spill op0 to memory, and reload it in a
10875 different mode. */
10876 else if (!MEM_P (op0))
10878 /* If the operand is not a MEM, force it into memory. Since we
10879 are going to be changing the mode of the MEM, don't call
10880 force_const_mem for constants because we don't allow pool
10881 constants to change mode. */
10882 tree inner_type = TREE_TYPE (treeop0);
10884 gcc_assert (!TREE_ADDRESSABLE (exp));
10886 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10887 target
10888 = assign_stack_temp_for_type
10889 (TYPE_MODE (inner_type),
10890 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10892 emit_move_insn (target, op0);
10893 op0 = target;
10896 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10897 output type is such that the operand is known to be aligned, indicate
10898 that it is. Otherwise, we need only be concerned about alignment for
10899 non-BLKmode results. */
10900 if (MEM_P (op0))
10902 enum insn_code icode;
10904 if (modifier != EXPAND_WRITE
10905 && modifier != EXPAND_MEMORY
10906 && !inner_reference_p
10907 && mode != BLKmode
10908 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10910 /* If the target does have special handling for unaligned
10911 loads of mode then use them. */
10912 if ((icode = optab_handler (movmisalign_optab, mode))
10913 != CODE_FOR_nothing)
10915 rtx reg;
10917 op0 = adjust_address (op0, mode, 0);
10918 /* We've already validated the memory, and we're creating a
10919 new pseudo destination. The predicates really can't
10920 fail. */
10921 reg = gen_reg_rtx (mode);
10923 /* Nor can the insn generator. */
10924 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10925 emit_insn (insn);
10926 return reg;
10928 else if (STRICT_ALIGNMENT)
10930 tree inner_type = TREE_TYPE (treeop0);
10931 HOST_WIDE_INT temp_size
10932 = MAX (int_size_in_bytes (inner_type),
10933 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10934 rtx new_rtx
10935 = assign_stack_temp_for_type (mode, temp_size, type);
10936 rtx new_with_op0_mode
10937 = adjust_address (new_rtx, GET_MODE (op0), 0);
10939 gcc_assert (!TREE_ADDRESSABLE (exp));
10941 if (GET_MODE (op0) == BLKmode)
10942 emit_block_move (new_with_op0_mode, op0,
10943 GEN_INT (GET_MODE_SIZE (mode)),
10944 (modifier == EXPAND_STACK_PARM
10945 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10946 else
10947 emit_move_insn (new_with_op0_mode, op0);
10949 op0 = new_rtx;
10953 op0 = adjust_address (op0, mode, 0);
10956 return op0;
10958 case MODIFY_EXPR:
10960 tree lhs = treeop0;
10961 tree rhs = treeop1;
10962 gcc_assert (ignore);
10964 /* Check for |= or &= of a bitfield of size one into another bitfield
10965 of size 1. In this case, (unless we need the result of the
10966 assignment) we can do this more efficiently with a
10967 test followed by an assignment, if necessary.
10969 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10970 things change so we do, this code should be enhanced to
10971 support it. */
10972 if (TREE_CODE (lhs) == COMPONENT_REF
10973 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10974 || TREE_CODE (rhs) == BIT_AND_EXPR)
10975 && TREE_OPERAND (rhs, 0) == lhs
10976 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10977 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10978 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10980 rtx_code_label *label = gen_label_rtx ();
10981 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10982 do_jump (TREE_OPERAND (rhs, 1),
10983 value ? label : 0,
10984 value ? 0 : label, -1);
10985 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10986 false);
10987 do_pending_stack_adjust ();
10988 emit_label (label);
10989 return const0_rtx;
10992 expand_assignment (lhs, rhs, false);
10993 return const0_rtx;
10996 case ADDR_EXPR:
10997 return expand_expr_addr_expr (exp, target, tmode, modifier);
10999 case REALPART_EXPR:
11000 op0 = expand_normal (treeop0);
11001 return read_complex_part (op0, false);
11003 case IMAGPART_EXPR:
11004 op0 = expand_normal (treeop0);
11005 return read_complex_part (op0, true);
11007 case RETURN_EXPR:
11008 case LABEL_EXPR:
11009 case GOTO_EXPR:
11010 case SWITCH_EXPR:
11011 case ASM_EXPR:
11012 /* Expanded in cfgexpand.c. */
11013 gcc_unreachable ();
11015 case TRY_CATCH_EXPR:
11016 case CATCH_EXPR:
11017 case EH_FILTER_EXPR:
11018 case TRY_FINALLY_EXPR:
11019 /* Lowered by tree-eh.c. */
11020 gcc_unreachable ();
11022 case WITH_CLEANUP_EXPR:
11023 case CLEANUP_POINT_EXPR:
11024 case TARGET_EXPR:
11025 case CASE_LABEL_EXPR:
11026 case VA_ARG_EXPR:
11027 case BIND_EXPR:
11028 case INIT_EXPR:
11029 case CONJ_EXPR:
11030 case COMPOUND_EXPR:
11031 case PREINCREMENT_EXPR:
11032 case PREDECREMENT_EXPR:
11033 case POSTINCREMENT_EXPR:
11034 case POSTDECREMENT_EXPR:
11035 case LOOP_EXPR:
11036 case EXIT_EXPR:
11037 case COMPOUND_LITERAL_EXPR:
11038 /* Lowered by gimplify.c. */
11039 gcc_unreachable ();
11041 case FDESC_EXPR:
11042 /* Function descriptors are not valid except for as
11043 initialization constants, and should not be expanded. */
11044 gcc_unreachable ();
11046 case WITH_SIZE_EXPR:
11047 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11048 have pulled out the size to use in whatever context it needed. */
11049 return expand_expr_real (treeop0, original_target, tmode,
11050 modifier, alt_rtl, inner_reference_p);
11052 default:
11053 return expand_expr_real_2 (&ops, target, tmode, modifier);
11057 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11058 signedness of TYPE), possibly returning the result in TARGET. */
11059 static rtx
11060 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11062 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11063 if (target && GET_MODE (target) != GET_MODE (exp))
11064 target = 0;
11065 /* For constant values, reduce using build_int_cst_type. */
11066 if (CONST_INT_P (exp))
11068 HOST_WIDE_INT value = INTVAL (exp);
11069 tree t = build_int_cst_type (type, value);
11070 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11072 else if (TYPE_UNSIGNED (type))
11074 machine_mode mode = GET_MODE (exp);
11075 rtx mask = immed_wide_int_const
11076 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11077 return expand_and (mode, exp, mask, target);
11079 else
11081 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11082 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11083 exp, count, target, 0);
11084 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11085 exp, count, target, 0);
11089 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11090 when applied to the address of EXP produces an address known to be
11091 aligned more than BIGGEST_ALIGNMENT. */
11093 static int
11094 is_aligning_offset (const_tree offset, const_tree exp)
11096 /* Strip off any conversions. */
11097 while (CONVERT_EXPR_P (offset))
11098 offset = TREE_OPERAND (offset, 0);
11100 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11101 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11102 if (TREE_CODE (offset) != BIT_AND_EXPR
11103 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11104 || compare_tree_int (TREE_OPERAND (offset, 1),
11105 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11106 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11107 return 0;
11109 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11110 It must be NEGATE_EXPR. Then strip any more conversions. */
11111 offset = TREE_OPERAND (offset, 0);
11112 while (CONVERT_EXPR_P (offset))
11113 offset = TREE_OPERAND (offset, 0);
11115 if (TREE_CODE (offset) != NEGATE_EXPR)
11116 return 0;
11118 offset = TREE_OPERAND (offset, 0);
11119 while (CONVERT_EXPR_P (offset))
11120 offset = TREE_OPERAND (offset, 0);
11122 /* This must now be the address of EXP. */
11123 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11126 /* Return the tree node if an ARG corresponds to a string constant or zero
11127 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11128 in bytes within the string that ARG is accessing. The type of the
11129 offset will be `sizetype'. */
11131 tree
11132 string_constant (tree arg, tree *ptr_offset)
11134 tree array, offset, lower_bound;
11135 STRIP_NOPS (arg);
11137 if (TREE_CODE (arg) == ADDR_EXPR)
11139 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11141 *ptr_offset = size_zero_node;
11142 return TREE_OPERAND (arg, 0);
11144 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11146 array = TREE_OPERAND (arg, 0);
11147 offset = size_zero_node;
11149 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11151 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11152 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11153 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11154 return 0;
11156 /* Check if the array has a nonzero lower bound. */
11157 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11158 if (!integer_zerop (lower_bound))
11160 /* If the offset and base aren't both constants, return 0. */
11161 if (TREE_CODE (lower_bound) != INTEGER_CST)
11162 return 0;
11163 if (TREE_CODE (offset) != INTEGER_CST)
11164 return 0;
11165 /* Adjust offset by the lower bound. */
11166 offset = size_diffop (fold_convert (sizetype, offset),
11167 fold_convert (sizetype, lower_bound));
11170 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11172 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11173 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11174 if (TREE_CODE (array) != ADDR_EXPR)
11175 return 0;
11176 array = TREE_OPERAND (array, 0);
11177 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11178 return 0;
11180 else
11181 return 0;
11183 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11185 tree arg0 = TREE_OPERAND (arg, 0);
11186 tree arg1 = TREE_OPERAND (arg, 1);
11188 STRIP_NOPS (arg0);
11189 STRIP_NOPS (arg1);
11191 if (TREE_CODE (arg0) == ADDR_EXPR
11192 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11193 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11195 array = TREE_OPERAND (arg0, 0);
11196 offset = arg1;
11198 else if (TREE_CODE (arg1) == ADDR_EXPR
11199 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11200 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11202 array = TREE_OPERAND (arg1, 0);
11203 offset = arg0;
11205 else
11206 return 0;
11208 else
11209 return 0;
11211 if (TREE_CODE (array) == STRING_CST)
11213 *ptr_offset = fold_convert (sizetype, offset);
11214 return array;
11216 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11218 int length;
11219 tree init = ctor_for_folding (array);
11221 /* Variables initialized to string literals can be handled too. */
11222 if (init == error_mark_node
11223 || !init
11224 || TREE_CODE (init) != STRING_CST)
11225 return 0;
11227 /* Avoid const char foo[4] = "abcde"; */
11228 if (DECL_SIZE_UNIT (array) == NULL_TREE
11229 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11230 || (length = TREE_STRING_LENGTH (init)) <= 0
11231 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11232 return 0;
11234 /* If variable is bigger than the string literal, OFFSET must be constant
11235 and inside of the bounds of the string literal. */
11236 offset = fold_convert (sizetype, offset);
11237 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11238 && (! tree_fits_uhwi_p (offset)
11239 || compare_tree_int (offset, length) >= 0))
11240 return 0;
11242 *ptr_offset = offset;
11243 return init;
11246 return 0;
11249 /* Generate code to calculate OPS, and exploded expression
11250 using a store-flag instruction and return an rtx for the result.
11251 OPS reflects a comparison.
11253 If TARGET is nonzero, store the result there if convenient.
11255 Return zero if there is no suitable set-flag instruction
11256 available on this machine.
11258 Once expand_expr has been called on the arguments of the comparison,
11259 we are committed to doing the store flag, since it is not safe to
11260 re-evaluate the expression. We emit the store-flag insn by calling
11261 emit_store_flag, but only expand the arguments if we have a reason
11262 to believe that emit_store_flag will be successful. If we think that
11263 it will, but it isn't, we have to simulate the store-flag with a
11264 set/jump/set sequence. */
11266 static rtx
11267 do_store_flag (sepops ops, rtx target, machine_mode mode)
11269 enum rtx_code code;
11270 tree arg0, arg1, type;
11271 machine_mode operand_mode;
11272 int unsignedp;
11273 rtx op0, op1;
11274 rtx subtarget = target;
11275 location_t loc = ops->location;
11277 arg0 = ops->op0;
11278 arg1 = ops->op1;
11280 /* Don't crash if the comparison was erroneous. */
11281 if (arg0 == error_mark_node || arg1 == error_mark_node)
11282 return const0_rtx;
11284 type = TREE_TYPE (arg0);
11285 operand_mode = TYPE_MODE (type);
11286 unsignedp = TYPE_UNSIGNED (type);
11288 /* We won't bother with BLKmode store-flag operations because it would mean
11289 passing a lot of information to emit_store_flag. */
11290 if (operand_mode == BLKmode)
11291 return 0;
11293 /* We won't bother with store-flag operations involving function pointers
11294 when function pointers must be canonicalized before comparisons. */
11295 if (targetm.have_canonicalize_funcptr_for_compare ()
11296 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11297 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11298 == FUNCTION_TYPE))
11299 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11300 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11301 == FUNCTION_TYPE))))
11302 return 0;
11304 STRIP_NOPS (arg0);
11305 STRIP_NOPS (arg1);
11307 /* For vector typed comparisons emit code to generate the desired
11308 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11309 expander for this. */
11310 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11312 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11313 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11314 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11315 return expand_vec_cmp_expr (ops->type, ifexp, target);
11316 else
11318 tree if_true = constant_boolean_node (true, ops->type);
11319 tree if_false = constant_boolean_node (false, ops->type);
11320 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11321 if_false, target);
11325 /* Get the rtx comparison code to use. We know that EXP is a comparison
11326 operation of some type. Some comparisons against 1 and -1 can be
11327 converted to comparisons with zero. Do so here so that the tests
11328 below will be aware that we have a comparison with zero. These
11329 tests will not catch constants in the first operand, but constants
11330 are rarely passed as the first operand. */
11332 switch (ops->code)
11334 case EQ_EXPR:
11335 code = EQ;
11336 break;
11337 case NE_EXPR:
11338 code = NE;
11339 break;
11340 case LT_EXPR:
11341 if (integer_onep (arg1))
11342 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11343 else
11344 code = unsignedp ? LTU : LT;
11345 break;
11346 case LE_EXPR:
11347 if (! unsignedp && integer_all_onesp (arg1))
11348 arg1 = integer_zero_node, code = LT;
11349 else
11350 code = unsignedp ? LEU : LE;
11351 break;
11352 case GT_EXPR:
11353 if (! unsignedp && integer_all_onesp (arg1))
11354 arg1 = integer_zero_node, code = GE;
11355 else
11356 code = unsignedp ? GTU : GT;
11357 break;
11358 case GE_EXPR:
11359 if (integer_onep (arg1))
11360 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11361 else
11362 code = unsignedp ? GEU : GE;
11363 break;
11365 case UNORDERED_EXPR:
11366 code = UNORDERED;
11367 break;
11368 case ORDERED_EXPR:
11369 code = ORDERED;
11370 break;
11371 case UNLT_EXPR:
11372 code = UNLT;
11373 break;
11374 case UNLE_EXPR:
11375 code = UNLE;
11376 break;
11377 case UNGT_EXPR:
11378 code = UNGT;
11379 break;
11380 case UNGE_EXPR:
11381 code = UNGE;
11382 break;
11383 case UNEQ_EXPR:
11384 code = UNEQ;
11385 break;
11386 case LTGT_EXPR:
11387 code = LTGT;
11388 break;
11390 default:
11391 gcc_unreachable ();
11394 /* Put a constant second. */
11395 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11396 || TREE_CODE (arg0) == FIXED_CST)
11398 std::swap (arg0, arg1);
11399 code = swap_condition (code);
11402 /* If this is an equality or inequality test of a single bit, we can
11403 do this by shifting the bit being tested to the low-order bit and
11404 masking the result with the constant 1. If the condition was EQ,
11405 we xor it with 1. This does not require an scc insn and is faster
11406 than an scc insn even if we have it.
11408 The code to make this transformation was moved into fold_single_bit_test,
11409 so we just call into the folder and expand its result. */
11411 if ((code == NE || code == EQ)
11412 && integer_zerop (arg1)
11413 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11415 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11416 if (srcstmt
11417 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11419 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11420 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11421 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11422 gimple_assign_rhs1 (srcstmt),
11423 gimple_assign_rhs2 (srcstmt));
11424 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11425 if (temp)
11426 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11430 if (! get_subtarget (target)
11431 || GET_MODE (subtarget) != operand_mode)
11432 subtarget = 0;
11434 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11436 if (target == 0)
11437 target = gen_reg_rtx (mode);
11439 /* Try a cstore if possible. */
11440 return emit_store_flag_force (target, code, op0, op1,
11441 operand_mode, unsignedp,
11442 (TYPE_PRECISION (ops->type) == 1
11443 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11446 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11447 0 otherwise (i.e. if there is no casesi instruction).
11449 DEFAULT_PROBABILITY is the probability of jumping to the default
11450 label. */
11452 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11453 rtx table_label, rtx default_label, rtx fallback_label,
11454 int default_probability)
11456 struct expand_operand ops[5];
11457 machine_mode index_mode = SImode;
11458 rtx op1, op2, index;
11460 if (! targetm.have_casesi ())
11461 return 0;
11463 /* Convert the index to SImode. */
11464 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11466 machine_mode omode = TYPE_MODE (index_type);
11467 rtx rangertx = expand_normal (range);
11469 /* We must handle the endpoints in the original mode. */
11470 index_expr = build2 (MINUS_EXPR, index_type,
11471 index_expr, minval);
11472 minval = integer_zero_node;
11473 index = expand_normal (index_expr);
11474 if (default_label)
11475 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11476 omode, 1, default_label,
11477 default_probability);
11478 /* Now we can safely truncate. */
11479 index = convert_to_mode (index_mode, index, 0);
11481 else
11483 if (TYPE_MODE (index_type) != index_mode)
11485 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11486 index_expr = fold_convert (index_type, index_expr);
11489 index = expand_normal (index_expr);
11492 do_pending_stack_adjust ();
11494 op1 = expand_normal (minval);
11495 op2 = expand_normal (range);
11497 create_input_operand (&ops[0], index, index_mode);
11498 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11499 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11500 create_fixed_operand (&ops[3], table_label);
11501 create_fixed_operand (&ops[4], (default_label
11502 ? default_label
11503 : fallback_label));
11504 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11505 return 1;
11508 /* Attempt to generate a tablejump instruction; same concept. */
11509 /* Subroutine of the next function.
11511 INDEX is the value being switched on, with the lowest value
11512 in the table already subtracted.
11513 MODE is its expected mode (needed if INDEX is constant).
11514 RANGE is the length of the jump table.
11515 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11517 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11518 index value is out of range.
11519 DEFAULT_PROBABILITY is the probability of jumping to
11520 the default label. */
11522 static void
11523 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11524 rtx default_label, int default_probability)
11526 rtx temp, vector;
11528 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11529 cfun->cfg->max_jumptable_ents = INTVAL (range);
11531 /* Do an unsigned comparison (in the proper mode) between the index
11532 expression and the value which represents the length of the range.
11533 Since we just finished subtracting the lower bound of the range
11534 from the index expression, this comparison allows us to simultaneously
11535 check that the original index expression value is both greater than
11536 or equal to the minimum value of the range and less than or equal to
11537 the maximum value of the range. */
11539 if (default_label)
11540 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11541 default_label, default_probability);
11544 /* If index is in range, it must fit in Pmode.
11545 Convert to Pmode so we can index with it. */
11546 if (mode != Pmode)
11547 index = convert_to_mode (Pmode, index, 1);
11549 /* Don't let a MEM slip through, because then INDEX that comes
11550 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11551 and break_out_memory_refs will go to work on it and mess it up. */
11552 #ifdef PIC_CASE_VECTOR_ADDRESS
11553 if (flag_pic && !REG_P (index))
11554 index = copy_to_mode_reg (Pmode, index);
11555 #endif
11557 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11558 GET_MODE_SIZE, because this indicates how large insns are. The other
11559 uses should all be Pmode, because they are addresses. This code
11560 could fail if addresses and insns are not the same size. */
11561 index = simplify_gen_binary (MULT, Pmode, index,
11562 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11563 Pmode));
11564 index = simplify_gen_binary (PLUS, Pmode, index,
11565 gen_rtx_LABEL_REF (Pmode, table_label));
11567 #ifdef PIC_CASE_VECTOR_ADDRESS
11568 if (flag_pic)
11569 index = PIC_CASE_VECTOR_ADDRESS (index);
11570 else
11571 #endif
11572 index = memory_address (CASE_VECTOR_MODE, index);
11573 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11574 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11575 convert_move (temp, vector, 0);
11577 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11579 /* If we are generating PIC code or if the table is PC-relative, the
11580 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11581 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11582 emit_barrier ();
11586 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11587 rtx table_label, rtx default_label, int default_probability)
11589 rtx index;
11591 if (! targetm.have_tablejump ())
11592 return 0;
11594 index_expr = fold_build2 (MINUS_EXPR, index_type,
11595 fold_convert (index_type, index_expr),
11596 fold_convert (index_type, minval));
11597 index = expand_normal (index_expr);
11598 do_pending_stack_adjust ();
11600 do_tablejump (index, TYPE_MODE (index_type),
11601 convert_modes (TYPE_MODE (index_type),
11602 TYPE_MODE (TREE_TYPE (range)),
11603 expand_normal (range),
11604 TYPE_UNSIGNED (TREE_TYPE (range))),
11605 table_label, default_label, default_probability);
11606 return 1;
11609 /* Return a CONST_VECTOR rtx representing vector mask for
11610 a VECTOR_CST of booleans. */
11611 static rtx
11612 const_vector_mask_from_tree (tree exp)
11614 rtvec v;
11615 unsigned i;
11616 int units;
11617 tree elt;
11618 machine_mode inner, mode;
11620 mode = TYPE_MODE (TREE_TYPE (exp));
11621 units = GET_MODE_NUNITS (mode);
11622 inner = GET_MODE_INNER (mode);
11624 v = rtvec_alloc (units);
11626 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11628 elt = VECTOR_CST_ELT (exp, i);
11630 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11631 if (integer_zerop (elt))
11632 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11633 else if (integer_onep (elt)
11634 || integer_minus_onep (elt))
11635 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11636 else
11637 gcc_unreachable ();
11640 return gen_rtx_CONST_VECTOR (mode, v);
11643 /* Return a CONST_INT rtx representing vector mask for
11644 a VECTOR_CST of booleans. */
11645 static rtx
11646 const_scalar_mask_from_tree (tree exp)
11648 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11649 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11650 tree elt;
11651 unsigned i;
11653 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11655 elt = VECTOR_CST_ELT (exp, i);
11656 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11657 if (integer_all_onesp (elt))
11658 res = wi::set_bit (res, i);
11659 else
11660 gcc_assert (integer_zerop (elt));
11663 return immed_wide_int_const (res, mode);
11666 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11667 static rtx
11668 const_vector_from_tree (tree exp)
11670 rtvec v;
11671 unsigned i;
11672 int units;
11673 tree elt;
11674 machine_mode inner, mode;
11676 mode = TYPE_MODE (TREE_TYPE (exp));
11678 if (initializer_zerop (exp))
11679 return CONST0_RTX (mode);
11681 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11682 return const_vector_mask_from_tree (exp);
11684 units = GET_MODE_NUNITS (mode);
11685 inner = GET_MODE_INNER (mode);
11687 v = rtvec_alloc (units);
11689 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11691 elt = VECTOR_CST_ELT (exp, i);
11693 if (TREE_CODE (elt) == REAL_CST)
11694 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11695 inner);
11696 else if (TREE_CODE (elt) == FIXED_CST)
11697 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11698 inner);
11699 else
11700 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11703 return gen_rtx_CONST_VECTOR (mode, v);
11706 /* Build a decl for a personality function given a language prefix. */
11708 tree
11709 build_personality_function (const char *lang)
11711 const char *unwind_and_version;
11712 tree decl, type;
11713 char *name;
11715 switch (targetm_common.except_unwind_info (&global_options))
11717 case UI_NONE:
11718 return NULL;
11719 case UI_SJLJ:
11720 unwind_and_version = "_sj0";
11721 break;
11722 case UI_DWARF2:
11723 case UI_TARGET:
11724 unwind_and_version = "_v0";
11725 break;
11726 case UI_SEH:
11727 unwind_and_version = "_seh0";
11728 break;
11729 default:
11730 gcc_unreachable ();
11733 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11735 type = build_function_type_list (integer_type_node, integer_type_node,
11736 long_long_unsigned_type_node,
11737 ptr_type_node, ptr_type_node, NULL_TREE);
11738 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11739 get_identifier (name), type);
11740 DECL_ARTIFICIAL (decl) = 1;
11741 DECL_EXTERNAL (decl) = 1;
11742 TREE_PUBLIC (decl) = 1;
11744 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11745 are the flags assigned by targetm.encode_section_info. */
11746 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11748 return decl;
11751 /* Extracts the personality function of DECL and returns the corresponding
11752 libfunc. */
11755 get_personality_function (tree decl)
11757 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11758 enum eh_personality_kind pk;
11760 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11761 if (pk == eh_personality_none)
11762 return NULL;
11764 if (!personality
11765 && pk == eh_personality_any)
11766 personality = lang_hooks.eh_personality ();
11768 if (pk == eh_personality_lang)
11769 gcc_assert (personality != NULL_TREE);
11771 return XEXP (DECL_RTL (personality), 0);
11774 /* Returns a tree for the size of EXP in bytes. */
11776 static tree
11777 tree_expr_size (const_tree exp)
11779 if (DECL_P (exp)
11780 && DECL_SIZE_UNIT (exp) != 0)
11781 return DECL_SIZE_UNIT (exp);
11782 else
11783 return size_in_bytes (TREE_TYPE (exp));
11786 /* Return an rtx for the size in bytes of the value of EXP. */
11789 expr_size (tree exp)
11791 tree size;
11793 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11794 size = TREE_OPERAND (exp, 1);
11795 else
11797 size = tree_expr_size (exp);
11798 gcc_assert (size);
11799 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11802 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11805 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11806 if the size can vary or is larger than an integer. */
11808 static HOST_WIDE_INT
11809 int_expr_size (tree exp)
11811 tree size;
11813 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11814 size = TREE_OPERAND (exp, 1);
11815 else
11817 size = tree_expr_size (exp);
11818 gcc_assert (size);
11821 if (size == 0 || !tree_fits_shwi_p (size))
11822 return -1;
11824 return tree_to_shwi (size);