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