]> gcc.gnu.org Git - gcc.git/blame - gcc/expr.c
re PR middle-end/53790 (ICE on dereferencing a extern union in asm statement)
[gcc.git] / gcc / expr.c
CommitLineData
bbf6f052 1/* Convert tree expression to rtl instructions, for GNU compiler.
8752c357 2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
319638ed
JJ
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
bbf6f052 5
1322177d 6This file is part of GCC.
bbf6f052 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
bbf6f052 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
bbf6f052
RK
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
bbf6f052 21
bbf6f052 22#include "config.h"
670ee920 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
ca695ac9 26#include "machmode.h"
bbf6f052
RK
27#include "rtl.h"
28#include "tree.h"
29#include "flags.h"
bf76bb5a 30#include "regs.h"
4ed67205 31#include "hard-reg-set.h"
3d195391 32#include "except.h"
bbf6f052 33#include "function.h"
bbf6f052 34#include "insn-config.h"
34e81b5a 35#include "insn-attr.h"
3a94c984 36/* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
d6f4ec51 37#include "expr.h"
e78d8e51
ZW
38#include "optabs.h"
39#include "libfuncs.h"
bbf6f052 40#include "recog.h"
3ef1eef4 41#include "reload.h"
bbf6f052 42#include "typeclass.h"
10f0ad3d 43#include "toplev.h"
ac79cd5a 44#include "langhooks.h"
e2c49ac2 45#include "intl.h"
b1474bb7 46#include "tm_p.h"
6de9cd9a 47#include "tree-iterator.h"
2f8e398b
PB
48#include "tree-pass.h"
49#include "tree-flow.h"
c988af2b 50#include "target.h"
677f3fa8 51#include "common/common-target.h"
2f8e398b 52#include "timevar.h"
6fb5fa3c 53#include "df.h"
a509ebb5 54#include "diagnostic.h"
4e3825db 55#include "ssaexpand.h"
9d86796b 56#include "target-globals.h"
1169e45d 57#include "params.h"
bbf6f052 58
bbf6f052 59/* Decide whether a function's arguments should be processed
bbc8a071
RK
60 from first to last or from last to first.
61
62 They should if the stack and args grow in opposite directions, but
63 only if we have push insns. */
bbf6f052 64
bbf6f052 65#ifdef PUSH_ROUNDING
bbc8a071 66
2da4124d 67#ifndef PUSH_ARGS_REVERSED
3319a347 68#if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
3a94c984 69#define PUSH_ARGS_REVERSED /* If it's last to first. */
bbf6f052 70#endif
2da4124d 71#endif
bbc8a071 72
bbf6f052
RK
73#endif
74
75#ifndef STACK_PUSH_CODE
76#ifdef STACK_GROWS_DOWNWARD
77#define STACK_PUSH_CODE PRE_DEC
78#else
79#define STACK_PUSH_CODE PRE_INC
80#endif
81#endif
82
4ca79136 83
bbf6f052
RK
84/* If this is nonzero, we do not bother generating VOLATILE
85 around volatile memory references, and we are willing to
86 output indirect addresses. If cse is to follow, we reject
87 indirect addresses so a useful potential cse is generated;
88 if it is used only once, instruction combination will produce
89 the same indirect address eventually. */
90int cse_not_expected;
91
4969d05d
RK
92/* This structure is used by move_by_pieces to describe the move to
93 be performed. */
b0f43ca0 94struct move_by_pieces_d
4969d05d
RK
95{
96 rtx to;
97 rtx to_addr;
98 int autinc_to;
99 int explicit_inc_to;
100 rtx from;
101 rtx from_addr;
102 int autinc_from;
103 int explicit_inc_from;
3bdf5ad1
RK
104 unsigned HOST_WIDE_INT len;
105 HOST_WIDE_INT offset;
4969d05d
RK
106 int reverse;
107};
108
57814e5e 109/* This structure is used by store_by_pieces to describe the clear to
9de08200
RK
110 be performed. */
111
b0f43ca0 112struct store_by_pieces_d
9de08200
RK
113{
114 rtx to;
115 rtx to_addr;
116 int autinc_to;
117 int explicit_inc_to;
3bdf5ad1
RK
118 unsigned HOST_WIDE_INT len;
119 HOST_WIDE_INT offset;
502b8322 120 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
fad205ff 121 void *constfundata;
9de08200
RK
122 int reverse;
123};
124
502b8322 125static void move_by_pieces_1 (rtx (*) (rtx, ...), enum machine_mode,
b0f43ca0 126 struct move_by_pieces_d *);
502b8322 127static bool block_move_libcall_safe_for_call_parm (void);
079a182e 128static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT);
502b8322
AJ
129static tree emit_block_move_libcall_fn (int);
130static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
131static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
132static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
b0f43ca0 133static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
502b8322 134static void store_by_pieces_2 (rtx (*) (rtx, ...), enum machine_mode,
b0f43ca0 135 struct store_by_pieces_d *);
502b8322
AJ
136static tree clear_storage_libcall_fn (int);
137static rtx compress_float_constant (rtx, rtx);
138static rtx get_subtarget (rtx);
502b8322
AJ
139static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
140 HOST_WIDE_INT, enum machine_mode,
4862826d 141 tree, tree, int, alias_set_type);
502b8322 142static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
1169e45d
AH
143static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
144 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
145 enum machine_mode,
4862826d 146 tree, tree, alias_set_type, bool);
502b8322 147
fa233e34 148static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
502b8322 149
22ea9ec0 150static int is_aligning_offset (const_tree, const_tree);
eb698c58
RS
151static void expand_operands (tree, tree, rtx, rtx*, rtx*,
152 enum expand_modifier);
bc15d0ef 153static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
8e7aa1f9 154static rtx do_store_flag (sepops, rtx, enum machine_mode);
21d93687 155#ifdef PUSH_ROUNDING
502b8322 156static void emit_single_push_insn (enum machine_mode, rtx, tree);
21d93687 157#endif
502b8322
AJ
158static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx);
159static rtx const_vector_from_tree (tree);
57aaef66 160static void write_complex_part (rtx, rtx, bool);
bbf6f052 161
fbe1758d 162/* This macro is used to determine whether move_by_pieces should be called
3a94c984 163 to perform a structure copy. */
fbe1758d 164#ifndef MOVE_BY_PIECES_P
19caa751 165#define MOVE_BY_PIECES_P(SIZE, ALIGN) \
45d78e7f 166 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
e04ad03d 167 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
fbe1758d
AM
168#endif
169
78762e3b
RS
170/* This macro is used to determine whether clear_by_pieces should be
171 called to clear storage. */
172#ifndef CLEAR_BY_PIECES_P
173#define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
45d78e7f 174 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
e04ad03d 175 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
78762e3b
RS
176#endif
177
4977bab6 178/* This macro is used to determine whether store_by_pieces should be
cfa31150
SL
179 called to "memset" storage with byte values other than zero. */
180#ifndef SET_BY_PIECES_P
181#define SET_BY_PIECES_P(SIZE, ALIGN) \
182 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
e04ad03d 183 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
cfa31150
SL
184#endif
185
186/* This macro is used to determine whether store_by_pieces should be
187 called to "memcpy" storage when the source is a constant string. */
4977bab6 188#ifndef STORE_BY_PIECES_P
45d78e7f
JJ
189#define STORE_BY_PIECES_P(SIZE, ALIGN) \
190 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
e04ad03d 191 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
4977bab6
ZW
192#endif
193
cc2902df 194/* SLOW_UNALIGNED_ACCESS is nonzero if unaligned accesses are very slow. */
e87b4f3f
RS
195
196#ifndef SLOW_UNALIGNED_ACCESS
e1565e65 197#define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
e87b4f3f 198#endif
bbf6f052 199\f
b5deb7b6
SL
200/* This is run to set up which modes can be used
201 directly in memory and to initialize the block move optab. It is run
202 at the beginning of compilation and when the target is reinitialized. */
4fa52007
RK
203
204void
b5deb7b6 205init_expr_target (void)
4fa52007
RK
206{
207 rtx insn, pat;
208 enum machine_mode mode;
cff48d8f 209 int num_clobbers;
9ec36da5 210 rtx mem, mem1;
bf1660a6 211 rtx reg;
9ec36da5 212
e2549997
RS
213 /* Try indexing by frame ptr and try by stack ptr.
214 It is known that on the Convex the stack ptr isn't a valid index.
215 With luck, one or the other is valid on any machine. */
9ec36da5
JL
216 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
217 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
4fa52007 218
bf1660a6
JL
219 /* A scratch register we can modify in-place below to avoid
220 useless RTL allocations. */
221 reg = gen_rtx_REG (VOIDmode, -1);
222
1f8c3c5b 223 insn = rtx_alloc (INSN);
bbbbb16a 224 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
1f8c3c5b 225 PATTERN (insn) = pat;
4fa52007
RK
226
227 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
228 mode = (enum machine_mode) ((int) mode + 1))
229 {
230 int regno;
4fa52007
RK
231
232 direct_load[(int) mode] = direct_store[(int) mode] = 0;
233 PUT_MODE (mem, mode);
e2549997 234 PUT_MODE (mem1, mode);
bf1660a6 235 PUT_MODE (reg, mode);
4fa52007 236
e6fe56a4
RK
237 /* See if there is some register that can be used in this mode and
238 directly loaded or stored from memory. */
239
7308a047
RS
240 if (mode != VOIDmode && mode != BLKmode)
241 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
242 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
243 regno++)
244 {
245 if (! HARD_REGNO_MODE_OK (regno, mode))
246 continue;
e6fe56a4 247
6fb5fa3c 248 SET_REGNO (reg, regno);
e6fe56a4 249
7308a047
RS
250 SET_SRC (pat) = mem;
251 SET_DEST (pat) = reg;
252 if (recog (pat, insn, &num_clobbers) >= 0)
253 direct_load[(int) mode] = 1;
e6fe56a4 254
e2549997
RS
255 SET_SRC (pat) = mem1;
256 SET_DEST (pat) = reg;
257 if (recog (pat, insn, &num_clobbers) >= 0)
258 direct_load[(int) mode] = 1;
259
7308a047
RS
260 SET_SRC (pat) = reg;
261 SET_DEST (pat) = mem;
262 if (recog (pat, insn, &num_clobbers) >= 0)
263 direct_store[(int) mode] = 1;
e2549997
RS
264
265 SET_SRC (pat) = reg;
266 SET_DEST (pat) = mem1;
267 if (recog (pat, insn, &num_clobbers) >= 0)
268 direct_store[(int) mode] = 1;
7308a047 269 }
4fa52007
RK
270 }
271
51286de6
RH
272 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
273
274 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
275 mode = GET_MODE_WIDER_MODE (mode))
276 {
277 enum machine_mode srcmode;
278 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
0fb7aeda 279 srcmode = GET_MODE_WIDER_MODE (srcmode))
51286de6
RH
280 {
281 enum insn_code ic;
282
283 ic = can_extend_p (mode, srcmode, 0);
284 if (ic == CODE_FOR_nothing)
285 continue;
286
287 PUT_MODE (mem, srcmode);
0fb7aeda 288
2ef6ce06 289 if (insn_operand_matches (ic, 1, mem))
51286de6
RH
290 float_extend_from_mem[mode][srcmode] = true;
291 }
292 }
4fa52007 293}
cff48d8f 294
bbf6f052
RK
295/* This is run at the start of compiling a function. */
296
297void
502b8322 298init_expr (void)
bbf6f052 299{
3e029763 300 memset (&crtl->expr, 0, sizeof (crtl->expr));
bbf6f052 301}
bbf6f052
RK
302\f
303/* Copy data from FROM to TO, where the machine modes are not the same.
0f996086
CF
304 Both modes may be integer, or both may be floating, or both may be
305 fixed-point.
bbf6f052
RK
306 UNSIGNEDP should be nonzero if FROM is an unsigned type.
307 This causes zero-extension instead of sign-extension. */
308
309void
502b8322 310convert_move (rtx to, rtx from, int unsignedp)
bbf6f052
RK
311{
312 enum machine_mode to_mode = GET_MODE (to);
313 enum machine_mode from_mode = GET_MODE (from);
3d8bf70f
BE
314 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
315 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
bbf6f052
RK
316 enum insn_code code;
317 rtx libcall;
318
319 /* rtx code for making an equivalent value. */
37d0b254
SE
320 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
321 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
bbf6f052 322
bbf6f052 323
5b0264cb 324 gcc_assert (to_real == from_real);
ed1223ba
EC
325 gcc_assert (to_mode != BLKmode);
326 gcc_assert (from_mode != BLKmode);
bbf6f052 327
6de9cd9a
DN
328 /* If the source and destination are already the same, then there's
329 nothing to do. */
330 if (to == from)
331 return;
332
1499e0a8
RK
333 /* If FROM is a SUBREG that indicates that we have already done at least
334 the required extension, strip it. We don't handle such SUBREGs as
335 TO here. */
336
337 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
69660a70
BS
338 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
339 >= GET_MODE_PRECISION (to_mode))
1499e0a8
RK
340 && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
341 from = gen_lowpart (to_mode, from), from_mode = to_mode;
342
5b0264cb 343 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
1499e0a8 344
bbf6f052
RK
345 if (to_mode == from_mode
346 || (from_mode == VOIDmode && CONSTANT_P (from)))
347 {
348 emit_move_insn (to, from);
349 return;
350 }
351
0b4565c9
BS
352 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
353 {
5b0264cb 354 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
3a94c984 355
0b4565c9 356 if (VECTOR_MODE_P (to_mode))
bafe341a 357 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
0b4565c9 358 else
bafe341a 359 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
0b4565c9
BS
360
361 emit_move_insn (to, from);
362 return;
363 }
364
06765df1
R
365 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
366 {
367 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
368 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
369 return;
370 }
371
bbf6f052
RK
372 if (to_real)
373 {
642dfa8b 374 rtx value, insns;
85363ca0 375 convert_optab tab;
81d79e2c 376
15ed7b52
JG
377 gcc_assert ((GET_MODE_PRECISION (from_mode)
378 != GET_MODE_PRECISION (to_mode))
379 || (DECIMAL_FLOAT_MODE_P (from_mode)
380 != DECIMAL_FLOAT_MODE_P (to_mode)));
ed1223ba 381
15ed7b52
JG
382 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
383 /* Conversion between decimal float and binary float, same size. */
384 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
385 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
85363ca0 386 tab = sext_optab;
85363ca0 387 else
5b0264cb 388 tab = trunc_optab;
2b01c326 389
85363ca0 390 /* Try converting directly if the insn is supported. */
2b01c326 391
947131ba 392 code = convert_optab_handler (tab, to_mode, from_mode);
85363ca0 393 if (code != CODE_FOR_nothing)
b092b471 394 {
85363ca0
ZW
395 emit_unop_insn (code, to, from,
396 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
b092b471
JW
397 return;
398 }
b092b471 399
85363ca0 400 /* Otherwise use a libcall. */
8a33f100 401 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
3a94c984 402
5b0264cb
NS
403 /* Is this conversion implemented yet? */
404 gcc_assert (libcall);
bbf6f052 405
642dfa8b 406 start_sequence ();
ebb1b59a 407 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
81d79e2c 408 1, from, from_mode);
642dfa8b
BS
409 insns = get_insns ();
410 end_sequence ();
450b1728
EC
411 emit_libcall_block (insns, to, value,
412 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
413 from)
414 : gen_rtx_FLOAT_EXTEND (to_mode, from));
bbf6f052
RK
415 return;
416 }
417
85363ca0
ZW
418 /* Handle pointer conversion. */ /* SPEE 900220. */
419 /* Targets are expected to provide conversion insns between PxImode and
420 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
421 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
422 {
423 enum machine_mode full_mode
424 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
425
947131ba 426 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
5b0264cb 427 != CODE_FOR_nothing);
85363ca0
ZW
428
429 if (full_mode != from_mode)
430 from = convert_to_mode (full_mode, from, unsignedp);
947131ba 431 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
85363ca0
ZW
432 to, from, UNKNOWN);
433 return;
434 }
435 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
436 {
d2348bd5 437 rtx new_from;
85363ca0
ZW
438 enum machine_mode full_mode
439 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
e90247f8
BS
440 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
441 enum insn_code icode;
85363ca0 442
e90247f8
BS
443 icode = convert_optab_handler (ctab, full_mode, from_mode);
444 gcc_assert (icode != CODE_FOR_nothing);
85363ca0 445
85363ca0 446 if (to_mode == full_mode)
d2348bd5 447 {
e90247f8 448 emit_unop_insn (icode, to, from, UNKNOWN);
d2348bd5
DD
449 return;
450 }
451
452 new_from = gen_reg_rtx (full_mode);
e90247f8 453 emit_unop_insn (icode, new_from, from, UNKNOWN);
85363ca0 454
a1105617 455 /* else proceed to integer conversions below. */
85363ca0 456 from_mode = full_mode;
d2348bd5 457 from = new_from;
85363ca0
ZW
458 }
459
0f996086
CF
460 /* Make sure both are fixed-point modes or both are not. */
461 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
462 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
463 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
464 {
465 /* If we widen from_mode to to_mode and they are in the same class,
466 we won't saturate the result.
467 Otherwise, always saturate the result to play safe. */
468 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
469 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
470 expand_fixed_convert (to, from, 0, 0);
471 else
472 expand_fixed_convert (to, from, 0, 1);
473 return;
474 }
475
bbf6f052
RK
476 /* Now both modes are integers. */
477
478 /* Handle expanding beyond a word. */
69660a70
BS
479 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
480 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
bbf6f052
RK
481 {
482 rtx insns;
483 rtx lowpart;
484 rtx fill_value;
485 rtx lowfrom;
486 int i;
487 enum machine_mode lowpart_mode;
488 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
489
490 /* Try converting directly if the insn is supported. */
491 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
492 != CODE_FOR_nothing)
493 {
cd1b4b44
RK
494 /* If FROM is a SUBREG, put it into a register. Do this
495 so that we always generate the same set of insns for
496 better cse'ing; if an intermediate assignment occurred,
497 we won't be doing the operation directly on the SUBREG. */
498 if (optimize > 0 && GET_CODE (from) == SUBREG)
499 from = force_reg (from_mode, from);
bbf6f052
RK
500 emit_unop_insn (code, to, from, equiv_code);
501 return;
502 }
503 /* Next, try converting via full word. */
69660a70 504 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
bbf6f052
RK
505 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
506 != CODE_FOR_nothing))
507 {
2f6025a1 508 rtx word_to = gen_reg_rtx (word_mode);
f8cfc6aa 509 if (REG_P (to))
6a2d136b
EB
510 {
511 if (reg_overlap_mentioned_p (to, from))
512 from = force_reg (from_mode, from);
c41c1387 513 emit_clobber (to);
6a2d136b 514 }
2f6025a1
PB
515 convert_move (word_to, from, unsignedp);
516 emit_unop_insn (code, to, word_to, equiv_code);
bbf6f052
RK
517 return;
518 }
519
520 /* No special multiword conversion insn; do it by hand. */
521 start_sequence ();
522
ecd49d51
OH
523 /* Since we will turn this into a no conflict block, we must ensure the
524 the source does not overlap the target so force it into an isolated
525 register when maybe so. Likewise for any MEM input, since the
526 conversion sequence might require several references to it and we
527 must ensure we're getting the same value every time. */
5c5033c3 528
ecd49d51 529 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
5c5033c3
RK
530 from = force_reg (from_mode, from);
531
bbf6f052 532 /* Get a copy of FROM widened to a word, if necessary. */
69660a70 533 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
bbf6f052
RK
534 lowpart_mode = word_mode;
535 else
536 lowpart_mode = from_mode;
537
538 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
539
540 lowpart = gen_lowpart (lowpart_mode, to);
541 emit_move_insn (lowpart, lowfrom);
542
543 /* Compute the value to put in each remaining word. */
544 if (unsignedp)
545 fill_value = const0_rtx;
546 else
9a53bc17
PB
547 fill_value = emit_store_flag (gen_reg_rtx (word_mode),
548 LT, lowfrom, const0_rtx,
549 VOIDmode, 0, -1);
bbf6f052
RK
550
551 /* Fill the remaining words. */
552 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
553 {
554 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
555 rtx subword = operand_subword (to, index, 1, to_mode);
556
5b0264cb 557 gcc_assert (subword);
bbf6f052
RK
558
559 if (fill_value != subword)
560 emit_move_insn (subword, fill_value);
561 }
562
563 insns = get_insns ();
564 end_sequence ();
565
d70dcf29 566 emit_insn (insns);
bbf6f052
RK
567 return;
568 }
569
d3c64ee3 570 /* Truncating multi-word to a word or less. */
69660a70
BS
571 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
572 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
bbf6f052 573 {
3c0cb5de 574 if (!((MEM_P (from)
431a6eca
JW
575 && ! MEM_VOLATILE_P (from)
576 && direct_load[(int) to_mode]
577 && ! mode_dependent_address_p (XEXP (from, 0)))
f8cfc6aa 578 || REG_P (from)
431a6eca
JW
579 || GET_CODE (from) == SUBREG))
580 from = force_reg (from_mode, from);
bbf6f052
RK
581 convert_move (to, gen_lowpart (word_mode, from), 0);
582 return;
583 }
584
bbf6f052
RK
585 /* Now follow all the conversions between integers
586 no more than a word long. */
587
588 /* For truncation, usually we can just refer to FROM in a narrower mode. */
589 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
d0edd768 590 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
bbf6f052 591 {
3c0cb5de 592 if (!((MEM_P (from)
d3c64ee3
RS
593 && ! MEM_VOLATILE_P (from)
594 && direct_load[(int) to_mode]
595 && ! mode_dependent_address_p (XEXP (from, 0)))
f8cfc6aa 596 || REG_P (from)
d3c64ee3
RS
597 || GET_CODE (from) == SUBREG))
598 from = force_reg (from_mode, from);
f8cfc6aa 599 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
34aa3599
RK
600 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
601 from = copy_to_reg (from);
bbf6f052
RK
602 emit_move_insn (to, gen_lowpart (to_mode, from));
603 return;
604 }
605
d3c64ee3 606 /* Handle extension. */
69660a70 607 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
bbf6f052
RK
608 {
609 /* Convert directly if that works. */
610 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
611 != CODE_FOR_nothing)
612 {
613 emit_unop_insn (code, to, from, equiv_code);
614 return;
615 }
616 else
617 {
618 enum machine_mode intermediate;
2b28d92e 619 rtx tmp;
eb6c3df1 620 int shift_amount;
bbf6f052
RK
621
622 /* Search for a mode to convert via. */
623 for (intermediate = from_mode; intermediate != VOIDmode;
624 intermediate = GET_MODE_WIDER_MODE (intermediate))
930b4e39
RK
625 if (((can_extend_p (to_mode, intermediate, unsignedp)
626 != CODE_FOR_nothing)
627 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
d0edd768 628 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
bbf6f052
RK
629 && (can_extend_p (intermediate, from_mode, unsignedp)
630 != CODE_FOR_nothing))
631 {
632 convert_move (to, convert_to_mode (intermediate, from,
633 unsignedp), unsignedp);
634 return;
635 }
636
2b28d92e 637 /* No suitable intermediate mode.
3a94c984 638 Generate what we need with shifts. */
69660a70
BS
639 shift_amount = (GET_MODE_PRECISION (to_mode)
640 - GET_MODE_PRECISION (from_mode));
2b28d92e
NC
641 from = gen_lowpart (to_mode, force_reg (from_mode, from));
642 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
643 to, unsignedp);
3a94c984 644 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
2b28d92e
NC
645 to, unsignedp);
646 if (tmp != to)
647 emit_move_insn (to, tmp);
648 return;
bbf6f052
RK
649 }
650 }
651
3a94c984 652 /* Support special truncate insns for certain modes. */
947131ba
RS
653 if (convert_optab_handler (trunc_optab, to_mode,
654 from_mode) != CODE_FOR_nothing)
bbf6f052 655 {
947131ba 656 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
85363ca0 657 to, from, UNKNOWN);
b9bcad65
RK
658 return;
659 }
660
bbf6f052
RK
661 /* Handle truncation of volatile memrefs, and so on;
662 the things that couldn't be truncated directly,
85363ca0
ZW
663 and for which there was no special instruction.
664
665 ??? Code above formerly short-circuited this, for most integer
666 mode pairs, with a force_reg in from_mode followed by a recursive
667 call to this routine. Appears always to have been wrong. */
69660a70 668 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
bbf6f052
RK
669 {
670 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
671 emit_move_insn (to, temp);
672 return;
673 }
674
675 /* Mode combination is not recognized. */
5b0264cb 676 gcc_unreachable ();
bbf6f052
RK
677}
678
679/* Return an rtx for a value that would result
680 from converting X to mode MODE.
681 Both X and MODE may be floating, or both integer.
682 UNSIGNEDP is nonzero if X is an unsigned value.
683 This can be done by referring to a part of X in place
ad76cef8 684 or by copying to a new temporary with conversion. */
bbf6f052
RK
685
686rtx
502b8322 687convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
5ffe63ed
RS
688{
689 return convert_modes (mode, VOIDmode, x, unsignedp);
690}
691
692/* Return an rtx for a value that would result
693 from converting X from mode OLDMODE to mode MODE.
694 Both modes may be floating, or both integer.
695 UNSIGNEDP is nonzero if X is an unsigned value.
696
697 This can be done by referring to a part of X in place
698 or by copying to a new temporary with conversion.
699
ad76cef8 700 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
5ffe63ed
RS
701
702rtx
502b8322 703convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
bbf6f052 704{
b3694847 705 rtx temp;
5ffe63ed 706
1499e0a8
RK
707 /* If FROM is a SUBREG that indicates that we have already done at least
708 the required extension, strip it. */
709
710 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
711 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
712 && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
713 x = gen_lowpart (mode, x);
bbf6f052 714
64791b18
RK
715 if (GET_MODE (x) != VOIDmode)
716 oldmode = GET_MODE (x);
3a94c984 717
3a3d54f2
UB
718 if (mode == oldmode)
719 return x;
720
bbf6f052 721 /* There is one case that we must handle specially: If we are converting
906c4e36 722 a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
bbf6f052
RK
723 we are to interpret the constant as unsigned, gen_lowpart will do
724 the wrong if the constant appears negative. What we want to do is
725 make the high-order word of the constant zero, not all ones. */
726
727 if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
49ab6098 728 && GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT
481683e1 729 && CONST_INT_P (x) && INTVAL (x) < 0)
96ff8a16 730 {
54fb1ae0 731 double_int val = uhwi_to_double_int (INTVAL (x));
96ff8a16 732
54fb1ae0
AS
733 /* We need to zero extend VAL. */
734 if (oldmode != VOIDmode)
735 val = double_int_zext (val, GET_MODE_BITSIZE (oldmode));
96ff8a16 736
54fb1ae0 737 return immed_double_int_const (val, mode);
96ff8a16 738 }
bbf6f052
RK
739
740 /* We can do this with a gen_lowpart if both desired and current modes
741 are integer, and this is either a constant integer, a register, or a
ba2e110c
RK
742 non-volatile MEM. Except for the constant case where MODE is no
743 wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand. */
bbf6f052 744
481683e1 745 if ((CONST_INT_P (x)
69660a70 746 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT)
bbf6f052 747 || (GET_MODE_CLASS (mode) == MODE_INT
5ffe63ed 748 && GET_MODE_CLASS (oldmode) == MODE_INT
bbf6f052 749 && (GET_CODE (x) == CONST_DOUBLE
69660a70 750 || (GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
3c0cb5de 751 && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
d57c66da 752 && direct_load[(int) mode])
f8cfc6aa 753 || (REG_P (x)
006c9f4a
SE
754 && (! HARD_REGISTER_P (x)
755 || HARD_REGNO_MODE_OK (REGNO (x), mode))
d0edd768
BS
756 && TRULY_NOOP_TRUNCATION_MODES_P (mode,
757 GET_MODE (x))))))))
ba2e110c
RK
758 {
759 /* ?? If we don't know OLDMODE, we have to assume here that
760 X does not need sign- or zero-extension. This may not be
761 the case, but it's the best we can do. */
481683e1 762 if (CONST_INT_P (x) && oldmode != VOIDmode
69660a70 763 && GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (oldmode))
ba2e110c
RK
764 {
765 HOST_WIDE_INT val = INTVAL (x);
ba2e110c
RK
766
767 /* We must sign or zero-extend in this case. Start by
768 zero-extending, then sign extend if we need to. */
2d0c270f 769 val &= GET_MODE_MASK (oldmode);
ba2e110c 770 if (! unsignedp
2d0c270f
BS
771 && val_signbit_known_set_p (oldmode, val))
772 val |= ~GET_MODE_MASK (oldmode);
ba2e110c 773
2496c7bd 774 return gen_int_mode (val, mode);
ba2e110c
RK
775 }
776
777 return gen_lowpart (mode, x);
778 }
bbf6f052 779
ebe75517
JH
780 /* Converting from integer constant into mode is always equivalent to an
781 subreg operation. */
782 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
783 {
5b0264cb 784 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
ebe75517
JH
785 return simplify_gen_subreg (mode, x, oldmode, 0);
786 }
787
bbf6f052
RK
788 temp = gen_reg_rtx (mode);
789 convert_move (temp, x, unsignedp);
790 return temp;
791}
792\f
d824aea2
NF
793/* Return the largest alignment we can use for doing a move (or store)
794 of MAX_PIECES. ALIGN is the largest alignment we could use. */
795
796static unsigned int
797alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
798{
799 enum machine_mode tmode;
800
801 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
802 if (align >= GET_MODE_ALIGNMENT (tmode))
803 align = GET_MODE_ALIGNMENT (tmode);
804 else
805 {
806 enum machine_mode tmode, xmode;
807
808 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
809 tmode != VOIDmode;
810 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
811 if (GET_MODE_SIZE (tmode) > max_pieces
812 || SLOW_UNALIGNED_ACCESS (tmode, align))
813 break;
814
815 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
816 }
817
818 return align;
819}
820
821/* Return the widest integer mode no wider than SIZE. If no such mode
822 can be found, return VOIDmode. */
823
824static enum machine_mode
825widest_int_mode_for_size (unsigned int size)
826{
827 enum machine_mode tmode, mode = VOIDmode;
828
829 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
830 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
831 if (GET_MODE_SIZE (tmode) < size)
832 mode = tmode;
833
834 return mode;
835}
836
cf5124f6
RS
837/* STORE_MAX_PIECES is the number of bytes at a time that we can
838 store efficiently. Due to internal GCC limitations, this is
839 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
840 for an immediate constant. */
841
842#define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
843
8fd3cf4e
JJ
844/* Determine whether the LEN bytes can be moved by using several move
845 instructions. Return nonzero if a call to move_by_pieces should
846 succeed. */
847
848int
502b8322
AJ
849can_move_by_pieces (unsigned HOST_WIDE_INT len,
850 unsigned int align ATTRIBUTE_UNUSED)
8fd3cf4e
JJ
851{
852 return MOVE_BY_PIECES_P (len, align);
853}
854
21d93687 855/* Generate several move instructions to copy LEN bytes from block FROM to
ad76cef8 856 block TO. (These are MEM rtx's with BLKmode).
566aa174 857
21d93687
RK
858 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
859 used to push FROM to the stack.
566aa174 860
8fd3cf4e 861 ALIGN is maximum stack alignment we can assume.
bbf6f052 862
8fd3cf4e
JJ
863 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
864 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
865 stpcpy. */
866
867rtx
502b8322
AJ
868move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
869 unsigned int align, int endp)
bbf6f052 870{
b0f43ca0 871 struct move_by_pieces_d data;
372d6395
RS
872 enum machine_mode to_addr_mode;
873 enum machine_mode from_addr_mode = get_address_mode (from);
566aa174 874 rtx to_addr, from_addr = XEXP (from, 0);
770ae6cc 875 unsigned int max_size = MOVE_MAX_PIECES + 1;
fbe1758d 876 enum insn_code icode;
bbf6f052 877
f26aca6d
DD
878 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
879
bbf6f052 880 data.offset = 0;
bbf6f052 881 data.from_addr = from_addr;
566aa174
JH
882 if (to)
883 {
372d6395 884 to_addr_mode = get_address_mode (to);
566aa174
JH
885 to_addr = XEXP (to, 0);
886 data.to = to;
887 data.autinc_to
888 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
889 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
890 data.reverse
891 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
892 }
893 else
894 {
d4ebfa65 895 to_addr_mode = VOIDmode;
566aa174
JH
896 to_addr = NULL_RTX;
897 data.to = NULL_RTX;
898 data.autinc_to = 1;
899#ifdef STACK_GROWS_DOWNWARD
900 data.reverse = 1;
901#else
902 data.reverse = 0;
903#endif
904 }
905 data.to_addr = to_addr;
bbf6f052 906 data.from = from;
bbf6f052
RK
907 data.autinc_from
908 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
909 || GET_CODE (from_addr) == POST_INC
910 || GET_CODE (from_addr) == POST_DEC);
911
912 data.explicit_inc_from = 0;
913 data.explicit_inc_to = 0;
bbf6f052
RK
914 if (data.reverse) data.offset = len;
915 data.len = len;
916
917 /* If copying requires more than two move insns,
918 copy addresses to registers (to make displacements shorter)
919 and use post-increment if available. */
920 if (!(data.autinc_from && data.autinc_to)
45d78e7f 921 && move_by_pieces_ninsns (len, align, max_size) > 2)
bbf6f052 922 {
d824aea2
NF
923 /* Find the mode of the largest move...
924 MODE might not be used depending on the definitions of the
925 USE_* macros below. */
926 enum machine_mode mode ATTRIBUTE_UNUSED
927 = widest_int_mode_for_size (max_size);
fbe1758d
AM
928
929 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
bbf6f052 930 {
d4ebfa65 931 data.from_addr = copy_to_mode_reg (from_addr_mode,
0a81f074
RS
932 plus_constant (from_addr_mode,
933 from_addr, len));
bbf6f052
RK
934 data.autinc_from = 1;
935 data.explicit_inc_from = -1;
936 }
fbe1758d 937 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
bbf6f052 938 {
d4ebfa65 939 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
bbf6f052
RK
940 data.autinc_from = 1;
941 data.explicit_inc_from = 1;
942 }
bbf6f052 943 if (!data.autinc_from && CONSTANT_P (from_addr))
d4ebfa65 944 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
fbe1758d 945 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
bbf6f052 946 {
d4ebfa65 947 data.to_addr = copy_to_mode_reg (to_addr_mode,
0a81f074
RS
948 plus_constant (to_addr_mode,
949 to_addr, len));
bbf6f052
RK
950 data.autinc_to = 1;
951 data.explicit_inc_to = -1;
952 }
fbe1758d 953 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
bbf6f052 954 {
d4ebfa65 955 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
bbf6f052
RK
956 data.autinc_to = 1;
957 data.explicit_inc_to = 1;
958 }
bbf6f052 959 if (!data.autinc_to && CONSTANT_P (to_addr))
d4ebfa65 960 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
bbf6f052
RK
961 }
962
d824aea2 963 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
bbf6f052
RK
964
965 /* First move what we can in the largest integer mode, then go to
966 successively smaller modes. */
967
968 while (max_size > 1)
969 {
d824aea2 970 enum machine_mode mode = widest_int_mode_for_size (max_size);
bbf6f052
RK
971
972 if (mode == VOIDmode)
973 break;
974
947131ba 975 icode = optab_handler (mov_optab, mode);
19caa751 976 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
bbf6f052
RK
977 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
978
979 max_size = GET_MODE_SIZE (mode);
980 }
981
982 /* The code above should have handled everything. */
5b0264cb 983 gcc_assert (!data.len);
8fd3cf4e
JJ
984
985 if (endp)
986 {
987 rtx to1;
988
5b0264cb 989 gcc_assert (!data.reverse);
8fd3cf4e
JJ
990 if (data.autinc_to)
991 {
992 if (endp == 2)
993 {
994 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
995 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
996 else
d4ebfa65 997 data.to_addr = copy_to_mode_reg (to_addr_mode,
0a81f074
RS
998 plus_constant (to_addr_mode,
999 data.to_addr,
8fd3cf4e
JJ
1000 -1));
1001 }
1002 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
1003 data.offset);
1004 }
1005 else
1006 {
1007 if (endp == 2)
1008 --data.offset;
1009 to1 = adjust_address (data.to, QImode, data.offset);
1010 }
1011 return to1;
1012 }
1013 else
1014 return data.to;
bbf6f052
RK
1015}
1016
1017/* Return number of insns required to move L bytes by pieces.
f1eaaf73 1018 ALIGN (in bits) is maximum alignment we can assume. */
bbf6f052 1019
0d8f5d62 1020unsigned HOST_WIDE_INT
45d78e7f
JJ
1021move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1022 unsigned int max_size)
bbf6f052 1023{
3bdf5ad1 1024 unsigned HOST_WIDE_INT n_insns = 0;
bbf6f052 1025
d824aea2 1026 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
bbf6f052
RK
1027
1028 while (max_size > 1)
1029 {
d824aea2 1030 enum machine_mode mode;
bbf6f052
RK
1031 enum insn_code icode;
1032
d824aea2 1033 mode = widest_int_mode_for_size (max_size);
bbf6f052
RK
1034
1035 if (mode == VOIDmode)
1036 break;
1037
947131ba 1038 icode = optab_handler (mov_optab, mode);
19caa751 1039 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
bbf6f052
RK
1040 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1041
1042 max_size = GET_MODE_SIZE (mode);
1043 }
1044
5b0264cb 1045 gcc_assert (!l);
bbf6f052
RK
1046 return n_insns;
1047}
1048
1049/* Subroutine of move_by_pieces. Move as many bytes as appropriate
1050 with move instructions for mode MODE. GENFUN is the gen_... function
1051 to make a move insn for that mode. DATA has all the other info. */
1052
1053static void
502b8322 1054move_by_pieces_1 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
b0f43ca0 1055 struct move_by_pieces_d *data)
bbf6f052 1056{
3bdf5ad1 1057 unsigned int size = GET_MODE_SIZE (mode);
ae0ed63a 1058 rtx to1 = NULL_RTX, from1;
bbf6f052
RK
1059
1060 while (data->len >= size)
1061 {
3bdf5ad1
RK
1062 if (data->reverse)
1063 data->offset -= size;
1064
566aa174 1065 if (data->to)
3bdf5ad1 1066 {
566aa174 1067 if (data->autinc_to)
630036c6
JJ
1068 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1069 data->offset);
566aa174 1070 else
f4ef873c 1071 to1 = adjust_address (data->to, mode, data->offset);
3bdf5ad1 1072 }
3bdf5ad1
RK
1073
1074 if (data->autinc_from)
630036c6
JJ
1075 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1076 data->offset);
3bdf5ad1 1077 else
f4ef873c 1078 from1 = adjust_address (data->from, mode, data->offset);
bbf6f052 1079
940da324 1080 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
3d709fd3
RH
1081 emit_insn (gen_add2_insn (data->to_addr,
1082 GEN_INT (-(HOST_WIDE_INT)size)));
940da324 1083 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
3d709fd3
RH
1084 emit_insn (gen_add2_insn (data->from_addr,
1085 GEN_INT (-(HOST_WIDE_INT)size)));
bbf6f052 1086
566aa174
JH
1087 if (data->to)
1088 emit_insn ((*genfun) (to1, from1));
1089 else
21d93687
RK
1090 {
1091#ifdef PUSH_ROUNDING
1092 emit_single_push_insn (mode, from1, NULL);
1093#else
5b0264cb 1094 gcc_unreachable ();
21d93687
RK
1095#endif
1096 }
3bdf5ad1 1097
940da324 1098 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
906c4e36 1099 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
940da324 1100 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
906c4e36 1101 emit_insn (gen_add2_insn (data->from_addr, GEN_INT (size)));
bbf6f052 1102
3bdf5ad1
RK
1103 if (! data->reverse)
1104 data->offset += size;
bbf6f052
RK
1105
1106 data->len -= size;
1107 }
1108}
1109\f
4ca79136
RH
1110/* Emit code to move a block Y to a block X. This may be done with
1111 string-move instructions, with multiple scalar move instructions,
1112 or with a library call.
bbf6f052 1113
4ca79136 1114 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
bbf6f052 1115 SIZE is an rtx that says how long they are.
19caa751 1116 ALIGN is the maximum alignment we can assume they have.
44bb111a 1117 METHOD describes what kind of copy this is, and what mechanisms may be used.
bbf6f052 1118
e9a25f70
JL
1119 Return the address of the new block, if memcpy is called and returns it,
1120 0 otherwise. */
1121
1122rtx
079a182e
JH
1123emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1124 unsigned int expected_align, HOST_WIDE_INT expected_size)
bbf6f052 1125{
44bb111a 1126 bool may_use_call;
e9a25f70 1127 rtx retval = 0;
44bb111a
RH
1128 unsigned int align;
1129
8b4c365c
RG
1130 gcc_assert (size);
1131 if (CONST_INT_P (size)
1132 && INTVAL (size) == 0)
1133 return 0;
1134
44bb111a
RH
1135 switch (method)
1136 {
1137 case BLOCK_OP_NORMAL:
8148fe65 1138 case BLOCK_OP_TAILCALL:
44bb111a
RH
1139 may_use_call = true;
1140 break;
1141
1142 case BLOCK_OP_CALL_PARM:
1143 may_use_call = block_move_libcall_safe_for_call_parm ();
1144
1145 /* Make inhibit_defer_pop nonzero around the library call
1146 to force it to pop the arguments right away. */
1147 NO_DEFER_POP;
1148 break;
1149
1150 case BLOCK_OP_NO_LIBCALL:
1151 may_use_call = false;
1152 break;
1153
1154 default:
5b0264cb 1155 gcc_unreachable ();
44bb111a
RH
1156 }
1157
8b4c365c 1158 gcc_assert (MEM_P (x) && MEM_P (y));
44bb111a 1159 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
e100f395 1160 gcc_assert (align >= BITS_PER_UNIT);
e9a25f70 1161
82c82743
RH
1162 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1163 block copy is more efficient for other large modes, e.g. DCmode. */
1164 x = adjust_address (x, BLKmode, 0);
1165 y = adjust_address (y, BLKmode, 0);
1166
cb38fd88
RH
1167 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1168 can be incorrect is coming from __builtin_memcpy. */
481683e1 1169 if (CONST_INT_P (size))
cb38fd88
RH
1170 {
1171 x = shallow_copy_rtx (x);
1172 y = shallow_copy_rtx (y);
f5541398
RS
1173 set_mem_size (x, INTVAL (size));
1174 set_mem_size (y, INTVAL (size));
cb38fd88
RH
1175 }
1176
481683e1 1177 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
8fd3cf4e 1178 move_by_pieces (x, y, INTVAL (size), align, 0);
079a182e
JH
1179 else if (emit_block_move_via_movmem (x, y, size, align,
1180 expected_align, expected_size))
4ca79136 1181 ;
09e881c9
BE
1182 else if (may_use_call
1183 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1184 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
c4b9a87e
ER
1185 {
1186 /* Since x and y are passed to a libcall, mark the corresponding
1187 tree EXPR as addressable. */
1188 tree y_expr = MEM_EXPR (y);
1189 tree x_expr = MEM_EXPR (x);
1190 if (y_expr)
1191 mark_addressable (y_expr);
1192 if (x_expr)
1193 mark_addressable (x_expr);
1194 retval = emit_block_move_via_libcall (x, y, size,
1195 method == BLOCK_OP_TAILCALL);
1196 }
1197
44bb111a
RH
1198 else
1199 emit_block_move_via_loop (x, y, size, align);
1200
1201 if (method == BLOCK_OP_CALL_PARM)
1202 OK_DEFER_POP;
266007a7 1203
4ca79136
RH
1204 return retval;
1205}
266007a7 1206
079a182e
JH
1207rtx
1208emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1209{
1210 return emit_block_move_hints (x, y, size, method, 0, -1);
1211}
1212
502b8322 1213/* A subroutine of emit_block_move. Returns true if calling the
44bb111a
RH
1214 block move libcall will not clobber any parameters which may have
1215 already been placed on the stack. */
1216
1217static bool
502b8322 1218block_move_libcall_safe_for_call_parm (void)
44bb111a 1219{
81464b2c
KT
1220#if defined (REG_PARM_STACK_SPACE)
1221 tree fn;
1222#endif
1223
a357a6d4 1224 /* If arguments are pushed on the stack, then they're safe. */
44bb111a
RH
1225 if (PUSH_ARGS)
1226 return true;
44bb111a 1227
450b1728 1228 /* If registers go on the stack anyway, any argument is sure to clobber
a357a6d4 1229 an outgoing argument. */
ac294f0b 1230#if defined (REG_PARM_STACK_SPACE)
81464b2c 1231 fn = emit_block_move_libcall_fn (false);
5a905a2b
JJ
1232 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1233 depend on its argument. */
1234 (void) fn;
81464b2c
KT
1235 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1236 && REG_PARM_STACK_SPACE (fn) != 0)
1237 return false;
44bb111a 1238#endif
44bb111a 1239
a357a6d4
GK
1240 /* If any argument goes in memory, then it might clobber an outgoing
1241 argument. */
1242 {
d5cc9181
JR
1243 CUMULATIVE_ARGS args_so_far_v;
1244 cumulative_args_t args_so_far;
a357a6d4 1245 tree fn, arg;
450b1728 1246
a357a6d4 1247 fn = emit_block_move_libcall_fn (false);
d5cc9181
JR
1248 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1249 args_so_far = pack_cumulative_args (&args_so_far_v);
450b1728 1250
a357a6d4
GK
1251 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1252 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1253 {
1254 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
d5cc9181 1255 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
3c07301f 1256 NULL_TREE, true);
a357a6d4 1257 if (!tmp || !REG_P (tmp))
44bb111a 1258 return false;
d5cc9181 1259 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
a357a6d4 1260 return false;
d5cc9181 1261 targetm.calls.function_arg_advance (args_so_far, mode,
3c07301f 1262 NULL_TREE, true);
a357a6d4
GK
1263 }
1264 }
1265 return true;
44bb111a
RH
1266}
1267
70128ad9 1268/* A subroutine of emit_block_move. Expand a movmem pattern;
4ca79136 1269 return true if successful. */
3ef1eef4 1270
4ca79136 1271static bool
079a182e
JH
1272emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1273 unsigned int expected_align, HOST_WIDE_INT expected_size)
4ca79136 1274{
a5e9c810 1275 int save_volatile_ok = volatile_ok;
4ca79136 1276 enum machine_mode mode;
266007a7 1277
079a182e
JH
1278 if (expected_align < align)
1279 expected_align = align;
1280
4ca79136
RH
1281 /* Since this is a move insn, we don't care about volatility. */
1282 volatile_ok = 1;
1283
ee960939
OH
1284 /* Try the most limited insn first, because there's no point
1285 including more than one in the machine description unless
1286 the more limited one has some advantage. */
1287
4ca79136
RH
1288 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1289 mode = GET_MODE_WIDER_MODE (mode))
1290 {
f9621cc4 1291 enum insn_code code = direct_optab_handler (movmem_optab, mode);
4ca79136
RH
1292
1293 if (code != CODE_FOR_nothing
1294 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1295 here because if SIZE is less than the mode mask, as it is
1296 returned by the macro, it will definitely be less than the
1297 actual mode mask. */
481683e1 1298 && ((CONST_INT_P (size)
4ca79136
RH
1299 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1300 <= (GET_MODE_MASK (mode) >> 1)))
a5c7d693
RS
1301 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
1302 {
1303 struct expand_operand ops[6];
1304 unsigned int nops;
4ca79136
RH
1305
1306 /* ??? When called via emit_block_move_for_call, it'd be
1307 nice if there were some way to inform the backend, so
1308 that it doesn't fail the expansion because it thinks
1309 emitting the libcall would be more efficient. */
f04713ee
RS
1310 nops = insn_data[(int) code].n_generator_args;
1311 gcc_assert (nops == 4 || nops == 6);
c2654ded 1312
a5c7d693
RS
1313 create_fixed_operand (&ops[0], x);
1314 create_fixed_operand (&ops[1], y);
1315 /* The check above guarantees that this size conversion is valid. */
1316 create_convert_operand_to (&ops[2], size, mode, true);
1317 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
c2654ded 1318 if (nops == 6)
a5c7d693
RS
1319 {
1320 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1321 create_integer_operand (&ops[5], expected_size);
a5c7d693
RS
1322 }
1323 if (maybe_expand_insn (code, nops, ops))
4ca79136 1324 {
a5e9c810 1325 volatile_ok = save_volatile_ok;
4ca79136 1326 return true;
bbf6f052
RK
1327 }
1328 }
4ca79136 1329 }
bbf6f052 1330
a5e9c810 1331 volatile_ok = save_volatile_ok;
4ca79136
RH
1332 return false;
1333}
3ef1eef4 1334
8f99553f 1335/* A subroutine of emit_block_move. Expand a call to memcpy.
4ca79136 1336 Return the return value from memcpy, 0 otherwise. */
4bc973ae 1337
8c996513 1338rtx
8148fe65 1339emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
4ca79136 1340{
ee960939 1341 rtx dst_addr, src_addr;
5039610b 1342 tree call_expr, fn, src_tree, dst_tree, size_tree;
4ca79136
RH
1343 enum machine_mode size_mode;
1344 rtx retval;
4bc973ae 1345
ad76cef8
PB
1346 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1347 pseudos. We can then place those new pseudos into a VAR_DECL and
1348 use them later. */
ee960939 1349
18ae1560
UB
1350 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1351 src_addr = copy_addr_to_reg (XEXP (src, 0));
4ca79136 1352
ee960939
OH
1353 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1354 src_addr = convert_memory_address (ptr_mode, src_addr);
ee960939
OH
1355
1356 dst_tree = make_tree (ptr_type_node, dst_addr);
1357 src_tree = make_tree (ptr_type_node, src_addr);
4ca79136 1358
8f99553f 1359 size_mode = TYPE_MODE (sizetype);
ee960939 1360
4ca79136
RH
1361 size = convert_to_mode (size_mode, size, 1);
1362 size = copy_to_mode_reg (size_mode, size);
1363
1364 /* It is incorrect to use the libcall calling conventions to call
1365 memcpy in this context. This could be a user call to memcpy and
1366 the user may wish to examine the return value from memcpy. For
1367 targets where libcalls and normal calls have different conventions
8f99553f 1368 for returning pointers, we could end up generating incorrect code. */
4ca79136 1369
8f99553f 1370 size_tree = make_tree (sizetype, size);
4ca79136
RH
1371
1372 fn = emit_block_move_libcall_fn (true);
5039610b 1373 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
8148fe65 1374 CALL_EXPR_TAILCALL (call_expr) = tailcall;
4ca79136 1375
84217346 1376 retval = expand_normal (call_expr);
4ca79136 1377
8f99553f 1378 return retval;
4ca79136 1379}
52cf7115 1380
4ca79136 1381/* A subroutine of emit_block_move_via_libcall. Create the tree node
bbee5843 1382 for the function we use for block copies. */
52cf7115 1383
4ca79136
RH
1384static GTY(()) tree block_move_fn;
1385
9661b15f 1386void
502b8322 1387init_block_move_fn (const char *asmspec)
4ca79136 1388{
9661b15f 1389 if (!block_move_fn)
4ca79136 1390 {
e384e6b5 1391 tree args, fn, attrs, attr_args;
9661b15f 1392
8f99553f
JM
1393 fn = get_identifier ("memcpy");
1394 args = build_function_type_list (ptr_type_node, ptr_type_node,
1395 const_ptr_type_node, sizetype,
1396 NULL_TREE);
52cf7115 1397
c2255bc4 1398 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
4ca79136
RH
1399 DECL_EXTERNAL (fn) = 1;
1400 TREE_PUBLIC (fn) = 1;
1401 DECL_ARTIFICIAL (fn) = 1;
1402 TREE_NOTHROW (fn) = 1;
5b5cba1f
JM
1403 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1404 DECL_VISIBILITY_SPECIFIED (fn) = 1;
66c60e67 1405
e384e6b5
BS
1406 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1407 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1408
1409 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1410
4ca79136 1411 block_move_fn = fn;
bbf6f052 1412 }
e9a25f70 1413
9661b15f 1414 if (asmspec)
0e6df31e 1415 set_user_assembler_name (block_move_fn, asmspec);
9661b15f
JJ
1416}
1417
1418static tree
502b8322 1419emit_block_move_libcall_fn (int for_call)
9661b15f
JJ
1420{
1421 static bool emitted_extern;
1422
1423 if (!block_move_fn)
1424 init_block_move_fn (NULL);
1425
4ca79136
RH
1426 if (for_call && !emitted_extern)
1427 {
1428 emitted_extern = true;
0e6df31e 1429 make_decl_rtl (block_move_fn);
4ca79136
RH
1430 }
1431
9661b15f 1432 return block_move_fn;
bbf6f052 1433}
44bb111a
RH
1434
1435/* A subroutine of emit_block_move. Copy the data via an explicit
1436 loop. This is used only when libcalls are forbidden. */
1437/* ??? It'd be nice to copy in hunks larger than QImode. */
1438
1439static void
502b8322
AJ
1440emit_block_move_via_loop (rtx x, rtx y, rtx size,
1441 unsigned int align ATTRIBUTE_UNUSED)
44bb111a
RH
1442{
1443 rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
372d6395
RS
1444 enum machine_mode x_addr_mode = get_address_mode (x);
1445 enum machine_mode y_addr_mode = get_address_mode (y);
44bb111a
RH
1446 enum machine_mode iter_mode;
1447
1448 iter_mode = GET_MODE (size);
1449 if (iter_mode == VOIDmode)
1450 iter_mode = word_mode;
1451
1452 top_label = gen_label_rtx ();
1453 cmp_label = gen_label_rtx ();
1454 iter = gen_reg_rtx (iter_mode);
1455
1456 emit_move_insn (iter, const0_rtx);
1457
1458 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1459 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1460 do_pending_stack_adjust ();
1461
44bb111a
RH
1462 emit_jump (cmp_label);
1463 emit_label (top_label);
1464
d4ebfa65
BE
1465 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1466 x_addr = gen_rtx_PLUS (x_addr_mode, x_addr, tmp);
1467
1468 if (x_addr_mode != y_addr_mode)
1469 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1470 y_addr = gen_rtx_PLUS (y_addr_mode, y_addr, tmp);
1471
44bb111a
RH
1472 x = change_address (x, QImode, x_addr);
1473 y = change_address (y, QImode, y_addr);
1474
1475 emit_move_insn (x, y);
1476
1477 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1478 true, OPTAB_LIB_WIDEN);
1479 if (tmp != iter)
1480 emit_move_insn (iter, tmp);
1481
44bb111a
RH
1482 emit_label (cmp_label);
1483
1484 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1485 true, top_label);
44bb111a 1486}
bbf6f052
RK
1487\f
1488/* Copy all or part of a value X into registers starting at REGNO.
1489 The number of registers to be filled is NREGS. */
1490
1491void
502b8322 1492move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
bbf6f052
RK
1493{
1494 int i;
381127e8 1495#ifdef HAVE_load_multiple
3a94c984 1496 rtx pat;
381127e8
RL
1497 rtx last;
1498#endif
bbf6f052 1499
72bb9717
RK
1500 if (nregs == 0)
1501 return;
1502
1a627b35 1503 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
bbf6f052
RK
1504 x = validize_mem (force_const_mem (mode, x));
1505
1506 /* See if the machine can do this with a load multiple insn. */
1507#ifdef HAVE_load_multiple
c3a02afe 1508 if (HAVE_load_multiple)
bbf6f052 1509 {
c3a02afe 1510 last = get_last_insn ();
38a448ca 1511 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
c3a02afe
RK
1512 GEN_INT (nregs));
1513 if (pat)
1514 {
1515 emit_insn (pat);
1516 return;
1517 }
1518 else
1519 delete_insns_since (last);
bbf6f052 1520 }
bbf6f052
RK
1521#endif
1522
1523 for (i = 0; i < nregs; i++)
38a448ca 1524 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
bbf6f052
RK
1525 operand_subword_force (x, i, mode));
1526}
1527
1528/* Copy all or part of a BLKmode value X out of registers starting at REGNO.
c6b97fac 1529 The number of registers to be filled is NREGS. */
0040593d 1530
bbf6f052 1531void
502b8322 1532move_block_from_reg (int regno, rtx x, int nregs)
bbf6f052
RK
1533{
1534 int i;
bbf6f052 1535
2954d7db
RK
1536 if (nregs == 0)
1537 return;
1538
bbf6f052
RK
1539 /* See if the machine can do this with a store multiple insn. */
1540#ifdef HAVE_store_multiple
c3a02afe 1541 if (HAVE_store_multiple)
bbf6f052 1542 {
c6b97fac
AM
1543 rtx last = get_last_insn ();
1544 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1545 GEN_INT (nregs));
c3a02afe
RK
1546 if (pat)
1547 {
1548 emit_insn (pat);
1549 return;
1550 }
1551 else
1552 delete_insns_since (last);
bbf6f052 1553 }
bbf6f052
RK
1554#endif
1555
1556 for (i = 0; i < nregs; i++)
1557 {
1558 rtx tem = operand_subword (x, i, 1, BLKmode);
1559
5b0264cb 1560 gcc_assert (tem);
bbf6f052 1561
38a448ca 1562 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
bbf6f052
RK
1563 }
1564}
1565
084a1106
JDA
1566/* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1567 ORIG, where ORIG is a non-consecutive group of registers represented by
1568 a PARALLEL. The clone is identical to the original except in that the
1569 original set of registers is replaced by a new set of pseudo registers.
1570 The new set has the same modes as the original set. */
1571
1572rtx
502b8322 1573gen_group_rtx (rtx orig)
084a1106
JDA
1574{
1575 int i, length;
1576 rtx *tmps;
1577
5b0264cb 1578 gcc_assert (GET_CODE (orig) == PARALLEL);
084a1106
JDA
1579
1580 length = XVECLEN (orig, 0);
1b4572a8 1581 tmps = XALLOCAVEC (rtx, length);
084a1106
JDA
1582
1583 /* Skip a NULL entry in first slot. */
1584 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1585
1586 if (i)
1587 tmps[0] = 0;
1588
1589 for (; i < length; i++)
1590 {
1591 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1592 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1593
1594 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1595 }
1596
1597 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1598}
1599
27e29549
RH
1600/* A subroutine of emit_group_load. Arguments as for emit_group_load,
1601 except that values are placed in TMPS[i], and must later be moved
daa956d0 1602 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
fffa9c1d 1603
27e29549
RH
1604static void
1605emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
fffa9c1d 1606{
27e29549 1607 rtx src;
aac5cc16 1608 int start, i;
7ef7000b 1609 enum machine_mode m = GET_MODE (orig_src);
fffa9c1d 1610
5b0264cb 1611 gcc_assert (GET_CODE (dst) == PARALLEL);
fffa9c1d 1612
f2978871
AM
1613 if (m != VOIDmode
1614 && !SCALAR_INT_MODE_P (m)
1615 && !MEM_P (orig_src)
1616 && GET_CODE (orig_src) != CONCAT)
782fa603
AH
1617 {
1618 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1619 if (imode == BLKmode)
9474e8ab 1620 src = assign_stack_temp (GET_MODE (orig_src), ssize);
782fa603
AH
1621 else
1622 src = gen_reg_rtx (imode);
1623 if (imode != BLKmode)
1624 src = gen_lowpart (GET_MODE (orig_src), src);
1625 emit_move_insn (src, orig_src);
1626 /* ...and back again. */
1627 if (imode != BLKmode)
1628 src = gen_lowpart (imode, src);
27e29549 1629 emit_group_load_1 (tmps, dst, src, type, ssize);
782fa603
AH
1630 return;
1631 }
1632
fffa9c1d
JW
1633 /* Check for a NULL entry, used to indicate that the parameter goes
1634 both on the stack and in registers. */
aac5cc16
RH
1635 if (XEXP (XVECEXP (dst, 0, 0), 0))
1636 start = 0;
fffa9c1d 1637 else
aac5cc16
RH
1638 start = 1;
1639
aac5cc16
RH
1640 /* Process the pieces. */
1641 for (i = start; i < XVECLEN (dst, 0); i++)
1642 {
1643 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
770ae6cc
RK
1644 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1645 unsigned int bytelen = GET_MODE_SIZE (mode);
aac5cc16
RH
1646 int shift = 0;
1647
1648 /* Handle trailing fragments that run over the size of the struct. */
8752c357 1649 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
aac5cc16 1650 {
6e985040
AM
1651 /* Arrange to shift the fragment to where it belongs.
1652 extract_bit_field loads to the lsb of the reg. */
1653 if (
1654#ifdef BLOCK_REG_PADDING
1655 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1656 == (BYTES_BIG_ENDIAN ? upward : downward)
1657#else
1658 BYTES_BIG_ENDIAN
1659#endif
1660 )
1661 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
aac5cc16 1662 bytelen = ssize - bytepos;
5b0264cb 1663 gcc_assert (bytelen > 0);
aac5cc16
RH
1664 }
1665
f3ce87a9
DE
1666 /* If we won't be loading directly from memory, protect the real source
1667 from strange tricks we might play; but make sure that the source can
1668 be loaded directly into the destination. */
1669 src = orig_src;
3c0cb5de 1670 if (!MEM_P (orig_src)
f3ce87a9
DE
1671 && (!CONSTANT_P (orig_src)
1672 || (GET_MODE (orig_src) != mode
1673 && GET_MODE (orig_src) != VOIDmode)))
1674 {
1675 if (GET_MODE (orig_src) == VOIDmode)
1676 src = gen_reg_rtx (mode);
1677 else
1678 src = gen_reg_rtx (GET_MODE (orig_src));
04050c69 1679
f3ce87a9
DE
1680 emit_move_insn (src, orig_src);
1681 }
1682
aac5cc16 1683 /* Optimize the access just a bit. */
3c0cb5de 1684 if (MEM_P (src)
6e985040
AM
1685 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1686 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
729a2125 1687 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
aac5cc16
RH
1688 && bytelen == GET_MODE_SIZE (mode))
1689 {
1690 tmps[i] = gen_reg_rtx (mode);
f4ef873c 1691 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
fffa9c1d 1692 }
d20b1190
EB
1693 else if (COMPLEX_MODE_P (mode)
1694 && GET_MODE (src) == mode
1695 && bytelen == GET_MODE_SIZE (mode))
1696 /* Let emit_move_complex do the bulk of the work. */
1697 tmps[i] = src;
7c4a6db0
JW
1698 else if (GET_CODE (src) == CONCAT)
1699 {
015b1ad1
JDA
1700 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1701 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1702
1703 if ((bytepos == 0 && bytelen == slen0)
1704 || (bytepos != 0 && bytepos + bytelen <= slen))
cbb92744 1705 {
015b1ad1
JDA
1706 /* The following assumes that the concatenated objects all
1707 have the same size. In this case, a simple calculation
1708 can be used to determine the object and the bit field
1709 to be extracted. */
1710 tmps[i] = XEXP (src, bytepos / slen0);
cbb92744 1711 if (! CONSTANT_P (tmps[i])
f8cfc6aa 1712 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
cbb92744 1713 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
015b1ad1 1714 (bytepos % slen0) * BITS_PER_UNIT,
62519f7f 1715 1, false, NULL_RTX, mode, mode);
cbb92744 1716 }
5b0264cb 1717 else
58f69841 1718 {
5b0264cb 1719 rtx mem;
f58c00e3 1720
5b0264cb 1721 gcc_assert (!bytepos);
9474e8ab 1722 mem = assign_stack_temp (GET_MODE (src), slen);
58f69841 1723 emit_move_insn (mem, src);
f58c00e3 1724 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
62519f7f 1725 0, 1, false, NULL_RTX, mode, mode);
58f69841 1726 }
7c4a6db0 1727 }
9c0631a7
AH
1728 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1729 SIMD register, which is currently broken. While we get GCC
1730 to emit proper RTL for these cases, let's dump to memory. */
1731 else if (VECTOR_MODE_P (GET_MODE (dst))
f8cfc6aa 1732 && REG_P (src))
9c0631a7
AH
1733 {
1734 int slen = GET_MODE_SIZE (GET_MODE (src));
1735 rtx mem;
1736
9474e8ab 1737 mem = assign_stack_temp (GET_MODE (src), slen);
9c0631a7
AH
1738 emit_move_insn (mem, src);
1739 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1740 }
d3a16cbd
FJ
1741 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1742 && XVECLEN (dst, 0) > 1)
1743 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE(dst), bytepos);
7cefcade
JDA
1744 else if (CONSTANT_P (src))
1745 {
1746 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1747
1748 if (len == ssize)
1749 tmps[i] = src;
1750 else
1751 {
1752 rtx first, second;
1753
1754 gcc_assert (2 * len == ssize);
1755 split_double (src, &first, &second);
1756 if (i)
1757 tmps[i] = second;
1758 else
1759 tmps[i] = first;
1760 }
1761 }
1762 else if (REG_P (src) && GET_MODE (src) == mode)
2ee5437b 1763 tmps[i] = src;
fffa9c1d 1764 else
19caa751 1765 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
62519f7f 1766 bytepos * BITS_PER_UNIT, 1, false, NULL_RTX,
b3520980 1767 mode, mode);
fffa9c1d 1768
6e985040 1769 if (shift)
09b52670 1770 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
eb6c3df1 1771 shift, tmps[i], 0);
fffa9c1d 1772 }
27e29549
RH
1773}
1774
1775/* Emit code to move a block SRC of type TYPE to a block DST,
1776 where DST is non-consecutive registers represented by a PARALLEL.
1777 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1778 if not known. */
1779
1780void
1781emit_group_load (rtx dst, rtx src, tree type, int ssize)
1782{
1783 rtx *tmps;
1784 int i;
1785
1b4572a8 1786 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
27e29549 1787 emit_group_load_1 (tmps, dst, src, type, ssize);
19caa751 1788
aac5cc16 1789 /* Copy the extracted pieces into the proper (probable) hard regs. */
27e29549
RH
1790 for (i = 0; i < XVECLEN (dst, 0); i++)
1791 {
1792 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1793 if (d == NULL)
1794 continue;
1795 emit_move_insn (d, tmps[i]);
1796 }
1797}
1798
1799/* Similar, but load SRC into new pseudos in a format that looks like
1800 PARALLEL. This can later be fed to emit_group_move to get things
1801 in the right place. */
1802
1803rtx
1804emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1805{
1806 rtvec vec;
1807 int i;
1808
1809 vec = rtvec_alloc (XVECLEN (parallel, 0));
1810 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1811
1812 /* Convert the vector to look just like the original PARALLEL, except
1813 with the computed values. */
1814 for (i = 0; i < XVECLEN (parallel, 0); i++)
1815 {
1816 rtx e = XVECEXP (parallel, 0, i);
1817 rtx d = XEXP (e, 0);
1818
1819 if (d)
1820 {
1821 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1822 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1823 }
1824 RTVEC_ELT (vec, i) = e;
1825 }
1826
1827 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
fffa9c1d
JW
1828}
1829
084a1106
JDA
1830/* Emit code to move a block SRC to block DST, where SRC and DST are
1831 non-consecutive groups of registers, each represented by a PARALLEL. */
1832
1833void
502b8322 1834emit_group_move (rtx dst, rtx src)
084a1106
JDA
1835{
1836 int i;
1837
5b0264cb
NS
1838 gcc_assert (GET_CODE (src) == PARALLEL
1839 && GET_CODE (dst) == PARALLEL
1840 && XVECLEN (src, 0) == XVECLEN (dst, 0));
084a1106
JDA
1841
1842 /* Skip first entry if NULL. */
1843 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1844 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1845 XEXP (XVECEXP (src, 0, i), 0));
1846}
1847
27e29549
RH
1848/* Move a group of registers represented by a PARALLEL into pseudos. */
1849
1850rtx
1851emit_group_move_into_temps (rtx src)
1852{
1853 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1854 int i;
1855
1856 for (i = 0; i < XVECLEN (src, 0); i++)
1857 {
1858 rtx e = XVECEXP (src, 0, i);
1859 rtx d = XEXP (e, 0);
1860
1861 if (d)
1862 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1863 RTVEC_ELT (vec, i) = e;
1864 }
1865
1866 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1867}
1868
6e985040
AM
1869/* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1870 where SRC is non-consecutive registers represented by a PARALLEL.
1871 SSIZE represents the total size of block ORIG_DST, or -1 if not
1872 known. */
fffa9c1d
JW
1873
1874void
6e985040 1875emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
fffa9c1d 1876{
aac5cc16 1877 rtx *tmps, dst;
79edfde8 1878 int start, finish, i;
7ef7000b 1879 enum machine_mode m = GET_MODE (orig_dst);
fffa9c1d 1880
5b0264cb 1881 gcc_assert (GET_CODE (src) == PARALLEL);
fffa9c1d 1882
0da34ce4
RH
1883 if (!SCALAR_INT_MODE_P (m)
1884 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
782fa603
AH
1885 {
1886 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1887 if (imode == BLKmode)
9474e8ab 1888 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
782fa603
AH
1889 else
1890 dst = gen_reg_rtx (imode);
1891 emit_group_store (dst, src, type, ssize);
1892 if (imode != BLKmode)
1893 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1894 emit_move_insn (orig_dst, dst);
1895 return;
1896 }
1897
fffa9c1d
JW
1898 /* Check for a NULL entry, used to indicate that the parameter goes
1899 both on the stack and in registers. */
aac5cc16
RH
1900 if (XEXP (XVECEXP (src, 0, 0), 0))
1901 start = 0;
fffa9c1d 1902 else
aac5cc16 1903 start = 1;
79edfde8 1904 finish = XVECLEN (src, 0);
aac5cc16 1905
1b4572a8 1906 tmps = XALLOCAVEC (rtx, finish);
fffa9c1d 1907
aac5cc16 1908 /* Copy the (probable) hard regs into pseudos. */
79edfde8 1909 for (i = start; i < finish; i++)
fffa9c1d 1910 {
aac5cc16 1911 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
5ac60669
RS
1912 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1913 {
1914 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1915 emit_move_insn (tmps[i], reg);
1916 }
1917 else
1918 tmps[i] = reg;
aac5cc16 1919 }
fffa9c1d 1920
aac5cc16
RH
1921 /* If we won't be storing directly into memory, protect the real destination
1922 from strange tricks we might play. */
1923 dst = orig_dst;
10a9f2be
JW
1924 if (GET_CODE (dst) == PARALLEL)
1925 {
1926 rtx temp;
1927
1928 /* We can get a PARALLEL dst if there is a conditional expression in
1929 a return statement. In that case, the dst and src are the same,
1930 so no action is necessary. */
1931 if (rtx_equal_p (dst, src))
1932 return;
1933
1934 /* It is unclear if we can ever reach here, but we may as well handle
1935 it. Allocate a temporary, and split this into a store/load to/from
1936 the temporary. */
1937
9474e8ab 1938 temp = assign_stack_temp (GET_MODE (dst), ssize);
6e985040
AM
1939 emit_group_store (temp, src, type, ssize);
1940 emit_group_load (dst, temp, type, ssize);
10a9f2be
JW
1941 return;
1942 }
3c0cb5de 1943 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
aac5cc16 1944 {
79edfde8
RS
1945 enum machine_mode outer = GET_MODE (dst);
1946 enum machine_mode inner;
5650dfbd 1947 HOST_WIDE_INT bytepos;
79edfde8
RS
1948 bool done = false;
1949 rtx temp;
1950
5ac60669 1951 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
79edfde8
RS
1952 dst = gen_reg_rtx (outer);
1953
aac5cc16 1954 /* Make life a bit easier for combine. */
79edfde8
RS
1955 /* If the first element of the vector is the low part
1956 of the destination mode, use a paradoxical subreg to
1957 initialize the destination. */
1958 if (start < finish)
1959 {
1960 inner = GET_MODE (tmps[start]);
7488662d 1961 bytepos = subreg_lowpart_offset (inner, outer);
79edfde8
RS
1962 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1963 {
1964 temp = simplify_gen_subreg (outer, tmps[start],
7488662d 1965 inner, 0);
9fd20553
RS
1966 if (temp)
1967 {
1968 emit_move_insn (dst, temp);
1969 done = true;
1970 start++;
1971 }
79edfde8
RS
1972 }
1973 }
1974
1975 /* If the first element wasn't the low part, try the last. */
1976 if (!done
1977 && start < finish - 1)
1978 {
1979 inner = GET_MODE (tmps[finish - 1]);
7488662d 1980 bytepos = subreg_lowpart_offset (inner, outer);
79edfde8
RS
1981 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1982 {
1983 temp = simplify_gen_subreg (outer, tmps[finish - 1],
7488662d 1984 inner, 0);
9fd20553
RS
1985 if (temp)
1986 {
1987 emit_move_insn (dst, temp);
1988 done = true;
1989 finish--;
1990 }
79edfde8
RS
1991 }
1992 }
1993
1994 /* Otherwise, simply initialize the result to zero. */
1995 if (!done)
1996 emit_move_insn (dst, CONST0_RTX (outer));
aac5cc16 1997 }
aac5cc16
RH
1998
1999 /* Process the pieces. */
79edfde8 2000 for (i = start; i < finish; i++)
aac5cc16 2001 {
770ae6cc 2002 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
aac5cc16 2003 enum machine_mode mode = GET_MODE (tmps[i]);
770ae6cc 2004 unsigned int bytelen = GET_MODE_SIZE (mode);
38c4df0b 2005 unsigned int adj_bytelen = bytelen;
6ddae612 2006 rtx dest = dst;
aac5cc16
RH
2007
2008 /* Handle trailing fragments that run over the size of the struct. */
8752c357 2009 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
38c4df0b 2010 adj_bytelen = ssize - bytepos;
fffa9c1d 2011
6ddae612
JJ
2012 if (GET_CODE (dst) == CONCAT)
2013 {
38c4df0b
JM
2014 if (bytepos + adj_bytelen
2015 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
6ddae612
JJ
2016 dest = XEXP (dst, 0);
2017 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2018 {
2019 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2020 dest = XEXP (dst, 1);
2021 }
5b0264cb 2022 else
0d446150 2023 {
6cd7942d
L
2024 enum machine_mode dest_mode = GET_MODE (dest);
2025 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
6cd7942d 2026
e0978eba 2027 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
6cd7942d
L
2028
2029 if (GET_MODE_ALIGNMENT (dest_mode)
2030 >= GET_MODE_ALIGNMENT (tmp_mode))
2031 {
e0978eba 2032 dest = assign_stack_temp (dest_mode,
9474e8ab 2033 GET_MODE_SIZE (dest_mode));
6cd7942d
L
2034 emit_move_insn (adjust_address (dest,
2035 tmp_mode,
2036 bytepos),
2037 tmps[i]);
2038 dst = dest;
2039 }
2040 else
2041 {
e0978eba 2042 dest = assign_stack_temp (tmp_mode,
9474e8ab 2043 GET_MODE_SIZE (tmp_mode));
6cd7942d
L
2044 emit_move_insn (dest, tmps[i]);
2045 dst = adjust_address (dest, dest_mode, bytepos);
2046 }
0d446150
JH
2047 break;
2048 }
6ddae612
JJ
2049 }
2050
38c4df0b
JM
2051 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2052 {
2053 /* store_bit_field always takes its value from the lsb.
2054 Move the fragment to the lsb if it's not already there. */
2055 if (
2056#ifdef BLOCK_REG_PADDING
2057 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2058 == (BYTES_BIG_ENDIAN ? upward : downward)
2059#else
2060 BYTES_BIG_ENDIAN
2061#endif
2062 )
2063 {
2064 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2065 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
eb6c3df1 2066 shift, tmps[i], 0);
38c4df0b
JM
2067 }
2068 bytelen = adj_bytelen;
2069 }
2070
aac5cc16 2071 /* Optimize the access just a bit. */
3c0cb5de 2072 if (MEM_P (dest)
6e985040
AM
2073 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2074 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
729a2125 2075 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
aac5cc16 2076 && bytelen == GET_MODE_SIZE (mode))
6ddae612 2077 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
aac5cc16 2078 else
6ddae612 2079 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
1169e45d 2080 0, 0, mode, tmps[i]);
fffa9c1d 2081 }
729a2125 2082
aac5cc16 2083 /* Copy from the pseudo into the (probable) hard reg. */
0d446150 2084 if (orig_dst != dst)
aac5cc16 2085 emit_move_insn (orig_dst, dst);
fffa9c1d
JW
2086}
2087
c36fce9a
GRK
2088/* Generate code to copy a BLKmode object of TYPE out of a
2089 set of registers starting with SRCREG into TGTBLK. If TGTBLK
2090 is null, a stack temporary is created. TGTBLK is returned.
2091
c988af2b
RS
2092 The purpose of this routine is to handle functions that return
2093 BLKmode structures in registers. Some machines (the PA for example)
2094 want to return all small structures in registers regardless of the
2095 structure's alignment. */
c36fce9a
GRK
2096
2097rtx
502b8322 2098copy_blkmode_from_reg (rtx tgtblk, rtx srcreg, tree type)
c36fce9a 2099{
19caa751
RK
2100 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2101 rtx src = NULL, dst = NULL;
2102 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
c988af2b 2103 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
15af420d 2104 enum machine_mode copy_mode;
19caa751
RK
2105
2106 if (tgtblk == 0)
2107 {
1da68f56
RK
2108 tgtblk = assign_temp (build_qualified_type (type,
2109 (TYPE_QUALS (type)
2110 | TYPE_QUAL_CONST)),
9474e8ab 2111 1, 1);
19caa751
RK
2112 preserve_temp_slots (tgtblk);
2113 }
3a94c984 2114
1ed1b4fb 2115 /* This code assumes srcreg is at least a full word. If it isn't, copy it
9ac3e73b 2116 into a new pseudo which is a full word. */
0d7839da 2117
19caa751
RK
2118 if (GET_MODE (srcreg) != BLKmode
2119 && GET_MODE_SIZE (GET_MODE (srcreg)) < UNITS_PER_WORD)
8df83eae 2120 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
19caa751 2121
c988af2b
RS
2122 /* If the structure doesn't take up a whole number of words, see whether
2123 SRCREG is padded on the left or on the right. If it's on the left,
2124 set PADDING_CORRECTION to the number of bits to skip.
2125
2126 In most ABIs, the structure will be returned at the least end of
2127 the register, which translates to right padding on little-endian
2128 targets and left padding on big-endian targets. The opposite
2129 holds if the structure is returned at the most significant
2130 end of the register. */
2131 if (bytes % UNITS_PER_WORD != 0
2132 && (targetm.calls.return_in_msb (type)
2133 ? !BYTES_BIG_ENDIAN
2134 : BYTES_BIG_ENDIAN))
2135 padding_correction
19caa751
RK
2136 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2137
15af420d
EB
2138 /* Copy the structure BITSIZE bits at a time. If the target lives in
2139 memory, take care of not reading/writing past its end by selecting
2140 a copy mode suited to BITSIZE. This should always be possible given
2141 how it is computed.
3a94c984 2142
19caa751
RK
2143 We could probably emit more efficient code for machines which do not use
2144 strict alignment, but it doesn't seem worth the effort at the current
2145 time. */
15af420d
EB
2146
2147 copy_mode = word_mode;
2148 if (MEM_P (tgtblk))
2149 {
2150 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2151 if (mem_mode != BLKmode)
2152 copy_mode = mem_mode;
2153 }
2154
c988af2b 2155 for (bitpos = 0, xbitpos = padding_correction;
19caa751
RK
2156 bitpos < bytes * BITS_PER_UNIT;
2157 bitpos += bitsize, xbitpos += bitsize)
2158 {
3a94c984 2159 /* We need a new source operand each time xbitpos is on a
c988af2b 2160 word boundary and when xbitpos == padding_correction
19caa751
RK
2161 (the first time through). */
2162 if (xbitpos % BITS_PER_WORD == 0
c988af2b 2163 || xbitpos == padding_correction)
b47f8cfc
JH
2164 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD,
2165 GET_MODE (srcreg));
19caa751
RK
2166
2167 /* We need a new destination operand each time bitpos is on
2168 a word boundary. */
2169 if (bitpos % BITS_PER_WORD == 0)
2170 dst = operand_subword (tgtblk, bitpos / BITS_PER_WORD, 1, BLKmode);
3a94c984 2171
19caa751 2172 /* Use xbitpos for the source extraction (right justified) and
15af420d 2173 bitpos for the destination store (left justified). */
1169e45d 2174 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
19caa751 2175 extract_bit_field (src, bitsize,
62519f7f 2176 xbitpos % BITS_PER_WORD, 1, false,
15af420d 2177 NULL_RTX, copy_mode, copy_mode));
19caa751
RK
2178 }
2179
2180 return tgtblk;
c36fce9a
GRK
2181}
2182
2ba87a29
RS
2183/* Copy BLKmode value SRC into a register of mode MODE. Return the
2184 register if it contains any data, otherwise return null.
2185
2186 This is used on targets that return BLKmode values in registers. */
2187
2188rtx
2189copy_blkmode_to_reg (enum machine_mode mode, tree src)
2190{
2191 int i, n_regs;
2192 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2193 unsigned int bitsize;
2194 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2195 enum machine_mode dst_mode;
2196
2197 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2198
2199 x = expand_normal (src);
2200
2201 bytes = int_size_in_bytes (TREE_TYPE (src));
2202 if (bytes == 0)
2203 return NULL_RTX;
2204
2205 /* If the structure doesn't take up a whole number of words, see
2206 whether the register value should be padded on the left or on
2207 the right. Set PADDING_CORRECTION to the number of padding
2208 bits needed on the left side.
2209
2210 In most ABIs, the structure will be returned at the least end of
2211 the register, which translates to right padding on little-endian
2212 targets and left padding on big-endian targets. The opposite
2213 holds if the structure is returned at the most significant
2214 end of the register. */
2215 if (bytes % UNITS_PER_WORD != 0
2216 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2217 ? !BYTES_BIG_ENDIAN
2218 : BYTES_BIG_ENDIAN))
2219 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2220 * BITS_PER_UNIT));
2221
2222 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2223 dst_words = XALLOCAVEC (rtx, n_regs);
2224 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2225
2226 /* Copy the structure BITSIZE bits at a time. */
2227 for (bitpos = 0, xbitpos = padding_correction;
2228 bitpos < bytes * BITS_PER_UNIT;
2229 bitpos += bitsize, xbitpos += bitsize)
2230 {
2231 /* We need a new destination pseudo each time xbitpos is
2232 on a word boundary and when xbitpos == padding_correction
2233 (the first time through). */
2234 if (xbitpos % BITS_PER_WORD == 0
2235 || xbitpos == padding_correction)
2236 {
2237 /* Generate an appropriate register. */
2238 dst_word = gen_reg_rtx (word_mode);
2239 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2240
2241 /* Clear the destination before we move anything into it. */
2242 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2243 }
2244
2245 /* We need a new source operand each time bitpos is on a word
2246 boundary. */
2247 if (bitpos % BITS_PER_WORD == 0)
2248 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2249
2250 /* Use bitpos for the source extraction (left justified) and
2251 xbitpos for the destination store (right justified). */
2252 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2253 0, 0, word_mode,
2254 extract_bit_field (src_word, bitsize,
2255 bitpos % BITS_PER_WORD, 1, false,
2256 NULL_RTX, word_mode, word_mode));
2257 }
2258
2259 if (mode == BLKmode)
2260 {
2261 /* Find the smallest integer mode large enough to hold the
2262 entire structure. */
2263 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2264 mode != VOIDmode;
2265 mode = GET_MODE_WIDER_MODE (mode))
2266 /* Have we found a large enough mode? */
2267 if (GET_MODE_SIZE (mode) >= bytes)
2268 break;
2269
2270 /* A suitable mode should have been found. */
2271 gcc_assert (mode != VOIDmode);
2272 }
2273
2274 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2275 dst_mode = word_mode;
2276 else
2277 dst_mode = mode;
2278 dst = gen_reg_rtx (dst_mode);
2279
2280 for (i = 0; i < n_regs; i++)
2281 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2282
2283 if (mode != dst_mode)
2284 dst = gen_lowpart (mode, dst);
2285
2286 return dst;
2287}
2288
94b25f81
RK
2289/* Add a USE expression for REG to the (possibly empty) list pointed
2290 to by CALL_FUSAGE. REG must denote a hard register. */
bbf6f052
RK
2291
2292void
7d810276 2293use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
b3f8cf4a 2294{
5b0264cb 2295 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
ed1223ba 2296
b3f8cf4a 2297 *call_fusage
7d810276 2298 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
b3f8cf4a
RK
2299}
2300
94b25f81
RK
2301/* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2302 starting at REGNO. All of these registers must be hard registers. */
b3f8cf4a
RK
2303
2304void
502b8322 2305use_regs (rtx *call_fusage, int regno, int nregs)
bbf6f052 2306{
0304dfbb 2307 int i;
bbf6f052 2308
5b0264cb 2309 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
0304dfbb
DE
2310
2311 for (i = 0; i < nregs; i++)
e50126e8 2312 use_reg (call_fusage, regno_reg_rtx[regno + i]);
bbf6f052 2313}
fffa9c1d
JW
2314
2315/* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2316 PARALLEL REGS. This is for calls that pass values in multiple
2317 non-contiguous locations. The Irix 6 ABI has examples of this. */
2318
2319void
502b8322 2320use_group_regs (rtx *call_fusage, rtx regs)
fffa9c1d
JW
2321{
2322 int i;
2323
6bd35f86
DE
2324 for (i = 0; i < XVECLEN (regs, 0); i++)
2325 {
2326 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
fffa9c1d 2327
6bd35f86
DE
2328 /* A NULL entry means the parameter goes both on the stack and in
2329 registers. This can also be a MEM for targets that pass values
2330 partially on the stack and partially in registers. */
f8cfc6aa 2331 if (reg != 0 && REG_P (reg))
6bd35f86
DE
2332 use_reg (call_fusage, reg);
2333 }
fffa9c1d 2334}
641cac0b
AN
2335
2336/* Return the defining gimple statement for SSA_NAME NAME if it is an
2337 assigment and the code of the expresion on the RHS is CODE. Return
2338 NULL otherwise. */
2339
2340static gimple
2341get_def_for_expr (tree name, enum tree_code code)
2342{
2343 gimple def_stmt;
2344
2345 if (TREE_CODE (name) != SSA_NAME)
2346 return NULL;
2347
2348 def_stmt = get_gimple_for_ssa_name (name);
2349 if (!def_stmt
2350 || gimple_assign_rhs_code (def_stmt) != code)
2351 return NULL;
2352
2353 return def_stmt;
2354}
683c600b 2355
18c56439 2356#ifdef HAVE_conditional_move
683c600b
AP
2357/* Return the defining gimple statement for SSA_NAME NAME if it is an
2358 assigment and the class of the expresion on the RHS is CLASS. Return
2359 NULL otherwise. */
2360
2361static gimple
2362get_def_for_expr_class (tree name, enum tree_code_class tclass)
2363{
2364 gimple def_stmt;
2365
2366 if (TREE_CODE (name) != SSA_NAME)
2367 return NULL;
2368
2369 def_stmt = get_gimple_for_ssa_name (name);
2370 if (!def_stmt
2371 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2372 return NULL;
2373
2374 return def_stmt;
2375}
18c56439 2376#endif
bbf6f052 2377\f
57814e5e 2378
cf5124f6
RS
2379/* Determine whether the LEN bytes generated by CONSTFUN can be
2380 stored to memory using several move instructions. CONSTFUNDATA is
2381 a pointer which will be passed as argument in every CONSTFUN call.
cfa31150
SL
2382 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2383 a memset operation and false if it's a copy of a constant string.
2384 Return nonzero if a call to store_by_pieces should succeed. */
cf5124f6 2385
57814e5e 2386int
502b8322
AJ
2387can_store_by_pieces (unsigned HOST_WIDE_INT len,
2388 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
cfa31150 2389 void *constfundata, unsigned int align, bool memsetp)
57814e5e 2390{
45d78e7f
JJ
2391 unsigned HOST_WIDE_INT l;
2392 unsigned int max_size;
57814e5e 2393 HOST_WIDE_INT offset = 0;
d824aea2 2394 enum machine_mode mode;
57814e5e
JJ
2395 enum insn_code icode;
2396 int reverse;
6ab31ade
JR
2397 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2398 rtx cst ATTRIBUTE_UNUSED;
57814e5e 2399
2c430630
RS
2400 if (len == 0)
2401 return 1;
2402
b8698a0f 2403 if (! (memsetp
cfa31150
SL
2404 ? SET_BY_PIECES_P (len, align)
2405 : STORE_BY_PIECES_P (len, align)))
57814e5e
JJ
2406 return 0;
2407
d824aea2 2408 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
57814e5e
JJ
2409
2410 /* We would first store what we can in the largest integer mode, then go to
2411 successively smaller modes. */
2412
2413 for (reverse = 0;
2414 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2415 reverse++)
2416 {
2417 l = len;
cf5124f6 2418 max_size = STORE_MAX_PIECES + 1;
57814e5e
JJ
2419 while (max_size > 1)
2420 {
d824aea2 2421 mode = widest_int_mode_for_size (max_size);
57814e5e
JJ
2422
2423 if (mode == VOIDmode)
2424 break;
2425
947131ba 2426 icode = optab_handler (mov_optab, mode);
57814e5e
JJ
2427 if (icode != CODE_FOR_nothing
2428 && align >= GET_MODE_ALIGNMENT (mode))
2429 {
2430 unsigned int size = GET_MODE_SIZE (mode);
2431
2432 while (l >= size)
2433 {
2434 if (reverse)
2435 offset -= size;
2436
2437 cst = (*constfun) (constfundata, offset, mode);
1a627b35 2438 if (!targetm.legitimate_constant_p (mode, cst))
57814e5e
JJ
2439 return 0;
2440
2441 if (!reverse)
2442 offset += size;
2443
2444 l -= size;
2445 }
2446 }
2447
2448 max_size = GET_MODE_SIZE (mode);
2449 }
2450
2451 /* The code above should have handled everything. */
5b0264cb 2452 gcc_assert (!l);
57814e5e
JJ
2453 }
2454
2455 return 1;
2456}
2457
2458/* Generate several move instructions to store LEN bytes generated by
2459 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2460 pointer which will be passed as argument in every CONSTFUN call.
cfa31150
SL
2461 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2462 a memset operation and false if it's a copy of a constant string.
8fd3cf4e
JJ
2463 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2464 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2465 stpcpy. */
57814e5e 2466
8fd3cf4e 2467rtx
502b8322
AJ
2468store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2469 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
cfa31150 2470 void *constfundata, unsigned int align, bool memsetp, int endp)
57814e5e 2471{
372d6395 2472 enum machine_mode to_addr_mode = get_address_mode (to);
b0f43ca0 2473 struct store_by_pieces_d data;
57814e5e 2474
2c430630
RS
2475 if (len == 0)
2476 {
5b0264cb 2477 gcc_assert (endp != 2);
2c430630
RS
2478 return to;
2479 }
2480
cfa31150
SL
2481 gcc_assert (memsetp
2482 ? SET_BY_PIECES_P (len, align)
2483 : STORE_BY_PIECES_P (len, align));
57814e5e
JJ
2484 data.constfun = constfun;
2485 data.constfundata = constfundata;
2486 data.len = len;
2487 data.to = to;
2488 store_by_pieces_1 (&data, align);
8fd3cf4e
JJ
2489 if (endp)
2490 {
2491 rtx to1;
2492
5b0264cb 2493 gcc_assert (!data.reverse);
8fd3cf4e
JJ
2494 if (data.autinc_to)
2495 {
2496 if (endp == 2)
2497 {
2498 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2499 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2500 else
d4ebfa65 2501 data.to_addr = copy_to_mode_reg (to_addr_mode,
0a81f074
RS
2502 plus_constant (to_addr_mode,
2503 data.to_addr,
8fd3cf4e
JJ
2504 -1));
2505 }
2506 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2507 data.offset);
2508 }
2509 else
2510 {
2511 if (endp == 2)
2512 --data.offset;
2513 to1 = adjust_address (data.to, QImode, data.offset);
2514 }
2515 return to1;
2516 }
2517 else
2518 return data.to;
57814e5e
JJ
2519}
2520
19caa751 2521/* Generate several move instructions to clear LEN bytes of block TO. (A MEM
ad76cef8 2522 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
9de08200
RK
2523
2524static void
342e2b74 2525clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
9de08200 2526{
b0f43ca0 2527 struct store_by_pieces_d data;
57814e5e 2528
2c430630
RS
2529 if (len == 0)
2530 return;
2531
57814e5e 2532 data.constfun = clear_by_pieces_1;
df4ae160 2533 data.constfundata = NULL;
57814e5e
JJ
2534 data.len = len;
2535 data.to = to;
2536 store_by_pieces_1 (&data, align);
2537}
2538
2539/* Callback routine for clear_by_pieces.
2540 Return const0_rtx unconditionally. */
2541
2542static rtx
502b8322
AJ
2543clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2544 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2545 enum machine_mode mode ATTRIBUTE_UNUSED)
57814e5e
JJ
2546{
2547 return const0_rtx;
2548}
2549
2550/* Subroutine of clear_by_pieces and store_by_pieces.
2551 Generate several move instructions to store LEN bytes of block TO. (A MEM
ad76cef8 2552 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
57814e5e
JJ
2553
2554static void
b0f43ca0 2555store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
502b8322 2556 unsigned int align ATTRIBUTE_UNUSED)
57814e5e 2557{
372d6395 2558 enum machine_mode to_addr_mode = get_address_mode (data->to);
57814e5e 2559 rtx to_addr = XEXP (data->to, 0);
45d78e7f 2560 unsigned int max_size = STORE_MAX_PIECES + 1;
fbe1758d 2561 enum insn_code icode;
9de08200 2562
57814e5e
JJ
2563 data->offset = 0;
2564 data->to_addr = to_addr;
2565 data->autinc_to
9de08200
RK
2566 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2567 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2568
57814e5e
JJ
2569 data->explicit_inc_to = 0;
2570 data->reverse
9de08200 2571 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
57814e5e
JJ
2572 if (data->reverse)
2573 data->offset = data->len;
9de08200 2574
57814e5e 2575 /* If storing requires more than two move insns,
9de08200
RK
2576 copy addresses to registers (to make displacements shorter)
2577 and use post-increment if available. */
57814e5e 2578 if (!data->autinc_to
45d78e7f 2579 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
9de08200 2580 {
d824aea2
NF
2581 /* Determine the main mode we'll be using.
2582 MODE might not be used depending on the definitions of the
2583 USE_* macros below. */
2584 enum machine_mode mode ATTRIBUTE_UNUSED
2585 = widest_int_mode_for_size (max_size);
fbe1758d 2586
57814e5e 2587 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
9de08200 2588 {
d4ebfa65 2589 data->to_addr = copy_to_mode_reg (to_addr_mode,
0a81f074
RS
2590 plus_constant (to_addr_mode,
2591 to_addr,
2592 data->len));
57814e5e
JJ
2593 data->autinc_to = 1;
2594 data->explicit_inc_to = -1;
9de08200 2595 }
3bdf5ad1 2596
57814e5e
JJ
2597 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2598 && ! data->autinc_to)
9de08200 2599 {
d4ebfa65 2600 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
57814e5e
JJ
2601 data->autinc_to = 1;
2602 data->explicit_inc_to = 1;
9de08200 2603 }
3bdf5ad1 2604
57814e5e 2605 if ( !data->autinc_to && CONSTANT_P (to_addr))
d4ebfa65 2606 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
9de08200
RK
2607 }
2608
d824aea2 2609 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
9de08200 2610
57814e5e 2611 /* First store what we can in the largest integer mode, then go to
9de08200
RK
2612 successively smaller modes. */
2613
2614 while (max_size > 1)
2615 {
d824aea2 2616 enum machine_mode mode = widest_int_mode_for_size (max_size);
9de08200
RK
2617
2618 if (mode == VOIDmode)
2619 break;
2620
947131ba 2621 icode = optab_handler (mov_optab, mode);
19caa751 2622 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
57814e5e 2623 store_by_pieces_2 (GEN_FCN (icode), mode, data);
9de08200
RK
2624
2625 max_size = GET_MODE_SIZE (mode);
2626 }
2627
2628 /* The code above should have handled everything. */
5b0264cb 2629 gcc_assert (!data->len);
9de08200
RK
2630}
2631
57814e5e 2632/* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
9de08200
RK
2633 with move instructions for mode MODE. GENFUN is the gen_... function
2634 to make a move insn for that mode. DATA has all the other info. */
2635
2636static void
502b8322 2637store_by_pieces_2 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
b0f43ca0 2638 struct store_by_pieces_d *data)
9de08200 2639{
3bdf5ad1 2640 unsigned int size = GET_MODE_SIZE (mode);
57814e5e 2641 rtx to1, cst;
9de08200
RK
2642
2643 while (data->len >= size)
2644 {
3bdf5ad1
RK
2645 if (data->reverse)
2646 data->offset -= size;
9de08200 2647
3bdf5ad1 2648 if (data->autinc_to)
630036c6
JJ
2649 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2650 data->offset);
3a94c984 2651 else
f4ef873c 2652 to1 = adjust_address (data->to, mode, data->offset);
9de08200 2653
940da324 2654 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
57814e5e
JJ
2655 emit_insn (gen_add2_insn (data->to_addr,
2656 GEN_INT (-(HOST_WIDE_INT) size)));
9de08200 2657
57814e5e
JJ
2658 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2659 emit_insn ((*genfun) (to1, cst));
3bdf5ad1 2660
940da324 2661 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
9de08200 2662 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
9de08200 2663
3bdf5ad1
RK
2664 if (! data->reverse)
2665 data->offset += size;
9de08200
RK
2666
2667 data->len -= size;
2668 }
2669}
2670\f
19caa751 2671/* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
8ac61af7 2672 its length in bytes. */
e9a25f70
JL
2673
2674rtx
079a182e
JH
2675clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2676 unsigned int expected_align, HOST_WIDE_INT expected_size)
bbf6f052 2677{
57aaef66
RH
2678 enum machine_mode mode = GET_MODE (object);
2679 unsigned int align;
e9a25f70 2680
8148fe65
JJ
2681 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2682
fcf1b822
RK
2683 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2684 just move a zero. Otherwise, do this a piece at a time. */
57aaef66 2685 if (mode != BLKmode
481683e1 2686 && CONST_INT_P (size)
57aaef66 2687 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
bbf6f052 2688 {
57aaef66
RH
2689 rtx zero = CONST0_RTX (mode);
2690 if (zero != NULL)
2691 {
2692 emit_move_insn (object, zero);
2693 return NULL;
2694 }
2695
2696 if (COMPLEX_MODE_P (mode))
2697 {
2698 zero = CONST0_RTX (GET_MODE_INNER (mode));
2699 if (zero != NULL)
2700 {
2701 write_complex_part (object, zero, 0);
2702 write_complex_part (object, zero, 1);
2703 return NULL;
2704 }
2705 }
4ca79136
RH
2706 }
2707
57aaef66
RH
2708 if (size == const0_rtx)
2709 return NULL;
2710
2711 align = MEM_ALIGN (object);
2712
481683e1 2713 if (CONST_INT_P (size)
57aaef66
RH
2714 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2715 clear_by_pieces (object, INTVAL (size), align);
079a182e
JH
2716 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2717 expected_align, expected_size))
57aaef66 2718 ;
09e881c9 2719 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
8c996513
JH
2720 return set_storage_via_libcall (object, size, const0_rtx,
2721 method == BLOCK_OP_TAILCALL);
09e881c9
BE
2722 else
2723 gcc_unreachable ();
57aaef66
RH
2724
2725 return NULL;
4ca79136
RH
2726}
2727
079a182e
JH
2728rtx
2729clear_storage (rtx object, rtx size, enum block_op_methods method)
2730{
2731 return clear_storage_hints (object, size, method, 0, -1);
2732}
2733
2734
8f99553f 2735/* A subroutine of clear_storage. Expand a call to memset.
4ca79136 2736 Return the return value of memset, 0 otherwise. */
9de08200 2737
8c996513
JH
2738rtx
2739set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
4ca79136 2740{
5039610b 2741 tree call_expr, fn, object_tree, size_tree, val_tree;
4ca79136
RH
2742 enum machine_mode size_mode;
2743 rtx retval;
9de08200 2744
ad76cef8
PB
2745 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2746 place those into new pseudos into a VAR_DECL and use them later. */
52cf7115 2747
18ae1560 2748 object = copy_addr_to_reg (XEXP (object, 0));
52cf7115 2749
8f99553f 2750 size_mode = TYPE_MODE (sizetype);
4ca79136
RH
2751 size = convert_to_mode (size_mode, size, 1);
2752 size = copy_to_mode_reg (size_mode, size);
52cf7115 2753
4ca79136
RH
2754 /* It is incorrect to use the libcall calling conventions to call
2755 memset in this context. This could be a user call to memset and
2756 the user may wish to examine the return value from memset. For
2757 targets where libcalls and normal calls have different conventions
8f99553f 2758 for returning pointers, we could end up generating incorrect code. */
4bc973ae 2759
4ca79136 2760 object_tree = make_tree (ptr_type_node, object);
481683e1 2761 if (!CONST_INT_P (val))
8c996513 2762 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
8f99553f 2763 size_tree = make_tree (sizetype, size);
8c996513 2764 val_tree = make_tree (integer_type_node, val);
4ca79136
RH
2765
2766 fn = clear_storage_libcall_fn (true);
038dc49a 2767 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
8148fe65 2768 CALL_EXPR_TAILCALL (call_expr) = tailcall;
4ca79136 2769
84217346 2770 retval = expand_normal (call_expr);
4ca79136 2771
8f99553f 2772 return retval;
4ca79136
RH
2773}
2774
8c996513 2775/* A subroutine of set_storage_via_libcall. Create the tree node
bbee5843 2776 for the function we use for block clears. */
4ca79136 2777
8dd5516b 2778tree block_clear_fn;
66c60e67 2779
9661b15f 2780void
502b8322 2781init_block_clear_fn (const char *asmspec)
4ca79136 2782{
9661b15f 2783 if (!block_clear_fn)
4ca79136 2784 {
9661b15f
JJ
2785 tree fn, args;
2786
8f99553f
JM
2787 fn = get_identifier ("memset");
2788 args = build_function_type_list (ptr_type_node, ptr_type_node,
2789 integer_type_node, sizetype,
2790 NULL_TREE);
4ca79136 2791
c2255bc4 2792 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
4ca79136
RH
2793 DECL_EXTERNAL (fn) = 1;
2794 TREE_PUBLIC (fn) = 1;
2795 DECL_ARTIFICIAL (fn) = 1;
2796 TREE_NOTHROW (fn) = 1;
5b5cba1f
JM
2797 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2798 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4ca79136
RH
2799
2800 block_clear_fn = fn;
bbf6f052 2801 }
e9a25f70 2802
9661b15f 2803 if (asmspec)
0e6df31e 2804 set_user_assembler_name (block_clear_fn, asmspec);
9661b15f
JJ
2805}
2806
2807static tree
502b8322 2808clear_storage_libcall_fn (int for_call)
9661b15f
JJ
2809{
2810 static bool emitted_extern;
2811
2812 if (!block_clear_fn)
2813 init_block_clear_fn (NULL);
2814
4ca79136
RH
2815 if (for_call && !emitted_extern)
2816 {
2817 emitted_extern = true;
0e6df31e 2818 make_decl_rtl (block_clear_fn);
4ca79136 2819 }
bbf6f052 2820
9661b15f 2821 return block_clear_fn;
4ca79136 2822}
57e84f18
AS
2823\f
2824/* Expand a setmem pattern; return true if successful. */
2825
2826bool
079a182e
JH
2827set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2828 unsigned int expected_align, HOST_WIDE_INT expected_size)
57e84f18
AS
2829{
2830 /* Try the most limited insn first, because there's no point
2831 including more than one in the machine description unless
2832 the more limited one has some advantage. */
2833
57e84f18
AS
2834 enum machine_mode mode;
2835
079a182e
JH
2836 if (expected_align < align)
2837 expected_align = align;
2838
57e84f18
AS
2839 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2840 mode = GET_MODE_WIDER_MODE (mode))
2841 {
f9621cc4 2842 enum insn_code code = direct_optab_handler (setmem_optab, mode);
57e84f18
AS
2843
2844 if (code != CODE_FOR_nothing
2845 /* We don't need MODE to be narrower than
2846 BITS_PER_HOST_WIDE_INT here because if SIZE is less than
2847 the mode mask, as it is returned by the macro, it will
2848 definitely be less than the actual mode mask. */
481683e1 2849 && ((CONST_INT_P (size)
57e84f18
AS
2850 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2851 <= (GET_MODE_MASK (mode) >> 1)))
a5c7d693
RS
2852 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
2853 {
2854 struct expand_operand ops[6];
2855 unsigned int nops;
2856
f04713ee
RS
2857 nops = insn_data[(int) code].n_generator_args;
2858 gcc_assert (nops == 4 || nops == 6);
c2654ded 2859
a5c7d693
RS
2860 create_fixed_operand (&ops[0], object);
2861 /* The check above guarantees that this size conversion is valid. */
2862 create_convert_operand_to (&ops[1], size, mode, true);
2863 create_convert_operand_from (&ops[2], val, byte_mode, true);
2864 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
c2654ded 2865 if (nops == 6)
9ed92901 2866 {
a5c7d693
RS
2867 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2868 create_integer_operand (&ops[5], expected_size);
9ed92901 2869 }
a5c7d693
RS
2870 if (maybe_expand_insn (code, nops, ops))
2871 return true;
57e84f18
AS
2872 }
2873 }
2874
2875 return false;
2876}
2877
4ca79136 2878\f
1466e387
RH
2879/* Write to one of the components of the complex value CPLX. Write VAL to
2880 the real part if IMAG_P is false, and the imaginary part if its true. */
bbf6f052 2881
1466e387
RH
2882static void
2883write_complex_part (rtx cplx, rtx val, bool imag_p)
2884{
ddf4e03f
RH
2885 enum machine_mode cmode;
2886 enum machine_mode imode;
2887 unsigned ibitsize;
2888
1466e387 2889 if (GET_CODE (cplx) == CONCAT)
1466e387 2890 {
ddf4e03f
RH
2891 emit_move_insn (XEXP (cplx, imag_p), val);
2892 return;
2893 }
2894
2895 cmode = GET_MODE (cplx);
2896 imode = GET_MODE_INNER (cmode);
2897 ibitsize = GET_MODE_BITSIZE (imode);
bbf6f052 2898
7a31c801
DE
2899 /* For MEMs simplify_gen_subreg may generate an invalid new address
2900 because, e.g., the original address is considered mode-dependent
2901 by the target, which restricts simplify_subreg from invoking
2902 adjust_address_nv. Instead of preparing fallback support for an
2903 invalid address, we call adjust_address_nv directly. */
2904 if (MEM_P (cplx))
22469409
BW
2905 {
2906 emit_move_insn (adjust_address_nv (cplx, imode,
2907 imag_p ? GET_MODE_SIZE (imode) : 0),
2908 val);
2909 return;
2910 }
7a31c801 2911
ddf4e03f
RH
2912 /* If the sub-object is at least word sized, then we know that subregging
2913 will work. This special case is important, since store_bit_field
2914 wants to operate on integer modes, and there's rarely an OImode to
2915 correspond to TCmode. */
36d7571c
EB
2916 if (ibitsize >= BITS_PER_WORD
2917 /* For hard regs we have exact predicates. Assume we can split
2918 the original object if it spans an even number of hard regs.
2919 This special case is important for SCmode on 64-bit platforms
2920 where the natural size of floating-point regs is 32-bit. */
2ca202e7 2921 || (REG_P (cplx)
36d7571c 2922 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
7a31c801 2923 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
ddf4e03f
RH
2924 {
2925 rtx part = simplify_gen_subreg (imode, cplx, cmode,
2926 imag_p ? GET_MODE_SIZE (imode) : 0);
36d7571c
EB
2927 if (part)
2928 {
2929 emit_move_insn (part, val);
2930 return;
2931 }
2932 else
2933 /* simplify_gen_subreg may fail for sub-word MEMs. */
2934 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
1466e387 2935 }
36d7571c 2936
1169e45d 2937 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
1466e387
RH
2938}
2939
2940/* Extract one of the components of the complex value CPLX. Extract the
2941 real part if IMAG_P is false, and the imaginary part if it's true. */
2942
2943static rtx
2944read_complex_part (rtx cplx, bool imag_p)
bbf6f052 2945{
1466e387
RH
2946 enum machine_mode cmode, imode;
2947 unsigned ibitsize;
bbf6f052 2948
1466e387
RH
2949 if (GET_CODE (cplx) == CONCAT)
2950 return XEXP (cplx, imag_p);
bbf6f052 2951
1466e387
RH
2952 cmode = GET_MODE (cplx);
2953 imode = GET_MODE_INNER (cmode);
2954 ibitsize = GET_MODE_BITSIZE (imode);
2955
2956 /* Special case reads from complex constants that got spilled to memory. */
2957 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
de1b33dd 2958 {
1466e387
RH
2959 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
2960 if (decl && TREE_CODE (decl) == COMPLEX_CST)
2961 {
2962 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
2963 if (CONSTANT_CLASS_P (part))
2964 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
2965 }
2966 }
51286de6 2967
7a31c801
DE
2968 /* For MEMs simplify_gen_subreg may generate an invalid new address
2969 because, e.g., the original address is considered mode-dependent
2970 by the target, which restricts simplify_subreg from invoking
2971 adjust_address_nv. Instead of preparing fallback support for an
2972 invalid address, we call adjust_address_nv directly. */
2973 if (MEM_P (cplx))
2974 return adjust_address_nv (cplx, imode,
2975 imag_p ? GET_MODE_SIZE (imode) : 0);
2976
ddf4e03f
RH
2977 /* If the sub-object is at least word sized, then we know that subregging
2978 will work. This special case is important, since extract_bit_field
2979 wants to operate on integer modes, and there's rarely an OImode to
2980 correspond to TCmode. */
36d7571c
EB
2981 if (ibitsize >= BITS_PER_WORD
2982 /* For hard regs we have exact predicates. Assume we can split
2983 the original object if it spans an even number of hard regs.
2984 This special case is important for SCmode on 64-bit platforms
2985 where the natural size of floating-point regs is 32-bit. */
2ca202e7 2986 || (REG_P (cplx)
36d7571c 2987 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
7a31c801 2988 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
ddf4e03f
RH
2989 {
2990 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
2991 imag_p ? GET_MODE_SIZE (imode) : 0);
36d7571c
EB
2992 if (ret)
2993 return ret;
2994 else
2995 /* simplify_gen_subreg may fail for sub-word MEMs. */
2996 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
ddf4e03f
RH
2997 }
2998
1466e387 2999 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
62519f7f 3000 true, false, NULL_RTX, imode, imode);
1466e387
RH
3001}
3002\f
539eaa3a 3003/* A subroutine of emit_move_insn_1. Yet another lowpart generator.
074e6d01 3004 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
539eaa3a
RH
3005 represented in NEW_MODE. If FORCE is true, this will never happen, as
3006 we'll force-create a SUBREG if needed. */
0c19a26f 3007
1466e387 3008static rtx
074e6d01 3009emit_move_change_mode (enum machine_mode new_mode,
539eaa3a 3010 enum machine_mode old_mode, rtx x, bool force)
1466e387 3011{
074e6d01 3012 rtx ret;
1466e387 3013
4bcc9de5
UB
3014 if (push_operand (x, GET_MODE (x)))
3015 {
3016 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3017 MEM_COPY_ATTRIBUTES (ret, x);
3018 }
3019 else if (MEM_P (x))
1466e387 3020 {
ef7befe0
BE
3021 /* We don't have to worry about changing the address since the
3022 size in bytes is supposed to be the same. */
3023 if (reload_in_progress)
3024 {
3025 /* Copy the MEM to change the mode and move any
3026 substitutions from the old MEM to the new one. */
3027 ret = adjust_address_nv (x, new_mode, 0);
3028 copy_replacements (x, ret);
3029 }
3030 else
3031 ret = adjust_address (x, new_mode, 0);
de1b33dd 3032 }
1466e387
RH
3033 else
3034 {
35fd3193 3035 /* Note that we do want simplify_subreg's behavior of validating
074e6d01
RH
3036 that the new mode is ok for a hard register. If we were to use
3037 simplify_gen_subreg, we would create the subreg, but would
3038 probably run into the target not being able to implement it. */
539eaa3a
RH
3039 /* Except, of course, when FORCE is true, when this is exactly what
3040 we want. Which is needed for CCmodes on some targets. */
3041 if (force)
3042 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3043 else
3044 ret = simplify_subreg (new_mode, x, old_mode, 0);
1466e387 3045 }
bbf6f052 3046
074e6d01
RH
3047 return ret;
3048}
3049
1466e387
RH
3050/* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3051 an integer mode of the same size as MODE. Returns the instruction
3052 emitted, or NULL if such a move could not be generated. */
bbf6f052 3053
1466e387 3054static rtx
652b0932 3055emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
1466e387
RH
3056{
3057 enum machine_mode imode;
3058 enum insn_code code;
bbf6f052 3059
1466e387
RH
3060 /* There must exist a mode of the exact size we require. */
3061 imode = int_mode_for_mode (mode);
3062 if (imode == BLKmode)
3063 return NULL_RTX;
de1b33dd 3064
1466e387 3065 /* The target must support moves in this mode. */
947131ba 3066 code = optab_handler (mov_optab, imode);
1466e387
RH
3067 if (code == CODE_FOR_nothing)
3068 return NULL_RTX;
de1b33dd 3069
652b0932 3070 x = emit_move_change_mode (imode, mode, x, force);
539eaa3a
RH
3071 if (x == NULL_RTX)
3072 return NULL_RTX;
652b0932 3073 y = emit_move_change_mode (imode, mode, y, force);
539eaa3a
RH
3074 if (y == NULL_RTX)
3075 return NULL_RTX;
3076 return emit_insn (GEN_FCN (code) (x, y));
261c4230
RS
3077}
3078
1466e387
RH
3079/* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3080 Return an equivalent MEM that does not use an auto-increment. */
261c4230 3081
1466e387
RH
3082static rtx
3083emit_move_resolve_push (enum machine_mode mode, rtx x)
261c4230 3084{
1466e387
RH
3085 enum rtx_code code = GET_CODE (XEXP (x, 0));
3086 HOST_WIDE_INT adjust;
3087 rtx temp;
261c4230 3088
1466e387
RH
3089 adjust = GET_MODE_SIZE (mode);
3090#ifdef PUSH_ROUNDING
3091 adjust = PUSH_ROUNDING (adjust);
3092#endif
3093 if (code == PRE_DEC || code == POST_DEC)
3094 adjust = -adjust;
6541fe75
JJ
3095 else if (code == PRE_MODIFY || code == POST_MODIFY)
3096 {
3097 rtx expr = XEXP (XEXP (x, 0), 1);
3098 HOST_WIDE_INT val;
3099
3100 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
481683e1 3101 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
6541fe75
JJ
3102 val = INTVAL (XEXP (expr, 1));
3103 if (GET_CODE (expr) == MINUS)
3104 val = -val;
3105 gcc_assert (adjust == val || adjust == -val);
3106 adjust = val;
3107 }
76bbe028 3108
1466e387
RH
3109 /* Do not use anti_adjust_stack, since we don't want to update
3110 stack_pointer_delta. */
3111 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3112 GEN_INT (adjust), stack_pointer_rtx,
3113 0, OPTAB_LIB_WIDEN);
3114 if (temp != stack_pointer_rtx)
3115 emit_move_insn (stack_pointer_rtx, temp);
bbf6f052 3116
1466e387 3117 switch (code)
7308a047 3118 {
1466e387
RH
3119 case PRE_INC:
3120 case PRE_DEC:
6541fe75 3121 case PRE_MODIFY:
1466e387
RH
3122 temp = stack_pointer_rtx;
3123 break;
3124 case POST_INC:
1466e387 3125 case POST_DEC:
6541fe75 3126 case POST_MODIFY:
0a81f074 3127 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
1466e387
RH
3128 break;
3129 default:
3130 gcc_unreachable ();
3131 }
7308a047 3132
1466e387
RH
3133 return replace_equiv_address (x, temp);
3134}
1a06f5fe 3135
1466e387
RH
3136/* A subroutine of emit_move_complex. Generate a move from Y into X.
3137 X is known to satisfy push_operand, and MODE is known to be complex.
3138 Returns the last instruction emitted. */
bb93b973 3139
ceca734e 3140rtx
1466e387
RH
3141emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3142{
3143 enum machine_mode submode = GET_MODE_INNER (mode);
3144 bool imag_first;
bb93b973 3145
1466e387
RH
3146#ifdef PUSH_ROUNDING
3147 unsigned int submodesize = GET_MODE_SIZE (submode);
bb93b973 3148
1466e387
RH
3149 /* In case we output to the stack, but the size is smaller than the
3150 machine can push exactly, we need to use move instructions. */
3151 if (PUSH_ROUNDING (submodesize) != submodesize)
3152 {
3153 x = emit_move_resolve_push (mode, x);
3154 return emit_move_insn (x, y);
3155 }
79ce92d7 3156#endif
7308a047 3157
1466e387
RH
3158 /* Note that the real part always precedes the imag part in memory
3159 regardless of machine's endianness. */
3160 switch (GET_CODE (XEXP (x, 0)))
3161 {
3162 case PRE_DEC:
3163 case POST_DEC:
3164 imag_first = true;
3165 break;
3166 case PRE_INC:
3167 case POST_INC:
3168 imag_first = false;
3169 break;
3170 default:
3171 gcc_unreachable ();
3172 }
beb72684 3173
1466e387
RH
3174 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3175 read_complex_part (y, imag_first));
3176 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3177 read_complex_part (y, !imag_first));
3178}
405f63da 3179
ceca734e
RH
3180/* A subroutine of emit_move_complex. Perform the move from Y to X
3181 via two moves of the parts. Returns the last instruction emitted. */
3182
3183rtx
3184emit_move_complex_parts (rtx x, rtx y)
3185{
3186 /* Show the output dies here. This is necessary for SUBREGs
3187 of pseudos since we cannot track their lifetimes correctly;
3188 hard regs shouldn't appear here except as return values. */
3189 if (!reload_completed && !reload_in_progress
3190 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
c41c1387 3191 emit_clobber (x);
ceca734e
RH
3192
3193 write_complex_part (x, read_complex_part (y, false), false);
3194 write_complex_part (x, read_complex_part (y, true), true);
3195
3196 return get_last_insn ();
3197}
3198
1466e387
RH
3199/* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3200 MODE is known to be complex. Returns the last instruction emitted. */
beb72684 3201
1466e387
RH
3202static rtx
3203emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3204{
3205 bool try_int;
405f63da 3206
1466e387
RH
3207 /* Need to take special care for pushes, to maintain proper ordering
3208 of the data, and possibly extra padding. */
3209 if (push_operand (x, mode))
3210 return emit_move_complex_push (mode, x, y);
7308a047 3211
1466e387
RH
3212 /* See if we can coerce the target into moving both values at once. */
3213
c6506442
DE
3214 /* Move floating point as parts. */
3215 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
947131ba 3216 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing)
c6506442 3217 try_int = false;
1466e387 3218 /* Not possible if the values are inherently not adjacent. */
c6506442 3219 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
1466e387
RH
3220 try_int = false;
3221 /* Is possible if both are registers (or subregs of registers). */
3222 else if (register_operand (x, mode) && register_operand (y, mode))
3223 try_int = true;
3224 /* If one of the operands is a memory, and alignment constraints
3225 are friendly enough, we may be able to do combined memory operations.
3226 We do not attempt this if Y is a constant because that combination is
3227 usually better with the by-parts thing below. */
3228 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3229 && (!STRICT_ALIGNMENT
3230 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3231 try_int = true;
3232 else
3233 try_int = false;
3234
3235 if (try_int)
a3600c71 3236 {
c6506442
DE
3237 rtx ret;
3238
3239 /* For memory to memory moves, optimal behavior can be had with the
3240 existing block move logic. */
3241 if (MEM_P (x) && MEM_P (y))
3242 {
3243 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3244 BLOCK_OP_NO_LIBCALL);
3245 return get_last_insn ();
3246 }
3247
652b0932 3248 ret = emit_move_via_integer (mode, x, y, true);
1466e387
RH
3249 if (ret)
3250 return ret;
3251 }
a3600c71 3252
ceca734e 3253 return emit_move_complex_parts (x, y);
1466e387 3254}
a3600c71 3255
1466e387
RH
3256/* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3257 MODE is known to be MODE_CC. Returns the last instruction emitted. */
a3600c71 3258
1466e387
RH
3259static rtx
3260emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3261{
3262 rtx ret;
a3600c71 3263
1466e387
RH
3264 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3265 if (mode != CCmode)
3266 {
947131ba 3267 enum insn_code code = optab_handler (mov_optab, CCmode);
1466e387 3268 if (code != CODE_FOR_nothing)
539eaa3a
RH
3269 {
3270 x = emit_move_change_mode (CCmode, mode, x, true);
3271 y = emit_move_change_mode (CCmode, mode, y, true);
3272 return emit_insn (GEN_FCN (code) (x, y));
3273 }
1466e387
RH
3274 }
3275
3276 /* Otherwise, find the MODE_INT mode of the same width. */
652b0932 3277 ret = emit_move_via_integer (mode, x, y, false);
1466e387
RH
3278 gcc_assert (ret != NULL);
3279 return ret;
3280}
3281
550ab0c6
JM
3282/* Return true if word I of OP lies entirely in the
3283 undefined bits of a paradoxical subreg. */
3284
3285static bool
22ea9ec0 3286undefined_operand_subword_p (const_rtx op, int i)
550ab0c6
JM
3287{
3288 enum machine_mode innermode, innermostmode;
3289 int offset;
3290 if (GET_CODE (op) != SUBREG)
3291 return false;
3292 innermode = GET_MODE (op);
3293 innermostmode = GET_MODE (SUBREG_REG (op));
3294 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3295 /* The SUBREG_BYTE represents offset, as if the value were stored in
3296 memory, except for a paradoxical subreg where we define
3297 SUBREG_BYTE to be 0; undo this exception as in
3298 simplify_subreg. */
3299 if (SUBREG_BYTE (op) == 0
3300 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3301 {
3302 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3303 if (WORDS_BIG_ENDIAN)
3304 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3305 if (BYTES_BIG_ENDIAN)
3306 offset += difference % UNITS_PER_WORD;
3307 }
3308 if (offset >= GET_MODE_SIZE (innermostmode)
3309 || offset <= -GET_MODE_SIZE (word_mode))
3310 return true;
3311 return false;
3312}
3313
1466e387
RH
3314/* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3315 MODE is any multi-word or full-word mode that lacks a move_insn
3316 pattern. Note that you will get better code if you define such
3317 patterns, even if they must turn into multiple assembler instructions. */
3318
3319static rtx
3320emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3321{
3322 rtx last_insn = 0;
3323 rtx seq, inner;
3324 bool need_clobber;
3325 int i;
ed1223ba 3326
1466e387 3327 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
ed1223ba 3328
1466e387
RH
3329 /* If X is a push on the stack, do the push now and replace
3330 X with a reference to the stack pointer. */
3331 if (push_operand (x, mode))
3332 x = emit_move_resolve_push (mode, x);
3333
3334 /* If we are in reload, see if either operand is a MEM whose address
3335 is scheduled for replacement. */
3336 if (reload_in_progress && MEM_P (x)
3337 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3338 x = replace_equiv_address_nv (x, inner);
3339 if (reload_in_progress && MEM_P (y)
3340 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3341 y = replace_equiv_address_nv (y, inner);
3342
3343 start_sequence ();
3344
3345 need_clobber = false;
3346 for (i = 0;
3347 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3348 i++)
3349 {
3350 rtx xpart = operand_subword (x, i, 1, mode);
550ab0c6
JM
3351 rtx ypart;
3352
3353 /* Do not generate code for a move if it would come entirely
3354 from the undefined bits of a paradoxical subreg. */
3355 if (undefined_operand_subword_p (y, i))
3356 continue;
3357
3358 ypart = operand_subword (y, i, 1, mode);
1466e387
RH
3359
3360 /* If we can't get a part of Y, put Y into memory if it is a
535a42b1
NS
3361 constant. Otherwise, force it into a register. Then we must
3362 be able to get a part of Y. */
1466e387 3363 if (ypart == 0 && CONSTANT_P (y))
a3600c71 3364 {
aacd3885 3365 y = use_anchored_address (force_const_mem (mode, y));
1466e387 3366 ypart = operand_subword (y, i, 1, mode);
a3600c71 3367 }
1466e387
RH
3368 else if (ypart == 0)
3369 ypart = operand_subword_force (y, i, mode);
3370
3371 gcc_assert (xpart && ypart);
3372
3373 need_clobber |= (GET_CODE (xpart) == SUBREG);
502b8322 3374
1466e387 3375 last_insn = emit_move_insn (xpart, ypart);
a3600c71
HPN
3376 }
3377
1466e387
RH
3378 seq = get_insns ();
3379 end_sequence ();
3380
3381 /* Show the output dies here. This is necessary for SUBREGs
3382 of pseudos since we cannot track their lifetimes correctly;
3383 hard regs shouldn't appear here except as return values.
3384 We never want to emit such a clobber after reload. */
3385 if (x != y
3386 && ! (reload_in_progress || reload_completed)
3387 && need_clobber != 0)
c41c1387 3388 emit_clobber (x);
1466e387
RH
3389
3390 emit_insn (seq);
3391
3392 return last_insn;
3393}
3394
3395/* Low level part of emit_move_insn.
3396 Called just like emit_move_insn, but assumes X and Y
3397 are basically valid. */
3398
3399rtx
3400emit_move_insn_1 (rtx x, rtx y)
3401{
3402 enum machine_mode mode = GET_MODE (x);
3403 enum insn_code code;
3404
3405 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3406
947131ba 3407 code = optab_handler (mov_optab, mode);
1466e387
RH
3408 if (code != CODE_FOR_nothing)
3409 return emit_insn (GEN_FCN (code) (x, y));
3410
3411 /* Expand complex moves by moving real part and imag part. */
3412 if (COMPLEX_MODE_P (mode))
3413 return emit_move_complex (mode, x, y);
3414
0f996086
CF
3415 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3416 || ALL_FIXED_POINT_MODE_P (mode))
ef7befe0
BE
3417 {
3418 rtx result = emit_move_via_integer (mode, x, y, true);
3419
3420 /* If we can't find an integer mode, use multi words. */
3421 if (result)
3422 return result;
3423 else
3424 return emit_move_multi_word (mode, x, y);
3425 }
3426
1466e387
RH
3427 if (GET_MODE_CLASS (mode) == MODE_CC)
3428 return emit_move_ccmode (mode, x, y);
3429
5581fc91
RS
3430 /* Try using a move pattern for the corresponding integer mode. This is
3431 only safe when simplify_subreg can convert MODE constants into integer
3432 constants. At present, it can only do this reliably if the value
3433 fits within a HOST_WIDE_INT. */
1466e387 3434 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
bbf6f052 3435 {
652b0932 3436 rtx ret = emit_move_via_integer (mode, x, y, false);
1466e387
RH
3437 if (ret)
3438 return ret;
3439 }
0fb7aeda 3440
1466e387
RH
3441 return emit_move_multi_word (mode, x, y);
3442}
918a6124 3443
1466e387
RH
3444/* Generate code to copy Y into X.
3445 Both Y and X must have the same mode, except that
3446 Y can be a constant with VOIDmode.
3447 This mode cannot be BLKmode; use emit_block_move for that.
3a94c984 3448
1466e387 3449 Return the last instruction emitted. */
3ef1eef4 3450
1466e387
RH
3451rtx
3452emit_move_insn (rtx x, rtx y)
3453{
3454 enum machine_mode mode = GET_MODE (x);
3455 rtx y_cst = NULL_RTX;
3456 rtx last_insn, set;
15a7a8ec 3457
1466e387
RH
3458 gcc_assert (mode != BLKmode
3459 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
bbf6f052 3460
1466e387
RH
3461 if (CONSTANT_P (y))
3462 {
3463 if (optimize
3464 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3465 && (last_insn = compress_float_constant (x, y)))
3466 return last_insn;
bbf6f052 3467
1466e387 3468 y_cst = y;
bbf6f052 3469
1a627b35 3470 if (!targetm.legitimate_constant_p (mode, y))
1466e387
RH
3471 {
3472 y = force_const_mem (mode, y);
235ae7be 3473
1466e387
RH
3474 /* If the target's cannot_force_const_mem prevented the spill,
3475 assume that the target's move expanders will also take care
3476 of the non-legitimate constant. */
3477 if (!y)
3478 y = y_cst;
aacd3885
RS
3479 else
3480 y = use_anchored_address (y);
bbf6f052 3481 }
1466e387 3482 }
6551fa4d 3483
1466e387
RH
3484 /* If X or Y are memory references, verify that their addresses are valid
3485 for the machine. */
3486 if (MEM_P (x)
09e881c9
BE
3487 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3488 MEM_ADDR_SPACE (x))
3de5e93a 3489 && ! push_operand (x, GET_MODE (x))))
1466e387 3490 x = validize_mem (x);
235ae7be 3491
1466e387 3492 if (MEM_P (y)
09e881c9
BE
3493 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3494 MEM_ADDR_SPACE (y)))
1466e387 3495 y = validize_mem (y);
235ae7be 3496
1466e387 3497 gcc_assert (mode != BLKmode);
235ae7be 3498
1466e387
RH
3499 last_insn = emit_move_insn_1 (x, y);
3500
3501 if (y_cst && REG_P (x)
3502 && (set = single_set (last_insn)) != NULL_RTX
3503 && SET_DEST (set) == x
3504 && ! rtx_equal_p (y_cst, SET_SRC (set)))
591e29d9 3505 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
1466e387
RH
3506
3507 return last_insn;
bbf6f052 3508}
51286de6
RH
3509
3510/* If Y is representable exactly in a narrower mode, and the target can
3511 perform the extension directly from constant or memory, then emit the
3512 move as an extension. */
3513
3514static rtx
502b8322 3515compress_float_constant (rtx x, rtx y)
51286de6
RH
3516{
3517 enum machine_mode dstmode = GET_MODE (x);
3518 enum machine_mode orig_srcmode = GET_MODE (y);
3519 enum machine_mode srcmode;
3520 REAL_VALUE_TYPE r;
e4541b7a 3521 int oldcost, newcost;
f40751dd 3522 bool speed = optimize_insn_for_speed_p ();
51286de6
RH
3523
3524 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3525
1a627b35 3526 if (targetm.legitimate_constant_p (dstmode, y))
5e8f01f4 3527 oldcost = set_src_cost (y, speed);
e4541b7a 3528 else
5e8f01f4 3529 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
e4541b7a 3530
51286de6
RH
3531 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3532 srcmode != orig_srcmode;
3533 srcmode = GET_MODE_WIDER_MODE (srcmode))
3534 {
3535 enum insn_code ic;
3536 rtx trunc_y, last_insn;
3537
3538 /* Skip if the target can't extend this way. */
3539 ic = can_extend_p (dstmode, srcmode, 0);
3540 if (ic == CODE_FOR_nothing)
3541 continue;
3542
3543 /* Skip if the narrowed value isn't exact. */
3544 if (! exact_real_truncate (srcmode, &r))
3545 continue;
3546
3547 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3548
1a627b35 3549 if (targetm.legitimate_constant_p (srcmode, trunc_y))
51286de6
RH
3550 {
3551 /* Skip if the target needs extra instructions to perform
3552 the extension. */
2ef6ce06 3553 if (!insn_operand_matches (ic, 1, trunc_y))
51286de6 3554 continue;
e4541b7a 3555 /* This is valid, but may not be cheaper than the original. */
5e8f01f4
RS
3556 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3557 speed);
e4541b7a
DJ
3558 if (oldcost < newcost)
3559 continue;
51286de6
RH
3560 }
3561 else if (float_extend_from_mem[dstmode][srcmode])
e4541b7a
DJ
3562 {
3563 trunc_y = force_const_mem (srcmode, trunc_y);
3564 /* This is valid, but may not be cheaper than the original. */
5e8f01f4
RS
3565 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3566 speed);
e4541b7a
DJ
3567 if (oldcost < newcost)
3568 continue;
3569 trunc_y = validize_mem (trunc_y);
3570 }
51286de6
RH
3571 else
3572 continue;
d763e130
RS
3573
3574 /* For CSE's benefit, force the compressed constant pool entry
3575 into a new pseudo. This constant may be used in different modes,
3576 and if not, combine will put things back together for us. */
3577 trunc_y = force_reg (srcmode, trunc_y);
51286de6
RH
3578 emit_unop_insn (ic, x, trunc_y, UNKNOWN);
3579 last_insn = get_last_insn ();
3580
f8cfc6aa 3581 if (REG_P (x))
0c19a26f 3582 set_unique_reg_note (last_insn, REG_EQUAL, y);
51286de6
RH
3583
3584 return last_insn;
3585 }
3586
3587 return NULL_RTX;
3588}
bbf6f052
RK
3589\f
3590/* Pushing data onto the stack. */
3591
3592/* Push a block of length SIZE (perhaps variable)
3593 and return an rtx to address the beginning of the block.
bbf6f052
RK
3594 The value may be virtual_outgoing_args_rtx.
3595
3596 EXTRA is the number of bytes of padding to push in addition to SIZE.
3597 BELOW nonzero means this padding comes at low addresses;
3598 otherwise, the padding comes at high addresses. */
3599
3600rtx
502b8322 3601push_block (rtx size, int extra, int below)
bbf6f052 3602{
b3694847 3603 rtx temp;
88f63c77
RK
3604
3605 size = convert_modes (Pmode, ptr_mode, size, 1);
bbf6f052 3606 if (CONSTANT_P (size))
0a81f074 3607 anti_adjust_stack (plus_constant (Pmode, size, extra));
f8cfc6aa 3608 else if (REG_P (size) && extra == 0)
bbf6f052
RK
3609 anti_adjust_stack (size);
3610 else
3611 {
ce48579b 3612 temp = copy_to_mode_reg (Pmode, size);
bbf6f052 3613 if (extra != 0)
906c4e36 3614 temp = expand_binop (Pmode, add_optab, temp, GEN_INT (extra),
bbf6f052
RK
3615 temp, 0, OPTAB_LIB_WIDEN);
3616 anti_adjust_stack (temp);
3617 }
3618
f73ad30e 3619#ifndef STACK_GROWS_DOWNWARD
f73ad30e 3620 if (0)
f73ad30e
JH
3621#else
3622 if (1)
bbf6f052 3623#endif
f73ad30e 3624 {
f73ad30e
JH
3625 temp = virtual_outgoing_args_rtx;
3626 if (extra != 0 && below)
0a81f074 3627 temp = plus_constant (Pmode, temp, extra);
f73ad30e
JH
3628 }
3629 else
3630 {
481683e1 3631 if (CONST_INT_P (size))
0a81f074 3632 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3a94c984 3633 -INTVAL (size) - (below ? 0 : extra));
f73ad30e
JH
3634 else if (extra != 0 && !below)
3635 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
0a81f074
RS
3636 negate_rtx (Pmode, plus_constant (Pmode, size,
3637 extra)));
f73ad30e
JH
3638 else
3639 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3640 negate_rtx (Pmode, size));
3641 }
bbf6f052
RK
3642
3643 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3644}
3645
9a08d230
RH
3646/* A utility routine that returns the base of an auto-inc memory, or NULL. */
3647
3648static rtx
3649mem_autoinc_base (rtx mem)
3650{
3651 if (MEM_P (mem))
3652 {
3653 rtx addr = XEXP (mem, 0);
3654 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3655 return XEXP (addr, 0);
3656 }
3657 return NULL;
3658}
3659
3660/* A utility routine used here, in reload, and in try_split. The insns
3661 after PREV up to and including LAST are known to adjust the stack,
3662 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3663 placing notes as appropriate. PREV may be NULL, indicating the
3664 entire insn sequence prior to LAST should be scanned.
3665
3666 The set of allowed stack pointer modifications is small:
3667 (1) One or more auto-inc style memory references (aka pushes),
3668 (2) One or more addition/subtraction with the SP as destination,
3669 (3) A single move insn with the SP as destination,
319638ed
JJ
3670 (4) A call_pop insn,
3671 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
9a08d230 3672
319638ed
JJ
3673 Insns in the sequence that do not modify the SP are ignored,
3674 except for noreturn calls.
9a08d230
RH
3675
3676 The return value is the amount of adjustment that can be trivially
3677 verified, via immediate operand or auto-inc. If the adjustment
3678 cannot be trivially extracted, the return value is INT_MIN. */
3679
42aa5124
RH
3680HOST_WIDE_INT
3681find_args_size_adjust (rtx insn)
9a08d230 3682{
42aa5124
RH
3683 rtx dest, set, pat;
3684 int i;
9a08d230 3685
42aa5124
RH
3686 pat = PATTERN (insn);
3687 set = NULL;
9a08d230 3688
42aa5124
RH
3689 /* Look for a call_pop pattern. */
3690 if (CALL_P (insn))
3691 {
3692 /* We have to allow non-call_pop patterns for the case
3693 of emit_single_push_insn of a TLS address. */
3694 if (GET_CODE (pat) != PARALLEL)
3695 return 0;
9a08d230 3696
42aa5124
RH
3697 /* All call_pop have a stack pointer adjust in the parallel.
3698 The call itself is always first, and the stack adjust is
3699 usually last, so search from the end. */
3700 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
9a08d230 3701 {
42aa5124
RH
3702 set = XVECEXP (pat, 0, i);
3703 if (GET_CODE (set) != SET)
6bfd73a6 3704 continue;
42aa5124
RH
3705 dest = SET_DEST (set);
3706 if (dest == stack_pointer_rtx)
3707 break;
9a08d230 3708 }
42aa5124
RH
3709 /* We'd better have found the stack pointer adjust. */
3710 if (i == 0)
3711 return 0;
3712 /* Fall through to process the extracted SET and DEST
3713 as if it was a standalone insn. */
3714 }
3715 else if (GET_CODE (pat) == SET)
3716 set = pat;
3717 else if ((set = single_set (insn)) != NULL)
3718 ;
3719 else if (GET_CODE (pat) == PARALLEL)
3720 {
3721 /* ??? Some older ports use a parallel with a stack adjust
3722 and a store for a PUSH_ROUNDING pattern, rather than a
3723 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3724 /* ??? See h8300 and m68k, pushqi1. */
3725 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
9a08d230 3726 {
42aa5124
RH
3727 set = XVECEXP (pat, 0, i);
3728 if (GET_CODE (set) != SET)
9a08d230 3729 continue;
42aa5124
RH
3730 dest = SET_DEST (set);
3731 if (dest == stack_pointer_rtx)
3732 break;
3733
3734 /* We do not expect an auto-inc of the sp in the parallel. */
3735 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3736 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3737 != stack_pointer_rtx);
9a08d230 3738 }
42aa5124
RH
3739 if (i < 0)
3740 return 0;
3741 }
3742 else
3743 return 0;
3744
3745 dest = SET_DEST (set);
3746
3747 /* Look for direct modifications of the stack pointer. */
3748 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3749 {
3750 /* Look for a trivial adjustment, otherwise assume nothing. */
3751 /* Note that the SPU restore_stack_block pattern refers to
3752 the stack pointer in V4SImode. Consider that non-trivial. */
3753 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3754 && GET_CODE (SET_SRC (set)) == PLUS
3755 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3756 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3757 return INTVAL (XEXP (SET_SRC (set), 1));
3758 /* ??? Reload can generate no-op moves, which will be cleaned
3759 up later. Recognize it and continue searching. */
3760 else if (rtx_equal_p (dest, SET_SRC (set)))
3761 return 0;
9a08d230 3762 else
42aa5124
RH
3763 return HOST_WIDE_INT_MIN;
3764 }
3765 else
3766 {
3767 rtx mem, addr;
9a08d230 3768
9a08d230 3769 /* Otherwise only think about autoinc patterns. */
42aa5124 3770 if (mem_autoinc_base (dest) == stack_pointer_rtx)
9a08d230 3771 {
42aa5124
RH
3772 mem = dest;
3773 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3774 != stack_pointer_rtx);
9a08d230 3775 }
42aa5124
RH
3776 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3777 mem = SET_SRC (set);
9a08d230 3778 else
42aa5124
RH
3779 return 0;
3780
3781 addr = XEXP (mem, 0);
3782 switch (GET_CODE (addr))
3783 {
3784 case PRE_INC:
3785 case POST_INC:
3786 return GET_MODE_SIZE (GET_MODE (mem));
3787 case PRE_DEC:
3788 case POST_DEC:
3789 return -GET_MODE_SIZE (GET_MODE (mem));
3790 case PRE_MODIFY:
3791 case POST_MODIFY:
3792 addr = XEXP (addr, 1);
3793 gcc_assert (GET_CODE (addr) == PLUS);
3794 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3795 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3796 return INTVAL (XEXP (addr, 1));
3797 default:
3798 gcc_unreachable ();
3799 }
3800 }
3801}
3802
3803int
3804fixup_args_size_notes (rtx prev, rtx last, int end_args_size)
3805{
3806 int args_size = end_args_size;
3807 bool saw_unknown = false;
3808 rtx insn;
3809
3810 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3811 {
3812 HOST_WIDE_INT this_delta;
3813
3814 if (!NONDEBUG_INSN_P (insn))
9a08d230
RH
3815 continue;
3816
42aa5124
RH
3817 this_delta = find_args_size_adjust (insn);
3818 if (this_delta == 0)
319638ed
JJ
3819 {
3820 if (!CALL_P (insn)
3821 || ACCUMULATE_OUTGOING_ARGS
3822 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3823 continue;
3824 }
42aa5124
RH
3825
3826 gcc_assert (!saw_unknown);
3827 if (this_delta == HOST_WIDE_INT_MIN)
3828 saw_unknown = true;
3829
9a08d230
RH
3830 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3831#ifdef STACK_GROWS_DOWNWARD
3832 this_delta = -this_delta;
3833#endif
3834 args_size -= this_delta;
3835 }
3836
3837 return saw_unknown ? INT_MIN : args_size;
3838}
21d93687 3839
9a08d230 3840#ifdef PUSH_ROUNDING
566aa174 3841/* Emit single push insn. */
21d93687 3842
566aa174 3843static void
9a08d230 3844emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
566aa174 3845{
566aa174 3846 rtx dest_addr;
918a6124 3847 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
566aa174 3848 rtx dest;
371b8fc0 3849 enum insn_code icode;
566aa174 3850
371b8fc0
JH
3851 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3852 /* If there is push pattern, use it. Otherwise try old way of throwing
3853 MEM representing push operation to move expander. */
947131ba 3854 icode = optab_handler (push_optab, mode);
371b8fc0
JH
3855 if (icode != CODE_FOR_nothing)
3856 {
a5c7d693
RS
3857 struct expand_operand ops[1];
3858
3859 create_input_operand (&ops[0], x, mode);
3860 if (maybe_expand_insn (icode, 1, ops))
3861 return;
371b8fc0 3862 }
566aa174
JH
3863 if (GET_MODE_SIZE (mode) == rounded_size)
3864 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
329d586f
KH
3865 /* If we are to pad downward, adjust the stack pointer first and
3866 then store X into the stack location using an offset. This is
3867 because emit_move_insn does not know how to pad; it does not have
3868 access to type. */
3869 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3870 {
3871 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3872 HOST_WIDE_INT offset;
3873
3874 emit_move_insn (stack_pointer_rtx,
3875 expand_binop (Pmode,
3876#ifdef STACK_GROWS_DOWNWARD
3877 sub_optab,
3878#else
3879 add_optab,
3880#endif
3881 stack_pointer_rtx,
3882 GEN_INT (rounded_size),
3883 NULL_RTX, 0, OPTAB_LIB_WIDEN));
3884
3885 offset = (HOST_WIDE_INT) padding_size;
3886#ifdef STACK_GROWS_DOWNWARD
3887 if (STACK_PUSH_CODE == POST_DEC)
3888 /* We have already decremented the stack pointer, so get the
3889 previous value. */
3890 offset += (HOST_WIDE_INT) rounded_size;
3891#else
3892 if (STACK_PUSH_CODE == POST_INC)
3893 /* We have already incremented the stack pointer, so get the
3894 previous value. */
3895 offset -= (HOST_WIDE_INT) rounded_size;
3896#endif
3897 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
3898 }
566aa174
JH
3899 else
3900 {
3901#ifdef STACK_GROWS_DOWNWARD
329d586f 3902 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
566aa174 3903 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
505ddab6 3904 GEN_INT (-(HOST_WIDE_INT) rounded_size));
566aa174 3905#else
329d586f 3906 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
566aa174
JH
3907 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3908 GEN_INT (rounded_size));
3909#endif
3910 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
3911 }
3912
3913 dest = gen_rtx_MEM (mode, dest_addr);
3914
566aa174
JH
3915 if (type != 0)
3916 {
3917 set_mem_attributes (dest, type, 1);
c3d32120
RK
3918
3919 if (flag_optimize_sibling_calls)
3920 /* Function incoming arguments may overlap with sibling call
3921 outgoing arguments and we cannot allow reordering of reads
3922 from function arguments with stores to outgoing arguments
3923 of sibling calls. */
3924 set_mem_alias_set (dest, 0);
566aa174
JH
3925 }
3926 emit_move_insn (dest, x);
566aa174 3927}
9a08d230
RH
3928
3929/* Emit and annotate a single push insn. */
3930
3931static void
3932emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
3933{
3934 int delta, old_delta = stack_pointer_delta;
3935 rtx prev = get_last_insn ();
3936 rtx last;
3937
3938 emit_single_push_insn_1 (mode, x, type);
3939
3940 last = get_last_insn ();
3941
3942 /* Notice the common case where we emitted exactly one insn. */
3943 if (PREV_INSN (last) == prev)
3944 {
3945 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
3946 return;
3947 }
3948
3949 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
3950 gcc_assert (delta == INT_MIN || delta == old_delta);
3951}
21d93687 3952#endif
566aa174 3953
bbf6f052
RK
3954/* Generate code to push X onto the stack, assuming it has mode MODE and
3955 type TYPE.
3956 MODE is redundant except when X is a CONST_INT (since they don't
3957 carry mode info).
3958 SIZE is an rtx for the size of data to be copied (in bytes),
3959 needed only if X is BLKmode.
3960
f1eaaf73 3961 ALIGN (in bits) is maximum alignment we can assume.
bbf6f052 3962
cd048831 3963 If PARTIAL and REG are both nonzero, then copy that many of the first
78a52f11
RH
3964 bytes of X into registers starting with REG, and push the rest of X.
3965 The amount of space pushed is decreased by PARTIAL bytes.
bbf6f052 3966 REG must be a hard register in this case.
cd048831
RK
3967 If REG is zero but PARTIAL is not, take any all others actions for an
3968 argument partially in registers, but do not actually load any
3969 registers.
bbf6f052
RK
3970
3971 EXTRA is the amount in bytes of extra space to leave next to this arg.
6dc42e49 3972 This is ignored if an argument block has already been allocated.
bbf6f052
RK
3973
3974 On a machine that lacks real push insns, ARGS_ADDR is the address of
3975 the bottom of the argument block for this call. We use indexing off there
3976 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
3977 argument block has not been preallocated.
3978
e5e809f4
JL
3979 ARGS_SO_FAR is the size of args previously pushed for this call.
3980
3981 REG_PARM_STACK_SPACE is nonzero if functions require stack space
3982 for arguments passed in registers. If nonzero, it will be the number
3983 of bytes required. */
bbf6f052
RK
3984
3985void
502b8322
AJ
3986emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
3987 unsigned int align, int partial, rtx reg, int extra,
3988 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
3989 rtx alignment_pad)
bbf6f052
RK
3990{
3991 rtx xinner;
3992 enum direction stack_direction
3993#ifdef STACK_GROWS_DOWNWARD
3994 = downward;
3995#else
3996 = upward;
3997#endif
3998
3999 /* Decide where to pad the argument: `downward' for below,
4000 `upward' for above, or `none' for don't pad it.
4001 Default is below for small data on big-endian machines; else above. */
4002 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4003
0fb7aeda 4004 /* Invert direction if stack is post-decrement.
9e0e11bf
GK
4005 FIXME: why? */
4006 if (STACK_PUSH_CODE == POST_DEC)
bbf6f052
RK
4007 if (where_pad != none)
4008 where_pad = (where_pad == downward ? upward : downward);
4009
ad76cef8 4010 xinner = x;
bbf6f052 4011
46bd2bee
JM
4012 if (mode == BLKmode
4013 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
bbf6f052
RK
4014 {
4015 /* Copy a block into the stack, entirely or partially. */
4016
b3694847 4017 rtx temp;
78a52f11 4018 int used;
531547e9 4019 int offset;
bbf6f052 4020 int skip;
3a94c984 4021
78a52f11
RH
4022 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4023 used = partial - offset;
531547e9 4024
46bd2bee
JM
4025 if (mode != BLKmode)
4026 {
4027 /* A value is to be stored in an insufficiently aligned
4028 stack slot; copy via a suitably aligned slot if
4029 necessary. */
4030 size = GEN_INT (GET_MODE_SIZE (mode));
4031 if (!MEM_P (xinner))
4032 {
9474e8ab 4033 temp = assign_temp (type, 1, 1);
46bd2bee
JM
4034 emit_move_insn (temp, xinner);
4035 xinner = temp;
4036 }
4037 }
4038
5b0264cb 4039 gcc_assert (size);
bbf6f052 4040
bbf6f052
RK
4041 /* USED is now the # of bytes we need not copy to the stack
4042 because registers will take care of them. */
4043
4044 if (partial != 0)
f4ef873c 4045 xinner = adjust_address (xinner, BLKmode, used);
bbf6f052
RK
4046
4047 /* If the partial register-part of the arg counts in its stack size,
4048 skip the part of stack space corresponding to the registers.
4049 Otherwise, start copying to the beginning of the stack space,
4050 by setting SKIP to 0. */
e5e809f4 4051 skip = (reg_parm_stack_space == 0) ? 0 : used;
bbf6f052
RK
4052
4053#ifdef PUSH_ROUNDING
4054 /* Do it with several push insns if that doesn't take lots of insns
4055 and if there is no difficulty with push insns that skip bytes
4056 on the stack for alignment purposes. */
4057 if (args_addr == 0
f73ad30e 4058 && PUSH_ARGS
481683e1 4059 && CONST_INT_P (size)
bbf6f052 4060 && skip == 0
f26aca6d 4061 && MEM_ALIGN (xinner) >= align
15914757 4062 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
bbf6f052
RK
4063 /* Here we avoid the case of a structure whose weak alignment
4064 forces many pushes of a small amount of data,
4065 and such small pushes do rounding that causes trouble. */
e1565e65 4066 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
19caa751 4067 || align >= BIGGEST_ALIGNMENT
f1eaaf73
DE
4068 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4069 == (align / BITS_PER_UNIT)))
3e926b4d 4070 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
bbf6f052
RK
4071 {
4072 /* Push padding now if padding above and stack grows down,
4073 or if padding below and stack grows up.
4074 But if space already allocated, this has already been done. */
4075 if (extra && args_addr == 0
4076 && where_pad != none && where_pad != stack_direction)
906c4e36 4077 anti_adjust_stack (GEN_INT (extra));
bbf6f052 4078
8fd3cf4e 4079 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
bbf6f052
RK
4080 }
4081 else
3a94c984 4082#endif /* PUSH_ROUNDING */
bbf6f052 4083 {
7ab923cc
JJ
4084 rtx target;
4085
bbf6f052
RK
4086 /* Otherwise make space on the stack and copy the data
4087 to the address of that space. */
4088
4089 /* Deduct words put into registers from the size we must copy. */
4090 if (partial != 0)
4091 {
481683e1 4092 if (CONST_INT_P (size))
906c4e36 4093 size = GEN_INT (INTVAL (size) - used);
bbf6f052
RK
4094 else
4095 size = expand_binop (GET_MODE (size), sub_optab, size,
906c4e36
RK
4096 GEN_INT (used), NULL_RTX, 0,
4097 OPTAB_LIB_WIDEN);
bbf6f052
RK
4098 }
4099
4100 /* Get the address of the stack space.
4101 In this case, we do not deal with EXTRA separately.
4102 A single stack adjust will do. */
4103 if (! args_addr)
4104 {
4105 temp = push_block (size, extra, where_pad == downward);
4106 extra = 0;
4107 }
481683e1 4108 else if (CONST_INT_P (args_so_far))
bbf6f052 4109 temp = memory_address (BLKmode,
0a81f074 4110 plus_constant (Pmode, args_addr,
bbf6f052
RK
4111 skip + INTVAL (args_so_far)));
4112 else
4113 temp = memory_address (BLKmode,
0a81f074
RS
4114 plus_constant (Pmode,
4115 gen_rtx_PLUS (Pmode,
38a448ca
RH
4116 args_addr,
4117 args_so_far),
bbf6f052 4118 skip));
4ca79136
RH
4119
4120 if (!ACCUMULATE_OUTGOING_ARGS)
4121 {
4122 /* If the source is referenced relative to the stack pointer,
4123 copy it to another register to stabilize it. We do not need
4124 to do this if we know that we won't be changing sp. */
4125
4126 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4127 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4128 temp = copy_to_reg (temp);
4129 }
4130
3a94c984 4131 target = gen_rtx_MEM (BLKmode, temp);
7ab923cc 4132
2bb16349
RH
4133 /* We do *not* set_mem_attributes here, because incoming arguments
4134 may overlap with sibling call outgoing arguments and we cannot
4135 allow reordering of reads from function arguments with stores
4136 to outgoing arguments of sibling calls. We do, however, want
4137 to record the alignment of the stack slot. */
44bb111a
RH
4138 /* ALIGN may well be better aligned than TYPE, e.g. due to
4139 PARM_BOUNDARY. Assume the caller isn't lying. */
4140 set_mem_align (target, align);
4ca79136 4141
44bb111a 4142 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
bbf6f052
RK
4143 }
4144 }
4145 else if (partial > 0)
4146 {
4147 /* Scalar partly in registers. */
4148
4149 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4150 int i;
4151 int not_stack;
78a52f11 4152 /* # bytes of start of argument
bbf6f052 4153 that we must make space for but need not store. */
ac7e839c 4154 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
bbf6f052
RK
4155 int args_offset = INTVAL (args_so_far);
4156 int skip;
4157
4158 /* Push padding now if padding above and stack grows down,
4159 or if padding below and stack grows up.
4160 But if space already allocated, this has already been done. */
4161 if (extra && args_addr == 0
4162 && where_pad != none && where_pad != stack_direction)
906c4e36 4163 anti_adjust_stack (GEN_INT (extra));
bbf6f052
RK
4164
4165 /* If we make space by pushing it, we might as well push
4166 the real data. Otherwise, we can leave OFFSET nonzero
4167 and leave the space uninitialized. */
4168 if (args_addr == 0)
4169 offset = 0;
4170
4171 /* Now NOT_STACK gets the number of words that we don't need to
40b0345d 4172 allocate on the stack. Convert OFFSET to words too. */
78a52f11 4173 not_stack = (partial - offset) / UNITS_PER_WORD;
ac7e839c 4174 offset /= UNITS_PER_WORD;
bbf6f052
RK
4175
4176 /* If the partial register-part of the arg counts in its stack size,
4177 skip the part of stack space corresponding to the registers.
4178 Otherwise, start copying to the beginning of the stack space,
4179 by setting SKIP to 0. */
e5e809f4 4180 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
bbf6f052 4181
1a627b35 4182 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
bbf6f052
RK
4183 x = validize_mem (force_const_mem (mode, x));
4184
4185 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4186 SUBREGs of such registers are not allowed. */
f8cfc6aa 4187 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
bbf6f052
RK
4188 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4189 x = copy_to_reg (x);
4190
4191 /* Loop over all the words allocated on the stack for this arg. */
4192 /* We can do it by words, because any scalar bigger than a word
4193 has a size a multiple of a word. */
4194#ifndef PUSH_ARGS_REVERSED
4195 for (i = not_stack; i < size; i++)
4196#else
4197 for (i = size - 1; i >= not_stack; i--)
4198#endif
4199 if (i >= not_stack + offset)
4200 emit_push_insn (operand_subword_force (x, i, mode),
906c4e36
RK
4201 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4202 0, args_addr,
4203 GEN_INT (args_offset + ((i - not_stack + skip)
e5e809f4 4204 * UNITS_PER_WORD)),
4fc026cd 4205 reg_parm_stack_space, alignment_pad);
bbf6f052
RK
4206 }
4207 else
4208 {
4209 rtx addr;
3bdf5ad1 4210 rtx dest;
bbf6f052
RK
4211
4212 /* Push padding now if padding above and stack grows down,
4213 or if padding below and stack grows up.
4214 But if space already allocated, this has already been done. */
4215 if (extra && args_addr == 0
4216 && where_pad != none && where_pad != stack_direction)
906c4e36 4217 anti_adjust_stack (GEN_INT (extra));
bbf6f052
RK
4218
4219#ifdef PUSH_ROUNDING
f73ad30e 4220 if (args_addr == 0 && PUSH_ARGS)
566aa174 4221 emit_single_push_insn (mode, x, type);
bbf6f052
RK
4222 else
4223#endif
921b3427 4224 {
481683e1 4225 if (CONST_INT_P (args_so_far))
921b3427
RK
4226 addr
4227 = memory_address (mode,
0a81f074 4228 plus_constant (Pmode, args_addr,
921b3427 4229 INTVAL (args_so_far)));
3a94c984 4230 else
38a448ca
RH
4231 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4232 args_so_far));
566aa174 4233 dest = gen_rtx_MEM (mode, addr);
2bb16349
RH
4234
4235 /* We do *not* set_mem_attributes here, because incoming arguments
4236 may overlap with sibling call outgoing arguments and we cannot
4237 allow reordering of reads from function arguments with stores
4238 to outgoing arguments of sibling calls. We do, however, want
4239 to record the alignment of the stack slot. */
4240 /* ALIGN may well be better aligned than TYPE, e.g. due to
4241 PARM_BOUNDARY. Assume the caller isn't lying. */
4242 set_mem_align (dest, align);
bbf6f052 4243
566aa174 4244 emit_move_insn (dest, x);
566aa174 4245 }
bbf6f052
RK
4246 }
4247
bbf6f052
RK
4248 /* If part should go in registers, copy that part
4249 into the appropriate registers. Do this now, at the end,
4250 since mem-to-mem copies above may do function calls. */
cd048831 4251 if (partial > 0 && reg != 0)
fffa9c1d
JW
4252 {
4253 /* Handle calls that pass values in multiple non-contiguous locations.
4254 The Irix 6 ABI has examples of this. */
4255 if (GET_CODE (reg) == PARALLEL)
6e985040 4256 emit_group_load (reg, x, type, -1);
fffa9c1d 4257 else
78a52f11
RH
4258 {
4259 gcc_assert (partial % UNITS_PER_WORD == 0);
4260 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4261 }
fffa9c1d 4262 }
bbf6f052
RK
4263
4264 if (extra && args_addr == 0 && where_pad == stack_direction)
906c4e36 4265 anti_adjust_stack (GEN_INT (extra));
3a94c984 4266
3ea2292a 4267 if (alignment_pad && args_addr == 0)
4fc026cd 4268 anti_adjust_stack (alignment_pad);
bbf6f052
RK
4269}
4270\f
296b4ed9
RK
4271/* Return X if X can be used as a subtarget in a sequence of arithmetic
4272 operations. */
4273
4274static rtx
502b8322 4275get_subtarget (rtx x)
296b4ed9 4276{
7c27e184
PB
4277 return (optimize
4278 || x == 0
296b4ed9 4279 /* Only registers can be subtargets. */
f8cfc6aa 4280 || !REG_P (x)
296b4ed9
RK
4281 /* Don't use hard regs to avoid extending their life. */
4282 || REGNO (x) < FIRST_PSEUDO_REGISTER
296b4ed9
RK
4283 ? 0 : x);
4284}
4285
8c1cfd5a
RH
4286/* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4287 FIELD is a bitfield. Returns true if the optimization was successful,
4288 and there's nothing else to do. */
4289
4290static bool
4291optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4292 unsigned HOST_WIDE_INT bitpos,
1169e45d
AH
4293 unsigned HOST_WIDE_INT bitregion_start,
4294 unsigned HOST_WIDE_INT bitregion_end,
8c1cfd5a
RH
4295 enum machine_mode mode1, rtx str_rtx,
4296 tree to, tree src)
4297{
4298 enum machine_mode str_mode = GET_MODE (str_rtx);
4299 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4300 tree op0, op1;
4301 rtx value, result;
4302 optab binop;
3bf162a0
AP
4303 gimple srcstmt;
4304 enum tree_code code;
8c1cfd5a
RH
4305
4306 if (mode1 != VOIDmode
4307 || bitsize >= BITS_PER_WORD
4308 || str_bitsize > BITS_PER_WORD
4309 || TREE_SIDE_EFFECTS (to)
4310 || TREE_THIS_VOLATILE (to))
4311 return false;
4312
4313 STRIP_NOPS (src);
3bf162a0
AP
4314 if (TREE_CODE (src) != SSA_NAME)
4315 return false;
4316 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4317 return false;
4318
4319 srcstmt = get_gimple_for_ssa_name (src);
4320 if (!srcstmt
4321 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
8c1cfd5a
RH
4322 return false;
4323
3bf162a0
AP
4324 code = gimple_assign_rhs_code (srcstmt);
4325
4326 op0 = gimple_assign_rhs1 (srcstmt);
4327
4328 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4329 to find its initialization. Hopefully the initialization will
4330 be from a bitfield load. */
4331 if (TREE_CODE (op0) == SSA_NAME)
4332 {
4333 gimple op0stmt = get_gimple_for_ssa_name (op0);
4334
4335 /* We want to eventually have OP0 be the same as TO, which
4336 should be a bitfield. */
4337 if (!op0stmt
4338 || !is_gimple_assign (op0stmt)
4339 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4340 return false;
4341 op0 = gimple_assign_rhs1 (op0stmt);
4342 }
4343
4344 op1 = gimple_assign_rhs2 (srcstmt);
8c1cfd5a
RH
4345
4346 if (!operand_equal_p (to, op0, 0))
4347 return false;
4348
4349 if (MEM_P (str_rtx))
4350 {
4351 unsigned HOST_WIDE_INT offset1;
4352
4353 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4354 str_mode = word_mode;
4355 str_mode = get_best_mode (bitsize, bitpos,
1169e45d 4356 bitregion_start, bitregion_end,
8c1cfd5a
RH
4357 MEM_ALIGN (str_rtx), str_mode, 0);
4358 if (str_mode == VOIDmode)
4359 return false;
4360 str_bitsize = GET_MODE_BITSIZE (str_mode);
4361
4362 offset1 = bitpos;
4363 bitpos %= str_bitsize;
4364 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4365 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4366 }
4367 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4368 return false;
4369
4370 /* If the bit field covers the whole REG/MEM, store_field
4371 will likely generate better code. */
4372 if (bitsize >= str_bitsize)
4373 return false;
4374
4375 /* We can't handle fields split across multiple entities. */
4376 if (bitpos + bitsize > str_bitsize)
4377 return false;
4378
4379 if (BYTES_BIG_ENDIAN)
4380 bitpos = str_bitsize - bitpos - bitsize;
4381
3bf162a0 4382 switch (code)
8c1cfd5a
RH
4383 {
4384 case PLUS_EXPR:
4385 case MINUS_EXPR:
4386 /* For now, just optimize the case of the topmost bitfield
4387 where we don't need to do any masking and also
4388 1 bit bitfields where xor can be used.
4389 We might win by one instruction for the other bitfields
4390 too if insv/extv instructions aren't used, so that
4391 can be added later. */
4392 if (bitpos + bitsize != str_bitsize
4393 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4394 break;
4395
49452c07 4396 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
8c1cfd5a
RH
4397 value = convert_modes (str_mode,
4398 TYPE_MODE (TREE_TYPE (op1)), value,
4399 TYPE_UNSIGNED (TREE_TYPE (op1)));
4400
4401 /* We may be accessing data outside the field, which means
4402 we can alias adjacent data. */
4403 if (MEM_P (str_rtx))
4404 {
4405 str_rtx = shallow_copy_rtx (str_rtx);
4406 set_mem_alias_set (str_rtx, 0);
4407 set_mem_expr (str_rtx, 0);
4408 }
4409
3bf162a0 4410 binop = code == PLUS_EXPR ? add_optab : sub_optab;
8c1cfd5a
RH
4411 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4412 {
4413 value = expand_and (str_mode, value, const1_rtx, NULL);
4414 binop = xor_optab;
4415 }
8c5f2327 4416 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
8c1cfd5a
RH
4417 result = expand_binop (str_mode, binop, str_rtx,
4418 value, str_rtx, 1, OPTAB_WIDEN);
4419 if (result != str_rtx)
4420 emit_move_insn (str_rtx, result);
4421 return true;
4422
92fb2d32
KH
4423 case BIT_IOR_EXPR:
4424 case BIT_XOR_EXPR:
4425 if (TREE_CODE (op1) != INTEGER_CST)
4426 break;
8c5f2327
EB
4427 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4428 value = convert_modes (str_mode,
92fb2d32
KH
4429 TYPE_MODE (TREE_TYPE (op1)), value,
4430 TYPE_UNSIGNED (TREE_TYPE (op1)));
4431
4432 /* We may be accessing data outside the field, which means
4433 we can alias adjacent data. */
4434 if (MEM_P (str_rtx))
4435 {
4436 str_rtx = shallow_copy_rtx (str_rtx);
4437 set_mem_alias_set (str_rtx, 0);
4438 set_mem_expr (str_rtx, 0);
4439 }
4440
3bf162a0 4441 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
8c5f2327 4442 if (bitpos + bitsize != str_bitsize)
92fb2d32 4443 {
8c5f2327
EB
4444 rtx mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1);
4445 value = expand_and (str_mode, value, mask, NULL_RTX);
92fb2d32 4446 }
8c5f2327
EB
4447 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4448 result = expand_binop (str_mode, binop, str_rtx,
92fb2d32
KH
4449 value, str_rtx, 1, OPTAB_WIDEN);
4450 if (result != str_rtx)
4451 emit_move_insn (str_rtx, result);
4452 return true;
4453
8c1cfd5a
RH
4454 default:
4455 break;
4456 }
4457
4458 return false;
4459}
4460
1169e45d
AH
4461/* In the C++ memory model, consecutive bit fields in a structure are
4462 considered one memory location.
4463
1d0bafd9 4464 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
26c71b93 4465 returns the bit range of consecutive bits in which this COMPONENT_REF
1d0bafd9
EB
4466 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4467 and *OFFSET may be adjusted in the process.
4468
4469 If the access does not need to be restricted, 0 is returned in both
26c71b93 4470 *BITSTART and *BITEND. */
1169e45d
AH
4471
4472static void
4473get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4474 unsigned HOST_WIDE_INT *bitend,
26c71b93 4475 tree exp,
1d0bafd9
EB
4476 HOST_WIDE_INT *bitpos,
4477 tree *offset)
1169e45d 4478{
1d0bafd9 4479 HOST_WIDE_INT bitoffset;
7ebf9677 4480 tree field, repr;
1169e45d
AH
4481
4482 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4483
26c71b93
RG
4484 field = TREE_OPERAND (exp, 1);
4485 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4486 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4487 need to limit the range we can access. */
4488 if (!repr)
1169e45d
AH
4489 {
4490 *bitstart = *bitend = 0;
4491 return;
4492 }
4493
a59b038c
EB
4494 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4495 part of a larger bit field, then the representative does not serve any
4496 useful purpose. This can occur in Ada. */
4497 if (handled_component_p (TREE_OPERAND (exp, 0)))
4498 {
4499 enum machine_mode rmode;
4500 HOST_WIDE_INT rbitsize, rbitpos;
4501 tree roffset;
4502 int unsignedp;
4503 int volatilep = 0;
4504 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4505 &roffset, &rmode, &unsignedp, &volatilep, false);
4506 if ((rbitpos % BITS_PER_UNIT) != 0)
4507 {
4508 *bitstart = *bitend = 0;
4509 return;
4510 }
4511 }
4512
26c71b93 4513 /* Compute the adjustment to bitpos from the offset of the field
7ebf9677
RG
4514 relative to the representative. DECL_FIELD_OFFSET of field and
4515 repr are the same by construction if they are not constants,
4516 see finish_bitfield_layout. */
4517 if (host_integerp (DECL_FIELD_OFFSET (field), 1)
4518 && host_integerp (DECL_FIELD_OFFSET (repr), 1))
4519 bitoffset = (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
4520 - tree_low_cst (DECL_FIELD_OFFSET (repr), 1)) * BITS_PER_UNIT;
4521 else
4522 bitoffset = 0;
4523 bitoffset += (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
4524 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
1169e45d 4525
1d0bafd9
EB
4526 /* If the adjustment is larger than bitpos, we would have a negative bit
4527 position for the lower bound and this may wreak havoc later. This can
4528 occur only if we have a non-null offset, so adjust offset and bitpos
4529 to make the lower bound non-negative. */
4530 if (bitoffset > *bitpos)
4531 {
4532 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4533
4534 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4535 gcc_assert (*offset != NULL_TREE);
4536
4537 *bitpos += adjust;
4538 *offset
4539 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4540 *bitstart = 0;
4541 }
4542 else
4543 *bitstart = *bitpos - bitoffset;
4544
26c71b93 4545 *bitend = *bitstart + tree_low_cst (DECL_SIZE (repr), 1) - 1;
1169e45d 4546}
8c1cfd5a 4547
22345357
RG
4548/* Returns true if the MEM_REF REF refers to an object that does not
4549 reside in memory and has non-BLKmode. */
4550
4551static bool
4552mem_ref_refers_to_non_mem_p (tree ref)
4553{
4554 tree base = TREE_OPERAND (ref, 0);
4555 if (TREE_CODE (base) != ADDR_EXPR)
4556 return false;
4557 base = TREE_OPERAND (base, 0);
4558 return (DECL_P (base)
4559 && !TREE_ADDRESSABLE (base)
4560 && DECL_MODE (base) != BLKmode
4561 && DECL_RTL_SET_P (base)
4562 && !MEM_P (DECL_RTL (base)));
4563}
4564
79f5e442
ZD
4565/* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4566 is true, try generating a nontemporal store. */
bbf6f052 4567
e836a5a2 4568void
79f5e442 4569expand_assignment (tree to, tree from, bool nontemporal)
bbf6f052 4570{
b3694847 4571 rtx to_rtx = 0;
bbf6f052 4572 rtx result;
be1ac4ec 4573 enum machine_mode mode;
cab35794 4574 unsigned int align;
a5c7d693 4575 enum insn_code icode;
bbf6f052
RK
4576
4577 /* Don't crash if the lhs of the assignment was erroneous. */
bbf6f052 4578 if (TREE_CODE (to) == ERROR_MARK)
709f5be1 4579 {
e16b6fd0 4580 expand_normal (from);
e836a5a2 4581 return;
709f5be1 4582 }
bbf6f052 4583
6cc1d694
RS
4584 /* Optimize away no-op moves without side-effects. */
4585 if (operand_equal_p (to, from, 0))
4586 return;
4587
22345357 4588 /* Handle misaligned stores. */
be1ac4ec
RG
4589 mode = TYPE_MODE (TREE_TYPE (to));
4590 if ((TREE_CODE (to) == MEM_REF
4591 || TREE_CODE (to) == TARGET_MEM_REF)
4592 && mode != BLKmode
43332529 4593 && !mem_ref_refers_to_non_mem_p (to)
cab35794
RG
4594 && ((align = get_object_or_type_alignment (to))
4595 < GET_MODE_ALIGNMENT (mode))
43332529
MJ
4596 && (((icode = optab_handler (movmisalign_optab, mode))
4597 != CODE_FOR_nothing)
4598 || SLOW_UNALIGNED_ACCESS (mode, align)))
be1ac4ec 4599 {
02f6574b 4600 rtx reg, mem;
be1ac4ec
RG
4601
4602 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4603 reg = force_not_mem (reg);
02f6574b 4604 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
be1ac4ec 4605
43332529
MJ
4606 if (icode != CODE_FOR_nothing)
4607 {
02f6574b
MJ
4608 struct expand_operand ops[2];
4609
43332529
MJ
4610 create_fixed_operand (&ops[0], mem);
4611 create_input_operand (&ops[1], reg, mode);
4612 /* The movmisalign<mode> pattern cannot fail, else the assignment
4613 would silently be omitted. */
4614 expand_insn (icode, 2, ops);
4615 }
4616 else
4617 store_bit_field (mem, GET_MODE_BITSIZE (mode),
4618 0, 0, 0, mode, reg);
be1ac4ec
RG
4619 return;
4620 }
4621
bbf6f052
RK
4622 /* Assignment of a structure component needs special treatment
4623 if the structure component's rtx is not simply a MEM.
6be58303
JW
4624 Assignment of an array element at a constant index, and assignment of
4625 an array element in an unaligned packed structure field, has the same
22345357 4626 problem. Same for (partially) storing into a non-memory object. */
8c1cfd5a 4627 if (handled_component_p (to)
70f34814 4628 || (TREE_CODE (to) == MEM_REF
22345357 4629 && mem_ref_refers_to_non_mem_p (to))
7c02ae17 4630 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
bbf6f052
RK
4631 {
4632 enum machine_mode mode1;
770ae6cc 4633 HOST_WIDE_INT bitsize, bitpos;
1169e45d
AH
4634 unsigned HOST_WIDE_INT bitregion_start = 0;
4635 unsigned HOST_WIDE_INT bitregion_end = 0;
7bb0943f 4636 tree offset;
bbf6f052
RK
4637 int unsignedp;
4638 int volatilep = 0;
0088fcb1 4639 tree tem;
22345357 4640 bool misalignp;
a3017cf1 4641 rtx mem = NULL_RTX;
0088fcb1
RK
4642
4643 push_temp_slots ();
839c4796 4644 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
2614034e 4645 &unsignedp, &volatilep, true);
bbf6f052 4646
1169e45d
AH
4647 if (TREE_CODE (to) == COMPONENT_REF
4648 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
1d0bafd9 4649 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
1169e45d 4650
bbf6f052
RK
4651 /* If we are going to use store_bit_field and extract_bit_field,
4652 make sure to_rtx will be safe for multiple use. */
22345357
RG
4653 mode = TYPE_MODE (TREE_TYPE (tem));
4654 if (TREE_CODE (tem) == MEM_REF
4655 && mode != BLKmode
4656 && ((align = get_object_or_type_alignment (tem))
4657 < GET_MODE_ALIGNMENT (mode))
4658 && ((icode = optab_handler (movmisalign_optab, mode))
4659 != CODE_FOR_nothing))
4660 {
a3017cf1 4661 struct expand_operand ops[2];
a3017cf1 4662
22345357
RG
4663 misalignp = true;
4664 to_rtx = gen_reg_rtx (mode);
02f6574b 4665 mem = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
a3017cf1
JJ
4666
4667 /* If the misaligned store doesn't overwrite all bits, perform
4668 rmw cycle on MEM. */
4669 if (bitsize != GET_MODE_BITSIZE (mode))
4670 {
4671 create_input_operand (&ops[0], to_rtx, mode);
4672 create_fixed_operand (&ops[1], mem);
4673 /* The movmisalign<mode> pattern cannot fail, else the assignment
4674 would silently be omitted. */
4675 expand_insn (icode, 2, ops);
4676
4677 mem = copy_rtx (mem);
4678 }
22345357
RG
4679 }
4680 else
4681 {
4682 misalignp = false;
02f6574b 4683 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
22345357 4684 }
1ed1b4fb 4685
6a78b724 4686 /* If the bitfield is volatile, we want to access it in the
1004f0c5
JJ
4687 field's mode, not the computed mode.
4688 If a MEM has VOIDmode (external with incomplete type),
4689 use BLKmode for it instead. */
4690 if (MEM_P (to_rtx))
4691 {
4692 if (volatilep && flag_strict_volatile_bitfields > 0)
4693 to_rtx = adjust_address (to_rtx, mode1, 0);
4694 else if (GET_MODE (to_rtx) == VOIDmode)
4695 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4696 }
6a78b724 4697
7bb0943f
RS
4698 if (offset != 0)
4699 {
d4ebfa65 4700 enum machine_mode address_mode;
1e188d1e 4701 rtx offset_rtx;
7bb0943f 4702
1e188d1e
RH
4703 if (!MEM_P (to_rtx))
4704 {
4705 /* We can get constant negative offsets into arrays with broken
4706 user code. Translate this to a trap instead of ICEing. */
4707 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4708 expand_builtin_trap ();
4709 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4710 }
bd070e1a 4711
1e188d1e 4712 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
372d6395 4713 address_mode = get_address_mode (to_rtx);
d4ebfa65
BE
4714 if (GET_MODE (offset_rtx) != address_mode)
4715 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
bd070e1a 4716
9a7b9f4f
JL
4717 /* A constant address in TO_RTX can have VOIDmode, we must not try
4718 to call force_reg for that case. Avoid that case. */
3c0cb5de 4719 if (MEM_P (to_rtx)
89752202 4720 && GET_MODE (to_rtx) == BLKmode
9a7b9f4f 4721 && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
a06ef755 4722 && bitsize > 0
3a94c984 4723 && (bitpos % bitsize) == 0
89752202 4724 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
a06ef755 4725 && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
89752202 4726 {
e3c8ea67 4727 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
89752202
HB
4728 bitpos = 0;
4729 }
4730
0d4903b8 4731 to_rtx = offset_address (to_rtx, offset_rtx,
d50a16c4
EB
4732 highest_pow2_factor_for_target (to,
4733 offset));
7bb0943f 4734 }
c5c76735 4735
6a866023
JJ
4736 /* No action is needed if the target is not a memory and the field
4737 lies completely outside that target. This can occur if the source
4738 code contains an out-of-bounds access to a small array. */
4739 if (!MEM_P (to_rtx)
4740 && GET_MODE (to_rtx) != BLKmode
4741 && (unsigned HOST_WIDE_INT) bitpos
69660a70 4742 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
6a866023
JJ
4743 {
4744 expand_normal (from);
4745 result = NULL;
4746 }
8c1cfd5a 4747 /* Handle expand_expr of a complex value returning a CONCAT. */
6a866023 4748 else if (GET_CODE (to_rtx) == CONCAT)
a06ef755 4749 {
19228b93
JJ
4750 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4751 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4752 && bitpos == 0
4753 && bitsize == mode_bitsize)
4754 result = store_expr (from, to_rtx, false, nontemporal);
4755 else if (bitsize == mode_bitsize / 2
4756 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4757 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4758 nontemporal);
4759 else if (bitpos + bitsize <= mode_bitsize / 2)
4760 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
1169e45d 4761 bitregion_start, bitregion_end,
19228b93
JJ
4762 mode1, from, TREE_TYPE (tem),
4763 get_alias_set (to), nontemporal);
4764 else if (bitpos >= mode_bitsize / 2)
4765 result = store_field (XEXP (to_rtx, 1), bitsize,
1169e45d
AH
4766 bitpos - mode_bitsize / 2,
4767 bitregion_start, bitregion_end,
4768 mode1, from,
19228b93
JJ
4769 TREE_TYPE (tem), get_alias_set (to),
4770 nontemporal);
4771 else if (bitpos == 0 && bitsize == mode_bitsize)
0becc986 4772 {
19228b93
JJ
4773 rtx from_rtx;
4774 result = expand_normal (from);
4775 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4776 TYPE_MODE (TREE_TYPE (from)), 0);
4777 emit_move_insn (XEXP (to_rtx, 0),
4778 read_complex_part (from_rtx, false));
4779 emit_move_insn (XEXP (to_rtx, 1),
4780 read_complex_part (from_rtx, true));
0becc986
RH
4781 }
4782 else
4783 {
19228b93 4784 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
9474e8ab 4785 GET_MODE_SIZE (GET_MODE (to_rtx)));
19228b93
JJ
4786 write_complex_part (temp, XEXP (to_rtx, 0), false);
4787 write_complex_part (temp, XEXP (to_rtx, 1), true);
1169e45d
AH
4788 result = store_field (temp, bitsize, bitpos,
4789 bitregion_start, bitregion_end,
4790 mode1, from,
19228b93
JJ
4791 TREE_TYPE (tem), get_alias_set (to),
4792 nontemporal);
4793 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4794 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
0becc986 4795 }
bbf6f052 4796 }
8c1cfd5a 4797 else
df62f18a 4798 {
8c1cfd5a 4799 if (MEM_P (to_rtx))
b8b139c7 4800 {
8c1cfd5a
RH
4801 /* If the field is at offset zero, we could have been given the
4802 DECL_RTX of the parent struct. Don't munge it. */
4803 to_rtx = shallow_copy_rtx (to_rtx);
b8b139c7 4804
8c1cfd5a 4805 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
b8b139c7 4806
8c1cfd5a
RH
4807 /* Deal with volatile and readonly fields. The former is only
4808 done for MEM. Also set MEM_KEEP_ALIAS_SET_P if needed. */
4809 if (volatilep)
4810 MEM_VOLATILE_P (to_rtx) = 1;
2039d7aa 4811 if (component_uses_parent_alias_set (to))
8c1cfd5a 4812 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
df62f18a 4813 }
60ba25bf 4814
1169e45d
AH
4815 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4816 bitregion_start, bitregion_end,
4817 mode1,
8c1cfd5a
RH
4818 to_rtx, to, from))
4819 result = NULL;
4820 else
1169e45d
AH
4821 result = store_field (to_rtx, bitsize, bitpos,
4822 bitregion_start, bitregion_end,
4823 mode1, from,
79f5e442
ZD
4824 TREE_TYPE (tem), get_alias_set (to),
4825 nontemporal);
df62f18a
JJ
4826 }
4827
22345357
RG
4828 if (misalignp)
4829 {
4830 struct expand_operand ops[2];
22345357
RG
4831
4832 create_fixed_operand (&ops[0], mem);
4833 create_input_operand (&ops[1], to_rtx, mode);
4834 /* The movmisalign<mode> pattern cannot fail, else the assignment
4835 would silently be omitted. */
4836 expand_insn (icode, 2, ops);
4837 }
4838
8c1cfd5a
RH
4839 if (result)
4840 preserve_temp_slots (result);
a06ef755 4841 pop_temp_slots ();
e836a5a2 4842 return;
bbf6f052
RK
4843 }
4844
cd1db108
RS
4845 /* If the rhs is a function call and its value is not an aggregate,
4846 call the function before we start to compute the lhs.
4847 This is needed for correct code for cases such as
4848 val = setjmp (buf) on machines where reference to val
1ad87b63
RK
4849 requires loading up part of an address in a separate insn.
4850
1858863b
JW
4851 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4852 since it might be a promoted variable where the zero- or sign- extension
4853 needs to be done. Handling this in the normal way is safe because no
4e3825db 4854 computation is done before the call. The same is true for SSA names. */
61f71b34 4855 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
90d245c5 4856 && COMPLETE_TYPE_P (TREE_TYPE (from))
b35cd3c1 4857 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
2ba87a29
RS
4858 && ! (((TREE_CODE (to) == VAR_DECL
4859 || TREE_CODE (to) == PARM_DECL
4860 || TREE_CODE (to) == RESULT_DECL)
4e3825db
MM
4861 && REG_P (DECL_RTL (to)))
4862 || TREE_CODE (to) == SSA_NAME))
cd1db108 4863 {
0088fcb1
RK
4864 rtx value;
4865
4866 push_temp_slots ();
84217346 4867 value = expand_normal (from);
cd1db108 4868 if (to_rtx == 0)
37a08a29 4869 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
aaf87c45 4870
fffa9c1d
JW
4871 /* Handle calls that return values in multiple non-contiguous locations.
4872 The Irix 6 ABI has examples of this. */
4873 if (GET_CODE (to_rtx) == PARALLEL)
6e985040
AM
4874 emit_group_load (to_rtx, value, TREE_TYPE (from),
4875 int_size_in_bytes (TREE_TYPE (from)));
fffa9c1d 4876 else if (GET_MODE (to_rtx) == BLKmode)
44bb111a 4877 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
aaf87c45 4878 else
6419e5b0 4879 {
5ae6cd0d 4880 if (POINTER_TYPE_P (TREE_TYPE (to)))
d4ebfa65
BE
4881 value = convert_memory_address_addr_space
4882 (GET_MODE (to_rtx), value,
4883 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
4884
6419e5b0
DT
4885 emit_move_insn (to_rtx, value);
4886 }
cd1db108 4887 preserve_temp_slots (to_rtx);
0088fcb1 4888 pop_temp_slots ();
e836a5a2 4889 return;
cd1db108
RS
4890 }
4891
22345357
RG
4892 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
4893 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
bbf6f052 4894
86d38d25 4895 /* Don't move directly into a return register. */
14a774a9 4896 if (TREE_CODE (to) == RESULT_DECL
f8cfc6aa 4897 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
86d38d25 4898 {
0088fcb1
RK
4899 rtx temp;
4900
4901 push_temp_slots ();
2ba87a29
RS
4902 if (REG_P (to_rtx) && TYPE_MODE (TREE_TYPE (from)) == BLKmode)
4903 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
4904 else
4905 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
14a774a9
RK
4906
4907 if (GET_CODE (to_rtx) == PARALLEL)
6e985040
AM
4908 emit_group_load (to_rtx, temp, TREE_TYPE (from),
4909 int_size_in_bytes (TREE_TYPE (from)));
2ba87a29 4910 else if (temp)
14a774a9
RK
4911 emit_move_insn (to_rtx, temp);
4912
86d38d25 4913 preserve_temp_slots (to_rtx);
0088fcb1 4914 pop_temp_slots ();
e836a5a2 4915 return;
86d38d25
RS
4916 }
4917
bbf6f052
RK
4918 /* In case we are returning the contents of an object which overlaps
4919 the place the value is being stored, use a safe function when copying
4920 a value through a pointer into a structure value return block. */
434c8f4b
RG
4921 if (TREE_CODE (to) == RESULT_DECL
4922 && TREE_CODE (from) == INDIRECT_REF
09e881c9 4923 && ADDR_SPACE_GENERIC_P
434c8f4b
RG
4924 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
4925 && refs_may_alias_p (to, from)
e3b5732b
JH
4926 && cfun->returns_struct
4927 && !cfun->returns_pcc_struct)
bbf6f052 4928 {
0088fcb1
RK
4929 rtx from_rtx, size;
4930
4931 push_temp_slots ();
33a20d10 4932 size = expr_size (from);
84217346 4933 from_rtx = expand_normal (from);
bbf6f052 4934
8f99553f
JM
4935 emit_library_call (memmove_libfunc, LCT_NORMAL,
4936 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
4937 XEXP (from_rtx, 0), Pmode,
4938 convert_to_mode (TYPE_MODE (sizetype),
4939 size, TYPE_UNSIGNED (sizetype)),
4940 TYPE_MODE (sizetype));
bbf6f052
RK
4941
4942 preserve_temp_slots (to_rtx);
0088fcb1 4943 pop_temp_slots ();
e836a5a2 4944 return;
bbf6f052
RK
4945 }
4946
4947 /* Compute FROM and store the value in the rtx we got. */
4948
0088fcb1 4949 push_temp_slots ();
79f5e442 4950 result = store_expr (from, to_rtx, 0, nontemporal);
bbf6f052 4951 preserve_temp_slots (result);
0088fcb1 4952 pop_temp_slots ();
e836a5a2 4953 return;
bbf6f052
RK
4954}
4955
79f5e442
ZD
4956/* Emits nontemporal store insn that moves FROM to TO. Returns true if this
4957 succeeded, false otherwise. */
4958
28ed065e 4959bool
79f5e442
ZD
4960emit_storent_insn (rtx to, rtx from)
4961{
a5c7d693
RS
4962 struct expand_operand ops[2];
4963 enum machine_mode mode = GET_MODE (to);
947131ba 4964 enum insn_code code = optab_handler (storent_optab, mode);
79f5e442
ZD
4965
4966 if (code == CODE_FOR_nothing)
4967 return false;
4968
a5c7d693
RS
4969 create_fixed_operand (&ops[0], to);
4970 create_input_operand (&ops[1], from, mode);
4971 return maybe_expand_insn (code, 2, ops);
79f5e442
ZD
4972}
4973
bbf6f052
RK
4974/* Generate code for computing expression EXP,
4975 and storing the value into TARGET.
bbf6f052 4976
709f5be1
RS
4977 If the mode is BLKmode then we may return TARGET itself.
4978 It turns out that in BLKmode it doesn't cause a problem.
4979 because C has no operators that could combine two different
4980 assignments into the same BLKmode object with different values
4981 with no sequence point. Will other languages need this to
4982 be more thorough?
4983
6f4fd16d 4984 If CALL_PARAM_P is nonzero, this is a store into a call param on the
79f5e442 4985 stack, and block moves may need to be treated specially.
b8698a0f 4986
79f5e442 4987 If NONTEMPORAL is true, try using a nontemporal store instruction. */
bbf6f052
RK
4988
4989rtx
79f5e442 4990store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
bbf6f052 4991{
b3694847 4992 rtx temp;
0fab64a3 4993 rtx alt_rtl = NULL_RTX;
db3927fb 4994 location_t loc = EXPR_LOCATION (exp);
bbf6f052 4995
847311f4
AL
4996 if (VOID_TYPE_P (TREE_TYPE (exp)))
4997 {
4998 /* C++ can generate ?: expressions with a throw expression in one
4999 branch and an rvalue in the other. Here, we resolve attempts to
4d6922ee 5000 store the throw expression's nonexistent result. */
6f4fd16d 5001 gcc_assert (!call_param_p);
49452c07 5002 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
847311f4
AL
5003 return NULL_RTX;
5004 }
bbf6f052
RK
5005 if (TREE_CODE (exp) == COMPOUND_EXPR)
5006 {
5007 /* Perform first part of compound expression, then assign from second
5008 part. */
8403445a 5009 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6f4fd16d 5010 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
79f5e442
ZD
5011 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5012 nontemporal);
bbf6f052
RK
5013 }
5014 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5015 {
5016 /* For conditional expression, get safe form of the target. Then
5017 test the condition, doing the appropriate assignment on either
5018 side. This avoids the creation of unnecessary temporaries.
5019 For non-BLKmode, it is more efficient not to do this. */
5020
5021 rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
5022
dabf8373 5023 do_pending_stack_adjust ();
bbf6f052 5024 NO_DEFER_POP;
40e90eac 5025 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
79f5e442
ZD
5026 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5027 nontemporal);
bbf6f052
RK
5028 emit_jump_insn (gen_jump (lab2));
5029 emit_barrier ();
5030 emit_label (lab1);
79f5e442
ZD
5031 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5032 nontemporal);
bbf6f052
RK
5033 emit_label (lab2);
5034 OK_DEFER_POP;
a3a58acc 5035
436d948e 5036 return NULL_RTX;
12f06d17 5037 }
1499e0a8 5038 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
09da1532 5039 /* If this is a scalar in a register that is stored in a wider mode
1499e0a8
RK
5040 than the declared mode, compute the result into its declared mode
5041 and then convert to the wider mode. Our value is the computed
5042 expression. */
5043 {
b76b08ef
RK
5044 rtx inner_target = 0;
5045
436d948e
KH
5046 /* We can do the conversion inside EXP, which will often result
5047 in some optimizations. Do the conversion in two steps: first
5048 change the signedness, if needed, then the extend. But don't
5049 do this if the type of EXP is a subtype of something else
5050 since then the conversion might involve more than just
5051 converting modes. */
5052 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
7e7d1b4b 5053 && TREE_TYPE (TREE_TYPE (exp)) == 0
ac5dc795
PB
5054 && GET_MODE_PRECISION (GET_MODE (target))
5055 == TYPE_PRECISION (TREE_TYPE (exp)))
f635a84d 5056 {
8df83eae 5057 if (TYPE_UNSIGNED (TREE_TYPE (exp))
f635a84d 5058 != SUBREG_PROMOTED_UNSIGNED_P (target))
fdd84500
RH
5059 {
5060 /* Some types, e.g. Fortran's logical*4, won't have a signed
5061 version, so use the mode instead. */
5062 tree ntype
12753674 5063 = (signed_or_unsigned_type_for
fdd84500
RH
5064 (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
5065 if (ntype == NULL)
5066 ntype = lang_hooks.types.type_for_mode
5067 (TYPE_MODE (TREE_TYPE (exp)),
5068 SUBREG_PROMOTED_UNSIGNED_P (target));
5069
db3927fb 5070 exp = fold_convert_loc (loc, ntype, exp);
fdd84500 5071 }
f635a84d 5072
db3927fb
AH
5073 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5074 (GET_MODE (SUBREG_REG (target)),
5075 SUBREG_PROMOTED_UNSIGNED_P (target)),
5076 exp);
b76b08ef
RK
5077
5078 inner_target = SUBREG_REG (target);
f635a84d 5079 }
3a94c984 5080
8403445a 5081 temp = expand_expr (exp, inner_target, VOIDmode,
6f4fd16d 5082 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
b258707c
RS
5083
5084 /* If TEMP is a VOIDmode constant, use convert_modes to make
5085 sure that we properly convert it. */
5086 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1f1b0541
RH
5087 {
5088 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5089 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
5090 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5091 GET_MODE (target), temp,
5092 SUBREG_PROMOTED_UNSIGNED_P (target));
5093 }
b258707c 5094
1499e0a8
RK
5095 convert_move (SUBREG_REG (target), temp,
5096 SUBREG_PROMOTED_UNSIGNED_P (target));
3dbecef9 5097
436d948e 5098 return NULL_RTX;
1499e0a8 5099 }
a4feaa71
JJ
5100 else if ((TREE_CODE (exp) == STRING_CST
5101 || (TREE_CODE (exp) == MEM_REF
5102 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5103 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5104 == STRING_CST
5105 && integer_zerop (TREE_OPERAND (exp, 1))))
14a43348 5106 && !nontemporal && !call_param_p
a4feaa71 5107 && MEM_P (target))
14a43348
JJ
5108 {
5109 /* Optimize initialization of an array with a STRING_CST. */
5110 HOST_WIDE_INT exp_len, str_copy_len;
5111 rtx dest_mem;
a4feaa71
JJ
5112 tree str = TREE_CODE (exp) == STRING_CST
5113 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
14a43348
JJ
5114
5115 exp_len = int_expr_size (exp);
5116 if (exp_len <= 0)
5117 goto normal_expr;
5118
a4feaa71 5119 if (TREE_STRING_LENGTH (str) <= 0)
70f34814
RG
5120 goto normal_expr;
5121
5122 str_copy_len = strlen (TREE_STRING_POINTER (str));
5123 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5124 goto normal_expr;
5125
5126 str_copy_len = TREE_STRING_LENGTH (str);
a4feaa71
JJ
5127 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5128 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
70f34814
RG
5129 {
5130 str_copy_len += STORE_MAX_PIECES - 1;
5131 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5132 }
5133 str_copy_len = MIN (str_copy_len, exp_len);
5134 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
a4feaa71 5135 CONST_CAST (char *, TREE_STRING_POINTER (str)),
70f34814
RG
5136 MEM_ALIGN (target), false))
5137 goto normal_expr;
5138
5139 dest_mem = target;
5140
5141 dest_mem = store_by_pieces (dest_mem,
5142 str_copy_len, builtin_strncpy_read_str,
a4feaa71
JJ
5143 CONST_CAST (char *,
5144 TREE_STRING_POINTER (str)),
70f34814
RG
5145 MEM_ALIGN (target), false,
5146 exp_len > str_copy_len ? 1 : 0);
5147 if (exp_len > str_copy_len)
5148 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5149 GEN_INT (exp_len - str_copy_len),
5150 BLOCK_OP_NORMAL);
5151 return NULL_RTX;
5152 }
bbf6f052
RK
5153 else
5154 {
79f5e442
ZD
5155 rtx tmp_target;
5156
14a43348 5157 normal_expr:
79f5e442
ZD
5158 /* If we want to use a nontemporal store, force the value to
5159 register first. */
5160 tmp_target = nontemporal ? NULL_RTX : target;
5161 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
6f4fd16d 5162 (call_param_p
0fab64a3
MM
5163 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5164 &alt_rtl);
bbf6f052
RK
5165 }
5166
b258707c
RS
5167 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5168 the same as that of TARGET, adjust the constant. This is needed, for
5169 example, in case it is a CONST_DOUBLE and we want only a word-sized
5170 value. */
5171 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
c1da1f33 5172 && TREE_CODE (exp) != ERROR_MARK
b258707c
RS
5173 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5174 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
8df83eae 5175 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
b258707c 5176
bbf6f052 5177 /* If value was not generated in the target, store it there.
1bbd65cd
EB
5178 Convert the value to TARGET's type first if necessary and emit the
5179 pending incrementations that have been queued when expanding EXP.
5180 Note that we cannot emit the whole queue blindly because this will
5181 effectively disable the POST_INC optimization later.
5182
37a08a29 5183 If TEMP and TARGET compare equal according to rtx_equal_p, but
f3f2255a
R
5184 one or both of them are volatile memory refs, we have to distinguish
5185 two cases:
5186 - expand_expr has used TARGET. In this case, we must not generate
5187 another copy. This can be detected by TARGET being equal according
5188 to == .
5189 - expand_expr has not used TARGET - that means that the source just
5190 happens to have the same RTX form. Since temp will have been created
5191 by expand_expr, it will compare unequal according to == .
5192 We must generate a copy in this case, to reach the correct number
5193 of volatile memory references. */
bbf6f052 5194
6036acbb 5195 if ((! rtx_equal_p (temp, target)
f3f2255a
R
5196 || (temp != target && (side_effects_p (temp)
5197 || side_effects_p (target))))
e5408e52 5198 && TREE_CODE (exp) != ERROR_MARK
9c5c5f2c
MM
5199 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5200 but TARGET is not valid memory reference, TEMP will differ
5201 from TARGET although it is really the same location. */
ef72faaf
JJ
5202 && !(alt_rtl
5203 && rtx_equal_p (alt_rtl, target)
5204 && !side_effects_p (alt_rtl)
5205 && !side_effects_p (target))
535a42b1
NS
5206 /* If there's nothing to copy, don't bother. Don't call
5207 expr_size unless necessary, because some front-ends (C++)
5208 expr_size-hook must not be given objects that are not
5209 supposed to be bit-copied or bit-initialized. */
e56fc090 5210 && expr_size (exp) != const0_rtx)
bbf6f052 5211 {
bbf6f052 5212 if (GET_MODE (temp) != GET_MODE (target)
f0348c25 5213 && GET_MODE (temp) != VOIDmode)
bbf6f052 5214 {
8df83eae 5215 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
28ed065e 5216 if (GET_MODE (target) == BLKmode
29e9efdf 5217 && GET_MODE (temp) == BLKmode)
ed1223ba
EC
5218 emit_block_move (target, temp, expr_size (exp),
5219 (call_param_p
5220 ? BLOCK_OP_CALL_PARM
5221 : BLOCK_OP_NORMAL));
29e9efdf
RG
5222 else if (GET_MODE (target) == BLKmode)
5223 store_bit_field (target, INTVAL (expr_size (exp)) * BITS_PER_UNIT,
1169e45d 5224 0, 0, 0, GET_MODE (temp), temp);
bbf6f052
RK
5225 else
5226 convert_move (target, temp, unsignedp);
5227 }
5228
5229 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5230 {
c24ae149
RK
5231 /* Handle copying a string constant into an array. The string
5232 constant may be shorter than the array. So copy just the string's
5233 actual length, and clear the rest. First get the size of the data
5234 type of the string, which is actually the size of the target. */
5235 rtx size = expr_size (exp);
bbf6f052 5236
481683e1 5237 if (CONST_INT_P (size)
e87b4f3f 5238 && INTVAL (size) < TREE_STRING_LENGTH (exp))
8403445a 5239 emit_block_move (target, temp, size,
6f4fd16d 5240 (call_param_p
8403445a 5241 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
e87b4f3f 5242 else
bbf6f052 5243 {
d4ebfa65
BE
5244 enum machine_mode pointer_mode
5245 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
372d6395 5246 enum machine_mode address_mode = get_address_mode (target);
d4ebfa65 5247
e87b4f3f
RS
5248 /* Compute the size of the data to copy from the string. */
5249 tree copy_size
db3927fb
AH
5250 = size_binop_loc (loc, MIN_EXPR,
5251 make_tree (sizetype, size),
5252 size_int (TREE_STRING_LENGTH (exp)));
8403445a
AM
5253 rtx copy_size_rtx
5254 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6f4fd16d 5255 (call_param_p
8403445a 5256 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
e87b4f3f
RS
5257 rtx label = 0;
5258
5259 /* Copy that much. */
d4ebfa65 5260 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
8df83eae 5261 TYPE_UNSIGNED (sizetype));
8403445a 5262 emit_block_move (target, temp, copy_size_rtx,
6f4fd16d 5263 (call_param_p
8403445a 5264 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
e87b4f3f 5265
88f63c77 5266 /* Figure out how much is left in TARGET that we have to clear.
d4ebfa65 5267 Do all calculations in pointer_mode. */
481683e1 5268 if (CONST_INT_P (copy_size_rtx))
e87b4f3f 5269 {
0a81f074
RS
5270 size = plus_constant (address_mode, size,
5271 -INTVAL (copy_size_rtx));
c24ae149
RK
5272 target = adjust_address (target, BLKmode,
5273 INTVAL (copy_size_rtx));
e87b4f3f
RS
5274 }
5275 else
5276 {
fa06ab5c 5277 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
906c4e36
RK
5278 copy_size_rtx, NULL_RTX, 0,
5279 OPTAB_LIB_WIDEN);
e87b4f3f 5280
d4ebfa65
BE
5281 if (GET_MODE (copy_size_rtx) != address_mode)
5282 copy_size_rtx = convert_to_mode (address_mode,
5283 copy_size_rtx,
8df83eae 5284 TYPE_UNSIGNED (sizetype));
c24ae149
RK
5285
5286 target = offset_address (target, copy_size_rtx,
5287 highest_pow2_factor (copy_size));
e87b4f3f 5288 label = gen_label_rtx ();
c5d5d461 5289 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
a06ef755 5290 GET_MODE (size), 0, label);
e87b4f3f
RS
5291 }
5292
5293 if (size != const0_rtx)
8148fe65 5294 clear_storage (target, size, BLOCK_OP_NORMAL);
22619c3f 5295
e87b4f3f
RS
5296 if (label)
5297 emit_label (label);
bbf6f052
RK
5298 }
5299 }
fffa9c1d
JW
5300 /* Handle calls that return values in multiple non-contiguous locations.
5301 The Irix 6 ABI has examples of this. */
5302 else if (GET_CODE (target) == PARALLEL)
6e985040
AM
5303 emit_group_load (target, temp, TREE_TYPE (exp),
5304 int_size_in_bytes (TREE_TYPE (exp)));
bbf6f052 5305 else if (GET_MODE (temp) == BLKmode)
8403445a 5306 emit_block_move (target, temp, expr_size (exp),
6f4fd16d 5307 (call_param_p
8403445a 5308 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
79f5e442
ZD
5309 else if (nontemporal
5310 && emit_storent_insn (target, temp))
5311 /* If we managed to emit a nontemporal store, there is nothing else to
5312 do. */
5313 ;
bbf6f052 5314 else
b0dccb00
RH
5315 {
5316 temp = force_operand (temp, target);
5317 if (temp != target)
5318 emit_move_insn (target, temp);
5319 }
bbf6f052 5320 }
709f5be1 5321
436d948e 5322 return NULL_RTX;
bbf6f052
RK
5323}
5324\f
953d0c90
RS
5325/* Return true if field F of structure TYPE is a flexible array. */
5326
5327static bool
5328flexible_array_member_p (const_tree f, const_tree type)
5329{
5330 const_tree tf;
5331
5332 tf = TREE_TYPE (f);
5333 return (DECL_CHAIN (f) == NULL
5334 && TREE_CODE (tf) == ARRAY_TYPE
5335 && TYPE_DOMAIN (tf)
5336 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5337 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5338 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5339 && int_size_in_bytes (type) >= 0);
5340}
5341
5342/* If FOR_CTOR_P, return the number of top-level elements that a constructor
5343 must have in order for it to completely initialize a value of type TYPE.
5344 Return -1 if the number isn't known.
5345
5346 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5347
5348static HOST_WIDE_INT
5349count_type_elements (const_tree type, bool for_ctor_p)
5350{
5351 switch (TREE_CODE (type))
5352 {
5353 case ARRAY_TYPE:
5354 {
5355 tree nelts;
5356
5357 nelts = array_type_nelts (type);
5358 if (nelts && host_integerp (nelts, 1))
5359 {
5360 unsigned HOST_WIDE_INT n;
5361
5362 n = tree_low_cst (nelts, 1) + 1;
5363 if (n == 0 || for_ctor_p)
5364 return n;
5365 else
5366 return n * count_type_elements (TREE_TYPE (type), false);
5367 }
5368 return for_ctor_p ? -1 : 1;
5369 }
5370
5371 case RECORD_TYPE:
5372 {
5373 unsigned HOST_WIDE_INT n;
5374 tree f;
5375
5376 n = 0;
5377 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5378 if (TREE_CODE (f) == FIELD_DECL)
5379 {
5380 if (!for_ctor_p)
5381 n += count_type_elements (TREE_TYPE (f), false);
5382 else if (!flexible_array_member_p (f, type))
5383 /* Don't count flexible arrays, which are not supposed
5384 to be initialized. */
5385 n += 1;
5386 }
5387
5388 return n;
5389 }
5390
5391 case UNION_TYPE:
5392 case QUAL_UNION_TYPE:
5393 {
5394 tree f;
5395 HOST_WIDE_INT n, m;
5396
5397 gcc_assert (!for_ctor_p);
5398 /* Estimate the number of scalars in each field and pick the
5399 maximum. Other estimates would do instead; the idea is simply
5400 to make sure that the estimate is not sensitive to the ordering
5401 of the fields. */
5402 n = 1;
5403 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5404 if (TREE_CODE (f) == FIELD_DECL)
5405 {
5406 m = count_type_elements (TREE_TYPE (f), false);
5407 /* If the field doesn't span the whole union, add an extra
5408 scalar for the rest. */
5409 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5410 TYPE_SIZE (type)) != 1)
5411 m++;
5412 if (n < m)
5413 n = m;
5414 }
5415 return n;
5416 }
5417
5418 case COMPLEX_TYPE:
5419 return 2;
5420
5421 case VECTOR_TYPE:
5422 return TYPE_VECTOR_SUBPARTS (type);
5423
5424 case INTEGER_TYPE:
5425 case REAL_TYPE:
5426 case FIXED_POINT_TYPE:
5427 case ENUMERAL_TYPE:
5428 case BOOLEAN_TYPE:
5429 case POINTER_TYPE:
5430 case OFFSET_TYPE:
5431 case REFERENCE_TYPE:
f298ca26 5432 case NULLPTR_TYPE:
953d0c90
RS
5433 return 1;
5434
5435 case ERROR_MARK:
5436 return 0;
5437
5438 case VOID_TYPE:
5439 case METHOD_TYPE:
5440 case FUNCTION_TYPE:
5441 case LANG_TYPE:
5442 default:
5443 gcc_unreachable ();
5444 }
5445}
5446
fe24d485 5447/* Helper for categorize_ctor_elements. Identical interface. */
9de08200 5448
fe24d485 5449static bool
fa233e34 5450categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
953d0c90 5451 HOST_WIDE_INT *p_init_elts, bool *p_complete)
9de08200 5452{
4038c495 5453 unsigned HOST_WIDE_INT idx;
953d0c90
RS
5454 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5455 tree value, purpose, elt_type;
9de08200 5456
fe24d485
OH
5457 /* Whether CTOR is a valid constant initializer, in accordance with what
5458 initializer_constant_valid_p does. If inferred from the constructor
5459 elements, true until proven otherwise. */
5460 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5461 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5462
6de9cd9a 5463 nz_elts = 0;
953d0c90
RS
5464 init_elts = 0;
5465 num_fields = 0;
5466 elt_type = NULL_TREE;
caf93cb0 5467
4038c495 5468 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
9de08200 5469 {
675c873b 5470 HOST_WIDE_INT mult = 1;
9de08200 5471
6de9cd9a
DN
5472 if (TREE_CODE (purpose) == RANGE_EXPR)
5473 {
5474 tree lo_index = TREE_OPERAND (purpose, 0);
5475 tree hi_index = TREE_OPERAND (purpose, 1);
9de08200 5476
6de9cd9a
DN
5477 if (host_integerp (lo_index, 1) && host_integerp (hi_index, 1))
5478 mult = (tree_low_cst (hi_index, 1)
5479 - tree_low_cst (lo_index, 1) + 1);
5480 }
953d0c90
RS
5481 num_fields += mult;
5482 elt_type = TREE_TYPE (value);
9de08200 5483
6de9cd9a
DN
5484 switch (TREE_CODE (value))
5485 {
5486 case CONSTRUCTOR:
5487 {
fe24d485 5488 HOST_WIDE_INT nz = 0, ic = 0;
ed1223ba 5489
953d0c90
RS
5490 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5491 p_complete);
fe24d485 5492
6de9cd9a 5493 nz_elts += mult * nz;
953d0c90 5494 init_elts += mult * ic;
fe24d485
OH
5495
5496 if (const_from_elts_p && const_p)
5497 const_p = const_elt_p;
6de9cd9a
DN
5498 }
5499 break;
9de08200 5500
6de9cd9a
DN
5501 case INTEGER_CST:
5502 case REAL_CST:
0f996086 5503 case FIXED_CST:
6de9cd9a
DN
5504 if (!initializer_zerop (value))
5505 nz_elts += mult;
953d0c90 5506 init_elts += mult;
6de9cd9a 5507 break;
97f8d136
RK
5508
5509 case STRING_CST:
5510 nz_elts += mult * TREE_STRING_LENGTH (value);
953d0c90 5511 init_elts += mult * TREE_STRING_LENGTH (value);
97f8d136
RK
5512 break;
5513
6de9cd9a
DN
5514 case COMPLEX_CST:
5515 if (!initializer_zerop (TREE_REALPART (value)))
5516 nz_elts += mult;
5517 if (!initializer_zerop (TREE_IMAGPART (value)))
5518 nz_elts += mult;
953d0c90 5519 init_elts += mult;
6de9cd9a 5520 break;
97f8d136 5521
6de9cd9a
DN
5522 case VECTOR_CST:
5523 {
d2a12ae7
RG
5524 unsigned i;
5525 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
6fa91b48 5526 {
d2a12ae7
RG
5527 tree v = VECTOR_CST_ELT (value, i);
5528 if (!initializer_zerop (v))
6fa91b48 5529 nz_elts += mult;
953d0c90 5530 init_elts += mult;
6fa91b48 5531 }
6de9cd9a
DN
5532 }
5533 break;
69ef87e2 5534
6de9cd9a 5535 default:
675c873b 5536 {
953d0c90 5537 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
675c873b 5538 nz_elts += mult * tc;
953d0c90 5539 init_elts += mult * tc;
fe24d485 5540
675c873b 5541 if (const_from_elts_p && const_p)
953d0c90 5542 const_p = initializer_constant_valid_p (value, elt_type)
675c873b
EB
5543 != NULL_TREE;
5544 }
6de9cd9a
DN
5545 break;
5546 }
5547 }
69ef87e2 5548
953d0c90
RS
5549 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5550 num_fields, elt_type))
5551 *p_complete = false;
6f642f98 5552
6de9cd9a 5553 *p_nz_elts += nz_elts;
953d0c90 5554 *p_init_elts += init_elts;
fe24d485
OH
5555
5556 return const_p;
6de9cd9a
DN
5557}
5558
fe24d485
OH
5559/* Examine CTOR to discover:
5560 * how many scalar fields are set to nonzero values,
5561 and place it in *P_NZ_ELTS;
5562 * how many scalar fields in total are in CTOR,
5563 and place it in *P_ELT_COUNT.
953d0c90
RS
5564 * whether the constructor is complete -- in the sense that every
5565 meaningful byte is explicitly given a value --
5566 and place it in *P_COMPLETE.
fe24d485
OH
5567
5568 Return whether or not CTOR is a valid static constant initializer, the same
5569 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5570
5571bool
fa233e34 5572categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
953d0c90 5573 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6de9cd9a
DN
5574{
5575 *p_nz_elts = 0;
953d0c90
RS
5576 *p_init_elts = 0;
5577 *p_complete = true;
fe24d485 5578
953d0c90 5579 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6de9cd9a
DN
5580}
5581
953d0c90
RS
5582/* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5583 of which had type LAST_TYPE. Each element was itself a complete
5584 initializer, in the sense that every meaningful byte was explicitly
5585 given a value. Return true if the same is true for the constructor
5586 as a whole. */
6de9cd9a 5587
953d0c90
RS
5588bool
5589complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5590 const_tree last_type)
6de9cd9a 5591{
953d0c90
RS
5592 if (TREE_CODE (type) == UNION_TYPE
5593 || TREE_CODE (type) == QUAL_UNION_TYPE)
6de9cd9a 5594 {
953d0c90
RS
5595 if (num_elts == 0)
5596 return false;
3a94c984 5597
953d0c90 5598 gcc_assert (num_elts == 1 && last_type);
16d5ffec 5599
953d0c90
RS
5600 /* ??? We could look at each element of the union, and find the
5601 largest element. Which would avoid comparing the size of the
5602 initialized element against any tail padding in the union.
5603 Doesn't seem worth the effort... */
5604 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
9de08200 5605 }
953d0c90
RS
5606
5607 return count_type_elements (type, true) == num_elts;
9de08200
RK
5608}
5609
5610/* Return 1 if EXP contains mostly (3/4) zeros. */
5611
e0ce7708 5612static int
22ea9ec0 5613mostly_zeros_p (const_tree exp)
9de08200 5614{
9de08200
RK
5615 if (TREE_CODE (exp) == CONSTRUCTOR)
5616 {
953d0c90
RS
5617 HOST_WIDE_INT nz_elts, init_elts;
5618 bool complete_p;
6de9cd9a 5619
953d0c90
RS
5620 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5621 return !complete_p || nz_elts < init_elts / 4;
9de08200
RK
5622 }
5623
6de9cd9a 5624 return initializer_zerop (exp);
9de08200 5625}
c5250139
RG
5626
5627/* Return 1 if EXP contains all zeros. */
5628
5629static int
22ea9ec0 5630all_zeros_p (const_tree exp)
c5250139
RG
5631{
5632 if (TREE_CODE (exp) == CONSTRUCTOR)
c5250139 5633 {
953d0c90
RS
5634 HOST_WIDE_INT nz_elts, init_elts;
5635 bool complete_p;
c5250139 5636
953d0c90 5637 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
1f098f07 5638 return nz_elts == 0;
c5250139
RG
5639 }
5640
5641 return initializer_zerop (exp);
5642}
9de08200 5643\f
e1a43f73
PB
5644/* Helper function for store_constructor.
5645 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5646 TYPE is the type of the CONSTRUCTOR, not the element type.
04050c69 5647 CLEARED is as for store_constructor.
23cb1766 5648 ALIAS_SET is the alias set to use for any stores.
23ccec44
JW
5649
5650 This provides a recursive shortcut back to store_constructor when it isn't
5651 necessary to go through store_field. This is so that we can pass through
5652 the cleared field to let store_constructor know that we may not have to
5653 clear a substructure if the outer structure has already been cleared. */
e1a43f73
PB
5654
5655static void
502b8322
AJ
5656store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5657 HOST_WIDE_INT bitpos, enum machine_mode mode,
4862826d
ILT
5658 tree exp, tree type, int cleared,
5659 alias_set_type alias_set)
e1a43f73
PB
5660{
5661 if (TREE_CODE (exp) == CONSTRUCTOR
6c89c39a
RK
5662 /* We can only call store_constructor recursively if the size and
5663 bit position are on a byte boundary. */
23ccec44 5664 && bitpos % BITS_PER_UNIT == 0
6c89c39a 5665 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
cc2902df 5666 /* If we have a nonzero bitpos for a register target, then we just
23ccec44
JW
5667 let store_field do the bitfield handling. This is unlikely to
5668 generate unnecessary clear instructions anyways. */
3c0cb5de 5669 && (bitpos == 0 || MEM_P (target)))
e1a43f73 5670 {
3c0cb5de 5671 if (MEM_P (target))
61cb205c
RK
5672 target
5673 = adjust_address (target,
5674 GET_MODE (target) == BLKmode
5675 || 0 != (bitpos
5676 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5677 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
23cb1766 5678
e0339ef7 5679
04050c69 5680 /* Update the alias set, if required. */
3c0cb5de 5681 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
10b76d73 5682 && MEM_ALIAS_SET (target) != 0)
70072ed9
RK
5683 {
5684 target = copy_rtx (target);
5685 set_mem_alias_set (target, alias_set);
5686 }
e0339ef7 5687
dbb5c281 5688 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
e1a43f73
PB
5689 }
5690 else
1169e45d
AH
5691 store_field (target, bitsize, bitpos, 0, 0, mode, exp, type, alias_set,
5692 false);
e1a43f73
PB
5693}
5694
bbf6f052 5695/* Store the value of constructor EXP into the rtx TARGET.
04050c69
RK
5696 TARGET is either a REG or a MEM; we know it cannot conflict, since
5697 safe_from_p has been called.
dbb5c281
RK
5698 CLEARED is true if TARGET is known to have been zero'd.
5699 SIZE is the number of bytes of TARGET we are allowed to modify: this
b7010412
RK
5700 may not be the same as the size of EXP if we are assigning to a field
5701 which has been packed to exclude padding bits. */
bbf6f052
RK
5702
5703static void
502b8322 5704store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
bbf6f052 5705{
4af3895e 5706 tree type = TREE_TYPE (exp);
a5efcd63 5707#ifdef WORD_REGISTER_OPERATIONS
13eb1f7f 5708 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
a5efcd63 5709#endif
4af3895e 5710
5b0264cb 5711 switch (TREE_CODE (type))
bbf6f052 5712 {
5b0264cb
NS
5713 case RECORD_TYPE:
5714 case UNION_TYPE:
5715 case QUAL_UNION_TYPE:
5716 {
4038c495
GB
5717 unsigned HOST_WIDE_INT idx;
5718 tree field, value;
9de08200 5719
5b0264cb
NS
5720 /* If size is zero or the target is already cleared, do nothing. */
5721 if (size == 0 || cleared)
9de08200 5722 cleared = 1;
5b0264cb
NS
5723 /* We either clear the aggregate or indicate the value is dead. */
5724 else if ((TREE_CODE (type) == UNION_TYPE
5725 || TREE_CODE (type) == QUAL_UNION_TYPE)
5726 && ! CONSTRUCTOR_ELTS (exp))
5727 /* If the constructor is empty, clear the union. */
5728 {
8148fe65 5729 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5b0264cb
NS
5730 cleared = 1;
5731 }
bbf6f052 5732
5b0264cb
NS
5733 /* If we are building a static constructor into a register,
5734 set the initial value as zero so we can fold the value into
5735 a constant. But if more than one register is involved,
5736 this probably loses. */
5737 else if (REG_P (target) && TREE_STATIC (exp)
5738 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5739 {
5740 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5741 cleared = 1;
5742 }
3a94c984 5743
5b0264cb
NS
5744 /* If the constructor has fewer fields than the structure or
5745 if we are initializing the structure to mostly zeros, clear
5746 the whole structure first. Don't do this if TARGET is a
5747 register whose mode size isn't equal to SIZE since
5748 clear_storage can't handle this case. */
5749 else if (size > 0
4038c495 5750 && (((int)VEC_length (constructor_elt, CONSTRUCTOR_ELTS (exp))
5b0264cb
NS
5751 != fields_length (type))
5752 || mostly_zeros_p (exp))
5753 && (!REG_P (target)
5754 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5755 == size)))
5756 {
8148fe65 5757 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5b0264cb
NS
5758 cleared = 1;
5759 }
b50d17a1 5760
58f7fcc3 5761 if (REG_P (target) && !cleared)
c41c1387 5762 emit_clobber (target);
bbf6f052 5763
5b0264cb
NS
5764 /* Store each element of the constructor into the
5765 corresponding field of TARGET. */
4038c495 5766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5b0264cb 5767 {
5b0264cb
NS
5768 enum machine_mode mode;
5769 HOST_WIDE_INT bitsize;
5770 HOST_WIDE_INT bitpos = 0;
5771 tree offset;
5772 rtx to_rtx = target;
ed1223ba 5773
5b0264cb
NS
5774 /* Just ignore missing fields. We cleared the whole
5775 structure, above, if any fields are missing. */
5776 if (field == 0)
5777 continue;
ed1223ba 5778
5b0264cb
NS
5779 if (cleared && initializer_zerop (value))
5780 continue;
ed1223ba 5781
5b0264cb
NS
5782 if (host_integerp (DECL_SIZE (field), 1))
5783 bitsize = tree_low_cst (DECL_SIZE (field), 1);
5784 else
5785 bitsize = -1;
ed1223ba 5786
5b0264cb
NS
5787 mode = DECL_MODE (field);
5788 if (DECL_BIT_FIELD (field))
5789 mode = VOIDmode;
ed1223ba 5790
5b0264cb
NS
5791 offset = DECL_FIELD_OFFSET (field);
5792 if (host_integerp (offset, 0)
5793 && host_integerp (bit_position (field), 0))
5794 {
5795 bitpos = int_bit_position (field);
5796 offset = 0;
5797 }
5798 else
5799 bitpos = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0);
ed1223ba 5800
5b0264cb
NS
5801 if (offset)
5802 {
d4ebfa65 5803 enum machine_mode address_mode;
5b0264cb 5804 rtx offset_rtx;
ed1223ba 5805
5b0264cb
NS
5806 offset
5807 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5808 make_tree (TREE_TYPE (exp),
5809 target));
5810
84217346 5811 offset_rtx = expand_normal (offset);
5b0264cb 5812 gcc_assert (MEM_P (to_rtx));
ed1223ba 5813
372d6395 5814 address_mode = get_address_mode (to_rtx);
d4ebfa65
BE
5815 if (GET_MODE (offset_rtx) != address_mode)
5816 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
bd070e1a 5817
5b0264cb
NS
5818 to_rtx = offset_address (to_rtx, offset_rtx,
5819 highest_pow2_factor (offset));
5820 }
c5c76735 5821
34c73909 5822#ifdef WORD_REGISTER_OPERATIONS
5b0264cb
NS
5823 /* If this initializes a field that is smaller than a
5824 word, at the start of a word, try to widen it to a full
5825 word. This special case allows us to output C++ member
5826 function initializations in a form that the optimizers
5827 can understand. */
5828 if (REG_P (target)
5829 && bitsize < BITS_PER_WORD
5830 && bitpos % BITS_PER_WORD == 0
5831 && GET_MODE_CLASS (mode) == MODE_INT
5832 && TREE_CODE (value) == INTEGER_CST
5833 && exp_size >= 0
5834 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
5835 {
5836 tree type = TREE_TYPE (value);
ed1223ba 5837
5b0264cb
NS
5838 if (TYPE_PRECISION (type) < BITS_PER_WORD)
5839 {
0f250839
RG
5840 type = lang_hooks.types.type_for_mode
5841 (word_mode, TYPE_UNSIGNED (type));
3967bc2d 5842 value = fold_convert (type, value);
5b0264cb 5843 }
ed1223ba 5844
5b0264cb
NS
5845 if (BYTES_BIG_ENDIAN)
5846 value
4845b383 5847 = fold_build2 (LSHIFT_EXPR, type, value,
3967bc2d 5848 build_int_cst (type,
4845b383 5849 BITS_PER_WORD - bitsize));
5b0264cb
NS
5850 bitsize = BITS_PER_WORD;
5851 mode = word_mode;
5852 }
34c73909 5853#endif
10b76d73 5854
5b0264cb
NS
5855 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
5856 && DECL_NONADDRESSABLE_P (field))
5857 {
5858 to_rtx = copy_rtx (to_rtx);
5859 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
5860 }
ed1223ba 5861
5b0264cb
NS
5862 store_constructor_field (to_rtx, bitsize, bitpos, mode,
5863 value, type, cleared,
5864 get_alias_set (TREE_TYPE (field)));
5865 }
5866 break;
5867 }
5868 case ARRAY_TYPE:
5869 {
4038c495
GB
5870 tree value, index;
5871 unsigned HOST_WIDE_INT i;
5b0264cb
NS
5872 int need_to_clear;
5873 tree domain;
5874 tree elttype = TREE_TYPE (type);
5875 int const_bounds_p;
5876 HOST_WIDE_INT minelt = 0;
5877 HOST_WIDE_INT maxelt = 0;
5878
5879 domain = TYPE_DOMAIN (type);
5880 const_bounds_p = (TYPE_MIN_VALUE (domain)
5881 && TYPE_MAX_VALUE (domain)
5882 && host_integerp (TYPE_MIN_VALUE (domain), 0)
5883 && host_integerp (TYPE_MAX_VALUE (domain), 0));
5884
5885 /* If we have constant bounds for the range of the type, get them. */
5886 if (const_bounds_p)
5887 {
5888 minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
5889 maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
5890 }
3a021db2 5891
5b0264cb
NS
5892 /* If the constructor has fewer elements than the array, clear
5893 the whole array first. Similarly if this is static
5894 constructor of a non-BLKmode object. */
5895 if (cleared)
5896 need_to_clear = 0;
5897 else if (REG_P (target) && TREE_STATIC (exp))
5898 need_to_clear = 1;
5899 else
5900 {
4038c495
GB
5901 unsigned HOST_WIDE_INT idx;
5902 tree index, value;
5b0264cb
NS
5903 HOST_WIDE_INT count = 0, zero_count = 0;
5904 need_to_clear = ! const_bounds_p;
ed1223ba 5905
5b0264cb
NS
5906 /* This loop is a more accurate version of the loop in
5907 mostly_zeros_p (it handles RANGE_EXPR in an index). It
5908 is also needed to check for missing elements. */
4038c495 5909 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
5b0264cb 5910 {
5b0264cb 5911 HOST_WIDE_INT this_node_count;
4038c495
GB
5912
5913 if (need_to_clear)
5914 break;
ed1223ba 5915
5b0264cb
NS
5916 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5917 {
5918 tree lo_index = TREE_OPERAND (index, 0);
5919 tree hi_index = TREE_OPERAND (index, 1);
ed1223ba 5920
5b0264cb
NS
5921 if (! host_integerp (lo_index, 1)
5922 || ! host_integerp (hi_index, 1))
5923 {
5924 need_to_clear = 1;
5925 break;
5926 }
ed1223ba 5927
5b0264cb
NS
5928 this_node_count = (tree_low_cst (hi_index, 1)
5929 - tree_low_cst (lo_index, 1) + 1);
5930 }
5931 else
5932 this_node_count = 1;
ed1223ba 5933
5b0264cb 5934 count += this_node_count;
4038c495 5935 if (mostly_zeros_p (value))
5b0264cb
NS
5936 zero_count += this_node_count;
5937 }
ed1223ba 5938
5b0264cb
NS
5939 /* Clear the entire array first if there are any missing
5940 elements, or if the incidence of zero elements is >=
5941 75%. */
5942 if (! need_to_clear
5943 && (count < maxelt - minelt + 1
5944 || 4 * zero_count >= 3 * count))
5945 need_to_clear = 1;
5946 }
ed1223ba 5947
5b0264cb
NS
5948 if (need_to_clear && size > 0)
5949 {
5950 if (REG_P (target))
5951 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5952 else
8148fe65 5953 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5b0264cb
NS
5954 cleared = 1;
5955 }
3a021db2 5956
5b0264cb
NS
5957 if (!cleared && REG_P (target))
5958 /* Inform later passes that the old value is dead. */
c41c1387 5959 emit_clobber (target);
3a021db2 5960
5b0264cb
NS
5961 /* Store each element of the constructor into the
5962 corresponding element of TARGET, determined by counting the
5963 elements. */
4038c495 5964 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
5b0264cb
NS
5965 {
5966 enum machine_mode mode;
5967 HOST_WIDE_INT bitsize;
5968 HOST_WIDE_INT bitpos;
5b0264cb 5969 rtx xtarget = target;
ed1223ba 5970
5b0264cb
NS
5971 if (cleared && initializer_zerop (value))
5972 continue;
ed1223ba 5973
5b0264cb
NS
5974 mode = TYPE_MODE (elttype);
5975 if (mode == BLKmode)
5976 bitsize = (host_integerp (TYPE_SIZE (elttype), 1)
5977 ? tree_low_cst (TYPE_SIZE (elttype), 1)
5978 : -1);
5979 else
5980 bitsize = GET_MODE_BITSIZE (mode);
ed1223ba 5981
5b0264cb
NS
5982 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5983 {
5984 tree lo_index = TREE_OPERAND (index, 0);
5985 tree hi_index = TREE_OPERAND (index, 1);
5986 rtx index_r, pos_rtx;
5987 HOST_WIDE_INT lo, hi, count;
5988 tree position;
ed1223ba 5989
5b0264cb
NS
5990 /* If the range is constant and "small", unroll the loop. */
5991 if (const_bounds_p
5992 && host_integerp (lo_index, 0)
5993 && host_integerp (hi_index, 0)
5994 && (lo = tree_low_cst (lo_index, 0),
5995 hi = tree_low_cst (hi_index, 0),
5996 count = hi - lo + 1,
5997 (!MEM_P (target)
5998 || count <= 2
5999 || (host_integerp (TYPE_SIZE (elttype), 1)
6000 && (tree_low_cst (TYPE_SIZE (elttype), 1) * count
6001 <= 40 * 8)))))
6002 {
6003 lo -= minelt; hi -= minelt;
6004 for (; lo <= hi; lo++)
6005 {
6006 bitpos = lo * tree_low_cst (TYPE_SIZE (elttype), 0);
ed1223ba 6007
5b0264cb
NS
6008 if (MEM_P (target)
6009 && !MEM_KEEP_ALIAS_SET_P (target)
6010 && TREE_CODE (type) == ARRAY_TYPE
6011 && TYPE_NONALIASED_COMPONENT (type))
6012 {
6013 target = copy_rtx (target);
6014 MEM_KEEP_ALIAS_SET_P (target) = 1;
6015 }
ed1223ba 6016
5b0264cb
NS
6017 store_constructor_field
6018 (target, bitsize, bitpos, mode, value, type, cleared,
6019 get_alias_set (elttype));
6020 }
6021 }
6022 else
6023 {
6024 rtx loop_start = gen_label_rtx ();
6025 rtx loop_end = gen_label_rtx ();
6026 tree exit_cond;
ed1223ba 6027
84217346 6028 expand_normal (hi_index);
ed1223ba 6029
c2255bc4
AH
6030 index = build_decl (EXPR_LOCATION (exp),
6031 VAR_DECL, NULL_TREE, domain);
cde0f3fd 6032 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
5b0264cb 6033 SET_DECL_RTL (index, index_r);
79f5e442 6034 store_expr (lo_index, index_r, 0, false);
ed1223ba 6035
5b0264cb
NS
6036 /* Build the head of the loop. */
6037 do_pending_stack_adjust ();
6038 emit_label (loop_start);
6039
6040 /* Assign value to element index. */
3967bc2d
RS
6041 position =
6042 fold_convert (ssizetype,
6043 fold_build2 (MINUS_EXPR,
6044 TREE_TYPE (index),
6045 index,
6046 TYPE_MIN_VALUE (domain)));
6047
6048 position =
6049 size_binop (MULT_EXPR, position,
6050 fold_convert (ssizetype,
6051 TYPE_SIZE_UNIT (elttype)));
ed1223ba 6052
84217346 6053 pos_rtx = expand_normal (position);
5b0264cb
NS
6054 xtarget = offset_address (target, pos_rtx,
6055 highest_pow2_factor (position));
6056 xtarget = adjust_address (xtarget, mode, 0);
6057 if (TREE_CODE (value) == CONSTRUCTOR)
6058 store_constructor (value, xtarget, cleared,
6059 bitsize / BITS_PER_UNIT);
6060 else
79f5e442 6061 store_expr (value, xtarget, 0, false);
5b0264cb
NS
6062
6063 /* Generate a conditional jump to exit the loop. */
6064 exit_cond = build2 (LT_EXPR, integer_type_node,
6065 index, hi_index);
40e90eac 6066 jumpif (exit_cond, loop_end, -1);
ed1223ba 6067
5b0264cb
NS
6068 /* Update the loop counter, and jump to the head of
6069 the loop. */
6070 expand_assignment (index,
6071 build2 (PLUS_EXPR, TREE_TYPE (index),
79f5e442
ZD
6072 index, integer_one_node),
6073 false);
ed1223ba 6074
5b0264cb 6075 emit_jump (loop_start);
ed1223ba 6076
5b0264cb
NS
6077 /* Build the end of the loop. */
6078 emit_label (loop_end);
6079 }
6080 }
6081 else if ((index != 0 && ! host_integerp (index, 0))
6082 || ! host_integerp (TYPE_SIZE (elttype), 1))
6083 {
6084 tree position;
ed1223ba 6085
5b0264cb
NS
6086 if (index == 0)
6087 index = ssize_int (1);
ed1223ba 6088
5b0264cb
NS
6089 if (minelt)
6090 index = fold_convert (ssizetype,
4845b383
KH
6091 fold_build2 (MINUS_EXPR,
6092 TREE_TYPE (index),
6093 index,
6094 TYPE_MIN_VALUE (domain)));
ed1223ba 6095
3967bc2d
RS
6096 position =
6097 size_binop (MULT_EXPR, index,
6098 fold_convert (ssizetype,
6099 TYPE_SIZE_UNIT (elttype)));
5b0264cb 6100 xtarget = offset_address (target,
84217346 6101 expand_normal (position),
5b0264cb
NS
6102 highest_pow2_factor (position));
6103 xtarget = adjust_address (xtarget, mode, 0);
79f5e442 6104 store_expr (value, xtarget, 0, false);
5b0264cb
NS
6105 }
6106 else
6107 {
6108 if (index != 0)
6109 bitpos = ((tree_low_cst (index, 0) - minelt)
6110 * tree_low_cst (TYPE_SIZE (elttype), 1));
6111 else
6112 bitpos = (i * tree_low_cst (TYPE_SIZE (elttype), 1));
ed1223ba 6113
5b0264cb
NS
6114 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6115 && TREE_CODE (type) == ARRAY_TYPE
6116 && TYPE_NONALIASED_COMPONENT (type))
6117 {
6118 target = copy_rtx (target);
6119 MEM_KEEP_ALIAS_SET_P (target) = 1;
6120 }
6121 store_constructor_field (target, bitsize, bitpos, mode, value,
6122 type, cleared, get_alias_set (elttype));
6123 }
6124 }
6125 break;
6126 }
3a021db2 6127
5b0264cb
NS
6128 case VECTOR_TYPE:
6129 {
4038c495
GB
6130 unsigned HOST_WIDE_INT idx;
6131 constructor_elt *ce;
5b0264cb
NS
6132 int i;
6133 int need_to_clear;
6134 int icode = 0;
6135 tree elttype = TREE_TYPE (type);
6136 int elt_size = tree_low_cst (TYPE_SIZE (elttype), 1);
6137 enum machine_mode eltmode = TYPE_MODE (elttype);
6138 HOST_WIDE_INT bitsize;
6139 HOST_WIDE_INT bitpos;
201dd46b 6140 rtvec vector = NULL;
5b0264cb 6141 unsigned n_elts;
723a7ced 6142 alias_set_type alias;
ed1223ba 6143
5b0264cb 6144 gcc_assert (eltmode != BLKmode);
ed1223ba 6145
5b0264cb
NS
6146 n_elts = TYPE_VECTOR_SUBPARTS (type);
6147 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6148 {
6149 enum machine_mode mode = GET_MODE (target);
ed1223ba 6150
947131ba 6151 icode = (int) optab_handler (vec_init_optab, mode);
5b0264cb
NS
6152 if (icode != CODE_FOR_nothing)
6153 {
6154 unsigned int i;
ed1223ba 6155
201dd46b 6156 vector = rtvec_alloc (n_elts);
5b0264cb 6157 for (i = 0; i < n_elts; i++)
201dd46b 6158 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
5b0264cb
NS
6159 }
6160 }
ed1223ba 6161
5b0264cb
NS
6162 /* If the constructor has fewer elements than the vector,
6163 clear the whole array first. Similarly if this is static
6164 constructor of a non-BLKmode object. */
6165 if (cleared)
6166 need_to_clear = 0;
6167 else if (REG_P (target) && TREE_STATIC (exp))
6168 need_to_clear = 1;
6169 else
6170 {
6171 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
4038c495 6172 tree value;
ed1223ba 6173
4038c495 6174 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
5b0264cb
NS
6175 {
6176 int n_elts_here = tree_low_cst
6177 (int_const_binop (TRUNC_DIV_EXPR,
4038c495 6178 TYPE_SIZE (TREE_TYPE (value)),
d35936ab 6179 TYPE_SIZE (elttype)), 1);
ed1223ba 6180
5b0264cb 6181 count += n_elts_here;
4038c495 6182 if (mostly_zeros_p (value))
5b0264cb
NS
6183 zero_count += n_elts_here;
6184 }
3a021db2 6185
5b0264cb
NS
6186 /* Clear the entire vector first if there are any missing elements,
6187 or if the incidence of zero elements is >= 75%. */
6188 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6189 }
ed1223ba 6190
5b0264cb
NS
6191 if (need_to_clear && size > 0 && !vector)
6192 {
6193 if (REG_P (target))
723a7ced 6194 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5b0264cb 6195 else
8148fe65 6196 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5b0264cb
NS
6197 cleared = 1;
6198 }
ed1223ba 6199
2ab1754e 6200 /* Inform later passes that the old value is dead. */
cf26aa89 6201 if (!cleared && !vector && REG_P (target))
2ab1754e 6202 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5b0264cb 6203
723a7ced
JJ
6204 if (MEM_P (target))
6205 alias = MEM_ALIAS_SET (target);
6206 else
6207 alias = get_alias_set (elttype);
6208
5b0264cb
NS
6209 /* Store each element of the constructor into the corresponding
6210 element of TARGET, determined by counting the elements. */
4038c495
GB
6211 for (idx = 0, i = 0;
6212 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce);
6213 idx++, i += bitsize / elt_size)
5b0264cb 6214 {
5b0264cb 6215 HOST_WIDE_INT eltpos;
4038c495 6216 tree value = ce->value;
ed1223ba 6217
5b0264cb
NS
6218 bitsize = tree_low_cst (TYPE_SIZE (TREE_TYPE (value)), 1);
6219 if (cleared && initializer_zerop (value))
6220 continue;
ed1223ba 6221
4038c495
GB
6222 if (ce->index)
6223 eltpos = tree_low_cst (ce->index, 1);
5b0264cb
NS
6224 else
6225 eltpos = i;
ed1223ba 6226
5b0264cb
NS
6227 if (vector)
6228 {
6229 /* Vector CONSTRUCTORs should only be built from smaller
6230 vectors in the case of BLKmode vectors. */
6231 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
201dd46b 6232 RTVEC_ELT (vector, eltpos)
84217346 6233 = expand_normal (value);
5b0264cb
NS
6234 }
6235 else
6236 {
6237 enum machine_mode value_mode =
6238 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
3a021db2
PB
6239 ? TYPE_MODE (TREE_TYPE (value))
6240 : eltmode;
5b0264cb
NS
6241 bitpos = eltpos * elt_size;
6242 store_constructor_field (target, bitsize, bitpos,
6243 value_mode, value, type,
723a7ced 6244 cleared, alias);
5b0264cb
NS
6245 }
6246 }
ed1223ba 6247
5b0264cb
NS
6248 if (vector)
6249 emit_insn (GEN_FCN (icode)
6250 (target,
201dd46b 6251 gen_rtx_PARALLEL (GET_MODE (target), vector)));
5b0264cb
NS
6252 break;
6253 }
ed1223ba 6254
5b0264cb
NS
6255 default:
6256 gcc_unreachable ();
071a6595 6257 }
bbf6f052
RK
6258}
6259
6260/* Store the value of EXP (an expression tree)
6261 into a subfield of TARGET which has mode MODE and occupies
6262 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6263 If MODE is VOIDmode, it means that we are storing into a bit-field.
6264
1169e45d
AH
6265 BITREGION_START is bitpos of the first bitfield in this region.
6266 BITREGION_END is the bitpos of the ending bitfield in this region.
6267 These two fields are 0, if the C++ memory model does not apply,
6268 or we are not interested in keeping track of bitfield regions.
6269
f45bdcd0
KH
6270 Always return const0_rtx unless we have something particular to
6271 return.
bbf6f052 6272
a06ef755 6273 TYPE is the type of the underlying object,
ece32014
MM
6274
6275 ALIAS_SET is the alias set for the destination. This value will
6276 (in general) be different from that for TARGET, since TARGET is a
79f5e442 6277 reference to the containing structure.
b8698a0f 6278
79f5e442 6279 If NONTEMPORAL is true, try generating a nontemporal store. */
bbf6f052
RK
6280
6281static rtx
502b8322 6282store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
1169e45d
AH
6283 unsigned HOST_WIDE_INT bitregion_start,
6284 unsigned HOST_WIDE_INT bitregion_end,
4862826d
ILT
6285 enum machine_mode mode, tree exp, tree type,
6286 alias_set_type alias_set, bool nontemporal)
bbf6f052 6287{
e9a25f70
JL
6288 if (TREE_CODE (exp) == ERROR_MARK)
6289 return const0_rtx;
6290
2be6a7e9
RK
6291 /* If we have nothing to store, do nothing unless the expression has
6292 side-effects. */
6293 if (bitsize == 0)
49452c07 6294 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
bbf6f052
RK
6295
6296 /* If we are storing into an unaligned field of an aligned union that is
6297 in a register, we may have the mode of TARGET being an integer mode but
6298 MODE == BLKmode. In that case, get an aligned object whose size and
6299 alignment are the same as TARGET and store TARGET into it (we can avoid
6300 the store if the field being stored is the entire width of TARGET). Then
6301 call ourselves recursively to store the field into a BLKmode version of
6302 that object. Finally, load from the object into TARGET. This is not
6303 very efficient in general, but should only be slightly more expensive
6304 than the otherwise-required unaligned accesses. Perhaps this can be
85a43a2f
RK
6305 cleaned up later. It's tempting to make OBJECT readonly, but it's set
6306 twice, once with emit_move_insn and once via store_field. */
bbf6f052
RK
6307
6308 if (mode == BLKmode
f8cfc6aa 6309 && (REG_P (target) || GET_CODE (target) == SUBREG))
bbf6f052 6310 {
9474e8ab 6311 rtx object = assign_temp (type, 1, 1);
c4e59f51 6312 rtx blk_object = adjust_address (object, BLKmode, 0);
bbf6f052 6313
8752c357 6314 if (bitsize != (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (target)))
bbf6f052
RK
6315 emit_move_insn (object, target);
6316
1169e45d
AH
6317 store_field (blk_object, bitsize, bitpos,
6318 bitregion_start, bitregion_end,
45c83429 6319 mode, exp, type, MEM_ALIAS_SET (blk_object), nontemporal);
bbf6f052
RK
6320
6321 emit_move_insn (target, object);
6322
a06ef755 6323 /* We want to return the BLKmode version of the data. */
46093b97 6324 return blk_object;
bbf6f052 6325 }
c3b247b4
JM
6326
6327 if (GET_CODE (target) == CONCAT)
6328 {
6329 /* We're storing into a struct containing a single __complex. */
6330
5b0264cb 6331 gcc_assert (!bitpos);
79f5e442 6332 return store_expr (exp, target, 0, nontemporal);
c3b247b4 6333 }
bbf6f052
RK
6334
6335 /* If the structure is in a register or if the component
6336 is a bit field, we cannot use addressing to access it.
6337 Use bit-field techniques or SUBREG to store in it. */
6338
4fa52007 6339 if (mode == VOIDmode
6ab06cbb
JW
6340 || (mode != BLKmode && ! direct_store[(int) mode]
6341 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6342 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
f8cfc6aa 6343 || REG_P (target)
c980ac49 6344 || GET_CODE (target) == SUBREG
ccc98036
RS
6345 /* If the field isn't aligned enough to store as an ordinary memref,
6346 store it as a bit field. */
15b19a7d 6347 || (mode != BLKmode
9e5f281f
OH
6348 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6349 || bitpos % GET_MODE_ALIGNMENT (mode))
6350 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
502b8322 6351 || (bitpos % BITS_PER_UNIT != 0)))
36acc1a2
BS
6352 || (bitsize >= 0 && mode != BLKmode
6353 && GET_MODE_BITSIZE (mode) > bitsize)
14a774a9
RK
6354 /* If the RHS and field are a constant size and the size of the
6355 RHS isn't the same size as the bitfield, we must use bitfield
6356 operations. */
05bccae2
RK
6357 || (bitsize >= 0
6358 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
70f34814
RG
6359 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6360 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6361 decl we must use bitfield operations. */
6362 || (bitsize >= 0
6363 && TREE_CODE (exp) == MEM_REF
6364 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6365 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6366 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6367 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
bbf6f052 6368 {
48cc8d3b 6369 rtx temp;
641cac0b 6370 gimple nop_def;
48cc8d3b
RH
6371
6372 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6373 implies a mask operation. If the precision is the same size as
6374 the field we're storing into, that mask is redundant. This is
6375 particularly common with bit field assignments generated by the
6376 C front end. */
641cac0b
AN
6377 nop_def = get_def_for_expr (exp, NOP_EXPR);
6378 if (nop_def)
8d740330
RH
6379 {
6380 tree type = TREE_TYPE (exp);
6381 if (INTEGRAL_TYPE_P (type)
6382 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6383 && bitsize == TYPE_PRECISION (type))
6384 {
641cac0b
AN
6385 tree op = gimple_assign_rhs1 (nop_def);
6386 type = TREE_TYPE (op);
8d740330 6387 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
641cac0b 6388 exp = op;
8d740330
RH
6389 }
6390 }
48cc8d3b 6391
84217346 6392 temp = expand_normal (exp);
bbd6cf73 6393
ef19912d
RK
6394 /* If BITSIZE is narrower than the size of the type of EXP
6395 we will be narrowing TEMP. Normally, what's wanted are the
6396 low-order bits. However, if EXP's type is a record and this is
6397 big-endian machine, we want the upper BITSIZE bits. */
6398 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
65a07688 6399 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
ef19912d
RK
6400 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6401 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
eb6c3df1 6402 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
c1853da7 6403 NULL_RTX, 1);
ef19912d 6404
8c5f2327 6405 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
bbd6cf73
RK
6406 if (mode != VOIDmode && mode != BLKmode
6407 && mode != TYPE_MODE (TREE_TYPE (exp)))
6408 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6409
bd323ce5 6410 /* If the modes of TEMP and TARGET are both BLKmode, both
a281e72d 6411 must be in memory and BITPOS must be aligned on a byte
bd323ce5
EB
6412 boundary. If so, we simply do a block copy. Likewise
6413 for a BLKmode-like TARGET. */
6414 if (GET_MODE (temp) == BLKmode
6415 && (GET_MODE (target) == BLKmode
6416 || (MEM_P (target)
6417 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6418 && (bitpos % BITS_PER_UNIT) == 0
6419 && (bitsize % BITS_PER_UNIT) == 0)))
a281e72d 6420 {
5b0264cb 6421 gcc_assert (MEM_P (target) && MEM_P (temp)
bd323ce5 6422 && (bitpos % BITS_PER_UNIT) == 0);
a281e72d 6423
f4ef873c 6424 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
a281e72d 6425 emit_block_move (target, temp,
a06ef755 6426 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
44bb111a
RH
6427 / BITS_PER_UNIT),
6428 BLOCK_OP_NORMAL);
a281e72d 6429
f45bdcd0 6430 return const0_rtx;
a281e72d
RK
6431 }
6432
bbf6f052 6433 /* Store the value in the bitfield. */
1169e45d
AH
6434 store_bit_field (target, bitsize, bitpos,
6435 bitregion_start, bitregion_end,
6436 mode, temp);
a06ef755 6437
bbf6f052
RK
6438 return const0_rtx;
6439 }
6440 else
6441 {
bbf6f052 6442 /* Now build a reference to just the desired component. */
f45bdcd0 6443 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
a06ef755
RK
6444
6445 if (to_rtx == target)
6446 to_rtx = copy_rtx (to_rtx);
792760b9 6447
10b76d73 6448 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
a06ef755 6449 set_mem_alias_set (to_rtx, alias_set);
bbf6f052 6450
79f5e442 6451 return store_expr (exp, to_rtx, 0, nontemporal);
bbf6f052
RK
6452 }
6453}
6454\f
6455/* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
b4e3fabb
RK
6456 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6457 codes and find the ultimate containing object, which we return.
bbf6f052
RK
6458
6459 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6460 bit position, and *PUNSIGNEDP to the signedness of the field.
7bb0943f
RS
6461 If the position of the field is variable, we store a tree
6462 giving the variable offset (in units) in *POFFSET.
6463 This offset is in addition to the bit position.
6464 If the position is not variable, we store 0 in *POFFSET.
bbf6f052
RK
6465
6466 If any of the extraction expressions is volatile,
6467 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6468
bd323ce5
EB
6469 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6470 Otherwise, it is a mode that can be used to access the field.
e7c33f54
RK
6471
6472 If the field describes a variable-sized object, *PMODE is set to
bd323ce5 6473 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
2614034e
EB
6474 this case, but the address of the object can be found.
6475
6476 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6477 look through nodes that serve as markers of a greater alignment than
6478 the one that can be deduced from the expression. These nodes make it
6479 possible for front-ends to prevent temporaries from being created by
6480 the middle-end on alignment considerations. For that purpose, the
6481 normal operating mode at high-level is to always pass FALSE so that
6482 the ultimate containing object is really returned; moreover, the
6483 associated predicate handled_component_p will always return TRUE
6484 on these nodes, thus indicating that they are essentially handled
6485 by get_inner_reference. TRUE should only be passed when the caller
6486 is scanning the expression in order to build another representation
6487 and specifically knows how to handle these nodes; as such, this is
6488 the normal operating mode in the RTL expanders. */
bbf6f052
RK
6489
6490tree
502b8322
AJ
6491get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6492 HOST_WIDE_INT *pbitpos, tree *poffset,
6493 enum machine_mode *pmode, int *punsignedp,
2614034e 6494 int *pvolatilep, bool keep_aligning)
bbf6f052
RK
6495{
6496 tree size_tree = 0;
6497 enum machine_mode mode = VOIDmode;
bd323ce5 6498 bool blkmode_bitfield = false;
fed3cef0 6499 tree offset = size_zero_node;
7fa5296e 6500 double_int bit_offset = double_int_zero;
bbf6f052 6501
770ae6cc
RK
6502 /* First get the mode, signedness, and size. We do this from just the
6503 outermost expression. */
997ac87b 6504 *pbitsize = -1;
bbf6f052
RK
6505 if (TREE_CODE (exp) == COMPONENT_REF)
6506 {
bd323ce5
EB
6507 tree field = TREE_OPERAND (exp, 1);
6508 size_tree = DECL_SIZE (field);
6509 if (!DECL_BIT_FIELD (field))
6510 mode = DECL_MODE (field);
6511 else if (DECL_MODE (field) == BLKmode)
6512 blkmode_bitfield = true;
6a78b724
DD
6513 else if (TREE_THIS_VOLATILE (exp)
6514 && flag_strict_volatile_bitfields > 0)
6515 /* Volatile bitfields should be accessed in the mode of the
6516 field's type, not the mode computed based on the bit
6517 size. */
6518 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
bd323ce5
EB
6519
6520 *punsignedp = DECL_UNSIGNED (field);
bbf6f052
RK
6521 }
6522 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6523 {
6524 size_tree = TREE_OPERAND (exp, 1);
fc0f49f3
RG
6525 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6526 || TYPE_UNSIGNED (TREE_TYPE (exp)));
ed1223ba 6527
0890b981
AP
6528 /* For vector types, with the correct size of access, use the mode of
6529 inner type. */
6530 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6531 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6532 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6533 mode = TYPE_MODE (TREE_TYPE (exp));
bbf6f052
RK
6534 }
6535 else
6536 {
6537 mode = TYPE_MODE (TREE_TYPE (exp));
8df83eae 6538 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
770ae6cc 6539
ab87f8c8
JL
6540 if (mode == BLKmode)
6541 size_tree = TYPE_SIZE (TREE_TYPE (exp));
770ae6cc
RK
6542 else
6543 *pbitsize = GET_MODE_BITSIZE (mode);
bbf6f052 6544 }
3a94c984 6545
770ae6cc 6546 if (size_tree != 0)
bbf6f052 6547 {
770ae6cc 6548 if (! host_integerp (size_tree, 1))
e7c33f54
RK
6549 mode = BLKmode, *pbitsize = -1;
6550 else
770ae6cc 6551 *pbitsize = tree_low_cst (size_tree, 1);
bbf6f052
RK
6552 }
6553
6554 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6555 and find the ultimate containing object. */
bbf6f052
RK
6556 while (1)
6557 {
afe84921 6558 switch (TREE_CODE (exp))
bbf6f052 6559 {
afe84921 6560 case BIT_FIELD_REF:
7fa5296e
RG
6561 bit_offset
6562 = double_int_add (bit_offset,
6563 tree_to_double_int (TREE_OPERAND (exp, 2)));
afe84921 6564 break;
bbf6f052 6565
afe84921
RH
6566 case COMPONENT_REF:
6567 {
6568 tree field = TREE_OPERAND (exp, 1);
6569 tree this_offset = component_ref_field_offset (exp);
e7f3c83f 6570
afe84921
RH
6571 /* If this field hasn't been filled in yet, don't go past it.
6572 This should only happen when folding expressions made during
6573 type construction. */
6574 if (this_offset == 0)
6575 break;
e6d8c385 6576
afe84921 6577 offset = size_binop (PLUS_EXPR, offset, this_offset);
7fa5296e
RG
6578 bit_offset = double_int_add (bit_offset,
6579 tree_to_double_int
6580 (DECL_FIELD_BIT_OFFSET (field)));
7156dead 6581
afe84921
RH
6582 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6583 }
6584 break;
7156dead 6585
afe84921
RH
6586 case ARRAY_REF:
6587 case ARRAY_RANGE_REF:
6588 {
6589 tree index = TREE_OPERAND (exp, 1);
6590 tree low_bound = array_ref_low_bound (exp);
6591 tree unit_size = array_ref_element_size (exp);
6592
6593 /* We assume all arrays have sizes that are a multiple of a byte.
6594 First subtract the lower bound, if any, in the type of the
6595 index, then convert to sizetype and multiply by the size of
6596 the array element. */
6597 if (! integer_zerop (low_bound))
4845b383
KH
6598 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6599 index, low_bound);
afe84921
RH
6600
6601 offset = size_binop (PLUS_EXPR, offset,
6602 size_binop (MULT_EXPR,
3967bc2d 6603 fold_convert (sizetype, index),
afe84921
RH
6604 unit_size));
6605 }
6606 break;
6607
6608 case REALPART_EXPR:
afe84921
RH
6609 break;
6610
6611 case IMAGPART_EXPR:
7fa5296e
RG
6612 bit_offset = double_int_add (bit_offset,
6613 uhwi_to_double_int (*pbitsize));
afe84921
RH
6614 break;
6615
afe84921 6616 case VIEW_CONVERT_EXPR:
2614034e
EB
6617 if (keep_aligning && STRICT_ALIGNMENT
6618 && (TYPE_ALIGN (TREE_TYPE (exp))
afe84921 6619 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
afe84921
RH
6620 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6621 < BIGGEST_ALIGNMENT)
6622 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6623 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6624 goto done;
6625 break;
6626
70f34814
RG
6627 case MEM_REF:
6628 /* Hand back the decl for MEM[&decl, off]. */
6629 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6630 {
6631 tree off = TREE_OPERAND (exp, 1);
6632 if (!integer_zerop (off))
6633 {
6634 double_int boff, coff = mem_ref_offset (exp);
6635 boff = double_int_lshift (coff,
6636 BITS_PER_UNIT == 8
6637 ? 3 : exact_log2 (BITS_PER_UNIT),
6638 HOST_BITS_PER_DOUBLE_INT, true);
6639 bit_offset = double_int_add (bit_offset, boff);
6640 }
6641 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6642 }
6643 goto done;
6644
afe84921
RH
6645 default:
6646 goto done;
6647 }
7bb0943f
RS
6648
6649 /* If any reference in the chain is volatile, the effect is volatile. */
6650 if (TREE_THIS_VOLATILE (exp))
6651 *pvolatilep = 1;
839c4796 6652
bbf6f052
RK
6653 exp = TREE_OPERAND (exp, 0);
6654 }
afe84921 6655 done:
bbf6f052 6656
770ae6cc 6657 /* If OFFSET is constant, see if we can return the whole thing as a
9c219b9b
RG
6658 constant bit position. Make sure to handle overflow during
6659 this conversion. */
97919ae7
RG
6660 if (TREE_CODE (offset) == INTEGER_CST)
6661 {
6662 double_int tem = tree_to_double_int (offset);
6663 tem = double_int_sext (tem, TYPE_PRECISION (sizetype));
fdb4f315 6664 tem = double_int_lshift (tem,
97919ae7
RG
6665 BITS_PER_UNIT == 8
6666 ? 3 : exact_log2 (BITS_PER_UNIT),
6667 HOST_BITS_PER_DOUBLE_INT, true);
7fa5296e 6668 tem = double_int_add (tem, bit_offset);
9c219b9b
RG
6669 if (double_int_fits_in_shwi_p (tem))
6670 {
6671 *pbitpos = double_int_to_shwi (tem);
bd323ce5 6672 *poffset = offset = NULL_TREE;
9c219b9b
RG
6673 }
6674 }
6675
6676 /* Otherwise, split it up. */
bd323ce5
EB
6677 if (offset)
6678 {
f22cfd73
EB
6679 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6680 if (double_int_negative_p (bit_offset))
6681 {
6682 double_int mask
6683 = double_int_mask (BITS_PER_UNIT == 8
6684 ? 3 : exact_log2 (BITS_PER_UNIT));
6685 double_int tem = double_int_and_not (bit_offset, mask);
6686 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6687 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6688 bit_offset = double_int_sub (bit_offset, tem);
6689 tem = double_int_rshift (tem,
6690 BITS_PER_UNIT == 8
6691 ? 3 : exact_log2 (BITS_PER_UNIT),
6692 HOST_BITS_PER_DOUBLE_INT, true);
6693 offset = size_binop (PLUS_EXPR, offset,
6694 double_int_to_tree (sizetype, tem));
6695 }
6696
7fa5296e 6697 *pbitpos = double_int_to_shwi (bit_offset);
bd323ce5
EB
6698 *poffset = offset;
6699 }
6700
6701 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6702 if (mode == VOIDmode
6703 && blkmode_bitfield
6704 && (*pbitpos % BITS_PER_UNIT) == 0
6705 && (*pbitsize % BITS_PER_UNIT) == 0)
6706 *pmode = BLKmode;
6707 else
6708 *pmode = mode;
b50d17a1 6709
bbf6f052
RK
6710 return exp;
6711}
921b3427 6712
9f7ccf69
EB
6713/* Given an expression EXP that may be a COMPONENT_REF, an ARRAY_REF or an
6714 ARRAY_RANGE_REF, look for whether EXP or any nested component-refs within
6715 EXP is marked as PACKED. */
5b900a4c
DN
6716
6717bool
fa233e34 6718contains_packed_reference (const_tree exp)
5b900a4c
DN
6719{
6720 bool packed_p = false;
6721
6722 while (1)
6723 {
6724 switch (TREE_CODE (exp))
6725 {
6726 case COMPONENT_REF:
6727 {
6728 tree field = TREE_OPERAND (exp, 1);
b8698a0f 6729 packed_p = DECL_PACKED (field)
5b900a4c
DN
6730 || TYPE_PACKED (TREE_TYPE (field))
6731 || TYPE_PACKED (TREE_TYPE (exp));
6732 if (packed_p)
6733 goto done;
6734 }
6735 break;
6736
6737 case BIT_FIELD_REF:
6738 case ARRAY_REF:
6739 case ARRAY_RANGE_REF:
6740 case REALPART_EXPR:
6741 case IMAGPART_EXPR:
6742 case VIEW_CONVERT_EXPR:
6743 break;
6744
6745 default:
6746 goto done;
6747 }
6748 exp = TREE_OPERAND (exp, 0);
6749 }
6750 done:
6751 return packed_p;
6752}
6753
44de5aeb 6754/* Return a tree of sizetype representing the size, in bytes, of the element
9f7ccf69 6755 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
44de5aeb
RK
6756
6757tree
6758array_ref_element_size (tree exp)
6759{
6760 tree aligned_size = TREE_OPERAND (exp, 3);
6761 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
db3927fb 6762 location_t loc = EXPR_LOCATION (exp);
44de5aeb
RK
6763
6764 /* If a size was specified in the ARRAY_REF, it's the size measured
6765 in alignment units of the element type. So multiply by that value. */
6766 if (aligned_size)
bc482be4
RH
6767 {
6768 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6769 sizetype from another type of the same width and signedness. */
6770 if (TREE_TYPE (aligned_size) != sizetype)
db3927fb
AH
6771 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6772 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6773 size_int (TYPE_ALIGN_UNIT (elmt_type)));
bc482be4 6774 }
44de5aeb 6775
caf93cb0 6776 /* Otherwise, take the size from that of the element type. Substitute
44de5aeb
RK
6777 any PLACEHOLDER_EXPR that we have. */
6778 else
6779 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6780}
6781
6782/* Return a tree representing the lower bound of the array mentioned in
9f7ccf69 6783 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
44de5aeb
RK
6784
6785tree
6786array_ref_low_bound (tree exp)
6787{
6788 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6789
6790 /* If a lower bound is specified in EXP, use it. */
6791 if (TREE_OPERAND (exp, 2))
6792 return TREE_OPERAND (exp, 2);
6793
6794 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6795 substituting for a PLACEHOLDER_EXPR as needed. */
6796 if (domain_type && TYPE_MIN_VALUE (domain_type))
6797 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6798
6799 /* Otherwise, return a zero of the appropriate type. */
5212068f 6800 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
44de5aeb
RK
6801}
6802
d8279b12
RG
6803/* Returns true if REF is an array reference to an array at the end of
6804 a structure. If this is the case, the array may be allocated larger
6805 than its upper bound implies. */
6806
6807bool
6808array_at_struct_end_p (tree ref)
6809{
6810 if (TREE_CODE (ref) != ARRAY_REF
6811 && TREE_CODE (ref) != ARRAY_RANGE_REF)
6812 return false;
6813
6814 while (handled_component_p (ref))
6815 {
6816 /* If the reference chain contains a component reference to a
6817 non-union type and there follows another field the reference
6818 is not at the end of a structure. */
6819 if (TREE_CODE (ref) == COMPONENT_REF
6820 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6821 {
6822 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6823 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6824 nextf = DECL_CHAIN (nextf);
6825 if (nextf)
6826 return false;
6827 }
6828
6829 ref = TREE_OPERAND (ref, 0);
6830 }
6831
6832 /* If the reference is based on a declared entity, the size of the array
6833 is constrained by its given domain. */
6834 if (DECL_P (ref))
6835 return false;
6836
6837 return true;
6838}
6839
a7e5372d 6840/* Return a tree representing the upper bound of the array mentioned in
9f7ccf69 6841 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
a7e5372d
ZD
6842
6843tree
6844array_ref_up_bound (tree exp)
6845{
6846 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6847
6848 /* If there is a domain type and it has an upper bound, use it, substituting
6849 for a PLACEHOLDER_EXPR as needed. */
6850 if (domain_type && TYPE_MAX_VALUE (domain_type))
6851 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6852
6853 /* Otherwise fail. */
6854 return NULL_TREE;
6855}
6856
44de5aeb
RK
6857/* Return a tree representing the offset, in bytes, of the field referenced
6858 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
6859
6860tree
6861component_ref_field_offset (tree exp)
6862{
6863 tree aligned_offset = TREE_OPERAND (exp, 2);
6864 tree field = TREE_OPERAND (exp, 1);
db3927fb 6865 location_t loc = EXPR_LOCATION (exp);
44de5aeb
RK
6866
6867 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
6868 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
6869 value. */
6870 if (aligned_offset)
bc482be4
RH
6871 {
6872 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6873 sizetype from another type of the same width and signedness. */
6874 if (TREE_TYPE (aligned_offset) != sizetype)
db3927fb
AH
6875 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
6876 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
6877 size_int (DECL_OFFSET_ALIGN (field)
6878 / BITS_PER_UNIT));
bc482be4 6879 }
44de5aeb 6880
caf93cb0 6881 /* Otherwise, take the offset from that of the field. Substitute
44de5aeb
RK
6882 any PLACEHOLDER_EXPR that we have. */
6883 else
6884 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
6885}
ceadb728
OH
6886
6887/* Alignment in bits the TARGET of an assignment may be assumed to have. */
6888
6889static unsigned HOST_WIDE_INT
6890target_align (const_tree target)
6891{
6892 /* We might have a chain of nested references with intermediate misaligning
6893 bitfields components, so need to recurse to find out. */
6894
6895 unsigned HOST_WIDE_INT this_align, outer_align;
6896
6897 switch (TREE_CODE (target))
6898 {
6899 case BIT_FIELD_REF:
6900 return 1;
6901
6902 case COMPONENT_REF:
6903 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
6904 outer_align = target_align (TREE_OPERAND (target, 0));
6905 return MIN (this_align, outer_align);
6906
6907 case ARRAY_REF:
6908 case ARRAY_RANGE_REF:
6909 this_align = TYPE_ALIGN (TREE_TYPE (target));
6910 outer_align = target_align (TREE_OPERAND (target, 0));
6911 return MIN (this_align, outer_align);
6912
6913 CASE_CONVERT:
6914 case NON_LVALUE_EXPR:
6915 case VIEW_CONVERT_EXPR:
6916 this_align = TYPE_ALIGN (TREE_TYPE (target));
6917 outer_align = target_align (TREE_OPERAND (target, 0));
6918 return MAX (this_align, outer_align);
6919
6920 default:
6921 return TYPE_ALIGN (TREE_TYPE (target));
6922 }
6923}
6924
bbf6f052 6925\f
3fe44edd
RK
6926/* Given an rtx VALUE that may contain additions and multiplications, return
6927 an equivalent value that just refers to a register, memory, or constant.
6928 This is done by generating instructions to perform the arithmetic and
6929 returning a pseudo-register containing the value.
c45a13a6
RK
6930
6931 The returned value may be a REG, SUBREG, MEM or constant. */
bbf6f052
RK
6932
6933rtx
502b8322 6934force_operand (rtx value, rtx target)
bbf6f052 6935{
8a28dbcc 6936 rtx op1, op2;
bbf6f052 6937 /* Use subtarget as the target for operand 0 of a binary operation. */
b3694847 6938 rtx subtarget = get_subtarget (target);
8a28dbcc 6939 enum rtx_code code = GET_CODE (value);
bbf6f052 6940
50654f6c
ZD
6941 /* Check for subreg applied to an expression produced by loop optimizer. */
6942 if (code == SUBREG
f8cfc6aa 6943 && !REG_P (SUBREG_REG (value))
3c0cb5de 6944 && !MEM_P (SUBREG_REG (value)))
50654f6c 6945 {
b7e6d1da
UB
6946 value
6947 = simplify_gen_subreg (GET_MODE (value),
6948 force_reg (GET_MODE (SUBREG_REG (value)),
6949 force_operand (SUBREG_REG (value),
6950 NULL_RTX)),
6951 GET_MODE (SUBREG_REG (value)),
6952 SUBREG_BYTE (value));
50654f6c
ZD
6953 code = GET_CODE (value);
6954 }
6955
8b015896 6956 /* Check for a PIC address load. */
8a28dbcc 6957 if ((code == PLUS || code == MINUS)
8b015896
RH
6958 && XEXP (value, 0) == pic_offset_table_rtx
6959 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
6960 || GET_CODE (XEXP (value, 1)) == LABEL_REF
6961 || GET_CODE (XEXP (value, 1)) == CONST))
6962 {
6963 if (!subtarget)
6964 subtarget = gen_reg_rtx (GET_MODE (value));
6965 emit_move_insn (subtarget, value);
6966 return subtarget;
6967 }
6968
ec8e098d 6969 if (ARITHMETIC_P (value))
bbf6f052
RK
6970 {
6971 op2 = XEXP (value, 1);
f8cfc6aa 6972 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
bbf6f052 6973 subtarget = 0;
481683e1 6974 if (code == MINUS && CONST_INT_P (op2))
bbf6f052 6975 {
8a28dbcc 6976 code = PLUS;
bbf6f052
RK
6977 op2 = negate_rtx (GET_MODE (value), op2);
6978 }
6979
6980 /* Check for an addition with OP2 a constant integer and our first
8a28dbcc
JH
6981 operand a PLUS of a virtual register and something else. In that
6982 case, we want to emit the sum of the virtual register and the
6983 constant first and then add the other value. This allows virtual
6984 register instantiation to simply modify the constant rather than
6985 creating another one around this addition. */
481683e1 6986 if (code == PLUS && CONST_INT_P (op2)
bbf6f052 6987 && GET_CODE (XEXP (value, 0)) == PLUS
f8cfc6aa 6988 && REG_P (XEXP (XEXP (value, 0), 0))
bbf6f052
RK
6989 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
6990 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
6991 {
8a28dbcc
JH
6992 rtx temp = expand_simple_binop (GET_MODE (value), code,
6993 XEXP (XEXP (value, 0), 0), op2,
6994 subtarget, 0, OPTAB_LIB_WIDEN);
6995 return expand_simple_binop (GET_MODE (value), code, temp,
6996 force_operand (XEXP (XEXP (value,
6997 0), 1), 0),
6998 target, 0, OPTAB_LIB_WIDEN);
bbf6f052 6999 }
3a94c984 7000
8a28dbcc
JH
7001 op1 = force_operand (XEXP (value, 0), subtarget);
7002 op2 = force_operand (op2, NULL_RTX);
7003 switch (code)
7004 {
7005 case MULT:
7006 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7007 case DIV:
7008 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7009 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7010 target, 1, OPTAB_LIB_WIDEN);
7011 else
7012 return expand_divmod (0,
7013 FLOAT_MODE_P (GET_MODE (value))
7014 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7015 GET_MODE (value), op1, op2, target, 0);
8a28dbcc
JH
7016 case MOD:
7017 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7018 target, 0);
8a28dbcc
JH
7019 case UDIV:
7020 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7021 target, 1);
8a28dbcc
JH
7022 case UMOD:
7023 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7024 target, 1);
8a28dbcc
JH
7025 case ASHIFTRT:
7026 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7027 target, 0, OPTAB_LIB_WIDEN);
8a28dbcc
JH
7028 default:
7029 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7030 target, 1, OPTAB_LIB_WIDEN);
7031 }
7032 }
ec8e098d 7033 if (UNARY_P (value))
8a28dbcc 7034 {
72a10eff
RS
7035 if (!target)
7036 target = gen_reg_rtx (GET_MODE (value));
8a28dbcc 7037 op1 = force_operand (XEXP (value, 0), NULL_RTX);
1fd5360d
R
7038 switch (code)
7039 {
72a10eff
RS
7040 case ZERO_EXTEND:
7041 case SIGN_EXTEND:
1fd5360d 7042 case TRUNCATE:
e69e3d0e
ZD
7043 case FLOAT_EXTEND:
7044 case FLOAT_TRUNCATE:
72a10eff
RS
7045 convert_move (target, op1, code == ZERO_EXTEND);
7046 return target;
7047
7048 case FIX:
7049 case UNSIGNED_FIX:
7050 expand_fix (target, op1, code == UNSIGNED_FIX);
7051 return target;
7052
7053 case FLOAT:
7054 case UNSIGNED_FLOAT:
7055 expand_float (target, op1, code == UNSIGNED_FLOAT);
7056 return target;
7057
1fd5360d
R
7058 default:
7059 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7060 }
bbf6f052 7061 }
34e81b5a
RK
7062
7063#ifdef INSN_SCHEDULING
7064 /* On machines that have insn scheduling, we want all memory reference to be
7065 explicit, so we need to deal with such paradoxical SUBREGs. */
6a4bdc79 7066 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
34e81b5a
RK
7067 value
7068 = simplify_gen_subreg (GET_MODE (value),
7069 force_reg (GET_MODE (SUBREG_REG (value)),
7070 force_operand (SUBREG_REG (value),
7071 NULL_RTX)),
7072 GET_MODE (SUBREG_REG (value)),
7073 SUBREG_BYTE (value));
7074#endif
7075
bbf6f052
RK
7076 return value;
7077}
7078\f
bbf6f052 7079/* Subroutine of expand_expr: return nonzero iff there is no way that
e5e809f4
JL
7080 EXP can reference X, which is being modified. TOP_P is nonzero if this
7081 call is going to be used to determine whether we need a temporary
ff439b5f
CB
7082 for EXP, as opposed to a recursive call to this function.
7083
7084 It is always safe for this routine to return zero since it merely
7085 searches for optimization opportunities. */
bbf6f052 7086
8f17b5c5 7087int
22ea9ec0 7088safe_from_p (const_rtx x, tree exp, int top_p)
bbf6f052
RK
7089{
7090 rtx exp_rtl = 0;
7091 int i, nops;
7092
6676e72f
RK
7093 if (x == 0
7094 /* If EXP has varying size, we MUST use a target since we currently
8f6562d0
PB
7095 have no way of allocating temporaries of variable size
7096 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7097 So we assume here that something at a higher level has prevented a
f4510f37 7098 clash. This is somewhat bogus, but the best we can do. Only
e5e809f4 7099 do this when X is BLKmode and when we are at the top level. */
d0f062fb 7100 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
f4510f37 7101 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8f6562d0
PB
7102 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7103 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7104 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7105 != INTEGER_CST)
1da68f56
RK
7106 && GET_MODE (x) == BLKmode)
7107 /* If X is in the outgoing argument area, it is always safe. */
3c0cb5de 7108 || (MEM_P (x)
1da68f56
RK
7109 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7110 || (GET_CODE (XEXP (x, 0)) == PLUS
7111 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
bbf6f052
RK
7112 return 1;
7113
7114 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7115 find the underlying pseudo. */
7116 if (GET_CODE (x) == SUBREG)
7117 {
7118 x = SUBREG_REG (x);
f8cfc6aa 7119 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
bbf6f052
RK
7120 return 0;
7121 }
7122
1da68f56 7123 /* Now look at our tree code and possibly recurse. */
bbf6f052
RK
7124 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7125 {
6615c446 7126 case tcc_declaration:
a9772b60 7127 exp_rtl = DECL_RTL_IF_SET (exp);
bbf6f052
RK
7128 break;
7129
6615c446 7130 case tcc_constant:
bbf6f052
RK
7131 return 1;
7132
6615c446 7133 case tcc_exceptional:
bbf6f052 7134 if (TREE_CODE (exp) == TREE_LIST)
f8d4be57
CE
7135 {
7136 while (1)
7137 {
7138 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7139 return 0;
7140 exp = TREE_CHAIN (exp);
7141 if (!exp)
7142 return 1;
7143 if (TREE_CODE (exp) != TREE_LIST)
7144 return safe_from_p (x, exp, 0);
7145 }
7146 }
33598a1b
BS
7147 else if (TREE_CODE (exp) == CONSTRUCTOR)
7148 {
7149 constructor_elt *ce;
7150 unsigned HOST_WIDE_INT idx;
7151
ac47786e 7152 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce)
33598a1b
BS
7153 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7154 || !safe_from_p (x, ce->value, 0))
7155 return 0;
7156 return 1;
7157 }
ff439b5f
CB
7158 else if (TREE_CODE (exp) == ERROR_MARK)
7159 return 1; /* An already-visited SAVE_EXPR? */
bbf6f052
RK
7160 else
7161 return 0;
7162
6615c446 7163 case tcc_statement:
350fae66
RK
7164 /* The only case we look at here is the DECL_INITIAL inside a
7165 DECL_EXPR. */
7166 return (TREE_CODE (exp) != DECL_EXPR
7167 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7168 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7169 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7170
6615c446
JO
7171 case tcc_binary:
7172 case tcc_comparison:
f8d4be57
CE
7173 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7174 return 0;
5d3cc252 7175 /* Fall through. */
f8d4be57 7176
6615c446 7177 case tcc_unary:
f8d4be57 7178 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
bbf6f052 7179
6615c446
JO
7180 case tcc_expression:
7181 case tcc_reference:
5039610b 7182 case tcc_vl_exp:
bbf6f052
RK
7183 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7184 the expression. If it is set, we conflict iff we are that rtx or
7185 both are in memory. Otherwise, we check all operands of the
7186 expression recursively. */
7187
7188 switch (TREE_CODE (exp))
7189 {
7190 case ADDR_EXPR:
70072ed9
RK
7191 /* If the operand is static or we are static, we can't conflict.
7192 Likewise if we don't conflict with the operand at all. */
7193 if (staticp (TREE_OPERAND (exp, 0))
7194 || TREE_STATIC (exp)
7195 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7196 return 1;
7197
7198 /* Otherwise, the only way this can conflict is if we are taking
7199 the address of a DECL a that address if part of X, which is
7200 very rare. */
7201 exp = TREE_OPERAND (exp, 0);
7202 if (DECL_P (exp))
7203 {
7204 if (!DECL_RTL_SET_P (exp)
3c0cb5de 7205 || !MEM_P (DECL_RTL (exp)))
70072ed9
RK
7206 return 0;
7207 else
7208 exp_rtl = XEXP (DECL_RTL (exp), 0);
7209 }
7210 break;
bbf6f052 7211
be1ac4ec 7212 case MEM_REF:
3c0cb5de 7213 if (MEM_P (x)
1da68f56
RK
7214 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7215 get_alias_set (exp)))
bbf6f052
RK
7216 return 0;
7217 break;
7218
7219 case CALL_EXPR:
f9808f81
MM
7220 /* Assume that the call will clobber all hard registers and
7221 all of memory. */
f8cfc6aa 7222 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
3c0cb5de 7223 || MEM_P (x))
f9808f81 7224 return 0;
bbf6f052
RK
7225 break;
7226
bbf6f052 7227 case WITH_CLEANUP_EXPR:
5dab5552 7228 case CLEANUP_POINT_EXPR:
ac45df5d 7229 /* Lowered by gimplify.c. */
5b0264cb 7230 gcc_unreachable ();
ac45df5d 7231
bbf6f052 7232 case SAVE_EXPR:
82c82743 7233 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
bbf6f052 7234
e9a25f70
JL
7235 default:
7236 break;
bbf6f052
RK
7237 }
7238
7239 /* If we have an rtx, we do not need to scan our operands. */
7240 if (exp_rtl)
7241 break;
7242
5039610b 7243 nops = TREE_OPERAND_LENGTH (exp);
bbf6f052
RK
7244 for (i = 0; i < nops; i++)
7245 if (TREE_OPERAND (exp, i) != 0
e5e809f4 7246 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
bbf6f052 7247 return 0;
8f17b5c5 7248
6615c446
JO
7249 break;
7250
7251 case tcc_type:
7252 /* Should never get a type here. */
7253 gcc_unreachable ();
bbf6f052
RK
7254 }
7255
7256 /* If we have an rtl, find any enclosed object. Then see if we conflict
7257 with it. */
7258 if (exp_rtl)
7259 {
7260 if (GET_CODE (exp_rtl) == SUBREG)
7261 {
7262 exp_rtl = SUBREG_REG (exp_rtl);
f8cfc6aa 7263 if (REG_P (exp_rtl)
bbf6f052
RK
7264 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7265 return 0;
7266 }
7267
7268 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
1da68f56 7269 are memory and they conflict. */
bbf6f052 7270 return ! (rtx_equal_p (x, exp_rtl)
3c0cb5de 7271 || (MEM_P (x) && MEM_P (exp_rtl)
53d9622b 7272 && true_dependence (exp_rtl, VOIDmode, x)));
bbf6f052
RK
7273 }
7274
7275 /* If we reach here, it is safe. */
7276 return 1;
7277}
7278
14a774a9 7279\f
0d4903b8
RK
7280/* Return the highest power of two that EXP is known to be a multiple of.
7281 This is used in updating alignment of MEMs in array references. */
7282
86a07404 7283unsigned HOST_WIDE_INT
fa233e34 7284highest_pow2_factor (const_tree exp)
0d4903b8 7285{
9ceca302 7286 unsigned HOST_WIDE_INT c0, c1;
0d4903b8
RK
7287
7288 switch (TREE_CODE (exp))
7289 {
7290 case INTEGER_CST:
e0f1be5c
JJ
7291 /* We can find the lowest bit that's a one. If the low
7292 HOST_BITS_PER_WIDE_INT bits are zero, return BIGGEST_ALIGNMENT.
7293 We need to handle this case since we can find it in a COND_EXPR,
a98ebe2e 7294 a MIN_EXPR, or a MAX_EXPR. If the constant overflows, we have an
e0f1be5c 7295 erroneous program, so return BIGGEST_ALIGNMENT to avoid any
3a531a8b 7296 later ICE. */
455f14dd 7297 if (TREE_OVERFLOW (exp))
1ed1b4fb 7298 return BIGGEST_ALIGNMENT;
e0f1be5c 7299 else
0d4903b8 7300 {
e0f1be5c
JJ
7301 /* Note: tree_low_cst is intentionally not used here,
7302 we don't care about the upper bits. */
7303 c0 = TREE_INT_CST_LOW (exp);
7304 c0 &= -c0;
7305 return c0 ? c0 : BIGGEST_ALIGNMENT;
0d4903b8
RK
7306 }
7307 break;
7308
65a07688 7309 case PLUS_EXPR: case MINUS_EXPR: case MIN_EXPR: case MAX_EXPR:
0d4903b8
RK
7310 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7311 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7312 return MIN (c0, c1);
7313
7314 case MULT_EXPR:
7315 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7316 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7317 return c0 * c1;
7318
7319 case ROUND_DIV_EXPR: case TRUNC_DIV_EXPR: case FLOOR_DIV_EXPR:
7320 case CEIL_DIV_EXPR:
65a07688
RK
7321 if (integer_pow2p (TREE_OPERAND (exp, 1))
7322 && host_integerp (TREE_OPERAND (exp, 1), 1))
7323 {
7324 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7325 c1 = tree_low_cst (TREE_OPERAND (exp, 1), 1);
7326 return MAX (1, c0 / c1);
7327 }
7328 break;
0d4903b8 7329
a2acd8bf
EB
7330 case BIT_AND_EXPR:
7331 /* The highest power of two of a bit-and expression is the maximum of
7332 that of its operands. We typically get here for a complex LHS and
7333 a constant negative power of two on the RHS to force an explicit
7334 alignment, so don't bother looking at the LHS. */
7335 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7336
1043771b 7337 CASE_CONVERT:
6fce44af 7338 case SAVE_EXPR:
0d4903b8
RK
7339 return highest_pow2_factor (TREE_OPERAND (exp, 0));
7340
65a07688
RK
7341 case COMPOUND_EXPR:
7342 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7343
0d4903b8
RK
7344 case COND_EXPR:
7345 c0 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7346 c1 = highest_pow2_factor (TREE_OPERAND (exp, 2));
7347 return MIN (c0, c1);
7348
7349 default:
7350 break;
7351 }
7352
7353 return 1;
7354}
818c0c94 7355
d50a16c4
EB
7356/* Similar, except that the alignment requirements of TARGET are
7357 taken into account. Assume it is at least as aligned as its
7358 type, unless it is a COMPONENT_REF in which case the layout of
7359 the structure gives the alignment. */
818c0c94 7360
9ceca302 7361static unsigned HOST_WIDE_INT
fa233e34 7362highest_pow2_factor_for_target (const_tree target, const_tree exp)
818c0c94 7363{
ceadb728
OH
7364 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7365 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
b8698a0f 7366
ceadb728 7367 return MAX (factor, talign);
818c0c94 7368}
0d4903b8 7369\f
18c56439 7370#ifdef HAVE_conditional_move
073a8998 7371/* Convert the tree comparison code TCODE to the rtl one where the
683c600b
AP
7372 signedness is UNSIGNEDP. */
7373
7374static enum rtx_code
7375convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7376{
7377 enum rtx_code code;
7378 switch (tcode)
7379 {
7380 case EQ_EXPR:
7381 code = EQ;
7382 break;
7383 case NE_EXPR:
7384 code = NE;
7385 break;
7386 case LT_EXPR:
7387 code = unsignedp ? LTU : LT;
7388 break;
7389 case LE_EXPR:
7390 code = unsignedp ? LEU : LE;
7391 break;
7392 case GT_EXPR:
7393 code = unsignedp ? GTU : GT;
7394 break;
7395 case GE_EXPR:
7396 code = unsignedp ? GEU : GE;
7397 break;
7398 case UNORDERED_EXPR:
7399 code = UNORDERED;
7400 break;
7401 case ORDERED_EXPR:
7402 code = ORDERED;
7403 break;
7404 case UNLT_EXPR:
7405 code = UNLT;
7406 break;
7407 case UNLE_EXPR:
7408 code = UNLE;
7409 break;
7410 case UNGT_EXPR:
7411 code = UNGT;
7412 break;
7413 case UNGE_EXPR:
7414 code = UNGE;
7415 break;
7416 case UNEQ_EXPR:
7417 code = UNEQ;
7418 break;
7419 case LTGT_EXPR:
7420 code = LTGT;
7421 break;
7422
7423 default:
7424 gcc_unreachable ();
7425 }
7426 return code;
7427}
18c56439 7428#endif
683c600b 7429
eb698c58
RS
7430/* Subroutine of expand_expr. Expand the two operands of a binary
7431 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7432 The value may be stored in TARGET if TARGET is nonzero. The
7433 MODIFIER argument is as documented by expand_expr. */
7434
7435static void
7436expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7437 enum expand_modifier modifier)
7438{
7439 if (! safe_from_p (target, exp1, 1))
7440 target = 0;
7441 if (operand_equal_p (exp0, exp1, 0))
7442 {
7443 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7444 *op1 = copy_rtx (*op0);
7445 }
7446 else
7447 {
c67e6e14
RS
7448 /* If we need to preserve evaluation order, copy exp0 into its own
7449 temporary variable so that it can't be clobbered by exp1. */
7450 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7451 exp0 = save_expr (exp0);
eb698c58
RS
7452 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7453 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7454 }
7455}
7456
f47e9b4e 7457\f
c0220ea4 7458/* Return a MEM that contains constant EXP. DEFER is as for
aacd3885
RS
7459 output_constant_def and MODIFIER is as for expand_expr. */
7460
7461static rtx
7462expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7463{
7464 rtx mem;
7465
7466 mem = output_constant_def (exp, defer);
7467 if (modifier != EXPAND_INITIALIZER)
7468 mem = use_anchored_address (mem);
7469 return mem;
7470}
7471
70bb498a 7472/* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
6377bb9a
RH
7473 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7474
7475static rtx
70bb498a 7476expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
d4ebfa65 7477 enum expand_modifier modifier, addr_space_t as)
6377bb9a
RH
7478{
7479 rtx result, subtarget;
7480 tree inner, offset;
7481 HOST_WIDE_INT bitsize, bitpos;
7482 int volatilep, unsignedp;
7483 enum machine_mode mode1;
7484
7485 /* If we are taking the address of a constant and are at the top level,
7486 we have to use output_constant_def since we can't call force_const_mem
7487 at top level. */
7488 /* ??? This should be considered a front-end bug. We should not be
7489 generating ADDR_EXPR of something that isn't an LVALUE. The only
7490 exception here is STRING_CST. */
16089886 7491 if (CONSTANT_CLASS_P (exp))
b1b95093
JJ
7492 {
7493 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7494 if (modifier < EXPAND_SUM)
7495 result = force_operand (result, target);
7496 return result;
7497 }
6377bb9a
RH
7498
7499 /* Everything must be something allowed by is_gimple_addressable. */
7500 switch (TREE_CODE (exp))
7501 {
7502 case INDIRECT_REF:
7503 /* This case will happen via recursion for &a->b. */
aacd3885 7504 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
6377bb9a 7505
70f34814
RG
7506 case MEM_REF:
7507 {
7508 tree tem = TREE_OPERAND (exp, 0);
7509 if (!integer_zerop (TREE_OPERAND (exp, 1)))
0d82a1c8 7510 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
70f34814
RG
7511 return expand_expr (tem, target, tmode, modifier);
7512 }
7513
6377bb9a 7514 case CONST_DECL:
ffab1d07 7515 /* Expand the initializer like constants above. */
b1b95093
JJ
7516 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7517 0, modifier), 0);
7518 if (modifier < EXPAND_SUM)
7519 result = force_operand (result, target);
7520 return result;
6377bb9a
RH
7521
7522 case REALPART_EXPR:
7523 /* The real part of the complex number is always first, therefore
7524 the address is the same as the address of the parent object. */
7525 offset = 0;
7526 bitpos = 0;
7527 inner = TREE_OPERAND (exp, 0);
7528 break;
7529
7530 case IMAGPART_EXPR:
7531 /* The imaginary part of the complex number is always second.
2a7e31df 7532 The expression is therefore always offset by the size of the
6377bb9a
RH
7533 scalar type. */
7534 offset = 0;
7535 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7536 inner = TREE_OPERAND (exp, 0);
7537 break;
7538
7539 default:
7540 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7541 expand_expr, as that can have various side effects; LABEL_DECLs for
16089886
RS
7542 example, may not have their DECL_RTL set yet. Expand the rtl of
7543 CONSTRUCTORs too, which should yield a memory reference for the
7544 constructor's contents. Assume language specific tree nodes can
7545 be expanded in some interesting way. */
2ec5deb5 7546 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
6377bb9a 7547 if (DECL_P (exp)
16089886 7548 || TREE_CODE (exp) == CONSTRUCTOR
2ec5deb5 7549 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
6377bb9a
RH
7550 {
7551 result = expand_expr (exp, target, tmode,
7552 modifier == EXPAND_INITIALIZER
7553 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7554
7555 /* If the DECL isn't in memory, then the DECL wasn't properly
7556 marked TREE_ADDRESSABLE, which will be either a front-end
7557 or a tree optimizer bug. */
69135c94
DD
7558
7559 if (TREE_ADDRESSABLE (exp)
7560 && ! MEM_P (result)
7561 && ! targetm.calls.allocate_stack_slots_for_args())
7562 {
7563 error ("local frame unavailable (naked function?)");
7564 return result;
7565 }
7566 else
7567 gcc_assert (MEM_P (result));
6377bb9a
RH
7568 result = XEXP (result, 0);
7569
7570 /* ??? Is this needed anymore? */
bbee5843
SB
7571 if (DECL_P (exp))
7572 TREE_USED (exp) = 1;
6377bb9a
RH
7573
7574 if (modifier != EXPAND_INITIALIZER
825298c4
JJ
7575 && modifier != EXPAND_CONST_ADDRESS
7576 && modifier != EXPAND_SUM)
6377bb9a
RH
7577 result = force_operand (result, target);
7578 return result;
7579 }
7580
2614034e
EB
7581 /* Pass FALSE as the last argument to get_inner_reference although
7582 we are expanding to RTL. The rationale is that we know how to
7583 handle "aligning nodes" here: we can just bypass them because
7584 they won't change the final object whose address will be returned
7585 (they actually exist only for that purpose). */
6377bb9a 7586 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2614034e 7587 &mode1, &unsignedp, &volatilep, false);
6377bb9a
RH
7588 break;
7589 }
7590
7591 /* We must have made progress. */
5b0264cb 7592 gcc_assert (inner != exp);
6377bb9a
RH
7593
7594 subtarget = offset || bitpos ? NULL_RTX : target;
8ebec1a5
JJ
7595 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7596 inner alignment, force the inner to be sufficiently aligned. */
7597 if (CONSTANT_CLASS_P (inner)
7598 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7599 {
7600 inner = copy_node (inner);
7601 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7602 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7603 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7604 }
d4ebfa65 7605 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
6377bb9a 7606
6377bb9a
RH
7607 if (offset)
7608 {
7609 rtx tmp;
7610
7611 if (modifier != EXPAND_NORMAL)
7612 result = force_operand (result, NULL);
b8698a0f 7613 tmp = expand_expr (offset, NULL_RTX, tmode,
4543943a
AP
7614 modifier == EXPAND_INITIALIZER
7615 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
6377bb9a 7616
d4ebfa65
BE
7617 result = convert_memory_address_addr_space (tmode, result, as);
7618 tmp = convert_memory_address_addr_space (tmode, tmp, as);
b0b324b0 7619
d047a201 7620 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
419e1853 7621 result = simplify_gen_binary (PLUS, tmode, result, tmp);
6377bb9a
RH
7622 else
7623 {
7624 subtarget = bitpos ? NULL_RTX : target;
7625 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7626 1, OPTAB_LIB_WIDEN);
7627 }
7628 }
7629
7630 if (bitpos)
7631 {
7632 /* Someone beforehand should have rejected taking the address
7633 of such an object. */
b0b324b0 7634 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
6377bb9a 7635
b7104c55 7636 result = convert_memory_address_addr_space (tmode, result, as);
0a81f074 7637 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
6377bb9a
RH
7638 if (modifier < EXPAND_SUM)
7639 result = force_operand (result, target);
7640 }
7641
7642 return result;
7643}
7644
70bb498a
RH
7645/* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7646 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7647
7648static rtx
7649expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7650 enum expand_modifier modifier)
7651{
d4ebfa65
BE
7652 addr_space_t as = ADDR_SPACE_GENERIC;
7653 enum machine_mode address_mode = Pmode;
7654 enum machine_mode pointer_mode = ptr_mode;
70bb498a
RH
7655 enum machine_mode rmode;
7656 rtx result;
7657
b0b324b0
RH
7658 /* Target mode of VOIDmode says "whatever's natural". */
7659 if (tmode == VOIDmode)
7660 tmode = TYPE_MODE (TREE_TYPE (exp));
7661
d4ebfa65
BE
7662 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7663 {
7664 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7665 address_mode = targetm.addr_space.address_mode (as);
7666 pointer_mode = targetm.addr_space.pointer_mode (as);
7667 }
7668
b0b324b0
RH
7669 /* We can get called with some Weird Things if the user does silliness
7670 like "(short) &a". In that case, convert_memory_address won't do
7671 the right thing, so ignore the given target mode. */
d4ebfa65
BE
7672 if (tmode != address_mode && tmode != pointer_mode)
7673 tmode = address_mode;
b0b324b0 7674
70bb498a 7675 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
d4ebfa65 7676 tmode, modifier, as);
70bb498a
RH
7677
7678 /* Despite expand_expr claims concerning ignoring TMODE when not
b0b324b0
RH
7679 strictly convenient, stuff breaks if we don't honor it. Note
7680 that combined with the above, we only do this for pointer modes. */
70bb498a
RH
7681 rmode = GET_MODE (result);
7682 if (rmode == VOIDmode)
7683 rmode = tmode;
7684 if (rmode != tmode)
d4ebfa65 7685 result = convert_memory_address_addr_space (tmode, result, as);
b0b324b0 7686
70bb498a
RH
7687 return result;
7688}
7689
32eed045
JJ
7690/* Generate code for computing CONSTRUCTOR EXP.
7691 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7692 is TRUE, instead of creating a temporary variable in memory
7693 NULL is returned and the caller needs to handle it differently. */
7694
7695static rtx
7696expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7697 bool avoid_temp_mem)
7698{
7699 tree type = TREE_TYPE (exp);
7700 enum machine_mode mode = TYPE_MODE (type);
7701
7702 /* Try to avoid creating a temporary at all. This is possible
7703 if all of the initializer is zero.
7704 FIXME: try to handle all [0..255] initializers we can handle
7705 with memset. */
7706 if (TREE_STATIC (exp)
7707 && !TREE_ADDRESSABLE (exp)
7708 && target != 0 && mode == BLKmode
7709 && all_zeros_p (exp))
7710 {
7711 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7712 return target;
7713 }
7714
7715 /* All elts simple constants => refer to a constant in memory. But
7716 if this is a non-BLKmode mode, let it store a field at a time
7717 since that should make a CONST_INT or CONST_DOUBLE when we
7718 fold. Likewise, if we have a target we can use, it is best to
7719 store directly into the target unless the type is large enough
7720 that memcpy will be used. If we are making an initializer and
7721 all operands are constant, put it in memory as well.
7722
7723 FIXME: Avoid trying to fill vector constructors piece-meal.
7724 Output them with output_constant_def below unless we're sure
7725 they're zeros. This should go away when vector initializers
7726 are treated like VECTOR_CST instead of arrays. */
7727 if ((TREE_STATIC (exp)
7728 && ((mode == BLKmode
7729 && ! (target != 0 && safe_from_p (target, exp, 1)))
7730 || TREE_ADDRESSABLE (exp)
7731 || (host_integerp (TYPE_SIZE_UNIT (type), 1)
7732 && (! MOVE_BY_PIECES_P
7733 (tree_low_cst (TYPE_SIZE_UNIT (type), 1),
7734 TYPE_ALIGN (type)))
7735 && ! mostly_zeros_p (exp))))
7736 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7737 && TREE_CONSTANT (exp)))
7738 {
7739 rtx constructor;
7740
7741 if (avoid_temp_mem)
7742 return NULL_RTX;
7743
7744 constructor = expand_expr_constant (exp, 1, modifier);
7745
7746 if (modifier != EXPAND_CONST_ADDRESS
7747 && modifier != EXPAND_INITIALIZER
7748 && modifier != EXPAND_SUM)
7749 constructor = validize_mem (constructor);
7750
7751 return constructor;
7752 }
7753
7754 /* Handle calls that pass values in multiple non-contiguous
7755 locations. The Irix 6 ABI has examples of this. */
7756 if (target == 0 || ! safe_from_p (target, exp, 1)
7757 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7758 {
7759 if (avoid_temp_mem)
7760 return NULL_RTX;
7761
7762 target
7763 = assign_temp (build_qualified_type (type, (TYPE_QUALS (type)
7764 | (TREE_READONLY (exp)
7765 * TYPE_QUAL_CONST))),
9474e8ab 7766 TREE_ADDRESSABLE (exp), 1);
32eed045
JJ
7767 }
7768
7769 store_constructor (exp, target, 0, int_expr_size (exp));
7770 return target;
7771}
7772
70bb498a 7773
bbf6f052
RK
7774/* expand_expr: generate code for computing expression EXP.
7775 An rtx for the computed value is returned. The value is never null.
7776 In the case of a void EXP, const0_rtx is returned.
7777
7778 The value may be stored in TARGET if TARGET is nonzero.
7779 TARGET is just a suggestion; callers must assume that
7780 the rtx returned may not be the same as TARGET.
7781
7782 If TARGET is CONST0_RTX, it means that the value will be ignored.
7783
7784 If TMODE is not VOIDmode, it suggests generating the
7785 result in mode TMODE. But this is done only when convenient.
7786 Otherwise, TMODE is ignored and the value generated in its natural mode.
7787 TMODE is just a suggestion; callers must assume that
7788 the rtx returned may not have mode TMODE.
7789
d6a5ac33
RK
7790 Note that TARGET may have neither TMODE nor MODE. In that case, it
7791 probably will not be used.
bbf6f052
RK
7792
7793 If MODIFIER is EXPAND_SUM then when EXP is an addition
7794 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7795 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7796 products as above, or REG or MEM, or constant.
7797 Ordinarily in such cases we would output mul or add instructions
7798 and then return a pseudo reg containing the sum.
7799
7800 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7801 it also marks a label as absolutely required (it can't be dead).
26fcb35a 7802 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
d6a5ac33
RK
7803 This is used for outputting expressions used in initializers.
7804
7805 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7806 with a constant address even if that address is not normally legitimate.
8403445a
AM
7807 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7808
7809 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7810 a call parameter. Such targets require special care as we haven't yet
7811 marked TARGET so that it's safe from being trashed by libcalls. We
7812 don't want to use TARGET for anything but the final result;
7813 Intermediate values must go elsewhere. Additionally, calls to
caf93cb0 7814 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
0fab64a3
MM
7815
7816 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7817 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7818 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7819 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7820 recursively. */
bbf6f052
RK
7821
7822rtx
0fab64a3
MM
7823expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7824 enum expand_modifier modifier, rtx *alt_rtl)
6de9cd9a 7825{
a5883ba0 7826 rtx ret;
6de9cd9a
DN
7827
7828 /* Handle ERROR_MARK before anybody tries to access its type. */
7829 if (TREE_CODE (exp) == ERROR_MARK
726a989a 7830 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
6de9cd9a
DN
7831 {
7832 ret = CONST0_RTX (tmode);
7833 return ret ? ret : const0_rtx;
7834 }
7835
6de9cd9a 7836 /* If this is an expression of some kind and it has an associated line
caf93cb0 7837 number, then emit the line number before expanding the expression.
6de9cd9a
DN
7838
7839 We need to save and restore the file and line information so that
7840 errors discovered during expansion are emitted with the right
caf93cb0 7841 information. It would be better of the diagnostic routines
6de9cd9a
DN
7842 used the file/line information embedded in the tree nodes rather
7843 than globals. */
55e092c4 7844 if (cfun && EXPR_HAS_LOCATION (exp))
6de9cd9a
DN
7845 {
7846 location_t saved_location = input_location;
d0ed412a
JJ
7847 location_t saved_curr_loc = get_curr_insn_source_location ();
7848 tree saved_block = get_curr_insn_block ();
6de9cd9a 7849 input_location = EXPR_LOCATION (exp);
55e092c4 7850 set_curr_insn_source_location (input_location);
caf93cb0 7851
6de9cd9a 7852 /* Record where the insns produced belong. */
55e092c4 7853 set_curr_insn_block (TREE_BLOCK (exp));
6de9cd9a
DN
7854
7855 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7856
7857 input_location = saved_location;
d0ed412a
JJ
7858 set_curr_insn_block (saved_block);
7859 set_curr_insn_source_location (saved_curr_loc);
6de9cd9a
DN
7860 }
7861 else
7862 {
7863 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7864 }
7865
6de9cd9a
DN
7866 return ret;
7867}
7868
683c600b
AP
7869/* Try to expand the conditional expression which is represented by
7870 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7871 return the rtl reg which repsents the result. Otherwise return
7872 NULL_RTL. */
7873
7874static rtx
7875expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7876 tree treeop1 ATTRIBUTE_UNUSED,
7877 tree treeop2 ATTRIBUTE_UNUSED)
7878{
7879#ifdef HAVE_conditional_move
7880 rtx insn;
7881 rtx op00, op01, op1, op2;
7882 enum rtx_code comparison_code;
7883 enum machine_mode comparison_mode;
7884 gimple srcstmt;
7885 rtx temp;
7886 tree type = TREE_TYPE (treeop1);
7887 int unsignedp = TYPE_UNSIGNED (type);
7888 enum machine_mode mode = TYPE_MODE (type);
7889
9474e8ab 7890 temp = assign_temp (type, 0, 1);
683c600b
AP
7891
7892 /* If we cannot do a conditional move on the mode, try doing it
7893 with the promoted mode. */
7894 if (!can_conditionally_move_p (mode))
7895 mode = promote_mode (type, mode, &unsignedp);
7896
7897 if (!can_conditionally_move_p (mode))
7898 return NULL_RTX;
7899
7900 start_sequence ();
7901 expand_operands (treeop1, treeop2,
7902 temp, &op1, &op2, EXPAND_NORMAL);
7903
7904 if (TREE_CODE (treeop0) == SSA_NAME
7905 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7906 {
7907 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7908 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7909 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7910 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7911 comparison_mode = TYPE_MODE (type);
7912 unsignedp = TYPE_UNSIGNED (type);
7913 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7914 }
7915 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
7916 {
7917 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7918 enum tree_code cmpcode = TREE_CODE (treeop0);
7919 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7920 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7921 unsignedp = TYPE_UNSIGNED (type);
7922 comparison_mode = TYPE_MODE (type);
7923 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7924 }
7925 else
7926 {
7927 op00 = expand_normal (treeop0);
7928 op01 = const0_rtx;
7929 comparison_code = NE;
7930 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7931 }
7932
7933 if (GET_MODE (op1) != mode)
7934 op1 = gen_lowpart (mode, op1);
7935
7936 if (GET_MODE (op2) != mode)
7937 op2 = gen_lowpart (mode, op2);
7938
7939 /* Try to emit the conditional move. */
7940 insn = emit_conditional_move (temp, comparison_code,
7941 op00, op01, comparison_mode,
7942 op1, op2, mode,
7943 unsignedp);
7944
7945 /* If we could do the conditional move, emit the sequence,
7946 and return. */
7947 if (insn)
7948 {
7949 rtx seq = get_insns ();
7950 end_sequence ();
7951 emit_insn (seq);
7952 return temp;
7953 }
7954
7955 /* Otherwise discard the sequence and fall back to code with
7956 branches. */
7957 end_sequence ();
7958#endif
7959 return NULL_RTX;
7960}
7961
28ed065e 7962rtx
f994f296
MM
7963expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
7964 enum expand_modifier modifier)
bbf6f052 7965{
f994f296 7966 rtx op0, op1, op2, temp;
07beea0d 7967 tree type;
8df83eae 7968 int unsignedp;
b3694847 7969 enum machine_mode mode;
f994f296 7970 enum tree_code code = ops->code;
bbf6f052 7971 optab this_optab;
68557e14
ML
7972 rtx subtarget, original_target;
7973 int ignore;
ac5dc795 7974 bool reduce_bit_field;
f994f296 7975 location_t loc = ops->location;
16949072 7976 tree treeop0, treeop1, treeop2;
ac5dc795 7977#define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
bc15d0ef
JM
7978 ? reduce_to_bit_field_precision ((expr), \
7979 target, \
7980 type) \
7981 : (expr))
bbf6f052 7982
f994f296 7983 type = ops->type;
726a989a
RB
7984 mode = TYPE_MODE (type);
7985 unsignedp = TYPE_UNSIGNED (type);
8df83eae 7986
f994f296
MM
7987 treeop0 = ops->op0;
7988 treeop1 = ops->op1;
16949072 7989 treeop2 = ops->op2;
f994f296
MM
7990
7991 /* We should be called only on simple (binary or unary) expressions,
7992 exactly those that are valid in gimple expressions that aren't
7993 GIMPLE_SINGLE_RHS (or invalid). */
7994 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
0354c0c7
BS
7995 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
7996 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
b32e7cdb 7997
68557e14 7998 ignore = (target == const0_rtx
1a87cf0c 7999 || ((CONVERT_EXPR_CODE_P (code)
a134e5f3 8000 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
68557e14
ML
8001 && TREE_CODE (type) == VOID_TYPE));
8002
f994f296
MM
8003 /* We should be called only if we need the result. */
8004 gcc_assert (!ignore);
8005
ac5dc795
PB
8006 /* An operation in what may be a bit-field type needs the
8007 result to be reduced to the precision of the bit-field type,
8008 which is narrower than that of the type's mode. */
a577c405 8009 reduce_bit_field = (INTEGRAL_TYPE_P (type)
ac5dc795
PB
8010 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8011
ac5dc795
PB
8012 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8013 target = 0;
8014
8015 /* Use subtarget as the target for operand 0 of a binary operation. */
8016 subtarget = get_subtarget (target);
8017 original_target = target;
bbf6f052 8018
bbf6f052
RK
8019 switch (code)
8020 {
1d65f45c 8021 case NON_LVALUE_EXPR:
f994f296
MM
8022 case PAREN_EXPR:
8023 CASE_CONVERT:
8024 if (treeop0 == error_mark_node)
8025 return const0_rtx;
6de9cd9a 8026
f994f296
MM
8027 if (TREE_CODE (type) == UNION_TYPE)
8028 {
8029 tree valtype = TREE_TYPE (treeop0);
6de9cd9a 8030
f994f296
MM
8031 /* If both input and output are BLKmode, this conversion isn't doing
8032 anything except possibly changing memory attribute. */
8033 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8034 {
8035 rtx result = expand_expr (treeop0, target, tmode,
8036 modifier);
bbf6f052 8037
f994f296
MM
8038 result = copy_rtx (result);
8039 set_mem_attributes (result, type, 0);
8040 return result;
8041 }
8b11a64c 8042
f994f296
MM
8043 if (target == 0)
8044 {
8045 if (TYPE_MODE (type) != BLKmode)
8046 target = gen_reg_rtx (TYPE_MODE (type));
8047 else
9474e8ab 8048 target = assign_temp (type, 1, 1);
f994f296 8049 }
921b3427 8050
f994f296
MM
8051 if (MEM_P (target))
8052 /* Store data into beginning of memory target. */
8053 store_expr (treeop0,
8054 adjust_address (target, TYPE_MODE (valtype), 0),
8055 modifier == EXPAND_STACK_PARM,
8056 false);
8893239d 8057
f994f296
MM
8058 else
8059 {
8060 gcc_assert (REG_P (target));
d6a5ac33 8061
f994f296
MM
8062 /* Store this field into a union of the proper type. */
8063 store_field (target,
8064 MIN ((int_size_in_bytes (TREE_TYPE
8065 (treeop0))
8066 * BITS_PER_UNIT),
8067 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
1169e45d 8068 0, 0, 0, TYPE_MODE (valtype), treeop0,
f994f296
MM
8069 type, 0, false);
8070 }
d6a5ac33 8071
f994f296
MM
8072 /* Return the entire union. */
8073 return target;
e44842fe
RK
8074 }
8075
f994f296
MM
8076 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8077 {
8078 op0 = expand_expr (treeop0, target, VOIDmode,
8079 modifier);
d6a5ac33 8080
f994f296
MM
8081 /* If the signedness of the conversion differs and OP0 is
8082 a promoted SUBREG, clear that indication since we now
8083 have to do the proper extension. */
8084 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8085 && GET_CODE (op0) == SUBREG)
8086 SUBREG_PROMOTED_VAR_P (op0) = 0;
d6a5ac33 8087
f994f296 8088 return REDUCE_BIT_FIELD (op0);
0fab64a3 8089 }
1499e0a8 8090
f994f296
MM
8091 op0 = expand_expr (treeop0, NULL_RTX, mode,
8092 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8093 if (GET_MODE (op0) == mode)
8094 ;
8095
8096 /* If OP0 is a constant, just convert it into the proper mode. */
8097 else if (CONSTANT_P (op0))
dc6d66b3 8098 {
f994f296 8099 tree inner_type = TREE_TYPE (treeop0);
7254cb57
MM
8100 enum machine_mode inner_mode = GET_MODE (op0);
8101
8102 if (inner_mode == VOIDmode)
8103 inner_mode = TYPE_MODE (inner_type);
dc6d66b3 8104
f994f296
MM
8105 if (modifier == EXPAND_INITIALIZER)
8106 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8107 subreg_lowpart_offset (mode,
8108 inner_mode));
8109 else
8110 op0= convert_modes (mode, inner_mode, op0,
8111 TYPE_UNSIGNED (inner_type));
dc6d66b3
RK
8112 }
8113
f994f296
MM
8114 else if (modifier == EXPAND_INITIALIZER)
8115 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
1499e0a8 8116
f994f296
MM
8117 else if (target == 0)
8118 op0 = convert_to_mode (mode, op0,
8119 TYPE_UNSIGNED (TREE_TYPE
8120 (treeop0)));
8121 else
1499e0a8 8122 {
f994f296
MM
8123 convert_move (target, op0,
8124 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8125 op0 = target;
8126 }
ed1223ba 8127
f994f296 8128 return REDUCE_BIT_FIELD (op0);
bbf6f052 8129
09e881c9
BE
8130 case ADDR_SPACE_CONVERT_EXPR:
8131 {
8132 tree treeop0_type = TREE_TYPE (treeop0);
8133 addr_space_t as_to;
8134 addr_space_t as_from;
8135
8136 gcc_assert (POINTER_TYPE_P (type));
8137 gcc_assert (POINTER_TYPE_P (treeop0_type));
8138
8139 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8140 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8141
8142 /* Conversions between pointers to the same address space should
8143 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8144 gcc_assert (as_to != as_from);
8145
8146 /* Ask target code to handle conversion between pointers
8147 to overlapping address spaces. */
8148 if (targetm.addr_space.subset_p (as_to, as_from)
8149 || targetm.addr_space.subset_p (as_from, as_to))
8150 {
8151 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8152 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8153 gcc_assert (op0);
8154 return op0;
8155 }
8156
8157 /* For disjoint address spaces, converting anything but
8158 a null pointer invokes undefined behaviour. We simply
8159 always return a null pointer here. */
8160 return CONST0_RTX (mode);
8161 }
8162
b8698a0f 8163 case POINTER_PLUS_EXPR:
f994f296 8164 /* Even though the sizetype mode and the pointer's mode can be different
b8698a0f 8165 expand is able to handle this correctly and get the correct result out
f994f296
MM
8166 of the PLUS_EXPR code. */
8167 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8168 if sizetype precision is smaller than pointer precision. */
8169 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8170 treeop1 = fold_convert_loc (loc, type,
8171 fold_convert_loc (loc, ssizetype,
8172 treeop1));
9516c54e
TG
8173 /* If sizetype precision is larger than pointer precision, truncate the
8174 offset to have matching modes. */
8175 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8176 treeop1 = fold_convert_loc (loc, type, treeop1);
8177
f994f296 8178 case PLUS_EXPR:
f994f296
MM
8179 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8180 something else, make sure we add the register to the constant and
8181 then to the other thing. This case can occur during strength
8182 reduction and doing it this way will produce better code if the
8183 frame pointer or argument pointer is eliminated.
bbf6f052 8184
f994f296
MM
8185 fold-const.c will ensure that the constant is always in the inner
8186 PLUS_EXPR, so the only case we need to do anything about is if
8187 sp, ap, or fp is our second argument, in which case we must swap
8188 the innermost first argument and our second argument. */
3a94c984 8189
f994f296
MM
8190 if (TREE_CODE (treeop0) == PLUS_EXPR
8191 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8192 && TREE_CODE (treeop1) == VAR_DECL
8193 && (DECL_RTL (treeop1) == frame_pointer_rtx
8194 || DECL_RTL (treeop1) == stack_pointer_rtx
8195 || DECL_RTL (treeop1) == arg_pointer_rtx))
8196 {
e384e6b5 8197 gcc_unreachable ();
f994f296 8198 }
bbf6f052 8199
f994f296
MM
8200 /* If the result is to be ptr_mode and we are adding an integer to
8201 something, we might be forming a constant. So try to use
8202 plus_constant. If it produces a sum and we can't accept it,
8203 use force_operand. This allows P = &ARR[const] to generate
8204 efficient code on machines where a SYMBOL_REF is not a valid
8205 address.
0f996086 8206
f994f296
MM
8207 If this is an EXPAND_SUM call, always return the sum. */
8208 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8209 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9ad58e09 8210 {
f994f296
MM
8211 if (modifier == EXPAND_STACK_PARM)
8212 target = 0;
8213 if (TREE_CODE (treeop0) == INTEGER_CST
69660a70 8214 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
f994f296
MM
8215 && TREE_CONSTANT (treeop1))
8216 {
8217 rtx constant_part;
9ad58e09 8218
f994f296
MM
8219 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8220 EXPAND_SUM);
8221 /* Use immed_double_const to ensure that the constant is
8222 truncated according to the mode of OP1, then sign extended
8223 to a HOST_WIDE_INT. Using the constant directly can result
8224 in non-canonical RTL in a 64x32 cross compile. */
8225 constant_part
8226 = immed_double_const (TREE_INT_CST_LOW (treeop0),
8227 (HOST_WIDE_INT) 0,
8228 TYPE_MODE (TREE_TYPE (treeop1)));
0a81f074 8229 op1 = plus_constant (mode, op1, INTVAL (constant_part));
f994f296
MM
8230 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8231 op1 = force_operand (op1, target);
8232 return REDUCE_BIT_FIELD (op1);
8233 }
9ad58e09 8234
f994f296 8235 else if (TREE_CODE (treeop1) == INTEGER_CST
69660a70 8236 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
f994f296
MM
8237 && TREE_CONSTANT (treeop0))
8238 {
8239 rtx constant_part;
9ad58e09 8240
f994f296
MM
8241 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8242 (modifier == EXPAND_INITIALIZER
8243 ? EXPAND_INITIALIZER : EXPAND_SUM));
8244 if (! CONSTANT_P (op0))
8245 {
8246 op1 = expand_expr (treeop1, NULL_RTX,
8247 VOIDmode, modifier);
8248 /* Return a PLUS if modifier says it's OK. */
8249 if (modifier == EXPAND_SUM
8250 || modifier == EXPAND_INITIALIZER)
8251 return simplify_gen_binary (PLUS, mode, op0, op1);
8252 goto binop2;
8253 }
8254 /* Use immed_double_const to ensure that the constant is
8255 truncated according to the mode of OP1, then sign extended
8256 to a HOST_WIDE_INT. Using the constant directly can result
8257 in non-canonical RTL in a 64x32 cross compile. */
8258 constant_part
8259 = immed_double_const (TREE_INT_CST_LOW (treeop1),
8260 (HOST_WIDE_INT) 0,
8261 TYPE_MODE (TREE_TYPE (treeop0)));
0a81f074 8262 op0 = plus_constant (mode, op0, INTVAL (constant_part));
f994f296
MM
8263 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8264 op0 = force_operand (op0, target);
8265 return REDUCE_BIT_FIELD (op0);
8266 }
9ad58e09
RS
8267 }
8268
cc99c5fe
JJ
8269 /* Use TER to expand pointer addition of a negated value
8270 as pointer subtraction. */
8271 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8272 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8273 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8274 && TREE_CODE (treeop1) == SSA_NAME
8275 && TYPE_MODE (TREE_TYPE (treeop0))
8276 == TYPE_MODE (TREE_TYPE (treeop1)))
8277 {
8278 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8279 if (def)
8280 {
8281 treeop1 = gimple_assign_rhs1 (def);
8282 code = MINUS_EXPR;
8283 goto do_minus;
8284 }
8285 }
8286
f994f296
MM
8287 /* No sense saving up arithmetic to be done
8288 if it's all in the wrong mode to form part of an address.
8289 And force_operand won't know whether to sign-extend or
8290 zero-extend. */
8291 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8292 || mode != ptr_mode)
8293 {
8294 expand_operands (treeop0, treeop1,
8295 subtarget, &op0, &op1, EXPAND_NORMAL);
8296 if (op0 == const0_rtx)
8297 return op1;
8298 if (op1 == const0_rtx)
8299 return op0;
8300 goto binop2;
8301 }
9ad58e09 8302
f994f296
MM
8303 expand_operands (treeop0, treeop1,
8304 subtarget, &op0, &op1, modifier);
8305 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
bbf6f052 8306
f994f296 8307 case MINUS_EXPR:
cc99c5fe 8308 do_minus:
f994f296
MM
8309 /* For initializers, we are allowed to return a MINUS of two
8310 symbolic constants. Here we handle all cases when both operands
8311 are constant. */
8312 /* Handle difference of two symbolic constants,
8313 for the sake of an initializer. */
8314 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8315 && really_constant_p (treeop0)
8316 && really_constant_p (treeop1))
8317 {
8318 expand_operands (treeop0, treeop1,
8319 NULL_RTX, &op0, &op1, modifier);
1499e0a8 8320
f994f296
MM
8321 /* If the last operand is a CONST_INT, use plus_constant of
8322 the negated constant. Else make the MINUS. */
8323 if (CONST_INT_P (op1))
0a81f074
RS
8324 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8325 -INTVAL (op1)));
f994f296
MM
8326 else
8327 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8328 }
1499e0a8 8329
f994f296
MM
8330 /* No sense saving up arithmetic to be done
8331 if it's all in the wrong mode to form part of an address.
8332 And force_operand won't know whether to sign-extend or
8333 zero-extend. */
8334 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8335 || mode != ptr_mode)
8336 goto binop;
bbf6f052 8337
f994f296
MM
8338 expand_operands (treeop0, treeop1,
8339 subtarget, &op0, &op1, modifier);
70e6ca43 8340
f994f296
MM
8341 /* Convert A - const to A + (-const). */
8342 if (CONST_INT_P (op1))
dd27116b 8343 {
f994f296
MM
8344 op1 = negate_rtx (mode, op1);
8345 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
dd27116b 8346 }
3207b172 8347
f994f296 8348 goto binop2;
3a94c984 8349
0354c0c7
BS
8350 case WIDEN_MULT_PLUS_EXPR:
8351 case WIDEN_MULT_MINUS_EXPR:
8352 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
16949072 8353 op2 = expand_normal (treeop2);
0354c0c7
BS
8354 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8355 target, unsignedp);
8356 return target;
8357
5b58b39b 8358 case WIDEN_MULT_EXPR:
f994f296
MM
8359 /* If first operand is constant, swap them.
8360 Thus the following special case checks need only
8361 check the second operand. */
8362 if (TREE_CODE (treeop0) == INTEGER_CST)
8363 {
8364 tree t1 = treeop0;
8365 treeop0 = treeop1;
8366 treeop1 = t1;
8367 }
bbf6f052 8368
f994f296
MM
8369 /* First, check if we have a multiplication of one signed and one
8370 unsigned operand. */
5b58b39b
BS
8371 if (TREE_CODE (treeop1) != INTEGER_CST
8372 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8373 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
f994f296 8374 {
5b58b39b 8375 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
f994f296 8376 this_optab = usmul_widen_optab;
5dfe80ba
AS
8377 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8378 != CODE_FOR_nothing)
f994f296 8379 {
5dfe80ba
AS
8380 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8381 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8382 EXPAND_NORMAL);
8383 else
8384 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8385 EXPAND_NORMAL);
8386 goto binop3;
f994f296
MM
8387 }
8388 }
5b58b39b
BS
8389 /* Check for a multiplication with matching signedness. */
8390 else if ((TREE_CODE (treeop1) == INTEGER_CST
8391 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8392 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8393 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
f994f296 8394 {
5b58b39b 8395 tree op0type = TREE_TYPE (treeop0);
f994f296
MM
8396 enum machine_mode innermode = TYPE_MODE (op0type);
8397 bool zextend_p = TYPE_UNSIGNED (op0type);
8398 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8399 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
1e0598e2 8400
5dfe80ba 8401 if (TREE_CODE (treeop0) != INTEGER_CST)
f994f296 8402 {
5dfe80ba 8403 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
a484f6ba 8404 != CODE_FOR_nothing)
f994f296 8405 {
5b58b39b
BS
8406 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8407 EXPAND_NORMAL);
8408 temp = expand_widening_mult (mode, op0, op1, target,
8409 unsignedp, this_optab);
8410 return REDUCE_BIT_FIELD (temp);
f994f296 8411 }
5dfe80ba 8412 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
a484f6ba 8413 != CODE_FOR_nothing
5b58b39b 8414 && innermode == word_mode)
f994f296
MM
8415 {
8416 rtx htem, hipart;
5b58b39b
BS
8417 op0 = expand_normal (treeop0);
8418 if (TREE_CODE (treeop1) == INTEGER_CST)
f994f296 8419 op1 = convert_modes (innermode, mode,
5b58b39b 8420 expand_normal (treeop1), unsignedp);
f994f296 8421 else
5b58b39b 8422 op1 = expand_normal (treeop1);
f994f296
MM
8423 temp = expand_binop (mode, other_optab, op0, op1, target,
8424 unsignedp, OPTAB_LIB_WIDEN);
8425 hipart = gen_highpart (innermode, temp);
8426 htem = expand_mult_highpart_adjust (innermode, hipart,
8427 op0, op1, hipart,
8428 zextend_p);
8429 if (htem != hipart)
8430 emit_move_insn (hipart, htem);
8431 return REDUCE_BIT_FIELD (temp);
8432 }
8433 }
8434 }
5b58b39b
BS
8435 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8436 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8437 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8438 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8439
16949072
RG
8440 case FMA_EXPR:
8441 {
8442 optab opt = fma_optab;
8443 gimple def0, def2;
8444
65450d64
JJ
8445 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8446 call. */
8447 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8448 {
8449 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8450 tree call_expr;
8451
8452 gcc_assert (fn != NULL_TREE);
8453 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8454 return expand_builtin (call_expr, target, subtarget, mode, false);
8455 }
8456
16949072
RG
8457 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8458 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8459
8460 op0 = op2 = NULL;
8461
8462 if (def0 && def2
8463 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8464 {
8465 opt = fnms_optab;
8466 op0 = expand_normal (gimple_assign_rhs1 (def0));
8467 op2 = expand_normal (gimple_assign_rhs1 (def2));
8468 }
8469 else if (def0
8470 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8471 {
8472 opt = fnma_optab;
8473 op0 = expand_normal (gimple_assign_rhs1 (def0));
8474 }
8475 else if (def2
8476 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8477 {
8478 opt = fms_optab;
8479 op2 = expand_normal (gimple_assign_rhs1 (def2));
8480 }
8481
8482 if (op0 == NULL)
8483 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8484 if (op2 == NULL)
8485 op2 = expand_normal (treeop2);
8486 op1 = expand_normal (treeop1);
8487
8488 return expand_ternary_op (TYPE_MODE (type), opt,
8489 op0, op1, op2, target, 0);
8490 }
8491
5b58b39b
BS
8492 case MULT_EXPR:
8493 /* If this is a fixed-point operation, then we cannot use the code
8494 below because "expand_mult" doesn't support sat/no-sat fixed-point
8495 multiplications. */
8496 if (ALL_FIXED_POINT_MODE_P (mode))
8497 goto binop;
8498
8499 /* If first operand is constant, swap them.
8500 Thus the following special case checks need only
8501 check the second operand. */
8502 if (TREE_CODE (treeop0) == INTEGER_CST)
8503 {
8504 tree t1 = treeop0;
8505 treeop0 = treeop1;
8506 treeop1 = t1;
8507 }
8508
8509 /* Attempt to return something suitable for generating an
8510 indexed address, for machines that support that. */
8511
8512 if (modifier == EXPAND_SUM && mode == ptr_mode
8513 && host_integerp (treeop1, 0))
8514 {
8515 tree exp1 = treeop1;
8516
8517 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8518 EXPAND_SUM);
8519
8520 if (!REG_P (op0))
8521 op0 = force_operand (op0, NULL_RTX);
8522 if (!REG_P (op0))
8523 op0 = copy_to_mode_reg (mode, op0);
8524
8525 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8526 gen_int_mode (tree_low_cst (exp1, 0),
8527 TYPE_MODE (TREE_TYPE (exp1)))));
8528 }
8529
8530 if (modifier == EXPAND_STACK_PARM)
8531 target = 0;
8532
8533 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
f994f296 8534 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
bbf6f052 8535
f994f296
MM
8536 case TRUNC_DIV_EXPR:
8537 case FLOOR_DIV_EXPR:
8538 case CEIL_DIV_EXPR:
8539 case ROUND_DIV_EXPR:
8540 case EXACT_DIV_EXPR:
8541 /* If this is a fixed-point operation, then we cannot use the code
8542 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8543 divisions. */
8544 if (ALL_FIXED_POINT_MODE_P (mode))
8545 goto binop;
ac182688 8546
f994f296
MM
8547 if (modifier == EXPAND_STACK_PARM)
8548 target = 0;
8549 /* Possible optimization: compute the dividend with EXPAND_SUM
8550 then if the divisor is constant can optimize the case
8551 where some terms of the dividend have coeffs divisible by it. */
8552 expand_operands (treeop0, treeop1,
8553 subtarget, &op0, &op1, EXPAND_NORMAL);
8554 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
ac182688 8555
f994f296 8556 case RDIV_EXPR:
98449720 8557 case MULT_HIGHPART_EXPR:
f994f296 8558 goto binop;
6de9cd9a 8559
f994f296
MM
8560 case TRUNC_MOD_EXPR:
8561 case FLOOR_MOD_EXPR:
8562 case CEIL_MOD_EXPR:
8563 case ROUND_MOD_EXPR:
8564 if (modifier == EXPAND_STACK_PARM)
8565 target = 0;
8566 expand_operands (treeop0, treeop1,
8567 subtarget, &op0, &op1, EXPAND_NORMAL);
8568 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
742920c7 8569
f994f296
MM
8570 case FIXED_CONVERT_EXPR:
8571 op0 = expand_normal (treeop0);
8572 if (target == 0 || modifier == EXPAND_STACK_PARM)
8573 target = gen_reg_rtx (mode);
742920c7 8574
f994f296
MM
8575 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8576 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8577 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8578 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8579 else
8580 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8581 return target;
6de9cd9a 8582
f994f296
MM
8583 case FIX_TRUNC_EXPR:
8584 op0 = expand_normal (treeop0);
8585 if (target == 0 || modifier == EXPAND_STACK_PARM)
8586 target = gen_reg_rtx (mode);
8587 expand_fix (target, op0, unsignedp);
8588 return target;
bbf6f052 8589
f994f296
MM
8590 case FLOAT_EXPR:
8591 op0 = expand_normal (treeop0);
8592 if (target == 0 || modifier == EXPAND_STACK_PARM)
8593 target = gen_reg_rtx (mode);
8594 /* expand_float can't figure out what to do if FROM has VOIDmode.
8595 So give it the correct mode. With -O, cse will optimize this. */
8596 if (GET_MODE (op0) == VOIDmode)
8597 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8598 op0);
8599 expand_float (target, op0,
8600 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8601 return target;
4af3895e 8602
f994f296
MM
8603 case NEGATE_EXPR:
8604 op0 = expand_expr (treeop0, subtarget,
8605 VOIDmode, EXPAND_NORMAL);
8606 if (modifier == EXPAND_STACK_PARM)
8607 target = 0;
8608 temp = expand_unop (mode,
8609 optab_for_tree_code (NEGATE_EXPR, type,
8610 optab_default),
8611 op0, target, 0);
8612 gcc_assert (temp);
8613 return REDUCE_BIT_FIELD (temp);
05bccae2 8614
f994f296
MM
8615 case ABS_EXPR:
8616 op0 = expand_expr (treeop0, subtarget,
8617 VOIDmode, EXPAND_NORMAL);
8618 if (modifier == EXPAND_STACK_PARM)
8619 target = 0;
3a94c984 8620
f994f296
MM
8621 /* ABS_EXPR is not valid for complex arguments. */
8622 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8623 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
742920c7 8624
f994f296
MM
8625 /* Unsigned abs is simply the operand. Testing here means we don't
8626 risk generating incorrect code below. */
8627 if (TYPE_UNSIGNED (type))
8628 return op0;
4038c495 8629
f994f296
MM
8630 return expand_abs (mode, op0, target, unsignedp,
8631 safe_from_p (target, treeop0, 1));
32eed045 8632
f994f296
MM
8633 case MAX_EXPR:
8634 case MIN_EXPR:
8635 target = original_target;
8636 if (target == 0
8637 || modifier == EXPAND_STACK_PARM
8638 || (MEM_P (target) && MEM_VOLATILE_P (target))
8639 || GET_MODE (target) != mode
8640 || (REG_P (target)
8641 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8642 target = gen_reg_rtx (mode);
8643 expand_operands (treeop0, treeop1,
8644 target, &op0, &op1, EXPAND_NORMAL);
32eed045 8645
f994f296
MM
8646 /* First try to do it with a special MIN or MAX instruction.
8647 If that does not win, use a conditional jump to select the proper
8648 value. */
8649 this_optab = optab_for_tree_code (code, type, optab_default);
8650 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8651 OPTAB_WIDEN);
8652 if (temp != 0)
8653 return temp;
ed1223ba 8654
f994f296
MM
8655 /* At this point, a MEM target is no longer useful; we will get better
8656 code without it. */
ed1223ba 8657
f994f296
MM
8658 if (! REG_P (target))
8659 target = gen_reg_rtx (mode);
ed1223ba 8660
f994f296
MM
8661 /* If op1 was placed in target, swap op0 and op1. */
8662 if (target != op0 && target == op1)
8663 {
8664 temp = op0;
8665 op0 = op1;
8666 op1 = temp;
8667 }
ed1223ba 8668
f994f296
MM
8669 /* We generate better code and avoid problems with op1 mentioning
8670 target by forcing op1 into a pseudo if it isn't a constant. */
8671 if (! CONSTANT_P (op1))
8672 op1 = force_reg (mode, op1);
4af3895e 8673
bbf6f052 8674 {
f994f296
MM
8675 enum rtx_code comparison_code;
8676 rtx cmpop1 = op1;
ae00112b 8677
f994f296
MM
8678 if (code == MAX_EXPR)
8679 comparison_code = unsignedp ? GEU : GE;
8680 else
8681 comparison_code = unsignedp ? LEU : LE;
ae00112b 8682
f994f296
MM
8683 /* Canonicalize to comparisons against 0. */
8684 if (op1 == const1_rtx)
b8e444f4 8685 {
f994f296
MM
8686 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8687 or (a != 0 ? a : 1) for unsigned.
8688 For MIN we are safe converting (a <= 1 ? a : 1)
8689 into (a <= 0 ? a : 1) */
8690 cmpop1 = const0_rtx;
8691 if (code == MAX_EXPR)
8692 comparison_code = unsignedp ? NE : GT;
b8e444f4 8693 }
f994f296 8694 if (op1 == constm1_rtx && !unsignedp)
8d2e5f72 8695 {
f994f296
MM
8696 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8697 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8698 cmpop1 = const0_rtx;
8699 if (code == MIN_EXPR)
8700 comparison_code = LT;
8d2e5f72 8701 }
f994f296
MM
8702#ifdef HAVE_conditional_move
8703 /* Use a conditional move if possible. */
8704 if (can_conditionally_move_p (mode))
7bb0943f 8705 {
f994f296 8706 rtx insn;
7bb0943f 8707
f994f296
MM
8708 /* ??? Same problem as in expmed.c: emit_conditional_move
8709 forces a stack adjustment via compare_from_rtx, and we
8710 lose the stack adjustment if the sequence we are about
8711 to create is discarded. */
8712 do_pending_stack_adjust ();
2d48c13d 8713
f994f296 8714 start_sequence ();
2d48c13d 8715
f994f296
MM
8716 /* Try to emit the conditional move. */
8717 insn = emit_conditional_move (target, comparison_code,
8718 op0, cmpop1, mode,
8719 op0, op1, mode,
8720 unsignedp);
8721
8722 /* If we could do the conditional move, emit the sequence,
8723 and return. */
8724 if (insn)
89752202 8725 {
f994f296
MM
8726 rtx seq = get_insns ();
8727 end_sequence ();
8728 emit_insn (seq);
8729 return target;
89752202
HB
8730 }
8731
f994f296
MM
8732 /* Otherwise discard the sequence and fall back to code with
8733 branches. */
8734 end_sequence ();
7bb0943f 8735 }
f994f296
MM
8736#endif
8737 if (target != op0)
8738 emit_move_insn (target, op0);
7bb0943f 8739
f994f296
MM
8740 temp = gen_label_rtx ();
8741 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
40e90eac
JJ
8742 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8743 -1);
f994f296
MM
8744 }
8745 emit_move_insn (target, op1);
8746 emit_label (temp);
8747 return target;
1ce7f3c2 8748
f994f296
MM
8749 case BIT_NOT_EXPR:
8750 op0 = expand_expr (treeop0, subtarget,
8751 VOIDmode, EXPAND_NORMAL);
8752 if (modifier == EXPAND_STACK_PARM)
8753 target = 0;
e002c7cb 8754 /* In case we have to reduce the result to bitfield precision
205ec405
JJ
8755 for unsigned bitfield expand this as XOR with a proper constant
8756 instead. */
8757 if (reduce_bit_field && TYPE_UNSIGNED (type))
e002c7cb
RG
8758 temp = expand_binop (mode, xor_optab, op0,
8759 immed_double_int_const
8760 (double_int_mask (TYPE_PRECISION (type)), mode),
8761 target, 1, OPTAB_LIB_WIDEN);
8762 else
8763 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
f994f296
MM
8764 gcc_assert (temp);
8765 return temp;
f47e9b4e 8766
f994f296
MM
8767 /* ??? Can optimize bitwise operations with one arg constant.
8768 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8769 and (a bitwise1 b) bitwise2 b (etc)
8770 but that is probably not worth while. */
bbf6f052 8771
f994f296 8772 case BIT_AND_EXPR:
f994f296 8773 case BIT_IOR_EXPR:
f994f296
MM
8774 case BIT_XOR_EXPR:
8775 goto binop;
7a06d606 8776
f994f296
MM
8777 case LROTATE_EXPR:
8778 case RROTATE_EXPR:
8779 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8780 || (GET_MODE_PRECISION (TYPE_MODE (type))
8781 == TYPE_PRECISION (type)));
8782 /* fall through */
a281e72d 8783
f994f296
MM
8784 case LSHIFT_EXPR:
8785 case RSHIFT_EXPR:
8786 /* If this is a fixed-point operation, then we cannot use the code
8787 below because "expand_shift" doesn't support sat/no-sat fixed-point
8788 shifts. */
8789 if (ALL_FIXED_POINT_MODE_P (mode))
8790 goto binop;
3a94c984 8791
f994f296
MM
8792 if (! safe_from_p (subtarget, treeop1, 1))
8793 subtarget = 0;
8794 if (modifier == EXPAND_STACK_PARM)
8795 target = 0;
8796 op0 = expand_expr (treeop0, subtarget,
8797 VOIDmode, EXPAND_NORMAL);
eb6c3df1
RG
8798 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8799 unsignedp);
f994f296
MM
8800 if (code == LSHIFT_EXPR)
8801 temp = REDUCE_BIT_FIELD (temp);
8802 return temp;
bbf6f052 8803
f994f296
MM
8804 /* Could determine the answer when only additive constants differ. Also,
8805 the addition of one can be handled by changing the condition. */
8806 case LT_EXPR:
8807 case LE_EXPR:
8808 case GT_EXPR:
8809 case GE_EXPR:
8810 case EQ_EXPR:
8811 case NE_EXPR:
8812 case UNORDERED_EXPR:
8813 case ORDERED_EXPR:
8814 case UNLT_EXPR:
8815 case UNLE_EXPR:
8816 case UNGT_EXPR:
8817 case UNGE_EXPR:
8818 case UNEQ_EXPR:
8819 case LTGT_EXPR:
8820 temp = do_store_flag (ops,
8821 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8822 tmode != VOIDmode ? tmode : mode);
8823 if (temp)
8824 return temp;
dc6d66b3 8825
f994f296
MM
8826 /* Use a compare and a jump for BLKmode comparisons, or for function
8827 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
dc6d66b3 8828
f994f296
MM
8829 if ((target == 0
8830 || modifier == EXPAND_STACK_PARM
8831 || ! safe_from_p (target, treeop0, 1)
8832 || ! safe_from_p (target, treeop1, 1)
8833 /* Make sure we don't have a hard reg (such as function's return
8834 value) live across basic blocks, if not optimizing. */
8835 || (!optimize && REG_P (target)
8836 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8837 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
ef19912d 8838
f994f296 8839 emit_move_insn (target, const0_rtx);
ef19912d 8840
f994f296 8841 op1 = gen_label_rtx ();
40e90eac 8842 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
befdad07 8843
7bba408b
JJ
8844 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8845 emit_move_insn (target, constm1_rtx);
8846 else
8847 emit_move_insn (target, const1_rtx);
bbf6f052 8848
f994f296
MM
8849 emit_label (op1);
8850 return target;
bbf6f052 8851
f994f296
MM
8852 case COMPLEX_EXPR:
8853 /* Get the rtx code of the operands. */
8854 op0 = expand_normal (treeop0);
8855 op1 = expand_normal (treeop1);
05019f83 8856
f994f296
MM
8857 if (!target)
8858 target = gen_reg_rtx (TYPE_MODE (type));
41472af8 8859
f994f296
MM
8860 /* Move the real (op0) and imaginary (op1) parts to their location. */
8861 write_complex_part (target, op0, false);
8862 write_complex_part (target, op1, true);
f47e9b4e 8863
f994f296 8864 return target;
dc6d66b3 8865
f994f296
MM
8866 case WIDEN_SUM_EXPR:
8867 {
8868 tree oprnd0 = treeop0;
8869 tree oprnd1 = treeop1;
0d15e60c 8870
f994f296
MM
8871 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8872 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
8873 target, unsignedp);
8874 return target;
bbf6f052
RK
8875 }
8876
f994f296
MM
8877 case REDUC_MAX_EXPR:
8878 case REDUC_MIN_EXPR:
8879 case REDUC_PLUS_EXPR:
d2af6a68 8880 {
f994f296
MM
8881 op0 = expand_normal (treeop0);
8882 this_optab = optab_for_tree_code (code, type, optab_default);
8883 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
8884 gcc_assert (temp);
8885 return temp;
8886 }
d2af6a68 8887
f994f296
MM
8888 case VEC_LSHIFT_EXPR:
8889 case VEC_RSHIFT_EXPR:
8890 {
8891 target = expand_vec_shift_expr (ops, target);
8892 return target;
8893 }
14a774a9 8894
f994f296
MM
8895 case VEC_UNPACK_HI_EXPR:
8896 case VEC_UNPACK_LO_EXPR:
8897 {
8898 op0 = expand_normal (treeop0);
f994f296
MM
8899 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
8900 target, unsignedp);
8901 gcc_assert (temp);
8902 return temp;
8903 }
c3d32120 8904
f994f296
MM
8905 case VEC_UNPACK_FLOAT_HI_EXPR:
8906 case VEC_UNPACK_FLOAT_LO_EXPR:
8907 {
8908 op0 = expand_normal (treeop0);
8909 /* The signedness is determined from input operand. */
f994f296
MM
8910 temp = expand_widen_pattern_expr
8911 (ops, op0, NULL_RTX, NULL_RTX,
8912 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
14a774a9 8913
f994f296
MM
8914 gcc_assert (temp);
8915 return temp;
8916 }
d6a5ac33 8917
f994f296
MM
8918 case VEC_WIDEN_MULT_HI_EXPR:
8919 case VEC_WIDEN_MULT_LO_EXPR:
8920 {
8921 tree oprnd0 = treeop0;
8922 tree oprnd1 = treeop1;
1499e0a8 8923
f994f296
MM
8924 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8925 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8926 target, unsignedp);
8927 gcc_assert (target);
8928 return target;
8929 }
ed1223ba 8930
36ba4aae
IR
8931 case VEC_WIDEN_LSHIFT_HI_EXPR:
8932 case VEC_WIDEN_LSHIFT_LO_EXPR:
8933 {
8934 tree oprnd0 = treeop0;
8935 tree oprnd1 = treeop1;
8936
8937 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8938 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8939 target, unsignedp);
8940 gcc_assert (target);
8941 return target;
8942 }
8943
f994f296
MM
8944 case VEC_PACK_TRUNC_EXPR:
8945 case VEC_PACK_SAT_EXPR:
8946 case VEC_PACK_FIX_TRUNC_EXPR:
8947 mode = TYPE_MODE (TREE_TYPE (treeop0));
8948 goto binop;
2205ed25
RH
8949
8950 case VEC_PERM_EXPR:
22e4dee7
RH
8951 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
8952 op2 = expand_normal (treeop2);
ccdfb0e2
RH
8953
8954 /* Careful here: if the target doesn't support integral vector modes,
8955 a constant selection vector could wind up smooshed into a normal
8956 integral constant. */
8957 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
8958 {
8959 tree sel_type = TREE_TYPE (treeop2);
8960 enum machine_mode vmode
8961 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
8962 TYPE_VECTOR_SUBPARTS (sel_type));
8963 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
8964 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
8965 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
8966 }
8967 else
8968 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
8969
22e4dee7
RH
8970 temp = expand_vec_perm (mode, op0, op1, op2, target);
8971 gcc_assert (temp);
8972 return temp;
bbf6f052 8973
f471fe72
RG
8974 case DOT_PROD_EXPR:
8975 {
8976 tree oprnd0 = treeop0;
8977 tree oprnd1 = treeop1;
8978 tree oprnd2 = treeop2;
8979 rtx op2;
8980
8981 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8982 op2 = expand_normal (oprnd2);
8983 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8984 target, unsignedp);
8985 return target;
8986 }
8987
8988 case REALIGN_LOAD_EXPR:
8989 {
8990 tree oprnd0 = treeop0;
8991 tree oprnd1 = treeop1;
8992 tree oprnd2 = treeop2;
8993 rtx op2;
8994
8995 this_optab = optab_for_tree_code (code, type, optab_default);
8996 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8997 op2 = expand_normal (oprnd2);
8998 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
8999 target, unsignedp);
9000 gcc_assert (temp);
9001 return temp;
9002 }
9003
4e71066d
RG
9004 case COND_EXPR:
9005 /* A COND_EXPR with its type being VOID_TYPE represents a
9006 conditional jump and is handled in
9007 expand_gimple_cond_expr. */
9008 gcc_assert (!VOID_TYPE_P (type));
9009
9010 /* Note that COND_EXPRs whose type is a structure or union
9011 are required to be constructed to contain assignments of
9012 a temporary variable, so that we can evaluate them here
9013 for side effect only. If type is void, we must do likewise. */
9014
9015 gcc_assert (!TREE_ADDRESSABLE (type)
9016 && !ignore
9017 && TREE_TYPE (treeop1) != void_type_node
9018 && TREE_TYPE (treeop2) != void_type_node);
9019
683c600b
AP
9020 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9021 if (temp)
9022 return temp;
9023
4e71066d
RG
9024 /* If we are not to produce a result, we have no target. Otherwise,
9025 if a target was specified use it; it will not be used as an
9026 intermediate target unless it is safe. If no target, use a
9027 temporary. */
9028
9029 if (modifier != EXPAND_STACK_PARM
9030 && original_target
9031 && safe_from_p (original_target, treeop0, 1)
9032 && GET_MODE (original_target) == mode
4e71066d
RG
9033 && !MEM_P (original_target))
9034 temp = original_target;
9035 else
9474e8ab 9036 temp = assign_temp (type, 0, 1);
4e71066d
RG
9037
9038 do_pending_stack_adjust ();
9039 NO_DEFER_POP;
9040 op0 = gen_label_rtx ();
9041 op1 = gen_label_rtx ();
9042 jumpifnot (treeop0, op0, -1);
9043 store_expr (treeop1, temp,
9044 modifier == EXPAND_STACK_PARM,
9045 false);
9046
9047 emit_jump_insn (gen_jump (op1));
9048 emit_barrier ();
9049 emit_label (op0);
9050 store_expr (treeop2, temp,
9051 modifier == EXPAND_STACK_PARM,
9052 false);
9053
9054 emit_label (op1);
9055 OK_DEFER_POP;
9056 return temp;
9057
9058 case VEC_COND_EXPR:
9059 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9060 return target;
9061
f994f296
MM
9062 default:
9063 gcc_unreachable ();
9064 }
d6a5ac33 9065
f994f296
MM
9066 /* Here to do an ordinary binary operator. */
9067 binop:
9068 expand_operands (treeop0, treeop1,
9069 subtarget, &op0, &op1, EXPAND_NORMAL);
9070 binop2:
9071 this_optab = optab_for_tree_code (code, type, optab_default);
9072 binop3:
9073 if (modifier == EXPAND_STACK_PARM)
9074 target = 0;
9075 temp = expand_binop (mode, this_optab, op0, op1, target,
9076 unsignedp, OPTAB_LIB_WIDEN);
9077 gcc_assert (temp);
d906fd7f
RG
9078 /* Bitwise operations do not need bitfield reduction as we expect their
9079 operands being properly truncated. */
9080 if (code == BIT_XOR_EXPR
9081 || code == BIT_AND_EXPR
9082 || code == BIT_IOR_EXPR)
9083 return temp;
f994f296
MM
9084 return REDUCE_BIT_FIELD (temp);
9085}
9086#undef REDUCE_BIT_FIELD
7f62854a 9087
28ed065e 9088rtx
f994f296
MM
9089expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9090 enum expand_modifier modifier, rtx *alt_rtl)
9091{
9092 rtx op0, op1, temp, decl_rtl;
9093 tree type;
9094 int unsignedp;
9095 enum machine_mode mode;
9096 enum tree_code code = TREE_CODE (exp);
f994f296
MM
9097 rtx subtarget, original_target;
9098 int ignore;
9099 tree context;
9100 bool reduce_bit_field;
9101 location_t loc = EXPR_LOCATION (exp);
9102 struct separate_ops ops;
9103 tree treeop0, treeop1, treeop2;
d36d83e9
EB
9104 tree ssa_name = NULL_TREE;
9105 gimple g;
7f62854a 9106
f994f296
MM
9107 type = TREE_TYPE (exp);
9108 mode = TYPE_MODE (type);
9109 unsignedp = TYPE_UNSIGNED (type);
7f62854a 9110
f994f296
MM
9111 treeop0 = treeop1 = treeop2 = NULL_TREE;
9112 if (!VL_EXP_CLASS_P (exp))
9113 switch (TREE_CODE_LENGTH (code))
9114 {
9115 default:
9116 case 3: treeop2 = TREE_OPERAND (exp, 2);
9117 case 2: treeop1 = TREE_OPERAND (exp, 1);
9118 case 1: treeop0 = TREE_OPERAND (exp, 0);
9119 case 0: break;
9120 }
9121 ops.code = code;
9122 ops.type = type;
9123 ops.op0 = treeop0;
9124 ops.op1 = treeop1;
9125 ops.op2 = treeop2;
9126 ops.location = loc;
12342f90 9127
f994f296
MM
9128 ignore = (target == const0_rtx
9129 || ((CONVERT_EXPR_CODE_P (code)
9130 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9131 && TREE_CODE (type) == VOID_TYPE));
fdf473ae 9132
f994f296
MM
9133 /* An operation in what may be a bit-field type needs the
9134 result to be reduced to the precision of the bit-field type,
9135 which is narrower than that of the type's mode. */
9136 reduce_bit_field = (!ignore
a577c405 9137 && INTEGRAL_TYPE_P (type)
f994f296 9138 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
12342f90 9139
f994f296
MM
9140 /* If we are going to ignore this result, we need only do something
9141 if there is a side-effect somewhere in the expression. If there
9142 is, short-circuit the most common cases here. Note that we must
9143 not call expand_expr with anything but const0_rtx in case this
9144 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
d6a5ac33 9145
f994f296
MM
9146 if (ignore)
9147 {
9148 if (! TREE_SIDE_EFFECTS (exp))
9149 return const0_rtx;
9150
9151 /* Ensure we reference a volatile object even if value is ignored, but
9152 don't do this if all we are doing is taking its address. */
9153 if (TREE_THIS_VOLATILE (exp)
9154 && TREE_CODE (exp) != FUNCTION_DECL
9155 && mode != VOIDmode && mode != BLKmode
9156 && modifier != EXPAND_CONST_ADDRESS)
7acda552 9157 {
f994f296
MM
9158 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9159 if (MEM_P (temp))
e16b6fd0 9160 copy_to_reg (temp);
f994f296 9161 return const0_rtx;
7acda552
RK
9162 }
9163
f994f296
MM
9164 if (TREE_CODE_CLASS (code) == tcc_unary
9165 || code == COMPONENT_REF || code == INDIRECT_REF)
9166 return expand_expr (treeop0, const0_rtx, VOIDmode,
9167 modifier);
e675826d 9168
f994f296
MM
9169 else if (TREE_CODE_CLASS (code) == tcc_binary
9170 || TREE_CODE_CLASS (code) == tcc_comparison
9171 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9172 {
9173 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9174 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9175 return const0_rtx;
9176 }
9177 else if (code == BIT_FIELD_REF)
9178 {
9179 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9180 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9181 expand_expr (treeop2, const0_rtx, VOIDmode, modifier);
9182 return const0_rtx;
9183 }
e675826d 9184
f994f296
MM
9185 target = 0;
9186 }
e675826d 9187
f994f296
MM
9188 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9189 target = 0;
e675826d 9190
f994f296
MM
9191 /* Use subtarget as the target for operand 0 of a binary operation. */
9192 subtarget = get_subtarget (target);
9193 original_target = target;
e675826d 9194
f994f296
MM
9195 switch (code)
9196 {
9197 case LABEL_DECL:
9198 {
9199 tree function = decl_function_context (exp);
e675826d 9200
f994f296
MM
9201 temp = label_rtx (exp);
9202 temp = gen_rtx_LABEL_REF (Pmode, temp);
e675826d 9203
f994f296
MM
9204 if (function != current_function_decl
9205 && function != 0)
9206 LABEL_REF_NONLOCAL_P (temp) = 1;
9207
9208 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9209 return temp;
e675826d
EB
9210 }
9211
f994f296
MM
9212 case SSA_NAME:
9213 /* ??? ivopts calls expander, without any preparation from
9214 out-of-ssa. So fake instructions as if this was an access to the
9215 base variable. This unnecessarily allocates a pseudo, see how we can
9216 reuse it, if partition base vars have it set already. */
9217 if (!currently_expanding_to_rtl)
d36d83e9
EB
9218 return expand_expr_real_1 (SSA_NAME_VAR (exp), target, tmode, modifier,
9219 NULL);
9220
9221 g = get_gimple_for_ssa_name (exp);
dcde5957
JJ
9222 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9223 if (g == NULL
9224 && modifier == EXPAND_INITIALIZER
9225 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9226 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9227 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9228 g = SSA_NAME_DEF_STMT (exp);
d36d83e9 9229 if (g)
7bc14a04
PB
9230 {
9231 rtx r = expand_expr_real (gimple_assign_rhs_to_tree (g), target,
9232 tmode, modifier, NULL);
9233 if (REG_P (r) && !REG_EXPR (r))
9234 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9235 return r;
9236 }
d36d83e9
EB
9237
9238 ssa_name = exp;
9239 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9240 exp = SSA_NAME_VAR (ssa_name);
f994f296 9241 goto expand_decl_rtl;
ed239f5a 9242
f994f296
MM
9243 case PARM_DECL:
9244 case VAR_DECL:
9245 /* If a static var's type was incomplete when the decl was written,
9246 but the type is complete now, lay out the decl now. */
9247 if (DECL_SIZE (exp) == 0
9248 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9249 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9250 layout_decl (exp, 0);
9251
f994f296 9252 /* ... fall through ... */
ed239f5a 9253
f994f296
MM
9254 case FUNCTION_DECL:
9255 case RESULT_DECL:
9256 decl_rtl = DECL_RTL (exp);
9257 expand_decl_rtl:
9258 gcc_assert (decl_rtl);
9259 decl_rtl = copy_rtx (decl_rtl);
b748fbd6 9260 /* Record writes to register variables. */
d108e679
AS
9261 if (modifier == EXPAND_WRITE
9262 && REG_P (decl_rtl)
9263 && HARD_REGISTER_P (decl_rtl))
9264 add_to_hard_reg_set (&crtl->asm_clobbers,
9265 GET_MODE (decl_rtl), REGNO (decl_rtl));
ed239f5a 9266
f994f296
MM
9267 /* Ensure variable marked as used even if it doesn't go through
9268 a parser. If it hasn't be used yet, write out an external
9269 definition. */
bbee5843 9270 TREE_USED (exp) = 1;
ed239f5a 9271
f994f296
MM
9272 /* Show we haven't gotten RTL for this yet. */
9273 temp = 0;
ed239f5a 9274
f994f296
MM
9275 /* Variables inherited from containing functions should have
9276 been lowered by this point. */
9277 context = decl_function_context (exp);
9278 gcc_assert (!context
9279 || context == current_function_decl
9280 || TREE_STATIC (exp)
bd39cb52 9281 || DECL_EXTERNAL (exp)
f994f296
MM
9282 /* ??? C++ creates functions that are not TREE_STATIC. */
9283 || TREE_CODE (exp) == FUNCTION_DECL);
ed239f5a 9284
f994f296
MM
9285 /* This is the case of an array whose size is to be determined
9286 from its initializer, while the initializer is still being parsed.
9474e8ab 9287 ??? We aren't parsing while expanding anymore. */
c11c10d8 9288
f994f296
MM
9289 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9290 temp = validize_mem (decl_rtl);
ed239f5a 9291
f994f296
MM
9292 /* If DECL_RTL is memory, we are in the normal case and the
9293 address is not valid, get the address into a register. */
0fb7aeda 9294
f994f296
MM
9295 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9296 {
9297 if (alt_rtl)
9298 *alt_rtl = decl_rtl;
9299 decl_rtl = use_anchored_address (decl_rtl);
9300 if (modifier != EXPAND_CONST_ADDRESS
9301 && modifier != EXPAND_SUM
09e881c9
BE
9302 && !memory_address_addr_space_p (DECL_MODE (exp),
9303 XEXP (decl_rtl, 0),
9304 MEM_ADDR_SPACE (decl_rtl)))
f994f296
MM
9305 temp = replace_equiv_address (decl_rtl,
9306 copy_rtx (XEXP (decl_rtl, 0)));
ed239f5a
RK
9307 }
9308
f994f296
MM
9309 /* If we got something, return it. But first, set the alignment
9310 if the address is a register. */
9311 if (temp != 0)
9312 {
9313 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9314 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
ed239f5a 9315
f994f296
MM
9316 return temp;
9317 }
5be014d5 9318
2ba87a29
RS
9319 /* If the mode of DECL_RTL does not match that of the decl,
9320 there are two cases: we are dealing with a BLKmode value
9321 that is returned in a register, or we are dealing with
9322 a promoted value. In the latter case, return a SUBREG
9323 of the wanted mode, but mark it so that we know that it
9324 was already extended. */
9325 if (REG_P (decl_rtl)
9326 && DECL_MODE (exp) != BLKmode
9327 && GET_MODE (decl_rtl) != DECL_MODE (exp))
7f9844ca 9328 {
f994f296 9329 enum machine_mode pmode;
7f9844ca 9330
d36d83e9
EB
9331 /* Get the signedness to be used for this variable. Ensure we get
9332 the same mode we got when the variable was declared. */
9333 if (code == SSA_NAME
9334 && (g = SSA_NAME_DEF_STMT (ssa_name))
9335 && gimple_code (g) == GIMPLE_CALL)
25583c4f
RS
9336 {
9337 gcc_assert (!gimple_call_internal_p (g));
9338 pmode = promote_function_mode (type, mode, &unsignedp,
9339 gimple_call_fntype (g),
9340 2);
9341 }
d36d83e9
EB
9342 else
9343 pmode = promote_decl_mode (exp, &unsignedp);
f994f296
MM
9344 gcc_assert (GET_MODE (decl_rtl) == pmode);
9345
9346 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9347 SUBREG_PROMOTED_VAR_P (temp) = 1;
9348 SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
9349 return temp;
7f9844ca
RS
9350 }
9351
f994f296 9352 return decl_rtl;
bbf6f052 9353
f994f296
MM
9354 case INTEGER_CST:
9355 temp = immed_double_const (TREE_INT_CST_LOW (exp),
9356 TREE_INT_CST_HIGH (exp), mode);
bbf6f052 9357
f994f296 9358 return temp;
bbf6f052 9359
f994f296
MM
9360 case VECTOR_CST:
9361 {
9362 tree tmp = NULL_TREE;
9363 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9364 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9365 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9366 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9367 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9368 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9369 return const_vector_from_tree (exp);
9370 if (GET_MODE_CLASS (mode) == MODE_INT)
9371 {
9372 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9373 if (type_for_mode)
9374 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9375 }
9376 if (!tmp)
d2a12ae7
RG
9377 {
9378 VEC(constructor_elt,gc) *v;
9379 unsigned i;
9380 v = VEC_alloc (constructor_elt, gc, VECTOR_CST_NELTS (exp));
9381 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9382 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9383 tmp = build_constructor (type, v);
9384 }
f994f296
MM
9385 return expand_expr (tmp, ignore ? const0_rtx : target,
9386 tmode, modifier);
9387 }
bbf6f052 9388
f994f296
MM
9389 case CONST_DECL:
9390 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
bbf6f052 9391
f994f296
MM
9392 case REAL_CST:
9393 /* If optimized, generate immediate CONST_DOUBLE
9394 which will be turned into memory by reload if necessary.
cbbc503e 9395
f994f296
MM
9396 We used to force a register so that loop.c could see it. But
9397 this does not allow gen_* patterns to perform optimizations with
9398 the constants. It also produces two insns in cases like "x = 1.0;".
9399 On most machines, floating-point constants are not permitted in
9400 many insns, so we'd end up copying it to a register in any case.
bbf6f052 9401
f994f296
MM
9402 Now, we do the copying in expand_binop, if appropriate. */
9403 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9404 TYPE_MODE (TREE_TYPE (exp)));
cbbc503e 9405
f994f296
MM
9406 case FIXED_CST:
9407 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9408 TYPE_MODE (TREE_TYPE (exp)));
bbf6f052 9409
f994f296
MM
9410 case COMPLEX_CST:
9411 /* Handle evaluating a complex constant in a CONCAT target. */
9412 if (original_target && GET_CODE (original_target) == CONCAT)
4ef7870a 9413 {
f994f296
MM
9414 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9415 rtx rtarg, itarg;
9416
9417 rtarg = XEXP (original_target, 0);
9418 itarg = XEXP (original_target, 1);
9419
9420 /* Move the real and imaginary parts separately. */
9421 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9422 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9423
9424 if (op0 != rtarg)
9425 emit_move_insn (rtarg, op0);
9426 if (op1 != itarg)
9427 emit_move_insn (itarg, op1);
9428
9429 return original_target;
4ef7870a 9430 }
bbf6f052 9431
f994f296 9432 /* ... fall through ... */
bbf6f052 9433
f994f296
MM
9434 case STRING_CST:
9435 temp = expand_expr_constant (exp, 1, modifier);
14661f36 9436
f994f296
MM
9437 /* temp contains a constant address.
9438 On RISC machines where a constant address isn't valid,
9439 make some insns to get that address into a register. */
9440 if (modifier != EXPAND_CONST_ADDRESS
9441 && modifier != EXPAND_INITIALIZER
9442 && modifier != EXPAND_SUM
09e881c9
BE
9443 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9444 MEM_ADDR_SPACE (temp)))
f994f296
MM
9445 return replace_equiv_address (temp,
9446 copy_rtx (XEXP (temp, 0)));
9447 return temp;
14661f36 9448
f994f296
MM
9449 case SAVE_EXPR:
9450 {
9451 tree val = treeop0;
9452 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl);
ea87523e 9453
f994f296
MM
9454 if (!SAVE_EXPR_RESOLVED_P (exp))
9455 {
9456 /* We can indeed still hit this case, typically via builtin
9457 expanders calling save_expr immediately before expanding
9458 something. Assume this means that we only have to deal
9459 with non-BLKmode values. */
9460 gcc_assert (GET_MODE (ret) != BLKmode);
ae431183 9461
f994f296
MM
9462 val = build_decl (EXPR_LOCATION (exp),
9463 VAR_DECL, NULL, TREE_TYPE (exp));
9464 DECL_ARTIFICIAL (val) = 1;
9465 DECL_IGNORED_P (val) = 1;
9466 treeop0 = val;
9467 TREE_OPERAND (exp, 0) = treeop0;
9468 SAVE_EXPR_RESOLVED_P (exp) = 1;
1717e19e 9469
f994f296
MM
9470 if (!CONSTANT_P (ret))
9471 ret = copy_to_reg (ret);
9472 SET_DECL_RTL (val, ret);
9473 }
1717e19e 9474
f994f296
MM
9475 return ret;
9476 }
9477
f994f296
MM
9478
9479 case CONSTRUCTOR:
9480 /* If we don't need the result, just ensure we evaluate any
9481 subexpressions. */
9482 if (ignore)
1717e19e 9483 {
f994f296
MM
9484 unsigned HOST_WIDE_INT idx;
9485 tree value;
9486
9487 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9488 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9489
9490 return const0_rtx;
1717e19e
UW
9491 }
9492
f994f296 9493 return expand_constructor (exp, target, modifier, false);
bbf6f052 9494
be1ac4ec 9495 case TARGET_MEM_REF:
f994f296 9496 {
e831c1e8
RG
9497 addr_space_t as
9498 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
be1ac4ec 9499 struct mem_address addr;
28164eed 9500 enum insn_code icode;
cab35794 9501 unsigned int align;
09e881c9 9502
be1ac4ec
RG
9503 get_address_description (exp, &addr);
9504 op0 = addr_for_mem_ref (&addr, as, true);
09e881c9 9505 op0 = memory_address_addr_space (mode, op0, as);
f994f296 9506 temp = gen_rtx_MEM (mode, op0);
f994f296 9507 set_mem_attributes (temp, exp, 0);
09e881c9 9508 set_mem_addr_space (temp, as);
cab35794 9509 align = get_object_or_type_alignment (exp);
02f6574b
MJ
9510 if (modifier != EXPAND_WRITE
9511 && mode != BLKmode
cab35794 9512 && align < GET_MODE_ALIGNMENT (mode)
be1ac4ec
RG
9513 /* If the target does not have special handling for unaligned
9514 loads of mode then it can use regular moves for them. */
9515 && ((icode = optab_handler (movmisalign_optab, mode))
9516 != CODE_FOR_nothing))
f994f296 9517 {
28164eed 9518 struct expand_operand ops[2];
8403445a 9519
f994f296 9520 /* We've already validated the memory, and we're creating a
28164eed
RS
9521 new pseudo destination. The predicates really can't fail,
9522 nor can the generator. */
9523 create_output_operand (&ops[0], NULL_RTX, mode);
9524 create_fixed_operand (&ops[1], temp);
9525 expand_insn (icode, 2, ops);
9526 return ops[0].value;
f994f296 9527 }
f994f296
MM
9528 return temp;
9529 }
9530
70f34814
RG
9531 case MEM_REF:
9532 {
9533 addr_space_t as
e831c1e8 9534 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
70f34814
RG
9535 enum machine_mode address_mode;
9536 tree base = TREE_OPERAND (exp, 0);
75421dcd 9537 gimple def_stmt;
28164eed 9538 enum insn_code icode;
cab35794 9539 unsigned align;
70f34814
RG
9540 /* Handle expansion of non-aliased memory with non-BLKmode. That
9541 might end up in a register. */
22345357 9542 if (mem_ref_refers_to_non_mem_p (exp))
70f34814
RG
9543 {
9544 HOST_WIDE_INT offset = mem_ref_offset (exp).low;
9545 tree bit_offset;
22345357 9546 tree bftype;
70f34814 9547 base = TREE_OPERAND (base, 0);
22345357
RG
9548 if (offset == 0
9549 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
9550 && (GET_MODE_BITSIZE (DECL_MODE (base))
9551 == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))
9552 return expand_expr (build1 (VIEW_CONVERT_EXPR,
9553 TREE_TYPE (exp), base),
9554 target, tmode, modifier);
9555 bit_offset = bitsize_int (offset * BITS_PER_UNIT);
9556 bftype = TREE_TYPE (base);
9557 if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
9558 bftype = TREE_TYPE (exp);
9559 else
70f34814 9560 {
22345357 9561 temp = assign_stack_temp (DECL_MODE (base),
9474e8ab 9562 GET_MODE_SIZE (DECL_MODE (base)));
22345357
RG
9563 store_expr (base, temp, 0, false);
9564 temp = adjust_address (temp, BLKmode, offset);
9565 set_mem_size (temp, int_size_in_bytes (TREE_TYPE (exp)));
9566 return temp;
70f34814 9567 }
22345357
RG
9568 return expand_expr (build3 (BIT_FIELD_REF, bftype,
9569 base,
9570 TYPE_SIZE (TREE_TYPE (exp)),
9571 bit_offset),
9572 target, tmode, modifier);
70f34814
RG
9573 }
9574 address_mode = targetm.addr_space.address_mode (as);
75421dcd
RG
9575 base = TREE_OPERAND (exp, 0);
9576 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
be1ac4ec
RG
9577 {
9578 tree mask = gimple_assign_rhs2 (def_stmt);
9579 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9580 gimple_assign_rhs1 (def_stmt), mask);
9581 TREE_OPERAND (exp, 0) = base;
9582 }
cab35794 9583 align = get_object_or_type_alignment (exp);
ee01a467 9584 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
42a48c4f 9585 op0 = memory_address_addr_space (address_mode, op0, as);
70f34814 9586 if (!integer_zerop (TREE_OPERAND (exp, 1)))
b4351367
EB
9587 {
9588 rtx off
9589 = immed_double_int_const (mem_ref_offset (exp), address_mode);
9590 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9591 }
70f34814
RG
9592 op0 = memory_address_addr_space (mode, op0, as);
9593 temp = gen_rtx_MEM (mode, op0);
9594 set_mem_attributes (temp, exp, 0);
9595 set_mem_addr_space (temp, as);
9596 if (TREE_THIS_VOLATILE (exp))
9597 MEM_VOLATILE_P (temp) = 1;
02f6574b
MJ
9598 if (modifier != EXPAND_WRITE
9599 && mode != BLKmode
0a1c20dc 9600 && align < GET_MODE_ALIGNMENT (mode))
be1ac4ec 9601 {
0a1c20dc
MJ
9602 if ((icode = optab_handler (movmisalign_optab, mode))
9603 != CODE_FOR_nothing)
9604 {
9605 struct expand_operand ops[2];
9606
9607 /* We've already validated the memory, and we're creating a
9608 new pseudo destination. The predicates really can't fail,
9609 nor can the generator. */
9610 create_output_operand (&ops[0], NULL_RTX, mode);
9611 create_fixed_operand (&ops[1], temp);
9612 expand_insn (icode, 2, ops);
9613 return ops[0].value;
9614 }
9615 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9616 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9617 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9618 true, (modifier == EXPAND_STACK_PARM
9619 ? NULL_RTX : target),
9620 mode, mode);
be1ac4ec 9621 }
70f34814
RG
9622 return temp;
9623 }
9624
f994f296
MM
9625 case ARRAY_REF:
9626
9627 {
9628 tree array = treeop0;
9629 tree index = treeop1;
9630
9631 /* Fold an expression like: "foo"[2].
9632 This is not done in fold so it won't happen inside &.
9633 Don't fold if this is for wide characters since it's too
9634 difficult to do correctly and this is a very rare case. */
9635
9636 if (modifier != EXPAND_CONST_ADDRESS
9637 && modifier != EXPAND_INITIALIZER
9638 && modifier != EXPAND_MEMORY)
9639 {
9640 tree t = fold_read_from_constant_string (exp);
9641
9642 if (t)
9643 return expand_expr (t, target, tmode, modifier);
9644 }
9645
9646 /* If this is a constant index into a constant array,
9647 just get the value from the array. Handle both the cases when
9648 we have an explicit constructor and when our operand is a variable
9649 that was declared const. */
9650
9651 if (modifier != EXPAND_CONST_ADDRESS
9652 && modifier != EXPAND_INITIALIZER
9653 && modifier != EXPAND_MEMORY
9654 && TREE_CODE (array) == CONSTRUCTOR
9655 && ! TREE_SIDE_EFFECTS (array)
9656 && TREE_CODE (index) == INTEGER_CST)
9657 {
9658 unsigned HOST_WIDE_INT ix;
9659 tree field, value;
9660
9661 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9662 field, value)
9663 if (tree_int_cst_equal (field, index))
b10af0c8 9664 {
f994f296
MM
9665 if (!TREE_SIDE_EFFECTS (value))
9666 return expand_expr (fold (value), target, tmode, modifier);
9667 break;
b10af0c8 9668 }
f994f296 9669 }
bbf6f052 9670
f994f296
MM
9671 else if (optimize >= 1
9672 && modifier != EXPAND_CONST_ADDRESS
9673 && modifier != EXPAND_INITIALIZER
9674 && modifier != EXPAND_MEMORY
9675 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9676 && TREE_CODE (array) == VAR_DECL && DECL_INITIAL (array)
9677 && TREE_CODE (DECL_INITIAL (array)) != ERROR_MARK
051f8cc6 9678 && const_value_known_p (array))
f994f296
MM
9679 {
9680 if (TREE_CODE (index) == INTEGER_CST)
9681 {
9682 tree init = DECL_INITIAL (array);
0f996086 9683
f994f296
MM
9684 if (TREE_CODE (init) == CONSTRUCTOR)
9685 {
9686 unsigned HOST_WIDE_INT ix;
9687 tree field, value;
bbf6f052 9688
f994f296
MM
9689 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9690 field, value)
9691 if (tree_int_cst_equal (field, index))
9692 {
9693 if (TREE_SIDE_EFFECTS (value))
9694 break;
bbf6f052 9695
f994f296
MM
9696 if (TREE_CODE (value) == CONSTRUCTOR)
9697 {
9698 /* If VALUE is a CONSTRUCTOR, this
9699 optimization is only useful if
9700 this doesn't store the CONSTRUCTOR
9701 into memory. If it does, it is more
9702 efficient to just load the data from
9703 the array directly. */
9704 rtx ret = expand_constructor (value, target,
9705 modifier, true);
9706 if (ret == NULL_RTX)
9707 break;
9708 }
bbf6f052 9709
f994f296
MM
9710 return expand_expr (fold (value), target, tmode,
9711 modifier);
9712 }
9713 }
9714 else if(TREE_CODE (init) == STRING_CST)
9715 {
9716 tree index1 = index;
9717 tree low_bound = array_ref_low_bound (exp);
9718 index1 = fold_convert_loc (loc, sizetype,
9719 treeop1);
0f996086 9720
f994f296 9721 /* Optimize the special-case of a zero lower bound.
0f996086 9722
f994f296
MM
9723 We convert the low_bound to sizetype to avoid some problems
9724 with constant folding. (E.g. suppose the lower bound is 1,
9725 and its mode is QI. Without the conversion,l (ARRAY
9726 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9727 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
bbf6f052 9728
f994f296
MM
9729 if (! integer_zerop (low_bound))
9730 index1 = size_diffop_loc (loc, index1,
9731 fold_convert_loc (loc, sizetype,
9732 low_bound));
bbf6f052 9733
f994f296
MM
9734 if (0 > compare_tree_int (index1,
9735 TREE_STRING_LENGTH (init)))
9736 {
9737 tree type = TREE_TYPE (TREE_TYPE (init));
9738 enum machine_mode mode = TYPE_MODE (type);
bbf6f052 9739
f994f296
MM
9740 if (GET_MODE_CLASS (mode) == MODE_INT
9741 && GET_MODE_SIZE (mode) == 1)
9742 return gen_int_mode (TREE_STRING_POINTER (init)
9743 [TREE_INT_CST_LOW (index1)],
9744 mode);
9745 }
9746 }
9747 }
9748 }
9749 }
9750 goto normal_inner_ref;
9751
9752 case COMPONENT_REF:
9753 /* If the operand is a CONSTRUCTOR, we can just extract the
9754 appropriate field if it is present. */
9755 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9756 {
9757 unsigned HOST_WIDE_INT idx;
9758 tree field, value;
9759
9760 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9761 idx, field, value)
9762 if (field == treeop1
9763 /* We can normally use the value of the field in the
9764 CONSTRUCTOR. However, if this is a bitfield in
9765 an integral mode that we can fit in a HOST_WIDE_INT,
9766 we must mask only the number of bits in the bitfield,
9767 since this is done implicitly by the constructor. If
9768 the bitfield does not meet either of those conditions,
9769 we can't do this optimization. */
9770 && (! DECL_BIT_FIELD (field)
9771 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
69660a70 9772 && (GET_MODE_PRECISION (DECL_MODE (field))
f994f296
MM
9773 <= HOST_BITS_PER_WIDE_INT))))
9774 {
9775 if (DECL_BIT_FIELD (field)
9776 && modifier == EXPAND_STACK_PARM)
9777 target = 0;
9778 op0 = expand_expr (value, target, tmode, modifier);
9779 if (DECL_BIT_FIELD (field))
9780 {
9781 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9782 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9783
9784 if (TYPE_UNSIGNED (TREE_TYPE (field)))
9785 {
9786 op1 = GEN_INT (((HOST_WIDE_INT) 1 << bitsize) - 1);
9787 op0 = expand_and (imode, op0, op1, target);
9788 }
9789 else
9790 {
69660a70 9791 int count = GET_MODE_PRECISION (imode) - bitsize;
f994f296
MM
9792
9793 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9794 target, 0);
9795 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9796 target, 0);
9797 }
9798 }
9799
9800 return op0;
9801 }
9802 }
9803 goto normal_inner_ref;
9804
9805 case BIT_FIELD_REF:
9806 case ARRAY_RANGE_REF:
9807 normal_inner_ref:
9808 {
9809 enum machine_mode mode1, mode2;
9810 HOST_WIDE_INT bitsize, bitpos;
9811 tree offset;
9812 int volatilep = 0, must_force_mem;
62519f7f 9813 bool packedp = false;
f994f296
MM
9814 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
9815 &mode1, &unsignedp, &volatilep, true);
9816 rtx orig_op0, memloc;
65dc645a 9817 bool mem_attrs_from_type = false;
f994f296
MM
9818
9819 /* If we got back the original object, something is wrong. Perhaps
9820 we are evaluating an expression too early. In any event, don't
9821 infinitely recurse. */
9822 gcc_assert (tem != exp);
9823
62519f7f
JZ
9824 if (TYPE_PACKED (TREE_TYPE (TREE_OPERAND (exp, 0)))
9825 || (TREE_CODE (TREE_OPERAND (exp, 1)) == FIELD_DECL
9826 && DECL_PACKED (TREE_OPERAND (exp, 1))))
9827 packedp = true;
9828
f994f296
MM
9829 /* If TEM's type is a union of variable size, pass TARGET to the inner
9830 computation, since it will need a temporary and TARGET is known
9831 to have to do. This occurs in unchecked conversion in Ada. */
9832 orig_op0 = op0
9833 = expand_expr (tem,
9834 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
4519d745 9835 && COMPLETE_TYPE_P (TREE_TYPE (tem))
f994f296
MM
9836 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9837 != INTEGER_CST)
9838 && modifier != EXPAND_STACK_PARM
9839 ? target : NULL_RTX),
9840 VOIDmode,
9841 (modifier == EXPAND_INITIALIZER
9842 || modifier == EXPAND_CONST_ADDRESS
9843 || modifier == EXPAND_STACK_PARM)
9844 ? modifier : EXPAND_NORMAL);
9845
6a78b724
DD
9846
9847 /* If the bitfield is volatile, we want to access it in the
1004f0c5
JJ
9848 field's mode, not the computed mode.
9849 If a MEM has VOIDmode (external with incomplete type),
9850 use BLKmode for it instead. */
9851 if (MEM_P (op0))
9852 {
9853 if (volatilep && flag_strict_volatile_bitfields > 0)
9854 op0 = adjust_address (op0, mode1, 0);
9855 else if (GET_MODE (op0) == VOIDmode)
9856 op0 = adjust_address (op0, BLKmode, 0);
9857 }
6a78b724 9858
f994f296
MM
9859 mode2
9860 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
9861
9862 /* If we have either an offset, a BLKmode result, or a reference
9863 outside the underlying object, we must force it to memory.
9864 Such a case can occur in Ada if we have unchecked conversion
9865 of an expression from a scalar type to an aggregate type or
9866 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
9867 passed a partially uninitialized object or a view-conversion
9868 to a larger size. */
9869 must_force_mem = (offset
9870 || mode1 == BLKmode
9871 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
9872
9873 /* Handle CONCAT first. */
9874 if (GET_CODE (op0) == CONCAT && !must_force_mem)
9875 {
9876 if (bitpos == 0
9877 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
9878 return op0;
9879 if (bitpos == 0
9880 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9881 && bitsize)
9882 {
9883 op0 = XEXP (op0, 0);
9884 mode2 = GET_MODE (op0);
9885 }
9886 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9887 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
9888 && bitpos
9889 && bitsize)
9890 {
9891 op0 = XEXP (op0, 1);
9892 bitpos = 0;
9893 mode2 = GET_MODE (op0);
9894 }
9895 else
9896 /* Otherwise force into memory. */
9897 must_force_mem = 1;
9898 }
9899
9900 /* If this is a constant, put it in a register if it is a legitimate
9901 constant and we don't need a memory reference. */
9902 if (CONSTANT_P (op0)
9903 && mode2 != BLKmode
1a627b35 9904 && targetm.legitimate_constant_p (mode2, op0)
f994f296
MM
9905 && !must_force_mem)
9906 op0 = force_reg (mode2, op0);
9907
9908 /* Otherwise, if this is a constant, try to force it to the constant
9909 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
9910 is a legitimate constant. */
9911 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
9912 op0 = validize_mem (memloc);
9913
9914 /* Otherwise, if this is a constant or the object is not in memory
9915 and need be, put it there. */
9916 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
9917 {
9918 tree nt = build_qualified_type (TREE_TYPE (tem),
9919 (TYPE_QUALS (TREE_TYPE (tem))
9920 | TYPE_QUAL_CONST));
9474e8ab 9921 memloc = assign_temp (nt, 1, 1);
f994f296
MM
9922 emit_move_insn (memloc, op0);
9923 op0 = memloc;
65dc645a 9924 mem_attrs_from_type = true;
f994f296
MM
9925 }
9926
9927 if (offset)
9928 {
d4ebfa65 9929 enum machine_mode address_mode;
f994f296
MM
9930 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
9931 EXPAND_SUM);
9932
9933 gcc_assert (MEM_P (op0));
9934
372d6395 9935 address_mode = get_address_mode (op0);
d4ebfa65
BE
9936 if (GET_MODE (offset_rtx) != address_mode)
9937 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
f994f296
MM
9938
9939 if (GET_MODE (op0) == BLKmode
9940 /* A constant address in OP0 can have VOIDmode, we must
9941 not try to call force_reg in that case. */
9942 && GET_MODE (XEXP (op0, 0)) != VOIDmode
9943 && bitsize != 0
9944 && (bitpos % bitsize) == 0
9945 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
9946 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
9947 {
9948 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
9949 bitpos = 0;
9950 }
9951
9952 op0 = offset_address (op0, offset_rtx,
9953 highest_pow2_factor (offset));
9954 }
9955
9956 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
9957 record its alignment as BIGGEST_ALIGNMENT. */
9958 if (MEM_P (op0) && bitpos == 0 && offset != 0
9959 && is_aligning_offset (offset, tem))
9960 set_mem_align (op0, BIGGEST_ALIGNMENT);
9961
9962 /* Don't forget about volatility even if this is a bitfield. */
9963 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
9964 {
9965 if (op0 == orig_op0)
9966 op0 = copy_rtx (op0);
9967
9968 MEM_VOLATILE_P (op0) = 1;
9969 }
9970
9971 /* In cases where an aligned union has an unaligned object
9972 as a field, we might be extracting a BLKmode value from
9973 an integer-mode (e.g., SImode) object. Handle this case
9974 by doing the extract into an object as wide as the field
9975 (which we know to be the width of a basic mode), then
9976 storing into memory, and changing the mode to BLKmode. */
9977 if (mode1 == VOIDmode
9978 || REG_P (op0) || GET_CODE (op0) == SUBREG
9979 || (mode1 != BLKmode && ! direct_load[(int) mode1]
9980 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9981 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
9982 && modifier != EXPAND_CONST_ADDRESS
9983 && modifier != EXPAND_INITIALIZER)
6a78b724 9984 /* If the field is volatile, we always want an aligned
e5fe4319
JY
9985 access. Do this in following two situations:
9986 1. the access is not already naturally
bf90208f 9987 aligned, otherwise "normal" (non-bitfield) volatile fields
e5fe4319
JY
9988 become non-addressable.
9989 2. the bitsize is narrower than the access size. Need
9990 to extract bitfields from the access. */
bf90208f 9991 || (volatilep && flag_strict_volatile_bitfields > 0
e5fe4319
JY
9992 && (bitpos % GET_MODE_ALIGNMENT (mode) != 0
9993 || (mode1 != BLKmode
9994 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)))
f994f296
MM
9995 /* If the field isn't aligned enough to fetch as a memref,
9996 fetch it as a bit field. */
9997 || (mode1 != BLKmode
9998 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
9999 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10000 || (MEM_P (op0)
10001 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10002 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10003 && ((modifier == EXPAND_CONST_ADDRESS
10004 || modifier == EXPAND_INITIALIZER)
10005 ? STRICT_ALIGNMENT
10006 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10007 || (bitpos % BITS_PER_UNIT != 0)))
10008 /* If the type and the field are a constant size and the
10009 size of the type isn't the same size as the bitfield,
10010 we must use bitfield operations. */
10011 || (bitsize >= 0
10012 && TYPE_SIZE (TREE_TYPE (exp))
10013 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10014 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10015 bitsize)))
10016 {
10017 enum machine_mode ext_mode = mode;
10018
10019 if (ext_mode == BLKmode
10020 && ! (target != 0 && MEM_P (op0)
10021 && MEM_P (target)
10022 && bitpos % BITS_PER_UNIT == 0))
10023 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10024
10025 if (ext_mode == BLKmode)
10026 {
10027 if (target == 0)
9474e8ab 10028 target = assign_temp (type, 1, 1);
f994f296
MM
10029
10030 if (bitsize == 0)
10031 return target;
10032
10033 /* In this case, BITPOS must start at a byte boundary and
10034 TARGET, if specified, must be a MEM. */
10035 gcc_assert (MEM_P (op0)
10036 && (!target || MEM_P (target))
10037 && !(bitpos % BITS_PER_UNIT));
10038
10039 emit_block_move (target,
10040 adjust_address (op0, VOIDmode,
10041 bitpos / BITS_PER_UNIT),
10042 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10043 / BITS_PER_UNIT),
10044 (modifier == EXPAND_STACK_PARM
10045 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10046
10047 return target;
10048 }
10049
10050 op0 = validize_mem (op0);
10051
10052 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10053 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10054
62519f7f 10055 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, packedp,
f994f296
MM
10056 (modifier == EXPAND_STACK_PARM
10057 ? NULL_RTX : target),
10058 ext_mode, ext_mode);
10059
10060 /* If the result is a record type and BITSIZE is narrower than
10061 the mode of OP0, an integral mode, and this is a big endian
10062 machine, we must put the field into the high-order bits. */
10063 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10064 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10065 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10066 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
eb6c3df1
RG
10067 GET_MODE_BITSIZE (GET_MODE (op0))
10068 - bitsize, op0, 1);
f994f296
MM
10069
10070 /* If the result type is BLKmode, store the data into a temporary
10071 of the appropriate type, but with the mode corresponding to the
10072 mode for the data we have (op0's mode). It's tempting to make
10073 this a constant type, since we know it's only being stored once,
10074 but that can cause problems if we are taking the address of this
10075 COMPONENT_REF because the MEM of any reference via that address
10076 will have flags corresponding to the type, which will not
10077 necessarily be constant. */
10078 if (mode == BLKmode)
10079 {
f994f296
MM
10080 rtx new_rtx;
10081
e9d68de9
MJ
10082 new_rtx = assign_stack_temp_for_type (ext_mode,
10083 GET_MODE_BITSIZE (ext_mode),
9474e8ab 10084 type);
f994f296
MM
10085 emit_move_insn (new_rtx, op0);
10086 op0 = copy_rtx (new_rtx);
10087 PUT_MODE (op0, BLKmode);
f994f296 10088 }
2d7050fd 10089
f994f296
MM
10090 return op0;
10091 }
bbf6f052 10092
f994f296
MM
10093 /* If the result is BLKmode, use that to access the object
10094 now as well. */
10095 if (mode == BLKmode)
10096 mode1 = BLKmode;
bbf6f052 10097
f994f296
MM
10098 /* Get a reference to just this component. */
10099 if (modifier == EXPAND_CONST_ADDRESS
10100 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10101 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10102 else
10103 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
bbf6f052 10104
f994f296
MM
10105 if (op0 == orig_op0)
10106 op0 = copy_rtx (op0);
bbf6f052 10107
65dc645a
MJ
10108 /* If op0 is a temporary because of forcing to memory, pass only the
10109 type to set_mem_attributes so that the original expression is never
10110 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10111 if (mem_attrs_from_type)
10112 set_mem_attributes (op0, type, 0);
10113 else
10114 set_mem_attributes (op0, exp, 0);
10115
f994f296
MM
10116 if (REG_P (XEXP (op0, 0)))
10117 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
3a94c984 10118
f994f296
MM
10119 MEM_VOLATILE_P (op0) |= volatilep;
10120 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10121 || modifier == EXPAND_CONST_ADDRESS
10122 || modifier == EXPAND_INITIALIZER)
10123 return op0;
10124 else if (target == 0)
10125 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
fa2981d8 10126
f994f296
MM
10127 convert_move (target, op0, unsignedp);
10128 return target;
10129 }
e3be1116 10130
f994f296
MM
10131 case OBJ_TYPE_REF:
10132 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
dbedefae 10133
f994f296
MM
10134 case CALL_EXPR:
10135 /* All valid uses of __builtin_va_arg_pack () are removed during
10136 inlining. */
10137 if (CALL_EXPR_VA_ARG_PACK (exp))
10138 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
230dedb3 10139 {
f994f296 10140 tree fndecl = get_callee_fndecl (exp), attr;
927630a5 10141
f994f296
MM
10142 if (fndecl
10143 && (attr = lookup_attribute ("error",
10144 DECL_ATTRIBUTES (fndecl))) != NULL)
10145 error ("%Kcall to %qs declared with attribute error: %s",
10146 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10147 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10148 if (fndecl
10149 && (attr = lookup_attribute ("warning",
10150 DECL_ATTRIBUTES (fndecl))) != NULL)
10151 warning_at (tree_nonartificial_location (exp),
10152 0, "%Kcall to %qs declared with attribute warning: %s",
10153 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10154 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
927630a5 10155
f994f296
MM
10156 /* Check for a built-in function. */
10157 if (fndecl && DECL_BUILT_IN (fndecl))
230dedb3 10158 {
f994f296
MM
10159 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10160 return expand_builtin (exp, target, subtarget, tmode, ignore);
230dedb3 10161 }
f994f296
MM
10162 }
10163 return expand_call (exp, target, ignore);
927630a5 10164
f994f296
MM
10165 case VIEW_CONVERT_EXPR:
10166 op0 = NULL_RTX;
927630a5 10167
f994f296
MM
10168 /* If we are converting to BLKmode, try to avoid an intermediate
10169 temporary by fetching an inner memory reference. */
10170 if (mode == BLKmode
10171 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10172 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10173 && handled_component_p (treeop0))
10174 {
10175 enum machine_mode mode1;
10176 HOST_WIDE_INT bitsize, bitpos;
10177 tree offset;
10178 int unsignedp;
10179 int volatilep = 0;
10180 tree tem
10181 = get_inner_reference (treeop0, &bitsize, &bitpos,
10182 &offset, &mode1, &unsignedp, &volatilep,
10183 true);
10184 rtx orig_op0;
927630a5 10185
f994f296
MM
10186 /* ??? We should work harder and deal with non-zero offsets. */
10187 if (!offset
10188 && (bitpos % BITS_PER_UNIT) == 0
10189 && bitsize >= 0
10190 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) == 0)
10191 {
10192 /* See the normal_inner_ref case for the rationale. */
10193 orig_op0
10194 = expand_expr (tem,
10195 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10196 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10197 != INTEGER_CST)
10198 && modifier != EXPAND_STACK_PARM
10199 ? target : NULL_RTX),
10200 VOIDmode,
10201 (modifier == EXPAND_INITIALIZER
10202 || modifier == EXPAND_CONST_ADDRESS
10203 || modifier == EXPAND_STACK_PARM)
10204 ? modifier : EXPAND_NORMAL);
927630a5 10205
f994f296 10206 if (MEM_P (orig_op0))
230dedb3 10207 {
f994f296 10208 op0 = orig_op0;
230dedb3 10209
f994f296
MM
10210 /* Get a reference to just this component. */
10211 if (modifier == EXPAND_CONST_ADDRESS
10212 || modifier == EXPAND_SUM
10213 || modifier == EXPAND_INITIALIZER)
10214 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10215 else
10216 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
d6a5ac33 10217
f994f296
MM
10218 if (op0 == orig_op0)
10219 op0 = copy_rtx (op0);
10220
10221 set_mem_attributes (op0, treeop0, 0);
10222 if (REG_P (XEXP (op0, 0)))
10223 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10224
10225 MEM_VOLATILE_P (op0) |= volatilep;
10226 }
10227 }
230dedb3 10228 }
bbf6f052 10229
f994f296
MM
10230 if (!op0)
10231 op0 = expand_expr (treeop0,
10232 NULL_RTX, VOIDmode, modifier);
10233
10234 /* If the input and output modes are both the same, we are done. */
10235 if (mode == GET_MODE (op0))
10236 ;
10237 /* If neither mode is BLKmode, and both modes are the same size
10238 then we can use gen_lowpart. */
10239 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
69660a70
BS
10240 && (GET_MODE_PRECISION (mode)
10241 == GET_MODE_PRECISION (GET_MODE (op0)))
f994f296
MM
10242 && !COMPLEX_MODE_P (GET_MODE (op0)))
10243 {
10244 if (GET_CODE (op0) == SUBREG)
10245 op0 = force_reg (GET_MODE (op0), op0);
220c5f0c
JJ
10246 temp = gen_lowpart_common (mode, op0);
10247 if (temp)
10248 op0 = temp;
10249 else
10250 {
10251 if (!REG_P (op0) && !MEM_P (op0))
10252 op0 = force_reg (GET_MODE (op0), op0);
10253 op0 = gen_lowpart (mode, op0);
10254 }
f994f296 10255 }
915f5921
EB
10256 /* If both types are integral, convert from one mode to the other. */
10257 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
b8698a0f 10258 op0 = convert_modes (mode, GET_MODE (op0), op0,
f994f296
MM
10259 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10260 /* As a last resort, spill op0 to memory, and reload it in a
10261 different mode. */
10262 else if (!MEM_P (op0))
10263 {
10264 /* If the operand is not a MEM, force it into memory. Since we
10265 are going to be changing the mode of the MEM, don't call
10266 force_const_mem for constants because we don't allow pool
10267 constants to change mode. */
10268 tree inner_type = TREE_TYPE (treeop0);
10269
10270 gcc_assert (!TREE_ADDRESSABLE (exp));
bbf6f052 10271
f994f296
MM
10272 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10273 target
10274 = assign_stack_temp_for_type
10275 (TYPE_MODE (inner_type),
9474e8ab 10276 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
d6a5ac33 10277
f994f296
MM
10278 emit_move_insn (target, op0);
10279 op0 = target;
10280 }
d6a5ac33 10281
f994f296
MM
10282 /* At this point, OP0 is in the correct mode. If the output type is
10283 such that the operand is known to be aligned, indicate that it is.
10284 Otherwise, we need only be concerned about alignment for non-BLKmode
10285 results. */
10286 if (MEM_P (op0))
10287 {
a9d3ac1e
RG
10288 enum insn_code icode;
10289
f994f296 10290 op0 = copy_rtx (op0);
bbf6f052 10291
f994f296
MM
10292 if (TYPE_ALIGN_OK (type))
10293 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
a9d3ac1e
RG
10294 else if (mode != BLKmode
10295 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)
10296 /* If the target does have special handling for unaligned
10297 loads of mode then use them. */
10298 && ((icode = optab_handler (movmisalign_optab, mode))
10299 != CODE_FOR_nothing))
10300 {
10301 rtx reg, insn;
10302
10303 op0 = adjust_address (op0, mode, 0);
10304 /* We've already validated the memory, and we're creating a
10305 new pseudo destination. The predicates really can't
10306 fail. */
10307 reg = gen_reg_rtx (mode);
10308
10309 /* Nor can the insn generator. */
10310 insn = GEN_FCN (icode) (reg, op0);
10311 emit_insn (insn);
10312 return reg;
10313 }
f994f296
MM
10314 else if (STRICT_ALIGNMENT
10315 && mode != BLKmode
10316 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10317 {
10318 tree inner_type = TREE_TYPE (treeop0);
10319 HOST_WIDE_INT temp_size
10320 = MAX (int_size_in_bytes (inner_type),
10321 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10322 rtx new_rtx
9474e8ab 10323 = assign_stack_temp_for_type (mode, temp_size, type);
f994f296
MM
10324 rtx new_with_op0_mode
10325 = adjust_address (new_rtx, GET_MODE (op0), 0);
bbf6f052 10326
f994f296 10327 gcc_assert (!TREE_ADDRESSABLE (exp));
bbf6f052 10328
f994f296
MM
10329 if (GET_MODE (op0) == BLKmode)
10330 emit_block_move (new_with_op0_mode, op0,
10331 GEN_INT (GET_MODE_SIZE (mode)),
10332 (modifier == EXPAND_STACK_PARM
10333 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10334 else
10335 emit_move_insn (new_with_op0_mode, op0);
70582b3a 10336
f994f296
MM
10337 op0 = new_rtx;
10338 }
0f996086 10339
f994f296
MM
10340 op0 = adjust_address (op0, mode, 0);
10341 }
bbf6f052 10342
f994f296 10343 return op0;
6de9cd9a 10344
939409af
RS
10345 case MODIFY_EXPR:
10346 {
b32e7cdb
MM
10347 tree lhs = treeop0;
10348 tree rhs = treeop1;
df9af2bb
KH
10349 gcc_assert (ignore);
10350
bbf6f052
RK
10351 /* Check for |= or &= of a bitfield of size one into another bitfield
10352 of size 1. In this case, (unless we need the result of the
10353 assignment) we can do this more efficiently with a
10354 test followed by an assignment, if necessary.
10355
10356 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10357 things change so we do, this code should be enhanced to
10358 support it. */
df9af2bb 10359 if (TREE_CODE (lhs) == COMPONENT_REF
bbf6f052
RK
10360 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10361 || TREE_CODE (rhs) == BIT_AND_EXPR)
10362 && TREE_OPERAND (rhs, 0) == lhs
10363 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
05bccae2
RK
10364 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10365 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
bbf6f052
RK
10366 {
10367 rtx label = gen_label_rtx ();
3967bc2d 10368 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
bbf6f052 10369 do_jump (TREE_OPERAND (rhs, 1),
3967bc2d 10370 value ? label : 0,
40e90eac 10371 value ? 0 : label, -1);
79f5e442
ZD
10372 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10373 MOVE_NONTEMPORAL (exp));
e7c33f54 10374 do_pending_stack_adjust ();
bbf6f052
RK
10375 emit_label (label);
10376 return const0_rtx;
10377 }
10378
79f5e442 10379 expand_assignment (lhs, rhs, MOVE_NONTEMPORAL (exp));
7f8adc4e 10380 return const0_rtx;
bbf6f052
RK
10381 }
10382
bbf6f052 10383 case ADDR_EXPR:
70bb498a 10384 return expand_expr_addr_expr (exp, target, tmode, modifier);
bbf6f052 10385
7308a047 10386 case REALPART_EXPR:
b32e7cdb 10387 op0 = expand_normal (treeop0);
1466e387 10388 return read_complex_part (op0, false);
3a94c984 10389
7308a047 10390 case IMAGPART_EXPR:
b32e7cdb 10391 op0 = expand_normal (treeop0);
1466e387 10392 return read_complex_part (op0, true);
7308a047 10393
28ed065e
MM
10394 case RETURN_EXPR:
10395 case LABEL_EXPR:
10396 case GOTO_EXPR:
10397 case SWITCH_EXPR:
10398 case ASM_EXPR:
28ed065e
MM
10399 /* Expanded in cfgexpand.c. */
10400 gcc_unreachable ();
6de9cd9a 10401
e976b8b2 10402 case TRY_CATCH_EXPR:
6de9cd9a 10403 case CATCH_EXPR:
6de9cd9a 10404 case EH_FILTER_EXPR:
b335b813 10405 case TRY_FINALLY_EXPR:
ac45df5d 10406 /* Lowered by tree-eh.c. */
5b0264cb 10407 gcc_unreachable ();
b335b813 10408
ac45df5d
RH
10409 case WITH_CLEANUP_EXPR:
10410 case CLEANUP_POINT_EXPR:
10411 case TARGET_EXPR:
165b54c3 10412 case CASE_LABEL_EXPR:
77c9db77 10413 case VA_ARG_EXPR:
caf93cb0 10414 case BIND_EXPR:
e5bacf32
PB
10415 case INIT_EXPR:
10416 case CONJ_EXPR:
10417 case COMPOUND_EXPR:
10418 case PREINCREMENT_EXPR:
10419 case PREDECREMENT_EXPR:
10420 case POSTINCREMENT_EXPR:
10421 case POSTDECREMENT_EXPR:
10422 case LOOP_EXPR:
10423 case EXIT_EXPR:
ca5f4331 10424 case COMPOUND_LITERAL_EXPR:
ac45df5d 10425 /* Lowered by gimplify.c. */
5b0264cb 10426 gcc_unreachable ();
b335b813 10427
67231816
RH
10428 case FDESC_EXPR:
10429 /* Function descriptors are not valid except for as
10430 initialization constants, and should not be expanded. */
5b0264cb 10431 gcc_unreachable ();
67231816 10432
d25cee4d
RH
10433 case WITH_SIZE_EXPR:
10434 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10435 have pulled out the size to use in whatever context it needed. */
b32e7cdb 10436 return expand_expr_real (treeop0, original_target, tmode,
d25cee4d
RH
10437 modifier, alt_rtl);
10438
bbf6f052 10439 default:
f994f296 10440 return expand_expr_real_2 (&ops, target, tmode, modifier);
bbf6f052 10441 }
bc15d0ef 10442}
bc15d0ef
JM
10443\f
10444/* Subroutine of above: reduce EXP to the precision of TYPE (in the
10445 signedness of TYPE), possibly returning the result in TARGET. */
10446static rtx
10447reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10448{
10449 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10450 if (target && GET_MODE (target) != GET_MODE (exp))
10451 target = 0;
1f2ad84c 10452 /* For constant values, reduce using build_int_cst_type. */
481683e1 10453 if (CONST_INT_P (exp))
1f2ad84c
AP
10454 {
10455 HOST_WIDE_INT value = INTVAL (exp);
10456 tree t = build_int_cst_type (type, value);
10457 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10458 }
10459 else if (TYPE_UNSIGNED (type))
bc15d0ef 10460 {
54fb1ae0
AS
10461 rtx mask = immed_double_int_const (double_int_mask (prec),
10462 GET_MODE (exp));
bc15d0ef
JM
10463 return expand_and (GET_MODE (exp), exp, mask, target);
10464 }
10465 else
10466 {
69660a70 10467 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
eb6c3df1
RG
10468 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10469 exp, count, target, 0);
10470 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10471 exp, count, target, 0);
bc15d0ef 10472 }
bbf6f052 10473}
b93a436e 10474\f
1ce7f3c2
RK
10475/* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10476 when applied to the address of EXP produces an address known to be
10477 aligned more than BIGGEST_ALIGNMENT. */
10478
10479static int
22ea9ec0 10480is_aligning_offset (const_tree offset, const_tree exp)
1ce7f3c2 10481{
6fce44af 10482 /* Strip off any conversions. */
1043771b 10483 while (CONVERT_EXPR_P (offset))
1ce7f3c2
RK
10484 offset = TREE_OPERAND (offset, 0);
10485
10486 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10487 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10488 if (TREE_CODE (offset) != BIT_AND_EXPR
10489 || !host_integerp (TREE_OPERAND (offset, 1), 1)
caf93cb0 10490 || compare_tree_int (TREE_OPERAND (offset, 1),
c0cfc691 10491 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
1ce7f3c2
RK
10492 || !exact_log2 (tree_low_cst (TREE_OPERAND (offset, 1), 1) + 1) < 0)
10493 return 0;
10494
10495 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10496 It must be NEGATE_EXPR. Then strip any more conversions. */
10497 offset = TREE_OPERAND (offset, 0);
1043771b 10498 while (CONVERT_EXPR_P (offset))
1ce7f3c2
RK
10499 offset = TREE_OPERAND (offset, 0);
10500
10501 if (TREE_CODE (offset) != NEGATE_EXPR)
10502 return 0;
10503
10504 offset = TREE_OPERAND (offset, 0);
1043771b 10505 while (CONVERT_EXPR_P (offset))
1ce7f3c2
RK
10506 offset = TREE_OPERAND (offset, 0);
10507
6fce44af
RK
10508 /* This must now be the address of EXP. */
10509 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
1ce7f3c2
RK
10510}
10511\f
e0a2f705 10512/* Return the tree node if an ARG corresponds to a string constant or zero
cc2902df 10513 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
fed3cef0
RK
10514 in bytes within the string that ARG is accessing. The type of the
10515 offset will be `sizetype'. */
b93a436e 10516
28f4ec01 10517tree
502b8322 10518string_constant (tree arg, tree *ptr_offset)
b93a436e 10519{
a3de5951 10520 tree array, offset, lower_bound;
b93a436e
JL
10521 STRIP_NOPS (arg);
10522
a45f71f5 10523 if (TREE_CODE (arg) == ADDR_EXPR)
b93a436e 10524 {
a45f71f5
JJ
10525 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10526 {
10527 *ptr_offset = size_zero_node;
10528 return TREE_OPERAND (arg, 0);
10529 }
10530 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10531 {
10532 array = TREE_OPERAND (arg, 0);
10533 offset = size_zero_node;
10534 }
10535 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10536 {
10537 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10538 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10539 if (TREE_CODE (array) != STRING_CST
10540 && TREE_CODE (array) != VAR_DECL)
10541 return 0;
a3de5951 10542
9f5ed61a 10543 /* Check if the array has a nonzero lower bound. */
a3de5951
AM
10544 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10545 if (!integer_zerop (lower_bound))
10546 {
10547 /* If the offset and base aren't both constants, return 0. */
10548 if (TREE_CODE (lower_bound) != INTEGER_CST)
10549 return 0;
10550 if (TREE_CODE (offset) != INTEGER_CST)
10551 return 0;
10552 /* Adjust offset by the lower bound. */
ed1223ba 10553 offset = size_diffop (fold_convert (sizetype, offset),
a3de5951
AM
10554 fold_convert (sizetype, lower_bound));
10555 }
a45f71f5 10556 }
321358d4
RG
10557 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10558 {
10559 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10560 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10561 if (TREE_CODE (array) != ADDR_EXPR)
10562 return 0;
10563 array = TREE_OPERAND (array, 0);
10564 if (TREE_CODE (array) != STRING_CST
10565 && TREE_CODE (array) != VAR_DECL)
10566 return 0;
10567 }
a45f71f5
JJ
10568 else
10569 return 0;
6de9cd9a 10570 }
5be014d5 10571 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
b93a436e
JL
10572 {
10573 tree arg0 = TREE_OPERAND (arg, 0);
10574 tree arg1 = TREE_OPERAND (arg, 1);
10575
10576 STRIP_NOPS (arg0);
10577 STRIP_NOPS (arg1);
10578
10579 if (TREE_CODE (arg0) == ADDR_EXPR
a45f71f5
JJ
10580 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10581 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
bbf6f052 10582 {
a45f71f5
JJ
10583 array = TREE_OPERAND (arg0, 0);
10584 offset = arg1;
bbf6f052 10585 }
b93a436e 10586 else if (TREE_CODE (arg1) == ADDR_EXPR
a45f71f5
JJ
10587 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10588 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
bbf6f052 10589 {
a45f71f5
JJ
10590 array = TREE_OPERAND (arg1, 0);
10591 offset = arg0;
bbf6f052 10592 }
a45f71f5
JJ
10593 else
10594 return 0;
10595 }
10596 else
10597 return 0;
10598
10599 if (TREE_CODE (array) == STRING_CST)
10600 {
3967bc2d 10601 *ptr_offset = fold_convert (sizetype, offset);
a45f71f5
JJ
10602 return array;
10603 }
1d0804d4
JH
10604 else if (TREE_CODE (array) == VAR_DECL
10605 || TREE_CODE (array) == CONST_DECL)
a45f71f5
JJ
10606 {
10607 int length;
10608
10609 /* Variables initialized to string literals can be handled too. */
64e0f5ff 10610 if (!const_value_known_p (array)
1d0804d4 10611 || !DECL_INITIAL (array)
a45f71f5
JJ
10612 || TREE_CODE (DECL_INITIAL (array)) != STRING_CST)
10613 return 0;
10614
a45f71f5
JJ
10615 /* Avoid const char foo[4] = "abcde"; */
10616 if (DECL_SIZE_UNIT (array) == NULL_TREE
10617 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10618 || (length = TREE_STRING_LENGTH (DECL_INITIAL (array))) <= 0
10619 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10620 return 0;
10621
10622 /* If variable is bigger than the string literal, OFFSET must be constant
10623 and inside of the bounds of the string literal. */
3967bc2d 10624 offset = fold_convert (sizetype, offset);
a45f71f5
JJ
10625 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10626 && (! host_integerp (offset, 1)
10627 || compare_tree_int (offset, length) >= 0))
10628 return 0;
10629
10630 *ptr_offset = offset;
10631 return DECL_INITIAL (array);
b93a436e 10632 }
ca695ac9 10633
b93a436e
JL
10634 return 0;
10635}
ca695ac9 10636\f
8e7aa1f9
MM
10637/* Generate code to calculate OPS, and exploded expression
10638 using a store-flag instruction and return an rtx for the result.
10639 OPS reflects a comparison.
ca695ac9 10640
b93a436e 10641 If TARGET is nonzero, store the result there if convenient.
ca695ac9 10642
b93a436e
JL
10643 Return zero if there is no suitable set-flag instruction
10644 available on this machine.
ca695ac9 10645
b93a436e
JL
10646 Once expand_expr has been called on the arguments of the comparison,
10647 we are committed to doing the store flag, since it is not safe to
10648 re-evaluate the expression. We emit the store-flag insn by calling
10649 emit_store_flag, but only expand the arguments if we have a reason
10650 to believe that emit_store_flag will be successful. If we think that
10651 it will, but it isn't, we have to simulate the store-flag with a
10652 set/jump/set sequence. */
ca695ac9 10653
b93a436e 10654static rtx
8e7aa1f9 10655do_store_flag (sepops ops, rtx target, enum machine_mode mode)
b93a436e
JL
10656{
10657 enum rtx_code code;
10658 tree arg0, arg1, type;
10659 tree tem;
10660 enum machine_mode operand_mode;
b93a436e
JL
10661 int unsignedp;
10662 rtx op0, op1;
b93a436e 10663 rtx subtarget = target;
8e7aa1f9 10664 location_t loc = ops->location;
ca695ac9 10665
8e7aa1f9
MM
10666 arg0 = ops->op0;
10667 arg1 = ops->op1;
5129d2ce
AH
10668
10669 /* Don't crash if the comparison was erroneous. */
10670 if (arg0 == error_mark_node || arg1 == error_mark_node)
10671 return const0_rtx;
10672
b93a436e
JL
10673 type = TREE_TYPE (arg0);
10674 operand_mode = TYPE_MODE (type);
8df83eae 10675 unsignedp = TYPE_UNSIGNED (type);
ca695ac9 10676
b93a436e
JL
10677 /* We won't bother with BLKmode store-flag operations because it would mean
10678 passing a lot of information to emit_store_flag. */
10679 if (operand_mode == BLKmode)
10680 return 0;
ca695ac9 10681
b93a436e
JL
10682 /* We won't bother with store-flag operations involving function pointers
10683 when function pointers must be canonicalized before comparisons. */
10684#ifdef HAVE_canonicalize_funcptr_for_compare
10685 if (HAVE_canonicalize_funcptr_for_compare
8e7aa1f9
MM
10686 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10687 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
b93a436e 10688 == FUNCTION_TYPE))
8e7aa1f9
MM
10689 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10690 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
b93a436e
JL
10691 == FUNCTION_TYPE))))
10692 return 0;
ca695ac9
JB
10693#endif
10694
b93a436e
JL
10695 STRIP_NOPS (arg0);
10696 STRIP_NOPS (arg1);
f90e8e2e
AS
10697
10698 /* For vector typed comparisons emit code to generate the desired
10699 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10700 expander for this. */
10701 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10702 {
10703 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10704 tree if_true = constant_boolean_node (true, ops->type);
10705 tree if_false = constant_boolean_node (false, ops->type);
10706 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10707 }
ca695ac9 10708
d246ab4f
AS
10709 /* For vector typed comparisons emit code to generate the desired
10710 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10711 expander for this. */
10712 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10713 {
10714 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10715 tree if_true = constant_boolean_node (true, ops->type);
10716 tree if_false = constant_boolean_node (false, ops->type);
10717 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10718 }
10719
b93a436e
JL
10720 /* Get the rtx comparison code to use. We know that EXP is a comparison
10721 operation of some type. Some comparisons against 1 and -1 can be
10722 converted to comparisons with zero. Do so here so that the tests
10723 below will be aware that we have a comparison with zero. These
10724 tests will not catch constants in the first operand, but constants
10725 are rarely passed as the first operand. */
ca695ac9 10726
8e7aa1f9 10727 switch (ops->code)
b93a436e
JL
10728 {
10729 case EQ_EXPR:
10730 code = EQ;
bbf6f052 10731 break;
b93a436e
JL
10732 case NE_EXPR:
10733 code = NE;
bbf6f052 10734 break;
b93a436e
JL
10735 case LT_EXPR:
10736 if (integer_onep (arg1))
10737 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10738 else
10739 code = unsignedp ? LTU : LT;
ca695ac9 10740 break;
b93a436e
JL
10741 case LE_EXPR:
10742 if (! unsignedp && integer_all_onesp (arg1))
10743 arg1 = integer_zero_node, code = LT;
10744 else
10745 code = unsignedp ? LEU : LE;
ca695ac9 10746 break;
b93a436e
JL
10747 case GT_EXPR:
10748 if (! unsignedp && integer_all_onesp (arg1))
10749 arg1 = integer_zero_node, code = GE;
10750 else
10751 code = unsignedp ? GTU : GT;
10752 break;
10753 case GE_EXPR:
10754 if (integer_onep (arg1))
10755 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10756 else
10757 code = unsignedp ? GEU : GE;
ca695ac9 10758 break;
1eb8759b
RH
10759
10760 case UNORDERED_EXPR:
10761 code = UNORDERED;
10762 break;
10763 case ORDERED_EXPR:
10764 code = ORDERED;
10765 break;
10766 case UNLT_EXPR:
10767 code = UNLT;
10768 break;
10769 case UNLE_EXPR:
10770 code = UNLE;
10771 break;
10772 case UNGT_EXPR:
10773 code = UNGT;
10774 break;
10775 case UNGE_EXPR:
10776 code = UNGE;
10777 break;
10778 case UNEQ_EXPR:
10779 code = UNEQ;
10780 break;
d1a7edaf
PB
10781 case LTGT_EXPR:
10782 code = LTGT;
10783 break;
1eb8759b 10784
ca695ac9 10785 default:
5b0264cb 10786 gcc_unreachable ();
bbf6f052 10787 }
bbf6f052 10788
b93a436e 10789 /* Put a constant second. */
0f996086
CF
10790 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10791 || TREE_CODE (arg0) == FIXED_CST)
b93a436e
JL
10792 {
10793 tem = arg0; arg0 = arg1; arg1 = tem;
10794 code = swap_condition (code);
ca695ac9 10795 }
bbf6f052 10796
b93a436e
JL
10797 /* If this is an equality or inequality test of a single bit, we can
10798 do this by shifting the bit being tested to the low-order bit and
10799 masking the result with the constant 1. If the condition was EQ,
10800 we xor it with 1. This does not require an scc insn and is faster
7960bf22
JL
10801 than an scc insn even if we have it.
10802
10803 The code to make this transformation was moved into fold_single_bit_test,
10804 so we just call into the folder and expand its result. */
d39985fa 10805
b93a436e 10806 if ((code == NE || code == EQ)
3c411f3f 10807 && integer_zerop (arg1)
7bba408b 10808 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
60cd4dae 10809 {
3c411f3f
AP
10810 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
10811 if (srcstmt
10812 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
10813 {
10814 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
10815 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10816 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
10817 gimple_assign_rhs1 (srcstmt),
10818 gimple_assign_rhs2 (srcstmt));
10819 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
10820 if (temp)
10821 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
10822 }
60cd4dae 10823 }
bbf6f052 10824
296b4ed9 10825 if (! get_subtarget (target)
e3be1116 10826 || GET_MODE (subtarget) != operand_mode)
b93a436e
JL
10827 subtarget = 0;
10828
bbbbb16a 10829 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
b93a436e
JL
10830
10831 if (target == 0)
10832 target = gen_reg_rtx (mode);
10833
495499da
PB
10834 /* Try a cstore if possible. */
10835 return emit_store_flag_force (target, code, op0, op1,
7bba408b
JJ
10836 operand_mode, unsignedp,
10837 (TYPE_PRECISION (ops->type) == 1
10838 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
ca695ac9 10839}
b93a436e 10840\f
b93a436e 10841
ad82abb8
ZW
10842/* Stubs in case we haven't got a casesi insn. */
10843#ifndef HAVE_casesi
10844# define HAVE_casesi 0
10845# define gen_casesi(a, b, c, d, e) (0)
10846# define CODE_FOR_casesi CODE_FOR_nothing
10847#endif
10848
ad82abb8
ZW
10849/* Attempt to generate a casesi instruction. Returns 1 if successful,
10850 0 otherwise (i.e. if there is no casesi instruction). */
10851int
502b8322 10852try_casesi (tree index_type, tree index_expr, tree minval, tree range,
55187c8a
RG
10853 rtx table_label ATTRIBUTE_UNUSED, rtx default_label,
10854 rtx fallback_label ATTRIBUTE_UNUSED)
ad82abb8 10855{
a5c7d693 10856 struct expand_operand ops[5];
ad82abb8 10857 enum machine_mode index_mode = SImode;
ad82abb8 10858 rtx op1, op2, index;
ad82abb8
ZW
10859
10860 if (! HAVE_casesi)
10861 return 0;
10862
10863 /* Convert the index to SImode. */
10864 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
10865 {
10866 enum machine_mode omode = TYPE_MODE (index_type);
84217346 10867 rtx rangertx = expand_normal (range);
ad82abb8
ZW
10868
10869 /* We must handle the endpoints in the original mode. */
3244e67d
RS
10870 index_expr = build2 (MINUS_EXPR, index_type,
10871 index_expr, minval);
ad82abb8 10872 minval = integer_zero_node;
84217346 10873 index = expand_normal (index_expr);
b7814a18
RG
10874 if (default_label)
10875 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
10876 omode, 1, default_label);
ad82abb8
ZW
10877 /* Now we can safely truncate. */
10878 index = convert_to_mode (index_mode, index, 0);
10879 }
10880 else
10881 {
10882 if (TYPE_MODE (index_type) != index_mode)
10883 {
0f250839 10884 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
3967bc2d 10885 index_expr = fold_convert (index_type, index_expr);
ad82abb8
ZW
10886 }
10887
84217346 10888 index = expand_normal (index_expr);
ad82abb8 10889 }
ad76cef8 10890
ad82abb8
ZW
10891 do_pending_stack_adjust ();
10892
84217346 10893 op1 = expand_normal (minval);
84217346 10894 op2 = expand_normal (range);
ad82abb8 10895
a5c7d693
RS
10896 create_input_operand (&ops[0], index, index_mode);
10897 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
10898 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
10899 create_fixed_operand (&ops[3], table_label);
10900 create_fixed_operand (&ops[4], (default_label
10901 ? default_label
10902 : fallback_label));
10903 expand_jump_insn (CODE_FOR_casesi, 5, ops);
ad82abb8
ZW
10904 return 1;
10905}
10906
10907/* Attempt to generate a tablejump instruction; same concept. */
10908#ifndef HAVE_tablejump
10909#define HAVE_tablejump 0
10910#define gen_tablejump(x, y) (0)
10911#endif
10912
10913/* Subroutine of the next function.
10914
10915 INDEX is the value being switched on, with the lowest value
b93a436e
JL
10916 in the table already subtracted.
10917 MODE is its expected mode (needed if INDEX is constant).
10918 RANGE is the length of the jump table.
10919 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
88d3b7f0 10920
b93a436e
JL
10921 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
10922 index value is out of range. */
0f41302f 10923
ad82abb8 10924static void
502b8322
AJ
10925do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
10926 rtx default_label)
ca695ac9 10927{
b3694847 10928 rtx temp, vector;
88d3b7f0 10929
cb91fab0
JH
10930 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
10931 cfun->cfg->max_jumptable_ents = INTVAL (range);
1877be45 10932
b93a436e
JL
10933 /* Do an unsigned comparison (in the proper mode) between the index
10934 expression and the value which represents the length of the range.
10935 Since we just finished subtracting the lower bound of the range
10936 from the index expression, this comparison allows us to simultaneously
10937 check that the original index expression value is both greater than
10938 or equal to the minimum value of the range and less than or equal to
10939 the maximum value of the range. */
709f5be1 10940
b7814a18
RG
10941 if (default_label)
10942 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
10943 default_label);
bbf6f052 10944
b93a436e
JL
10945 /* If index is in range, it must fit in Pmode.
10946 Convert to Pmode so we can index with it. */
10947 if (mode != Pmode)
10948 index = convert_to_mode (Pmode, index, 1);
bbf6f052 10949
ba228239 10950 /* Don't let a MEM slip through, because then INDEX that comes
b93a436e
JL
10951 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
10952 and break_out_memory_refs will go to work on it and mess it up. */
10953#ifdef PIC_CASE_VECTOR_ADDRESS
f8cfc6aa 10954 if (flag_pic && !REG_P (index))
b93a436e
JL
10955 index = copy_to_mode_reg (Pmode, index);
10956#endif
ca695ac9 10957
b93a436e
JL
10958 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
10959 GET_MODE_SIZE, because this indicates how large insns are. The other
10960 uses should all be Pmode, because they are addresses. This code
10961 could fail if addresses and insns are not the same size. */
10962 index = gen_rtx_PLUS (Pmode,
10963 gen_rtx_MULT (Pmode, index,
10964 GEN_INT (GET_MODE_SIZE (CASE_VECTOR_MODE))),
10965 gen_rtx_LABEL_REF (Pmode, table_label));
10966#ifdef PIC_CASE_VECTOR_ADDRESS
10967 if (flag_pic)
10968 index = PIC_CASE_VECTOR_ADDRESS (index);
10969 else
bbf6f052 10970#endif
3de5e93a 10971 index = memory_address (CASE_VECTOR_MODE, index);
b93a436e 10972 temp = gen_reg_rtx (CASE_VECTOR_MODE);
542a8afa 10973 vector = gen_const_mem (CASE_VECTOR_MODE, index);
b93a436e
JL
10974 convert_move (temp, vector, 0);
10975
10976 emit_jump_insn (gen_tablejump (temp, table_label));
10977
10978 /* If we are generating PIC code or if the table is PC-relative, the
10979 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
10980 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
10981 emit_barrier ();
bbf6f052 10982}
b93a436e 10983
ad82abb8 10984int
502b8322
AJ
10985try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
10986 rtx table_label, rtx default_label)
ad82abb8
ZW
10987{
10988 rtx index;
10989
10990 if (! HAVE_tablejump)
10991 return 0;
10992
4845b383 10993 index_expr = fold_build2 (MINUS_EXPR, index_type,
3967bc2d
RS
10994 fold_convert (index_type, index_expr),
10995 fold_convert (index_type, minval));
84217346 10996 index = expand_normal (index_expr);
ad82abb8
ZW
10997 do_pending_stack_adjust ();
10998
10999 do_tablejump (index, TYPE_MODE (index_type),
11000 convert_modes (TYPE_MODE (index_type),
11001 TYPE_MODE (TREE_TYPE (range)),
84217346 11002 expand_normal (range),
8df83eae 11003 TYPE_UNSIGNED (TREE_TYPE (range))),
ad82abb8
ZW
11004 table_label, default_label);
11005 return 1;
11006}
e2500fed 11007
d744e06e
AH
11008/* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11009static rtx
502b8322 11010const_vector_from_tree (tree exp)
d744e06e
AH
11011{
11012 rtvec v;
d2a12ae7
RG
11013 unsigned i;
11014 int units;
11015 tree elt;
d744e06e
AH
11016 enum machine_mode inner, mode;
11017
11018 mode = TYPE_MODE (TREE_TYPE (exp));
11019
6de9cd9a 11020 if (initializer_zerop (exp))
d744e06e
AH
11021 return CONST0_RTX (mode);
11022
11023 units = GET_MODE_NUNITS (mode);
11024 inner = GET_MODE_INNER (mode);
11025
11026 v = rtvec_alloc (units);
11027
d2a12ae7 11028 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
d744e06e 11029 {
d2a12ae7 11030 elt = VECTOR_CST_ELT (exp, i);
d744e06e
AH
11031
11032 if (TREE_CODE (elt) == REAL_CST)
11033 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11034 inner);
0f996086
CF
11035 else if (TREE_CODE (elt) == FIXED_CST)
11036 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11037 inner);
d744e06e 11038 else
54fb1ae0
AS
11039 RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
11040 inner);
d744e06e
AH
11041 }
11042
a73b091d 11043 return gen_rtx_CONST_VECTOR (mode, v);
d744e06e 11044}
f9417da1 11045
bde8a146 11046/* Build a decl for a personality function given a language prefix. */
f9417da1
RG
11047
11048tree
bde8a146 11049build_personality_function (const char *lang)
f9417da1 11050{
bde8a146 11051 const char *unwind_and_version;
f9417da1 11052 tree decl, type;
bde8a146
RH
11053 char *name;
11054
677f3fa8 11055 switch (targetm_common.except_unwind_info (&global_options))
bde8a146
RH
11056 {
11057 case UI_NONE:
11058 return NULL;
11059 case UI_SJLJ:
11060 unwind_and_version = "_sj0";
11061 break;
11062 case UI_DWARF2:
11063 case UI_TARGET:
11064 unwind_and_version = "_v0";
11065 break;
11066 default:
11067 gcc_unreachable ();
11068 }
11069
11070 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
f9417da1
RG
11071
11072 type = build_function_type_list (integer_type_node, integer_type_node,
11073 long_long_unsigned_type_node,
11074 ptr_type_node, ptr_type_node, NULL_TREE);
11075 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11076 get_identifier (name), type);
11077 DECL_ARTIFICIAL (decl) = 1;
11078 DECL_EXTERNAL (decl) = 1;
11079 TREE_PUBLIC (decl) = 1;
11080
11081 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11082 are the flags assigned by targetm.encode_section_info. */
11083 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11084
11085 return decl;
11086}
11087
11088/* Extracts the personality function of DECL and returns the corresponding
11089 libfunc. */
11090
11091rtx
11092get_personality_function (tree decl)
11093{
11094 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11095 enum eh_personality_kind pk;
11096
11097 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11098 if (pk == eh_personality_none)
11099 return NULL;
11100
11101 if (!personality
11102 && pk == eh_personality_any)
11103 personality = lang_hooks.eh_personality ();
11104
11105 if (pk == eh_personality_lang)
11106 gcc_assert (personality != NULL_TREE);
11107
11108 return XEXP (DECL_RTL (personality), 0);
11109}
11110
e2500fed 11111#include "gt-expr.h"
This page took 7.51199 seconds and 5 git commands to generate.