2016-10-22 Thomas Koenig <tkoenig@gcc.gnu.org>
[official-gcc.git] / gcc / expr.c
blob28242b2551d017ca354ada6db1fcd39521f4af6c
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 insn, 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 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 (std::make_pair (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)));
2179 if ((bytepos == 0 && bytelen == slen0)
2180 || (bytepos != 0 && bytepos + bytelen <= slen))
2182 /* The following assumes that the concatenated objects all
2183 have the same size. In this case, a simple calculation
2184 can be used to determine the object and the bit field
2185 to be extracted. */
2186 tmps[i] = XEXP (src, bytepos / slen0);
2187 if (! CONSTANT_P (tmps[i])
2188 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
2189 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2190 (bytepos % slen0) * BITS_PER_UNIT,
2191 1, NULL_RTX, mode, mode, false);
2193 else
2195 rtx mem;
2197 gcc_assert (!bytepos);
2198 mem = assign_stack_temp (GET_MODE (src), slen);
2199 emit_move_insn (mem, src);
2200 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2201 0, 1, NULL_RTX, mode, mode, false);
2204 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2205 SIMD register, which is currently broken. While we get GCC
2206 to emit proper RTL for these cases, let's dump to memory. */
2207 else if (VECTOR_MODE_P (GET_MODE (dst))
2208 && REG_P (src))
2210 int slen = GET_MODE_SIZE (GET_MODE (src));
2211 rtx mem;
2213 mem = assign_stack_temp (GET_MODE (src), slen);
2214 emit_move_insn (mem, src);
2215 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2217 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2218 && XVECLEN (dst, 0) > 1)
2219 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2220 else if (CONSTANT_P (src))
2222 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2224 if (len == ssize)
2225 tmps[i] = src;
2226 else
2228 rtx first, second;
2230 /* TODO: const_wide_int can have sizes other than this... */
2231 gcc_assert (2 * len == ssize);
2232 split_double (src, &first, &second);
2233 if (i)
2234 tmps[i] = second;
2235 else
2236 tmps[i] = first;
2239 else if (REG_P (src) && GET_MODE (src) == mode)
2240 tmps[i] = src;
2241 else
2242 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2243 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2244 mode, mode, false);
2246 if (shift)
2247 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2248 shift, tmps[i], 0);
2252 /* Emit code to move a block SRC of type TYPE to a block DST,
2253 where DST is non-consecutive registers represented by a PARALLEL.
2254 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2255 if not known. */
2257 void
2258 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2260 rtx *tmps;
2261 int i;
2263 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2264 emit_group_load_1 (tmps, dst, src, type, ssize);
2266 /* Copy the extracted pieces into the proper (probable) hard regs. */
2267 for (i = 0; i < XVECLEN (dst, 0); i++)
2269 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2270 if (d == NULL)
2271 continue;
2272 emit_move_insn (d, tmps[i]);
2276 /* Similar, but load SRC into new pseudos in a format that looks like
2277 PARALLEL. This can later be fed to emit_group_move to get things
2278 in the right place. */
2281 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2283 rtvec vec;
2284 int i;
2286 vec = rtvec_alloc (XVECLEN (parallel, 0));
2287 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2289 /* Convert the vector to look just like the original PARALLEL, except
2290 with the computed values. */
2291 for (i = 0; i < XVECLEN (parallel, 0); i++)
2293 rtx e = XVECEXP (parallel, 0, i);
2294 rtx d = XEXP (e, 0);
2296 if (d)
2298 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2299 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2301 RTVEC_ELT (vec, i) = e;
2304 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2307 /* Emit code to move a block SRC to block DST, where SRC and DST are
2308 non-consecutive groups of registers, each represented by a PARALLEL. */
2310 void
2311 emit_group_move (rtx dst, rtx src)
2313 int i;
2315 gcc_assert (GET_CODE (src) == PARALLEL
2316 && GET_CODE (dst) == PARALLEL
2317 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2319 /* Skip first entry if NULL. */
2320 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2321 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2322 XEXP (XVECEXP (src, 0, i), 0));
2325 /* Move a group of registers represented by a PARALLEL into pseudos. */
2328 emit_group_move_into_temps (rtx src)
2330 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2331 int i;
2333 for (i = 0; i < XVECLEN (src, 0); i++)
2335 rtx e = XVECEXP (src, 0, i);
2336 rtx d = XEXP (e, 0);
2338 if (d)
2339 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2340 RTVEC_ELT (vec, i) = e;
2343 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2346 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2347 where SRC is non-consecutive registers represented by a PARALLEL.
2348 SSIZE represents the total size of block ORIG_DST, or -1 if not
2349 known. */
2351 void
2352 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2354 rtx *tmps, dst;
2355 int start, finish, i;
2356 machine_mode m = GET_MODE (orig_dst);
2358 gcc_assert (GET_CODE (src) == PARALLEL);
2360 if (!SCALAR_INT_MODE_P (m)
2361 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2363 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2364 if (imode == BLKmode)
2365 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2366 else
2367 dst = gen_reg_rtx (imode);
2368 emit_group_store (dst, src, type, ssize);
2369 if (imode != BLKmode)
2370 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2371 emit_move_insn (orig_dst, dst);
2372 return;
2375 /* Check for a NULL entry, used to indicate that the parameter goes
2376 both on the stack and in registers. */
2377 if (XEXP (XVECEXP (src, 0, 0), 0))
2378 start = 0;
2379 else
2380 start = 1;
2381 finish = XVECLEN (src, 0);
2383 tmps = XALLOCAVEC (rtx, finish);
2385 /* Copy the (probable) hard regs into pseudos. */
2386 for (i = start; i < finish; i++)
2388 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2389 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2391 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2392 emit_move_insn (tmps[i], reg);
2394 else
2395 tmps[i] = reg;
2398 /* If we won't be storing directly into memory, protect the real destination
2399 from strange tricks we might play. */
2400 dst = orig_dst;
2401 if (GET_CODE (dst) == PARALLEL)
2403 rtx temp;
2405 /* We can get a PARALLEL dst if there is a conditional expression in
2406 a return statement. In that case, the dst and src are the same,
2407 so no action is necessary. */
2408 if (rtx_equal_p (dst, src))
2409 return;
2411 /* It is unclear if we can ever reach here, but we may as well handle
2412 it. Allocate a temporary, and split this into a store/load to/from
2413 the temporary. */
2414 temp = assign_stack_temp (GET_MODE (dst), ssize);
2415 emit_group_store (temp, src, type, ssize);
2416 emit_group_load (dst, temp, type, ssize);
2417 return;
2419 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2421 machine_mode outer = GET_MODE (dst);
2422 machine_mode inner;
2423 HOST_WIDE_INT bytepos;
2424 bool done = false;
2425 rtx temp;
2427 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2428 dst = gen_reg_rtx (outer);
2430 /* Make life a bit easier for combine. */
2431 /* If the first element of the vector is the low part
2432 of the destination mode, use a paradoxical subreg to
2433 initialize the destination. */
2434 if (start < finish)
2436 inner = GET_MODE (tmps[start]);
2437 bytepos = subreg_lowpart_offset (inner, outer);
2438 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2440 temp = simplify_gen_subreg (outer, tmps[start],
2441 inner, 0);
2442 if (temp)
2444 emit_move_insn (dst, temp);
2445 done = true;
2446 start++;
2451 /* If the first element wasn't the low part, try the last. */
2452 if (!done
2453 && start < finish - 1)
2455 inner = GET_MODE (tmps[finish - 1]);
2456 bytepos = subreg_lowpart_offset (inner, outer);
2457 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2459 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2460 inner, 0);
2461 if (temp)
2463 emit_move_insn (dst, temp);
2464 done = true;
2465 finish--;
2470 /* Otherwise, simply initialize the result to zero. */
2471 if (!done)
2472 emit_move_insn (dst, CONST0_RTX (outer));
2475 /* Process the pieces. */
2476 for (i = start; i < finish; i++)
2478 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2479 machine_mode mode = GET_MODE (tmps[i]);
2480 unsigned int bytelen = GET_MODE_SIZE (mode);
2481 unsigned int adj_bytelen;
2482 rtx dest = dst;
2484 /* Handle trailing fragments that run over the size of the struct. */
2485 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2486 adj_bytelen = ssize - bytepos;
2487 else
2488 adj_bytelen = bytelen;
2490 if (GET_CODE (dst) == CONCAT)
2492 if (bytepos + adj_bytelen
2493 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2494 dest = XEXP (dst, 0);
2495 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2497 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2498 dest = XEXP (dst, 1);
2500 else
2502 machine_mode dest_mode = GET_MODE (dest);
2503 machine_mode tmp_mode = GET_MODE (tmps[i]);
2505 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2507 if (GET_MODE_ALIGNMENT (dest_mode)
2508 >= GET_MODE_ALIGNMENT (tmp_mode))
2510 dest = assign_stack_temp (dest_mode,
2511 GET_MODE_SIZE (dest_mode));
2512 emit_move_insn (adjust_address (dest,
2513 tmp_mode,
2514 bytepos),
2515 tmps[i]);
2516 dst = dest;
2518 else
2520 dest = assign_stack_temp (tmp_mode,
2521 GET_MODE_SIZE (tmp_mode));
2522 emit_move_insn (dest, tmps[i]);
2523 dst = adjust_address (dest, dest_mode, bytepos);
2525 break;
2529 /* Handle trailing fragments that run over the size of the struct. */
2530 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2532 /* store_bit_field always takes its value from the lsb.
2533 Move the fragment to the lsb if it's not already there. */
2534 if (
2535 #ifdef BLOCK_REG_PADDING
2536 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2537 == (BYTES_BIG_ENDIAN ? upward : downward)
2538 #else
2539 BYTES_BIG_ENDIAN
2540 #endif
2543 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2544 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2545 shift, tmps[i], 0);
2548 /* Make sure not to write past the end of the struct. */
2549 store_bit_field (dest,
2550 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2551 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2552 VOIDmode, tmps[i], false);
2555 /* Optimize the access just a bit. */
2556 else if (MEM_P (dest)
2557 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2558 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2559 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2560 && bytelen == GET_MODE_SIZE (mode))
2561 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2563 else
2564 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2565 0, 0, mode, tmps[i], false);
2568 /* Copy from the pseudo into the (probable) hard reg. */
2569 if (orig_dst != dst)
2570 emit_move_insn (orig_dst, dst);
2573 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2574 of the value stored in X. */
2577 maybe_emit_group_store (rtx x, tree type)
2579 machine_mode mode = TYPE_MODE (type);
2580 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2581 if (GET_CODE (x) == PARALLEL)
2583 rtx result = gen_reg_rtx (mode);
2584 emit_group_store (result, x, type, int_size_in_bytes (type));
2585 return result;
2587 return x;
2590 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2592 This is used on targets that return BLKmode values in registers. */
2594 void
2595 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2597 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2598 rtx src = NULL, dst = NULL;
2599 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2600 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2601 machine_mode mode = GET_MODE (srcreg);
2602 machine_mode tmode = GET_MODE (target);
2603 machine_mode copy_mode;
2605 /* BLKmode registers created in the back-end shouldn't have survived. */
2606 gcc_assert (mode != BLKmode);
2608 /* If the structure doesn't take up a whole number of words, see whether
2609 SRCREG is padded on the left or on the right. If it's on the left,
2610 set PADDING_CORRECTION to the number of bits to skip.
2612 In most ABIs, the structure will be returned at the least end of
2613 the register, which translates to right padding on little-endian
2614 targets and left padding on big-endian targets. The opposite
2615 holds if the structure is returned at the most significant
2616 end of the register. */
2617 if (bytes % UNITS_PER_WORD != 0
2618 && (targetm.calls.return_in_msb (type)
2619 ? !BYTES_BIG_ENDIAN
2620 : BYTES_BIG_ENDIAN))
2621 padding_correction
2622 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2624 /* We can use a single move if we have an exact mode for the size. */
2625 else if (MEM_P (target)
2626 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2627 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2628 && bytes == GET_MODE_SIZE (mode))
2630 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2631 return;
2634 /* And if we additionally have the same mode for a register. */
2635 else if (REG_P (target)
2636 && GET_MODE (target) == mode
2637 && bytes == GET_MODE_SIZE (mode))
2639 emit_move_insn (target, srcreg);
2640 return;
2643 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2644 into a new pseudo which is a full word. */
2645 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2647 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2648 mode = word_mode;
2651 /* Copy the structure BITSIZE bits at a time. If the target lives in
2652 memory, take care of not reading/writing past its end by selecting
2653 a copy mode suited to BITSIZE. This should always be possible given
2654 how it is computed.
2656 If the target lives in register, make sure not to select a copy mode
2657 larger than the mode of the register.
2659 We could probably emit more efficient code for machines which do not use
2660 strict alignment, but it doesn't seem worth the effort at the current
2661 time. */
2663 copy_mode = word_mode;
2664 if (MEM_P (target))
2666 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2667 if (mem_mode != BLKmode)
2668 copy_mode = mem_mode;
2670 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2671 copy_mode = tmode;
2673 for (bitpos = 0, xbitpos = padding_correction;
2674 bitpos < bytes * BITS_PER_UNIT;
2675 bitpos += bitsize, xbitpos += bitsize)
2677 /* We need a new source operand each time xbitpos is on a
2678 word boundary and when xbitpos == padding_correction
2679 (the first time through). */
2680 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2681 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2683 /* We need a new destination operand each time bitpos is on
2684 a word boundary. */
2685 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2686 dst = target;
2687 else if (bitpos % BITS_PER_WORD == 0)
2688 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2690 /* Use xbitpos for the source extraction (right justified) and
2691 bitpos for the destination store (left justified). */
2692 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2693 extract_bit_field (src, bitsize,
2694 xbitpos % BITS_PER_WORD, 1,
2695 NULL_RTX, copy_mode, copy_mode,
2696 false),
2697 false);
2701 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2702 register if it contains any data, otherwise return null.
2704 This is used on targets that return BLKmode values in registers. */
2707 copy_blkmode_to_reg (machine_mode mode, tree src)
2709 int i, n_regs;
2710 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2711 unsigned int bitsize;
2712 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2713 machine_mode dst_mode;
2715 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2717 x = expand_normal (src);
2719 bytes = int_size_in_bytes (TREE_TYPE (src));
2720 if (bytes == 0)
2721 return NULL_RTX;
2723 /* If the structure doesn't take up a whole number of words, see
2724 whether the register value should be padded on the left or on
2725 the right. Set PADDING_CORRECTION to the number of padding
2726 bits needed on the left side.
2728 In most ABIs, the structure will be returned at the least end of
2729 the register, which translates to right padding on little-endian
2730 targets and left padding on big-endian targets. The opposite
2731 holds if the structure is returned at the most significant
2732 end of the register. */
2733 if (bytes % UNITS_PER_WORD != 0
2734 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2735 ? !BYTES_BIG_ENDIAN
2736 : BYTES_BIG_ENDIAN))
2737 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2738 * BITS_PER_UNIT));
2740 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2741 dst_words = XALLOCAVEC (rtx, n_regs);
2742 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2744 /* Copy the structure BITSIZE bits at a time. */
2745 for (bitpos = 0, xbitpos = padding_correction;
2746 bitpos < bytes * BITS_PER_UNIT;
2747 bitpos += bitsize, xbitpos += bitsize)
2749 /* We need a new destination pseudo each time xbitpos is
2750 on a word boundary and when xbitpos == padding_correction
2751 (the first time through). */
2752 if (xbitpos % BITS_PER_WORD == 0
2753 || xbitpos == padding_correction)
2755 /* Generate an appropriate register. */
2756 dst_word = gen_reg_rtx (word_mode);
2757 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2759 /* Clear the destination before we move anything into it. */
2760 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2763 /* We need a new source operand each time bitpos is on a word
2764 boundary. */
2765 if (bitpos % BITS_PER_WORD == 0)
2766 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2768 /* Use bitpos for the source extraction (left justified) and
2769 xbitpos for the destination store (right justified). */
2770 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2771 0, 0, word_mode,
2772 extract_bit_field (src_word, bitsize,
2773 bitpos % BITS_PER_WORD, 1,
2774 NULL_RTX, word_mode, word_mode,
2775 false),
2776 false);
2779 if (mode == BLKmode)
2781 /* Find the smallest integer mode large enough to hold the
2782 entire structure. */
2783 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2784 mode != VOIDmode;
2785 mode = GET_MODE_WIDER_MODE (mode))
2786 /* Have we found a large enough mode? */
2787 if (GET_MODE_SIZE (mode) >= bytes)
2788 break;
2790 /* A suitable mode should have been found. */
2791 gcc_assert (mode != VOIDmode);
2794 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2795 dst_mode = word_mode;
2796 else
2797 dst_mode = mode;
2798 dst = gen_reg_rtx (dst_mode);
2800 for (i = 0; i < n_regs; i++)
2801 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2803 if (mode != dst_mode)
2804 dst = gen_lowpart (mode, dst);
2806 return dst;
2809 /* Add a USE expression for REG to the (possibly empty) list pointed
2810 to by CALL_FUSAGE. REG must denote a hard register. */
2812 void
2813 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2815 gcc_assert (REG_P (reg));
2817 if (!HARD_REGISTER_P (reg))
2818 return;
2820 *call_fusage
2821 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2824 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2825 to by CALL_FUSAGE. REG must denote a hard register. */
2827 void
2828 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2830 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2832 *call_fusage
2833 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2836 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2837 starting at REGNO. All of these registers must be hard registers. */
2839 void
2840 use_regs (rtx *call_fusage, int regno, int nregs)
2842 int i;
2844 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2846 for (i = 0; i < nregs; i++)
2847 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2850 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2851 PARALLEL REGS. This is for calls that pass values in multiple
2852 non-contiguous locations. The Irix 6 ABI has examples of this. */
2854 void
2855 use_group_regs (rtx *call_fusage, rtx regs)
2857 int i;
2859 for (i = 0; i < XVECLEN (regs, 0); i++)
2861 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2863 /* A NULL entry means the parameter goes both on the stack and in
2864 registers. This can also be a MEM for targets that pass values
2865 partially on the stack and partially in registers. */
2866 if (reg != 0 && REG_P (reg))
2867 use_reg (call_fusage, reg);
2871 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2872 assigment and the code of the expresion on the RHS is CODE. Return
2873 NULL otherwise. */
2875 static gimple *
2876 get_def_for_expr (tree name, enum tree_code code)
2878 gimple *def_stmt;
2880 if (TREE_CODE (name) != SSA_NAME)
2881 return NULL;
2883 def_stmt = get_gimple_for_ssa_name (name);
2884 if (!def_stmt
2885 || gimple_assign_rhs_code (def_stmt) != code)
2886 return NULL;
2888 return def_stmt;
2891 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2892 assigment and the class of the expresion on the RHS is CLASS. Return
2893 NULL otherwise. */
2895 static gimple *
2896 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2898 gimple *def_stmt;
2900 if (TREE_CODE (name) != SSA_NAME)
2901 return NULL;
2903 def_stmt = get_gimple_for_ssa_name (name);
2904 if (!def_stmt
2905 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2906 return NULL;
2908 return def_stmt;
2911 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2912 its length in bytes. */
2915 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2916 unsigned int expected_align, HOST_WIDE_INT expected_size,
2917 unsigned HOST_WIDE_INT min_size,
2918 unsigned HOST_WIDE_INT max_size,
2919 unsigned HOST_WIDE_INT probable_max_size)
2921 machine_mode mode = GET_MODE (object);
2922 unsigned int align;
2924 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2926 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2927 just move a zero. Otherwise, do this a piece at a time. */
2928 if (mode != BLKmode
2929 && CONST_INT_P (size)
2930 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2932 rtx zero = CONST0_RTX (mode);
2933 if (zero != NULL)
2935 emit_move_insn (object, zero);
2936 return NULL;
2939 if (COMPLEX_MODE_P (mode))
2941 zero = CONST0_RTX (GET_MODE_INNER (mode));
2942 if (zero != NULL)
2944 write_complex_part (object, zero, 0);
2945 write_complex_part (object, zero, 1);
2946 return NULL;
2951 if (size == const0_rtx)
2952 return NULL;
2954 align = MEM_ALIGN (object);
2956 if (CONST_INT_P (size)
2957 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2958 CLEAR_BY_PIECES,
2959 optimize_insn_for_speed_p ()))
2960 clear_by_pieces (object, INTVAL (size), align);
2961 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2962 expected_align, expected_size,
2963 min_size, max_size, probable_max_size))
2965 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2966 return set_storage_via_libcall (object, size, const0_rtx,
2967 method == BLOCK_OP_TAILCALL);
2968 else
2969 gcc_unreachable ();
2971 return NULL;
2975 clear_storage (rtx object, rtx size, enum block_op_methods method)
2977 unsigned HOST_WIDE_INT max, min = 0;
2978 if (GET_CODE (size) == CONST_INT)
2979 min = max = UINTVAL (size);
2980 else
2981 max = GET_MODE_MASK (GET_MODE (size));
2982 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2986 /* A subroutine of clear_storage. Expand a call to memset.
2987 Return the return value of memset, 0 otherwise. */
2990 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2992 tree call_expr, fn, object_tree, size_tree, val_tree;
2993 machine_mode size_mode;
2995 object = copy_addr_to_reg (XEXP (object, 0));
2996 object_tree = make_tree (ptr_type_node, object);
2998 if (!CONST_INT_P (val))
2999 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3000 val_tree = make_tree (integer_type_node, val);
3002 size_mode = TYPE_MODE (sizetype);
3003 size = convert_to_mode (size_mode, size, 1);
3004 size = copy_to_mode_reg (size_mode, size);
3005 size_tree = make_tree (sizetype, size);
3007 /* It is incorrect to use the libcall calling conventions for calls to
3008 memset because it can be provided by the user. */
3009 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3010 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3011 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3013 return expand_call (call_expr, NULL_RTX, false);
3016 /* Expand a setmem pattern; return true if successful. */
3018 bool
3019 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3020 unsigned int expected_align, HOST_WIDE_INT expected_size,
3021 unsigned HOST_WIDE_INT min_size,
3022 unsigned HOST_WIDE_INT max_size,
3023 unsigned HOST_WIDE_INT probable_max_size)
3025 /* Try the most limited insn first, because there's no point
3026 including more than one in the machine description unless
3027 the more limited one has some advantage. */
3029 machine_mode mode;
3031 if (expected_align < align)
3032 expected_align = align;
3033 if (expected_size != -1)
3035 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3036 expected_size = max_size;
3037 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3038 expected_size = min_size;
3041 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3042 mode = GET_MODE_WIDER_MODE (mode))
3044 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3046 if (code != CODE_FOR_nothing
3047 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3048 here because if SIZE is less than the mode mask, as it is
3049 returned by the macro, it will definitely be less than the
3050 actual mode mask. Since SIZE is within the Pmode address
3051 space, we limit MODE to Pmode. */
3052 && ((CONST_INT_P (size)
3053 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3054 <= (GET_MODE_MASK (mode) >> 1)))
3055 || max_size <= (GET_MODE_MASK (mode) >> 1)
3056 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3058 struct expand_operand ops[9];
3059 unsigned int nops;
3061 nops = insn_data[(int) code].n_generator_args;
3062 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3064 create_fixed_operand (&ops[0], object);
3065 /* The check above guarantees that this size conversion is valid. */
3066 create_convert_operand_to (&ops[1], size, mode, true);
3067 create_convert_operand_from (&ops[2], val, byte_mode, true);
3068 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3069 if (nops >= 6)
3071 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3072 create_integer_operand (&ops[5], expected_size);
3074 if (nops >= 8)
3076 create_integer_operand (&ops[6], min_size);
3077 /* If we can not represent the maximal size,
3078 make parameter NULL. */
3079 if ((HOST_WIDE_INT) max_size != -1)
3080 create_integer_operand (&ops[7], max_size);
3081 else
3082 create_fixed_operand (&ops[7], NULL);
3084 if (nops == 9)
3086 /* If we can not represent the maximal size,
3087 make parameter NULL. */
3088 if ((HOST_WIDE_INT) probable_max_size != -1)
3089 create_integer_operand (&ops[8], probable_max_size);
3090 else
3091 create_fixed_operand (&ops[8], NULL);
3093 if (maybe_expand_insn (code, nops, ops))
3094 return true;
3098 return false;
3102 /* Write to one of the components of the complex value CPLX. Write VAL to
3103 the real part if IMAG_P is false, and the imaginary part if its true. */
3105 void
3106 write_complex_part (rtx cplx, rtx val, bool imag_p)
3108 machine_mode cmode;
3109 machine_mode imode;
3110 unsigned ibitsize;
3112 if (GET_CODE (cplx) == CONCAT)
3114 emit_move_insn (XEXP (cplx, imag_p), val);
3115 return;
3118 cmode = GET_MODE (cplx);
3119 imode = GET_MODE_INNER (cmode);
3120 ibitsize = GET_MODE_BITSIZE (imode);
3122 /* For MEMs simplify_gen_subreg may generate an invalid new address
3123 because, e.g., the original address is considered mode-dependent
3124 by the target, which restricts simplify_subreg from invoking
3125 adjust_address_nv. Instead of preparing fallback support for an
3126 invalid address, we call adjust_address_nv directly. */
3127 if (MEM_P (cplx))
3129 emit_move_insn (adjust_address_nv (cplx, imode,
3130 imag_p ? GET_MODE_SIZE (imode) : 0),
3131 val);
3132 return;
3135 /* If the sub-object is at least word sized, then we know that subregging
3136 will work. This special case is important, since store_bit_field
3137 wants to operate on integer modes, and there's rarely an OImode to
3138 correspond to TCmode. */
3139 if (ibitsize >= BITS_PER_WORD
3140 /* For hard regs we have exact predicates. Assume we can split
3141 the original object if it spans an even number of hard regs.
3142 This special case is important for SCmode on 64-bit platforms
3143 where the natural size of floating-point regs is 32-bit. */
3144 || (REG_P (cplx)
3145 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3146 && REG_NREGS (cplx) % 2 == 0))
3148 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3149 imag_p ? GET_MODE_SIZE (imode) : 0);
3150 if (part)
3152 emit_move_insn (part, val);
3153 return;
3155 else
3156 /* simplify_gen_subreg may fail for sub-word MEMs. */
3157 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3160 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3161 false);
3164 /* Extract one of the components of the complex value CPLX. Extract the
3165 real part if IMAG_P is false, and the imaginary part if it's true. */
3168 read_complex_part (rtx cplx, bool imag_p)
3170 machine_mode cmode, imode;
3171 unsigned ibitsize;
3173 if (GET_CODE (cplx) == CONCAT)
3174 return XEXP (cplx, imag_p);
3176 cmode = GET_MODE (cplx);
3177 imode = GET_MODE_INNER (cmode);
3178 ibitsize = GET_MODE_BITSIZE (imode);
3180 /* Special case reads from complex constants that got spilled to memory. */
3181 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3183 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3184 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3186 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3187 if (CONSTANT_CLASS_P (part))
3188 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3192 /* For MEMs simplify_gen_subreg may generate an invalid new address
3193 because, e.g., the original address is considered mode-dependent
3194 by the target, which restricts simplify_subreg from invoking
3195 adjust_address_nv. Instead of preparing fallback support for an
3196 invalid address, we call adjust_address_nv directly. */
3197 if (MEM_P (cplx))
3198 return adjust_address_nv (cplx, imode,
3199 imag_p ? GET_MODE_SIZE (imode) : 0);
3201 /* If the sub-object is at least word sized, then we know that subregging
3202 will work. This special case is important, since extract_bit_field
3203 wants to operate on integer modes, and there's rarely an OImode to
3204 correspond to TCmode. */
3205 if (ibitsize >= BITS_PER_WORD
3206 /* For hard regs we have exact predicates. Assume we can split
3207 the original object if it spans an even number of hard regs.
3208 This special case is important for SCmode on 64-bit platforms
3209 where the natural size of floating-point regs is 32-bit. */
3210 || (REG_P (cplx)
3211 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3212 && REG_NREGS (cplx) % 2 == 0))
3214 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3215 imag_p ? GET_MODE_SIZE (imode) : 0);
3216 if (ret)
3217 return ret;
3218 else
3219 /* simplify_gen_subreg may fail for sub-word MEMs. */
3220 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3223 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3224 true, NULL_RTX, imode, imode, false);
3227 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3228 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3229 represented in NEW_MODE. If FORCE is true, this will never happen, as
3230 we'll force-create a SUBREG if needed. */
3232 static rtx
3233 emit_move_change_mode (machine_mode new_mode,
3234 machine_mode old_mode, rtx x, bool force)
3236 rtx ret;
3238 if (push_operand (x, GET_MODE (x)))
3240 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3241 MEM_COPY_ATTRIBUTES (ret, x);
3243 else if (MEM_P (x))
3245 /* We don't have to worry about changing the address since the
3246 size in bytes is supposed to be the same. */
3247 if (reload_in_progress)
3249 /* Copy the MEM to change the mode and move any
3250 substitutions from the old MEM to the new one. */
3251 ret = adjust_address_nv (x, new_mode, 0);
3252 copy_replacements (x, ret);
3254 else
3255 ret = adjust_address (x, new_mode, 0);
3257 else
3259 /* Note that we do want simplify_subreg's behavior of validating
3260 that the new mode is ok for a hard register. If we were to use
3261 simplify_gen_subreg, we would create the subreg, but would
3262 probably run into the target not being able to implement it. */
3263 /* Except, of course, when FORCE is true, when this is exactly what
3264 we want. Which is needed for CCmodes on some targets. */
3265 if (force)
3266 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3267 else
3268 ret = simplify_subreg (new_mode, x, old_mode, 0);
3271 return ret;
3274 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3275 an integer mode of the same size as MODE. Returns the instruction
3276 emitted, or NULL if such a move could not be generated. */
3278 static rtx_insn *
3279 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3281 machine_mode imode;
3282 enum insn_code code;
3284 /* There must exist a mode of the exact size we require. */
3285 imode = int_mode_for_mode (mode);
3286 if (imode == BLKmode)
3287 return NULL;
3289 /* The target must support moves in this mode. */
3290 code = optab_handler (mov_optab, imode);
3291 if (code == CODE_FOR_nothing)
3292 return NULL;
3294 x = emit_move_change_mode (imode, mode, x, force);
3295 if (x == NULL_RTX)
3296 return NULL;
3297 y = emit_move_change_mode (imode, mode, y, force);
3298 if (y == NULL_RTX)
3299 return NULL;
3300 return emit_insn (GEN_FCN (code) (x, y));
3303 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3304 Return an equivalent MEM that does not use an auto-increment. */
3307 emit_move_resolve_push (machine_mode mode, rtx x)
3309 enum rtx_code code = GET_CODE (XEXP (x, 0));
3310 HOST_WIDE_INT adjust;
3311 rtx temp;
3313 adjust = GET_MODE_SIZE (mode);
3314 #ifdef PUSH_ROUNDING
3315 adjust = PUSH_ROUNDING (adjust);
3316 #endif
3317 if (code == PRE_DEC || code == POST_DEC)
3318 adjust = -adjust;
3319 else if (code == PRE_MODIFY || code == POST_MODIFY)
3321 rtx expr = XEXP (XEXP (x, 0), 1);
3322 HOST_WIDE_INT val;
3324 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3325 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3326 val = INTVAL (XEXP (expr, 1));
3327 if (GET_CODE (expr) == MINUS)
3328 val = -val;
3329 gcc_assert (adjust == val || adjust == -val);
3330 adjust = val;
3333 /* Do not use anti_adjust_stack, since we don't want to update
3334 stack_pointer_delta. */
3335 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3336 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3337 0, OPTAB_LIB_WIDEN);
3338 if (temp != stack_pointer_rtx)
3339 emit_move_insn (stack_pointer_rtx, temp);
3341 switch (code)
3343 case PRE_INC:
3344 case PRE_DEC:
3345 case PRE_MODIFY:
3346 temp = stack_pointer_rtx;
3347 break;
3348 case POST_INC:
3349 case POST_DEC:
3350 case POST_MODIFY:
3351 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3352 break;
3353 default:
3354 gcc_unreachable ();
3357 return replace_equiv_address (x, temp);
3360 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3361 X is known to satisfy push_operand, and MODE is known to be complex.
3362 Returns the last instruction emitted. */
3364 rtx_insn *
3365 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3367 machine_mode submode = GET_MODE_INNER (mode);
3368 bool imag_first;
3370 #ifdef PUSH_ROUNDING
3371 unsigned int submodesize = GET_MODE_SIZE (submode);
3373 /* In case we output to the stack, but the size is smaller than the
3374 machine can push exactly, we need to use move instructions. */
3375 if (PUSH_ROUNDING (submodesize) != submodesize)
3377 x = emit_move_resolve_push (mode, x);
3378 return emit_move_insn (x, y);
3380 #endif
3382 /* Note that the real part always precedes the imag part in memory
3383 regardless of machine's endianness. */
3384 switch (GET_CODE (XEXP (x, 0)))
3386 case PRE_DEC:
3387 case POST_DEC:
3388 imag_first = true;
3389 break;
3390 case PRE_INC:
3391 case POST_INC:
3392 imag_first = false;
3393 break;
3394 default:
3395 gcc_unreachable ();
3398 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3399 read_complex_part (y, imag_first));
3400 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3401 read_complex_part (y, !imag_first));
3404 /* A subroutine of emit_move_complex. Perform the move from Y to X
3405 via two moves of the parts. Returns the last instruction emitted. */
3407 rtx_insn *
3408 emit_move_complex_parts (rtx x, rtx y)
3410 /* Show the output dies here. This is necessary for SUBREGs
3411 of pseudos since we cannot track their lifetimes correctly;
3412 hard regs shouldn't appear here except as return values. */
3413 if (!reload_completed && !reload_in_progress
3414 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3415 emit_clobber (x);
3417 write_complex_part (x, read_complex_part (y, false), false);
3418 write_complex_part (x, read_complex_part (y, true), true);
3420 return get_last_insn ();
3423 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3424 MODE is known to be complex. Returns the last instruction emitted. */
3426 static rtx_insn *
3427 emit_move_complex (machine_mode mode, rtx x, rtx y)
3429 bool try_int;
3431 /* Need to take special care for pushes, to maintain proper ordering
3432 of the data, and possibly extra padding. */
3433 if (push_operand (x, mode))
3434 return emit_move_complex_push (mode, x, y);
3436 /* See if we can coerce the target into moving both values at once, except
3437 for floating point where we favor moving as parts if this is easy. */
3438 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3439 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3440 && !(REG_P (x)
3441 && HARD_REGISTER_P (x)
3442 && REG_NREGS (x) == 1)
3443 && !(REG_P (y)
3444 && HARD_REGISTER_P (y)
3445 && REG_NREGS (y) == 1))
3446 try_int = false;
3447 /* Not possible if the values are inherently not adjacent. */
3448 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3449 try_int = false;
3450 /* Is possible if both are registers (or subregs of registers). */
3451 else if (register_operand (x, mode) && register_operand (y, mode))
3452 try_int = true;
3453 /* If one of the operands is a memory, and alignment constraints
3454 are friendly enough, we may be able to do combined memory operations.
3455 We do not attempt this if Y is a constant because that combination is
3456 usually better with the by-parts thing below. */
3457 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3458 && (!STRICT_ALIGNMENT
3459 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3460 try_int = true;
3461 else
3462 try_int = false;
3464 if (try_int)
3466 rtx_insn *ret;
3468 /* For memory to memory moves, optimal behavior can be had with the
3469 existing block move logic. */
3470 if (MEM_P (x) && MEM_P (y))
3472 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3473 BLOCK_OP_NO_LIBCALL);
3474 return get_last_insn ();
3477 ret = emit_move_via_integer (mode, x, y, true);
3478 if (ret)
3479 return ret;
3482 return emit_move_complex_parts (x, y);
3485 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3486 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3488 static rtx_insn *
3489 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3491 rtx_insn *ret;
3493 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3494 if (mode != CCmode)
3496 enum insn_code code = optab_handler (mov_optab, CCmode);
3497 if (code != CODE_FOR_nothing)
3499 x = emit_move_change_mode (CCmode, mode, x, true);
3500 y = emit_move_change_mode (CCmode, mode, y, true);
3501 return emit_insn (GEN_FCN (code) (x, y));
3505 /* Otherwise, find the MODE_INT mode of the same width. */
3506 ret = emit_move_via_integer (mode, x, y, false);
3507 gcc_assert (ret != NULL);
3508 return ret;
3511 /* Return true if word I of OP lies entirely in the
3512 undefined bits of a paradoxical subreg. */
3514 static bool
3515 undefined_operand_subword_p (const_rtx op, int i)
3517 machine_mode innermode, innermostmode;
3518 int offset;
3519 if (GET_CODE (op) != SUBREG)
3520 return false;
3521 innermode = GET_MODE (op);
3522 innermostmode = GET_MODE (SUBREG_REG (op));
3523 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3524 /* The SUBREG_BYTE represents offset, as if the value were stored in
3525 memory, except for a paradoxical subreg where we define
3526 SUBREG_BYTE to be 0; undo this exception as in
3527 simplify_subreg. */
3528 if (SUBREG_BYTE (op) == 0
3529 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3531 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3532 if (WORDS_BIG_ENDIAN)
3533 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3534 if (BYTES_BIG_ENDIAN)
3535 offset += difference % UNITS_PER_WORD;
3537 if (offset >= GET_MODE_SIZE (innermostmode)
3538 || offset <= -GET_MODE_SIZE (word_mode))
3539 return true;
3540 return false;
3543 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3544 MODE is any multi-word or full-word mode that lacks a move_insn
3545 pattern. Note that you will get better code if you define such
3546 patterns, even if they must turn into multiple assembler instructions. */
3548 static rtx_insn *
3549 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3551 rtx_insn *last_insn = 0;
3552 rtx_insn *seq;
3553 rtx inner;
3554 bool need_clobber;
3555 int i;
3557 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3559 /* If X is a push on the stack, do the push now and replace
3560 X with a reference to the stack pointer. */
3561 if (push_operand (x, mode))
3562 x = emit_move_resolve_push (mode, x);
3564 /* If we are in reload, see if either operand is a MEM whose address
3565 is scheduled for replacement. */
3566 if (reload_in_progress && MEM_P (x)
3567 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3568 x = replace_equiv_address_nv (x, inner);
3569 if (reload_in_progress && MEM_P (y)
3570 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3571 y = replace_equiv_address_nv (y, inner);
3573 start_sequence ();
3575 need_clobber = false;
3576 for (i = 0;
3577 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3578 i++)
3580 rtx xpart = operand_subword (x, i, 1, mode);
3581 rtx ypart;
3583 /* Do not generate code for a move if it would come entirely
3584 from the undefined bits of a paradoxical subreg. */
3585 if (undefined_operand_subword_p (y, i))
3586 continue;
3588 ypart = operand_subword (y, i, 1, mode);
3590 /* If we can't get a part of Y, put Y into memory if it is a
3591 constant. Otherwise, force it into a register. Then we must
3592 be able to get a part of Y. */
3593 if (ypart == 0 && CONSTANT_P (y))
3595 y = use_anchored_address (force_const_mem (mode, y));
3596 ypart = operand_subword (y, i, 1, mode);
3598 else if (ypart == 0)
3599 ypart = operand_subword_force (y, i, mode);
3601 gcc_assert (xpart && ypart);
3603 need_clobber |= (GET_CODE (xpart) == SUBREG);
3605 last_insn = emit_move_insn (xpart, ypart);
3608 seq = get_insns ();
3609 end_sequence ();
3611 /* Show the output dies here. This is necessary for SUBREGs
3612 of pseudos since we cannot track their lifetimes correctly;
3613 hard regs shouldn't appear here except as return values.
3614 We never want to emit such a clobber after reload. */
3615 if (x != y
3616 && ! (reload_in_progress || reload_completed)
3617 && need_clobber != 0)
3618 emit_clobber (x);
3620 emit_insn (seq);
3622 return last_insn;
3625 /* Low level part of emit_move_insn.
3626 Called just like emit_move_insn, but assumes X and Y
3627 are basically valid. */
3629 rtx_insn *
3630 emit_move_insn_1 (rtx x, rtx y)
3632 machine_mode mode = GET_MODE (x);
3633 enum insn_code code;
3635 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3637 code = optab_handler (mov_optab, mode);
3638 if (code != CODE_FOR_nothing)
3639 return emit_insn (GEN_FCN (code) (x, y));
3641 /* Expand complex moves by moving real part and imag part. */
3642 if (COMPLEX_MODE_P (mode))
3643 return emit_move_complex (mode, x, y);
3645 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3646 || ALL_FIXED_POINT_MODE_P (mode))
3648 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3650 /* If we can't find an integer mode, use multi words. */
3651 if (result)
3652 return result;
3653 else
3654 return emit_move_multi_word (mode, x, y);
3657 if (GET_MODE_CLASS (mode) == MODE_CC)
3658 return emit_move_ccmode (mode, x, y);
3660 /* Try using a move pattern for the corresponding integer mode. This is
3661 only safe when simplify_subreg can convert MODE constants into integer
3662 constants. At present, it can only do this reliably if the value
3663 fits within a HOST_WIDE_INT. */
3664 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3666 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3668 if (ret)
3670 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3671 return ret;
3675 return emit_move_multi_word (mode, x, y);
3678 /* Generate code to copy Y into X.
3679 Both Y and X must have the same mode, except that
3680 Y can be a constant with VOIDmode.
3681 This mode cannot be BLKmode; use emit_block_move for that.
3683 Return the last instruction emitted. */
3685 rtx_insn *
3686 emit_move_insn (rtx x, rtx y)
3688 machine_mode mode = GET_MODE (x);
3689 rtx y_cst = NULL_RTX;
3690 rtx_insn *last_insn;
3691 rtx set;
3693 gcc_assert (mode != BLKmode
3694 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3696 if (CONSTANT_P (y))
3698 if (optimize
3699 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3700 && (last_insn = compress_float_constant (x, y)))
3701 return last_insn;
3703 y_cst = y;
3705 if (!targetm.legitimate_constant_p (mode, y))
3707 y = force_const_mem (mode, y);
3709 /* If the target's cannot_force_const_mem prevented the spill,
3710 assume that the target's move expanders will also take care
3711 of the non-legitimate constant. */
3712 if (!y)
3713 y = y_cst;
3714 else
3715 y = use_anchored_address (y);
3719 /* If X or Y are memory references, verify that their addresses are valid
3720 for the machine. */
3721 if (MEM_P (x)
3722 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3723 MEM_ADDR_SPACE (x))
3724 && ! push_operand (x, GET_MODE (x))))
3725 x = validize_mem (x);
3727 if (MEM_P (y)
3728 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3729 MEM_ADDR_SPACE (y)))
3730 y = validize_mem (y);
3732 gcc_assert (mode != BLKmode);
3734 last_insn = emit_move_insn_1 (x, y);
3736 if (y_cst && REG_P (x)
3737 && (set = single_set (last_insn)) != NULL_RTX
3738 && SET_DEST (set) == x
3739 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3740 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3742 return last_insn;
3745 /* Generate the body of an instruction to copy Y into X.
3746 It may be a list of insns, if one insn isn't enough. */
3748 rtx_insn *
3749 gen_move_insn (rtx x, rtx y)
3751 rtx_insn *seq;
3753 start_sequence ();
3754 emit_move_insn_1 (x, y);
3755 seq = get_insns ();
3756 end_sequence ();
3757 return seq;
3760 /* If Y is representable exactly in a narrower mode, and the target can
3761 perform the extension directly from constant or memory, then emit the
3762 move as an extension. */
3764 static rtx_insn *
3765 compress_float_constant (rtx x, rtx y)
3767 machine_mode dstmode = GET_MODE (x);
3768 machine_mode orig_srcmode = GET_MODE (y);
3769 machine_mode srcmode;
3770 const REAL_VALUE_TYPE *r;
3771 int oldcost, newcost;
3772 bool speed = optimize_insn_for_speed_p ();
3774 r = CONST_DOUBLE_REAL_VALUE (y);
3776 if (targetm.legitimate_constant_p (dstmode, y))
3777 oldcost = set_src_cost (y, orig_srcmode, speed);
3778 else
3779 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3781 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3782 srcmode != orig_srcmode;
3783 srcmode = GET_MODE_WIDER_MODE (srcmode))
3785 enum insn_code ic;
3786 rtx trunc_y;
3787 rtx_insn *last_insn;
3789 /* Skip if the target can't extend this way. */
3790 ic = can_extend_p (dstmode, srcmode, 0);
3791 if (ic == CODE_FOR_nothing)
3792 continue;
3794 /* Skip if the narrowed value isn't exact. */
3795 if (! exact_real_truncate (srcmode, r))
3796 continue;
3798 trunc_y = const_double_from_real_value (*r, srcmode);
3800 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3802 /* Skip if the target needs extra instructions to perform
3803 the extension. */
3804 if (!insn_operand_matches (ic, 1, trunc_y))
3805 continue;
3806 /* This is valid, but may not be cheaper than the original. */
3807 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3808 dstmode, speed);
3809 if (oldcost < newcost)
3810 continue;
3812 else if (float_extend_from_mem[dstmode][srcmode])
3814 trunc_y = force_const_mem (srcmode, trunc_y);
3815 /* This is valid, but may not be cheaper than the original. */
3816 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3817 dstmode, speed);
3818 if (oldcost < newcost)
3819 continue;
3820 trunc_y = validize_mem (trunc_y);
3822 else
3823 continue;
3825 /* For CSE's benefit, force the compressed constant pool entry
3826 into a new pseudo. This constant may be used in different modes,
3827 and if not, combine will put things back together for us. */
3828 trunc_y = force_reg (srcmode, trunc_y);
3830 /* If x is a hard register, perform the extension into a pseudo,
3831 so that e.g. stack realignment code is aware of it. */
3832 rtx target = x;
3833 if (REG_P (x) && HARD_REGISTER_P (x))
3834 target = gen_reg_rtx (dstmode);
3836 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3837 last_insn = get_last_insn ();
3839 if (REG_P (target))
3840 set_unique_reg_note (last_insn, REG_EQUAL, y);
3842 if (target != x)
3843 return emit_move_insn (x, target);
3844 return last_insn;
3847 return NULL;
3850 /* Pushing data onto the stack. */
3852 /* Push a block of length SIZE (perhaps variable)
3853 and return an rtx to address the beginning of the block.
3854 The value may be virtual_outgoing_args_rtx.
3856 EXTRA is the number of bytes of padding to push in addition to SIZE.
3857 BELOW nonzero means this padding comes at low addresses;
3858 otherwise, the padding comes at high addresses. */
3861 push_block (rtx size, int extra, int below)
3863 rtx temp;
3865 size = convert_modes (Pmode, ptr_mode, size, 1);
3866 if (CONSTANT_P (size))
3867 anti_adjust_stack (plus_constant (Pmode, size, extra));
3868 else if (REG_P (size) && extra == 0)
3869 anti_adjust_stack (size);
3870 else
3872 temp = copy_to_mode_reg (Pmode, size);
3873 if (extra != 0)
3874 temp = expand_binop (Pmode, add_optab, temp,
3875 gen_int_mode (extra, Pmode),
3876 temp, 0, OPTAB_LIB_WIDEN);
3877 anti_adjust_stack (temp);
3880 if (STACK_GROWS_DOWNWARD)
3882 temp = virtual_outgoing_args_rtx;
3883 if (extra != 0 && below)
3884 temp = plus_constant (Pmode, temp, extra);
3886 else
3888 if (CONST_INT_P (size))
3889 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3890 -INTVAL (size) - (below ? 0 : extra));
3891 else if (extra != 0 && !below)
3892 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3893 negate_rtx (Pmode, plus_constant (Pmode, size,
3894 extra)));
3895 else
3896 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3897 negate_rtx (Pmode, size));
3900 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3903 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3905 static rtx
3906 mem_autoinc_base (rtx mem)
3908 if (MEM_P (mem))
3910 rtx addr = XEXP (mem, 0);
3911 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3912 return XEXP (addr, 0);
3914 return NULL;
3917 /* A utility routine used here, in reload, and in try_split. The insns
3918 after PREV up to and including LAST are known to adjust the stack,
3919 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3920 placing notes as appropriate. PREV may be NULL, indicating the
3921 entire insn sequence prior to LAST should be scanned.
3923 The set of allowed stack pointer modifications is small:
3924 (1) One or more auto-inc style memory references (aka pushes),
3925 (2) One or more addition/subtraction with the SP as destination,
3926 (3) A single move insn with the SP as destination,
3927 (4) A call_pop insn,
3928 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3930 Insns in the sequence that do not modify the SP are ignored,
3931 except for noreturn calls.
3933 The return value is the amount of adjustment that can be trivially
3934 verified, via immediate operand or auto-inc. If the adjustment
3935 cannot be trivially extracted, the return value is INT_MIN. */
3937 HOST_WIDE_INT
3938 find_args_size_adjust (rtx_insn *insn)
3940 rtx dest, set, pat;
3941 int i;
3943 pat = PATTERN (insn);
3944 set = NULL;
3946 /* Look for a call_pop pattern. */
3947 if (CALL_P (insn))
3949 /* We have to allow non-call_pop patterns for the case
3950 of emit_single_push_insn of a TLS address. */
3951 if (GET_CODE (pat) != PARALLEL)
3952 return 0;
3954 /* All call_pop have a stack pointer adjust in the parallel.
3955 The call itself is always first, and the stack adjust is
3956 usually last, so search from the end. */
3957 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3959 set = XVECEXP (pat, 0, i);
3960 if (GET_CODE (set) != SET)
3961 continue;
3962 dest = SET_DEST (set);
3963 if (dest == stack_pointer_rtx)
3964 break;
3966 /* We'd better have found the stack pointer adjust. */
3967 if (i == 0)
3968 return 0;
3969 /* Fall through to process the extracted SET and DEST
3970 as if it was a standalone insn. */
3972 else if (GET_CODE (pat) == SET)
3973 set = pat;
3974 else if ((set = single_set (insn)) != NULL)
3976 else if (GET_CODE (pat) == PARALLEL)
3978 /* ??? Some older ports use a parallel with a stack adjust
3979 and a store for a PUSH_ROUNDING pattern, rather than a
3980 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3981 /* ??? See h8300 and m68k, pushqi1. */
3982 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3984 set = XVECEXP (pat, 0, i);
3985 if (GET_CODE (set) != SET)
3986 continue;
3987 dest = SET_DEST (set);
3988 if (dest == stack_pointer_rtx)
3989 break;
3991 /* We do not expect an auto-inc of the sp in the parallel. */
3992 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3993 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3994 != stack_pointer_rtx);
3996 if (i < 0)
3997 return 0;
3999 else
4000 return 0;
4002 dest = SET_DEST (set);
4004 /* Look for direct modifications of the stack pointer. */
4005 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4007 /* Look for a trivial adjustment, otherwise assume nothing. */
4008 /* Note that the SPU restore_stack_block pattern refers to
4009 the stack pointer in V4SImode. Consider that non-trivial. */
4010 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4011 && GET_CODE (SET_SRC (set)) == PLUS
4012 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4013 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4014 return INTVAL (XEXP (SET_SRC (set), 1));
4015 /* ??? Reload can generate no-op moves, which will be cleaned
4016 up later. Recognize it and continue searching. */
4017 else if (rtx_equal_p (dest, SET_SRC (set)))
4018 return 0;
4019 else
4020 return HOST_WIDE_INT_MIN;
4022 else
4024 rtx mem, addr;
4026 /* Otherwise only think about autoinc patterns. */
4027 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4029 mem = dest;
4030 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4031 != stack_pointer_rtx);
4033 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4034 mem = SET_SRC (set);
4035 else
4036 return 0;
4038 addr = XEXP (mem, 0);
4039 switch (GET_CODE (addr))
4041 case PRE_INC:
4042 case POST_INC:
4043 return GET_MODE_SIZE (GET_MODE (mem));
4044 case PRE_DEC:
4045 case POST_DEC:
4046 return -GET_MODE_SIZE (GET_MODE (mem));
4047 case PRE_MODIFY:
4048 case POST_MODIFY:
4049 addr = XEXP (addr, 1);
4050 gcc_assert (GET_CODE (addr) == PLUS);
4051 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4052 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4053 return INTVAL (XEXP (addr, 1));
4054 default:
4055 gcc_unreachable ();
4061 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4063 int args_size = end_args_size;
4064 bool saw_unknown = false;
4065 rtx_insn *insn;
4067 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4069 HOST_WIDE_INT this_delta;
4071 if (!NONDEBUG_INSN_P (insn))
4072 continue;
4074 this_delta = find_args_size_adjust (insn);
4075 if (this_delta == 0)
4077 if (!CALL_P (insn)
4078 || ACCUMULATE_OUTGOING_ARGS
4079 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4080 continue;
4083 gcc_assert (!saw_unknown);
4084 if (this_delta == HOST_WIDE_INT_MIN)
4085 saw_unknown = true;
4087 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4088 if (STACK_GROWS_DOWNWARD)
4089 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4091 args_size -= this_delta;
4094 return saw_unknown ? INT_MIN : args_size;
4097 #ifdef PUSH_ROUNDING
4098 /* Emit single push insn. */
4100 static void
4101 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4103 rtx dest_addr;
4104 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4105 rtx dest;
4106 enum insn_code icode;
4108 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4109 /* If there is push pattern, use it. Otherwise try old way of throwing
4110 MEM representing push operation to move expander. */
4111 icode = optab_handler (push_optab, mode);
4112 if (icode != CODE_FOR_nothing)
4114 struct expand_operand ops[1];
4116 create_input_operand (&ops[0], x, mode);
4117 if (maybe_expand_insn (icode, 1, ops))
4118 return;
4120 if (GET_MODE_SIZE (mode) == rounded_size)
4121 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4122 /* If we are to pad downward, adjust the stack pointer first and
4123 then store X into the stack location using an offset. This is
4124 because emit_move_insn does not know how to pad; it does not have
4125 access to type. */
4126 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4128 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4129 HOST_WIDE_INT offset;
4131 emit_move_insn (stack_pointer_rtx,
4132 expand_binop (Pmode,
4133 STACK_GROWS_DOWNWARD ? sub_optab
4134 : add_optab,
4135 stack_pointer_rtx,
4136 gen_int_mode (rounded_size, Pmode),
4137 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4139 offset = (HOST_WIDE_INT) padding_size;
4140 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4141 /* We have already decremented the stack pointer, so get the
4142 previous value. */
4143 offset += (HOST_WIDE_INT) rounded_size;
4145 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4146 /* We have already incremented the stack pointer, so get the
4147 previous value. */
4148 offset -= (HOST_WIDE_INT) rounded_size;
4150 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4151 gen_int_mode (offset, Pmode));
4153 else
4155 if (STACK_GROWS_DOWNWARD)
4156 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4157 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4158 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4159 Pmode));
4160 else
4161 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4162 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4163 gen_int_mode (rounded_size, Pmode));
4165 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4168 dest = gen_rtx_MEM (mode, dest_addr);
4170 if (type != 0)
4172 set_mem_attributes (dest, type, 1);
4174 if (cfun->tail_call_marked)
4175 /* Function incoming arguments may overlap with sibling call
4176 outgoing arguments and we cannot allow reordering of reads
4177 from function arguments with stores to outgoing arguments
4178 of sibling calls. */
4179 set_mem_alias_set (dest, 0);
4181 emit_move_insn (dest, x);
4184 /* Emit and annotate a single push insn. */
4186 static void
4187 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4189 int delta, old_delta = stack_pointer_delta;
4190 rtx_insn *prev = get_last_insn ();
4191 rtx_insn *last;
4193 emit_single_push_insn_1 (mode, x, type);
4195 last = get_last_insn ();
4197 /* Notice the common case where we emitted exactly one insn. */
4198 if (PREV_INSN (last) == prev)
4200 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4201 return;
4204 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4205 gcc_assert (delta == INT_MIN || delta == old_delta);
4207 #endif
4209 /* If reading SIZE bytes from X will end up reading from
4210 Y return the number of bytes that overlap. Return -1
4211 if there is no overlap or -2 if we can't determine
4212 (for example when X and Y have different base registers). */
4214 static int
4215 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4217 rtx tmp = plus_constant (Pmode, x, size);
4218 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4220 if (!CONST_INT_P (sub))
4221 return -2;
4223 HOST_WIDE_INT val = INTVAL (sub);
4225 return IN_RANGE (val, 1, size) ? val : -1;
4228 /* Generate code to push X onto the stack, assuming it has mode MODE and
4229 type TYPE.
4230 MODE is redundant except when X is a CONST_INT (since they don't
4231 carry mode info).
4232 SIZE is an rtx for the size of data to be copied (in bytes),
4233 needed only if X is BLKmode.
4234 Return true if successful. May return false if asked to push a
4235 partial argument during a sibcall optimization (as specified by
4236 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4237 to not overlap.
4239 ALIGN (in bits) is maximum alignment we can assume.
4241 If PARTIAL and REG are both nonzero, then copy that many of the first
4242 bytes of X into registers starting with REG, and push the rest of X.
4243 The amount of space pushed is decreased by PARTIAL bytes.
4244 REG must be a hard register in this case.
4245 If REG is zero but PARTIAL is not, take any all others actions for an
4246 argument partially in registers, but do not actually load any
4247 registers.
4249 EXTRA is the amount in bytes of extra space to leave next to this arg.
4250 This is ignored if an argument block has already been allocated.
4252 On a machine that lacks real push insns, ARGS_ADDR is the address of
4253 the bottom of the argument block for this call. We use indexing off there
4254 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4255 argument block has not been preallocated.
4257 ARGS_SO_FAR is the size of args previously pushed for this call.
4259 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4260 for arguments passed in registers. If nonzero, it will be the number
4261 of bytes required. */
4263 bool
4264 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4265 unsigned int align, int partial, rtx reg, int extra,
4266 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4267 rtx alignment_pad, bool sibcall_p)
4269 rtx xinner;
4270 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4272 /* Decide where to pad the argument: `downward' for below,
4273 `upward' for above, or `none' for don't pad it.
4274 Default is below for small data on big-endian machines; else above. */
4275 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4277 /* Invert direction if stack is post-decrement.
4278 FIXME: why? */
4279 if (STACK_PUSH_CODE == POST_DEC)
4280 if (where_pad != none)
4281 where_pad = (where_pad == downward ? upward : downward);
4283 xinner = x;
4285 int nregs = partial / UNITS_PER_WORD;
4286 rtx *tmp_regs = NULL;
4287 int overlapping = 0;
4289 if (mode == BLKmode
4290 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4292 /* Copy a block into the stack, entirely or partially. */
4294 rtx temp;
4295 int used;
4296 int offset;
4297 int skip;
4299 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4300 used = partial - offset;
4302 if (mode != BLKmode)
4304 /* A value is to be stored in an insufficiently aligned
4305 stack slot; copy via a suitably aligned slot if
4306 necessary. */
4307 size = GEN_INT (GET_MODE_SIZE (mode));
4308 if (!MEM_P (xinner))
4310 temp = assign_temp (type, 1, 1);
4311 emit_move_insn (temp, xinner);
4312 xinner = temp;
4316 gcc_assert (size);
4318 /* USED is now the # of bytes we need not copy to the stack
4319 because registers will take care of them. */
4321 if (partial != 0)
4322 xinner = adjust_address (xinner, BLKmode, used);
4324 /* If the partial register-part of the arg counts in its stack size,
4325 skip the part of stack space corresponding to the registers.
4326 Otherwise, start copying to the beginning of the stack space,
4327 by setting SKIP to 0. */
4328 skip = (reg_parm_stack_space == 0) ? 0 : used;
4330 #ifdef PUSH_ROUNDING
4331 /* Do it with several push insns if that doesn't take lots of insns
4332 and if there is no difficulty with push insns that skip bytes
4333 on the stack for alignment purposes. */
4334 if (args_addr == 0
4335 && PUSH_ARGS
4336 && CONST_INT_P (size)
4337 && skip == 0
4338 && MEM_ALIGN (xinner) >= align
4339 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4340 /* Here we avoid the case of a structure whose weak alignment
4341 forces many pushes of a small amount of data,
4342 and such small pushes do rounding that causes trouble. */
4343 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4344 || align >= BIGGEST_ALIGNMENT
4345 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4346 == (align / BITS_PER_UNIT)))
4347 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4349 /* Push padding now if padding above and stack grows down,
4350 or if padding below and stack grows up.
4351 But if space already allocated, this has already been done. */
4352 if (extra && args_addr == 0
4353 && where_pad != none && where_pad != stack_direction)
4354 anti_adjust_stack (GEN_INT (extra));
4356 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4358 else
4359 #endif /* PUSH_ROUNDING */
4361 rtx target;
4363 /* Otherwise make space on the stack and copy the data
4364 to the address of that space. */
4366 /* Deduct words put into registers from the size we must copy. */
4367 if (partial != 0)
4369 if (CONST_INT_P (size))
4370 size = GEN_INT (INTVAL (size) - used);
4371 else
4372 size = expand_binop (GET_MODE (size), sub_optab, size,
4373 gen_int_mode (used, GET_MODE (size)),
4374 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4377 /* Get the address of the stack space.
4378 In this case, we do not deal with EXTRA separately.
4379 A single stack adjust will do. */
4380 if (! args_addr)
4382 temp = push_block (size, extra, where_pad == downward);
4383 extra = 0;
4385 else if (CONST_INT_P (args_so_far))
4386 temp = memory_address (BLKmode,
4387 plus_constant (Pmode, args_addr,
4388 skip + INTVAL (args_so_far)));
4389 else
4390 temp = memory_address (BLKmode,
4391 plus_constant (Pmode,
4392 gen_rtx_PLUS (Pmode,
4393 args_addr,
4394 args_so_far),
4395 skip));
4397 if (!ACCUMULATE_OUTGOING_ARGS)
4399 /* If the source is referenced relative to the stack pointer,
4400 copy it to another register to stabilize it. We do not need
4401 to do this if we know that we won't be changing sp. */
4403 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4404 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4405 temp = copy_to_reg (temp);
4408 target = gen_rtx_MEM (BLKmode, temp);
4410 /* We do *not* set_mem_attributes here, because incoming arguments
4411 may overlap with sibling call outgoing arguments and we cannot
4412 allow reordering of reads from function arguments with stores
4413 to outgoing arguments of sibling calls. We do, however, want
4414 to record the alignment of the stack slot. */
4415 /* ALIGN may well be better aligned than TYPE, e.g. due to
4416 PARM_BOUNDARY. Assume the caller isn't lying. */
4417 set_mem_align (target, align);
4419 /* If part should go in registers and pushing to that part would
4420 overwrite some of the values that need to go into regs, load the
4421 overlapping values into temporary pseudos to be moved into the hard
4422 regs at the end after the stack pushing has completed.
4423 We cannot load them directly into the hard regs here because
4424 they can be clobbered by the block move expansions.
4425 See PR 65358. */
4427 if (partial > 0 && reg != 0 && mode == BLKmode
4428 && GET_CODE (reg) != PARALLEL)
4430 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4431 if (overlapping > 0)
4433 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4434 overlapping /= UNITS_PER_WORD;
4436 tmp_regs = XALLOCAVEC (rtx, overlapping);
4438 for (int i = 0; i < overlapping; i++)
4439 tmp_regs[i] = gen_reg_rtx (word_mode);
4441 for (int i = 0; i < overlapping; i++)
4442 emit_move_insn (tmp_regs[i],
4443 operand_subword_force (target, i, mode));
4445 else if (overlapping == -1)
4446 overlapping = 0;
4447 /* Could not determine whether there is overlap.
4448 Fail the sibcall. */
4449 else
4451 overlapping = 0;
4452 if (sibcall_p)
4453 return false;
4456 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4459 else if (partial > 0)
4461 /* Scalar partly in registers. */
4463 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4464 int i;
4465 int not_stack;
4466 /* # bytes of start of argument
4467 that we must make space for but need not store. */
4468 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4469 int args_offset = INTVAL (args_so_far);
4470 int skip;
4472 /* Push padding now if padding above and stack grows down,
4473 or if padding below and stack grows up.
4474 But if space already allocated, this has already been done. */
4475 if (extra && args_addr == 0
4476 && where_pad != none && where_pad != stack_direction)
4477 anti_adjust_stack (GEN_INT (extra));
4479 /* If we make space by pushing it, we might as well push
4480 the real data. Otherwise, we can leave OFFSET nonzero
4481 and leave the space uninitialized. */
4482 if (args_addr == 0)
4483 offset = 0;
4485 /* Now NOT_STACK gets the number of words that we don't need to
4486 allocate on the stack. Convert OFFSET to words too. */
4487 not_stack = (partial - offset) / UNITS_PER_WORD;
4488 offset /= UNITS_PER_WORD;
4490 /* If the partial register-part of the arg counts in its stack size,
4491 skip the part of stack space corresponding to the registers.
4492 Otherwise, start copying to the beginning of the stack space,
4493 by setting SKIP to 0. */
4494 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4496 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4497 x = validize_mem (force_const_mem (mode, x));
4499 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4500 SUBREGs of such registers are not allowed. */
4501 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4502 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4503 x = copy_to_reg (x);
4505 /* Loop over all the words allocated on the stack for this arg. */
4506 /* We can do it by words, because any scalar bigger than a word
4507 has a size a multiple of a word. */
4508 for (i = size - 1; i >= not_stack; i--)
4509 if (i >= not_stack + offset)
4510 if (!emit_push_insn (operand_subword_force (x, i, mode),
4511 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4512 0, args_addr,
4513 GEN_INT (args_offset + ((i - not_stack + skip)
4514 * UNITS_PER_WORD)),
4515 reg_parm_stack_space, alignment_pad, sibcall_p))
4516 return false;
4518 else
4520 rtx addr;
4521 rtx dest;
4523 /* Push padding now if padding above and stack grows down,
4524 or if padding below and stack grows up.
4525 But if space already allocated, this has already been done. */
4526 if (extra && args_addr == 0
4527 && where_pad != none && where_pad != stack_direction)
4528 anti_adjust_stack (GEN_INT (extra));
4530 #ifdef PUSH_ROUNDING
4531 if (args_addr == 0 && PUSH_ARGS)
4532 emit_single_push_insn (mode, x, type);
4533 else
4534 #endif
4536 if (CONST_INT_P (args_so_far))
4537 addr
4538 = memory_address (mode,
4539 plus_constant (Pmode, args_addr,
4540 INTVAL (args_so_far)));
4541 else
4542 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4543 args_so_far));
4544 dest = gen_rtx_MEM (mode, addr);
4546 /* We do *not* set_mem_attributes here, because incoming arguments
4547 may overlap with sibling call outgoing arguments and we cannot
4548 allow reordering of reads from function arguments with stores
4549 to outgoing arguments of sibling calls. We do, however, want
4550 to record the alignment of the stack slot. */
4551 /* ALIGN may well be better aligned than TYPE, e.g. due to
4552 PARM_BOUNDARY. Assume the caller isn't lying. */
4553 set_mem_align (dest, align);
4555 emit_move_insn (dest, x);
4559 /* Move the partial arguments into the registers and any overlapping
4560 values that we moved into the pseudos in tmp_regs. */
4561 if (partial > 0 && reg != 0)
4563 /* Handle calls that pass values in multiple non-contiguous locations.
4564 The Irix 6 ABI has examples of this. */
4565 if (GET_CODE (reg) == PARALLEL)
4566 emit_group_load (reg, x, type, -1);
4567 else
4569 gcc_assert (partial % UNITS_PER_WORD == 0);
4570 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4572 for (int i = 0; i < overlapping; i++)
4573 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4574 + nregs - overlapping + i),
4575 tmp_regs[i]);
4580 if (extra && args_addr == 0 && where_pad == stack_direction)
4581 anti_adjust_stack (GEN_INT (extra));
4583 if (alignment_pad && args_addr == 0)
4584 anti_adjust_stack (alignment_pad);
4586 return true;
4589 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4590 operations. */
4592 static rtx
4593 get_subtarget (rtx x)
4595 return (optimize
4596 || x == 0
4597 /* Only registers can be subtargets. */
4598 || !REG_P (x)
4599 /* Don't use hard regs to avoid extending their life. */
4600 || REGNO (x) < FIRST_PSEUDO_REGISTER
4601 ? 0 : x);
4604 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4605 FIELD is a bitfield. Returns true if the optimization was successful,
4606 and there's nothing else to do. */
4608 static bool
4609 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4610 unsigned HOST_WIDE_INT bitpos,
4611 unsigned HOST_WIDE_INT bitregion_start,
4612 unsigned HOST_WIDE_INT bitregion_end,
4613 machine_mode mode1, rtx str_rtx,
4614 tree to, tree src, bool reverse)
4616 machine_mode str_mode = GET_MODE (str_rtx);
4617 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4618 tree op0, op1;
4619 rtx value, result;
4620 optab binop;
4621 gimple *srcstmt;
4622 enum tree_code code;
4624 if (mode1 != VOIDmode
4625 || bitsize >= BITS_PER_WORD
4626 || str_bitsize > BITS_PER_WORD
4627 || TREE_SIDE_EFFECTS (to)
4628 || TREE_THIS_VOLATILE (to))
4629 return false;
4631 STRIP_NOPS (src);
4632 if (TREE_CODE (src) != SSA_NAME)
4633 return false;
4634 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4635 return false;
4637 srcstmt = get_gimple_for_ssa_name (src);
4638 if (!srcstmt
4639 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4640 return false;
4642 code = gimple_assign_rhs_code (srcstmt);
4644 op0 = gimple_assign_rhs1 (srcstmt);
4646 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4647 to find its initialization. Hopefully the initialization will
4648 be from a bitfield load. */
4649 if (TREE_CODE (op0) == SSA_NAME)
4651 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4653 /* We want to eventually have OP0 be the same as TO, which
4654 should be a bitfield. */
4655 if (!op0stmt
4656 || !is_gimple_assign (op0stmt)
4657 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4658 return false;
4659 op0 = gimple_assign_rhs1 (op0stmt);
4662 op1 = gimple_assign_rhs2 (srcstmt);
4664 if (!operand_equal_p (to, op0, 0))
4665 return false;
4667 if (MEM_P (str_rtx))
4669 unsigned HOST_WIDE_INT offset1;
4671 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4672 str_mode = word_mode;
4673 str_mode = get_best_mode (bitsize, bitpos,
4674 bitregion_start, bitregion_end,
4675 MEM_ALIGN (str_rtx), str_mode, 0);
4676 if (str_mode == VOIDmode)
4677 return false;
4678 str_bitsize = GET_MODE_BITSIZE (str_mode);
4680 offset1 = bitpos;
4681 bitpos %= str_bitsize;
4682 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4683 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4685 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4686 return false;
4687 else
4688 gcc_assert (!reverse);
4690 /* If the bit field covers the whole REG/MEM, store_field
4691 will likely generate better code. */
4692 if (bitsize >= str_bitsize)
4693 return false;
4695 /* We can't handle fields split across multiple entities. */
4696 if (bitpos + bitsize > str_bitsize)
4697 return false;
4699 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4700 bitpos = str_bitsize - bitpos - bitsize;
4702 switch (code)
4704 case PLUS_EXPR:
4705 case MINUS_EXPR:
4706 /* For now, just optimize the case of the topmost bitfield
4707 where we don't need to do any masking and also
4708 1 bit bitfields where xor can be used.
4709 We might win by one instruction for the other bitfields
4710 too if insv/extv instructions aren't used, so that
4711 can be added later. */
4712 if ((reverse || bitpos + bitsize != str_bitsize)
4713 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4714 break;
4716 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4717 value = convert_modes (str_mode,
4718 TYPE_MODE (TREE_TYPE (op1)), value,
4719 TYPE_UNSIGNED (TREE_TYPE (op1)));
4721 /* We may be accessing data outside the field, which means
4722 we can alias adjacent data. */
4723 if (MEM_P (str_rtx))
4725 str_rtx = shallow_copy_rtx (str_rtx);
4726 set_mem_alias_set (str_rtx, 0);
4727 set_mem_expr (str_rtx, 0);
4730 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4732 value = expand_and (str_mode, value, const1_rtx, NULL);
4733 binop = xor_optab;
4735 else
4736 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4738 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4739 if (reverse)
4740 value = flip_storage_order (str_mode, value);
4741 result = expand_binop (str_mode, binop, str_rtx,
4742 value, str_rtx, 1, OPTAB_WIDEN);
4743 if (result != str_rtx)
4744 emit_move_insn (str_rtx, result);
4745 return true;
4747 case BIT_IOR_EXPR:
4748 case BIT_XOR_EXPR:
4749 if (TREE_CODE (op1) != INTEGER_CST)
4750 break;
4751 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4752 value = convert_modes (str_mode,
4753 TYPE_MODE (TREE_TYPE (op1)), value,
4754 TYPE_UNSIGNED (TREE_TYPE (op1)));
4756 /* We may be accessing data outside the field, which means
4757 we can alias adjacent data. */
4758 if (MEM_P (str_rtx))
4760 str_rtx = shallow_copy_rtx (str_rtx);
4761 set_mem_alias_set (str_rtx, 0);
4762 set_mem_expr (str_rtx, 0);
4765 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4766 if (bitpos + bitsize != str_bitsize)
4768 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4769 str_mode);
4770 value = expand_and (str_mode, value, mask, NULL_RTX);
4772 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4773 if (reverse)
4774 value = flip_storage_order (str_mode, value);
4775 result = expand_binop (str_mode, binop, str_rtx,
4776 value, str_rtx, 1, OPTAB_WIDEN);
4777 if (result != str_rtx)
4778 emit_move_insn (str_rtx, result);
4779 return true;
4781 default:
4782 break;
4785 return false;
4788 /* In the C++ memory model, consecutive bit fields in a structure are
4789 considered one memory location.
4791 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4792 returns the bit range of consecutive bits in which this COMPONENT_REF
4793 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4794 and *OFFSET may be adjusted in the process.
4796 If the access does not need to be restricted, 0 is returned in both
4797 *BITSTART and *BITEND. */
4799 void
4800 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4801 unsigned HOST_WIDE_INT *bitend,
4802 tree exp,
4803 HOST_WIDE_INT *bitpos,
4804 tree *offset)
4806 HOST_WIDE_INT bitoffset;
4807 tree field, repr;
4809 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4811 field = TREE_OPERAND (exp, 1);
4812 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4813 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4814 need to limit the range we can access. */
4815 if (!repr)
4817 *bitstart = *bitend = 0;
4818 return;
4821 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4822 part of a larger bit field, then the representative does not serve any
4823 useful purpose. This can occur in Ada. */
4824 if (handled_component_p (TREE_OPERAND (exp, 0)))
4826 machine_mode rmode;
4827 HOST_WIDE_INT rbitsize, rbitpos;
4828 tree roffset;
4829 int unsignedp, reversep, volatilep = 0;
4830 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4831 &roffset, &rmode, &unsignedp, &reversep,
4832 &volatilep);
4833 if ((rbitpos % BITS_PER_UNIT) != 0)
4835 *bitstart = *bitend = 0;
4836 return;
4840 /* Compute the adjustment to bitpos from the offset of the field
4841 relative to the representative. DECL_FIELD_OFFSET of field and
4842 repr are the same by construction if they are not constants,
4843 see finish_bitfield_layout. */
4844 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4845 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4846 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4847 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4848 else
4849 bitoffset = 0;
4850 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4851 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4853 /* If the adjustment is larger than bitpos, we would have a negative bit
4854 position for the lower bound and this may wreak havoc later. Adjust
4855 offset and bitpos to make the lower bound non-negative in that case. */
4856 if (bitoffset > *bitpos)
4858 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4859 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4861 *bitpos += adjust;
4862 if (*offset == NULL_TREE)
4863 *offset = size_int (-adjust / BITS_PER_UNIT);
4864 else
4865 *offset
4866 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4867 *bitstart = 0;
4869 else
4870 *bitstart = *bitpos - bitoffset;
4872 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4875 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4876 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4877 DECL_RTL was not set yet, return NORTL. */
4879 static inline bool
4880 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4882 if (TREE_CODE (addr) != ADDR_EXPR)
4883 return false;
4885 tree base = TREE_OPERAND (addr, 0);
4887 if (!DECL_P (base)
4888 || TREE_ADDRESSABLE (base)
4889 || DECL_MODE (base) == BLKmode)
4890 return false;
4892 if (!DECL_RTL_SET_P (base))
4893 return nortl;
4895 return (!MEM_P (DECL_RTL (base)));
4898 /* Returns true if the MEM_REF REF refers to an object that does not
4899 reside in memory and has non-BLKmode. */
4901 static inline bool
4902 mem_ref_refers_to_non_mem_p (tree ref)
4904 tree base = TREE_OPERAND (ref, 0);
4905 return addr_expr_of_non_mem_decl_p_1 (base, false);
4908 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4909 is true, try generating a nontemporal store. */
4911 void
4912 expand_assignment (tree to, tree from, bool nontemporal)
4914 rtx to_rtx = 0;
4915 rtx result;
4916 machine_mode mode;
4917 unsigned int align;
4918 enum insn_code icode;
4920 /* Don't crash if the lhs of the assignment was erroneous. */
4921 if (TREE_CODE (to) == ERROR_MARK)
4923 expand_normal (from);
4924 return;
4927 /* Optimize away no-op moves without side-effects. */
4928 if (operand_equal_p (to, from, 0))
4929 return;
4931 /* Handle misaligned stores. */
4932 mode = TYPE_MODE (TREE_TYPE (to));
4933 if ((TREE_CODE (to) == MEM_REF
4934 || TREE_CODE (to) == TARGET_MEM_REF)
4935 && mode != BLKmode
4936 && !mem_ref_refers_to_non_mem_p (to)
4937 && ((align = get_object_alignment (to))
4938 < GET_MODE_ALIGNMENT (mode))
4939 && (((icode = optab_handler (movmisalign_optab, mode))
4940 != CODE_FOR_nothing)
4941 || SLOW_UNALIGNED_ACCESS (mode, align)))
4943 rtx reg, mem;
4945 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4946 reg = force_not_mem (reg);
4947 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4948 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4949 reg = flip_storage_order (mode, reg);
4951 if (icode != CODE_FOR_nothing)
4953 struct expand_operand ops[2];
4955 create_fixed_operand (&ops[0], mem);
4956 create_input_operand (&ops[1], reg, mode);
4957 /* The movmisalign<mode> pattern cannot fail, else the assignment
4958 would silently be omitted. */
4959 expand_insn (icode, 2, ops);
4961 else
4962 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4963 false);
4964 return;
4967 /* Assignment of a structure component needs special treatment
4968 if the structure component's rtx is not simply a MEM.
4969 Assignment of an array element at a constant index, and assignment of
4970 an array element in an unaligned packed structure field, has the same
4971 problem. Same for (partially) storing into a non-memory object. */
4972 if (handled_component_p (to)
4973 || (TREE_CODE (to) == MEM_REF
4974 && (REF_REVERSE_STORAGE_ORDER (to)
4975 || mem_ref_refers_to_non_mem_p (to)))
4976 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4978 machine_mode mode1;
4979 HOST_WIDE_INT bitsize, bitpos;
4980 unsigned HOST_WIDE_INT bitregion_start = 0;
4981 unsigned HOST_WIDE_INT bitregion_end = 0;
4982 tree offset;
4983 int unsignedp, reversep, volatilep = 0;
4984 tree tem;
4986 push_temp_slots ();
4987 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4988 &unsignedp, &reversep, &volatilep);
4990 /* Make sure bitpos is not negative, it can wreak havoc later. */
4991 if (bitpos < 0)
4993 gcc_assert (offset == NULL_TREE);
4994 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
4995 bitpos &= BITS_PER_UNIT - 1;
4998 if (TREE_CODE (to) == COMPONENT_REF
4999 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5000 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5001 /* The C++ memory model naturally applies to byte-aligned fields.
5002 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5003 BITSIZE are not byte-aligned, there is no need to limit the range
5004 we can access. This can occur with packed structures in Ada. */
5005 else if (bitsize > 0
5006 && bitsize % BITS_PER_UNIT == 0
5007 && bitpos % BITS_PER_UNIT == 0)
5009 bitregion_start = bitpos;
5010 bitregion_end = bitpos + bitsize - 1;
5013 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5015 /* If the field has a mode, we want to access it in the
5016 field's mode, not the computed mode.
5017 If a MEM has VOIDmode (external with incomplete type),
5018 use BLKmode for it instead. */
5019 if (MEM_P (to_rtx))
5021 if (mode1 != VOIDmode)
5022 to_rtx = adjust_address (to_rtx, mode1, 0);
5023 else if (GET_MODE (to_rtx) == VOIDmode)
5024 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5027 if (offset != 0)
5029 machine_mode address_mode;
5030 rtx offset_rtx;
5032 if (!MEM_P (to_rtx))
5034 /* We can get constant negative offsets into arrays with broken
5035 user code. Translate this to a trap instead of ICEing. */
5036 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5037 expand_builtin_trap ();
5038 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5041 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5042 address_mode = get_address_mode (to_rtx);
5043 if (GET_MODE (offset_rtx) != address_mode)
5045 /* We cannot be sure that the RTL in offset_rtx is valid outside
5046 of a memory address context, so force it into a register
5047 before attempting to convert it to the desired mode. */
5048 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5049 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5052 /* If we have an expression in OFFSET_RTX and a non-zero
5053 byte offset in BITPOS, adding the byte offset before the
5054 OFFSET_RTX results in better intermediate code, which makes
5055 later rtl optimization passes perform better.
5057 We prefer intermediate code like this:
5059 r124:DI=r123:DI+0x18
5060 [r124:DI]=r121:DI
5062 ... instead of ...
5064 r124:DI=r123:DI+0x10
5065 [r124:DI+0x8]=r121:DI
5067 This is only done for aligned data values, as these can
5068 be expected to result in single move instructions. */
5069 if (mode1 != VOIDmode
5070 && bitpos != 0
5071 && bitsize > 0
5072 && (bitpos % bitsize) == 0
5073 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5074 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5076 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5077 bitregion_start = 0;
5078 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5079 bitregion_end -= bitpos;
5080 bitpos = 0;
5083 to_rtx = offset_address (to_rtx, offset_rtx,
5084 highest_pow2_factor_for_target (to,
5085 offset));
5088 /* No action is needed if the target is not a memory and the field
5089 lies completely outside that target. This can occur if the source
5090 code contains an out-of-bounds access to a small array. */
5091 if (!MEM_P (to_rtx)
5092 && GET_MODE (to_rtx) != BLKmode
5093 && (unsigned HOST_WIDE_INT) bitpos
5094 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5096 expand_normal (from);
5097 result = NULL;
5099 /* Handle expand_expr of a complex value returning a CONCAT. */
5100 else if (GET_CODE (to_rtx) == CONCAT)
5102 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5103 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5104 && bitpos == 0
5105 && bitsize == mode_bitsize)
5106 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5107 else if (bitsize == mode_bitsize / 2
5108 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5109 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5110 nontemporal, reversep);
5111 else if (bitpos + bitsize <= mode_bitsize / 2)
5112 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5113 bitregion_start, bitregion_end,
5114 mode1, from, get_alias_set (to),
5115 nontemporal, reversep);
5116 else if (bitpos >= mode_bitsize / 2)
5117 result = store_field (XEXP (to_rtx, 1), bitsize,
5118 bitpos - mode_bitsize / 2,
5119 bitregion_start, bitregion_end,
5120 mode1, from, get_alias_set (to),
5121 nontemporal, reversep);
5122 else if (bitpos == 0 && bitsize == mode_bitsize)
5124 rtx from_rtx;
5125 result = expand_normal (from);
5126 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5127 TYPE_MODE (TREE_TYPE (from)), 0);
5128 emit_move_insn (XEXP (to_rtx, 0),
5129 read_complex_part (from_rtx, false));
5130 emit_move_insn (XEXP (to_rtx, 1),
5131 read_complex_part (from_rtx, true));
5133 else
5135 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5136 GET_MODE_SIZE (GET_MODE (to_rtx)));
5137 write_complex_part (temp, XEXP (to_rtx, 0), false);
5138 write_complex_part (temp, XEXP (to_rtx, 1), true);
5139 result = store_field (temp, bitsize, bitpos,
5140 bitregion_start, bitregion_end,
5141 mode1, from, get_alias_set (to),
5142 nontemporal, reversep);
5143 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5144 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5147 else
5149 if (MEM_P (to_rtx))
5151 /* If the field is at offset zero, we could have been given the
5152 DECL_RTX of the parent struct. Don't munge it. */
5153 to_rtx = shallow_copy_rtx (to_rtx);
5154 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5155 if (volatilep)
5156 MEM_VOLATILE_P (to_rtx) = 1;
5159 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5160 bitregion_start, bitregion_end,
5161 mode1, to_rtx, to, from,
5162 reversep))
5163 result = NULL;
5164 else
5165 result = store_field (to_rtx, bitsize, bitpos,
5166 bitregion_start, bitregion_end,
5167 mode1, from, get_alias_set (to),
5168 nontemporal, reversep);
5171 if (result)
5172 preserve_temp_slots (result);
5173 pop_temp_slots ();
5174 return;
5177 /* If the rhs is a function call and its value is not an aggregate,
5178 call the function before we start to compute the lhs.
5179 This is needed for correct code for cases such as
5180 val = setjmp (buf) on machines where reference to val
5181 requires loading up part of an address in a separate insn.
5183 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5184 since it might be a promoted variable where the zero- or sign- extension
5185 needs to be done. Handling this in the normal way is safe because no
5186 computation is done before the call. The same is true for SSA names. */
5187 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5188 && COMPLETE_TYPE_P (TREE_TYPE (from))
5189 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5190 && ! (((VAR_P (to)
5191 || TREE_CODE (to) == PARM_DECL
5192 || TREE_CODE (to) == RESULT_DECL)
5193 && REG_P (DECL_RTL (to)))
5194 || TREE_CODE (to) == SSA_NAME))
5196 rtx value;
5197 rtx bounds;
5199 push_temp_slots ();
5200 value = expand_normal (from);
5202 /* Split value and bounds to store them separately. */
5203 chkp_split_slot (value, &value, &bounds);
5205 if (to_rtx == 0)
5206 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5208 /* Handle calls that return values in multiple non-contiguous locations.
5209 The Irix 6 ABI has examples of this. */
5210 if (GET_CODE (to_rtx) == PARALLEL)
5212 if (GET_CODE (value) == PARALLEL)
5213 emit_group_move (to_rtx, value);
5214 else
5215 emit_group_load (to_rtx, value, TREE_TYPE (from),
5216 int_size_in_bytes (TREE_TYPE (from)));
5218 else if (GET_CODE (value) == PARALLEL)
5219 emit_group_store (to_rtx, value, TREE_TYPE (from),
5220 int_size_in_bytes (TREE_TYPE (from)));
5221 else if (GET_MODE (to_rtx) == BLKmode)
5223 /* Handle calls that return BLKmode values in registers. */
5224 if (REG_P (value))
5225 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5226 else
5227 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5229 else
5231 if (POINTER_TYPE_P (TREE_TYPE (to)))
5232 value = convert_memory_address_addr_space
5233 (GET_MODE (to_rtx), value,
5234 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5236 emit_move_insn (to_rtx, value);
5239 /* Store bounds if required. */
5240 if (bounds
5241 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5243 gcc_assert (MEM_P (to_rtx));
5244 chkp_emit_bounds_store (bounds, value, to_rtx);
5247 preserve_temp_slots (to_rtx);
5248 pop_temp_slots ();
5249 return;
5252 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5253 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5255 /* Don't move directly into a return register. */
5256 if (TREE_CODE (to) == RESULT_DECL
5257 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5259 rtx temp;
5261 push_temp_slots ();
5263 /* If the source is itself a return value, it still is in a pseudo at
5264 this point so we can move it back to the return register directly. */
5265 if (REG_P (to_rtx)
5266 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5267 && TREE_CODE (from) != CALL_EXPR)
5268 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5269 else
5270 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5272 /* Handle calls that return values in multiple non-contiguous locations.
5273 The Irix 6 ABI has examples of this. */
5274 if (GET_CODE (to_rtx) == PARALLEL)
5276 if (GET_CODE (temp) == PARALLEL)
5277 emit_group_move (to_rtx, temp);
5278 else
5279 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5280 int_size_in_bytes (TREE_TYPE (from)));
5282 else if (temp)
5283 emit_move_insn (to_rtx, temp);
5285 preserve_temp_slots (to_rtx);
5286 pop_temp_slots ();
5287 return;
5290 /* In case we are returning the contents of an object which overlaps
5291 the place the value is being stored, use a safe function when copying
5292 a value through a pointer into a structure value return block. */
5293 if (TREE_CODE (to) == RESULT_DECL
5294 && TREE_CODE (from) == INDIRECT_REF
5295 && ADDR_SPACE_GENERIC_P
5296 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5297 && refs_may_alias_p (to, from)
5298 && cfun->returns_struct
5299 && !cfun->returns_pcc_struct)
5301 rtx from_rtx, size;
5303 push_temp_slots ();
5304 size = expr_size (from);
5305 from_rtx = expand_normal (from);
5307 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5309 preserve_temp_slots (to_rtx);
5310 pop_temp_slots ();
5311 return;
5314 /* Compute FROM and store the value in the rtx we got. */
5316 push_temp_slots ();
5317 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5318 preserve_temp_slots (result);
5319 pop_temp_slots ();
5320 return;
5323 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5324 succeeded, false otherwise. */
5326 bool
5327 emit_storent_insn (rtx to, rtx from)
5329 struct expand_operand ops[2];
5330 machine_mode mode = GET_MODE (to);
5331 enum insn_code code = optab_handler (storent_optab, mode);
5333 if (code == CODE_FOR_nothing)
5334 return false;
5336 create_fixed_operand (&ops[0], to);
5337 create_input_operand (&ops[1], from, mode);
5338 return maybe_expand_insn (code, 2, ops);
5341 /* Generate code for computing expression EXP,
5342 and storing the value into TARGET.
5344 If the mode is BLKmode then we may return TARGET itself.
5345 It turns out that in BLKmode it doesn't cause a problem.
5346 because C has no operators that could combine two different
5347 assignments into the same BLKmode object with different values
5348 with no sequence point. Will other languages need this to
5349 be more thorough?
5351 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5352 stack, and block moves may need to be treated specially.
5354 If NONTEMPORAL is true, try using a nontemporal store instruction.
5356 If REVERSE is true, the store is to be done in reverse order.
5358 If BTARGET is not NULL then computed bounds of EXP are
5359 associated with BTARGET. */
5362 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5363 bool nontemporal, bool reverse, tree btarget)
5365 rtx temp;
5366 rtx alt_rtl = NULL_RTX;
5367 location_t loc = curr_insn_location ();
5369 if (VOID_TYPE_P (TREE_TYPE (exp)))
5371 /* C++ can generate ?: expressions with a throw expression in one
5372 branch and an rvalue in the other. Here, we resolve attempts to
5373 store the throw expression's nonexistent result. */
5374 gcc_assert (!call_param_p);
5375 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5376 return NULL_RTX;
5378 if (TREE_CODE (exp) == COMPOUND_EXPR)
5380 /* Perform first part of compound expression, then assign from second
5381 part. */
5382 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5383 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5384 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5385 call_param_p, nontemporal, reverse,
5386 btarget);
5388 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5390 /* For conditional expression, get safe form of the target. Then
5391 test the condition, doing the appropriate assignment on either
5392 side. This avoids the creation of unnecessary temporaries.
5393 For non-BLKmode, it is more efficient not to do this. */
5395 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5397 do_pending_stack_adjust ();
5398 NO_DEFER_POP;
5399 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5400 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5401 nontemporal, reverse, btarget);
5402 emit_jump_insn (targetm.gen_jump (lab2));
5403 emit_barrier ();
5404 emit_label (lab1);
5405 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5406 nontemporal, reverse, btarget);
5407 emit_label (lab2);
5408 OK_DEFER_POP;
5410 return NULL_RTX;
5412 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5413 /* If this is a scalar in a register that is stored in a wider mode
5414 than the declared mode, compute the result into its declared mode
5415 and then convert to the wider mode. Our value is the computed
5416 expression. */
5418 rtx inner_target = 0;
5420 /* We can do the conversion inside EXP, which will often result
5421 in some optimizations. Do the conversion in two steps: first
5422 change the signedness, if needed, then the extend. But don't
5423 do this if the type of EXP is a subtype of something else
5424 since then the conversion might involve more than just
5425 converting modes. */
5426 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5427 && TREE_TYPE (TREE_TYPE (exp)) == 0
5428 && GET_MODE_PRECISION (GET_MODE (target))
5429 == TYPE_PRECISION (TREE_TYPE (exp)))
5431 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5432 TYPE_UNSIGNED (TREE_TYPE (exp))))
5434 /* Some types, e.g. Fortran's logical*4, won't have a signed
5435 version, so use the mode instead. */
5436 tree ntype
5437 = (signed_or_unsigned_type_for
5438 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5439 if (ntype == NULL)
5440 ntype = lang_hooks.types.type_for_mode
5441 (TYPE_MODE (TREE_TYPE (exp)),
5442 SUBREG_PROMOTED_SIGN (target));
5444 exp = fold_convert_loc (loc, ntype, exp);
5447 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5448 (GET_MODE (SUBREG_REG (target)),
5449 SUBREG_PROMOTED_SIGN (target)),
5450 exp);
5452 inner_target = SUBREG_REG (target);
5455 temp = expand_expr (exp, inner_target, VOIDmode,
5456 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5458 /* Handle bounds returned by call. */
5459 if (TREE_CODE (exp) == CALL_EXPR)
5461 rtx bounds;
5462 chkp_split_slot (temp, &temp, &bounds);
5463 if (bounds && btarget)
5465 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5466 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5467 chkp_set_rtl_bounds (btarget, tmp);
5471 /* If TEMP is a VOIDmode constant, use convert_modes to make
5472 sure that we properly convert it. */
5473 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5475 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5476 temp, SUBREG_PROMOTED_SIGN (target));
5477 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5478 GET_MODE (target), temp,
5479 SUBREG_PROMOTED_SIGN (target));
5482 convert_move (SUBREG_REG (target), temp,
5483 SUBREG_PROMOTED_SIGN (target));
5485 return NULL_RTX;
5487 else if ((TREE_CODE (exp) == STRING_CST
5488 || (TREE_CODE (exp) == MEM_REF
5489 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5490 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5491 == STRING_CST
5492 && integer_zerop (TREE_OPERAND (exp, 1))))
5493 && !nontemporal && !call_param_p
5494 && MEM_P (target))
5496 /* Optimize initialization of an array with a STRING_CST. */
5497 HOST_WIDE_INT exp_len, str_copy_len;
5498 rtx dest_mem;
5499 tree str = TREE_CODE (exp) == STRING_CST
5500 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5502 exp_len = int_expr_size (exp);
5503 if (exp_len <= 0)
5504 goto normal_expr;
5506 if (TREE_STRING_LENGTH (str) <= 0)
5507 goto normal_expr;
5509 str_copy_len = strlen (TREE_STRING_POINTER (str));
5510 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5511 goto normal_expr;
5513 str_copy_len = TREE_STRING_LENGTH (str);
5514 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5515 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5517 str_copy_len += STORE_MAX_PIECES - 1;
5518 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5520 str_copy_len = MIN (str_copy_len, exp_len);
5521 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5522 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5523 MEM_ALIGN (target), false))
5524 goto normal_expr;
5526 dest_mem = target;
5528 dest_mem = store_by_pieces (dest_mem,
5529 str_copy_len, builtin_strncpy_read_str,
5530 CONST_CAST (char *,
5531 TREE_STRING_POINTER (str)),
5532 MEM_ALIGN (target), false,
5533 exp_len > str_copy_len ? 1 : 0);
5534 if (exp_len > str_copy_len)
5535 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5536 GEN_INT (exp_len - str_copy_len),
5537 BLOCK_OP_NORMAL);
5538 return NULL_RTX;
5540 else
5542 rtx tmp_target;
5544 normal_expr:
5545 /* If we want to use a nontemporal or a reverse order store, force the
5546 value into a register first. */
5547 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5548 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5549 (call_param_p
5550 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5551 &alt_rtl, false);
5553 /* Handle bounds returned by call. */
5554 if (TREE_CODE (exp) == CALL_EXPR)
5556 rtx bounds;
5557 chkp_split_slot (temp, &temp, &bounds);
5558 if (bounds && btarget)
5560 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5561 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5562 chkp_set_rtl_bounds (btarget, tmp);
5567 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5568 the same as that of TARGET, adjust the constant. This is needed, for
5569 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5570 only a word-sized value. */
5571 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5572 && TREE_CODE (exp) != ERROR_MARK
5573 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5574 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5575 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5577 /* If value was not generated in the target, store it there.
5578 Convert the value to TARGET's type first if necessary and emit the
5579 pending incrementations that have been queued when expanding EXP.
5580 Note that we cannot emit the whole queue blindly because this will
5581 effectively disable the POST_INC optimization later.
5583 If TEMP and TARGET compare equal according to rtx_equal_p, but
5584 one or both of them are volatile memory refs, we have to distinguish
5585 two cases:
5586 - expand_expr has used TARGET. In this case, we must not generate
5587 another copy. This can be detected by TARGET being equal according
5588 to == .
5589 - expand_expr has not used TARGET - that means that the source just
5590 happens to have the same RTX form. Since temp will have been created
5591 by expand_expr, it will compare unequal according to == .
5592 We must generate a copy in this case, to reach the correct number
5593 of volatile memory references. */
5595 if ((! rtx_equal_p (temp, target)
5596 || (temp != target && (side_effects_p (temp)
5597 || side_effects_p (target))))
5598 && TREE_CODE (exp) != ERROR_MARK
5599 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5600 but TARGET is not valid memory reference, TEMP will differ
5601 from TARGET although it is really the same location. */
5602 && !(alt_rtl
5603 && rtx_equal_p (alt_rtl, target)
5604 && !side_effects_p (alt_rtl)
5605 && !side_effects_p (target))
5606 /* If there's nothing to copy, don't bother. Don't call
5607 expr_size unless necessary, because some front-ends (C++)
5608 expr_size-hook must not be given objects that are not
5609 supposed to be bit-copied or bit-initialized. */
5610 && expr_size (exp) != const0_rtx)
5612 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5614 if (GET_MODE (target) == BLKmode)
5616 /* Handle calls that return BLKmode values in registers. */
5617 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5618 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5619 else
5620 store_bit_field (target,
5621 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5622 0, 0, 0, GET_MODE (temp), temp, reverse);
5624 else
5625 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5628 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5630 /* Handle copying a string constant into an array. The string
5631 constant may be shorter than the array. So copy just the string's
5632 actual length, and clear the rest. First get the size of the data
5633 type of the string, which is actually the size of the target. */
5634 rtx size = expr_size (exp);
5636 if (CONST_INT_P (size)
5637 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5638 emit_block_move (target, temp, size,
5639 (call_param_p
5640 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5641 else
5643 machine_mode pointer_mode
5644 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5645 machine_mode address_mode = get_address_mode (target);
5647 /* Compute the size of the data to copy from the string. */
5648 tree copy_size
5649 = size_binop_loc (loc, MIN_EXPR,
5650 make_tree (sizetype, size),
5651 size_int (TREE_STRING_LENGTH (exp)));
5652 rtx copy_size_rtx
5653 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5654 (call_param_p
5655 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5656 rtx_code_label *label = 0;
5658 /* Copy that much. */
5659 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5660 TYPE_UNSIGNED (sizetype));
5661 emit_block_move (target, temp, copy_size_rtx,
5662 (call_param_p
5663 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5665 /* Figure out how much is left in TARGET that we have to clear.
5666 Do all calculations in pointer_mode. */
5667 if (CONST_INT_P (copy_size_rtx))
5669 size = plus_constant (address_mode, size,
5670 -INTVAL (copy_size_rtx));
5671 target = adjust_address (target, BLKmode,
5672 INTVAL (copy_size_rtx));
5674 else
5676 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5677 copy_size_rtx, NULL_RTX, 0,
5678 OPTAB_LIB_WIDEN);
5680 if (GET_MODE (copy_size_rtx) != address_mode)
5681 copy_size_rtx = convert_to_mode (address_mode,
5682 copy_size_rtx,
5683 TYPE_UNSIGNED (sizetype));
5685 target = offset_address (target, copy_size_rtx,
5686 highest_pow2_factor (copy_size));
5687 label = gen_label_rtx ();
5688 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5689 GET_MODE (size), 0, label);
5692 if (size != const0_rtx)
5693 clear_storage (target, size, BLOCK_OP_NORMAL);
5695 if (label)
5696 emit_label (label);
5699 /* Handle calls that return values in multiple non-contiguous locations.
5700 The Irix 6 ABI has examples of this. */
5701 else if (GET_CODE (target) == PARALLEL)
5703 if (GET_CODE (temp) == PARALLEL)
5704 emit_group_move (target, temp);
5705 else
5706 emit_group_load (target, temp, TREE_TYPE (exp),
5707 int_size_in_bytes (TREE_TYPE (exp)));
5709 else if (GET_CODE (temp) == PARALLEL)
5710 emit_group_store (target, temp, TREE_TYPE (exp),
5711 int_size_in_bytes (TREE_TYPE (exp)));
5712 else if (GET_MODE (temp) == BLKmode)
5713 emit_block_move (target, temp, expr_size (exp),
5714 (call_param_p
5715 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5716 /* If we emit a nontemporal store, there is nothing else to do. */
5717 else if (nontemporal && emit_storent_insn (target, temp))
5719 else
5721 if (reverse)
5722 temp = flip_storage_order (GET_MODE (target), temp);
5723 temp = force_operand (temp, target);
5724 if (temp != target)
5725 emit_move_insn (target, temp);
5729 return NULL_RTX;
5732 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5734 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5735 bool reverse)
5737 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5738 reverse, NULL);
5741 /* Return true if field F of structure TYPE is a flexible array. */
5743 static bool
5744 flexible_array_member_p (const_tree f, const_tree type)
5746 const_tree tf;
5748 tf = TREE_TYPE (f);
5749 return (DECL_CHAIN (f) == NULL
5750 && TREE_CODE (tf) == ARRAY_TYPE
5751 && TYPE_DOMAIN (tf)
5752 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5753 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5754 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5755 && int_size_in_bytes (type) >= 0);
5758 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5759 must have in order for it to completely initialize a value of type TYPE.
5760 Return -1 if the number isn't known.
5762 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5764 static HOST_WIDE_INT
5765 count_type_elements (const_tree type, bool for_ctor_p)
5767 switch (TREE_CODE (type))
5769 case ARRAY_TYPE:
5771 tree nelts;
5773 nelts = array_type_nelts (type);
5774 if (nelts && tree_fits_uhwi_p (nelts))
5776 unsigned HOST_WIDE_INT n;
5778 n = tree_to_uhwi (nelts) + 1;
5779 if (n == 0 || for_ctor_p)
5780 return n;
5781 else
5782 return n * count_type_elements (TREE_TYPE (type), false);
5784 return for_ctor_p ? -1 : 1;
5787 case RECORD_TYPE:
5789 unsigned HOST_WIDE_INT n;
5790 tree f;
5792 n = 0;
5793 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5794 if (TREE_CODE (f) == FIELD_DECL)
5796 if (!for_ctor_p)
5797 n += count_type_elements (TREE_TYPE (f), false);
5798 else if (!flexible_array_member_p (f, type))
5799 /* Don't count flexible arrays, which are not supposed
5800 to be initialized. */
5801 n += 1;
5804 return n;
5807 case UNION_TYPE:
5808 case QUAL_UNION_TYPE:
5810 tree f;
5811 HOST_WIDE_INT n, m;
5813 gcc_assert (!for_ctor_p);
5814 /* Estimate the number of scalars in each field and pick the
5815 maximum. Other estimates would do instead; the idea is simply
5816 to make sure that the estimate is not sensitive to the ordering
5817 of the fields. */
5818 n = 1;
5819 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5820 if (TREE_CODE (f) == FIELD_DECL)
5822 m = count_type_elements (TREE_TYPE (f), false);
5823 /* If the field doesn't span the whole union, add an extra
5824 scalar for the rest. */
5825 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5826 TYPE_SIZE (type)) != 1)
5827 m++;
5828 if (n < m)
5829 n = m;
5831 return n;
5834 case COMPLEX_TYPE:
5835 return 2;
5837 case VECTOR_TYPE:
5838 return TYPE_VECTOR_SUBPARTS (type);
5840 case INTEGER_TYPE:
5841 case REAL_TYPE:
5842 case FIXED_POINT_TYPE:
5843 case ENUMERAL_TYPE:
5844 case BOOLEAN_TYPE:
5845 case POINTER_TYPE:
5846 case OFFSET_TYPE:
5847 case REFERENCE_TYPE:
5848 case NULLPTR_TYPE:
5849 return 1;
5851 case ERROR_MARK:
5852 return 0;
5854 case VOID_TYPE:
5855 case METHOD_TYPE:
5856 case FUNCTION_TYPE:
5857 case LANG_TYPE:
5858 default:
5859 gcc_unreachable ();
5863 /* Helper for categorize_ctor_elements. Identical interface. */
5865 static bool
5866 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5867 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5869 unsigned HOST_WIDE_INT idx;
5870 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5871 tree value, purpose, elt_type;
5873 /* Whether CTOR is a valid constant initializer, in accordance with what
5874 initializer_constant_valid_p does. If inferred from the constructor
5875 elements, true until proven otherwise. */
5876 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5877 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5879 nz_elts = 0;
5880 init_elts = 0;
5881 num_fields = 0;
5882 elt_type = NULL_TREE;
5884 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5886 HOST_WIDE_INT mult = 1;
5888 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5890 tree lo_index = TREE_OPERAND (purpose, 0);
5891 tree hi_index = TREE_OPERAND (purpose, 1);
5893 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5894 mult = (tree_to_uhwi (hi_index)
5895 - tree_to_uhwi (lo_index) + 1);
5897 num_fields += mult;
5898 elt_type = TREE_TYPE (value);
5900 switch (TREE_CODE (value))
5902 case CONSTRUCTOR:
5904 HOST_WIDE_INT nz = 0, ic = 0;
5906 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5907 p_complete);
5909 nz_elts += mult * nz;
5910 init_elts += mult * ic;
5912 if (const_from_elts_p && const_p)
5913 const_p = const_elt_p;
5915 break;
5917 case INTEGER_CST:
5918 case REAL_CST:
5919 case FIXED_CST:
5920 if (!initializer_zerop (value))
5921 nz_elts += mult;
5922 init_elts += mult;
5923 break;
5925 case STRING_CST:
5926 nz_elts += mult * TREE_STRING_LENGTH (value);
5927 init_elts += mult * TREE_STRING_LENGTH (value);
5928 break;
5930 case COMPLEX_CST:
5931 if (!initializer_zerop (TREE_REALPART (value)))
5932 nz_elts += mult;
5933 if (!initializer_zerop (TREE_IMAGPART (value)))
5934 nz_elts += mult;
5935 init_elts += mult;
5936 break;
5938 case VECTOR_CST:
5940 unsigned i;
5941 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5943 tree v = VECTOR_CST_ELT (value, i);
5944 if (!initializer_zerop (v))
5945 nz_elts += mult;
5946 init_elts += mult;
5949 break;
5951 default:
5953 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5954 nz_elts += mult * tc;
5955 init_elts += mult * tc;
5957 if (const_from_elts_p && const_p)
5958 const_p
5959 = initializer_constant_valid_p (value,
5960 elt_type,
5961 TYPE_REVERSE_STORAGE_ORDER
5962 (TREE_TYPE (ctor)))
5963 != NULL_TREE;
5965 break;
5969 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5970 num_fields, elt_type))
5971 *p_complete = false;
5973 *p_nz_elts += nz_elts;
5974 *p_init_elts += init_elts;
5976 return const_p;
5979 /* Examine CTOR to discover:
5980 * how many scalar fields are set to nonzero values,
5981 and place it in *P_NZ_ELTS;
5982 * how many scalar fields in total are in CTOR,
5983 and place it in *P_ELT_COUNT.
5984 * whether the constructor is complete -- in the sense that every
5985 meaningful byte is explicitly given a value --
5986 and place it in *P_COMPLETE.
5988 Return whether or not CTOR is a valid static constant initializer, the same
5989 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5991 bool
5992 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5993 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5995 *p_nz_elts = 0;
5996 *p_init_elts = 0;
5997 *p_complete = true;
5999 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6002 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6003 of which had type LAST_TYPE. Each element was itself a complete
6004 initializer, in the sense that every meaningful byte was explicitly
6005 given a value. Return true if the same is true for the constructor
6006 as a whole. */
6008 bool
6009 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6010 const_tree last_type)
6012 if (TREE_CODE (type) == UNION_TYPE
6013 || TREE_CODE (type) == QUAL_UNION_TYPE)
6015 if (num_elts == 0)
6016 return false;
6018 gcc_assert (num_elts == 1 && last_type);
6020 /* ??? We could look at each element of the union, and find the
6021 largest element. Which would avoid comparing the size of the
6022 initialized element against any tail padding in the union.
6023 Doesn't seem worth the effort... */
6024 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6027 return count_type_elements (type, true) == num_elts;
6030 /* Return 1 if EXP contains mostly (3/4) zeros. */
6032 static int
6033 mostly_zeros_p (const_tree exp)
6035 if (TREE_CODE (exp) == CONSTRUCTOR)
6037 HOST_WIDE_INT nz_elts, init_elts;
6038 bool complete_p;
6040 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6041 return !complete_p || nz_elts < init_elts / 4;
6044 return initializer_zerop (exp);
6047 /* Return 1 if EXP contains all zeros. */
6049 static int
6050 all_zeros_p (const_tree exp)
6052 if (TREE_CODE (exp) == CONSTRUCTOR)
6054 HOST_WIDE_INT nz_elts, init_elts;
6055 bool complete_p;
6057 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6058 return nz_elts == 0;
6061 return initializer_zerop (exp);
6064 /* Helper function for store_constructor.
6065 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6066 CLEARED is as for store_constructor.
6067 ALIAS_SET is the alias set to use for any stores.
6068 If REVERSE is true, the store is to be done in reverse order.
6070 This provides a recursive shortcut back to store_constructor when it isn't
6071 necessary to go through store_field. This is so that we can pass through
6072 the cleared field to let store_constructor know that we may not have to
6073 clear a substructure if the outer structure has already been cleared. */
6075 static void
6076 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6077 HOST_WIDE_INT bitpos, machine_mode mode,
6078 tree exp, int cleared,
6079 alias_set_type alias_set, bool reverse)
6081 if (TREE_CODE (exp) == CONSTRUCTOR
6082 /* We can only call store_constructor recursively if the size and
6083 bit position are on a byte boundary. */
6084 && bitpos % BITS_PER_UNIT == 0
6085 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6086 /* If we have a nonzero bitpos for a register target, then we just
6087 let store_field do the bitfield handling. This is unlikely to
6088 generate unnecessary clear instructions anyways. */
6089 && (bitpos == 0 || MEM_P (target)))
6091 if (MEM_P (target))
6092 target
6093 = adjust_address (target,
6094 GET_MODE (target) == BLKmode
6095 || 0 != (bitpos
6096 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6097 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6100 /* Update the alias set, if required. */
6101 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6102 && MEM_ALIAS_SET (target) != 0)
6104 target = copy_rtx (target);
6105 set_mem_alias_set (target, alias_set);
6108 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6109 reverse);
6111 else
6112 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false,
6113 reverse);
6117 /* Returns the number of FIELD_DECLs in TYPE. */
6119 static int
6120 fields_length (const_tree type)
6122 tree t = TYPE_FIELDS (type);
6123 int count = 0;
6125 for (; t; t = DECL_CHAIN (t))
6126 if (TREE_CODE (t) == FIELD_DECL)
6127 ++count;
6129 return count;
6133 /* Store the value of constructor EXP into the rtx TARGET.
6134 TARGET is either a REG or a MEM; we know it cannot conflict, since
6135 safe_from_p has been called.
6136 CLEARED is true if TARGET is known to have been zero'd.
6137 SIZE is the number of bytes of TARGET we are allowed to modify: this
6138 may not be the same as the size of EXP if we are assigning to a field
6139 which has been packed to exclude padding bits.
6140 If REVERSE is true, the store is to be done in reverse order. */
6142 static void
6143 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6144 bool reverse)
6146 tree type = TREE_TYPE (exp);
6147 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6149 switch (TREE_CODE (type))
6151 case RECORD_TYPE:
6152 case UNION_TYPE:
6153 case QUAL_UNION_TYPE:
6155 unsigned HOST_WIDE_INT idx;
6156 tree field, value;
6158 /* The storage order is specified for every aggregate type. */
6159 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6161 /* If size is zero or the target is already cleared, do nothing. */
6162 if (size == 0 || cleared)
6163 cleared = 1;
6164 /* We either clear the aggregate or indicate the value is dead. */
6165 else if ((TREE_CODE (type) == UNION_TYPE
6166 || TREE_CODE (type) == QUAL_UNION_TYPE)
6167 && ! CONSTRUCTOR_ELTS (exp))
6168 /* If the constructor is empty, clear the union. */
6170 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6171 cleared = 1;
6174 /* If we are building a static constructor into a register,
6175 set the initial value as zero so we can fold the value into
6176 a constant. But if more than one register is involved,
6177 this probably loses. */
6178 else if (REG_P (target) && TREE_STATIC (exp)
6179 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6181 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6182 cleared = 1;
6185 /* If the constructor has fewer fields than the structure or
6186 if we are initializing the structure to mostly zeros, clear
6187 the whole structure first. Don't do this if TARGET is a
6188 register whose mode size isn't equal to SIZE since
6189 clear_storage can't handle this case. */
6190 else if (size > 0
6191 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6192 || mostly_zeros_p (exp))
6193 && (!REG_P (target)
6194 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6195 == size)))
6197 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6198 cleared = 1;
6201 if (REG_P (target) && !cleared)
6202 emit_clobber (target);
6204 /* Store each element of the constructor into the
6205 corresponding field of TARGET. */
6206 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6208 machine_mode mode;
6209 HOST_WIDE_INT bitsize;
6210 HOST_WIDE_INT bitpos = 0;
6211 tree offset;
6212 rtx to_rtx = target;
6214 /* Just ignore missing fields. We cleared the whole
6215 structure, above, if any fields are missing. */
6216 if (field == 0)
6217 continue;
6219 if (cleared && initializer_zerop (value))
6220 continue;
6222 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6223 bitsize = tree_to_uhwi (DECL_SIZE (field));
6224 else
6225 bitsize = -1;
6227 mode = DECL_MODE (field);
6228 if (DECL_BIT_FIELD (field))
6229 mode = VOIDmode;
6231 offset = DECL_FIELD_OFFSET (field);
6232 if (tree_fits_shwi_p (offset)
6233 && tree_fits_shwi_p (bit_position (field)))
6235 bitpos = int_bit_position (field);
6236 offset = 0;
6238 else
6239 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6241 if (offset)
6243 machine_mode address_mode;
6244 rtx offset_rtx;
6246 offset
6247 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6248 make_tree (TREE_TYPE (exp),
6249 target));
6251 offset_rtx = expand_normal (offset);
6252 gcc_assert (MEM_P (to_rtx));
6254 address_mode = get_address_mode (to_rtx);
6255 if (GET_MODE (offset_rtx) != address_mode)
6256 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6258 to_rtx = offset_address (to_rtx, offset_rtx,
6259 highest_pow2_factor (offset));
6262 /* If this initializes a field that is smaller than a
6263 word, at the start of a word, try to widen it to a full
6264 word. This special case allows us to output C++ member
6265 function initializations in a form that the optimizers
6266 can understand. */
6267 if (WORD_REGISTER_OPERATIONS
6268 && REG_P (target)
6269 && bitsize < BITS_PER_WORD
6270 && bitpos % BITS_PER_WORD == 0
6271 && GET_MODE_CLASS (mode) == MODE_INT
6272 && TREE_CODE (value) == INTEGER_CST
6273 && exp_size >= 0
6274 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6276 tree type = TREE_TYPE (value);
6278 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6280 type = lang_hooks.types.type_for_mode
6281 (word_mode, TYPE_UNSIGNED (type));
6282 value = fold_convert (type, value);
6283 /* Make sure the bits beyond the original bitsize are zero
6284 so that we can correctly avoid extra zeroing stores in
6285 later constructor elements. */
6286 tree bitsize_mask
6287 = wide_int_to_tree (type, wi::mask (bitsize, false,
6288 BITS_PER_WORD));
6289 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6292 if (BYTES_BIG_ENDIAN)
6293 value
6294 = fold_build2 (LSHIFT_EXPR, type, value,
6295 build_int_cst (type,
6296 BITS_PER_WORD - bitsize));
6297 bitsize = BITS_PER_WORD;
6298 mode = word_mode;
6301 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6302 && DECL_NONADDRESSABLE_P (field))
6304 to_rtx = copy_rtx (to_rtx);
6305 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6308 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6309 value, cleared,
6310 get_alias_set (TREE_TYPE (field)),
6311 reverse);
6313 break;
6315 case ARRAY_TYPE:
6317 tree value, index;
6318 unsigned HOST_WIDE_INT i;
6319 int need_to_clear;
6320 tree domain;
6321 tree elttype = TREE_TYPE (type);
6322 int const_bounds_p;
6323 HOST_WIDE_INT minelt = 0;
6324 HOST_WIDE_INT maxelt = 0;
6326 /* The storage order is specified for every aggregate type. */
6327 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6329 domain = TYPE_DOMAIN (type);
6330 const_bounds_p = (TYPE_MIN_VALUE (domain)
6331 && TYPE_MAX_VALUE (domain)
6332 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6333 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6335 /* If we have constant bounds for the range of the type, get them. */
6336 if (const_bounds_p)
6338 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6339 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6342 /* If the constructor has fewer elements than the array, clear
6343 the whole array first. Similarly if this is static
6344 constructor of a non-BLKmode object. */
6345 if (cleared)
6346 need_to_clear = 0;
6347 else if (REG_P (target) && TREE_STATIC (exp))
6348 need_to_clear = 1;
6349 else
6351 unsigned HOST_WIDE_INT idx;
6352 tree index, value;
6353 HOST_WIDE_INT count = 0, zero_count = 0;
6354 need_to_clear = ! const_bounds_p;
6356 /* This loop is a more accurate version of the loop in
6357 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6358 is also needed to check for missing elements. */
6359 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6361 HOST_WIDE_INT this_node_count;
6363 if (need_to_clear)
6364 break;
6366 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6368 tree lo_index = TREE_OPERAND (index, 0);
6369 tree hi_index = TREE_OPERAND (index, 1);
6371 if (! tree_fits_uhwi_p (lo_index)
6372 || ! tree_fits_uhwi_p (hi_index))
6374 need_to_clear = 1;
6375 break;
6378 this_node_count = (tree_to_uhwi (hi_index)
6379 - tree_to_uhwi (lo_index) + 1);
6381 else
6382 this_node_count = 1;
6384 count += this_node_count;
6385 if (mostly_zeros_p (value))
6386 zero_count += this_node_count;
6389 /* Clear the entire array first if there are any missing
6390 elements, or if the incidence of zero elements is >=
6391 75%. */
6392 if (! need_to_clear
6393 && (count < maxelt - minelt + 1
6394 || 4 * zero_count >= 3 * count))
6395 need_to_clear = 1;
6398 if (need_to_clear && size > 0)
6400 if (REG_P (target))
6401 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6402 else
6403 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6404 cleared = 1;
6407 if (!cleared && REG_P (target))
6408 /* Inform later passes that the old value is dead. */
6409 emit_clobber (target);
6411 /* Store each element of the constructor into the
6412 corresponding element of TARGET, determined by counting the
6413 elements. */
6414 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6416 machine_mode mode;
6417 HOST_WIDE_INT bitsize;
6418 HOST_WIDE_INT bitpos;
6419 rtx xtarget = target;
6421 if (cleared && initializer_zerop (value))
6422 continue;
6424 mode = TYPE_MODE (elttype);
6425 if (mode == BLKmode)
6426 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6427 ? tree_to_uhwi (TYPE_SIZE (elttype))
6428 : -1);
6429 else
6430 bitsize = GET_MODE_BITSIZE (mode);
6432 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6434 tree lo_index = TREE_OPERAND (index, 0);
6435 tree hi_index = TREE_OPERAND (index, 1);
6436 rtx index_r, pos_rtx;
6437 HOST_WIDE_INT lo, hi, count;
6438 tree position;
6440 /* If the range is constant and "small", unroll the loop. */
6441 if (const_bounds_p
6442 && tree_fits_shwi_p (lo_index)
6443 && tree_fits_shwi_p (hi_index)
6444 && (lo = tree_to_shwi (lo_index),
6445 hi = tree_to_shwi (hi_index),
6446 count = hi - lo + 1,
6447 (!MEM_P (target)
6448 || count <= 2
6449 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6450 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6451 <= 40 * 8)))))
6453 lo -= minelt; hi -= minelt;
6454 for (; lo <= hi; lo++)
6456 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6458 if (MEM_P (target)
6459 && !MEM_KEEP_ALIAS_SET_P (target)
6460 && TREE_CODE (type) == ARRAY_TYPE
6461 && TYPE_NONALIASED_COMPONENT (type))
6463 target = copy_rtx (target);
6464 MEM_KEEP_ALIAS_SET_P (target) = 1;
6467 store_constructor_field
6468 (target, bitsize, bitpos, mode, value, cleared,
6469 get_alias_set (elttype), reverse);
6472 else
6474 rtx_code_label *loop_start = gen_label_rtx ();
6475 rtx_code_label *loop_end = gen_label_rtx ();
6476 tree exit_cond;
6478 expand_normal (hi_index);
6480 index = build_decl (EXPR_LOCATION (exp),
6481 VAR_DECL, NULL_TREE, domain);
6482 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6483 SET_DECL_RTL (index, index_r);
6484 store_expr (lo_index, index_r, 0, false, reverse);
6486 /* Build the head of the loop. */
6487 do_pending_stack_adjust ();
6488 emit_label (loop_start);
6490 /* Assign value to element index. */
6491 position =
6492 fold_convert (ssizetype,
6493 fold_build2 (MINUS_EXPR,
6494 TREE_TYPE (index),
6495 index,
6496 TYPE_MIN_VALUE (domain)));
6498 position =
6499 size_binop (MULT_EXPR, position,
6500 fold_convert (ssizetype,
6501 TYPE_SIZE_UNIT (elttype)));
6503 pos_rtx = expand_normal (position);
6504 xtarget = offset_address (target, pos_rtx,
6505 highest_pow2_factor (position));
6506 xtarget = adjust_address (xtarget, mode, 0);
6507 if (TREE_CODE (value) == CONSTRUCTOR)
6508 store_constructor (value, xtarget, cleared,
6509 bitsize / BITS_PER_UNIT, reverse);
6510 else
6511 store_expr (value, xtarget, 0, false, reverse);
6513 /* Generate a conditional jump to exit the loop. */
6514 exit_cond = build2 (LT_EXPR, integer_type_node,
6515 index, hi_index);
6516 jumpif (exit_cond, loop_end, -1);
6518 /* Update the loop counter, and jump to the head of
6519 the loop. */
6520 expand_assignment (index,
6521 build2 (PLUS_EXPR, TREE_TYPE (index),
6522 index, integer_one_node),
6523 false);
6525 emit_jump (loop_start);
6527 /* Build the end of the loop. */
6528 emit_label (loop_end);
6531 else if ((index != 0 && ! tree_fits_shwi_p (index))
6532 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6534 tree position;
6536 if (index == 0)
6537 index = ssize_int (1);
6539 if (minelt)
6540 index = fold_convert (ssizetype,
6541 fold_build2 (MINUS_EXPR,
6542 TREE_TYPE (index),
6543 index,
6544 TYPE_MIN_VALUE (domain)));
6546 position =
6547 size_binop (MULT_EXPR, index,
6548 fold_convert (ssizetype,
6549 TYPE_SIZE_UNIT (elttype)));
6550 xtarget = offset_address (target,
6551 expand_normal (position),
6552 highest_pow2_factor (position));
6553 xtarget = adjust_address (xtarget, mode, 0);
6554 store_expr (value, xtarget, 0, false, reverse);
6556 else
6558 if (index != 0)
6559 bitpos = ((tree_to_shwi (index) - minelt)
6560 * tree_to_uhwi (TYPE_SIZE (elttype)));
6561 else
6562 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6564 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6565 && TREE_CODE (type) == ARRAY_TYPE
6566 && TYPE_NONALIASED_COMPONENT (type))
6568 target = copy_rtx (target);
6569 MEM_KEEP_ALIAS_SET_P (target) = 1;
6571 store_constructor_field (target, bitsize, bitpos, mode, value,
6572 cleared, get_alias_set (elttype),
6573 reverse);
6576 break;
6579 case VECTOR_TYPE:
6581 unsigned HOST_WIDE_INT idx;
6582 constructor_elt *ce;
6583 int i;
6584 int need_to_clear;
6585 int icode = CODE_FOR_nothing;
6586 tree elttype = TREE_TYPE (type);
6587 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6588 machine_mode eltmode = TYPE_MODE (elttype);
6589 HOST_WIDE_INT bitsize;
6590 HOST_WIDE_INT bitpos;
6591 rtvec vector = NULL;
6592 unsigned n_elts;
6593 alias_set_type alias;
6595 gcc_assert (eltmode != BLKmode);
6597 n_elts = TYPE_VECTOR_SUBPARTS (type);
6598 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6600 machine_mode mode = GET_MODE (target);
6602 icode = (int) optab_handler (vec_init_optab, mode);
6603 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6604 if (icode != CODE_FOR_nothing)
6606 tree value;
6608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6609 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6611 icode = CODE_FOR_nothing;
6612 break;
6615 if (icode != CODE_FOR_nothing)
6617 unsigned int i;
6619 vector = rtvec_alloc (n_elts);
6620 for (i = 0; i < n_elts; i++)
6621 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6625 /* If the constructor has fewer elements than the vector,
6626 clear the whole array first. Similarly if this is static
6627 constructor of a non-BLKmode object. */
6628 if (cleared)
6629 need_to_clear = 0;
6630 else if (REG_P (target) && TREE_STATIC (exp))
6631 need_to_clear = 1;
6632 else
6634 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6635 tree value;
6637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6639 int n_elts_here = tree_to_uhwi
6640 (int_const_binop (TRUNC_DIV_EXPR,
6641 TYPE_SIZE (TREE_TYPE (value)),
6642 TYPE_SIZE (elttype)));
6644 count += n_elts_here;
6645 if (mostly_zeros_p (value))
6646 zero_count += n_elts_here;
6649 /* Clear the entire vector first if there are any missing elements,
6650 or if the incidence of zero elements is >= 75%. */
6651 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6654 if (need_to_clear && size > 0 && !vector)
6656 if (REG_P (target))
6657 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6658 else
6659 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6660 cleared = 1;
6663 /* Inform later passes that the old value is dead. */
6664 if (!cleared && !vector && REG_P (target))
6665 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6667 if (MEM_P (target))
6668 alias = MEM_ALIAS_SET (target);
6669 else
6670 alias = get_alias_set (elttype);
6672 /* Store each element of the constructor into the corresponding
6673 element of TARGET, determined by counting the elements. */
6674 for (idx = 0, i = 0;
6675 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6676 idx++, i += bitsize / elt_size)
6678 HOST_WIDE_INT eltpos;
6679 tree value = ce->value;
6681 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6682 if (cleared && initializer_zerop (value))
6683 continue;
6685 if (ce->index)
6686 eltpos = tree_to_uhwi (ce->index);
6687 else
6688 eltpos = i;
6690 if (vector)
6692 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6693 elements. */
6694 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6695 RTVEC_ELT (vector, eltpos)
6696 = expand_normal (value);
6698 else
6700 machine_mode value_mode =
6701 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6702 ? TYPE_MODE (TREE_TYPE (value))
6703 : eltmode;
6704 bitpos = eltpos * elt_size;
6705 store_constructor_field (target, bitsize, bitpos, value_mode,
6706 value, cleared, alias, reverse);
6710 if (vector)
6711 emit_insn (GEN_FCN (icode)
6712 (target,
6713 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6714 break;
6717 default:
6718 gcc_unreachable ();
6722 /* Store the value of EXP (an expression tree)
6723 into a subfield of TARGET which has mode MODE and occupies
6724 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6725 If MODE is VOIDmode, it means that we are storing into a bit-field.
6727 BITREGION_START is bitpos of the first bitfield in this region.
6728 BITREGION_END is the bitpos of the ending bitfield in this region.
6729 These two fields are 0, if the C++ memory model does not apply,
6730 or we are not interested in keeping track of bitfield regions.
6732 Always return const0_rtx unless we have something particular to
6733 return.
6735 ALIAS_SET is the alias set for the destination. This value will
6736 (in general) be different from that for TARGET, since TARGET is a
6737 reference to the containing structure.
6739 If NONTEMPORAL is true, try generating a nontemporal store.
6741 If REVERSE is true, the store is to be done in reverse order. */
6743 static rtx
6744 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6745 unsigned HOST_WIDE_INT bitregion_start,
6746 unsigned HOST_WIDE_INT bitregion_end,
6747 machine_mode mode, tree exp,
6748 alias_set_type alias_set, bool nontemporal, bool reverse)
6750 if (TREE_CODE (exp) == ERROR_MARK)
6751 return const0_rtx;
6753 /* If we have nothing to store, do nothing unless the expression has
6754 side-effects. */
6755 if (bitsize == 0)
6756 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6758 if (GET_CODE (target) == CONCAT)
6760 /* We're storing into a struct containing a single __complex. */
6762 gcc_assert (!bitpos);
6763 return store_expr (exp, target, 0, nontemporal, reverse);
6766 /* If the structure is in a register or if the component
6767 is a bit field, we cannot use addressing to access it.
6768 Use bit-field techniques or SUBREG to store in it. */
6770 if (mode == VOIDmode
6771 || (mode != BLKmode && ! direct_store[(int) mode]
6772 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6773 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6774 || REG_P (target)
6775 || GET_CODE (target) == SUBREG
6776 /* If the field isn't aligned enough to store as an ordinary memref,
6777 store it as a bit field. */
6778 || (mode != BLKmode
6779 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6780 || bitpos % GET_MODE_ALIGNMENT (mode))
6781 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6782 || (bitpos % BITS_PER_UNIT != 0)))
6783 || (bitsize >= 0 && mode != BLKmode
6784 && GET_MODE_BITSIZE (mode) > bitsize)
6785 /* If the RHS and field are a constant size and the size of the
6786 RHS isn't the same size as the bitfield, we must use bitfield
6787 operations. */
6788 || (bitsize >= 0
6789 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6790 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6791 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6792 we will handle specially below. */
6793 && !(TREE_CODE (exp) == CONSTRUCTOR
6794 && bitsize % BITS_PER_UNIT == 0)
6795 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6796 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6797 includes some extra padding. store_expr / expand_expr will in
6798 that case call get_inner_reference that will have the bitsize
6799 we check here and thus the block move will not clobber the
6800 padding that shouldn't be clobbered. In the future we could
6801 replace the TREE_ADDRESSABLE check with a check that
6802 get_base_address needs to live in memory. */
6803 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6804 || TREE_CODE (exp) != COMPONENT_REF
6805 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6806 || (bitsize % BITS_PER_UNIT != 0)
6807 || (bitpos % BITS_PER_UNIT != 0)
6808 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6809 != 0)))
6810 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6811 decl we must use bitfield operations. */
6812 || (bitsize >= 0
6813 && TREE_CODE (exp) == MEM_REF
6814 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6815 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6816 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6817 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6819 rtx temp;
6820 gimple *nop_def;
6822 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6823 implies a mask operation. If the precision is the same size as
6824 the field we're storing into, that mask is redundant. This is
6825 particularly common with bit field assignments generated by the
6826 C front end. */
6827 nop_def = get_def_for_expr (exp, NOP_EXPR);
6828 if (nop_def)
6830 tree type = TREE_TYPE (exp);
6831 if (INTEGRAL_TYPE_P (type)
6832 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6833 && bitsize == TYPE_PRECISION (type))
6835 tree op = gimple_assign_rhs1 (nop_def);
6836 type = TREE_TYPE (op);
6837 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6838 exp = op;
6842 temp = expand_normal (exp);
6844 /* If the value has a record type and an integral mode then, if BITSIZE
6845 is narrower than this mode and this is for big-endian data, we must
6846 first put the value into the low-order bits. Moreover, the field may
6847 be not aligned on a byte boundary; in this case, if it has reverse
6848 storage order, it needs to be accessed as a scalar field with reverse
6849 storage order and we must first put the value into target order. */
6850 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
6851 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6853 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6855 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6857 if (reverse)
6858 temp = flip_storage_order (GET_MODE (temp), temp);
6860 if (bitsize < size
6861 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6862 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6863 size - bitsize, NULL_RTX, 1);
6866 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6867 if (mode != VOIDmode && mode != BLKmode
6868 && mode != TYPE_MODE (TREE_TYPE (exp)))
6869 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6871 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6872 are both BLKmode, both must be in memory and BITPOS must be aligned
6873 on a byte boundary. If so, we simply do a block copy. Likewise for
6874 a BLKmode-like TARGET. */
6875 if (GET_CODE (temp) != PARALLEL
6876 && GET_MODE (temp) == BLKmode
6877 && (GET_MODE (target) == BLKmode
6878 || (MEM_P (target)
6879 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6880 && (bitpos % BITS_PER_UNIT) == 0
6881 && (bitsize % BITS_PER_UNIT) == 0)))
6883 gcc_assert (MEM_P (target) && MEM_P (temp)
6884 && (bitpos % BITS_PER_UNIT) == 0);
6886 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6887 emit_block_move (target, temp,
6888 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6889 / BITS_PER_UNIT),
6890 BLOCK_OP_NORMAL);
6892 return const0_rtx;
6895 /* Handle calls that return values in multiple non-contiguous locations.
6896 The Irix 6 ABI has examples of this. */
6897 if (GET_CODE (temp) == PARALLEL)
6899 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6900 rtx temp_target;
6901 if (mode == BLKmode || mode == VOIDmode)
6902 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6903 temp_target = gen_reg_rtx (mode);
6904 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6905 temp = temp_target;
6907 else if (mode == BLKmode)
6909 /* Handle calls that return BLKmode values in registers. */
6910 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6912 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6913 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6914 temp = temp_target;
6916 else
6918 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6919 rtx temp_target;
6920 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6921 temp_target = gen_reg_rtx (mode);
6922 temp_target
6923 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6924 temp_target, mode, mode, false);
6925 temp = temp_target;
6929 /* Store the value in the bitfield. */
6930 store_bit_field (target, bitsize, bitpos,
6931 bitregion_start, bitregion_end,
6932 mode, temp, reverse);
6934 return const0_rtx;
6936 else
6938 /* Now build a reference to just the desired component. */
6939 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6941 if (to_rtx == target)
6942 to_rtx = copy_rtx (to_rtx);
6944 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6945 set_mem_alias_set (to_rtx, alias_set);
6947 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6948 into a target smaller than its type; handle that case now. */
6949 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6951 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6952 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6953 return to_rtx;
6956 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6960 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6961 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6962 codes and find the ultimate containing object, which we return.
6964 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6965 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6966 storage order of the field.
6967 If the position of the field is variable, we store a tree
6968 giving the variable offset (in units) in *POFFSET.
6969 This offset is in addition to the bit position.
6970 If the position is not variable, we store 0 in *POFFSET.
6972 If any of the extraction expressions is volatile,
6973 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6975 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6976 Otherwise, it is a mode that can be used to access the field.
6978 If the field describes a variable-sized object, *PMODE is set to
6979 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6980 this case, but the address of the object can be found. */
6982 tree
6983 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6984 HOST_WIDE_INT *pbitpos, tree *poffset,
6985 machine_mode *pmode, int *punsignedp,
6986 int *preversep, int *pvolatilep)
6988 tree size_tree = 0;
6989 machine_mode mode = VOIDmode;
6990 bool blkmode_bitfield = false;
6991 tree offset = size_zero_node;
6992 offset_int bit_offset = 0;
6994 /* First get the mode, signedness, storage order and size. We do this from
6995 just the outermost expression. */
6996 *pbitsize = -1;
6997 if (TREE_CODE (exp) == COMPONENT_REF)
6999 tree field = TREE_OPERAND (exp, 1);
7000 size_tree = DECL_SIZE (field);
7001 if (flag_strict_volatile_bitfields > 0
7002 && TREE_THIS_VOLATILE (exp)
7003 && DECL_BIT_FIELD_TYPE (field)
7004 && DECL_MODE (field) != BLKmode)
7005 /* Volatile bitfields should be accessed in the mode of the
7006 field's type, not the mode computed based on the bit
7007 size. */
7008 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7009 else if (!DECL_BIT_FIELD (field))
7010 mode = DECL_MODE (field);
7011 else if (DECL_MODE (field) == BLKmode)
7012 blkmode_bitfield = true;
7014 *punsignedp = DECL_UNSIGNED (field);
7016 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7018 size_tree = TREE_OPERAND (exp, 1);
7019 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7020 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7022 /* For vector types, with the correct size of access, use the mode of
7023 inner type. */
7024 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7025 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7026 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7027 mode = TYPE_MODE (TREE_TYPE (exp));
7029 else
7031 mode = TYPE_MODE (TREE_TYPE (exp));
7032 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7034 if (mode == BLKmode)
7035 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7036 else
7037 *pbitsize = GET_MODE_BITSIZE (mode);
7040 if (size_tree != 0)
7042 if (! tree_fits_uhwi_p (size_tree))
7043 mode = BLKmode, *pbitsize = -1;
7044 else
7045 *pbitsize = tree_to_uhwi (size_tree);
7048 *preversep = reverse_storage_order_for_component_p (exp);
7050 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7051 and find the ultimate containing object. */
7052 while (1)
7054 switch (TREE_CODE (exp))
7056 case BIT_FIELD_REF:
7057 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7058 break;
7060 case COMPONENT_REF:
7062 tree field = TREE_OPERAND (exp, 1);
7063 tree this_offset = component_ref_field_offset (exp);
7065 /* If this field hasn't been filled in yet, don't go past it.
7066 This should only happen when folding expressions made during
7067 type construction. */
7068 if (this_offset == 0)
7069 break;
7071 offset = size_binop (PLUS_EXPR, offset, this_offset);
7072 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7074 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7076 break;
7078 case ARRAY_REF:
7079 case ARRAY_RANGE_REF:
7081 tree index = TREE_OPERAND (exp, 1);
7082 tree low_bound = array_ref_low_bound (exp);
7083 tree unit_size = array_ref_element_size (exp);
7085 /* We assume all arrays have sizes that are a multiple of a byte.
7086 First subtract the lower bound, if any, in the type of the
7087 index, then convert to sizetype and multiply by the size of
7088 the array element. */
7089 if (! integer_zerop (low_bound))
7090 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7091 index, low_bound);
7093 offset = size_binop (PLUS_EXPR, offset,
7094 size_binop (MULT_EXPR,
7095 fold_convert (sizetype, index),
7096 unit_size));
7098 break;
7100 case REALPART_EXPR:
7101 break;
7103 case IMAGPART_EXPR:
7104 bit_offset += *pbitsize;
7105 break;
7107 case VIEW_CONVERT_EXPR:
7108 break;
7110 case MEM_REF:
7111 /* Hand back the decl for MEM[&decl, off]. */
7112 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7114 tree off = TREE_OPERAND (exp, 1);
7115 if (!integer_zerop (off))
7117 offset_int boff, coff = mem_ref_offset (exp);
7118 boff = coff << LOG2_BITS_PER_UNIT;
7119 bit_offset += boff;
7121 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7123 goto done;
7125 default:
7126 goto done;
7129 /* If any reference in the chain is volatile, the effect is volatile. */
7130 if (TREE_THIS_VOLATILE (exp))
7131 *pvolatilep = 1;
7133 exp = TREE_OPERAND (exp, 0);
7135 done:
7137 /* If OFFSET is constant, see if we can return the whole thing as a
7138 constant bit position. Make sure to handle overflow during
7139 this conversion. */
7140 if (TREE_CODE (offset) == INTEGER_CST)
7142 offset_int tem = wi::sext (wi::to_offset (offset),
7143 TYPE_PRECISION (sizetype));
7144 tem <<= LOG2_BITS_PER_UNIT;
7145 tem += bit_offset;
7146 if (wi::fits_shwi_p (tem))
7148 *pbitpos = tem.to_shwi ();
7149 *poffset = offset = NULL_TREE;
7153 /* Otherwise, split it up. */
7154 if (offset)
7156 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7157 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7159 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7160 offset_int tem = bit_offset.and_not (mask);
7161 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7162 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7163 bit_offset -= tem;
7164 tem >>= LOG2_BITS_PER_UNIT;
7165 offset = size_binop (PLUS_EXPR, offset,
7166 wide_int_to_tree (sizetype, tem));
7169 *pbitpos = bit_offset.to_shwi ();
7170 *poffset = offset;
7173 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7174 if (mode == VOIDmode
7175 && blkmode_bitfield
7176 && (*pbitpos % BITS_PER_UNIT) == 0
7177 && (*pbitsize % BITS_PER_UNIT) == 0)
7178 *pmode = BLKmode;
7179 else
7180 *pmode = mode;
7182 return exp;
7185 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7187 static unsigned HOST_WIDE_INT
7188 target_align (const_tree target)
7190 /* We might have a chain of nested references with intermediate misaligning
7191 bitfields components, so need to recurse to find out. */
7193 unsigned HOST_WIDE_INT this_align, outer_align;
7195 switch (TREE_CODE (target))
7197 case BIT_FIELD_REF:
7198 return 1;
7200 case COMPONENT_REF:
7201 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7202 outer_align = target_align (TREE_OPERAND (target, 0));
7203 return MIN (this_align, outer_align);
7205 case ARRAY_REF:
7206 case ARRAY_RANGE_REF:
7207 this_align = TYPE_ALIGN (TREE_TYPE (target));
7208 outer_align = target_align (TREE_OPERAND (target, 0));
7209 return MIN (this_align, outer_align);
7211 CASE_CONVERT:
7212 case NON_LVALUE_EXPR:
7213 case VIEW_CONVERT_EXPR:
7214 this_align = TYPE_ALIGN (TREE_TYPE (target));
7215 outer_align = target_align (TREE_OPERAND (target, 0));
7216 return MAX (this_align, outer_align);
7218 default:
7219 return TYPE_ALIGN (TREE_TYPE (target));
7224 /* Given an rtx VALUE that may contain additions and multiplications, return
7225 an equivalent value that just refers to a register, memory, or constant.
7226 This is done by generating instructions to perform the arithmetic and
7227 returning a pseudo-register containing the value.
7229 The returned value may be a REG, SUBREG, MEM or constant. */
7232 force_operand (rtx value, rtx target)
7234 rtx op1, op2;
7235 /* Use subtarget as the target for operand 0 of a binary operation. */
7236 rtx subtarget = get_subtarget (target);
7237 enum rtx_code code = GET_CODE (value);
7239 /* Check for subreg applied to an expression produced by loop optimizer. */
7240 if (code == SUBREG
7241 && !REG_P (SUBREG_REG (value))
7242 && !MEM_P (SUBREG_REG (value)))
7244 value
7245 = simplify_gen_subreg (GET_MODE (value),
7246 force_reg (GET_MODE (SUBREG_REG (value)),
7247 force_operand (SUBREG_REG (value),
7248 NULL_RTX)),
7249 GET_MODE (SUBREG_REG (value)),
7250 SUBREG_BYTE (value));
7251 code = GET_CODE (value);
7254 /* Check for a PIC address load. */
7255 if ((code == PLUS || code == MINUS)
7256 && XEXP (value, 0) == pic_offset_table_rtx
7257 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7258 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7259 || GET_CODE (XEXP (value, 1)) == CONST))
7261 if (!subtarget)
7262 subtarget = gen_reg_rtx (GET_MODE (value));
7263 emit_move_insn (subtarget, value);
7264 return subtarget;
7267 if (ARITHMETIC_P (value))
7269 op2 = XEXP (value, 1);
7270 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7271 subtarget = 0;
7272 if (code == MINUS && CONST_INT_P (op2))
7274 code = PLUS;
7275 op2 = negate_rtx (GET_MODE (value), op2);
7278 /* Check for an addition with OP2 a constant integer and our first
7279 operand a PLUS of a virtual register and something else. In that
7280 case, we want to emit the sum of the virtual register and the
7281 constant first and then add the other value. This allows virtual
7282 register instantiation to simply modify the constant rather than
7283 creating another one around this addition. */
7284 if (code == PLUS && CONST_INT_P (op2)
7285 && GET_CODE (XEXP (value, 0)) == PLUS
7286 && REG_P (XEXP (XEXP (value, 0), 0))
7287 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7288 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7290 rtx temp = expand_simple_binop (GET_MODE (value), code,
7291 XEXP (XEXP (value, 0), 0), op2,
7292 subtarget, 0, OPTAB_LIB_WIDEN);
7293 return expand_simple_binop (GET_MODE (value), code, temp,
7294 force_operand (XEXP (XEXP (value,
7295 0), 1), 0),
7296 target, 0, OPTAB_LIB_WIDEN);
7299 op1 = force_operand (XEXP (value, 0), subtarget);
7300 op2 = force_operand (op2, NULL_RTX);
7301 switch (code)
7303 case MULT:
7304 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7305 case DIV:
7306 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7307 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7308 target, 1, OPTAB_LIB_WIDEN);
7309 else
7310 return expand_divmod (0,
7311 FLOAT_MODE_P (GET_MODE (value))
7312 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7313 GET_MODE (value), op1, op2, target, 0);
7314 case MOD:
7315 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7316 target, 0);
7317 case UDIV:
7318 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7319 target, 1);
7320 case UMOD:
7321 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7322 target, 1);
7323 case ASHIFTRT:
7324 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7325 target, 0, OPTAB_LIB_WIDEN);
7326 default:
7327 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7328 target, 1, OPTAB_LIB_WIDEN);
7331 if (UNARY_P (value))
7333 if (!target)
7334 target = gen_reg_rtx (GET_MODE (value));
7335 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7336 switch (code)
7338 case ZERO_EXTEND:
7339 case SIGN_EXTEND:
7340 case TRUNCATE:
7341 case FLOAT_EXTEND:
7342 case FLOAT_TRUNCATE:
7343 convert_move (target, op1, code == ZERO_EXTEND);
7344 return target;
7346 case FIX:
7347 case UNSIGNED_FIX:
7348 expand_fix (target, op1, code == UNSIGNED_FIX);
7349 return target;
7351 case FLOAT:
7352 case UNSIGNED_FLOAT:
7353 expand_float (target, op1, code == UNSIGNED_FLOAT);
7354 return target;
7356 default:
7357 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7361 #ifdef INSN_SCHEDULING
7362 /* On machines that have insn scheduling, we want all memory reference to be
7363 explicit, so we need to deal with such paradoxical SUBREGs. */
7364 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7365 value
7366 = simplify_gen_subreg (GET_MODE (value),
7367 force_reg (GET_MODE (SUBREG_REG (value)),
7368 force_operand (SUBREG_REG (value),
7369 NULL_RTX)),
7370 GET_MODE (SUBREG_REG (value)),
7371 SUBREG_BYTE (value));
7372 #endif
7374 return value;
7377 /* Subroutine of expand_expr: return nonzero iff there is no way that
7378 EXP can reference X, which is being modified. TOP_P is nonzero if this
7379 call is going to be used to determine whether we need a temporary
7380 for EXP, as opposed to a recursive call to this function.
7382 It is always safe for this routine to return zero since it merely
7383 searches for optimization opportunities. */
7386 safe_from_p (const_rtx x, tree exp, int top_p)
7388 rtx exp_rtl = 0;
7389 int i, nops;
7391 if (x == 0
7392 /* If EXP has varying size, we MUST use a target since we currently
7393 have no way of allocating temporaries of variable size
7394 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7395 So we assume here that something at a higher level has prevented a
7396 clash. This is somewhat bogus, but the best we can do. Only
7397 do this when X is BLKmode and when we are at the top level. */
7398 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7399 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7400 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7401 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7402 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7403 != INTEGER_CST)
7404 && GET_MODE (x) == BLKmode)
7405 /* If X is in the outgoing argument area, it is always safe. */
7406 || (MEM_P (x)
7407 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7408 || (GET_CODE (XEXP (x, 0)) == PLUS
7409 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7410 return 1;
7412 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7413 find the underlying pseudo. */
7414 if (GET_CODE (x) == SUBREG)
7416 x = SUBREG_REG (x);
7417 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7418 return 0;
7421 /* Now look at our tree code and possibly recurse. */
7422 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7424 case tcc_declaration:
7425 exp_rtl = DECL_RTL_IF_SET (exp);
7426 break;
7428 case tcc_constant:
7429 return 1;
7431 case tcc_exceptional:
7432 if (TREE_CODE (exp) == TREE_LIST)
7434 while (1)
7436 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7437 return 0;
7438 exp = TREE_CHAIN (exp);
7439 if (!exp)
7440 return 1;
7441 if (TREE_CODE (exp) != TREE_LIST)
7442 return safe_from_p (x, exp, 0);
7445 else if (TREE_CODE (exp) == CONSTRUCTOR)
7447 constructor_elt *ce;
7448 unsigned HOST_WIDE_INT idx;
7450 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7451 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7452 || !safe_from_p (x, ce->value, 0))
7453 return 0;
7454 return 1;
7456 else if (TREE_CODE (exp) == ERROR_MARK)
7457 return 1; /* An already-visited SAVE_EXPR? */
7458 else
7459 return 0;
7461 case tcc_statement:
7462 /* The only case we look at here is the DECL_INITIAL inside a
7463 DECL_EXPR. */
7464 return (TREE_CODE (exp) != DECL_EXPR
7465 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7466 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7467 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7469 case tcc_binary:
7470 case tcc_comparison:
7471 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7472 return 0;
7473 /* Fall through. */
7475 case tcc_unary:
7476 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7478 case tcc_expression:
7479 case tcc_reference:
7480 case tcc_vl_exp:
7481 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7482 the expression. If it is set, we conflict iff we are that rtx or
7483 both are in memory. Otherwise, we check all operands of the
7484 expression recursively. */
7486 switch (TREE_CODE (exp))
7488 case ADDR_EXPR:
7489 /* If the operand is static or we are static, we can't conflict.
7490 Likewise if we don't conflict with the operand at all. */
7491 if (staticp (TREE_OPERAND (exp, 0))
7492 || TREE_STATIC (exp)
7493 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7494 return 1;
7496 /* Otherwise, the only way this can conflict is if we are taking
7497 the address of a DECL a that address if part of X, which is
7498 very rare. */
7499 exp = TREE_OPERAND (exp, 0);
7500 if (DECL_P (exp))
7502 if (!DECL_RTL_SET_P (exp)
7503 || !MEM_P (DECL_RTL (exp)))
7504 return 0;
7505 else
7506 exp_rtl = XEXP (DECL_RTL (exp), 0);
7508 break;
7510 case MEM_REF:
7511 if (MEM_P (x)
7512 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7513 get_alias_set (exp)))
7514 return 0;
7515 break;
7517 case CALL_EXPR:
7518 /* Assume that the call will clobber all hard registers and
7519 all of memory. */
7520 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7521 || MEM_P (x))
7522 return 0;
7523 break;
7525 case WITH_CLEANUP_EXPR:
7526 case CLEANUP_POINT_EXPR:
7527 /* Lowered by gimplify.c. */
7528 gcc_unreachable ();
7530 case SAVE_EXPR:
7531 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7533 default:
7534 break;
7537 /* If we have an rtx, we do not need to scan our operands. */
7538 if (exp_rtl)
7539 break;
7541 nops = TREE_OPERAND_LENGTH (exp);
7542 for (i = 0; i < nops; i++)
7543 if (TREE_OPERAND (exp, i) != 0
7544 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7545 return 0;
7547 break;
7549 case tcc_type:
7550 /* Should never get a type here. */
7551 gcc_unreachable ();
7554 /* If we have an rtl, find any enclosed object. Then see if we conflict
7555 with it. */
7556 if (exp_rtl)
7558 if (GET_CODE (exp_rtl) == SUBREG)
7560 exp_rtl = SUBREG_REG (exp_rtl);
7561 if (REG_P (exp_rtl)
7562 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7563 return 0;
7566 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7567 are memory and they conflict. */
7568 return ! (rtx_equal_p (x, exp_rtl)
7569 || (MEM_P (x) && MEM_P (exp_rtl)
7570 && true_dependence (exp_rtl, VOIDmode, x)));
7573 /* If we reach here, it is safe. */
7574 return 1;
7578 /* Return the highest power of two that EXP is known to be a multiple of.
7579 This is used in updating alignment of MEMs in array references. */
7581 unsigned HOST_WIDE_INT
7582 highest_pow2_factor (const_tree exp)
7584 unsigned HOST_WIDE_INT ret;
7585 int trailing_zeros = tree_ctz (exp);
7586 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7587 return BIGGEST_ALIGNMENT;
7588 ret = HOST_WIDE_INT_1U << trailing_zeros;
7589 if (ret > BIGGEST_ALIGNMENT)
7590 return BIGGEST_ALIGNMENT;
7591 return ret;
7594 /* Similar, except that the alignment requirements of TARGET are
7595 taken into account. Assume it is at least as aligned as its
7596 type, unless it is a COMPONENT_REF in which case the layout of
7597 the structure gives the alignment. */
7599 static unsigned HOST_WIDE_INT
7600 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7602 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7603 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7605 return MAX (factor, talign);
7608 /* Convert the tree comparison code TCODE to the rtl one where the
7609 signedness is UNSIGNEDP. */
7611 static enum rtx_code
7612 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7614 enum rtx_code code;
7615 switch (tcode)
7617 case EQ_EXPR:
7618 code = EQ;
7619 break;
7620 case NE_EXPR:
7621 code = NE;
7622 break;
7623 case LT_EXPR:
7624 code = unsignedp ? LTU : LT;
7625 break;
7626 case LE_EXPR:
7627 code = unsignedp ? LEU : LE;
7628 break;
7629 case GT_EXPR:
7630 code = unsignedp ? GTU : GT;
7631 break;
7632 case GE_EXPR:
7633 code = unsignedp ? GEU : GE;
7634 break;
7635 case UNORDERED_EXPR:
7636 code = UNORDERED;
7637 break;
7638 case ORDERED_EXPR:
7639 code = ORDERED;
7640 break;
7641 case UNLT_EXPR:
7642 code = UNLT;
7643 break;
7644 case UNLE_EXPR:
7645 code = UNLE;
7646 break;
7647 case UNGT_EXPR:
7648 code = UNGT;
7649 break;
7650 case UNGE_EXPR:
7651 code = UNGE;
7652 break;
7653 case UNEQ_EXPR:
7654 code = UNEQ;
7655 break;
7656 case LTGT_EXPR:
7657 code = LTGT;
7658 break;
7660 default:
7661 gcc_unreachable ();
7663 return code;
7666 /* Subroutine of expand_expr. Expand the two operands of a binary
7667 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7668 The value may be stored in TARGET if TARGET is nonzero. The
7669 MODIFIER argument is as documented by expand_expr. */
7671 void
7672 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7673 enum expand_modifier modifier)
7675 if (! safe_from_p (target, exp1, 1))
7676 target = 0;
7677 if (operand_equal_p (exp0, exp1, 0))
7679 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7680 *op1 = copy_rtx (*op0);
7682 else
7684 /* If we need to preserve evaluation order, copy exp0 into its own
7685 temporary variable so that it can't be clobbered by exp1. */
7686 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7687 exp0 = save_expr (exp0);
7688 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7689 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7694 /* Return a MEM that contains constant EXP. DEFER is as for
7695 output_constant_def and MODIFIER is as for expand_expr. */
7697 static rtx
7698 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7700 rtx mem;
7702 mem = output_constant_def (exp, defer);
7703 if (modifier != EXPAND_INITIALIZER)
7704 mem = use_anchored_address (mem);
7705 return mem;
7708 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7709 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7711 static rtx
7712 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7713 enum expand_modifier modifier, addr_space_t as)
7715 rtx result, subtarget;
7716 tree inner, offset;
7717 HOST_WIDE_INT bitsize, bitpos;
7718 int unsignedp, reversep, volatilep = 0;
7719 machine_mode mode1;
7721 /* If we are taking the address of a constant and are at the top level,
7722 we have to use output_constant_def since we can't call force_const_mem
7723 at top level. */
7724 /* ??? This should be considered a front-end bug. We should not be
7725 generating ADDR_EXPR of something that isn't an LVALUE. The only
7726 exception here is STRING_CST. */
7727 if (CONSTANT_CLASS_P (exp))
7729 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7730 if (modifier < EXPAND_SUM)
7731 result = force_operand (result, target);
7732 return result;
7735 /* Everything must be something allowed by is_gimple_addressable. */
7736 switch (TREE_CODE (exp))
7738 case INDIRECT_REF:
7739 /* This case will happen via recursion for &a->b. */
7740 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7742 case MEM_REF:
7744 tree tem = TREE_OPERAND (exp, 0);
7745 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7746 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7747 return expand_expr (tem, target, tmode, modifier);
7750 case CONST_DECL:
7751 /* Expand the initializer like constants above. */
7752 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7753 0, modifier), 0);
7754 if (modifier < EXPAND_SUM)
7755 result = force_operand (result, target);
7756 return result;
7758 case REALPART_EXPR:
7759 /* The real part of the complex number is always first, therefore
7760 the address is the same as the address of the parent object. */
7761 offset = 0;
7762 bitpos = 0;
7763 inner = TREE_OPERAND (exp, 0);
7764 break;
7766 case IMAGPART_EXPR:
7767 /* The imaginary part of the complex number is always second.
7768 The expression is therefore always offset by the size of the
7769 scalar type. */
7770 offset = 0;
7771 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7772 inner = TREE_OPERAND (exp, 0);
7773 break;
7775 case COMPOUND_LITERAL_EXPR:
7776 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7777 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7778 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7779 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7780 the initializers aren't gimplified. */
7781 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7782 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7783 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7784 target, tmode, modifier, as);
7785 /* FALLTHRU */
7786 default:
7787 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7788 expand_expr, as that can have various side effects; LABEL_DECLs for
7789 example, may not have their DECL_RTL set yet. Expand the rtl of
7790 CONSTRUCTORs too, which should yield a memory reference for the
7791 constructor's contents. Assume language specific tree nodes can
7792 be expanded in some interesting way. */
7793 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7794 if (DECL_P (exp)
7795 || TREE_CODE (exp) == CONSTRUCTOR
7796 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7798 result = expand_expr (exp, target, tmode,
7799 modifier == EXPAND_INITIALIZER
7800 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7802 /* If the DECL isn't in memory, then the DECL wasn't properly
7803 marked TREE_ADDRESSABLE, which will be either a front-end
7804 or a tree optimizer bug. */
7806 gcc_assert (MEM_P (result));
7807 result = XEXP (result, 0);
7809 /* ??? Is this needed anymore? */
7810 if (DECL_P (exp))
7811 TREE_USED (exp) = 1;
7813 if (modifier != EXPAND_INITIALIZER
7814 && modifier != EXPAND_CONST_ADDRESS
7815 && modifier != EXPAND_SUM)
7816 result = force_operand (result, target);
7817 return result;
7820 /* Pass FALSE as the last argument to get_inner_reference although
7821 we are expanding to RTL. The rationale is that we know how to
7822 handle "aligning nodes" here: we can just bypass them because
7823 they won't change the final object whose address will be returned
7824 (they actually exist only for that purpose). */
7825 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7826 &unsignedp, &reversep, &volatilep);
7827 break;
7830 /* We must have made progress. */
7831 gcc_assert (inner != exp);
7833 subtarget = offset || bitpos ? NULL_RTX : target;
7834 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7835 inner alignment, force the inner to be sufficiently aligned. */
7836 if (CONSTANT_CLASS_P (inner)
7837 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7839 inner = copy_node (inner);
7840 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7841 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7842 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7844 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7846 if (offset)
7848 rtx tmp;
7850 if (modifier != EXPAND_NORMAL)
7851 result = force_operand (result, NULL);
7852 tmp = expand_expr (offset, NULL_RTX, tmode,
7853 modifier == EXPAND_INITIALIZER
7854 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7856 /* expand_expr is allowed to return an object in a mode other
7857 than TMODE. If it did, we need to convert. */
7858 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7859 tmp = convert_modes (tmode, GET_MODE (tmp),
7860 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7861 result = convert_memory_address_addr_space (tmode, result, as);
7862 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7864 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7865 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7866 else
7868 subtarget = bitpos ? NULL_RTX : target;
7869 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7870 1, OPTAB_LIB_WIDEN);
7874 if (bitpos)
7876 /* Someone beforehand should have rejected taking the address
7877 of such an object. */
7878 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7880 result = convert_memory_address_addr_space (tmode, result, as);
7881 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7882 if (modifier < EXPAND_SUM)
7883 result = force_operand (result, target);
7886 return result;
7889 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7890 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7892 static rtx
7893 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7894 enum expand_modifier modifier)
7896 addr_space_t as = ADDR_SPACE_GENERIC;
7897 machine_mode address_mode = Pmode;
7898 machine_mode pointer_mode = ptr_mode;
7899 machine_mode rmode;
7900 rtx result;
7902 /* Target mode of VOIDmode says "whatever's natural". */
7903 if (tmode == VOIDmode)
7904 tmode = TYPE_MODE (TREE_TYPE (exp));
7906 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7908 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7909 address_mode = targetm.addr_space.address_mode (as);
7910 pointer_mode = targetm.addr_space.pointer_mode (as);
7913 /* We can get called with some Weird Things if the user does silliness
7914 like "(short) &a". In that case, convert_memory_address won't do
7915 the right thing, so ignore the given target mode. */
7916 if (tmode != address_mode && tmode != pointer_mode)
7917 tmode = address_mode;
7919 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7920 tmode, modifier, as);
7922 /* Despite expand_expr claims concerning ignoring TMODE when not
7923 strictly convenient, stuff breaks if we don't honor it. Note
7924 that combined with the above, we only do this for pointer modes. */
7925 rmode = GET_MODE (result);
7926 if (rmode == VOIDmode)
7927 rmode = tmode;
7928 if (rmode != tmode)
7929 result = convert_memory_address_addr_space (tmode, result, as);
7931 return result;
7934 /* Generate code for computing CONSTRUCTOR EXP.
7935 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7936 is TRUE, instead of creating a temporary variable in memory
7937 NULL is returned and the caller needs to handle it differently. */
7939 static rtx
7940 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7941 bool avoid_temp_mem)
7943 tree type = TREE_TYPE (exp);
7944 machine_mode mode = TYPE_MODE (type);
7946 /* Try to avoid creating a temporary at all. This is possible
7947 if all of the initializer is zero.
7948 FIXME: try to handle all [0..255] initializers we can handle
7949 with memset. */
7950 if (TREE_STATIC (exp)
7951 && !TREE_ADDRESSABLE (exp)
7952 && target != 0 && mode == BLKmode
7953 && all_zeros_p (exp))
7955 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7956 return target;
7959 /* All elts simple constants => refer to a constant in memory. But
7960 if this is a non-BLKmode mode, let it store a field at a time
7961 since that should make a CONST_INT, CONST_WIDE_INT or
7962 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7963 use, it is best to store directly into the target unless the type
7964 is large enough that memcpy will be used. If we are making an
7965 initializer and all operands are constant, put it in memory as
7966 well.
7968 FIXME: Avoid trying to fill vector constructors piece-meal.
7969 Output them with output_constant_def below unless we're sure
7970 they're zeros. This should go away when vector initializers
7971 are treated like VECTOR_CST instead of arrays. */
7972 if ((TREE_STATIC (exp)
7973 && ((mode == BLKmode
7974 && ! (target != 0 && safe_from_p (target, exp, 1)))
7975 || TREE_ADDRESSABLE (exp)
7976 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7977 && (! can_move_by_pieces
7978 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7979 TYPE_ALIGN (type)))
7980 && ! mostly_zeros_p (exp))))
7981 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7982 && TREE_CONSTANT (exp)))
7984 rtx constructor;
7986 if (avoid_temp_mem)
7987 return NULL_RTX;
7989 constructor = expand_expr_constant (exp, 1, modifier);
7991 if (modifier != EXPAND_CONST_ADDRESS
7992 && modifier != EXPAND_INITIALIZER
7993 && modifier != EXPAND_SUM)
7994 constructor = validize_mem (constructor);
7996 return constructor;
7999 /* Handle calls that pass values in multiple non-contiguous
8000 locations. The Irix 6 ABI has examples of this. */
8001 if (target == 0 || ! safe_from_p (target, exp, 1)
8002 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8004 if (avoid_temp_mem)
8005 return NULL_RTX;
8007 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8010 store_constructor (exp, target, 0, int_expr_size (exp), false);
8011 return target;
8015 /* expand_expr: generate code for computing expression EXP.
8016 An rtx for the computed value is returned. The value is never null.
8017 In the case of a void EXP, const0_rtx is returned.
8019 The value may be stored in TARGET if TARGET is nonzero.
8020 TARGET is just a suggestion; callers must assume that
8021 the rtx returned may not be the same as TARGET.
8023 If TARGET is CONST0_RTX, it means that the value will be ignored.
8025 If TMODE is not VOIDmode, it suggests generating the
8026 result in mode TMODE. But this is done only when convenient.
8027 Otherwise, TMODE is ignored and the value generated in its natural mode.
8028 TMODE is just a suggestion; callers must assume that
8029 the rtx returned may not have mode TMODE.
8031 Note that TARGET may have neither TMODE nor MODE. In that case, it
8032 probably will not be used.
8034 If MODIFIER is EXPAND_SUM then when EXP is an addition
8035 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8036 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8037 products as above, or REG or MEM, or constant.
8038 Ordinarily in such cases we would output mul or add instructions
8039 and then return a pseudo reg containing the sum.
8041 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8042 it also marks a label as absolutely required (it can't be dead).
8043 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8044 This is used for outputting expressions used in initializers.
8046 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8047 with a constant address even if that address is not normally legitimate.
8048 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8050 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8051 a call parameter. Such targets require special care as we haven't yet
8052 marked TARGET so that it's safe from being trashed by libcalls. We
8053 don't want to use TARGET for anything but the final result;
8054 Intermediate values must go elsewhere. Additionally, calls to
8055 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8057 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8058 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8059 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8060 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8061 recursively.
8063 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8064 In this case, we don't adjust a returned MEM rtx that wouldn't be
8065 sufficiently aligned for its mode; instead, it's up to the caller
8066 to deal with it afterwards. This is used to make sure that unaligned
8067 base objects for which out-of-bounds accesses are supported, for
8068 example record types with trailing arrays, aren't realigned behind
8069 the back of the caller.
8070 The normal operating mode is to pass FALSE for this parameter. */
8073 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8074 enum expand_modifier modifier, rtx *alt_rtl,
8075 bool inner_reference_p)
8077 rtx ret;
8079 /* Handle ERROR_MARK before anybody tries to access its type. */
8080 if (TREE_CODE (exp) == ERROR_MARK
8081 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8083 ret = CONST0_RTX (tmode);
8084 return ret ? ret : const0_rtx;
8087 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8088 inner_reference_p);
8089 return ret;
8092 /* Try to expand the conditional expression which is represented by
8093 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8094 return the rtl reg which represents the result. Otherwise return
8095 NULL_RTX. */
8097 static rtx
8098 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8099 tree treeop1 ATTRIBUTE_UNUSED,
8100 tree treeop2 ATTRIBUTE_UNUSED)
8102 rtx insn;
8103 rtx op00, op01, op1, op2;
8104 enum rtx_code comparison_code;
8105 machine_mode comparison_mode;
8106 gimple *srcstmt;
8107 rtx temp;
8108 tree type = TREE_TYPE (treeop1);
8109 int unsignedp = TYPE_UNSIGNED (type);
8110 machine_mode mode = TYPE_MODE (type);
8111 machine_mode orig_mode = mode;
8113 /* If we cannot do a conditional move on the mode, try doing it
8114 with the promoted mode. */
8115 if (!can_conditionally_move_p (mode))
8117 mode = promote_mode (type, mode, &unsignedp);
8118 if (!can_conditionally_move_p (mode))
8119 return NULL_RTX;
8120 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8122 else
8123 temp = assign_temp (type, 0, 1);
8125 start_sequence ();
8126 expand_operands (treeop1, treeop2,
8127 temp, &op1, &op2, EXPAND_NORMAL);
8129 if (TREE_CODE (treeop0) == SSA_NAME
8130 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8132 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8133 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8134 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8135 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8136 comparison_mode = TYPE_MODE (type);
8137 unsignedp = TYPE_UNSIGNED (type);
8138 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8140 else if (COMPARISON_CLASS_P (treeop0))
8142 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8143 enum tree_code cmpcode = TREE_CODE (treeop0);
8144 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8145 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8146 unsignedp = TYPE_UNSIGNED (type);
8147 comparison_mode = TYPE_MODE (type);
8148 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8150 else
8152 op00 = expand_normal (treeop0);
8153 op01 = const0_rtx;
8154 comparison_code = NE;
8155 comparison_mode = GET_MODE (op00);
8156 if (comparison_mode == VOIDmode)
8157 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8160 if (GET_MODE (op1) != mode)
8161 op1 = gen_lowpart (mode, op1);
8163 if (GET_MODE (op2) != mode)
8164 op2 = gen_lowpart (mode, op2);
8166 /* Try to emit the conditional move. */
8167 insn = emit_conditional_move (temp, comparison_code,
8168 op00, op01, comparison_mode,
8169 op1, op2, mode,
8170 unsignedp);
8172 /* If we could do the conditional move, emit the sequence,
8173 and return. */
8174 if (insn)
8176 rtx_insn *seq = get_insns ();
8177 end_sequence ();
8178 emit_insn (seq);
8179 return convert_modes (orig_mode, mode, temp, 0);
8182 /* Otherwise discard the sequence and fall back to code with
8183 branches. */
8184 end_sequence ();
8185 return NULL_RTX;
8189 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8190 enum expand_modifier modifier)
8192 rtx op0, op1, op2, temp;
8193 rtx_code_label *lab;
8194 tree type;
8195 int unsignedp;
8196 machine_mode mode;
8197 enum tree_code code = ops->code;
8198 optab this_optab;
8199 rtx subtarget, original_target;
8200 int ignore;
8201 bool reduce_bit_field;
8202 location_t loc = ops->location;
8203 tree treeop0, treeop1, treeop2;
8204 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8205 ? reduce_to_bit_field_precision ((expr), \
8206 target, \
8207 type) \
8208 : (expr))
8210 type = ops->type;
8211 mode = TYPE_MODE (type);
8212 unsignedp = TYPE_UNSIGNED (type);
8214 treeop0 = ops->op0;
8215 treeop1 = ops->op1;
8216 treeop2 = ops->op2;
8218 /* We should be called only on simple (binary or unary) expressions,
8219 exactly those that are valid in gimple expressions that aren't
8220 GIMPLE_SINGLE_RHS (or invalid). */
8221 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8222 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8223 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8225 ignore = (target == const0_rtx
8226 || ((CONVERT_EXPR_CODE_P (code)
8227 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8228 && TREE_CODE (type) == VOID_TYPE));
8230 /* We should be called only if we need the result. */
8231 gcc_assert (!ignore);
8233 /* An operation in what may be a bit-field type needs the
8234 result to be reduced to the precision of the bit-field type,
8235 which is narrower than that of the type's mode. */
8236 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8237 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8239 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8240 target = 0;
8242 /* Use subtarget as the target for operand 0 of a binary operation. */
8243 subtarget = get_subtarget (target);
8244 original_target = target;
8246 switch (code)
8248 case NON_LVALUE_EXPR:
8249 case PAREN_EXPR:
8250 CASE_CONVERT:
8251 if (treeop0 == error_mark_node)
8252 return const0_rtx;
8254 if (TREE_CODE (type) == UNION_TYPE)
8256 tree valtype = TREE_TYPE (treeop0);
8258 /* If both input and output are BLKmode, this conversion isn't doing
8259 anything except possibly changing memory attribute. */
8260 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8262 rtx result = expand_expr (treeop0, target, tmode,
8263 modifier);
8265 result = copy_rtx (result);
8266 set_mem_attributes (result, type, 0);
8267 return result;
8270 if (target == 0)
8272 if (TYPE_MODE (type) != BLKmode)
8273 target = gen_reg_rtx (TYPE_MODE (type));
8274 else
8275 target = assign_temp (type, 1, 1);
8278 if (MEM_P (target))
8279 /* Store data into beginning of memory target. */
8280 store_expr (treeop0,
8281 adjust_address (target, TYPE_MODE (valtype), 0),
8282 modifier == EXPAND_STACK_PARM,
8283 false, TYPE_REVERSE_STORAGE_ORDER (type));
8285 else
8287 gcc_assert (REG_P (target)
8288 && !TYPE_REVERSE_STORAGE_ORDER (type));
8290 /* Store this field into a union of the proper type. */
8291 store_field (target,
8292 MIN ((int_size_in_bytes (TREE_TYPE
8293 (treeop0))
8294 * BITS_PER_UNIT),
8295 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8296 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8297 false, false);
8300 /* Return the entire union. */
8301 return target;
8304 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8306 op0 = expand_expr (treeop0, target, VOIDmode,
8307 modifier);
8309 /* If the signedness of the conversion differs and OP0 is
8310 a promoted SUBREG, clear that indication since we now
8311 have to do the proper extension. */
8312 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8313 && GET_CODE (op0) == SUBREG)
8314 SUBREG_PROMOTED_VAR_P (op0) = 0;
8316 return REDUCE_BIT_FIELD (op0);
8319 op0 = expand_expr (treeop0, NULL_RTX, mode,
8320 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8321 if (GET_MODE (op0) == mode)
8324 /* If OP0 is a constant, just convert it into the proper mode. */
8325 else if (CONSTANT_P (op0))
8327 tree inner_type = TREE_TYPE (treeop0);
8328 machine_mode inner_mode = GET_MODE (op0);
8330 if (inner_mode == VOIDmode)
8331 inner_mode = TYPE_MODE (inner_type);
8333 if (modifier == EXPAND_INITIALIZER)
8334 op0 = lowpart_subreg (mode, op0, inner_mode);
8335 else
8336 op0= convert_modes (mode, inner_mode, op0,
8337 TYPE_UNSIGNED (inner_type));
8340 else if (modifier == EXPAND_INITIALIZER)
8341 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8343 else if (target == 0)
8344 op0 = convert_to_mode (mode, op0,
8345 TYPE_UNSIGNED (TREE_TYPE
8346 (treeop0)));
8347 else
8349 convert_move (target, op0,
8350 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8351 op0 = target;
8354 return REDUCE_BIT_FIELD (op0);
8356 case ADDR_SPACE_CONVERT_EXPR:
8358 tree treeop0_type = TREE_TYPE (treeop0);
8360 gcc_assert (POINTER_TYPE_P (type));
8361 gcc_assert (POINTER_TYPE_P (treeop0_type));
8363 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8364 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8366 /* Conversions between pointers to the same address space should
8367 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8368 gcc_assert (as_to != as_from);
8370 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8372 /* Ask target code to handle conversion between pointers
8373 to overlapping address spaces. */
8374 if (targetm.addr_space.subset_p (as_to, as_from)
8375 || targetm.addr_space.subset_p (as_from, as_to))
8377 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8379 else
8381 /* For disjoint address spaces, converting anything but a null
8382 pointer invokes undefined behavior. We truncate or extend the
8383 value as if we'd converted via integers, which handles 0 as
8384 required, and all others as the programmer likely expects. */
8385 #ifndef POINTERS_EXTEND_UNSIGNED
8386 const int POINTERS_EXTEND_UNSIGNED = 1;
8387 #endif
8388 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8389 op0, POINTERS_EXTEND_UNSIGNED);
8391 gcc_assert (op0);
8392 return op0;
8395 case POINTER_PLUS_EXPR:
8396 /* Even though the sizetype mode and the pointer's mode can be different
8397 expand is able to handle this correctly and get the correct result out
8398 of the PLUS_EXPR code. */
8399 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8400 if sizetype precision is smaller than pointer precision. */
8401 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8402 treeop1 = fold_convert_loc (loc, type,
8403 fold_convert_loc (loc, ssizetype,
8404 treeop1));
8405 /* If sizetype precision is larger than pointer precision, truncate the
8406 offset to have matching modes. */
8407 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8408 treeop1 = fold_convert_loc (loc, type, treeop1);
8409 /* FALLTHRU */
8411 case PLUS_EXPR:
8412 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8413 something else, make sure we add the register to the constant and
8414 then to the other thing. This case can occur during strength
8415 reduction and doing it this way will produce better code if the
8416 frame pointer or argument pointer is eliminated.
8418 fold-const.c will ensure that the constant is always in the inner
8419 PLUS_EXPR, so the only case we need to do anything about is if
8420 sp, ap, or fp is our second argument, in which case we must swap
8421 the innermost first argument and our second argument. */
8423 if (TREE_CODE (treeop0) == PLUS_EXPR
8424 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8425 && VAR_P (treeop1)
8426 && (DECL_RTL (treeop1) == frame_pointer_rtx
8427 || DECL_RTL (treeop1) == stack_pointer_rtx
8428 || DECL_RTL (treeop1) == arg_pointer_rtx))
8430 gcc_unreachable ();
8433 /* If the result is to be ptr_mode and we are adding an integer to
8434 something, we might be forming a constant. So try to use
8435 plus_constant. If it produces a sum and we can't accept it,
8436 use force_operand. This allows P = &ARR[const] to generate
8437 efficient code on machines where a SYMBOL_REF is not a valid
8438 address.
8440 If this is an EXPAND_SUM call, always return the sum. */
8441 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8442 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8444 if (modifier == EXPAND_STACK_PARM)
8445 target = 0;
8446 if (TREE_CODE (treeop0) == INTEGER_CST
8447 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8448 && TREE_CONSTANT (treeop1))
8450 rtx constant_part;
8451 HOST_WIDE_INT wc;
8452 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8454 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8455 EXPAND_SUM);
8456 /* Use wi::shwi to ensure that the constant is
8457 truncated according to the mode of OP1, then sign extended
8458 to a HOST_WIDE_INT. Using the constant directly can result
8459 in non-canonical RTL in a 64x32 cross compile. */
8460 wc = TREE_INT_CST_LOW (treeop0);
8461 constant_part =
8462 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8463 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8464 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8465 op1 = force_operand (op1, target);
8466 return REDUCE_BIT_FIELD (op1);
8469 else if (TREE_CODE (treeop1) == INTEGER_CST
8470 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8471 && TREE_CONSTANT (treeop0))
8473 rtx constant_part;
8474 HOST_WIDE_INT wc;
8475 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8477 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8478 (modifier == EXPAND_INITIALIZER
8479 ? EXPAND_INITIALIZER : EXPAND_SUM));
8480 if (! CONSTANT_P (op0))
8482 op1 = expand_expr (treeop1, NULL_RTX,
8483 VOIDmode, modifier);
8484 /* Return a PLUS if modifier says it's OK. */
8485 if (modifier == EXPAND_SUM
8486 || modifier == EXPAND_INITIALIZER)
8487 return simplify_gen_binary (PLUS, mode, op0, op1);
8488 goto binop2;
8490 /* Use wi::shwi to ensure that the constant is
8491 truncated according to the mode of OP1, then sign extended
8492 to a HOST_WIDE_INT. Using the constant directly can result
8493 in non-canonical RTL in a 64x32 cross compile. */
8494 wc = TREE_INT_CST_LOW (treeop1);
8495 constant_part
8496 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8497 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8498 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8499 op0 = force_operand (op0, target);
8500 return REDUCE_BIT_FIELD (op0);
8504 /* Use TER to expand pointer addition of a negated value
8505 as pointer subtraction. */
8506 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8507 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8508 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8509 && TREE_CODE (treeop1) == SSA_NAME
8510 && TYPE_MODE (TREE_TYPE (treeop0))
8511 == TYPE_MODE (TREE_TYPE (treeop1)))
8513 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8514 if (def)
8516 treeop1 = gimple_assign_rhs1 (def);
8517 code = MINUS_EXPR;
8518 goto do_minus;
8522 /* No sense saving up arithmetic to be done
8523 if it's all in the wrong mode to form part of an address.
8524 And force_operand won't know whether to sign-extend or
8525 zero-extend. */
8526 if (modifier != EXPAND_INITIALIZER
8527 && (modifier != EXPAND_SUM || mode != ptr_mode))
8529 expand_operands (treeop0, treeop1,
8530 subtarget, &op0, &op1, modifier);
8531 if (op0 == const0_rtx)
8532 return op1;
8533 if (op1 == const0_rtx)
8534 return op0;
8535 goto binop2;
8538 expand_operands (treeop0, treeop1,
8539 subtarget, &op0, &op1, modifier);
8540 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8542 case MINUS_EXPR:
8543 do_minus:
8544 /* For initializers, we are allowed to return a MINUS of two
8545 symbolic constants. Here we handle all cases when both operands
8546 are constant. */
8547 /* Handle difference of two symbolic constants,
8548 for the sake of an initializer. */
8549 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8550 && really_constant_p (treeop0)
8551 && really_constant_p (treeop1))
8553 expand_operands (treeop0, treeop1,
8554 NULL_RTX, &op0, &op1, modifier);
8556 /* If the last operand is a CONST_INT, use plus_constant of
8557 the negated constant. Else make the MINUS. */
8558 if (CONST_INT_P (op1))
8559 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8560 -INTVAL (op1)));
8561 else
8562 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8565 /* No sense saving up arithmetic to be done
8566 if it's all in the wrong mode to form part of an address.
8567 And force_operand won't know whether to sign-extend or
8568 zero-extend. */
8569 if (modifier != EXPAND_INITIALIZER
8570 && (modifier != EXPAND_SUM || mode != ptr_mode))
8571 goto binop;
8573 expand_operands (treeop0, treeop1,
8574 subtarget, &op0, &op1, modifier);
8576 /* Convert A - const to A + (-const). */
8577 if (CONST_INT_P (op1))
8579 op1 = negate_rtx (mode, op1);
8580 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8583 goto binop2;
8585 case WIDEN_MULT_PLUS_EXPR:
8586 case WIDEN_MULT_MINUS_EXPR:
8587 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8588 op2 = expand_normal (treeop2);
8589 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8590 target, unsignedp);
8591 return target;
8593 case WIDEN_MULT_EXPR:
8594 /* If first operand is constant, swap them.
8595 Thus the following special case checks need only
8596 check the second operand. */
8597 if (TREE_CODE (treeop0) == INTEGER_CST)
8598 std::swap (treeop0, treeop1);
8600 /* First, check if we have a multiplication of one signed and one
8601 unsigned operand. */
8602 if (TREE_CODE (treeop1) != INTEGER_CST
8603 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8604 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8606 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8607 this_optab = usmul_widen_optab;
8608 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8609 != CODE_FOR_nothing)
8611 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8612 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8613 EXPAND_NORMAL);
8614 else
8615 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8616 EXPAND_NORMAL);
8617 /* op0 and op1 might still be constant, despite the above
8618 != INTEGER_CST check. Handle it. */
8619 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8621 op0 = convert_modes (innermode, mode, op0, true);
8622 op1 = convert_modes (innermode, mode, op1, false);
8623 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8624 target, unsignedp));
8626 goto binop3;
8629 /* Check for a multiplication with matching signedness. */
8630 else if ((TREE_CODE (treeop1) == INTEGER_CST
8631 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8632 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8633 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8635 tree op0type = TREE_TYPE (treeop0);
8636 machine_mode innermode = TYPE_MODE (op0type);
8637 bool zextend_p = TYPE_UNSIGNED (op0type);
8638 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8639 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8641 if (TREE_CODE (treeop0) != INTEGER_CST)
8643 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8644 != CODE_FOR_nothing)
8646 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8647 EXPAND_NORMAL);
8648 /* op0 and op1 might still be constant, despite the above
8649 != INTEGER_CST check. Handle it. */
8650 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8652 widen_mult_const:
8653 op0 = convert_modes (innermode, mode, op0, zextend_p);
8655 = convert_modes (innermode, mode, op1,
8656 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8657 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8658 target,
8659 unsignedp));
8661 temp = expand_widening_mult (mode, op0, op1, target,
8662 unsignedp, this_optab);
8663 return REDUCE_BIT_FIELD (temp);
8665 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8666 != CODE_FOR_nothing
8667 && innermode == word_mode)
8669 rtx htem, hipart;
8670 op0 = expand_normal (treeop0);
8671 if (TREE_CODE (treeop1) == INTEGER_CST)
8672 op1 = convert_modes (innermode, mode,
8673 expand_normal (treeop1),
8674 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8675 else
8676 op1 = expand_normal (treeop1);
8677 /* op0 and op1 might still be constant, despite the above
8678 != INTEGER_CST check. Handle it. */
8679 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8680 goto widen_mult_const;
8681 temp = expand_binop (mode, other_optab, op0, op1, target,
8682 unsignedp, OPTAB_LIB_WIDEN);
8683 hipart = gen_highpart (innermode, temp);
8684 htem = expand_mult_highpart_adjust (innermode, hipart,
8685 op0, op1, hipart,
8686 zextend_p);
8687 if (htem != hipart)
8688 emit_move_insn (hipart, htem);
8689 return REDUCE_BIT_FIELD (temp);
8693 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8694 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8695 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8696 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8698 case FMA_EXPR:
8700 optab opt = fma_optab;
8701 gimple *def0, *def2;
8703 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8704 call. */
8705 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8707 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8708 tree call_expr;
8710 gcc_assert (fn != NULL_TREE);
8711 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8712 return expand_builtin (call_expr, target, subtarget, mode, false);
8715 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8716 /* The multiplication is commutative - look at its 2nd operand
8717 if the first isn't fed by a negate. */
8718 if (!def0)
8720 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8721 /* Swap operands if the 2nd operand is fed by a negate. */
8722 if (def0)
8723 std::swap (treeop0, treeop1);
8725 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8727 op0 = op2 = NULL;
8729 if (def0 && def2
8730 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8732 opt = fnms_optab;
8733 op0 = expand_normal (gimple_assign_rhs1 (def0));
8734 op2 = expand_normal (gimple_assign_rhs1 (def2));
8736 else if (def0
8737 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8739 opt = fnma_optab;
8740 op0 = expand_normal (gimple_assign_rhs1 (def0));
8742 else if (def2
8743 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8745 opt = fms_optab;
8746 op2 = expand_normal (gimple_assign_rhs1 (def2));
8749 if (op0 == NULL)
8750 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8751 if (op2 == NULL)
8752 op2 = expand_normal (treeop2);
8753 op1 = expand_normal (treeop1);
8755 return expand_ternary_op (TYPE_MODE (type), opt,
8756 op0, op1, op2, target, 0);
8759 case MULT_EXPR:
8760 /* If this is a fixed-point operation, then we cannot use the code
8761 below because "expand_mult" doesn't support sat/no-sat fixed-point
8762 multiplications. */
8763 if (ALL_FIXED_POINT_MODE_P (mode))
8764 goto binop;
8766 /* If first operand is constant, swap them.
8767 Thus the following special case checks need only
8768 check the second operand. */
8769 if (TREE_CODE (treeop0) == INTEGER_CST)
8770 std::swap (treeop0, treeop1);
8772 /* Attempt to return something suitable for generating an
8773 indexed address, for machines that support that. */
8775 if (modifier == EXPAND_SUM && mode == ptr_mode
8776 && tree_fits_shwi_p (treeop1))
8778 tree exp1 = treeop1;
8780 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8781 EXPAND_SUM);
8783 if (!REG_P (op0))
8784 op0 = force_operand (op0, NULL_RTX);
8785 if (!REG_P (op0))
8786 op0 = copy_to_mode_reg (mode, op0);
8788 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8789 gen_int_mode (tree_to_shwi (exp1),
8790 TYPE_MODE (TREE_TYPE (exp1)))));
8793 if (modifier == EXPAND_STACK_PARM)
8794 target = 0;
8796 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8797 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8799 case TRUNC_DIV_EXPR:
8800 case FLOOR_DIV_EXPR:
8801 case CEIL_DIV_EXPR:
8802 case ROUND_DIV_EXPR:
8803 case EXACT_DIV_EXPR:
8804 /* If this is a fixed-point operation, then we cannot use the code
8805 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8806 divisions. */
8807 if (ALL_FIXED_POINT_MODE_P (mode))
8808 goto binop;
8810 if (modifier == EXPAND_STACK_PARM)
8811 target = 0;
8812 /* Possible optimization: compute the dividend with EXPAND_SUM
8813 then if the divisor is constant can optimize the case
8814 where some terms of the dividend have coeffs divisible by it. */
8815 expand_operands (treeop0, treeop1,
8816 subtarget, &op0, &op1, EXPAND_NORMAL);
8817 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8819 case RDIV_EXPR:
8820 goto binop;
8822 case MULT_HIGHPART_EXPR:
8823 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8824 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8825 gcc_assert (temp);
8826 return temp;
8828 case TRUNC_MOD_EXPR:
8829 case FLOOR_MOD_EXPR:
8830 case CEIL_MOD_EXPR:
8831 case ROUND_MOD_EXPR:
8832 if (modifier == EXPAND_STACK_PARM)
8833 target = 0;
8834 expand_operands (treeop0, treeop1,
8835 subtarget, &op0, &op1, EXPAND_NORMAL);
8836 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8838 case FIXED_CONVERT_EXPR:
8839 op0 = expand_normal (treeop0);
8840 if (target == 0 || modifier == EXPAND_STACK_PARM)
8841 target = gen_reg_rtx (mode);
8843 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8844 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8845 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8846 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8847 else
8848 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8849 return target;
8851 case FIX_TRUNC_EXPR:
8852 op0 = expand_normal (treeop0);
8853 if (target == 0 || modifier == EXPAND_STACK_PARM)
8854 target = gen_reg_rtx (mode);
8855 expand_fix (target, op0, unsignedp);
8856 return target;
8858 case FLOAT_EXPR:
8859 op0 = expand_normal (treeop0);
8860 if (target == 0 || modifier == EXPAND_STACK_PARM)
8861 target = gen_reg_rtx (mode);
8862 /* expand_float can't figure out what to do if FROM has VOIDmode.
8863 So give it the correct mode. With -O, cse will optimize this. */
8864 if (GET_MODE (op0) == VOIDmode)
8865 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8866 op0);
8867 expand_float (target, op0,
8868 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8869 return target;
8871 case NEGATE_EXPR:
8872 op0 = expand_expr (treeop0, subtarget,
8873 VOIDmode, EXPAND_NORMAL);
8874 if (modifier == EXPAND_STACK_PARM)
8875 target = 0;
8876 temp = expand_unop (mode,
8877 optab_for_tree_code (NEGATE_EXPR, type,
8878 optab_default),
8879 op0, target, 0);
8880 gcc_assert (temp);
8881 return REDUCE_BIT_FIELD (temp);
8883 case ABS_EXPR:
8884 op0 = expand_expr (treeop0, subtarget,
8885 VOIDmode, EXPAND_NORMAL);
8886 if (modifier == EXPAND_STACK_PARM)
8887 target = 0;
8889 /* ABS_EXPR is not valid for complex arguments. */
8890 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8891 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8893 /* Unsigned abs is simply the operand. Testing here means we don't
8894 risk generating incorrect code below. */
8895 if (TYPE_UNSIGNED (type))
8896 return op0;
8898 return expand_abs (mode, op0, target, unsignedp,
8899 safe_from_p (target, treeop0, 1));
8901 case MAX_EXPR:
8902 case MIN_EXPR:
8903 target = original_target;
8904 if (target == 0
8905 || modifier == EXPAND_STACK_PARM
8906 || (MEM_P (target) && MEM_VOLATILE_P (target))
8907 || GET_MODE (target) != mode
8908 || (REG_P (target)
8909 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8910 target = gen_reg_rtx (mode);
8911 expand_operands (treeop0, treeop1,
8912 target, &op0, &op1, EXPAND_NORMAL);
8914 /* First try to do it with a special MIN or MAX instruction.
8915 If that does not win, use a conditional jump to select the proper
8916 value. */
8917 this_optab = optab_for_tree_code (code, type, optab_default);
8918 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8919 OPTAB_WIDEN);
8920 if (temp != 0)
8921 return temp;
8923 /* At this point, a MEM target is no longer useful; we will get better
8924 code without it. */
8926 if (! REG_P (target))
8927 target = gen_reg_rtx (mode);
8929 /* If op1 was placed in target, swap op0 and op1. */
8930 if (target != op0 && target == op1)
8931 std::swap (op0, op1);
8933 /* We generate better code and avoid problems with op1 mentioning
8934 target by forcing op1 into a pseudo if it isn't a constant. */
8935 if (! CONSTANT_P (op1))
8936 op1 = force_reg (mode, op1);
8939 enum rtx_code comparison_code;
8940 rtx cmpop1 = op1;
8942 if (code == MAX_EXPR)
8943 comparison_code = unsignedp ? GEU : GE;
8944 else
8945 comparison_code = unsignedp ? LEU : LE;
8947 /* Canonicalize to comparisons against 0. */
8948 if (op1 == const1_rtx)
8950 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8951 or (a != 0 ? a : 1) for unsigned.
8952 For MIN we are safe converting (a <= 1 ? a : 1)
8953 into (a <= 0 ? a : 1) */
8954 cmpop1 = const0_rtx;
8955 if (code == MAX_EXPR)
8956 comparison_code = unsignedp ? NE : GT;
8958 if (op1 == constm1_rtx && !unsignedp)
8960 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8961 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8962 cmpop1 = const0_rtx;
8963 if (code == MIN_EXPR)
8964 comparison_code = LT;
8967 /* Use a conditional move if possible. */
8968 if (can_conditionally_move_p (mode))
8970 rtx insn;
8972 start_sequence ();
8974 /* Try to emit the conditional move. */
8975 insn = emit_conditional_move (target, comparison_code,
8976 op0, cmpop1, mode,
8977 op0, op1, mode,
8978 unsignedp);
8980 /* If we could do the conditional move, emit the sequence,
8981 and return. */
8982 if (insn)
8984 rtx_insn *seq = get_insns ();
8985 end_sequence ();
8986 emit_insn (seq);
8987 return target;
8990 /* Otherwise discard the sequence and fall back to code with
8991 branches. */
8992 end_sequence ();
8995 if (target != op0)
8996 emit_move_insn (target, op0);
8998 lab = gen_label_rtx ();
8999 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9000 unsignedp, mode, NULL_RTX, NULL, lab,
9001 -1);
9003 emit_move_insn (target, op1);
9004 emit_label (lab);
9005 return target;
9007 case BIT_NOT_EXPR:
9008 op0 = expand_expr (treeop0, subtarget,
9009 VOIDmode, EXPAND_NORMAL);
9010 if (modifier == EXPAND_STACK_PARM)
9011 target = 0;
9012 /* In case we have to reduce the result to bitfield precision
9013 for unsigned bitfield expand this as XOR with a proper constant
9014 instead. */
9015 if (reduce_bit_field && TYPE_UNSIGNED (type))
9017 wide_int mask = wi::mask (TYPE_PRECISION (type),
9018 false, GET_MODE_PRECISION (mode));
9020 temp = expand_binop (mode, xor_optab, op0,
9021 immed_wide_int_const (mask, mode),
9022 target, 1, OPTAB_LIB_WIDEN);
9024 else
9025 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9026 gcc_assert (temp);
9027 return temp;
9029 /* ??? Can optimize bitwise operations with one arg constant.
9030 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9031 and (a bitwise1 b) bitwise2 b (etc)
9032 but that is probably not worth while. */
9034 case BIT_AND_EXPR:
9035 case BIT_IOR_EXPR:
9036 case BIT_XOR_EXPR:
9037 goto binop;
9039 case LROTATE_EXPR:
9040 case RROTATE_EXPR:
9041 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9042 || (GET_MODE_PRECISION (TYPE_MODE (type))
9043 == TYPE_PRECISION (type)));
9044 /* fall through */
9046 case LSHIFT_EXPR:
9047 case RSHIFT_EXPR:
9049 /* If this is a fixed-point operation, then we cannot use the code
9050 below because "expand_shift" doesn't support sat/no-sat fixed-point
9051 shifts. */
9052 if (ALL_FIXED_POINT_MODE_P (mode))
9053 goto binop;
9055 if (! safe_from_p (subtarget, treeop1, 1))
9056 subtarget = 0;
9057 if (modifier == EXPAND_STACK_PARM)
9058 target = 0;
9059 op0 = expand_expr (treeop0, subtarget,
9060 VOIDmode, EXPAND_NORMAL);
9062 /* Left shift optimization when shifting across word_size boundary.
9064 If mode == GET_MODE_WIDER_MODE (word_mode), then normally there isn't
9065 native instruction to support this wide mode left shift. Given below
9066 scenario:
9068 Type A = (Type) B << C
9070 |< T >|
9071 | dest_high | dest_low |
9073 | word_size |
9075 If the shift amount C caused we shift B to across the word size
9076 boundary, i.e part of B shifted into high half of destination
9077 register, and part of B remains in the low half, then GCC will use
9078 the following left shift expand logic:
9080 1. Initialize dest_low to B.
9081 2. Initialize every bit of dest_high to the sign bit of B.
9082 3. Logic left shift dest_low by C bit to finalize dest_low.
9083 The value of dest_low before this shift is kept in a temp D.
9084 4. Logic left shift dest_high by C.
9085 5. Logic right shift D by (word_size - C).
9086 6. Or the result of 4 and 5 to finalize dest_high.
9088 While, by checking gimple statements, if operand B is coming from
9089 signed extension, then we can simplify above expand logic into:
9091 1. dest_high = src_low >> (word_size - C).
9092 2. dest_low = src_low << C.
9094 We can use one arithmetic right shift to finish all the purpose of
9095 steps 2, 4, 5, 6, thus we reduce the steps needed from 6 into 2. */
9097 temp = NULL_RTX;
9098 if (code == LSHIFT_EXPR
9099 && target
9100 && REG_P (target)
9101 && ! unsignedp
9102 && mode == GET_MODE_WIDER_MODE (word_mode)
9103 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9104 && TREE_CONSTANT (treeop1)
9105 && TREE_CODE (treeop0) == SSA_NAME)
9107 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9108 if (is_gimple_assign (def)
9109 && gimple_assign_rhs_code (def) == NOP_EXPR)
9111 machine_mode rmode = TYPE_MODE
9112 (TREE_TYPE (gimple_assign_rhs1 (def)));
9114 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9115 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9116 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9117 >= GET_MODE_BITSIZE (word_mode)))
9119 rtx_insn *seq, *seq_old;
9120 unsigned int high_off = subreg_highpart_offset (word_mode,
9121 mode);
9122 rtx low = lowpart_subreg (word_mode, op0, mode);
9123 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9124 rtx dest_high = simplify_gen_subreg (word_mode, target,
9125 mode, high_off);
9126 HOST_WIDE_INT ramount = (BITS_PER_WORD
9127 - TREE_INT_CST_LOW (treeop1));
9128 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9130 start_sequence ();
9131 /* dest_high = src_low >> (word_size - C). */
9132 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9133 rshift, dest_high, unsignedp);
9134 if (temp != dest_high)
9135 emit_move_insn (dest_high, temp);
9137 /* dest_low = src_low << C. */
9138 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9139 treeop1, dest_low, unsignedp);
9140 if (temp != dest_low)
9141 emit_move_insn (dest_low, temp);
9143 seq = get_insns ();
9144 end_sequence ();
9145 temp = target ;
9147 if (have_insn_for (ASHIFT, mode))
9149 bool speed_p = optimize_insn_for_speed_p ();
9150 start_sequence ();
9151 rtx ret_old = expand_variable_shift (code, mode, op0,
9152 treeop1, target,
9153 unsignedp);
9155 seq_old = get_insns ();
9156 end_sequence ();
9157 if (seq_cost (seq, speed_p)
9158 >= seq_cost (seq_old, speed_p))
9160 seq = seq_old;
9161 temp = ret_old;
9164 emit_insn (seq);
9169 if (temp == NULL_RTX)
9170 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9171 unsignedp);
9172 if (code == LSHIFT_EXPR)
9173 temp = REDUCE_BIT_FIELD (temp);
9174 return temp;
9177 /* Could determine the answer when only additive constants differ. Also,
9178 the addition of one can be handled by changing the condition. */
9179 case LT_EXPR:
9180 case LE_EXPR:
9181 case GT_EXPR:
9182 case GE_EXPR:
9183 case EQ_EXPR:
9184 case NE_EXPR:
9185 case UNORDERED_EXPR:
9186 case ORDERED_EXPR:
9187 case UNLT_EXPR:
9188 case UNLE_EXPR:
9189 case UNGT_EXPR:
9190 case UNGE_EXPR:
9191 case UNEQ_EXPR:
9192 case LTGT_EXPR:
9194 temp = do_store_flag (ops,
9195 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9196 tmode != VOIDmode ? tmode : mode);
9197 if (temp)
9198 return temp;
9200 /* Use a compare and a jump for BLKmode comparisons, or for function
9201 type comparisons is have_canonicalize_funcptr_for_compare. */
9203 if ((target == 0
9204 || modifier == EXPAND_STACK_PARM
9205 || ! safe_from_p (target, treeop0, 1)
9206 || ! safe_from_p (target, treeop1, 1)
9207 /* Make sure we don't have a hard reg (such as function's return
9208 value) live across basic blocks, if not optimizing. */
9209 || (!optimize && REG_P (target)
9210 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9211 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9213 emit_move_insn (target, const0_rtx);
9215 rtx_code_label *lab1 = gen_label_rtx ();
9216 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9218 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9219 emit_move_insn (target, constm1_rtx);
9220 else
9221 emit_move_insn (target, const1_rtx);
9223 emit_label (lab1);
9224 return target;
9226 case COMPLEX_EXPR:
9227 /* Get the rtx code of the operands. */
9228 op0 = expand_normal (treeop0);
9229 op1 = expand_normal (treeop1);
9231 if (!target)
9232 target = gen_reg_rtx (TYPE_MODE (type));
9233 else
9234 /* If target overlaps with op1, then either we need to force
9235 op1 into a pseudo (if target also overlaps with op0),
9236 or write the complex parts in reverse order. */
9237 switch (GET_CODE (target))
9239 case CONCAT:
9240 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9242 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9244 complex_expr_force_op1:
9245 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9246 emit_move_insn (temp, op1);
9247 op1 = temp;
9248 break;
9250 complex_expr_swap_order:
9251 /* Move the imaginary (op1) and real (op0) parts to their
9252 location. */
9253 write_complex_part (target, op1, true);
9254 write_complex_part (target, op0, false);
9256 return target;
9258 break;
9259 case MEM:
9260 temp = adjust_address_nv (target,
9261 GET_MODE_INNER (GET_MODE (target)), 0);
9262 if (reg_overlap_mentioned_p (temp, op1))
9264 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9265 temp = adjust_address_nv (target, imode,
9266 GET_MODE_SIZE (imode));
9267 if (reg_overlap_mentioned_p (temp, op0))
9268 goto complex_expr_force_op1;
9269 goto complex_expr_swap_order;
9271 break;
9272 default:
9273 if (reg_overlap_mentioned_p (target, op1))
9275 if (reg_overlap_mentioned_p (target, op0))
9276 goto complex_expr_force_op1;
9277 goto complex_expr_swap_order;
9279 break;
9282 /* Move the real (op0) and imaginary (op1) parts to their location. */
9283 write_complex_part (target, op0, false);
9284 write_complex_part (target, op1, true);
9286 return target;
9288 case WIDEN_SUM_EXPR:
9290 tree oprnd0 = treeop0;
9291 tree oprnd1 = treeop1;
9293 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9294 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9295 target, unsignedp);
9296 return target;
9299 case REDUC_MAX_EXPR:
9300 case REDUC_MIN_EXPR:
9301 case REDUC_PLUS_EXPR:
9303 op0 = expand_normal (treeop0);
9304 this_optab = optab_for_tree_code (code, type, optab_default);
9305 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9307 struct expand_operand ops[2];
9308 enum insn_code icode = optab_handler (this_optab, vec_mode);
9310 create_output_operand (&ops[0], target, mode);
9311 create_input_operand (&ops[1], op0, vec_mode);
9312 expand_insn (icode, 2, ops);
9313 target = ops[0].value;
9314 if (GET_MODE (target) != mode)
9315 return gen_lowpart (tmode, target);
9316 return target;
9319 case VEC_UNPACK_HI_EXPR:
9320 case VEC_UNPACK_LO_EXPR:
9322 op0 = expand_normal (treeop0);
9323 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9324 target, unsignedp);
9325 gcc_assert (temp);
9326 return temp;
9329 case VEC_UNPACK_FLOAT_HI_EXPR:
9330 case VEC_UNPACK_FLOAT_LO_EXPR:
9332 op0 = expand_normal (treeop0);
9333 /* The signedness is determined from input operand. */
9334 temp = expand_widen_pattern_expr
9335 (ops, op0, NULL_RTX, NULL_RTX,
9336 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9338 gcc_assert (temp);
9339 return temp;
9342 case VEC_WIDEN_MULT_HI_EXPR:
9343 case VEC_WIDEN_MULT_LO_EXPR:
9344 case VEC_WIDEN_MULT_EVEN_EXPR:
9345 case VEC_WIDEN_MULT_ODD_EXPR:
9346 case VEC_WIDEN_LSHIFT_HI_EXPR:
9347 case VEC_WIDEN_LSHIFT_LO_EXPR:
9348 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9349 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9350 target, unsignedp);
9351 gcc_assert (target);
9352 return target;
9354 case VEC_PACK_TRUNC_EXPR:
9355 case VEC_PACK_SAT_EXPR:
9356 case VEC_PACK_FIX_TRUNC_EXPR:
9357 mode = TYPE_MODE (TREE_TYPE (treeop0));
9358 goto binop;
9360 case VEC_PERM_EXPR:
9361 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9362 op2 = expand_normal (treeop2);
9364 /* Careful here: if the target doesn't support integral vector modes,
9365 a constant selection vector could wind up smooshed into a normal
9366 integral constant. */
9367 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9369 tree sel_type = TREE_TYPE (treeop2);
9370 machine_mode vmode
9371 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9372 TYPE_VECTOR_SUBPARTS (sel_type));
9373 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9374 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9375 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9377 else
9378 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9380 temp = expand_vec_perm (mode, op0, op1, op2, target);
9381 gcc_assert (temp);
9382 return temp;
9384 case DOT_PROD_EXPR:
9386 tree oprnd0 = treeop0;
9387 tree oprnd1 = treeop1;
9388 tree oprnd2 = treeop2;
9389 rtx op2;
9391 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9392 op2 = expand_normal (oprnd2);
9393 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9394 target, unsignedp);
9395 return target;
9398 case SAD_EXPR:
9400 tree oprnd0 = treeop0;
9401 tree oprnd1 = treeop1;
9402 tree oprnd2 = treeop2;
9403 rtx op2;
9405 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9406 op2 = expand_normal (oprnd2);
9407 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9408 target, unsignedp);
9409 return target;
9412 case REALIGN_LOAD_EXPR:
9414 tree oprnd0 = treeop0;
9415 tree oprnd1 = treeop1;
9416 tree oprnd2 = treeop2;
9417 rtx op2;
9419 this_optab = optab_for_tree_code (code, type, optab_default);
9420 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9421 op2 = expand_normal (oprnd2);
9422 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9423 target, unsignedp);
9424 gcc_assert (temp);
9425 return temp;
9428 case COND_EXPR:
9430 /* A COND_EXPR with its type being VOID_TYPE represents a
9431 conditional jump and is handled in
9432 expand_gimple_cond_expr. */
9433 gcc_assert (!VOID_TYPE_P (type));
9435 /* Note that COND_EXPRs whose type is a structure or union
9436 are required to be constructed to contain assignments of
9437 a temporary variable, so that we can evaluate them here
9438 for side effect only. If type is void, we must do likewise. */
9440 gcc_assert (!TREE_ADDRESSABLE (type)
9441 && !ignore
9442 && TREE_TYPE (treeop1) != void_type_node
9443 && TREE_TYPE (treeop2) != void_type_node);
9445 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9446 if (temp)
9447 return temp;
9449 /* If we are not to produce a result, we have no target. Otherwise,
9450 if a target was specified use it; it will not be used as an
9451 intermediate target unless it is safe. If no target, use a
9452 temporary. */
9454 if (modifier != EXPAND_STACK_PARM
9455 && original_target
9456 && safe_from_p (original_target, treeop0, 1)
9457 && GET_MODE (original_target) == mode
9458 && !MEM_P (original_target))
9459 temp = original_target;
9460 else
9461 temp = assign_temp (type, 0, 1);
9463 do_pending_stack_adjust ();
9464 NO_DEFER_POP;
9465 rtx_code_label *lab0 = gen_label_rtx ();
9466 rtx_code_label *lab1 = gen_label_rtx ();
9467 jumpifnot (treeop0, lab0, -1);
9468 store_expr (treeop1, temp,
9469 modifier == EXPAND_STACK_PARM,
9470 false, false);
9472 emit_jump_insn (targetm.gen_jump (lab1));
9473 emit_barrier ();
9474 emit_label (lab0);
9475 store_expr (treeop2, temp,
9476 modifier == EXPAND_STACK_PARM,
9477 false, false);
9479 emit_label (lab1);
9480 OK_DEFER_POP;
9481 return temp;
9484 case VEC_COND_EXPR:
9485 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9486 return target;
9488 case BIT_INSERT_EXPR:
9490 unsigned bitpos = tree_to_uhwi (treeop2);
9491 unsigned bitsize;
9492 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9493 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9494 else
9495 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9496 rtx op0 = expand_normal (treeop0);
9497 rtx op1 = expand_normal (treeop1);
9498 rtx dst = gen_reg_rtx (mode);
9499 emit_move_insn (dst, op0);
9500 store_bit_field (dst, bitsize, bitpos, 0, 0,
9501 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9502 return dst;
9505 default:
9506 gcc_unreachable ();
9509 /* Here to do an ordinary binary operator. */
9510 binop:
9511 expand_operands (treeop0, treeop1,
9512 subtarget, &op0, &op1, EXPAND_NORMAL);
9513 binop2:
9514 this_optab = optab_for_tree_code (code, type, optab_default);
9515 binop3:
9516 if (modifier == EXPAND_STACK_PARM)
9517 target = 0;
9518 temp = expand_binop (mode, this_optab, op0, op1, target,
9519 unsignedp, OPTAB_LIB_WIDEN);
9520 gcc_assert (temp);
9521 /* Bitwise operations do not need bitfield reduction as we expect their
9522 operands being properly truncated. */
9523 if (code == BIT_XOR_EXPR
9524 || code == BIT_AND_EXPR
9525 || code == BIT_IOR_EXPR)
9526 return temp;
9527 return REDUCE_BIT_FIELD (temp);
9529 #undef REDUCE_BIT_FIELD
9532 /* Return TRUE if expression STMT is suitable for replacement.
9533 Never consider memory loads as replaceable, because those don't ever lead
9534 into constant expressions. */
9536 static bool
9537 stmt_is_replaceable_p (gimple *stmt)
9539 if (ssa_is_replaceable_p (stmt))
9541 /* Don't move around loads. */
9542 if (!gimple_assign_single_p (stmt)
9543 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9544 return true;
9546 return false;
9550 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9551 enum expand_modifier modifier, rtx *alt_rtl,
9552 bool inner_reference_p)
9554 rtx op0, op1, temp, decl_rtl;
9555 tree type;
9556 int unsignedp;
9557 machine_mode mode, dmode;
9558 enum tree_code code = TREE_CODE (exp);
9559 rtx subtarget, original_target;
9560 int ignore;
9561 tree context;
9562 bool reduce_bit_field;
9563 location_t loc = EXPR_LOCATION (exp);
9564 struct separate_ops ops;
9565 tree treeop0, treeop1, treeop2;
9566 tree ssa_name = NULL_TREE;
9567 gimple *g;
9569 type = TREE_TYPE (exp);
9570 mode = TYPE_MODE (type);
9571 unsignedp = TYPE_UNSIGNED (type);
9573 treeop0 = treeop1 = treeop2 = NULL_TREE;
9574 if (!VL_EXP_CLASS_P (exp))
9575 switch (TREE_CODE_LENGTH (code))
9577 default:
9578 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9579 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9580 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9581 case 0: break;
9583 ops.code = code;
9584 ops.type = type;
9585 ops.op0 = treeop0;
9586 ops.op1 = treeop1;
9587 ops.op2 = treeop2;
9588 ops.location = loc;
9590 ignore = (target == const0_rtx
9591 || ((CONVERT_EXPR_CODE_P (code)
9592 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9593 && TREE_CODE (type) == VOID_TYPE));
9595 /* An operation in what may be a bit-field type needs the
9596 result to be reduced to the precision of the bit-field type,
9597 which is narrower than that of the type's mode. */
9598 reduce_bit_field = (!ignore
9599 && INTEGRAL_TYPE_P (type)
9600 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9602 /* If we are going to ignore this result, we need only do something
9603 if there is a side-effect somewhere in the expression. If there
9604 is, short-circuit the most common cases here. Note that we must
9605 not call expand_expr with anything but const0_rtx in case this
9606 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9608 if (ignore)
9610 if (! TREE_SIDE_EFFECTS (exp))
9611 return const0_rtx;
9613 /* Ensure we reference a volatile object even if value is ignored, but
9614 don't do this if all we are doing is taking its address. */
9615 if (TREE_THIS_VOLATILE (exp)
9616 && TREE_CODE (exp) != FUNCTION_DECL
9617 && mode != VOIDmode && mode != BLKmode
9618 && modifier != EXPAND_CONST_ADDRESS)
9620 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9621 if (MEM_P (temp))
9622 copy_to_reg (temp);
9623 return const0_rtx;
9626 if (TREE_CODE_CLASS (code) == tcc_unary
9627 || code == BIT_FIELD_REF
9628 || code == COMPONENT_REF
9629 || code == INDIRECT_REF)
9630 return expand_expr (treeop0, const0_rtx, VOIDmode,
9631 modifier);
9633 else if (TREE_CODE_CLASS (code) == tcc_binary
9634 || TREE_CODE_CLASS (code) == tcc_comparison
9635 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9637 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9638 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9639 return const0_rtx;
9642 target = 0;
9645 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9646 target = 0;
9648 /* Use subtarget as the target for operand 0 of a binary operation. */
9649 subtarget = get_subtarget (target);
9650 original_target = target;
9652 switch (code)
9654 case LABEL_DECL:
9656 tree function = decl_function_context (exp);
9658 temp = label_rtx (exp);
9659 temp = gen_rtx_LABEL_REF (Pmode, temp);
9661 if (function != current_function_decl
9662 && function != 0)
9663 LABEL_REF_NONLOCAL_P (temp) = 1;
9665 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9666 return temp;
9669 case SSA_NAME:
9670 /* ??? ivopts calls expander, without any preparation from
9671 out-of-ssa. So fake instructions as if this was an access to the
9672 base variable. This unnecessarily allocates a pseudo, see how we can
9673 reuse it, if partition base vars have it set already. */
9674 if (!currently_expanding_to_rtl)
9676 tree var = SSA_NAME_VAR (exp);
9677 if (var && DECL_RTL_SET_P (var))
9678 return DECL_RTL (var);
9679 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9680 LAST_VIRTUAL_REGISTER + 1);
9683 g = get_gimple_for_ssa_name (exp);
9684 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9685 if (g == NULL
9686 && modifier == EXPAND_INITIALIZER
9687 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9688 && (optimize || !SSA_NAME_VAR (exp)
9689 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9690 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9691 g = SSA_NAME_DEF_STMT (exp);
9692 if (g)
9694 rtx r;
9695 location_t saved_loc = curr_insn_location ();
9696 location_t loc = gimple_location (g);
9697 if (loc != UNKNOWN_LOCATION)
9698 set_curr_insn_location (loc);
9699 ops.code = gimple_assign_rhs_code (g);
9700 switch (get_gimple_rhs_class (ops.code))
9702 case GIMPLE_TERNARY_RHS:
9703 ops.op2 = gimple_assign_rhs3 (g);
9704 /* Fallthru */
9705 case GIMPLE_BINARY_RHS:
9706 ops.op1 = gimple_assign_rhs2 (g);
9708 /* Try to expand conditonal compare. */
9709 if (targetm.gen_ccmp_first)
9711 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9712 r = expand_ccmp_expr (g);
9713 if (r)
9714 break;
9716 /* Fallthru */
9717 case GIMPLE_UNARY_RHS:
9718 ops.op0 = gimple_assign_rhs1 (g);
9719 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9720 ops.location = loc;
9721 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9722 break;
9723 case GIMPLE_SINGLE_RHS:
9725 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9726 tmode, modifier, NULL, inner_reference_p);
9727 break;
9729 default:
9730 gcc_unreachable ();
9732 set_curr_insn_location (saved_loc);
9733 if (REG_P (r) && !REG_EXPR (r))
9734 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9735 return r;
9738 ssa_name = exp;
9739 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9740 exp = SSA_NAME_VAR (ssa_name);
9741 goto expand_decl_rtl;
9743 case PARM_DECL:
9744 case VAR_DECL:
9745 /* If a static var's type was incomplete when the decl was written,
9746 but the type is complete now, lay out the decl now. */
9747 if (DECL_SIZE (exp) == 0
9748 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9749 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9750 layout_decl (exp, 0);
9752 /* fall through */
9754 case FUNCTION_DECL:
9755 case RESULT_DECL:
9756 decl_rtl = DECL_RTL (exp);
9757 expand_decl_rtl:
9758 gcc_assert (decl_rtl);
9760 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9761 settings for VECTOR_TYPE_P that might switch for the function. */
9762 if (currently_expanding_to_rtl
9763 && code == VAR_DECL && MEM_P (decl_rtl)
9764 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9765 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9766 else
9767 decl_rtl = copy_rtx (decl_rtl);
9769 /* Record writes to register variables. */
9770 if (modifier == EXPAND_WRITE
9771 && REG_P (decl_rtl)
9772 && HARD_REGISTER_P (decl_rtl))
9773 add_to_hard_reg_set (&crtl->asm_clobbers,
9774 GET_MODE (decl_rtl), REGNO (decl_rtl));
9776 /* Ensure variable marked as used even if it doesn't go through
9777 a parser. If it hasn't be used yet, write out an external
9778 definition. */
9779 if (exp)
9780 TREE_USED (exp) = 1;
9782 /* Show we haven't gotten RTL for this yet. */
9783 temp = 0;
9785 /* Variables inherited from containing functions should have
9786 been lowered by this point. */
9787 if (exp)
9788 context = decl_function_context (exp);
9789 gcc_assert (!exp
9790 || SCOPE_FILE_SCOPE_P (context)
9791 || context == current_function_decl
9792 || TREE_STATIC (exp)
9793 || DECL_EXTERNAL (exp)
9794 /* ??? C++ creates functions that are not TREE_STATIC. */
9795 || TREE_CODE (exp) == FUNCTION_DECL);
9797 /* This is the case of an array whose size is to be determined
9798 from its initializer, while the initializer is still being parsed.
9799 ??? We aren't parsing while expanding anymore. */
9801 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9802 temp = validize_mem (decl_rtl);
9804 /* If DECL_RTL is memory, we are in the normal case and the
9805 address is not valid, get the address into a register. */
9807 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9809 if (alt_rtl)
9810 *alt_rtl = decl_rtl;
9811 decl_rtl = use_anchored_address (decl_rtl);
9812 if (modifier != EXPAND_CONST_ADDRESS
9813 && modifier != EXPAND_SUM
9814 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9815 : GET_MODE (decl_rtl),
9816 XEXP (decl_rtl, 0),
9817 MEM_ADDR_SPACE (decl_rtl)))
9818 temp = replace_equiv_address (decl_rtl,
9819 copy_rtx (XEXP (decl_rtl, 0)));
9822 /* If we got something, return it. But first, set the alignment
9823 if the address is a register. */
9824 if (temp != 0)
9826 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9827 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9829 return temp;
9832 if (exp)
9833 dmode = DECL_MODE (exp);
9834 else
9835 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9837 /* If the mode of DECL_RTL does not match that of the decl,
9838 there are two cases: we are dealing with a BLKmode value
9839 that is returned in a register, or we are dealing with
9840 a promoted value. In the latter case, return a SUBREG
9841 of the wanted mode, but mark it so that we know that it
9842 was already extended. */
9843 if (REG_P (decl_rtl)
9844 && dmode != BLKmode
9845 && GET_MODE (decl_rtl) != dmode)
9847 machine_mode pmode;
9849 /* Get the signedness to be used for this variable. Ensure we get
9850 the same mode we got when the variable was declared. */
9851 if (code != SSA_NAME)
9852 pmode = promote_decl_mode (exp, &unsignedp);
9853 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9854 && gimple_code (g) == GIMPLE_CALL
9855 && !gimple_call_internal_p (g))
9856 pmode = promote_function_mode (type, mode, &unsignedp,
9857 gimple_call_fntype (g),
9859 else
9860 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9861 gcc_assert (GET_MODE (decl_rtl) == pmode);
9863 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9864 SUBREG_PROMOTED_VAR_P (temp) = 1;
9865 SUBREG_PROMOTED_SET (temp, unsignedp);
9866 return temp;
9869 return decl_rtl;
9871 case INTEGER_CST:
9872 /* Given that TYPE_PRECISION (type) is not always equal to
9873 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9874 the former to the latter according to the signedness of the
9875 type. */
9876 temp = immed_wide_int_const (wi::to_wide
9877 (exp,
9878 GET_MODE_PRECISION (TYPE_MODE (type))),
9879 TYPE_MODE (type));
9880 return temp;
9882 case VECTOR_CST:
9884 tree tmp = NULL_TREE;
9885 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9886 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9887 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9888 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9889 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9890 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9891 return const_vector_from_tree (exp);
9892 if (GET_MODE_CLASS (mode) == MODE_INT)
9894 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9895 return const_scalar_mask_from_tree (exp);
9896 else
9898 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9899 if (type_for_mode)
9900 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9901 type_for_mode, exp);
9904 if (!tmp)
9906 vec<constructor_elt, va_gc> *v;
9907 unsigned i;
9908 vec_alloc (v, VECTOR_CST_NELTS (exp));
9909 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9910 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9911 tmp = build_constructor (type, v);
9913 return expand_expr (tmp, ignore ? const0_rtx : target,
9914 tmode, modifier);
9917 case CONST_DECL:
9918 if (modifier == EXPAND_WRITE)
9920 /* Writing into CONST_DECL is always invalid, but handle it
9921 gracefully. */
9922 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9923 machine_mode address_mode = targetm.addr_space.address_mode (as);
9924 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9925 EXPAND_NORMAL, as);
9926 op0 = memory_address_addr_space (mode, op0, as);
9927 temp = gen_rtx_MEM (mode, op0);
9928 set_mem_addr_space (temp, as);
9929 return temp;
9931 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9933 case REAL_CST:
9934 /* If optimized, generate immediate CONST_DOUBLE
9935 which will be turned into memory by reload if necessary.
9937 We used to force a register so that loop.c could see it. But
9938 this does not allow gen_* patterns to perform optimizations with
9939 the constants. It also produces two insns in cases like "x = 1.0;".
9940 On most machines, floating-point constants are not permitted in
9941 many insns, so we'd end up copying it to a register in any case.
9943 Now, we do the copying in expand_binop, if appropriate. */
9944 return const_double_from_real_value (TREE_REAL_CST (exp),
9945 TYPE_MODE (TREE_TYPE (exp)));
9947 case FIXED_CST:
9948 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9949 TYPE_MODE (TREE_TYPE (exp)));
9951 case COMPLEX_CST:
9952 /* Handle evaluating a complex constant in a CONCAT target. */
9953 if (original_target && GET_CODE (original_target) == CONCAT)
9955 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9956 rtx rtarg, itarg;
9958 rtarg = XEXP (original_target, 0);
9959 itarg = XEXP (original_target, 1);
9961 /* Move the real and imaginary parts separately. */
9962 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9963 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9965 if (op0 != rtarg)
9966 emit_move_insn (rtarg, op0);
9967 if (op1 != itarg)
9968 emit_move_insn (itarg, op1);
9970 return original_target;
9973 /* fall through */
9975 case STRING_CST:
9976 temp = expand_expr_constant (exp, 1, modifier);
9978 /* temp contains a constant address.
9979 On RISC machines where a constant address isn't valid,
9980 make some insns to get that address into a register. */
9981 if (modifier != EXPAND_CONST_ADDRESS
9982 && modifier != EXPAND_INITIALIZER
9983 && modifier != EXPAND_SUM
9984 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9985 MEM_ADDR_SPACE (temp)))
9986 return replace_equiv_address (temp,
9987 copy_rtx (XEXP (temp, 0)));
9988 return temp;
9990 case SAVE_EXPR:
9992 tree val = treeop0;
9993 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9994 inner_reference_p);
9996 if (!SAVE_EXPR_RESOLVED_P (exp))
9998 /* We can indeed still hit this case, typically via builtin
9999 expanders calling save_expr immediately before expanding
10000 something. Assume this means that we only have to deal
10001 with non-BLKmode values. */
10002 gcc_assert (GET_MODE (ret) != BLKmode);
10004 val = build_decl (curr_insn_location (),
10005 VAR_DECL, NULL, TREE_TYPE (exp));
10006 DECL_ARTIFICIAL (val) = 1;
10007 DECL_IGNORED_P (val) = 1;
10008 treeop0 = val;
10009 TREE_OPERAND (exp, 0) = treeop0;
10010 SAVE_EXPR_RESOLVED_P (exp) = 1;
10012 if (!CONSTANT_P (ret))
10013 ret = copy_to_reg (ret);
10014 SET_DECL_RTL (val, ret);
10017 return ret;
10021 case CONSTRUCTOR:
10022 /* If we don't need the result, just ensure we evaluate any
10023 subexpressions. */
10024 if (ignore)
10026 unsigned HOST_WIDE_INT idx;
10027 tree value;
10029 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10030 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10032 return const0_rtx;
10035 return expand_constructor (exp, target, modifier, false);
10037 case TARGET_MEM_REF:
10039 addr_space_t as
10040 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10041 enum insn_code icode;
10042 unsigned int align;
10044 op0 = addr_for_mem_ref (exp, as, true);
10045 op0 = memory_address_addr_space (mode, op0, as);
10046 temp = gen_rtx_MEM (mode, op0);
10047 set_mem_attributes (temp, exp, 0);
10048 set_mem_addr_space (temp, as);
10049 align = get_object_alignment (exp);
10050 if (modifier != EXPAND_WRITE
10051 && modifier != EXPAND_MEMORY
10052 && mode != BLKmode
10053 && align < GET_MODE_ALIGNMENT (mode)
10054 /* If the target does not have special handling for unaligned
10055 loads of mode then it can use regular moves for them. */
10056 && ((icode = optab_handler (movmisalign_optab, mode))
10057 != CODE_FOR_nothing))
10059 struct expand_operand ops[2];
10061 /* We've already validated the memory, and we're creating a
10062 new pseudo destination. The predicates really can't fail,
10063 nor can the generator. */
10064 create_output_operand (&ops[0], NULL_RTX, mode);
10065 create_fixed_operand (&ops[1], temp);
10066 expand_insn (icode, 2, ops);
10067 temp = ops[0].value;
10069 return temp;
10072 case MEM_REF:
10074 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10075 addr_space_t as
10076 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10077 machine_mode address_mode;
10078 tree base = TREE_OPERAND (exp, 0);
10079 gimple *def_stmt;
10080 enum insn_code icode;
10081 unsigned align;
10082 /* Handle expansion of non-aliased memory with non-BLKmode. That
10083 might end up in a register. */
10084 if (mem_ref_refers_to_non_mem_p (exp))
10086 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10087 base = TREE_OPERAND (base, 0);
10088 if (offset == 0
10089 && !reverse
10090 && tree_fits_uhwi_p (TYPE_SIZE (type))
10091 && (GET_MODE_BITSIZE (DECL_MODE (base))
10092 == tree_to_uhwi (TYPE_SIZE (type))))
10093 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10094 target, tmode, modifier);
10095 if (TYPE_MODE (type) == BLKmode)
10097 temp = assign_stack_temp (DECL_MODE (base),
10098 GET_MODE_SIZE (DECL_MODE (base)));
10099 store_expr (base, temp, 0, false, false);
10100 temp = adjust_address (temp, BLKmode, offset);
10101 set_mem_size (temp, int_size_in_bytes (type));
10102 return temp;
10104 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10105 bitsize_int (offset * BITS_PER_UNIT));
10106 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10107 return expand_expr (exp, target, tmode, modifier);
10109 address_mode = targetm.addr_space.address_mode (as);
10110 base = TREE_OPERAND (exp, 0);
10111 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10113 tree mask = gimple_assign_rhs2 (def_stmt);
10114 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10115 gimple_assign_rhs1 (def_stmt), mask);
10116 TREE_OPERAND (exp, 0) = base;
10118 align = get_object_alignment (exp);
10119 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10120 op0 = memory_address_addr_space (mode, op0, as);
10121 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10123 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10124 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10125 op0 = memory_address_addr_space (mode, op0, as);
10127 temp = gen_rtx_MEM (mode, op0);
10128 set_mem_attributes (temp, exp, 0);
10129 set_mem_addr_space (temp, as);
10130 if (TREE_THIS_VOLATILE (exp))
10131 MEM_VOLATILE_P (temp) = 1;
10132 if (modifier != EXPAND_WRITE
10133 && modifier != EXPAND_MEMORY
10134 && !inner_reference_p
10135 && mode != BLKmode
10136 && align < GET_MODE_ALIGNMENT (mode))
10138 if ((icode = optab_handler (movmisalign_optab, mode))
10139 != CODE_FOR_nothing)
10141 struct expand_operand ops[2];
10143 /* We've already validated the memory, and we're creating a
10144 new pseudo destination. The predicates really can't fail,
10145 nor can the generator. */
10146 create_output_operand (&ops[0], NULL_RTX, mode);
10147 create_fixed_operand (&ops[1], temp);
10148 expand_insn (icode, 2, ops);
10149 temp = ops[0].value;
10151 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10152 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10153 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10154 (modifier == EXPAND_STACK_PARM
10155 ? NULL_RTX : target),
10156 mode, mode, false);
10158 if (reverse
10159 && modifier != EXPAND_MEMORY
10160 && modifier != EXPAND_WRITE)
10161 temp = flip_storage_order (mode, temp);
10162 return temp;
10165 case ARRAY_REF:
10168 tree array = treeop0;
10169 tree index = treeop1;
10170 tree init;
10172 /* Fold an expression like: "foo"[2].
10173 This is not done in fold so it won't happen inside &.
10174 Don't fold if this is for wide characters since it's too
10175 difficult to do correctly and this is a very rare case. */
10177 if (modifier != EXPAND_CONST_ADDRESS
10178 && modifier != EXPAND_INITIALIZER
10179 && modifier != EXPAND_MEMORY)
10181 tree t = fold_read_from_constant_string (exp);
10183 if (t)
10184 return expand_expr (t, target, tmode, modifier);
10187 /* If this is a constant index into a constant array,
10188 just get the value from the array. Handle both the cases when
10189 we have an explicit constructor and when our operand is a variable
10190 that was declared const. */
10192 if (modifier != EXPAND_CONST_ADDRESS
10193 && modifier != EXPAND_INITIALIZER
10194 && modifier != EXPAND_MEMORY
10195 && TREE_CODE (array) == CONSTRUCTOR
10196 && ! TREE_SIDE_EFFECTS (array)
10197 && TREE_CODE (index) == INTEGER_CST)
10199 unsigned HOST_WIDE_INT ix;
10200 tree field, value;
10202 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10203 field, value)
10204 if (tree_int_cst_equal (field, index))
10206 if (!TREE_SIDE_EFFECTS (value))
10207 return expand_expr (fold (value), target, tmode, modifier);
10208 break;
10212 else if (optimize >= 1
10213 && modifier != EXPAND_CONST_ADDRESS
10214 && modifier != EXPAND_INITIALIZER
10215 && modifier != EXPAND_MEMORY
10216 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10217 && TREE_CODE (index) == INTEGER_CST
10218 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10219 && (init = ctor_for_folding (array)) != error_mark_node)
10221 if (init == NULL_TREE)
10223 tree value = build_zero_cst (type);
10224 if (TREE_CODE (value) == CONSTRUCTOR)
10226 /* If VALUE is a CONSTRUCTOR, this optimization is only
10227 useful if this doesn't store the CONSTRUCTOR into
10228 memory. If it does, it is more efficient to just
10229 load the data from the array directly. */
10230 rtx ret = expand_constructor (value, target,
10231 modifier, true);
10232 if (ret == NULL_RTX)
10233 value = NULL_TREE;
10236 if (value)
10237 return expand_expr (value, target, tmode, modifier);
10239 else if (TREE_CODE (init) == CONSTRUCTOR)
10241 unsigned HOST_WIDE_INT ix;
10242 tree field, value;
10244 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10245 field, value)
10246 if (tree_int_cst_equal (field, index))
10248 if (TREE_SIDE_EFFECTS (value))
10249 break;
10251 if (TREE_CODE (value) == CONSTRUCTOR)
10253 /* If VALUE is a CONSTRUCTOR, this
10254 optimization is only useful if
10255 this doesn't store the CONSTRUCTOR
10256 into memory. If it does, it is more
10257 efficient to just load the data from
10258 the array directly. */
10259 rtx ret = expand_constructor (value, target,
10260 modifier, true);
10261 if (ret == NULL_RTX)
10262 break;
10265 return
10266 expand_expr (fold (value), target, tmode, modifier);
10269 else if (TREE_CODE (init) == STRING_CST)
10271 tree low_bound = array_ref_low_bound (exp);
10272 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10274 /* Optimize the special case of a zero lower bound.
10276 We convert the lower bound to sizetype to avoid problems
10277 with constant folding. E.g. suppose the lower bound is
10278 1 and its mode is QI. Without the conversion
10279 (ARRAY + (INDEX - (unsigned char)1))
10280 becomes
10281 (ARRAY + (-(unsigned char)1) + INDEX)
10282 which becomes
10283 (ARRAY + 255 + INDEX). Oops! */
10284 if (!integer_zerop (low_bound))
10285 index1 = size_diffop_loc (loc, index1,
10286 fold_convert_loc (loc, sizetype,
10287 low_bound));
10289 if (tree_fits_uhwi_p (index1)
10290 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10292 tree type = TREE_TYPE (TREE_TYPE (init));
10293 machine_mode mode = TYPE_MODE (type);
10295 if (GET_MODE_CLASS (mode) == MODE_INT
10296 && GET_MODE_SIZE (mode) == 1)
10297 return gen_int_mode (TREE_STRING_POINTER (init)
10298 [TREE_INT_CST_LOW (index1)],
10299 mode);
10304 goto normal_inner_ref;
10306 case COMPONENT_REF:
10307 /* If the operand is a CONSTRUCTOR, we can just extract the
10308 appropriate field if it is present. */
10309 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10311 unsigned HOST_WIDE_INT idx;
10312 tree field, value;
10314 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10315 idx, field, value)
10316 if (field == treeop1
10317 /* We can normally use the value of the field in the
10318 CONSTRUCTOR. However, if this is a bitfield in
10319 an integral mode that we can fit in a HOST_WIDE_INT,
10320 we must mask only the number of bits in the bitfield,
10321 since this is done implicitly by the constructor. If
10322 the bitfield does not meet either of those conditions,
10323 we can't do this optimization. */
10324 && (! DECL_BIT_FIELD (field)
10325 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10326 && (GET_MODE_PRECISION (DECL_MODE (field))
10327 <= HOST_BITS_PER_WIDE_INT))))
10329 if (DECL_BIT_FIELD (field)
10330 && modifier == EXPAND_STACK_PARM)
10331 target = 0;
10332 op0 = expand_expr (value, target, tmode, modifier);
10333 if (DECL_BIT_FIELD (field))
10335 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10336 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10338 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10340 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10341 imode);
10342 op0 = expand_and (imode, op0, op1, target);
10344 else
10346 int count = GET_MODE_PRECISION (imode) - bitsize;
10348 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10349 target, 0);
10350 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10351 target, 0);
10355 return op0;
10358 goto normal_inner_ref;
10360 case BIT_FIELD_REF:
10361 case ARRAY_RANGE_REF:
10362 normal_inner_ref:
10364 machine_mode mode1, mode2;
10365 HOST_WIDE_INT bitsize, bitpos;
10366 tree offset;
10367 int reversep, volatilep = 0, must_force_mem;
10368 tree tem
10369 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10370 &unsignedp, &reversep, &volatilep);
10371 rtx orig_op0, memloc;
10372 bool clear_mem_expr = false;
10374 /* If we got back the original object, something is wrong. Perhaps
10375 we are evaluating an expression too early. In any event, don't
10376 infinitely recurse. */
10377 gcc_assert (tem != exp);
10379 /* If TEM's type is a union of variable size, pass TARGET to the inner
10380 computation, since it will need a temporary and TARGET is known
10381 to have to do. This occurs in unchecked conversion in Ada. */
10382 orig_op0 = op0
10383 = expand_expr_real (tem,
10384 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10385 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10386 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10387 != INTEGER_CST)
10388 && modifier != EXPAND_STACK_PARM
10389 ? target : NULL_RTX),
10390 VOIDmode,
10391 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10392 NULL, true);
10394 /* If the field has a mode, we want to access it in the
10395 field's mode, not the computed mode.
10396 If a MEM has VOIDmode (external with incomplete type),
10397 use BLKmode for it instead. */
10398 if (MEM_P (op0))
10400 if (mode1 != VOIDmode)
10401 op0 = adjust_address (op0, mode1, 0);
10402 else if (GET_MODE (op0) == VOIDmode)
10403 op0 = adjust_address (op0, BLKmode, 0);
10406 mode2
10407 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10409 /* If we have either an offset, a BLKmode result, or a reference
10410 outside the underlying object, we must force it to memory.
10411 Such a case can occur in Ada if we have unchecked conversion
10412 of an expression from a scalar type to an aggregate type or
10413 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10414 passed a partially uninitialized object or a view-conversion
10415 to a larger size. */
10416 must_force_mem = (offset
10417 || mode1 == BLKmode
10418 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10420 /* Handle CONCAT first. */
10421 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10423 if (bitpos == 0
10424 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10426 if (reversep)
10427 op0 = flip_storage_order (GET_MODE (op0), op0);
10428 return op0;
10430 if (bitpos == 0
10431 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10432 && bitsize)
10434 op0 = XEXP (op0, 0);
10435 mode2 = GET_MODE (op0);
10437 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10438 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10439 && bitpos
10440 && bitsize)
10442 op0 = XEXP (op0, 1);
10443 bitpos = 0;
10444 mode2 = GET_MODE (op0);
10446 else
10447 /* Otherwise force into memory. */
10448 must_force_mem = 1;
10451 /* If this is a constant, put it in a register if it is a legitimate
10452 constant and we don't need a memory reference. */
10453 if (CONSTANT_P (op0)
10454 && mode2 != BLKmode
10455 && targetm.legitimate_constant_p (mode2, op0)
10456 && !must_force_mem)
10457 op0 = force_reg (mode2, op0);
10459 /* Otherwise, if this is a constant, try to force it to the constant
10460 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10461 is a legitimate constant. */
10462 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10463 op0 = validize_mem (memloc);
10465 /* Otherwise, if this is a constant or the object is not in memory
10466 and need be, put it there. */
10467 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10469 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10470 emit_move_insn (memloc, op0);
10471 op0 = memloc;
10472 clear_mem_expr = true;
10475 if (offset)
10477 machine_mode address_mode;
10478 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10479 EXPAND_SUM);
10481 gcc_assert (MEM_P (op0));
10483 address_mode = get_address_mode (op0);
10484 if (GET_MODE (offset_rtx) != address_mode)
10486 /* We cannot be sure that the RTL in offset_rtx is valid outside
10487 of a memory address context, so force it into a register
10488 before attempting to convert it to the desired mode. */
10489 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10490 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10493 /* See the comment in expand_assignment for the rationale. */
10494 if (mode1 != VOIDmode
10495 && bitpos != 0
10496 && bitsize > 0
10497 && (bitpos % bitsize) == 0
10498 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10499 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10501 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10502 bitpos = 0;
10505 op0 = offset_address (op0, offset_rtx,
10506 highest_pow2_factor (offset));
10509 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10510 record its alignment as BIGGEST_ALIGNMENT. */
10511 if (MEM_P (op0) && bitpos == 0 && offset != 0
10512 && is_aligning_offset (offset, tem))
10513 set_mem_align (op0, BIGGEST_ALIGNMENT);
10515 /* Don't forget about volatility even if this is a bitfield. */
10516 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10518 if (op0 == orig_op0)
10519 op0 = copy_rtx (op0);
10521 MEM_VOLATILE_P (op0) = 1;
10524 /* In cases where an aligned union has an unaligned object
10525 as a field, we might be extracting a BLKmode value from
10526 an integer-mode (e.g., SImode) object. Handle this case
10527 by doing the extract into an object as wide as the field
10528 (which we know to be the width of a basic mode), then
10529 storing into memory, and changing the mode to BLKmode. */
10530 if (mode1 == VOIDmode
10531 || REG_P (op0) || GET_CODE (op0) == SUBREG
10532 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10533 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10534 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10535 && modifier != EXPAND_CONST_ADDRESS
10536 && modifier != EXPAND_INITIALIZER
10537 && modifier != EXPAND_MEMORY)
10538 /* If the bitfield is volatile and the bitsize
10539 is narrower than the access size of the bitfield,
10540 we need to extract bitfields from the access. */
10541 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10542 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10543 && mode1 != BLKmode
10544 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10545 /* If the field isn't aligned enough to fetch as a memref,
10546 fetch it as a bit field. */
10547 || (mode1 != BLKmode
10548 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10549 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10550 || (MEM_P (op0)
10551 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10552 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10553 && modifier != EXPAND_MEMORY
10554 && ((modifier == EXPAND_CONST_ADDRESS
10555 || modifier == EXPAND_INITIALIZER)
10556 ? STRICT_ALIGNMENT
10557 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10558 || (bitpos % BITS_PER_UNIT != 0)))
10559 /* If the type and the field are a constant size and the
10560 size of the type isn't the same size as the bitfield,
10561 we must use bitfield operations. */
10562 || (bitsize >= 0
10563 && TYPE_SIZE (TREE_TYPE (exp))
10564 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10565 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10566 bitsize)))
10568 machine_mode ext_mode = mode;
10570 if (ext_mode == BLKmode
10571 && ! (target != 0 && MEM_P (op0)
10572 && MEM_P (target)
10573 && bitpos % BITS_PER_UNIT == 0))
10574 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10576 if (ext_mode == BLKmode)
10578 if (target == 0)
10579 target = assign_temp (type, 1, 1);
10581 /* ??? Unlike the similar test a few lines below, this one is
10582 very likely obsolete. */
10583 if (bitsize == 0)
10584 return target;
10586 /* In this case, BITPOS must start at a byte boundary and
10587 TARGET, if specified, must be a MEM. */
10588 gcc_assert (MEM_P (op0)
10589 && (!target || MEM_P (target))
10590 && !(bitpos % BITS_PER_UNIT));
10592 emit_block_move (target,
10593 adjust_address (op0, VOIDmode,
10594 bitpos / BITS_PER_UNIT),
10595 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10596 / BITS_PER_UNIT),
10597 (modifier == EXPAND_STACK_PARM
10598 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10600 return target;
10603 /* If we have nothing to extract, the result will be 0 for targets
10604 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10605 return 0 for the sake of consistency, as reading a zero-sized
10606 bitfield is valid in Ada and the value is fully specified. */
10607 if (bitsize == 0)
10608 return const0_rtx;
10610 op0 = validize_mem (op0);
10612 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10613 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10615 /* If the result has a record type and the extraction is done in
10616 an integral mode, then the field may be not aligned on a byte
10617 boundary; in this case, if it has reverse storage order, it
10618 needs to be extracted as a scalar field with reverse storage
10619 order and put back into memory order afterwards. */
10620 if (TREE_CODE (type) == RECORD_TYPE
10621 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10622 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10624 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10625 (modifier == EXPAND_STACK_PARM
10626 ? NULL_RTX : target),
10627 ext_mode, ext_mode, reversep);
10629 /* If the result has a record type and the mode of OP0 is an
10630 integral mode then, if BITSIZE is narrower than this mode
10631 and this is for big-endian data, we must put the field
10632 into the high-order bits. And we must also put it back
10633 into memory order if it has been previously reversed. */
10634 if (TREE_CODE (type) == RECORD_TYPE
10635 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10637 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10639 if (bitsize < size
10640 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10641 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10642 size - bitsize, op0, 1);
10644 if (reversep)
10645 op0 = flip_storage_order (GET_MODE (op0), op0);
10648 /* If the result type is BLKmode, store the data into a temporary
10649 of the appropriate type, but with the mode corresponding to the
10650 mode for the data we have (op0's mode). */
10651 if (mode == BLKmode)
10653 rtx new_rtx
10654 = assign_stack_temp_for_type (ext_mode,
10655 GET_MODE_BITSIZE (ext_mode),
10656 type);
10657 emit_move_insn (new_rtx, op0);
10658 op0 = copy_rtx (new_rtx);
10659 PUT_MODE (op0, BLKmode);
10662 return op0;
10665 /* If the result is BLKmode, use that to access the object
10666 now as well. */
10667 if (mode == BLKmode)
10668 mode1 = BLKmode;
10670 /* Get a reference to just this component. */
10671 if (modifier == EXPAND_CONST_ADDRESS
10672 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10673 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10674 else
10675 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10677 if (op0 == orig_op0)
10678 op0 = copy_rtx (op0);
10680 /* Don't set memory attributes if the base expression is
10681 SSA_NAME that got expanded as a MEM. In that case, we should
10682 just honor its original memory attributes. */
10683 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10684 set_mem_attributes (op0, exp, 0);
10686 if (REG_P (XEXP (op0, 0)))
10687 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10689 /* If op0 is a temporary because the original expressions was forced
10690 to memory, clear MEM_EXPR so that the original expression cannot
10691 be marked as addressable through MEM_EXPR of the temporary. */
10692 if (clear_mem_expr)
10693 set_mem_expr (op0, NULL_TREE);
10695 MEM_VOLATILE_P (op0) |= volatilep;
10697 if (reversep
10698 && modifier != EXPAND_MEMORY
10699 && modifier != EXPAND_WRITE)
10700 op0 = flip_storage_order (mode1, op0);
10702 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10703 || modifier == EXPAND_CONST_ADDRESS
10704 || modifier == EXPAND_INITIALIZER)
10705 return op0;
10707 if (target == 0)
10708 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10710 convert_move (target, op0, unsignedp);
10711 return target;
10714 case OBJ_TYPE_REF:
10715 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10717 case CALL_EXPR:
10718 /* All valid uses of __builtin_va_arg_pack () are removed during
10719 inlining. */
10720 if (CALL_EXPR_VA_ARG_PACK (exp))
10721 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10723 tree fndecl = get_callee_fndecl (exp), attr;
10725 if (fndecl
10726 && (attr = lookup_attribute ("error",
10727 DECL_ATTRIBUTES (fndecl))) != NULL)
10728 error ("%Kcall to %qs declared with attribute error: %s",
10729 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10730 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10731 if (fndecl
10732 && (attr = lookup_attribute ("warning",
10733 DECL_ATTRIBUTES (fndecl))) != NULL)
10734 warning_at (tree_nonartificial_location (exp),
10735 0, "%Kcall to %qs declared with attribute warning: %s",
10736 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10737 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10739 /* Check for a built-in function. */
10740 if (fndecl && DECL_BUILT_IN (fndecl))
10742 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10743 if (CALL_WITH_BOUNDS_P (exp))
10744 return expand_builtin_with_bounds (exp, target, subtarget,
10745 tmode, ignore);
10746 else
10747 return expand_builtin (exp, target, subtarget, tmode, ignore);
10750 return expand_call (exp, target, ignore);
10752 case VIEW_CONVERT_EXPR:
10753 op0 = NULL_RTX;
10755 /* If we are converting to BLKmode, try to avoid an intermediate
10756 temporary by fetching an inner memory reference. */
10757 if (mode == BLKmode
10758 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10759 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10760 && handled_component_p (treeop0))
10762 machine_mode mode1;
10763 HOST_WIDE_INT bitsize, bitpos;
10764 tree offset;
10765 int unsignedp, reversep, volatilep = 0;
10766 tree tem
10767 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10768 &unsignedp, &reversep, &volatilep);
10769 rtx orig_op0;
10771 /* ??? We should work harder and deal with non-zero offsets. */
10772 if (!offset
10773 && (bitpos % BITS_PER_UNIT) == 0
10774 && !reversep
10775 && bitsize >= 0
10776 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10778 /* See the normal_inner_ref case for the rationale. */
10779 orig_op0
10780 = expand_expr_real (tem,
10781 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10782 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10783 != INTEGER_CST)
10784 && modifier != EXPAND_STACK_PARM
10785 ? target : NULL_RTX),
10786 VOIDmode,
10787 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10788 NULL, true);
10790 if (MEM_P (orig_op0))
10792 op0 = orig_op0;
10794 /* Get a reference to just this component. */
10795 if (modifier == EXPAND_CONST_ADDRESS
10796 || modifier == EXPAND_SUM
10797 || modifier == EXPAND_INITIALIZER)
10798 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10799 else
10800 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10802 if (op0 == orig_op0)
10803 op0 = copy_rtx (op0);
10805 set_mem_attributes (op0, treeop0, 0);
10806 if (REG_P (XEXP (op0, 0)))
10807 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10809 MEM_VOLATILE_P (op0) |= volatilep;
10814 if (!op0)
10815 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10816 NULL, inner_reference_p);
10818 /* If the input and output modes are both the same, we are done. */
10819 if (mode == GET_MODE (op0))
10821 /* If neither mode is BLKmode, and both modes are the same size
10822 then we can use gen_lowpart. */
10823 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10824 && (GET_MODE_PRECISION (mode)
10825 == GET_MODE_PRECISION (GET_MODE (op0)))
10826 && !COMPLEX_MODE_P (GET_MODE (op0)))
10828 if (GET_CODE (op0) == SUBREG)
10829 op0 = force_reg (GET_MODE (op0), op0);
10830 temp = gen_lowpart_common (mode, op0);
10831 if (temp)
10832 op0 = temp;
10833 else
10835 if (!REG_P (op0) && !MEM_P (op0))
10836 op0 = force_reg (GET_MODE (op0), op0);
10837 op0 = gen_lowpart (mode, op0);
10840 /* If both types are integral, convert from one mode to the other. */
10841 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10842 op0 = convert_modes (mode, GET_MODE (op0), op0,
10843 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10844 /* If the output type is a bit-field type, do an extraction. */
10845 else if (reduce_bit_field)
10846 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10847 TYPE_UNSIGNED (type), NULL_RTX,
10848 mode, mode, false);
10849 /* As a last resort, spill op0 to memory, and reload it in a
10850 different mode. */
10851 else if (!MEM_P (op0))
10853 /* If the operand is not a MEM, force it into memory. Since we
10854 are going to be changing the mode of the MEM, don't call
10855 force_const_mem for constants because we don't allow pool
10856 constants to change mode. */
10857 tree inner_type = TREE_TYPE (treeop0);
10859 gcc_assert (!TREE_ADDRESSABLE (exp));
10861 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10862 target
10863 = assign_stack_temp_for_type
10864 (TYPE_MODE (inner_type),
10865 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10867 emit_move_insn (target, op0);
10868 op0 = target;
10871 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10872 output type is such that the operand is known to be aligned, indicate
10873 that it is. Otherwise, we need only be concerned about alignment for
10874 non-BLKmode results. */
10875 if (MEM_P (op0))
10877 enum insn_code icode;
10879 if (modifier != EXPAND_WRITE
10880 && modifier != EXPAND_MEMORY
10881 && !inner_reference_p
10882 && mode != BLKmode
10883 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10885 /* If the target does have special handling for unaligned
10886 loads of mode then use them. */
10887 if ((icode = optab_handler (movmisalign_optab, mode))
10888 != CODE_FOR_nothing)
10890 rtx reg;
10892 op0 = adjust_address (op0, mode, 0);
10893 /* We've already validated the memory, and we're creating a
10894 new pseudo destination. The predicates really can't
10895 fail. */
10896 reg = gen_reg_rtx (mode);
10898 /* Nor can the insn generator. */
10899 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10900 emit_insn (insn);
10901 return reg;
10903 else if (STRICT_ALIGNMENT)
10905 tree inner_type = TREE_TYPE (treeop0);
10906 HOST_WIDE_INT temp_size
10907 = MAX (int_size_in_bytes (inner_type),
10908 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10909 rtx new_rtx
10910 = assign_stack_temp_for_type (mode, temp_size, type);
10911 rtx new_with_op0_mode
10912 = adjust_address (new_rtx, GET_MODE (op0), 0);
10914 gcc_assert (!TREE_ADDRESSABLE (exp));
10916 if (GET_MODE (op0) == BLKmode)
10917 emit_block_move (new_with_op0_mode, op0,
10918 GEN_INT (GET_MODE_SIZE (mode)),
10919 (modifier == EXPAND_STACK_PARM
10920 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10921 else
10922 emit_move_insn (new_with_op0_mode, op0);
10924 op0 = new_rtx;
10928 op0 = adjust_address (op0, mode, 0);
10931 return op0;
10933 case MODIFY_EXPR:
10935 tree lhs = treeop0;
10936 tree rhs = treeop1;
10937 gcc_assert (ignore);
10939 /* Check for |= or &= of a bitfield of size one into another bitfield
10940 of size 1. In this case, (unless we need the result of the
10941 assignment) we can do this more efficiently with a
10942 test followed by an assignment, if necessary.
10944 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10945 things change so we do, this code should be enhanced to
10946 support it. */
10947 if (TREE_CODE (lhs) == COMPONENT_REF
10948 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10949 || TREE_CODE (rhs) == BIT_AND_EXPR)
10950 && TREE_OPERAND (rhs, 0) == lhs
10951 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10952 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10953 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10955 rtx_code_label *label = gen_label_rtx ();
10956 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10957 do_jump (TREE_OPERAND (rhs, 1),
10958 value ? label : 0,
10959 value ? 0 : label, -1);
10960 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10961 false);
10962 do_pending_stack_adjust ();
10963 emit_label (label);
10964 return const0_rtx;
10967 expand_assignment (lhs, rhs, false);
10968 return const0_rtx;
10971 case ADDR_EXPR:
10972 return expand_expr_addr_expr (exp, target, tmode, modifier);
10974 case REALPART_EXPR:
10975 op0 = expand_normal (treeop0);
10976 return read_complex_part (op0, false);
10978 case IMAGPART_EXPR:
10979 op0 = expand_normal (treeop0);
10980 return read_complex_part (op0, true);
10982 case RETURN_EXPR:
10983 case LABEL_EXPR:
10984 case GOTO_EXPR:
10985 case SWITCH_EXPR:
10986 case ASM_EXPR:
10987 /* Expanded in cfgexpand.c. */
10988 gcc_unreachable ();
10990 case TRY_CATCH_EXPR:
10991 case CATCH_EXPR:
10992 case EH_FILTER_EXPR:
10993 case TRY_FINALLY_EXPR:
10994 /* Lowered by tree-eh.c. */
10995 gcc_unreachable ();
10997 case WITH_CLEANUP_EXPR:
10998 case CLEANUP_POINT_EXPR:
10999 case TARGET_EXPR:
11000 case CASE_LABEL_EXPR:
11001 case VA_ARG_EXPR:
11002 case BIND_EXPR:
11003 case INIT_EXPR:
11004 case CONJ_EXPR:
11005 case COMPOUND_EXPR:
11006 case PREINCREMENT_EXPR:
11007 case PREDECREMENT_EXPR:
11008 case POSTINCREMENT_EXPR:
11009 case POSTDECREMENT_EXPR:
11010 case LOOP_EXPR:
11011 case EXIT_EXPR:
11012 case COMPOUND_LITERAL_EXPR:
11013 /* Lowered by gimplify.c. */
11014 gcc_unreachable ();
11016 case FDESC_EXPR:
11017 /* Function descriptors are not valid except for as
11018 initialization constants, and should not be expanded. */
11019 gcc_unreachable ();
11021 case WITH_SIZE_EXPR:
11022 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11023 have pulled out the size to use in whatever context it needed. */
11024 return expand_expr_real (treeop0, original_target, tmode,
11025 modifier, alt_rtl, inner_reference_p);
11027 default:
11028 return expand_expr_real_2 (&ops, target, tmode, modifier);
11032 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11033 signedness of TYPE), possibly returning the result in TARGET. */
11034 static rtx
11035 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11037 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11038 if (target && GET_MODE (target) != GET_MODE (exp))
11039 target = 0;
11040 /* For constant values, reduce using build_int_cst_type. */
11041 if (CONST_INT_P (exp))
11043 HOST_WIDE_INT value = INTVAL (exp);
11044 tree t = build_int_cst_type (type, value);
11045 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11047 else if (TYPE_UNSIGNED (type))
11049 machine_mode mode = GET_MODE (exp);
11050 rtx mask = immed_wide_int_const
11051 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11052 return expand_and (mode, exp, mask, target);
11054 else
11056 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11057 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11058 exp, count, target, 0);
11059 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11060 exp, count, target, 0);
11064 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11065 when applied to the address of EXP produces an address known to be
11066 aligned more than BIGGEST_ALIGNMENT. */
11068 static int
11069 is_aligning_offset (const_tree offset, const_tree exp)
11071 /* Strip off any conversions. */
11072 while (CONVERT_EXPR_P (offset))
11073 offset = TREE_OPERAND (offset, 0);
11075 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11076 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11077 if (TREE_CODE (offset) != BIT_AND_EXPR
11078 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11079 || compare_tree_int (TREE_OPERAND (offset, 1),
11080 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11081 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11082 return 0;
11084 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11085 It must be NEGATE_EXPR. Then strip any more conversions. */
11086 offset = TREE_OPERAND (offset, 0);
11087 while (CONVERT_EXPR_P (offset))
11088 offset = TREE_OPERAND (offset, 0);
11090 if (TREE_CODE (offset) != NEGATE_EXPR)
11091 return 0;
11093 offset = TREE_OPERAND (offset, 0);
11094 while (CONVERT_EXPR_P (offset))
11095 offset = TREE_OPERAND (offset, 0);
11097 /* This must now be the address of EXP. */
11098 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11101 /* Return the tree node if an ARG corresponds to a string constant or zero
11102 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11103 in bytes within the string that ARG is accessing. The type of the
11104 offset will be `sizetype'. */
11106 tree
11107 string_constant (tree arg, tree *ptr_offset)
11109 tree array, offset, lower_bound;
11110 STRIP_NOPS (arg);
11112 if (TREE_CODE (arg) == ADDR_EXPR)
11114 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11116 *ptr_offset = size_zero_node;
11117 return TREE_OPERAND (arg, 0);
11119 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11121 array = TREE_OPERAND (arg, 0);
11122 offset = size_zero_node;
11124 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11126 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11127 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11128 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11129 return 0;
11131 /* Check if the array has a nonzero lower bound. */
11132 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11133 if (!integer_zerop (lower_bound))
11135 /* If the offset and base aren't both constants, return 0. */
11136 if (TREE_CODE (lower_bound) != INTEGER_CST)
11137 return 0;
11138 if (TREE_CODE (offset) != INTEGER_CST)
11139 return 0;
11140 /* Adjust offset by the lower bound. */
11141 offset = size_diffop (fold_convert (sizetype, offset),
11142 fold_convert (sizetype, lower_bound));
11145 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11147 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11148 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11149 if (TREE_CODE (array) != ADDR_EXPR)
11150 return 0;
11151 array = TREE_OPERAND (array, 0);
11152 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11153 return 0;
11155 else
11156 return 0;
11158 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11160 tree arg0 = TREE_OPERAND (arg, 0);
11161 tree arg1 = TREE_OPERAND (arg, 1);
11163 STRIP_NOPS (arg0);
11164 STRIP_NOPS (arg1);
11166 if (TREE_CODE (arg0) == ADDR_EXPR
11167 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11168 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11170 array = TREE_OPERAND (arg0, 0);
11171 offset = arg1;
11173 else if (TREE_CODE (arg1) == ADDR_EXPR
11174 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11175 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11177 array = TREE_OPERAND (arg1, 0);
11178 offset = arg0;
11180 else
11181 return 0;
11183 else
11184 return 0;
11186 if (TREE_CODE (array) == STRING_CST)
11188 *ptr_offset = fold_convert (sizetype, offset);
11189 return array;
11191 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11193 int length;
11194 tree init = ctor_for_folding (array);
11196 /* Variables initialized to string literals can be handled too. */
11197 if (init == error_mark_node
11198 || !init
11199 || TREE_CODE (init) != STRING_CST)
11200 return 0;
11202 /* Avoid const char foo[4] = "abcde"; */
11203 if (DECL_SIZE_UNIT (array) == NULL_TREE
11204 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11205 || (length = TREE_STRING_LENGTH (init)) <= 0
11206 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11207 return 0;
11209 /* If variable is bigger than the string literal, OFFSET must be constant
11210 and inside of the bounds of the string literal. */
11211 offset = fold_convert (sizetype, offset);
11212 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11213 && (! tree_fits_uhwi_p (offset)
11214 || compare_tree_int (offset, length) >= 0))
11215 return 0;
11217 *ptr_offset = offset;
11218 return init;
11221 return 0;
11224 /* Generate code to calculate OPS, and exploded expression
11225 using a store-flag instruction and return an rtx for the result.
11226 OPS reflects a comparison.
11228 If TARGET is nonzero, store the result there if convenient.
11230 Return zero if there is no suitable set-flag instruction
11231 available on this machine.
11233 Once expand_expr has been called on the arguments of the comparison,
11234 we are committed to doing the store flag, since it is not safe to
11235 re-evaluate the expression. We emit the store-flag insn by calling
11236 emit_store_flag, but only expand the arguments if we have a reason
11237 to believe that emit_store_flag will be successful. If we think that
11238 it will, but it isn't, we have to simulate the store-flag with a
11239 set/jump/set sequence. */
11241 static rtx
11242 do_store_flag (sepops ops, rtx target, machine_mode mode)
11244 enum rtx_code code;
11245 tree arg0, arg1, type;
11246 machine_mode operand_mode;
11247 int unsignedp;
11248 rtx op0, op1;
11249 rtx subtarget = target;
11250 location_t loc = ops->location;
11252 arg0 = ops->op0;
11253 arg1 = ops->op1;
11255 /* Don't crash if the comparison was erroneous. */
11256 if (arg0 == error_mark_node || arg1 == error_mark_node)
11257 return const0_rtx;
11259 type = TREE_TYPE (arg0);
11260 operand_mode = TYPE_MODE (type);
11261 unsignedp = TYPE_UNSIGNED (type);
11263 /* We won't bother with BLKmode store-flag operations because it would mean
11264 passing a lot of information to emit_store_flag. */
11265 if (operand_mode == BLKmode)
11266 return 0;
11268 /* We won't bother with store-flag operations involving function pointers
11269 when function pointers must be canonicalized before comparisons. */
11270 if (targetm.have_canonicalize_funcptr_for_compare ()
11271 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11272 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11273 == FUNCTION_TYPE))
11274 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11275 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11276 == FUNCTION_TYPE))))
11277 return 0;
11279 STRIP_NOPS (arg0);
11280 STRIP_NOPS (arg1);
11282 /* For vector typed comparisons emit code to generate the desired
11283 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11284 expander for this. */
11285 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11287 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11288 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11289 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type))
11290 return expand_vec_cmp_expr (ops->type, ifexp, target);
11291 else
11293 tree if_true = constant_boolean_node (true, ops->type);
11294 tree if_false = constant_boolean_node (false, ops->type);
11295 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11296 if_false, target);
11300 /* Get the rtx comparison code to use. We know that EXP is a comparison
11301 operation of some type. Some comparisons against 1 and -1 can be
11302 converted to comparisons with zero. Do so here so that the tests
11303 below will be aware that we have a comparison with zero. These
11304 tests will not catch constants in the first operand, but constants
11305 are rarely passed as the first operand. */
11307 switch (ops->code)
11309 case EQ_EXPR:
11310 code = EQ;
11311 break;
11312 case NE_EXPR:
11313 code = NE;
11314 break;
11315 case LT_EXPR:
11316 if (integer_onep (arg1))
11317 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11318 else
11319 code = unsignedp ? LTU : LT;
11320 break;
11321 case LE_EXPR:
11322 if (! unsignedp && integer_all_onesp (arg1))
11323 arg1 = integer_zero_node, code = LT;
11324 else
11325 code = unsignedp ? LEU : LE;
11326 break;
11327 case GT_EXPR:
11328 if (! unsignedp && integer_all_onesp (arg1))
11329 arg1 = integer_zero_node, code = GE;
11330 else
11331 code = unsignedp ? GTU : GT;
11332 break;
11333 case GE_EXPR:
11334 if (integer_onep (arg1))
11335 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11336 else
11337 code = unsignedp ? GEU : GE;
11338 break;
11340 case UNORDERED_EXPR:
11341 code = UNORDERED;
11342 break;
11343 case ORDERED_EXPR:
11344 code = ORDERED;
11345 break;
11346 case UNLT_EXPR:
11347 code = UNLT;
11348 break;
11349 case UNLE_EXPR:
11350 code = UNLE;
11351 break;
11352 case UNGT_EXPR:
11353 code = UNGT;
11354 break;
11355 case UNGE_EXPR:
11356 code = UNGE;
11357 break;
11358 case UNEQ_EXPR:
11359 code = UNEQ;
11360 break;
11361 case LTGT_EXPR:
11362 code = LTGT;
11363 break;
11365 default:
11366 gcc_unreachable ();
11369 /* Put a constant second. */
11370 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11371 || TREE_CODE (arg0) == FIXED_CST)
11373 std::swap (arg0, arg1);
11374 code = swap_condition (code);
11377 /* If this is an equality or inequality test of a single bit, we can
11378 do this by shifting the bit being tested to the low-order bit and
11379 masking the result with the constant 1. If the condition was EQ,
11380 we xor it with 1. This does not require an scc insn and is faster
11381 than an scc insn even if we have it.
11383 The code to make this transformation was moved into fold_single_bit_test,
11384 so we just call into the folder and expand its result. */
11386 if ((code == NE || code == EQ)
11387 && integer_zerop (arg1)
11388 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11390 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11391 if (srcstmt
11392 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11394 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11395 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11396 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11397 gimple_assign_rhs1 (srcstmt),
11398 gimple_assign_rhs2 (srcstmt));
11399 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11400 if (temp)
11401 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11405 if (! get_subtarget (target)
11406 || GET_MODE (subtarget) != operand_mode)
11407 subtarget = 0;
11409 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11411 if (target == 0)
11412 target = gen_reg_rtx (mode);
11414 /* Try a cstore if possible. */
11415 return emit_store_flag_force (target, code, op0, op1,
11416 operand_mode, unsignedp,
11417 (TYPE_PRECISION (ops->type) == 1
11418 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11421 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11422 0 otherwise (i.e. if there is no casesi instruction).
11424 DEFAULT_PROBABILITY is the probability of jumping to the default
11425 label. */
11427 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11428 rtx table_label, rtx default_label, rtx fallback_label,
11429 int default_probability)
11431 struct expand_operand ops[5];
11432 machine_mode index_mode = SImode;
11433 rtx op1, op2, index;
11435 if (! targetm.have_casesi ())
11436 return 0;
11438 /* Convert the index to SImode. */
11439 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11441 machine_mode omode = TYPE_MODE (index_type);
11442 rtx rangertx = expand_normal (range);
11444 /* We must handle the endpoints in the original mode. */
11445 index_expr = build2 (MINUS_EXPR, index_type,
11446 index_expr, minval);
11447 minval = integer_zero_node;
11448 index = expand_normal (index_expr);
11449 if (default_label)
11450 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11451 omode, 1, default_label,
11452 default_probability);
11453 /* Now we can safely truncate. */
11454 index = convert_to_mode (index_mode, index, 0);
11456 else
11458 if (TYPE_MODE (index_type) != index_mode)
11460 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11461 index_expr = fold_convert (index_type, index_expr);
11464 index = expand_normal (index_expr);
11467 do_pending_stack_adjust ();
11469 op1 = expand_normal (minval);
11470 op2 = expand_normal (range);
11472 create_input_operand (&ops[0], index, index_mode);
11473 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11474 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11475 create_fixed_operand (&ops[3], table_label);
11476 create_fixed_operand (&ops[4], (default_label
11477 ? default_label
11478 : fallback_label));
11479 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11480 return 1;
11483 /* Attempt to generate a tablejump instruction; same concept. */
11484 /* Subroutine of the next function.
11486 INDEX is the value being switched on, with the lowest value
11487 in the table already subtracted.
11488 MODE is its expected mode (needed if INDEX is constant).
11489 RANGE is the length of the jump table.
11490 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11492 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11493 index value is out of range.
11494 DEFAULT_PROBABILITY is the probability of jumping to
11495 the default label. */
11497 static void
11498 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11499 rtx default_label, int default_probability)
11501 rtx temp, vector;
11503 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11504 cfun->cfg->max_jumptable_ents = INTVAL (range);
11506 /* Do an unsigned comparison (in the proper mode) between the index
11507 expression and the value which represents the length of the range.
11508 Since we just finished subtracting the lower bound of the range
11509 from the index expression, this comparison allows us to simultaneously
11510 check that the original index expression value is both greater than
11511 or equal to the minimum value of the range and less than or equal to
11512 the maximum value of the range. */
11514 if (default_label)
11515 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11516 default_label, default_probability);
11519 /* If index is in range, it must fit in Pmode.
11520 Convert to Pmode so we can index with it. */
11521 if (mode != Pmode)
11522 index = convert_to_mode (Pmode, index, 1);
11524 /* Don't let a MEM slip through, because then INDEX that comes
11525 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11526 and break_out_memory_refs will go to work on it and mess it up. */
11527 #ifdef PIC_CASE_VECTOR_ADDRESS
11528 if (flag_pic && !REG_P (index))
11529 index = copy_to_mode_reg (Pmode, index);
11530 #endif
11532 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11533 GET_MODE_SIZE, because this indicates how large insns are. The other
11534 uses should all be Pmode, because they are addresses. This code
11535 could fail if addresses and insns are not the same size. */
11536 index = simplify_gen_binary (MULT, Pmode, index,
11537 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11538 Pmode));
11539 index = simplify_gen_binary (PLUS, Pmode, index,
11540 gen_rtx_LABEL_REF (Pmode, table_label));
11542 #ifdef PIC_CASE_VECTOR_ADDRESS
11543 if (flag_pic)
11544 index = PIC_CASE_VECTOR_ADDRESS (index);
11545 else
11546 #endif
11547 index = memory_address (CASE_VECTOR_MODE, index);
11548 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11549 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11550 convert_move (temp, vector, 0);
11552 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11554 /* If we are generating PIC code or if the table is PC-relative, the
11555 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11556 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11557 emit_barrier ();
11561 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11562 rtx table_label, rtx default_label, int default_probability)
11564 rtx index;
11566 if (! targetm.have_tablejump ())
11567 return 0;
11569 index_expr = fold_build2 (MINUS_EXPR, index_type,
11570 fold_convert (index_type, index_expr),
11571 fold_convert (index_type, minval));
11572 index = expand_normal (index_expr);
11573 do_pending_stack_adjust ();
11575 do_tablejump (index, TYPE_MODE (index_type),
11576 convert_modes (TYPE_MODE (index_type),
11577 TYPE_MODE (TREE_TYPE (range)),
11578 expand_normal (range),
11579 TYPE_UNSIGNED (TREE_TYPE (range))),
11580 table_label, default_label, default_probability);
11581 return 1;
11584 /* Return a CONST_VECTOR rtx representing vector mask for
11585 a VECTOR_CST of booleans. */
11586 static rtx
11587 const_vector_mask_from_tree (tree exp)
11589 rtvec v;
11590 unsigned i;
11591 int units;
11592 tree elt;
11593 machine_mode inner, mode;
11595 mode = TYPE_MODE (TREE_TYPE (exp));
11596 units = GET_MODE_NUNITS (mode);
11597 inner = GET_MODE_INNER (mode);
11599 v = rtvec_alloc (units);
11601 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11603 elt = VECTOR_CST_ELT (exp, i);
11605 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11606 if (integer_zerop (elt))
11607 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11608 else if (integer_onep (elt)
11609 || integer_minus_onep (elt))
11610 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11611 else
11612 gcc_unreachable ();
11615 return gen_rtx_CONST_VECTOR (mode, v);
11618 /* Return a CONST_INT rtx representing vector mask for
11619 a VECTOR_CST of booleans. */
11620 static rtx
11621 const_scalar_mask_from_tree (tree exp)
11623 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11624 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11625 tree elt;
11626 unsigned i;
11628 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11630 elt = VECTOR_CST_ELT (exp, i);
11631 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11632 if (integer_all_onesp (elt))
11633 res = wi::set_bit (res, i);
11634 else
11635 gcc_assert (integer_zerop (elt));
11638 return immed_wide_int_const (res, mode);
11641 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11642 static rtx
11643 const_vector_from_tree (tree exp)
11645 rtvec v;
11646 unsigned i;
11647 int units;
11648 tree elt;
11649 machine_mode inner, mode;
11651 mode = TYPE_MODE (TREE_TYPE (exp));
11653 if (initializer_zerop (exp))
11654 return CONST0_RTX (mode);
11656 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11657 return const_vector_mask_from_tree (exp);
11659 units = GET_MODE_NUNITS (mode);
11660 inner = GET_MODE_INNER (mode);
11662 v = rtvec_alloc (units);
11664 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11666 elt = VECTOR_CST_ELT (exp, i);
11668 if (TREE_CODE (elt) == REAL_CST)
11669 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11670 inner);
11671 else if (TREE_CODE (elt) == FIXED_CST)
11672 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11673 inner);
11674 else
11675 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11678 return gen_rtx_CONST_VECTOR (mode, v);
11681 /* Build a decl for a personality function given a language prefix. */
11683 tree
11684 build_personality_function (const char *lang)
11686 const char *unwind_and_version;
11687 tree decl, type;
11688 char *name;
11690 switch (targetm_common.except_unwind_info (&global_options))
11692 case UI_NONE:
11693 return NULL;
11694 case UI_SJLJ:
11695 unwind_and_version = "_sj0";
11696 break;
11697 case UI_DWARF2:
11698 case UI_TARGET:
11699 unwind_and_version = "_v0";
11700 break;
11701 case UI_SEH:
11702 unwind_and_version = "_seh0";
11703 break;
11704 default:
11705 gcc_unreachable ();
11708 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11710 type = build_function_type_list (integer_type_node, integer_type_node,
11711 long_long_unsigned_type_node,
11712 ptr_type_node, ptr_type_node, NULL_TREE);
11713 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11714 get_identifier (name), type);
11715 DECL_ARTIFICIAL (decl) = 1;
11716 DECL_EXTERNAL (decl) = 1;
11717 TREE_PUBLIC (decl) = 1;
11719 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11720 are the flags assigned by targetm.encode_section_info. */
11721 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11723 return decl;
11726 /* Extracts the personality function of DECL and returns the corresponding
11727 libfunc. */
11730 get_personality_function (tree decl)
11732 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11733 enum eh_personality_kind pk;
11735 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11736 if (pk == eh_personality_none)
11737 return NULL;
11739 if (!personality
11740 && pk == eh_personality_any)
11741 personality = lang_hooks.eh_personality ();
11743 if (pk == eh_personality_lang)
11744 gcc_assert (personality != NULL_TREE);
11746 return XEXP (DECL_RTL (personality), 0);
11749 /* Returns a tree for the size of EXP in bytes. */
11751 static tree
11752 tree_expr_size (const_tree exp)
11754 if (DECL_P (exp)
11755 && DECL_SIZE_UNIT (exp) != 0)
11756 return DECL_SIZE_UNIT (exp);
11757 else
11758 return size_in_bytes (TREE_TYPE (exp));
11761 /* Return an rtx for the size in bytes of the value of EXP. */
11764 expr_size (tree exp)
11766 tree size;
11768 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11769 size = TREE_OPERAND (exp, 1);
11770 else
11772 size = tree_expr_size (exp);
11773 gcc_assert (size);
11774 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11777 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11780 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11781 if the size can vary or is larger than an integer. */
11783 static HOST_WIDE_INT
11784 int_expr_size (tree exp)
11786 tree size;
11788 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11789 size = TREE_OPERAND (exp, 1);
11790 else
11792 size = tree_expr_size (exp);
11793 gcc_assert (size);
11796 if (size == 0 || !tree_fits_shwi_p (size))
11797 return -1;
11799 return tree_to_shwi (size);