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