Backport r203445 from v17
[official-gcc.git] / gcc-4_8 / gcc / expr.c
blob94ae4c1277d4b37cf3629fd3f6a303936639f24a
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2013 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 "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "except.h"
31 #include "function.h"
32 #include "insn-config.h"
33 #include "insn-attr.h"
34 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
35 #include "expr.h"
36 #include "optabs.h"
37 #include "libfuncs.h"
38 #include "recog.h"
39 #include "reload.h"
40 #include "typeclass.h"
41 #include "toplev.h"
42 #include "langhooks.h"
43 #include "intl.h"
44 #include "tm_p.h"
45 #include "tree-iterator.h"
46 #include "tree-flow.h"
47 #include "target.h"
48 #include "common/common-target.h"
49 #include "timevar.h"
50 #include "df.h"
51 #include "diagnostic.h"
52 #include "ssaexpand.h"
53 #include "target-globals.h"
54 #include "params.h"
56 /* Decide whether a function's arguments should be processed
57 from first to last or from last to first.
59 They should if the stack and args grow in opposite directions, but
60 only if we have push insns. */
62 #ifdef PUSH_ROUNDING
64 #ifndef PUSH_ARGS_REVERSED
65 #if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
66 #define PUSH_ARGS_REVERSED /* If it's last to first. */
67 #endif
68 #endif
70 #endif
72 #ifndef STACK_PUSH_CODE
73 #ifdef STACK_GROWS_DOWNWARD
74 #define STACK_PUSH_CODE PRE_DEC
75 #else
76 #define STACK_PUSH_CODE PRE_INC
77 #endif
78 #endif
81 /* If this is nonzero, we do not bother generating VOLATILE
82 around volatile memory references, and we are willing to
83 output indirect addresses. If cse is to follow, we reject
84 indirect addresses so a useful potential cse is generated;
85 if it is used only once, instruction combination will produce
86 the same indirect address eventually. */
87 int cse_not_expected;
89 /* This structure is used by move_by_pieces to describe the move to
90 be performed. */
91 struct move_by_pieces_d
93 rtx to;
94 rtx to_addr;
95 int autinc_to;
96 int explicit_inc_to;
97 rtx from;
98 rtx from_addr;
99 int autinc_from;
100 int explicit_inc_from;
101 unsigned HOST_WIDE_INT len;
102 HOST_WIDE_INT offset;
103 int reverse;
106 /* This structure is used by store_by_pieces to describe the clear to
107 be performed. */
109 struct store_by_pieces_d
111 rtx to;
112 rtx to_addr;
113 int autinc_to;
114 int explicit_inc_to;
115 unsigned HOST_WIDE_INT len;
116 HOST_WIDE_INT offset;
117 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
118 void *constfundata;
119 int reverse;
122 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
123 struct move_by_pieces_d *);
124 static bool block_move_libcall_safe_for_call_parm (void);
125 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT);
126 static tree emit_block_move_libcall_fn (int);
127 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
128 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
129 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
130 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
131 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
132 struct store_by_pieces_d *);
133 static tree clear_storage_libcall_fn (int);
134 static rtx compress_float_constant (rtx, rtx);
135 static rtx get_subtarget (rtx);
136 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
137 HOST_WIDE_INT, enum machine_mode,
138 tree, int, alias_set_type);
139 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
140 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
141 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
142 enum machine_mode, tree, alias_set_type, bool);
144 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
146 static int is_aligning_offset (const_tree, const_tree);
147 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
148 enum expand_modifier);
149 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
150 static rtx do_store_flag (sepops, rtx, enum machine_mode);
151 #ifdef PUSH_ROUNDING
152 static void emit_single_push_insn (enum machine_mode, rtx, tree);
153 #endif
154 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx, int);
155 static rtx const_vector_from_tree (tree);
156 static void write_complex_part (rtx, rtx, bool);
158 /* This macro is used to determine whether move_by_pieces should be called
159 to perform a structure copy. */
160 #ifndef MOVE_BY_PIECES_P
161 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
162 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
163 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
164 #endif
166 /* This macro is used to determine whether clear_by_pieces should be
167 called to clear storage. */
168 #ifndef CLEAR_BY_PIECES_P
169 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
170 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
171 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
172 #endif
174 /* This macro is used to determine whether store_by_pieces should be
175 called to "memset" storage with byte values other than zero. */
176 #ifndef SET_BY_PIECES_P
177 #define SET_BY_PIECES_P(SIZE, ALIGN) \
178 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
179 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
180 #endif
182 /* This macro is used to determine whether store_by_pieces should be
183 called to "memcpy" storage when the source is a constant string. */
184 #ifndef STORE_BY_PIECES_P
185 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
186 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
187 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
188 #endif
190 /* This is run to set up which modes can be used
191 directly in memory and to initialize the block move optab. It is run
192 at the beginning of compilation and when the target is reinitialized. */
194 void
195 init_expr_target (void)
197 rtx insn, pat;
198 enum machine_mode mode;
199 int num_clobbers;
200 rtx mem, mem1;
201 rtx reg;
203 /* Try indexing by frame ptr and try by stack ptr.
204 It is known that on the Convex the stack ptr isn't a valid index.
205 With luck, one or the other is valid on any machine. */
206 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
207 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
209 /* A scratch register we can modify in-place below to avoid
210 useless RTL allocations. */
211 reg = gen_rtx_REG (VOIDmode, -1);
213 insn = rtx_alloc (INSN);
214 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
215 PATTERN (insn) = pat;
217 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
218 mode = (enum machine_mode) ((int) mode + 1))
220 int regno;
222 direct_load[(int) mode] = direct_store[(int) mode] = 0;
223 PUT_MODE (mem, mode);
224 PUT_MODE (mem1, mode);
225 PUT_MODE (reg, mode);
227 /* See if there is some register that can be used in this mode and
228 directly loaded or stored from memory. */
230 if (mode != VOIDmode && mode != BLKmode)
231 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
232 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
233 regno++)
235 if (! HARD_REGNO_MODE_OK (regno, mode))
236 continue;
238 SET_REGNO (reg, regno);
240 SET_SRC (pat) = mem;
241 SET_DEST (pat) = reg;
242 if (recog (pat, insn, &num_clobbers) >= 0)
243 direct_load[(int) mode] = 1;
245 SET_SRC (pat) = mem1;
246 SET_DEST (pat) = reg;
247 if (recog (pat, insn, &num_clobbers) >= 0)
248 direct_load[(int) mode] = 1;
250 SET_SRC (pat) = reg;
251 SET_DEST (pat) = mem;
252 if (recog (pat, insn, &num_clobbers) >= 0)
253 direct_store[(int) mode] = 1;
255 SET_SRC (pat) = reg;
256 SET_DEST (pat) = mem1;
257 if (recog (pat, insn, &num_clobbers) >= 0)
258 direct_store[(int) mode] = 1;
262 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
264 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
265 mode = GET_MODE_WIDER_MODE (mode))
267 enum machine_mode srcmode;
268 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
269 srcmode = GET_MODE_WIDER_MODE (srcmode))
271 enum insn_code ic;
273 ic = can_extend_p (mode, srcmode, 0);
274 if (ic == CODE_FOR_nothing)
275 continue;
277 PUT_MODE (mem, srcmode);
279 if (insn_operand_matches (ic, 1, mem))
280 float_extend_from_mem[mode][srcmode] = true;
285 /* This is run at the start of compiling a function. */
287 void
288 init_expr (void)
290 memset (&crtl->expr, 0, sizeof (crtl->expr));
293 /* Copy data from FROM to TO, where the machine modes are not the same.
294 Both modes may be integer, or both may be floating, or both may be
295 fixed-point.
296 UNSIGNEDP should be nonzero if FROM is an unsigned type.
297 This causes zero-extension instead of sign-extension. */
299 void
300 convert_move (rtx to, rtx from, int unsignedp)
302 enum machine_mode to_mode = GET_MODE (to);
303 enum machine_mode from_mode = GET_MODE (from);
304 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
305 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
306 enum insn_code code;
307 rtx libcall;
309 /* rtx code for making an equivalent value. */
310 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
311 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
314 gcc_assert (to_real == from_real);
315 gcc_assert (to_mode != BLKmode);
316 gcc_assert (from_mode != BLKmode);
318 /* If the source and destination are already the same, then there's
319 nothing to do. */
320 if (to == from)
321 return;
323 /* If FROM is a SUBREG that indicates that we have already done at least
324 the required extension, strip it. We don't handle such SUBREGs as
325 TO here. */
327 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
328 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
329 >= GET_MODE_PRECISION (to_mode))
330 && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
331 from = gen_lowpart (to_mode, from), from_mode = to_mode;
333 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
335 if (to_mode == from_mode
336 || (from_mode == VOIDmode && CONSTANT_P (from)))
338 emit_move_insn (to, from);
339 return;
342 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
344 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
346 if (VECTOR_MODE_P (to_mode))
347 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
348 else
349 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
351 emit_move_insn (to, from);
352 return;
355 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
357 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
358 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
359 return;
362 if (to_real)
364 rtx value, insns;
365 convert_optab tab;
367 gcc_assert ((GET_MODE_PRECISION (from_mode)
368 != GET_MODE_PRECISION (to_mode))
369 || (DECIMAL_FLOAT_MODE_P (from_mode)
370 != DECIMAL_FLOAT_MODE_P (to_mode)));
372 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
373 /* Conversion between decimal float and binary float, same size. */
374 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
375 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
376 tab = sext_optab;
377 else
378 tab = trunc_optab;
380 /* Try converting directly if the insn is supported. */
382 code = convert_optab_handler (tab, to_mode, from_mode);
383 if (code != CODE_FOR_nothing)
385 emit_unop_insn (code, to, from,
386 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
387 return;
390 /* Otherwise use a libcall. */
391 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
393 /* Is this conversion implemented yet? */
394 gcc_assert (libcall);
396 start_sequence ();
397 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
398 1, from, from_mode);
399 insns = get_insns ();
400 end_sequence ();
401 emit_libcall_block (insns, to, value,
402 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
403 from)
404 : gen_rtx_FLOAT_EXTEND (to_mode, from));
405 return;
408 /* Handle pointer conversion. */ /* SPEE 900220. */
409 /* Targets are expected to provide conversion insns between PxImode and
410 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
411 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
413 enum machine_mode full_mode
414 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
416 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
417 != CODE_FOR_nothing);
419 if (full_mode != from_mode)
420 from = convert_to_mode (full_mode, from, unsignedp);
421 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
422 to, from, UNKNOWN);
423 return;
425 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
427 rtx new_from;
428 enum machine_mode full_mode
429 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
430 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
431 enum insn_code icode;
433 icode = convert_optab_handler (ctab, full_mode, from_mode);
434 gcc_assert (icode != CODE_FOR_nothing);
436 if (to_mode == full_mode)
438 emit_unop_insn (icode, to, from, UNKNOWN);
439 return;
442 new_from = gen_reg_rtx (full_mode);
443 emit_unop_insn (icode, new_from, from, UNKNOWN);
445 /* else proceed to integer conversions below. */
446 from_mode = full_mode;
447 from = new_from;
450 /* Make sure both are fixed-point modes or both are not. */
451 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
452 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
453 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
455 /* If we widen from_mode to to_mode and they are in the same class,
456 we won't saturate the result.
457 Otherwise, always saturate the result to play safe. */
458 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
459 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
460 expand_fixed_convert (to, from, 0, 0);
461 else
462 expand_fixed_convert (to, from, 0, 1);
463 return;
466 /* Now both modes are integers. */
468 /* Handle expanding beyond a word. */
469 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
470 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
472 rtx insns;
473 rtx lowpart;
474 rtx fill_value;
475 rtx lowfrom;
476 int i;
477 enum machine_mode lowpart_mode;
478 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
480 /* Try converting directly if the insn is supported. */
481 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
482 != CODE_FOR_nothing)
484 /* If FROM is a SUBREG, put it into a register. Do this
485 so that we always generate the same set of insns for
486 better cse'ing; if an intermediate assignment occurred,
487 we won't be doing the operation directly on the SUBREG. */
488 if (optimize > 0 && GET_CODE (from) == SUBREG)
489 from = force_reg (from_mode, from);
490 emit_unop_insn (code, to, from, equiv_code);
491 return;
493 /* Next, try converting via full word. */
494 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
495 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
496 != CODE_FOR_nothing))
498 rtx word_to = gen_reg_rtx (word_mode);
499 if (REG_P (to))
501 if (reg_overlap_mentioned_p (to, from))
502 from = force_reg (from_mode, from);
503 emit_clobber (to);
505 convert_move (word_to, from, unsignedp);
506 emit_unop_insn (code, to, word_to, equiv_code);
507 return;
510 /* No special multiword conversion insn; do it by hand. */
511 start_sequence ();
513 /* Since we will turn this into a no conflict block, we must ensure the
514 the source does not overlap the target so force it into an isolated
515 register when maybe so. Likewise for any MEM input, since the
516 conversion sequence might require several references to it and we
517 must ensure we're getting the same value every time. */
519 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
520 from = force_reg (from_mode, from);
522 /* Get a copy of FROM widened to a word, if necessary. */
523 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
524 lowpart_mode = word_mode;
525 else
526 lowpart_mode = from_mode;
528 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
530 lowpart = gen_lowpart (lowpart_mode, to);
531 emit_move_insn (lowpart, lowfrom);
533 /* Compute the value to put in each remaining word. */
534 if (unsignedp)
535 fill_value = const0_rtx;
536 else
537 fill_value = emit_store_flag (gen_reg_rtx (word_mode),
538 LT, lowfrom, const0_rtx,
539 VOIDmode, 0, -1);
541 /* Fill the remaining words. */
542 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
544 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
545 rtx subword = operand_subword (to, index, 1, to_mode);
547 gcc_assert (subword);
549 if (fill_value != subword)
550 emit_move_insn (subword, fill_value);
553 insns = get_insns ();
554 end_sequence ();
556 emit_insn (insns);
557 return;
560 /* Truncating multi-word to a word or less. */
561 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
562 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
564 if (!((MEM_P (from)
565 && ! MEM_VOLATILE_P (from)
566 && direct_load[(int) to_mode]
567 && ! mode_dependent_address_p (XEXP (from, 0),
568 MEM_ADDR_SPACE (from)))
569 || REG_P (from)
570 || GET_CODE (from) == SUBREG))
571 from = force_reg (from_mode, from);
572 convert_move (to, gen_lowpart (word_mode, from), 0);
573 return;
576 /* Now follow all the conversions between integers
577 no more than a word long. */
579 /* For truncation, usually we can just refer to FROM in a narrower mode. */
580 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
581 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
583 if (!((MEM_P (from)
584 && ! MEM_VOLATILE_P (from)
585 && direct_load[(int) to_mode]
586 && ! mode_dependent_address_p (XEXP (from, 0),
587 MEM_ADDR_SPACE (from)))
588 || REG_P (from)
589 || GET_CODE (from) == SUBREG))
590 from = force_reg (from_mode, from);
591 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
592 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
593 from = copy_to_reg (from);
594 emit_move_insn (to, gen_lowpart (to_mode, from));
595 return;
598 /* Handle extension. */
599 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
601 /* Convert directly if that works. */
602 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
603 != CODE_FOR_nothing)
605 emit_unop_insn (code, to, from, equiv_code);
606 return;
608 else
610 enum machine_mode intermediate;
611 rtx tmp;
612 int shift_amount;
614 /* Search for a mode to convert via. */
615 for (intermediate = from_mode; intermediate != VOIDmode;
616 intermediate = GET_MODE_WIDER_MODE (intermediate))
617 if (((can_extend_p (to_mode, intermediate, unsignedp)
618 != CODE_FOR_nothing)
619 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
620 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
621 && (can_extend_p (intermediate, from_mode, unsignedp)
622 != CODE_FOR_nothing))
624 convert_move (to, convert_to_mode (intermediate, from,
625 unsignedp), unsignedp);
626 return;
629 /* No suitable intermediate mode.
630 Generate what we need with shifts. */
631 shift_amount = (GET_MODE_PRECISION (to_mode)
632 - GET_MODE_PRECISION (from_mode));
633 from = gen_lowpart (to_mode, force_reg (from_mode, from));
634 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
635 to, unsignedp);
636 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
637 to, unsignedp);
638 if (tmp != to)
639 emit_move_insn (to, tmp);
640 return;
644 /* Support special truncate insns for certain modes. */
645 if (convert_optab_handler (trunc_optab, to_mode,
646 from_mode) != CODE_FOR_nothing)
648 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
649 to, from, UNKNOWN);
650 return;
653 /* Handle truncation of volatile memrefs, and so on;
654 the things that couldn't be truncated directly,
655 and for which there was no special instruction.
657 ??? Code above formerly short-circuited this, for most integer
658 mode pairs, with a force_reg in from_mode followed by a recursive
659 call to this routine. Appears always to have been wrong. */
660 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
662 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
663 emit_move_insn (to, temp);
664 return;
667 /* Mode combination is not recognized. */
668 gcc_unreachable ();
671 /* Return an rtx for a value that would result
672 from converting X to mode MODE.
673 Both X and MODE may be floating, or both integer.
674 UNSIGNEDP is nonzero if X is an unsigned value.
675 This can be done by referring to a part of X in place
676 or by copying to a new temporary with conversion. */
679 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
681 return convert_modes (mode, VOIDmode, x, unsignedp);
684 /* Return an rtx for a value that would result
685 from converting X from mode OLDMODE to mode MODE.
686 Both modes may be floating, or both integer.
687 UNSIGNEDP is nonzero if X is an unsigned value.
689 This can be done by referring to a part of X in place
690 or by copying to a new temporary with conversion.
692 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
695 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
697 rtx temp;
699 /* If FROM is a SUBREG that indicates that we have already done at least
700 the required extension, strip it. */
702 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
703 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
704 && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
705 x = gen_lowpart (mode, x);
707 if (GET_MODE (x) != VOIDmode)
708 oldmode = GET_MODE (x);
710 if (mode == oldmode)
711 return x;
713 /* There is one case that we must handle specially: If we are converting
714 a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
715 we are to interpret the constant as unsigned, gen_lowpart will do
716 the wrong if the constant appears negative. What we want to do is
717 make the high-order word of the constant zero, not all ones. */
719 if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
720 && GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT
721 && CONST_INT_P (x) && INTVAL (x) < 0)
723 double_int val = double_int::from_uhwi (INTVAL (x));
725 /* We need to zero extend VAL. */
726 if (oldmode != VOIDmode)
727 val = val.zext (GET_MODE_BITSIZE (oldmode));
729 return immed_double_int_const (val, mode);
732 /* We can do this with a gen_lowpart if both desired and current modes
733 are integer, and this is either a constant integer, a register, or a
734 non-volatile MEM. Except for the constant case where MODE is no
735 wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand. */
737 if ((CONST_INT_P (x)
738 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT)
739 || (GET_MODE_CLASS (mode) == MODE_INT
740 && GET_MODE_CLASS (oldmode) == MODE_INT
741 && (CONST_DOUBLE_AS_INT_P (x)
742 || (GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
743 && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
744 && direct_load[(int) mode])
745 || (REG_P (x)
746 && (! HARD_REGISTER_P (x)
747 || HARD_REGNO_MODE_OK (REGNO (x), mode))
748 && TRULY_NOOP_TRUNCATION_MODES_P (mode,
749 GET_MODE (x))))))))
751 /* ?? If we don't know OLDMODE, we have to assume here that
752 X does not need sign- or zero-extension. This may not be
753 the case, but it's the best we can do. */
754 if (CONST_INT_P (x) && oldmode != VOIDmode
755 && GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (oldmode))
757 HOST_WIDE_INT val = INTVAL (x);
759 /* We must sign or zero-extend in this case. Start by
760 zero-extending, then sign extend if we need to. */
761 val &= GET_MODE_MASK (oldmode);
762 if (! unsignedp
763 && val_signbit_known_set_p (oldmode, val))
764 val |= ~GET_MODE_MASK (oldmode);
766 return gen_int_mode (val, mode);
769 return gen_lowpart (mode, x);
772 /* Converting from integer constant into mode is always equivalent to an
773 subreg operation. */
774 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
776 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
777 return simplify_gen_subreg (mode, x, oldmode, 0);
780 temp = gen_reg_rtx (mode);
781 convert_move (temp, x, unsignedp);
782 return temp;
785 /* Return the largest alignment we can use for doing a move (or store)
786 of MAX_PIECES. ALIGN is the largest alignment we could use. */
788 static unsigned int
789 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
791 enum machine_mode tmode;
793 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
794 if (align >= GET_MODE_ALIGNMENT (tmode))
795 align = GET_MODE_ALIGNMENT (tmode);
796 else
798 enum machine_mode tmode, xmode;
800 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
801 tmode != VOIDmode;
802 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
803 if (GET_MODE_SIZE (tmode) > max_pieces
804 || SLOW_UNALIGNED_ACCESS (tmode, align))
805 break;
807 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
810 return align;
813 /* Return the widest integer mode no wider than SIZE. If no such mode
814 can be found, return VOIDmode. */
816 static enum machine_mode
817 widest_int_mode_for_size (unsigned int size)
819 enum machine_mode tmode, mode = VOIDmode;
821 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
822 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
823 if (GET_MODE_SIZE (tmode) < size)
824 mode = tmode;
826 return mode;
829 /* STORE_MAX_PIECES is the number of bytes at a time that we can
830 store efficiently. Due to internal GCC limitations, this is
831 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
832 for an immediate constant. */
834 #define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
836 /* Determine whether the LEN bytes can be moved by using several move
837 instructions. Return nonzero if a call to move_by_pieces should
838 succeed. */
841 can_move_by_pieces (unsigned HOST_WIDE_INT len ATTRIBUTE_UNUSED,
842 unsigned int align ATTRIBUTE_UNUSED)
844 return MOVE_BY_PIECES_P (len, align);
847 /* Generate several move instructions to copy LEN bytes from block FROM to
848 block TO. (These are MEM rtx's with BLKmode).
850 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
851 used to push FROM to the stack.
853 ALIGN is maximum stack alignment we can assume.
855 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
856 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
857 stpcpy. */
860 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
861 unsigned int align, int endp)
863 struct move_by_pieces_d data;
864 enum machine_mode to_addr_mode;
865 enum machine_mode from_addr_mode = get_address_mode (from);
866 rtx to_addr, from_addr = XEXP (from, 0);
867 unsigned int max_size = MOVE_MAX_PIECES + 1;
868 enum insn_code icode;
870 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
872 data.offset = 0;
873 data.from_addr = from_addr;
874 if (to)
876 to_addr_mode = get_address_mode (to);
877 to_addr = XEXP (to, 0);
878 data.to = to;
879 data.autinc_to
880 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
881 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
882 data.reverse
883 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
885 else
887 to_addr_mode = VOIDmode;
888 to_addr = NULL_RTX;
889 data.to = NULL_RTX;
890 data.autinc_to = 1;
891 #ifdef STACK_GROWS_DOWNWARD
892 data.reverse = 1;
893 #else
894 data.reverse = 0;
895 #endif
897 data.to_addr = to_addr;
898 data.from = from;
899 data.autinc_from
900 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
901 || GET_CODE (from_addr) == POST_INC
902 || GET_CODE (from_addr) == POST_DEC);
904 data.explicit_inc_from = 0;
905 data.explicit_inc_to = 0;
906 if (data.reverse) data.offset = len;
907 data.len = len;
909 /* If copying requires more than two move insns,
910 copy addresses to registers (to make displacements shorter)
911 and use post-increment if available. */
912 if (!(data.autinc_from && data.autinc_to)
913 && move_by_pieces_ninsns (len, align, max_size) > 2)
915 /* Find the mode of the largest move...
916 MODE might not be used depending on the definitions of the
917 USE_* macros below. */
918 enum machine_mode mode ATTRIBUTE_UNUSED
919 = widest_int_mode_for_size (max_size);
921 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
923 data.from_addr = copy_to_mode_reg (from_addr_mode,
924 plus_constant (from_addr_mode,
925 from_addr, len));
926 data.autinc_from = 1;
927 data.explicit_inc_from = -1;
929 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
931 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
932 data.autinc_from = 1;
933 data.explicit_inc_from = 1;
935 if (!data.autinc_from && CONSTANT_P (from_addr))
936 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
937 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
939 data.to_addr = copy_to_mode_reg (to_addr_mode,
940 plus_constant (to_addr_mode,
941 to_addr, len));
942 data.autinc_to = 1;
943 data.explicit_inc_to = -1;
945 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
947 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
948 data.autinc_to = 1;
949 data.explicit_inc_to = 1;
951 if (!data.autinc_to && CONSTANT_P (to_addr))
952 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
955 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
957 /* First move what we can in the largest integer mode, then go to
958 successively smaller modes. */
960 while (max_size > 1 && data.len > 0)
962 enum machine_mode mode = widest_int_mode_for_size (max_size);
964 if (mode == VOIDmode)
965 break;
967 icode = optab_handler (mov_optab, mode);
968 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
969 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
971 max_size = GET_MODE_SIZE (mode);
974 /* The code above should have handled everything. */
975 gcc_assert (!data.len);
977 if (endp)
979 rtx to1;
981 gcc_assert (!data.reverse);
982 if (data.autinc_to)
984 if (endp == 2)
986 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
987 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
988 else
989 data.to_addr = copy_to_mode_reg (to_addr_mode,
990 plus_constant (to_addr_mode,
991 data.to_addr,
992 -1));
994 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
995 data.offset);
997 else
999 if (endp == 2)
1000 --data.offset;
1001 to1 = adjust_address (data.to, QImode, data.offset);
1003 return to1;
1005 else
1006 return data.to;
1009 /* Return number of insns required to move L bytes by pieces.
1010 ALIGN (in bits) is maximum alignment we can assume. */
1012 unsigned HOST_WIDE_INT
1013 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1014 unsigned int max_size)
1016 unsigned HOST_WIDE_INT n_insns = 0;
1018 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1020 while (max_size > 1 && l > 0)
1022 enum machine_mode mode;
1023 enum insn_code icode;
1025 mode = widest_int_mode_for_size (max_size);
1027 if (mode == VOIDmode)
1028 break;
1030 icode = optab_handler (mov_optab, mode);
1031 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1032 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1034 max_size = GET_MODE_SIZE (mode);
1037 gcc_assert (!l);
1038 return n_insns;
1041 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1042 with move instructions for mode MODE. GENFUN is the gen_... function
1043 to make a move insn for that mode. DATA has all the other info. */
1045 static void
1046 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1047 struct move_by_pieces_d *data)
1049 unsigned int size = GET_MODE_SIZE (mode);
1050 rtx to1 = NULL_RTX, from1;
1052 while (data->len >= size)
1054 if (data->reverse)
1055 data->offset -= size;
1057 if (data->to)
1059 if (data->autinc_to)
1060 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1061 data->offset);
1062 else
1063 to1 = adjust_address (data->to, mode, data->offset);
1066 if (data->autinc_from)
1067 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1068 data->offset);
1069 else
1070 from1 = adjust_address (data->from, mode, data->offset);
1072 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1073 emit_insn (gen_add2_insn (data->to_addr,
1074 GEN_INT (-(HOST_WIDE_INT)size)));
1075 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1076 emit_insn (gen_add2_insn (data->from_addr,
1077 GEN_INT (-(HOST_WIDE_INT)size)));
1079 if (data->to)
1080 emit_insn ((*genfun) (to1, from1));
1081 else
1083 #ifdef PUSH_ROUNDING
1084 emit_single_push_insn (mode, from1, NULL);
1085 #else
1086 gcc_unreachable ();
1087 #endif
1090 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1091 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
1092 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1093 emit_insn (gen_add2_insn (data->from_addr, GEN_INT (size)));
1095 if (! data->reverse)
1096 data->offset += size;
1098 data->len -= size;
1102 /* Emit code to move a block Y to a block X. This may be done with
1103 string-move instructions, with multiple scalar move instructions,
1104 or with a library call.
1106 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1107 SIZE is an rtx that says how long they are.
1108 ALIGN is the maximum alignment we can assume they have.
1109 METHOD describes what kind of copy this is, and what mechanisms may be used.
1111 Return the address of the new block, if memcpy is called and returns it,
1112 0 otherwise. */
1115 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1116 unsigned int expected_align, HOST_WIDE_INT expected_size)
1118 bool may_use_call;
1119 rtx retval = 0;
1120 unsigned int align;
1122 gcc_assert (size);
1123 if (CONST_INT_P (size)
1124 && INTVAL (size) == 0)
1125 return 0;
1127 switch (method)
1129 case BLOCK_OP_NORMAL:
1130 case BLOCK_OP_TAILCALL:
1131 may_use_call = true;
1132 break;
1134 case BLOCK_OP_CALL_PARM:
1135 may_use_call = block_move_libcall_safe_for_call_parm ();
1137 /* Make inhibit_defer_pop nonzero around the library call
1138 to force it to pop the arguments right away. */
1139 NO_DEFER_POP;
1140 break;
1142 case BLOCK_OP_NO_LIBCALL:
1143 may_use_call = false;
1144 break;
1146 default:
1147 gcc_unreachable ();
1150 gcc_assert (MEM_P (x) && MEM_P (y));
1151 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1152 gcc_assert (align >= BITS_PER_UNIT);
1154 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1155 block copy is more efficient for other large modes, e.g. DCmode. */
1156 x = adjust_address (x, BLKmode, 0);
1157 y = adjust_address (y, BLKmode, 0);
1159 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1160 can be incorrect is coming from __builtin_memcpy. */
1161 if (CONST_INT_P (size))
1163 x = shallow_copy_rtx (x);
1164 y = shallow_copy_rtx (y);
1165 set_mem_size (x, INTVAL (size));
1166 set_mem_size (y, INTVAL (size));
1169 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1170 move_by_pieces (x, y, INTVAL (size), align, 0);
1171 else if (emit_block_move_via_movmem (x, y, size, align,
1172 expected_align, expected_size))
1174 else if (may_use_call
1175 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1176 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1178 /* Since x and y are passed to a libcall, mark the corresponding
1179 tree EXPR as addressable. */
1180 tree y_expr = MEM_EXPR (y);
1181 tree x_expr = MEM_EXPR (x);
1182 if (y_expr)
1183 mark_addressable (y_expr);
1184 if (x_expr)
1185 mark_addressable (x_expr);
1186 retval = emit_block_move_via_libcall (x, y, size,
1187 method == BLOCK_OP_TAILCALL);
1190 else
1191 emit_block_move_via_loop (x, y, size, align);
1193 if (method == BLOCK_OP_CALL_PARM)
1194 OK_DEFER_POP;
1196 return retval;
1200 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1202 return emit_block_move_hints (x, y, size, method, 0, -1);
1205 /* A subroutine of emit_block_move. Returns true if calling the
1206 block move libcall will not clobber any parameters which may have
1207 already been placed on the stack. */
1209 static bool
1210 block_move_libcall_safe_for_call_parm (void)
1212 #if defined (REG_PARM_STACK_SPACE)
1213 tree fn;
1214 #endif
1216 /* If arguments are pushed on the stack, then they're safe. */
1217 if (PUSH_ARGS)
1218 return true;
1220 /* If registers go on the stack anyway, any argument is sure to clobber
1221 an outgoing argument. */
1222 #if defined (REG_PARM_STACK_SPACE)
1223 fn = emit_block_move_libcall_fn (false);
1224 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1225 depend on its argument. */
1226 (void) fn;
1227 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1228 && REG_PARM_STACK_SPACE (fn) != 0)
1229 return false;
1230 #endif
1232 /* If any argument goes in memory, then it might clobber an outgoing
1233 argument. */
1235 CUMULATIVE_ARGS args_so_far_v;
1236 cumulative_args_t args_so_far;
1237 tree fn, arg;
1239 fn = emit_block_move_libcall_fn (false);
1240 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1241 args_so_far = pack_cumulative_args (&args_so_far_v);
1243 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1244 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1246 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1247 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1248 NULL_TREE, true);
1249 if (!tmp || !REG_P (tmp))
1250 return false;
1251 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1252 return false;
1253 targetm.calls.function_arg_advance (args_so_far, mode,
1254 NULL_TREE, true);
1257 return true;
1260 /* A subroutine of emit_block_move. Expand a movmem pattern;
1261 return true if successful. */
1263 static bool
1264 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1265 unsigned int expected_align, HOST_WIDE_INT expected_size)
1267 int save_volatile_ok = volatile_ok;
1268 enum machine_mode mode;
1270 if (expected_align < align)
1271 expected_align = align;
1273 /* Since this is a move insn, we don't care about volatility. */
1274 volatile_ok = 1;
1276 /* Try the most limited insn first, because there's no point
1277 including more than one in the machine description unless
1278 the more limited one has some advantage. */
1280 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1281 mode = GET_MODE_WIDER_MODE (mode))
1283 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1285 if (code != CODE_FOR_nothing
1286 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1287 here because if SIZE is less than the mode mask, as it is
1288 returned by the macro, it will definitely be less than the
1289 actual mode mask. */
1290 && ((CONST_INT_P (size)
1291 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1292 <= (GET_MODE_MASK (mode) >> 1)))
1293 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
1295 struct expand_operand ops[6];
1296 unsigned int nops;
1298 /* ??? When called via emit_block_move_for_call, it'd be
1299 nice if there were some way to inform the backend, so
1300 that it doesn't fail the expansion because it thinks
1301 emitting the libcall would be more efficient. */
1302 nops = insn_data[(int) code].n_generator_args;
1303 gcc_assert (nops == 4 || nops == 6);
1305 create_fixed_operand (&ops[0], x);
1306 create_fixed_operand (&ops[1], y);
1307 /* The check above guarantees that this size conversion is valid. */
1308 create_convert_operand_to (&ops[2], size, mode, true);
1309 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1310 if (nops == 6)
1312 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1313 create_integer_operand (&ops[5], expected_size);
1315 if (maybe_expand_insn (code, nops, ops))
1317 volatile_ok = save_volatile_ok;
1318 return true;
1323 volatile_ok = save_volatile_ok;
1324 return false;
1327 /* A subroutine of emit_block_move. Expand a call to memcpy.
1328 Return the return value from memcpy, 0 otherwise. */
1331 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1333 rtx dst_addr, src_addr;
1334 tree call_expr, fn, src_tree, dst_tree, size_tree;
1335 enum machine_mode size_mode;
1336 rtx retval;
1338 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1339 pseudos. We can then place those new pseudos into a VAR_DECL and
1340 use them later. */
1342 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1343 src_addr = copy_addr_to_reg (XEXP (src, 0));
1345 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1346 src_addr = convert_memory_address (ptr_mode, src_addr);
1348 dst_tree = make_tree (ptr_type_node, dst_addr);
1349 src_tree = make_tree (ptr_type_node, src_addr);
1351 size_mode = TYPE_MODE (sizetype);
1353 size = convert_to_mode (size_mode, size, 1);
1354 size = copy_to_mode_reg (size_mode, size);
1356 /* It is incorrect to use the libcall calling conventions to call
1357 memcpy in this context. This could be a user call to memcpy and
1358 the user may wish to examine the return value from memcpy. For
1359 targets where libcalls and normal calls have different conventions
1360 for returning pointers, we could end up generating incorrect code. */
1362 size_tree = make_tree (sizetype, size);
1364 fn = emit_block_move_libcall_fn (true);
1365 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1366 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1368 retval = expand_normal (call_expr);
1370 return retval;
1373 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1374 for the function we use for block copies. */
1376 static GTY(()) tree block_move_fn;
1378 void
1379 init_block_move_fn (const char *asmspec)
1381 if (!block_move_fn)
1383 tree args, fn, attrs, attr_args;
1385 fn = get_identifier ("memcpy");
1386 args = build_function_type_list (ptr_type_node, ptr_type_node,
1387 const_ptr_type_node, sizetype,
1388 NULL_TREE);
1390 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1391 DECL_EXTERNAL (fn) = 1;
1392 TREE_PUBLIC (fn) = 1;
1393 DECL_ARTIFICIAL (fn) = 1;
1394 TREE_NOTHROW (fn) = 1;
1395 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1396 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1398 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1399 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1401 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1403 block_move_fn = fn;
1406 if (asmspec)
1407 set_user_assembler_name (block_move_fn, asmspec);
1410 static tree
1411 emit_block_move_libcall_fn (int for_call)
1413 static bool emitted_extern;
1415 if (!block_move_fn)
1416 init_block_move_fn (NULL);
1418 if (for_call && !emitted_extern)
1420 emitted_extern = true;
1421 make_decl_rtl (block_move_fn);
1424 return block_move_fn;
1427 /* A subroutine of emit_block_move. Copy the data via an explicit
1428 loop. This is used only when libcalls are forbidden. */
1429 /* ??? It'd be nice to copy in hunks larger than QImode. */
1431 static void
1432 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1433 unsigned int align ATTRIBUTE_UNUSED)
1435 rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
1436 enum machine_mode x_addr_mode = get_address_mode (x);
1437 enum machine_mode y_addr_mode = get_address_mode (y);
1438 enum machine_mode iter_mode;
1440 iter_mode = GET_MODE (size);
1441 if (iter_mode == VOIDmode)
1442 iter_mode = word_mode;
1444 top_label = gen_label_rtx ();
1445 cmp_label = gen_label_rtx ();
1446 iter = gen_reg_rtx (iter_mode);
1448 emit_move_insn (iter, const0_rtx);
1450 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1451 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1452 do_pending_stack_adjust ();
1454 emit_jump (cmp_label);
1455 emit_label (top_label);
1457 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1458 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1460 if (x_addr_mode != y_addr_mode)
1461 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1462 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1464 x = change_address (x, QImode, x_addr);
1465 y = change_address (y, QImode, y_addr);
1467 emit_move_insn (x, y);
1469 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1470 true, OPTAB_LIB_WIDEN);
1471 if (tmp != iter)
1472 emit_move_insn (iter, tmp);
1474 emit_label (cmp_label);
1476 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1477 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1480 /* Copy all or part of a value X into registers starting at REGNO.
1481 The number of registers to be filled is NREGS. */
1483 void
1484 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1486 int i;
1487 #ifdef HAVE_load_multiple
1488 rtx pat;
1489 rtx last;
1490 #endif
1492 if (nregs == 0)
1493 return;
1495 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1496 x = validize_mem (force_const_mem (mode, x));
1498 /* See if the machine can do this with a load multiple insn. */
1499 #ifdef HAVE_load_multiple
1500 if (HAVE_load_multiple)
1502 last = get_last_insn ();
1503 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1504 GEN_INT (nregs));
1505 if (pat)
1507 emit_insn (pat);
1508 return;
1510 else
1511 delete_insns_since (last);
1513 #endif
1515 for (i = 0; i < nregs; i++)
1516 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1517 operand_subword_force (x, i, mode));
1520 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1521 The number of registers to be filled is NREGS. */
1523 void
1524 move_block_from_reg (int regno, rtx x, int nregs)
1526 int i;
1528 if (nregs == 0)
1529 return;
1531 /* See if the machine can do this with a store multiple insn. */
1532 #ifdef HAVE_store_multiple
1533 if (HAVE_store_multiple)
1535 rtx last = get_last_insn ();
1536 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1537 GEN_INT (nregs));
1538 if (pat)
1540 emit_insn (pat);
1541 return;
1543 else
1544 delete_insns_since (last);
1546 #endif
1548 for (i = 0; i < nregs; i++)
1550 rtx tem = operand_subword (x, i, 1, BLKmode);
1552 gcc_assert (tem);
1554 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1558 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1559 ORIG, where ORIG is a non-consecutive group of registers represented by
1560 a PARALLEL. The clone is identical to the original except in that the
1561 original set of registers is replaced by a new set of pseudo registers.
1562 The new set has the same modes as the original set. */
1565 gen_group_rtx (rtx orig)
1567 int i, length;
1568 rtx *tmps;
1570 gcc_assert (GET_CODE (orig) == PARALLEL);
1572 length = XVECLEN (orig, 0);
1573 tmps = XALLOCAVEC (rtx, length);
1575 /* Skip a NULL entry in first slot. */
1576 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1578 if (i)
1579 tmps[0] = 0;
1581 for (; i < length; i++)
1583 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1584 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1586 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1589 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1592 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1593 except that values are placed in TMPS[i], and must later be moved
1594 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1596 static void
1597 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1599 rtx src;
1600 int start, i;
1601 enum machine_mode m = GET_MODE (orig_src);
1603 gcc_assert (GET_CODE (dst) == PARALLEL);
1605 if (m != VOIDmode
1606 && !SCALAR_INT_MODE_P (m)
1607 && !MEM_P (orig_src)
1608 && GET_CODE (orig_src) != CONCAT)
1610 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1611 if (imode == BLKmode)
1612 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1613 else
1614 src = gen_reg_rtx (imode);
1615 if (imode != BLKmode)
1616 src = gen_lowpart (GET_MODE (orig_src), src);
1617 emit_move_insn (src, orig_src);
1618 /* ...and back again. */
1619 if (imode != BLKmode)
1620 src = gen_lowpart (imode, src);
1621 emit_group_load_1 (tmps, dst, src, type, ssize);
1622 return;
1625 /* Check for a NULL entry, used to indicate that the parameter goes
1626 both on the stack and in registers. */
1627 if (XEXP (XVECEXP (dst, 0, 0), 0))
1628 start = 0;
1629 else
1630 start = 1;
1632 /* Process the pieces. */
1633 for (i = start; i < XVECLEN (dst, 0); i++)
1635 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1636 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1637 unsigned int bytelen = GET_MODE_SIZE (mode);
1638 int shift = 0;
1640 /* Handle trailing fragments that run over the size of the struct. */
1641 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1643 /* Arrange to shift the fragment to where it belongs.
1644 extract_bit_field loads to the lsb of the reg. */
1645 if (
1646 #ifdef BLOCK_REG_PADDING
1647 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1648 == (BYTES_BIG_ENDIAN ? upward : downward)
1649 #else
1650 BYTES_BIG_ENDIAN
1651 #endif
1653 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1654 bytelen = ssize - bytepos;
1655 gcc_assert (bytelen > 0);
1658 /* If we won't be loading directly from memory, protect the real source
1659 from strange tricks we might play; but make sure that the source can
1660 be loaded directly into the destination. */
1661 src = orig_src;
1662 if (!MEM_P (orig_src)
1663 && (!CONSTANT_P (orig_src)
1664 || (GET_MODE (orig_src) != mode
1665 && GET_MODE (orig_src) != VOIDmode)))
1667 if (GET_MODE (orig_src) == VOIDmode)
1668 src = gen_reg_rtx (mode);
1669 else
1670 src = gen_reg_rtx (GET_MODE (orig_src));
1672 emit_move_insn (src, orig_src);
1675 /* Optimize the access just a bit. */
1676 if (MEM_P (src)
1677 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1678 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1679 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1680 && bytelen == GET_MODE_SIZE (mode))
1682 tmps[i] = gen_reg_rtx (mode);
1683 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1685 else if (COMPLEX_MODE_P (mode)
1686 && GET_MODE (src) == mode
1687 && bytelen == GET_MODE_SIZE (mode))
1688 /* Let emit_move_complex do the bulk of the work. */
1689 tmps[i] = src;
1690 else if (GET_CODE (src) == CONCAT)
1692 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1693 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1695 if ((bytepos == 0 && bytelen == slen0)
1696 || (bytepos != 0 && bytepos + bytelen <= slen))
1698 /* The following assumes that the concatenated objects all
1699 have the same size. In this case, a simple calculation
1700 can be used to determine the object and the bit field
1701 to be extracted. */
1702 tmps[i] = XEXP (src, bytepos / slen0);
1703 if (! CONSTANT_P (tmps[i])
1704 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1705 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1706 (bytepos % slen0) * BITS_PER_UNIT,
1707 1, false, NULL_RTX, mode, mode);
1709 else
1711 rtx mem;
1713 gcc_assert (!bytepos);
1714 mem = assign_stack_temp (GET_MODE (src), slen);
1715 emit_move_insn (mem, src);
1716 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1717 0, 1, false, NULL_RTX, mode, mode);
1720 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1721 SIMD register, which is currently broken. While we get GCC
1722 to emit proper RTL for these cases, let's dump to memory. */
1723 else if (VECTOR_MODE_P (GET_MODE (dst))
1724 && REG_P (src))
1726 int slen = GET_MODE_SIZE (GET_MODE (src));
1727 rtx mem;
1729 mem = assign_stack_temp (GET_MODE (src), slen);
1730 emit_move_insn (mem, src);
1731 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1733 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1734 && XVECLEN (dst, 0) > 1)
1735 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE(dst), bytepos);
1736 else if (CONSTANT_P (src))
1738 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1740 if (len == ssize)
1741 tmps[i] = src;
1742 else
1744 rtx first, second;
1746 gcc_assert (2 * len == ssize);
1747 split_double (src, &first, &second);
1748 if (i)
1749 tmps[i] = second;
1750 else
1751 tmps[i] = first;
1754 else if (REG_P (src) && GET_MODE (src) == mode)
1755 tmps[i] = src;
1756 else
1757 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1758 bytepos * BITS_PER_UNIT, 1, false, NULL_RTX,
1759 mode, mode);
1761 if (shift)
1762 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1763 shift, tmps[i], 0);
1767 /* Emit code to move a block SRC of type TYPE to a block DST,
1768 where DST is non-consecutive registers represented by a PARALLEL.
1769 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1770 if not known. */
1772 void
1773 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1775 rtx *tmps;
1776 int i;
1778 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1779 emit_group_load_1 (tmps, dst, src, type, ssize);
1781 /* Copy the extracted pieces into the proper (probable) hard regs. */
1782 for (i = 0; i < XVECLEN (dst, 0); i++)
1784 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1785 if (d == NULL)
1786 continue;
1787 emit_move_insn (d, tmps[i]);
1791 /* Similar, but load SRC into new pseudos in a format that looks like
1792 PARALLEL. This can later be fed to emit_group_move to get things
1793 in the right place. */
1796 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1798 rtvec vec;
1799 int i;
1801 vec = rtvec_alloc (XVECLEN (parallel, 0));
1802 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1804 /* Convert the vector to look just like the original PARALLEL, except
1805 with the computed values. */
1806 for (i = 0; i < XVECLEN (parallel, 0); i++)
1808 rtx e = XVECEXP (parallel, 0, i);
1809 rtx d = XEXP (e, 0);
1811 if (d)
1813 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1814 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1816 RTVEC_ELT (vec, i) = e;
1819 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1822 /* Emit code to move a block SRC to block DST, where SRC and DST are
1823 non-consecutive groups of registers, each represented by a PARALLEL. */
1825 void
1826 emit_group_move (rtx dst, rtx src)
1828 int i;
1830 gcc_assert (GET_CODE (src) == PARALLEL
1831 && GET_CODE (dst) == PARALLEL
1832 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1834 /* Skip first entry if NULL. */
1835 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1836 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1837 XEXP (XVECEXP (src, 0, i), 0));
1840 /* Move a group of registers represented by a PARALLEL into pseudos. */
1843 emit_group_move_into_temps (rtx src)
1845 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1846 int i;
1848 for (i = 0; i < XVECLEN (src, 0); i++)
1850 rtx e = XVECEXP (src, 0, i);
1851 rtx d = XEXP (e, 0);
1853 if (d)
1854 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1855 RTVEC_ELT (vec, i) = e;
1858 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1861 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1862 where SRC is non-consecutive registers represented by a PARALLEL.
1863 SSIZE represents the total size of block ORIG_DST, or -1 if not
1864 known. */
1866 void
1867 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1869 rtx *tmps, dst;
1870 int start, finish, i;
1871 enum machine_mode m = GET_MODE (orig_dst);
1873 gcc_assert (GET_CODE (src) == PARALLEL);
1875 if (!SCALAR_INT_MODE_P (m)
1876 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1878 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1879 if (imode == BLKmode)
1880 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1881 else
1882 dst = gen_reg_rtx (imode);
1883 emit_group_store (dst, src, type, ssize);
1884 if (imode != BLKmode)
1885 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1886 emit_move_insn (orig_dst, dst);
1887 return;
1890 /* Check for a NULL entry, used to indicate that the parameter goes
1891 both on the stack and in registers. */
1892 if (XEXP (XVECEXP (src, 0, 0), 0))
1893 start = 0;
1894 else
1895 start = 1;
1896 finish = XVECLEN (src, 0);
1898 tmps = XALLOCAVEC (rtx, finish);
1900 /* Copy the (probable) hard regs into pseudos. */
1901 for (i = start; i < finish; i++)
1903 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1904 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1906 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1907 emit_move_insn (tmps[i], reg);
1909 else
1910 tmps[i] = reg;
1913 /* If we won't be storing directly into memory, protect the real destination
1914 from strange tricks we might play. */
1915 dst = orig_dst;
1916 if (GET_CODE (dst) == PARALLEL)
1918 rtx temp;
1920 /* We can get a PARALLEL dst if there is a conditional expression in
1921 a return statement. In that case, the dst and src are the same,
1922 so no action is necessary. */
1923 if (rtx_equal_p (dst, src))
1924 return;
1926 /* It is unclear if we can ever reach here, but we may as well handle
1927 it. Allocate a temporary, and split this into a store/load to/from
1928 the temporary. */
1930 temp = assign_stack_temp (GET_MODE (dst), ssize);
1931 emit_group_store (temp, src, type, ssize);
1932 emit_group_load (dst, temp, type, ssize);
1933 return;
1935 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1937 enum machine_mode outer = GET_MODE (dst);
1938 enum machine_mode inner;
1939 HOST_WIDE_INT bytepos;
1940 bool done = false;
1941 rtx temp;
1943 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1944 dst = gen_reg_rtx (outer);
1946 /* Make life a bit easier for combine. */
1947 /* If the first element of the vector is the low part
1948 of the destination mode, use a paradoxical subreg to
1949 initialize the destination. */
1950 if (start < finish)
1952 inner = GET_MODE (tmps[start]);
1953 bytepos = subreg_lowpart_offset (inner, outer);
1954 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1956 temp = simplify_gen_subreg (outer, tmps[start],
1957 inner, 0);
1958 if (temp)
1960 emit_move_insn (dst, temp);
1961 done = true;
1962 start++;
1967 /* If the first element wasn't the low part, try the last. */
1968 if (!done
1969 && start < finish - 1)
1971 inner = GET_MODE (tmps[finish - 1]);
1972 bytepos = subreg_lowpart_offset (inner, outer);
1973 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1975 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1976 inner, 0);
1977 if (temp)
1979 emit_move_insn (dst, temp);
1980 done = true;
1981 finish--;
1986 /* Otherwise, simply initialize the result to zero. */
1987 if (!done)
1988 emit_move_insn (dst, CONST0_RTX (outer));
1991 /* Process the pieces. */
1992 for (i = start; i < finish; i++)
1994 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
1995 enum machine_mode mode = GET_MODE (tmps[i]);
1996 unsigned int bytelen = GET_MODE_SIZE (mode);
1997 unsigned int adj_bytelen;
1998 rtx dest = dst;
2000 /* Handle trailing fragments that run over the size of the struct. */
2001 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2002 adj_bytelen = ssize - bytepos;
2003 else
2004 adj_bytelen = bytelen;
2006 if (GET_CODE (dst) == CONCAT)
2008 if (bytepos + adj_bytelen
2009 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2010 dest = XEXP (dst, 0);
2011 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2013 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2014 dest = XEXP (dst, 1);
2016 else
2018 enum machine_mode dest_mode = GET_MODE (dest);
2019 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2021 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2023 if (GET_MODE_ALIGNMENT (dest_mode)
2024 >= GET_MODE_ALIGNMENT (tmp_mode))
2026 dest = assign_stack_temp (dest_mode,
2027 GET_MODE_SIZE (dest_mode));
2028 emit_move_insn (adjust_address (dest,
2029 tmp_mode,
2030 bytepos),
2031 tmps[i]);
2032 dst = dest;
2034 else
2036 dest = assign_stack_temp (tmp_mode,
2037 GET_MODE_SIZE (tmp_mode));
2038 emit_move_insn (dest, tmps[i]);
2039 dst = adjust_address (dest, dest_mode, bytepos);
2041 break;
2045 /* Handle trailing fragments that run over the size of the struct. */
2046 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2048 /* store_bit_field always takes its value from the lsb.
2049 Move the fragment to the lsb if it's not already there. */
2050 if (
2051 #ifdef BLOCK_REG_PADDING
2052 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2053 == (BYTES_BIG_ENDIAN ? upward : downward)
2054 #else
2055 BYTES_BIG_ENDIAN
2056 #endif
2059 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2060 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2061 shift, tmps[i], 0);
2064 /* Make sure not to write past the end of the struct. */
2065 store_bit_field (dest,
2066 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2067 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2068 VOIDmode, tmps[i]);
2071 /* Optimize the access just a bit. */
2072 else if (MEM_P (dest)
2073 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2074 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2075 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2076 && bytelen == GET_MODE_SIZE (mode))
2077 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2079 else
2080 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2081 0, 0, mode, tmps[i]);
2084 /* Copy from the pseudo into the (probable) hard reg. */
2085 if (orig_dst != dst)
2086 emit_move_insn (orig_dst, dst);
2089 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2090 of the value stored in X. */
2093 maybe_emit_group_store (rtx x, tree type)
2095 enum machine_mode mode = TYPE_MODE (type);
2096 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2097 if (GET_CODE (x) == PARALLEL)
2099 rtx result = gen_reg_rtx (mode);
2100 emit_group_store (result, x, type, int_size_in_bytes (type));
2101 return result;
2103 return x;
2106 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2108 This is used on targets that return BLKmode values in registers. */
2110 void
2111 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2113 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2114 rtx src = NULL, dst = NULL;
2115 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2116 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2117 enum machine_mode mode = GET_MODE (srcreg);
2118 enum machine_mode tmode = GET_MODE (target);
2119 enum machine_mode copy_mode;
2121 /* BLKmode registers created in the back-end shouldn't have survived. */
2122 gcc_assert (mode != BLKmode);
2124 /* If the structure doesn't take up a whole number of words, see whether
2125 SRCREG is padded on the left or on the right. If it's on the left,
2126 set PADDING_CORRECTION to the number of bits to skip.
2128 In most ABIs, the structure will be returned at the least end of
2129 the register, which translates to right padding on little-endian
2130 targets and left padding on big-endian targets. The opposite
2131 holds if the structure is returned at the most significant
2132 end of the register. */
2133 if (bytes % UNITS_PER_WORD != 0
2134 && (targetm.calls.return_in_msb (type)
2135 ? !BYTES_BIG_ENDIAN
2136 : BYTES_BIG_ENDIAN))
2137 padding_correction
2138 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2140 /* We can use a single move if we have an exact mode for the size. */
2141 else if (MEM_P (target)
2142 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2143 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2144 && bytes == GET_MODE_SIZE (mode))
2146 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2147 return;
2150 /* And if we additionally have the same mode for a register. */
2151 else if (REG_P (target)
2152 && GET_MODE (target) == mode
2153 && bytes == GET_MODE_SIZE (mode))
2155 emit_move_insn (target, srcreg);
2156 return;
2159 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2160 into a new pseudo which is a full word. */
2161 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2163 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2164 mode = word_mode;
2167 /* Copy the structure BITSIZE bits at a time. If the target lives in
2168 memory, take care of not reading/writing past its end by selecting
2169 a copy mode suited to BITSIZE. This should always be possible given
2170 how it is computed.
2172 If the target lives in register, make sure not to select a copy mode
2173 larger than the mode of the register.
2175 We could probably emit more efficient code for machines which do not use
2176 strict alignment, but it doesn't seem worth the effort at the current
2177 time. */
2179 copy_mode = word_mode;
2180 if (MEM_P (target))
2182 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2183 if (mem_mode != BLKmode)
2184 copy_mode = mem_mode;
2186 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2187 copy_mode = tmode;
2189 for (bitpos = 0, xbitpos = padding_correction;
2190 bitpos < bytes * BITS_PER_UNIT;
2191 bitpos += bitsize, xbitpos += bitsize)
2193 /* We need a new source operand each time xbitpos is on a
2194 word boundary and when xbitpos == padding_correction
2195 (the first time through). */
2196 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2197 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2199 /* We need a new destination operand each time bitpos is on
2200 a word boundary. */
2201 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2202 dst = target;
2203 else if (bitpos % BITS_PER_WORD == 0)
2204 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2206 /* Use xbitpos for the source extraction (right justified) and
2207 bitpos for the destination store (left justified). */
2208 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2209 extract_bit_field (src, bitsize,
2210 xbitpos % BITS_PER_WORD, 1, false,
2211 NULL_RTX, copy_mode, copy_mode));
2215 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2216 register if it contains any data, otherwise return null.
2218 This is used on targets that return BLKmode values in registers. */
2221 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2223 int i, n_regs;
2224 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2225 unsigned int bitsize;
2226 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2227 enum machine_mode dst_mode;
2229 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2231 x = expand_normal (src);
2233 bytes = int_size_in_bytes (TREE_TYPE (src));
2234 if (bytes == 0)
2235 return NULL_RTX;
2237 /* If the structure doesn't take up a whole number of words, see
2238 whether the register value should be padded on the left or on
2239 the right. Set PADDING_CORRECTION to the number of padding
2240 bits needed on the left side.
2242 In most ABIs, the structure will be returned at the least end of
2243 the register, which translates to right padding on little-endian
2244 targets and left padding on big-endian targets. The opposite
2245 holds if the structure is returned at the most significant
2246 end of the register. */
2247 if (bytes % UNITS_PER_WORD != 0
2248 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2249 ? !BYTES_BIG_ENDIAN
2250 : BYTES_BIG_ENDIAN))
2251 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2252 * BITS_PER_UNIT));
2254 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2255 dst_words = XALLOCAVEC (rtx, n_regs);
2256 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2258 /* Copy the structure BITSIZE bits at a time. */
2259 for (bitpos = 0, xbitpos = padding_correction;
2260 bitpos < bytes * BITS_PER_UNIT;
2261 bitpos += bitsize, xbitpos += bitsize)
2263 /* We need a new destination pseudo each time xbitpos is
2264 on a word boundary and when xbitpos == padding_correction
2265 (the first time through). */
2266 if (xbitpos % BITS_PER_WORD == 0
2267 || xbitpos == padding_correction)
2269 /* Generate an appropriate register. */
2270 dst_word = gen_reg_rtx (word_mode);
2271 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2273 /* Clear the destination before we move anything into it. */
2274 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2277 /* We need a new source operand each time bitpos is on a word
2278 boundary. */
2279 if (bitpos % BITS_PER_WORD == 0)
2280 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2282 /* Use bitpos for the source extraction (left justified) and
2283 xbitpos for the destination store (right justified). */
2284 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2285 0, 0, word_mode,
2286 extract_bit_field (src_word, bitsize,
2287 bitpos % BITS_PER_WORD, 1, false,
2288 NULL_RTX, word_mode, word_mode));
2291 if (mode == BLKmode)
2293 /* Find the smallest integer mode large enough to hold the
2294 entire structure. */
2295 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2296 mode != VOIDmode;
2297 mode = GET_MODE_WIDER_MODE (mode))
2298 /* Have we found a large enough mode? */
2299 if (GET_MODE_SIZE (mode) >= bytes)
2300 break;
2302 /* A suitable mode should have been found. */
2303 gcc_assert (mode != VOIDmode);
2306 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2307 dst_mode = word_mode;
2308 else
2309 dst_mode = mode;
2310 dst = gen_reg_rtx (dst_mode);
2312 for (i = 0; i < n_regs; i++)
2313 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2315 if (mode != dst_mode)
2316 dst = gen_lowpart (mode, dst);
2318 return dst;
2321 /* Add a USE expression for REG to the (possibly empty) list pointed
2322 to by CALL_FUSAGE. REG must denote a hard register. */
2324 void
2325 use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2327 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2329 *call_fusage
2330 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2333 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2334 starting at REGNO. All of these registers must be hard registers. */
2336 void
2337 use_regs (rtx *call_fusage, int regno, int nregs)
2339 int i;
2341 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2343 for (i = 0; i < nregs; i++)
2344 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2347 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2348 PARALLEL REGS. This is for calls that pass values in multiple
2349 non-contiguous locations. The Irix 6 ABI has examples of this. */
2351 void
2352 use_group_regs (rtx *call_fusage, rtx regs)
2354 int i;
2356 for (i = 0; i < XVECLEN (regs, 0); i++)
2358 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2360 /* A NULL entry means the parameter goes both on the stack and in
2361 registers. This can also be a MEM for targets that pass values
2362 partially on the stack and partially in registers. */
2363 if (reg != 0 && REG_P (reg))
2364 use_reg (call_fusage, reg);
2368 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2369 assigment and the code of the expresion on the RHS is CODE. Return
2370 NULL otherwise. */
2372 static gimple
2373 get_def_for_expr (tree name, enum tree_code code)
2375 gimple def_stmt;
2377 if (TREE_CODE (name) != SSA_NAME)
2378 return NULL;
2380 def_stmt = get_gimple_for_ssa_name (name);
2381 if (!def_stmt
2382 || gimple_assign_rhs_code (def_stmt) != code)
2383 return NULL;
2385 return def_stmt;
2388 #ifdef HAVE_conditional_move
2389 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2390 assigment and the class of the expresion on the RHS is CLASS. Return
2391 NULL otherwise. */
2393 static gimple
2394 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2396 gimple def_stmt;
2398 if (TREE_CODE (name) != SSA_NAME)
2399 return NULL;
2401 def_stmt = get_gimple_for_ssa_name (name);
2402 if (!def_stmt
2403 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2404 return NULL;
2406 return def_stmt;
2408 #endif
2411 /* Determine whether the LEN bytes generated by CONSTFUN can be
2412 stored to memory using several move instructions. CONSTFUNDATA is
2413 a pointer which will be passed as argument in every CONSTFUN call.
2414 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2415 a memset operation and false if it's a copy of a constant string.
2416 Return nonzero if a call to store_by_pieces should succeed. */
2419 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2420 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2421 void *constfundata, unsigned int align, bool memsetp)
2423 unsigned HOST_WIDE_INT l;
2424 unsigned int max_size;
2425 HOST_WIDE_INT offset = 0;
2426 enum machine_mode mode;
2427 enum insn_code icode;
2428 int reverse;
2429 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2430 rtx cst ATTRIBUTE_UNUSED;
2432 if (len == 0)
2433 return 1;
2435 if (! (memsetp
2436 ? SET_BY_PIECES_P (len, align)
2437 : STORE_BY_PIECES_P (len, align)))
2438 return 0;
2440 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2442 /* We would first store what we can in the largest integer mode, then go to
2443 successively smaller modes. */
2445 for (reverse = 0;
2446 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2447 reverse++)
2449 l = len;
2450 max_size = STORE_MAX_PIECES + 1;
2451 while (max_size > 1 && l > 0)
2453 mode = widest_int_mode_for_size (max_size);
2455 if (mode == VOIDmode)
2456 break;
2458 icode = optab_handler (mov_optab, mode);
2459 if (icode != CODE_FOR_nothing
2460 && align >= GET_MODE_ALIGNMENT (mode))
2462 unsigned int size = GET_MODE_SIZE (mode);
2464 while (l >= size)
2466 if (reverse)
2467 offset -= size;
2469 cst = (*constfun) (constfundata, offset, mode);
2470 if (!targetm.legitimate_constant_p (mode, cst))
2471 return 0;
2473 if (!reverse)
2474 offset += size;
2476 l -= size;
2480 max_size = GET_MODE_SIZE (mode);
2483 /* The code above should have handled everything. */
2484 gcc_assert (!l);
2487 return 1;
2490 /* Generate several move instructions to store LEN bytes generated by
2491 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2492 pointer which will be passed as argument in every CONSTFUN call.
2493 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2494 a memset operation and false if it's a copy of a constant string.
2495 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2496 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2497 stpcpy. */
2500 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2501 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2502 void *constfundata, unsigned int align, bool memsetp, int endp)
2504 enum machine_mode to_addr_mode = get_address_mode (to);
2505 struct store_by_pieces_d data;
2507 if (len == 0)
2509 gcc_assert (endp != 2);
2510 return to;
2513 gcc_assert (memsetp
2514 ? SET_BY_PIECES_P (len, align)
2515 : STORE_BY_PIECES_P (len, align));
2516 data.constfun = constfun;
2517 data.constfundata = constfundata;
2518 data.len = len;
2519 data.to = to;
2520 store_by_pieces_1 (&data, align);
2521 if (endp)
2523 rtx to1;
2525 gcc_assert (!data.reverse);
2526 if (data.autinc_to)
2528 if (endp == 2)
2530 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2531 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2532 else
2533 data.to_addr = copy_to_mode_reg (to_addr_mode,
2534 plus_constant (to_addr_mode,
2535 data.to_addr,
2536 -1));
2538 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2539 data.offset);
2541 else
2543 if (endp == 2)
2544 --data.offset;
2545 to1 = adjust_address (data.to, QImode, data.offset);
2547 return to1;
2549 else
2550 return data.to;
2553 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2554 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2556 static void
2557 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2559 struct store_by_pieces_d data;
2561 if (len == 0)
2562 return;
2564 data.constfun = clear_by_pieces_1;
2565 data.constfundata = NULL;
2566 data.len = len;
2567 data.to = to;
2568 store_by_pieces_1 (&data, align);
2571 /* Callback routine for clear_by_pieces.
2572 Return const0_rtx unconditionally. */
2574 static rtx
2575 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2576 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2577 enum machine_mode mode ATTRIBUTE_UNUSED)
2579 return const0_rtx;
2582 /* Subroutine of clear_by_pieces and store_by_pieces.
2583 Generate several move instructions to store LEN bytes of block TO. (A MEM
2584 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2586 static void
2587 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2588 unsigned int align ATTRIBUTE_UNUSED)
2590 enum machine_mode to_addr_mode = get_address_mode (data->to);
2591 rtx to_addr = XEXP (data->to, 0);
2592 unsigned int max_size = STORE_MAX_PIECES + 1;
2593 enum insn_code icode;
2595 data->offset = 0;
2596 data->to_addr = to_addr;
2597 data->autinc_to
2598 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2599 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2601 data->explicit_inc_to = 0;
2602 data->reverse
2603 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2604 if (data->reverse)
2605 data->offset = data->len;
2607 /* If storing requires more than two move insns,
2608 copy addresses to registers (to make displacements shorter)
2609 and use post-increment if available. */
2610 if (!data->autinc_to
2611 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2613 /* Determine the main mode we'll be using.
2614 MODE might not be used depending on the definitions of the
2615 USE_* macros below. */
2616 enum machine_mode mode ATTRIBUTE_UNUSED
2617 = widest_int_mode_for_size (max_size);
2619 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2621 data->to_addr = copy_to_mode_reg (to_addr_mode,
2622 plus_constant (to_addr_mode,
2623 to_addr,
2624 data->len));
2625 data->autinc_to = 1;
2626 data->explicit_inc_to = -1;
2629 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2630 && ! data->autinc_to)
2632 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2633 data->autinc_to = 1;
2634 data->explicit_inc_to = 1;
2637 if ( !data->autinc_to && CONSTANT_P (to_addr))
2638 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2641 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2643 /* First store what we can in the largest integer mode, then go to
2644 successively smaller modes. */
2646 while (max_size > 1 && data->len > 0)
2648 enum machine_mode mode = widest_int_mode_for_size (max_size);
2650 if (mode == VOIDmode)
2651 break;
2653 icode = optab_handler (mov_optab, mode);
2654 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2655 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2657 max_size = GET_MODE_SIZE (mode);
2660 /* The code above should have handled everything. */
2661 gcc_assert (!data->len);
2664 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2665 with move instructions for mode MODE. GENFUN is the gen_... function
2666 to make a move insn for that mode. DATA has all the other info. */
2668 static void
2669 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2670 struct store_by_pieces_d *data)
2672 unsigned int size = GET_MODE_SIZE (mode);
2673 rtx to1, cst;
2675 while (data->len >= size)
2677 if (data->reverse)
2678 data->offset -= size;
2680 if (data->autinc_to)
2681 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2682 data->offset);
2683 else
2684 to1 = adjust_address (data->to, mode, data->offset);
2686 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2687 emit_insn (gen_add2_insn (data->to_addr,
2688 GEN_INT (-(HOST_WIDE_INT) size)));
2690 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2691 emit_insn ((*genfun) (to1, cst));
2693 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2694 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
2696 if (! data->reverse)
2697 data->offset += size;
2699 data->len -= size;
2703 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2704 its length in bytes. */
2707 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2708 unsigned int expected_align, HOST_WIDE_INT expected_size)
2710 enum machine_mode mode = GET_MODE (object);
2711 unsigned int align;
2713 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2715 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2716 just move a zero. Otherwise, do this a piece at a time. */
2717 if (mode != BLKmode
2718 && CONST_INT_P (size)
2719 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2721 rtx zero = CONST0_RTX (mode);
2722 if (zero != NULL)
2724 emit_move_insn (object, zero);
2725 return NULL;
2728 if (COMPLEX_MODE_P (mode))
2730 zero = CONST0_RTX (GET_MODE_INNER (mode));
2731 if (zero != NULL)
2733 write_complex_part (object, zero, 0);
2734 write_complex_part (object, zero, 1);
2735 return NULL;
2740 if (size == const0_rtx)
2741 return NULL;
2743 align = MEM_ALIGN (object);
2745 if (CONST_INT_P (size)
2746 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2747 clear_by_pieces (object, INTVAL (size), align);
2748 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2749 expected_align, expected_size))
2751 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2752 return set_storage_via_libcall (object, size, const0_rtx,
2753 method == BLOCK_OP_TAILCALL);
2754 else
2755 gcc_unreachable ();
2757 return NULL;
2761 clear_storage (rtx object, rtx size, enum block_op_methods method)
2763 return clear_storage_hints (object, size, method, 0, -1);
2767 /* A subroutine of clear_storage. Expand a call to memset.
2768 Return the return value of memset, 0 otherwise. */
2771 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2773 tree call_expr, fn, object_tree, size_tree, val_tree;
2774 enum machine_mode size_mode;
2775 rtx retval;
2777 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2778 place those into new pseudos into a VAR_DECL and use them later. */
2780 object = copy_addr_to_reg (XEXP (object, 0));
2782 size_mode = TYPE_MODE (sizetype);
2783 size = convert_to_mode (size_mode, size, 1);
2784 size = copy_to_mode_reg (size_mode, size);
2786 /* It is incorrect to use the libcall calling conventions to call
2787 memset in this context. This could be a user call to memset and
2788 the user may wish to examine the return value from memset. For
2789 targets where libcalls and normal calls have different conventions
2790 for returning pointers, we could end up generating incorrect code. */
2792 object_tree = make_tree (ptr_type_node, object);
2793 if (!CONST_INT_P (val))
2794 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2795 size_tree = make_tree (sizetype, size);
2796 val_tree = make_tree (integer_type_node, val);
2798 fn = clear_storage_libcall_fn (true);
2799 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2800 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2802 retval = expand_normal (call_expr);
2804 return retval;
2807 /* A subroutine of set_storage_via_libcall. Create the tree node
2808 for the function we use for block clears. */
2810 tree block_clear_fn;
2812 void
2813 init_block_clear_fn (const char *asmspec)
2815 if (!block_clear_fn)
2817 tree fn, args;
2819 fn = get_identifier ("memset");
2820 args = build_function_type_list (ptr_type_node, ptr_type_node,
2821 integer_type_node, sizetype,
2822 NULL_TREE);
2824 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2825 DECL_EXTERNAL (fn) = 1;
2826 TREE_PUBLIC (fn) = 1;
2827 DECL_ARTIFICIAL (fn) = 1;
2828 TREE_NOTHROW (fn) = 1;
2829 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2830 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2832 block_clear_fn = fn;
2835 if (asmspec)
2836 set_user_assembler_name (block_clear_fn, asmspec);
2839 static tree
2840 clear_storage_libcall_fn (int for_call)
2842 static bool emitted_extern;
2844 if (!block_clear_fn)
2845 init_block_clear_fn (NULL);
2847 if (for_call && !emitted_extern)
2849 emitted_extern = true;
2850 make_decl_rtl (block_clear_fn);
2853 return block_clear_fn;
2856 /* Expand a setmem pattern; return true if successful. */
2858 bool
2859 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2860 unsigned int expected_align, HOST_WIDE_INT expected_size)
2862 /* Try the most limited insn first, because there's no point
2863 including more than one in the machine description unless
2864 the more limited one has some advantage. */
2866 enum machine_mode mode;
2868 if (expected_align < align)
2869 expected_align = align;
2871 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2872 mode = GET_MODE_WIDER_MODE (mode))
2874 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2876 if (code != CODE_FOR_nothing
2877 /* We don't need MODE to be narrower than
2878 BITS_PER_HOST_WIDE_INT here because if SIZE is less than
2879 the mode mask, as it is returned by the macro, it will
2880 definitely be less than the actual mode mask. */
2881 && ((CONST_INT_P (size)
2882 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2883 <= (GET_MODE_MASK (mode) >> 1)))
2884 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
2886 struct expand_operand ops[6];
2887 unsigned int nops;
2889 nops = insn_data[(int) code].n_generator_args;
2890 gcc_assert (nops == 4 || nops == 6);
2892 create_fixed_operand (&ops[0], object);
2893 /* The check above guarantees that this size conversion is valid. */
2894 create_convert_operand_to (&ops[1], size, mode, true);
2895 create_convert_operand_from (&ops[2], val, byte_mode, true);
2896 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2897 if (nops == 6)
2899 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2900 create_integer_operand (&ops[5], expected_size);
2902 if (maybe_expand_insn (code, nops, ops))
2903 return true;
2907 return false;
2911 /* Write to one of the components of the complex value CPLX. Write VAL to
2912 the real part if IMAG_P is false, and the imaginary part if its true. */
2914 static void
2915 write_complex_part (rtx cplx, rtx val, bool imag_p)
2917 enum machine_mode cmode;
2918 enum machine_mode imode;
2919 unsigned ibitsize;
2921 if (GET_CODE (cplx) == CONCAT)
2923 emit_move_insn (XEXP (cplx, imag_p), val);
2924 return;
2927 cmode = GET_MODE (cplx);
2928 imode = GET_MODE_INNER (cmode);
2929 ibitsize = GET_MODE_BITSIZE (imode);
2931 /* For MEMs simplify_gen_subreg may generate an invalid new address
2932 because, e.g., the original address is considered mode-dependent
2933 by the target, which restricts simplify_subreg from invoking
2934 adjust_address_nv. Instead of preparing fallback support for an
2935 invalid address, we call adjust_address_nv directly. */
2936 if (MEM_P (cplx))
2938 emit_move_insn (adjust_address_nv (cplx, imode,
2939 imag_p ? GET_MODE_SIZE (imode) : 0),
2940 val);
2941 return;
2944 /* If the sub-object is at least word sized, then we know that subregging
2945 will work. This special case is important, since store_bit_field
2946 wants to operate on integer modes, and there's rarely an OImode to
2947 correspond to TCmode. */
2948 if (ibitsize >= BITS_PER_WORD
2949 /* For hard regs we have exact predicates. Assume we can split
2950 the original object if it spans an even number of hard regs.
2951 This special case is important for SCmode on 64-bit platforms
2952 where the natural size of floating-point regs is 32-bit. */
2953 || (REG_P (cplx)
2954 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2955 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2957 rtx part = simplify_gen_subreg (imode, cplx, cmode,
2958 imag_p ? GET_MODE_SIZE (imode) : 0);
2959 if (part)
2961 emit_move_insn (part, val);
2962 return;
2964 else
2965 /* simplify_gen_subreg may fail for sub-word MEMs. */
2966 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2969 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
2972 /* Extract one of the components of the complex value CPLX. Extract the
2973 real part if IMAG_P is false, and the imaginary part if it's true. */
2975 static rtx
2976 read_complex_part (rtx cplx, bool imag_p)
2978 enum machine_mode cmode, imode;
2979 unsigned ibitsize;
2981 if (GET_CODE (cplx) == CONCAT)
2982 return XEXP (cplx, imag_p);
2984 cmode = GET_MODE (cplx);
2985 imode = GET_MODE_INNER (cmode);
2986 ibitsize = GET_MODE_BITSIZE (imode);
2988 /* Special case reads from complex constants that got spilled to memory. */
2989 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
2991 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
2992 if (decl && TREE_CODE (decl) == COMPLEX_CST)
2994 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
2995 if (CONSTANT_CLASS_P (part))
2996 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3000 /* For MEMs simplify_gen_subreg may generate an invalid new address
3001 because, e.g., the original address is considered mode-dependent
3002 by the target, which restricts simplify_subreg from invoking
3003 adjust_address_nv. Instead of preparing fallback support for an
3004 invalid address, we call adjust_address_nv directly. */
3005 if (MEM_P (cplx))
3006 return adjust_address_nv (cplx, imode,
3007 imag_p ? GET_MODE_SIZE (imode) : 0);
3009 /* If the sub-object is at least word sized, then we know that subregging
3010 will work. This special case is important, since extract_bit_field
3011 wants to operate on integer modes, and there's rarely an OImode to
3012 correspond to TCmode. */
3013 if (ibitsize >= BITS_PER_WORD
3014 /* For hard regs we have exact predicates. Assume we can split
3015 the original object if it spans an even number of hard regs.
3016 This special case is important for SCmode on 64-bit platforms
3017 where the natural size of floating-point regs is 32-bit. */
3018 || (REG_P (cplx)
3019 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3020 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3022 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3023 imag_p ? GET_MODE_SIZE (imode) : 0);
3024 if (ret)
3025 return ret;
3026 else
3027 /* simplify_gen_subreg may fail for sub-word MEMs. */
3028 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3031 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3032 true, false, NULL_RTX, imode, imode);
3035 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3036 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3037 represented in NEW_MODE. If FORCE is true, this will never happen, as
3038 we'll force-create a SUBREG if needed. */
3040 static rtx
3041 emit_move_change_mode (enum machine_mode new_mode,
3042 enum machine_mode old_mode, rtx x, bool force)
3044 rtx ret;
3046 if (push_operand (x, GET_MODE (x)))
3048 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3049 MEM_COPY_ATTRIBUTES (ret, x);
3051 else if (MEM_P (x))
3053 /* We don't have to worry about changing the address since the
3054 size in bytes is supposed to be the same. */
3055 if (reload_in_progress)
3057 /* Copy the MEM to change the mode and move any
3058 substitutions from the old MEM to the new one. */
3059 ret = adjust_address_nv (x, new_mode, 0);
3060 copy_replacements (x, ret);
3062 else
3063 ret = adjust_address (x, new_mode, 0);
3065 else
3067 /* Note that we do want simplify_subreg's behavior of validating
3068 that the new mode is ok for a hard register. If we were to use
3069 simplify_gen_subreg, we would create the subreg, but would
3070 probably run into the target not being able to implement it. */
3071 /* Except, of course, when FORCE is true, when this is exactly what
3072 we want. Which is needed for CCmodes on some targets. */
3073 if (force)
3074 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3075 else
3076 ret = simplify_subreg (new_mode, x, old_mode, 0);
3079 return ret;
3082 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3083 an integer mode of the same size as MODE. Returns the instruction
3084 emitted, or NULL if such a move could not be generated. */
3086 static rtx
3087 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3089 enum machine_mode imode;
3090 enum insn_code code;
3092 /* There must exist a mode of the exact size we require. */
3093 imode = int_mode_for_mode (mode);
3094 if (imode == BLKmode)
3095 return NULL_RTX;
3097 /* The target must support moves in this mode. */
3098 code = optab_handler (mov_optab, imode);
3099 if (code == CODE_FOR_nothing)
3100 return NULL_RTX;
3102 x = emit_move_change_mode (imode, mode, x, force);
3103 if (x == NULL_RTX)
3104 return NULL_RTX;
3105 y = emit_move_change_mode (imode, mode, y, force);
3106 if (y == NULL_RTX)
3107 return NULL_RTX;
3108 return emit_insn (GEN_FCN (code) (x, y));
3111 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3112 Return an equivalent MEM that does not use an auto-increment. */
3114 static rtx
3115 emit_move_resolve_push (enum machine_mode mode, rtx x)
3117 enum rtx_code code = GET_CODE (XEXP (x, 0));
3118 HOST_WIDE_INT adjust;
3119 rtx temp;
3121 adjust = GET_MODE_SIZE (mode);
3122 #ifdef PUSH_ROUNDING
3123 adjust = PUSH_ROUNDING (adjust);
3124 #endif
3125 if (code == PRE_DEC || code == POST_DEC)
3126 adjust = -adjust;
3127 else if (code == PRE_MODIFY || code == POST_MODIFY)
3129 rtx expr = XEXP (XEXP (x, 0), 1);
3130 HOST_WIDE_INT val;
3132 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3133 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3134 val = INTVAL (XEXP (expr, 1));
3135 if (GET_CODE (expr) == MINUS)
3136 val = -val;
3137 gcc_assert (adjust == val || adjust == -val);
3138 adjust = val;
3141 /* Do not use anti_adjust_stack, since we don't want to update
3142 stack_pointer_delta. */
3143 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3144 GEN_INT (adjust), stack_pointer_rtx,
3145 0, OPTAB_LIB_WIDEN);
3146 if (temp != stack_pointer_rtx)
3147 emit_move_insn (stack_pointer_rtx, temp);
3149 switch (code)
3151 case PRE_INC:
3152 case PRE_DEC:
3153 case PRE_MODIFY:
3154 temp = stack_pointer_rtx;
3155 break;
3156 case POST_INC:
3157 case POST_DEC:
3158 case POST_MODIFY:
3159 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3160 break;
3161 default:
3162 gcc_unreachable ();
3165 return replace_equiv_address (x, temp);
3168 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3169 X is known to satisfy push_operand, and MODE is known to be complex.
3170 Returns the last instruction emitted. */
3173 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3175 enum machine_mode submode = GET_MODE_INNER (mode);
3176 bool imag_first;
3178 #ifdef PUSH_ROUNDING
3179 unsigned int submodesize = GET_MODE_SIZE (submode);
3181 /* In case we output to the stack, but the size is smaller than the
3182 machine can push exactly, we need to use move instructions. */
3183 if (PUSH_ROUNDING (submodesize) != submodesize)
3185 x = emit_move_resolve_push (mode, x);
3186 return emit_move_insn (x, y);
3188 #endif
3190 /* Note that the real part always precedes the imag part in memory
3191 regardless of machine's endianness. */
3192 switch (GET_CODE (XEXP (x, 0)))
3194 case PRE_DEC:
3195 case POST_DEC:
3196 imag_first = true;
3197 break;
3198 case PRE_INC:
3199 case POST_INC:
3200 imag_first = false;
3201 break;
3202 default:
3203 gcc_unreachable ();
3206 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3207 read_complex_part (y, imag_first));
3208 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3209 read_complex_part (y, !imag_first));
3212 /* A subroutine of emit_move_complex. Perform the move from Y to X
3213 via two moves of the parts. Returns the last instruction emitted. */
3216 emit_move_complex_parts (rtx x, rtx y)
3218 /* Show the output dies here. This is necessary for SUBREGs
3219 of pseudos since we cannot track their lifetimes correctly;
3220 hard regs shouldn't appear here except as return values. */
3221 if (!reload_completed && !reload_in_progress
3222 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3223 emit_clobber (x);
3225 write_complex_part (x, read_complex_part (y, false), false);
3226 write_complex_part (x, read_complex_part (y, true), true);
3228 return get_last_insn ();
3231 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3232 MODE is known to be complex. Returns the last instruction emitted. */
3234 static rtx
3235 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3237 bool try_int;
3239 /* Need to take special care for pushes, to maintain proper ordering
3240 of the data, and possibly extra padding. */
3241 if (push_operand (x, mode))
3242 return emit_move_complex_push (mode, x, y);
3244 /* See if we can coerce the target into moving both values at once. */
3246 /* Move floating point as parts. */
3247 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3248 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing)
3249 try_int = false;
3250 /* Not possible if the values are inherently not adjacent. */
3251 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3252 try_int = false;
3253 /* Is possible if both are registers (or subregs of registers). */
3254 else if (register_operand (x, mode) && register_operand (y, mode))
3255 try_int = true;
3256 /* If one of the operands is a memory, and alignment constraints
3257 are friendly enough, we may be able to do combined memory operations.
3258 We do not attempt this if Y is a constant because that combination is
3259 usually better with the by-parts thing below. */
3260 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3261 && (!STRICT_ALIGNMENT
3262 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3263 try_int = true;
3264 else
3265 try_int = false;
3267 if (try_int)
3269 rtx ret;
3271 /* For memory to memory moves, optimal behavior can be had with the
3272 existing block move logic. */
3273 if (MEM_P (x) && MEM_P (y))
3275 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3276 BLOCK_OP_NO_LIBCALL);
3277 return get_last_insn ();
3280 ret = emit_move_via_integer (mode, x, y, true);
3281 if (ret)
3282 return ret;
3285 return emit_move_complex_parts (x, y);
3288 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3289 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3291 static rtx
3292 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3294 rtx ret;
3296 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3297 if (mode != CCmode)
3299 enum insn_code code = optab_handler (mov_optab, CCmode);
3300 if (code != CODE_FOR_nothing)
3302 x = emit_move_change_mode (CCmode, mode, x, true);
3303 y = emit_move_change_mode (CCmode, mode, y, true);
3304 return emit_insn (GEN_FCN (code) (x, y));
3308 /* Otherwise, find the MODE_INT mode of the same width. */
3309 ret = emit_move_via_integer (mode, x, y, false);
3310 gcc_assert (ret != NULL);
3311 return ret;
3314 /* Return true if word I of OP lies entirely in the
3315 undefined bits of a paradoxical subreg. */
3317 static bool
3318 undefined_operand_subword_p (const_rtx op, int i)
3320 enum machine_mode innermode, innermostmode;
3321 int offset;
3322 if (GET_CODE (op) != SUBREG)
3323 return false;
3324 innermode = GET_MODE (op);
3325 innermostmode = GET_MODE (SUBREG_REG (op));
3326 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3327 /* The SUBREG_BYTE represents offset, as if the value were stored in
3328 memory, except for a paradoxical subreg where we define
3329 SUBREG_BYTE to be 0; undo this exception as in
3330 simplify_subreg. */
3331 if (SUBREG_BYTE (op) == 0
3332 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3334 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3335 if (WORDS_BIG_ENDIAN)
3336 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3337 if (BYTES_BIG_ENDIAN)
3338 offset += difference % UNITS_PER_WORD;
3340 if (offset >= GET_MODE_SIZE (innermostmode)
3341 || offset <= -GET_MODE_SIZE (word_mode))
3342 return true;
3343 return false;
3346 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3347 MODE is any multi-word or full-word mode that lacks a move_insn
3348 pattern. Note that you will get better code if you define such
3349 patterns, even if they must turn into multiple assembler instructions. */
3351 static rtx
3352 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3354 rtx last_insn = 0;
3355 rtx seq, inner;
3356 bool need_clobber;
3357 int i;
3359 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3361 /* If X is a push on the stack, do the push now and replace
3362 X with a reference to the stack pointer. */
3363 if (push_operand (x, mode))
3364 x = emit_move_resolve_push (mode, x);
3366 /* If we are in reload, see if either operand is a MEM whose address
3367 is scheduled for replacement. */
3368 if (reload_in_progress && MEM_P (x)
3369 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3370 x = replace_equiv_address_nv (x, inner);
3371 if (reload_in_progress && MEM_P (y)
3372 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3373 y = replace_equiv_address_nv (y, inner);
3375 start_sequence ();
3377 need_clobber = false;
3378 for (i = 0;
3379 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3380 i++)
3382 rtx xpart = operand_subword (x, i, 1, mode);
3383 rtx ypart;
3385 /* Do not generate code for a move if it would come entirely
3386 from the undefined bits of a paradoxical subreg. */
3387 if (undefined_operand_subword_p (y, i))
3388 continue;
3390 ypart = operand_subword (y, i, 1, mode);
3392 /* If we can't get a part of Y, put Y into memory if it is a
3393 constant. Otherwise, force it into a register. Then we must
3394 be able to get a part of Y. */
3395 if (ypart == 0 && CONSTANT_P (y))
3397 y = use_anchored_address (force_const_mem (mode, y));
3398 ypart = operand_subword (y, i, 1, mode);
3400 else if (ypart == 0)
3401 ypart = operand_subword_force (y, i, mode);
3403 gcc_assert (xpart && ypart);
3405 need_clobber |= (GET_CODE (xpart) == SUBREG);
3407 last_insn = emit_move_insn (xpart, ypart);
3410 seq = get_insns ();
3411 end_sequence ();
3413 /* Show the output dies here. This is necessary for SUBREGs
3414 of pseudos since we cannot track their lifetimes correctly;
3415 hard regs shouldn't appear here except as return values.
3416 We never want to emit such a clobber after reload. */
3417 if (x != y
3418 && ! (reload_in_progress || reload_completed)
3419 && need_clobber != 0)
3420 emit_clobber (x);
3422 emit_insn (seq);
3424 return last_insn;
3427 /* Low level part of emit_move_insn.
3428 Called just like emit_move_insn, but assumes X and Y
3429 are basically valid. */
3432 emit_move_insn_1 (rtx x, rtx y)
3434 enum machine_mode mode = GET_MODE (x);
3435 enum insn_code code;
3437 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3439 code = optab_handler (mov_optab, mode);
3440 if (code != CODE_FOR_nothing)
3441 return emit_insn (GEN_FCN (code) (x, y));
3443 /* Expand complex moves by moving real part and imag part. */
3444 if (COMPLEX_MODE_P (mode))
3445 return emit_move_complex (mode, x, y);
3447 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3448 || ALL_FIXED_POINT_MODE_P (mode))
3450 rtx result = emit_move_via_integer (mode, x, y, true);
3452 /* If we can't find an integer mode, use multi words. */
3453 if (result)
3454 return result;
3455 else
3456 return emit_move_multi_word (mode, x, y);
3459 if (GET_MODE_CLASS (mode) == MODE_CC)
3460 return emit_move_ccmode (mode, x, y);
3462 /* Try using a move pattern for the corresponding integer mode. This is
3463 only safe when simplify_subreg can convert MODE constants into integer
3464 constants. At present, it can only do this reliably if the value
3465 fits within a HOST_WIDE_INT. */
3466 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3468 rtx ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3470 if (ret)
3472 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3473 return ret;
3477 return emit_move_multi_word (mode, x, y);
3480 /* Generate code to copy Y into X.
3481 Both Y and X must have the same mode, except that
3482 Y can be a constant with VOIDmode.
3483 This mode cannot be BLKmode; use emit_block_move for that.
3485 Return the last instruction emitted. */
3488 emit_move_insn (rtx x, rtx y)
3490 enum machine_mode mode = GET_MODE (x);
3491 rtx y_cst = NULL_RTX;
3492 rtx last_insn, set;
3494 gcc_assert (mode != BLKmode
3495 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3497 if (CONSTANT_P (y))
3499 if (optimize
3500 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3501 && (last_insn = compress_float_constant (x, y)))
3502 return last_insn;
3504 y_cst = y;
3506 if (!targetm.legitimate_constant_p (mode, y))
3508 y = force_const_mem (mode, y);
3510 /* If the target's cannot_force_const_mem prevented the spill,
3511 assume that the target's move expanders will also take care
3512 of the non-legitimate constant. */
3513 if (!y)
3514 y = y_cst;
3515 else
3516 y = use_anchored_address (y);
3520 /* If X or Y are memory references, verify that their addresses are valid
3521 for the machine. */
3522 if (MEM_P (x)
3523 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3524 MEM_ADDR_SPACE (x))
3525 && ! push_operand (x, GET_MODE (x))))
3526 x = validize_mem (x);
3528 if (MEM_P (y)
3529 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3530 MEM_ADDR_SPACE (y)))
3531 y = validize_mem (y);
3533 gcc_assert (mode != BLKmode);
3535 last_insn = emit_move_insn_1 (x, y);
3537 if (y_cst && REG_P (x)
3538 && (set = single_set (last_insn)) != NULL_RTX
3539 && SET_DEST (set) == x
3540 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3541 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3543 return last_insn;
3546 /* If Y is representable exactly in a narrower mode, and the target can
3547 perform the extension directly from constant or memory, then emit the
3548 move as an extension. */
3550 static rtx
3551 compress_float_constant (rtx x, rtx y)
3553 enum machine_mode dstmode = GET_MODE (x);
3554 enum machine_mode orig_srcmode = GET_MODE (y);
3555 enum machine_mode srcmode;
3556 REAL_VALUE_TYPE r;
3557 int oldcost, newcost;
3558 bool speed = optimize_insn_for_speed_p ();
3560 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3562 if (targetm.legitimate_constant_p (dstmode, y))
3563 oldcost = set_src_cost (y, speed);
3564 else
3565 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3567 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3568 srcmode != orig_srcmode;
3569 srcmode = GET_MODE_WIDER_MODE (srcmode))
3571 enum insn_code ic;
3572 rtx trunc_y, last_insn;
3574 /* Skip if the target can't extend this way. */
3575 ic = can_extend_p (dstmode, srcmode, 0);
3576 if (ic == CODE_FOR_nothing)
3577 continue;
3579 /* Skip if the narrowed value isn't exact. */
3580 if (! exact_real_truncate (srcmode, &r))
3581 continue;
3583 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3585 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3587 /* Skip if the target needs extra instructions to perform
3588 the extension. */
3589 if (!insn_operand_matches (ic, 1, trunc_y))
3590 continue;
3591 /* This is valid, but may not be cheaper than the original. */
3592 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3593 speed);
3594 if (oldcost < newcost)
3595 continue;
3597 else if (float_extend_from_mem[dstmode][srcmode])
3599 trunc_y = force_const_mem (srcmode, trunc_y);
3600 /* This is valid, but may not be cheaper than the original. */
3601 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3602 speed);
3603 if (oldcost < newcost)
3604 continue;
3605 trunc_y = validize_mem (trunc_y);
3607 else
3608 continue;
3610 /* For CSE's benefit, force the compressed constant pool entry
3611 into a new pseudo. This constant may be used in different modes,
3612 and if not, combine will put things back together for us. */
3613 trunc_y = force_reg (srcmode, trunc_y);
3615 /* If x is a hard register, perform the extension into a pseudo,
3616 so that e.g. stack realignment code is aware of it. */
3617 rtx target = x;
3618 if (REG_P (x) && HARD_REGISTER_P (x))
3619 target = gen_reg_rtx (dstmode);
3621 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3622 last_insn = get_last_insn ();
3624 if (REG_P (target))
3625 set_unique_reg_note (last_insn, REG_EQUAL, y);
3627 if (target != x)
3628 return emit_move_insn (x, target);
3629 return last_insn;
3632 return NULL_RTX;
3635 /* Pushing data onto the stack. */
3637 /* Push a block of length SIZE (perhaps variable)
3638 and return an rtx to address the beginning of the block.
3639 The value may be virtual_outgoing_args_rtx.
3641 EXTRA is the number of bytes of padding to push in addition to SIZE.
3642 BELOW nonzero means this padding comes at low addresses;
3643 otherwise, the padding comes at high addresses. */
3646 push_block (rtx size, int extra, int below)
3648 rtx temp;
3650 size = convert_modes (Pmode, ptr_mode, size, 1);
3651 if (CONSTANT_P (size))
3652 anti_adjust_stack (plus_constant (Pmode, size, extra));
3653 else if (REG_P (size) && extra == 0)
3654 anti_adjust_stack (size);
3655 else
3657 temp = copy_to_mode_reg (Pmode, size);
3658 if (extra != 0)
3659 temp = expand_binop (Pmode, add_optab, temp, GEN_INT (extra),
3660 temp, 0, OPTAB_LIB_WIDEN);
3661 anti_adjust_stack (temp);
3664 #ifndef STACK_GROWS_DOWNWARD
3665 if (0)
3666 #else
3667 if (1)
3668 #endif
3670 temp = virtual_outgoing_args_rtx;
3671 if (extra != 0 && below)
3672 temp = plus_constant (Pmode, temp, extra);
3674 else
3676 if (CONST_INT_P (size))
3677 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3678 -INTVAL (size) - (below ? 0 : extra));
3679 else if (extra != 0 && !below)
3680 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3681 negate_rtx (Pmode, plus_constant (Pmode, size,
3682 extra)));
3683 else
3684 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3685 negate_rtx (Pmode, size));
3688 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3691 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3693 static rtx
3694 mem_autoinc_base (rtx mem)
3696 if (MEM_P (mem))
3698 rtx addr = XEXP (mem, 0);
3699 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3700 return XEXP (addr, 0);
3702 return NULL;
3705 /* A utility routine used here, in reload, and in try_split. The insns
3706 after PREV up to and including LAST are known to adjust the stack,
3707 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3708 placing notes as appropriate. PREV may be NULL, indicating the
3709 entire insn sequence prior to LAST should be scanned.
3711 The set of allowed stack pointer modifications is small:
3712 (1) One or more auto-inc style memory references (aka pushes),
3713 (2) One or more addition/subtraction with the SP as destination,
3714 (3) A single move insn with the SP as destination,
3715 (4) A call_pop insn,
3716 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3718 Insns in the sequence that do not modify the SP are ignored,
3719 except for noreturn calls.
3721 The return value is the amount of adjustment that can be trivially
3722 verified, via immediate operand or auto-inc. If the adjustment
3723 cannot be trivially extracted, the return value is INT_MIN. */
3725 HOST_WIDE_INT
3726 find_args_size_adjust (rtx insn)
3728 rtx dest, set, pat;
3729 int i;
3731 pat = PATTERN (insn);
3732 set = NULL;
3734 /* Look for a call_pop pattern. */
3735 if (CALL_P (insn))
3737 /* We have to allow non-call_pop patterns for the case
3738 of emit_single_push_insn of a TLS address. */
3739 if (GET_CODE (pat) != PARALLEL)
3740 return 0;
3742 /* All call_pop have a stack pointer adjust in the parallel.
3743 The call itself is always first, and the stack adjust is
3744 usually last, so search from the end. */
3745 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3747 set = XVECEXP (pat, 0, i);
3748 if (GET_CODE (set) != SET)
3749 continue;
3750 dest = SET_DEST (set);
3751 if (dest == stack_pointer_rtx)
3752 break;
3754 /* We'd better have found the stack pointer adjust. */
3755 if (i == 0)
3756 return 0;
3757 /* Fall through to process the extracted SET and DEST
3758 as if it was a standalone insn. */
3760 else if (GET_CODE (pat) == SET)
3761 set = pat;
3762 else if ((set = single_set (insn)) != NULL)
3764 else if (GET_CODE (pat) == PARALLEL)
3766 /* ??? Some older ports use a parallel with a stack adjust
3767 and a store for a PUSH_ROUNDING pattern, rather than a
3768 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3769 /* ??? See h8300 and m68k, pushqi1. */
3770 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3772 set = XVECEXP (pat, 0, i);
3773 if (GET_CODE (set) != SET)
3774 continue;
3775 dest = SET_DEST (set);
3776 if (dest == stack_pointer_rtx)
3777 break;
3779 /* We do not expect an auto-inc of the sp in the parallel. */
3780 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3781 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3782 != stack_pointer_rtx);
3784 if (i < 0)
3785 return 0;
3787 else
3788 return 0;
3790 dest = SET_DEST (set);
3792 /* Look for direct modifications of the stack pointer. */
3793 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3795 /* Look for a trivial adjustment, otherwise assume nothing. */
3796 /* Note that the SPU restore_stack_block pattern refers to
3797 the stack pointer in V4SImode. Consider that non-trivial. */
3798 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3799 && GET_CODE (SET_SRC (set)) == PLUS
3800 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3801 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3802 return INTVAL (XEXP (SET_SRC (set), 1));
3803 /* ??? Reload can generate no-op moves, which will be cleaned
3804 up later. Recognize it and continue searching. */
3805 else if (rtx_equal_p (dest, SET_SRC (set)))
3806 return 0;
3807 else
3808 return HOST_WIDE_INT_MIN;
3810 else
3812 rtx mem, addr;
3814 /* Otherwise only think about autoinc patterns. */
3815 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3817 mem = dest;
3818 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3819 != stack_pointer_rtx);
3821 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3822 mem = SET_SRC (set);
3823 else
3824 return 0;
3826 addr = XEXP (mem, 0);
3827 switch (GET_CODE (addr))
3829 case PRE_INC:
3830 case POST_INC:
3831 return GET_MODE_SIZE (GET_MODE (mem));
3832 case PRE_DEC:
3833 case POST_DEC:
3834 return -GET_MODE_SIZE (GET_MODE (mem));
3835 case PRE_MODIFY:
3836 case POST_MODIFY:
3837 addr = XEXP (addr, 1);
3838 gcc_assert (GET_CODE (addr) == PLUS);
3839 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3840 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3841 return INTVAL (XEXP (addr, 1));
3842 default:
3843 gcc_unreachable ();
3849 fixup_args_size_notes (rtx prev, rtx last, int end_args_size)
3851 int args_size = end_args_size;
3852 bool saw_unknown = false;
3853 rtx insn;
3855 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3857 HOST_WIDE_INT this_delta;
3859 if (!NONDEBUG_INSN_P (insn))
3860 continue;
3862 this_delta = find_args_size_adjust (insn);
3863 if (this_delta == 0)
3865 if (!CALL_P (insn)
3866 || ACCUMULATE_OUTGOING_ARGS
3867 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3868 continue;
3871 gcc_assert (!saw_unknown);
3872 if (this_delta == HOST_WIDE_INT_MIN)
3873 saw_unknown = true;
3875 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3876 #ifdef STACK_GROWS_DOWNWARD
3877 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3878 #endif
3879 args_size -= this_delta;
3882 return saw_unknown ? INT_MIN : args_size;
3885 #ifdef PUSH_ROUNDING
3886 /* Emit single push insn. */
3888 static void
3889 emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
3891 rtx dest_addr;
3892 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3893 rtx dest;
3894 enum insn_code icode;
3896 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3897 /* If there is push pattern, use it. Otherwise try old way of throwing
3898 MEM representing push operation to move expander. */
3899 icode = optab_handler (push_optab, mode);
3900 if (icode != CODE_FOR_nothing)
3902 struct expand_operand ops[1];
3904 create_input_operand (&ops[0], x, mode);
3905 if (maybe_expand_insn (icode, 1, ops))
3906 return;
3908 if (GET_MODE_SIZE (mode) == rounded_size)
3909 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3910 /* If we are to pad downward, adjust the stack pointer first and
3911 then store X into the stack location using an offset. This is
3912 because emit_move_insn does not know how to pad; it does not have
3913 access to type. */
3914 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3916 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3917 HOST_WIDE_INT offset;
3919 emit_move_insn (stack_pointer_rtx,
3920 expand_binop (Pmode,
3921 #ifdef STACK_GROWS_DOWNWARD
3922 sub_optab,
3923 #else
3924 add_optab,
3925 #endif
3926 stack_pointer_rtx,
3927 GEN_INT (rounded_size),
3928 NULL_RTX, 0, OPTAB_LIB_WIDEN));
3930 offset = (HOST_WIDE_INT) padding_size;
3931 #ifdef STACK_GROWS_DOWNWARD
3932 if (STACK_PUSH_CODE == POST_DEC)
3933 /* We have already decremented the stack pointer, so get the
3934 previous value. */
3935 offset += (HOST_WIDE_INT) rounded_size;
3936 #else
3937 if (STACK_PUSH_CODE == POST_INC)
3938 /* We have already incremented the stack pointer, so get the
3939 previous value. */
3940 offset -= (HOST_WIDE_INT) rounded_size;
3941 #endif
3942 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
3944 else
3946 #ifdef STACK_GROWS_DOWNWARD
3947 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
3948 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3949 GEN_INT (-(HOST_WIDE_INT) rounded_size));
3950 #else
3951 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
3952 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3953 GEN_INT (rounded_size));
3954 #endif
3955 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
3958 dest = gen_rtx_MEM (mode, dest_addr);
3960 if (type != 0)
3962 set_mem_attributes (dest, type, 1);
3964 if (flag_optimize_sibling_calls)
3965 /* Function incoming arguments may overlap with sibling call
3966 outgoing arguments and we cannot allow reordering of reads
3967 from function arguments with stores to outgoing arguments
3968 of sibling calls. */
3969 set_mem_alias_set (dest, 0);
3971 emit_move_insn (dest, x);
3974 /* Emit and annotate a single push insn. */
3976 static void
3977 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
3979 int delta, old_delta = stack_pointer_delta;
3980 rtx prev = get_last_insn ();
3981 rtx last;
3983 emit_single_push_insn_1 (mode, x, type);
3985 last = get_last_insn ();
3987 /* Notice the common case where we emitted exactly one insn. */
3988 if (PREV_INSN (last) == prev)
3990 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
3991 return;
3994 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
3995 gcc_assert (delta == INT_MIN || delta == old_delta);
3997 #endif
3999 /* Generate code to push X onto the stack, assuming it has mode MODE and
4000 type TYPE.
4001 MODE is redundant except when X is a CONST_INT (since they don't
4002 carry mode info).
4003 SIZE is an rtx for the size of data to be copied (in bytes),
4004 needed only if X is BLKmode.
4006 ALIGN (in bits) is maximum alignment we can assume.
4008 If PARTIAL and REG are both nonzero, then copy that many of the first
4009 bytes of X into registers starting with REG, and push the rest of X.
4010 The amount of space pushed is decreased by PARTIAL bytes.
4011 REG must be a hard register in this case.
4012 If REG is zero but PARTIAL is not, take any all others actions for an
4013 argument partially in registers, but do not actually load any
4014 registers.
4016 EXTRA is the amount in bytes of extra space to leave next to this arg.
4017 This is ignored if an argument block has already been allocated.
4019 On a machine that lacks real push insns, ARGS_ADDR is the address of
4020 the bottom of the argument block for this call. We use indexing off there
4021 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4022 argument block has not been preallocated.
4024 ARGS_SO_FAR is the size of args previously pushed for this call.
4026 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4027 for arguments passed in registers. If nonzero, it will be the number
4028 of bytes required. */
4030 void
4031 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
4032 unsigned int align, int partial, rtx reg, int extra,
4033 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4034 rtx alignment_pad)
4036 rtx xinner;
4037 enum direction stack_direction
4038 #ifdef STACK_GROWS_DOWNWARD
4039 = downward;
4040 #else
4041 = upward;
4042 #endif
4044 /* Decide where to pad the argument: `downward' for below,
4045 `upward' for above, or `none' for don't pad it.
4046 Default is below for small data on big-endian machines; else above. */
4047 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4049 /* Invert direction if stack is post-decrement.
4050 FIXME: why? */
4051 if (STACK_PUSH_CODE == POST_DEC)
4052 if (where_pad != none)
4053 where_pad = (where_pad == downward ? upward : downward);
4055 xinner = x;
4057 if (mode == BLKmode
4058 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4060 /* Copy a block into the stack, entirely or partially. */
4062 rtx temp;
4063 int used;
4064 int offset;
4065 int skip;
4067 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4068 used = partial - offset;
4070 if (mode != BLKmode)
4072 /* A value is to be stored in an insufficiently aligned
4073 stack slot; copy via a suitably aligned slot if
4074 necessary. */
4075 size = GEN_INT (GET_MODE_SIZE (mode));
4076 if (!MEM_P (xinner))
4078 temp = assign_temp (type, 1, 1);
4079 emit_move_insn (temp, xinner);
4080 xinner = temp;
4084 gcc_assert (size);
4086 /* USED is now the # of bytes we need not copy to the stack
4087 because registers will take care of them. */
4089 if (partial != 0)
4090 xinner = adjust_address (xinner, BLKmode, used);
4092 /* If the partial register-part of the arg counts in its stack size,
4093 skip the part of stack space corresponding to the registers.
4094 Otherwise, start copying to the beginning of the stack space,
4095 by setting SKIP to 0. */
4096 skip = (reg_parm_stack_space == 0) ? 0 : used;
4098 #ifdef PUSH_ROUNDING
4099 /* Do it with several push insns if that doesn't take lots of insns
4100 and if there is no difficulty with push insns that skip bytes
4101 on the stack for alignment purposes. */
4102 if (args_addr == 0
4103 && PUSH_ARGS
4104 && CONST_INT_P (size)
4105 && skip == 0
4106 && MEM_ALIGN (xinner) >= align
4107 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
4108 /* Here we avoid the case of a structure whose weak alignment
4109 forces many pushes of a small amount of data,
4110 and such small pushes do rounding that causes trouble. */
4111 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4112 || align >= BIGGEST_ALIGNMENT
4113 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4114 == (align / BITS_PER_UNIT)))
4115 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4117 /* Push padding now if padding above and stack grows down,
4118 or if padding below and stack grows up.
4119 But if space already allocated, this has already been done. */
4120 if (extra && args_addr == 0
4121 && where_pad != none && where_pad != stack_direction)
4122 anti_adjust_stack (GEN_INT (extra));
4124 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4126 else
4127 #endif /* PUSH_ROUNDING */
4129 rtx target;
4131 /* Otherwise make space on the stack and copy the data
4132 to the address of that space. */
4134 /* Deduct words put into registers from the size we must copy. */
4135 if (partial != 0)
4137 if (CONST_INT_P (size))
4138 size = GEN_INT (INTVAL (size) - used);
4139 else
4140 size = expand_binop (GET_MODE (size), sub_optab, size,
4141 GEN_INT (used), NULL_RTX, 0,
4142 OPTAB_LIB_WIDEN);
4145 /* Get the address of the stack space.
4146 In this case, we do not deal with EXTRA separately.
4147 A single stack adjust will do. */
4148 if (! args_addr)
4150 temp = push_block (size, extra, where_pad == downward);
4151 extra = 0;
4153 else if (CONST_INT_P (args_so_far))
4154 temp = memory_address (BLKmode,
4155 plus_constant (Pmode, args_addr,
4156 skip + INTVAL (args_so_far)));
4157 else
4158 temp = memory_address (BLKmode,
4159 plus_constant (Pmode,
4160 gen_rtx_PLUS (Pmode,
4161 args_addr,
4162 args_so_far),
4163 skip));
4165 if (!ACCUMULATE_OUTGOING_ARGS)
4167 /* If the source is referenced relative to the stack pointer,
4168 copy it to another register to stabilize it. We do not need
4169 to do this if we know that we won't be changing sp. */
4171 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4172 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4173 temp = copy_to_reg (temp);
4176 target = gen_rtx_MEM (BLKmode, temp);
4178 /* We do *not* set_mem_attributes here, because incoming arguments
4179 may overlap with sibling call outgoing arguments and we cannot
4180 allow reordering of reads from function arguments with stores
4181 to outgoing arguments of sibling calls. We do, however, want
4182 to record the alignment of the stack slot. */
4183 /* ALIGN may well be better aligned than TYPE, e.g. due to
4184 PARM_BOUNDARY. Assume the caller isn't lying. */
4185 set_mem_align (target, align);
4187 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4190 else if (partial > 0)
4192 /* Scalar partly in registers. */
4194 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4195 int i;
4196 int not_stack;
4197 /* # bytes of start of argument
4198 that we must make space for but need not store. */
4199 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4200 int args_offset = INTVAL (args_so_far);
4201 int skip;
4203 /* Push padding now if padding above and stack grows down,
4204 or if padding below and stack grows up.
4205 But if space already allocated, this has already been done. */
4206 if (extra && args_addr == 0
4207 && where_pad != none && where_pad != stack_direction)
4208 anti_adjust_stack (GEN_INT (extra));
4210 /* If we make space by pushing it, we might as well push
4211 the real data. Otherwise, we can leave OFFSET nonzero
4212 and leave the space uninitialized. */
4213 if (args_addr == 0)
4214 offset = 0;
4216 /* Now NOT_STACK gets the number of words that we don't need to
4217 allocate on the stack. Convert OFFSET to words too. */
4218 not_stack = (partial - offset) / UNITS_PER_WORD;
4219 offset /= UNITS_PER_WORD;
4221 /* If the partial register-part of the arg counts in its stack size,
4222 skip the part of stack space corresponding to the registers.
4223 Otherwise, start copying to the beginning of the stack space,
4224 by setting SKIP to 0. */
4225 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4227 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4228 x = validize_mem (force_const_mem (mode, x));
4230 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4231 SUBREGs of such registers are not allowed. */
4232 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4233 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4234 x = copy_to_reg (x);
4236 /* Loop over all the words allocated on the stack for this arg. */
4237 /* We can do it by words, because any scalar bigger than a word
4238 has a size a multiple of a word. */
4239 #ifndef PUSH_ARGS_REVERSED
4240 for (i = not_stack; i < size; i++)
4241 #else
4242 for (i = size - 1; i >= not_stack; i--)
4243 #endif
4244 if (i >= not_stack + offset)
4245 emit_push_insn (operand_subword_force (x, i, mode),
4246 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4247 0, args_addr,
4248 GEN_INT (args_offset + ((i - not_stack + skip)
4249 * UNITS_PER_WORD)),
4250 reg_parm_stack_space, alignment_pad);
4252 else
4254 rtx addr;
4255 rtx dest;
4257 /* Push padding now if padding above and stack grows down,
4258 or if padding below and stack grows up.
4259 But if space already allocated, this has already been done. */
4260 if (extra && args_addr == 0
4261 && where_pad != none && where_pad != stack_direction)
4262 anti_adjust_stack (GEN_INT (extra));
4264 #ifdef PUSH_ROUNDING
4265 if (args_addr == 0 && PUSH_ARGS)
4266 emit_single_push_insn (mode, x, type);
4267 else
4268 #endif
4270 if (CONST_INT_P (args_so_far))
4271 addr
4272 = memory_address (mode,
4273 plus_constant (Pmode, args_addr,
4274 INTVAL (args_so_far)));
4275 else
4276 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4277 args_so_far));
4278 dest = gen_rtx_MEM (mode, addr);
4280 /* We do *not* set_mem_attributes here, because incoming arguments
4281 may overlap with sibling call outgoing arguments and we cannot
4282 allow reordering of reads from function arguments with stores
4283 to outgoing arguments of sibling calls. We do, however, want
4284 to record the alignment of the stack slot. */
4285 /* ALIGN may well be better aligned than TYPE, e.g. due to
4286 PARM_BOUNDARY. Assume the caller isn't lying. */
4287 set_mem_align (dest, align);
4289 emit_move_insn (dest, x);
4293 /* If part should go in registers, copy that part
4294 into the appropriate registers. Do this now, at the end,
4295 since mem-to-mem copies above may do function calls. */
4296 if (partial > 0 && reg != 0)
4298 /* Handle calls that pass values in multiple non-contiguous locations.
4299 The Irix 6 ABI has examples of this. */
4300 if (GET_CODE (reg) == PARALLEL)
4301 emit_group_load (reg, x, type, -1);
4302 else
4304 gcc_assert (partial % UNITS_PER_WORD == 0);
4305 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4309 if (extra && args_addr == 0 && where_pad == stack_direction)
4310 anti_adjust_stack (GEN_INT (extra));
4312 if (alignment_pad && args_addr == 0)
4313 anti_adjust_stack (alignment_pad);
4316 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4317 operations. */
4319 static rtx
4320 get_subtarget (rtx x)
4322 return (optimize
4323 || x == 0
4324 /* Only registers can be subtargets. */
4325 || !REG_P (x)
4326 /* Don't use hard regs to avoid extending their life. */
4327 || REGNO (x) < FIRST_PSEUDO_REGISTER
4328 ? 0 : x);
4331 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4332 FIELD is a bitfield. Returns true if the optimization was successful,
4333 and there's nothing else to do. */
4335 static bool
4336 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4337 unsigned HOST_WIDE_INT bitpos,
4338 unsigned HOST_WIDE_INT bitregion_start,
4339 unsigned HOST_WIDE_INT bitregion_end,
4340 enum machine_mode mode1, rtx str_rtx,
4341 tree to, tree src)
4343 enum machine_mode str_mode = GET_MODE (str_rtx);
4344 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4345 tree op0, op1;
4346 rtx value, result;
4347 optab binop;
4348 gimple srcstmt;
4349 enum tree_code code;
4351 if (mode1 != VOIDmode
4352 || bitsize >= BITS_PER_WORD
4353 || str_bitsize > BITS_PER_WORD
4354 || TREE_SIDE_EFFECTS (to)
4355 || TREE_THIS_VOLATILE (to))
4356 return false;
4358 STRIP_NOPS (src);
4359 if (TREE_CODE (src) != SSA_NAME)
4360 return false;
4361 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4362 return false;
4364 srcstmt = get_gimple_for_ssa_name (src);
4365 if (!srcstmt
4366 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4367 return false;
4369 code = gimple_assign_rhs_code (srcstmt);
4371 op0 = gimple_assign_rhs1 (srcstmt);
4373 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4374 to find its initialization. Hopefully the initialization will
4375 be from a bitfield load. */
4376 if (TREE_CODE (op0) == SSA_NAME)
4378 gimple op0stmt = get_gimple_for_ssa_name (op0);
4380 /* We want to eventually have OP0 be the same as TO, which
4381 should be a bitfield. */
4382 if (!op0stmt
4383 || !is_gimple_assign (op0stmt)
4384 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4385 return false;
4386 op0 = gimple_assign_rhs1 (op0stmt);
4389 op1 = gimple_assign_rhs2 (srcstmt);
4391 if (!operand_equal_p (to, op0, 0))
4392 return false;
4394 if (MEM_P (str_rtx))
4396 unsigned HOST_WIDE_INT offset1;
4398 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4399 str_mode = word_mode;
4400 str_mode = get_best_mode (bitsize, bitpos,
4401 bitregion_start, bitregion_end,
4402 MEM_ALIGN (str_rtx), str_mode, 0);
4403 if (str_mode == VOIDmode)
4404 return false;
4405 str_bitsize = GET_MODE_BITSIZE (str_mode);
4407 offset1 = bitpos;
4408 bitpos %= str_bitsize;
4409 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4410 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4412 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4413 return false;
4415 /* If the bit field covers the whole REG/MEM, store_field
4416 will likely generate better code. */
4417 if (bitsize >= str_bitsize)
4418 return false;
4420 /* We can't handle fields split across multiple entities. */
4421 if (bitpos + bitsize > str_bitsize)
4422 return false;
4424 if (BYTES_BIG_ENDIAN)
4425 bitpos = str_bitsize - bitpos - bitsize;
4427 switch (code)
4429 case PLUS_EXPR:
4430 case MINUS_EXPR:
4431 /* For now, just optimize the case of the topmost bitfield
4432 where we don't need to do any masking and also
4433 1 bit bitfields where xor can be used.
4434 We might win by one instruction for the other bitfields
4435 too if insv/extv instructions aren't used, so that
4436 can be added later. */
4437 if (bitpos + bitsize != str_bitsize
4438 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4439 break;
4441 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4442 value = convert_modes (str_mode,
4443 TYPE_MODE (TREE_TYPE (op1)), value,
4444 TYPE_UNSIGNED (TREE_TYPE (op1)));
4446 /* We may be accessing data outside the field, which means
4447 we can alias adjacent data. */
4448 if (MEM_P (str_rtx))
4450 str_rtx = shallow_copy_rtx (str_rtx);
4451 set_mem_alias_set (str_rtx, 0);
4452 set_mem_expr (str_rtx, 0);
4455 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4456 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4458 value = expand_and (str_mode, value, const1_rtx, NULL);
4459 binop = xor_optab;
4461 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4462 result = expand_binop (str_mode, binop, str_rtx,
4463 value, str_rtx, 1, OPTAB_WIDEN);
4464 if (result != str_rtx)
4465 emit_move_insn (str_rtx, result);
4466 return true;
4468 case BIT_IOR_EXPR:
4469 case BIT_XOR_EXPR:
4470 if (TREE_CODE (op1) != INTEGER_CST)
4471 break;
4472 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4473 value = convert_modes (str_mode,
4474 TYPE_MODE (TREE_TYPE (op1)), value,
4475 TYPE_UNSIGNED (TREE_TYPE (op1)));
4477 /* We may be accessing data outside the field, which means
4478 we can alias adjacent data. */
4479 if (MEM_P (str_rtx))
4481 str_rtx = shallow_copy_rtx (str_rtx);
4482 set_mem_alias_set (str_rtx, 0);
4483 set_mem_expr (str_rtx, 0);
4486 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4487 if (bitpos + bitsize != str_bitsize)
4489 rtx mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1);
4490 value = expand_and (str_mode, value, mask, NULL_RTX);
4492 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4493 result = expand_binop (str_mode, binop, str_rtx,
4494 value, str_rtx, 1, OPTAB_WIDEN);
4495 if (result != str_rtx)
4496 emit_move_insn (str_rtx, result);
4497 return true;
4499 default:
4500 break;
4503 return false;
4506 /* In the C++ memory model, consecutive bit fields in a structure are
4507 considered one memory location.
4509 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4510 returns the bit range of consecutive bits in which this COMPONENT_REF
4511 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4512 and *OFFSET may be adjusted in the process.
4514 If the access does not need to be restricted, 0 is returned in both
4515 *BITSTART and *BITEND. */
4517 static void
4518 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4519 unsigned HOST_WIDE_INT *bitend,
4520 tree exp,
4521 HOST_WIDE_INT *bitpos,
4522 tree *offset)
4524 HOST_WIDE_INT bitoffset;
4525 tree field, repr;
4527 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4529 field = TREE_OPERAND (exp, 1);
4530 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4531 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4532 need to limit the range we can access. */
4533 if (!repr)
4535 *bitstart = *bitend = 0;
4536 return;
4539 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4540 part of a larger bit field, then the representative does not serve any
4541 useful purpose. This can occur in Ada. */
4542 if (handled_component_p (TREE_OPERAND (exp, 0)))
4544 enum machine_mode rmode;
4545 HOST_WIDE_INT rbitsize, rbitpos;
4546 tree roffset;
4547 int unsignedp;
4548 int volatilep = 0;
4549 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4550 &roffset, &rmode, &unsignedp, &volatilep, false);
4551 if ((rbitpos % BITS_PER_UNIT) != 0)
4553 *bitstart = *bitend = 0;
4554 return;
4558 /* Compute the adjustment to bitpos from the offset of the field
4559 relative to the representative. DECL_FIELD_OFFSET of field and
4560 repr are the same by construction if they are not constants,
4561 see finish_bitfield_layout. */
4562 if (host_integerp (DECL_FIELD_OFFSET (field), 1)
4563 && host_integerp (DECL_FIELD_OFFSET (repr), 1))
4564 bitoffset = (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
4565 - tree_low_cst (DECL_FIELD_OFFSET (repr), 1)) * BITS_PER_UNIT;
4566 else
4567 bitoffset = 0;
4568 bitoffset += (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
4569 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
4571 /* If the adjustment is larger than bitpos, we would have a negative bit
4572 position for the lower bound and this may wreak havoc later. Adjust
4573 offset and bitpos to make the lower bound non-negative in that case. */
4574 if (bitoffset > *bitpos)
4576 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4577 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4579 *bitpos += adjust;
4580 if (*offset == NULL_TREE)
4581 *offset = size_int (-adjust / BITS_PER_UNIT);
4582 else
4583 *offset
4584 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4585 *bitstart = 0;
4587 else
4588 *bitstart = *bitpos - bitoffset;
4590 *bitend = *bitstart + tree_low_cst (DECL_SIZE (repr), 1) - 1;
4593 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4594 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4595 DECL_RTL was not set yet, return NORTL. */
4597 static inline bool
4598 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4600 if (TREE_CODE (addr) != ADDR_EXPR)
4601 return false;
4603 tree base = TREE_OPERAND (addr, 0);
4605 if (!DECL_P (base)
4606 || TREE_ADDRESSABLE (base)
4607 || DECL_MODE (base) == BLKmode)
4608 return false;
4610 if (!DECL_RTL_SET_P (base))
4611 return nortl;
4613 return (!MEM_P (DECL_RTL (base)));
4616 /* Returns true if the MEM_REF REF refers to an object that does not
4617 reside in memory and has non-BLKmode. */
4619 static inline bool
4620 mem_ref_refers_to_non_mem_p (tree ref)
4622 tree base = TREE_OPERAND (ref, 0);
4623 return addr_expr_of_non_mem_decl_p_1 (base, false);
4626 /* Return TRUE iff OP is an ADDR_EXPR of a DECL that's not
4627 addressable. This is very much like mem_ref_refers_to_non_mem_p,
4628 but instead of the MEM_REF, it takes its base, and it doesn't
4629 assume a DECL is in memory just because its RTL is not set yet. */
4631 bool
4632 addr_expr_of_non_mem_decl_p (tree op)
4634 return addr_expr_of_non_mem_decl_p_1 (op, true);
4637 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4638 is true, try generating a nontemporal store. */
4640 void
4641 expand_assignment (tree to, tree from, bool nontemporal)
4643 rtx to_rtx = 0;
4644 rtx result;
4645 enum machine_mode mode;
4646 unsigned int align;
4647 enum insn_code icode;
4649 /* Don't crash if the lhs of the assignment was erroneous. */
4650 if (TREE_CODE (to) == ERROR_MARK)
4652 expand_normal (from);
4653 return;
4656 /* Optimize away no-op moves without side-effects. */
4657 if (operand_equal_p (to, from, 0))
4658 return;
4660 /* Handle misaligned stores. */
4661 mode = TYPE_MODE (TREE_TYPE (to));
4662 if ((TREE_CODE (to) == MEM_REF
4663 || TREE_CODE (to) == TARGET_MEM_REF)
4664 && mode != BLKmode
4665 && !mem_ref_refers_to_non_mem_p (to)
4666 && ((align = get_object_alignment (to))
4667 < GET_MODE_ALIGNMENT (mode))
4668 && (((icode = optab_handler (movmisalign_optab, mode))
4669 != CODE_FOR_nothing)
4670 || SLOW_UNALIGNED_ACCESS (mode, align)))
4672 rtx reg, mem;
4674 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4675 reg = force_not_mem (reg);
4676 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4678 if (icode != CODE_FOR_nothing)
4680 struct expand_operand ops[2];
4682 create_fixed_operand (&ops[0], mem);
4683 create_input_operand (&ops[1], reg, mode);
4684 /* The movmisalign<mode> pattern cannot fail, else the assignment
4685 would silently be omitted. */
4686 expand_insn (icode, 2, ops);
4688 else
4689 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4690 return;
4693 /* Assignment of a structure component needs special treatment
4694 if the structure component's rtx is not simply a MEM.
4695 Assignment of an array element at a constant index, and assignment of
4696 an array element in an unaligned packed structure field, has the same
4697 problem. Same for (partially) storing into a non-memory object. */
4698 if (handled_component_p (to)
4699 || (TREE_CODE (to) == MEM_REF
4700 && mem_ref_refers_to_non_mem_p (to))
4701 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4703 enum machine_mode mode1;
4704 HOST_WIDE_INT bitsize, bitpos;
4705 unsigned HOST_WIDE_INT bitregion_start = 0;
4706 unsigned HOST_WIDE_INT bitregion_end = 0;
4707 tree offset;
4708 int unsignedp;
4709 int volatilep = 0;
4710 tree tem;
4711 bool misalignp;
4712 rtx mem = NULL_RTX;
4714 push_temp_slots ();
4715 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4716 &unsignedp, &volatilep, true);
4718 /* Make sure bitpos is not negative, it can wreak havoc later. */
4719 if (bitpos < 0)
4721 gcc_assert (offset == NULL_TREE);
4722 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4723 ? 3 : exact_log2 (BITS_PER_UNIT)));
4724 bitpos &= BITS_PER_UNIT - 1;
4727 if (TREE_CODE (to) == COMPONENT_REF
4728 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4729 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4731 /* If we are going to use store_bit_field and extract_bit_field,
4732 make sure to_rtx will be safe for multiple use. */
4733 mode = TYPE_MODE (TREE_TYPE (tem));
4734 if (TREE_CODE (tem) == MEM_REF
4735 && mode != BLKmode
4736 && ((align = get_object_alignment (tem))
4737 < GET_MODE_ALIGNMENT (mode))
4738 && ((icode = optab_handler (movmisalign_optab, mode))
4739 != CODE_FOR_nothing))
4741 struct expand_operand ops[2];
4743 misalignp = true;
4744 to_rtx = gen_reg_rtx (mode);
4745 mem = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4747 /* If the misaligned store doesn't overwrite all bits, perform
4748 rmw cycle on MEM. */
4749 if (bitsize != GET_MODE_BITSIZE (mode))
4751 create_input_operand (&ops[0], to_rtx, mode);
4752 create_fixed_operand (&ops[1], mem);
4753 /* The movmisalign<mode> pattern cannot fail, else the assignment
4754 would silently be omitted. */
4755 expand_insn (icode, 2, ops);
4757 mem = copy_rtx (mem);
4760 else
4762 misalignp = false;
4763 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4766 /* If the bitfield is volatile, we want to access it in the
4767 field's mode, not the computed mode.
4768 If a MEM has VOIDmode (external with incomplete type),
4769 use BLKmode for it instead. */
4770 if (MEM_P (to_rtx))
4772 if (volatilep && flag_strict_volatile_bitfields > 0)
4773 to_rtx = adjust_address (to_rtx, mode1, 0);
4774 else if (GET_MODE (to_rtx) == VOIDmode)
4775 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4778 if (offset != 0)
4780 enum machine_mode address_mode;
4781 rtx offset_rtx;
4783 if (!MEM_P (to_rtx))
4785 /* We can get constant negative offsets into arrays with broken
4786 user code. Translate this to a trap instead of ICEing. */
4787 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4788 expand_builtin_trap ();
4789 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4792 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4793 address_mode = get_address_mode (to_rtx);
4794 if (GET_MODE (offset_rtx) != address_mode)
4795 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4797 /* A constant address in TO_RTX can have VOIDmode, we must not try
4798 to call force_reg for that case. Avoid that case. */
4799 if (MEM_P (to_rtx)
4800 && GET_MODE (to_rtx) == BLKmode
4801 && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
4802 && bitsize > 0
4803 && (bitpos % bitsize) == 0
4804 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4805 && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
4807 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4808 bitpos = 0;
4811 to_rtx = offset_address (to_rtx, offset_rtx,
4812 highest_pow2_factor_for_target (to,
4813 offset));
4816 /* No action is needed if the target is not a memory and the field
4817 lies completely outside that target. This can occur if the source
4818 code contains an out-of-bounds access to a small array. */
4819 if (!MEM_P (to_rtx)
4820 && GET_MODE (to_rtx) != BLKmode
4821 && (unsigned HOST_WIDE_INT) bitpos
4822 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4824 expand_normal (from);
4825 result = NULL;
4827 /* Handle expand_expr of a complex value returning a CONCAT. */
4828 else if (GET_CODE (to_rtx) == CONCAT)
4830 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4831 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4832 && bitpos == 0
4833 && bitsize == mode_bitsize)
4834 result = store_expr (from, to_rtx, false, nontemporal);
4835 else if (bitsize == mode_bitsize / 2
4836 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4837 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4838 nontemporal);
4839 else if (bitpos + bitsize <= mode_bitsize / 2)
4840 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4841 bitregion_start, bitregion_end,
4842 mode1, from,
4843 get_alias_set (to), nontemporal);
4844 else if (bitpos >= mode_bitsize / 2)
4845 result = store_field (XEXP (to_rtx, 1), bitsize,
4846 bitpos - mode_bitsize / 2,
4847 bitregion_start, bitregion_end,
4848 mode1, from,
4849 get_alias_set (to), nontemporal);
4850 else if (bitpos == 0 && bitsize == mode_bitsize)
4852 rtx from_rtx;
4853 result = expand_normal (from);
4854 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4855 TYPE_MODE (TREE_TYPE (from)), 0);
4856 emit_move_insn (XEXP (to_rtx, 0),
4857 read_complex_part (from_rtx, false));
4858 emit_move_insn (XEXP (to_rtx, 1),
4859 read_complex_part (from_rtx, true));
4861 else
4863 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4864 GET_MODE_SIZE (GET_MODE (to_rtx)));
4865 write_complex_part (temp, XEXP (to_rtx, 0), false);
4866 write_complex_part (temp, XEXP (to_rtx, 1), true);
4867 result = store_field (temp, bitsize, bitpos,
4868 bitregion_start, bitregion_end,
4869 mode1, from,
4870 get_alias_set (to), nontemporal);
4871 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4872 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4875 else
4877 if (MEM_P (to_rtx))
4879 /* If the field is at offset zero, we could have been given the
4880 DECL_RTX of the parent struct. Don't munge it. */
4881 to_rtx = shallow_copy_rtx (to_rtx);
4883 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4885 /* Deal with volatile and readonly fields. The former is only
4886 done for MEM. Also set MEM_KEEP_ALIAS_SET_P if needed. */
4887 if (volatilep)
4888 MEM_VOLATILE_P (to_rtx) = 1;
4891 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4892 bitregion_start, bitregion_end,
4893 mode1,
4894 to_rtx, to, from))
4895 result = NULL;
4896 else
4897 result = store_field (to_rtx, bitsize, bitpos,
4898 bitregion_start, bitregion_end,
4899 mode1, from,
4900 get_alias_set (to), nontemporal);
4903 if (misalignp)
4905 struct expand_operand ops[2];
4907 create_fixed_operand (&ops[0], mem);
4908 create_input_operand (&ops[1], to_rtx, mode);
4909 /* The movmisalign<mode> pattern cannot fail, else the assignment
4910 would silently be omitted. */
4911 expand_insn (icode, 2, ops);
4914 if (result)
4915 preserve_temp_slots (result);
4916 pop_temp_slots ();
4917 return;
4920 /* If the rhs is a function call and its value is not an aggregate,
4921 call the function before we start to compute the lhs.
4922 This is needed for correct code for cases such as
4923 val = setjmp (buf) on machines where reference to val
4924 requires loading up part of an address in a separate insn.
4926 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4927 since it might be a promoted variable where the zero- or sign- extension
4928 needs to be done. Handling this in the normal way is safe because no
4929 computation is done before the call. The same is true for SSA names. */
4930 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
4931 && COMPLETE_TYPE_P (TREE_TYPE (from))
4932 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
4933 && ! (((TREE_CODE (to) == VAR_DECL
4934 || TREE_CODE (to) == PARM_DECL
4935 || TREE_CODE (to) == RESULT_DECL)
4936 && REG_P (DECL_RTL (to)))
4937 || TREE_CODE (to) == SSA_NAME))
4939 rtx value;
4941 push_temp_slots ();
4942 value = expand_normal (from);
4943 if (to_rtx == 0)
4944 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4946 /* Handle calls that return values in multiple non-contiguous locations.
4947 The Irix 6 ABI has examples of this. */
4948 if (GET_CODE (to_rtx) == PARALLEL)
4950 if (GET_CODE (value) == PARALLEL)
4951 emit_group_move (to_rtx, value);
4952 else
4953 emit_group_load (to_rtx, value, TREE_TYPE (from),
4954 int_size_in_bytes (TREE_TYPE (from)));
4956 else if (GET_CODE (value) == PARALLEL)
4957 emit_group_store (to_rtx, value, TREE_TYPE (from),
4958 int_size_in_bytes (TREE_TYPE (from)));
4959 else if (GET_MODE (to_rtx) == BLKmode)
4961 /* Handle calls that return BLKmode values in registers. */
4962 if (REG_P (value))
4963 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
4964 else
4965 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
4967 else
4969 if (POINTER_TYPE_P (TREE_TYPE (to)))
4970 value = convert_memory_address_addr_space
4971 (GET_MODE (to_rtx), value,
4972 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
4974 emit_move_insn (to_rtx, value);
4976 preserve_temp_slots (to_rtx);
4977 pop_temp_slots ();
4978 return;
4981 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
4982 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4984 /* Don't move directly into a return register. */
4985 if (TREE_CODE (to) == RESULT_DECL
4986 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
4988 rtx temp;
4990 push_temp_slots ();
4992 /* If the source is itself a return value, it still is in a pseudo at
4993 this point so we can move it back to the return register directly. */
4994 if (REG_P (to_rtx)
4995 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
4996 && TREE_CODE (from) != CALL_EXPR)
4997 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
4998 else
4999 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5001 /* Handle calls that return values in multiple non-contiguous locations.
5002 The Irix 6 ABI has examples of this. */
5003 if (GET_CODE (to_rtx) == PARALLEL)
5005 if (GET_CODE (temp) == PARALLEL)
5006 emit_group_move (to_rtx, temp);
5007 else
5008 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5009 int_size_in_bytes (TREE_TYPE (from)));
5011 else if (temp)
5012 emit_move_insn (to_rtx, temp);
5014 preserve_temp_slots (to_rtx);
5015 pop_temp_slots ();
5016 return;
5019 /* In case we are returning the contents of an object which overlaps
5020 the place the value is being stored, use a safe function when copying
5021 a value through a pointer into a structure value return block. */
5022 if (TREE_CODE (to) == RESULT_DECL
5023 && TREE_CODE (from) == INDIRECT_REF
5024 && ADDR_SPACE_GENERIC_P
5025 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5026 && refs_may_alias_p (to, from)
5027 && cfun->returns_struct
5028 && !cfun->returns_pcc_struct)
5030 rtx from_rtx, size;
5032 push_temp_slots ();
5033 size = expr_size (from);
5034 from_rtx = expand_normal (from);
5036 emit_library_call (memmove_libfunc, LCT_NORMAL,
5037 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5038 XEXP (from_rtx, 0), Pmode,
5039 convert_to_mode (TYPE_MODE (sizetype),
5040 size, TYPE_UNSIGNED (sizetype)),
5041 TYPE_MODE (sizetype));
5043 preserve_temp_slots (to_rtx);
5044 pop_temp_slots ();
5045 return;
5048 /* Compute FROM and store the value in the rtx we got. */
5050 push_temp_slots ();
5051 result = store_expr (from, to_rtx, 0, nontemporal);
5052 preserve_temp_slots (result);
5053 pop_temp_slots ();
5054 return;
5057 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5058 succeeded, false otherwise. */
5060 bool
5061 emit_storent_insn (rtx to, rtx from)
5063 struct expand_operand ops[2];
5064 enum machine_mode mode = GET_MODE (to);
5065 enum insn_code code = optab_handler (storent_optab, mode);
5067 if (code == CODE_FOR_nothing)
5068 return false;
5070 create_fixed_operand (&ops[0], to);
5071 create_input_operand (&ops[1], from, mode);
5072 return maybe_expand_insn (code, 2, ops);
5075 /* Generate code for computing expression EXP,
5076 and storing the value into TARGET.
5078 If the mode is BLKmode then we may return TARGET itself.
5079 It turns out that in BLKmode it doesn't cause a problem.
5080 because C has no operators that could combine two different
5081 assignments into the same BLKmode object with different values
5082 with no sequence point. Will other languages need this to
5083 be more thorough?
5085 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5086 stack, and block moves may need to be treated specially.
5088 If NONTEMPORAL is true, try using a nontemporal store instruction. */
5091 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5093 rtx temp;
5094 rtx alt_rtl = NULL_RTX;
5095 location_t loc = curr_insn_location ();
5097 if (VOID_TYPE_P (TREE_TYPE (exp)))
5099 /* C++ can generate ?: expressions with a throw expression in one
5100 branch and an rvalue in the other. Here, we resolve attempts to
5101 store the throw expression's nonexistent result. */
5102 gcc_assert (!call_param_p);
5103 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5104 return NULL_RTX;
5106 if (TREE_CODE (exp) == COMPOUND_EXPR)
5108 /* Perform first part of compound expression, then assign from second
5109 part. */
5110 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5111 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5112 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5113 nontemporal);
5115 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5117 /* For conditional expression, get safe form of the target. Then
5118 test the condition, doing the appropriate assignment on either
5119 side. This avoids the creation of unnecessary temporaries.
5120 For non-BLKmode, it is more efficient not to do this. */
5122 rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
5124 do_pending_stack_adjust ();
5125 NO_DEFER_POP;
5126 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5127 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5128 nontemporal);
5129 emit_jump_insn (gen_jump (lab2));
5130 emit_barrier ();
5131 emit_label (lab1);
5132 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5133 nontemporal);
5134 emit_label (lab2);
5135 OK_DEFER_POP;
5137 return NULL_RTX;
5139 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5140 /* If this is a scalar in a register that is stored in a wider mode
5141 than the declared mode, compute the result into its declared mode
5142 and then convert to the wider mode. Our value is the computed
5143 expression. */
5145 rtx inner_target = 0;
5147 /* We can do the conversion inside EXP, which will often result
5148 in some optimizations. Do the conversion in two steps: first
5149 change the signedness, if needed, then the extend. But don't
5150 do this if the type of EXP is a subtype of something else
5151 since then the conversion might involve more than just
5152 converting modes. */
5153 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5154 && TREE_TYPE (TREE_TYPE (exp)) == 0
5155 && GET_MODE_PRECISION (GET_MODE (target))
5156 == TYPE_PRECISION (TREE_TYPE (exp)))
5158 if (TYPE_UNSIGNED (TREE_TYPE (exp))
5159 != SUBREG_PROMOTED_UNSIGNED_P (target))
5161 /* Some types, e.g. Fortran's logical*4, won't have a signed
5162 version, so use the mode instead. */
5163 tree ntype
5164 = (signed_or_unsigned_type_for
5165 (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
5166 if (ntype == NULL)
5167 ntype = lang_hooks.types.type_for_mode
5168 (TYPE_MODE (TREE_TYPE (exp)),
5169 SUBREG_PROMOTED_UNSIGNED_P (target));
5171 exp = fold_convert_loc (loc, ntype, exp);
5174 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5175 (GET_MODE (SUBREG_REG (target)),
5176 SUBREG_PROMOTED_UNSIGNED_P (target)),
5177 exp);
5179 inner_target = SUBREG_REG (target);
5182 temp = expand_expr (exp, inner_target, VOIDmode,
5183 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5185 /* If TEMP is a VOIDmode constant, use convert_modes to make
5186 sure that we properly convert it. */
5187 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5189 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5190 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
5191 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5192 GET_MODE (target), temp,
5193 SUBREG_PROMOTED_UNSIGNED_P (target));
5196 convert_move (SUBREG_REG (target), temp,
5197 SUBREG_PROMOTED_UNSIGNED_P (target));
5199 return NULL_RTX;
5201 else if ((TREE_CODE (exp) == STRING_CST
5202 || (TREE_CODE (exp) == MEM_REF
5203 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5204 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5205 == STRING_CST
5206 && integer_zerop (TREE_OPERAND (exp, 1))))
5207 && !nontemporal && !call_param_p
5208 && MEM_P (target))
5210 /* Optimize initialization of an array with a STRING_CST. */
5211 HOST_WIDE_INT exp_len, str_copy_len;
5212 rtx dest_mem;
5213 tree str = TREE_CODE (exp) == STRING_CST
5214 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5216 exp_len = int_expr_size (exp);
5217 if (exp_len <= 0)
5218 goto normal_expr;
5220 if (TREE_STRING_LENGTH (str) <= 0)
5221 goto normal_expr;
5223 str_copy_len = strlen (TREE_STRING_POINTER (str));
5224 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5225 goto normal_expr;
5227 str_copy_len = TREE_STRING_LENGTH (str);
5228 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5229 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5231 str_copy_len += STORE_MAX_PIECES - 1;
5232 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5234 str_copy_len = MIN (str_copy_len, exp_len);
5235 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5236 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5237 MEM_ALIGN (target), false))
5238 goto normal_expr;
5240 dest_mem = target;
5242 dest_mem = store_by_pieces (dest_mem,
5243 str_copy_len, builtin_strncpy_read_str,
5244 CONST_CAST (char *,
5245 TREE_STRING_POINTER (str)),
5246 MEM_ALIGN (target), false,
5247 exp_len > str_copy_len ? 1 : 0);
5248 if (exp_len > str_copy_len)
5249 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5250 GEN_INT (exp_len - str_copy_len),
5251 BLOCK_OP_NORMAL);
5252 return NULL_RTX;
5254 else
5256 rtx tmp_target;
5258 normal_expr:
5259 /* If we want to use a nontemporal store, force the value to
5260 register first. */
5261 tmp_target = nontemporal ? NULL_RTX : target;
5262 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5263 (call_param_p
5264 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5265 &alt_rtl);
5268 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5269 the same as that of TARGET, adjust the constant. This is needed, for
5270 example, in case it is a CONST_DOUBLE and we want only a word-sized
5271 value. */
5272 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5273 && TREE_CODE (exp) != ERROR_MARK
5274 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5275 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5276 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5278 /* If value was not generated in the target, store it there.
5279 Convert the value to TARGET's type first if necessary and emit the
5280 pending incrementations that have been queued when expanding EXP.
5281 Note that we cannot emit the whole queue blindly because this will
5282 effectively disable the POST_INC optimization later.
5284 If TEMP and TARGET compare equal according to rtx_equal_p, but
5285 one or both of them are volatile memory refs, we have to distinguish
5286 two cases:
5287 - expand_expr has used TARGET. In this case, we must not generate
5288 another copy. This can be detected by TARGET being equal according
5289 to == .
5290 - expand_expr has not used TARGET - that means that the source just
5291 happens to have the same RTX form. Since temp will have been created
5292 by expand_expr, it will compare unequal according to == .
5293 We must generate a copy in this case, to reach the correct number
5294 of volatile memory references. */
5296 if ((! rtx_equal_p (temp, target)
5297 || (temp != target && (side_effects_p (temp)
5298 || side_effects_p (target))))
5299 && TREE_CODE (exp) != ERROR_MARK
5300 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5301 but TARGET is not valid memory reference, TEMP will differ
5302 from TARGET although it is really the same location. */
5303 && !(alt_rtl
5304 && rtx_equal_p (alt_rtl, target)
5305 && !side_effects_p (alt_rtl)
5306 && !side_effects_p (target))
5307 /* If there's nothing to copy, don't bother. Don't call
5308 expr_size unless necessary, because some front-ends (C++)
5309 expr_size-hook must not be given objects that are not
5310 supposed to be bit-copied or bit-initialized. */
5311 && expr_size (exp) != const0_rtx)
5313 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5315 if (GET_MODE (target) == BLKmode)
5317 /* Handle calls that return BLKmode values in registers. */
5318 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5319 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5320 else
5321 store_bit_field (target,
5322 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5323 0, 0, 0, GET_MODE (temp), temp);
5325 else
5326 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5329 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5331 /* Handle copying a string constant into an array. The string
5332 constant may be shorter than the array. So copy just the string's
5333 actual length, and clear the rest. First get the size of the data
5334 type of the string, which is actually the size of the target. */
5335 rtx size = expr_size (exp);
5337 if (CONST_INT_P (size)
5338 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5339 emit_block_move (target, temp, size,
5340 (call_param_p
5341 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5342 else
5344 enum machine_mode pointer_mode
5345 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5346 enum machine_mode address_mode = get_address_mode (target);
5348 /* Compute the size of the data to copy from the string. */
5349 tree copy_size
5350 = size_binop_loc (loc, MIN_EXPR,
5351 make_tree (sizetype, size),
5352 size_int (TREE_STRING_LENGTH (exp)));
5353 rtx copy_size_rtx
5354 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5355 (call_param_p
5356 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5357 rtx label = 0;
5359 /* Copy that much. */
5360 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5361 TYPE_UNSIGNED (sizetype));
5362 emit_block_move (target, temp, copy_size_rtx,
5363 (call_param_p
5364 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5366 /* Figure out how much is left in TARGET that we have to clear.
5367 Do all calculations in pointer_mode. */
5368 if (CONST_INT_P (copy_size_rtx))
5370 size = plus_constant (address_mode, size,
5371 -INTVAL (copy_size_rtx));
5372 target = adjust_address (target, BLKmode,
5373 INTVAL (copy_size_rtx));
5375 else
5377 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5378 copy_size_rtx, NULL_RTX, 0,
5379 OPTAB_LIB_WIDEN);
5381 if (GET_MODE (copy_size_rtx) != address_mode)
5382 copy_size_rtx = convert_to_mode (address_mode,
5383 copy_size_rtx,
5384 TYPE_UNSIGNED (sizetype));
5386 target = offset_address (target, copy_size_rtx,
5387 highest_pow2_factor (copy_size));
5388 label = gen_label_rtx ();
5389 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5390 GET_MODE (size), 0, label);
5393 if (size != const0_rtx)
5394 clear_storage (target, size, BLOCK_OP_NORMAL);
5396 if (label)
5397 emit_label (label);
5400 /* Handle calls that return values in multiple non-contiguous locations.
5401 The Irix 6 ABI has examples of this. */
5402 else if (GET_CODE (target) == PARALLEL)
5404 if (GET_CODE (temp) == PARALLEL)
5405 emit_group_move (target, temp);
5406 else
5407 emit_group_load (target, temp, TREE_TYPE (exp),
5408 int_size_in_bytes (TREE_TYPE (exp)));
5410 else if (GET_CODE (temp) == PARALLEL)
5411 emit_group_store (target, temp, TREE_TYPE (exp),
5412 int_size_in_bytes (TREE_TYPE (exp)));
5413 else if (GET_MODE (temp) == BLKmode)
5414 emit_block_move (target, temp, expr_size (exp),
5415 (call_param_p
5416 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5417 /* If we emit a nontemporal store, there is nothing else to do. */
5418 else if (nontemporal && emit_storent_insn (target, temp))
5420 else
5422 temp = force_operand (temp, target);
5423 if (temp != target)
5424 emit_move_insn (target, temp);
5428 return NULL_RTX;
5431 /* Return true if field F of structure TYPE is a flexible array. */
5433 static bool
5434 flexible_array_member_p (const_tree f, const_tree type)
5436 const_tree tf;
5438 tf = TREE_TYPE (f);
5439 return (DECL_CHAIN (f) == NULL
5440 && TREE_CODE (tf) == ARRAY_TYPE
5441 && TYPE_DOMAIN (tf)
5442 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5443 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5444 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5445 && int_size_in_bytes (type) >= 0);
5448 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5449 must have in order for it to completely initialize a value of type TYPE.
5450 Return -1 if the number isn't known.
5452 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5454 static HOST_WIDE_INT
5455 count_type_elements (const_tree type, bool for_ctor_p)
5457 switch (TREE_CODE (type))
5459 case ARRAY_TYPE:
5461 tree nelts;
5463 nelts = array_type_nelts (type);
5464 if (nelts && host_integerp (nelts, 1))
5466 unsigned HOST_WIDE_INT n;
5468 n = tree_low_cst (nelts, 1) + 1;
5469 if (n == 0 || for_ctor_p)
5470 return n;
5471 else
5472 return n * count_type_elements (TREE_TYPE (type), false);
5474 return for_ctor_p ? -1 : 1;
5477 case RECORD_TYPE:
5479 unsigned HOST_WIDE_INT n;
5480 tree f;
5482 n = 0;
5483 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5484 if (TREE_CODE (f) == FIELD_DECL)
5486 if (!for_ctor_p)
5487 n += count_type_elements (TREE_TYPE (f), false);
5488 else if (!flexible_array_member_p (f, type))
5489 /* Don't count flexible arrays, which are not supposed
5490 to be initialized. */
5491 n += 1;
5494 return n;
5497 case UNION_TYPE:
5498 case QUAL_UNION_TYPE:
5500 tree f;
5501 HOST_WIDE_INT n, m;
5503 gcc_assert (!for_ctor_p);
5504 /* Estimate the number of scalars in each field and pick the
5505 maximum. Other estimates would do instead; the idea is simply
5506 to make sure that the estimate is not sensitive to the ordering
5507 of the fields. */
5508 n = 1;
5509 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5510 if (TREE_CODE (f) == FIELD_DECL)
5512 m = count_type_elements (TREE_TYPE (f), false);
5513 /* If the field doesn't span the whole union, add an extra
5514 scalar for the rest. */
5515 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5516 TYPE_SIZE (type)) != 1)
5517 m++;
5518 if (n < m)
5519 n = m;
5521 return n;
5524 case COMPLEX_TYPE:
5525 return 2;
5527 case VECTOR_TYPE:
5528 return TYPE_VECTOR_SUBPARTS (type);
5530 case INTEGER_TYPE:
5531 case REAL_TYPE:
5532 case FIXED_POINT_TYPE:
5533 case ENUMERAL_TYPE:
5534 case BOOLEAN_TYPE:
5535 case POINTER_TYPE:
5536 case OFFSET_TYPE:
5537 case REFERENCE_TYPE:
5538 case NULLPTR_TYPE:
5539 return 1;
5541 case ERROR_MARK:
5542 return 0;
5544 case VOID_TYPE:
5545 case METHOD_TYPE:
5546 case FUNCTION_TYPE:
5547 case LANG_TYPE:
5548 default:
5549 gcc_unreachable ();
5553 /* Helper for categorize_ctor_elements. Identical interface. */
5555 static bool
5556 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5557 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5559 unsigned HOST_WIDE_INT idx;
5560 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5561 tree value, purpose, elt_type;
5563 /* Whether CTOR is a valid constant initializer, in accordance with what
5564 initializer_constant_valid_p does. If inferred from the constructor
5565 elements, true until proven otherwise. */
5566 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5567 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5569 nz_elts = 0;
5570 init_elts = 0;
5571 num_fields = 0;
5572 elt_type = NULL_TREE;
5574 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5576 HOST_WIDE_INT mult = 1;
5578 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5580 tree lo_index = TREE_OPERAND (purpose, 0);
5581 tree hi_index = TREE_OPERAND (purpose, 1);
5583 if (host_integerp (lo_index, 1) && host_integerp (hi_index, 1))
5584 mult = (tree_low_cst (hi_index, 1)
5585 - tree_low_cst (lo_index, 1) + 1);
5587 num_fields += mult;
5588 elt_type = TREE_TYPE (value);
5590 switch (TREE_CODE (value))
5592 case CONSTRUCTOR:
5594 HOST_WIDE_INT nz = 0, ic = 0;
5596 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5597 p_complete);
5599 nz_elts += mult * nz;
5600 init_elts += mult * ic;
5602 if (const_from_elts_p && const_p)
5603 const_p = const_elt_p;
5605 break;
5607 case INTEGER_CST:
5608 case REAL_CST:
5609 case FIXED_CST:
5610 if (!initializer_zerop (value))
5611 nz_elts += mult;
5612 init_elts += mult;
5613 break;
5615 case STRING_CST:
5616 nz_elts += mult * TREE_STRING_LENGTH (value);
5617 init_elts += mult * TREE_STRING_LENGTH (value);
5618 break;
5620 case COMPLEX_CST:
5621 if (!initializer_zerop (TREE_REALPART (value)))
5622 nz_elts += mult;
5623 if (!initializer_zerop (TREE_IMAGPART (value)))
5624 nz_elts += mult;
5625 init_elts += mult;
5626 break;
5628 case VECTOR_CST:
5630 unsigned i;
5631 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5633 tree v = VECTOR_CST_ELT (value, i);
5634 if (!initializer_zerop (v))
5635 nz_elts += mult;
5636 init_elts += mult;
5639 break;
5641 default:
5643 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5644 nz_elts += mult * tc;
5645 init_elts += mult * tc;
5647 if (const_from_elts_p && const_p)
5648 const_p = initializer_constant_valid_p (value, elt_type)
5649 != NULL_TREE;
5651 break;
5655 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5656 num_fields, elt_type))
5657 *p_complete = false;
5659 *p_nz_elts += nz_elts;
5660 *p_init_elts += init_elts;
5662 return const_p;
5665 /* Examine CTOR to discover:
5666 * how many scalar fields are set to nonzero values,
5667 and place it in *P_NZ_ELTS;
5668 * how many scalar fields in total are in CTOR,
5669 and place it in *P_ELT_COUNT.
5670 * whether the constructor is complete -- in the sense that every
5671 meaningful byte is explicitly given a value --
5672 and place it in *P_COMPLETE.
5674 Return whether or not CTOR is a valid static constant initializer, the same
5675 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5677 bool
5678 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5679 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5681 *p_nz_elts = 0;
5682 *p_init_elts = 0;
5683 *p_complete = true;
5685 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5688 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5689 of which had type LAST_TYPE. Each element was itself a complete
5690 initializer, in the sense that every meaningful byte was explicitly
5691 given a value. Return true if the same is true for the constructor
5692 as a whole. */
5694 bool
5695 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5696 const_tree last_type)
5698 if (TREE_CODE (type) == UNION_TYPE
5699 || TREE_CODE (type) == QUAL_UNION_TYPE)
5701 if (num_elts == 0)
5702 return false;
5704 gcc_assert (num_elts == 1 && last_type);
5706 /* ??? We could look at each element of the union, and find the
5707 largest element. Which would avoid comparing the size of the
5708 initialized element against any tail padding in the union.
5709 Doesn't seem worth the effort... */
5710 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5713 return count_type_elements (type, true) == num_elts;
5716 /* Return 1 if EXP contains mostly (3/4) zeros. */
5718 static int
5719 mostly_zeros_p (const_tree exp)
5721 if (TREE_CODE (exp) == CONSTRUCTOR)
5723 HOST_WIDE_INT nz_elts, init_elts;
5724 bool complete_p;
5726 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5727 return !complete_p || nz_elts < init_elts / 4;
5730 return initializer_zerop (exp);
5733 /* Return 1 if EXP contains all zeros. */
5735 static int
5736 all_zeros_p (const_tree exp)
5738 if (TREE_CODE (exp) == CONSTRUCTOR)
5740 HOST_WIDE_INT nz_elts, init_elts;
5741 bool complete_p;
5743 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5744 return nz_elts == 0;
5747 return initializer_zerop (exp);
5750 /* Helper function for store_constructor.
5751 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5752 CLEARED is as for store_constructor.
5753 ALIAS_SET is the alias set to use for any stores.
5755 This provides a recursive shortcut back to store_constructor when it isn't
5756 necessary to go through store_field. This is so that we can pass through
5757 the cleared field to let store_constructor know that we may not have to
5758 clear a substructure if the outer structure has already been cleared. */
5760 static void
5761 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5762 HOST_WIDE_INT bitpos, enum machine_mode mode,
5763 tree exp, int cleared, alias_set_type alias_set)
5765 if (TREE_CODE (exp) == CONSTRUCTOR
5766 /* We can only call store_constructor recursively if the size and
5767 bit position are on a byte boundary. */
5768 && bitpos % BITS_PER_UNIT == 0
5769 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5770 /* If we have a nonzero bitpos for a register target, then we just
5771 let store_field do the bitfield handling. This is unlikely to
5772 generate unnecessary clear instructions anyways. */
5773 && (bitpos == 0 || MEM_P (target)))
5775 if (MEM_P (target))
5776 target
5777 = adjust_address (target,
5778 GET_MODE (target) == BLKmode
5779 || 0 != (bitpos
5780 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5781 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5784 /* Update the alias set, if required. */
5785 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5786 && MEM_ALIAS_SET (target) != 0)
5788 target = copy_rtx (target);
5789 set_mem_alias_set (target, alias_set);
5792 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5794 else
5795 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5798 /* Store the value of constructor EXP into the rtx TARGET.
5799 TARGET is either a REG or a MEM; we know it cannot conflict, since
5800 safe_from_p has been called.
5801 CLEARED is true if TARGET is known to have been zero'd.
5802 SIZE is the number of bytes of TARGET we are allowed to modify: this
5803 may not be the same as the size of EXP if we are assigning to a field
5804 which has been packed to exclude padding bits. */
5806 static void
5807 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5809 tree type = TREE_TYPE (exp);
5810 #ifdef WORD_REGISTER_OPERATIONS
5811 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5812 #endif
5814 switch (TREE_CODE (type))
5816 case RECORD_TYPE:
5817 case UNION_TYPE:
5818 case QUAL_UNION_TYPE:
5820 unsigned HOST_WIDE_INT idx;
5821 tree field, value;
5823 /* If size is zero or the target is already cleared, do nothing. */
5824 if (size == 0 || cleared)
5825 cleared = 1;
5826 /* We either clear the aggregate or indicate the value is dead. */
5827 else if ((TREE_CODE (type) == UNION_TYPE
5828 || TREE_CODE (type) == QUAL_UNION_TYPE)
5829 && ! CONSTRUCTOR_ELTS (exp))
5830 /* If the constructor is empty, clear the union. */
5832 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5833 cleared = 1;
5836 /* If we are building a static constructor into a register,
5837 set the initial value as zero so we can fold the value into
5838 a constant. But if more than one register is involved,
5839 this probably loses. */
5840 else if (REG_P (target) && TREE_STATIC (exp)
5841 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5843 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5844 cleared = 1;
5847 /* If the constructor has fewer fields than the structure or
5848 if we are initializing the structure to mostly zeros, clear
5849 the whole structure first. Don't do this if TARGET is a
5850 register whose mode size isn't equal to SIZE since
5851 clear_storage can't handle this case. */
5852 else if (size > 0
5853 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5854 != fields_length (type))
5855 || mostly_zeros_p (exp))
5856 && (!REG_P (target)
5857 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5858 == size)))
5860 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5861 cleared = 1;
5864 if (REG_P (target) && !cleared)
5865 emit_clobber (target);
5867 /* Store each element of the constructor into the
5868 corresponding field of TARGET. */
5869 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5871 enum machine_mode mode;
5872 HOST_WIDE_INT bitsize;
5873 HOST_WIDE_INT bitpos = 0;
5874 tree offset;
5875 rtx to_rtx = target;
5877 /* Just ignore missing fields. We cleared the whole
5878 structure, above, if any fields are missing. */
5879 if (field == 0)
5880 continue;
5882 if (cleared && initializer_zerop (value))
5883 continue;
5885 if (host_integerp (DECL_SIZE (field), 1))
5886 bitsize = tree_low_cst (DECL_SIZE (field), 1);
5887 else
5888 bitsize = -1;
5890 mode = DECL_MODE (field);
5891 if (DECL_BIT_FIELD (field))
5892 mode = VOIDmode;
5894 offset = DECL_FIELD_OFFSET (field);
5895 if (host_integerp (offset, 0)
5896 && host_integerp (bit_position (field), 0))
5898 bitpos = int_bit_position (field);
5899 offset = 0;
5901 else
5902 bitpos = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0);
5904 if (offset)
5906 enum machine_mode address_mode;
5907 rtx offset_rtx;
5909 offset
5910 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5911 make_tree (TREE_TYPE (exp),
5912 target));
5914 offset_rtx = expand_normal (offset);
5915 gcc_assert (MEM_P (to_rtx));
5917 address_mode = get_address_mode (to_rtx);
5918 if (GET_MODE (offset_rtx) != address_mode)
5919 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5921 to_rtx = offset_address (to_rtx, offset_rtx,
5922 highest_pow2_factor (offset));
5925 #ifdef WORD_REGISTER_OPERATIONS
5926 /* If this initializes a field that is smaller than a
5927 word, at the start of a word, try to widen it to a full
5928 word. This special case allows us to output C++ member
5929 function initializations in a form that the optimizers
5930 can understand. */
5931 if (REG_P (target)
5932 && bitsize < BITS_PER_WORD
5933 && bitpos % BITS_PER_WORD == 0
5934 && GET_MODE_CLASS (mode) == MODE_INT
5935 && TREE_CODE (value) == INTEGER_CST
5936 && exp_size >= 0
5937 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
5939 tree type = TREE_TYPE (value);
5941 if (TYPE_PRECISION (type) < BITS_PER_WORD)
5943 type = lang_hooks.types.type_for_mode
5944 (word_mode, TYPE_UNSIGNED (type));
5945 value = fold_convert (type, value);
5948 if (BYTES_BIG_ENDIAN)
5949 value
5950 = fold_build2 (LSHIFT_EXPR, type, value,
5951 build_int_cst (type,
5952 BITS_PER_WORD - bitsize));
5953 bitsize = BITS_PER_WORD;
5954 mode = word_mode;
5956 #endif
5958 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
5959 && DECL_NONADDRESSABLE_P (field))
5961 to_rtx = copy_rtx (to_rtx);
5962 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
5965 store_constructor_field (to_rtx, bitsize, bitpos, mode,
5966 value, cleared,
5967 get_alias_set (TREE_TYPE (field)));
5969 break;
5971 case ARRAY_TYPE:
5973 tree value, index;
5974 unsigned HOST_WIDE_INT i;
5975 int need_to_clear;
5976 tree domain;
5977 tree elttype = TREE_TYPE (type);
5978 int const_bounds_p;
5979 HOST_WIDE_INT minelt = 0;
5980 HOST_WIDE_INT maxelt = 0;
5982 domain = TYPE_DOMAIN (type);
5983 const_bounds_p = (TYPE_MIN_VALUE (domain)
5984 && TYPE_MAX_VALUE (domain)
5985 && host_integerp (TYPE_MIN_VALUE (domain), 0)
5986 && host_integerp (TYPE_MAX_VALUE (domain), 0));
5988 /* If we have constant bounds for the range of the type, get them. */
5989 if (const_bounds_p)
5991 minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
5992 maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
5995 /* If the constructor has fewer elements than the array, clear
5996 the whole array first. Similarly if this is static
5997 constructor of a non-BLKmode object. */
5998 if (cleared)
5999 need_to_clear = 0;
6000 else if (REG_P (target) && TREE_STATIC (exp))
6001 need_to_clear = 1;
6002 else
6004 unsigned HOST_WIDE_INT idx;
6005 tree index, value;
6006 HOST_WIDE_INT count = 0, zero_count = 0;
6007 need_to_clear = ! const_bounds_p;
6009 /* This loop is a more accurate version of the loop in
6010 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6011 is also needed to check for missing elements. */
6012 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6014 HOST_WIDE_INT this_node_count;
6016 if (need_to_clear)
6017 break;
6019 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6021 tree lo_index = TREE_OPERAND (index, 0);
6022 tree hi_index = TREE_OPERAND (index, 1);
6024 if (! host_integerp (lo_index, 1)
6025 || ! host_integerp (hi_index, 1))
6027 need_to_clear = 1;
6028 break;
6031 this_node_count = (tree_low_cst (hi_index, 1)
6032 - tree_low_cst (lo_index, 1) + 1);
6034 else
6035 this_node_count = 1;
6037 count += this_node_count;
6038 if (mostly_zeros_p (value))
6039 zero_count += this_node_count;
6042 /* Clear the entire array first if there are any missing
6043 elements, or if the incidence of zero elements is >=
6044 75%. */
6045 if (! need_to_clear
6046 && (count < maxelt - minelt + 1
6047 || 4 * zero_count >= 3 * count))
6048 need_to_clear = 1;
6051 if (need_to_clear && size > 0)
6053 if (REG_P (target))
6054 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6055 else
6056 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6057 cleared = 1;
6060 if (!cleared && REG_P (target))
6061 /* Inform later passes that the old value is dead. */
6062 emit_clobber (target);
6064 /* Store each element of the constructor into the
6065 corresponding element of TARGET, determined by counting the
6066 elements. */
6067 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6069 enum machine_mode mode;
6070 HOST_WIDE_INT bitsize;
6071 HOST_WIDE_INT bitpos;
6072 rtx xtarget = target;
6074 if (cleared && initializer_zerop (value))
6075 continue;
6077 mode = TYPE_MODE (elttype);
6078 if (mode == BLKmode)
6079 bitsize = (host_integerp (TYPE_SIZE (elttype), 1)
6080 ? tree_low_cst (TYPE_SIZE (elttype), 1)
6081 : -1);
6082 else
6083 bitsize = GET_MODE_BITSIZE (mode);
6085 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6087 tree lo_index = TREE_OPERAND (index, 0);
6088 tree hi_index = TREE_OPERAND (index, 1);
6089 rtx index_r, pos_rtx;
6090 HOST_WIDE_INT lo, hi, count;
6091 tree position;
6093 /* If the range is constant and "small", unroll the loop. */
6094 if (const_bounds_p
6095 && host_integerp (lo_index, 0)
6096 && host_integerp (hi_index, 0)
6097 && (lo = tree_low_cst (lo_index, 0),
6098 hi = tree_low_cst (hi_index, 0),
6099 count = hi - lo + 1,
6100 (!MEM_P (target)
6101 || count <= 2
6102 || (host_integerp (TYPE_SIZE (elttype), 1)
6103 && (tree_low_cst (TYPE_SIZE (elttype), 1) * count
6104 <= 40 * 8)))))
6106 lo -= minelt; hi -= minelt;
6107 for (; lo <= hi; lo++)
6109 bitpos = lo * tree_low_cst (TYPE_SIZE (elttype), 0);
6111 if (MEM_P (target)
6112 && !MEM_KEEP_ALIAS_SET_P (target)
6113 && TREE_CODE (type) == ARRAY_TYPE
6114 && TYPE_NONALIASED_COMPONENT (type))
6116 target = copy_rtx (target);
6117 MEM_KEEP_ALIAS_SET_P (target) = 1;
6120 store_constructor_field
6121 (target, bitsize, bitpos, mode, value, cleared,
6122 get_alias_set (elttype));
6125 else
6127 rtx loop_start = gen_label_rtx ();
6128 rtx loop_end = gen_label_rtx ();
6129 tree exit_cond;
6131 expand_normal (hi_index);
6133 index = build_decl (EXPR_LOCATION (exp),
6134 VAR_DECL, NULL_TREE, domain);
6135 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6136 SET_DECL_RTL (index, index_r);
6137 store_expr (lo_index, index_r, 0, false);
6139 /* Build the head of the loop. */
6140 do_pending_stack_adjust ();
6141 emit_label (loop_start);
6143 /* Assign value to element index. */
6144 position =
6145 fold_convert (ssizetype,
6146 fold_build2 (MINUS_EXPR,
6147 TREE_TYPE (index),
6148 index,
6149 TYPE_MIN_VALUE (domain)));
6151 position =
6152 size_binop (MULT_EXPR, position,
6153 fold_convert (ssizetype,
6154 TYPE_SIZE_UNIT (elttype)));
6156 pos_rtx = expand_normal (position);
6157 xtarget = offset_address (target, pos_rtx,
6158 highest_pow2_factor (position));
6159 xtarget = adjust_address (xtarget, mode, 0);
6160 if (TREE_CODE (value) == CONSTRUCTOR)
6161 store_constructor (value, xtarget, cleared,
6162 bitsize / BITS_PER_UNIT);
6163 else
6164 store_expr (value, xtarget, 0, false);
6166 /* Generate a conditional jump to exit the loop. */
6167 exit_cond = build2 (LT_EXPR, integer_type_node,
6168 index, hi_index);
6169 jumpif (exit_cond, loop_end, -1);
6171 /* Update the loop counter, and jump to the head of
6172 the loop. */
6173 expand_assignment (index,
6174 build2 (PLUS_EXPR, TREE_TYPE (index),
6175 index, integer_one_node),
6176 false);
6178 emit_jump (loop_start);
6180 /* Build the end of the loop. */
6181 emit_label (loop_end);
6184 else if ((index != 0 && ! host_integerp (index, 0))
6185 || ! host_integerp (TYPE_SIZE (elttype), 1))
6187 tree position;
6189 if (index == 0)
6190 index = ssize_int (1);
6192 if (minelt)
6193 index = fold_convert (ssizetype,
6194 fold_build2 (MINUS_EXPR,
6195 TREE_TYPE (index),
6196 index,
6197 TYPE_MIN_VALUE (domain)));
6199 position =
6200 size_binop (MULT_EXPR, index,
6201 fold_convert (ssizetype,
6202 TYPE_SIZE_UNIT (elttype)));
6203 xtarget = offset_address (target,
6204 expand_normal (position),
6205 highest_pow2_factor (position));
6206 xtarget = adjust_address (xtarget, mode, 0);
6207 store_expr (value, xtarget, 0, false);
6209 else
6211 if (index != 0)
6212 bitpos = ((tree_low_cst (index, 0) - minelt)
6213 * tree_low_cst (TYPE_SIZE (elttype), 1));
6214 else
6215 bitpos = (i * tree_low_cst (TYPE_SIZE (elttype), 1));
6217 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6218 && TREE_CODE (type) == ARRAY_TYPE
6219 && TYPE_NONALIASED_COMPONENT (type))
6221 target = copy_rtx (target);
6222 MEM_KEEP_ALIAS_SET_P (target) = 1;
6224 store_constructor_field (target, bitsize, bitpos, mode, value,
6225 cleared, get_alias_set (elttype));
6228 break;
6231 case VECTOR_TYPE:
6233 unsigned HOST_WIDE_INT idx;
6234 constructor_elt *ce;
6235 int i;
6236 int need_to_clear;
6237 int icode = CODE_FOR_nothing;
6238 tree elttype = TREE_TYPE (type);
6239 int elt_size = tree_low_cst (TYPE_SIZE (elttype), 1);
6240 enum machine_mode eltmode = TYPE_MODE (elttype);
6241 HOST_WIDE_INT bitsize;
6242 HOST_WIDE_INT bitpos;
6243 rtvec vector = NULL;
6244 unsigned n_elts;
6245 alias_set_type alias;
6247 gcc_assert (eltmode != BLKmode);
6249 n_elts = TYPE_VECTOR_SUBPARTS (type);
6250 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6252 enum machine_mode mode = GET_MODE (target);
6254 icode = (int) optab_handler (vec_init_optab, mode);
6255 if (icode != CODE_FOR_nothing)
6257 unsigned int i;
6259 vector = rtvec_alloc (n_elts);
6260 for (i = 0; i < n_elts; i++)
6261 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6265 /* If the constructor has fewer elements than the vector,
6266 clear the whole array first. Similarly if this is static
6267 constructor of a non-BLKmode object. */
6268 if (cleared)
6269 need_to_clear = 0;
6270 else if (REG_P (target) && TREE_STATIC (exp))
6271 need_to_clear = 1;
6272 else
6274 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6275 tree value;
6277 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6279 int n_elts_here = tree_low_cst
6280 (int_const_binop (TRUNC_DIV_EXPR,
6281 TYPE_SIZE (TREE_TYPE (value)),
6282 TYPE_SIZE (elttype)), 1);
6284 count += n_elts_here;
6285 if (mostly_zeros_p (value))
6286 zero_count += n_elts_here;
6289 /* Clear the entire vector first if there are any missing elements,
6290 or if the incidence of zero elements is >= 75%. */
6291 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6294 if (need_to_clear && size > 0 && !vector)
6296 if (REG_P (target))
6297 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6298 else
6299 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6300 cleared = 1;
6303 /* Inform later passes that the old value is dead. */
6304 if (!cleared && !vector && REG_P (target))
6305 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6307 if (MEM_P (target))
6308 alias = MEM_ALIAS_SET (target);
6309 else
6310 alias = get_alias_set (elttype);
6312 /* Store each element of the constructor into the corresponding
6313 element of TARGET, determined by counting the elements. */
6314 for (idx = 0, i = 0;
6315 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6316 idx++, i += bitsize / elt_size)
6318 HOST_WIDE_INT eltpos;
6319 tree value = ce->value;
6321 bitsize = tree_low_cst (TYPE_SIZE (TREE_TYPE (value)), 1);
6322 if (cleared && initializer_zerop (value))
6323 continue;
6325 if (ce->index)
6326 eltpos = tree_low_cst (ce->index, 1);
6327 else
6328 eltpos = i;
6330 if (vector)
6332 /* Vector CONSTRUCTORs should only be built from smaller
6333 vectors in the case of BLKmode vectors. */
6334 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6335 RTVEC_ELT (vector, eltpos)
6336 = expand_normal (value);
6338 else
6340 enum machine_mode value_mode =
6341 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6342 ? TYPE_MODE (TREE_TYPE (value))
6343 : eltmode;
6344 bitpos = eltpos * elt_size;
6345 store_constructor_field (target, bitsize, bitpos, value_mode,
6346 value, cleared, alias);
6350 if (vector)
6351 emit_insn (GEN_FCN (icode)
6352 (target,
6353 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6354 break;
6357 default:
6358 gcc_unreachable ();
6362 /* Store the value of EXP (an expression tree)
6363 into a subfield of TARGET which has mode MODE and occupies
6364 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6365 If MODE is VOIDmode, it means that we are storing into a bit-field.
6367 BITREGION_START is bitpos of the first bitfield in this region.
6368 BITREGION_END is the bitpos of the ending bitfield in this region.
6369 These two fields are 0, if the C++ memory model does not apply,
6370 or we are not interested in keeping track of bitfield regions.
6372 Always return const0_rtx unless we have something particular to
6373 return.
6375 ALIAS_SET is the alias set for the destination. This value will
6376 (in general) be different from that for TARGET, since TARGET is a
6377 reference to the containing structure.
6379 If NONTEMPORAL is true, try generating a nontemporal store. */
6381 static rtx
6382 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6383 unsigned HOST_WIDE_INT bitregion_start,
6384 unsigned HOST_WIDE_INT bitregion_end,
6385 enum machine_mode mode, tree exp,
6386 alias_set_type alias_set, bool nontemporal)
6388 if (TREE_CODE (exp) == ERROR_MARK)
6389 return const0_rtx;
6391 /* If we have nothing to store, do nothing unless the expression has
6392 side-effects. */
6393 if (bitsize == 0)
6394 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6396 if (GET_CODE (target) == CONCAT)
6398 /* We're storing into a struct containing a single __complex. */
6400 gcc_assert (!bitpos);
6401 return store_expr (exp, target, 0, nontemporal);
6404 /* If the structure is in a register or if the component
6405 is a bit field, we cannot use addressing to access it.
6406 Use bit-field techniques or SUBREG to store in it. */
6408 if (mode == VOIDmode
6409 || (mode != BLKmode && ! direct_store[(int) mode]
6410 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6411 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6412 || REG_P (target)
6413 || GET_CODE (target) == SUBREG
6414 /* If the field isn't aligned enough to store as an ordinary memref,
6415 store it as a bit field. */
6416 || (mode != BLKmode
6417 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6418 || bitpos % GET_MODE_ALIGNMENT (mode))
6419 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6420 || (bitpos % BITS_PER_UNIT != 0)))
6421 || (bitsize >= 0 && mode != BLKmode
6422 && GET_MODE_BITSIZE (mode) > bitsize)
6423 /* If the RHS and field are a constant size and the size of the
6424 RHS isn't the same size as the bitfield, we must use bitfield
6425 operations. */
6426 || (bitsize >= 0
6427 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6428 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6429 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6430 decl we must use bitfield operations. */
6431 || (bitsize >= 0
6432 && TREE_CODE (exp) == MEM_REF
6433 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6434 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6435 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6436 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6438 rtx temp;
6439 gimple nop_def;
6441 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6442 implies a mask operation. If the precision is the same size as
6443 the field we're storing into, that mask is redundant. This is
6444 particularly common with bit field assignments generated by the
6445 C front end. */
6446 nop_def = get_def_for_expr (exp, NOP_EXPR);
6447 if (nop_def)
6449 tree type = TREE_TYPE (exp);
6450 if (INTEGRAL_TYPE_P (type)
6451 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6452 && bitsize == TYPE_PRECISION (type))
6454 tree op = gimple_assign_rhs1 (nop_def);
6455 type = TREE_TYPE (op);
6456 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6457 exp = op;
6461 temp = expand_normal (exp);
6463 /* If BITSIZE is narrower than the size of the type of EXP
6464 we will be narrowing TEMP. Normally, what's wanted are the
6465 low-order bits. However, if EXP's type is a record and this is
6466 big-endian machine, we want the upper BITSIZE bits. */
6467 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6468 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6469 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6470 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6471 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6472 NULL_RTX, 1);
6474 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6475 if (mode != VOIDmode && mode != BLKmode
6476 && mode != TYPE_MODE (TREE_TYPE (exp)))
6477 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6479 /* If the modes of TEMP and TARGET are both BLKmode, both
6480 must be in memory and BITPOS must be aligned on a byte
6481 boundary. If so, we simply do a block copy. Likewise
6482 for a BLKmode-like TARGET. */
6483 if (GET_MODE (temp) == BLKmode
6484 && (GET_MODE (target) == BLKmode
6485 || (MEM_P (target)
6486 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6487 && (bitpos % BITS_PER_UNIT) == 0
6488 && (bitsize % BITS_PER_UNIT) == 0)))
6490 gcc_assert (MEM_P (target) && MEM_P (temp)
6491 && (bitpos % BITS_PER_UNIT) == 0);
6493 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6494 emit_block_move (target, temp,
6495 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6496 / BITS_PER_UNIT),
6497 BLOCK_OP_NORMAL);
6499 return const0_rtx;
6502 /* Handle calls that return values in multiple non-contiguous locations.
6503 The Irix 6 ABI has examples of this. */
6504 if (GET_CODE (temp) == PARALLEL)
6506 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6507 rtx temp_target;
6508 if (mode == BLKmode)
6509 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6510 temp_target = gen_reg_rtx (mode);
6511 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6512 temp = temp_target;
6514 else if (mode == BLKmode)
6516 /* Handle calls that return BLKmode values in registers. */
6517 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6519 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6520 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6521 temp = temp_target;
6523 else
6525 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6526 rtx temp_target;
6527 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6528 temp_target = gen_reg_rtx (mode);
6529 temp_target
6530 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6531 false, temp_target, mode, mode);
6532 temp = temp_target;
6536 /* Store the value in the bitfield. */
6537 store_bit_field (target, bitsize, bitpos,
6538 bitregion_start, bitregion_end,
6539 mode, temp);
6541 return const0_rtx;
6543 else
6545 /* Now build a reference to just the desired component. */
6546 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6548 if (to_rtx == target)
6549 to_rtx = copy_rtx (to_rtx);
6551 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6552 set_mem_alias_set (to_rtx, alias_set);
6554 return store_expr (exp, to_rtx, 0, nontemporal);
6558 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6559 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6560 codes and find the ultimate containing object, which we return.
6562 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6563 bit position, and *PUNSIGNEDP to the signedness of the field.
6564 If the position of the field is variable, we store a tree
6565 giving the variable offset (in units) in *POFFSET.
6566 This offset is in addition to the bit position.
6567 If the position is not variable, we store 0 in *POFFSET.
6569 If any of the extraction expressions is volatile,
6570 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6572 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6573 Otherwise, it is a mode that can be used to access the field.
6575 If the field describes a variable-sized object, *PMODE is set to
6576 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6577 this case, but the address of the object can be found.
6579 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6580 look through nodes that serve as markers of a greater alignment than
6581 the one that can be deduced from the expression. These nodes make it
6582 possible for front-ends to prevent temporaries from being created by
6583 the middle-end on alignment considerations. For that purpose, the
6584 normal operating mode at high-level is to always pass FALSE so that
6585 the ultimate containing object is really returned; moreover, the
6586 associated predicate handled_component_p will always return TRUE
6587 on these nodes, thus indicating that they are essentially handled
6588 by get_inner_reference. TRUE should only be passed when the caller
6589 is scanning the expression in order to build another representation
6590 and specifically knows how to handle these nodes; as such, this is
6591 the normal operating mode in the RTL expanders. */
6593 tree
6594 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6595 HOST_WIDE_INT *pbitpos, tree *poffset,
6596 enum machine_mode *pmode, int *punsignedp,
6597 int *pvolatilep, bool keep_aligning)
6599 tree size_tree = 0;
6600 enum machine_mode mode = VOIDmode;
6601 bool blkmode_bitfield = false;
6602 tree offset = size_zero_node;
6603 double_int bit_offset = double_int_zero;
6605 /* First get the mode, signedness, and size. We do this from just the
6606 outermost expression. */
6607 *pbitsize = -1;
6608 if (TREE_CODE (exp) == COMPONENT_REF)
6610 tree field = TREE_OPERAND (exp, 1);
6611 size_tree = DECL_SIZE (field);
6612 if (!DECL_BIT_FIELD (field))
6613 mode = DECL_MODE (field);
6614 else if (DECL_MODE (field) == BLKmode)
6615 blkmode_bitfield = true;
6616 else if (TREE_THIS_VOLATILE (exp)
6617 && flag_strict_volatile_bitfields > 0)
6618 /* Volatile bitfields should be accessed in the mode of the
6619 field's type, not the mode computed based on the bit
6620 size. */
6621 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6623 *punsignedp = DECL_UNSIGNED (field);
6625 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6627 size_tree = TREE_OPERAND (exp, 1);
6628 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6629 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6631 /* For vector types, with the correct size of access, use the mode of
6632 inner type. */
6633 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6634 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6635 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6636 mode = TYPE_MODE (TREE_TYPE (exp));
6638 else
6640 mode = TYPE_MODE (TREE_TYPE (exp));
6641 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6643 if (mode == BLKmode)
6644 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6645 else
6646 *pbitsize = GET_MODE_BITSIZE (mode);
6649 if (size_tree != 0)
6651 if (! host_integerp (size_tree, 1))
6652 mode = BLKmode, *pbitsize = -1;
6653 else
6654 *pbitsize = tree_low_cst (size_tree, 1);
6657 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6658 and find the ultimate containing object. */
6659 while (1)
6661 switch (TREE_CODE (exp))
6663 case BIT_FIELD_REF:
6664 bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2));
6665 break;
6667 case COMPONENT_REF:
6669 tree field = TREE_OPERAND (exp, 1);
6670 tree this_offset = component_ref_field_offset (exp);
6672 /* If this field hasn't been filled in yet, don't go past it.
6673 This should only happen when folding expressions made during
6674 type construction. */
6675 if (this_offset == 0)
6676 break;
6678 offset = size_binop (PLUS_EXPR, offset, this_offset);
6679 bit_offset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field));
6681 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6683 break;
6685 case ARRAY_REF:
6686 case ARRAY_RANGE_REF:
6688 tree index = TREE_OPERAND (exp, 1);
6689 tree low_bound = array_ref_low_bound (exp);
6690 tree unit_size = array_ref_element_size (exp);
6692 /* We assume all arrays have sizes that are a multiple of a byte.
6693 First subtract the lower bound, if any, in the type of the
6694 index, then convert to sizetype and multiply by the size of
6695 the array element. */
6696 if (! integer_zerop (low_bound))
6697 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6698 index, low_bound);
6700 offset = size_binop (PLUS_EXPR, offset,
6701 size_binop (MULT_EXPR,
6702 fold_convert (sizetype, index),
6703 unit_size));
6705 break;
6707 case REALPART_EXPR:
6708 break;
6710 case IMAGPART_EXPR:
6711 bit_offset += double_int::from_uhwi (*pbitsize);
6712 break;
6714 case VIEW_CONVERT_EXPR:
6715 if (keep_aligning && STRICT_ALIGNMENT
6716 && (TYPE_ALIGN (TREE_TYPE (exp))
6717 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6718 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6719 < BIGGEST_ALIGNMENT)
6720 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6721 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6722 goto done;
6723 break;
6725 case MEM_REF:
6726 /* Hand back the decl for MEM[&decl, off]. */
6727 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6729 tree off = TREE_OPERAND (exp, 1);
6730 if (!integer_zerop (off))
6732 double_int boff, coff = mem_ref_offset (exp);
6733 boff = coff.alshift (BITS_PER_UNIT == 8
6734 ? 3 : exact_log2 (BITS_PER_UNIT),
6735 HOST_BITS_PER_DOUBLE_INT);
6736 bit_offset += boff;
6738 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6740 goto done;
6742 default:
6743 goto done;
6746 /* If any reference in the chain is volatile, the effect is volatile. */
6747 if (TREE_THIS_VOLATILE (exp))
6748 *pvolatilep = 1;
6750 exp = TREE_OPERAND (exp, 0);
6752 done:
6754 /* If OFFSET is constant, see if we can return the whole thing as a
6755 constant bit position. Make sure to handle overflow during
6756 this conversion. */
6757 if (TREE_CODE (offset) == INTEGER_CST)
6759 double_int tem = tree_to_double_int (offset);
6760 tem = tem.sext (TYPE_PRECISION (sizetype));
6761 tem = tem.alshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT),
6762 HOST_BITS_PER_DOUBLE_INT);
6763 tem += bit_offset;
6764 if (tem.fits_shwi ())
6766 *pbitpos = tem.to_shwi ();
6767 *poffset = offset = NULL_TREE;
6771 /* Otherwise, split it up. */
6772 if (offset)
6774 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6775 if (bit_offset.is_negative ())
6777 double_int mask
6778 = double_int::mask (BITS_PER_UNIT == 8
6779 ? 3 : exact_log2 (BITS_PER_UNIT));
6780 double_int tem = bit_offset.and_not (mask);
6781 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6782 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6783 bit_offset -= tem;
6784 tem = tem.arshift (BITS_PER_UNIT == 8
6785 ? 3 : exact_log2 (BITS_PER_UNIT),
6786 HOST_BITS_PER_DOUBLE_INT);
6787 offset = size_binop (PLUS_EXPR, offset,
6788 double_int_to_tree (sizetype, tem));
6791 *pbitpos = bit_offset.to_shwi ();
6792 *poffset = offset;
6795 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6796 if (mode == VOIDmode
6797 && blkmode_bitfield
6798 && (*pbitpos % BITS_PER_UNIT) == 0
6799 && (*pbitsize % BITS_PER_UNIT) == 0)
6800 *pmode = BLKmode;
6801 else
6802 *pmode = mode;
6804 return exp;
6807 /* Return a tree of sizetype representing the size, in bytes, of the element
6808 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6810 tree
6811 array_ref_element_size (tree exp)
6813 tree aligned_size = TREE_OPERAND (exp, 3);
6814 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6815 location_t loc = EXPR_LOCATION (exp);
6817 /* If a size was specified in the ARRAY_REF, it's the size measured
6818 in alignment units of the element type. So multiply by that value. */
6819 if (aligned_size)
6821 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6822 sizetype from another type of the same width and signedness. */
6823 if (TREE_TYPE (aligned_size) != sizetype)
6824 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6825 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6826 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6829 /* Otherwise, take the size from that of the element type. Substitute
6830 any PLACEHOLDER_EXPR that we have. */
6831 else
6832 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6835 /* Return a tree representing the lower bound of the array mentioned in
6836 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6838 tree
6839 array_ref_low_bound (tree exp)
6841 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6843 /* If a lower bound is specified in EXP, use it. */
6844 if (TREE_OPERAND (exp, 2))
6845 return TREE_OPERAND (exp, 2);
6847 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6848 substituting for a PLACEHOLDER_EXPR as needed. */
6849 if (domain_type && TYPE_MIN_VALUE (domain_type))
6850 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6852 /* Otherwise, return a zero of the appropriate type. */
6853 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6856 /* Returns true if REF is an array reference to an array at the end of
6857 a structure. If this is the case, the array may be allocated larger
6858 than its upper bound implies. */
6860 bool
6861 array_at_struct_end_p (tree ref)
6863 if (TREE_CODE (ref) != ARRAY_REF
6864 && TREE_CODE (ref) != ARRAY_RANGE_REF)
6865 return false;
6867 while (handled_component_p (ref))
6869 /* If the reference chain contains a component reference to a
6870 non-union type and there follows another field the reference
6871 is not at the end of a structure. */
6872 if (TREE_CODE (ref) == COMPONENT_REF
6873 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6875 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6876 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6877 nextf = DECL_CHAIN (nextf);
6878 if (nextf)
6879 return false;
6882 ref = TREE_OPERAND (ref, 0);
6885 /* If the reference is based on a declared entity, the size of the array
6886 is constrained by its given domain. */
6887 if (DECL_P (ref))
6888 return false;
6890 return true;
6893 /* Return a tree representing the upper bound of the array mentioned in
6894 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6896 tree
6897 array_ref_up_bound (tree exp)
6899 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6901 /* If there is a domain type and it has an upper bound, use it, substituting
6902 for a PLACEHOLDER_EXPR as needed. */
6903 if (domain_type && TYPE_MAX_VALUE (domain_type))
6904 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6906 /* Otherwise fail. */
6907 return NULL_TREE;
6910 /* Return a tree representing the offset, in bytes, of the field referenced
6911 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
6913 tree
6914 component_ref_field_offset (tree exp)
6916 tree aligned_offset = TREE_OPERAND (exp, 2);
6917 tree field = TREE_OPERAND (exp, 1);
6918 location_t loc = EXPR_LOCATION (exp);
6920 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
6921 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
6922 value. */
6923 if (aligned_offset)
6925 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6926 sizetype from another type of the same width and signedness. */
6927 if (TREE_TYPE (aligned_offset) != sizetype)
6928 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
6929 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
6930 size_int (DECL_OFFSET_ALIGN (field)
6931 / BITS_PER_UNIT));
6934 /* Otherwise, take the offset from that of the field. Substitute
6935 any PLACEHOLDER_EXPR that we have. */
6936 else
6937 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
6940 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
6942 static unsigned HOST_WIDE_INT
6943 target_align (const_tree target)
6945 /* We might have a chain of nested references with intermediate misaligning
6946 bitfields components, so need to recurse to find out. */
6948 unsigned HOST_WIDE_INT this_align, outer_align;
6950 switch (TREE_CODE (target))
6952 case BIT_FIELD_REF:
6953 return 1;
6955 case COMPONENT_REF:
6956 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
6957 outer_align = target_align (TREE_OPERAND (target, 0));
6958 return MIN (this_align, outer_align);
6960 case ARRAY_REF:
6961 case ARRAY_RANGE_REF:
6962 this_align = TYPE_ALIGN (TREE_TYPE (target));
6963 outer_align = target_align (TREE_OPERAND (target, 0));
6964 return MIN (this_align, outer_align);
6966 CASE_CONVERT:
6967 case NON_LVALUE_EXPR:
6968 case VIEW_CONVERT_EXPR:
6969 this_align = TYPE_ALIGN (TREE_TYPE (target));
6970 outer_align = target_align (TREE_OPERAND (target, 0));
6971 return MAX (this_align, outer_align);
6973 default:
6974 return TYPE_ALIGN (TREE_TYPE (target));
6979 /* Given an rtx VALUE that may contain additions and multiplications, return
6980 an equivalent value that just refers to a register, memory, or constant.
6981 This is done by generating instructions to perform the arithmetic and
6982 returning a pseudo-register containing the value.
6984 The returned value may be a REG, SUBREG, MEM or constant. */
6987 force_operand (rtx value, rtx target)
6989 rtx op1, op2;
6990 /* Use subtarget as the target for operand 0 of a binary operation. */
6991 rtx subtarget = get_subtarget (target);
6992 enum rtx_code code = GET_CODE (value);
6994 /* Check for subreg applied to an expression produced by loop optimizer. */
6995 if (code == SUBREG
6996 && !REG_P (SUBREG_REG (value))
6997 && !MEM_P (SUBREG_REG (value)))
6999 value
7000 = simplify_gen_subreg (GET_MODE (value),
7001 force_reg (GET_MODE (SUBREG_REG (value)),
7002 force_operand (SUBREG_REG (value),
7003 NULL_RTX)),
7004 GET_MODE (SUBREG_REG (value)),
7005 SUBREG_BYTE (value));
7006 code = GET_CODE (value);
7009 /* Check for a PIC address load. */
7010 if ((code == PLUS || code == MINUS)
7011 && XEXP (value, 0) == pic_offset_table_rtx
7012 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7013 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7014 || GET_CODE (XEXP (value, 1)) == CONST))
7016 if (!subtarget)
7017 subtarget = gen_reg_rtx (GET_MODE (value));
7018 emit_move_insn (subtarget, value);
7019 return subtarget;
7022 if (ARITHMETIC_P (value))
7024 op2 = XEXP (value, 1);
7025 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7026 subtarget = 0;
7027 if (code == MINUS && CONST_INT_P (op2))
7029 code = PLUS;
7030 op2 = negate_rtx (GET_MODE (value), op2);
7033 /* Check for an addition with OP2 a constant integer and our first
7034 operand a PLUS of a virtual register and something else. In that
7035 case, we want to emit the sum of the virtual register and the
7036 constant first and then add the other value. This allows virtual
7037 register instantiation to simply modify the constant rather than
7038 creating another one around this addition. */
7039 if (code == PLUS && CONST_INT_P (op2)
7040 && GET_CODE (XEXP (value, 0)) == PLUS
7041 && REG_P (XEXP (XEXP (value, 0), 0))
7042 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7043 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7045 rtx temp = expand_simple_binop (GET_MODE (value), code,
7046 XEXP (XEXP (value, 0), 0), op2,
7047 subtarget, 0, OPTAB_LIB_WIDEN);
7048 return expand_simple_binop (GET_MODE (value), code, temp,
7049 force_operand (XEXP (XEXP (value,
7050 0), 1), 0),
7051 target, 0, OPTAB_LIB_WIDEN);
7054 op1 = force_operand (XEXP (value, 0), subtarget);
7055 op2 = force_operand (op2, NULL_RTX);
7056 switch (code)
7058 case MULT:
7059 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7060 case DIV:
7061 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7062 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7063 target, 1, OPTAB_LIB_WIDEN);
7064 else
7065 return expand_divmod (0,
7066 FLOAT_MODE_P (GET_MODE (value))
7067 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7068 GET_MODE (value), op1, op2, target, 0);
7069 case MOD:
7070 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7071 target, 0);
7072 case UDIV:
7073 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7074 target, 1);
7075 case UMOD:
7076 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7077 target, 1);
7078 case ASHIFTRT:
7079 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7080 target, 0, OPTAB_LIB_WIDEN);
7081 default:
7082 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7083 target, 1, OPTAB_LIB_WIDEN);
7086 if (UNARY_P (value))
7088 if (!target)
7089 target = gen_reg_rtx (GET_MODE (value));
7090 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7091 switch (code)
7093 case ZERO_EXTEND:
7094 case SIGN_EXTEND:
7095 case TRUNCATE:
7096 case FLOAT_EXTEND:
7097 case FLOAT_TRUNCATE:
7098 convert_move (target, op1, code == ZERO_EXTEND);
7099 return target;
7101 case FIX:
7102 case UNSIGNED_FIX:
7103 expand_fix (target, op1, code == UNSIGNED_FIX);
7104 return target;
7106 case FLOAT:
7107 case UNSIGNED_FLOAT:
7108 expand_float (target, op1, code == UNSIGNED_FLOAT);
7109 return target;
7111 default:
7112 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7116 #ifdef INSN_SCHEDULING
7117 /* On machines that have insn scheduling, we want all memory reference to be
7118 explicit, so we need to deal with such paradoxical SUBREGs. */
7119 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7120 value
7121 = simplify_gen_subreg (GET_MODE (value),
7122 force_reg (GET_MODE (SUBREG_REG (value)),
7123 force_operand (SUBREG_REG (value),
7124 NULL_RTX)),
7125 GET_MODE (SUBREG_REG (value)),
7126 SUBREG_BYTE (value));
7127 #endif
7129 return value;
7132 /* Subroutine of expand_expr: return nonzero iff there is no way that
7133 EXP can reference X, which is being modified. TOP_P is nonzero if this
7134 call is going to be used to determine whether we need a temporary
7135 for EXP, as opposed to a recursive call to this function.
7137 It is always safe for this routine to return zero since it merely
7138 searches for optimization opportunities. */
7141 safe_from_p (const_rtx x, tree exp, int top_p)
7143 rtx exp_rtl = 0;
7144 int i, nops;
7146 if (x == 0
7147 /* If EXP has varying size, we MUST use a target since we currently
7148 have no way of allocating temporaries of variable size
7149 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7150 So we assume here that something at a higher level has prevented a
7151 clash. This is somewhat bogus, but the best we can do. Only
7152 do this when X is BLKmode and when we are at the top level. */
7153 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7154 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7155 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7156 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7157 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7158 != INTEGER_CST)
7159 && GET_MODE (x) == BLKmode)
7160 /* If X is in the outgoing argument area, it is always safe. */
7161 || (MEM_P (x)
7162 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7163 || (GET_CODE (XEXP (x, 0)) == PLUS
7164 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7165 return 1;
7167 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7168 find the underlying pseudo. */
7169 if (GET_CODE (x) == SUBREG)
7171 x = SUBREG_REG (x);
7172 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7173 return 0;
7176 /* Now look at our tree code and possibly recurse. */
7177 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7179 case tcc_declaration:
7180 exp_rtl = DECL_RTL_IF_SET (exp);
7181 break;
7183 case tcc_constant:
7184 return 1;
7186 case tcc_exceptional:
7187 if (TREE_CODE (exp) == TREE_LIST)
7189 while (1)
7191 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7192 return 0;
7193 exp = TREE_CHAIN (exp);
7194 if (!exp)
7195 return 1;
7196 if (TREE_CODE (exp) != TREE_LIST)
7197 return safe_from_p (x, exp, 0);
7200 else if (TREE_CODE (exp) == CONSTRUCTOR)
7202 constructor_elt *ce;
7203 unsigned HOST_WIDE_INT idx;
7205 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7206 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7207 || !safe_from_p (x, ce->value, 0))
7208 return 0;
7209 return 1;
7211 else if (TREE_CODE (exp) == ERROR_MARK)
7212 return 1; /* An already-visited SAVE_EXPR? */
7213 else
7214 return 0;
7216 case tcc_statement:
7217 /* The only case we look at here is the DECL_INITIAL inside a
7218 DECL_EXPR. */
7219 return (TREE_CODE (exp) != DECL_EXPR
7220 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7221 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7222 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7224 case tcc_binary:
7225 case tcc_comparison:
7226 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7227 return 0;
7228 /* Fall through. */
7230 case tcc_unary:
7231 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7233 case tcc_expression:
7234 case tcc_reference:
7235 case tcc_vl_exp:
7236 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7237 the expression. If it is set, we conflict iff we are that rtx or
7238 both are in memory. Otherwise, we check all operands of the
7239 expression recursively. */
7241 switch (TREE_CODE (exp))
7243 case ADDR_EXPR:
7244 /* If the operand is static or we are static, we can't conflict.
7245 Likewise if we don't conflict with the operand at all. */
7246 if (staticp (TREE_OPERAND (exp, 0))
7247 || TREE_STATIC (exp)
7248 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7249 return 1;
7251 /* Otherwise, the only way this can conflict is if we are taking
7252 the address of a DECL a that address if part of X, which is
7253 very rare. */
7254 exp = TREE_OPERAND (exp, 0);
7255 if (DECL_P (exp))
7257 if (!DECL_RTL_SET_P (exp)
7258 || !MEM_P (DECL_RTL (exp)))
7259 return 0;
7260 else
7261 exp_rtl = XEXP (DECL_RTL (exp), 0);
7263 break;
7265 case MEM_REF:
7266 if (MEM_P (x)
7267 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7268 get_alias_set (exp)))
7269 return 0;
7270 break;
7272 case CALL_EXPR:
7273 /* Assume that the call will clobber all hard registers and
7274 all of memory. */
7275 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7276 || MEM_P (x))
7277 return 0;
7278 break;
7280 case WITH_CLEANUP_EXPR:
7281 case CLEANUP_POINT_EXPR:
7282 /* Lowered by gimplify.c. */
7283 gcc_unreachable ();
7285 case SAVE_EXPR:
7286 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7288 default:
7289 break;
7292 /* If we have an rtx, we do not need to scan our operands. */
7293 if (exp_rtl)
7294 break;
7296 nops = TREE_OPERAND_LENGTH (exp);
7297 for (i = 0; i < nops; i++)
7298 if (TREE_OPERAND (exp, i) != 0
7299 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7300 return 0;
7302 break;
7304 case tcc_type:
7305 /* Should never get a type here. */
7306 gcc_unreachable ();
7309 /* If we have an rtl, find any enclosed object. Then see if we conflict
7310 with it. */
7311 if (exp_rtl)
7313 if (GET_CODE (exp_rtl) == SUBREG)
7315 exp_rtl = SUBREG_REG (exp_rtl);
7316 if (REG_P (exp_rtl)
7317 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7318 return 0;
7321 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7322 are memory and they conflict. */
7323 return ! (rtx_equal_p (x, exp_rtl)
7324 || (MEM_P (x) && MEM_P (exp_rtl)
7325 && true_dependence (exp_rtl, VOIDmode, x)));
7328 /* If we reach here, it is safe. */
7329 return 1;
7333 /* Return the highest power of two that EXP is known to be a multiple of.
7334 This is used in updating alignment of MEMs in array references. */
7336 unsigned HOST_WIDE_INT
7337 highest_pow2_factor (const_tree exp)
7339 unsigned HOST_WIDE_INT c0, c1;
7341 switch (TREE_CODE (exp))
7343 case INTEGER_CST:
7344 /* We can find the lowest bit that's a one. If the low
7345 HOST_BITS_PER_WIDE_INT bits are zero, return BIGGEST_ALIGNMENT.
7346 We need to handle this case since we can find it in a COND_EXPR,
7347 a MIN_EXPR, or a MAX_EXPR. If the constant overflows, we have an
7348 erroneous program, so return BIGGEST_ALIGNMENT to avoid any
7349 later ICE. */
7350 if (TREE_OVERFLOW (exp))
7351 return BIGGEST_ALIGNMENT;
7352 else
7354 /* Note: tree_low_cst is intentionally not used here,
7355 we don't care about the upper bits. */
7356 c0 = TREE_INT_CST_LOW (exp);
7357 c0 &= -c0;
7358 return c0 ? c0 : BIGGEST_ALIGNMENT;
7360 break;
7362 case PLUS_EXPR: case MINUS_EXPR: case MIN_EXPR: case MAX_EXPR:
7363 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7364 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7365 return MIN (c0, c1);
7367 case MULT_EXPR:
7368 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7369 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7370 return c0 * c1;
7372 case ROUND_DIV_EXPR: case TRUNC_DIV_EXPR: case FLOOR_DIV_EXPR:
7373 case CEIL_DIV_EXPR:
7374 if (integer_pow2p (TREE_OPERAND (exp, 1))
7375 && host_integerp (TREE_OPERAND (exp, 1), 1))
7377 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7378 c1 = tree_low_cst (TREE_OPERAND (exp, 1), 1);
7379 return MAX (1, c0 / c1);
7381 break;
7383 case BIT_AND_EXPR:
7384 /* The highest power of two of a bit-and expression is the maximum of
7385 that of its operands. We typically get here for a complex LHS and
7386 a constant negative power of two on the RHS to force an explicit
7387 alignment, so don't bother looking at the LHS. */
7388 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7390 CASE_CONVERT:
7391 case SAVE_EXPR:
7392 return highest_pow2_factor (TREE_OPERAND (exp, 0));
7394 case COMPOUND_EXPR:
7395 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7397 case COND_EXPR:
7398 c0 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7399 c1 = highest_pow2_factor (TREE_OPERAND (exp, 2));
7400 return MIN (c0, c1);
7402 default:
7403 break;
7406 return 1;
7409 /* Similar, except that the alignment requirements of TARGET are
7410 taken into account. Assume it is at least as aligned as its
7411 type, unless it is a COMPONENT_REF in which case the layout of
7412 the structure gives the alignment. */
7414 static unsigned HOST_WIDE_INT
7415 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7417 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7418 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7420 return MAX (factor, talign);
7423 #ifdef HAVE_conditional_move
7424 /* Convert the tree comparison code TCODE to the rtl one where the
7425 signedness is UNSIGNEDP. */
7427 static enum rtx_code
7428 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7430 enum rtx_code code;
7431 switch (tcode)
7433 case EQ_EXPR:
7434 code = EQ;
7435 break;
7436 case NE_EXPR:
7437 code = NE;
7438 break;
7439 case LT_EXPR:
7440 code = unsignedp ? LTU : LT;
7441 break;
7442 case LE_EXPR:
7443 code = unsignedp ? LEU : LE;
7444 break;
7445 case GT_EXPR:
7446 code = unsignedp ? GTU : GT;
7447 break;
7448 case GE_EXPR:
7449 code = unsignedp ? GEU : GE;
7450 break;
7451 case UNORDERED_EXPR:
7452 code = UNORDERED;
7453 break;
7454 case ORDERED_EXPR:
7455 code = ORDERED;
7456 break;
7457 case UNLT_EXPR:
7458 code = UNLT;
7459 break;
7460 case UNLE_EXPR:
7461 code = UNLE;
7462 break;
7463 case UNGT_EXPR:
7464 code = UNGT;
7465 break;
7466 case UNGE_EXPR:
7467 code = UNGE;
7468 break;
7469 case UNEQ_EXPR:
7470 code = UNEQ;
7471 break;
7472 case LTGT_EXPR:
7473 code = LTGT;
7474 break;
7476 default:
7477 gcc_unreachable ();
7479 return code;
7481 #endif
7483 /* Subroutine of expand_expr. Expand the two operands of a binary
7484 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7485 The value may be stored in TARGET if TARGET is nonzero. The
7486 MODIFIER argument is as documented by expand_expr. */
7488 static void
7489 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7490 enum expand_modifier modifier)
7492 if (! safe_from_p (target, exp1, 1))
7493 target = 0;
7494 if (operand_equal_p (exp0, exp1, 0))
7496 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7497 *op1 = copy_rtx (*op0);
7499 else
7501 /* If we need to preserve evaluation order, copy exp0 into its own
7502 temporary variable so that it can't be clobbered by exp1. */
7503 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7504 exp0 = save_expr (exp0);
7505 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7506 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7511 /* Return a MEM that contains constant EXP. DEFER is as for
7512 output_constant_def and MODIFIER is as for expand_expr. */
7514 static rtx
7515 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7517 rtx mem;
7519 mem = output_constant_def (exp, defer);
7520 if (modifier != EXPAND_INITIALIZER)
7521 mem = use_anchored_address (mem);
7522 return mem;
7525 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7526 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7528 static rtx
7529 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7530 enum expand_modifier modifier, addr_space_t as)
7532 rtx result, subtarget;
7533 tree inner, offset;
7534 HOST_WIDE_INT bitsize, bitpos;
7535 int volatilep, unsignedp;
7536 enum machine_mode mode1;
7538 /* If we are taking the address of a constant and are at the top level,
7539 we have to use output_constant_def since we can't call force_const_mem
7540 at top level. */
7541 /* ??? This should be considered a front-end bug. We should not be
7542 generating ADDR_EXPR of something that isn't an LVALUE. The only
7543 exception here is STRING_CST. */
7544 if (CONSTANT_CLASS_P (exp))
7546 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7547 if (modifier < EXPAND_SUM)
7548 result = force_operand (result, target);
7549 return result;
7552 /* Everything must be something allowed by is_gimple_addressable. */
7553 switch (TREE_CODE (exp))
7555 case INDIRECT_REF:
7556 /* This case will happen via recursion for &a->b. */
7557 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7559 case MEM_REF:
7561 tree tem = TREE_OPERAND (exp, 0);
7562 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7563 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7564 return expand_expr (tem, target, tmode, modifier);
7566 case TARGET_MEM_REF:
7568 /* For TARGET_MEM_REF node, it expects to keep the memory access mode
7569 instead of linearizing the addr expr during expand, so
7570 cse_not_expected will be set to 1 temporarily before expanding addr
7571 expr, and then set back after it. */
7572 struct mem_address addr;
7573 int old_cse_not_expected;
7574 addr_space_t as
7575 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
7577 get_address_description (exp, &addr);
7578 result = addr_for_mem_ref (&addr, as, true);
7579 old_cse_not_expected = cse_not_expected;
7580 cse_not_expected = 1;
7581 result = memory_address_addr_space (tmode, result, as);
7582 cse_not_expected = old_cse_not_expected;
7583 return result;
7585 case CONST_DECL:
7586 /* Expand the initializer like constants above. */
7587 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7588 0, modifier), 0);
7589 if (modifier < EXPAND_SUM)
7590 result = force_operand (result, target);
7591 return result;
7593 case REALPART_EXPR:
7594 /* The real part of the complex number is always first, therefore
7595 the address is the same as the address of the parent object. */
7596 offset = 0;
7597 bitpos = 0;
7598 inner = TREE_OPERAND (exp, 0);
7599 break;
7601 case IMAGPART_EXPR:
7602 /* The imaginary part of the complex number is always second.
7603 The expression is therefore always offset by the size of the
7604 scalar type. */
7605 offset = 0;
7606 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7607 inner = TREE_OPERAND (exp, 0);
7608 break;
7610 case COMPOUND_LITERAL_EXPR:
7611 /* Allow COMPOUND_LITERAL_EXPR in initializers, if e.g.
7612 rtl_for_decl_init is called on DECL_INITIAL with
7613 COMPOUNT_LITERAL_EXPRs in it, they aren't gimplified. */
7614 if (modifier == EXPAND_INITIALIZER
7615 && COMPOUND_LITERAL_EXPR_DECL (exp))
7616 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7617 target, tmode, modifier, as);
7618 /* FALLTHRU */
7619 default:
7620 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7621 expand_expr, as that can have various side effects; LABEL_DECLs for
7622 example, may not have their DECL_RTL set yet. Expand the rtl of
7623 CONSTRUCTORs too, which should yield a memory reference for the
7624 constructor's contents. Assume language specific tree nodes can
7625 be expanded in some interesting way. */
7626 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7627 if (DECL_P (exp)
7628 || TREE_CODE (exp) == CONSTRUCTOR
7629 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7631 result = expand_expr (exp, target, tmode,
7632 modifier == EXPAND_INITIALIZER
7633 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7635 /* If the DECL isn't in memory, then the DECL wasn't properly
7636 marked TREE_ADDRESSABLE, which will be either a front-end
7637 or a tree optimizer bug. */
7639 if (TREE_ADDRESSABLE (exp)
7640 && ! MEM_P (result)
7641 && ! targetm.calls.allocate_stack_slots_for_args())
7643 error ("local frame unavailable (naked function?)");
7644 return result;
7646 else
7647 gcc_assert (MEM_P (result));
7648 result = XEXP (result, 0);
7650 /* ??? Is this needed anymore? */
7651 if (DECL_P (exp))
7652 TREE_USED (exp) = 1;
7654 if (modifier != EXPAND_INITIALIZER
7655 && modifier != EXPAND_CONST_ADDRESS
7656 && modifier != EXPAND_SUM)
7657 result = force_operand (result, target);
7658 return result;
7661 /* Pass FALSE as the last argument to get_inner_reference although
7662 we are expanding to RTL. The rationale is that we know how to
7663 handle "aligning nodes" here: we can just bypass them because
7664 they won't change the final object whose address will be returned
7665 (they actually exist only for that purpose). */
7666 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7667 &mode1, &unsignedp, &volatilep, false);
7668 break;
7671 /* We must have made progress. */
7672 gcc_assert (inner != exp);
7674 subtarget = offset || bitpos ? NULL_RTX : target;
7675 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7676 inner alignment, force the inner to be sufficiently aligned. */
7677 if (CONSTANT_CLASS_P (inner)
7678 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7680 inner = copy_node (inner);
7681 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7682 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7683 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7685 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7687 if (offset)
7689 rtx tmp;
7691 if (modifier != EXPAND_NORMAL)
7692 result = force_operand (result, NULL);
7693 tmp = expand_expr (offset, NULL_RTX, tmode,
7694 modifier == EXPAND_INITIALIZER
7695 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7697 result = convert_memory_address_addr_space (tmode, result, as);
7698 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7700 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7701 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7702 else
7704 subtarget = bitpos ? NULL_RTX : target;
7705 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7706 1, OPTAB_LIB_WIDEN);
7710 if (bitpos)
7712 /* Someone beforehand should have rejected taking the address
7713 of such an object. */
7714 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7716 result = convert_memory_address_addr_space (tmode, result, as);
7717 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7718 if (modifier < EXPAND_SUM)
7719 result = force_operand (result, target);
7722 return result;
7725 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7726 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7728 static rtx
7729 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7730 enum expand_modifier modifier)
7732 addr_space_t as = ADDR_SPACE_GENERIC;
7733 enum machine_mode address_mode = Pmode;
7734 enum machine_mode pointer_mode = ptr_mode;
7735 enum machine_mode rmode;
7736 rtx result;
7738 /* Target mode of VOIDmode says "whatever's natural". */
7739 if (tmode == VOIDmode)
7740 tmode = TYPE_MODE (TREE_TYPE (exp));
7742 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7744 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7745 address_mode = targetm.addr_space.address_mode (as);
7746 pointer_mode = targetm.addr_space.pointer_mode (as);
7749 /* We can get called with some Weird Things if the user does silliness
7750 like "(short) &a". In that case, convert_memory_address won't do
7751 the right thing, so ignore the given target mode. */
7752 if (tmode != address_mode && tmode != pointer_mode)
7753 tmode = address_mode;
7755 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7756 tmode, modifier, as);
7758 /* Despite expand_expr claims concerning ignoring TMODE when not
7759 strictly convenient, stuff breaks if we don't honor it. Note
7760 that combined with the above, we only do this for pointer modes. */
7761 rmode = GET_MODE (result);
7762 if (rmode == VOIDmode)
7763 rmode = tmode;
7764 if (rmode != tmode)
7765 result = convert_memory_address_addr_space (tmode, result, as);
7767 return result;
7770 /* Generate code for computing CONSTRUCTOR EXP.
7771 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7772 is TRUE, instead of creating a temporary variable in memory
7773 NULL is returned and the caller needs to handle it differently. */
7775 static rtx
7776 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7777 bool avoid_temp_mem)
7779 tree type = TREE_TYPE (exp);
7780 enum machine_mode mode = TYPE_MODE (type);
7782 /* Try to avoid creating a temporary at all. This is possible
7783 if all of the initializer is zero.
7784 FIXME: try to handle all [0..255] initializers we can handle
7785 with memset. */
7786 if (TREE_STATIC (exp)
7787 && !TREE_ADDRESSABLE (exp)
7788 && target != 0 && mode == BLKmode
7789 && all_zeros_p (exp))
7791 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7792 return target;
7795 /* All elts simple constants => refer to a constant in memory. But
7796 if this is a non-BLKmode mode, let it store a field at a time
7797 since that should make a CONST_INT or CONST_DOUBLE when we
7798 fold. Likewise, if we have a target we can use, it is best to
7799 store directly into the target unless the type is large enough
7800 that memcpy will be used. If we are making an initializer and
7801 all operands are constant, put it in memory as well.
7803 FIXME: Avoid trying to fill vector constructors piece-meal.
7804 Output them with output_constant_def below unless we're sure
7805 they're zeros. This should go away when vector initializers
7806 are treated like VECTOR_CST instead of arrays. */
7807 if ((TREE_STATIC (exp)
7808 && ((mode == BLKmode
7809 && ! (target != 0 && safe_from_p (target, exp, 1)))
7810 || TREE_ADDRESSABLE (exp)
7811 || (host_integerp (TYPE_SIZE_UNIT (type), 1)
7812 && (! MOVE_BY_PIECES_P
7813 (tree_low_cst (TYPE_SIZE_UNIT (type), 1),
7814 TYPE_ALIGN (type)))
7815 && ! mostly_zeros_p (exp))))
7816 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7817 && TREE_CONSTANT (exp)))
7819 rtx constructor;
7821 if (avoid_temp_mem)
7822 return NULL_RTX;
7824 constructor = expand_expr_constant (exp, 1, modifier);
7826 if (modifier != EXPAND_CONST_ADDRESS
7827 && modifier != EXPAND_INITIALIZER
7828 && modifier != EXPAND_SUM)
7829 constructor = validize_mem (constructor);
7831 return constructor;
7834 /* Handle calls that pass values in multiple non-contiguous
7835 locations. The Irix 6 ABI has examples of this. */
7836 if (target == 0 || ! safe_from_p (target, exp, 1)
7837 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7839 if (avoid_temp_mem)
7840 return NULL_RTX;
7842 target
7843 = assign_temp (build_qualified_type (type, (TYPE_QUALS (type)
7844 | (TREE_READONLY (exp)
7845 * TYPE_QUAL_CONST))),
7846 TREE_ADDRESSABLE (exp), 1);
7849 store_constructor (exp, target, 0, int_expr_size (exp));
7850 return target;
7854 /* expand_expr: generate code for computing expression EXP.
7855 An rtx for the computed value is returned. The value is never null.
7856 In the case of a void EXP, const0_rtx is returned.
7858 The value may be stored in TARGET if TARGET is nonzero.
7859 TARGET is just a suggestion; callers must assume that
7860 the rtx returned may not be the same as TARGET.
7862 If TARGET is CONST0_RTX, it means that the value will be ignored.
7864 If TMODE is not VOIDmode, it suggests generating the
7865 result in mode TMODE. But this is done only when convenient.
7866 Otherwise, TMODE is ignored and the value generated in its natural mode.
7867 TMODE is just a suggestion; callers must assume that
7868 the rtx returned may not have mode TMODE.
7870 Note that TARGET may have neither TMODE nor MODE. In that case, it
7871 probably will not be used.
7873 If MODIFIER is EXPAND_SUM then when EXP is an addition
7874 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7875 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7876 products as above, or REG or MEM, or constant.
7877 Ordinarily in such cases we would output mul or add instructions
7878 and then return a pseudo reg containing the sum.
7880 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7881 it also marks a label as absolutely required (it can't be dead).
7882 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7883 This is used for outputting expressions used in initializers.
7885 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7886 with a constant address even if that address is not normally legitimate.
7887 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7889 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7890 a call parameter. Such targets require special care as we haven't yet
7891 marked TARGET so that it's safe from being trashed by libcalls. We
7892 don't want to use TARGET for anything but the final result;
7893 Intermediate values must go elsewhere. Additionally, calls to
7894 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7896 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7897 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7898 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7899 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7900 recursively. */
7903 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7904 enum expand_modifier modifier, rtx *alt_rtl)
7906 rtx ret;
7908 /* Handle ERROR_MARK before anybody tries to access its type. */
7909 if (TREE_CODE (exp) == ERROR_MARK
7910 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7912 ret = CONST0_RTX (tmode);
7913 return ret ? ret : const0_rtx;
7916 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7917 return ret;
7920 /* Try to expand the conditional expression which is represented by
7921 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7922 return the rtl reg which repsents the result. Otherwise return
7923 NULL_RTL. */
7925 static rtx
7926 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7927 tree treeop1 ATTRIBUTE_UNUSED,
7928 tree treeop2 ATTRIBUTE_UNUSED)
7930 #ifdef HAVE_conditional_move
7931 rtx insn;
7932 rtx op00, op01, op1, op2;
7933 enum rtx_code comparison_code;
7934 enum machine_mode comparison_mode;
7935 gimple srcstmt;
7936 rtx temp;
7937 tree type = TREE_TYPE (treeop1);
7938 int unsignedp = TYPE_UNSIGNED (type);
7939 enum machine_mode mode = TYPE_MODE (type);
7940 enum machine_mode orig_mode = mode;
7942 /* If we cannot do a conditional move on the mode, try doing it
7943 with the promoted mode. */
7944 if (!can_conditionally_move_p (mode))
7946 mode = promote_mode (type, mode, &unsignedp);
7947 if (!can_conditionally_move_p (mode))
7948 return NULL_RTX;
7949 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7951 else
7952 temp = assign_temp (type, 0, 1);
7954 start_sequence ();
7955 expand_operands (treeop1, treeop2,
7956 temp, &op1, &op2, EXPAND_NORMAL);
7958 if (TREE_CODE (treeop0) == SSA_NAME
7959 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7961 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7962 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7963 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7964 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7965 comparison_mode = TYPE_MODE (type);
7966 unsignedp = TYPE_UNSIGNED (type);
7967 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7969 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
7971 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7972 enum tree_code cmpcode = TREE_CODE (treeop0);
7973 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7974 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7975 unsignedp = TYPE_UNSIGNED (type);
7976 comparison_mode = TYPE_MODE (type);
7977 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7979 else
7981 op00 = expand_normal (treeop0);
7982 op01 = const0_rtx;
7983 comparison_code = NE;
7984 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7987 if (GET_MODE (op1) != mode)
7988 op1 = gen_lowpart (mode, op1);
7990 if (GET_MODE (op2) != mode)
7991 op2 = gen_lowpart (mode, op2);
7993 /* Try to emit the conditional move. */
7994 insn = emit_conditional_move (temp, comparison_code,
7995 op00, op01, comparison_mode,
7996 op1, op2, mode,
7997 unsignedp);
7999 /* If we could do the conditional move, emit the sequence,
8000 and return. */
8001 if (insn)
8003 rtx seq = get_insns ();
8004 end_sequence ();
8005 emit_insn (seq);
8006 return convert_modes (orig_mode, mode, temp, 0);
8009 /* Otherwise discard the sequence and fall back to code with
8010 branches. */
8011 end_sequence ();
8012 #endif
8013 return NULL_RTX;
8017 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
8018 enum expand_modifier modifier)
8020 rtx op0, op1, op2, temp;
8021 tree type;
8022 int unsignedp;
8023 enum machine_mode mode;
8024 enum tree_code code = ops->code;
8025 optab this_optab;
8026 rtx subtarget, original_target;
8027 int ignore;
8028 bool reduce_bit_field;
8029 location_t loc = ops->location;
8030 tree treeop0, treeop1, treeop2;
8031 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8032 ? reduce_to_bit_field_precision ((expr), \
8033 target, \
8034 type) \
8035 : (expr))
8037 type = ops->type;
8038 mode = TYPE_MODE (type);
8039 unsignedp = TYPE_UNSIGNED (type);
8041 treeop0 = ops->op0;
8042 treeop1 = ops->op1;
8043 treeop2 = ops->op2;
8045 /* We should be called only on simple (binary or unary) expressions,
8046 exactly those that are valid in gimple expressions that aren't
8047 GIMPLE_SINGLE_RHS (or invalid). */
8048 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8049 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8050 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8052 ignore = (target == const0_rtx
8053 || ((CONVERT_EXPR_CODE_P (code)
8054 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8055 && TREE_CODE (type) == VOID_TYPE));
8057 /* We should be called only if we need the result. */
8058 gcc_assert (!ignore);
8060 /* An operation in what may be a bit-field type needs the
8061 result to be reduced to the precision of the bit-field type,
8062 which is narrower than that of the type's mode. */
8063 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8064 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8066 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8067 target = 0;
8069 /* Use subtarget as the target for operand 0 of a binary operation. */
8070 subtarget = get_subtarget (target);
8071 original_target = target;
8073 switch (code)
8075 case NON_LVALUE_EXPR:
8076 case PAREN_EXPR:
8077 CASE_CONVERT:
8078 if (treeop0 == error_mark_node)
8079 return const0_rtx;
8081 if (TREE_CODE (type) == UNION_TYPE)
8083 tree valtype = TREE_TYPE (treeop0);
8085 /* If both input and output are BLKmode, this conversion isn't doing
8086 anything except possibly changing memory attribute. */
8087 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8089 rtx result = expand_expr (treeop0, target, tmode,
8090 modifier);
8092 result = copy_rtx (result);
8093 set_mem_attributes (result, type, 0);
8094 return result;
8097 if (target == 0)
8099 if (TYPE_MODE (type) != BLKmode)
8100 target = gen_reg_rtx (TYPE_MODE (type));
8101 else
8102 target = assign_temp (type, 1, 1);
8105 if (MEM_P (target))
8106 /* Store data into beginning of memory target. */
8107 store_expr (treeop0,
8108 adjust_address (target, TYPE_MODE (valtype), 0),
8109 modifier == EXPAND_STACK_PARM,
8110 false);
8112 else
8114 gcc_assert (REG_P (target));
8116 /* Store this field into a union of the proper type. */
8117 store_field (target,
8118 MIN ((int_size_in_bytes (TREE_TYPE
8119 (treeop0))
8120 * BITS_PER_UNIT),
8121 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8122 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8125 /* Return the entire union. */
8126 return target;
8129 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8131 op0 = expand_expr (treeop0, target, VOIDmode,
8132 modifier);
8134 /* If the signedness of the conversion differs and OP0 is
8135 a promoted SUBREG, clear that indication since we now
8136 have to do the proper extension. */
8137 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8138 && GET_CODE (op0) == SUBREG)
8139 SUBREG_PROMOTED_VAR_P (op0) = 0;
8141 return REDUCE_BIT_FIELD (op0);
8144 op0 = expand_expr (treeop0, NULL_RTX, mode,
8145 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8146 if (GET_MODE (op0) == mode)
8149 /* If OP0 is a constant, just convert it into the proper mode. */
8150 else if (CONSTANT_P (op0))
8152 tree inner_type = TREE_TYPE (treeop0);
8153 enum machine_mode inner_mode = GET_MODE (op0);
8155 if (inner_mode == VOIDmode)
8156 inner_mode = TYPE_MODE (inner_type);
8158 if (modifier == EXPAND_INITIALIZER)
8159 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8160 subreg_lowpart_offset (mode,
8161 inner_mode));
8162 else
8163 op0= convert_modes (mode, inner_mode, op0,
8164 TYPE_UNSIGNED (inner_type));
8167 else if (modifier == EXPAND_INITIALIZER)
8168 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8170 else if (target == 0)
8171 op0 = convert_to_mode (mode, op0,
8172 TYPE_UNSIGNED (TREE_TYPE
8173 (treeop0)));
8174 else
8176 convert_move (target, op0,
8177 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8178 op0 = target;
8181 return REDUCE_BIT_FIELD (op0);
8183 case ADDR_SPACE_CONVERT_EXPR:
8185 tree treeop0_type = TREE_TYPE (treeop0);
8186 addr_space_t as_to;
8187 addr_space_t as_from;
8189 gcc_assert (POINTER_TYPE_P (type));
8190 gcc_assert (POINTER_TYPE_P (treeop0_type));
8192 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8193 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8195 /* Conversions between pointers to the same address space should
8196 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8197 gcc_assert (as_to != as_from);
8199 /* Ask target code to handle conversion between pointers
8200 to overlapping address spaces. */
8201 if (targetm.addr_space.subset_p (as_to, as_from)
8202 || targetm.addr_space.subset_p (as_from, as_to))
8204 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8205 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8206 gcc_assert (op0);
8207 return op0;
8210 /* For disjoint address spaces, converting anything but
8211 a null pointer invokes undefined behaviour. We simply
8212 always return a null pointer here. */
8213 return CONST0_RTX (mode);
8216 case POINTER_PLUS_EXPR:
8217 /* Even though the sizetype mode and the pointer's mode can be different
8218 expand is able to handle this correctly and get the correct result out
8219 of the PLUS_EXPR code. */
8220 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8221 if sizetype precision is smaller than pointer precision. */
8222 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8223 treeop1 = fold_convert_loc (loc, type,
8224 fold_convert_loc (loc, ssizetype,
8225 treeop1));
8226 /* If sizetype precision is larger than pointer precision, truncate the
8227 offset to have matching modes. */
8228 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8229 treeop1 = fold_convert_loc (loc, type, treeop1);
8231 case PLUS_EXPR:
8232 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8233 something else, make sure we add the register to the constant and
8234 then to the other thing. This case can occur during strength
8235 reduction and doing it this way will produce better code if the
8236 frame pointer or argument pointer is eliminated.
8238 fold-const.c will ensure that the constant is always in the inner
8239 PLUS_EXPR, so the only case we need to do anything about is if
8240 sp, ap, or fp is our second argument, in which case we must swap
8241 the innermost first argument and our second argument. */
8243 if (TREE_CODE (treeop0) == PLUS_EXPR
8244 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8245 && TREE_CODE (treeop1) == VAR_DECL
8246 && (DECL_RTL (treeop1) == frame_pointer_rtx
8247 || DECL_RTL (treeop1) == stack_pointer_rtx
8248 || DECL_RTL (treeop1) == arg_pointer_rtx))
8250 gcc_unreachable ();
8253 /* If the result is to be ptr_mode and we are adding an integer to
8254 something, we might be forming a constant. So try to use
8255 plus_constant. If it produces a sum and we can't accept it,
8256 use force_operand. This allows P = &ARR[const] to generate
8257 efficient code on machines where a SYMBOL_REF is not a valid
8258 address.
8260 If this is an EXPAND_SUM call, always return the sum. */
8261 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8262 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8264 if (modifier == EXPAND_STACK_PARM)
8265 target = 0;
8266 if (TREE_CODE (treeop0) == INTEGER_CST
8267 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8268 && TREE_CONSTANT (treeop1))
8270 rtx constant_part;
8272 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8273 EXPAND_SUM);
8274 /* Use immed_double_const to ensure that the constant is
8275 truncated according to the mode of OP1, then sign extended
8276 to a HOST_WIDE_INT. Using the constant directly can result
8277 in non-canonical RTL in a 64x32 cross compile. */
8278 constant_part
8279 = immed_double_const (TREE_INT_CST_LOW (treeop0),
8280 (HOST_WIDE_INT) 0,
8281 TYPE_MODE (TREE_TYPE (treeop1)));
8282 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8283 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8284 op1 = force_operand (op1, target);
8285 return REDUCE_BIT_FIELD (op1);
8288 else if (TREE_CODE (treeop1) == INTEGER_CST
8289 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8290 && TREE_CONSTANT (treeop0))
8292 rtx constant_part;
8294 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8295 (modifier == EXPAND_INITIALIZER
8296 ? EXPAND_INITIALIZER : EXPAND_SUM));
8297 if (! CONSTANT_P (op0))
8299 op1 = expand_expr (treeop1, NULL_RTX,
8300 VOIDmode, modifier);
8301 /* Return a PLUS if modifier says it's OK. */
8302 if (modifier == EXPAND_SUM
8303 || modifier == EXPAND_INITIALIZER)
8304 return simplify_gen_binary (PLUS, mode, op0, op1);
8305 goto binop2;
8307 /* Use immed_double_const to ensure that the constant is
8308 truncated according to the mode of OP1, then sign extended
8309 to a HOST_WIDE_INT. Using the constant directly can result
8310 in non-canonical RTL in a 64x32 cross compile. */
8311 constant_part
8312 = immed_double_const (TREE_INT_CST_LOW (treeop1),
8313 (HOST_WIDE_INT) 0,
8314 TYPE_MODE (TREE_TYPE (treeop0)));
8315 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8316 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8317 op0 = force_operand (op0, target);
8318 return REDUCE_BIT_FIELD (op0);
8322 /* Use TER to expand pointer addition of a negated value
8323 as pointer subtraction. */
8324 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8325 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8326 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8327 && TREE_CODE (treeop1) == SSA_NAME
8328 && TYPE_MODE (TREE_TYPE (treeop0))
8329 == TYPE_MODE (TREE_TYPE (treeop1)))
8331 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8332 if (def)
8334 treeop1 = gimple_assign_rhs1 (def);
8335 code = MINUS_EXPR;
8336 goto do_minus;
8340 /* No sense saving up arithmetic to be done
8341 if it's all in the wrong mode to form part of an address.
8342 And force_operand won't know whether to sign-extend or
8343 zero-extend. */
8344 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8345 || mode != ptr_mode)
8347 expand_operands (treeop0, treeop1,
8348 subtarget, &op0, &op1, EXPAND_NORMAL);
8349 if (op0 == const0_rtx)
8350 return op1;
8351 if (op1 == const0_rtx)
8352 return op0;
8353 goto binop2;
8356 expand_operands (treeop0, treeop1,
8357 subtarget, &op0, &op1, modifier);
8358 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8360 case MINUS_EXPR:
8361 do_minus:
8362 /* For initializers, we are allowed to return a MINUS of two
8363 symbolic constants. Here we handle all cases when both operands
8364 are constant. */
8365 /* Handle difference of two symbolic constants,
8366 for the sake of an initializer. */
8367 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8368 && really_constant_p (treeop0)
8369 && really_constant_p (treeop1))
8371 expand_operands (treeop0, treeop1,
8372 NULL_RTX, &op0, &op1, modifier);
8374 /* If the last operand is a CONST_INT, use plus_constant of
8375 the negated constant. Else make the MINUS. */
8376 if (CONST_INT_P (op1))
8377 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8378 -INTVAL (op1)));
8379 else
8380 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8383 /* No sense saving up arithmetic to be done
8384 if it's all in the wrong mode to form part of an address.
8385 And force_operand won't know whether to sign-extend or
8386 zero-extend. */
8387 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8388 || mode != ptr_mode)
8389 goto binop;
8391 expand_operands (treeop0, treeop1,
8392 subtarget, &op0, &op1, modifier);
8394 /* Convert A - const to A + (-const). */
8395 if (CONST_INT_P (op1))
8397 op1 = negate_rtx (mode, op1);
8398 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8401 goto binop2;
8403 case WIDEN_MULT_PLUS_EXPR:
8404 case WIDEN_MULT_MINUS_EXPR:
8405 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8406 op2 = expand_normal (treeop2);
8407 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8408 target, unsignedp);
8409 return target;
8411 case WIDEN_MULT_EXPR:
8412 /* If first operand is constant, swap them.
8413 Thus the following special case checks need only
8414 check the second operand. */
8415 if (TREE_CODE (treeop0) == INTEGER_CST)
8417 tree t1 = treeop0;
8418 treeop0 = treeop1;
8419 treeop1 = t1;
8422 /* First, check if we have a multiplication of one signed and one
8423 unsigned operand. */
8424 if (TREE_CODE (treeop1) != INTEGER_CST
8425 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8426 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8428 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8429 this_optab = usmul_widen_optab;
8430 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8431 != CODE_FOR_nothing)
8433 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8434 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8435 EXPAND_NORMAL);
8436 else
8437 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8438 EXPAND_NORMAL);
8439 /* op0 and op1 might still be constant, despite the above
8440 != INTEGER_CST check. Handle it. */
8441 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8443 op0 = convert_modes (innermode, mode, op0, true);
8444 op1 = convert_modes (innermode, mode, op1, false);
8445 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8446 target, unsignedp));
8448 goto binop3;
8451 /* Check for a multiplication with matching signedness. */
8452 else if ((TREE_CODE (treeop1) == INTEGER_CST
8453 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8454 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8455 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8457 tree op0type = TREE_TYPE (treeop0);
8458 enum machine_mode innermode = TYPE_MODE (op0type);
8459 bool zextend_p = TYPE_UNSIGNED (op0type);
8460 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8461 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8463 if (TREE_CODE (treeop0) != INTEGER_CST)
8465 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8466 != CODE_FOR_nothing)
8468 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8469 EXPAND_NORMAL);
8470 /* op0 and op1 might still be constant, despite the above
8471 != INTEGER_CST check. Handle it. */
8472 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8474 widen_mult_const:
8475 op0 = convert_modes (innermode, mode, op0, zextend_p);
8477 = convert_modes (innermode, mode, op1,
8478 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8479 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8480 target,
8481 unsignedp));
8483 temp = expand_widening_mult (mode, op0, op1, target,
8484 unsignedp, this_optab);
8485 return REDUCE_BIT_FIELD (temp);
8487 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8488 != CODE_FOR_nothing
8489 && innermode == word_mode)
8491 rtx htem, hipart;
8492 op0 = expand_normal (treeop0);
8493 if (TREE_CODE (treeop1) == INTEGER_CST)
8494 op1 = convert_modes (innermode, mode,
8495 expand_normal (treeop1),
8496 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8497 else
8498 op1 = expand_normal (treeop1);
8499 /* op0 and op1 might still be constant, despite the above
8500 != INTEGER_CST check. Handle it. */
8501 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8502 goto widen_mult_const;
8503 temp = expand_binop (mode, other_optab, op0, op1, target,
8504 unsignedp, OPTAB_LIB_WIDEN);
8505 hipart = gen_highpart (innermode, temp);
8506 htem = expand_mult_highpart_adjust (innermode, hipart,
8507 op0, op1, hipart,
8508 zextend_p);
8509 if (htem != hipart)
8510 emit_move_insn (hipart, htem);
8511 return REDUCE_BIT_FIELD (temp);
8515 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8516 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8517 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8518 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8520 case FMA_EXPR:
8522 optab opt = fma_optab;
8523 gimple def0, def2;
8525 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8526 call. */
8527 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8529 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8530 tree call_expr;
8532 gcc_assert (fn != NULL_TREE);
8533 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8534 return expand_builtin (call_expr, target, subtarget, mode, false);
8537 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8538 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8540 op0 = op2 = NULL;
8542 if (def0 && def2
8543 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8545 opt = fnms_optab;
8546 op0 = expand_normal (gimple_assign_rhs1 (def0));
8547 op2 = expand_normal (gimple_assign_rhs1 (def2));
8549 else if (def0
8550 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8552 opt = fnma_optab;
8553 op0 = expand_normal (gimple_assign_rhs1 (def0));
8555 else if (def2
8556 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8558 opt = fms_optab;
8559 op2 = expand_normal (gimple_assign_rhs1 (def2));
8562 if (op0 == NULL)
8563 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8564 if (op2 == NULL)
8565 op2 = expand_normal (treeop2);
8566 op1 = expand_normal (treeop1);
8568 return expand_ternary_op (TYPE_MODE (type), opt,
8569 op0, op1, op2, target, 0);
8572 case MULT_EXPR:
8573 /* If this is a fixed-point operation, then we cannot use the code
8574 below because "expand_mult" doesn't support sat/no-sat fixed-point
8575 multiplications. */
8576 if (ALL_FIXED_POINT_MODE_P (mode))
8577 goto binop;
8579 /* If first operand is constant, swap them.
8580 Thus the following special case checks need only
8581 check the second operand. */
8582 if (TREE_CODE (treeop0) == INTEGER_CST)
8584 tree t1 = treeop0;
8585 treeop0 = treeop1;
8586 treeop1 = t1;
8589 /* Attempt to return something suitable for generating an
8590 indexed address, for machines that support that. */
8592 if (modifier == EXPAND_SUM && mode == ptr_mode
8593 && host_integerp (treeop1, 0))
8595 tree exp1 = treeop1;
8597 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8598 EXPAND_SUM);
8600 if (!REG_P (op0))
8601 op0 = force_operand (op0, NULL_RTX);
8602 if (!REG_P (op0))
8603 op0 = copy_to_mode_reg (mode, op0);
8605 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8606 gen_int_mode (tree_low_cst (exp1, 0),
8607 TYPE_MODE (TREE_TYPE (exp1)))));
8610 if (modifier == EXPAND_STACK_PARM)
8611 target = 0;
8613 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8614 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8616 case TRUNC_DIV_EXPR:
8617 case FLOOR_DIV_EXPR:
8618 case CEIL_DIV_EXPR:
8619 case ROUND_DIV_EXPR:
8620 case EXACT_DIV_EXPR:
8621 /* If this is a fixed-point operation, then we cannot use the code
8622 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8623 divisions. */
8624 if (ALL_FIXED_POINT_MODE_P (mode))
8625 goto binop;
8627 if (modifier == EXPAND_STACK_PARM)
8628 target = 0;
8629 /* Possible optimization: compute the dividend with EXPAND_SUM
8630 then if the divisor is constant can optimize the case
8631 where some terms of the dividend have coeffs divisible by it. */
8632 expand_operands (treeop0, treeop1,
8633 subtarget, &op0, &op1, EXPAND_NORMAL);
8634 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8636 case RDIV_EXPR:
8637 goto binop;
8639 case MULT_HIGHPART_EXPR:
8640 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8641 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8642 gcc_assert (temp);
8643 return temp;
8645 case TRUNC_MOD_EXPR:
8646 case FLOOR_MOD_EXPR:
8647 case CEIL_MOD_EXPR:
8648 case ROUND_MOD_EXPR:
8649 if (modifier == EXPAND_STACK_PARM)
8650 target = 0;
8651 expand_operands (treeop0, treeop1,
8652 subtarget, &op0, &op1, EXPAND_NORMAL);
8653 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8655 case FIXED_CONVERT_EXPR:
8656 op0 = expand_normal (treeop0);
8657 if (target == 0 || modifier == EXPAND_STACK_PARM)
8658 target = gen_reg_rtx (mode);
8660 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8661 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8662 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8663 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8664 else
8665 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8666 return target;
8668 case FIX_TRUNC_EXPR:
8669 op0 = expand_normal (treeop0);
8670 if (target == 0 || modifier == EXPAND_STACK_PARM)
8671 target = gen_reg_rtx (mode);
8672 expand_fix (target, op0, unsignedp);
8673 return target;
8675 case FLOAT_EXPR:
8676 op0 = expand_normal (treeop0);
8677 if (target == 0 || modifier == EXPAND_STACK_PARM)
8678 target = gen_reg_rtx (mode);
8679 /* expand_float can't figure out what to do if FROM has VOIDmode.
8680 So give it the correct mode. With -O, cse will optimize this. */
8681 if (GET_MODE (op0) == VOIDmode)
8682 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8683 op0);
8684 expand_float (target, op0,
8685 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8686 return target;
8688 case NEGATE_EXPR:
8689 op0 = expand_expr (treeop0, subtarget,
8690 VOIDmode, EXPAND_NORMAL);
8691 if (modifier == EXPAND_STACK_PARM)
8692 target = 0;
8693 temp = expand_unop (mode,
8694 optab_for_tree_code (NEGATE_EXPR, type,
8695 optab_default),
8696 op0, target, 0);
8697 gcc_assert (temp);
8698 return REDUCE_BIT_FIELD (temp);
8700 case ABS_EXPR:
8701 op0 = expand_expr (treeop0, subtarget,
8702 VOIDmode, EXPAND_NORMAL);
8703 if (modifier == EXPAND_STACK_PARM)
8704 target = 0;
8706 /* ABS_EXPR is not valid for complex arguments. */
8707 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8708 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8710 /* Unsigned abs is simply the operand. Testing here means we don't
8711 risk generating incorrect code below. */
8712 if (TYPE_UNSIGNED (type))
8713 return op0;
8715 return expand_abs (mode, op0, target, unsignedp,
8716 safe_from_p (target, treeop0, 1));
8718 case MAX_EXPR:
8719 case MIN_EXPR:
8720 target = original_target;
8721 if (target == 0
8722 || modifier == EXPAND_STACK_PARM
8723 || (MEM_P (target) && MEM_VOLATILE_P (target))
8724 || GET_MODE (target) != mode
8725 || (REG_P (target)
8726 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8727 target = gen_reg_rtx (mode);
8728 expand_operands (treeop0, treeop1,
8729 target, &op0, &op1, EXPAND_NORMAL);
8731 /* First try to do it with a special MIN or MAX instruction.
8732 If that does not win, use a conditional jump to select the proper
8733 value. */
8734 this_optab = optab_for_tree_code (code, type, optab_default);
8735 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8736 OPTAB_WIDEN);
8737 if (temp != 0)
8738 return temp;
8740 /* At this point, a MEM target is no longer useful; we will get better
8741 code without it. */
8743 if (! REG_P (target))
8744 target = gen_reg_rtx (mode);
8746 /* If op1 was placed in target, swap op0 and op1. */
8747 if (target != op0 && target == op1)
8749 temp = op0;
8750 op0 = op1;
8751 op1 = temp;
8754 /* We generate better code and avoid problems with op1 mentioning
8755 target by forcing op1 into a pseudo if it isn't a constant. */
8756 if (! CONSTANT_P (op1))
8757 op1 = force_reg (mode, op1);
8760 enum rtx_code comparison_code;
8761 rtx cmpop1 = op1;
8763 if (code == MAX_EXPR)
8764 comparison_code = unsignedp ? GEU : GE;
8765 else
8766 comparison_code = unsignedp ? LEU : LE;
8768 /* Canonicalize to comparisons against 0. */
8769 if (op1 == const1_rtx)
8771 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8772 or (a != 0 ? a : 1) for unsigned.
8773 For MIN we are safe converting (a <= 1 ? a : 1)
8774 into (a <= 0 ? a : 1) */
8775 cmpop1 = const0_rtx;
8776 if (code == MAX_EXPR)
8777 comparison_code = unsignedp ? NE : GT;
8779 if (op1 == constm1_rtx && !unsignedp)
8781 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8782 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8783 cmpop1 = const0_rtx;
8784 if (code == MIN_EXPR)
8785 comparison_code = LT;
8787 #ifdef HAVE_conditional_move
8788 /* Use a conditional move if possible. */
8789 if (can_conditionally_move_p (mode))
8791 rtx insn;
8793 /* ??? Same problem as in expmed.c: emit_conditional_move
8794 forces a stack adjustment via compare_from_rtx, and we
8795 lose the stack adjustment if the sequence we are about
8796 to create is discarded. */
8797 do_pending_stack_adjust ();
8799 start_sequence ();
8801 /* Try to emit the conditional move. */
8802 insn = emit_conditional_move (target, comparison_code,
8803 op0, cmpop1, mode,
8804 op0, op1, mode,
8805 unsignedp);
8807 /* If we could do the conditional move, emit the sequence,
8808 and return. */
8809 if (insn)
8811 rtx seq = get_insns ();
8812 end_sequence ();
8813 emit_insn (seq);
8814 return target;
8817 /* Otherwise discard the sequence and fall back to code with
8818 branches. */
8819 end_sequence ();
8821 #endif
8822 if (target != op0)
8823 emit_move_insn (target, op0);
8825 temp = gen_label_rtx ();
8826 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8827 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8828 -1);
8830 emit_move_insn (target, op1);
8831 emit_label (temp);
8832 return target;
8834 case BIT_NOT_EXPR:
8835 op0 = expand_expr (treeop0, subtarget,
8836 VOIDmode, EXPAND_NORMAL);
8837 if (modifier == EXPAND_STACK_PARM)
8838 target = 0;
8839 /* In case we have to reduce the result to bitfield precision
8840 for unsigned bitfield expand this as XOR with a proper constant
8841 instead. */
8842 if (reduce_bit_field && TYPE_UNSIGNED (type))
8843 temp = expand_binop (mode, xor_optab, op0,
8844 immed_double_int_const
8845 (double_int::mask (TYPE_PRECISION (type)), mode),
8846 target, 1, OPTAB_LIB_WIDEN);
8847 else
8848 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8849 gcc_assert (temp);
8850 return temp;
8852 /* ??? Can optimize bitwise operations with one arg constant.
8853 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8854 and (a bitwise1 b) bitwise2 b (etc)
8855 but that is probably not worth while. */
8857 case BIT_AND_EXPR:
8858 case BIT_IOR_EXPR:
8859 case BIT_XOR_EXPR:
8860 goto binop;
8862 case LROTATE_EXPR:
8863 case RROTATE_EXPR:
8864 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8865 || (GET_MODE_PRECISION (TYPE_MODE (type))
8866 == TYPE_PRECISION (type)));
8867 /* fall through */
8869 case LSHIFT_EXPR:
8870 case RSHIFT_EXPR:
8871 /* If this is a fixed-point operation, then we cannot use the code
8872 below because "expand_shift" doesn't support sat/no-sat fixed-point
8873 shifts. */
8874 if (ALL_FIXED_POINT_MODE_P (mode))
8875 goto binop;
8877 if (! safe_from_p (subtarget, treeop1, 1))
8878 subtarget = 0;
8879 if (modifier == EXPAND_STACK_PARM)
8880 target = 0;
8881 op0 = expand_expr (treeop0, subtarget,
8882 VOIDmode, EXPAND_NORMAL);
8883 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8884 unsignedp);
8885 if (code == LSHIFT_EXPR)
8886 temp = REDUCE_BIT_FIELD (temp);
8887 return temp;
8889 /* Could determine the answer when only additive constants differ. Also,
8890 the addition of one can be handled by changing the condition. */
8891 case LT_EXPR:
8892 case LE_EXPR:
8893 case GT_EXPR:
8894 case GE_EXPR:
8895 case EQ_EXPR:
8896 case NE_EXPR:
8897 case UNORDERED_EXPR:
8898 case ORDERED_EXPR:
8899 case UNLT_EXPR:
8900 case UNLE_EXPR:
8901 case UNGT_EXPR:
8902 case UNGE_EXPR:
8903 case UNEQ_EXPR:
8904 case LTGT_EXPR:
8905 temp = do_store_flag (ops,
8906 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8907 tmode != VOIDmode ? tmode : mode);
8908 if (temp)
8909 return temp;
8911 /* Use a compare and a jump for BLKmode comparisons, or for function
8912 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8914 if ((target == 0
8915 || modifier == EXPAND_STACK_PARM
8916 || ! safe_from_p (target, treeop0, 1)
8917 || ! safe_from_p (target, treeop1, 1)
8918 /* Make sure we don't have a hard reg (such as function's return
8919 value) live across basic blocks, if not optimizing. */
8920 || (!optimize && REG_P (target)
8921 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8922 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8924 emit_move_insn (target, const0_rtx);
8926 op1 = gen_label_rtx ();
8927 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8929 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8930 emit_move_insn (target, constm1_rtx);
8931 else
8932 emit_move_insn (target, const1_rtx);
8934 emit_label (op1);
8935 return target;
8937 case COMPLEX_EXPR:
8938 /* Get the rtx code of the operands. */
8939 op0 = expand_normal (treeop0);
8940 op1 = expand_normal (treeop1);
8942 if (!target)
8943 target = gen_reg_rtx (TYPE_MODE (type));
8944 else
8945 /* If target overlaps with op1, then either we need to force
8946 op1 into a pseudo (if target also overlaps with op0),
8947 or write the complex parts in reverse order. */
8948 switch (GET_CODE (target))
8950 case CONCAT:
8951 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
8953 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
8955 complex_expr_force_op1:
8956 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
8957 emit_move_insn (temp, op1);
8958 op1 = temp;
8959 break;
8961 complex_expr_swap_order:
8962 /* Move the imaginary (op1) and real (op0) parts to their
8963 location. */
8964 write_complex_part (target, op1, true);
8965 write_complex_part (target, op0, false);
8967 return target;
8969 break;
8970 case MEM:
8971 temp = adjust_address_nv (target,
8972 GET_MODE_INNER (GET_MODE (target)), 0);
8973 if (reg_overlap_mentioned_p (temp, op1))
8975 enum machine_mode imode = GET_MODE_INNER (GET_MODE (target));
8976 temp = adjust_address_nv (target, imode,
8977 GET_MODE_SIZE (imode));
8978 if (reg_overlap_mentioned_p (temp, op0))
8979 goto complex_expr_force_op1;
8980 goto complex_expr_swap_order;
8982 break;
8983 default:
8984 if (reg_overlap_mentioned_p (target, op1))
8986 if (reg_overlap_mentioned_p (target, op0))
8987 goto complex_expr_force_op1;
8988 goto complex_expr_swap_order;
8990 break;
8993 /* Move the real (op0) and imaginary (op1) parts to their location. */
8994 write_complex_part (target, op0, false);
8995 write_complex_part (target, op1, true);
8997 return target;
8999 case WIDEN_SUM_EXPR:
9001 tree oprnd0 = treeop0;
9002 tree oprnd1 = treeop1;
9004 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9005 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9006 target, unsignedp);
9007 return target;
9010 case REDUC_MAX_EXPR:
9011 case REDUC_MIN_EXPR:
9012 case REDUC_PLUS_EXPR:
9014 op0 = expand_normal (treeop0);
9015 this_optab = optab_for_tree_code (code, type, optab_default);
9016 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
9017 gcc_assert (temp);
9018 return temp;
9021 case VEC_LSHIFT_EXPR:
9022 case VEC_RSHIFT_EXPR:
9024 target = expand_vec_shift_expr (ops, target);
9025 return target;
9028 case VEC_UNPACK_HI_EXPR:
9029 case VEC_UNPACK_LO_EXPR:
9031 op0 = expand_normal (treeop0);
9032 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9033 target, unsignedp);
9034 gcc_assert (temp);
9035 return temp;
9038 case VEC_UNPACK_FLOAT_HI_EXPR:
9039 case VEC_UNPACK_FLOAT_LO_EXPR:
9041 op0 = expand_normal (treeop0);
9042 /* The signedness is determined from input operand. */
9043 temp = expand_widen_pattern_expr
9044 (ops, op0, NULL_RTX, NULL_RTX,
9045 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9047 gcc_assert (temp);
9048 return temp;
9051 case VEC_WIDEN_MULT_HI_EXPR:
9052 case VEC_WIDEN_MULT_LO_EXPR:
9053 case VEC_WIDEN_MULT_EVEN_EXPR:
9054 case VEC_WIDEN_MULT_ODD_EXPR:
9055 case VEC_WIDEN_LSHIFT_HI_EXPR:
9056 case VEC_WIDEN_LSHIFT_LO_EXPR:
9057 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9058 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9059 target, unsignedp);
9060 gcc_assert (target);
9061 return target;
9063 case VEC_PACK_TRUNC_EXPR:
9064 case VEC_PACK_SAT_EXPR:
9065 case VEC_PACK_FIX_TRUNC_EXPR:
9066 mode = TYPE_MODE (TREE_TYPE (treeop0));
9067 goto binop;
9069 case VEC_PERM_EXPR:
9070 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9071 op2 = expand_normal (treeop2);
9073 /* Careful here: if the target doesn't support integral vector modes,
9074 a constant selection vector could wind up smooshed into a normal
9075 integral constant. */
9076 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9078 tree sel_type = TREE_TYPE (treeop2);
9079 enum machine_mode vmode
9080 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9081 TYPE_VECTOR_SUBPARTS (sel_type));
9082 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9083 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9084 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9086 else
9087 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9089 temp = expand_vec_perm (mode, op0, op1, op2, target);
9090 gcc_assert (temp);
9091 return temp;
9093 case DOT_PROD_EXPR:
9095 tree oprnd0 = treeop0;
9096 tree oprnd1 = treeop1;
9097 tree oprnd2 = treeop2;
9098 rtx op2;
9100 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9101 op2 = expand_normal (oprnd2);
9102 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9103 target, unsignedp);
9104 return target;
9107 case REALIGN_LOAD_EXPR:
9109 tree oprnd0 = treeop0;
9110 tree oprnd1 = treeop1;
9111 tree oprnd2 = treeop2;
9112 rtx op2;
9114 this_optab = optab_for_tree_code (code, type, optab_default);
9115 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9116 op2 = expand_normal (oprnd2);
9117 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9118 target, unsignedp);
9119 gcc_assert (temp);
9120 return temp;
9123 case COND_EXPR:
9124 /* A COND_EXPR with its type being VOID_TYPE represents a
9125 conditional jump and is handled in
9126 expand_gimple_cond_expr. */
9127 gcc_assert (!VOID_TYPE_P (type));
9129 /* Note that COND_EXPRs whose type is a structure or union
9130 are required to be constructed to contain assignments of
9131 a temporary variable, so that we can evaluate them here
9132 for side effect only. If type is void, we must do likewise. */
9134 gcc_assert (!TREE_ADDRESSABLE (type)
9135 && !ignore
9136 && TREE_TYPE (treeop1) != void_type_node
9137 && TREE_TYPE (treeop2) != void_type_node);
9139 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9140 if (temp)
9141 return temp;
9143 /* If we are not to produce a result, we have no target. Otherwise,
9144 if a target was specified use it; it will not be used as an
9145 intermediate target unless it is safe. If no target, use a
9146 temporary. */
9148 if (modifier != EXPAND_STACK_PARM
9149 && original_target
9150 && safe_from_p (original_target, treeop0, 1)
9151 && GET_MODE (original_target) == mode
9152 && !MEM_P (original_target))
9153 temp = original_target;
9154 else
9155 temp = assign_temp (type, 0, 1);
9157 do_pending_stack_adjust ();
9158 NO_DEFER_POP;
9159 op0 = gen_label_rtx ();
9160 op1 = gen_label_rtx ();
9161 jumpifnot (treeop0, op0, -1);
9162 store_expr (treeop1, temp,
9163 modifier == EXPAND_STACK_PARM,
9164 false);
9166 emit_jump_insn (gen_jump (op1));
9167 emit_barrier ();
9168 emit_label (op0);
9169 store_expr (treeop2, temp,
9170 modifier == EXPAND_STACK_PARM,
9171 false);
9173 emit_label (op1);
9174 OK_DEFER_POP;
9175 return temp;
9177 case VEC_COND_EXPR:
9178 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9179 return target;
9181 default:
9182 gcc_unreachable ();
9185 /* Here to do an ordinary binary operator. */
9186 binop:
9187 expand_operands (treeop0, treeop1,
9188 subtarget, &op0, &op1, EXPAND_NORMAL);
9189 binop2:
9190 this_optab = optab_for_tree_code (code, type, optab_default);
9191 binop3:
9192 if (modifier == EXPAND_STACK_PARM)
9193 target = 0;
9194 temp = expand_binop (mode, this_optab, op0, op1, target,
9195 unsignedp, OPTAB_LIB_WIDEN);
9196 gcc_assert (temp);
9197 /* Bitwise operations do not need bitfield reduction as we expect their
9198 operands being properly truncated. */
9199 if (code == BIT_XOR_EXPR
9200 || code == BIT_AND_EXPR
9201 || code == BIT_IOR_EXPR)
9202 return temp;
9203 return REDUCE_BIT_FIELD (temp);
9205 #undef REDUCE_BIT_FIELD
9208 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9209 enum expand_modifier modifier, rtx *alt_rtl)
9211 rtx op0, op1, temp, decl_rtl;
9212 tree type;
9213 int unsignedp;
9214 enum machine_mode mode;
9215 enum tree_code code = TREE_CODE (exp);
9216 rtx subtarget, original_target;
9217 int ignore;
9218 tree context;
9219 bool reduce_bit_field;
9220 location_t loc = EXPR_LOCATION (exp);
9221 struct separate_ops ops;
9222 tree treeop0, treeop1, treeop2;
9223 tree ssa_name = NULL_TREE;
9224 gimple g;
9226 type = TREE_TYPE (exp);
9227 mode = TYPE_MODE (type);
9228 unsignedp = TYPE_UNSIGNED (type);
9230 treeop0 = treeop1 = treeop2 = NULL_TREE;
9231 if (!VL_EXP_CLASS_P (exp))
9232 switch (TREE_CODE_LENGTH (code))
9234 default:
9235 case 3: treeop2 = TREE_OPERAND (exp, 2);
9236 case 2: treeop1 = TREE_OPERAND (exp, 1);
9237 case 1: treeop0 = TREE_OPERAND (exp, 0);
9238 case 0: break;
9240 ops.code = code;
9241 ops.type = type;
9242 ops.op0 = treeop0;
9243 ops.op1 = treeop1;
9244 ops.op2 = treeop2;
9245 ops.location = loc;
9247 ignore = (target == const0_rtx
9248 || ((CONVERT_EXPR_CODE_P (code)
9249 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9250 && TREE_CODE (type) == VOID_TYPE));
9252 /* An operation in what may be a bit-field type needs the
9253 result to be reduced to the precision of the bit-field type,
9254 which is narrower than that of the type's mode. */
9255 reduce_bit_field = (!ignore
9256 && INTEGRAL_TYPE_P (type)
9257 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9259 /* If we are going to ignore this result, we need only do something
9260 if there is a side-effect somewhere in the expression. If there
9261 is, short-circuit the most common cases here. Note that we must
9262 not call expand_expr with anything but const0_rtx in case this
9263 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9265 if (ignore)
9267 if (! TREE_SIDE_EFFECTS (exp))
9268 return const0_rtx;
9270 /* Ensure we reference a volatile object even if value is ignored, but
9271 don't do this if all we are doing is taking its address. */
9272 if (TREE_THIS_VOLATILE (exp)
9273 && TREE_CODE (exp) != FUNCTION_DECL
9274 && mode != VOIDmode && mode != BLKmode
9275 && modifier != EXPAND_CONST_ADDRESS)
9277 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9278 if (MEM_P (temp))
9279 copy_to_reg (temp);
9280 return const0_rtx;
9283 if (TREE_CODE_CLASS (code) == tcc_unary
9284 || code == BIT_FIELD_REF
9285 || code == COMPONENT_REF
9286 || code == INDIRECT_REF)
9287 return expand_expr (treeop0, const0_rtx, VOIDmode,
9288 modifier);
9290 else if (TREE_CODE_CLASS (code) == tcc_binary
9291 || TREE_CODE_CLASS (code) == tcc_comparison
9292 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9294 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9295 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9296 return const0_rtx;
9299 target = 0;
9302 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9303 target = 0;
9305 /* Use subtarget as the target for operand 0 of a binary operation. */
9306 subtarget = get_subtarget (target);
9307 original_target = target;
9309 switch (code)
9311 case LABEL_DECL:
9313 tree function = decl_function_context (exp);
9315 temp = label_rtx (exp);
9316 temp = gen_rtx_LABEL_REF (Pmode, temp);
9318 if (function != current_function_decl
9319 && function != 0)
9320 LABEL_REF_NONLOCAL_P (temp) = 1;
9322 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9323 return temp;
9326 case SSA_NAME:
9327 /* ??? ivopts calls expander, without any preparation from
9328 out-of-ssa. So fake instructions as if this was an access to the
9329 base variable. This unnecessarily allocates a pseudo, see how we can
9330 reuse it, if partition base vars have it set already. */
9331 if (!currently_expanding_to_rtl)
9333 tree var = SSA_NAME_VAR (exp);
9334 if (var && DECL_RTL_SET_P (var))
9335 return DECL_RTL (var);
9336 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9337 LAST_VIRTUAL_REGISTER + 1);
9340 g = get_gimple_for_ssa_name (exp);
9341 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9342 if (g == NULL
9343 && modifier == EXPAND_INITIALIZER
9344 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9345 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9346 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9347 g = SSA_NAME_DEF_STMT (exp);
9348 if (g)
9350 rtx r;
9351 location_t saved_loc = curr_insn_location ();
9353 set_curr_insn_location (gimple_location (g));
9354 r = expand_expr_real (gimple_assign_rhs_to_tree (g), target,
9355 tmode, modifier, NULL);
9356 set_curr_insn_location (saved_loc);
9357 if (REG_P (r) && !REG_EXPR (r))
9358 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9359 return r;
9362 ssa_name = exp;
9363 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9364 exp = SSA_NAME_VAR (ssa_name);
9365 goto expand_decl_rtl;
9367 case PARM_DECL:
9368 case VAR_DECL:
9369 /* If a static var's type was incomplete when the decl was written,
9370 but the type is complete now, lay out the decl now. */
9371 if (DECL_SIZE (exp) == 0
9372 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9373 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9374 layout_decl (exp, 0);
9376 /* ... fall through ... */
9378 case FUNCTION_DECL:
9379 case RESULT_DECL:
9380 decl_rtl = DECL_RTL (exp);
9381 expand_decl_rtl:
9382 gcc_assert (decl_rtl);
9383 decl_rtl = copy_rtx (decl_rtl);
9384 /* Record writes to register variables. */
9385 if (modifier == EXPAND_WRITE
9386 && REG_P (decl_rtl)
9387 && HARD_REGISTER_P (decl_rtl))
9388 add_to_hard_reg_set (&crtl->asm_clobbers,
9389 GET_MODE (decl_rtl), REGNO (decl_rtl));
9391 /* Ensure variable marked as used even if it doesn't go through
9392 a parser. If it hasn't be used yet, write out an external
9393 definition. */
9394 TREE_USED (exp) = 1;
9396 /* Show we haven't gotten RTL for this yet. */
9397 temp = 0;
9399 /* Variables inherited from containing functions should have
9400 been lowered by this point. */
9401 context = decl_function_context (exp);
9402 gcc_assert (!context
9403 || context == current_function_decl
9404 || TREE_STATIC (exp)
9405 || DECL_EXTERNAL (exp)
9406 /* ??? C++ creates functions that are not TREE_STATIC. */
9407 || TREE_CODE (exp) == FUNCTION_DECL);
9409 /* This is the case of an array whose size is to be determined
9410 from its initializer, while the initializer is still being parsed.
9411 ??? We aren't parsing while expanding anymore. */
9413 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9414 temp = validize_mem (decl_rtl);
9416 /* If DECL_RTL is memory, we are in the normal case and the
9417 address is not valid, get the address into a register. */
9419 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9421 if (alt_rtl)
9422 *alt_rtl = decl_rtl;
9423 decl_rtl = use_anchored_address (decl_rtl);
9424 if (modifier != EXPAND_CONST_ADDRESS
9425 && modifier != EXPAND_SUM
9426 && !memory_address_addr_space_p (DECL_MODE (exp),
9427 XEXP (decl_rtl, 0),
9428 MEM_ADDR_SPACE (decl_rtl)))
9429 temp = replace_equiv_address (decl_rtl,
9430 copy_rtx (XEXP (decl_rtl, 0)));
9433 /* If we got something, return it. But first, set the alignment
9434 if the address is a register. */
9435 if (temp != 0)
9437 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9438 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9440 return temp;
9443 /* If the mode of DECL_RTL does not match that of the decl,
9444 there are two cases: we are dealing with a BLKmode value
9445 that is returned in a register, or we are dealing with
9446 a promoted value. In the latter case, return a SUBREG
9447 of the wanted mode, but mark it so that we know that it
9448 was already extended. */
9449 if (REG_P (decl_rtl)
9450 && DECL_MODE (exp) != BLKmode
9451 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9453 enum machine_mode pmode;
9455 /* Get the signedness to be used for this variable. Ensure we get
9456 the same mode we got when the variable was declared. */
9457 if (code == SSA_NAME
9458 && (g = SSA_NAME_DEF_STMT (ssa_name))
9459 && gimple_code (g) == GIMPLE_CALL)
9461 gcc_assert (!gimple_call_internal_p (g));
9462 pmode = promote_function_mode (type, mode, &unsignedp,
9463 gimple_call_fntype (g),
9466 else
9467 pmode = promote_decl_mode (exp, &unsignedp);
9468 gcc_assert (GET_MODE (decl_rtl) == pmode);
9470 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9471 SUBREG_PROMOTED_VAR_P (temp) = 1;
9472 SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
9473 return temp;
9476 return decl_rtl;
9478 case INTEGER_CST:
9479 temp = immed_double_const (TREE_INT_CST_LOW (exp),
9480 TREE_INT_CST_HIGH (exp), mode);
9482 return temp;
9484 case VECTOR_CST:
9486 tree tmp = NULL_TREE;
9487 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9488 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9489 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9490 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9491 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9492 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9493 return const_vector_from_tree (exp);
9494 if (GET_MODE_CLASS (mode) == MODE_INT)
9496 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9497 if (type_for_mode)
9498 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9500 if (!tmp)
9502 vec<constructor_elt, va_gc> *v;
9503 unsigned i;
9504 vec_alloc (v, VECTOR_CST_NELTS (exp));
9505 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9506 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9507 tmp = build_constructor (type, v);
9509 return expand_expr (tmp, ignore ? const0_rtx : target,
9510 tmode, modifier);
9513 case CONST_DECL:
9514 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9516 case REAL_CST:
9517 /* If optimized, generate immediate CONST_DOUBLE
9518 which will be turned into memory by reload if necessary.
9520 We used to force a register so that loop.c could see it. But
9521 this does not allow gen_* patterns to perform optimizations with
9522 the constants. It also produces two insns in cases like "x = 1.0;".
9523 On most machines, floating-point constants are not permitted in
9524 many insns, so we'd end up copying it to a register in any case.
9526 Now, we do the copying in expand_binop, if appropriate. */
9527 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9528 TYPE_MODE (TREE_TYPE (exp)));
9530 case FIXED_CST:
9531 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9532 TYPE_MODE (TREE_TYPE (exp)));
9534 case COMPLEX_CST:
9535 /* Handle evaluating a complex constant in a CONCAT target. */
9536 if (original_target && GET_CODE (original_target) == CONCAT)
9538 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9539 rtx rtarg, itarg;
9541 rtarg = XEXP (original_target, 0);
9542 itarg = XEXP (original_target, 1);
9544 /* Move the real and imaginary parts separately. */
9545 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9546 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9548 if (op0 != rtarg)
9549 emit_move_insn (rtarg, op0);
9550 if (op1 != itarg)
9551 emit_move_insn (itarg, op1);
9553 return original_target;
9556 /* ... fall through ... */
9558 case STRING_CST:
9559 temp = expand_expr_constant (exp, 1, modifier);
9561 /* temp contains a constant address.
9562 On RISC machines where a constant address isn't valid,
9563 make some insns to get that address into a register. */
9564 if (modifier != EXPAND_CONST_ADDRESS
9565 && modifier != EXPAND_INITIALIZER
9566 && modifier != EXPAND_SUM
9567 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9568 MEM_ADDR_SPACE (temp)))
9569 return replace_equiv_address (temp,
9570 copy_rtx (XEXP (temp, 0)));
9571 return temp;
9573 case SAVE_EXPR:
9575 tree val = treeop0;
9576 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl);
9578 if (!SAVE_EXPR_RESOLVED_P (exp))
9580 /* We can indeed still hit this case, typically via builtin
9581 expanders calling save_expr immediately before expanding
9582 something. Assume this means that we only have to deal
9583 with non-BLKmode values. */
9584 gcc_assert (GET_MODE (ret) != BLKmode);
9586 val = build_decl (curr_insn_location (),
9587 VAR_DECL, NULL, TREE_TYPE (exp));
9588 DECL_ARTIFICIAL (val) = 1;
9589 DECL_IGNORED_P (val) = 1;
9590 treeop0 = val;
9591 TREE_OPERAND (exp, 0) = treeop0;
9592 SAVE_EXPR_RESOLVED_P (exp) = 1;
9594 if (!CONSTANT_P (ret))
9595 ret = copy_to_reg (ret);
9596 SET_DECL_RTL (val, ret);
9599 return ret;
9603 case CONSTRUCTOR:
9604 /* If we don't need the result, just ensure we evaluate any
9605 subexpressions. */
9606 if (ignore)
9608 unsigned HOST_WIDE_INT idx;
9609 tree value;
9611 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9612 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9614 return const0_rtx;
9617 return expand_constructor (exp, target, modifier, false);
9619 case TARGET_MEM_REF:
9621 addr_space_t as
9622 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9623 struct mem_address addr;
9624 enum insn_code icode;
9625 unsigned int align;
9626 int old_cse_not_expected;
9628 get_address_description (exp, &addr);
9629 op0 = addr_for_mem_ref (&addr, as, true);
9630 /* For TARGET_MEM_REF node, it expects to keep the memory access mode
9631 instead of linearizing the addr expr during expand, so
9632 cse_not_expected will be set to 1 temporarily before expanding addr
9633 expr, and then set back after it. */
9634 old_cse_not_expected = cse_not_expected;
9635 cse_not_expected = 1;
9636 op0 = memory_address_addr_space (mode, op0, as);
9637 cse_not_expected = old_cse_not_expected;
9638 temp = gen_rtx_MEM (mode, op0);
9639 set_mem_attributes (temp, exp, 0);
9640 set_mem_addr_space (temp, as);
9641 align = get_object_alignment (exp);
9642 if (modifier != EXPAND_WRITE
9643 && modifier != EXPAND_MEMORY
9644 && mode != BLKmode
9645 && align < GET_MODE_ALIGNMENT (mode)
9646 /* If the target does not have special handling for unaligned
9647 loads of mode then it can use regular moves for them. */
9648 && ((icode = optab_handler (movmisalign_optab, mode))
9649 != CODE_FOR_nothing))
9651 struct expand_operand ops[2];
9653 /* We've already validated the memory, and we're creating a
9654 new pseudo destination. The predicates really can't fail,
9655 nor can the generator. */
9656 create_output_operand (&ops[0], NULL_RTX, mode);
9657 create_fixed_operand (&ops[1], temp);
9658 expand_insn (icode, 2, ops);
9659 return ops[0].value;
9661 return temp;
9664 case MEM_REF:
9666 addr_space_t as
9667 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9668 enum machine_mode address_mode;
9669 tree base = TREE_OPERAND (exp, 0);
9670 gimple def_stmt;
9671 enum insn_code icode;
9672 unsigned align;
9673 /* Handle expansion of non-aliased memory with non-BLKmode. That
9674 might end up in a register. */
9675 if (mem_ref_refers_to_non_mem_p (exp))
9677 HOST_WIDE_INT offset = mem_ref_offset (exp).low;
9678 tree bit_offset;
9679 tree bftype;
9680 base = TREE_OPERAND (base, 0);
9681 if (offset == 0
9682 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
9683 && (GET_MODE_BITSIZE (DECL_MODE (base))
9684 == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))
9685 return expand_expr (build1 (VIEW_CONVERT_EXPR,
9686 TREE_TYPE (exp), base),
9687 target, tmode, modifier);
9688 bit_offset = bitsize_int (offset * BITS_PER_UNIT);
9689 bftype = TREE_TYPE (base);
9690 if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
9691 bftype = TREE_TYPE (exp);
9692 else
9694 temp = assign_stack_temp (DECL_MODE (base),
9695 GET_MODE_SIZE (DECL_MODE (base)));
9696 store_expr (base, temp, 0, false);
9697 temp = adjust_address (temp, BLKmode, offset);
9698 set_mem_size (temp, int_size_in_bytes (TREE_TYPE (exp)));
9699 return temp;
9701 return expand_expr (build3 (BIT_FIELD_REF, bftype,
9702 base,
9703 TYPE_SIZE (TREE_TYPE (exp)),
9704 bit_offset),
9705 target, tmode, modifier);
9707 address_mode = targetm.addr_space.address_mode (as);
9708 base = TREE_OPERAND (exp, 0);
9709 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9711 tree mask = gimple_assign_rhs2 (def_stmt);
9712 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9713 gimple_assign_rhs1 (def_stmt), mask);
9714 TREE_OPERAND (exp, 0) = base;
9716 align = get_object_alignment (exp);
9717 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9718 op0 = memory_address_addr_space (address_mode, op0, as);
9719 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9721 rtx off
9722 = immed_double_int_const (mem_ref_offset (exp), address_mode);
9723 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9725 op0 = memory_address_addr_space (mode, op0, as);
9726 temp = gen_rtx_MEM (mode, op0);
9727 set_mem_attributes (temp, exp, 0);
9728 set_mem_addr_space (temp, as);
9729 if (TREE_THIS_VOLATILE (exp))
9730 MEM_VOLATILE_P (temp) = 1;
9731 if (modifier != EXPAND_WRITE
9732 && modifier != EXPAND_MEMORY
9733 && mode != BLKmode
9734 && align < GET_MODE_ALIGNMENT (mode))
9736 if ((icode = optab_handler (movmisalign_optab, mode))
9737 != CODE_FOR_nothing)
9739 struct expand_operand ops[2];
9741 /* We've already validated the memory, and we're creating a
9742 new pseudo destination. The predicates really can't fail,
9743 nor can the generator. */
9744 create_output_operand (&ops[0], NULL_RTX, mode);
9745 create_fixed_operand (&ops[1], temp);
9746 expand_insn (icode, 2, ops);
9747 return ops[0].value;
9749 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9750 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9751 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9752 true, (modifier == EXPAND_STACK_PARM
9753 ? NULL_RTX : target),
9754 mode, mode);
9756 return temp;
9759 case ARRAY_REF:
9762 tree array = treeop0;
9763 tree index = treeop1;
9765 /* Fold an expression like: "foo"[2].
9766 This is not done in fold so it won't happen inside &.
9767 Don't fold if this is for wide characters since it's too
9768 difficult to do correctly and this is a very rare case. */
9770 if (modifier != EXPAND_CONST_ADDRESS
9771 && modifier != EXPAND_INITIALIZER
9772 && modifier != EXPAND_MEMORY)
9774 tree t = fold_read_from_constant_string (exp);
9776 if (t)
9777 return expand_expr (t, target, tmode, modifier);
9780 /* If this is a constant index into a constant array,
9781 just get the value from the array. Handle both the cases when
9782 we have an explicit constructor and when our operand is a variable
9783 that was declared const. */
9785 if (modifier != EXPAND_CONST_ADDRESS
9786 && modifier != EXPAND_INITIALIZER
9787 && modifier != EXPAND_MEMORY
9788 && TREE_CODE (array) == CONSTRUCTOR
9789 && ! TREE_SIDE_EFFECTS (array)
9790 && TREE_CODE (index) == INTEGER_CST)
9792 unsigned HOST_WIDE_INT ix;
9793 tree field, value;
9795 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9796 field, value)
9797 if (tree_int_cst_equal (field, index))
9799 if (!TREE_SIDE_EFFECTS (value))
9800 return expand_expr (fold (value), target, tmode, modifier);
9801 break;
9805 else if (optimize >= 1
9806 && modifier != EXPAND_CONST_ADDRESS
9807 && modifier != EXPAND_INITIALIZER
9808 && modifier != EXPAND_MEMORY
9809 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9810 && TREE_CODE (array) == VAR_DECL && DECL_INITIAL (array)
9811 && TREE_CODE (DECL_INITIAL (array)) != ERROR_MARK
9812 && const_value_known_p (array))
9814 if (TREE_CODE (index) == INTEGER_CST)
9816 tree init = DECL_INITIAL (array);
9818 if (TREE_CODE (init) == CONSTRUCTOR)
9820 unsigned HOST_WIDE_INT ix;
9821 tree field, value;
9823 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9824 field, value)
9825 if (tree_int_cst_equal (field, index))
9827 if (TREE_SIDE_EFFECTS (value))
9828 break;
9830 if (TREE_CODE (value) == CONSTRUCTOR)
9832 /* If VALUE is a CONSTRUCTOR, this
9833 optimization is only useful if
9834 this doesn't store the CONSTRUCTOR
9835 into memory. If it does, it is more
9836 efficient to just load the data from
9837 the array directly. */
9838 rtx ret = expand_constructor (value, target,
9839 modifier, true);
9840 if (ret == NULL_RTX)
9841 break;
9844 return expand_expr (fold (value), target, tmode,
9845 modifier);
9848 else if(TREE_CODE (init) == STRING_CST)
9850 tree index1 = index;
9851 tree low_bound = array_ref_low_bound (exp);
9852 index1 = fold_convert_loc (loc, sizetype,
9853 treeop1);
9855 /* Optimize the special-case of a zero lower bound.
9857 We convert the low_bound to sizetype to avoid some problems
9858 with constant folding. (E.g. suppose the lower bound is 1,
9859 and its mode is QI. Without the conversion,l (ARRAY
9860 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9861 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9863 if (! integer_zerop (low_bound))
9864 index1 = size_diffop_loc (loc, index1,
9865 fold_convert_loc (loc, sizetype,
9866 low_bound));
9868 if (0 > compare_tree_int (index1,
9869 TREE_STRING_LENGTH (init)))
9871 tree type = TREE_TYPE (TREE_TYPE (init));
9872 enum machine_mode mode = TYPE_MODE (type);
9874 if (GET_MODE_CLASS (mode) == MODE_INT
9875 && GET_MODE_SIZE (mode) == 1)
9876 return gen_int_mode (TREE_STRING_POINTER (init)
9877 [TREE_INT_CST_LOW (index1)],
9878 mode);
9884 goto normal_inner_ref;
9886 case COMPONENT_REF:
9887 /* If the operand is a CONSTRUCTOR, we can just extract the
9888 appropriate field if it is present. */
9889 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9891 unsigned HOST_WIDE_INT idx;
9892 tree field, value;
9894 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9895 idx, field, value)
9896 if (field == treeop1
9897 /* We can normally use the value of the field in the
9898 CONSTRUCTOR. However, if this is a bitfield in
9899 an integral mode that we can fit in a HOST_WIDE_INT,
9900 we must mask only the number of bits in the bitfield,
9901 since this is done implicitly by the constructor. If
9902 the bitfield does not meet either of those conditions,
9903 we can't do this optimization. */
9904 && (! DECL_BIT_FIELD (field)
9905 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9906 && (GET_MODE_PRECISION (DECL_MODE (field))
9907 <= HOST_BITS_PER_WIDE_INT))))
9909 if (DECL_BIT_FIELD (field)
9910 && modifier == EXPAND_STACK_PARM)
9911 target = 0;
9912 op0 = expand_expr (value, target, tmode, modifier);
9913 if (DECL_BIT_FIELD (field))
9915 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9916 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9918 if (TYPE_UNSIGNED (TREE_TYPE (field)))
9920 op1 = GEN_INT (((HOST_WIDE_INT) 1 << bitsize) - 1);
9921 op0 = expand_and (imode, op0, op1, target);
9923 else
9925 int count = GET_MODE_PRECISION (imode) - bitsize;
9927 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9928 target, 0);
9929 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9930 target, 0);
9934 return op0;
9937 goto normal_inner_ref;
9939 case BIT_FIELD_REF:
9940 case ARRAY_RANGE_REF:
9941 normal_inner_ref:
9943 enum machine_mode mode1, mode2;
9944 HOST_WIDE_INT bitsize, bitpos;
9945 tree offset;
9946 int volatilep = 0, must_force_mem;
9947 bool packedp = false;
9948 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
9949 &mode1, &unsignedp, &volatilep, true);
9950 rtx orig_op0, memloc;
9951 bool mem_attrs_from_type = false;
9953 /* If we got back the original object, something is wrong. Perhaps
9954 we are evaluating an expression too early. In any event, don't
9955 infinitely recurse. */
9956 gcc_assert (tem != exp);
9958 if (TYPE_PACKED (TREE_TYPE (TREE_OPERAND (exp, 0)))
9959 || (TREE_CODE (TREE_OPERAND (exp, 1)) == FIELD_DECL
9960 && DECL_PACKED (TREE_OPERAND (exp, 1))))
9961 packedp = true;
9963 /* If TEM's type is a union of variable size, pass TARGET to the inner
9964 computation, since it will need a temporary and TARGET is known
9965 to have to do. This occurs in unchecked conversion in Ada. */
9966 orig_op0 = op0
9967 = expand_expr (tem,
9968 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
9969 && COMPLETE_TYPE_P (TREE_TYPE (tem))
9970 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9971 != INTEGER_CST)
9972 && modifier != EXPAND_STACK_PARM
9973 ? target : NULL_RTX),
9974 VOIDmode,
9975 (modifier == EXPAND_INITIALIZER
9976 || modifier == EXPAND_CONST_ADDRESS
9977 || modifier == EXPAND_STACK_PARM)
9978 ? modifier : EXPAND_NORMAL);
9981 /* If the bitfield is volatile, we want to access it in the
9982 field's mode, not the computed mode.
9983 If a MEM has VOIDmode (external with incomplete type),
9984 use BLKmode for it instead. */
9985 if (MEM_P (op0))
9987 if (volatilep && flag_strict_volatile_bitfields > 0)
9988 op0 = adjust_address (op0, mode1, 0);
9989 else if (GET_MODE (op0) == VOIDmode)
9990 op0 = adjust_address (op0, BLKmode, 0);
9993 mode2
9994 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
9996 /* If we have either an offset, a BLKmode result, or a reference
9997 outside the underlying object, we must force it to memory.
9998 Such a case can occur in Ada if we have unchecked conversion
9999 of an expression from a scalar type to an aggregate type or
10000 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10001 passed a partially uninitialized object or a view-conversion
10002 to a larger size. */
10003 must_force_mem = (offset
10004 || mode1 == BLKmode
10005 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10007 /* Handle CONCAT first. */
10008 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10010 if (bitpos == 0
10011 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10012 return op0;
10013 if (bitpos == 0
10014 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10015 && bitsize)
10017 op0 = XEXP (op0, 0);
10018 mode2 = GET_MODE (op0);
10020 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10021 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10022 && bitpos
10023 && bitsize)
10025 op0 = XEXP (op0, 1);
10026 bitpos = 0;
10027 mode2 = GET_MODE (op0);
10029 else
10030 /* Otherwise force into memory. */
10031 must_force_mem = 1;
10034 /* If this is a constant, put it in a register if it is a legitimate
10035 constant and we don't need a memory reference. */
10036 if (CONSTANT_P (op0)
10037 && mode2 != BLKmode
10038 && targetm.legitimate_constant_p (mode2, op0)
10039 && !must_force_mem)
10040 op0 = force_reg (mode2, op0);
10042 /* Otherwise, if this is a constant, try to force it to the constant
10043 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10044 is a legitimate constant. */
10045 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10046 op0 = validize_mem (memloc);
10048 /* Otherwise, if this is a constant or the object is not in memory
10049 and need be, put it there. */
10050 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10052 tree nt = build_qualified_type (TREE_TYPE (tem),
10053 (TYPE_QUALS (TREE_TYPE (tem))
10054 | TYPE_QUAL_CONST));
10055 memloc = assign_temp (nt, 1, 1);
10056 emit_move_insn (memloc, op0);
10057 op0 = memloc;
10058 mem_attrs_from_type = true;
10061 if (offset)
10063 enum machine_mode address_mode;
10064 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10065 EXPAND_SUM);
10067 gcc_assert (MEM_P (op0));
10069 address_mode = get_address_mode (op0);
10070 if (GET_MODE (offset_rtx) != address_mode)
10071 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10073 if (GET_MODE (op0) == BLKmode
10074 /* A constant address in OP0 can have VOIDmode, we must
10075 not try to call force_reg in that case. */
10076 && GET_MODE (XEXP (op0, 0)) != VOIDmode
10077 && bitsize != 0
10078 && (bitpos % bitsize) == 0
10079 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10080 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
10082 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10083 bitpos = 0;
10086 op0 = offset_address (op0, offset_rtx,
10087 highest_pow2_factor (offset));
10090 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10091 record its alignment as BIGGEST_ALIGNMENT. */
10092 if (MEM_P (op0) && bitpos == 0 && offset != 0
10093 && is_aligning_offset (offset, tem))
10094 set_mem_align (op0, BIGGEST_ALIGNMENT);
10096 /* Don't forget about volatility even if this is a bitfield. */
10097 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10099 if (op0 == orig_op0)
10100 op0 = copy_rtx (op0);
10102 MEM_VOLATILE_P (op0) = 1;
10105 /* In cases where an aligned union has an unaligned object
10106 as a field, we might be extracting a BLKmode value from
10107 an integer-mode (e.g., SImode) object. Handle this case
10108 by doing the extract into an object as wide as the field
10109 (which we know to be the width of a basic mode), then
10110 storing into memory, and changing the mode to BLKmode. */
10111 if (mode1 == VOIDmode
10112 || REG_P (op0) || GET_CODE (op0) == SUBREG
10113 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10114 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10115 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10116 && modifier != EXPAND_CONST_ADDRESS
10117 && modifier != EXPAND_INITIALIZER
10118 && modifier != EXPAND_MEMORY)
10119 /* If the field is volatile, we always want an aligned
10120 access. Do this in following two situations:
10121 1. the access is not already naturally
10122 aligned, otherwise "normal" (non-bitfield) volatile fields
10123 become non-addressable.
10124 2. the bitsize is narrower than the access size. Need
10125 to extract bitfields from the access. */
10126 || (volatilep && flag_strict_volatile_bitfields > 0
10127 && (bitpos % GET_MODE_ALIGNMENT (mode) != 0
10128 || (mode1 != BLKmode
10129 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)))
10130 /* If the field isn't aligned enough to fetch as a memref,
10131 fetch it as a bit field. */
10132 || (mode1 != BLKmode
10133 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10134 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10135 || (MEM_P (op0)
10136 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10137 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10138 && ((modifier == EXPAND_CONST_ADDRESS
10139 || modifier == EXPAND_INITIALIZER)
10140 ? STRICT_ALIGNMENT
10141 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10142 || (bitpos % BITS_PER_UNIT != 0)))
10143 /* If the type and the field are a constant size and the
10144 size of the type isn't the same size as the bitfield,
10145 we must use bitfield operations. */
10146 || (bitsize >= 0
10147 && TYPE_SIZE (TREE_TYPE (exp))
10148 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10149 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10150 bitsize)))
10152 enum machine_mode ext_mode = mode;
10154 if (ext_mode == BLKmode
10155 && ! (target != 0 && MEM_P (op0)
10156 && MEM_P (target)
10157 && bitpos % BITS_PER_UNIT == 0))
10158 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10160 if (ext_mode == BLKmode)
10162 if (target == 0)
10163 target = assign_temp (type, 1, 1);
10165 if (bitsize == 0)
10166 return target;
10168 /* In this case, BITPOS must start at a byte boundary and
10169 TARGET, if specified, must be a MEM. */
10170 gcc_assert (MEM_P (op0)
10171 && (!target || MEM_P (target))
10172 && !(bitpos % BITS_PER_UNIT));
10174 emit_block_move (target,
10175 adjust_address (op0, VOIDmode,
10176 bitpos / BITS_PER_UNIT),
10177 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10178 / BITS_PER_UNIT),
10179 (modifier == EXPAND_STACK_PARM
10180 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10182 return target;
10185 op0 = validize_mem (op0);
10187 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10188 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10190 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, packedp,
10191 (modifier == EXPAND_STACK_PARM
10192 ? NULL_RTX : target),
10193 ext_mode, ext_mode);
10195 /* If the result is a record type and BITSIZE is narrower than
10196 the mode of OP0, an integral mode, and this is a big endian
10197 machine, we must put the field into the high-order bits. */
10198 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10199 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10200 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10201 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10202 GET_MODE_BITSIZE (GET_MODE (op0))
10203 - bitsize, op0, 1);
10205 /* If the result type is BLKmode, store the data into a temporary
10206 of the appropriate type, but with the mode corresponding to the
10207 mode for the data we have (op0's mode). It's tempting to make
10208 this a constant type, since we know it's only being stored once,
10209 but that can cause problems if we are taking the address of this
10210 COMPONENT_REF because the MEM of any reference via that address
10211 will have flags corresponding to the type, which will not
10212 necessarily be constant. */
10213 if (mode == BLKmode)
10215 rtx new_rtx;
10217 new_rtx = assign_stack_temp_for_type (ext_mode,
10218 GET_MODE_BITSIZE (ext_mode),
10219 type);
10220 emit_move_insn (new_rtx, op0);
10221 op0 = copy_rtx (new_rtx);
10222 PUT_MODE (op0, BLKmode);
10225 return op0;
10228 /* If the result is BLKmode, use that to access the object
10229 now as well. */
10230 if (mode == BLKmode)
10231 mode1 = BLKmode;
10233 /* Get a reference to just this component. */
10234 if (modifier == EXPAND_CONST_ADDRESS
10235 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10236 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10237 else
10238 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10240 if (op0 == orig_op0)
10241 op0 = copy_rtx (op0);
10243 /* If op0 is a temporary because of forcing to memory, pass only the
10244 type to set_mem_attributes so that the original expression is never
10245 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10246 if (mem_attrs_from_type)
10247 set_mem_attributes (op0, type, 0);
10248 else
10249 set_mem_attributes (op0, exp, 0);
10251 if (REG_P (XEXP (op0, 0)))
10252 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10254 MEM_VOLATILE_P (op0) |= volatilep;
10255 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10256 || modifier == EXPAND_CONST_ADDRESS
10257 || modifier == EXPAND_INITIALIZER)
10258 return op0;
10259 else if (target == 0)
10260 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10262 convert_move (target, op0, unsignedp);
10263 return target;
10266 case OBJ_TYPE_REF:
10267 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10269 case CALL_EXPR:
10270 /* All valid uses of __builtin_va_arg_pack () are removed during
10271 inlining. */
10272 if (CALL_EXPR_VA_ARG_PACK (exp))
10273 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10275 tree fndecl = get_callee_fndecl (exp), attr;
10277 if (fndecl
10278 && (attr = lookup_attribute ("error",
10279 DECL_ATTRIBUTES (fndecl))) != NULL)
10280 error ("%Kcall to %qs declared with attribute error: %s",
10281 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10282 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10283 if (fndecl
10284 && (attr = lookup_attribute ("warning",
10285 DECL_ATTRIBUTES (fndecl))) != NULL)
10286 warning_at (tree_nonartificial_location (exp),
10287 0, "%Kcall to %qs declared with attribute warning: %s",
10288 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10289 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10291 /* Check for a built-in function. */
10292 if (fndecl && DECL_BUILT_IN (fndecl))
10294 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10295 return expand_builtin (exp, target, subtarget, tmode, ignore);
10298 return expand_call (exp, target, ignore);
10300 case VIEW_CONVERT_EXPR:
10301 op0 = NULL_RTX;
10303 /* If we are converting to BLKmode, try to avoid an intermediate
10304 temporary by fetching an inner memory reference. */
10305 if (mode == BLKmode
10306 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10307 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10308 && handled_component_p (treeop0))
10310 enum machine_mode mode1;
10311 HOST_WIDE_INT bitsize, bitpos;
10312 tree offset;
10313 int unsignedp;
10314 int volatilep = 0;
10315 tree tem
10316 = get_inner_reference (treeop0, &bitsize, &bitpos,
10317 &offset, &mode1, &unsignedp, &volatilep,
10318 true);
10319 rtx orig_op0;
10321 /* ??? We should work harder and deal with non-zero offsets. */
10322 if (!offset
10323 && (bitpos % BITS_PER_UNIT) == 0
10324 && bitsize >= 0
10325 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) == 0)
10327 /* See the normal_inner_ref case for the rationale. */
10328 orig_op0
10329 = expand_expr (tem,
10330 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10331 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10332 != INTEGER_CST)
10333 && modifier != EXPAND_STACK_PARM
10334 ? target : NULL_RTX),
10335 VOIDmode,
10336 (modifier == EXPAND_INITIALIZER
10337 || modifier == EXPAND_CONST_ADDRESS
10338 || modifier == EXPAND_STACK_PARM)
10339 ? modifier : EXPAND_NORMAL);
10341 if (MEM_P (orig_op0))
10343 op0 = orig_op0;
10345 /* Get a reference to just this component. */
10346 if (modifier == EXPAND_CONST_ADDRESS
10347 || modifier == EXPAND_SUM
10348 || modifier == EXPAND_INITIALIZER)
10349 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10350 else
10351 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10353 if (op0 == orig_op0)
10354 op0 = copy_rtx (op0);
10356 set_mem_attributes (op0, treeop0, 0);
10357 if (REG_P (XEXP (op0, 0)))
10358 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10360 MEM_VOLATILE_P (op0) |= volatilep;
10365 if (!op0)
10366 op0 = expand_expr (treeop0,
10367 NULL_RTX, VOIDmode, modifier);
10369 /* If the input and output modes are both the same, we are done. */
10370 if (mode == GET_MODE (op0))
10372 /* If neither mode is BLKmode, and both modes are the same size
10373 then we can use gen_lowpart. */
10374 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10375 && (GET_MODE_PRECISION (mode)
10376 == GET_MODE_PRECISION (GET_MODE (op0)))
10377 && !COMPLEX_MODE_P (GET_MODE (op0)))
10379 if (GET_CODE (op0) == SUBREG)
10380 op0 = force_reg (GET_MODE (op0), op0);
10381 temp = gen_lowpart_common (mode, op0);
10382 if (temp)
10383 op0 = temp;
10384 else
10386 if (!REG_P (op0) && !MEM_P (op0))
10387 op0 = force_reg (GET_MODE (op0), op0);
10388 op0 = gen_lowpart (mode, op0);
10391 /* If both types are integral, convert from one mode to the other. */
10392 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10393 op0 = convert_modes (mode, GET_MODE (op0), op0,
10394 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10395 /* As a last resort, spill op0 to memory, and reload it in a
10396 different mode. */
10397 else if (!MEM_P (op0))
10399 /* If the operand is not a MEM, force it into memory. Since we
10400 are going to be changing the mode of the MEM, don't call
10401 force_const_mem for constants because we don't allow pool
10402 constants to change mode. */
10403 tree inner_type = TREE_TYPE (treeop0);
10405 gcc_assert (!TREE_ADDRESSABLE (exp));
10407 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10408 target
10409 = assign_stack_temp_for_type
10410 (TYPE_MODE (inner_type),
10411 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10413 emit_move_insn (target, op0);
10414 op0 = target;
10417 /* At this point, OP0 is in the correct mode. If the output type is
10418 such that the operand is known to be aligned, indicate that it is.
10419 Otherwise, we need only be concerned about alignment for non-BLKmode
10420 results. */
10421 if (MEM_P (op0))
10423 enum insn_code icode;
10425 if (TYPE_ALIGN_OK (type))
10427 /* ??? Copying the MEM without substantially changing it might
10428 run afoul of the code handling volatile memory references in
10429 store_expr, which assumes that TARGET is returned unmodified
10430 if it has been used. */
10431 op0 = copy_rtx (op0);
10432 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10434 else if (mode != BLKmode
10435 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)
10436 /* If the target does have special handling for unaligned
10437 loads of mode then use them. */
10438 && ((icode = optab_handler (movmisalign_optab, mode))
10439 != CODE_FOR_nothing))
10441 rtx reg, insn;
10443 op0 = adjust_address (op0, mode, 0);
10444 /* We've already validated the memory, and we're creating a
10445 new pseudo destination. The predicates really can't
10446 fail. */
10447 reg = gen_reg_rtx (mode);
10449 /* Nor can the insn generator. */
10450 insn = GEN_FCN (icode) (reg, op0);
10451 emit_insn (insn);
10452 return reg;
10454 else if (STRICT_ALIGNMENT
10455 && mode != BLKmode
10456 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10458 tree inner_type = TREE_TYPE (treeop0);
10459 HOST_WIDE_INT temp_size
10460 = MAX (int_size_in_bytes (inner_type),
10461 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10462 rtx new_rtx
10463 = assign_stack_temp_for_type (mode, temp_size, type);
10464 rtx new_with_op0_mode
10465 = adjust_address (new_rtx, GET_MODE (op0), 0);
10467 gcc_assert (!TREE_ADDRESSABLE (exp));
10469 if (GET_MODE (op0) == BLKmode)
10470 emit_block_move (new_with_op0_mode, op0,
10471 GEN_INT (GET_MODE_SIZE (mode)),
10472 (modifier == EXPAND_STACK_PARM
10473 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10474 else
10475 emit_move_insn (new_with_op0_mode, op0);
10477 op0 = new_rtx;
10480 op0 = adjust_address (op0, mode, 0);
10483 return op0;
10485 case MODIFY_EXPR:
10487 tree lhs = treeop0;
10488 tree rhs = treeop1;
10489 gcc_assert (ignore);
10491 /* Check for |= or &= of a bitfield of size one into another bitfield
10492 of size 1. In this case, (unless we need the result of the
10493 assignment) we can do this more efficiently with a
10494 test followed by an assignment, if necessary.
10496 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10497 things change so we do, this code should be enhanced to
10498 support it. */
10499 if (TREE_CODE (lhs) == COMPONENT_REF
10500 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10501 || TREE_CODE (rhs) == BIT_AND_EXPR)
10502 && TREE_OPERAND (rhs, 0) == lhs
10503 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10504 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10505 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10507 rtx label = gen_label_rtx ();
10508 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10509 do_jump (TREE_OPERAND (rhs, 1),
10510 value ? label : 0,
10511 value ? 0 : label, -1);
10512 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10513 false);
10514 do_pending_stack_adjust ();
10515 emit_label (label);
10516 return const0_rtx;
10519 expand_assignment (lhs, rhs, false);
10520 return const0_rtx;
10523 case ADDR_EXPR:
10524 return expand_expr_addr_expr (exp, target, tmode, modifier);
10526 case REALPART_EXPR:
10527 op0 = expand_normal (treeop0);
10528 return read_complex_part (op0, false);
10530 case IMAGPART_EXPR:
10531 op0 = expand_normal (treeop0);
10532 return read_complex_part (op0, true);
10534 case RETURN_EXPR:
10535 case LABEL_EXPR:
10536 case GOTO_EXPR:
10537 case SWITCH_EXPR:
10538 case ASM_EXPR:
10539 /* Expanded in cfgexpand.c. */
10540 gcc_unreachable ();
10542 case TRY_CATCH_EXPR:
10543 case CATCH_EXPR:
10544 case EH_FILTER_EXPR:
10545 case TRY_FINALLY_EXPR:
10546 /* Lowered by tree-eh.c. */
10547 gcc_unreachable ();
10549 case WITH_CLEANUP_EXPR:
10550 case CLEANUP_POINT_EXPR:
10551 case TARGET_EXPR:
10552 case CASE_LABEL_EXPR:
10553 case VA_ARG_EXPR:
10554 case BIND_EXPR:
10555 case INIT_EXPR:
10556 case CONJ_EXPR:
10557 case COMPOUND_EXPR:
10558 case PREINCREMENT_EXPR:
10559 case PREDECREMENT_EXPR:
10560 case POSTINCREMENT_EXPR:
10561 case POSTDECREMENT_EXPR:
10562 case LOOP_EXPR:
10563 case EXIT_EXPR:
10564 case COMPOUND_LITERAL_EXPR:
10565 /* Lowered by gimplify.c. */
10566 gcc_unreachable ();
10568 case FDESC_EXPR:
10569 /* Function descriptors are not valid except for as
10570 initialization constants, and should not be expanded. */
10571 gcc_unreachable ();
10573 case WITH_SIZE_EXPR:
10574 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10575 have pulled out the size to use in whatever context it needed. */
10576 return expand_expr_real (treeop0, original_target, tmode,
10577 modifier, alt_rtl);
10579 default:
10580 return expand_expr_real_2 (&ops, target, tmode, modifier);
10584 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10585 signedness of TYPE), possibly returning the result in TARGET. */
10586 static rtx
10587 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10589 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10590 if (target && GET_MODE (target) != GET_MODE (exp))
10591 target = 0;
10592 /* For constant values, reduce using build_int_cst_type. */
10593 if (CONST_INT_P (exp))
10595 HOST_WIDE_INT value = INTVAL (exp);
10596 tree t = build_int_cst_type (type, value);
10597 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10599 else if (TYPE_UNSIGNED (type))
10601 rtx mask = immed_double_int_const (double_int::mask (prec),
10602 GET_MODE (exp));
10603 return expand_and (GET_MODE (exp), exp, mask, target);
10605 else
10607 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10608 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10609 exp, count, target, 0);
10610 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10611 exp, count, target, 0);
10615 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10616 when applied to the address of EXP produces an address known to be
10617 aligned more than BIGGEST_ALIGNMENT. */
10619 static int
10620 is_aligning_offset (const_tree offset, const_tree exp)
10622 /* Strip off any conversions. */
10623 while (CONVERT_EXPR_P (offset))
10624 offset = TREE_OPERAND (offset, 0);
10626 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10627 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10628 if (TREE_CODE (offset) != BIT_AND_EXPR
10629 || !host_integerp (TREE_OPERAND (offset, 1), 1)
10630 || compare_tree_int (TREE_OPERAND (offset, 1),
10631 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10632 || !exact_log2 (tree_low_cst (TREE_OPERAND (offset, 1), 1) + 1) < 0)
10633 return 0;
10635 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10636 It must be NEGATE_EXPR. Then strip any more conversions. */
10637 offset = TREE_OPERAND (offset, 0);
10638 while (CONVERT_EXPR_P (offset))
10639 offset = TREE_OPERAND (offset, 0);
10641 if (TREE_CODE (offset) != NEGATE_EXPR)
10642 return 0;
10644 offset = TREE_OPERAND (offset, 0);
10645 while (CONVERT_EXPR_P (offset))
10646 offset = TREE_OPERAND (offset, 0);
10648 /* This must now be the address of EXP. */
10649 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10652 /* Return the tree node if an ARG corresponds to a string constant or zero
10653 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10654 in bytes within the string that ARG is accessing. The type of the
10655 offset will be `sizetype'. */
10657 tree
10658 string_constant (tree arg, tree *ptr_offset)
10660 tree array, offset, lower_bound;
10661 STRIP_NOPS (arg);
10663 if (TREE_CODE (arg) == ADDR_EXPR)
10665 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10667 *ptr_offset = size_zero_node;
10668 return TREE_OPERAND (arg, 0);
10670 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10672 array = TREE_OPERAND (arg, 0);
10673 offset = size_zero_node;
10675 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10677 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10678 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10679 if (TREE_CODE (array) != STRING_CST
10680 && TREE_CODE (array) != VAR_DECL)
10681 return 0;
10683 /* Check if the array has a nonzero lower bound. */
10684 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10685 if (!integer_zerop (lower_bound))
10687 /* If the offset and base aren't both constants, return 0. */
10688 if (TREE_CODE (lower_bound) != INTEGER_CST)
10689 return 0;
10690 if (TREE_CODE (offset) != INTEGER_CST)
10691 return 0;
10692 /* Adjust offset by the lower bound. */
10693 offset = size_diffop (fold_convert (sizetype, offset),
10694 fold_convert (sizetype, lower_bound));
10697 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10699 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10700 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10701 if (TREE_CODE (array) != ADDR_EXPR)
10702 return 0;
10703 array = TREE_OPERAND (array, 0);
10704 if (TREE_CODE (array) != STRING_CST
10705 && TREE_CODE (array) != VAR_DECL)
10706 return 0;
10708 else
10709 return 0;
10711 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10713 tree arg0 = TREE_OPERAND (arg, 0);
10714 tree arg1 = TREE_OPERAND (arg, 1);
10716 STRIP_NOPS (arg0);
10717 STRIP_NOPS (arg1);
10719 if (TREE_CODE (arg0) == ADDR_EXPR
10720 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10721 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10723 array = TREE_OPERAND (arg0, 0);
10724 offset = arg1;
10726 else if (TREE_CODE (arg1) == ADDR_EXPR
10727 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10728 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10730 array = TREE_OPERAND (arg1, 0);
10731 offset = arg0;
10733 else
10734 return 0;
10736 else
10737 return 0;
10739 if (TREE_CODE (array) == STRING_CST)
10741 *ptr_offset = fold_convert (sizetype, offset);
10742 return array;
10744 else if (TREE_CODE (array) == VAR_DECL
10745 || TREE_CODE (array) == CONST_DECL)
10747 int length;
10749 /* Variables initialized to string literals can be handled too. */
10750 if (!const_value_known_p (array)
10751 || !DECL_INITIAL (array)
10752 || TREE_CODE (DECL_INITIAL (array)) != STRING_CST)
10753 return 0;
10755 /* Avoid const char foo[4] = "abcde"; */
10756 if (DECL_SIZE_UNIT (array) == NULL_TREE
10757 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10758 || (length = TREE_STRING_LENGTH (DECL_INITIAL (array))) <= 0
10759 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10760 return 0;
10762 /* If variable is bigger than the string literal, OFFSET must be constant
10763 and inside of the bounds of the string literal. */
10764 offset = fold_convert (sizetype, offset);
10765 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10766 && (! host_integerp (offset, 1)
10767 || compare_tree_int (offset, length) >= 0))
10768 return 0;
10770 *ptr_offset = offset;
10771 return DECL_INITIAL (array);
10774 return 0;
10777 /* Generate code to calculate OPS, and exploded expression
10778 using a store-flag instruction and return an rtx for the result.
10779 OPS reflects a comparison.
10781 If TARGET is nonzero, store the result there if convenient.
10783 Return zero if there is no suitable set-flag instruction
10784 available on this machine.
10786 Once expand_expr has been called on the arguments of the comparison,
10787 we are committed to doing the store flag, since it is not safe to
10788 re-evaluate the expression. We emit the store-flag insn by calling
10789 emit_store_flag, but only expand the arguments if we have a reason
10790 to believe that emit_store_flag will be successful. If we think that
10791 it will, but it isn't, we have to simulate the store-flag with a
10792 set/jump/set sequence. */
10794 static rtx
10795 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10797 enum rtx_code code;
10798 tree arg0, arg1, type;
10799 tree tem;
10800 enum machine_mode operand_mode;
10801 int unsignedp;
10802 rtx op0, op1;
10803 rtx subtarget = target;
10804 location_t loc = ops->location;
10806 arg0 = ops->op0;
10807 arg1 = ops->op1;
10809 /* Don't crash if the comparison was erroneous. */
10810 if (arg0 == error_mark_node || arg1 == error_mark_node)
10811 return const0_rtx;
10813 type = TREE_TYPE (arg0);
10814 operand_mode = TYPE_MODE (type);
10815 unsignedp = TYPE_UNSIGNED (type);
10817 /* We won't bother with BLKmode store-flag operations because it would mean
10818 passing a lot of information to emit_store_flag. */
10819 if (operand_mode == BLKmode)
10820 return 0;
10822 /* We won't bother with store-flag operations involving function pointers
10823 when function pointers must be canonicalized before comparisons. */
10824 #ifdef HAVE_canonicalize_funcptr_for_compare
10825 if (HAVE_canonicalize_funcptr_for_compare
10826 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10827 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10828 == FUNCTION_TYPE))
10829 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10830 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10831 == FUNCTION_TYPE))))
10832 return 0;
10833 #endif
10835 STRIP_NOPS (arg0);
10836 STRIP_NOPS (arg1);
10838 /* For vector typed comparisons emit code to generate the desired
10839 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10840 expander for this. */
10841 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10843 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10844 tree if_true = constant_boolean_node (true, ops->type);
10845 tree if_false = constant_boolean_node (false, ops->type);
10846 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10849 /* Get the rtx comparison code to use. We know that EXP is a comparison
10850 operation of some type. Some comparisons against 1 and -1 can be
10851 converted to comparisons with zero. Do so here so that the tests
10852 below will be aware that we have a comparison with zero. These
10853 tests will not catch constants in the first operand, but constants
10854 are rarely passed as the first operand. */
10856 switch (ops->code)
10858 case EQ_EXPR:
10859 code = EQ;
10860 break;
10861 case NE_EXPR:
10862 code = NE;
10863 break;
10864 case LT_EXPR:
10865 if (integer_onep (arg1))
10866 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10867 else
10868 code = unsignedp ? LTU : LT;
10869 break;
10870 case LE_EXPR:
10871 if (! unsignedp && integer_all_onesp (arg1))
10872 arg1 = integer_zero_node, code = LT;
10873 else
10874 code = unsignedp ? LEU : LE;
10875 break;
10876 case GT_EXPR:
10877 if (! unsignedp && integer_all_onesp (arg1))
10878 arg1 = integer_zero_node, code = GE;
10879 else
10880 code = unsignedp ? GTU : GT;
10881 break;
10882 case GE_EXPR:
10883 if (integer_onep (arg1))
10884 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10885 else
10886 code = unsignedp ? GEU : GE;
10887 break;
10889 case UNORDERED_EXPR:
10890 code = UNORDERED;
10891 break;
10892 case ORDERED_EXPR:
10893 code = ORDERED;
10894 break;
10895 case UNLT_EXPR:
10896 code = UNLT;
10897 break;
10898 case UNLE_EXPR:
10899 code = UNLE;
10900 break;
10901 case UNGT_EXPR:
10902 code = UNGT;
10903 break;
10904 case UNGE_EXPR:
10905 code = UNGE;
10906 break;
10907 case UNEQ_EXPR:
10908 code = UNEQ;
10909 break;
10910 case LTGT_EXPR:
10911 code = LTGT;
10912 break;
10914 default:
10915 gcc_unreachable ();
10918 /* Put a constant second. */
10919 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10920 || TREE_CODE (arg0) == FIXED_CST)
10922 tem = arg0; arg0 = arg1; arg1 = tem;
10923 code = swap_condition (code);
10926 /* If this is an equality or inequality test of a single bit, we can
10927 do this by shifting the bit being tested to the low-order bit and
10928 masking the result with the constant 1. If the condition was EQ,
10929 we xor it with 1. This does not require an scc insn and is faster
10930 than an scc insn even if we have it.
10932 The code to make this transformation was moved into fold_single_bit_test,
10933 so we just call into the folder and expand its result. */
10935 if ((code == NE || code == EQ)
10936 && integer_zerop (arg1)
10937 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
10939 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
10940 if (srcstmt
10941 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
10943 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
10944 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10945 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
10946 gimple_assign_rhs1 (srcstmt),
10947 gimple_assign_rhs2 (srcstmt));
10948 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
10949 if (temp)
10950 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
10954 if (! get_subtarget (target)
10955 || GET_MODE (subtarget) != operand_mode)
10956 subtarget = 0;
10958 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
10960 if (target == 0)
10961 target = gen_reg_rtx (mode);
10963 /* Try a cstore if possible. */
10964 return emit_store_flag_force (target, code, op0, op1,
10965 operand_mode, unsignedp,
10966 (TYPE_PRECISION (ops->type) == 1
10967 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
10971 /* Stubs in case we haven't got a casesi insn. */
10972 #ifndef HAVE_casesi
10973 # define HAVE_casesi 0
10974 # define gen_casesi(a, b, c, d, e) (0)
10975 # define CODE_FOR_casesi CODE_FOR_nothing
10976 #endif
10978 /* Attempt to generate a casesi instruction. Returns 1 if successful,
10979 0 otherwise (i.e. if there is no casesi instruction).
10981 DEFAULT_PROBABILITY is the probability of jumping to the default
10982 label. */
10984 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
10985 rtx table_label, rtx default_label, rtx fallback_label,
10986 int default_probability)
10988 struct expand_operand ops[5];
10989 enum machine_mode index_mode = SImode;
10990 rtx op1, op2, index;
10992 if (! HAVE_casesi)
10993 return 0;
10995 /* Convert the index to SImode. */
10996 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
10998 enum machine_mode omode = TYPE_MODE (index_type);
10999 rtx rangertx = expand_normal (range);
11001 /* We must handle the endpoints in the original mode. */
11002 index_expr = build2 (MINUS_EXPR, index_type,
11003 index_expr, minval);
11004 minval = integer_zero_node;
11005 index = expand_normal (index_expr);
11006 if (default_label)
11007 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11008 omode, 1, default_label,
11009 default_probability);
11010 /* Now we can safely truncate. */
11011 index = convert_to_mode (index_mode, index, 0);
11013 else
11015 if (TYPE_MODE (index_type) != index_mode)
11017 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11018 index_expr = fold_convert (index_type, index_expr);
11021 index = expand_normal (index_expr);
11024 do_pending_stack_adjust ();
11026 op1 = expand_normal (minval);
11027 op2 = expand_normal (range);
11029 create_input_operand (&ops[0], index, index_mode);
11030 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11031 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11032 create_fixed_operand (&ops[3], table_label);
11033 create_fixed_operand (&ops[4], (default_label
11034 ? default_label
11035 : fallback_label));
11036 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11037 return 1;
11040 /* Attempt to generate a tablejump instruction; same concept. */
11041 #ifndef HAVE_tablejump
11042 #define HAVE_tablejump 0
11043 #define gen_tablejump(x, y) (0)
11044 #endif
11046 /* Subroutine of the next function.
11048 INDEX is the value being switched on, with the lowest value
11049 in the table already subtracted.
11050 MODE is its expected mode (needed if INDEX is constant).
11051 RANGE is the length of the jump table.
11052 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11054 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11055 index value is out of range.
11056 DEFAULT_PROBABILITY is the probability of jumping to
11057 the default label. */
11059 static void
11060 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
11061 rtx default_label, int default_probability)
11063 rtx temp, vector;
11065 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11066 cfun->cfg->max_jumptable_ents = INTVAL (range);
11068 /* Do an unsigned comparison (in the proper mode) between the index
11069 expression and the value which represents the length of the range.
11070 Since we just finished subtracting the lower bound of the range
11071 from the index expression, this comparison allows us to simultaneously
11072 check that the original index expression value is both greater than
11073 or equal to the minimum value of the range and less than or equal to
11074 the maximum value of the range. */
11076 if (default_label)
11077 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11078 default_label, default_probability);
11081 /* If index is in range, it must fit in Pmode.
11082 Convert to Pmode so we can index with it. */
11083 if (mode != Pmode)
11084 index = convert_to_mode (Pmode, index, 1);
11086 /* Don't let a MEM slip through, because then INDEX that comes
11087 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11088 and break_out_memory_refs will go to work on it and mess it up. */
11089 #ifdef PIC_CASE_VECTOR_ADDRESS
11090 if (flag_pic && !REG_P (index))
11091 index = copy_to_mode_reg (Pmode, index);
11092 #endif
11094 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11095 GET_MODE_SIZE, because this indicates how large insns are. The other
11096 uses should all be Pmode, because they are addresses. This code
11097 could fail if addresses and insns are not the same size. */
11098 index = gen_rtx_PLUS (Pmode,
11099 gen_rtx_MULT (Pmode, index,
11100 GEN_INT (GET_MODE_SIZE (CASE_VECTOR_MODE))),
11101 gen_rtx_LABEL_REF (Pmode, table_label));
11102 #ifdef PIC_CASE_VECTOR_ADDRESS
11103 if (flag_pic)
11104 index = PIC_CASE_VECTOR_ADDRESS (index);
11105 else
11106 #endif
11107 index = memory_address (CASE_VECTOR_MODE, index);
11108 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11109 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11110 convert_move (temp, vector, 0);
11112 emit_jump_insn (gen_tablejump (temp, table_label));
11114 /* If we are generating PIC code or if the table is PC-relative, the
11115 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11116 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11117 emit_barrier ();
11121 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11122 rtx table_label, rtx default_label, int default_probability)
11124 rtx index;
11126 if (! HAVE_tablejump)
11127 return 0;
11129 index_expr = fold_build2 (MINUS_EXPR, index_type,
11130 fold_convert (index_type, index_expr),
11131 fold_convert (index_type, minval));
11132 index = expand_normal (index_expr);
11133 do_pending_stack_adjust ();
11135 do_tablejump (index, TYPE_MODE (index_type),
11136 convert_modes (TYPE_MODE (index_type),
11137 TYPE_MODE (TREE_TYPE (range)),
11138 expand_normal (range),
11139 TYPE_UNSIGNED (TREE_TYPE (range))),
11140 table_label, default_label, default_probability);
11141 return 1;
11144 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11145 static rtx
11146 const_vector_from_tree (tree exp)
11148 rtvec v;
11149 unsigned i;
11150 int units;
11151 tree elt;
11152 enum machine_mode inner, mode;
11154 mode = TYPE_MODE (TREE_TYPE (exp));
11156 if (initializer_zerop (exp))
11157 return CONST0_RTX (mode);
11159 units = GET_MODE_NUNITS (mode);
11160 inner = GET_MODE_INNER (mode);
11162 v = rtvec_alloc (units);
11164 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11166 elt = VECTOR_CST_ELT (exp, i);
11168 if (TREE_CODE (elt) == REAL_CST)
11169 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11170 inner);
11171 else if (TREE_CODE (elt) == FIXED_CST)
11172 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11173 inner);
11174 else
11175 RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
11176 inner);
11179 return gen_rtx_CONST_VECTOR (mode, v);
11182 /* Build a decl for a personality function given a language prefix. */
11184 tree
11185 build_personality_function (const char *lang)
11187 const char *unwind_and_version;
11188 tree decl, type;
11189 char *name;
11191 switch (targetm_common.except_unwind_info (&global_options))
11193 case UI_NONE:
11194 return NULL;
11195 case UI_SJLJ:
11196 unwind_and_version = "_sj0";
11197 break;
11198 case UI_DWARF2:
11199 case UI_TARGET:
11200 unwind_and_version = "_v0";
11201 break;
11202 case UI_SEH:
11203 unwind_and_version = "_seh0";
11204 break;
11205 default:
11206 gcc_unreachable ();
11209 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11211 type = build_function_type_list (integer_type_node, integer_type_node,
11212 long_long_unsigned_type_node,
11213 ptr_type_node, ptr_type_node, NULL_TREE);
11214 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11215 get_identifier (name), type);
11216 DECL_ARTIFICIAL (decl) = 1;
11217 DECL_EXTERNAL (decl) = 1;
11218 TREE_PUBLIC (decl) = 1;
11220 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11221 are the flags assigned by targetm.encode_section_info. */
11222 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11224 return decl;
11227 /* Extracts the personality function of DECL and returns the corresponding
11228 libfunc. */
11231 get_personality_function (tree decl)
11233 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11234 enum eh_personality_kind pk;
11236 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11237 if (pk == eh_personality_none)
11238 return NULL;
11240 if (!personality
11241 && pk == eh_personality_any)
11242 personality = lang_hooks.eh_personality ();
11244 if (pk == eh_personality_lang)
11245 gcc_assert (personality != NULL_TREE);
11247 return XEXP (DECL_RTL (personality), 0);
11250 #include "gt-expr.h"