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