c++: Implement __is_member_function_pointer built-in trait
[official-gcc.git] / gcc / expr.cc
blob6da51f2aca296450848000ed7dc957fd28ebf55f
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
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 "optabs.h"
33 #include "expmed.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-dfa.h"
58 #include "tree-ssa-live.h"
59 #include "tree-outof-ssa.h"
60 #include "tree-ssa-address.h"
61 #include "builtins.h"
62 #include "ccmp.h"
63 #include "gimple-iterator.h"
64 #include "gimple-fold.h"
65 #include "rtx-vector-builder.h"
66 #include "tree-pretty-print.h"
67 #include "flags.h"
70 /* If this is nonzero, we do not bother generating VOLATILE
71 around volatile memory references, and we are willing to
72 output indirect addresses. If cse is to follow, we reject
73 indirect addresses so a useful potential cse is generated;
74 if it is used only once, instruction combination will produce
75 the same indirect address eventually. */
76 int cse_not_expected;
78 static bool block_move_libcall_safe_for_call_parm (void);
79 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
80 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
81 unsigned HOST_WIDE_INT,
82 unsigned HOST_WIDE_INT, bool);
83 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned, int);
84 static void emit_block_move_via_sized_loop (rtx, rtx, rtx, unsigned, unsigned);
85 static void emit_block_move_via_oriented_loop (rtx, rtx, rtx, unsigned, unsigned);
86 static rtx emit_block_cmp_via_loop (rtx, rtx, rtx, tree, rtx, bool,
87 unsigned, unsigned);
88 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
89 static rtx_insn *compress_float_constant (rtx, rtx);
90 static rtx get_subtarget (rtx);
91 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
92 machine_mode, tree, alias_set_type, bool, bool);
94 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
96 static bool is_aligning_offset (const_tree, const_tree);
97 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
98 static rtx do_store_flag (sepops, rtx, machine_mode);
99 #ifdef PUSH_ROUNDING
100 static void emit_single_push_insn (machine_mode, rtx, tree);
101 #endif
102 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
103 profile_probability);
104 static rtx const_vector_from_tree (tree);
105 static tree tree_expr_size (const_tree);
106 static void convert_mode_scalar (rtx, rtx, int);
109 /* This is run to set up which modes can be used
110 directly in memory and to initialize the block move optab. It is run
111 at the beginning of compilation and when the target is reinitialized. */
113 void
114 init_expr_target (void)
116 rtx pat;
117 int num_clobbers;
118 rtx mem, mem1;
119 rtx reg;
121 /* Try indexing by frame ptr and try by stack ptr.
122 It is known that on the Convex the stack ptr isn't a valid index.
123 With luck, one or the other is valid on any machine. */
124 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
125 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
127 /* A scratch register we can modify in-place below to avoid
128 useless RTL allocations. */
129 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
131 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
132 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
133 PATTERN (insn) = pat;
135 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
136 mode = (machine_mode) ((int) mode + 1))
138 int regno;
140 direct_load[(int) mode] = direct_store[(int) mode] = 0;
141 PUT_MODE (mem, mode);
142 PUT_MODE (mem1, mode);
144 /* See if there is some register that can be used in this mode and
145 directly loaded or stored from memory. */
147 if (mode != VOIDmode && mode != BLKmode)
148 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
149 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
150 regno++)
152 if (!targetm.hard_regno_mode_ok (regno, mode))
153 continue;
155 set_mode_and_regno (reg, mode, regno);
157 SET_SRC (pat) = mem;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
162 SET_SRC (pat) = mem1;
163 SET_DEST (pat) = reg;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_load[(int) mode] = 1;
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
172 SET_SRC (pat) = reg;
173 SET_DEST (pat) = mem1;
174 if (recog (pat, insn, &num_clobbers) >= 0)
175 direct_store[(int) mode] = 1;
179 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
181 opt_scalar_float_mode mode_iter;
182 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
184 scalar_float_mode mode = mode_iter.require ();
185 scalar_float_mode srcmode;
186 FOR_EACH_MODE_UNTIL (srcmode, mode)
188 enum insn_code ic;
190 ic = can_extend_p (mode, srcmode, 0);
191 if (ic == CODE_FOR_nothing)
192 continue;
194 PUT_MODE (mem, srcmode);
196 if (insn_operand_matches (ic, 1, mem))
197 float_extend_from_mem[mode][srcmode] = true;
202 /* This is run at the start of compiling a function. */
204 void
205 init_expr (void)
207 memset (&crtl->expr, 0, sizeof (crtl->expr));
210 /* Copy data from FROM to TO, where the machine modes are not the same.
211 Both modes may be integer, or both may be floating, or both may be
212 fixed-point.
213 UNSIGNEDP should be nonzero if FROM is an unsigned type.
214 This causes zero-extension instead of sign-extension. */
216 void
217 convert_move (rtx to, rtx from, int unsignedp)
219 machine_mode to_mode = GET_MODE (to);
220 machine_mode from_mode = GET_MODE (from);
222 gcc_assert (to_mode != BLKmode);
223 gcc_assert (from_mode != BLKmode);
225 /* If the source and destination are already the same, then there's
226 nothing to do. */
227 if (to == from)
228 return;
230 /* If FROM is a SUBREG that indicates that we have already done at least
231 the required extension, strip it. We don't handle such SUBREGs as
232 TO here. */
234 scalar_int_mode to_int_mode;
235 if (GET_CODE (from) == SUBREG
236 && SUBREG_PROMOTED_VAR_P (from)
237 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
238 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
239 >= GET_MODE_PRECISION (to_int_mode))
240 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
242 scalar_int_mode int_orig_mode;
243 scalar_int_mode int_inner_mode;
244 machine_mode orig_mode = GET_MODE (from);
246 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
247 from_mode = to_int_mode;
249 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
250 the original mode, but narrower than the inner mode. */
251 if (GET_CODE (from) == SUBREG
252 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
253 && GET_MODE_PRECISION (to_int_mode)
254 > GET_MODE_PRECISION (int_orig_mode)
255 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)),
256 &int_inner_mode)
257 && GET_MODE_PRECISION (int_inner_mode)
258 > GET_MODE_PRECISION (to_int_mode))
260 SUBREG_PROMOTED_VAR_P (from) = 1;
261 SUBREG_PROMOTED_SET (from, unsignedp);
265 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
267 if (to_mode == from_mode
268 || (from_mode == VOIDmode && CONSTANT_P (from)))
270 emit_move_insn (to, from);
271 return;
274 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
276 if (GET_MODE_UNIT_PRECISION (to_mode)
277 > GET_MODE_UNIT_PRECISION (from_mode))
279 optab op = unsignedp ? zext_optab : sext_optab;
280 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
281 if (icode != CODE_FOR_nothing)
283 emit_unop_insn (icode, to, from,
284 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
285 return;
289 if (GET_MODE_UNIT_PRECISION (to_mode)
290 < GET_MODE_UNIT_PRECISION (from_mode))
292 insn_code icode = convert_optab_handler (trunc_optab,
293 to_mode, from_mode);
294 if (icode != CODE_FOR_nothing)
296 emit_unop_insn (icode, to, from, TRUNCATE);
297 return;
301 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
302 GET_MODE_BITSIZE (to_mode)));
304 if (VECTOR_MODE_P (to_mode))
305 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
306 else
307 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
309 emit_move_insn (to, from);
310 return;
313 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
315 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
316 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
317 return;
320 convert_mode_scalar (to, from, unsignedp);
323 /* Like convert_move, but deals only with scalar modes. */
325 static void
326 convert_mode_scalar (rtx to, rtx from, int unsignedp)
328 /* Both modes should be scalar types. */
329 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
330 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
331 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
332 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
333 enum insn_code code;
334 rtx libcall;
336 gcc_assert (to_real == from_real);
338 /* rtx code for making an equivalent value. */
339 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
340 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
342 if (to_real)
344 rtx value;
345 rtx_insn *insns;
346 convert_optab tab;
348 gcc_assert ((GET_MODE_PRECISION (from_mode)
349 != GET_MODE_PRECISION (to_mode))
350 || (DECIMAL_FLOAT_MODE_P (from_mode)
351 != DECIMAL_FLOAT_MODE_P (to_mode))
352 || (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
353 && REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
354 || (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
355 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format));
357 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
358 /* Conversion between decimal float and binary float, same size. */
359 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
360 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
361 tab = sext_optab;
362 else
363 tab = trunc_optab;
365 /* Try converting directly if the insn is supported. */
367 code = convert_optab_handler (tab, to_mode, from_mode);
368 if (code != CODE_FOR_nothing)
370 emit_unop_insn (code, to, from,
371 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
372 return;
375 #ifdef HAVE_SFmode
376 if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
377 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
379 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode))
381 /* To cut down on libgcc size, implement
382 BFmode -> {DF,XF,TF}mode conversions by
383 BFmode -> SFmode -> {DF,XF,TF}mode conversions. */
384 rtx temp = gen_reg_rtx (SFmode);
385 convert_mode_scalar (temp, from, unsignedp);
386 convert_mode_scalar (to, temp, unsignedp);
387 return;
389 if (REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
391 /* Similarly, implement BFmode -> HFmode as
392 BFmode -> SFmode -> HFmode conversion where SFmode
393 has superset of BFmode values. We don't need
394 to handle sNaNs by raising exception and turning
395 into into qNaN though, as that can be done in the
396 SFmode -> HFmode conversion too. */
397 rtx temp = gen_reg_rtx (SFmode);
398 int save_flag_finite_math_only = flag_finite_math_only;
399 flag_finite_math_only = true;
400 convert_mode_scalar (temp, from, unsignedp);
401 flag_finite_math_only = save_flag_finite_math_only;
402 convert_mode_scalar (to, temp, unsignedp);
403 return;
405 if (to_mode == SFmode
406 && !HONOR_NANS (from_mode)
407 && !HONOR_NANS (to_mode)
408 && optimize_insn_for_speed_p ())
410 /* If we don't expect sNaNs, for BFmode -> SFmode we can just
411 shift the bits up. */
412 machine_mode fromi_mode, toi_mode;
413 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
414 0).exists (&fromi_mode)
415 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
416 0).exists (&toi_mode))
418 start_sequence ();
419 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
420 rtx tof = NULL_RTX;
421 if (fromi)
423 rtx toi;
424 if (GET_MODE (fromi) == VOIDmode)
425 toi = simplify_unary_operation (ZERO_EXTEND, toi_mode,
426 fromi, fromi_mode);
427 else
429 toi = gen_reg_rtx (toi_mode);
430 convert_mode_scalar (toi, fromi, 1);
433 = maybe_expand_shift (LSHIFT_EXPR, toi_mode, toi,
434 GET_MODE_PRECISION (to_mode)
435 - GET_MODE_PRECISION (from_mode),
436 NULL_RTX, 1);
437 if (toi)
439 tof = lowpart_subreg (to_mode, toi, toi_mode);
440 if (tof)
441 emit_move_insn (to, tof);
444 insns = get_insns ();
445 end_sequence ();
446 if (tof)
448 emit_insn (insns);
449 return;
454 if (REAL_MODE_FORMAT (from_mode) == &ieee_single_format
455 && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
456 && !HONOR_NANS (from_mode)
457 && !HONOR_NANS (to_mode)
458 && !flag_rounding_math
459 && optimize_insn_for_speed_p ())
461 /* If we don't expect qNaNs nor sNaNs and can assume rounding
462 to nearest, we can expand the conversion inline as
463 (fromi + 0x7fff + ((fromi >> 16) & 1)) >> 16. */
464 machine_mode fromi_mode, toi_mode;
465 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
466 0).exists (&fromi_mode)
467 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
468 0).exists (&toi_mode))
470 start_sequence ();
471 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
472 rtx tof = NULL_RTX;
475 if (!fromi)
476 break;
477 int shift = (GET_MODE_PRECISION (from_mode)
478 - GET_MODE_PRECISION (to_mode));
479 rtx temp1
480 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, fromi,
481 shift, NULL_RTX, 1);
482 if (!temp1)
483 break;
484 rtx temp2
485 = expand_binop (fromi_mode, and_optab, temp1, const1_rtx,
486 NULL_RTX, 1, OPTAB_DIRECT);
487 if (!temp2)
488 break;
489 rtx temp3
490 = expand_binop (fromi_mode, add_optab, fromi,
491 gen_int_mode ((HOST_WIDE_INT_1U
492 << (shift - 1)) - 1,
493 fromi_mode), NULL_RTX,
494 1, OPTAB_DIRECT);
495 if (!temp3)
496 break;
497 rtx temp4
498 = expand_binop (fromi_mode, add_optab, temp3, temp2,
499 NULL_RTX, 1, OPTAB_DIRECT);
500 if (!temp4)
501 break;
502 rtx temp5 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode,
503 temp4, shift, NULL_RTX, 1);
504 if (!temp5)
505 break;
506 rtx temp6 = lowpart_subreg (toi_mode, temp5, fromi_mode);
507 if (!temp6)
508 break;
509 tof = lowpart_subreg (to_mode, force_reg (toi_mode, temp6),
510 toi_mode);
511 if (tof)
512 emit_move_insn (to, tof);
514 while (0);
515 insns = get_insns ();
516 end_sequence ();
517 if (tof)
519 emit_insn (insns);
520 return;
524 #endif
526 /* Otherwise use a libcall. */
527 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
529 /* Is this conversion implemented yet? */
530 gcc_assert (libcall);
532 start_sequence ();
533 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
534 from, from_mode);
535 insns = get_insns ();
536 end_sequence ();
537 emit_libcall_block (insns, to, value,
538 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
539 from)
540 : gen_rtx_FLOAT_EXTEND (to_mode, from));
541 return;
544 /* Handle pointer conversion. */ /* SPEE 900220. */
545 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
547 convert_optab ctab;
549 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
550 ctab = trunc_optab;
551 else if (unsignedp)
552 ctab = zext_optab;
553 else
554 ctab = sext_optab;
556 if (convert_optab_handler (ctab, to_mode, from_mode)
557 != CODE_FOR_nothing)
559 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
560 to, from, UNKNOWN);
561 return;
565 /* Targets are expected to provide conversion insns between PxImode and
566 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
567 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
569 scalar_int_mode full_mode
570 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
572 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
573 != CODE_FOR_nothing);
575 if (full_mode != from_mode)
576 from = convert_to_mode (full_mode, from, unsignedp);
577 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
578 to, from, UNKNOWN);
579 return;
581 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
583 rtx new_from;
584 scalar_int_mode full_mode
585 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
586 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
587 enum insn_code icode;
589 icode = convert_optab_handler (ctab, full_mode, from_mode);
590 gcc_assert (icode != CODE_FOR_nothing);
592 if (to_mode == full_mode)
594 emit_unop_insn (icode, to, from, UNKNOWN);
595 return;
598 new_from = gen_reg_rtx (full_mode);
599 emit_unop_insn (icode, new_from, from, UNKNOWN);
601 /* else proceed to integer conversions below. */
602 from_mode = full_mode;
603 from = new_from;
606 /* Make sure both are fixed-point modes or both are not. */
607 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
608 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
609 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
611 /* If we widen from_mode to to_mode and they are in the same class,
612 we won't saturate the result.
613 Otherwise, always saturate the result to play safe. */
614 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
615 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
616 expand_fixed_convert (to, from, 0, 0);
617 else
618 expand_fixed_convert (to, from, 0, 1);
619 return;
622 /* Now both modes are integers. */
624 /* Handle expanding beyond a word. */
625 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
626 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
628 rtx_insn *insns;
629 rtx lowpart;
630 rtx fill_value;
631 rtx lowfrom;
632 int i;
633 scalar_mode lowpart_mode;
634 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
636 /* Try converting directly if the insn is supported. */
637 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
638 != CODE_FOR_nothing)
640 /* If FROM is a SUBREG, put it into a register. Do this
641 so that we always generate the same set of insns for
642 better cse'ing; if an intermediate assignment occurred,
643 we won't be doing the operation directly on the SUBREG. */
644 if (optimize > 0 && GET_CODE (from) == SUBREG)
645 from = force_reg (from_mode, from);
646 emit_unop_insn (code, to, from, equiv_code);
647 return;
649 /* Next, try converting via full word. */
650 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
651 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
652 != CODE_FOR_nothing))
654 rtx word_to = gen_reg_rtx (word_mode);
655 if (REG_P (to))
657 if (reg_overlap_mentioned_p (to, from))
658 from = force_reg (from_mode, from);
659 emit_clobber (to);
661 convert_move (word_to, from, unsignedp);
662 emit_unop_insn (code, to, word_to, equiv_code);
663 return;
666 /* No special multiword conversion insn; do it by hand. */
667 start_sequence ();
669 /* Since we will turn this into a no conflict block, we must ensure
670 the source does not overlap the target so force it into an isolated
671 register when maybe so. Likewise for any MEM input, since the
672 conversion sequence might require several references to it and we
673 must ensure we're getting the same value every time. */
675 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
676 from = force_reg (from_mode, from);
678 /* Get a copy of FROM widened to a word, if necessary. */
679 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
680 lowpart_mode = word_mode;
681 else
682 lowpart_mode = from_mode;
684 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
686 lowpart = gen_lowpart (lowpart_mode, to);
687 emit_move_insn (lowpart, lowfrom);
689 /* Compute the value to put in each remaining word. */
690 if (unsignedp)
691 fill_value = const0_rtx;
692 else
693 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
694 LT, lowfrom, const0_rtx,
695 lowpart_mode, 0, -1);
697 /* Fill the remaining words. */
698 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
700 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
701 rtx subword = operand_subword (to, index, 1, to_mode);
703 gcc_assert (subword);
705 if (fill_value != subword)
706 emit_move_insn (subword, fill_value);
709 insns = get_insns ();
710 end_sequence ();
712 emit_insn (insns);
713 return;
716 /* Truncating multi-word to a word or less. */
717 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
718 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
720 if (!((MEM_P (from)
721 && ! MEM_VOLATILE_P (from)
722 && direct_load[(int) to_mode]
723 && ! mode_dependent_address_p (XEXP (from, 0),
724 MEM_ADDR_SPACE (from)))
725 || REG_P (from)
726 || GET_CODE (from) == SUBREG))
727 from = force_reg (from_mode, from);
728 convert_move (to, gen_lowpart (word_mode, from), 0);
729 return;
732 /* Now follow all the conversions between integers
733 no more than a word long. */
735 /* For truncation, usually we can just refer to FROM in a narrower mode. */
736 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
737 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
739 if (!((MEM_P (from)
740 && ! MEM_VOLATILE_P (from)
741 && direct_load[(int) to_mode]
742 && ! mode_dependent_address_p (XEXP (from, 0),
743 MEM_ADDR_SPACE (from)))
744 || REG_P (from)
745 || GET_CODE (from) == SUBREG))
746 from = force_reg (from_mode, from);
747 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
748 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
749 from = copy_to_reg (from);
750 emit_move_insn (to, gen_lowpart (to_mode, from));
751 return;
754 /* Handle extension. */
755 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
757 /* Convert directly if that works. */
758 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
759 != CODE_FOR_nothing)
761 emit_unop_insn (code, to, from, equiv_code);
762 return;
764 else
766 rtx tmp;
767 int shift_amount;
769 /* Search for a mode to convert via. */
770 opt_scalar_mode intermediate_iter;
771 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
773 scalar_mode intermediate = intermediate_iter.require ();
774 if (((can_extend_p (to_mode, intermediate, unsignedp)
775 != CODE_FOR_nothing)
776 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
777 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
778 intermediate)))
779 && (can_extend_p (intermediate, from_mode, unsignedp)
780 != CODE_FOR_nothing))
782 convert_move (to, convert_to_mode (intermediate, from,
783 unsignedp), unsignedp);
784 return;
788 /* No suitable intermediate mode.
789 Generate what we need with shifts. */
790 shift_amount = (GET_MODE_PRECISION (to_mode)
791 - GET_MODE_PRECISION (from_mode));
792 from = gen_lowpart (to_mode, force_reg (from_mode, from));
793 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
794 to, unsignedp);
795 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
796 to, unsignedp);
797 if (tmp != to)
798 emit_move_insn (to, tmp);
799 return;
803 /* Support special truncate insns for certain modes. */
804 if (convert_optab_handler (trunc_optab, to_mode,
805 from_mode) != CODE_FOR_nothing)
807 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
808 to, from, UNKNOWN);
809 return;
812 /* Handle truncation of volatile memrefs, and so on;
813 the things that couldn't be truncated directly,
814 and for which there was no special instruction.
816 ??? Code above formerly short-circuited this, for most integer
817 mode pairs, with a force_reg in from_mode followed by a recursive
818 call to this routine. Appears always to have been wrong. */
819 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
821 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
822 emit_move_insn (to, temp);
823 return;
826 /* Mode combination is not recognized. */
827 gcc_unreachable ();
830 /* Return an rtx for a value that would result
831 from converting X to mode MODE.
832 Both X and MODE may be floating, or both integer.
833 UNSIGNEDP is nonzero if X is an unsigned value.
834 This can be done by referring to a part of X in place
835 or by copying to a new temporary with conversion. */
838 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
840 return convert_modes (mode, VOIDmode, x, unsignedp);
843 /* Return an rtx for a value that would result
844 from converting X from mode OLDMODE to mode MODE.
845 Both modes may be floating, or both integer.
846 UNSIGNEDP is nonzero if X is an unsigned value.
848 This can be done by referring to a part of X in place
849 or by copying to a new temporary with conversion.
851 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
854 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
856 rtx temp;
857 scalar_int_mode int_mode;
859 /* If FROM is a SUBREG that indicates that we have already done at least
860 the required extension, strip it. */
862 if (GET_CODE (x) == SUBREG
863 && SUBREG_PROMOTED_VAR_P (x)
864 && is_a <scalar_int_mode> (mode, &int_mode)
865 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
866 >= GET_MODE_PRECISION (int_mode))
867 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
869 scalar_int_mode int_orig_mode;
870 scalar_int_mode int_inner_mode;
871 machine_mode orig_mode = GET_MODE (x);
872 x = gen_lowpart (int_mode, SUBREG_REG (x));
874 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
875 the original mode, but narrower than the inner mode. */
876 if (GET_CODE (x) == SUBREG
877 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
878 && GET_MODE_PRECISION (int_mode)
879 > GET_MODE_PRECISION (int_orig_mode)
880 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
881 &int_inner_mode)
882 && GET_MODE_PRECISION (int_inner_mode)
883 > GET_MODE_PRECISION (int_mode))
885 SUBREG_PROMOTED_VAR_P (x) = 1;
886 SUBREG_PROMOTED_SET (x, unsignedp);
890 if (GET_MODE (x) != VOIDmode)
891 oldmode = GET_MODE (x);
893 if (mode == oldmode)
894 return x;
896 if (CONST_SCALAR_INT_P (x)
897 && is_a <scalar_int_mode> (mode, &int_mode))
899 /* If the caller did not tell us the old mode, then there is not
900 much to do with respect to canonicalization. We have to
901 assume that all the bits are significant. */
902 if (!is_a <scalar_int_mode> (oldmode))
903 oldmode = MAX_MODE_INT;
904 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
905 GET_MODE_PRECISION (int_mode),
906 unsignedp ? UNSIGNED : SIGNED);
907 return immed_wide_int_const (w, int_mode);
910 /* We can do this with a gen_lowpart if both desired and current modes
911 are integer, and this is either a constant integer, a register, or a
912 non-volatile MEM. */
913 scalar_int_mode int_oldmode;
914 if (is_int_mode (mode, &int_mode)
915 && is_int_mode (oldmode, &int_oldmode)
916 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
917 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
918 || CONST_POLY_INT_P (x)
919 || (REG_P (x)
920 && (!HARD_REGISTER_P (x)
921 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
922 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
923 return gen_lowpart (int_mode, x);
925 /* Converting from integer constant into mode is always equivalent to an
926 subreg operation. */
927 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
929 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
930 GET_MODE_BITSIZE (oldmode)));
931 return simplify_gen_subreg (mode, x, oldmode, 0);
934 temp = gen_reg_rtx (mode);
935 convert_move (temp, x, unsignedp);
936 return temp;
939 /* Variant of convert_modes for ABI parameter passing/return.
940 Return an rtx for a value that would result from converting X from
941 a floating point mode FMODE to wider integer mode MODE. */
944 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x)
946 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode));
947 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require ();
948 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
949 return convert_modes (mode, tmp_mode, tmp, 1);
952 /* Variant of convert_modes for ABI parameter passing/return.
953 Return an rtx for a value that would result from converting X from
954 an integer mode IMODE to a narrower floating point mode MODE. */
957 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x)
959 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode));
960 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require ();
961 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
962 return gen_lowpart_SUBREG (mode, tmp);
965 /* Return the largest alignment we can use for doing a move (or store)
966 of MAX_PIECES. ALIGN is the largest alignment we could use. */
968 static unsigned int
969 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
971 scalar_int_mode tmode
972 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
974 if (align >= GET_MODE_ALIGNMENT (tmode))
975 align = GET_MODE_ALIGNMENT (tmode);
976 else
978 scalar_int_mode xmode = NARROWEST_INT_MODE;
979 opt_scalar_int_mode mode_iter;
980 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
982 tmode = mode_iter.require ();
983 if (GET_MODE_SIZE (tmode) > max_pieces
984 || targetm.slow_unaligned_access (tmode, align))
985 break;
986 xmode = tmode;
989 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
992 return align;
995 /* Return true if we know how to implement OP using vectors of bytes. */
996 static bool
997 can_use_qi_vectors (by_pieces_operation op)
999 return (op == COMPARE_BY_PIECES
1000 || op == SET_BY_PIECES
1001 || op == CLEAR_BY_PIECES);
1004 /* Return true if optabs exists for the mode and certain by pieces
1005 operations. */
1006 static bool
1007 by_pieces_mode_supported_p (fixed_size_mode mode, by_pieces_operation op)
1009 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
1010 return false;
1012 if ((op == SET_BY_PIECES || op == CLEAR_BY_PIECES)
1013 && VECTOR_MODE_P (mode)
1014 && optab_handler (vec_duplicate_optab, mode) == CODE_FOR_nothing)
1015 return false;
1017 if (op == COMPARE_BY_PIECES
1018 && !can_compare_p (EQ, mode, ccp_jump))
1019 return false;
1021 return true;
1024 /* Return the widest mode that can be used to perform part of an
1025 operation OP on SIZE bytes. Try to use QI vector modes where
1026 possible. */
1027 static fixed_size_mode
1028 widest_fixed_size_mode_for_size (unsigned int size, by_pieces_operation op)
1030 fixed_size_mode result = NARROWEST_INT_MODE;
1032 gcc_checking_assert (size > 1);
1034 /* Use QI vector only if size is wider than a WORD. */
1035 if (can_use_qi_vectors (op) && size > UNITS_PER_WORD)
1037 machine_mode mode;
1038 fixed_size_mode candidate;
1039 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1040 if (is_a<fixed_size_mode> (mode, &candidate)
1041 && GET_MODE_INNER (candidate) == QImode)
1043 if (GET_MODE_SIZE (candidate) >= size)
1044 break;
1045 if (by_pieces_mode_supported_p (candidate, op))
1046 result = candidate;
1049 if (result != NARROWEST_INT_MODE)
1050 return result;
1053 opt_scalar_int_mode tmode;
1054 scalar_int_mode mode;
1055 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
1057 mode = tmode.require ();
1058 if (GET_MODE_SIZE (mode) < size
1059 && by_pieces_mode_supported_p (mode, op))
1060 result = mode;
1063 return result;
1066 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
1067 and should be performed piecewise. */
1069 static bool
1070 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
1071 enum by_pieces_operation op)
1073 return targetm.use_by_pieces_infrastructure_p (len, align, op,
1074 optimize_insn_for_speed_p ());
1077 /* Determine whether the LEN bytes can be moved by using several move
1078 instructions. Return nonzero if a call to move_by_pieces should
1079 succeed. */
1081 bool
1082 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
1084 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
1087 /* Return number of insns required to perform operation OP by pieces
1088 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
1090 unsigned HOST_WIDE_INT
1091 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1092 unsigned int max_size, by_pieces_operation op)
1094 unsigned HOST_WIDE_INT n_insns = 0;
1095 fixed_size_mode mode;
1097 if (targetm.overlap_op_by_pieces_p ())
1099 /* NB: Round up L and ALIGN to the widest integer mode for
1100 MAX_SIZE. */
1101 mode = widest_fixed_size_mode_for_size (max_size, op);
1102 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1103 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1104 if (up > l)
1105 l = up;
1106 align = GET_MODE_ALIGNMENT (mode);
1109 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1111 while (max_size > 1 && l > 0)
1113 mode = widest_fixed_size_mode_for_size (max_size, op);
1114 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1116 unsigned int modesize = GET_MODE_SIZE (mode);
1118 if (align >= GET_MODE_ALIGNMENT (mode))
1120 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1121 l %= modesize;
1122 switch (op)
1124 default:
1125 n_insns += n_pieces;
1126 break;
1128 case COMPARE_BY_PIECES:
1129 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1130 int batch_ops = 4 * batch - 1;
1131 unsigned HOST_WIDE_INT full = n_pieces / batch;
1132 n_insns += full * batch_ops;
1133 if (n_pieces % batch != 0)
1134 n_insns++;
1135 break;
1139 max_size = modesize;
1142 gcc_assert (!l);
1143 return n_insns;
1146 /* Used when performing piecewise block operations, holds information
1147 about one of the memory objects involved. The member functions
1148 can be used to generate code for loading from the object and
1149 updating the address when iterating. */
1151 class pieces_addr
1153 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1154 stack pushes. */
1155 rtx m_obj;
1156 /* The address of the object. Can differ from that seen in the
1157 MEM rtx if we copied the address to a register. */
1158 rtx m_addr;
1159 /* Nonzero if the address on the object has an autoincrement already,
1160 signifies whether that was an increment or decrement. */
1161 signed char m_addr_inc;
1162 /* Nonzero if we intend to use autoinc without the address already
1163 having autoinc form. We will insert add insns around each memory
1164 reference, expecting later passes to form autoinc addressing modes.
1165 The only supported options are predecrement and postincrement. */
1166 signed char m_explicit_inc;
1167 /* True if we have either of the two possible cases of using
1168 autoincrement. */
1169 bool m_auto;
1170 /* True if this is an address to be used for load operations rather
1171 than stores. */
1172 bool m_is_load;
1174 /* Optionally, a function to obtain constants for any given offset into
1175 the objects, and data associated with it. */
1176 by_pieces_constfn m_constfn;
1177 void *m_cfndata;
1178 public:
1179 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1180 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1181 void increment_address (HOST_WIDE_INT);
1182 void maybe_predec (HOST_WIDE_INT);
1183 void maybe_postinc (HOST_WIDE_INT);
1184 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1185 int get_addr_inc ()
1187 return m_addr_inc;
1191 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1192 true if the operation to be performed on this object is a load
1193 rather than a store. For stores, OBJ can be NULL, in which case we
1194 assume the operation is a stack push. For loads, the optional
1195 CONSTFN and its associated CFNDATA can be used in place of the
1196 memory load. */
1198 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1199 void *cfndata)
1200 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1202 m_addr_inc = 0;
1203 m_auto = false;
1204 if (obj)
1206 rtx addr = XEXP (obj, 0);
1207 rtx_code code = GET_CODE (addr);
1208 m_addr = addr;
1209 bool dec = code == PRE_DEC || code == POST_DEC;
1210 bool inc = code == PRE_INC || code == POST_INC;
1211 m_auto = inc || dec;
1212 if (m_auto)
1213 m_addr_inc = dec ? -1 : 1;
1215 /* While we have always looked for these codes here, the code
1216 implementing the memory operation has never handled them.
1217 Support could be added later if necessary or beneficial. */
1218 gcc_assert (code != PRE_INC && code != POST_DEC);
1220 else
1222 m_addr = NULL_RTX;
1223 if (!is_load)
1225 m_auto = true;
1226 if (STACK_GROWS_DOWNWARD)
1227 m_addr_inc = -1;
1228 else
1229 m_addr_inc = 1;
1231 else
1232 gcc_assert (constfn != NULL);
1234 m_explicit_inc = 0;
1235 if (constfn)
1236 gcc_assert (is_load);
1239 /* Decide whether to use autoinc for an address involved in a memory op.
1240 MODE is the mode of the accesses, REVERSE is true if we've decided to
1241 perform the operation starting from the end, and LEN is the length of
1242 the operation. Don't override an earlier decision to set m_auto. */
1244 void
1245 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1246 HOST_WIDE_INT len)
1248 if (m_auto || m_obj == NULL_RTX)
1249 return;
1251 bool use_predec = (m_is_load
1252 ? USE_LOAD_PRE_DECREMENT (mode)
1253 : USE_STORE_PRE_DECREMENT (mode));
1254 bool use_postinc = (m_is_load
1255 ? USE_LOAD_POST_INCREMENT (mode)
1256 : USE_STORE_POST_INCREMENT (mode));
1257 machine_mode addr_mode = get_address_mode (m_obj);
1259 if (use_predec && reverse)
1261 m_addr = copy_to_mode_reg (addr_mode,
1262 plus_constant (addr_mode,
1263 m_addr, len));
1264 m_auto = true;
1265 m_explicit_inc = -1;
1267 else if (use_postinc && !reverse)
1269 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1270 m_auto = true;
1271 m_explicit_inc = 1;
1273 else if (CONSTANT_P (m_addr))
1274 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1277 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1278 are using autoincrement for this address, we don't add the offset,
1279 but we still modify the MEM's properties. */
1282 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1283 by_pieces_prev *prev)
1285 if (m_constfn)
1286 /* Pass the previous data to m_constfn. */
1287 return m_constfn (m_cfndata, prev, offset, mode);
1288 if (m_obj == NULL_RTX)
1289 return NULL_RTX;
1290 if (m_auto)
1291 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1292 else
1293 return adjust_address (m_obj, mode, offset);
1296 /* Emit an add instruction to increment the address by SIZE. */
1298 void
1299 pieces_addr::increment_address (HOST_WIDE_INT size)
1301 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1302 emit_insn (gen_add2_insn (m_addr, amount));
1305 /* If we are supposed to decrement the address after each access, emit code
1306 to do so now. Increment by SIZE (which has should have the correct sign
1307 already). */
1309 void
1310 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1312 if (m_explicit_inc >= 0)
1313 return;
1314 gcc_assert (HAVE_PRE_DECREMENT);
1315 increment_address (size);
1318 /* If we are supposed to decrement the address after each access, emit code
1319 to do so now. Increment by SIZE. */
1321 void
1322 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1324 if (m_explicit_inc <= 0)
1325 return;
1326 gcc_assert (HAVE_POST_INCREMENT);
1327 increment_address (size);
1330 /* This structure is used by do_op_by_pieces to describe the operation
1331 to be performed. */
1333 class op_by_pieces_d
1335 private:
1336 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1337 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1339 protected:
1340 pieces_addr m_to, m_from;
1341 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1342 use it to check the valid mode size. */
1343 const unsigned HOST_WIDE_INT m_len;
1344 HOST_WIDE_INT m_offset;
1345 unsigned int m_align;
1346 unsigned int m_max_size;
1347 bool m_reverse;
1348 /* True if this is a stack push. */
1349 bool m_push;
1350 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1351 bool m_overlap_op_by_pieces;
1352 /* The type of operation that we're performing. */
1353 by_pieces_operation m_op;
1355 /* Virtual functions, overriden by derived classes for the specific
1356 operation. */
1357 virtual void generate (rtx, rtx, machine_mode) = 0;
1358 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1359 virtual void finish_mode (machine_mode)
1363 public:
1364 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1365 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1366 by_pieces_operation);
1367 void run ();
1370 /* The constructor for an op_by_pieces_d structure. We require two
1371 objects named TO and FROM, which are identified as loads or stores
1372 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1373 and its associated FROM_CFN_DATA can be used to replace loads with
1374 constant values. MAX_PIECES describes the maximum number of bytes
1375 at a time which can be moved efficiently. LEN describes the length
1376 of the operation. */
1378 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1379 bool to_load, rtx from, bool from_load,
1380 by_pieces_constfn from_cfn,
1381 void *from_cfn_data,
1382 unsigned HOST_WIDE_INT len,
1383 unsigned int align, bool push,
1384 by_pieces_operation op)
1385 : m_to (to, to_load, NULL, NULL),
1386 m_from (from, from_load, from_cfn, from_cfn_data),
1387 m_len (len), m_max_size (max_pieces + 1),
1388 m_push (push), m_op (op)
1390 int toi = m_to.get_addr_inc ();
1391 int fromi = m_from.get_addr_inc ();
1392 if (toi >= 0 && fromi >= 0)
1393 m_reverse = false;
1394 else if (toi <= 0 && fromi <= 0)
1395 m_reverse = true;
1396 else
1397 gcc_unreachable ();
1399 m_offset = m_reverse ? len : 0;
1400 align = MIN (to ? MEM_ALIGN (to) : align,
1401 from ? MEM_ALIGN (from) : align);
1403 /* If copying requires more than two move insns,
1404 copy addresses to registers (to make displacements shorter)
1405 and use post-increment if available. */
1406 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1408 /* Find the mode of the largest comparison. */
1409 fixed_size_mode mode
1410 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1412 m_from.decide_autoinc (mode, m_reverse, len);
1413 m_to.decide_autoinc (mode, m_reverse, len);
1416 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1417 m_align = align;
1419 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1422 /* This function returns the largest usable integer mode for LEN bytes
1423 whose size is no bigger than size of MODE. */
1425 fixed_size_mode
1426 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1428 unsigned int size;
1431 size = GET_MODE_SIZE (mode);
1432 if (len >= size && prepare_mode (mode, m_align))
1433 break;
1434 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1435 mode = widest_fixed_size_mode_for_size (size, m_op);
1437 while (1);
1438 return mode;
1441 /* Return the smallest integer or QI vector mode that is not narrower
1442 than SIZE bytes. */
1444 fixed_size_mode
1445 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1447 /* Use QI vector only for > size of WORD. */
1448 if (can_use_qi_vectors (m_op) && size > UNITS_PER_WORD)
1450 machine_mode mode;
1451 fixed_size_mode candidate;
1452 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1453 if (is_a<fixed_size_mode> (mode, &candidate)
1454 && GET_MODE_INNER (candidate) == QImode)
1456 /* Don't return a mode wider than M_LEN. */
1457 if (GET_MODE_SIZE (candidate) > m_len)
1458 break;
1460 if (GET_MODE_SIZE (candidate) >= size
1461 && by_pieces_mode_supported_p (candidate, m_op))
1462 return candidate;
1466 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1469 /* This function contains the main loop used for expanding a block
1470 operation. First move what we can in the largest integer mode,
1471 then go to successively smaller modes. For every access, call
1472 GENFUN with the two operands and the EXTRA_DATA. */
1474 void
1475 op_by_pieces_d::run ()
1477 if (m_len == 0)
1478 return;
1480 unsigned HOST_WIDE_INT length = m_len;
1482 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1483 fixed_size_mode mode
1484 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1485 mode = get_usable_mode (mode, length);
1487 by_pieces_prev to_prev = { nullptr, mode };
1488 by_pieces_prev from_prev = { nullptr, mode };
1492 unsigned int size = GET_MODE_SIZE (mode);
1493 rtx to1 = NULL_RTX, from1;
1495 while (length >= size)
1497 if (m_reverse)
1498 m_offset -= size;
1500 to1 = m_to.adjust (mode, m_offset, &to_prev);
1501 to_prev.data = to1;
1502 to_prev.mode = mode;
1503 from1 = m_from.adjust (mode, m_offset, &from_prev);
1504 from_prev.data = from1;
1505 from_prev.mode = mode;
1507 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1508 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1510 generate (to1, from1, mode);
1512 m_to.maybe_postinc (size);
1513 m_from.maybe_postinc (size);
1515 if (!m_reverse)
1516 m_offset += size;
1518 length -= size;
1521 finish_mode (mode);
1523 if (length == 0)
1524 return;
1526 if (!m_push && m_overlap_op_by_pieces)
1528 /* NB: Generate overlapping operations if it is not a stack
1529 push since stack push must not overlap. Get the smallest
1530 fixed size mode for M_LEN bytes. */
1531 mode = smallest_fixed_size_mode_for_size (length);
1532 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1533 int gap = GET_MODE_SIZE (mode) - length;
1534 if (gap > 0)
1536 /* If size of MODE > M_LEN, generate the last operation
1537 in MODE for the remaining bytes with ovelapping memory
1538 from the previois operation. */
1539 if (m_reverse)
1540 m_offset += gap;
1541 else
1542 m_offset -= gap;
1543 length += gap;
1546 else
1548 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1549 mode = widest_fixed_size_mode_for_size (size, m_op);
1550 mode = get_usable_mode (mode, length);
1553 while (1);
1556 /* Derived class from op_by_pieces_d, providing support for block move
1557 operations. */
1559 #ifdef PUSH_ROUNDING
1560 #define PUSHG_P(to) ((to) == nullptr)
1561 #else
1562 #define PUSHG_P(to) false
1563 #endif
1565 class move_by_pieces_d : public op_by_pieces_d
1567 insn_gen_fn m_gen_fun;
1568 void generate (rtx, rtx, machine_mode) final override;
1569 bool prepare_mode (machine_mode, unsigned int) final override;
1571 public:
1572 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1573 unsigned int align)
1574 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1575 NULL, len, align, PUSHG_P (to), MOVE_BY_PIECES)
1578 rtx finish_retmode (memop_ret);
1581 /* Return true if MODE can be used for a set of copies, given an
1582 alignment ALIGN. Prepare whatever data is necessary for later
1583 calls to generate. */
1585 bool
1586 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1588 insn_code icode = optab_handler (mov_optab, mode);
1589 m_gen_fun = GEN_FCN (icode);
1590 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1593 /* A callback used when iterating for a compare_by_pieces_operation.
1594 OP0 and OP1 are the values that have been loaded and should be
1595 compared in MODE. If OP0 is NULL, this means we should generate a
1596 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1597 gen function that should be used to generate the mode. */
1599 void
1600 move_by_pieces_d::generate (rtx op0, rtx op1,
1601 machine_mode mode ATTRIBUTE_UNUSED)
1603 #ifdef PUSH_ROUNDING
1604 if (op0 == NULL_RTX)
1606 emit_single_push_insn (mode, op1, NULL);
1607 return;
1609 #endif
1610 emit_insn (m_gen_fun (op0, op1));
1613 /* Perform the final adjustment at the end of a string to obtain the
1614 correct return value for the block operation.
1615 Return value is based on RETMODE argument. */
1618 move_by_pieces_d::finish_retmode (memop_ret retmode)
1620 gcc_assert (!m_reverse);
1621 if (retmode == RETURN_END_MINUS_ONE)
1623 m_to.maybe_postinc (-1);
1624 --m_offset;
1626 return m_to.adjust (QImode, m_offset);
1629 /* Generate several move instructions to copy LEN bytes from block FROM to
1630 block TO. (These are MEM rtx's with BLKmode).
1632 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1633 used to push FROM to the stack.
1635 ALIGN is maximum stack alignment we can assume.
1637 Return value is based on RETMODE argument. */
1640 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1641 unsigned int align, memop_ret retmode)
1643 #ifndef PUSH_ROUNDING
1644 if (to == NULL)
1645 gcc_unreachable ();
1646 #endif
1648 move_by_pieces_d data (to, from, len, align);
1650 data.run ();
1652 if (retmode != RETURN_BEGIN)
1653 return data.finish_retmode (retmode);
1654 else
1655 return to;
1658 /* Derived class from op_by_pieces_d, providing support for block move
1659 operations. */
1661 class store_by_pieces_d : public op_by_pieces_d
1663 insn_gen_fn m_gen_fun;
1665 void generate (rtx, rtx, machine_mode) final override;
1666 bool prepare_mode (machine_mode, unsigned int) final override;
1668 public:
1669 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1670 unsigned HOST_WIDE_INT len, unsigned int align,
1671 by_pieces_operation op)
1672 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1673 cfn_data, len, align, false, op)
1676 rtx finish_retmode (memop_ret);
1679 /* Return true if MODE can be used for a set of stores, given an
1680 alignment ALIGN. Prepare whatever data is necessary for later
1681 calls to generate. */
1683 bool
1684 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1686 insn_code icode = optab_handler (mov_optab, mode);
1687 m_gen_fun = GEN_FCN (icode);
1688 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1691 /* A callback used when iterating for a store_by_pieces_operation.
1692 OP0 and OP1 are the values that have been loaded and should be
1693 compared in MODE. If OP0 is NULL, this means we should generate a
1694 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1695 gen function that should be used to generate the mode. */
1697 void
1698 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1700 emit_insn (m_gen_fun (op0, op1));
1703 /* Perform the final adjustment at the end of a string to obtain the
1704 correct return value for the block operation.
1705 Return value is based on RETMODE argument. */
1708 store_by_pieces_d::finish_retmode (memop_ret retmode)
1710 gcc_assert (!m_reverse);
1711 if (retmode == RETURN_END_MINUS_ONE)
1713 m_to.maybe_postinc (-1);
1714 --m_offset;
1716 return m_to.adjust (QImode, m_offset);
1719 /* Determine whether the LEN bytes generated by CONSTFUN can be
1720 stored to memory using several move instructions. CONSTFUNDATA is
1721 a pointer which will be passed as argument in every CONSTFUN call.
1722 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1723 a memset operation and false if it's a copy of a constant string.
1724 Return true if a call to store_by_pieces should succeed. */
1726 bool
1727 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1728 by_pieces_constfn constfun,
1729 void *constfundata, unsigned int align, bool memsetp)
1731 unsigned HOST_WIDE_INT l;
1732 unsigned int max_size;
1733 HOST_WIDE_INT offset = 0;
1734 enum insn_code icode;
1735 int reverse;
1736 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1737 rtx cst ATTRIBUTE_UNUSED;
1739 if (len == 0)
1740 return true;
1742 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1743 memsetp
1744 ? SET_BY_PIECES
1745 : STORE_BY_PIECES,
1746 optimize_insn_for_speed_p ()))
1747 return false;
1749 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1751 /* We would first store what we can in the largest integer mode, then go to
1752 successively smaller modes. */
1754 for (reverse = 0;
1755 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1756 reverse++)
1758 l = len;
1759 max_size = STORE_MAX_PIECES + 1;
1760 while (max_size > 1 && l > 0)
1762 auto op = memsetp ? SET_BY_PIECES : STORE_BY_PIECES;
1763 auto mode = widest_fixed_size_mode_for_size (max_size, op);
1765 icode = optab_handler (mov_optab, mode);
1766 if (icode != CODE_FOR_nothing
1767 && align >= GET_MODE_ALIGNMENT (mode))
1769 unsigned int size = GET_MODE_SIZE (mode);
1771 while (l >= size)
1773 if (reverse)
1774 offset -= size;
1776 cst = (*constfun) (constfundata, nullptr, offset, mode);
1777 /* All CONST_VECTORs can be loaded for memset since
1778 vec_duplicate_optab is a precondition to pick a
1779 vector mode for the memset expander. */
1780 if (!((memsetp && VECTOR_MODE_P (mode))
1781 || targetm.legitimate_constant_p (mode, cst)))
1782 return false;
1784 if (!reverse)
1785 offset += size;
1787 l -= size;
1791 max_size = GET_MODE_SIZE (mode);
1794 /* The code above should have handled everything. */
1795 gcc_assert (!l);
1798 return true;
1801 /* Generate several move instructions to store LEN bytes generated by
1802 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1803 pointer which will be passed as argument in every CONSTFUN call.
1804 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1805 a memset operation and false if it's a copy of a constant string.
1806 Return value is based on RETMODE argument. */
1809 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1810 by_pieces_constfn constfun,
1811 void *constfundata, unsigned int align, bool memsetp,
1812 memop_ret retmode)
1814 if (len == 0)
1816 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1817 return to;
1820 gcc_assert (targetm.use_by_pieces_infrastructure_p
1821 (len, align,
1822 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1823 optimize_insn_for_speed_p ()));
1825 store_by_pieces_d data (to, constfun, constfundata, len, align,
1826 memsetp ? SET_BY_PIECES : STORE_BY_PIECES);
1827 data.run ();
1829 if (retmode != RETURN_BEGIN)
1830 return data.finish_retmode (retmode);
1831 else
1832 return to;
1835 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1836 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1838 static void
1839 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1841 if (len == 0)
1842 return;
1844 /* Use builtin_memset_read_str to support vector mode broadcast. */
1845 char c = 0;
1846 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1847 CLEAR_BY_PIECES);
1848 data.run ();
1851 /* Context used by compare_by_pieces_genfn. It stores the fail label
1852 to jump to in case of miscomparison, and for branch ratios greater than 1,
1853 it stores an accumulator and the current and maximum counts before
1854 emitting another branch. */
1856 class compare_by_pieces_d : public op_by_pieces_d
1858 rtx_code_label *m_fail_label;
1859 rtx m_accumulator;
1860 int m_count, m_batch;
1862 void generate (rtx, rtx, machine_mode) final override;
1863 bool prepare_mode (machine_mode, unsigned int) final override;
1864 void finish_mode (machine_mode) final override;
1866 public:
1867 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1868 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1869 rtx_code_label *fail_label)
1870 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1871 op1_cfn_data, len, align, false, COMPARE_BY_PIECES)
1873 m_fail_label = fail_label;
1877 /* A callback used when iterating for a compare_by_pieces_operation.
1878 OP0 and OP1 are the values that have been loaded and should be
1879 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1880 context structure. */
1882 void
1883 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1885 if (m_batch > 1)
1887 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1888 true, OPTAB_LIB_WIDEN);
1889 if (m_count != 0)
1890 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1891 true, OPTAB_LIB_WIDEN);
1892 m_accumulator = temp;
1894 if (++m_count < m_batch)
1895 return;
1897 m_count = 0;
1898 op0 = m_accumulator;
1899 op1 = const0_rtx;
1900 m_accumulator = NULL_RTX;
1902 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1903 m_fail_label, profile_probability::uninitialized ());
1906 /* Return true if MODE can be used for a set of moves and comparisons,
1907 given an alignment ALIGN. Prepare whatever data is necessary for
1908 later calls to generate. */
1910 bool
1911 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1913 insn_code icode = optab_handler (mov_optab, mode);
1914 if (icode == CODE_FOR_nothing
1915 || align < GET_MODE_ALIGNMENT (mode)
1916 || !can_compare_p (EQ, mode, ccp_jump))
1917 return false;
1918 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1919 if (m_batch < 0)
1920 return false;
1921 m_accumulator = NULL_RTX;
1922 m_count = 0;
1923 return true;
1926 /* Called after expanding a series of comparisons in MODE. If we have
1927 accumulated results for which we haven't emitted a branch yet, do
1928 so now. */
1930 void
1931 compare_by_pieces_d::finish_mode (machine_mode mode)
1933 if (m_accumulator != NULL_RTX)
1934 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1935 NULL_RTX, NULL, m_fail_label,
1936 profile_probability::uninitialized ());
1939 /* Generate several move instructions to compare LEN bytes from blocks
1940 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1942 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1943 used to push FROM to the stack.
1945 ALIGN is maximum stack alignment we can assume.
1947 Optionally, the caller can pass a constfn and associated data in A1_CFN
1948 and A1_CFN_DATA. describing that the second operand being compared is a
1949 known constant and how to obtain its data. */
1951 static rtx
1952 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1953 rtx target, unsigned int align,
1954 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1956 rtx_code_label *fail_label = gen_label_rtx ();
1957 rtx_code_label *end_label = gen_label_rtx ();
1959 if (target == NULL_RTX
1960 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1961 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1963 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1964 fail_label);
1966 data.run ();
1968 emit_move_insn (target, const0_rtx);
1969 emit_jump (end_label);
1970 emit_barrier ();
1971 emit_label (fail_label);
1972 emit_move_insn (target, const1_rtx);
1973 emit_label (end_label);
1975 return target;
1978 /* Emit code to move a block Y to a block X. This may be done with
1979 string-move instructions, with multiple scalar move instructions,
1980 or with a library call.
1982 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1983 SIZE is an rtx that says how long they are.
1984 ALIGN is the maximum alignment we can assume they have.
1985 METHOD describes what kind of copy this is, and what mechanisms may be used.
1986 MIN_SIZE is the minimal size of block to move
1987 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1988 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1989 CTZ_SIZE is the trailing-zeros count of SIZE; even a nonconstant SIZE is
1990 known to be a multiple of 1<<CTZ_SIZE.
1992 Return the address of the new block, if memcpy is called and returns it,
1993 0 otherwise. */
1996 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1997 unsigned int expected_align, HOST_WIDE_INT expected_size,
1998 unsigned HOST_WIDE_INT min_size,
1999 unsigned HOST_WIDE_INT max_size,
2000 unsigned HOST_WIDE_INT probable_max_size,
2001 bool bail_out_libcall, bool *is_move_done,
2002 bool might_overlap, unsigned ctz_size)
2004 int may_use_call;
2005 rtx retval = 0;
2006 unsigned int align;
2008 if (is_move_done)
2009 *is_move_done = true;
2011 gcc_assert (size);
2012 if (CONST_INT_P (size) && INTVAL (size) == 0)
2013 return 0;
2015 switch (method)
2017 case BLOCK_OP_NORMAL:
2018 case BLOCK_OP_TAILCALL:
2019 may_use_call = 1;
2020 break;
2022 case BLOCK_OP_CALL_PARM:
2023 may_use_call = block_move_libcall_safe_for_call_parm ();
2025 /* Make inhibit_defer_pop nonzero around the library call
2026 to force it to pop the arguments right away. */
2027 NO_DEFER_POP;
2028 break;
2030 case BLOCK_OP_NO_LIBCALL:
2031 may_use_call = 0;
2032 break;
2034 case BLOCK_OP_NO_LIBCALL_RET:
2035 may_use_call = -1;
2036 break;
2038 default:
2039 gcc_unreachable ();
2042 gcc_assert (MEM_P (x) && MEM_P (y));
2043 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2044 gcc_assert (align >= BITS_PER_UNIT);
2046 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2047 block copy is more efficient for other large modes, e.g. DCmode. */
2048 x = adjust_address (x, BLKmode, 0);
2049 y = adjust_address (y, BLKmode, 0);
2051 /* If source and destination are the same, no need to copy anything. */
2052 if (rtx_equal_p (x, y)
2053 && !MEM_VOLATILE_P (x)
2054 && !MEM_VOLATILE_P (y))
2055 return 0;
2057 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2058 can be incorrect is coming from __builtin_memcpy. */
2059 poly_int64 const_size;
2060 if (poly_int_rtx_p (size, &const_size))
2062 x = shallow_copy_rtx (x);
2063 y = shallow_copy_rtx (y);
2064 set_mem_size (x, const_size);
2065 set_mem_size (y, const_size);
2068 bool pieces_ok = CONST_INT_P (size)
2069 && can_move_by_pieces (INTVAL (size), align);
2070 bool pattern_ok = false;
2072 if (!pieces_ok || might_overlap)
2074 pattern_ok
2075 = emit_block_move_via_pattern (x, y, size, align,
2076 expected_align, expected_size,
2077 min_size, max_size, probable_max_size,
2078 might_overlap);
2079 if (!pattern_ok && might_overlap)
2081 /* Do not try any of the other methods below as they are not safe
2082 for overlapping moves. */
2083 *is_move_done = false;
2084 return retval;
2088 bool dynamic_direction = false;
2089 if (!pattern_ok && !pieces_ok && may_use_call
2090 && (flag_inline_stringops & (might_overlap ? ILSOP_MEMMOVE : ILSOP_MEMCPY)))
2092 may_use_call = 0;
2093 dynamic_direction = might_overlap;
2096 if (pattern_ok)
2098 else if (pieces_ok)
2099 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2100 else if (may_use_call && !might_overlap
2101 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2102 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2104 if (bail_out_libcall)
2106 if (is_move_done)
2107 *is_move_done = false;
2108 return retval;
2111 if (may_use_call < 0)
2112 return pc_rtx;
2114 retval = emit_block_copy_via_libcall (x, y, size,
2115 method == BLOCK_OP_TAILCALL);
2117 else if (dynamic_direction)
2118 emit_block_move_via_oriented_loop (x, y, size, align, ctz_size);
2119 else if (might_overlap)
2120 *is_move_done = false;
2121 else
2122 emit_block_move_via_sized_loop (x, y, size, align, ctz_size);
2124 if (method == BLOCK_OP_CALL_PARM)
2125 OK_DEFER_POP;
2127 return retval;
2131 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method,
2132 unsigned int ctz_size)
2134 unsigned HOST_WIDE_INT max, min = 0;
2135 if (GET_CODE (size) == CONST_INT)
2136 min = max = UINTVAL (size);
2137 else
2138 max = GET_MODE_MASK (GET_MODE (size));
2139 return emit_block_move_hints (x, y, size, method, 0, -1,
2140 min, max, max,
2141 false, NULL, false, ctz_size);
2144 /* A subroutine of emit_block_move. Returns true if calling the
2145 block move libcall will not clobber any parameters which may have
2146 already been placed on the stack. */
2148 static bool
2149 block_move_libcall_safe_for_call_parm (void)
2151 tree fn;
2153 /* If arguments are pushed on the stack, then they're safe. */
2154 if (targetm.calls.push_argument (0))
2155 return true;
2157 /* If registers go on the stack anyway, any argument is sure to clobber
2158 an outgoing argument. */
2159 #if defined (REG_PARM_STACK_SPACE)
2160 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2161 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2162 depend on its argument. */
2163 (void) fn;
2164 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2165 && REG_PARM_STACK_SPACE (fn) != 0)
2166 return false;
2167 #endif
2169 /* If any argument goes in memory, then it might clobber an outgoing
2170 argument. */
2172 CUMULATIVE_ARGS args_so_far_v;
2173 cumulative_args_t args_so_far;
2174 tree arg;
2176 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2177 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2178 args_so_far = pack_cumulative_args (&args_so_far_v);
2180 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2181 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2183 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2184 function_arg_info arg_info (mode, /*named=*/true);
2185 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2186 if (!tmp || !REG_P (tmp))
2187 return false;
2188 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2189 return false;
2190 targetm.calls.function_arg_advance (args_so_far, arg_info);
2193 return true;
2196 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2197 return true if successful.
2199 X is the destination of the copy or move.
2200 Y is the source of the copy or move.
2201 SIZE is the size of the block to be moved.
2203 MIGHT_OVERLAP indicates this originated with expansion of a
2204 builtin_memmove() and the source and destination blocks may
2205 overlap.
2208 static bool
2209 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2210 unsigned int expected_align,
2211 HOST_WIDE_INT expected_size,
2212 unsigned HOST_WIDE_INT min_size,
2213 unsigned HOST_WIDE_INT max_size,
2214 unsigned HOST_WIDE_INT probable_max_size,
2215 bool might_overlap)
2217 if (expected_align < align)
2218 expected_align = align;
2219 if (expected_size != -1)
2221 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2222 expected_size = probable_max_size;
2223 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2224 expected_size = min_size;
2227 /* Since this is a move insn, we don't care about volatility. */
2228 temporary_volatile_ok v (true);
2230 /* Try the most limited insn first, because there's no point
2231 including more than one in the machine description unless
2232 the more limited one has some advantage. */
2234 opt_scalar_int_mode mode_iter;
2235 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2237 scalar_int_mode mode = mode_iter.require ();
2238 enum insn_code code;
2239 if (might_overlap)
2240 code = direct_optab_handler (movmem_optab, mode);
2241 else
2242 code = direct_optab_handler (cpymem_optab, mode);
2244 if (code != CODE_FOR_nothing
2245 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2246 here because if SIZE is less than the mode mask, as it is
2247 returned by the macro, it will definitely be less than the
2248 actual mode mask. Since SIZE is within the Pmode address
2249 space, we limit MODE to Pmode. */
2250 && ((CONST_INT_P (size)
2251 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2252 <= (GET_MODE_MASK (mode) >> 1)))
2253 || max_size <= (GET_MODE_MASK (mode) >> 1)
2254 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2256 class expand_operand ops[9];
2257 unsigned int nops;
2259 /* ??? When called via emit_block_move_for_call, it'd be
2260 nice if there were some way to inform the backend, so
2261 that it doesn't fail the expansion because it thinks
2262 emitting the libcall would be more efficient. */
2263 nops = insn_data[(int) code].n_generator_args;
2264 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2266 create_fixed_operand (&ops[0], x);
2267 create_fixed_operand (&ops[1], y);
2268 /* The check above guarantees that this size conversion is valid. */
2269 create_convert_operand_to (&ops[2], size, mode, true);
2270 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2271 if (nops >= 6)
2273 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2274 create_integer_operand (&ops[5], expected_size);
2276 if (nops >= 8)
2278 create_integer_operand (&ops[6], min_size);
2279 /* If we cannot represent the maximal size,
2280 make parameter NULL. */
2281 if ((HOST_WIDE_INT) max_size != -1)
2282 create_integer_operand (&ops[7], max_size);
2283 else
2284 create_fixed_operand (&ops[7], NULL);
2286 if (nops == 9)
2288 /* If we cannot represent the maximal size,
2289 make parameter NULL. */
2290 if ((HOST_WIDE_INT) probable_max_size != -1)
2291 create_integer_operand (&ops[8], probable_max_size);
2292 else
2293 create_fixed_operand (&ops[8], NULL);
2295 if (maybe_expand_insn (code, nops, ops))
2296 return true;
2300 return false;
2303 /* Like emit_block_move_via_loop, but choose a suitable INCR based on
2304 ALIGN and CTZ_SIZE. */
2306 static void
2307 emit_block_move_via_sized_loop (rtx x, rtx y, rtx size,
2308 unsigned int align,
2309 unsigned int ctz_size)
2311 int incr = align / BITS_PER_UNIT;
2313 if (CONST_INT_P (size))
2314 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size)));
2316 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr)
2317 incr = HOST_WIDE_INT_1U << ctz_size;
2319 while (incr > 1 && !can_move_by_pieces (incr, align))
2320 incr >>= 1;
2322 gcc_checking_assert (incr);
2324 return emit_block_move_via_loop (x, y, size, align, incr);
2327 /* Like emit_block_move_via_sized_loop, but besides choosing INCR so
2328 as to ensure safe moves even in case of overlap, output dynamic
2329 tests to choose between two loops, one moving downwards, another
2330 moving upwards. */
2332 static void
2333 emit_block_move_via_oriented_loop (rtx x, rtx y, rtx size,
2334 unsigned int align,
2335 unsigned int ctz_size)
2337 int incr = align / BITS_PER_UNIT;
2339 if (CONST_INT_P (size))
2340 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size)));
2342 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr)
2343 incr = HOST_WIDE_INT_1U << ctz_size;
2345 while (incr > 1 && !int_mode_for_size (incr, 0).exists ())
2346 incr >>= 1;
2348 gcc_checking_assert (incr);
2350 rtx_code_label *upw_label, *end_label;
2351 upw_label = gen_label_rtx ();
2352 end_label = gen_label_rtx ();
2354 rtx x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2355 rtx y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2356 do_pending_stack_adjust ();
2358 machine_mode mode = GET_MODE (x_addr);
2359 if (mode != GET_MODE (y_addr))
2361 scalar_int_mode xmode
2362 = smallest_int_mode_for_size (GET_MODE_BITSIZE (mode));
2363 scalar_int_mode ymode
2364 = smallest_int_mode_for_size (GET_MODE_BITSIZE
2365 (GET_MODE (y_addr)));
2366 if (GET_MODE_BITSIZE (xmode) < GET_MODE_BITSIZE (ymode))
2367 mode = ymode;
2368 else
2369 mode = xmode;
2371 #ifndef POINTERS_EXTEND_UNSIGNED
2372 const int POINTERS_EXTEND_UNSIGNED = 1;
2373 #endif
2374 x_addr = convert_modes (mode, GET_MODE (x_addr), x_addr,
2375 POINTERS_EXTEND_UNSIGNED);
2376 y_addr = convert_modes (mode, GET_MODE (y_addr), y_addr,
2377 POINTERS_EXTEND_UNSIGNED);
2380 /* Test for overlap: if (x >= y || x + size <= y) goto upw_label. */
2381 emit_cmp_and_jump_insns (x_addr, y_addr, GEU, NULL_RTX, mode,
2382 true, upw_label,
2383 profile_probability::guessed_always ()
2384 .apply_scale (5, 10));
2385 rtx tmp = convert_modes (GET_MODE (x_addr), GET_MODE (size), size, true);
2386 tmp = simplify_gen_binary (PLUS, GET_MODE (x_addr), x_addr, tmp);
2388 emit_cmp_and_jump_insns (tmp, y_addr, LEU, NULL_RTX, mode,
2389 true, upw_label,
2390 profile_probability::guessed_always ()
2391 .apply_scale (8, 10));
2393 emit_block_move_via_loop (x, y, size, align, -incr);
2395 emit_jump (end_label);
2396 emit_label (upw_label);
2398 emit_block_move_via_loop (x, y, size, align, incr);
2400 emit_label (end_label);
2403 /* A subroutine of emit_block_move. Copy the data via an explicit
2404 loop. This is used only when libcalls are forbidden, or when
2405 inlining is required. INCR is the block size to be copied in each
2406 loop iteration. If it is negative, the absolute value is used, and
2407 the block is copied backwards. INCR must be a power of two, an
2408 exact divisor for SIZE and ALIGN, and imply a mode that can be
2409 safely copied per iteration assuming no overlap. */
2411 static void
2412 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2413 unsigned int align, int incr)
2415 rtx_code_label *cmp_label, *top_label;
2416 rtx iter, x_addr, y_addr, tmp;
2417 machine_mode x_addr_mode = get_address_mode (x);
2418 machine_mode y_addr_mode = get_address_mode (y);
2419 machine_mode iter_mode;
2421 iter_mode = GET_MODE (size);
2422 if (iter_mode == VOIDmode)
2423 iter_mode = word_mode;
2425 top_label = gen_label_rtx ();
2426 cmp_label = gen_label_rtx ();
2427 iter = gen_reg_rtx (iter_mode);
2429 bool downwards = incr < 0;
2430 rtx iter_init;
2431 rtx_code iter_cond;
2432 rtx iter_limit;
2433 rtx iter_incr;
2434 machine_mode move_mode;
2435 if (downwards)
2437 incr = -incr;
2438 iter_init = size;
2439 iter_cond = GEU;
2440 iter_limit = const0_rtx;
2441 iter_incr = GEN_INT (incr);
2443 else
2445 iter_init = const0_rtx;
2446 iter_cond = LTU;
2447 iter_limit = size;
2448 iter_incr = GEN_INT (incr);
2450 emit_move_insn (iter, iter_init);
2452 scalar_int_mode int_move_mode
2453 = smallest_int_mode_for_size (incr * BITS_PER_UNIT);
2454 if (GET_MODE_BITSIZE (int_move_mode) != incr * BITS_PER_UNIT)
2456 move_mode = BLKmode;
2457 gcc_checking_assert (can_move_by_pieces (incr, align));
2459 else
2460 move_mode = int_move_mode;
2462 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2463 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2464 do_pending_stack_adjust ();
2466 emit_jump (cmp_label);
2467 emit_label (top_label);
2469 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2470 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2472 if (x_addr_mode != y_addr_mode)
2473 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2474 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2476 x = change_address (x, move_mode, x_addr);
2477 y = change_address (y, move_mode, y_addr);
2479 if (move_mode == BLKmode)
2481 bool done;
2482 emit_block_move_hints (x, y, iter_incr, BLOCK_OP_NO_LIBCALL,
2483 align, incr, incr, incr, incr,
2484 false, &done, false);
2485 gcc_checking_assert (done);
2487 else
2488 emit_move_insn (x, y);
2490 if (downwards)
2491 emit_label (cmp_label);
2493 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2494 true, OPTAB_LIB_WIDEN);
2495 if (tmp != iter)
2496 emit_move_insn (iter, tmp);
2498 if (!downwards)
2499 emit_label (cmp_label);
2501 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2502 true, top_label,
2503 profile_probability::guessed_always ()
2504 .apply_scale (9, 10));
2507 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2508 TAILCALL is true if this is a tail call. */
2511 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2512 rtx size, bool tailcall)
2514 rtx dst_addr, src_addr;
2515 tree call_expr, dst_tree, src_tree, size_tree;
2516 machine_mode size_mode;
2518 /* Since dst and src are passed to a libcall, mark the corresponding
2519 tree EXPR as addressable. */
2520 tree dst_expr = MEM_EXPR (dst);
2521 tree src_expr = MEM_EXPR (src);
2522 if (dst_expr)
2523 mark_addressable (dst_expr);
2524 if (src_expr)
2525 mark_addressable (src_expr);
2527 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2528 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2529 dst_tree = make_tree (ptr_type_node, dst_addr);
2531 src_addr = copy_addr_to_reg (XEXP (src, 0));
2532 src_addr = convert_memory_address (ptr_mode, src_addr);
2533 src_tree = make_tree (ptr_type_node, src_addr);
2535 size_mode = TYPE_MODE (sizetype);
2536 size = convert_to_mode (size_mode, size, 1);
2537 size = copy_to_mode_reg (size_mode, size);
2538 size_tree = make_tree (sizetype, size);
2540 /* It is incorrect to use the libcall calling conventions for calls to
2541 memcpy/memmove/memcmp because they can be provided by the user. */
2542 tree fn = builtin_decl_implicit (fncode);
2543 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2544 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2546 return expand_call (call_expr, NULL_RTX, false);
2549 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2550 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2551 otherwise return null. */
2554 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2555 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2556 HOST_WIDE_INT align)
2558 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2560 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2561 target = NULL_RTX;
2563 class expand_operand ops[5];
2564 create_output_operand (&ops[0], target, insn_mode);
2565 create_fixed_operand (&ops[1], arg1_rtx);
2566 create_fixed_operand (&ops[2], arg2_rtx);
2567 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2568 TYPE_UNSIGNED (arg3_type));
2569 create_integer_operand (&ops[4], align);
2570 if (maybe_expand_insn (icode, 5, ops))
2571 return ops[0].value;
2572 return NULL_RTX;
2575 /* Expand a block compare between X and Y with length LEN using the
2576 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2577 of the expression that was used to calculate the length. ALIGN
2578 gives the known minimum common alignment. */
2580 static rtx
2581 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2582 unsigned align)
2584 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2585 implementing memcmp because it will stop if it encounters two
2586 zero bytes. */
2587 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2589 if (icode == CODE_FOR_nothing)
2590 return NULL_RTX;
2592 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2595 /* Emit code to compare a block Y to a block X. This may be done with
2596 string-compare instructions, with multiple scalar instructions,
2597 or with a library call.
2599 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2600 they are. LEN_TYPE is the type of the expression that was used to
2601 calculate it, and CTZ_LEN is the known trailing-zeros count of LEN,
2602 so LEN must be a multiple of 1<<CTZ_LEN even if it's not constant.
2604 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2605 value of a normal memcmp call, instead we can just compare for equality.
2606 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2607 returning NULL_RTX.
2609 Optionally, the caller can pass a constfn and associated data in Y_CFN
2610 and Y_CFN_DATA. describing that the second operand being compared is a
2611 known constant and how to obtain its data.
2612 Return the result of the comparison, or NULL_RTX if we failed to
2613 perform the operation. */
2616 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2617 bool equality_only, by_pieces_constfn y_cfn,
2618 void *y_cfndata, unsigned ctz_len)
2620 rtx result = 0;
2622 if (CONST_INT_P (len) && INTVAL (len) == 0)
2623 return const0_rtx;
2625 gcc_assert (MEM_P (x) && MEM_P (y));
2626 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2627 gcc_assert (align >= BITS_PER_UNIT);
2629 x = adjust_address (x, BLKmode, 0);
2630 y = adjust_address (y, BLKmode, 0);
2632 if (equality_only
2633 && CONST_INT_P (len)
2634 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2635 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2636 y_cfn, y_cfndata);
2637 else
2638 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2640 if (!result && (flag_inline_stringops & ILSOP_MEMCMP))
2641 result = emit_block_cmp_via_loop (x, y, len, len_type,
2642 target, equality_only,
2643 align, ctz_len);
2645 return result;
2648 /* Like emit_block_cmp_hints, but with known alignment and no support
2649 for constats. Always expand to a loop with iterations that compare
2650 blocks of the largest compare-by-pieces size that divides both len
2651 and align, and then, if !EQUALITY_ONLY, identify the word and then
2652 the unit that first differs to return the result. */
2655 emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
2656 bool equality_only, unsigned align, unsigned ctz_len)
2658 unsigned incr = align / BITS_PER_UNIT;
2660 if (CONST_INT_P (len))
2661 ctz_len = MAX (ctz_len, (unsigned) wi::ctz (UINTVAL (len)));
2663 if (HOST_WIDE_INT_1U << ctz_len < (unsigned HOST_WIDE_INT) incr)
2664 incr = HOST_WIDE_INT_1U << ctz_len;
2666 while (incr > 1
2667 && !can_do_by_pieces (incr, align, COMPARE_BY_PIECES))
2668 incr >>= 1;
2670 rtx_code_label *cmp_label, *top_label, *ne_label, *res_label;
2671 rtx iter, x_addr, y_addr, tmp;
2672 machine_mode x_addr_mode = get_address_mode (x);
2673 machine_mode y_addr_mode = get_address_mode (y);
2674 machine_mode iter_mode;
2676 iter_mode = GET_MODE (len);
2677 if (iter_mode == VOIDmode)
2678 iter_mode = word_mode;
2680 rtx iter_init = const0_rtx;
2681 rtx_code iter_cond = LTU;
2682 rtx_code entry_cond = GEU;
2683 rtx iter_limit = len;
2684 rtx iter_incr = GEN_INT (incr);
2685 machine_mode cmp_mode;
2687 /* We can drop the loop back edge if we know there's exactly one
2688 iteration. */
2689 top_label = (!rtx_equal_p (len, iter_incr)
2690 ? gen_label_rtx ()
2691 : NULL);
2692 /* We need not test before entering the loop if len is known
2693 nonzero. ??? This could be even stricter, testing whether a
2694 nonconstant LEN could possibly be zero. */
2695 cmp_label = (!CONSTANT_P (len) || rtx_equal_p (len, iter_init)
2696 ? gen_label_rtx ()
2697 : NULL);
2698 ne_label = gen_label_rtx ();
2699 res_label = gen_label_rtx ();
2701 iter = gen_reg_rtx (iter_mode);
2702 emit_move_insn (iter, iter_init);
2704 scalar_int_mode int_cmp_mode
2705 = smallest_int_mode_for_size (incr * BITS_PER_UNIT);
2706 if (GET_MODE_BITSIZE (int_cmp_mode) != incr * BITS_PER_UNIT
2707 || !can_compare_p (NE, int_cmp_mode, ccp_jump))
2709 cmp_mode = BLKmode;
2710 gcc_checking_assert (incr != 1);
2712 else
2713 cmp_mode = int_cmp_mode;
2715 /* Save the base addresses. */
2716 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2717 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2718 do_pending_stack_adjust ();
2720 if (cmp_label)
2722 if (top_label)
2723 emit_jump (cmp_label);
2724 else
2725 emit_cmp_and_jump_insns (iter, iter_limit, entry_cond,
2726 NULL_RTX, iter_mode,
2727 true, cmp_label,
2728 profile_probability::guessed_always ()
2729 .apply_scale (1, 10));
2731 if (top_label)
2732 emit_label (top_label);
2734 /* Offset the base addresses by ITER. */
2735 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2736 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2738 if (x_addr_mode != y_addr_mode)
2739 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2740 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2742 x = change_address (x, cmp_mode, x_addr);
2743 y = change_address (y, cmp_mode, y_addr);
2745 /* Compare one block. */
2746 rtx part_res;
2747 if (cmp_mode == BLKmode)
2748 part_res = compare_by_pieces (x, y, incr, target, align, 0, 0);
2749 else
2750 part_res = expand_binop (cmp_mode, sub_optab, x, y, NULL_RTX,
2751 true, OPTAB_LIB_WIDEN);
2753 /* Stop if we found a difference. */
2754 emit_cmp_and_jump_insns (part_res, GEN_INT (0), NE, NULL_RTX,
2755 GET_MODE (part_res), true, ne_label,
2756 profile_probability::guessed_always ()
2757 .apply_scale (1, 10));
2759 /* Increment ITER. */
2760 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2761 true, OPTAB_LIB_WIDEN);
2762 if (tmp != iter)
2763 emit_move_insn (iter, tmp);
2765 if (cmp_label)
2766 emit_label (cmp_label);
2767 /* Loop until we reach the limit. */
2769 if (top_label)
2770 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2771 true, top_label,
2772 profile_probability::guessed_always ()
2773 .apply_scale (9, 10));
2775 /* We got to the end without differences, so the result is zero. */
2776 if (target == NULL_RTX
2777 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
2778 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
2780 emit_move_insn (target, const0_rtx);
2781 emit_jump (res_label);
2783 emit_label (ne_label);
2785 /* Return nonzero, or pinpoint the difference to return the expected
2786 result for non-equality tests. */
2787 if (equality_only)
2788 emit_move_insn (target, const1_rtx);
2789 else
2791 if (incr > UNITS_PER_WORD)
2792 /* ??? Re-compare the block found to be different one word at a
2793 time. */
2794 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2795 target, equality_only,
2796 BITS_PER_WORD, 0);
2797 else if (incr > 1)
2798 /* ??? Re-compare the block found to be different one byte at a
2799 time. We could do better using part_res, and being careful
2800 about endianness. */
2801 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2802 target, equality_only,
2803 BITS_PER_UNIT, 0);
2804 else if (known_gt (GET_MODE_BITSIZE (GET_MODE (target)),
2805 GET_MODE_BITSIZE (cmp_mode)))
2806 part_res = expand_binop (GET_MODE (target), sub_optab, x, y, target,
2807 true, OPTAB_LIB_WIDEN);
2808 else
2810 /* In the odd chance target is QImode, we can't count on
2811 widening subtract to capture the result of the unsigned
2812 compares. */
2813 rtx_code_label *ltu_label;
2814 ltu_label = gen_label_rtx ();
2815 emit_cmp_and_jump_insns (x, y, LTU, NULL_RTX,
2816 cmp_mode, true, ltu_label,
2817 profile_probability::guessed_always ()
2818 .apply_scale (5, 10));
2820 emit_move_insn (target, const1_rtx);
2821 emit_jump (res_label);
2823 emit_label (ltu_label);
2824 emit_move_insn (target, constm1_rtx);
2825 part_res = target;
2828 if (target != part_res)
2829 convert_move (target, part_res, false);
2832 emit_label (res_label);
2834 return target;
2838 /* Copy all or part of a value X into registers starting at REGNO.
2839 The number of registers to be filled is NREGS. */
2841 void
2842 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2844 if (nregs == 0)
2845 return;
2847 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2848 x = validize_mem (force_const_mem (mode, x));
2850 /* See if the machine can do this with a load multiple insn. */
2851 if (targetm.have_load_multiple ())
2853 rtx_insn *last = get_last_insn ();
2854 rtx first = gen_rtx_REG (word_mode, regno);
2855 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2856 GEN_INT (nregs)))
2858 emit_insn (pat);
2859 return;
2861 else
2862 delete_insns_since (last);
2865 for (int i = 0; i < nregs; i++)
2866 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2867 operand_subword_force (x, i, mode));
2870 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2871 The number of registers to be filled is NREGS. */
2873 void
2874 move_block_from_reg (int regno, rtx x, int nregs)
2876 if (nregs == 0)
2877 return;
2879 /* See if the machine can do this with a store multiple insn. */
2880 if (targetm.have_store_multiple ())
2882 rtx_insn *last = get_last_insn ();
2883 rtx first = gen_rtx_REG (word_mode, regno);
2884 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2885 GEN_INT (nregs)))
2887 emit_insn (pat);
2888 return;
2890 else
2891 delete_insns_since (last);
2894 for (int i = 0; i < nregs; i++)
2896 rtx tem = operand_subword (x, i, 1, BLKmode);
2898 gcc_assert (tem);
2900 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2904 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2905 ORIG, where ORIG is a non-consecutive group of registers represented by
2906 a PARALLEL. The clone is identical to the original except in that the
2907 original set of registers is replaced by a new set of pseudo registers.
2908 The new set has the same modes as the original set. */
2911 gen_group_rtx (rtx orig)
2913 int i, length;
2914 rtx *tmps;
2916 gcc_assert (GET_CODE (orig) == PARALLEL);
2918 length = XVECLEN (orig, 0);
2919 tmps = XALLOCAVEC (rtx, length);
2921 /* Skip a NULL entry in first slot. */
2922 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2924 if (i)
2925 tmps[0] = 0;
2927 for (; i < length; i++)
2929 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2930 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2932 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2935 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2938 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2939 except that values are placed in TMPS[i], and must later be moved
2940 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2942 static void
2943 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2944 poly_int64 ssize)
2946 rtx src;
2947 int start, i;
2948 machine_mode m = GET_MODE (orig_src);
2950 gcc_assert (GET_CODE (dst) == PARALLEL);
2952 if (m != VOIDmode
2953 && !SCALAR_INT_MODE_P (m)
2954 && !MEM_P (orig_src)
2955 && GET_CODE (orig_src) != CONCAT)
2957 scalar_int_mode imode;
2958 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2960 src = gen_reg_rtx (imode);
2961 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2963 else
2965 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2966 emit_move_insn (src, orig_src);
2968 emit_group_load_1 (tmps, dst, src, type, ssize);
2969 return;
2972 /* Check for a NULL entry, used to indicate that the parameter goes
2973 both on the stack and in registers. */
2974 if (XEXP (XVECEXP (dst, 0, 0), 0))
2975 start = 0;
2976 else
2977 start = 1;
2979 /* Process the pieces. */
2980 for (i = start; i < XVECLEN (dst, 0); i++)
2982 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2983 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2984 poly_int64 bytelen = GET_MODE_SIZE (mode);
2985 poly_int64 shift = 0;
2987 /* Handle trailing fragments that run over the size of the struct.
2988 It's the target's responsibility to make sure that the fragment
2989 cannot be strictly smaller in some cases and strictly larger
2990 in others. */
2991 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2992 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2994 /* Arrange to shift the fragment to where it belongs.
2995 extract_bit_field loads to the lsb of the reg. */
2996 if (
2997 #ifdef BLOCK_REG_PADDING
2998 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2999 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3000 #else
3001 BYTES_BIG_ENDIAN
3002 #endif
3004 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3005 bytelen = ssize - bytepos;
3006 gcc_assert (maybe_gt (bytelen, 0));
3009 /* If we won't be loading directly from memory, protect the real source
3010 from strange tricks we might play; but make sure that the source can
3011 be loaded directly into the destination. */
3012 src = orig_src;
3013 if (!MEM_P (orig_src)
3014 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src))
3015 && !CONSTANT_P (orig_src))
3017 gcc_assert (GET_MODE (orig_src) != VOIDmode);
3018 src = force_reg (GET_MODE (orig_src), orig_src);
3021 /* Optimize the access just a bit. */
3022 if (MEM_P (src)
3023 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
3024 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
3025 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
3026 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3028 tmps[i] = gen_reg_rtx (mode);
3029 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
3031 else if (COMPLEX_MODE_P (mode)
3032 && GET_MODE (src) == mode
3033 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3034 /* Let emit_move_complex do the bulk of the work. */
3035 tmps[i] = src;
3036 else if (GET_CODE (src) == CONCAT)
3038 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
3039 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
3040 unsigned int elt;
3041 poly_int64 subpos;
3043 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
3044 && known_le (subpos + bytelen, slen0))
3046 /* The following assumes that the concatenated objects all
3047 have the same size. In this case, a simple calculation
3048 can be used to determine the object and the bit field
3049 to be extracted. */
3050 tmps[i] = XEXP (src, elt);
3051 if (maybe_ne (subpos, 0)
3052 || maybe_ne (subpos + bytelen, slen0)
3053 || (!CONSTANT_P (tmps[i])
3054 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
3055 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
3056 subpos * BITS_PER_UNIT,
3057 1, NULL_RTX, mode, mode, false,
3058 NULL);
3060 else
3062 rtx mem;
3064 gcc_assert (known_eq (bytepos, 0));
3065 mem = assign_stack_temp (GET_MODE (src), slen);
3066 emit_move_insn (mem, src);
3067 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
3068 0, 1, NULL_RTX, mode, mode, false,
3069 NULL);
3072 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
3073 && XVECLEN (dst, 0) > 1)
3074 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
3075 else if (CONSTANT_P (src))
3077 if (known_eq (bytelen, ssize))
3078 tmps[i] = src;
3079 else
3081 rtx first, second;
3083 /* TODO: const_wide_int can have sizes other than this... */
3084 gcc_assert (known_eq (2 * bytelen, ssize));
3085 split_double (src, &first, &second);
3086 if (i)
3087 tmps[i] = second;
3088 else
3089 tmps[i] = first;
3092 else if (REG_P (src) && GET_MODE (src) == mode)
3093 tmps[i] = src;
3094 else
3095 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
3096 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
3097 mode, mode, false, NULL);
3099 if (maybe_ne (shift, 0))
3100 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
3101 shift, tmps[i], 0);
3105 /* Emit code to move a block SRC of type TYPE to a block DST,
3106 where DST is non-consecutive registers represented by a PARALLEL.
3107 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
3108 if not known. */
3110 void
3111 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
3113 rtx *tmps;
3114 int i;
3116 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
3117 emit_group_load_1 (tmps, dst, src, type, ssize);
3119 /* Copy the extracted pieces into the proper (probable) hard regs. */
3120 for (i = 0; i < XVECLEN (dst, 0); i++)
3122 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
3123 if (d == NULL)
3124 continue;
3125 emit_move_insn (d, tmps[i]);
3129 /* Similar, but load SRC into new pseudos in a format that looks like
3130 PARALLEL. This can later be fed to emit_group_move to get things
3131 in the right place. */
3134 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
3136 rtvec vec;
3137 int i;
3139 vec = rtvec_alloc (XVECLEN (parallel, 0));
3140 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
3142 /* Convert the vector to look just like the original PARALLEL, except
3143 with the computed values. */
3144 for (i = 0; i < XVECLEN (parallel, 0); i++)
3146 rtx e = XVECEXP (parallel, 0, i);
3147 rtx d = XEXP (e, 0);
3149 if (d)
3151 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
3152 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
3154 RTVEC_ELT (vec, i) = e;
3157 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
3160 /* Emit code to move a block SRC to block DST, where SRC and DST are
3161 non-consecutive groups of registers, each represented by a PARALLEL. */
3163 void
3164 emit_group_move (rtx dst, rtx src)
3166 int i;
3168 gcc_assert (GET_CODE (src) == PARALLEL
3169 && GET_CODE (dst) == PARALLEL
3170 && XVECLEN (src, 0) == XVECLEN (dst, 0));
3172 /* Skip first entry if NULL. */
3173 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
3174 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
3175 XEXP (XVECEXP (src, 0, i), 0));
3178 /* Move a group of registers represented by a PARALLEL into pseudos. */
3181 emit_group_move_into_temps (rtx src)
3183 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
3184 int i;
3186 for (i = 0; i < XVECLEN (src, 0); i++)
3188 rtx e = XVECEXP (src, 0, i);
3189 rtx d = XEXP (e, 0);
3191 if (d)
3192 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
3193 RTVEC_ELT (vec, i) = e;
3196 return gen_rtx_PARALLEL (GET_MODE (src), vec);
3199 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
3200 where SRC is non-consecutive registers represented by a PARALLEL.
3201 SSIZE represents the total size of block ORIG_DST, or -1 if not
3202 known. */
3204 void
3205 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
3206 poly_int64 ssize)
3208 rtx *tmps, dst;
3209 int start, finish, i;
3210 machine_mode m = GET_MODE (orig_dst);
3212 gcc_assert (GET_CODE (src) == PARALLEL);
3214 if (!SCALAR_INT_MODE_P (m)
3215 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
3217 scalar_int_mode imode;
3218 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
3220 dst = gen_reg_rtx (imode);
3221 emit_group_store (dst, src, type, ssize);
3222 dst = gen_lowpart (GET_MODE (orig_dst), dst);
3224 else
3226 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
3227 emit_group_store (dst, src, type, ssize);
3229 emit_move_insn (orig_dst, dst);
3230 return;
3233 /* Check for a NULL entry, used to indicate that the parameter goes
3234 both on the stack and in registers. */
3235 if (XEXP (XVECEXP (src, 0, 0), 0))
3236 start = 0;
3237 else
3238 start = 1;
3239 finish = XVECLEN (src, 0);
3241 tmps = XALLOCAVEC (rtx, finish);
3243 /* Copy the (probable) hard regs into pseudos. */
3244 for (i = start; i < finish; i++)
3246 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
3247 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
3249 tmps[i] = gen_reg_rtx (GET_MODE (reg));
3250 emit_move_insn (tmps[i], reg);
3252 else
3253 tmps[i] = reg;
3256 /* If we won't be storing directly into memory, protect the real destination
3257 from strange tricks we might play. */
3258 dst = orig_dst;
3259 if (GET_CODE (dst) == PARALLEL)
3261 rtx temp;
3263 /* We can get a PARALLEL dst if there is a conditional expression in
3264 a return statement. In that case, the dst and src are the same,
3265 so no action is necessary. */
3266 if (rtx_equal_p (dst, src))
3267 return;
3269 /* It is unclear if we can ever reach here, but we may as well handle
3270 it. Allocate a temporary, and split this into a store/load to/from
3271 the temporary. */
3272 temp = assign_stack_temp (GET_MODE (dst), ssize);
3273 emit_group_store (temp, src, type, ssize);
3274 emit_group_load (dst, temp, type, ssize);
3275 return;
3277 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
3279 machine_mode outer = GET_MODE (dst);
3280 machine_mode inner;
3281 poly_int64 bytepos;
3282 bool done = false;
3283 rtx temp;
3285 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
3286 dst = gen_reg_rtx (outer);
3288 /* Make life a bit easier for combine: if the first element of the
3289 vector is the low part of the destination mode, use a paradoxical
3290 subreg to initialize the destination. */
3291 if (start < finish)
3293 inner = GET_MODE (tmps[start]);
3294 bytepos = subreg_lowpart_offset (inner, outer);
3295 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
3296 bytepos))
3298 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
3299 if (temp)
3301 emit_move_insn (dst, temp);
3302 done = true;
3303 start++;
3308 /* If the first element wasn't the low part, try the last. */
3309 if (!done
3310 && start < finish - 1)
3312 inner = GET_MODE (tmps[finish - 1]);
3313 bytepos = subreg_lowpart_offset (inner, outer);
3314 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
3315 finish - 1), 1)),
3316 bytepos))
3318 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
3319 if (temp)
3321 emit_move_insn (dst, temp);
3322 done = true;
3323 finish--;
3328 /* Otherwise, simply initialize the result to zero. */
3329 if (!done)
3330 emit_move_insn (dst, CONST0_RTX (outer));
3333 /* Process the pieces. */
3334 for (i = start; i < finish; i++)
3336 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
3337 machine_mode mode = GET_MODE (tmps[i]);
3338 poly_int64 bytelen = GET_MODE_SIZE (mode);
3339 poly_uint64 adj_bytelen;
3340 rtx dest = dst;
3342 /* Handle trailing fragments that run over the size of the struct.
3343 It's the target's responsibility to make sure that the fragment
3344 cannot be strictly smaller in some cases and strictly larger
3345 in others. */
3346 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
3347 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3348 adj_bytelen = ssize - bytepos;
3349 else
3350 adj_bytelen = bytelen;
3352 /* Deal with destination CONCATs by either storing into one of the parts
3353 or doing a copy after storing into a register or stack temporary. */
3354 if (GET_CODE (dst) == CONCAT)
3356 if (known_le (bytepos + adj_bytelen,
3357 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3358 dest = XEXP (dst, 0);
3360 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3362 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
3363 dest = XEXP (dst, 1);
3366 else
3368 machine_mode dest_mode = GET_MODE (dest);
3369 machine_mode tmp_mode = GET_MODE (tmps[i]);
3370 scalar_int_mode dest_imode;
3372 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
3374 /* If the source is a single scalar integer register, and the
3375 destination has a complex mode for which a same-sized integer
3376 mode exists, then we can take the left-justified part of the
3377 source in the complex mode. */
3378 if (finish == start + 1
3379 && REG_P (tmps[i])
3380 && SCALAR_INT_MODE_P (tmp_mode)
3381 && COMPLEX_MODE_P (dest_mode)
3382 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3384 const scalar_int_mode tmp_imode
3385 = as_a <scalar_int_mode> (tmp_mode);
3387 if (GET_MODE_BITSIZE (dest_imode)
3388 < GET_MODE_BITSIZE (tmp_imode))
3390 dest = gen_reg_rtx (dest_imode);
3391 if (BYTES_BIG_ENDIAN)
3392 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3393 GET_MODE_BITSIZE (tmp_imode)
3394 - GET_MODE_BITSIZE (dest_imode),
3395 NULL_RTX, 1);
3396 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3397 dst = gen_lowpart (dest_mode, dest);
3399 else
3400 dst = gen_lowpart (dest_mode, tmps[i]);
3403 /* Otherwise spill the source onto the stack using the more
3404 aligned of the two modes. */
3405 else if (GET_MODE_ALIGNMENT (dest_mode)
3406 >= GET_MODE_ALIGNMENT (tmp_mode))
3408 dest = assign_stack_temp (dest_mode,
3409 GET_MODE_SIZE (dest_mode));
3410 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3411 tmps[i]);
3412 dst = dest;
3415 else
3417 dest = assign_stack_temp (tmp_mode,
3418 GET_MODE_SIZE (tmp_mode));
3419 emit_move_insn (dest, tmps[i]);
3420 dst = adjust_address (dest, dest_mode, bytepos);
3423 break;
3427 /* Handle trailing fragments that run over the size of the struct. */
3428 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3430 /* store_bit_field always takes its value from the lsb.
3431 Move the fragment to the lsb if it's not already there. */
3432 if (
3433 #ifdef BLOCK_REG_PADDING
3434 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3435 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3436 #else
3437 BYTES_BIG_ENDIAN
3438 #endif
3441 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3442 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3443 shift, tmps[i], 0);
3446 /* Make sure not to write past the end of the struct. */
3447 store_bit_field (dest,
3448 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3449 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3450 VOIDmode, tmps[i], false, false);
3453 /* Optimize the access just a bit. */
3454 else if (MEM_P (dest)
3455 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3456 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3457 && multiple_p (bytepos * BITS_PER_UNIT,
3458 GET_MODE_ALIGNMENT (mode))
3459 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3460 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3462 else
3463 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3464 0, 0, mode, tmps[i], false, false);
3467 /* Copy from the pseudo into the (probable) hard reg. */
3468 if (orig_dst != dst)
3469 emit_move_insn (orig_dst, dst);
3472 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3473 of the value stored in X. */
3476 maybe_emit_group_store (rtx x, tree type)
3478 machine_mode mode = TYPE_MODE (type);
3479 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3480 if (GET_CODE (x) == PARALLEL)
3482 rtx result = gen_reg_rtx (mode);
3483 emit_group_store (result, x, type, int_size_in_bytes (type));
3484 return result;
3486 return x;
3489 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3491 This is used on targets that return BLKmode values in registers. */
3493 static void
3494 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3496 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3497 rtx src = NULL, dst = NULL;
3498 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3499 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3500 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3501 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3502 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3503 fixed_size_mode copy_mode;
3505 /* BLKmode registers created in the back-end shouldn't have survived. */
3506 gcc_assert (mode != BLKmode);
3508 /* If the structure doesn't take up a whole number of words, see whether
3509 SRCREG is padded on the left or on the right. If it's on the left,
3510 set PADDING_CORRECTION to the number of bits to skip.
3512 In most ABIs, the structure will be returned at the least end of
3513 the register, which translates to right padding on little-endian
3514 targets and left padding on big-endian targets. The opposite
3515 holds if the structure is returned at the most significant
3516 end of the register. */
3517 if (bytes % UNITS_PER_WORD != 0
3518 && (targetm.calls.return_in_msb (type)
3519 ? !BYTES_BIG_ENDIAN
3520 : BYTES_BIG_ENDIAN))
3521 padding_correction
3522 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3524 /* We can use a single move if we have an exact mode for the size. */
3525 else if (MEM_P (target)
3526 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3527 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3528 && bytes == GET_MODE_SIZE (mode))
3530 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3531 return;
3534 /* And if we additionally have the same mode for a register. */
3535 else if (REG_P (target)
3536 && GET_MODE (target) == mode
3537 && bytes == GET_MODE_SIZE (mode))
3539 emit_move_insn (target, srcreg);
3540 return;
3543 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3544 into a new pseudo which is a full word. */
3545 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3547 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3548 mode = word_mode;
3551 /* Copy the structure BITSIZE bits at a time. If the target lives in
3552 memory, take care of not reading/writing past its end by selecting
3553 a copy mode suited to BITSIZE. This should always be possible given
3554 how it is computed.
3556 If the target lives in register, make sure not to select a copy mode
3557 larger than the mode of the register.
3559 We could probably emit more efficient code for machines which do not use
3560 strict alignment, but it doesn't seem worth the effort at the current
3561 time. */
3563 copy_mode = word_mode;
3564 if (MEM_P (target))
3566 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3567 if (mem_mode.exists ())
3568 copy_mode = mem_mode.require ();
3570 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3571 copy_mode = tmode;
3573 for (bitpos = 0, xbitpos = padding_correction;
3574 bitpos < bytes * BITS_PER_UNIT;
3575 bitpos += bitsize, xbitpos += bitsize)
3577 /* We need a new source operand each time xbitpos is on a
3578 word boundary and when xbitpos == padding_correction
3579 (the first time through). */
3580 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3581 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3583 /* We need a new destination operand each time bitpos is on
3584 a word boundary. */
3585 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3586 dst = target;
3587 else if (bitpos % BITS_PER_WORD == 0)
3588 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3590 /* Use xbitpos for the source extraction (right justified) and
3591 bitpos for the destination store (left justified). */
3592 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3593 extract_bit_field (src, bitsize,
3594 xbitpos % BITS_PER_WORD, 1,
3595 NULL_RTX, copy_mode, copy_mode,
3596 false, NULL),
3597 false, false);
3601 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3602 register if it contains any data, otherwise return null.
3604 This is used on targets that return BLKmode values in registers. */
3607 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3609 int i, n_regs;
3610 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3611 unsigned int bitsize;
3612 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3613 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3614 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3615 fixed_size_mode dst_mode;
3616 scalar_int_mode min_mode;
3618 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3620 x = expand_normal (src);
3622 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3623 if (bytes == 0)
3624 return NULL_RTX;
3626 /* If the structure doesn't take up a whole number of words, see
3627 whether the register value should be padded on the left or on
3628 the right. Set PADDING_CORRECTION to the number of padding
3629 bits needed on the left side.
3631 In most ABIs, the structure will be returned at the least end of
3632 the register, which translates to right padding on little-endian
3633 targets and left padding on big-endian targets. The opposite
3634 holds if the structure is returned at the most significant
3635 end of the register. */
3636 if (bytes % UNITS_PER_WORD != 0
3637 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3638 ? !BYTES_BIG_ENDIAN
3639 : BYTES_BIG_ENDIAN))
3640 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3641 * BITS_PER_UNIT));
3643 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3644 dst_words = XALLOCAVEC (rtx, n_regs);
3645 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3646 min_mode = smallest_int_mode_for_size (bitsize);
3648 /* Copy the structure BITSIZE bits at a time. */
3649 for (bitpos = 0, xbitpos = padding_correction;
3650 bitpos < bytes * BITS_PER_UNIT;
3651 bitpos += bitsize, xbitpos += bitsize)
3653 /* We need a new destination pseudo each time xbitpos is
3654 on a word boundary and when xbitpos == padding_correction
3655 (the first time through). */
3656 if (xbitpos % BITS_PER_WORD == 0
3657 || xbitpos == padding_correction)
3659 /* Generate an appropriate register. */
3660 dst_word = gen_reg_rtx (word_mode);
3661 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3663 /* Clear the destination before we move anything into it. */
3664 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3667 /* Find the largest integer mode that can be used to copy all or as
3668 many bits as possible of the structure if the target supports larger
3669 copies. There are too many corner cases here w.r.t to alignments on
3670 the read/writes. So if there is any padding just use single byte
3671 operations. */
3672 opt_scalar_int_mode mode_iter;
3673 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3675 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3677 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3678 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3679 && msize <= BITS_PER_WORD)
3680 bitsize = msize;
3681 else
3682 break;
3686 /* We need a new source operand each time bitpos is on a word
3687 boundary. */
3688 if (bitpos % BITS_PER_WORD == 0)
3689 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3691 /* Use bitpos for the source extraction (left justified) and
3692 xbitpos for the destination store (right justified). */
3693 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3694 0, 0, word_mode,
3695 extract_bit_field (src_word, bitsize,
3696 bitpos % BITS_PER_WORD, 1,
3697 NULL_RTX, word_mode, word_mode,
3698 false, NULL),
3699 false, false);
3702 if (mode == BLKmode)
3704 /* Find the smallest integer mode large enough to hold the
3705 entire structure. */
3706 opt_scalar_int_mode mode_iter;
3707 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3708 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3709 break;
3711 /* A suitable mode should have been found. */
3712 mode = mode_iter.require ();
3715 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3716 dst_mode = word_mode;
3717 else
3718 dst_mode = mode;
3719 dst = gen_reg_rtx (dst_mode);
3721 for (i = 0; i < n_regs; i++)
3722 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3724 if (mode != dst_mode)
3725 dst = gen_lowpart (mode, dst);
3727 return dst;
3730 /* Add a USE expression for REG to the (possibly empty) list pointed
3731 to by CALL_FUSAGE. REG must denote a hard register. */
3733 void
3734 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3736 gcc_assert (REG_P (reg));
3738 if (!HARD_REGISTER_P (reg))
3739 return;
3741 *call_fusage
3742 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3745 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3746 to by CALL_FUSAGE. REG must denote a hard register. */
3748 void
3749 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3751 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3753 *call_fusage
3754 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3757 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3758 starting at REGNO. All of these registers must be hard registers. */
3760 void
3761 use_regs (rtx *call_fusage, int regno, int nregs)
3763 int i;
3765 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3767 for (i = 0; i < nregs; i++)
3768 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3771 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3772 PARALLEL REGS. This is for calls that pass values in multiple
3773 non-contiguous locations. The Irix 6 ABI has examples of this. */
3775 void
3776 use_group_regs (rtx *call_fusage, rtx regs)
3778 int i;
3780 for (i = 0; i < XVECLEN (regs, 0); i++)
3782 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3784 /* A NULL entry means the parameter goes both on the stack and in
3785 registers. This can also be a MEM for targets that pass values
3786 partially on the stack and partially in registers. */
3787 if (reg != 0 && REG_P (reg))
3788 use_reg (call_fusage, reg);
3792 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3793 assigment and the code of the expresion on the RHS is CODE. Return
3794 NULL otherwise. */
3796 static gimple *
3797 get_def_for_expr (tree name, enum tree_code code)
3799 gimple *def_stmt;
3801 if (TREE_CODE (name) != SSA_NAME)
3802 return NULL;
3804 def_stmt = get_gimple_for_ssa_name (name);
3805 if (!def_stmt
3806 || gimple_assign_rhs_code (def_stmt) != code)
3807 return NULL;
3809 return def_stmt;
3812 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3813 assigment and the class of the expresion on the RHS is CLASS. Return
3814 NULL otherwise. */
3816 static gimple *
3817 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3819 gimple *def_stmt;
3821 if (TREE_CODE (name) != SSA_NAME)
3822 return NULL;
3824 def_stmt = get_gimple_for_ssa_name (name);
3825 if (!def_stmt
3826 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3827 return NULL;
3829 return def_stmt;
3832 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3833 its length in bytes. */
3836 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3837 unsigned int expected_align, HOST_WIDE_INT expected_size,
3838 unsigned HOST_WIDE_INT min_size,
3839 unsigned HOST_WIDE_INT max_size,
3840 unsigned HOST_WIDE_INT probable_max_size,
3841 unsigned ctz_size)
3843 machine_mode mode = GET_MODE (object);
3844 unsigned int align;
3846 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3848 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3849 just move a zero. Otherwise, do this a piece at a time. */
3850 poly_int64 size_val;
3851 if (mode != BLKmode
3852 && poly_int_rtx_p (size, &size_val)
3853 && known_eq (size_val, GET_MODE_SIZE (mode)))
3855 rtx zero = CONST0_RTX (mode);
3856 if (zero != NULL)
3858 emit_move_insn (object, zero);
3859 return NULL;
3862 if (COMPLEX_MODE_P (mode))
3864 zero = CONST0_RTX (GET_MODE_INNER (mode));
3865 if (zero != NULL)
3867 write_complex_part (object, zero, 0, true);
3868 write_complex_part (object, zero, 1, false);
3869 return NULL;
3874 if (size == const0_rtx)
3875 return NULL;
3877 align = MEM_ALIGN (object);
3879 if (CONST_INT_P (size)
3880 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3881 CLEAR_BY_PIECES,
3882 optimize_insn_for_speed_p ()))
3883 clear_by_pieces (object, INTVAL (size), align);
3884 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3885 expected_align, expected_size,
3886 min_size, max_size, probable_max_size))
3888 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3889 min_size, max_size,
3890 NULL_RTX, 0, align))
3892 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3893 return set_storage_via_libcall (object, size, const0_rtx,
3894 method == BLOCK_OP_TAILCALL);
3895 else
3896 gcc_unreachable ();
3898 return NULL;
3902 clear_storage (rtx object, rtx size, enum block_op_methods method)
3904 unsigned HOST_WIDE_INT max, min = 0;
3905 if (GET_CODE (size) == CONST_INT)
3906 min = max = UINTVAL (size);
3907 else
3908 max = GET_MODE_MASK (GET_MODE (size));
3909 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3913 /* A subroutine of clear_storage. Expand a call to memset.
3914 Return the return value of memset, 0 otherwise. */
3917 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3919 tree call_expr, fn, object_tree, size_tree, val_tree;
3920 machine_mode size_mode;
3922 object = copy_addr_to_reg (XEXP (object, 0));
3923 object_tree = make_tree (ptr_type_node, object);
3925 if (!CONST_INT_P (val))
3926 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3927 val_tree = make_tree (integer_type_node, val);
3929 size_mode = TYPE_MODE (sizetype);
3930 size = convert_to_mode (size_mode, size, 1);
3931 size = copy_to_mode_reg (size_mode, size);
3932 size_tree = make_tree (sizetype, size);
3934 /* It is incorrect to use the libcall calling conventions for calls to
3935 memset because it can be provided by the user. */
3936 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3937 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3938 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3940 return expand_call (call_expr, NULL_RTX, false);
3943 /* Expand a setmem pattern; return true if successful. */
3945 bool
3946 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3947 unsigned int expected_align, HOST_WIDE_INT expected_size,
3948 unsigned HOST_WIDE_INT min_size,
3949 unsigned HOST_WIDE_INT max_size,
3950 unsigned HOST_WIDE_INT probable_max_size)
3952 /* Try the most limited insn first, because there's no point
3953 including more than one in the machine description unless
3954 the more limited one has some advantage. */
3956 if (expected_align < align)
3957 expected_align = align;
3958 if (expected_size != -1)
3960 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3961 expected_size = max_size;
3962 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3963 expected_size = min_size;
3966 opt_scalar_int_mode mode_iter;
3967 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3969 scalar_int_mode mode = mode_iter.require ();
3970 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3972 if (code != CODE_FOR_nothing
3973 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3974 here because if SIZE is less than the mode mask, as it is
3975 returned by the macro, it will definitely be less than the
3976 actual mode mask. Since SIZE is within the Pmode address
3977 space, we limit MODE to Pmode. */
3978 && ((CONST_INT_P (size)
3979 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3980 <= (GET_MODE_MASK (mode) >> 1)))
3981 || max_size <= (GET_MODE_MASK (mode) >> 1)
3982 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3984 class expand_operand ops[9];
3985 unsigned int nops;
3987 nops = insn_data[(int) code].n_generator_args;
3988 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3990 create_fixed_operand (&ops[0], object);
3991 /* The check above guarantees that this size conversion is valid. */
3992 create_convert_operand_to (&ops[1], size, mode, true);
3993 create_convert_operand_from (&ops[2], val, byte_mode, true);
3994 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3995 if (nops >= 6)
3997 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3998 create_integer_operand (&ops[5], expected_size);
4000 if (nops >= 8)
4002 create_integer_operand (&ops[6], min_size);
4003 /* If we cannot represent the maximal size,
4004 make parameter NULL. */
4005 if ((HOST_WIDE_INT) max_size != -1)
4006 create_integer_operand (&ops[7], max_size);
4007 else
4008 create_fixed_operand (&ops[7], NULL);
4010 if (nops == 9)
4012 /* If we cannot represent the maximal size,
4013 make parameter NULL. */
4014 if ((HOST_WIDE_INT) probable_max_size != -1)
4015 create_integer_operand (&ops[8], probable_max_size);
4016 else
4017 create_fixed_operand (&ops[8], NULL);
4019 if (maybe_expand_insn (code, nops, ops))
4020 return true;
4024 return false;
4028 /* Write to one of the components of the complex value CPLX. Write VAL to
4029 the real part if IMAG_P is false, and the imaginary part if its true.
4030 If UNDEFINED_P then the value in CPLX is currently undefined. */
4032 void
4033 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
4035 machine_mode cmode;
4036 scalar_mode imode;
4037 unsigned ibitsize;
4039 if (GET_CODE (cplx) == CONCAT)
4041 emit_move_insn (XEXP (cplx, imag_p), val);
4042 return;
4045 cmode = GET_MODE (cplx);
4046 imode = GET_MODE_INNER (cmode);
4047 ibitsize = GET_MODE_BITSIZE (imode);
4049 /* For MEMs simplify_gen_subreg may generate an invalid new address
4050 because, e.g., the original address is considered mode-dependent
4051 by the target, which restricts simplify_subreg from invoking
4052 adjust_address_nv. Instead of preparing fallback support for an
4053 invalid address, we call adjust_address_nv directly. */
4054 if (MEM_P (cplx))
4056 emit_move_insn (adjust_address_nv (cplx, imode,
4057 imag_p ? GET_MODE_SIZE (imode) : 0),
4058 val);
4059 return;
4062 /* If the sub-object is at least word sized, then we know that subregging
4063 will work. This special case is important, since store_bit_field
4064 wants to operate on integer modes, and there's rarely an OImode to
4065 correspond to TCmode. */
4066 if (ibitsize >= BITS_PER_WORD
4067 /* For hard regs we have exact predicates. Assume we can split
4068 the original object if it spans an even number of hard regs.
4069 This special case is important for SCmode on 64-bit platforms
4070 where the natural size of floating-point regs is 32-bit. */
4071 || (REG_P (cplx)
4072 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4073 && REG_NREGS (cplx) % 2 == 0))
4075 rtx part = simplify_gen_subreg (imode, cplx, cmode,
4076 imag_p ? GET_MODE_SIZE (imode) : 0);
4077 if (part)
4079 emit_move_insn (part, val);
4080 return;
4082 else
4083 /* simplify_gen_subreg may fail for sub-word MEMs. */
4084 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4087 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
4088 false, undefined_p);
4091 /* Extract one of the components of the complex value CPLX. Extract the
4092 real part if IMAG_P is false, and the imaginary part if it's true. */
4095 read_complex_part (rtx cplx, bool imag_p)
4097 machine_mode cmode;
4098 scalar_mode imode;
4099 unsigned ibitsize;
4101 if (GET_CODE (cplx) == CONCAT)
4102 return XEXP (cplx, imag_p);
4104 cmode = GET_MODE (cplx);
4105 imode = GET_MODE_INNER (cmode);
4106 ibitsize = GET_MODE_BITSIZE (imode);
4108 /* Special case reads from complex constants that got spilled to memory. */
4109 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
4111 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
4112 if (decl && TREE_CODE (decl) == COMPLEX_CST)
4114 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
4115 if (CONSTANT_CLASS_P (part))
4116 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
4120 /* For MEMs simplify_gen_subreg may generate an invalid new address
4121 because, e.g., the original address is considered mode-dependent
4122 by the target, which restricts simplify_subreg from invoking
4123 adjust_address_nv. Instead of preparing fallback support for an
4124 invalid address, we call adjust_address_nv directly. */
4125 if (MEM_P (cplx))
4126 return adjust_address_nv (cplx, imode,
4127 imag_p ? GET_MODE_SIZE (imode) : 0);
4129 /* If the sub-object is at least word sized, then we know that subregging
4130 will work. This special case is important, since extract_bit_field
4131 wants to operate on integer modes, and there's rarely an OImode to
4132 correspond to TCmode. */
4133 if (ibitsize >= BITS_PER_WORD
4134 /* For hard regs we have exact predicates. Assume we can split
4135 the original object if it spans an even number of hard regs.
4136 This special case is important for SCmode on 64-bit platforms
4137 where the natural size of floating-point regs is 32-bit. */
4138 || (REG_P (cplx)
4139 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4140 && REG_NREGS (cplx) % 2 == 0))
4142 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
4143 imag_p ? GET_MODE_SIZE (imode) : 0);
4144 if (ret)
4145 return ret;
4146 else
4147 /* simplify_gen_subreg may fail for sub-word MEMs. */
4148 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4151 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
4152 true, NULL_RTX, imode, imode, false, NULL);
4155 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
4156 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
4157 represented in NEW_MODE. If FORCE is true, this will never happen, as
4158 we'll force-create a SUBREG if needed. */
4160 static rtx
4161 emit_move_change_mode (machine_mode new_mode,
4162 machine_mode old_mode, rtx x, bool force)
4164 rtx ret;
4166 if (push_operand (x, GET_MODE (x)))
4168 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
4169 MEM_COPY_ATTRIBUTES (ret, x);
4171 else if (MEM_P (x))
4173 /* We don't have to worry about changing the address since the
4174 size in bytes is supposed to be the same. */
4175 if (reload_in_progress)
4177 /* Copy the MEM to change the mode and move any
4178 substitutions from the old MEM to the new one. */
4179 ret = adjust_address_nv (x, new_mode, 0);
4180 copy_replacements (x, ret);
4182 else
4183 ret = adjust_address (x, new_mode, 0);
4185 else
4187 /* Note that we do want simplify_subreg's behavior of validating
4188 that the new mode is ok for a hard register. If we were to use
4189 simplify_gen_subreg, we would create the subreg, but would
4190 probably run into the target not being able to implement it. */
4191 /* Except, of course, when FORCE is true, when this is exactly what
4192 we want. Which is needed for CCmodes on some targets. */
4193 if (force)
4194 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
4195 else
4196 ret = simplify_subreg (new_mode, x, old_mode, 0);
4199 return ret;
4202 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
4203 an integer mode of the same size as MODE. Returns the instruction
4204 emitted, or NULL if such a move could not be generated. */
4206 static rtx_insn *
4207 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
4209 scalar_int_mode imode;
4210 enum insn_code code;
4212 /* There must exist a mode of the exact size we require. */
4213 if (!int_mode_for_mode (mode).exists (&imode))
4214 return NULL;
4216 /* The target must support moves in this mode. */
4217 code = optab_handler (mov_optab, imode);
4218 if (code == CODE_FOR_nothing)
4219 return NULL;
4221 x = emit_move_change_mode (imode, mode, x, force);
4222 if (x == NULL_RTX)
4223 return NULL;
4224 y = emit_move_change_mode (imode, mode, y, force);
4225 if (y == NULL_RTX)
4226 return NULL;
4227 return emit_insn (GEN_FCN (code) (x, y));
4230 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
4231 Return an equivalent MEM that does not use an auto-increment. */
4234 emit_move_resolve_push (machine_mode mode, rtx x)
4236 enum rtx_code code = GET_CODE (XEXP (x, 0));
4237 rtx temp;
4239 poly_int64 adjust = GET_MODE_SIZE (mode);
4240 #ifdef PUSH_ROUNDING
4241 adjust = PUSH_ROUNDING (adjust);
4242 #endif
4243 if (code == PRE_DEC || code == POST_DEC)
4244 adjust = -adjust;
4245 else if (code == PRE_MODIFY || code == POST_MODIFY)
4247 rtx expr = XEXP (XEXP (x, 0), 1);
4249 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
4250 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
4251 if (GET_CODE (expr) == MINUS)
4252 val = -val;
4253 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
4254 adjust = val;
4257 /* Do not use anti_adjust_stack, since we don't want to update
4258 stack_pointer_delta. */
4259 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
4260 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
4261 0, OPTAB_LIB_WIDEN);
4262 if (temp != stack_pointer_rtx)
4263 emit_move_insn (stack_pointer_rtx, temp);
4265 switch (code)
4267 case PRE_INC:
4268 case PRE_DEC:
4269 case PRE_MODIFY:
4270 temp = stack_pointer_rtx;
4271 break;
4272 case POST_INC:
4273 case POST_DEC:
4274 case POST_MODIFY:
4275 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
4276 break;
4277 default:
4278 gcc_unreachable ();
4281 return replace_equiv_address (x, temp);
4284 /* A subroutine of emit_move_complex. Generate a move from Y into X.
4285 X is known to satisfy push_operand, and MODE is known to be complex.
4286 Returns the last instruction emitted. */
4288 rtx_insn *
4289 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
4291 scalar_mode submode = GET_MODE_INNER (mode);
4292 bool imag_first;
4294 #ifdef PUSH_ROUNDING
4295 poly_int64 submodesize = GET_MODE_SIZE (submode);
4297 /* In case we output to the stack, but the size is smaller than the
4298 machine can push exactly, we need to use move instructions. */
4299 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
4301 x = emit_move_resolve_push (mode, x);
4302 return emit_move_insn (x, y);
4304 #endif
4306 /* Note that the real part always precedes the imag part in memory
4307 regardless of machine's endianness. */
4308 switch (GET_CODE (XEXP (x, 0)))
4310 case PRE_DEC:
4311 case POST_DEC:
4312 imag_first = true;
4313 break;
4314 case PRE_INC:
4315 case POST_INC:
4316 imag_first = false;
4317 break;
4318 default:
4319 gcc_unreachable ();
4322 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4323 read_complex_part (y, imag_first));
4324 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4325 read_complex_part (y, !imag_first));
4328 /* A subroutine of emit_move_complex. Perform the move from Y to X
4329 via two moves of the parts. Returns the last instruction emitted. */
4331 rtx_insn *
4332 emit_move_complex_parts (rtx x, rtx y)
4334 /* Show the output dies here. This is necessary for SUBREGs
4335 of pseudos since we cannot track their lifetimes correctly;
4336 hard regs shouldn't appear here except as return values. */
4337 if (!reload_completed && !reload_in_progress
4338 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
4339 emit_clobber (x);
4341 write_complex_part (x, read_complex_part (y, false), false, true);
4342 write_complex_part (x, read_complex_part (y, true), true, false);
4344 return get_last_insn ();
4347 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4348 MODE is known to be complex. Returns the last instruction emitted. */
4350 static rtx_insn *
4351 emit_move_complex (machine_mode mode, rtx x, rtx y)
4353 bool try_int;
4355 /* Need to take special care for pushes, to maintain proper ordering
4356 of the data, and possibly extra padding. */
4357 if (push_operand (x, mode))
4358 return emit_move_complex_push (mode, x, y);
4360 /* See if we can coerce the target into moving both values at once, except
4361 for floating point where we favor moving as parts if this is easy. */
4362 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4363 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
4364 && !(REG_P (x)
4365 && HARD_REGISTER_P (x)
4366 && REG_NREGS (x) == 1)
4367 && !(REG_P (y)
4368 && HARD_REGISTER_P (y)
4369 && REG_NREGS (y) == 1))
4370 try_int = false;
4371 /* Not possible if the values are inherently not adjacent. */
4372 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
4373 try_int = false;
4374 /* Is possible if both are registers (or subregs of registers). */
4375 else if (register_operand (x, mode) && register_operand (y, mode))
4376 try_int = true;
4377 /* If one of the operands is a memory, and alignment constraints
4378 are friendly enough, we may be able to do combined memory operations.
4379 We do not attempt this if Y is a constant because that combination is
4380 usually better with the by-parts thing below. */
4381 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4382 && (!STRICT_ALIGNMENT
4383 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4384 try_int = true;
4385 else
4386 try_int = false;
4388 if (try_int)
4390 rtx_insn *ret;
4392 /* For memory to memory moves, optimal behavior can be had with the
4393 existing block move logic. But use normal expansion if optimizing
4394 for size. */
4395 if (MEM_P (x) && MEM_P (y))
4397 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4398 (optimize_insn_for_speed_p()
4399 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4400 return get_last_insn ();
4403 ret = emit_move_via_integer (mode, x, y, true);
4404 if (ret)
4405 return ret;
4408 return emit_move_complex_parts (x, y);
4411 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4412 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4414 static rtx_insn *
4415 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4417 rtx_insn *ret;
4419 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4420 if (mode != CCmode)
4422 enum insn_code code = optab_handler (mov_optab, CCmode);
4423 if (code != CODE_FOR_nothing)
4425 x = emit_move_change_mode (CCmode, mode, x, true);
4426 y = emit_move_change_mode (CCmode, mode, y, true);
4427 return emit_insn (GEN_FCN (code) (x, y));
4431 /* Otherwise, find the MODE_INT mode of the same width. */
4432 ret = emit_move_via_integer (mode, x, y, false);
4433 gcc_assert (ret != NULL);
4434 return ret;
4437 /* Return true if word I of OP lies entirely in the
4438 undefined bits of a paradoxical subreg. */
4440 static bool
4441 undefined_operand_subword_p (const_rtx op, int i)
4443 if (GET_CODE (op) != SUBREG)
4444 return false;
4445 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4446 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4447 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4448 || known_le (offset, -UNITS_PER_WORD));
4451 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4452 MODE is any multi-word or full-word mode that lacks a move_insn
4453 pattern. Note that you will get better code if you define such
4454 patterns, even if they must turn into multiple assembler instructions. */
4456 static rtx_insn *
4457 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4459 rtx_insn *last_insn = 0;
4460 rtx_insn *seq;
4461 rtx inner;
4462 bool need_clobber;
4463 int i, mode_size;
4465 /* This function can only handle cases where the number of words is
4466 known at compile time. */
4467 mode_size = GET_MODE_SIZE (mode).to_constant ();
4468 gcc_assert (mode_size >= UNITS_PER_WORD);
4470 /* If X is a push on the stack, do the push now and replace
4471 X with a reference to the stack pointer. */
4472 if (push_operand (x, mode))
4473 x = emit_move_resolve_push (mode, x);
4475 /* If we are in reload, see if either operand is a MEM whose address
4476 is scheduled for replacement. */
4477 if (reload_in_progress && MEM_P (x)
4478 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4479 x = replace_equiv_address_nv (x, inner);
4480 if (reload_in_progress && MEM_P (y)
4481 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4482 y = replace_equiv_address_nv (y, inner);
4484 start_sequence ();
4486 need_clobber = false;
4487 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4489 /* Do not generate code for a move if it would go entirely
4490 to the non-existing bits of a paradoxical subreg. */
4491 if (undefined_operand_subword_p (x, i))
4492 continue;
4494 rtx xpart = operand_subword (x, i, 1, mode);
4495 rtx ypart;
4497 /* Do not generate code for a move if it would come entirely
4498 from the undefined bits of a paradoxical subreg. */
4499 if (undefined_operand_subword_p (y, i))
4500 continue;
4502 ypart = operand_subword (y, i, 1, mode);
4504 /* If we can't get a part of Y, put Y into memory if it is a
4505 constant. Otherwise, force it into a register. Then we must
4506 be able to get a part of Y. */
4507 if (ypart == 0 && CONSTANT_P (y))
4509 y = use_anchored_address (force_const_mem (mode, y));
4510 ypart = operand_subword (y, i, 1, mode);
4512 else if (ypart == 0)
4513 ypart = operand_subword_force (y, i, mode);
4515 gcc_assert (xpart && ypart);
4517 need_clobber |= (GET_CODE (xpart) == SUBREG);
4519 last_insn = emit_move_insn (xpart, ypart);
4522 seq = get_insns ();
4523 end_sequence ();
4525 /* Show the output dies here. This is necessary for SUBREGs
4526 of pseudos since we cannot track their lifetimes correctly;
4527 hard regs shouldn't appear here except as return values.
4528 We never want to emit such a clobber after reload. */
4529 if (x != y
4530 && ! (reload_in_progress || reload_completed)
4531 && need_clobber != 0)
4532 emit_clobber (x);
4534 emit_insn (seq);
4536 return last_insn;
4539 /* Low level part of emit_move_insn.
4540 Called just like emit_move_insn, but assumes X and Y
4541 are basically valid. */
4543 rtx_insn *
4544 emit_move_insn_1 (rtx x, rtx y)
4546 machine_mode mode = GET_MODE (x);
4547 enum insn_code code;
4549 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4551 code = optab_handler (mov_optab, mode);
4552 if (code != CODE_FOR_nothing)
4553 return emit_insn (GEN_FCN (code) (x, y));
4555 /* Expand complex moves by moving real part and imag part. */
4556 if (COMPLEX_MODE_P (mode))
4557 return emit_move_complex (mode, x, y);
4559 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4560 || ALL_FIXED_POINT_MODE_P (mode))
4562 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4564 /* If we can't find an integer mode, use multi words. */
4565 if (result)
4566 return result;
4567 else
4568 return emit_move_multi_word (mode, x, y);
4571 if (GET_MODE_CLASS (mode) == MODE_CC)
4572 return emit_move_ccmode (mode, x, y);
4574 /* Try using a move pattern for the corresponding integer mode. This is
4575 only safe when simplify_subreg can convert MODE constants into integer
4576 constants. At present, it can only do this reliably if the value
4577 fits within a HOST_WIDE_INT. */
4578 if (!CONSTANT_P (y)
4579 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4581 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4583 if (ret)
4585 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4586 return ret;
4590 return emit_move_multi_word (mode, x, y);
4593 /* Generate code to copy Y into X.
4594 Both Y and X must have the same mode, except that
4595 Y can be a constant with VOIDmode.
4596 This mode cannot be BLKmode; use emit_block_move for that.
4598 Return the last instruction emitted. */
4600 rtx_insn *
4601 emit_move_insn (rtx x, rtx y)
4603 machine_mode mode = GET_MODE (x);
4604 rtx y_cst = NULL_RTX;
4605 rtx_insn *last_insn;
4606 rtx set;
4608 gcc_assert (mode != BLKmode
4609 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4611 /* If we have a copy that looks like one of the following patterns:
4612 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4613 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4614 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4615 (set (subreg:M1 (reg:M2 ...)) (constant C))
4616 where mode M1 is equal in size to M2, try to detect whether the
4617 mode change involves an implicit round trip through memory.
4618 If so, see if we can avoid that by removing the subregs and
4619 doing the move in mode M2 instead. */
4621 rtx x_inner = NULL_RTX;
4622 rtx y_inner = NULL_RTX;
4624 auto candidate_subreg_p = [&](rtx subreg) {
4625 return (REG_P (SUBREG_REG (subreg))
4626 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4627 GET_MODE_SIZE (GET_MODE (subreg)))
4628 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4629 != CODE_FOR_nothing);
4632 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4633 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4634 && !push_operand (mem, GET_MODE (mem))
4635 /* Not a candiate if innermode requires too much alignment. */
4636 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4637 || targetm.slow_unaligned_access (GET_MODE (mem),
4638 MEM_ALIGN (mem))
4639 || !targetm.slow_unaligned_access (innermode,
4640 MEM_ALIGN (mem))));
4643 if (SUBREG_P (x) && candidate_subreg_p (x))
4644 x_inner = SUBREG_REG (x);
4646 if (SUBREG_P (y) && candidate_subreg_p (y))
4647 y_inner = SUBREG_REG (y);
4649 if (x_inner != NULL_RTX
4650 && y_inner != NULL_RTX
4651 && GET_MODE (x_inner) == GET_MODE (y_inner)
4652 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4654 x = x_inner;
4655 y = y_inner;
4656 mode = GET_MODE (x_inner);
4658 else if (x_inner != NULL_RTX
4659 && MEM_P (y)
4660 && candidate_mem_p (GET_MODE (x_inner), y))
4662 x = x_inner;
4663 y = adjust_address (y, GET_MODE (x_inner), 0);
4664 mode = GET_MODE (x_inner);
4666 else if (y_inner != NULL_RTX
4667 && MEM_P (x)
4668 && candidate_mem_p (GET_MODE (y_inner), x))
4670 x = adjust_address (x, GET_MODE (y_inner), 0);
4671 y = y_inner;
4672 mode = GET_MODE (y_inner);
4674 else if (x_inner != NULL_RTX
4675 && CONSTANT_P (y)
4676 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4677 mode, ALL_REGS)
4678 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4680 x = x_inner;
4681 y = y_inner;
4682 mode = GET_MODE (x_inner);
4685 if (CONSTANT_P (y))
4687 if (optimize
4688 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4689 && (last_insn = compress_float_constant (x, y)))
4690 return last_insn;
4692 y_cst = y;
4694 if (!targetm.legitimate_constant_p (mode, y))
4696 y = force_const_mem (mode, y);
4698 /* If the target's cannot_force_const_mem prevented the spill,
4699 assume that the target's move expanders will also take care
4700 of the non-legitimate constant. */
4701 if (!y)
4702 y = y_cst;
4703 else
4704 y = use_anchored_address (y);
4708 /* If X or Y are memory references, verify that their addresses are valid
4709 for the machine. */
4710 if (MEM_P (x)
4711 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4712 MEM_ADDR_SPACE (x))
4713 && ! push_operand (x, GET_MODE (x))))
4714 x = validize_mem (x);
4716 if (MEM_P (y)
4717 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4718 MEM_ADDR_SPACE (y)))
4719 y = validize_mem (y);
4721 gcc_assert (mode != BLKmode);
4723 last_insn = emit_move_insn_1 (x, y);
4725 if (y_cst && REG_P (x)
4726 && (set = single_set (last_insn)) != NULL_RTX
4727 && SET_DEST (set) == x
4728 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4729 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4731 return last_insn;
4734 /* Generate the body of an instruction to copy Y into X.
4735 It may be a list of insns, if one insn isn't enough. */
4737 rtx_insn *
4738 gen_move_insn (rtx x, rtx y)
4740 rtx_insn *seq;
4742 start_sequence ();
4743 emit_move_insn_1 (x, y);
4744 seq = get_insns ();
4745 end_sequence ();
4746 return seq;
4749 /* If Y is representable exactly in a narrower mode, and the target can
4750 perform the extension directly from constant or memory, then emit the
4751 move as an extension. */
4753 static rtx_insn *
4754 compress_float_constant (rtx x, rtx y)
4756 machine_mode dstmode = GET_MODE (x);
4757 machine_mode orig_srcmode = GET_MODE (y);
4758 machine_mode srcmode;
4759 const REAL_VALUE_TYPE *r;
4760 int oldcost, newcost;
4761 bool speed = optimize_insn_for_speed_p ();
4763 r = CONST_DOUBLE_REAL_VALUE (y);
4765 if (targetm.legitimate_constant_p (dstmode, y))
4766 oldcost = set_src_cost (y, orig_srcmode, speed);
4767 else
4768 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4770 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4772 enum insn_code ic;
4773 rtx trunc_y;
4774 rtx_insn *last_insn;
4776 /* Skip if the target can't extend this way. */
4777 ic = can_extend_p (dstmode, srcmode, 0);
4778 if (ic == CODE_FOR_nothing)
4779 continue;
4781 /* Skip if the narrowed value isn't exact. */
4782 if (! exact_real_truncate (srcmode, r))
4783 continue;
4785 trunc_y = const_double_from_real_value (*r, srcmode);
4787 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4789 /* Skip if the target needs extra instructions to perform
4790 the extension. */
4791 if (!insn_operand_matches (ic, 1, trunc_y))
4792 continue;
4793 /* This is valid, but may not be cheaper than the original. */
4794 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4795 dstmode, speed);
4796 if (oldcost < newcost)
4797 continue;
4799 else if (float_extend_from_mem[dstmode][srcmode])
4801 trunc_y = force_const_mem (srcmode, trunc_y);
4802 /* This is valid, but may not be cheaper than the original. */
4803 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4804 dstmode, speed);
4805 if (oldcost < newcost)
4806 continue;
4807 trunc_y = validize_mem (trunc_y);
4809 else
4810 continue;
4812 /* For CSE's benefit, force the compressed constant pool entry
4813 into a new pseudo. This constant may be used in different modes,
4814 and if not, combine will put things back together for us. */
4815 trunc_y = force_reg (srcmode, trunc_y);
4817 /* If x is a hard register, perform the extension into a pseudo,
4818 so that e.g. stack realignment code is aware of it. */
4819 rtx target = x;
4820 if (REG_P (x) && HARD_REGISTER_P (x))
4821 target = gen_reg_rtx (dstmode);
4823 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4824 last_insn = get_last_insn ();
4826 if (REG_P (target))
4827 set_unique_reg_note (last_insn, REG_EQUAL, y);
4829 if (target != x)
4830 return emit_move_insn (x, target);
4831 return last_insn;
4834 return NULL;
4837 /* Pushing data onto the stack. */
4839 /* Push a block of length SIZE (perhaps variable)
4840 and return an rtx to address the beginning of the block.
4841 The value may be virtual_outgoing_args_rtx.
4843 EXTRA is the number of bytes of padding to push in addition to SIZE.
4844 BELOW nonzero means this padding comes at low addresses;
4845 otherwise, the padding comes at high addresses. */
4848 push_block (rtx size, poly_int64 extra, int below)
4850 rtx temp;
4852 size = convert_modes (Pmode, ptr_mode, size, 1);
4853 if (CONSTANT_P (size))
4854 anti_adjust_stack (plus_constant (Pmode, size, extra));
4855 else if (REG_P (size) && known_eq (extra, 0))
4856 anti_adjust_stack (size);
4857 else
4859 temp = copy_to_mode_reg (Pmode, size);
4860 if (maybe_ne (extra, 0))
4861 temp = expand_binop (Pmode, add_optab, temp,
4862 gen_int_mode (extra, Pmode),
4863 temp, 0, OPTAB_LIB_WIDEN);
4864 anti_adjust_stack (temp);
4867 if (STACK_GROWS_DOWNWARD)
4869 temp = virtual_outgoing_args_rtx;
4870 if (maybe_ne (extra, 0) && below)
4871 temp = plus_constant (Pmode, temp, extra);
4873 else
4875 poly_int64 csize;
4876 if (poly_int_rtx_p (size, &csize))
4877 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4878 -csize - (below ? 0 : extra));
4879 else if (maybe_ne (extra, 0) && !below)
4880 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4881 negate_rtx (Pmode, plus_constant (Pmode, size,
4882 extra)));
4883 else
4884 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4885 negate_rtx (Pmode, size));
4888 return memory_address (NARROWEST_INT_MODE, temp);
4891 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4893 static rtx
4894 mem_autoinc_base (rtx mem)
4896 if (MEM_P (mem))
4898 rtx addr = XEXP (mem, 0);
4899 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4900 return XEXP (addr, 0);
4902 return NULL;
4905 /* A utility routine used here, in reload, and in try_split. The insns
4906 after PREV up to and including LAST are known to adjust the stack,
4907 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4908 placing notes as appropriate. PREV may be NULL, indicating the
4909 entire insn sequence prior to LAST should be scanned.
4911 The set of allowed stack pointer modifications is small:
4912 (1) One or more auto-inc style memory references (aka pushes),
4913 (2) One or more addition/subtraction with the SP as destination,
4914 (3) A single move insn with the SP as destination,
4915 (4) A call_pop insn,
4916 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4918 Insns in the sequence that do not modify the SP are ignored,
4919 except for noreturn calls.
4921 The return value is the amount of adjustment that can be trivially
4922 verified, via immediate operand or auto-inc. If the adjustment
4923 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4925 poly_int64
4926 find_args_size_adjust (rtx_insn *insn)
4928 rtx dest, set, pat;
4929 int i;
4931 pat = PATTERN (insn);
4932 set = NULL;
4934 /* Look for a call_pop pattern. */
4935 if (CALL_P (insn))
4937 /* We have to allow non-call_pop patterns for the case
4938 of emit_single_push_insn of a TLS address. */
4939 if (GET_CODE (pat) != PARALLEL)
4940 return 0;
4942 /* All call_pop have a stack pointer adjust in the parallel.
4943 The call itself is always first, and the stack adjust is
4944 usually last, so search from the end. */
4945 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4947 set = XVECEXP (pat, 0, i);
4948 if (GET_CODE (set) != SET)
4949 continue;
4950 dest = SET_DEST (set);
4951 if (dest == stack_pointer_rtx)
4952 break;
4954 /* We'd better have found the stack pointer adjust. */
4955 if (i == 0)
4956 return 0;
4957 /* Fall through to process the extracted SET and DEST
4958 as if it was a standalone insn. */
4960 else if (GET_CODE (pat) == SET)
4961 set = pat;
4962 else if ((set = single_set (insn)) != NULL)
4964 else if (GET_CODE (pat) == PARALLEL)
4966 /* ??? Some older ports use a parallel with a stack adjust
4967 and a store for a PUSH_ROUNDING pattern, rather than a
4968 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4969 /* ??? See h8300 and m68k, pushqi1. */
4970 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4972 set = XVECEXP (pat, 0, i);
4973 if (GET_CODE (set) != SET)
4974 continue;
4975 dest = SET_DEST (set);
4976 if (dest == stack_pointer_rtx)
4977 break;
4979 /* We do not expect an auto-inc of the sp in the parallel. */
4980 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4981 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4982 != stack_pointer_rtx);
4984 if (i < 0)
4985 return 0;
4987 else
4988 return 0;
4990 dest = SET_DEST (set);
4992 /* Look for direct modifications of the stack pointer. */
4993 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4995 /* Look for a trivial adjustment, otherwise assume nothing. */
4996 /* Note that the SPU restore_stack_block pattern refers to
4997 the stack pointer in V4SImode. Consider that non-trivial. */
4998 poly_int64 offset;
4999 if (SCALAR_INT_MODE_P (GET_MODE (dest))
5000 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
5001 return offset;
5002 /* ??? Reload can generate no-op moves, which will be cleaned
5003 up later. Recognize it and continue searching. */
5004 else if (rtx_equal_p (dest, SET_SRC (set)))
5005 return 0;
5006 else
5007 return HOST_WIDE_INT_MIN;
5009 else
5011 rtx mem, addr;
5013 /* Otherwise only think about autoinc patterns. */
5014 if (mem_autoinc_base (dest) == stack_pointer_rtx)
5016 mem = dest;
5017 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
5018 != stack_pointer_rtx);
5020 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
5021 mem = SET_SRC (set);
5022 else
5023 return 0;
5025 addr = XEXP (mem, 0);
5026 switch (GET_CODE (addr))
5028 case PRE_INC:
5029 case POST_INC:
5030 return GET_MODE_SIZE (GET_MODE (mem));
5031 case PRE_DEC:
5032 case POST_DEC:
5033 return -GET_MODE_SIZE (GET_MODE (mem));
5034 case PRE_MODIFY:
5035 case POST_MODIFY:
5036 addr = XEXP (addr, 1);
5037 gcc_assert (GET_CODE (addr) == PLUS);
5038 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
5039 return rtx_to_poly_int64 (XEXP (addr, 1));
5040 default:
5041 gcc_unreachable ();
5046 poly_int64
5047 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
5048 poly_int64 end_args_size)
5050 poly_int64 args_size = end_args_size;
5051 bool saw_unknown = false;
5052 rtx_insn *insn;
5054 for (insn = last; insn != prev; insn = PREV_INSN (insn))
5056 if (!NONDEBUG_INSN_P (insn))
5057 continue;
5059 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
5060 a call argument containing a TLS address that itself requires
5061 a call to __tls_get_addr. The handling of stack_pointer_delta
5062 in emit_single_push_insn is supposed to ensure that any such
5063 notes are already correct. */
5064 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
5065 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
5067 poly_int64 this_delta = find_args_size_adjust (insn);
5068 if (known_eq (this_delta, 0))
5070 if (!CALL_P (insn)
5071 || ACCUMULATE_OUTGOING_ARGS
5072 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
5073 continue;
5076 gcc_assert (!saw_unknown);
5077 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
5078 saw_unknown = true;
5080 if (!note)
5081 add_args_size_note (insn, args_size);
5082 if (STACK_GROWS_DOWNWARD)
5083 this_delta = -poly_uint64 (this_delta);
5085 if (saw_unknown)
5086 args_size = HOST_WIDE_INT_MIN;
5087 else
5088 args_size -= this_delta;
5091 return args_size;
5094 #ifdef PUSH_ROUNDING
5095 /* Emit single push insn. */
5097 static void
5098 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
5100 rtx dest_addr;
5101 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
5102 rtx dest;
5103 enum insn_code icode;
5105 /* If there is push pattern, use it. Otherwise try old way of throwing
5106 MEM representing push operation to move expander. */
5107 icode = optab_handler (push_optab, mode);
5108 if (icode != CODE_FOR_nothing)
5110 class expand_operand ops[1];
5112 create_input_operand (&ops[0], x, mode);
5113 if (maybe_expand_insn (icode, 1, ops))
5114 return;
5116 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
5117 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
5118 /* If we are to pad downward, adjust the stack pointer first and
5119 then store X into the stack location using an offset. This is
5120 because emit_move_insn does not know how to pad; it does not have
5121 access to type. */
5122 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
5124 emit_move_insn (stack_pointer_rtx,
5125 expand_binop (Pmode,
5126 STACK_GROWS_DOWNWARD ? sub_optab
5127 : add_optab,
5128 stack_pointer_rtx,
5129 gen_int_mode (rounded_size, Pmode),
5130 NULL_RTX, 0, OPTAB_LIB_WIDEN));
5132 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
5133 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
5134 /* We have already decremented the stack pointer, so get the
5135 previous value. */
5136 offset += rounded_size;
5138 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
5139 /* We have already incremented the stack pointer, so get the
5140 previous value. */
5141 offset -= rounded_size;
5143 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
5145 else
5147 if (STACK_GROWS_DOWNWARD)
5148 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
5149 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
5150 else
5151 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
5152 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
5154 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
5157 dest = gen_rtx_MEM (mode, dest_addr);
5159 if (type != 0)
5161 set_mem_attributes (dest, type, 1);
5163 if (cfun->tail_call_marked)
5164 /* Function incoming arguments may overlap with sibling call
5165 outgoing arguments and we cannot allow reordering of reads
5166 from function arguments with stores to outgoing arguments
5167 of sibling calls. */
5168 set_mem_alias_set (dest, 0);
5170 emit_move_insn (dest, x);
5173 /* Emit and annotate a single push insn. */
5175 static void
5176 emit_single_push_insn (machine_mode mode, rtx x, tree type)
5178 poly_int64 delta, old_delta = stack_pointer_delta;
5179 rtx_insn *prev = get_last_insn ();
5180 rtx_insn *last;
5182 emit_single_push_insn_1 (mode, x, type);
5184 /* Adjust stack_pointer_delta to describe the situation after the push
5185 we just performed. Note that we must do this after the push rather
5186 than before the push in case calculating X needs pushes and pops of
5187 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
5188 for such pushes and pops must not include the effect of the future
5189 push of X. */
5190 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
5192 last = get_last_insn ();
5194 /* Notice the common case where we emitted exactly one insn. */
5195 if (PREV_INSN (last) == prev)
5197 add_args_size_note (last, stack_pointer_delta);
5198 return;
5201 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
5202 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
5203 || known_eq (delta, old_delta));
5205 #endif
5207 /* If reading SIZE bytes from X will end up reading from
5208 Y return the number of bytes that overlap. Return -1
5209 if there is no overlap or -2 if we can't determine
5210 (for example when X and Y have different base registers). */
5212 static int
5213 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
5215 rtx tmp = plus_constant (Pmode, x, size);
5216 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
5218 if (!CONST_INT_P (sub))
5219 return -2;
5221 HOST_WIDE_INT val = INTVAL (sub);
5223 return IN_RANGE (val, 1, size) ? val : -1;
5226 /* Generate code to push X onto the stack, assuming it has mode MODE and
5227 type TYPE.
5228 MODE is redundant except when X is a CONST_INT (since they don't
5229 carry mode info).
5230 SIZE is an rtx for the size of data to be copied (in bytes),
5231 needed only if X is BLKmode.
5232 Return true if successful. May return false if asked to push a
5233 partial argument during a sibcall optimization (as specified by
5234 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
5235 to not overlap.
5237 ALIGN (in bits) is maximum alignment we can assume.
5239 If PARTIAL and REG are both nonzero, then copy that many of the first
5240 bytes of X into registers starting with REG, and push the rest of X.
5241 The amount of space pushed is decreased by PARTIAL bytes.
5242 REG must be a hard register in this case.
5243 If REG is zero but PARTIAL is not, take any all others actions for an
5244 argument partially in registers, but do not actually load any
5245 registers.
5247 EXTRA is the amount in bytes of extra space to leave next to this arg.
5248 This is ignored if an argument block has already been allocated.
5250 On a machine that lacks real push insns, ARGS_ADDR is the address of
5251 the bottom of the argument block for this call. We use indexing off there
5252 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
5253 argument block has not been preallocated.
5255 ARGS_SO_FAR is the size of args previously pushed for this call.
5257 REG_PARM_STACK_SPACE is nonzero if functions require stack space
5258 for arguments passed in registers. If nonzero, it will be the number
5259 of bytes required. */
5261 bool
5262 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
5263 unsigned int align, int partial, rtx reg, poly_int64 extra,
5264 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
5265 rtx alignment_pad, bool sibcall_p)
5267 rtx xinner;
5268 pad_direction stack_direction
5269 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
5271 /* Decide where to pad the argument: PAD_DOWNWARD for below,
5272 PAD_UPWARD for above, or PAD_NONE for don't pad it.
5273 Default is below for small data on big-endian machines; else above. */
5274 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
5276 /* Invert direction if stack is post-decrement.
5277 FIXME: why? */
5278 if (STACK_PUSH_CODE == POST_DEC)
5279 if (where_pad != PAD_NONE)
5280 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
5282 xinner = x;
5284 int nregs = partial / UNITS_PER_WORD;
5285 rtx *tmp_regs = NULL;
5286 int overlapping = 0;
5288 if (mode == BLKmode
5289 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
5291 /* Copy a block into the stack, entirely or partially. */
5293 rtx temp;
5294 int used;
5295 int offset;
5296 int skip;
5298 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5299 used = partial - offset;
5301 if (mode != BLKmode)
5303 /* A value is to be stored in an insufficiently aligned
5304 stack slot; copy via a suitably aligned slot if
5305 necessary. */
5306 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
5307 if (!MEM_P (xinner))
5309 temp = assign_temp (type, 1, 1);
5310 emit_move_insn (temp, xinner);
5311 xinner = temp;
5315 gcc_assert (size);
5317 /* USED is now the # of bytes we need not copy to the stack
5318 because registers will take care of them. */
5320 if (partial != 0)
5321 xinner = adjust_address (xinner, BLKmode, used);
5323 /* If the partial register-part of the arg counts in its stack size,
5324 skip the part of stack space corresponding to the registers.
5325 Otherwise, start copying to the beginning of the stack space,
5326 by setting SKIP to 0. */
5327 skip = (reg_parm_stack_space == 0) ? 0 : used;
5329 #ifdef PUSH_ROUNDING
5330 /* NB: Let the backend known the number of bytes to push and
5331 decide if push insns should be generated. */
5332 unsigned int push_size;
5333 if (CONST_INT_P (size))
5334 push_size = INTVAL (size);
5335 else
5336 push_size = 0;
5338 /* Do it with several push insns if that doesn't take lots of insns
5339 and if there is no difficulty with push insns that skip bytes
5340 on the stack for alignment purposes. */
5341 if (args_addr == 0
5342 && targetm.calls.push_argument (push_size)
5343 && CONST_INT_P (size)
5344 && skip == 0
5345 && MEM_ALIGN (xinner) >= align
5346 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
5347 /* Here we avoid the case of a structure whose weak alignment
5348 forces many pushes of a small amount of data,
5349 and such small pushes do rounding that causes trouble. */
5350 && ((!targetm.slow_unaligned_access (word_mode, align))
5351 || align >= BIGGEST_ALIGNMENT
5352 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
5353 align / BITS_PER_UNIT))
5354 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
5356 /* Push padding now if padding above and stack grows down,
5357 or if padding below and stack grows up.
5358 But if space already allocated, this has already been done. */
5359 if (maybe_ne (extra, 0)
5360 && args_addr == 0
5361 && where_pad != PAD_NONE
5362 && where_pad != stack_direction)
5363 anti_adjust_stack (gen_int_mode (extra, Pmode));
5365 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
5366 RETURN_BEGIN);
5368 else
5369 #endif /* PUSH_ROUNDING */
5371 rtx target;
5373 /* Otherwise make space on the stack and copy the data
5374 to the address of that space. */
5376 /* Deduct words put into registers from the size we must copy. */
5377 if (partial != 0)
5379 if (CONST_INT_P (size))
5380 size = GEN_INT (INTVAL (size) - used);
5381 else
5382 size = expand_binop (GET_MODE (size), sub_optab, size,
5383 gen_int_mode (used, GET_MODE (size)),
5384 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5387 /* Get the address of the stack space.
5388 In this case, we do not deal with EXTRA separately.
5389 A single stack adjust will do. */
5390 poly_int64 const_args_so_far;
5391 if (! args_addr)
5393 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5394 extra = 0;
5396 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5397 temp = memory_address (BLKmode,
5398 plus_constant (Pmode, args_addr,
5399 skip + const_args_so_far));
5400 else
5401 temp = memory_address (BLKmode,
5402 plus_constant (Pmode,
5403 gen_rtx_PLUS (Pmode,
5404 args_addr,
5405 args_so_far),
5406 skip));
5408 if (!ACCUMULATE_OUTGOING_ARGS)
5410 /* If the source is referenced relative to the stack pointer,
5411 copy it to another register to stabilize it. We do not need
5412 to do this if we know that we won't be changing sp. */
5414 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5415 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5416 temp = copy_to_reg (temp);
5419 target = gen_rtx_MEM (BLKmode, temp);
5421 /* We do *not* set_mem_attributes here, because incoming arguments
5422 may overlap with sibling call outgoing arguments and we cannot
5423 allow reordering of reads from function arguments with stores
5424 to outgoing arguments of sibling calls. We do, however, want
5425 to record the alignment of the stack slot. */
5426 /* ALIGN may well be better aligned than TYPE, e.g. due to
5427 PARM_BOUNDARY. Assume the caller isn't lying. */
5428 set_mem_align (target, align);
5430 /* If part should go in registers and pushing to that part would
5431 overwrite some of the values that need to go into regs, load the
5432 overlapping values into temporary pseudos to be moved into the hard
5433 regs at the end after the stack pushing has completed.
5434 We cannot load them directly into the hard regs here because
5435 they can be clobbered by the block move expansions.
5436 See PR 65358. */
5438 if (partial > 0 && reg != 0 && mode == BLKmode
5439 && GET_CODE (reg) != PARALLEL)
5441 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5442 if (overlapping > 0)
5444 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5445 overlapping /= UNITS_PER_WORD;
5447 tmp_regs = XALLOCAVEC (rtx, overlapping);
5449 for (int i = 0; i < overlapping; i++)
5450 tmp_regs[i] = gen_reg_rtx (word_mode);
5452 for (int i = 0; i < overlapping; i++)
5453 emit_move_insn (tmp_regs[i],
5454 operand_subword_force (target, i, mode));
5456 else if (overlapping == -1)
5457 overlapping = 0;
5458 /* Could not determine whether there is overlap.
5459 Fail the sibcall. */
5460 else
5462 overlapping = 0;
5463 if (sibcall_p)
5464 return false;
5468 /* If source is a constant VAR_DECL with a simple constructor,
5469 store the constructor to the stack instead of moving it. */
5470 const_tree decl;
5471 if (partial == 0
5472 && MEM_P (xinner)
5473 && SYMBOL_REF_P (XEXP (xinner, 0))
5474 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5475 && VAR_P (decl)
5476 && TREE_READONLY (decl)
5477 && !TREE_SIDE_EFFECTS (decl)
5478 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5479 store_constructor (DECL_INITIAL (decl), target, 0,
5480 int_expr_size (DECL_INITIAL (decl)), false);
5481 else
5482 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5485 else if (partial > 0)
5487 /* Scalar partly in registers. This case is only supported
5488 for fixed-wdth modes. */
5489 int num_words = GET_MODE_SIZE (mode).to_constant ();
5490 num_words /= UNITS_PER_WORD;
5491 int i;
5492 int not_stack;
5493 /* # bytes of start of argument
5494 that we must make space for but need not store. */
5495 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5496 int args_offset = INTVAL (args_so_far);
5497 int skip;
5499 /* Push padding now if padding above and stack grows down,
5500 or if padding below and stack grows up.
5501 But if space already allocated, this has already been done. */
5502 if (maybe_ne (extra, 0)
5503 && args_addr == 0
5504 && where_pad != PAD_NONE
5505 && where_pad != stack_direction)
5506 anti_adjust_stack (gen_int_mode (extra, Pmode));
5508 /* If we make space by pushing it, we might as well push
5509 the real data. Otherwise, we can leave OFFSET nonzero
5510 and leave the space uninitialized. */
5511 if (args_addr == 0)
5512 offset = 0;
5514 /* Now NOT_STACK gets the number of words that we don't need to
5515 allocate on the stack. Convert OFFSET to words too. */
5516 not_stack = (partial - offset) / UNITS_PER_WORD;
5517 offset /= UNITS_PER_WORD;
5519 /* If the partial register-part of the arg counts in its stack size,
5520 skip the part of stack space corresponding to the registers.
5521 Otherwise, start copying to the beginning of the stack space,
5522 by setting SKIP to 0. */
5523 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5525 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5526 x = validize_mem (force_const_mem (mode, x));
5528 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5529 SUBREGs of such registers are not allowed. */
5530 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5531 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5532 x = copy_to_reg (x);
5534 /* Loop over all the words allocated on the stack for this arg. */
5535 /* We can do it by words, because any scalar bigger than a word
5536 has a size a multiple of a word. */
5537 for (i = num_words - 1; i >= not_stack; i--)
5538 if (i >= not_stack + offset)
5539 if (!emit_push_insn (operand_subword_force (x, i, mode),
5540 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
5541 0, args_addr,
5542 GEN_INT (args_offset + ((i - not_stack + skip)
5543 * UNITS_PER_WORD)),
5544 reg_parm_stack_space, alignment_pad, sibcall_p))
5545 return false;
5547 else
5549 rtx addr;
5550 rtx dest;
5552 /* Push padding now if padding above and stack grows down,
5553 or if padding below and stack grows up.
5554 But if space already allocated, this has already been done. */
5555 if (maybe_ne (extra, 0)
5556 && args_addr == 0
5557 && where_pad != PAD_NONE
5558 && where_pad != stack_direction)
5559 anti_adjust_stack (gen_int_mode (extra, Pmode));
5561 #ifdef PUSH_ROUNDING
5562 if (args_addr == 0 && targetm.calls.push_argument (0))
5563 emit_single_push_insn (mode, x, type);
5564 else
5565 #endif
5567 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5568 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5570 /* We do *not* set_mem_attributes here, because incoming arguments
5571 may overlap with sibling call outgoing arguments and we cannot
5572 allow reordering of reads from function arguments with stores
5573 to outgoing arguments of sibling calls. We do, however, want
5574 to record the alignment of the stack slot. */
5575 /* ALIGN may well be better aligned than TYPE, e.g. due to
5576 PARM_BOUNDARY. Assume the caller isn't lying. */
5577 set_mem_align (dest, align);
5579 emit_move_insn (dest, x);
5583 /* Move the partial arguments into the registers and any overlapping
5584 values that we moved into the pseudos in tmp_regs. */
5585 if (partial > 0 && reg != 0)
5587 /* Handle calls that pass values in multiple non-contiguous locations.
5588 The Irix 6 ABI has examples of this. */
5589 if (GET_CODE (reg) == PARALLEL)
5590 emit_group_load (reg, x, type, -1);
5591 else
5593 gcc_assert (partial % UNITS_PER_WORD == 0);
5594 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5596 for (int i = 0; i < overlapping; i++)
5597 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5598 + nregs - overlapping + i),
5599 tmp_regs[i]);
5604 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5605 anti_adjust_stack (gen_int_mode (extra, Pmode));
5607 if (alignment_pad && args_addr == 0)
5608 anti_adjust_stack (alignment_pad);
5610 return true;
5613 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5614 operations. */
5616 static rtx
5617 get_subtarget (rtx x)
5619 return (optimize
5620 || x == 0
5621 /* Only registers can be subtargets. */
5622 || !REG_P (x)
5623 /* Don't use hard regs to avoid extending their life. */
5624 || REGNO (x) < FIRST_PSEUDO_REGISTER
5625 ? 0 : x);
5628 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5629 FIELD is a bitfield. Returns true if the optimization was successful,
5630 and there's nothing else to do. */
5632 static bool
5633 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5634 poly_uint64 pbitpos,
5635 poly_uint64 pbitregion_start,
5636 poly_uint64 pbitregion_end,
5637 machine_mode mode1, rtx str_rtx,
5638 tree to, tree src, bool reverse)
5640 /* str_mode is not guaranteed to be a scalar type. */
5641 machine_mode str_mode = GET_MODE (str_rtx);
5642 unsigned int str_bitsize;
5643 tree op0, op1;
5644 rtx value, result;
5645 optab binop;
5646 gimple *srcstmt;
5647 enum tree_code code;
5649 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5650 if (mode1 != VOIDmode
5651 || !pbitsize.is_constant (&bitsize)
5652 || !pbitpos.is_constant (&bitpos)
5653 || !pbitregion_start.is_constant (&bitregion_start)
5654 || !pbitregion_end.is_constant (&bitregion_end)
5655 || bitsize >= BITS_PER_WORD
5656 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5657 || str_bitsize > BITS_PER_WORD
5658 || TREE_SIDE_EFFECTS (to)
5659 || TREE_THIS_VOLATILE (to))
5660 return false;
5662 STRIP_NOPS (src);
5663 if (TREE_CODE (src) != SSA_NAME)
5664 return false;
5665 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5666 return false;
5668 srcstmt = get_gimple_for_ssa_name (src);
5669 if (!srcstmt
5670 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5671 return false;
5673 code = gimple_assign_rhs_code (srcstmt);
5675 op0 = gimple_assign_rhs1 (srcstmt);
5677 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5678 to find its initialization. Hopefully the initialization will
5679 be from a bitfield load. */
5680 if (TREE_CODE (op0) == SSA_NAME)
5682 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5684 /* We want to eventually have OP0 be the same as TO, which
5685 should be a bitfield. */
5686 if (!op0stmt
5687 || !is_gimple_assign (op0stmt)
5688 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5689 return false;
5690 op0 = gimple_assign_rhs1 (op0stmt);
5693 op1 = gimple_assign_rhs2 (srcstmt);
5695 if (!operand_equal_p (to, op0, 0))
5696 return false;
5698 if (MEM_P (str_rtx))
5700 unsigned HOST_WIDE_INT offset1;
5702 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5703 str_bitsize = BITS_PER_WORD;
5705 scalar_int_mode best_mode;
5706 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5707 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5708 return false;
5709 str_mode = best_mode;
5710 str_bitsize = GET_MODE_BITSIZE (best_mode);
5712 offset1 = bitpos;
5713 bitpos %= str_bitsize;
5714 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5715 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5717 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5718 return false;
5720 /* If the bit field covers the whole REG/MEM, store_field
5721 will likely generate better code. */
5722 if (bitsize >= str_bitsize)
5723 return false;
5725 /* We can't handle fields split across multiple entities. */
5726 if (bitpos + bitsize > str_bitsize)
5727 return false;
5729 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5730 bitpos = str_bitsize - bitpos - bitsize;
5732 switch (code)
5734 case PLUS_EXPR:
5735 case MINUS_EXPR:
5736 /* For now, just optimize the case of the topmost bitfield
5737 where we don't need to do any masking and also
5738 1 bit bitfields where xor can be used.
5739 We might win by one instruction for the other bitfields
5740 too if insv/extv instructions aren't used, so that
5741 can be added later. */
5742 if ((reverse || bitpos + bitsize != str_bitsize)
5743 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5744 break;
5746 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5747 value = convert_modes (str_mode,
5748 TYPE_MODE (TREE_TYPE (op1)), value,
5749 TYPE_UNSIGNED (TREE_TYPE (op1)));
5751 /* We may be accessing data outside the field, which means
5752 we can alias adjacent data. */
5753 if (MEM_P (str_rtx))
5755 str_rtx = shallow_copy_rtx (str_rtx);
5756 set_mem_alias_set (str_rtx, 0);
5757 set_mem_expr (str_rtx, 0);
5760 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5762 value = expand_and (str_mode, value, const1_rtx, NULL);
5763 binop = xor_optab;
5765 else
5766 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5768 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5769 if (reverse)
5770 value = flip_storage_order (str_mode, value);
5771 result = expand_binop (str_mode, binop, str_rtx,
5772 value, str_rtx, 1, OPTAB_WIDEN);
5773 if (result != str_rtx)
5774 emit_move_insn (str_rtx, result);
5775 return true;
5777 case BIT_IOR_EXPR:
5778 case BIT_XOR_EXPR:
5779 if (TREE_CODE (op1) != INTEGER_CST)
5780 break;
5781 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5782 value = convert_modes (str_mode,
5783 TYPE_MODE (TREE_TYPE (op1)), value,
5784 TYPE_UNSIGNED (TREE_TYPE (op1)));
5786 /* We may be accessing data outside the field, which means
5787 we can alias adjacent data. */
5788 if (MEM_P (str_rtx))
5790 str_rtx = shallow_copy_rtx (str_rtx);
5791 set_mem_alias_set (str_rtx, 0);
5792 set_mem_expr (str_rtx, 0);
5795 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5796 if (bitpos + bitsize != str_bitsize)
5798 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5799 str_mode);
5800 value = expand_and (str_mode, value, mask, NULL_RTX);
5802 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5803 if (reverse)
5804 value = flip_storage_order (str_mode, value);
5805 result = expand_binop (str_mode, binop, str_rtx,
5806 value, str_rtx, 1, OPTAB_WIDEN);
5807 if (result != str_rtx)
5808 emit_move_insn (str_rtx, result);
5809 return true;
5811 default:
5812 break;
5815 return false;
5818 /* In the C++ memory model, consecutive bit fields in a structure are
5819 considered one memory location.
5821 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5822 returns the bit range of consecutive bits in which this COMPONENT_REF
5823 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5824 and *OFFSET may be adjusted in the process.
5826 If the access does not need to be restricted, 0 is returned in both
5827 *BITSTART and *BITEND. */
5829 void
5830 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp,
5831 poly_int64 *bitpos, tree *offset)
5833 poly_int64 bitoffset;
5834 tree field, repr;
5836 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5838 field = TREE_OPERAND (exp, 1);
5839 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5840 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5841 need to limit the range we can access. */
5842 if (!repr)
5844 *bitstart = *bitend = 0;
5845 return;
5848 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5849 part of a larger bit field, then the representative does not serve any
5850 useful purpose. This can occur in Ada. */
5851 if (handled_component_p (TREE_OPERAND (exp, 0)))
5853 machine_mode rmode;
5854 poly_int64 rbitsize, rbitpos;
5855 tree roffset;
5856 int unsignedp, reversep, volatilep = 0;
5857 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5858 &roffset, &rmode, &unsignedp, &reversep,
5859 &volatilep);
5860 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5862 *bitstart = *bitend = 0;
5863 return;
5867 /* Compute the adjustment to bitpos from the offset of the field
5868 relative to the representative. DECL_FIELD_OFFSET of field and
5869 repr are the same by construction if they are not constants,
5870 see finish_bitfield_layout. */
5871 poly_uint64 field_offset, repr_offset;
5872 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5873 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5874 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5875 else
5876 bitoffset = 0;
5877 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5878 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5880 /* If the adjustment is larger than bitpos, we would have a negative bit
5881 position for the lower bound and this may wreak havoc later. Adjust
5882 offset and bitpos to make the lower bound non-negative in that case. */
5883 if (maybe_gt (bitoffset, *bitpos))
5885 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5886 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5888 *bitpos += adjust_bits;
5889 if (*offset == NULL_TREE)
5890 *offset = size_int (-adjust_bytes);
5891 else
5892 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5893 *bitstart = 0;
5895 else
5896 *bitstart = *bitpos - bitoffset;
5898 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5901 /* Returns true if BASE is a DECL that does not reside in memory and
5902 has non-BLKmode. DECL_RTL must not be a MEM; if
5903 DECL_RTL was not set yet, return false. */
5905 bool
5906 non_mem_decl_p (tree base)
5908 if (!DECL_P (base)
5909 || TREE_ADDRESSABLE (base)
5910 || DECL_MODE (base) == BLKmode)
5911 return false;
5913 if (!DECL_RTL_SET_P (base))
5914 return false;
5916 return (!MEM_P (DECL_RTL (base)));
5919 /* Returns true if REF refers to an object that does not
5920 reside in memory and has non-BLKmode. */
5922 bool
5923 mem_ref_refers_to_non_mem_p (tree ref)
5925 tree base;
5927 if (TREE_CODE (ref) == MEM_REF
5928 || TREE_CODE (ref) == TARGET_MEM_REF)
5930 tree addr = TREE_OPERAND (ref, 0);
5932 if (TREE_CODE (addr) != ADDR_EXPR)
5933 return false;
5935 base = TREE_OPERAND (addr, 0);
5937 else
5938 base = ref;
5940 return non_mem_decl_p (base);
5943 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5944 is true, try generating a nontemporal store. */
5946 void
5947 expand_assignment (tree to, tree from, bool nontemporal)
5949 rtx to_rtx = 0;
5950 rtx result;
5951 machine_mode mode;
5952 unsigned int align;
5953 enum insn_code icode;
5955 /* Don't crash if the lhs of the assignment was erroneous. */
5956 if (TREE_CODE (to) == ERROR_MARK)
5958 expand_normal (from);
5959 return;
5962 /* Optimize away no-op moves without side-effects. */
5963 if (operand_equal_p (to, from, 0))
5964 return;
5966 /* Handle misaligned stores. */
5967 mode = TYPE_MODE (TREE_TYPE (to));
5968 if ((TREE_CODE (to) == MEM_REF
5969 || TREE_CODE (to) == TARGET_MEM_REF
5970 || DECL_P (to))
5971 && mode != BLKmode
5972 && !mem_ref_refers_to_non_mem_p (to)
5973 && ((align = get_object_alignment (to))
5974 < GET_MODE_ALIGNMENT (mode))
5975 && (((icode = optab_handler (movmisalign_optab, mode))
5976 != CODE_FOR_nothing)
5977 || targetm.slow_unaligned_access (mode, align)))
5979 rtx reg, mem;
5981 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5982 /* Handle PARALLEL. */
5983 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5984 reg = force_not_mem (reg);
5985 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5986 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5987 reg = flip_storage_order (mode, reg);
5989 if (icode != CODE_FOR_nothing)
5991 class expand_operand ops[2];
5993 create_fixed_operand (&ops[0], mem);
5994 create_input_operand (&ops[1], reg, mode);
5995 /* The movmisalign<mode> pattern cannot fail, else the assignment
5996 would silently be omitted. */
5997 expand_insn (icode, 2, ops);
5999 else
6000 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
6001 false, false);
6002 return;
6005 /* Assignment of a structure component needs special treatment
6006 if the structure component's rtx is not simply a MEM.
6007 Assignment of an array element at a constant index, and assignment of
6008 an array element in an unaligned packed structure field, has the same
6009 problem. Same for (partially) storing into a non-memory object. */
6010 if (handled_component_p (to)
6011 || (TREE_CODE (to) == MEM_REF
6012 && (REF_REVERSE_STORAGE_ORDER (to)
6013 || mem_ref_refers_to_non_mem_p (to)))
6014 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
6016 machine_mode mode1;
6017 poly_int64 bitsize, bitpos;
6018 poly_uint64 bitregion_start = 0;
6019 poly_uint64 bitregion_end = 0;
6020 tree offset;
6021 int unsignedp, reversep, volatilep = 0;
6022 tree tem;
6024 push_temp_slots ();
6025 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
6026 &unsignedp, &reversep, &volatilep);
6028 /* Make sure bitpos is not negative, it can wreak havoc later. */
6029 if (maybe_lt (bitpos, 0))
6031 gcc_assert (offset == NULL_TREE);
6032 offset = size_int (bits_to_bytes_round_down (bitpos));
6033 bitpos = num_trailing_bits (bitpos);
6036 if (TREE_CODE (to) == COMPONENT_REF
6037 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
6038 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
6039 /* The C++ memory model naturally applies to byte-aligned fields.
6040 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
6041 BITSIZE are not byte-aligned, there is no need to limit the range
6042 we can access. This can occur with packed structures in Ada. */
6043 else if (maybe_gt (bitsize, 0)
6044 && multiple_p (bitsize, BITS_PER_UNIT)
6045 && multiple_p (bitpos, BITS_PER_UNIT))
6047 bitregion_start = bitpos;
6048 bitregion_end = bitpos + bitsize - 1;
6051 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
6053 /* If the field has a mode, we want to access it in the
6054 field's mode, not the computed mode.
6055 If a MEM has VOIDmode (external with incomplete type),
6056 use BLKmode for it instead. */
6057 if (MEM_P (to_rtx))
6059 if (mode1 != VOIDmode)
6060 to_rtx = adjust_address (to_rtx, mode1, 0);
6061 else if (GET_MODE (to_rtx) == VOIDmode)
6062 to_rtx = adjust_address (to_rtx, BLKmode, 0);
6065 if (offset != 0)
6067 machine_mode address_mode;
6068 rtx offset_rtx;
6070 if (!MEM_P (to_rtx))
6072 /* We can get constant negative offsets into arrays with broken
6073 user code. Translate this to a trap instead of ICEing. */
6074 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
6075 expand_builtin_trap ();
6076 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
6079 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
6080 address_mode = get_address_mode (to_rtx);
6081 if (GET_MODE (offset_rtx) != address_mode)
6083 /* We cannot be sure that the RTL in offset_rtx is valid outside
6084 of a memory address context, so force it into a register
6085 before attempting to convert it to the desired mode. */
6086 offset_rtx = force_operand (offset_rtx, NULL_RTX);
6087 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6090 /* If we have an expression in OFFSET_RTX and a non-zero
6091 byte offset in BITPOS, adding the byte offset before the
6092 OFFSET_RTX results in better intermediate code, which makes
6093 later rtl optimization passes perform better.
6095 We prefer intermediate code like this:
6097 r124:DI=r123:DI+0x18
6098 [r124:DI]=r121:DI
6100 ... instead of ...
6102 r124:DI=r123:DI+0x10
6103 [r124:DI+0x8]=r121:DI
6105 This is only done for aligned data values, as these can
6106 be expected to result in single move instructions. */
6107 poly_int64 bytepos;
6108 if (mode1 != VOIDmode
6109 && maybe_ne (bitpos, 0)
6110 && maybe_gt (bitsize, 0)
6111 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6112 && multiple_p (bitpos, bitsize)
6113 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
6114 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
6116 to_rtx = adjust_address (to_rtx, mode1, bytepos);
6117 bitregion_start = 0;
6118 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
6119 bitregion_end -= bitpos;
6120 bitpos = 0;
6123 to_rtx = offset_address (to_rtx, offset_rtx,
6124 highest_pow2_factor_for_target (to,
6125 offset));
6128 /* No action is needed if the target is not a memory and the field
6129 lies completely outside that target. This can occur if the source
6130 code contains an out-of-bounds access to a small array. */
6131 if (!MEM_P (to_rtx)
6132 && GET_MODE (to_rtx) != BLKmode
6133 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
6135 expand_normal (from);
6136 result = NULL;
6138 /* Handle expand_expr of a complex value returning a CONCAT. */
6139 else if (GET_CODE (to_rtx) == CONCAT)
6141 machine_mode to_mode = GET_MODE (to_rtx);
6142 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
6143 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
6144 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
6145 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
6146 && known_eq (bitpos, 0)
6147 && known_eq (bitsize, mode_bitsize))
6148 result = store_expr (from, to_rtx, false, nontemporal, reversep);
6149 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
6150 && known_eq (bitsize, inner_bitsize)
6151 && (known_eq (bitpos, 0)
6152 || known_eq (bitpos, inner_bitsize)))
6153 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
6154 false, nontemporal, reversep);
6155 else if (known_le (bitpos + bitsize, inner_bitsize))
6156 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
6157 bitregion_start, bitregion_end,
6158 mode1, from, get_alias_set (to),
6159 nontemporal, reversep);
6160 else if (known_ge (bitpos, inner_bitsize))
6161 result = store_field (XEXP (to_rtx, 1), bitsize,
6162 bitpos - inner_bitsize,
6163 bitregion_start, bitregion_end,
6164 mode1, from, get_alias_set (to),
6165 nontemporal, reversep);
6166 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
6168 result = expand_normal (from);
6169 if (GET_CODE (result) == CONCAT)
6171 to_mode = GET_MODE_INNER (to_mode);
6172 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
6173 rtx from_real
6174 = simplify_gen_subreg (to_mode, XEXP (result, 0),
6175 from_mode, 0);
6176 rtx from_imag
6177 = simplify_gen_subreg (to_mode, XEXP (result, 1),
6178 from_mode, 0);
6179 if (!from_real || !from_imag)
6180 goto concat_store_slow;
6181 emit_move_insn (XEXP (to_rtx, 0), from_real);
6182 emit_move_insn (XEXP (to_rtx, 1), from_imag);
6184 else
6186 machine_mode from_mode
6187 = GET_MODE (result) == VOIDmode
6188 ? TYPE_MODE (TREE_TYPE (from))
6189 : GET_MODE (result);
6190 rtx from_rtx;
6191 if (MEM_P (result))
6192 from_rtx = change_address (result, to_mode, NULL_RTX);
6193 else
6194 from_rtx
6195 = simplify_gen_subreg (to_mode, result, from_mode, 0);
6196 if (from_rtx)
6198 emit_move_insn (XEXP (to_rtx, 0),
6199 read_complex_part (from_rtx, false));
6200 emit_move_insn (XEXP (to_rtx, 1),
6201 read_complex_part (from_rtx, true));
6203 else
6205 to_mode = GET_MODE_INNER (to_mode);
6206 rtx from_real
6207 = simplify_gen_subreg (to_mode, result, from_mode, 0);
6208 rtx from_imag
6209 = simplify_gen_subreg (to_mode, result, from_mode,
6210 GET_MODE_SIZE (to_mode));
6211 if (!from_real || !from_imag)
6212 goto concat_store_slow;
6213 emit_move_insn (XEXP (to_rtx, 0), from_real);
6214 emit_move_insn (XEXP (to_rtx, 1), from_imag);
6218 else
6220 concat_store_slow:;
6221 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6222 GET_MODE_SIZE (GET_MODE (to_rtx)));
6223 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
6224 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
6225 result = store_field (temp, bitsize, bitpos,
6226 bitregion_start, bitregion_end,
6227 mode1, from, get_alias_set (to),
6228 nontemporal, reversep);
6229 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
6230 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
6233 /* For calls to functions returning variable length structures, if TO_RTX
6234 is not a MEM, go through a MEM because we must not create temporaries
6235 of the VLA type. */
6236 else if (!MEM_P (to_rtx)
6237 && TREE_CODE (from) == CALL_EXPR
6238 && COMPLETE_TYPE_P (TREE_TYPE (from))
6239 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
6241 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6242 GET_MODE_SIZE (GET_MODE (to_rtx)));
6243 result = store_field (temp, bitsize, bitpos, bitregion_start,
6244 bitregion_end, mode1, from, get_alias_set (to),
6245 nontemporal, reversep);
6246 emit_move_insn (to_rtx, temp);
6248 else
6250 if (MEM_P (to_rtx))
6252 /* If the field is at offset zero, we could have been given the
6253 DECL_RTX of the parent struct. Don't munge it. */
6254 to_rtx = shallow_copy_rtx (to_rtx);
6255 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
6256 if (volatilep)
6257 MEM_VOLATILE_P (to_rtx) = 1;
6260 gcc_checking_assert (known_ge (bitpos, 0));
6261 if (optimize_bitfield_assignment_op (bitsize, bitpos,
6262 bitregion_start, bitregion_end,
6263 mode1, to_rtx, to, from,
6264 reversep))
6265 result = NULL;
6266 else if (SUBREG_P (to_rtx)
6267 && SUBREG_PROMOTED_VAR_P (to_rtx))
6269 /* If to_rtx is a promoted subreg, we need to zero or sign
6270 extend the value afterwards. */
6271 if (TREE_CODE (to) == MEM_REF
6272 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
6273 && !REF_REVERSE_STORAGE_ORDER (to)
6274 && known_eq (bitpos, 0)
6275 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
6276 result = store_expr (from, to_rtx, 0, nontemporal, false);
6277 else
6279 rtx to_rtx1
6280 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
6281 SUBREG_REG (to_rtx),
6282 subreg_promoted_mode (to_rtx));
6283 result = store_field (to_rtx1, bitsize, bitpos,
6284 bitregion_start, bitregion_end,
6285 mode1, from, get_alias_set (to),
6286 nontemporal, reversep);
6287 convert_move (SUBREG_REG (to_rtx), to_rtx1,
6288 SUBREG_PROMOTED_SIGN (to_rtx));
6291 else
6292 result = store_field (to_rtx, bitsize, bitpos,
6293 bitregion_start, bitregion_end,
6294 mode1, from, get_alias_set (to),
6295 nontemporal, reversep);
6298 if (result)
6299 preserve_temp_slots (result);
6300 pop_temp_slots ();
6301 return;
6304 /* If the rhs is a function call and its value is not an aggregate,
6305 call the function before we start to compute the lhs.
6306 This is needed for correct code for cases such as
6307 val = setjmp (buf) on machines where reference to val
6308 requires loading up part of an address in a separate insn.
6310 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
6311 since it might be a promoted variable where the zero- or sign- extension
6312 needs to be done. Handling this in the normal way is safe because no
6313 computation is done before the call. The same is true for SSA names. */
6314 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
6315 && COMPLETE_TYPE_P (TREE_TYPE (from))
6316 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
6317 && ! (((VAR_P (to)
6318 || TREE_CODE (to) == PARM_DECL
6319 || TREE_CODE (to) == RESULT_DECL)
6320 && REG_P (DECL_RTL (to)))
6321 || TREE_CODE (to) == SSA_NAME))
6323 rtx value;
6325 push_temp_slots ();
6326 value = expand_normal (from);
6328 if (to_rtx == 0)
6329 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6331 /* Handle calls that return values in multiple non-contiguous locations.
6332 The Irix 6 ABI has examples of this. */
6333 if (GET_CODE (to_rtx) == PARALLEL)
6335 if (GET_CODE (value) == PARALLEL)
6336 emit_group_move (to_rtx, value);
6337 else
6338 emit_group_load (to_rtx, value, TREE_TYPE (from),
6339 int_size_in_bytes (TREE_TYPE (from)));
6341 else if (GET_CODE (value) == PARALLEL)
6342 emit_group_store (to_rtx, value, TREE_TYPE (from),
6343 int_size_in_bytes (TREE_TYPE (from)));
6344 else if (GET_MODE (to_rtx) == BLKmode)
6346 /* Handle calls that return BLKmode values in registers. */
6347 if (REG_P (value))
6348 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
6349 else
6350 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
6352 else
6354 if (POINTER_TYPE_P (TREE_TYPE (to)))
6355 value = convert_memory_address_addr_space
6356 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
6357 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
6359 emit_move_insn (to_rtx, value);
6362 preserve_temp_slots (to_rtx);
6363 pop_temp_slots ();
6364 return;
6367 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
6368 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6370 /* Don't move directly into a return register. */
6371 if (TREE_CODE (to) == RESULT_DECL
6372 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
6374 rtx temp;
6376 push_temp_slots ();
6378 /* If the source is itself a return value, it still is in a pseudo at
6379 this point so we can move it back to the return register directly. */
6380 if (REG_P (to_rtx)
6381 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6382 && TREE_CODE (from) != CALL_EXPR)
6383 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6384 else
6385 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6387 /* Handle calls that return values in multiple non-contiguous locations.
6388 The Irix 6 ABI has examples of this. */
6389 if (GET_CODE (to_rtx) == PARALLEL)
6391 if (GET_CODE (temp) == PARALLEL)
6392 emit_group_move (to_rtx, temp);
6393 else
6394 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6395 int_size_in_bytes (TREE_TYPE (from)));
6397 else if (temp)
6398 emit_move_insn (to_rtx, temp);
6400 preserve_temp_slots (to_rtx);
6401 pop_temp_slots ();
6402 return;
6405 /* In case we are returning the contents of an object which overlaps
6406 the place the value is being stored, use a safe function when copying
6407 a value through a pointer into a structure value return block. */
6408 if (TREE_CODE (to) == RESULT_DECL
6409 && TREE_CODE (from) == INDIRECT_REF
6410 && ADDR_SPACE_GENERIC_P
6411 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6412 && refs_may_alias_p (to, from)
6413 && cfun->returns_struct
6414 && !cfun->returns_pcc_struct)
6416 rtx from_rtx, size;
6418 push_temp_slots ();
6419 size = expr_size (from);
6420 from_rtx = expand_normal (from);
6422 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6424 preserve_temp_slots (to_rtx);
6425 pop_temp_slots ();
6426 return;
6429 /* Compute FROM and store the value in the rtx we got. */
6431 push_temp_slots ();
6432 result = store_expr (from, to_rtx, 0, nontemporal, false);
6433 preserve_temp_slots (result);
6434 pop_temp_slots ();
6435 return;
6438 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6439 succeeded, false otherwise. */
6441 bool
6442 emit_storent_insn (rtx to, rtx from)
6444 class expand_operand ops[2];
6445 machine_mode mode = GET_MODE (to);
6446 enum insn_code code = optab_handler (storent_optab, mode);
6448 if (code == CODE_FOR_nothing)
6449 return false;
6451 create_fixed_operand (&ops[0], to);
6452 create_input_operand (&ops[1], from, mode);
6453 return maybe_expand_insn (code, 2, ops);
6456 /* Helper function for store_expr storing of STRING_CST. */
6458 static rtx
6459 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6460 fixed_size_mode mode)
6462 tree str = (tree) data;
6464 gcc_assert (offset >= 0);
6465 if (offset >= TREE_STRING_LENGTH (str))
6466 return const0_rtx;
6468 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6469 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6471 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6472 size_t l = TREE_STRING_LENGTH (str) - offset;
6473 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6474 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6475 return c_readstr (p, mode, false);
6478 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
6481 /* Generate code for computing expression EXP,
6482 and storing the value into TARGET.
6484 If the mode is BLKmode then we may return TARGET itself.
6485 It turns out that in BLKmode it doesn't cause a problem.
6486 because C has no operators that could combine two different
6487 assignments into the same BLKmode object with different values
6488 with no sequence point. Will other languages need this to
6489 be more thorough?
6491 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6492 stack, and block moves may need to be treated specially.
6494 If NONTEMPORAL is true, try using a nontemporal store instruction.
6496 If REVERSE is true, the store is to be done in reverse order. */
6499 store_expr (tree exp, rtx target, int call_param_p,
6500 bool nontemporal, bool reverse)
6502 rtx temp;
6503 rtx alt_rtl = NULL_RTX;
6504 location_t loc = curr_insn_location ();
6505 bool shortened_string_cst = false;
6507 if (VOID_TYPE_P (TREE_TYPE (exp)))
6509 /* C++ can generate ?: expressions with a throw expression in one
6510 branch and an rvalue in the other. Here, we resolve attempts to
6511 store the throw expression's nonexistent result. */
6512 gcc_assert (!call_param_p);
6513 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6514 return NULL_RTX;
6516 if (TREE_CODE (exp) == COMPOUND_EXPR)
6518 /* Perform first part of compound expression, then assign from second
6519 part. */
6520 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6521 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6522 return store_expr (TREE_OPERAND (exp, 1), target,
6523 call_param_p, nontemporal, reverse);
6525 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6527 /* For conditional expression, get safe form of the target. Then
6528 test the condition, doing the appropriate assignment on either
6529 side. This avoids the creation of unnecessary temporaries.
6530 For non-BLKmode, it is more efficient not to do this. */
6532 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6534 do_pending_stack_adjust ();
6535 NO_DEFER_POP;
6536 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6537 profile_probability::uninitialized ());
6538 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6539 nontemporal, reverse);
6540 emit_jump_insn (targetm.gen_jump (lab2));
6541 emit_barrier ();
6542 emit_label (lab1);
6543 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6544 nontemporal, reverse);
6545 emit_label (lab2);
6546 OK_DEFER_POP;
6548 return NULL_RTX;
6550 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6551 /* If this is a scalar in a register that is stored in a wider mode
6552 than the declared mode, compute the result into its declared mode
6553 and then convert to the wider mode. Our value is the computed
6554 expression. */
6556 rtx inner_target = 0;
6557 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6558 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6560 /* We can do the conversion inside EXP, which will often result
6561 in some optimizations. Do the conversion in two steps: first
6562 change the signedness, if needed, then the extend. But don't
6563 do this if the type of EXP is a subtype of something else
6564 since then the conversion might involve more than just
6565 converting modes. */
6566 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6567 && TREE_TYPE (TREE_TYPE (exp)) == 0
6568 && GET_MODE_PRECISION (outer_mode)
6569 == TYPE_PRECISION (TREE_TYPE (exp)))
6571 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6572 TYPE_UNSIGNED (TREE_TYPE (exp))))
6574 /* Some types, e.g. Fortran's logical*4, won't have a signed
6575 version, so use the mode instead. */
6576 tree ntype
6577 = (signed_or_unsigned_type_for
6578 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6579 if (ntype == NULL)
6580 ntype = lang_hooks.types.type_for_mode
6581 (TYPE_MODE (TREE_TYPE (exp)),
6582 SUBREG_PROMOTED_SIGN (target));
6584 exp = fold_convert_loc (loc, ntype, exp);
6587 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6588 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6589 exp);
6591 inner_target = SUBREG_REG (target);
6594 temp = expand_expr (exp, inner_target, VOIDmode,
6595 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6598 /* If TEMP is a VOIDmode constant, use convert_modes to make
6599 sure that we properly convert it. */
6600 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6602 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6603 temp, SUBREG_PROMOTED_SIGN (target));
6604 temp = convert_modes (inner_mode, outer_mode, temp,
6605 SUBREG_PROMOTED_SIGN (target));
6607 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6608 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6609 temp, SUBREG_PROMOTED_SIGN (target));
6611 convert_move (SUBREG_REG (target), temp,
6612 SUBREG_PROMOTED_SIGN (target));
6614 return NULL_RTX;
6616 else if ((TREE_CODE (exp) == STRING_CST
6617 || (TREE_CODE (exp) == MEM_REF
6618 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6619 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6620 == STRING_CST
6621 && integer_zerop (TREE_OPERAND (exp, 1))))
6622 && !nontemporal && !call_param_p
6623 && MEM_P (target))
6625 /* Optimize initialization of an array with a STRING_CST. */
6626 HOST_WIDE_INT exp_len, str_copy_len;
6627 rtx dest_mem;
6628 tree str = TREE_CODE (exp) == STRING_CST
6629 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6631 exp_len = int_expr_size (exp);
6632 if (exp_len <= 0)
6633 goto normal_expr;
6635 if (TREE_STRING_LENGTH (str) <= 0)
6636 goto normal_expr;
6638 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6639 MEM_ALIGN (target), false))
6641 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6642 MEM_ALIGN (target), false, RETURN_BEGIN);
6643 return NULL_RTX;
6646 str_copy_len = TREE_STRING_LENGTH (str);
6648 /* Trailing NUL bytes in EXP will be handled by the call to
6649 clear_storage, which is more efficient than copying them from
6650 the STRING_CST, so trim those from STR_COPY_LEN. */
6651 while (str_copy_len)
6653 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6654 break;
6655 str_copy_len--;
6658 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6660 str_copy_len += STORE_MAX_PIECES - 1;
6661 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6663 if (str_copy_len >= exp_len)
6664 goto normal_expr;
6666 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6667 (void *) str, MEM_ALIGN (target), false))
6668 goto normal_expr;
6670 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6671 (void *) str, MEM_ALIGN (target), false,
6672 RETURN_END);
6673 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6674 exp_len - str_copy_len),
6675 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6676 return NULL_RTX;
6678 else
6680 rtx tmp_target;
6682 normal_expr:
6683 /* If we want to use a nontemporal or a reverse order store, force the
6684 value into a register first. */
6685 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6686 tree rexp = exp;
6687 if (TREE_CODE (exp) == STRING_CST
6688 && tmp_target == target
6689 && GET_MODE (target) == BLKmode
6690 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6692 rtx size = expr_size (exp);
6693 if (CONST_INT_P (size)
6694 && size != const0_rtx
6695 && (UINTVAL (size)
6696 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6698 /* If the STRING_CST has much larger array type than
6699 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6700 it into the rodata section as the code later on will use
6701 memset zero for the remainder anyway. See PR95052. */
6702 tmp_target = NULL_RTX;
6703 rexp = copy_node (exp);
6704 tree index
6705 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6706 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6707 index);
6708 shortened_string_cst = true;
6711 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6712 (call_param_p
6713 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6714 &alt_rtl, false);
6715 if (shortened_string_cst)
6717 gcc_assert (MEM_P (temp));
6718 temp = change_address (temp, BLKmode, NULL_RTX);
6722 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6723 the same as that of TARGET, adjust the constant. This is needed, for
6724 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6725 only a word-sized value. */
6726 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6727 && TREE_CODE (exp) != ERROR_MARK
6728 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6730 gcc_assert (!shortened_string_cst);
6731 if (GET_MODE_CLASS (GET_MODE (target))
6732 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6733 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6734 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6736 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6737 TYPE_MODE (TREE_TYPE (exp)), 0);
6738 if (t)
6739 temp = t;
6741 if (GET_MODE (temp) == VOIDmode)
6742 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6743 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6746 /* If value was not generated in the target, store it there.
6747 Convert the value to TARGET's type first if necessary and emit the
6748 pending incrementations that have been queued when expanding EXP.
6749 Note that we cannot emit the whole queue blindly because this will
6750 effectively disable the POST_INC optimization later.
6752 If TEMP and TARGET compare equal according to rtx_equal_p, but
6753 one or both of them are volatile memory refs, we have to distinguish
6754 two cases:
6755 - expand_expr has used TARGET. In this case, we must not generate
6756 another copy. This can be detected by TARGET being equal according
6757 to == .
6758 - expand_expr has not used TARGET - that means that the source just
6759 happens to have the same RTX form. Since temp will have been created
6760 by expand_expr, it will compare unequal according to == .
6761 We must generate a copy in this case, to reach the correct number
6762 of volatile memory references. */
6764 if ((! rtx_equal_p (temp, target)
6765 || (temp != target && (side_effects_p (temp)
6766 || side_effects_p (target)
6767 || (MEM_P (temp)
6768 && !mems_same_for_tbaa_p (temp, target)))))
6769 && TREE_CODE (exp) != ERROR_MARK
6770 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6771 but TARGET is not valid memory reference, TEMP will differ
6772 from TARGET although it is really the same location. */
6773 && !(alt_rtl
6774 && rtx_equal_p (alt_rtl, target)
6775 && !side_effects_p (alt_rtl)
6776 && !side_effects_p (target))
6777 /* If there's nothing to copy, don't bother. Don't call
6778 expr_size unless necessary, because some front-ends (C++)
6779 expr_size-hook must not be given objects that are not
6780 supposed to be bit-copied or bit-initialized. */
6781 && expr_size (exp) != const0_rtx)
6783 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6785 gcc_assert (!shortened_string_cst);
6786 if (GET_MODE (target) == BLKmode)
6788 /* Handle calls that return BLKmode values in registers. */
6789 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6790 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6791 else
6792 store_bit_field (target,
6793 rtx_to_poly_int64 (expr_size (exp))
6794 * BITS_PER_UNIT,
6795 0, 0, 0, GET_MODE (temp), temp, reverse,
6796 false);
6798 else
6799 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6802 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6804 /* Handle copying a string constant into an array. The string
6805 constant may be shorter than the array. So copy just the string's
6806 actual length, and clear the rest. First get the size of the data
6807 type of the string, which is actually the size of the target. */
6808 rtx size = expr_size (exp);
6810 if (CONST_INT_P (size)
6811 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6812 emit_block_move (target, temp, size,
6813 (call_param_p
6814 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6815 else
6817 machine_mode pointer_mode
6818 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6819 machine_mode address_mode = get_address_mode (target);
6821 /* Compute the size of the data to copy from the string. */
6822 tree copy_size
6823 = size_binop_loc (loc, MIN_EXPR,
6824 make_tree (sizetype, size),
6825 size_int (TREE_STRING_LENGTH (exp)));
6826 rtx copy_size_rtx
6827 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6828 (call_param_p
6829 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6830 rtx_code_label *label = 0;
6832 /* Copy that much. */
6833 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6834 TYPE_UNSIGNED (sizetype));
6835 emit_block_move (target, temp, copy_size_rtx,
6836 (call_param_p
6837 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6839 /* Figure out how much is left in TARGET that we have to clear.
6840 Do all calculations in pointer_mode. */
6841 poly_int64 const_copy_size;
6842 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6844 size = plus_constant (address_mode, size, -const_copy_size);
6845 target = adjust_address (target, BLKmode, const_copy_size);
6847 else
6849 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6850 copy_size_rtx, NULL_RTX, 0,
6851 OPTAB_LIB_WIDEN);
6853 if (GET_MODE (copy_size_rtx) != address_mode)
6854 copy_size_rtx = convert_to_mode (address_mode,
6855 copy_size_rtx,
6856 TYPE_UNSIGNED (sizetype));
6858 target = offset_address (target, copy_size_rtx,
6859 highest_pow2_factor (copy_size));
6860 label = gen_label_rtx ();
6861 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6862 GET_MODE (size), 0, label);
6865 if (size != const0_rtx)
6866 clear_storage (target, size, BLOCK_OP_NORMAL);
6868 if (label)
6869 emit_label (label);
6872 else if (shortened_string_cst)
6873 gcc_unreachable ();
6874 /* Handle calls that return values in multiple non-contiguous locations.
6875 The Irix 6 ABI has examples of this. */
6876 else if (GET_CODE (target) == PARALLEL)
6878 if (GET_CODE (temp) == PARALLEL)
6879 emit_group_move (target, temp);
6880 else
6881 emit_group_load (target, temp, TREE_TYPE (exp),
6882 int_size_in_bytes (TREE_TYPE (exp)));
6884 else if (GET_CODE (temp) == PARALLEL)
6885 emit_group_store (target, temp, TREE_TYPE (exp),
6886 int_size_in_bytes (TREE_TYPE (exp)));
6887 else if (GET_MODE (temp) == BLKmode)
6888 emit_block_move (target, temp, expr_size (exp),
6889 (call_param_p
6890 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6891 /* If we emit a nontemporal store, there is nothing else to do. */
6892 else if (nontemporal && emit_storent_insn (target, temp))
6894 else
6896 if (reverse)
6897 temp = flip_storage_order (GET_MODE (target), temp);
6898 temp = force_operand (temp, target);
6899 if (temp != target)
6900 emit_move_insn (target, temp);
6903 else
6904 gcc_assert (!shortened_string_cst);
6906 return NULL_RTX;
6909 /* Return true if field F of structure TYPE is a flexible array. */
6911 static bool
6912 flexible_array_member_p (const_tree f, const_tree type)
6914 const_tree tf;
6916 tf = TREE_TYPE (f);
6917 return (DECL_CHAIN (f) == NULL
6918 && TREE_CODE (tf) == ARRAY_TYPE
6919 && TYPE_DOMAIN (tf)
6920 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6921 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6922 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6923 && int_size_in_bytes (type) >= 0);
6926 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6927 must have in order for it to completely initialize a value of type TYPE.
6928 Return -1 if the number isn't known.
6930 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6932 static HOST_WIDE_INT
6933 count_type_elements (const_tree type, bool for_ctor_p)
6935 switch (TREE_CODE (type))
6937 case ARRAY_TYPE:
6939 tree nelts;
6941 nelts = array_type_nelts (type);
6942 if (nelts && tree_fits_uhwi_p (nelts))
6944 unsigned HOST_WIDE_INT n;
6946 n = tree_to_uhwi (nelts) + 1;
6947 if (n == 0 || for_ctor_p)
6948 return n;
6949 else
6950 return n * count_type_elements (TREE_TYPE (type), false);
6952 return for_ctor_p ? -1 : 1;
6955 case RECORD_TYPE:
6957 unsigned HOST_WIDE_INT n;
6958 tree f;
6960 n = 0;
6961 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6962 if (TREE_CODE (f) == FIELD_DECL)
6964 if (!for_ctor_p)
6965 n += count_type_elements (TREE_TYPE (f), false);
6966 else if (!flexible_array_member_p (f, type))
6967 /* Don't count flexible arrays, which are not supposed
6968 to be initialized. */
6969 n += 1;
6972 return n;
6975 case UNION_TYPE:
6976 case QUAL_UNION_TYPE:
6978 tree f;
6979 HOST_WIDE_INT n, m;
6981 gcc_assert (!for_ctor_p);
6982 /* Estimate the number of scalars in each field and pick the
6983 maximum. Other estimates would do instead; the idea is simply
6984 to make sure that the estimate is not sensitive to the ordering
6985 of the fields. */
6986 n = 1;
6987 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6988 if (TREE_CODE (f) == FIELD_DECL)
6990 m = count_type_elements (TREE_TYPE (f), false);
6991 /* If the field doesn't span the whole union, add an extra
6992 scalar for the rest. */
6993 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6994 TYPE_SIZE (type)) != 1)
6995 m++;
6996 if (n < m)
6997 n = m;
6999 return n;
7002 case COMPLEX_TYPE:
7003 return 2;
7005 case VECTOR_TYPE:
7007 unsigned HOST_WIDE_INT nelts;
7008 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
7009 return nelts;
7010 else
7011 return -1;
7014 case INTEGER_TYPE:
7015 case REAL_TYPE:
7016 case FIXED_POINT_TYPE:
7017 case ENUMERAL_TYPE:
7018 case BOOLEAN_TYPE:
7019 case POINTER_TYPE:
7020 case OFFSET_TYPE:
7021 case REFERENCE_TYPE:
7022 case NULLPTR_TYPE:
7023 case OPAQUE_TYPE:
7024 case BITINT_TYPE:
7025 return 1;
7027 case ERROR_MARK:
7028 return 0;
7030 case VOID_TYPE:
7031 case METHOD_TYPE:
7032 case FUNCTION_TYPE:
7033 case LANG_TYPE:
7034 default:
7035 gcc_unreachable ();
7039 /* Helper for categorize_ctor_elements. Identical interface. */
7041 static bool
7042 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7043 HOST_WIDE_INT *p_unique_nz_elts,
7044 HOST_WIDE_INT *p_init_elts, bool *p_complete)
7046 unsigned HOST_WIDE_INT idx;
7047 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
7048 tree value, purpose, elt_type;
7050 /* Whether CTOR is a valid constant initializer, in accordance with what
7051 initializer_constant_valid_p does. If inferred from the constructor
7052 elements, true until proven otherwise. */
7053 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
7054 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
7056 nz_elts = 0;
7057 unique_nz_elts = 0;
7058 init_elts = 0;
7059 num_fields = 0;
7060 elt_type = NULL_TREE;
7062 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
7064 HOST_WIDE_INT mult = 1;
7066 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
7068 tree lo_index = TREE_OPERAND (purpose, 0);
7069 tree hi_index = TREE_OPERAND (purpose, 1);
7071 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
7072 mult = (tree_to_uhwi (hi_index)
7073 - tree_to_uhwi (lo_index) + 1);
7075 num_fields += mult;
7076 elt_type = TREE_TYPE (value);
7078 switch (TREE_CODE (value))
7080 case CONSTRUCTOR:
7082 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
7084 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
7085 &ic, p_complete);
7087 nz_elts += mult * nz;
7088 unique_nz_elts += unz;
7089 init_elts += mult * ic;
7091 if (const_from_elts_p && const_p)
7092 const_p = const_elt_p;
7094 break;
7096 case INTEGER_CST:
7097 case REAL_CST:
7098 case FIXED_CST:
7099 if (!initializer_zerop (value))
7101 nz_elts += mult;
7102 unique_nz_elts++;
7104 init_elts += mult;
7105 break;
7107 case STRING_CST:
7108 nz_elts += mult * TREE_STRING_LENGTH (value);
7109 unique_nz_elts += TREE_STRING_LENGTH (value);
7110 init_elts += mult * TREE_STRING_LENGTH (value);
7111 break;
7113 case COMPLEX_CST:
7114 if (!initializer_zerop (TREE_REALPART (value)))
7116 nz_elts += mult;
7117 unique_nz_elts++;
7119 if (!initializer_zerop (TREE_IMAGPART (value)))
7121 nz_elts += mult;
7122 unique_nz_elts++;
7124 init_elts += 2 * mult;
7125 break;
7127 case VECTOR_CST:
7129 /* We can only construct constant-length vectors using
7130 CONSTRUCTOR. */
7131 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
7132 for (unsigned int i = 0; i < nunits; ++i)
7134 tree v = VECTOR_CST_ELT (value, i);
7135 if (!initializer_zerop (v))
7137 nz_elts += mult;
7138 unique_nz_elts++;
7140 init_elts += mult;
7143 break;
7145 default:
7147 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
7148 nz_elts += mult * tc;
7149 unique_nz_elts += tc;
7150 init_elts += mult * tc;
7152 if (const_from_elts_p && const_p)
7153 const_p
7154 = initializer_constant_valid_p (value,
7155 elt_type,
7156 TYPE_REVERSE_STORAGE_ORDER
7157 (TREE_TYPE (ctor)))
7158 != NULL_TREE;
7160 break;
7164 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
7165 num_fields, elt_type))
7166 *p_complete = false;
7168 *p_nz_elts += nz_elts;
7169 *p_unique_nz_elts += unique_nz_elts;
7170 *p_init_elts += init_elts;
7172 return const_p;
7175 /* Examine CTOR to discover:
7176 * how many scalar fields are set to nonzero values,
7177 and place it in *P_NZ_ELTS;
7178 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
7179 high - low + 1 (this can be useful for callers to determine ctors
7180 that could be cheaply initialized with - perhaps nested - loops
7181 compared to copied from huge read-only data),
7182 and place it in *P_UNIQUE_NZ_ELTS;
7183 * how many scalar fields in total are in CTOR,
7184 and place it in *P_ELT_COUNT.
7185 * whether the constructor is complete -- in the sense that every
7186 meaningful byte is explicitly given a value --
7187 and place it in *P_COMPLETE.
7189 Return whether or not CTOR is a valid static constant initializer, the same
7190 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
7192 bool
7193 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7194 HOST_WIDE_INT *p_unique_nz_elts,
7195 HOST_WIDE_INT *p_init_elts, bool *p_complete)
7197 *p_nz_elts = 0;
7198 *p_unique_nz_elts = 0;
7199 *p_init_elts = 0;
7200 *p_complete = true;
7202 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
7203 p_init_elts, p_complete);
7206 /* Return true if constructor CTOR is simple enough to be materialized
7207 in an integer mode register. Limit the size to WORDS words, which
7208 is 1 by default. */
7210 bool
7211 immediate_const_ctor_p (const_tree ctor, unsigned int words)
7213 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
7214 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
7215 return false;
7217 return TREE_CONSTANT (ctor)
7218 && !TREE_ADDRESSABLE (ctor)
7219 && CONSTRUCTOR_NELTS (ctor)
7220 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
7221 && int_expr_size (ctor) <= words * UNITS_PER_WORD
7222 && initializer_constant_valid_for_bitfield_p (ctor);
7225 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
7226 of which had type LAST_TYPE. Each element was itself a complete
7227 initializer, in the sense that every meaningful byte was explicitly
7228 given a value. Return true if the same is true for the constructor
7229 as a whole. */
7231 bool
7232 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
7233 const_tree last_type)
7235 if (TREE_CODE (type) == UNION_TYPE
7236 || TREE_CODE (type) == QUAL_UNION_TYPE)
7238 if (num_elts == 0)
7239 return false;
7241 gcc_assert (num_elts == 1 && last_type);
7243 /* ??? We could look at each element of the union, and find the
7244 largest element. Which would avoid comparing the size of the
7245 initialized element against any tail padding in the union.
7246 Doesn't seem worth the effort... */
7247 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
7250 return count_type_elements (type, true) == num_elts;
7253 /* Return true if EXP contains mostly (3/4) zeros. */
7255 static bool
7256 mostly_zeros_p (const_tree exp)
7258 if (TREE_CODE (exp) == CONSTRUCTOR)
7260 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7261 bool complete_p;
7263 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7264 &complete_p);
7265 return !complete_p || nz_elts < init_elts / 4;
7268 return initializer_zerop (exp);
7271 /* Return true if EXP contains all zeros. */
7273 static bool
7274 all_zeros_p (const_tree exp)
7276 if (TREE_CODE (exp) == CONSTRUCTOR)
7278 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7279 bool complete_p;
7281 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7282 &complete_p);
7283 return nz_elts == 0;
7286 return initializer_zerop (exp);
7289 /* Helper function for store_constructor.
7290 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
7291 CLEARED is as for store_constructor.
7292 ALIAS_SET is the alias set to use for any stores.
7293 If REVERSE is true, the store is to be done in reverse order.
7295 This provides a recursive shortcut back to store_constructor when it isn't
7296 necessary to go through store_field. This is so that we can pass through
7297 the cleared field to let store_constructor know that we may not have to
7298 clear a substructure if the outer structure has already been cleared. */
7300 static void
7301 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
7302 poly_uint64 bitregion_start,
7303 poly_uint64 bitregion_end,
7304 machine_mode mode,
7305 tree exp, int cleared,
7306 alias_set_type alias_set, bool reverse)
7308 poly_int64 bytepos;
7309 poly_uint64 bytesize;
7310 if (TREE_CODE (exp) == CONSTRUCTOR
7311 /* We can only call store_constructor recursively if the size and
7312 bit position are on a byte boundary. */
7313 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
7314 && maybe_ne (bitsize, 0U)
7315 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
7316 /* If we have a nonzero bitpos for a register target, then we just
7317 let store_field do the bitfield handling. This is unlikely to
7318 generate unnecessary clear instructions anyways. */
7319 && (known_eq (bitpos, 0) || MEM_P (target)))
7321 if (MEM_P (target))
7323 machine_mode target_mode = GET_MODE (target);
7324 if (target_mode != BLKmode
7325 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
7326 target_mode = BLKmode;
7327 target = adjust_address (target, target_mode, bytepos);
7331 /* Update the alias set, if required. */
7332 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
7333 && MEM_ALIAS_SET (target) != 0)
7335 target = copy_rtx (target);
7336 set_mem_alias_set (target, alias_set);
7339 store_constructor (exp, target, cleared, bytesize, reverse);
7341 else
7342 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
7343 exp, alias_set, false, reverse);
7347 /* Returns the number of FIELD_DECLs in TYPE. */
7349 static int
7350 fields_length (const_tree type)
7352 tree t = TYPE_FIELDS (type);
7353 int count = 0;
7355 for (; t; t = DECL_CHAIN (t))
7356 if (TREE_CODE (t) == FIELD_DECL)
7357 ++count;
7359 return count;
7363 /* Store the value of constructor EXP into the rtx TARGET.
7364 TARGET is either a REG or a MEM; we know it cannot conflict, since
7365 safe_from_p has been called.
7366 CLEARED is true if TARGET is known to have been zero'd.
7367 SIZE is the number of bytes of TARGET we are allowed to modify: this
7368 may not be the same as the size of EXP if we are assigning to a field
7369 which has been packed to exclude padding bits.
7370 If REVERSE is true, the store is to be done in reverse order. */
7372 void
7373 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
7374 bool reverse)
7376 tree type = TREE_TYPE (exp);
7377 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
7378 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
7380 switch (TREE_CODE (type))
7382 case RECORD_TYPE:
7383 case UNION_TYPE:
7384 case QUAL_UNION_TYPE:
7386 unsigned HOST_WIDE_INT idx;
7387 tree field, value;
7389 /* The storage order is specified for every aggregate type. */
7390 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7392 /* If size is zero or the target is already cleared, do nothing. */
7393 if (known_eq (size, 0) || cleared)
7394 cleared = 1;
7395 /* We either clear the aggregate or indicate the value is dead. */
7396 else if ((TREE_CODE (type) == UNION_TYPE
7397 || TREE_CODE (type) == QUAL_UNION_TYPE)
7398 && ! CONSTRUCTOR_ELTS (exp))
7399 /* If the constructor is empty, clear the union. */
7401 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7402 cleared = 1;
7405 /* If we are building a static constructor into a register,
7406 set the initial value as zero so we can fold the value into
7407 a constant. But if more than one register is involved,
7408 this probably loses. */
7409 else if (REG_P (target) && TREE_STATIC (exp)
7410 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7411 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7413 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7414 cleared = 1;
7417 /* If the constructor has fewer fields than the structure or
7418 if we are initializing the structure to mostly zeros, clear
7419 the whole structure first. Don't do this if TARGET is a
7420 register whose mode size isn't equal to SIZE since
7421 clear_storage can't handle this case. */
7422 else if (known_size_p (size)
7423 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7424 || mostly_zeros_p (exp))
7425 && (!REG_P (target)
7426 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7428 clear_storage (target, gen_int_mode (size, Pmode),
7429 BLOCK_OP_NORMAL);
7430 cleared = 1;
7433 if (REG_P (target) && !cleared)
7434 emit_clobber (target);
7436 /* Store each element of the constructor into the
7437 corresponding field of TARGET. */
7438 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7440 machine_mode mode;
7441 HOST_WIDE_INT bitsize;
7442 HOST_WIDE_INT bitpos = 0;
7443 tree offset;
7444 rtx to_rtx = target;
7446 /* Just ignore missing fields. We cleared the whole
7447 structure, above, if any fields are missing. */
7448 if (field == 0)
7449 continue;
7451 if (cleared && initializer_zerop (value))
7452 continue;
7454 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7455 bitsize = tree_to_uhwi (DECL_SIZE (field));
7456 else
7457 gcc_unreachable ();
7459 mode = DECL_MODE (field);
7460 if (DECL_BIT_FIELD (field))
7461 mode = VOIDmode;
7463 offset = DECL_FIELD_OFFSET (field);
7464 if (tree_fits_shwi_p (offset)
7465 && tree_fits_shwi_p (bit_position (field)))
7467 bitpos = int_bit_position (field);
7468 offset = NULL_TREE;
7470 else
7471 gcc_unreachable ();
7473 /* If this initializes a field that is smaller than a
7474 word, at the start of a word, try to widen it to a full
7475 word. This special case allows us to output C++ member
7476 function initializations in a form that the optimizers
7477 can understand. */
7478 if (WORD_REGISTER_OPERATIONS
7479 && REG_P (target)
7480 && bitsize < BITS_PER_WORD
7481 && bitpos % BITS_PER_WORD == 0
7482 && GET_MODE_CLASS (mode) == MODE_INT
7483 && TREE_CODE (value) == INTEGER_CST
7484 && exp_size >= 0
7485 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7487 type = TREE_TYPE (value);
7489 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7491 type = lang_hooks.types.type_for_mode
7492 (word_mode, TYPE_UNSIGNED (type));
7493 value = fold_convert (type, value);
7494 /* Make sure the bits beyond the original bitsize are zero
7495 so that we can correctly avoid extra zeroing stores in
7496 later constructor elements. */
7497 tree bitsize_mask
7498 = wide_int_to_tree (type, wi::mask (bitsize, false,
7499 BITS_PER_WORD));
7500 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7503 if (BYTES_BIG_ENDIAN)
7504 value
7505 = fold_build2 (LSHIFT_EXPR, type, value,
7506 build_int_cst (type,
7507 BITS_PER_WORD - bitsize));
7508 bitsize = BITS_PER_WORD;
7509 mode = word_mode;
7512 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7513 && DECL_NONADDRESSABLE_P (field))
7515 to_rtx = copy_rtx (to_rtx);
7516 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7519 store_constructor_field (to_rtx, bitsize, bitpos,
7520 0, bitregion_end, mode,
7521 value, cleared,
7522 get_alias_set (TREE_TYPE (field)),
7523 reverse);
7525 break;
7527 case ARRAY_TYPE:
7529 tree value, index;
7530 unsigned HOST_WIDE_INT i;
7531 bool need_to_clear;
7532 tree domain;
7533 tree elttype = TREE_TYPE (type);
7534 bool const_bounds_p;
7535 HOST_WIDE_INT minelt = 0;
7536 HOST_WIDE_INT maxelt = 0;
7538 /* The storage order is specified for every aggregate type. */
7539 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7541 domain = TYPE_DOMAIN (type);
7542 const_bounds_p = (TYPE_MIN_VALUE (domain)
7543 && TYPE_MAX_VALUE (domain)
7544 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7545 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7547 /* If we have constant bounds for the range of the type, get them. */
7548 if (const_bounds_p)
7550 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7551 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7554 /* If the constructor has fewer elements than the array, clear
7555 the whole array first. Similarly if this is static
7556 constructor of a non-BLKmode object. */
7557 if (cleared)
7558 need_to_clear = false;
7559 else if (REG_P (target) && TREE_STATIC (exp))
7560 need_to_clear = true;
7561 else
7563 unsigned HOST_WIDE_INT idx;
7564 HOST_WIDE_INT count = 0, zero_count = 0;
7565 need_to_clear = ! const_bounds_p;
7567 /* This loop is a more accurate version of the loop in
7568 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7569 is also needed to check for missing elements. */
7570 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7572 HOST_WIDE_INT this_node_count;
7574 if (need_to_clear)
7575 break;
7577 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7579 tree lo_index = TREE_OPERAND (index, 0);
7580 tree hi_index = TREE_OPERAND (index, 1);
7582 if (! tree_fits_uhwi_p (lo_index)
7583 || ! tree_fits_uhwi_p (hi_index))
7585 need_to_clear = true;
7586 break;
7589 this_node_count = (tree_to_uhwi (hi_index)
7590 - tree_to_uhwi (lo_index) + 1);
7592 else
7593 this_node_count = 1;
7595 count += this_node_count;
7596 if (mostly_zeros_p (value))
7597 zero_count += this_node_count;
7600 /* Clear the entire array first if there are any missing
7601 elements, or if the incidence of zero elements is >=
7602 75%. */
7603 if (! need_to_clear
7604 && (count < maxelt - minelt + 1
7605 || 4 * zero_count >= 3 * count))
7606 need_to_clear = true;
7609 if (need_to_clear && maybe_gt (size, 0))
7611 if (REG_P (target))
7612 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7613 else
7614 clear_storage (target, gen_int_mode (size, Pmode),
7615 BLOCK_OP_NORMAL);
7616 cleared = 1;
7619 if (!cleared && REG_P (target))
7620 /* Inform later passes that the old value is dead. */
7621 emit_clobber (target);
7623 /* Store each element of the constructor into the
7624 corresponding element of TARGET, determined by counting the
7625 elements. */
7626 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7628 machine_mode mode;
7629 poly_int64 bitsize;
7630 HOST_WIDE_INT bitpos;
7631 rtx xtarget = target;
7633 if (cleared && initializer_zerop (value))
7634 continue;
7636 mode = TYPE_MODE (elttype);
7637 if (mode != BLKmode)
7638 bitsize = GET_MODE_BITSIZE (mode);
7639 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7640 bitsize = -1;
7642 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7644 tree lo_index = TREE_OPERAND (index, 0);
7645 tree hi_index = TREE_OPERAND (index, 1);
7646 rtx index_r, pos_rtx;
7647 HOST_WIDE_INT lo, hi, count;
7648 tree position;
7650 /* If the range is constant and "small", unroll the loop. */
7651 if (const_bounds_p
7652 && tree_fits_shwi_p (lo_index)
7653 && tree_fits_shwi_p (hi_index)
7654 && (lo = tree_to_shwi (lo_index),
7655 hi = tree_to_shwi (hi_index),
7656 count = hi - lo + 1,
7657 (!MEM_P (target)
7658 || count <= 2
7659 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7660 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7661 <= 40 * 8)))))
7663 lo -= minelt; hi -= minelt;
7664 for (; lo <= hi; lo++)
7666 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7668 if (MEM_P (target)
7669 && !MEM_KEEP_ALIAS_SET_P (target)
7670 && TREE_CODE (type) == ARRAY_TYPE
7671 && TYPE_NONALIASED_COMPONENT (type))
7673 target = copy_rtx (target);
7674 MEM_KEEP_ALIAS_SET_P (target) = 1;
7677 store_constructor_field
7678 (target, bitsize, bitpos, 0, bitregion_end,
7679 mode, value, cleared,
7680 get_alias_set (elttype), reverse);
7683 else
7685 rtx_code_label *loop_start = gen_label_rtx ();
7686 rtx_code_label *loop_end = gen_label_rtx ();
7687 tree exit_cond;
7689 expand_normal (hi_index);
7691 index = build_decl (EXPR_LOCATION (exp),
7692 VAR_DECL, NULL_TREE, domain);
7693 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7694 SET_DECL_RTL (index, index_r);
7695 store_expr (lo_index, index_r, 0, false, reverse);
7697 /* Build the head of the loop. */
7698 do_pending_stack_adjust ();
7699 emit_label (loop_start);
7701 /* Assign value to element index. */
7702 position =
7703 fold_convert (ssizetype,
7704 fold_build2 (MINUS_EXPR,
7705 TREE_TYPE (index),
7706 index,
7707 TYPE_MIN_VALUE (domain)));
7709 position =
7710 size_binop (MULT_EXPR, position,
7711 fold_convert (ssizetype,
7712 TYPE_SIZE_UNIT (elttype)));
7714 pos_rtx = expand_normal (position);
7715 xtarget = offset_address (target, pos_rtx,
7716 highest_pow2_factor (position));
7717 xtarget = adjust_address (xtarget, mode, 0);
7718 if (TREE_CODE (value) == CONSTRUCTOR)
7719 store_constructor (value, xtarget, cleared,
7720 exact_div (bitsize, BITS_PER_UNIT),
7721 reverse);
7722 else
7723 store_expr (value, xtarget, 0, false, reverse);
7725 /* Generate a conditional jump to exit the loop. */
7726 exit_cond = build2 (LT_EXPR, integer_type_node,
7727 index, hi_index);
7728 jumpif (exit_cond, loop_end,
7729 profile_probability::uninitialized ());
7731 /* Update the loop counter, and jump to the head of
7732 the loop. */
7733 expand_assignment (index,
7734 build2 (PLUS_EXPR, TREE_TYPE (index),
7735 index, integer_one_node),
7736 false);
7738 emit_jump (loop_start);
7740 /* Build the end of the loop. */
7741 emit_label (loop_end);
7744 else if ((index != 0 && ! tree_fits_shwi_p (index))
7745 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7747 tree position;
7749 if (index == 0)
7750 index = ssize_int (1);
7752 if (minelt)
7753 index = fold_convert (ssizetype,
7754 fold_build2 (MINUS_EXPR,
7755 TREE_TYPE (index),
7756 index,
7757 TYPE_MIN_VALUE (domain)));
7759 position =
7760 size_binop (MULT_EXPR, index,
7761 fold_convert (ssizetype,
7762 TYPE_SIZE_UNIT (elttype)));
7763 xtarget = offset_address (target,
7764 expand_normal (position),
7765 highest_pow2_factor (position));
7766 xtarget = adjust_address (xtarget, mode, 0);
7767 store_expr (value, xtarget, 0, false, reverse);
7769 else
7771 if (index != 0)
7772 bitpos = ((tree_to_shwi (index) - minelt)
7773 * tree_to_uhwi (TYPE_SIZE (elttype)));
7774 else
7775 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7777 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7778 && TREE_CODE (type) == ARRAY_TYPE
7779 && TYPE_NONALIASED_COMPONENT (type))
7781 target = copy_rtx (target);
7782 MEM_KEEP_ALIAS_SET_P (target) = 1;
7784 store_constructor_field (target, bitsize, bitpos, 0,
7785 bitregion_end, mode, value,
7786 cleared, get_alias_set (elttype),
7787 reverse);
7790 break;
7793 case VECTOR_TYPE:
7795 unsigned HOST_WIDE_INT idx;
7796 constructor_elt *ce;
7797 int i;
7798 bool need_to_clear;
7799 insn_code icode = CODE_FOR_nothing;
7800 tree elt;
7801 tree elttype = TREE_TYPE (type);
7802 int elt_size = vector_element_bits (type);
7803 machine_mode eltmode = TYPE_MODE (elttype);
7804 HOST_WIDE_INT bitsize;
7805 HOST_WIDE_INT bitpos;
7806 rtvec vector = NULL;
7807 poly_uint64 n_elts;
7808 unsigned HOST_WIDE_INT const_n_elts;
7809 alias_set_type alias;
7810 bool vec_vec_init_p = false;
7811 machine_mode mode = GET_MODE (target);
7813 gcc_assert (eltmode != BLKmode);
7815 /* Try using vec_duplicate_optab for uniform vectors. */
7816 if (!TREE_SIDE_EFFECTS (exp)
7817 && VECTOR_MODE_P (mode)
7818 && eltmode == GET_MODE_INNER (mode)
7819 && ((icode = optab_handler (vec_duplicate_optab, mode))
7820 != CODE_FOR_nothing)
7821 && (elt = uniform_vector_p (exp))
7822 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7824 class expand_operand ops[2];
7825 create_output_operand (&ops[0], target, mode);
7826 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7827 expand_insn (icode, 2, ops);
7828 if (!rtx_equal_p (target, ops[0].value))
7829 emit_move_insn (target, ops[0].value);
7830 break;
7832 /* Use sign-extension for uniform boolean vectors with
7833 integer modes. Effectively "vec_duplicate" for bitmasks. */
7834 if (!TREE_SIDE_EFFECTS (exp)
7835 && VECTOR_BOOLEAN_TYPE_P (type)
7836 && SCALAR_INT_MODE_P (mode)
7837 && (elt = uniform_vector_p (exp))
7838 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7840 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
7841 expand_normal (elt));
7842 rtx tmp = gen_reg_rtx (mode);
7843 convert_move (tmp, op0, 0);
7845 /* Ensure no excess bits are set.
7846 GCN needs this for nunits < 64.
7847 x86 needs this for nunits < 8. */
7848 auto nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
7849 if (maybe_ne (GET_MODE_PRECISION (mode), nunits))
7850 tmp = expand_binop (mode, and_optab, tmp,
7851 GEN_INT ((1 << nunits) - 1), target,
7852 true, OPTAB_WIDEN);
7853 if (tmp != target)
7854 emit_move_insn (target, tmp);
7855 break;
7858 n_elts = TYPE_VECTOR_SUBPARTS (type);
7859 if (REG_P (target)
7860 && VECTOR_MODE_P (mode)
7861 && n_elts.is_constant (&const_n_elts))
7863 machine_mode emode = eltmode;
7864 bool vector_typed_elts_p = false;
7866 if (CONSTRUCTOR_NELTS (exp)
7867 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7868 == VECTOR_TYPE))
7870 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7871 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7872 * TYPE_VECTOR_SUBPARTS (etype),
7873 n_elts));
7874 emode = TYPE_MODE (etype);
7875 vector_typed_elts_p = true;
7877 icode = convert_optab_handler (vec_init_optab, mode, emode);
7878 if (icode != CODE_FOR_nothing)
7880 unsigned int n = const_n_elts;
7882 if (vector_typed_elts_p)
7884 n = CONSTRUCTOR_NELTS (exp);
7885 vec_vec_init_p = true;
7887 vector = rtvec_alloc (n);
7888 for (unsigned int k = 0; k < n; k++)
7889 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7893 /* Compute the size of the elements in the CTOR. It differs
7894 from the size of the vector type elements only when the
7895 CTOR elements are vectors themselves. */
7896 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7897 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7898 : elttype);
7899 if (VECTOR_TYPE_P (val_type))
7900 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7901 else
7902 bitsize = elt_size;
7904 /* If the constructor has fewer elements than the vector,
7905 clear the whole array first. Similarly if this is static
7906 constructor of a non-BLKmode object. */
7907 if (cleared)
7908 need_to_clear = false;
7909 else if (REG_P (target) && TREE_STATIC (exp))
7910 need_to_clear = true;
7911 else
7913 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7914 tree value;
7916 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7918 int n_elts_here = bitsize / elt_size;
7919 count += n_elts_here;
7920 if (mostly_zeros_p (value))
7921 zero_count += n_elts_here;
7924 /* Clear the entire vector first if there are any missing elements,
7925 or if the incidence of zero elements is >= 75%. */
7926 need_to_clear = (maybe_lt (count, n_elts)
7927 || 4 * zero_count >= 3 * count);
7930 if (need_to_clear && maybe_gt (size, 0) && !vector)
7932 if (REG_P (target))
7933 emit_move_insn (target, CONST0_RTX (mode));
7934 else
7935 clear_storage (target, gen_int_mode (size, Pmode),
7936 BLOCK_OP_NORMAL);
7937 cleared = 1;
7940 /* Inform later passes that the old value is dead. */
7941 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
7943 emit_move_insn (target, CONST0_RTX (mode));
7944 cleared = 1;
7947 if (MEM_P (target))
7948 alias = MEM_ALIAS_SET (target);
7949 else
7950 alias = get_alias_set (elttype);
7952 /* Store each element of the constructor into the corresponding
7953 element of TARGET, determined by counting the elements. */
7954 for (idx = 0, i = 0;
7955 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7956 idx++, i += bitsize / elt_size)
7958 HOST_WIDE_INT eltpos;
7959 tree value = ce->value;
7961 if (cleared && initializer_zerop (value))
7962 continue;
7964 if (ce->index)
7965 eltpos = tree_to_uhwi (ce->index);
7966 else
7967 eltpos = i;
7969 if (vector)
7971 if (vec_vec_init_p)
7973 gcc_assert (ce->index == NULL_TREE);
7974 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7975 eltpos = idx;
7977 else
7978 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7979 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7981 else
7983 machine_mode value_mode
7984 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7985 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7986 bitpos = eltpos * elt_size;
7987 store_constructor_field (target, bitsize, bitpos, 0,
7988 bitregion_end, value_mode,
7989 value, cleared, alias, reverse);
7993 if (vector)
7994 emit_insn (GEN_FCN (icode) (target,
7995 gen_rtx_PARALLEL (mode, vector)));
7996 break;
7999 default:
8000 gcc_unreachable ();
8004 /* Store the value of EXP (an expression tree)
8005 into a subfield of TARGET which has mode MODE and occupies
8006 BITSIZE bits, starting BITPOS bits from the start of TARGET.
8007 If MODE is VOIDmode, it means that we are storing into a bit-field.
8009 BITREGION_START is bitpos of the first bitfield in this region.
8010 BITREGION_END is the bitpos of the ending bitfield in this region.
8011 These two fields are 0, if the C++ memory model does not apply,
8012 or we are not interested in keeping track of bitfield regions.
8014 Always return const0_rtx unless we have something particular to
8015 return.
8017 ALIAS_SET is the alias set for the destination. This value will
8018 (in general) be different from that for TARGET, since TARGET is a
8019 reference to the containing structure.
8021 If NONTEMPORAL is true, try generating a nontemporal store.
8023 If REVERSE is true, the store is to be done in reverse order. */
8025 static rtx
8026 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
8027 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
8028 machine_mode mode, tree exp,
8029 alias_set_type alias_set, bool nontemporal, bool reverse)
8031 if (TREE_CODE (exp) == ERROR_MARK)
8032 return const0_rtx;
8034 /* If we have nothing to store, do nothing unless the expression has
8035 side-effects. Don't do that for zero sized addressable lhs of
8036 calls. */
8037 if (known_eq (bitsize, 0)
8038 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8039 || TREE_CODE (exp) != CALL_EXPR))
8040 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
8042 if (GET_CODE (target) == CONCAT)
8044 /* We're storing into a struct containing a single __complex. */
8046 gcc_assert (known_eq (bitpos, 0));
8047 return store_expr (exp, target, 0, nontemporal, reverse);
8050 /* If the structure is in a register or if the component
8051 is a bit field, we cannot use addressing to access it.
8052 Use bit-field techniques or SUBREG to store in it. */
8054 poly_int64 decl_bitsize;
8055 if (mode == VOIDmode
8056 || (mode != BLKmode && ! direct_store[(int) mode]
8057 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8058 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
8059 || REG_P (target)
8060 || GET_CODE (target) == SUBREG
8061 /* If the field isn't aligned enough to store as an ordinary memref,
8062 store it as a bit field. */
8063 || (mode != BLKmode
8064 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
8065 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
8066 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
8067 || !multiple_p (bitpos, BITS_PER_UNIT)))
8068 || (known_size_p (bitsize)
8069 && mode != BLKmode
8070 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
8071 /* If the RHS and field are a constant size and the size of the
8072 RHS isn't the same size as the bitfield, we must use bitfield
8073 operations. */
8074 || (known_size_p (bitsize)
8075 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
8076 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
8077 bitsize)
8078 /* Except for initialization of full bytes from a CONSTRUCTOR, which
8079 we will handle specially below. */
8080 && !(TREE_CODE (exp) == CONSTRUCTOR
8081 && multiple_p (bitsize, BITS_PER_UNIT))
8082 /* And except for bitwise copying of TREE_ADDRESSABLE types,
8083 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
8084 includes some extra padding. store_expr / expand_expr will in
8085 that case call get_inner_reference that will have the bitsize
8086 we check here and thus the block move will not clobber the
8087 padding that shouldn't be clobbered. In the future we could
8088 replace the TREE_ADDRESSABLE check with a check that
8089 get_base_address needs to live in memory. */
8090 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8091 || TREE_CODE (exp) != COMPONENT_REF
8092 || !multiple_p (bitsize, BITS_PER_UNIT)
8093 || !multiple_p (bitpos, BITS_PER_UNIT)
8094 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
8095 &decl_bitsize)
8096 || maybe_ne (decl_bitsize, bitsize))
8097 /* A call with an addressable return type and return-slot
8098 optimization must not need bitfield operations but we must
8099 pass down the original target. */
8100 && (TREE_CODE (exp) != CALL_EXPR
8101 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
8102 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
8103 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
8104 decl we must use bitfield operations. */
8105 || (known_size_p (bitsize)
8106 && TREE_CODE (exp) == MEM_REF
8107 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
8108 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8109 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8110 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
8112 rtx temp;
8113 gimple *nop_def;
8115 /* If EXP is a NOP_EXPR of precision less than its mode, then that
8116 implies a mask operation. If the precision is the same size as
8117 the field we're storing into, that mask is redundant. This is
8118 particularly common with bit field assignments generated by the
8119 C front end. */
8120 nop_def = get_def_for_expr (exp, NOP_EXPR);
8121 if (nop_def)
8123 tree type = TREE_TYPE (exp);
8124 if (INTEGRAL_TYPE_P (type)
8125 && maybe_ne (TYPE_PRECISION (type),
8126 GET_MODE_BITSIZE (TYPE_MODE (type)))
8127 && known_eq (bitsize, TYPE_PRECISION (type)))
8129 tree op = gimple_assign_rhs1 (nop_def);
8130 type = TREE_TYPE (op);
8131 if (INTEGRAL_TYPE_P (type)
8132 && known_ge (TYPE_PRECISION (type), bitsize))
8133 exp = op;
8137 temp = expand_normal (exp);
8139 /* We don't support variable-sized BLKmode bitfields, since our
8140 handling of BLKmode is bound up with the ability to break
8141 things into words. */
8142 gcc_assert (mode != BLKmode || bitsize.is_constant ());
8144 /* Handle calls that return values in multiple non-contiguous locations.
8145 The Irix 6 ABI has examples of this. */
8146 if (GET_CODE (temp) == PARALLEL)
8148 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
8149 machine_mode temp_mode = GET_MODE (temp);
8150 if (temp_mode == BLKmode || temp_mode == VOIDmode)
8151 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
8152 rtx temp_target = gen_reg_rtx (temp_mode);
8153 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
8154 temp = temp_target;
8157 /* Handle calls that return BLKmode values in registers. */
8158 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
8160 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
8161 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
8162 temp = temp_target;
8165 /* If the value has aggregate type and an integral mode then, if BITSIZE
8166 is narrower than this mode and this is for big-endian data, we first
8167 need to put the value into the low-order bits for store_bit_field,
8168 except when MODE is BLKmode and BITSIZE larger than the word size
8169 (see the handling of fields larger than a word in store_bit_field).
8170 Moreover, the field may be not aligned on a byte boundary; in this
8171 case, if it has reverse storage order, it needs to be accessed as a
8172 scalar field with reverse storage order and we must first put the
8173 value into target order. */
8174 scalar_int_mode temp_mode;
8175 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
8176 && is_int_mode (GET_MODE (temp), &temp_mode))
8178 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
8180 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
8182 if (reverse)
8183 temp = flip_storage_order (temp_mode, temp);
8185 gcc_checking_assert (known_le (bitsize, size));
8186 if (maybe_lt (bitsize, size)
8187 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
8188 /* Use of to_constant for BLKmode was checked above. */
8189 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
8190 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
8191 size - bitsize, NULL_RTX, 1);
8194 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
8195 if (mode != VOIDmode && mode != BLKmode
8196 && mode != TYPE_MODE (TREE_TYPE (exp)))
8197 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
8199 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
8200 and BITPOS must be aligned on a byte boundary. If so, we simply do
8201 a block copy. Likewise for a BLKmode-like TARGET. */
8202 if (GET_MODE (temp) == BLKmode
8203 && (GET_MODE (target) == BLKmode
8204 || (MEM_P (target)
8205 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
8206 && multiple_p (bitpos, BITS_PER_UNIT)
8207 && multiple_p (bitsize, BITS_PER_UNIT))))
8209 gcc_assert (MEM_P (target) && MEM_P (temp));
8210 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8211 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
8213 target = adjust_address (target, VOIDmode, bytepos);
8214 emit_block_move (target, temp,
8215 gen_int_mode (bytesize, Pmode),
8216 BLOCK_OP_NORMAL);
8218 return const0_rtx;
8221 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
8222 word size, we need to load the value (see again store_bit_field). */
8223 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
8225 temp_mode = smallest_int_mode_for_size (bitsize);
8226 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
8227 temp_mode, false, NULL);
8230 /* Store the value in the bitfield. */
8231 gcc_checking_assert (known_ge (bitpos, 0));
8232 store_bit_field (target, bitsize, bitpos,
8233 bitregion_start, bitregion_end,
8234 mode, temp, reverse, false);
8236 return const0_rtx;
8238 else
8240 /* Now build a reference to just the desired component. */
8241 rtx to_rtx = adjust_address (target, mode,
8242 exact_div (bitpos, BITS_PER_UNIT));
8244 if (to_rtx == target)
8245 to_rtx = copy_rtx (to_rtx);
8247 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
8248 set_mem_alias_set (to_rtx, alias_set);
8250 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
8251 into a target smaller than its type; handle that case now. */
8252 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
8254 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
8255 store_constructor (exp, to_rtx, 0, bytesize, reverse);
8256 return to_rtx;
8259 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
8263 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
8264 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
8265 codes and find the ultimate containing object, which we return.
8267 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
8268 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
8269 storage order of the field.
8270 If the position of the field is variable, we store a tree
8271 giving the variable offset (in units) in *POFFSET.
8272 This offset is in addition to the bit position.
8273 If the position is not variable, we store 0 in *POFFSET.
8275 If any of the extraction expressions is volatile,
8276 we store 1 in *PVOLATILEP. Otherwise we don't change that.
8278 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
8279 Otherwise, it is a mode that can be used to access the field.
8281 If the field describes a variable-sized object, *PMODE is set to
8282 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
8283 this case, but the address of the object can be found. */
8285 tree
8286 get_inner_reference (tree exp, poly_int64 *pbitsize,
8287 poly_int64 *pbitpos, tree *poffset,
8288 machine_mode *pmode, int *punsignedp,
8289 int *preversep, int *pvolatilep)
8291 tree size_tree = 0;
8292 machine_mode mode = VOIDmode;
8293 bool blkmode_bitfield = false;
8294 tree offset = size_zero_node;
8295 poly_offset_int bit_offset = 0;
8297 /* First get the mode, signedness, storage order and size. We do this from
8298 just the outermost expression. */
8299 *pbitsize = -1;
8300 if (TREE_CODE (exp) == COMPONENT_REF)
8302 tree field = TREE_OPERAND (exp, 1);
8303 size_tree = DECL_SIZE (field);
8304 if (flag_strict_volatile_bitfields > 0
8305 && TREE_THIS_VOLATILE (exp)
8306 && DECL_BIT_FIELD_TYPE (field)
8307 && DECL_MODE (field) != BLKmode)
8308 /* Volatile bitfields should be accessed in the mode of the
8309 field's type, not the mode computed based on the bit
8310 size. */
8311 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
8312 else if (!DECL_BIT_FIELD (field))
8314 mode = DECL_MODE (field);
8315 /* For vector fields re-check the target flags, as DECL_MODE
8316 could have been set with different target flags than
8317 the current function has. */
8318 if (VECTOR_TYPE_P (TREE_TYPE (field))
8319 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
8320 mode = TYPE_MODE (TREE_TYPE (field));
8322 else if (DECL_MODE (field) == BLKmode)
8323 blkmode_bitfield = true;
8325 *punsignedp = DECL_UNSIGNED (field);
8327 else if (TREE_CODE (exp) == BIT_FIELD_REF)
8329 size_tree = TREE_OPERAND (exp, 1);
8330 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
8331 || TYPE_UNSIGNED (TREE_TYPE (exp)));
8333 /* For vector element types with the correct size of access or for
8334 vector typed accesses use the mode of the access type. */
8335 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
8336 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
8337 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
8338 || VECTOR_TYPE_P (TREE_TYPE (exp)))
8339 mode = TYPE_MODE (TREE_TYPE (exp));
8341 else
8343 mode = TYPE_MODE (TREE_TYPE (exp));
8344 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
8346 if (mode == BLKmode)
8347 size_tree = TYPE_SIZE (TREE_TYPE (exp));
8348 else
8349 *pbitsize = GET_MODE_BITSIZE (mode);
8352 if (size_tree != 0)
8354 if (! tree_fits_uhwi_p (size_tree))
8355 mode = BLKmode, *pbitsize = -1;
8356 else
8357 *pbitsize = tree_to_uhwi (size_tree);
8360 *preversep = reverse_storage_order_for_component_p (exp);
8362 /* Compute cumulative bit-offset for nested component-refs and array-refs,
8363 and find the ultimate containing object. */
8364 while (1)
8366 switch (TREE_CODE (exp))
8368 case BIT_FIELD_REF:
8369 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
8370 break;
8372 case COMPONENT_REF:
8374 tree field = TREE_OPERAND (exp, 1);
8375 tree this_offset = component_ref_field_offset (exp);
8377 /* If this field hasn't been filled in yet, don't go past it.
8378 This should only happen when folding expressions made during
8379 type construction. */
8380 if (this_offset == 0)
8381 break;
8383 offset = size_binop (PLUS_EXPR, offset, this_offset);
8384 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
8386 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
8388 break;
8390 case ARRAY_REF:
8391 case ARRAY_RANGE_REF:
8393 tree index = TREE_OPERAND (exp, 1);
8394 tree low_bound = array_ref_low_bound (exp);
8395 tree unit_size = array_ref_element_size (exp);
8397 /* We assume all arrays have sizes that are a multiple of a byte.
8398 First subtract the lower bound, if any, in the type of the
8399 index, then convert to sizetype and multiply by the size of
8400 the array element. */
8401 if (! integer_zerop (low_bound))
8402 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8403 index, low_bound);
8405 offset = size_binop (PLUS_EXPR, offset,
8406 size_binop (MULT_EXPR,
8407 fold_convert (sizetype, index),
8408 unit_size));
8410 break;
8412 case REALPART_EXPR:
8413 break;
8415 case IMAGPART_EXPR:
8416 bit_offset += *pbitsize;
8417 break;
8419 case VIEW_CONVERT_EXPR:
8420 break;
8422 case MEM_REF:
8423 /* Hand back the decl for MEM[&decl, off]. */
8424 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8426 tree off = TREE_OPERAND (exp, 1);
8427 if (!integer_zerop (off))
8429 poly_offset_int boff = mem_ref_offset (exp);
8430 boff <<= LOG2_BITS_PER_UNIT;
8431 bit_offset += boff;
8433 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8435 goto done;
8437 default:
8438 goto done;
8441 /* If any reference in the chain is volatile, the effect is volatile. */
8442 if (TREE_THIS_VOLATILE (exp))
8443 *pvolatilep = 1;
8445 exp = TREE_OPERAND (exp, 0);
8447 done:
8449 /* If OFFSET is constant, see if we can return the whole thing as a
8450 constant bit position. Make sure to handle overflow during
8451 this conversion. */
8452 if (poly_int_tree_p (offset))
8454 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8455 TYPE_PRECISION (sizetype));
8456 tem <<= LOG2_BITS_PER_UNIT;
8457 tem += bit_offset;
8458 if (tem.to_shwi (pbitpos))
8459 *poffset = offset = NULL_TREE;
8462 /* Otherwise, split it up. */
8463 if (offset)
8465 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8466 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8468 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8469 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8470 offset = size_binop (PLUS_EXPR, offset,
8471 build_int_cst (sizetype, bytes.force_shwi ()));
8474 *poffset = offset;
8477 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8478 if (mode == VOIDmode
8479 && blkmode_bitfield
8480 && multiple_p (*pbitpos, BITS_PER_UNIT)
8481 && multiple_p (*pbitsize, BITS_PER_UNIT))
8482 *pmode = BLKmode;
8483 else
8484 *pmode = mode;
8486 return exp;
8489 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8491 static unsigned HOST_WIDE_INT
8492 target_align (const_tree target)
8494 /* We might have a chain of nested references with intermediate misaligning
8495 bitfields components, so need to recurse to find out. */
8497 unsigned HOST_WIDE_INT this_align, outer_align;
8499 switch (TREE_CODE (target))
8501 case BIT_FIELD_REF:
8502 return 1;
8504 case COMPONENT_REF:
8505 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8506 outer_align = target_align (TREE_OPERAND (target, 0));
8507 return MIN (this_align, outer_align);
8509 case ARRAY_REF:
8510 case ARRAY_RANGE_REF:
8511 this_align = TYPE_ALIGN (TREE_TYPE (target));
8512 outer_align = target_align (TREE_OPERAND (target, 0));
8513 return MIN (this_align, outer_align);
8515 CASE_CONVERT:
8516 case NON_LVALUE_EXPR:
8517 case VIEW_CONVERT_EXPR:
8518 this_align = TYPE_ALIGN (TREE_TYPE (target));
8519 outer_align = target_align (TREE_OPERAND (target, 0));
8520 return MAX (this_align, outer_align);
8522 default:
8523 return TYPE_ALIGN (TREE_TYPE (target));
8528 /* Given an rtx VALUE that may contain additions and multiplications, return
8529 an equivalent value that just refers to a register, memory, or constant.
8530 This is done by generating instructions to perform the arithmetic and
8531 returning a pseudo-register containing the value.
8533 The returned value may be a REG, SUBREG, MEM or constant. */
8536 force_operand (rtx value, rtx target)
8538 rtx op1, op2;
8539 /* Use subtarget as the target for operand 0 of a binary operation. */
8540 rtx subtarget = get_subtarget (target);
8541 enum rtx_code code = GET_CODE (value);
8543 /* Check for subreg applied to an expression produced by loop optimizer. */
8544 if (code == SUBREG
8545 && !REG_P (SUBREG_REG (value))
8546 && !MEM_P (SUBREG_REG (value)))
8548 value
8549 = simplify_gen_subreg (GET_MODE (value),
8550 force_reg (GET_MODE (SUBREG_REG (value)),
8551 force_operand (SUBREG_REG (value),
8552 NULL_RTX)),
8553 GET_MODE (SUBREG_REG (value)),
8554 SUBREG_BYTE (value));
8555 code = GET_CODE (value);
8558 /* Check for a PIC address load. */
8559 if ((code == PLUS || code == MINUS)
8560 && XEXP (value, 0) == pic_offset_table_rtx
8561 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8562 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8563 || GET_CODE (XEXP (value, 1)) == CONST))
8565 if (!subtarget)
8566 subtarget = gen_reg_rtx (GET_MODE (value));
8567 emit_move_insn (subtarget, value);
8568 return subtarget;
8571 if (ARITHMETIC_P (value))
8573 op2 = XEXP (value, 1);
8574 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8575 subtarget = 0;
8576 if (code == MINUS && CONST_INT_P (op2))
8578 code = PLUS;
8579 op2 = negate_rtx (GET_MODE (value), op2);
8582 /* Check for an addition with OP2 a constant integer and our first
8583 operand a PLUS of a virtual register and something else. In that
8584 case, we want to emit the sum of the virtual register and the
8585 constant first and then add the other value. This allows virtual
8586 register instantiation to simply modify the constant rather than
8587 creating another one around this addition. */
8588 if (code == PLUS && CONST_INT_P (op2)
8589 && GET_CODE (XEXP (value, 0)) == PLUS
8590 && REG_P (XEXP (XEXP (value, 0), 0))
8591 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8593 rtx temp = expand_simple_binop (GET_MODE (value), code,
8594 XEXP (XEXP (value, 0), 0), op2,
8595 subtarget, 0, OPTAB_LIB_WIDEN);
8596 return expand_simple_binop (GET_MODE (value), code, temp,
8597 force_operand (XEXP (XEXP (value,
8598 0), 1), 0),
8599 target, 0, OPTAB_LIB_WIDEN);
8602 op1 = force_operand (XEXP (value, 0), subtarget);
8603 op2 = force_operand (op2, NULL_RTX);
8604 switch (code)
8606 case MULT:
8607 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8608 case DIV:
8609 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8610 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8611 target, 1, OPTAB_LIB_WIDEN);
8612 else
8613 return expand_divmod (0,
8614 FLOAT_MODE_P (GET_MODE (value))
8615 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8616 GET_MODE (value), op1, op2, target, 0);
8617 case MOD:
8618 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8619 target, 0);
8620 case UDIV:
8621 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8622 target, 1);
8623 case UMOD:
8624 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8625 target, 1);
8626 case ASHIFTRT:
8627 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8628 target, 0, OPTAB_LIB_WIDEN);
8629 default:
8630 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8631 target, 1, OPTAB_LIB_WIDEN);
8634 if (UNARY_P (value))
8636 if (!target)
8637 target = gen_reg_rtx (GET_MODE (value));
8638 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8639 switch (code)
8641 case ZERO_EXTEND:
8642 case SIGN_EXTEND:
8643 case TRUNCATE:
8644 case FLOAT_EXTEND:
8645 case FLOAT_TRUNCATE:
8646 convert_move (target, op1, code == ZERO_EXTEND);
8647 return target;
8649 case FIX:
8650 case UNSIGNED_FIX:
8651 expand_fix (target, op1, code == UNSIGNED_FIX);
8652 return target;
8654 case FLOAT:
8655 case UNSIGNED_FLOAT:
8656 expand_float (target, op1, code == UNSIGNED_FLOAT);
8657 return target;
8659 default:
8660 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8664 #ifdef INSN_SCHEDULING
8665 /* On machines that have insn scheduling, we want all memory reference to be
8666 explicit, so we need to deal with such paradoxical SUBREGs. */
8667 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8668 value
8669 = simplify_gen_subreg (GET_MODE (value),
8670 force_reg (GET_MODE (SUBREG_REG (value)),
8671 force_operand (SUBREG_REG (value),
8672 NULL_RTX)),
8673 GET_MODE (SUBREG_REG (value)),
8674 SUBREG_BYTE (value));
8675 #endif
8677 return value;
8680 /* Subroutine of expand_expr: return true iff there is no way that
8681 EXP can reference X, which is being modified. TOP_P is nonzero if this
8682 call is going to be used to determine whether we need a temporary
8683 for EXP, as opposed to a recursive call to this function.
8685 It is always safe for this routine to return false since it merely
8686 searches for optimization opportunities. */
8688 bool
8689 safe_from_p (const_rtx x, tree exp, int top_p)
8691 rtx exp_rtl = 0;
8692 int i, nops;
8694 if (x == 0
8695 /* If EXP has varying size, we MUST use a target since we currently
8696 have no way of allocating temporaries of variable size
8697 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8698 So we assume here that something at a higher level has prevented a
8699 clash. This is somewhat bogus, but the best we can do. Only
8700 do this when X is BLKmode and when we are at the top level. */
8701 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8702 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8703 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8704 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8705 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8706 != INTEGER_CST)
8707 && GET_MODE (x) == BLKmode)
8708 /* If X is in the outgoing argument area, it is always safe. */
8709 || (MEM_P (x)
8710 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8711 || (GET_CODE (XEXP (x, 0)) == PLUS
8712 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8713 return true;
8715 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8716 find the underlying pseudo. */
8717 if (GET_CODE (x) == SUBREG)
8719 x = SUBREG_REG (x);
8720 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8721 return false;
8724 /* Now look at our tree code and possibly recurse. */
8725 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8727 case tcc_declaration:
8728 exp_rtl = DECL_RTL_IF_SET (exp);
8729 break;
8731 case tcc_constant:
8732 return true;
8734 case tcc_exceptional:
8735 if (TREE_CODE (exp) == TREE_LIST)
8737 while (1)
8739 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8740 return false;
8741 exp = TREE_CHAIN (exp);
8742 if (!exp)
8743 return true;
8744 if (TREE_CODE (exp) != TREE_LIST)
8745 return safe_from_p (x, exp, 0);
8748 else if (TREE_CODE (exp) == CONSTRUCTOR)
8750 constructor_elt *ce;
8751 unsigned HOST_WIDE_INT idx;
8753 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8754 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8755 || !safe_from_p (x, ce->value, 0))
8756 return false;
8757 return true;
8759 else if (TREE_CODE (exp) == ERROR_MARK)
8760 return true; /* An already-visited SAVE_EXPR? */
8761 else
8762 return false;
8764 case tcc_statement:
8765 /* The only case we look at here is the DECL_INITIAL inside a
8766 DECL_EXPR. */
8767 return (TREE_CODE (exp) != DECL_EXPR
8768 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8769 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8770 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8772 case tcc_binary:
8773 case tcc_comparison:
8774 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8775 return false;
8776 /* Fall through. */
8778 case tcc_unary:
8779 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8781 case tcc_expression:
8782 case tcc_reference:
8783 case tcc_vl_exp:
8784 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8785 the expression. If it is set, we conflict iff we are that rtx or
8786 both are in memory. Otherwise, we check all operands of the
8787 expression recursively. */
8789 switch (TREE_CODE (exp))
8791 case ADDR_EXPR:
8792 /* If the operand is static or we are static, we can't conflict.
8793 Likewise if we don't conflict with the operand at all. */
8794 if (staticp (TREE_OPERAND (exp, 0))
8795 || TREE_STATIC (exp)
8796 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8797 return true;
8799 /* Otherwise, the only way this can conflict is if we are taking
8800 the address of a DECL a that address if part of X, which is
8801 very rare. */
8802 exp = TREE_OPERAND (exp, 0);
8803 if (DECL_P (exp))
8805 if (!DECL_RTL_SET_P (exp)
8806 || !MEM_P (DECL_RTL (exp)))
8807 return false;
8808 else
8809 exp_rtl = XEXP (DECL_RTL (exp), 0);
8811 break;
8813 case MEM_REF:
8814 if (MEM_P (x)
8815 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8816 get_alias_set (exp)))
8817 return false;
8818 break;
8820 case CALL_EXPR:
8821 /* Assume that the call will clobber all hard registers and
8822 all of memory. */
8823 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8824 || MEM_P (x))
8825 return false;
8826 break;
8828 case WITH_CLEANUP_EXPR:
8829 case CLEANUP_POINT_EXPR:
8830 /* Lowered by gimplify.cc. */
8831 gcc_unreachable ();
8833 case SAVE_EXPR:
8834 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8836 default:
8837 break;
8840 /* If we have an rtx, we do not need to scan our operands. */
8841 if (exp_rtl)
8842 break;
8844 nops = TREE_OPERAND_LENGTH (exp);
8845 for (i = 0; i < nops; i++)
8846 if (TREE_OPERAND (exp, i) != 0
8847 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8848 return false;
8850 break;
8852 case tcc_type:
8853 /* Should never get a type here. */
8854 gcc_unreachable ();
8857 /* If we have an rtl, find any enclosed object. Then see if we conflict
8858 with it. */
8859 if (exp_rtl)
8861 if (GET_CODE (exp_rtl) == SUBREG)
8863 exp_rtl = SUBREG_REG (exp_rtl);
8864 if (REG_P (exp_rtl)
8865 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8866 return false;
8869 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8870 are memory and they conflict. */
8871 return ! (rtx_equal_p (x, exp_rtl)
8872 || (MEM_P (x) && MEM_P (exp_rtl)
8873 && true_dependence (exp_rtl, VOIDmode, x)));
8876 /* If we reach here, it is safe. */
8877 return true;
8881 /* Return the highest power of two that EXP is known to be a multiple of.
8882 This is used in updating alignment of MEMs in array references. */
8884 unsigned HOST_WIDE_INT
8885 highest_pow2_factor (const_tree exp)
8887 unsigned HOST_WIDE_INT ret;
8888 int trailing_zeros = tree_ctz (exp);
8889 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8890 return BIGGEST_ALIGNMENT;
8891 ret = HOST_WIDE_INT_1U << trailing_zeros;
8892 if (ret > BIGGEST_ALIGNMENT)
8893 return BIGGEST_ALIGNMENT;
8894 return ret;
8897 /* Similar, except that the alignment requirements of TARGET are
8898 taken into account. Assume it is at least as aligned as its
8899 type, unless it is a COMPONENT_REF in which case the layout of
8900 the structure gives the alignment. */
8902 static unsigned HOST_WIDE_INT
8903 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8905 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8906 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8908 return MAX (factor, talign);
8911 /* Convert the tree comparison code TCODE to the rtl one where the
8912 signedness is UNSIGNEDP. */
8914 static enum rtx_code
8915 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8917 enum rtx_code code;
8918 switch (tcode)
8920 case EQ_EXPR:
8921 code = EQ;
8922 break;
8923 case NE_EXPR:
8924 code = NE;
8925 break;
8926 case LT_EXPR:
8927 code = unsignedp ? LTU : LT;
8928 break;
8929 case LE_EXPR:
8930 code = unsignedp ? LEU : LE;
8931 break;
8932 case GT_EXPR:
8933 code = unsignedp ? GTU : GT;
8934 break;
8935 case GE_EXPR:
8936 code = unsignedp ? GEU : GE;
8937 break;
8938 case UNORDERED_EXPR:
8939 code = UNORDERED;
8940 break;
8941 case ORDERED_EXPR:
8942 code = ORDERED;
8943 break;
8944 case UNLT_EXPR:
8945 code = UNLT;
8946 break;
8947 case UNLE_EXPR:
8948 code = UNLE;
8949 break;
8950 case UNGT_EXPR:
8951 code = UNGT;
8952 break;
8953 case UNGE_EXPR:
8954 code = UNGE;
8955 break;
8956 case UNEQ_EXPR:
8957 code = UNEQ;
8958 break;
8959 case LTGT_EXPR:
8960 code = LTGT;
8961 break;
8963 default:
8964 gcc_unreachable ();
8966 return code;
8969 /* Subroutine of expand_expr. Expand the two operands of a binary
8970 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8971 The value may be stored in TARGET if TARGET is nonzero. The
8972 MODIFIER argument is as documented by expand_expr. */
8974 void
8975 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8976 enum expand_modifier modifier)
8978 if (! safe_from_p (target, exp1, 1))
8979 target = 0;
8980 if (operand_equal_p (exp0, exp1, 0))
8982 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8983 *op1 = copy_rtx (*op0);
8985 else
8987 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8988 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8993 /* Return a MEM that contains constant EXP. DEFER is as for
8994 output_constant_def and MODIFIER is as for expand_expr. */
8996 static rtx
8997 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8999 rtx mem;
9001 mem = output_constant_def (exp, defer);
9002 if (modifier != EXPAND_INITIALIZER)
9003 mem = use_anchored_address (mem);
9004 return mem;
9007 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
9008 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9010 static rtx
9011 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
9012 enum expand_modifier modifier, addr_space_t as)
9014 rtx result, subtarget;
9015 tree inner, offset;
9016 poly_int64 bitsize, bitpos;
9017 int unsignedp, reversep, volatilep = 0;
9018 machine_mode mode1;
9020 /* If we are taking the address of a constant and are at the top level,
9021 we have to use output_constant_def since we can't call force_const_mem
9022 at top level. */
9023 /* ??? This should be considered a front-end bug. We should not be
9024 generating ADDR_EXPR of something that isn't an LVALUE. The only
9025 exception here is STRING_CST. */
9026 if (CONSTANT_CLASS_P (exp))
9028 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
9029 if (modifier < EXPAND_SUM)
9030 result = force_operand (result, target);
9031 return result;
9034 /* Everything must be something allowed by is_gimple_addressable. */
9035 switch (TREE_CODE (exp))
9037 case INDIRECT_REF:
9038 /* This case will happen via recursion for &a->b. */
9039 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
9041 case MEM_REF:
9043 tree tem = TREE_OPERAND (exp, 0);
9044 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9045 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
9046 return expand_expr (tem, target, tmode, modifier);
9049 case TARGET_MEM_REF:
9050 return addr_for_mem_ref (exp, as, true);
9052 case CONST_DECL:
9053 /* Expand the initializer like constants above. */
9054 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
9055 0, modifier), 0);
9056 if (modifier < EXPAND_SUM)
9057 result = force_operand (result, target);
9058 return result;
9060 case REALPART_EXPR:
9061 /* The real part of the complex number is always first, therefore
9062 the address is the same as the address of the parent object. */
9063 offset = 0;
9064 bitpos = 0;
9065 inner = TREE_OPERAND (exp, 0);
9066 break;
9068 case IMAGPART_EXPR:
9069 /* The imaginary part of the complex number is always second.
9070 The expression is therefore always offset by the size of the
9071 scalar type. */
9072 offset = 0;
9073 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
9074 inner = TREE_OPERAND (exp, 0);
9075 break;
9077 case COMPOUND_LITERAL_EXPR:
9078 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
9079 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
9080 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
9081 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
9082 the initializers aren't gimplified. */
9083 if (COMPOUND_LITERAL_EXPR_DECL (exp)
9084 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
9085 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
9086 target, tmode, modifier, as);
9087 /* FALLTHRU */
9088 default:
9089 /* If the object is a DECL, then expand it for its rtl. Don't bypass
9090 expand_expr, as that can have various side effects; LABEL_DECLs for
9091 example, may not have their DECL_RTL set yet. Expand the rtl of
9092 CONSTRUCTORs too, which should yield a memory reference for the
9093 constructor's contents. Assume language specific tree nodes can
9094 be expanded in some interesting way. */
9095 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
9096 if (DECL_P (exp)
9097 || TREE_CODE (exp) == CONSTRUCTOR
9098 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
9100 result = expand_expr (exp, target, tmode,
9101 modifier == EXPAND_INITIALIZER
9102 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
9104 /* If the DECL isn't in memory, then the DECL wasn't properly
9105 marked TREE_ADDRESSABLE, which will be either a front-end
9106 or a tree optimizer bug. */
9108 gcc_assert (MEM_P (result));
9109 result = XEXP (result, 0);
9111 /* ??? Is this needed anymore? */
9112 if (DECL_P (exp))
9113 TREE_USED (exp) = 1;
9115 if (modifier != EXPAND_INITIALIZER
9116 && modifier != EXPAND_CONST_ADDRESS
9117 && modifier != EXPAND_SUM)
9118 result = force_operand (result, target);
9119 return result;
9122 /* Pass FALSE as the last argument to get_inner_reference although
9123 we are expanding to RTL. The rationale is that we know how to
9124 handle "aligning nodes" here: we can just bypass them because
9125 they won't change the final object whose address will be returned
9126 (they actually exist only for that purpose). */
9127 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
9128 &unsignedp, &reversep, &volatilep);
9129 break;
9132 /* We must have made progress. */
9133 gcc_assert (inner != exp);
9135 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
9136 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
9137 inner alignment, force the inner to be sufficiently aligned. */
9138 if (CONSTANT_CLASS_P (inner)
9139 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
9141 inner = copy_node (inner);
9142 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
9143 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
9144 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
9146 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
9148 if (offset)
9150 rtx tmp;
9152 if (modifier != EXPAND_NORMAL)
9153 result = force_operand (result, NULL);
9154 tmp = expand_expr (offset, NULL_RTX, tmode,
9155 modifier == EXPAND_INITIALIZER
9156 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
9158 /* expand_expr is allowed to return an object in a mode other
9159 than TMODE. If it did, we need to convert. */
9160 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
9161 tmp = convert_modes (tmode, GET_MODE (tmp),
9162 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
9163 result = convert_memory_address_addr_space (tmode, result, as);
9164 tmp = convert_memory_address_addr_space (tmode, tmp, as);
9166 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9167 result = simplify_gen_binary (PLUS, tmode, result, tmp);
9168 else
9170 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
9171 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
9172 1, OPTAB_LIB_WIDEN);
9176 if (maybe_ne (bitpos, 0))
9178 /* Someone beforehand should have rejected taking the address
9179 of an object that isn't byte-aligned. */
9180 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
9181 result = convert_memory_address_addr_space (tmode, result, as);
9182 result = plus_constant (tmode, result, bytepos);
9183 if (modifier < EXPAND_SUM)
9184 result = force_operand (result, target);
9187 return result;
9190 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
9191 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9193 static rtx
9194 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
9195 enum expand_modifier modifier)
9197 addr_space_t as = ADDR_SPACE_GENERIC;
9198 scalar_int_mode address_mode = Pmode;
9199 scalar_int_mode pointer_mode = ptr_mode;
9200 machine_mode rmode;
9201 rtx result;
9203 /* Target mode of VOIDmode says "whatever's natural". */
9204 if (tmode == VOIDmode)
9205 tmode = TYPE_MODE (TREE_TYPE (exp));
9207 if (POINTER_TYPE_P (TREE_TYPE (exp)))
9209 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
9210 address_mode = targetm.addr_space.address_mode (as);
9211 pointer_mode = targetm.addr_space.pointer_mode (as);
9214 /* We can get called with some Weird Things if the user does silliness
9215 like "(short) &a". In that case, convert_memory_address won't do
9216 the right thing, so ignore the given target mode. */
9217 scalar_int_mode new_tmode = (tmode == pointer_mode
9218 ? pointer_mode
9219 : address_mode);
9221 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
9222 new_tmode, modifier, as);
9224 /* Despite expand_expr claims concerning ignoring TMODE when not
9225 strictly convenient, stuff breaks if we don't honor it. Note
9226 that combined with the above, we only do this for pointer modes. */
9227 rmode = GET_MODE (result);
9228 if (rmode == VOIDmode)
9229 rmode = new_tmode;
9230 if (rmode != new_tmode)
9231 result = convert_memory_address_addr_space (new_tmode, result, as);
9233 return result;
9236 /* Generate code for computing CONSTRUCTOR EXP.
9237 An rtx for the computed value is returned. If AVOID_TEMP_MEM
9238 is TRUE, instead of creating a temporary variable in memory
9239 NULL is returned and the caller needs to handle it differently. */
9241 static rtx
9242 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
9243 bool avoid_temp_mem)
9245 tree type = TREE_TYPE (exp);
9246 machine_mode mode = TYPE_MODE (type);
9248 /* Try to avoid creating a temporary at all. This is possible
9249 if all of the initializer is zero.
9250 FIXME: try to handle all [0..255] initializers we can handle
9251 with memset. */
9252 if (TREE_STATIC (exp)
9253 && !TREE_ADDRESSABLE (exp)
9254 && target != 0 && mode == BLKmode
9255 && all_zeros_p (exp))
9257 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
9258 return target;
9261 /* All elts simple constants => refer to a constant in memory. But
9262 if this is a non-BLKmode mode, let it store a field at a time
9263 since that should make a CONST_INT, CONST_WIDE_INT or
9264 CONST_DOUBLE when we fold. Likewise, if we have a target we can
9265 use, it is best to store directly into the target unless the type
9266 is large enough that memcpy will be used. If we are making an
9267 initializer and all operands are constant, put it in memory as
9268 well.
9270 FIXME: Avoid trying to fill vector constructors piece-meal.
9271 Output them with output_constant_def below unless we're sure
9272 they're zeros. This should go away when vector initializers
9273 are treated like VECTOR_CST instead of arrays. */
9274 if ((TREE_STATIC (exp)
9275 && ((mode == BLKmode
9276 && ! (target != 0 && safe_from_p (target, exp, 1)))
9277 || TREE_ADDRESSABLE (exp)
9278 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9279 && (! can_move_by_pieces
9280 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9281 TYPE_ALIGN (type)))
9282 && ! mostly_zeros_p (exp))))
9283 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
9284 && TREE_CONSTANT (exp)))
9286 rtx constructor;
9288 if (avoid_temp_mem)
9289 return NULL_RTX;
9291 constructor = expand_expr_constant (exp, 1, modifier);
9293 if (modifier != EXPAND_CONST_ADDRESS
9294 && modifier != EXPAND_INITIALIZER
9295 && modifier != EXPAND_SUM)
9296 constructor = validize_mem (constructor);
9298 return constructor;
9301 /* If the CTOR is available in static storage and not mostly
9302 zeros and we can move it by pieces prefer to do so since
9303 that's usually more efficient than performing a series of
9304 stores from immediates. */
9305 if (avoid_temp_mem
9306 && TREE_STATIC (exp)
9307 && TREE_CONSTANT (exp)
9308 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9309 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9310 TYPE_ALIGN (type))
9311 && ! mostly_zeros_p (exp))
9312 return NULL_RTX;
9314 /* Handle calls that pass values in multiple non-contiguous
9315 locations. The Irix 6 ABI has examples of this. */
9316 if (target == 0 || ! safe_from_p (target, exp, 1)
9317 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
9318 /* Also make a temporary if the store is to volatile memory, to
9319 avoid individual accesses to aggregate members. */
9320 || (GET_CODE (target) == MEM
9321 && MEM_VOLATILE_P (target)
9322 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
9324 if (avoid_temp_mem)
9325 return NULL_RTX;
9327 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
9330 store_constructor (exp, target, 0, int_expr_size (exp), false);
9331 return target;
9335 /* expand_expr: generate code for computing expression EXP.
9336 An rtx for the computed value is returned. The value is never null.
9337 In the case of a void EXP, const0_rtx is returned.
9339 The value may be stored in TARGET if TARGET is nonzero.
9340 TARGET is just a suggestion; callers must assume that
9341 the rtx returned may not be the same as TARGET.
9343 If TARGET is CONST0_RTX, it means that the value will be ignored.
9345 If TMODE is not VOIDmode, it suggests generating the
9346 result in mode TMODE. But this is done only when convenient.
9347 Otherwise, TMODE is ignored and the value generated in its natural mode.
9348 TMODE is just a suggestion; callers must assume that
9349 the rtx returned may not have mode TMODE.
9351 Note that TARGET may have neither TMODE nor MODE. In that case, it
9352 probably will not be used.
9354 If MODIFIER is EXPAND_SUM then when EXP is an addition
9355 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
9356 or a nest of (PLUS ...) and (MINUS ...) where the terms are
9357 products as above, or REG or MEM, or constant.
9358 Ordinarily in such cases we would output mul or add instructions
9359 and then return a pseudo reg containing the sum.
9361 EXPAND_INITIALIZER is much like EXPAND_SUM except that
9362 it also marks a label as absolutely required (it can't be dead).
9363 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
9364 This is used for outputting expressions used in initializers.
9366 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
9367 with a constant address even if that address is not normally legitimate.
9368 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
9370 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
9371 a call parameter. Such targets require special care as we haven't yet
9372 marked TARGET so that it's safe from being trashed by libcalls. We
9373 don't want to use TARGET for anything but the final result;
9374 Intermediate values must go elsewhere. Additionally, calls to
9375 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
9377 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
9378 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
9379 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
9380 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
9381 recursively.
9382 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9383 then *ALT_RTL is set to TARGET (before legitimziation).
9385 If INNER_REFERENCE_P is true, we are expanding an inner reference.
9386 In this case, we don't adjust a returned MEM rtx that wouldn't be
9387 sufficiently aligned for its mode; instead, it's up to the caller
9388 to deal with it afterwards. This is used to make sure that unaligned
9389 base objects for which out-of-bounds accesses are supported, for
9390 example record types with trailing arrays, aren't realigned behind
9391 the back of the caller.
9392 The normal operating mode is to pass FALSE for this parameter. */
9395 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9396 enum expand_modifier modifier, rtx *alt_rtl,
9397 bool inner_reference_p)
9399 rtx ret;
9401 /* Handle ERROR_MARK before anybody tries to access its type. */
9402 if (TREE_CODE (exp) == ERROR_MARK
9403 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9405 ret = CONST0_RTX (tmode);
9406 return ret ? ret : const0_rtx;
9409 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9410 inner_reference_p);
9411 return ret;
9414 /* Try to expand the conditional expression which is represented by
9415 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9416 return the rtl reg which represents the result. Otherwise return
9417 NULL_RTX. */
9419 static rtx
9420 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9421 tree treeop1 ATTRIBUTE_UNUSED,
9422 tree treeop2 ATTRIBUTE_UNUSED)
9424 rtx insn;
9425 rtx op00, op01, op1, op2;
9426 enum rtx_code comparison_code;
9427 machine_mode comparison_mode;
9428 gimple *srcstmt;
9429 rtx temp;
9430 tree type = TREE_TYPE (treeop1);
9431 int unsignedp = TYPE_UNSIGNED (type);
9432 machine_mode mode = TYPE_MODE (type);
9433 machine_mode orig_mode = mode;
9434 static bool expanding_cond_expr_using_cmove = false;
9436 /* Conditional move expansion can end up TERing two operands which,
9437 when recursively hitting conditional expressions can result in
9438 exponential behavior if the cmove expansion ultimatively fails.
9439 It's hardly profitable to TER a cmove into a cmove so avoid doing
9440 that by failing early if we end up recursing. */
9441 if (expanding_cond_expr_using_cmove)
9442 return NULL_RTX;
9444 /* If we cannot do a conditional move on the mode, try doing it
9445 with the promoted mode. */
9446 if (!can_conditionally_move_p (mode))
9448 mode = promote_mode (type, mode, &unsignedp);
9449 if (!can_conditionally_move_p (mode))
9450 return NULL_RTX;
9451 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9453 else
9454 temp = assign_temp (type, 0, 1);
9456 expanding_cond_expr_using_cmove = true;
9457 start_sequence ();
9458 expand_operands (treeop1, treeop2,
9459 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9460 EXPAND_NORMAL);
9462 if (TREE_CODE (treeop0) == SSA_NAME
9463 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9465 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9466 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9467 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9468 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9469 comparison_mode = TYPE_MODE (type);
9470 unsignedp = TYPE_UNSIGNED (type);
9471 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9473 else if (COMPARISON_CLASS_P (treeop0))
9475 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9476 enum tree_code cmpcode = TREE_CODE (treeop0);
9477 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9478 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9479 unsignedp = TYPE_UNSIGNED (type);
9480 comparison_mode = TYPE_MODE (type);
9481 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9483 else
9485 op00 = expand_normal (treeop0);
9486 op01 = const0_rtx;
9487 comparison_code = NE;
9488 comparison_mode = GET_MODE (op00);
9489 if (comparison_mode == VOIDmode)
9490 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9492 expanding_cond_expr_using_cmove = false;
9494 if (GET_MODE (op1) != mode)
9495 op1 = gen_lowpart (mode, op1);
9497 if (GET_MODE (op2) != mode)
9498 op2 = gen_lowpart (mode, op2);
9500 /* Try to emit the conditional move. */
9501 insn = emit_conditional_move (temp,
9502 { comparison_code, op00, op01,
9503 comparison_mode },
9504 op1, op2, mode,
9505 unsignedp);
9507 /* If we could do the conditional move, emit the sequence,
9508 and return. */
9509 if (insn)
9511 rtx_insn *seq = get_insns ();
9512 end_sequence ();
9513 emit_insn (seq);
9514 return convert_modes (orig_mode, mode, temp, 0);
9517 /* Otherwise discard the sequence and fall back to code with
9518 branches. */
9519 end_sequence ();
9520 return NULL_RTX;
9523 /* A helper function for expand_expr_real_2 to be used with a
9524 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9525 is nonzero, with alignment ALIGN in bits.
9526 Store the value at TARGET if possible (if TARGET is nonzero).
9527 Regardless of TARGET, we return the rtx for where the value is placed.
9528 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9529 then *ALT_RTL is set to TARGET (before legitimziation). */
9531 static rtx
9532 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9533 unsigned int align, rtx target, rtx *alt_rtl)
9535 enum insn_code icode;
9537 if ((icode = optab_handler (movmisalign_optab, mode))
9538 != CODE_FOR_nothing)
9540 class expand_operand ops[2];
9542 /* We've already validated the memory, and we're creating a
9543 new pseudo destination. The predicates really can't fail,
9544 nor can the generator. */
9545 create_output_operand (&ops[0], NULL_RTX, mode);
9546 create_fixed_operand (&ops[1], temp);
9547 expand_insn (icode, 2, ops);
9548 temp = ops[0].value;
9550 else if (targetm.slow_unaligned_access (mode, align))
9551 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9552 0, unsignedp, target,
9553 mode, mode, false, alt_rtl);
9554 return temp;
9557 /* Helper function of expand_expr_2, expand a division or modulo.
9558 op0 and op1 should be already expanded treeop0 and treeop1, using
9559 expand_operands. */
9561 static rtx
9562 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9563 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9565 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9566 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9567 if (SCALAR_INT_MODE_P (mode)
9568 && optimize >= 2
9569 && get_range_pos_neg (treeop0) == 1
9570 && get_range_pos_neg (treeop1) == 1)
9572 /* If both arguments are known to be positive when interpreted
9573 as signed, we can expand it as both signed and unsigned
9574 division or modulo. Choose the cheaper sequence in that case. */
9575 bool speed_p = optimize_insn_for_speed_p ();
9576 do_pending_stack_adjust ();
9577 start_sequence ();
9578 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9579 rtx_insn *uns_insns = get_insns ();
9580 end_sequence ();
9581 start_sequence ();
9582 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9583 rtx_insn *sgn_insns = get_insns ();
9584 end_sequence ();
9585 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9586 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9588 /* If costs are the same then use as tie breaker the other other
9589 factor. */
9590 if (uns_cost == sgn_cost)
9592 uns_cost = seq_cost (uns_insns, !speed_p);
9593 sgn_cost = seq_cost (sgn_insns, !speed_p);
9596 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9598 emit_insn (uns_insns);
9599 return uns_ret;
9601 emit_insn (sgn_insns);
9602 return sgn_ret;
9604 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9608 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9609 enum expand_modifier modifier)
9611 rtx op0, op1, op2, temp;
9612 rtx_code_label *lab;
9613 tree type;
9614 int unsignedp;
9615 machine_mode mode;
9616 scalar_int_mode int_mode;
9617 enum tree_code code = ops->code;
9618 optab this_optab;
9619 rtx subtarget, original_target;
9620 int ignore;
9621 bool reduce_bit_field;
9622 location_t loc = ops->location;
9623 tree treeop0, treeop1, treeop2;
9624 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9625 ? reduce_to_bit_field_precision ((expr), \
9626 target, \
9627 type) \
9628 : (expr))
9630 type = ops->type;
9631 mode = TYPE_MODE (type);
9632 unsignedp = TYPE_UNSIGNED (type);
9634 treeop0 = ops->op0;
9635 treeop1 = ops->op1;
9636 treeop2 = ops->op2;
9638 /* We should be called only on simple (binary or unary) expressions,
9639 exactly those that are valid in gimple expressions that aren't
9640 GIMPLE_SINGLE_RHS (or invalid). */
9641 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9642 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9643 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9645 ignore = (target == const0_rtx
9646 || ((CONVERT_EXPR_CODE_P (code)
9647 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9648 && TREE_CODE (type) == VOID_TYPE));
9650 /* We should be called only if we need the result. */
9651 gcc_assert (!ignore);
9653 /* An operation in what may be a bit-field type needs the
9654 result to be reduced to the precision of the bit-field type,
9655 which is narrower than that of the type's mode. */
9656 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9657 && !type_has_mode_precision_p (type));
9659 if (reduce_bit_field
9660 && (modifier == EXPAND_STACK_PARM
9661 || (target && GET_MODE (target) != mode)))
9662 target = 0;
9664 /* Use subtarget as the target for operand 0 of a binary operation. */
9665 subtarget = get_subtarget (target);
9666 original_target = target;
9668 switch (code)
9670 case NON_LVALUE_EXPR:
9671 case PAREN_EXPR:
9672 CASE_CONVERT:
9673 if (treeop0 == error_mark_node)
9674 return const0_rtx;
9676 if (TREE_CODE (type) == UNION_TYPE)
9678 tree valtype = TREE_TYPE (treeop0);
9680 /* If both input and output are BLKmode, this conversion isn't doing
9681 anything except possibly changing memory attribute. */
9682 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9684 rtx result = expand_expr (treeop0, target, tmode,
9685 modifier);
9687 result = copy_rtx (result);
9688 set_mem_attributes (result, type, 0);
9689 return result;
9692 if (target == 0)
9694 if (TYPE_MODE (type) != BLKmode)
9695 target = gen_reg_rtx (TYPE_MODE (type));
9696 else
9697 target = assign_temp (type, 1, 1);
9700 if (MEM_P (target))
9701 /* Store data into beginning of memory target. */
9702 store_expr (treeop0,
9703 adjust_address (target, TYPE_MODE (valtype), 0),
9704 modifier == EXPAND_STACK_PARM,
9705 false, TYPE_REVERSE_STORAGE_ORDER (type));
9707 else
9709 gcc_assert (REG_P (target)
9710 && !TYPE_REVERSE_STORAGE_ORDER (type));
9712 /* Store this field into a union of the proper type. */
9713 poly_uint64 op0_size
9714 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9715 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9716 store_field (target,
9717 /* The conversion must be constructed so that
9718 we know at compile time how many bits
9719 to preserve. */
9720 ordered_min (op0_size, union_size),
9721 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9722 false, false);
9725 /* Return the entire union. */
9726 return target;
9729 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9731 op0 = expand_expr (treeop0, target, VOIDmode,
9732 modifier);
9734 return REDUCE_BIT_FIELD (op0);
9737 op0 = expand_expr (treeop0, NULL_RTX, mode,
9738 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9739 if (GET_MODE (op0) == mode)
9742 /* If OP0 is a constant, just convert it into the proper mode. */
9743 else if (CONSTANT_P (op0))
9745 tree inner_type = TREE_TYPE (treeop0);
9746 machine_mode inner_mode = GET_MODE (op0);
9748 if (inner_mode == VOIDmode)
9749 inner_mode = TYPE_MODE (inner_type);
9751 if (modifier == EXPAND_INITIALIZER)
9752 op0 = lowpart_subreg (mode, op0, inner_mode);
9753 else
9754 op0= convert_modes (mode, inner_mode, op0,
9755 TYPE_UNSIGNED (inner_type));
9758 else if (modifier == EXPAND_INITIALIZER)
9759 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9760 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9762 else if (target == 0)
9763 op0 = convert_to_mode (mode, op0,
9764 TYPE_UNSIGNED (TREE_TYPE
9765 (treeop0)));
9766 else
9768 convert_move (target, op0,
9769 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9770 op0 = target;
9773 return REDUCE_BIT_FIELD (op0);
9775 case ADDR_SPACE_CONVERT_EXPR:
9777 tree treeop0_type = TREE_TYPE (treeop0);
9779 gcc_assert (POINTER_TYPE_P (type));
9780 gcc_assert (POINTER_TYPE_P (treeop0_type));
9782 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9783 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9785 /* Conversions between pointers to the same address space should
9786 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9787 gcc_assert (as_to != as_from);
9789 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9791 /* Ask target code to handle conversion between pointers
9792 to overlapping address spaces. */
9793 if (targetm.addr_space.subset_p (as_to, as_from)
9794 || targetm.addr_space.subset_p (as_from, as_to))
9796 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9798 else
9800 /* For disjoint address spaces, converting anything but a null
9801 pointer invokes undefined behavior. We truncate or extend the
9802 value as if we'd converted via integers, which handles 0 as
9803 required, and all others as the programmer likely expects. */
9804 #ifndef POINTERS_EXTEND_UNSIGNED
9805 const int POINTERS_EXTEND_UNSIGNED = 1;
9806 #endif
9807 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9808 op0, POINTERS_EXTEND_UNSIGNED);
9810 gcc_assert (op0);
9811 return op0;
9814 case POINTER_PLUS_EXPR:
9815 /* Even though the sizetype mode and the pointer's mode can be different
9816 expand is able to handle this correctly and get the correct result out
9817 of the PLUS_EXPR code. */
9818 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9819 if sizetype precision is smaller than pointer precision. */
9820 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9821 treeop1 = fold_convert_loc (loc, type,
9822 fold_convert_loc (loc, ssizetype,
9823 treeop1));
9824 /* If sizetype precision is larger than pointer precision, truncate the
9825 offset to have matching modes. */
9826 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9827 treeop1 = fold_convert_loc (loc, type, treeop1);
9828 /* FALLTHRU */
9830 case PLUS_EXPR:
9831 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9832 something else, make sure we add the register to the constant and
9833 then to the other thing. This case can occur during strength
9834 reduction and doing it this way will produce better code if the
9835 frame pointer or argument pointer is eliminated.
9837 fold-const.cc will ensure that the constant is always in the inner
9838 PLUS_EXPR, so the only case we need to do anything about is if
9839 sp, ap, or fp is our second argument, in which case we must swap
9840 the innermost first argument and our second argument. */
9842 if (TREE_CODE (treeop0) == PLUS_EXPR
9843 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9844 && VAR_P (treeop1)
9845 && (DECL_RTL (treeop1) == frame_pointer_rtx
9846 || DECL_RTL (treeop1) == stack_pointer_rtx
9847 || DECL_RTL (treeop1) == arg_pointer_rtx))
9849 gcc_unreachable ();
9852 /* If the result is to be ptr_mode and we are adding an integer to
9853 something, we might be forming a constant. So try to use
9854 plus_constant. If it produces a sum and we can't accept it,
9855 use force_operand. This allows P = &ARR[const] to generate
9856 efficient code on machines where a SYMBOL_REF is not a valid
9857 address.
9859 If this is an EXPAND_SUM call, always return the sum. */
9860 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9861 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9863 if (modifier == EXPAND_STACK_PARM)
9864 target = 0;
9865 if (TREE_CODE (treeop0) == INTEGER_CST
9866 && HWI_COMPUTABLE_MODE_P (mode)
9867 && TREE_CONSTANT (treeop1))
9869 rtx constant_part;
9870 HOST_WIDE_INT wc;
9871 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9873 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9874 EXPAND_SUM);
9875 /* Use wi::shwi to ensure that the constant is
9876 truncated according to the mode of OP1, then sign extended
9877 to a HOST_WIDE_INT. Using the constant directly can result
9878 in non-canonical RTL in a 64x32 cross compile. */
9879 wc = TREE_INT_CST_LOW (treeop0);
9880 constant_part =
9881 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9882 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9883 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9884 op1 = force_operand (op1, target);
9885 return REDUCE_BIT_FIELD (op1);
9888 else if (TREE_CODE (treeop1) == INTEGER_CST
9889 && HWI_COMPUTABLE_MODE_P (mode)
9890 && TREE_CONSTANT (treeop0))
9892 rtx constant_part;
9893 HOST_WIDE_INT wc;
9894 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9896 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9897 (modifier == EXPAND_INITIALIZER
9898 ? EXPAND_INITIALIZER : EXPAND_SUM));
9899 if (! CONSTANT_P (op0))
9901 op1 = expand_expr (treeop1, NULL_RTX,
9902 VOIDmode, modifier);
9903 /* Return a PLUS if modifier says it's OK. */
9904 if (modifier == EXPAND_SUM
9905 || modifier == EXPAND_INITIALIZER)
9906 return simplify_gen_binary (PLUS, mode, op0, op1);
9907 goto binop2;
9909 /* Use wi::shwi to ensure that the constant is
9910 truncated according to the mode of OP1, then sign extended
9911 to a HOST_WIDE_INT. Using the constant directly can result
9912 in non-canonical RTL in a 64x32 cross compile. */
9913 wc = TREE_INT_CST_LOW (treeop1);
9914 constant_part
9915 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9916 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9917 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9918 op0 = force_operand (op0, target);
9919 return REDUCE_BIT_FIELD (op0);
9923 /* Use TER to expand pointer addition of a negated value
9924 as pointer subtraction. */
9925 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9926 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9927 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9928 && TREE_CODE (treeop1) == SSA_NAME
9929 && TYPE_MODE (TREE_TYPE (treeop0))
9930 == TYPE_MODE (TREE_TYPE (treeop1)))
9932 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9933 if (def)
9935 treeop1 = gimple_assign_rhs1 (def);
9936 code = MINUS_EXPR;
9937 goto do_minus;
9941 /* No sense saving up arithmetic to be done
9942 if it's all in the wrong mode to form part of an address.
9943 And force_operand won't know whether to sign-extend or
9944 zero-extend. */
9945 if (modifier != EXPAND_INITIALIZER
9946 && (modifier != EXPAND_SUM || mode != ptr_mode))
9948 expand_operands (treeop0, treeop1,
9949 subtarget, &op0, &op1, modifier);
9950 if (op0 == const0_rtx)
9951 return op1;
9952 if (op1 == const0_rtx)
9953 return op0;
9954 goto binop2;
9957 expand_operands (treeop0, treeop1,
9958 subtarget, &op0, &op1, modifier);
9959 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9961 case MINUS_EXPR:
9962 case POINTER_DIFF_EXPR:
9963 do_minus:
9964 /* For initializers, we are allowed to return a MINUS of two
9965 symbolic constants. Here we handle all cases when both operands
9966 are constant. */
9967 /* Handle difference of two symbolic constants,
9968 for the sake of an initializer. */
9969 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9970 && really_constant_p (treeop0)
9971 && really_constant_p (treeop1))
9973 expand_operands (treeop0, treeop1,
9974 NULL_RTX, &op0, &op1, modifier);
9975 return simplify_gen_binary (MINUS, mode, op0, op1);
9978 /* No sense saving up arithmetic to be done
9979 if it's all in the wrong mode to form part of an address.
9980 And force_operand won't know whether to sign-extend or
9981 zero-extend. */
9982 if (modifier != EXPAND_INITIALIZER
9983 && (modifier != EXPAND_SUM || mode != ptr_mode))
9984 goto binop;
9986 expand_operands (treeop0, treeop1,
9987 subtarget, &op0, &op1, modifier);
9989 /* Convert A - const to A + (-const). */
9990 if (CONST_INT_P (op1))
9992 op1 = negate_rtx (mode, op1);
9993 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9996 goto binop2;
9998 case WIDEN_MULT_PLUS_EXPR:
9999 case WIDEN_MULT_MINUS_EXPR:
10000 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10001 op2 = expand_normal (treeop2);
10002 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10003 target, unsignedp);
10004 return target;
10006 case WIDEN_MULT_EXPR:
10007 /* If first operand is constant, swap them.
10008 Thus the following special case checks need only
10009 check the second operand. */
10010 if (TREE_CODE (treeop0) == INTEGER_CST)
10011 std::swap (treeop0, treeop1);
10013 /* First, check if we have a multiplication of one signed and one
10014 unsigned operand. */
10015 if (TREE_CODE (treeop1) != INTEGER_CST
10016 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
10017 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
10019 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
10020 this_optab = usmul_widen_optab;
10021 if (find_widening_optab_handler (this_optab, mode, innermode)
10022 != CODE_FOR_nothing)
10024 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10025 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10026 EXPAND_NORMAL);
10027 else
10028 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
10029 EXPAND_NORMAL);
10030 /* op0 and op1 might still be constant, despite the above
10031 != INTEGER_CST check. Handle it. */
10032 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10034 op0 = convert_modes (mode, innermode, op0, true);
10035 op1 = convert_modes (mode, innermode, op1, false);
10036 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10037 target, unsignedp));
10039 goto binop3;
10042 /* Check for a multiplication with matching signedness. */
10043 else if ((TREE_CODE (treeop1) == INTEGER_CST
10044 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
10045 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
10046 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
10048 tree op0type = TREE_TYPE (treeop0);
10049 machine_mode innermode = TYPE_MODE (op0type);
10050 bool zextend_p = TYPE_UNSIGNED (op0type);
10051 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
10052 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
10054 if (TREE_CODE (treeop0) != INTEGER_CST)
10056 if (find_widening_optab_handler (this_optab, mode, innermode)
10057 != CODE_FOR_nothing)
10059 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10060 EXPAND_NORMAL);
10061 /* op0 and op1 might still be constant, despite the above
10062 != INTEGER_CST check. Handle it. */
10063 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10065 widen_mult_const:
10066 op0 = convert_modes (mode, innermode, op0, zextend_p);
10068 = convert_modes (mode, innermode, op1,
10069 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
10070 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10071 target,
10072 unsignedp));
10074 temp = expand_widening_mult (mode, op0, op1, target,
10075 unsignedp, this_optab);
10076 return REDUCE_BIT_FIELD (temp);
10078 if (find_widening_optab_handler (other_optab, mode, innermode)
10079 != CODE_FOR_nothing
10080 && innermode == word_mode)
10082 rtx htem, hipart;
10083 op0 = expand_normal (treeop0);
10084 op1 = expand_normal (treeop1);
10085 /* op0 and op1 might be constants, despite the above
10086 != INTEGER_CST check. Handle it. */
10087 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10088 goto widen_mult_const;
10089 temp = expand_binop (mode, other_optab, op0, op1, target,
10090 unsignedp, OPTAB_LIB_WIDEN);
10091 hipart = gen_highpart (word_mode, temp);
10092 htem = expand_mult_highpart_adjust (word_mode, hipart,
10093 op0, op1, hipart,
10094 zextend_p);
10095 if (htem != hipart)
10096 emit_move_insn (hipart, htem);
10097 return REDUCE_BIT_FIELD (temp);
10101 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
10102 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
10103 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10104 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10106 case MULT_EXPR:
10107 /* If this is a fixed-point operation, then we cannot use the code
10108 below because "expand_mult" doesn't support sat/no-sat fixed-point
10109 multiplications. */
10110 if (ALL_FIXED_POINT_MODE_P (mode))
10111 goto binop;
10113 /* If first operand is constant, swap them.
10114 Thus the following special case checks need only
10115 check the second operand. */
10116 if (TREE_CODE (treeop0) == INTEGER_CST)
10117 std::swap (treeop0, treeop1);
10119 /* Attempt to return something suitable for generating an
10120 indexed address, for machines that support that. */
10122 if (modifier == EXPAND_SUM && mode == ptr_mode
10123 && tree_fits_shwi_p (treeop1))
10125 tree exp1 = treeop1;
10127 op0 = expand_expr (treeop0, subtarget, VOIDmode,
10128 EXPAND_SUM);
10130 if (!REG_P (op0))
10131 op0 = force_operand (op0, NULL_RTX);
10132 if (!REG_P (op0))
10133 op0 = copy_to_mode_reg (mode, op0);
10135 op1 = gen_int_mode (tree_to_shwi (exp1),
10136 TYPE_MODE (TREE_TYPE (exp1)));
10137 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
10140 if (modifier == EXPAND_STACK_PARM)
10141 target = 0;
10143 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
10145 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
10146 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
10147 if (def_stmt0
10148 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
10149 def_stmt0 = NULL;
10150 if (def_stmt1
10151 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
10152 def_stmt1 = NULL;
10154 if (def_stmt0 || def_stmt1)
10156 /* X / Y * Y can be expanded as X - X % Y too.
10157 Choose the cheaper sequence of those two. */
10158 if (def_stmt0)
10159 treeop0 = gimple_assign_rhs1 (def_stmt0);
10160 else
10162 treeop1 = treeop0;
10163 treeop0 = gimple_assign_rhs1 (def_stmt1);
10165 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
10166 EXPAND_NORMAL);
10167 bool speed_p = optimize_insn_for_speed_p ();
10168 do_pending_stack_adjust ();
10169 start_sequence ();
10170 rtx divmul_ret
10171 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
10172 op0, op1, NULL_RTX, unsignedp);
10173 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
10174 unsignedp);
10175 rtx_insn *divmul_insns = get_insns ();
10176 end_sequence ();
10177 start_sequence ();
10178 rtx modsub_ret
10179 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
10180 op0, op1, NULL_RTX, unsignedp);
10181 this_optab = optab_for_tree_code (MINUS_EXPR, type,
10182 optab_default);
10183 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
10184 target, unsignedp, OPTAB_LIB_WIDEN);
10185 rtx_insn *modsub_insns = get_insns ();
10186 end_sequence ();
10187 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
10188 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
10189 /* If costs are the same then use as tie breaker the other other
10190 factor. */
10191 if (divmul_cost == modsub_cost)
10193 divmul_cost = seq_cost (divmul_insns, !speed_p);
10194 modsub_cost = seq_cost (modsub_insns, !speed_p);
10197 if (divmul_cost <= modsub_cost)
10199 emit_insn (divmul_insns);
10200 return REDUCE_BIT_FIELD (divmul_ret);
10202 emit_insn (modsub_insns);
10203 return REDUCE_BIT_FIELD (modsub_ret);
10207 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10209 /* Expand X*Y as X&-Y when Y must be zero or one. */
10210 if (SCALAR_INT_MODE_P (mode))
10212 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
10213 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
10215 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
10216 if (bit0_p && bit1_p)
10217 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
10219 if (bit0_p || bit1_p)
10221 bool speed = optimize_insn_for_speed_p ();
10222 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
10223 struct algorithm algorithm;
10224 enum mult_variant variant;
10225 if (CONST_INT_P (op1)
10226 ? !choose_mult_variant (mode, INTVAL (op1),
10227 &algorithm, &variant, cost)
10228 : cost < mul_cost (speed, mode))
10230 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
10231 op1, target)
10232 : expand_and (mode, op0,
10233 negate_rtx (mode, op1),
10234 target);
10235 return REDUCE_BIT_FIELD (target);
10240 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10242 case TRUNC_MOD_EXPR:
10243 case FLOOR_MOD_EXPR:
10244 case CEIL_MOD_EXPR:
10245 case ROUND_MOD_EXPR:
10247 case TRUNC_DIV_EXPR:
10248 case FLOOR_DIV_EXPR:
10249 case CEIL_DIV_EXPR:
10250 case ROUND_DIV_EXPR:
10251 case EXACT_DIV_EXPR:
10252 /* If this is a fixed-point operation, then we cannot use the code
10253 below because "expand_divmod" doesn't support sat/no-sat fixed-point
10254 divisions. */
10255 if (ALL_FIXED_POINT_MODE_P (mode))
10256 goto binop;
10258 if (modifier == EXPAND_STACK_PARM)
10259 target = 0;
10260 /* Possible optimization: compute the dividend with EXPAND_SUM
10261 then if the divisor is constant can optimize the case
10262 where some terms of the dividend have coeffs divisible by it. */
10263 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10264 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
10265 target, unsignedp);
10267 case RDIV_EXPR:
10268 goto binop;
10270 case MULT_HIGHPART_EXPR:
10271 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10272 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
10273 gcc_assert (temp);
10274 return temp;
10276 case FIXED_CONVERT_EXPR:
10277 op0 = expand_normal (treeop0);
10278 if (target == 0 || modifier == EXPAND_STACK_PARM)
10279 target = gen_reg_rtx (mode);
10281 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
10282 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10283 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
10284 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
10285 else
10286 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
10287 return target;
10289 case FIX_TRUNC_EXPR:
10290 op0 = expand_normal (treeop0);
10291 if (target == 0 || modifier == EXPAND_STACK_PARM)
10292 target = gen_reg_rtx (mode);
10293 expand_fix (target, op0, unsignedp);
10294 return target;
10296 case FLOAT_EXPR:
10297 op0 = expand_normal (treeop0);
10298 if (target == 0 || modifier == EXPAND_STACK_PARM)
10299 target = gen_reg_rtx (mode);
10300 /* expand_float can't figure out what to do if FROM has VOIDmode.
10301 So give it the correct mode. With -O, cse will optimize this. */
10302 if (GET_MODE (op0) == VOIDmode)
10303 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
10304 op0);
10305 expand_float (target, op0,
10306 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10307 return target;
10309 case NEGATE_EXPR:
10310 op0 = expand_expr (treeop0, subtarget,
10311 VOIDmode, EXPAND_NORMAL);
10312 if (modifier == EXPAND_STACK_PARM)
10313 target = 0;
10314 temp = expand_unop (mode,
10315 optab_for_tree_code (NEGATE_EXPR, type,
10316 optab_default),
10317 op0, target, 0);
10318 gcc_assert (temp);
10319 return REDUCE_BIT_FIELD (temp);
10321 case ABS_EXPR:
10322 case ABSU_EXPR:
10323 op0 = expand_expr (treeop0, subtarget,
10324 VOIDmode, EXPAND_NORMAL);
10325 if (modifier == EXPAND_STACK_PARM)
10326 target = 0;
10328 /* ABS_EXPR is not valid for complex arguments. */
10329 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10330 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
10332 /* Unsigned abs is simply the operand. Testing here means we don't
10333 risk generating incorrect code below. */
10334 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10335 return op0;
10337 return expand_abs (mode, op0, target, unsignedp,
10338 safe_from_p (target, treeop0, 1));
10340 case MAX_EXPR:
10341 case MIN_EXPR:
10342 target = original_target;
10343 if (target == 0
10344 || modifier == EXPAND_STACK_PARM
10345 || (MEM_P (target) && MEM_VOLATILE_P (target))
10346 || GET_MODE (target) != mode
10347 || (REG_P (target)
10348 && REGNO (target) < FIRST_PSEUDO_REGISTER))
10349 target = gen_reg_rtx (mode);
10350 expand_operands (treeop0, treeop1,
10351 target, &op0, &op1, EXPAND_NORMAL);
10353 /* First try to do it with a special MIN or MAX instruction.
10354 If that does not win, use a conditional jump to select the proper
10355 value. */
10356 this_optab = optab_for_tree_code (code, type, optab_default);
10357 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
10358 OPTAB_WIDEN);
10359 if (temp != 0)
10360 return temp;
10362 if (VECTOR_TYPE_P (type))
10363 gcc_unreachable ();
10365 /* At this point, a MEM target is no longer useful; we will get better
10366 code without it. */
10368 if (! REG_P (target))
10369 target = gen_reg_rtx (mode);
10371 /* If op1 was placed in target, swap op0 and op1. */
10372 if (target != op0 && target == op1)
10373 std::swap (op0, op1);
10375 /* We generate better code and avoid problems with op1 mentioning
10376 target by forcing op1 into a pseudo if it isn't a constant. */
10377 if (! CONSTANT_P (op1))
10378 op1 = force_reg (mode, op1);
10381 enum rtx_code comparison_code;
10382 rtx cmpop1 = op1;
10384 if (code == MAX_EXPR)
10385 comparison_code = unsignedp ? GEU : GE;
10386 else
10387 comparison_code = unsignedp ? LEU : LE;
10389 /* Canonicalize to comparisons against 0. */
10390 if (op1 == const1_rtx)
10392 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10393 or (a != 0 ? a : 1) for unsigned.
10394 For MIN we are safe converting (a <= 1 ? a : 1)
10395 into (a <= 0 ? a : 1) */
10396 cmpop1 = const0_rtx;
10397 if (code == MAX_EXPR)
10398 comparison_code = unsignedp ? NE : GT;
10400 if (op1 == constm1_rtx && !unsignedp)
10402 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10403 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10404 cmpop1 = const0_rtx;
10405 if (code == MIN_EXPR)
10406 comparison_code = LT;
10409 /* Use a conditional move if possible. */
10410 if (can_conditionally_move_p (mode))
10412 rtx insn;
10414 start_sequence ();
10416 /* Try to emit the conditional move. */
10417 insn = emit_conditional_move (target,
10418 { comparison_code,
10419 op0, cmpop1, mode },
10420 op0, op1, mode,
10421 unsignedp);
10423 /* If we could do the conditional move, emit the sequence,
10424 and return. */
10425 if (insn)
10427 rtx_insn *seq = get_insns ();
10428 end_sequence ();
10429 emit_insn (seq);
10430 return target;
10433 /* Otherwise discard the sequence and fall back to code with
10434 branches. */
10435 end_sequence ();
10438 if (target != op0)
10439 emit_move_insn (target, op0);
10441 lab = gen_label_rtx ();
10442 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10443 unsignedp, mode, NULL_RTX, NULL, lab,
10444 profile_probability::uninitialized ());
10446 emit_move_insn (target, op1);
10447 emit_label (lab);
10448 return target;
10450 case BIT_NOT_EXPR:
10451 op0 = expand_expr (treeop0, subtarget,
10452 VOIDmode, EXPAND_NORMAL);
10453 if (modifier == EXPAND_STACK_PARM)
10454 target = 0;
10455 /* In case we have to reduce the result to bitfield precision
10456 for unsigned bitfield expand this as XOR with a proper constant
10457 instead. */
10458 if (reduce_bit_field && TYPE_UNSIGNED (type))
10460 int_mode = SCALAR_INT_TYPE_MODE (type);
10461 wide_int mask = wi::mask (TYPE_PRECISION (type),
10462 false, GET_MODE_PRECISION (int_mode));
10464 temp = expand_binop (int_mode, xor_optab, op0,
10465 immed_wide_int_const (mask, int_mode),
10466 target, 1, OPTAB_LIB_WIDEN);
10468 else
10469 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10470 gcc_assert (temp);
10471 return temp;
10473 /* ??? Can optimize bitwise operations with one arg constant.
10474 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10475 and (a bitwise1 b) bitwise2 b (etc)
10476 but that is probably not worth while. */
10478 case BIT_AND_EXPR:
10479 case BIT_IOR_EXPR:
10480 case BIT_XOR_EXPR:
10481 goto binop;
10483 case LROTATE_EXPR:
10484 case RROTATE_EXPR:
10485 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10486 || type_has_mode_precision_p (type));
10487 /* fall through */
10489 case LSHIFT_EXPR:
10490 case RSHIFT_EXPR:
10492 /* If this is a fixed-point operation, then we cannot use the code
10493 below because "expand_shift" doesn't support sat/no-sat fixed-point
10494 shifts. */
10495 if (ALL_FIXED_POINT_MODE_P (mode))
10496 goto binop;
10498 if (! safe_from_p (subtarget, treeop1, 1))
10499 subtarget = 0;
10500 if (modifier == EXPAND_STACK_PARM)
10501 target = 0;
10502 op0 = expand_expr (treeop0, subtarget,
10503 VOIDmode, EXPAND_NORMAL);
10505 /* Left shift optimization when shifting across word_size boundary.
10507 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10508 there isn't native instruction to support this wide mode
10509 left shift. Given below scenario:
10511 Type A = (Type) B << C
10513 |< T >|
10514 | dest_high | dest_low |
10516 | word_size |
10518 If the shift amount C caused we shift B to across the word
10519 size boundary, i.e part of B shifted into high half of
10520 destination register, and part of B remains in the low
10521 half, then GCC will use the following left shift expand
10522 logic:
10524 1. Initialize dest_low to B.
10525 2. Initialize every bit of dest_high to the sign bit of B.
10526 3. Logic left shift dest_low by C bit to finalize dest_low.
10527 The value of dest_low before this shift is kept in a temp D.
10528 4. Logic left shift dest_high by C.
10529 5. Logic right shift D by (word_size - C).
10530 6. Or the result of 4 and 5 to finalize dest_high.
10532 While, by checking gimple statements, if operand B is
10533 coming from signed extension, then we can simplify above
10534 expand logic into:
10536 1. dest_high = src_low >> (word_size - C).
10537 2. dest_low = src_low << C.
10539 We can use one arithmetic right shift to finish all the
10540 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10541 needed from 6 into 2.
10543 The case is similar for zero extension, except that we
10544 initialize dest_high to zero rather than copies of the sign
10545 bit from B. Furthermore, we need to use a logical right shift
10546 in this case.
10548 The choice of sign-extension versus zero-extension is
10549 determined entirely by whether or not B is signed and is
10550 independent of the current setting of unsignedp. */
10552 temp = NULL_RTX;
10553 if (code == LSHIFT_EXPR
10554 && target
10555 && REG_P (target)
10556 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10557 && mode == int_mode
10558 && TREE_CONSTANT (treeop1)
10559 && TREE_CODE (treeop0) == SSA_NAME)
10561 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10562 if (is_gimple_assign (def)
10563 && gimple_assign_rhs_code (def) == NOP_EXPR)
10565 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10566 (TREE_TYPE (gimple_assign_rhs1 (def)));
10568 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10569 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10570 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10571 >= GET_MODE_BITSIZE (word_mode)))
10573 rtx_insn *seq, *seq_old;
10574 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10575 int_mode);
10576 bool extend_unsigned
10577 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10578 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10579 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10580 rtx dest_high = simplify_gen_subreg (word_mode, target,
10581 int_mode, high_off);
10582 HOST_WIDE_INT ramount = (BITS_PER_WORD
10583 - TREE_INT_CST_LOW (treeop1));
10584 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10586 start_sequence ();
10587 /* dest_high = src_low >> (word_size - C). */
10588 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10589 rshift, dest_high,
10590 extend_unsigned);
10591 if (temp != dest_high)
10592 emit_move_insn (dest_high, temp);
10594 /* dest_low = src_low << C. */
10595 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10596 treeop1, dest_low, unsignedp);
10597 if (temp != dest_low)
10598 emit_move_insn (dest_low, temp);
10600 seq = get_insns ();
10601 end_sequence ();
10602 temp = target ;
10604 if (have_insn_for (ASHIFT, int_mode))
10606 bool speed_p = optimize_insn_for_speed_p ();
10607 start_sequence ();
10608 rtx ret_old = expand_variable_shift (code, int_mode,
10609 op0, treeop1,
10610 target,
10611 unsignedp);
10613 seq_old = get_insns ();
10614 end_sequence ();
10615 if (seq_cost (seq, speed_p)
10616 >= seq_cost (seq_old, speed_p))
10618 seq = seq_old;
10619 temp = ret_old;
10622 emit_insn (seq);
10627 if (temp == NULL_RTX)
10628 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10629 unsignedp);
10630 if (code == LSHIFT_EXPR)
10631 temp = REDUCE_BIT_FIELD (temp);
10632 return temp;
10635 /* Could determine the answer when only additive constants differ. Also,
10636 the addition of one can be handled by changing the condition. */
10637 case LT_EXPR:
10638 case LE_EXPR:
10639 case GT_EXPR:
10640 case GE_EXPR:
10641 case EQ_EXPR:
10642 case NE_EXPR:
10643 case UNORDERED_EXPR:
10644 case ORDERED_EXPR:
10645 case UNLT_EXPR:
10646 case UNLE_EXPR:
10647 case UNGT_EXPR:
10648 case UNGE_EXPR:
10649 case UNEQ_EXPR:
10650 case LTGT_EXPR:
10652 temp = do_store_flag (ops,
10653 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10654 tmode != VOIDmode ? tmode : mode);
10655 if (temp)
10656 return temp;
10658 /* Use a compare and a jump for BLKmode comparisons, or for function
10659 type comparisons is have_canonicalize_funcptr_for_compare. */
10661 if ((target == 0
10662 || modifier == EXPAND_STACK_PARM
10663 || ! safe_from_p (target, treeop0, 1)
10664 || ! safe_from_p (target, treeop1, 1)
10665 /* Make sure we don't have a hard reg (such as function's return
10666 value) live across basic blocks, if not optimizing. */
10667 || (!optimize && REG_P (target)
10668 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10669 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10671 emit_move_insn (target, const0_rtx);
10673 rtx_code_label *lab1 = gen_label_rtx ();
10674 jumpifnot_1 (code, treeop0, treeop1, lab1,
10675 profile_probability::uninitialized ());
10677 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10678 emit_move_insn (target, constm1_rtx);
10679 else
10680 emit_move_insn (target, const1_rtx);
10682 emit_label (lab1);
10683 return target;
10685 case COMPLEX_EXPR:
10686 /* Get the rtx code of the operands. */
10687 op0 = expand_normal (treeop0);
10688 op1 = expand_normal (treeop1);
10690 if (!target)
10691 target = gen_reg_rtx (TYPE_MODE (type));
10692 else
10693 /* If target overlaps with op1, then either we need to force
10694 op1 into a pseudo (if target also overlaps with op0),
10695 or write the complex parts in reverse order. */
10696 switch (GET_CODE (target))
10698 case CONCAT:
10699 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10701 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10703 complex_expr_force_op1:
10704 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10705 emit_move_insn (temp, op1);
10706 op1 = temp;
10707 break;
10709 complex_expr_swap_order:
10710 /* Move the imaginary (op1) and real (op0) parts to their
10711 location. */
10712 write_complex_part (target, op1, true, true);
10713 write_complex_part (target, op0, false, false);
10715 return target;
10717 break;
10718 case MEM:
10719 temp = adjust_address_nv (target,
10720 GET_MODE_INNER (GET_MODE (target)), 0);
10721 if (reg_overlap_mentioned_p (temp, op1))
10723 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10724 temp = adjust_address_nv (target, imode,
10725 GET_MODE_SIZE (imode));
10726 if (reg_overlap_mentioned_p (temp, op0))
10727 goto complex_expr_force_op1;
10728 goto complex_expr_swap_order;
10730 break;
10731 default:
10732 if (reg_overlap_mentioned_p (target, op1))
10734 if (reg_overlap_mentioned_p (target, op0))
10735 goto complex_expr_force_op1;
10736 goto complex_expr_swap_order;
10738 break;
10741 /* Move the real (op0) and imaginary (op1) parts to their location. */
10742 write_complex_part (target, op0, false, true);
10743 write_complex_part (target, op1, true, false);
10745 return target;
10747 case WIDEN_SUM_EXPR:
10749 tree oprnd0 = treeop0;
10750 tree oprnd1 = treeop1;
10752 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10753 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10754 target, unsignedp);
10755 return target;
10758 case VEC_UNPACK_HI_EXPR:
10759 case VEC_UNPACK_LO_EXPR:
10760 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10761 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10763 op0 = expand_normal (treeop0);
10764 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10765 target, unsignedp);
10766 gcc_assert (temp);
10767 return temp;
10770 case VEC_UNPACK_FLOAT_HI_EXPR:
10771 case VEC_UNPACK_FLOAT_LO_EXPR:
10773 op0 = expand_normal (treeop0);
10774 /* The signedness is determined from input operand. */
10775 temp = expand_widen_pattern_expr
10776 (ops, op0, NULL_RTX, NULL_RTX,
10777 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10779 gcc_assert (temp);
10780 return temp;
10783 case VEC_WIDEN_MULT_HI_EXPR:
10784 case VEC_WIDEN_MULT_LO_EXPR:
10785 case VEC_WIDEN_MULT_EVEN_EXPR:
10786 case VEC_WIDEN_MULT_ODD_EXPR:
10787 case VEC_WIDEN_LSHIFT_HI_EXPR:
10788 case VEC_WIDEN_LSHIFT_LO_EXPR:
10789 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10790 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10791 target, unsignedp);
10792 gcc_assert (target);
10793 return target;
10795 case VEC_PACK_SAT_EXPR:
10796 case VEC_PACK_FIX_TRUNC_EXPR:
10797 mode = TYPE_MODE (TREE_TYPE (treeop0));
10798 subtarget = NULL_RTX;
10799 goto binop;
10801 case VEC_PACK_TRUNC_EXPR:
10802 if (VECTOR_BOOLEAN_TYPE_P (type)
10803 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10804 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10805 && SCALAR_INT_MODE_P (mode))
10807 class expand_operand eops[4];
10808 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10809 expand_operands (treeop0, treeop1,
10810 subtarget, &op0, &op1, EXPAND_NORMAL);
10811 this_optab = vec_pack_sbool_trunc_optab;
10812 enum insn_code icode = optab_handler (this_optab, imode);
10813 create_output_operand (&eops[0], target, mode);
10814 create_convert_operand_from (&eops[1], op0, imode, false);
10815 create_convert_operand_from (&eops[2], op1, imode, false);
10816 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10817 create_input_operand (&eops[3], temp, imode);
10818 expand_insn (icode, 4, eops);
10819 return eops[0].value;
10821 mode = TYPE_MODE (TREE_TYPE (treeop0));
10822 subtarget = NULL_RTX;
10823 goto binop;
10825 case VEC_PACK_FLOAT_EXPR:
10826 mode = TYPE_MODE (TREE_TYPE (treeop0));
10827 expand_operands (treeop0, treeop1,
10828 subtarget, &op0, &op1, EXPAND_NORMAL);
10829 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10830 optab_default);
10831 target = expand_binop (mode, this_optab, op0, op1, target,
10832 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10833 OPTAB_LIB_WIDEN);
10834 gcc_assert (target);
10835 return target;
10837 case VEC_PERM_EXPR:
10839 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10840 vec_perm_builder sel;
10841 if (TREE_CODE (treeop2) == VECTOR_CST
10842 && tree_to_vec_perm_builder (&sel, treeop2))
10844 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10845 temp = expand_vec_perm_const (mode, op0, op1, sel,
10846 sel_mode, target);
10848 else
10850 op2 = expand_normal (treeop2);
10851 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10853 gcc_assert (temp);
10854 return temp;
10857 case DOT_PROD_EXPR:
10859 tree oprnd0 = treeop0;
10860 tree oprnd1 = treeop1;
10861 tree oprnd2 = treeop2;
10863 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10864 op2 = expand_normal (oprnd2);
10865 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10866 target, unsignedp);
10867 return target;
10870 case SAD_EXPR:
10872 tree oprnd0 = treeop0;
10873 tree oprnd1 = treeop1;
10874 tree oprnd2 = treeop2;
10876 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10877 op2 = expand_normal (oprnd2);
10878 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10879 target, unsignedp);
10880 return target;
10883 case REALIGN_LOAD_EXPR:
10885 tree oprnd0 = treeop0;
10886 tree oprnd1 = treeop1;
10887 tree oprnd2 = treeop2;
10889 this_optab = optab_for_tree_code (code, type, optab_default);
10890 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10891 op2 = expand_normal (oprnd2);
10892 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10893 target, unsignedp);
10894 gcc_assert (temp);
10895 return temp;
10898 case COND_EXPR:
10900 /* A COND_EXPR with its type being VOID_TYPE represents a
10901 conditional jump and is handled in
10902 expand_gimple_cond_expr. */
10903 gcc_assert (!VOID_TYPE_P (type));
10905 /* Note that COND_EXPRs whose type is a structure or union
10906 are required to be constructed to contain assignments of
10907 a temporary variable, so that we can evaluate them here
10908 for side effect only. If type is void, we must do likewise. */
10910 gcc_assert (!TREE_ADDRESSABLE (type)
10911 && !ignore
10912 && TREE_TYPE (treeop1) != void_type_node
10913 && TREE_TYPE (treeop2) != void_type_node);
10915 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10916 if (temp)
10917 return temp;
10919 /* If we are not to produce a result, we have no target. Otherwise,
10920 if a target was specified use it; it will not be used as an
10921 intermediate target unless it is safe. If no target, use a
10922 temporary. */
10924 if (modifier != EXPAND_STACK_PARM
10925 && original_target
10926 && safe_from_p (original_target, treeop0, 1)
10927 && GET_MODE (original_target) == mode
10928 && !MEM_P (original_target))
10929 temp = original_target;
10930 else
10931 temp = assign_temp (type, 0, 1);
10933 do_pending_stack_adjust ();
10934 NO_DEFER_POP;
10935 rtx_code_label *lab0 = gen_label_rtx ();
10936 rtx_code_label *lab1 = gen_label_rtx ();
10937 jumpifnot (treeop0, lab0,
10938 profile_probability::uninitialized ());
10939 store_expr (treeop1, temp,
10940 modifier == EXPAND_STACK_PARM,
10941 false, false);
10943 emit_jump_insn (targetm.gen_jump (lab1));
10944 emit_barrier ();
10945 emit_label (lab0);
10946 store_expr (treeop2, temp,
10947 modifier == EXPAND_STACK_PARM,
10948 false, false);
10950 emit_label (lab1);
10951 OK_DEFER_POP;
10952 return temp;
10955 case VEC_DUPLICATE_EXPR:
10956 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10957 target = expand_vector_broadcast (mode, op0);
10958 gcc_assert (target);
10959 return target;
10961 case VEC_SERIES_EXPR:
10962 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10963 return expand_vec_series_expr (mode, op0, op1, target);
10965 case BIT_INSERT_EXPR:
10967 unsigned bitpos = tree_to_uhwi (treeop2);
10968 unsigned bitsize;
10969 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10970 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10971 else
10972 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10973 op0 = expand_normal (treeop0);
10974 op1 = expand_normal (treeop1);
10975 rtx dst = gen_reg_rtx (mode);
10976 emit_move_insn (dst, op0);
10977 store_bit_field (dst, bitsize, bitpos, 0, 0,
10978 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10979 return dst;
10982 default:
10983 gcc_unreachable ();
10986 /* Here to do an ordinary binary operator. */
10987 binop:
10988 expand_operands (treeop0, treeop1,
10989 subtarget, &op0, &op1, EXPAND_NORMAL);
10990 binop2:
10991 this_optab = optab_for_tree_code (code, type, optab_default);
10992 binop3:
10993 if (modifier == EXPAND_STACK_PARM)
10994 target = 0;
10995 temp = expand_binop (mode, this_optab, op0, op1, target,
10996 unsignedp, OPTAB_LIB_WIDEN);
10997 gcc_assert (temp);
10998 /* Bitwise operations do not need bitfield reduction as we expect their
10999 operands being properly truncated. */
11000 if (code == BIT_XOR_EXPR
11001 || code == BIT_AND_EXPR
11002 || code == BIT_IOR_EXPR)
11003 return temp;
11004 return REDUCE_BIT_FIELD (temp);
11006 #undef REDUCE_BIT_FIELD
11009 /* Return TRUE if expression STMT is suitable for replacement.
11010 Never consider memory loads as replaceable, because those don't ever lead
11011 into constant expressions. */
11013 static bool
11014 stmt_is_replaceable_p (gimple *stmt)
11016 if (ssa_is_replaceable_p (stmt))
11018 /* Don't move around loads. */
11019 if (!gimple_assign_single_p (stmt)
11020 || is_gimple_val (gimple_assign_rhs1 (stmt)))
11021 return true;
11023 return false;
11027 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
11028 enum expand_modifier modifier, rtx *alt_rtl,
11029 bool inner_reference_p)
11031 rtx op0, op1, temp, decl_rtl;
11032 tree type;
11033 int unsignedp;
11034 machine_mode mode, dmode;
11035 enum tree_code code = TREE_CODE (exp);
11036 rtx subtarget, original_target;
11037 int ignore;
11038 bool reduce_bit_field;
11039 location_t loc = EXPR_LOCATION (exp);
11040 struct separate_ops ops;
11041 tree treeop0, treeop1, treeop2;
11042 tree ssa_name = NULL_TREE;
11043 gimple *g;
11045 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL
11046 expansion sign/zero extends integral types with less than mode precision
11047 when reading from bit-fields and after arithmetic operations (see
11048 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies
11049 on those extensions to have been already performed, but because of the
11050 above for _BitInt they need to be sign/zero extended when reading from
11051 locations that could be exposed to ABI boundaries (when loading from
11052 objects in memory, or function arguments, return value). Because we
11053 internally extend after arithmetic operations, we can avoid doing that
11054 when reading from SSA_NAMEs of vars. */
11055 #define EXTEND_BITINT(expr) \
11056 ((TREE_CODE (type) == BITINT_TYPE \
11057 && reduce_bit_field \
11058 && mode != BLKmode \
11059 && modifier != EXPAND_MEMORY \
11060 && modifier != EXPAND_WRITE \
11061 && modifier != EXPAND_INITIALIZER \
11062 && modifier != EXPAND_CONST_ADDRESS) \
11063 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr))
11065 type = TREE_TYPE (exp);
11066 mode = TYPE_MODE (type);
11067 unsignedp = TYPE_UNSIGNED (type);
11069 treeop0 = treeop1 = treeop2 = NULL_TREE;
11070 if (!VL_EXP_CLASS_P (exp))
11071 switch (TREE_CODE_LENGTH (code))
11073 default:
11074 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
11075 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
11076 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
11077 case 0: break;
11079 ops.code = code;
11080 ops.type = type;
11081 ops.op0 = treeop0;
11082 ops.op1 = treeop1;
11083 ops.op2 = treeop2;
11084 ops.location = loc;
11086 ignore = (target == const0_rtx
11087 || ((CONVERT_EXPR_CODE_P (code)
11088 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
11089 && TREE_CODE (type) == VOID_TYPE));
11091 /* An operation in what may be a bit-field type needs the
11092 result to be reduced to the precision of the bit-field type,
11093 which is narrower than that of the type's mode. */
11094 reduce_bit_field = (!ignore
11095 && INTEGRAL_TYPE_P (type)
11096 && !type_has_mode_precision_p (type));
11098 /* If we are going to ignore this result, we need only do something
11099 if there is a side-effect somewhere in the expression. If there
11100 is, short-circuit the most common cases here. Note that we must
11101 not call expand_expr with anything but const0_rtx in case this
11102 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
11104 if (ignore)
11106 if (! TREE_SIDE_EFFECTS (exp))
11107 return const0_rtx;
11109 /* Ensure we reference a volatile object even if value is ignored, but
11110 don't do this if all we are doing is taking its address. */
11111 if (TREE_THIS_VOLATILE (exp)
11112 && TREE_CODE (exp) != FUNCTION_DECL
11113 && mode != VOIDmode && mode != BLKmode
11114 && modifier != EXPAND_CONST_ADDRESS)
11116 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
11117 if (MEM_P (temp))
11118 copy_to_reg (temp);
11119 return const0_rtx;
11122 if (TREE_CODE_CLASS (code) == tcc_unary
11123 || code == BIT_FIELD_REF
11124 || code == COMPONENT_REF
11125 || code == INDIRECT_REF)
11126 return expand_expr (treeop0, const0_rtx, VOIDmode,
11127 modifier);
11129 else if (TREE_CODE_CLASS (code) == tcc_binary
11130 || TREE_CODE_CLASS (code) == tcc_comparison
11131 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
11133 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
11134 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
11135 return const0_rtx;
11138 target = 0;
11141 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
11142 target = 0;
11144 /* Use subtarget as the target for operand 0 of a binary operation. */
11145 subtarget = get_subtarget (target);
11146 original_target = target;
11148 switch (code)
11150 case LABEL_DECL:
11152 tree function = decl_function_context (exp);
11154 temp = label_rtx (exp);
11155 temp = gen_rtx_LABEL_REF (Pmode, temp);
11157 if (function != current_function_decl
11158 && function != 0)
11159 LABEL_REF_NONLOCAL_P (temp) = 1;
11161 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
11162 return temp;
11165 case SSA_NAME:
11166 /* ??? ivopts calls expander, without any preparation from
11167 out-of-ssa. So fake instructions as if this was an access to the
11168 base variable. This unnecessarily allocates a pseudo, see how we can
11169 reuse it, if partition base vars have it set already. */
11170 if (!currently_expanding_to_rtl)
11172 tree var = SSA_NAME_VAR (exp);
11173 if (var && DECL_RTL_SET_P (var))
11174 return DECL_RTL (var);
11175 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
11176 LAST_VIRTUAL_REGISTER + 1);
11179 g = get_gimple_for_ssa_name (exp);
11180 /* For EXPAND_INITIALIZER try harder to get something simpler. */
11181 if (g == NULL
11182 && modifier == EXPAND_INITIALIZER
11183 && !SSA_NAME_IS_DEFAULT_DEF (exp)
11184 && (optimize || !SSA_NAME_VAR (exp)
11185 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
11186 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
11187 g = SSA_NAME_DEF_STMT (exp);
11188 if (g)
11190 rtx r;
11191 location_t saved_loc = curr_insn_location ();
11192 loc = gimple_location (g);
11193 if (loc != UNKNOWN_LOCATION)
11194 set_curr_insn_location (loc);
11195 ops.code = gimple_assign_rhs_code (g);
11196 switch (get_gimple_rhs_class (ops.code))
11198 case GIMPLE_TERNARY_RHS:
11199 ops.op2 = gimple_assign_rhs3 (g);
11200 /* Fallthru */
11201 case GIMPLE_BINARY_RHS:
11202 ops.op1 = gimple_assign_rhs2 (g);
11204 /* Try to expand conditonal compare. */
11205 if (targetm.gen_ccmp_first)
11207 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
11208 r = expand_ccmp_expr (g, mode);
11209 if (r)
11210 break;
11212 /* Fallthru */
11213 case GIMPLE_UNARY_RHS:
11214 ops.op0 = gimple_assign_rhs1 (g);
11215 ops.type = TREE_TYPE (gimple_assign_lhs (g));
11216 ops.location = loc;
11217 r = expand_expr_real_2 (&ops, target, tmode, modifier);
11218 break;
11219 case GIMPLE_SINGLE_RHS:
11221 r = expand_expr_real (gimple_assign_rhs1 (g), target,
11222 tmode, modifier, alt_rtl,
11223 inner_reference_p);
11224 break;
11226 default:
11227 gcc_unreachable ();
11229 set_curr_insn_location (saved_loc);
11230 if (REG_P (r) && !REG_EXPR (r))
11231 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
11232 return r;
11235 ssa_name = exp;
11236 decl_rtl = get_rtx_for_ssa_name (ssa_name);
11237 exp = SSA_NAME_VAR (ssa_name);
11238 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an
11239 SSA_NAME computed within the current function. In such case the
11240 value have been already extended before. While if it is a function
11241 parameter, result or some memory location, we need to be prepared
11242 for some other compiler leaving the bits uninitialized. */
11243 if (!exp || VAR_P (exp))
11244 reduce_bit_field = false;
11245 goto expand_decl_rtl;
11247 case VAR_DECL:
11248 /* Allow accel compiler to handle variables that require special
11249 treatment, e.g. if they have been modified in some way earlier in
11250 compilation by the adjust_private_decl OpenACC hook. */
11251 if (flag_openacc && targetm.goacc.expand_var_decl)
11253 temp = targetm.goacc.expand_var_decl (exp);
11254 if (temp)
11255 return temp;
11257 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
11258 have scalar integer modes to a reg via store_constructor. */
11259 if (TREE_READONLY (exp)
11260 && !TREE_SIDE_EFFECTS (exp)
11261 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
11262 && immediate_const_ctor_p (DECL_INITIAL (exp))
11263 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
11264 && crtl->emit.regno_pointer_align_length
11265 && !target)
11267 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
11268 store_constructor (DECL_INITIAL (exp), target, 0,
11269 int_expr_size (DECL_INITIAL (exp)), false);
11270 return target;
11272 /* ... fall through ... */
11274 case PARM_DECL:
11275 /* If a static var's type was incomplete when the decl was written,
11276 but the type is complete now, lay out the decl now. */
11277 if (DECL_SIZE (exp) == 0
11278 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
11279 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
11280 layout_decl (exp, 0);
11282 /* fall through */
11284 case FUNCTION_DECL:
11285 case RESULT_DECL:
11286 decl_rtl = DECL_RTL (exp);
11287 expand_decl_rtl:
11288 gcc_assert (decl_rtl);
11290 /* DECL_MODE might change when TYPE_MODE depends on attribute target
11291 settings for VECTOR_TYPE_P that might switch for the function. */
11292 if (currently_expanding_to_rtl
11293 && code == VAR_DECL && MEM_P (decl_rtl)
11294 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
11295 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
11296 else
11297 decl_rtl = copy_rtx (decl_rtl);
11299 /* Record writes to register variables. */
11300 if (modifier == EXPAND_WRITE
11301 && REG_P (decl_rtl)
11302 && HARD_REGISTER_P (decl_rtl))
11303 add_to_hard_reg_set (&crtl->asm_clobbers,
11304 GET_MODE (decl_rtl), REGNO (decl_rtl));
11306 /* Ensure variable marked as used even if it doesn't go through
11307 a parser. If it hasn't be used yet, write out an external
11308 definition. */
11309 if (exp)
11310 TREE_USED (exp) = 1;
11312 /* Show we haven't gotten RTL for this yet. */
11313 temp = 0;
11315 /* Variables inherited from containing functions should have
11316 been lowered by this point. */
11317 if (exp)
11319 tree context = decl_function_context (exp);
11320 gcc_assert (SCOPE_FILE_SCOPE_P (context)
11321 || context == current_function_decl
11322 || TREE_STATIC (exp)
11323 || DECL_EXTERNAL (exp)
11324 /* ??? C++ creates functions that are not
11325 TREE_STATIC. */
11326 || TREE_CODE (exp) == FUNCTION_DECL);
11329 /* This is the case of an array whose size is to be determined
11330 from its initializer, while the initializer is still being parsed.
11331 ??? We aren't parsing while expanding anymore. */
11333 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
11334 temp = validize_mem (decl_rtl);
11336 /* If DECL_RTL is memory, we are in the normal case and the
11337 address is not valid, get the address into a register. */
11339 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
11341 if (alt_rtl)
11342 *alt_rtl = decl_rtl;
11343 decl_rtl = use_anchored_address (decl_rtl);
11344 if (modifier != EXPAND_CONST_ADDRESS
11345 && modifier != EXPAND_SUM
11346 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
11347 : GET_MODE (decl_rtl),
11348 XEXP (decl_rtl, 0),
11349 MEM_ADDR_SPACE (decl_rtl)))
11350 temp = replace_equiv_address (decl_rtl,
11351 copy_rtx (XEXP (decl_rtl, 0)));
11354 /* If we got something, return it. But first, set the alignment
11355 if the address is a register. */
11356 if (temp != 0)
11358 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
11359 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
11361 else if (MEM_P (decl_rtl))
11362 temp = decl_rtl;
11364 if (temp != 0)
11366 if (MEM_P (temp)
11367 && modifier != EXPAND_WRITE
11368 && modifier != EXPAND_MEMORY
11369 && modifier != EXPAND_INITIALIZER
11370 && modifier != EXPAND_CONST_ADDRESS
11371 && modifier != EXPAND_SUM
11372 && !inner_reference_p
11373 && mode != BLKmode
11374 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
11375 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11376 MEM_ALIGN (temp), NULL_RTX, NULL);
11378 return EXTEND_BITINT (temp);
11381 if (exp)
11382 dmode = DECL_MODE (exp);
11383 else
11384 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
11386 /* If the mode of DECL_RTL does not match that of the decl,
11387 there are two cases: we are dealing with a BLKmode value
11388 that is returned in a register, or we are dealing with
11389 a promoted value. In the latter case, return a SUBREG
11390 of the wanted mode, but mark it so that we know that it
11391 was already extended. */
11392 if (REG_P (decl_rtl)
11393 && dmode != BLKmode
11394 && GET_MODE (decl_rtl) != dmode)
11396 machine_mode pmode;
11398 /* Get the signedness to be used for this variable. Ensure we get
11399 the same mode we got when the variable was declared. */
11400 if (code != SSA_NAME)
11401 pmode = promote_decl_mode (exp, &unsignedp);
11402 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
11403 && gimple_code (g) == GIMPLE_CALL
11404 && !gimple_call_internal_p (g))
11405 pmode = promote_function_mode (type, mode, &unsignedp,
11406 gimple_call_fntype (g),
11408 else
11409 pmode = promote_ssa_mode (ssa_name, &unsignedp);
11410 gcc_assert (GET_MODE (decl_rtl) == pmode);
11412 /* Some ABIs require scalar floating point modes to be passed
11413 in a wider scalar integer mode. We need to explicitly
11414 truncate to an integer mode of the correct precision before
11415 using a SUBREG to reinterpret as a floating point value. */
11416 if (SCALAR_FLOAT_MODE_P (mode)
11417 && SCALAR_INT_MODE_P (pmode)
11418 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11419 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11421 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11422 SUBREG_PROMOTED_VAR_P (temp) = 1;
11423 SUBREG_PROMOTED_SET (temp, unsignedp);
11424 return EXTEND_BITINT (temp);
11427 return EXTEND_BITINT (decl_rtl);
11429 case INTEGER_CST:
11431 if (TREE_CODE (type) == BITINT_TYPE)
11433 unsigned int prec = TYPE_PRECISION (type);
11434 struct bitint_info info;
11435 bool ok = targetm.c.bitint_type_info (prec, &info);
11436 gcc_assert (ok);
11437 scalar_int_mode limb_mode
11438 = as_a <scalar_int_mode> (info.limb_mode);
11439 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode);
11440 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE)
11442 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */
11443 exp = tree_output_constant_def (exp);
11444 return expand_expr (exp, target, VOIDmode, modifier);
11448 /* Given that TYPE_PRECISION (type) is not always equal to
11449 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11450 the former to the latter according to the signedness of the
11451 type. */
11452 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11453 temp = immed_wide_int_const
11454 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11455 return temp;
11458 case VECTOR_CST:
11460 tree tmp = NULL_TREE;
11461 if (VECTOR_MODE_P (mode))
11462 return const_vector_from_tree (exp);
11463 scalar_int_mode int_mode;
11464 if (is_int_mode (mode, &int_mode))
11466 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11467 if (type_for_mode)
11468 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11469 type_for_mode, exp);
11471 if (!tmp)
11473 vec<constructor_elt, va_gc> *v;
11474 /* Constructors need to be fixed-length. FIXME. */
11475 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11476 vec_alloc (v, nunits);
11477 for (unsigned int i = 0; i < nunits; ++i)
11478 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11479 tmp = build_constructor (type, v);
11481 return expand_expr (tmp, ignore ? const0_rtx : target,
11482 tmode, modifier);
11485 case CONST_DECL:
11486 if (modifier == EXPAND_WRITE)
11488 /* Writing into CONST_DECL is always invalid, but handle it
11489 gracefully. */
11490 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11491 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11492 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11493 EXPAND_NORMAL, as);
11494 op0 = memory_address_addr_space (mode, op0, as);
11495 temp = gen_rtx_MEM (mode, op0);
11496 set_mem_addr_space (temp, as);
11497 return temp;
11499 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11501 case REAL_CST:
11502 /* If optimized, generate immediate CONST_DOUBLE
11503 which will be turned into memory by reload if necessary.
11505 We used to force a register so that loop.c could see it. But
11506 this does not allow gen_* patterns to perform optimizations with
11507 the constants. It also produces two insns in cases like "x = 1.0;".
11508 On most machines, floating-point constants are not permitted in
11509 many insns, so we'd end up copying it to a register in any case.
11511 Now, we do the copying in expand_binop, if appropriate. */
11512 return const_double_from_real_value (TREE_REAL_CST (exp),
11513 TYPE_MODE (TREE_TYPE (exp)));
11515 case FIXED_CST:
11516 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11517 TYPE_MODE (TREE_TYPE (exp)));
11519 case COMPLEX_CST:
11520 /* Handle evaluating a complex constant in a CONCAT target. */
11521 if (original_target && GET_CODE (original_target) == CONCAT)
11523 rtx rtarg, itarg;
11525 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11526 rtarg = XEXP (original_target, 0);
11527 itarg = XEXP (original_target, 1);
11529 /* Move the real and imaginary parts separately. */
11530 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11531 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11533 if (op0 != rtarg)
11534 emit_move_insn (rtarg, op0);
11535 if (op1 != itarg)
11536 emit_move_insn (itarg, op1);
11538 return original_target;
11541 /* fall through */
11543 case STRING_CST:
11544 temp = expand_expr_constant (exp, 1, modifier);
11546 /* temp contains a constant address.
11547 On RISC machines where a constant address isn't valid,
11548 make some insns to get that address into a register. */
11549 if (modifier != EXPAND_CONST_ADDRESS
11550 && modifier != EXPAND_INITIALIZER
11551 && modifier != EXPAND_SUM
11552 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11553 MEM_ADDR_SPACE (temp)))
11554 return replace_equiv_address (temp,
11555 copy_rtx (XEXP (temp, 0)));
11556 return temp;
11558 case POLY_INT_CST:
11559 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11561 case SAVE_EXPR:
11563 tree val = treeop0;
11564 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11565 inner_reference_p);
11567 if (!SAVE_EXPR_RESOLVED_P (exp))
11569 /* We can indeed still hit this case, typically via builtin
11570 expanders calling save_expr immediately before expanding
11571 something. Assume this means that we only have to deal
11572 with non-BLKmode values. */
11573 gcc_assert (GET_MODE (ret) != BLKmode);
11575 val = build_decl (curr_insn_location (),
11576 VAR_DECL, NULL, TREE_TYPE (exp));
11577 DECL_ARTIFICIAL (val) = 1;
11578 DECL_IGNORED_P (val) = 1;
11579 treeop0 = val;
11580 TREE_OPERAND (exp, 0) = treeop0;
11581 SAVE_EXPR_RESOLVED_P (exp) = 1;
11583 if (!CONSTANT_P (ret))
11584 ret = copy_to_reg (ret);
11585 SET_DECL_RTL (val, ret);
11588 return ret;
11592 case CONSTRUCTOR:
11593 /* If we don't need the result, just ensure we evaluate any
11594 subexpressions. */
11595 if (ignore)
11597 unsigned HOST_WIDE_INT idx;
11598 tree value;
11600 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11601 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11603 return const0_rtx;
11606 return expand_constructor (exp, target, modifier, false);
11608 case TARGET_MEM_REF:
11610 addr_space_t as
11611 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11612 unsigned int align;
11614 op0 = addr_for_mem_ref (exp, as, true);
11615 op0 = memory_address_addr_space (mode, op0, as);
11616 temp = gen_rtx_MEM (mode, op0);
11617 set_mem_attributes (temp, exp, 0);
11618 set_mem_addr_space (temp, as);
11619 align = get_object_alignment (exp);
11620 if (modifier != EXPAND_WRITE
11621 && modifier != EXPAND_MEMORY
11622 && mode != BLKmode
11623 && align < GET_MODE_ALIGNMENT (mode))
11624 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11625 align, NULL_RTX, NULL);
11626 return EXTEND_BITINT (temp);
11629 case MEM_REF:
11631 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11632 addr_space_t as
11633 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11634 machine_mode address_mode;
11635 tree base = TREE_OPERAND (exp, 0);
11636 gimple *def_stmt;
11637 unsigned align;
11638 /* Handle expansion of non-aliased memory with non-BLKmode. That
11639 might end up in a register. */
11640 if (mem_ref_refers_to_non_mem_p (exp))
11642 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11643 base = TREE_OPERAND (base, 0);
11644 poly_uint64 type_size;
11645 if (known_eq (offset, 0)
11646 && !reverse
11647 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11648 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11649 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11650 target, tmode, modifier);
11651 if (TYPE_MODE (type) == BLKmode)
11653 temp = assign_stack_temp (DECL_MODE (base),
11654 GET_MODE_SIZE (DECL_MODE (base)));
11655 store_expr (base, temp, 0, false, false);
11656 temp = adjust_address (temp, BLKmode, offset);
11657 set_mem_size (temp, int_size_in_bytes (type));
11658 return temp;
11660 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11661 bitsize_int (offset * BITS_PER_UNIT));
11662 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11663 return expand_expr (exp, target, tmode, modifier);
11665 address_mode = targetm.addr_space.address_mode (as);
11666 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11668 tree mask = gimple_assign_rhs2 (def_stmt);
11669 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11670 gimple_assign_rhs1 (def_stmt), mask);
11671 TREE_OPERAND (exp, 0) = base;
11673 align = get_object_alignment (exp);
11674 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11675 op0 = memory_address_addr_space (mode, op0, as);
11676 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11678 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11679 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11680 op0 = memory_address_addr_space (mode, op0, as);
11682 temp = gen_rtx_MEM (mode, op0);
11683 set_mem_attributes (temp, exp, 0);
11684 set_mem_addr_space (temp, as);
11685 if (TREE_THIS_VOLATILE (exp))
11686 MEM_VOLATILE_P (temp) = 1;
11687 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY)
11688 return temp;
11689 if (!inner_reference_p
11690 && mode != BLKmode
11691 && align < GET_MODE_ALIGNMENT (mode))
11692 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11693 modifier == EXPAND_STACK_PARM
11694 ? NULL_RTX : target, alt_rtl);
11695 if (reverse)
11696 temp = flip_storage_order (mode, temp);
11697 return EXTEND_BITINT (temp);
11700 case ARRAY_REF:
11703 tree array = treeop0;
11704 tree index = treeop1;
11705 tree init;
11707 /* Fold an expression like: "foo"[2].
11708 This is not done in fold so it won't happen inside &.
11709 Don't fold if this is for wide characters since it's too
11710 difficult to do correctly and this is a very rare case. */
11712 if (modifier != EXPAND_CONST_ADDRESS
11713 && modifier != EXPAND_INITIALIZER
11714 && modifier != EXPAND_MEMORY)
11716 tree t = fold_read_from_constant_string (exp);
11718 if (t)
11719 return expand_expr (t, target, tmode, modifier);
11722 /* If this is a constant index into a constant array,
11723 just get the value from the array. Handle both the cases when
11724 we have an explicit constructor and when our operand is a variable
11725 that was declared const. */
11727 if (modifier != EXPAND_CONST_ADDRESS
11728 && modifier != EXPAND_INITIALIZER
11729 && modifier != EXPAND_MEMORY
11730 && TREE_CODE (array) == CONSTRUCTOR
11731 && ! TREE_SIDE_EFFECTS (array)
11732 && TREE_CODE (index) == INTEGER_CST)
11734 unsigned HOST_WIDE_INT ix;
11735 tree field, value;
11737 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11738 field, value)
11739 if (tree_int_cst_equal (field, index))
11741 if (!TREE_SIDE_EFFECTS (value))
11742 return expand_expr (fold (value), target, tmode, modifier);
11743 break;
11747 else if (optimize >= 1
11748 && modifier != EXPAND_CONST_ADDRESS
11749 && modifier != EXPAND_INITIALIZER
11750 && modifier != EXPAND_MEMORY
11751 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11752 && TREE_CODE (index) == INTEGER_CST
11753 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11754 && (init = ctor_for_folding (array)) != error_mark_node)
11756 if (init == NULL_TREE)
11758 tree value = build_zero_cst (type);
11759 if (TREE_CODE (value) == CONSTRUCTOR)
11761 /* If VALUE is a CONSTRUCTOR, this optimization is only
11762 useful if this doesn't store the CONSTRUCTOR into
11763 memory. If it does, it is more efficient to just
11764 load the data from the array directly. */
11765 rtx ret = expand_constructor (value, target,
11766 modifier, true);
11767 if (ret == NULL_RTX)
11768 value = NULL_TREE;
11771 if (value)
11772 return expand_expr (value, target, tmode, modifier);
11774 else if (TREE_CODE (init) == CONSTRUCTOR)
11776 unsigned HOST_WIDE_INT ix;
11777 tree field, value;
11779 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11780 field, value)
11781 if (tree_int_cst_equal (field, index))
11783 if (TREE_SIDE_EFFECTS (value))
11784 break;
11786 if (TREE_CODE (value) == CONSTRUCTOR)
11788 /* If VALUE is a CONSTRUCTOR, this
11789 optimization is only useful if
11790 this doesn't store the CONSTRUCTOR
11791 into memory. If it does, it is more
11792 efficient to just load the data from
11793 the array directly. */
11794 rtx ret = expand_constructor (value, target,
11795 modifier, true);
11796 if (ret == NULL_RTX)
11797 break;
11800 return
11801 expand_expr (fold (value), target, tmode, modifier);
11804 else if (TREE_CODE (init) == STRING_CST)
11806 tree low_bound = array_ref_low_bound (exp);
11807 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11809 /* Optimize the special case of a zero lower bound.
11811 We convert the lower bound to sizetype to avoid problems
11812 with constant folding. E.g. suppose the lower bound is
11813 1 and its mode is QI. Without the conversion
11814 (ARRAY + (INDEX - (unsigned char)1))
11815 becomes
11816 (ARRAY + (-(unsigned char)1) + INDEX)
11817 which becomes
11818 (ARRAY + 255 + INDEX). Oops! */
11819 if (!integer_zerop (low_bound))
11820 index1 = size_diffop_loc (loc, index1,
11821 fold_convert_loc (loc, sizetype,
11822 low_bound));
11824 if (tree_fits_uhwi_p (index1)
11825 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11827 tree char_type = TREE_TYPE (TREE_TYPE (init));
11828 scalar_int_mode char_mode;
11830 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11831 && GET_MODE_SIZE (char_mode) == 1)
11832 return gen_int_mode (TREE_STRING_POINTER (init)
11833 [TREE_INT_CST_LOW (index1)],
11834 char_mode);
11839 goto normal_inner_ref;
11841 case COMPONENT_REF:
11842 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11843 /* Fall through. */
11844 case BIT_FIELD_REF:
11845 case ARRAY_RANGE_REF:
11846 normal_inner_ref:
11848 machine_mode mode1, mode2;
11849 poly_int64 bitsize, bitpos, bytepos;
11850 tree offset;
11851 int reversep, volatilep = 0;
11852 tree tem
11853 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11854 &unsignedp, &reversep, &volatilep);
11855 rtx orig_op0, memloc;
11856 bool clear_mem_expr = false;
11857 bool must_force_mem;
11859 /* If we got back the original object, something is wrong. Perhaps
11860 we are evaluating an expression too early. In any event, don't
11861 infinitely recurse. */
11862 gcc_assert (tem != exp);
11864 /* Make sure bitpos is not negative, this can wreak havoc later. */
11865 if (maybe_lt (bitpos, 0))
11867 gcc_checking_assert (offset == NULL_TREE);
11868 offset = size_int (bits_to_bytes_round_down (bitpos));
11869 bitpos = num_trailing_bits (bitpos);
11872 /* If we have either an offset, a BLKmode result, or a reference
11873 outside the underlying object, we must force it to memory.
11874 Such a case can occur in Ada if we have unchecked conversion
11875 of an expression from a scalar type to an aggregate type or
11876 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11877 passed a partially uninitialized object or a view-conversion
11878 to a larger size. */
11879 must_force_mem = offset != NULL_TREE
11880 || mode1 == BLKmode
11881 || (mode == BLKmode
11882 && !int_mode_for_size (bitsize, 1).exists ());
11884 const enum expand_modifier tem_modifier
11885 = must_force_mem
11886 ? EXPAND_MEMORY
11887 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11889 /* If TEM's type is a union of variable size, pass TARGET to the inner
11890 computation, since it will need a temporary and TARGET is known
11891 to have to do. This occurs in unchecked conversion in Ada. */
11892 const rtx tem_target
11893 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11894 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11895 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11896 && modifier != EXPAND_STACK_PARM
11897 ? target
11898 : NULL_RTX;
11900 orig_op0 = op0
11901 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11902 true);
11904 /* If the field has a mode, we want to access it in the
11905 field's mode, not the computed mode.
11906 If a MEM has VOIDmode (external with incomplete type),
11907 use BLKmode for it instead. */
11908 if (MEM_P (op0))
11910 if (mode1 != VOIDmode)
11911 op0 = adjust_address (op0, mode1, 0);
11912 else if (GET_MODE (op0) == VOIDmode)
11913 op0 = adjust_address (op0, BLKmode, 0);
11916 mode2
11917 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11919 /* See above for the rationale. */
11920 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11921 must_force_mem = true;
11923 /* Handle CONCAT first. */
11924 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11926 if (known_eq (bitpos, 0)
11927 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11928 && COMPLEX_MODE_P (mode1)
11929 && COMPLEX_MODE_P (GET_MODE (op0))
11930 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11931 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11933 if (reversep)
11934 op0 = flip_storage_order (GET_MODE (op0), op0);
11935 if (mode1 != GET_MODE (op0))
11937 rtx parts[2];
11938 for (int i = 0; i < 2; i++)
11940 rtx op = read_complex_part (op0, i != 0);
11941 if (GET_CODE (op) == SUBREG)
11942 op = force_reg (GET_MODE (op), op);
11943 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11944 if (temp)
11945 op = temp;
11946 else
11948 if (!REG_P (op) && !MEM_P (op))
11949 op = force_reg (GET_MODE (op), op);
11950 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11952 parts[i] = op;
11954 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11956 return op0;
11958 if (known_eq (bitpos, 0)
11959 && known_eq (bitsize,
11960 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11961 && maybe_ne (bitsize, 0))
11963 op0 = XEXP (op0, 0);
11964 mode2 = GET_MODE (op0);
11966 else if (known_eq (bitpos,
11967 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11968 && known_eq (bitsize,
11969 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11970 && maybe_ne (bitpos, 0)
11971 && maybe_ne (bitsize, 0))
11973 op0 = XEXP (op0, 1);
11974 bitpos = 0;
11975 mode2 = GET_MODE (op0);
11977 else
11978 /* Otherwise force into memory. */
11979 must_force_mem = true;
11982 /* If this is a constant, put it in a register if it is a legitimate
11983 constant and we don't need a memory reference. */
11984 if (CONSTANT_P (op0)
11985 && mode2 != BLKmode
11986 && targetm.legitimate_constant_p (mode2, op0)
11987 && !must_force_mem)
11988 op0 = force_reg (mode2, op0);
11990 /* Otherwise, if this is a constant, try to force it to the constant
11991 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11992 is a legitimate constant. */
11993 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11994 op0 = validize_mem (memloc);
11996 /* Otherwise, if this is a constant or the object is not in memory
11997 and need be, put it there. */
11998 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
12000 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
12001 emit_move_insn (memloc, op0);
12002 op0 = memloc;
12003 clear_mem_expr = true;
12006 if (offset)
12008 machine_mode address_mode;
12009 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
12010 EXPAND_SUM);
12012 gcc_assert (MEM_P (op0));
12014 address_mode = get_address_mode (op0);
12015 if (GET_MODE (offset_rtx) != address_mode)
12017 /* We cannot be sure that the RTL in offset_rtx is valid outside
12018 of a memory address context, so force it into a register
12019 before attempting to convert it to the desired mode. */
12020 offset_rtx = force_operand (offset_rtx, NULL_RTX);
12021 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
12024 /* See the comment in expand_assignment for the rationale. */
12025 if (mode1 != VOIDmode
12026 && maybe_ne (bitpos, 0)
12027 && maybe_gt (bitsize, 0)
12028 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12029 && multiple_p (bitpos, bitsize)
12030 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
12031 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
12033 op0 = adjust_address (op0, mode1, bytepos);
12034 bitpos = 0;
12037 op0 = offset_address (op0, offset_rtx,
12038 highest_pow2_factor (offset));
12041 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
12042 record its alignment as BIGGEST_ALIGNMENT. */
12043 if (MEM_P (op0)
12044 && known_eq (bitpos, 0)
12045 && offset != 0
12046 && is_aligning_offset (offset, tem))
12047 set_mem_align (op0, BIGGEST_ALIGNMENT);
12049 /* Don't forget about volatility even if this is a bitfield. */
12050 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
12052 if (op0 == orig_op0)
12053 op0 = copy_rtx (op0);
12055 MEM_VOLATILE_P (op0) = 1;
12058 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
12060 if (op0 == orig_op0)
12061 op0 = copy_rtx (op0);
12063 set_mem_align (op0, BITS_PER_UNIT);
12066 /* In cases where an aligned union has an unaligned object
12067 as a field, we might be extracting a BLKmode value from
12068 an integer-mode (e.g., SImode) object. Handle this case
12069 by doing the extract into an object as wide as the field
12070 (which we know to be the width of a basic mode), then
12071 storing into memory, and changing the mode to BLKmode. */
12072 if (mode1 == VOIDmode
12073 || REG_P (op0) || GET_CODE (op0) == SUBREG
12074 || (mode1 != BLKmode && ! direct_load[(int) mode1]
12075 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
12076 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
12077 && modifier != EXPAND_CONST_ADDRESS
12078 && modifier != EXPAND_INITIALIZER
12079 && modifier != EXPAND_MEMORY)
12080 /* If the bitfield is volatile and the bitsize
12081 is narrower than the access size of the bitfield,
12082 we need to extract bitfields from the access. */
12083 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
12084 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
12085 && mode1 != BLKmode
12086 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
12087 /* If the field isn't aligned enough to fetch as a memref,
12088 fetch it as a bit field. */
12089 || (mode1 != BLKmode
12090 && (((MEM_P (op0)
12091 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
12092 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
12093 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
12094 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
12095 && modifier != EXPAND_MEMORY
12096 && ((modifier == EXPAND_CONST_ADDRESS
12097 || modifier == EXPAND_INITIALIZER)
12098 ? STRICT_ALIGNMENT
12099 : targetm.slow_unaligned_access (mode1,
12100 MEM_ALIGN (op0))))
12101 || !multiple_p (bitpos, BITS_PER_UNIT)))
12102 /* If the type and the field are a constant size and the
12103 size of the type isn't the same size as the bitfield,
12104 we must use bitfield operations. */
12105 || (known_size_p (bitsize)
12106 && TYPE_SIZE (TREE_TYPE (exp))
12107 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
12108 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
12109 bitsize)))
12111 machine_mode ext_mode = mode;
12113 if (ext_mode == BLKmode
12114 && ! (target != 0 && MEM_P (op0)
12115 && MEM_P (target)
12116 && multiple_p (bitpos, BITS_PER_UNIT)))
12117 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
12119 if (ext_mode == BLKmode)
12121 if (target == 0)
12122 target = assign_temp (type, 1, 1);
12124 /* ??? Unlike the similar test a few lines below, this one is
12125 very likely obsolete. */
12126 if (known_eq (bitsize, 0))
12127 return target;
12129 /* In this case, BITPOS must start at a byte boundary and
12130 TARGET, if specified, must be a MEM. */
12131 gcc_assert (MEM_P (op0)
12132 && (!target || MEM_P (target)));
12134 bytepos = exact_div (bitpos, BITS_PER_UNIT);
12135 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
12136 emit_block_move (target,
12137 adjust_address (op0, VOIDmode, bytepos),
12138 gen_int_mode (bytesize, Pmode),
12139 (modifier == EXPAND_STACK_PARM
12140 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
12142 return target;
12145 /* If we have nothing to extract, the result will be 0 for targets
12146 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
12147 return 0 for the sake of consistency, as reading a zero-sized
12148 bitfield is valid in Ada and the value is fully specified. */
12149 if (known_eq (bitsize, 0))
12150 return const0_rtx;
12152 op0 = validize_mem (op0);
12154 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
12155 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12157 /* If the result has aggregate type and the extraction is done in
12158 an integral mode, then the field may be not aligned on a byte
12159 boundary; in this case, if it has reverse storage order, it
12160 needs to be extracted as a scalar field with reverse storage
12161 order and put back into memory order afterwards. */
12162 if (AGGREGATE_TYPE_P (type)
12163 && GET_MODE_CLASS (ext_mode) == MODE_INT)
12164 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
12166 gcc_checking_assert (known_ge (bitpos, 0));
12167 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
12168 (modifier == EXPAND_STACK_PARM
12169 ? NULL_RTX : target),
12170 ext_mode, ext_mode, reversep, alt_rtl);
12172 /* If the result has aggregate type and the mode of OP0 is an
12173 integral mode then, if BITSIZE is narrower than this mode
12174 and this is for big-endian data, we must put the field
12175 into the high-order bits. And we must also put it back
12176 into memory order if it has been previously reversed. */
12177 scalar_int_mode op0_mode;
12178 if (AGGREGATE_TYPE_P (type)
12179 && is_int_mode (GET_MODE (op0), &op0_mode))
12181 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
12183 gcc_checking_assert (known_le (bitsize, size));
12184 if (maybe_lt (bitsize, size)
12185 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
12186 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
12187 size - bitsize, op0, 1);
12189 if (reversep)
12190 op0 = flip_storage_order (op0_mode, op0);
12193 /* If the result type is BLKmode, store the data into a temporary
12194 of the appropriate type, but with the mode corresponding to the
12195 mode for the data we have (op0's mode). */
12196 if (mode == BLKmode)
12198 rtx new_rtx
12199 = assign_stack_temp_for_type (ext_mode,
12200 GET_MODE_BITSIZE (ext_mode),
12201 type);
12202 emit_move_insn (new_rtx, op0);
12203 op0 = copy_rtx (new_rtx);
12204 PUT_MODE (op0, BLKmode);
12207 return op0;
12210 /* If the result is BLKmode, use that to access the object
12211 now as well. */
12212 if (mode == BLKmode)
12213 mode1 = BLKmode;
12215 /* Get a reference to just this component. */
12216 bytepos = bits_to_bytes_round_down (bitpos);
12217 if (modifier == EXPAND_CONST_ADDRESS
12218 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
12219 op0 = adjust_address_nv (op0, mode1, bytepos);
12220 else
12221 op0 = adjust_address (op0, mode1, bytepos);
12223 if (op0 == orig_op0)
12224 op0 = copy_rtx (op0);
12226 /* Don't set memory attributes if the base expression is
12227 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
12228 we should just honor its original memory attributes. */
12229 if (!(TREE_CODE (tem) == SSA_NAME
12230 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
12231 set_mem_attributes (op0, exp, 0);
12233 if (REG_P (XEXP (op0, 0)))
12234 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12236 /* If op0 is a temporary because the original expressions was forced
12237 to memory, clear MEM_EXPR so that the original expression cannot
12238 be marked as addressable through MEM_EXPR of the temporary. */
12239 if (clear_mem_expr)
12240 set_mem_expr (op0, NULL_TREE);
12242 MEM_VOLATILE_P (op0) |= volatilep;
12244 if (reversep
12245 && modifier != EXPAND_MEMORY
12246 && modifier != EXPAND_WRITE)
12247 op0 = flip_storage_order (mode1, op0);
12249 op0 = EXTEND_BITINT (op0);
12251 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
12252 || modifier == EXPAND_CONST_ADDRESS
12253 || modifier == EXPAND_INITIALIZER)
12254 return op0;
12256 if (target == 0)
12257 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
12259 convert_move (target, op0, unsignedp);
12260 return target;
12263 case OBJ_TYPE_REF:
12264 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
12266 case CALL_EXPR:
12267 /* All valid uses of __builtin_va_arg_pack () are removed during
12268 inlining. */
12269 if (CALL_EXPR_VA_ARG_PACK (exp))
12270 error ("invalid use of %<__builtin_va_arg_pack ()%>");
12272 tree fndecl = get_callee_fndecl (exp), attr;
12274 if (fndecl
12275 /* Don't diagnose the error attribute in thunks, those are
12276 artificially created. */
12277 && !CALL_FROM_THUNK_P (exp)
12278 && (attr = lookup_attribute ("error",
12279 DECL_ATTRIBUTES (fndecl))) != NULL)
12281 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12282 error ("call to %qs declared with attribute error: %s",
12283 identifier_to_locale (ident),
12284 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12286 if (fndecl
12287 /* Don't diagnose the warning attribute in thunks, those are
12288 artificially created. */
12289 && !CALL_FROM_THUNK_P (exp)
12290 && (attr = lookup_attribute ("warning",
12291 DECL_ATTRIBUTES (fndecl))) != NULL)
12293 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12294 warning_at (EXPR_LOCATION (exp),
12295 OPT_Wattribute_warning,
12296 "call to %qs declared with attribute warning: %s",
12297 identifier_to_locale (ident),
12298 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12301 /* Check for a built-in function. */
12302 if (fndecl && fndecl_built_in_p (fndecl))
12304 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
12305 return expand_builtin (exp, target, subtarget, tmode, ignore);
12308 return expand_call (exp, target, ignore);
12310 case VIEW_CONVERT_EXPR:
12311 op0 = NULL_RTX;
12313 /* If we are converting to BLKmode, try to avoid an intermediate
12314 temporary by fetching an inner memory reference. */
12315 if (mode == BLKmode
12316 && poly_int_tree_p (TYPE_SIZE (type))
12317 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
12318 && handled_component_p (treeop0))
12320 machine_mode mode1;
12321 poly_int64 bitsize, bitpos, bytepos;
12322 tree offset;
12323 int reversep, volatilep = 0;
12324 tree tem
12325 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
12326 &unsignedp, &reversep, &volatilep);
12328 /* ??? We should work harder and deal with non-zero offsets. */
12329 if (!offset
12330 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12331 && !reversep
12332 && known_size_p (bitsize)
12333 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
12335 /* See the normal_inner_ref case for the rationale. */
12336 rtx orig_op0
12337 = expand_expr_real (tem,
12338 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
12339 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
12340 != INTEGER_CST)
12341 && modifier != EXPAND_STACK_PARM
12342 ? target : NULL_RTX),
12343 VOIDmode,
12344 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
12345 NULL, true);
12347 if (MEM_P (orig_op0))
12349 op0 = orig_op0;
12351 /* Get a reference to just this component. */
12352 if (modifier == EXPAND_CONST_ADDRESS
12353 || modifier == EXPAND_SUM
12354 || modifier == EXPAND_INITIALIZER)
12355 op0 = adjust_address_nv (op0, mode, bytepos);
12356 else
12357 op0 = adjust_address (op0, mode, bytepos);
12359 if (op0 == orig_op0)
12360 op0 = copy_rtx (op0);
12362 set_mem_attributes (op0, treeop0, 0);
12363 if (REG_P (XEXP (op0, 0)))
12364 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12366 MEM_VOLATILE_P (op0) |= volatilep;
12371 if (!op0)
12372 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
12373 NULL, inner_reference_p);
12375 /* If the input and output modes are both the same, we are done. */
12376 if (mode == GET_MODE (op0))
12378 /* If neither mode is BLKmode, and both modes are the same size
12379 then we can use gen_lowpart. */
12380 else if (mode != BLKmode
12381 && GET_MODE (op0) != BLKmode
12382 && known_eq (GET_MODE_PRECISION (mode),
12383 GET_MODE_PRECISION (GET_MODE (op0)))
12384 && !COMPLEX_MODE_P (GET_MODE (op0)))
12386 if (GET_CODE (op0) == SUBREG)
12387 op0 = force_reg (GET_MODE (op0), op0);
12388 temp = gen_lowpart_common (mode, op0);
12389 if (temp)
12390 op0 = temp;
12391 else
12393 if (!REG_P (op0) && !MEM_P (op0))
12394 op0 = force_reg (GET_MODE (op0), op0);
12395 op0 = gen_lowpart (mode, op0);
12398 /* If both types are integral, convert from one mode to the other. */
12399 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
12400 op0 = convert_modes (mode, GET_MODE (op0), op0,
12401 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
12402 /* If the output type is a bit-field type, do an extraction. */
12403 else if (reduce_bit_field)
12404 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12405 TYPE_UNSIGNED (type), NULL_RTX,
12406 mode, mode, false, NULL);
12407 /* As a last resort, spill op0 to memory, and reload it in a
12408 different mode. */
12409 else if (!MEM_P (op0))
12411 /* If the operand is not a MEM, force it into memory. Since we
12412 are going to be changing the mode of the MEM, don't call
12413 force_const_mem for constants because we don't allow pool
12414 constants to change mode. */
12415 tree inner_type = TREE_TYPE (treeop0);
12417 gcc_assert (!TREE_ADDRESSABLE (exp));
12419 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
12420 target
12421 = assign_stack_temp_for_type
12422 (TYPE_MODE (inner_type),
12423 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
12425 emit_move_insn (target, op0);
12426 op0 = target;
12429 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
12430 output type is such that the operand is known to be aligned, indicate
12431 that it is. Otherwise, we need only be concerned about alignment for
12432 non-BLKmode results. */
12433 if (MEM_P (op0))
12435 enum insn_code icode;
12437 if (modifier != EXPAND_WRITE
12438 && modifier != EXPAND_MEMORY
12439 && !inner_reference_p
12440 && mode != BLKmode
12441 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12443 /* If the target does have special handling for unaligned
12444 loads of mode then use them. */
12445 if ((icode = optab_handler (movmisalign_optab, mode))
12446 != CODE_FOR_nothing)
12448 rtx reg;
12450 op0 = adjust_address (op0, mode, 0);
12451 /* We've already validated the memory, and we're creating a
12452 new pseudo destination. The predicates really can't
12453 fail. */
12454 reg = gen_reg_rtx (mode);
12456 /* Nor can the insn generator. */
12457 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12458 emit_insn (insn);
12459 return reg;
12461 else if (STRICT_ALIGNMENT)
12463 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12464 poly_uint64 temp_size = mode_size;
12465 if (GET_MODE (op0) != BLKmode)
12466 temp_size = upper_bound (temp_size,
12467 GET_MODE_SIZE (GET_MODE (op0)));
12468 rtx new_rtx
12469 = assign_stack_temp_for_type (mode, temp_size, type);
12470 rtx new_with_op0_mode
12471 = adjust_address (new_rtx, GET_MODE (op0), 0);
12473 gcc_assert (!TREE_ADDRESSABLE (exp));
12475 if (GET_MODE (op0) == BLKmode)
12477 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12478 emit_block_move (new_with_op0_mode, op0, size_rtx,
12479 (modifier == EXPAND_STACK_PARM
12480 ? BLOCK_OP_CALL_PARM
12481 : BLOCK_OP_NORMAL));
12483 else
12484 emit_move_insn (new_with_op0_mode, op0);
12486 op0 = new_rtx;
12490 op0 = adjust_address (op0, mode, 0);
12493 return op0;
12495 case MODIFY_EXPR:
12497 tree lhs = treeop0;
12498 tree rhs = treeop1;
12499 gcc_assert (ignore);
12501 /* Check for |= or &= of a bitfield of size one into another bitfield
12502 of size 1. In this case, (unless we need the result of the
12503 assignment) we can do this more efficiently with a
12504 test followed by an assignment, if necessary.
12506 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12507 things change so we do, this code should be enhanced to
12508 support it. */
12509 if (TREE_CODE (lhs) == COMPONENT_REF
12510 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12511 || TREE_CODE (rhs) == BIT_AND_EXPR)
12512 && TREE_OPERAND (rhs, 0) == lhs
12513 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12514 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12515 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12517 rtx_code_label *label = gen_label_rtx ();
12518 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12519 profile_probability prob = profile_probability::uninitialized ();
12520 if (value)
12521 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12522 else
12523 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12524 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12525 false);
12526 do_pending_stack_adjust ();
12527 emit_label (label);
12528 return const0_rtx;
12531 expand_assignment (lhs, rhs, false);
12532 return const0_rtx;
12535 case ADDR_EXPR:
12536 return expand_expr_addr_expr (exp, target, tmode, modifier);
12538 case REALPART_EXPR:
12539 op0 = expand_normal (treeop0);
12540 return read_complex_part (op0, false);
12542 case IMAGPART_EXPR:
12543 op0 = expand_normal (treeop0);
12544 return read_complex_part (op0, true);
12546 case RETURN_EXPR:
12547 case LABEL_EXPR:
12548 case GOTO_EXPR:
12549 case SWITCH_EXPR:
12550 case ASM_EXPR:
12551 /* Expanded in cfgexpand.cc. */
12552 gcc_unreachable ();
12554 case TRY_CATCH_EXPR:
12555 case CATCH_EXPR:
12556 case EH_FILTER_EXPR:
12557 case TRY_FINALLY_EXPR:
12558 case EH_ELSE_EXPR:
12559 /* Lowered by tree-eh.cc. */
12560 gcc_unreachable ();
12562 case WITH_CLEANUP_EXPR:
12563 case CLEANUP_POINT_EXPR:
12564 case TARGET_EXPR:
12565 case CASE_LABEL_EXPR:
12566 case VA_ARG_EXPR:
12567 case BIND_EXPR:
12568 case INIT_EXPR:
12569 case CONJ_EXPR:
12570 case COMPOUND_EXPR:
12571 case PREINCREMENT_EXPR:
12572 case PREDECREMENT_EXPR:
12573 case POSTINCREMENT_EXPR:
12574 case POSTDECREMENT_EXPR:
12575 case LOOP_EXPR:
12576 case EXIT_EXPR:
12577 case COMPOUND_LITERAL_EXPR:
12578 /* Lowered by gimplify.cc. */
12579 gcc_unreachable ();
12581 case FDESC_EXPR:
12582 /* Function descriptors are not valid except for as
12583 initialization constants, and should not be expanded. */
12584 gcc_unreachable ();
12586 case WITH_SIZE_EXPR:
12587 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12588 have pulled out the size to use in whatever context it needed. */
12589 return expand_expr_real (treeop0, original_target, tmode,
12590 modifier, alt_rtl, inner_reference_p);
12592 default:
12593 return expand_expr_real_2 (&ops, target, tmode, modifier);
12596 #undef EXTEND_BITINT
12598 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12599 signedness of TYPE), possibly returning the result in TARGET.
12600 TYPE is known to be a partial integer type. */
12601 static rtx
12602 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12604 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12605 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12606 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12607 && (!target || GET_MODE (target) == mode));
12609 /* For constant values, reduce using wide_int_to_tree. */
12610 if (poly_int_rtx_p (exp))
12612 auto value = wi::to_poly_wide (exp, mode);
12613 tree t = wide_int_to_tree (type, value);
12614 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12616 else if (TYPE_UNSIGNED (type))
12618 rtx mask = immed_wide_int_const
12619 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12620 return expand_and (mode, exp, mask, target);
12622 else
12624 int count = GET_MODE_PRECISION (mode) - prec;
12625 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12626 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12630 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12631 when applied to the address of EXP produces an address known to be
12632 aligned more than BIGGEST_ALIGNMENT. */
12634 static bool
12635 is_aligning_offset (const_tree offset, const_tree exp)
12637 /* Strip off any conversions. */
12638 while (CONVERT_EXPR_P (offset))
12639 offset = TREE_OPERAND (offset, 0);
12641 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12642 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12643 if (TREE_CODE (offset) != BIT_AND_EXPR
12644 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12645 || compare_tree_int (TREE_OPERAND (offset, 1),
12646 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12647 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12648 return false;
12650 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12651 It must be NEGATE_EXPR. Then strip any more conversions. */
12652 offset = TREE_OPERAND (offset, 0);
12653 while (CONVERT_EXPR_P (offset))
12654 offset = TREE_OPERAND (offset, 0);
12656 if (TREE_CODE (offset) != NEGATE_EXPR)
12657 return false;
12659 offset = TREE_OPERAND (offset, 0);
12660 while (CONVERT_EXPR_P (offset))
12661 offset = TREE_OPERAND (offset, 0);
12663 /* This must now be the address of EXP. */
12664 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12667 /* Return a STRING_CST corresponding to ARG's constant initializer either
12668 if it's a string constant, or, when VALREP is set, any other constant,
12669 or null otherwise.
12670 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12671 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12672 to the size of the byte string. If nonnull, set *DECL to the constant
12673 declaration ARG refers to. */
12675 static tree
12676 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12677 bool valrep = false)
12679 tree dummy = NULL_TREE;
12680 if (!mem_size)
12681 mem_size = &dummy;
12683 /* Store the type of the original expression before conversions
12684 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12685 removed. */
12686 tree argtype = TREE_TYPE (arg);
12688 tree array;
12689 STRIP_NOPS (arg);
12691 /* Non-constant index into the character array in an ARRAY_REF
12692 expression or null. */
12693 tree varidx = NULL_TREE;
12695 poly_int64 base_off = 0;
12697 if (TREE_CODE (arg) == ADDR_EXPR)
12699 arg = TREE_OPERAND (arg, 0);
12700 tree ref = arg;
12701 if (TREE_CODE (arg) == ARRAY_REF)
12703 tree idx = TREE_OPERAND (arg, 1);
12704 if (TREE_CODE (idx) != INTEGER_CST)
12706 /* From a pointer (but not array) argument extract the variable
12707 index to prevent get_addr_base_and_unit_offset() from failing
12708 due to it. Use it later to compute the non-constant offset
12709 into the string and return it to the caller. */
12710 varidx = idx;
12711 ref = TREE_OPERAND (arg, 0);
12713 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12714 return NULL_TREE;
12716 if (!integer_zerop (array_ref_low_bound (arg)))
12717 return NULL_TREE;
12719 if (!integer_onep (array_ref_element_size (arg)))
12720 return NULL_TREE;
12723 array = get_addr_base_and_unit_offset (ref, &base_off);
12724 if (!array
12725 || (TREE_CODE (array) != VAR_DECL
12726 && TREE_CODE (array) != CONST_DECL
12727 && TREE_CODE (array) != STRING_CST))
12728 return NULL_TREE;
12730 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12732 tree arg0 = TREE_OPERAND (arg, 0);
12733 tree arg1 = TREE_OPERAND (arg, 1);
12735 tree offset;
12736 tree str = string_constant (arg0, &offset, mem_size, decl);
12737 if (!str)
12739 str = string_constant (arg1, &offset, mem_size, decl);
12740 arg1 = arg0;
12743 if (str)
12745 /* Avoid pointers to arrays (see bug 86622). */
12746 if (POINTER_TYPE_P (TREE_TYPE (arg))
12747 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12748 && !(decl && !*decl)
12749 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12750 && tree_fits_uhwi_p (*mem_size)
12751 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12752 return NULL_TREE;
12754 tree type = TREE_TYPE (offset);
12755 arg1 = fold_convert (type, arg1);
12756 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12757 return str;
12759 return NULL_TREE;
12761 else if (TREE_CODE (arg) == SSA_NAME)
12763 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12764 if (!is_gimple_assign (stmt))
12765 return NULL_TREE;
12767 tree rhs1 = gimple_assign_rhs1 (stmt);
12768 tree_code code = gimple_assign_rhs_code (stmt);
12769 if (code == ADDR_EXPR)
12770 return string_constant (rhs1, ptr_offset, mem_size, decl);
12771 else if (code != POINTER_PLUS_EXPR)
12772 return NULL_TREE;
12774 tree offset;
12775 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12777 /* Avoid pointers to arrays (see bug 86622). */
12778 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12779 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12780 && !(decl && !*decl)
12781 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12782 && tree_fits_uhwi_p (*mem_size)
12783 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12784 return NULL_TREE;
12786 tree rhs2 = gimple_assign_rhs2 (stmt);
12787 tree type = TREE_TYPE (offset);
12788 rhs2 = fold_convert (type, rhs2);
12789 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12790 return str;
12792 return NULL_TREE;
12794 else if (DECL_P (arg))
12795 array = arg;
12796 else
12797 return NULL_TREE;
12799 tree offset = wide_int_to_tree (sizetype, base_off);
12800 if (varidx)
12802 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12803 return NULL_TREE;
12805 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12806 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12807 if (TREE_CODE (chartype) != INTEGER_TYPE)
12808 return NULL;
12810 offset = fold_convert (sizetype, varidx);
12813 if (TREE_CODE (array) == STRING_CST)
12815 *ptr_offset = fold_convert (sizetype, offset);
12816 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12817 if (decl)
12818 *decl = NULL_TREE;
12819 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12820 >= TREE_STRING_LENGTH (array));
12821 return array;
12824 tree init = ctor_for_folding (array);
12825 if (!init || init == error_mark_node)
12826 return NULL_TREE;
12828 if (valrep)
12830 HOST_WIDE_INT cstoff;
12831 if (!base_off.is_constant (&cstoff))
12832 return NULL_TREE;
12834 /* Check that the host and target are sane. */
12835 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12836 return NULL_TREE;
12838 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12839 if (typesz <= 0 || (int) typesz != typesz)
12840 return NULL_TREE;
12842 HOST_WIDE_INT size = typesz;
12843 if (VAR_P (array)
12844 && DECL_SIZE_UNIT (array)
12845 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12847 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12848 gcc_checking_assert (size >= typesz);
12851 /* If value representation was requested convert the initializer
12852 for the whole array or object into a string of bytes forming
12853 its value representation and return it. */
12854 unsigned char *bytes = XNEWVEC (unsigned char, size);
12855 int r = native_encode_initializer (init, bytes, size);
12856 if (r < typesz)
12858 XDELETEVEC (bytes);
12859 return NULL_TREE;
12862 if (r < size)
12863 memset (bytes + r, '\0', size - r);
12865 const char *p = reinterpret_cast<const char *>(bytes);
12866 init = build_string_literal (size, p, char_type_node);
12867 init = TREE_OPERAND (init, 0);
12868 init = TREE_OPERAND (init, 0);
12869 XDELETE (bytes);
12871 *mem_size = size_int (TREE_STRING_LENGTH (init));
12872 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12874 if (decl)
12875 *decl = array;
12877 return init;
12880 if (TREE_CODE (init) == CONSTRUCTOR)
12882 /* Convert the 64-bit constant offset to a wider type to avoid
12883 overflow and use it to obtain the initializer for the subobject
12884 it points into. */
12885 offset_int wioff;
12886 if (!base_off.is_constant (&wioff))
12887 return NULL_TREE;
12889 wioff *= BITS_PER_UNIT;
12890 if (!wi::fits_uhwi_p (wioff))
12891 return NULL_TREE;
12893 base_off = wioff.to_uhwi ();
12894 unsigned HOST_WIDE_INT fieldoff = 0;
12895 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12896 &fieldoff);
12897 if (!init || init == error_mark_node)
12898 return NULL_TREE;
12900 HOST_WIDE_INT cstoff;
12901 if (!base_off.is_constant (&cstoff))
12902 return NULL_TREE;
12904 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12905 tree off = build_int_cst (sizetype, cstoff);
12906 if (varidx)
12907 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12908 else
12909 offset = off;
12912 *ptr_offset = offset;
12914 tree inittype = TREE_TYPE (init);
12916 if (TREE_CODE (init) == INTEGER_CST
12917 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12918 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12920 /* Check that the host and target are sane. */
12921 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12922 return NULL_TREE;
12924 /* For a reference to (address of) a single constant character,
12925 store the native representation of the character in CHARBUF.
12926 If the reference is to an element of an array or a member
12927 of a struct, only consider narrow characters until ctors
12928 for wide character arrays are transformed to STRING_CSTs
12929 like those for narrow arrays. */
12930 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12931 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12932 if (len > 0)
12934 /* Construct a string literal with elements of INITTYPE and
12935 the representation above. Then strip
12936 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12937 init = build_string_literal (len, (char *)charbuf, inittype);
12938 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12942 tree initsize = TYPE_SIZE_UNIT (inittype);
12944 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12946 /* Fold an empty/zero constructor for an implicitly initialized
12947 object or subobject into the empty string. */
12949 /* Determine the character type from that of the original
12950 expression. */
12951 tree chartype = argtype;
12952 if (POINTER_TYPE_P (chartype))
12953 chartype = TREE_TYPE (chartype);
12954 while (TREE_CODE (chartype) == ARRAY_TYPE)
12955 chartype = TREE_TYPE (chartype);
12957 if (INTEGRAL_TYPE_P (chartype)
12958 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12960 /* Convert a char array to an empty STRING_CST having an array
12961 of the expected type and size. */
12962 if (!initsize)
12963 initsize = integer_zero_node;
12965 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12966 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12967 return NULL_TREE;
12969 init = build_string_literal (size, NULL, chartype, size);
12970 init = TREE_OPERAND (init, 0);
12971 init = TREE_OPERAND (init, 0);
12973 *ptr_offset = integer_zero_node;
12977 if (decl)
12978 *decl = array;
12980 if (TREE_CODE (init) != STRING_CST)
12981 return NULL_TREE;
12983 *mem_size = initsize;
12985 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12987 return init;
12990 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12991 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12992 non-constant) offset in bytes within the string that ARG is accessing.
12993 If MEM_SIZE is non-zero the storage size of the memory is returned.
12994 If DECL is non-zero the constant declaration is returned if available. */
12996 tree
12997 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12999 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
13002 /* Similar to string_constant, return a STRING_CST corresponding
13003 to the value representation of the first argument if it's
13004 a constant. */
13006 tree
13007 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
13009 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
13012 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
13013 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
13014 for C2 > 0 to x & C3 == C2
13015 for C2 < 0 to x & C3 == (C2 & C3). */
13016 enum tree_code
13017 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13019 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13020 tree treeop0 = gimple_assign_rhs1 (stmt);
13021 tree treeop1 = gimple_assign_rhs2 (stmt);
13022 tree type = TREE_TYPE (*arg0);
13023 scalar_int_mode mode;
13024 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13025 return code;
13026 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13027 || TYPE_PRECISION (type) <= 1
13028 || TYPE_UNSIGNED (type)
13029 /* Signed x % c == 0 should have been optimized into unsigned modulo
13030 earlier. */
13031 || integer_zerop (*arg1)
13032 /* If c is known to be non-negative, modulo will be expanded as unsigned
13033 modulo. */
13034 || get_range_pos_neg (treeop0) == 1)
13035 return code;
13037 /* x % c == d where d < 0 && d <= -c should be always false. */
13038 if (tree_int_cst_sgn (*arg1) == -1
13039 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
13040 return code;
13042 int prec = TYPE_PRECISION (type);
13043 wide_int w = wi::to_wide (treeop1) - 1;
13044 w |= wi::shifted_mask (0, prec - 1, true, prec);
13045 tree c3 = wide_int_to_tree (type, w);
13046 tree c4 = *arg1;
13047 if (tree_int_cst_sgn (*arg1) == -1)
13048 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
13050 rtx op0 = expand_normal (treeop0);
13051 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13053 bool speed_p = optimize_insn_for_speed_p ();
13055 do_pending_stack_adjust ();
13057 location_t loc = gimple_location (stmt);
13058 struct separate_ops ops;
13059 ops.code = TRUNC_MOD_EXPR;
13060 ops.location = loc;
13061 ops.type = TREE_TYPE (treeop0);
13062 ops.op0 = treeop0;
13063 ops.op1 = treeop1;
13064 ops.op2 = NULL_TREE;
13065 start_sequence ();
13066 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13067 EXPAND_NORMAL);
13068 rtx_insn *moinsns = get_insns ();
13069 end_sequence ();
13071 unsigned mocost = seq_cost (moinsns, speed_p);
13072 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13073 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13075 ops.code = BIT_AND_EXPR;
13076 ops.location = loc;
13077 ops.type = TREE_TYPE (treeop0);
13078 ops.op0 = treeop0;
13079 ops.op1 = c3;
13080 ops.op2 = NULL_TREE;
13081 start_sequence ();
13082 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13083 EXPAND_NORMAL);
13084 rtx_insn *muinsns = get_insns ();
13085 end_sequence ();
13087 unsigned mucost = seq_cost (muinsns, speed_p);
13088 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
13089 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
13091 if (mocost <= mucost)
13093 emit_insn (moinsns);
13094 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13095 return code;
13098 emit_insn (muinsns);
13099 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
13100 *arg1 = c4;
13101 return code;
13104 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
13105 If C1 is odd to:
13106 (X - C2) * C3 <= C4 (or >), where
13107 C3 is modular multiplicative inverse of C1 and 1<<prec and
13108 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
13109 if C2 > ((1<<prec) - 1) % C1).
13110 If C1 is even, S = ctz (C1) and C2 is 0, use
13111 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
13112 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
13114 For signed (X % C1) == 0 if C1 is odd to (all operations in it
13115 unsigned):
13116 (X * C3) + C4 <= 2 * C4, where
13117 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
13118 C4 is ((1<<(prec - 1) - 1) / C1).
13119 If C1 is even, S = ctz(C1), use
13120 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
13121 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
13122 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
13124 See the Hacker's Delight book, section 10-17. */
13125 enum tree_code
13126 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13128 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
13129 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
13131 if (optimize < 2)
13132 return code;
13134 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13135 if (stmt == NULL)
13136 return code;
13138 tree treeop0 = gimple_assign_rhs1 (stmt);
13139 tree treeop1 = gimple_assign_rhs2 (stmt);
13140 if (TREE_CODE (treeop0) != SSA_NAME
13141 || TREE_CODE (treeop1) != INTEGER_CST
13142 /* Don't optimize the undefined behavior case x % 0;
13143 x % 1 should have been optimized into zero, punt if
13144 it makes it here for whatever reason;
13145 x % -c should have been optimized into x % c. */
13146 || compare_tree_int (treeop1, 2) <= 0
13147 /* Likewise x % c == d where d >= c should be always false. */
13148 || tree_int_cst_le (treeop1, *arg1))
13149 return code;
13151 /* Unsigned x % pow2 is handled right already, for signed
13152 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
13153 if (integer_pow2p (treeop1))
13154 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
13156 tree type = TREE_TYPE (*arg0);
13157 scalar_int_mode mode;
13158 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13159 return code;
13160 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13161 || TYPE_PRECISION (type) <= 1)
13162 return code;
13164 signop sgn = UNSIGNED;
13165 /* If both operands are known to have the sign bit clear, handle
13166 even the signed modulo case as unsigned. treeop1 is always
13167 positive >= 2, checked above. */
13168 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
13169 sgn = SIGNED;
13171 if (!TYPE_UNSIGNED (type))
13173 if (tree_int_cst_sgn (*arg1) == -1)
13174 return code;
13175 type = unsigned_type_for (type);
13176 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
13177 return code;
13180 int prec = TYPE_PRECISION (type);
13181 wide_int w = wi::to_wide (treeop1);
13182 int shift = wi::ctz (w);
13183 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
13184 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
13185 If C1 is odd, we can handle all cases by subtracting
13186 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
13187 e.g. by testing for overflow on the subtraction, punt on that for now
13188 though. */
13189 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
13191 if (sgn == SIGNED)
13192 return code;
13193 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
13194 if (wi::gtu_p (wi::to_wide (*arg1), x))
13195 return code;
13198 imm_use_iterator imm_iter;
13199 use_operand_p use_p;
13200 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
13202 gimple *use_stmt = USE_STMT (use_p);
13203 /* Punt if treeop0 is used in the same bb in a division
13204 or another modulo with the same divisor. We should expect
13205 the division and modulo combined together. */
13206 if (use_stmt == stmt
13207 || gimple_bb (use_stmt) != gimple_bb (stmt))
13208 continue;
13209 if (!is_gimple_assign (use_stmt)
13210 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
13211 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
13212 continue;
13213 if (gimple_assign_rhs1 (use_stmt) != treeop0
13214 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
13215 continue;
13216 return code;
13219 w = wi::lrshift (w, shift);
13220 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
13221 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
13222 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
13223 tree c3 = wide_int_to_tree (type, m);
13224 tree c5 = NULL_TREE;
13225 wide_int d, e;
13226 if (sgn == UNSIGNED)
13228 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
13229 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
13230 otherwise use < or subtract one from C4. E.g. for
13231 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
13232 x % 3U == 1 already needs to be
13233 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
13234 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
13235 d -= 1;
13236 if (shift)
13237 d = wi::lrshift (d, shift);
13239 else
13241 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
13242 if (!shift)
13243 d = wi::lshift (e, 1);
13244 else
13246 e = wi::bit_and (e, wi::mask (shift, true, prec));
13247 d = wi::lrshift (e, shift - 1);
13249 c5 = wide_int_to_tree (type, e);
13251 tree c4 = wide_int_to_tree (type, d);
13253 rtx op0 = expand_normal (treeop0);
13254 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13256 bool speed_p = optimize_insn_for_speed_p ();
13258 do_pending_stack_adjust ();
13260 location_t loc = gimple_location (stmt);
13261 struct separate_ops ops;
13262 ops.code = TRUNC_MOD_EXPR;
13263 ops.location = loc;
13264 ops.type = TREE_TYPE (treeop0);
13265 ops.op0 = treeop0;
13266 ops.op1 = treeop1;
13267 ops.op2 = NULL_TREE;
13268 start_sequence ();
13269 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13270 EXPAND_NORMAL);
13271 rtx_insn *moinsns = get_insns ();
13272 end_sequence ();
13274 unsigned mocost = seq_cost (moinsns, speed_p);
13275 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13276 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13278 tree t = fold_convert_loc (loc, type, treeop0);
13279 if (!integer_zerop (*arg1))
13280 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
13281 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
13282 if (sgn == SIGNED)
13283 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
13284 if (shift)
13286 tree s = build_int_cst (NULL_TREE, shift);
13287 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
13290 start_sequence ();
13291 rtx mur = expand_normal (t);
13292 rtx_insn *muinsns = get_insns ();
13293 end_sequence ();
13295 unsigned mucost = seq_cost (muinsns, speed_p);
13296 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
13297 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
13299 if (mocost <= mucost)
13301 emit_insn (moinsns);
13302 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13303 return code;
13306 emit_insn (muinsns);
13307 *arg0 = make_tree (type, mur);
13308 *arg1 = c4;
13309 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
13312 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
13314 void
13315 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
13317 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
13318 || code == LT_EXPR || code == LE_EXPR);
13319 gcc_checking_assert (integer_zerop (*arg1));
13321 if (!optimize)
13322 return;
13324 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
13325 if (stmt == NULL)
13326 return;
13328 tree treeop0 = gimple_assign_rhs1 (stmt);
13329 tree treeop1 = gimple_assign_rhs2 (stmt);
13330 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
13331 return;
13333 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
13334 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
13335 "assuming signed overflow does not occur when "
13336 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
13337 op_symbol_code (code), op_symbol_code (code));
13339 *arg0 = treeop0;
13340 *arg1 = treeop1;
13344 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
13345 a single bit equality/inequality test, returns where the result is located. */
13347 static rtx
13348 expand_single_bit_test (location_t loc, enum tree_code code,
13349 tree inner, int bitnum,
13350 tree result_type, rtx target,
13351 machine_mode mode)
13353 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
13355 tree type = TREE_TYPE (inner);
13356 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
13357 int ops_unsigned;
13358 tree signed_type, unsigned_type, intermediate_type;
13359 gimple *inner_def;
13361 /* First, see if we can fold the single bit test into a sign-bit
13362 test. */
13363 if (bitnum == TYPE_PRECISION (type) - 1
13364 && type_has_mode_precision_p (type))
13366 tree stype = signed_type_for (type);
13367 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13368 result_type,
13369 fold_convert_loc (loc, stype, inner),
13370 build_int_cst (stype, 0));
13371 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
13374 /* Otherwise we have (A & C) != 0 where C is a single bit,
13375 convert that into ((A >> C2) & 1). Where C2 = log2(C).
13376 Similarly for (A & C) == 0. */
13378 /* If INNER is a right shift of a constant and it plus BITNUM does
13379 not overflow, adjust BITNUM and INNER. */
13380 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
13381 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
13382 && bitnum < TYPE_PRECISION (type)
13383 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
13384 TYPE_PRECISION (type) - bitnum))
13386 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
13387 inner = gimple_assign_rhs1 (inner_def);
13390 /* If we are going to be able to omit the AND below, we must do our
13391 operations as unsigned. If we must use the AND, we have a choice.
13392 Normally unsigned is faster, but for some machines signed is. */
13393 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
13394 && !flag_syntax_only) ? 0 : 1;
13396 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
13397 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
13398 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
13399 inner = fold_convert_loc (loc, intermediate_type, inner);
13401 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
13403 if (CONST_SCALAR_INT_P (inner0))
13405 wide_int t = rtx_mode_t (inner0, operand_mode);
13406 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
13407 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
13409 int bitpos = bitnum;
13411 if (BYTES_BIG_ENDIAN)
13412 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
13414 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
13415 operand_mode, mode, 0, NULL);
13417 if (code == EQ_EXPR)
13418 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
13419 NULL_RTX, 1, OPTAB_LIB_WIDEN);
13420 if (GET_MODE (inner0) != mode)
13422 rtx t = gen_reg_rtx (mode);
13423 convert_move (t, inner0, 0);
13424 return t;
13426 return inner0;
13429 /* Generate code to calculate OPS, and exploded expression
13430 using a store-flag instruction and return an rtx for the result.
13431 OPS reflects a comparison.
13433 If TARGET is nonzero, store the result there if convenient.
13435 Return zero if there is no suitable set-flag instruction
13436 available on this machine.
13438 Once expand_expr has been called on the arguments of the comparison,
13439 we are committed to doing the store flag, since it is not safe to
13440 re-evaluate the expression. We emit the store-flag insn by calling
13441 emit_store_flag, but only expand the arguments if we have a reason
13442 to believe that emit_store_flag will be successful. If we think that
13443 it will, but it isn't, we have to simulate the store-flag with a
13444 set/jump/set sequence. */
13446 static rtx
13447 do_store_flag (sepops ops, rtx target, machine_mode mode)
13449 enum rtx_code code;
13450 tree arg0, arg1, type;
13451 machine_mode operand_mode;
13452 int unsignedp;
13453 rtx op0, op1;
13454 rtx subtarget = target;
13455 location_t loc = ops->location;
13457 arg0 = ops->op0;
13458 arg1 = ops->op1;
13460 /* Don't crash if the comparison was erroneous. */
13461 if (arg0 == error_mark_node || arg1 == error_mark_node)
13462 return const0_rtx;
13464 type = TREE_TYPE (arg0);
13465 operand_mode = TYPE_MODE (type);
13466 unsignedp = TYPE_UNSIGNED (type);
13468 /* We won't bother with BLKmode store-flag operations because it would mean
13469 passing a lot of information to emit_store_flag. */
13470 if (operand_mode == BLKmode)
13471 return 0;
13473 /* We won't bother with store-flag operations involving function pointers
13474 when function pointers must be canonicalized before comparisons. */
13475 if (targetm.have_canonicalize_funcptr_for_compare ()
13476 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13477 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13478 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13479 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13480 return 0;
13482 STRIP_NOPS (arg0);
13483 STRIP_NOPS (arg1);
13485 /* For vector typed comparisons emit code to generate the desired
13486 all-ones or all-zeros mask. */
13487 if (VECTOR_TYPE_P (ops->type))
13489 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13490 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13491 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13492 return expand_vec_cmp_expr (ops->type, ifexp, target);
13493 else
13494 gcc_unreachable ();
13497 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13498 into (x - C2) * C3 < C4. */
13499 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13500 && TREE_CODE (arg0) == SSA_NAME
13501 && TREE_CODE (arg1) == INTEGER_CST)
13503 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13504 &arg0, &arg1);
13505 if (new_code != ops->code)
13507 struct separate_ops nops = *ops;
13508 nops.code = ops->code = new_code;
13509 nops.op0 = arg0;
13510 nops.op1 = arg1;
13511 nops.type = TREE_TYPE (arg0);
13512 return do_store_flag (&nops, target, mode);
13516 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13517 if (!unsignedp
13518 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13519 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13520 && integer_zerop (arg1)
13521 && TREE_CODE (arg0) == SSA_NAME)
13522 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13524 /* Get the rtx comparison code to use. We know that EXP is a comparison
13525 operation of some type. Some comparisons against 1 and -1 can be
13526 converted to comparisons with zero. Do so here so that the tests
13527 below will be aware that we have a comparison with zero. These
13528 tests will not catch constants in the first operand, but constants
13529 are rarely passed as the first operand. */
13531 switch (ops->code)
13533 case EQ_EXPR:
13534 code = EQ;
13535 break;
13536 case NE_EXPR:
13537 code = NE;
13538 break;
13539 case LT_EXPR:
13540 if (integer_onep (arg1))
13541 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13542 else
13543 code = unsignedp ? LTU : LT;
13544 break;
13545 case LE_EXPR:
13546 if (! unsignedp && integer_all_onesp (arg1))
13547 arg1 = integer_zero_node, code = LT;
13548 else
13549 code = unsignedp ? LEU : LE;
13550 break;
13551 case GT_EXPR:
13552 if (! unsignedp && integer_all_onesp (arg1))
13553 arg1 = integer_zero_node, code = GE;
13554 else
13555 code = unsignedp ? GTU : GT;
13556 break;
13557 case GE_EXPR:
13558 if (integer_onep (arg1))
13559 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13560 else
13561 code = unsignedp ? GEU : GE;
13562 break;
13564 case UNORDERED_EXPR:
13565 code = UNORDERED;
13566 break;
13567 case ORDERED_EXPR:
13568 code = ORDERED;
13569 break;
13570 case UNLT_EXPR:
13571 code = UNLT;
13572 break;
13573 case UNLE_EXPR:
13574 code = UNLE;
13575 break;
13576 case UNGT_EXPR:
13577 code = UNGT;
13578 break;
13579 case UNGE_EXPR:
13580 code = UNGE;
13581 break;
13582 case UNEQ_EXPR:
13583 code = UNEQ;
13584 break;
13585 case LTGT_EXPR:
13586 code = LTGT;
13587 break;
13589 default:
13590 gcc_unreachable ();
13593 /* Put a constant second. */
13594 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13595 || TREE_CODE (arg0) == FIXED_CST)
13597 std::swap (arg0, arg1);
13598 code = swap_condition (code);
13601 /* If this is an equality or inequality test of a single bit, we can
13602 do this by shifting the bit being tested to the low-order bit and
13603 masking the result with the constant 1. If the condition was EQ,
13604 we xor it with 1. This does not require an scc insn and is faster
13605 than an scc insn even if we have it. */
13607 if ((code == NE || code == EQ)
13608 && (integer_zerop (arg1)
13609 || integer_pow2p (arg1))
13610 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13612 tree narg0 = arg0;
13613 wide_int nz = tree_nonzero_bits (narg0);
13614 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR);
13615 /* If the defining statement was (x & POW2), then use that instead of
13616 the non-zero bits. */
13617 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13619 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13620 narg0 = gimple_assign_rhs1 (srcstmt);
13623 if (wi::popcount (nz) == 1
13624 && (integer_zerop (arg1)
13625 || wi::to_wide (arg1) == nz))
13627 int bitnum = wi::exact_log2 (nz);
13628 enum tree_code tcode = EQ_EXPR;
13629 if ((code == NE) ^ !integer_zerop (arg1))
13630 tcode = NE_EXPR;
13632 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13633 return expand_single_bit_test (loc, tcode,
13634 narg0,
13635 bitnum, type, target, mode);
13640 if (! get_subtarget (target)
13641 || GET_MODE (subtarget) != operand_mode)
13642 subtarget = 0;
13644 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13646 if (target == 0)
13647 target = gen_reg_rtx (mode);
13649 /* Try a cstore if possible. */
13650 return emit_store_flag_force (target, code, op0, op1,
13651 operand_mode, unsignedp,
13652 (TYPE_PRECISION (ops->type) == 1
13653 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13656 /* Attempt to generate a casesi instruction. Returns true if successful,
13657 false otherwise (i.e. if there is no casesi instruction).
13659 DEFAULT_PROBABILITY is the probability of jumping to the default
13660 label. */
13661 bool
13662 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13663 rtx table_label, rtx default_label, rtx fallback_label,
13664 profile_probability default_probability)
13666 class expand_operand ops[5];
13667 scalar_int_mode index_mode = SImode;
13668 rtx op1, op2, index;
13670 if (! targetm.have_casesi ())
13671 return false;
13673 /* The index must be some form of integer. Convert it to SImode. */
13674 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13675 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13677 rtx rangertx = expand_normal (range);
13679 /* We must handle the endpoints in the original mode. */
13680 index_expr = build2 (MINUS_EXPR, index_type,
13681 index_expr, minval);
13682 minval = integer_zero_node;
13683 index = expand_normal (index_expr);
13684 if (default_label)
13685 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13686 omode, 1, default_label,
13687 default_probability);
13688 /* Now we can safely truncate. */
13689 index = convert_to_mode (index_mode, index, 0);
13691 else
13693 if (omode != index_mode)
13695 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13696 index_expr = fold_convert (index_type, index_expr);
13699 index = expand_normal (index_expr);
13702 do_pending_stack_adjust ();
13704 op1 = expand_normal (minval);
13705 op2 = expand_normal (range);
13707 create_input_operand (&ops[0], index, index_mode);
13708 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13709 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13710 create_fixed_operand (&ops[3], table_label);
13711 create_fixed_operand (&ops[4], (default_label
13712 ? default_label
13713 : fallback_label));
13714 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13715 return true;
13718 /* Attempt to generate a tablejump instruction; same concept. */
13719 /* Subroutine of the next function.
13721 INDEX is the value being switched on, with the lowest value
13722 in the table already subtracted.
13723 MODE is its expected mode (needed if INDEX is constant).
13724 RANGE is the length of the jump table.
13725 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13727 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13728 index value is out of range.
13729 DEFAULT_PROBABILITY is the probability of jumping to
13730 the default label. */
13732 static void
13733 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13734 rtx default_label, profile_probability default_probability)
13736 rtx temp, vector;
13738 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13739 cfun->cfg->max_jumptable_ents = INTVAL (range);
13741 /* Do an unsigned comparison (in the proper mode) between the index
13742 expression and the value which represents the length of the range.
13743 Since we just finished subtracting the lower bound of the range
13744 from the index expression, this comparison allows us to simultaneously
13745 check that the original index expression value is both greater than
13746 or equal to the minimum value of the range and less than or equal to
13747 the maximum value of the range. */
13749 if (default_label)
13750 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13751 default_label, default_probability);
13753 /* If index is in range, it must fit in Pmode.
13754 Convert to Pmode so we can index with it. */
13755 if (mode != Pmode)
13757 unsigned int width;
13759 /* We know the value of INDEX is between 0 and RANGE. If we have a
13760 sign-extended subreg, and RANGE does not have the sign bit set, then
13761 we have a value that is valid for both sign and zero extension. In
13762 this case, we get better code if we sign extend. */
13763 if (GET_CODE (index) == SUBREG
13764 && SUBREG_PROMOTED_VAR_P (index)
13765 && SUBREG_PROMOTED_SIGNED_P (index)
13766 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13767 <= HOST_BITS_PER_WIDE_INT)
13768 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13769 index = convert_to_mode (Pmode, index, 0);
13770 else
13771 index = convert_to_mode (Pmode, index, 1);
13774 /* Don't let a MEM slip through, because then INDEX that comes
13775 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13776 and break_out_memory_refs will go to work on it and mess it up. */
13777 #ifdef PIC_CASE_VECTOR_ADDRESS
13778 if (flag_pic && !REG_P (index))
13779 index = copy_to_mode_reg (Pmode, index);
13780 #endif
13782 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13783 GET_MODE_SIZE, because this indicates how large insns are. The other
13784 uses should all be Pmode, because they are addresses. This code
13785 could fail if addresses and insns are not the same size. */
13786 index = simplify_gen_binary (MULT, Pmode, index,
13787 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13788 Pmode));
13789 index = simplify_gen_binary (PLUS, Pmode, index,
13790 gen_rtx_LABEL_REF (Pmode, table_label));
13792 #ifdef PIC_CASE_VECTOR_ADDRESS
13793 if (flag_pic)
13794 index = PIC_CASE_VECTOR_ADDRESS (index);
13795 else
13796 #endif
13797 index = memory_address (CASE_VECTOR_MODE, index);
13798 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13799 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13800 convert_move (temp, vector, 0);
13802 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13804 /* If we are generating PIC code or if the table is PC-relative, the
13805 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13806 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13807 emit_barrier ();
13810 bool
13811 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13812 rtx table_label, rtx default_label,
13813 profile_probability default_probability)
13815 rtx index;
13817 if (! targetm.have_tablejump ())
13818 return false;
13820 index_expr = fold_build2 (MINUS_EXPR, index_type,
13821 fold_convert (index_type, index_expr),
13822 fold_convert (index_type, minval));
13823 index = expand_normal (index_expr);
13824 do_pending_stack_adjust ();
13826 do_tablejump (index, TYPE_MODE (index_type),
13827 convert_modes (TYPE_MODE (index_type),
13828 TYPE_MODE (TREE_TYPE (range)),
13829 expand_normal (range),
13830 TYPE_UNSIGNED (TREE_TYPE (range))),
13831 table_label, default_label, default_probability);
13832 return true;
13835 /* Return a CONST_VECTOR rtx representing vector mask for
13836 a VECTOR_CST of booleans. */
13837 static rtx
13838 const_vector_mask_from_tree (tree exp)
13840 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13841 machine_mode inner = GET_MODE_INNER (mode);
13843 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13844 VECTOR_CST_NELTS_PER_PATTERN (exp));
13845 unsigned int count = builder.encoded_nelts ();
13846 for (unsigned int i = 0; i < count; ++i)
13848 tree elt = VECTOR_CST_ELT (exp, i);
13849 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13850 if (integer_zerop (elt))
13851 builder.quick_push (CONST0_RTX (inner));
13852 else if (integer_onep (elt)
13853 || integer_minus_onep (elt))
13854 builder.quick_push (CONSTM1_RTX (inner));
13855 else
13856 gcc_unreachable ();
13858 return builder.build ();
13861 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13862 static rtx
13863 const_vector_from_tree (tree exp)
13865 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13867 if (initializer_zerop (exp))
13868 return CONST0_RTX (mode);
13870 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13871 return const_vector_mask_from_tree (exp);
13873 machine_mode inner = GET_MODE_INNER (mode);
13875 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13876 VECTOR_CST_NELTS_PER_PATTERN (exp));
13877 unsigned int count = builder.encoded_nelts ();
13878 for (unsigned int i = 0; i < count; ++i)
13880 tree elt = VECTOR_CST_ELT (exp, i);
13881 if (TREE_CODE (elt) == REAL_CST)
13882 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13883 inner));
13884 else if (TREE_CODE (elt) == FIXED_CST)
13885 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13886 inner));
13887 else
13888 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13889 inner));
13891 return builder.build ();
13894 /* Build a decl for a personality function given a language prefix. */
13896 tree
13897 build_personality_function (const char *lang)
13899 const char *unwind_and_version;
13900 tree decl, type;
13901 char *name;
13903 switch (targetm_common.except_unwind_info (&global_options))
13905 case UI_NONE:
13906 return NULL;
13907 case UI_SJLJ:
13908 unwind_and_version = "_sj0";
13909 break;
13910 case UI_DWARF2:
13911 case UI_TARGET:
13912 unwind_and_version = "_v0";
13913 break;
13914 case UI_SEH:
13915 unwind_and_version = "_seh0";
13916 break;
13917 default:
13918 gcc_unreachable ();
13921 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13923 type = build_function_type_list (unsigned_type_node,
13924 integer_type_node, integer_type_node,
13925 long_long_unsigned_type_node,
13926 ptr_type_node, ptr_type_node, NULL_TREE);
13927 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13928 get_identifier (name), type);
13929 DECL_ARTIFICIAL (decl) = 1;
13930 DECL_EXTERNAL (decl) = 1;
13931 TREE_PUBLIC (decl) = 1;
13933 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13934 are the flags assigned by targetm.encode_section_info. */
13935 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13937 return decl;
13940 /* Extracts the personality function of DECL and returns the corresponding
13941 libfunc. */
13944 get_personality_function (tree decl)
13946 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13947 enum eh_personality_kind pk;
13949 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13950 if (pk == eh_personality_none)
13951 return NULL;
13953 if (!personality
13954 && pk == eh_personality_any)
13955 personality = lang_hooks.eh_personality ();
13957 if (pk == eh_personality_lang)
13958 gcc_assert (personality != NULL_TREE);
13960 return XEXP (DECL_RTL (personality), 0);
13963 /* Returns a tree for the size of EXP in bytes. */
13965 static tree
13966 tree_expr_size (const_tree exp)
13968 if (DECL_P (exp)
13969 && DECL_SIZE_UNIT (exp) != 0)
13970 return DECL_SIZE_UNIT (exp);
13971 else
13972 return size_in_bytes (TREE_TYPE (exp));
13975 /* Return an rtx for the size in bytes of the value of EXP. */
13978 expr_size (tree exp)
13980 tree size;
13982 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13983 size = TREE_OPERAND (exp, 1);
13984 else
13986 size = tree_expr_size (exp);
13987 gcc_assert (size);
13988 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13991 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13994 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13995 if the size can vary or is larger than an integer. */
13997 HOST_WIDE_INT
13998 int_expr_size (const_tree exp)
14000 tree size;
14002 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
14003 size = TREE_OPERAND (exp, 1);
14004 else
14006 size = tree_expr_size (exp);
14007 gcc_assert (size);
14010 if (size == 0 || !tree_fits_shwi_p (size))
14011 return -1;
14013 return tree_to_shwi (size);