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