c++: ttp TEMPLATE_DECL equivalence [PR112737]
[official-gcc.git] / gcc / expr.cc
blobfc5e998e3292063d093af4b54fb0e957ea433431
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2024 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 opt_scalar_int_mode int_move_mode
2453 = int_mode_for_size (incr * BITS_PER_UNIT, 1);
2454 if (!int_move_mode.exists (&move_mode)
2455 || GET_MODE_BITSIZE (int_move_mode.require ()) != incr * BITS_PER_UNIT)
2457 move_mode = BLKmode;
2458 gcc_checking_assert (can_move_by_pieces (incr, align));
2461 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2462 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2463 do_pending_stack_adjust ();
2465 emit_jump (cmp_label);
2466 emit_label (top_label);
2468 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2469 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2471 if (x_addr_mode != y_addr_mode)
2472 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2473 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2475 x = change_address (x, move_mode, x_addr);
2476 y = change_address (y, move_mode, y_addr);
2478 if (move_mode == BLKmode)
2480 bool done;
2481 emit_block_move_hints (x, y, iter_incr, BLOCK_OP_NO_LIBCALL,
2482 align, incr, incr, incr, incr,
2483 false, &done, false);
2484 gcc_checking_assert (done);
2486 else
2487 emit_move_insn (x, y);
2489 if (downwards)
2490 emit_label (cmp_label);
2492 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2493 true, OPTAB_LIB_WIDEN);
2494 if (tmp != iter)
2495 emit_move_insn (iter, tmp);
2497 if (!downwards)
2498 emit_label (cmp_label);
2500 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2501 true, top_label,
2502 profile_probability::guessed_always ()
2503 .apply_scale (9, 10));
2506 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2507 TAILCALL is true if this is a tail call. */
2510 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2511 rtx size, bool tailcall)
2513 rtx dst_addr, src_addr;
2514 tree call_expr, dst_tree, src_tree, size_tree;
2515 machine_mode size_mode;
2517 /* Since dst and src are passed to a libcall, mark the corresponding
2518 tree EXPR as addressable. */
2519 tree dst_expr = MEM_EXPR (dst);
2520 tree src_expr = MEM_EXPR (src);
2521 if (dst_expr)
2522 mark_addressable (dst_expr);
2523 if (src_expr)
2524 mark_addressable (src_expr);
2526 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2527 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2528 dst_tree = make_tree (ptr_type_node, dst_addr);
2530 src_addr = copy_addr_to_reg (XEXP (src, 0));
2531 src_addr = convert_memory_address (ptr_mode, src_addr);
2532 src_tree = make_tree (ptr_type_node, src_addr);
2534 size_mode = TYPE_MODE (sizetype);
2535 size = convert_to_mode (size_mode, size, 1);
2536 size = copy_to_mode_reg (size_mode, size);
2537 size_tree = make_tree (sizetype, size);
2539 /* It is incorrect to use the libcall calling conventions for calls to
2540 memcpy/memmove/memcmp because they can be provided by the user. */
2541 tree fn = builtin_decl_implicit (fncode);
2542 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2543 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2545 return expand_call (call_expr, NULL_RTX, false);
2548 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2549 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2550 otherwise return null. */
2553 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2554 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2555 HOST_WIDE_INT align)
2557 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2559 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2560 target = NULL_RTX;
2562 class expand_operand ops[5];
2563 create_output_operand (&ops[0], target, insn_mode);
2564 create_fixed_operand (&ops[1], arg1_rtx);
2565 create_fixed_operand (&ops[2], arg2_rtx);
2566 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2567 TYPE_UNSIGNED (arg3_type));
2568 create_integer_operand (&ops[4], align);
2569 if (maybe_expand_insn (icode, 5, ops))
2570 return ops[0].value;
2571 return NULL_RTX;
2574 /* Expand a block compare between X and Y with length LEN using the
2575 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2576 of the expression that was used to calculate the length. ALIGN
2577 gives the known minimum common alignment. */
2579 static rtx
2580 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2581 unsigned align)
2583 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2584 implementing memcmp because it will stop if it encounters two
2585 zero bytes. */
2586 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2588 if (icode == CODE_FOR_nothing)
2589 return NULL_RTX;
2591 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2594 /* Emit code to compare a block Y to a block X. This may be done with
2595 string-compare instructions, with multiple scalar instructions,
2596 or with a library call.
2598 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2599 they are. LEN_TYPE is the type of the expression that was used to
2600 calculate it, and CTZ_LEN is the known trailing-zeros count of LEN,
2601 so LEN must be a multiple of 1<<CTZ_LEN even if it's not constant.
2603 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2604 value of a normal memcmp call, instead we can just compare for equality.
2605 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2606 returning NULL_RTX.
2608 Optionally, the caller can pass a constfn and associated data in Y_CFN
2609 and Y_CFN_DATA. describing that the second operand being compared is a
2610 known constant and how to obtain its data.
2611 Return the result of the comparison, or NULL_RTX if we failed to
2612 perform the operation. */
2615 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2616 bool equality_only, by_pieces_constfn y_cfn,
2617 void *y_cfndata, unsigned ctz_len)
2619 rtx result = 0;
2621 if (CONST_INT_P (len) && INTVAL (len) == 0)
2622 return const0_rtx;
2624 gcc_assert (MEM_P (x) && MEM_P (y));
2625 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2626 gcc_assert (align >= BITS_PER_UNIT);
2628 x = adjust_address (x, BLKmode, 0);
2629 y = adjust_address (y, BLKmode, 0);
2631 if (equality_only
2632 && CONST_INT_P (len)
2633 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2634 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2635 y_cfn, y_cfndata);
2636 else
2637 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2639 if (!result && (flag_inline_stringops & ILSOP_MEMCMP))
2640 result = emit_block_cmp_via_loop (x, y, len, len_type,
2641 target, equality_only,
2642 align, ctz_len);
2644 return result;
2647 /* Like emit_block_cmp_hints, but with known alignment and no support
2648 for constats. Always expand to a loop with iterations that compare
2649 blocks of the largest compare-by-pieces size that divides both len
2650 and align, and then, if !EQUALITY_ONLY, identify the word and then
2651 the unit that first differs to return the result. */
2654 emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
2655 bool equality_only, unsigned align, unsigned ctz_len)
2657 unsigned incr = align / BITS_PER_UNIT;
2659 if (CONST_INT_P (len))
2660 ctz_len = MAX (ctz_len, (unsigned) wi::ctz (UINTVAL (len)));
2662 if (HOST_WIDE_INT_1U << ctz_len < (unsigned HOST_WIDE_INT) incr)
2663 incr = HOST_WIDE_INT_1U << ctz_len;
2665 while (incr > 1
2666 && !can_do_by_pieces (incr, align, COMPARE_BY_PIECES))
2667 incr >>= 1;
2669 rtx_code_label *cmp_label, *top_label, *ne_label, *res_label;
2670 rtx iter, x_addr, y_addr, tmp;
2671 machine_mode x_addr_mode = get_address_mode (x);
2672 machine_mode y_addr_mode = get_address_mode (y);
2673 machine_mode iter_mode;
2675 iter_mode = GET_MODE (len);
2676 if (iter_mode == VOIDmode)
2677 iter_mode = word_mode;
2679 rtx iter_init = const0_rtx;
2680 rtx_code iter_cond = LTU;
2681 rtx_code entry_cond = GEU;
2682 rtx iter_limit = len;
2683 rtx iter_incr = GEN_INT (incr);
2684 machine_mode cmp_mode;
2686 /* We can drop the loop back edge if we know there's exactly one
2687 iteration. */
2688 top_label = (!rtx_equal_p (len, iter_incr)
2689 ? gen_label_rtx ()
2690 : NULL);
2691 /* We need not test before entering the loop if len is known
2692 nonzero. ??? This could be even stricter, testing whether a
2693 nonconstant LEN could possibly be zero. */
2694 cmp_label = (!CONSTANT_P (len) || rtx_equal_p (len, iter_init)
2695 ? gen_label_rtx ()
2696 : NULL);
2697 ne_label = gen_label_rtx ();
2698 res_label = gen_label_rtx ();
2700 iter = gen_reg_rtx (iter_mode);
2701 emit_move_insn (iter, iter_init);
2703 opt_scalar_int_mode int_cmp_mode
2704 = int_mode_for_size (incr * BITS_PER_UNIT, 1);
2705 if (!int_cmp_mode.exists (&cmp_mode)
2706 || GET_MODE_BITSIZE (int_cmp_mode.require ()) != incr * BITS_PER_UNIT
2707 || !can_compare_p (NE, cmp_mode, ccp_jump))
2709 cmp_mode = BLKmode;
2710 gcc_checking_assert (incr != 1);
2713 /* Save the base addresses. */
2714 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2715 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2716 do_pending_stack_adjust ();
2718 if (cmp_label)
2720 if (top_label)
2721 emit_jump (cmp_label);
2722 else
2723 emit_cmp_and_jump_insns (iter, iter_limit, entry_cond,
2724 NULL_RTX, iter_mode,
2725 true, cmp_label,
2726 profile_probability::guessed_always ()
2727 .apply_scale (1, 10));
2729 if (top_label)
2730 emit_label (top_label);
2732 /* Offset the base addresses by ITER. */
2733 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2734 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2736 if (x_addr_mode != y_addr_mode)
2737 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2738 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2740 x = change_address (x, cmp_mode, x_addr);
2741 y = change_address (y, cmp_mode, y_addr);
2743 /* Compare one block. */
2744 rtx part_res;
2745 if (cmp_mode == BLKmode)
2746 part_res = compare_by_pieces (x, y, incr, target, align, 0, 0);
2747 else
2748 part_res = expand_binop (cmp_mode, sub_optab, x, y, NULL_RTX,
2749 true, OPTAB_LIB_WIDEN);
2751 /* Stop if we found a difference. */
2752 emit_cmp_and_jump_insns (part_res, GEN_INT (0), NE, NULL_RTX,
2753 GET_MODE (part_res), true, ne_label,
2754 profile_probability::guessed_always ()
2755 .apply_scale (1, 10));
2757 /* Increment ITER. */
2758 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2759 true, OPTAB_LIB_WIDEN);
2760 if (tmp != iter)
2761 emit_move_insn (iter, tmp);
2763 if (cmp_label)
2764 emit_label (cmp_label);
2765 /* Loop until we reach the limit. */
2767 if (top_label)
2768 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2769 true, top_label,
2770 profile_probability::guessed_always ()
2771 .apply_scale (9, 10));
2773 /* We got to the end without differences, so the result is zero. */
2774 if (target == NULL_RTX
2775 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
2776 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
2778 emit_move_insn (target, const0_rtx);
2779 emit_jump (res_label);
2781 emit_label (ne_label);
2783 /* Return nonzero, or pinpoint the difference to return the expected
2784 result for non-equality tests. */
2785 if (equality_only)
2786 emit_move_insn (target, const1_rtx);
2787 else
2789 if (incr > UNITS_PER_WORD)
2790 /* ??? Re-compare the block found to be different one word at a
2791 time. */
2792 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2793 target, equality_only,
2794 BITS_PER_WORD, 0);
2795 else if (incr > 1)
2796 /* ??? Re-compare the block found to be different one byte at a
2797 time. We could do better using part_res, and being careful
2798 about endianness. */
2799 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2800 target, equality_only,
2801 BITS_PER_UNIT, 0);
2802 else if (known_gt (GET_MODE_BITSIZE (GET_MODE (target)),
2803 GET_MODE_BITSIZE (cmp_mode)))
2804 part_res = expand_binop (GET_MODE (target), sub_optab, x, y, target,
2805 true, OPTAB_LIB_WIDEN);
2806 else
2808 /* In the odd chance target is QImode, we can't count on
2809 widening subtract to capture the result of the unsigned
2810 compares. */
2811 rtx_code_label *ltu_label;
2812 ltu_label = gen_label_rtx ();
2813 emit_cmp_and_jump_insns (x, y, LTU, NULL_RTX,
2814 cmp_mode, true, ltu_label,
2815 profile_probability::guessed_always ()
2816 .apply_scale (5, 10));
2818 emit_move_insn (target, const1_rtx);
2819 emit_jump (res_label);
2821 emit_label (ltu_label);
2822 emit_move_insn (target, constm1_rtx);
2823 part_res = target;
2826 if (target != part_res)
2827 convert_move (target, part_res, false);
2830 emit_label (res_label);
2832 return target;
2836 /* Copy all or part of a value X into registers starting at REGNO.
2837 The number of registers to be filled is NREGS. */
2839 void
2840 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2842 if (nregs == 0)
2843 return;
2845 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2846 x = validize_mem (force_const_mem (mode, x));
2848 /* See if the machine can do this with a load multiple insn. */
2849 if (targetm.have_load_multiple ())
2851 rtx_insn *last = get_last_insn ();
2852 rtx first = gen_rtx_REG (word_mode, regno);
2853 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2854 GEN_INT (nregs)))
2856 emit_insn (pat);
2857 return;
2859 else
2860 delete_insns_since (last);
2863 for (int i = 0; i < nregs; i++)
2864 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2865 operand_subword_force (x, i, mode));
2868 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2869 The number of registers to be filled is NREGS. */
2871 void
2872 move_block_from_reg (int regno, rtx x, int nregs)
2874 if (nregs == 0)
2875 return;
2877 /* See if the machine can do this with a store multiple insn. */
2878 if (targetm.have_store_multiple ())
2880 rtx_insn *last = get_last_insn ();
2881 rtx first = gen_rtx_REG (word_mode, regno);
2882 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2883 GEN_INT (nregs)))
2885 emit_insn (pat);
2886 return;
2888 else
2889 delete_insns_since (last);
2892 for (int i = 0; i < nregs; i++)
2894 rtx tem = operand_subword (x, i, 1, BLKmode);
2896 gcc_assert (tem);
2898 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2902 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2903 ORIG, where ORIG is a non-consecutive group of registers represented by
2904 a PARALLEL. The clone is identical to the original except in that the
2905 original set of registers is replaced by a new set of pseudo registers.
2906 The new set has the same modes as the original set. */
2909 gen_group_rtx (rtx orig)
2911 int i, length;
2912 rtx *tmps;
2914 gcc_assert (GET_CODE (orig) == PARALLEL);
2916 length = XVECLEN (orig, 0);
2917 tmps = XALLOCAVEC (rtx, length);
2919 /* Skip a NULL entry in first slot. */
2920 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2922 if (i)
2923 tmps[0] = 0;
2925 for (; i < length; i++)
2927 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2928 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2930 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2933 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2936 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2937 except that values are placed in TMPS[i], and must later be moved
2938 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2940 static void
2941 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2942 poly_int64 ssize)
2944 rtx src;
2945 int start, i;
2946 machine_mode m = GET_MODE (orig_src);
2948 gcc_assert (GET_CODE (dst) == PARALLEL);
2950 if (m != VOIDmode
2951 && !SCALAR_INT_MODE_P (m)
2952 && !MEM_P (orig_src)
2953 && GET_CODE (orig_src) != CONCAT)
2955 scalar_int_mode imode;
2956 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2958 src = gen_reg_rtx (imode);
2959 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2961 else
2963 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2964 emit_move_insn (src, orig_src);
2966 emit_group_load_1 (tmps, dst, src, type, ssize);
2967 return;
2970 /* Check for a NULL entry, used to indicate that the parameter goes
2971 both on the stack and in registers. */
2972 if (XEXP (XVECEXP (dst, 0, 0), 0))
2973 start = 0;
2974 else
2975 start = 1;
2977 /* Process the pieces. */
2978 for (i = start; i < XVECLEN (dst, 0); i++)
2980 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2981 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2982 poly_int64 bytelen = GET_MODE_SIZE (mode);
2983 poly_int64 shift = 0;
2985 /* Handle trailing fragments that run over the size of the struct.
2986 It's the target's responsibility to make sure that the fragment
2987 cannot be strictly smaller in some cases and strictly larger
2988 in others. */
2989 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2990 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2992 /* Arrange to shift the fragment to where it belongs.
2993 extract_bit_field loads to the lsb of the reg. */
2994 if (
2995 #ifdef BLOCK_REG_PADDING
2996 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2997 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2998 #else
2999 BYTES_BIG_ENDIAN
3000 #endif
3002 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3003 bytelen = ssize - bytepos;
3004 gcc_assert (maybe_gt (bytelen, 0));
3007 /* If we won't be loading directly from memory, protect the real source
3008 from strange tricks we might play; but make sure that the source can
3009 be loaded directly into the destination. */
3010 src = orig_src;
3011 if (!MEM_P (orig_src)
3012 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src))
3013 && !CONSTANT_P (orig_src))
3015 gcc_assert (GET_MODE (orig_src) != VOIDmode);
3016 src = force_reg (GET_MODE (orig_src), orig_src);
3019 /* Optimize the access just a bit. */
3020 if (MEM_P (src)
3021 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
3022 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
3023 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
3024 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3026 tmps[i] = gen_reg_rtx (mode);
3027 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
3029 else if (COMPLEX_MODE_P (mode)
3030 && GET_MODE (src) == mode
3031 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3032 /* Let emit_move_complex do the bulk of the work. */
3033 tmps[i] = src;
3034 else if (GET_CODE (src) == CONCAT)
3036 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
3037 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
3038 unsigned int elt;
3039 poly_int64 subpos;
3041 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
3042 && known_le (subpos + bytelen, slen0))
3044 /* The following assumes that the concatenated objects all
3045 have the same size. In this case, a simple calculation
3046 can be used to determine the object and the bit field
3047 to be extracted. */
3048 tmps[i] = XEXP (src, elt);
3049 if (maybe_ne (subpos, 0)
3050 || maybe_ne (subpos + bytelen, slen0)
3051 || (!CONSTANT_P (tmps[i])
3052 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
3053 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
3054 subpos * BITS_PER_UNIT,
3055 1, NULL_RTX, mode, mode, false,
3056 NULL);
3058 else
3060 rtx mem;
3062 gcc_assert (known_eq (bytepos, 0));
3063 mem = assign_stack_temp (GET_MODE (src), slen);
3064 emit_move_insn (mem, src);
3065 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
3066 0, 1, NULL_RTX, mode, mode, false,
3067 NULL);
3070 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
3071 && XVECLEN (dst, 0) > 1)
3072 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
3073 else if (CONSTANT_P (src))
3075 if (known_eq (bytelen, ssize))
3076 tmps[i] = src;
3077 else
3079 rtx first, second;
3081 /* TODO: const_wide_int can have sizes other than this... */
3082 gcc_assert (known_eq (2 * bytelen, ssize));
3083 split_double (src, &first, &second);
3084 if (i)
3085 tmps[i] = second;
3086 else
3087 tmps[i] = first;
3090 else if (REG_P (src) && GET_MODE (src) == mode)
3091 tmps[i] = src;
3092 else
3093 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
3094 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
3095 mode, mode, false, NULL);
3097 if (maybe_ne (shift, 0))
3098 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
3099 shift, tmps[i], 0);
3103 /* Emit code to move a block SRC of type TYPE to a block DST,
3104 where DST is non-consecutive registers represented by a PARALLEL.
3105 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
3106 if not known. */
3108 void
3109 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
3111 rtx *tmps;
3112 int i;
3114 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
3115 emit_group_load_1 (tmps, dst, src, type, ssize);
3117 /* Copy the extracted pieces into the proper (probable) hard regs. */
3118 for (i = 0; i < XVECLEN (dst, 0); i++)
3120 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
3121 if (d == NULL)
3122 continue;
3123 emit_move_insn (d, tmps[i]);
3127 /* Similar, but load SRC into new pseudos in a format that looks like
3128 PARALLEL. This can later be fed to emit_group_move to get things
3129 in the right place. */
3132 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
3134 rtvec vec;
3135 int i;
3137 vec = rtvec_alloc (XVECLEN (parallel, 0));
3138 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
3140 /* Convert the vector to look just like the original PARALLEL, except
3141 with the computed values. */
3142 for (i = 0; i < XVECLEN (parallel, 0); i++)
3144 rtx e = XVECEXP (parallel, 0, i);
3145 rtx d = XEXP (e, 0);
3147 if (d)
3149 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
3150 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
3152 RTVEC_ELT (vec, i) = e;
3155 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
3158 /* Emit code to move a block SRC to block DST, where SRC and DST are
3159 non-consecutive groups of registers, each represented by a PARALLEL. */
3161 void
3162 emit_group_move (rtx dst, rtx src)
3164 int i;
3166 gcc_assert (GET_CODE (src) == PARALLEL
3167 && GET_CODE (dst) == PARALLEL
3168 && XVECLEN (src, 0) == XVECLEN (dst, 0));
3170 /* Skip first entry if NULL. */
3171 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
3172 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
3173 XEXP (XVECEXP (src, 0, i), 0));
3176 /* Move a group of registers represented by a PARALLEL into pseudos. */
3179 emit_group_move_into_temps (rtx src)
3181 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
3182 int i;
3184 for (i = 0; i < XVECLEN (src, 0); i++)
3186 rtx e = XVECEXP (src, 0, i);
3187 rtx d = XEXP (e, 0);
3189 if (d)
3190 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
3191 RTVEC_ELT (vec, i) = e;
3194 return gen_rtx_PARALLEL (GET_MODE (src), vec);
3197 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
3198 where SRC is non-consecutive registers represented by a PARALLEL.
3199 SSIZE represents the total size of block ORIG_DST, or -1 if not
3200 known. */
3202 void
3203 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
3204 poly_int64 ssize)
3206 rtx *tmps, dst;
3207 int start, finish, i;
3208 machine_mode m = GET_MODE (orig_dst);
3210 gcc_assert (GET_CODE (src) == PARALLEL);
3212 if (!SCALAR_INT_MODE_P (m)
3213 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
3215 scalar_int_mode imode;
3216 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
3218 dst = gen_reg_rtx (imode);
3219 emit_group_store (dst, src, type, ssize);
3220 dst = gen_lowpart (GET_MODE (orig_dst), dst);
3222 else
3224 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
3225 emit_group_store (dst, src, type, ssize);
3227 emit_move_insn (orig_dst, dst);
3228 return;
3231 /* Check for a NULL entry, used to indicate that the parameter goes
3232 both on the stack and in registers. */
3233 if (XEXP (XVECEXP (src, 0, 0), 0))
3234 start = 0;
3235 else
3236 start = 1;
3237 finish = XVECLEN (src, 0);
3239 tmps = XALLOCAVEC (rtx, finish);
3241 /* Copy the (probable) hard regs into pseudos. */
3242 for (i = start; i < finish; i++)
3244 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
3245 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
3247 tmps[i] = gen_reg_rtx (GET_MODE (reg));
3248 emit_move_insn (tmps[i], reg);
3250 else
3251 tmps[i] = reg;
3254 /* If we won't be storing directly into memory, protect the real destination
3255 from strange tricks we might play. */
3256 dst = orig_dst;
3257 if (GET_CODE (dst) == PARALLEL)
3259 rtx temp;
3261 /* We can get a PARALLEL dst if there is a conditional expression in
3262 a return statement. In that case, the dst and src are the same,
3263 so no action is necessary. */
3264 if (rtx_equal_p (dst, src))
3265 return;
3267 /* It is unclear if we can ever reach here, but we may as well handle
3268 it. Allocate a temporary, and split this into a store/load to/from
3269 the temporary. */
3270 temp = assign_stack_temp (GET_MODE (dst), ssize);
3271 emit_group_store (temp, src, type, ssize);
3272 emit_group_load (dst, temp, type, ssize);
3273 return;
3275 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
3277 machine_mode outer = GET_MODE (dst);
3278 machine_mode inner;
3279 poly_int64 bytepos;
3280 bool done = false;
3281 rtx temp;
3283 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
3284 dst = gen_reg_rtx (outer);
3286 /* Make life a bit easier for combine: if the first element of the
3287 vector is the low part of the destination mode, use a paradoxical
3288 subreg to initialize the destination. */
3289 if (start < finish)
3291 inner = GET_MODE (tmps[start]);
3292 bytepos = subreg_lowpart_offset (inner, outer);
3293 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
3294 bytepos))
3296 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
3297 if (temp)
3299 emit_move_insn (dst, temp);
3300 done = true;
3301 start++;
3306 /* If the first element wasn't the low part, try the last. */
3307 if (!done
3308 && start < finish - 1)
3310 inner = GET_MODE (tmps[finish - 1]);
3311 bytepos = subreg_lowpart_offset (inner, outer);
3312 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
3313 finish - 1), 1)),
3314 bytepos))
3316 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
3317 if (temp)
3319 emit_move_insn (dst, temp);
3320 done = true;
3321 finish--;
3326 /* Otherwise, simply initialize the result to zero. */
3327 if (!done)
3328 emit_move_insn (dst, CONST0_RTX (outer));
3331 /* Process the pieces. */
3332 for (i = start; i < finish; i++)
3334 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
3335 machine_mode mode = GET_MODE (tmps[i]);
3336 poly_int64 bytelen = GET_MODE_SIZE (mode);
3337 poly_uint64 adj_bytelen;
3338 rtx dest = dst;
3340 /* Handle trailing fragments that run over the size of the struct.
3341 It's the target's responsibility to make sure that the fragment
3342 cannot be strictly smaller in some cases and strictly larger
3343 in others. */
3344 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
3345 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3346 adj_bytelen = ssize - bytepos;
3347 else
3348 adj_bytelen = bytelen;
3350 /* Deal with destination CONCATs by either storing into one of the parts
3351 or doing a copy after storing into a register or stack temporary. */
3352 if (GET_CODE (dst) == CONCAT)
3354 if (known_le (bytepos + adj_bytelen,
3355 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3356 dest = XEXP (dst, 0);
3358 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3360 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
3361 dest = XEXP (dst, 1);
3364 else
3366 machine_mode dest_mode = GET_MODE (dest);
3367 machine_mode tmp_mode = GET_MODE (tmps[i]);
3368 scalar_int_mode dest_imode;
3370 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
3372 /* If the source is a single scalar integer register, and the
3373 destination has a complex mode for which a same-sized integer
3374 mode exists, then we can take the left-justified part of the
3375 source in the complex mode. */
3376 if (finish == start + 1
3377 && REG_P (tmps[i])
3378 && SCALAR_INT_MODE_P (tmp_mode)
3379 && COMPLEX_MODE_P (dest_mode)
3380 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3382 const scalar_int_mode tmp_imode
3383 = as_a <scalar_int_mode> (tmp_mode);
3385 if (GET_MODE_BITSIZE (dest_imode)
3386 < GET_MODE_BITSIZE (tmp_imode))
3388 dest = gen_reg_rtx (dest_imode);
3389 if (BYTES_BIG_ENDIAN)
3390 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3391 GET_MODE_BITSIZE (tmp_imode)
3392 - GET_MODE_BITSIZE (dest_imode),
3393 NULL_RTX, 1);
3394 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3395 dst = gen_lowpart (dest_mode, dest);
3397 else
3398 dst = gen_lowpart (dest_mode, tmps[i]);
3401 /* Otherwise spill the source onto the stack using the more
3402 aligned of the two modes. */
3403 else if (GET_MODE_ALIGNMENT (dest_mode)
3404 >= GET_MODE_ALIGNMENT (tmp_mode))
3406 dest = assign_stack_temp (dest_mode,
3407 GET_MODE_SIZE (dest_mode));
3408 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3409 tmps[i]);
3410 dst = dest;
3413 else
3415 dest = assign_stack_temp (tmp_mode,
3416 GET_MODE_SIZE (tmp_mode));
3417 emit_move_insn (dest, tmps[i]);
3418 dst = adjust_address (dest, dest_mode, bytepos);
3421 break;
3425 /* Handle trailing fragments that run over the size of the struct. */
3426 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3428 /* store_bit_field always takes its value from the lsb.
3429 Move the fragment to the lsb if it's not already there. */
3430 if (
3431 #ifdef BLOCK_REG_PADDING
3432 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3433 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3434 #else
3435 BYTES_BIG_ENDIAN
3436 #endif
3439 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3440 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3441 shift, tmps[i], 0);
3444 /* Make sure not to write past the end of the struct. */
3445 store_bit_field (dest,
3446 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3447 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3448 VOIDmode, tmps[i], false, false);
3451 /* Optimize the access just a bit. */
3452 else if (MEM_P (dest)
3453 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3454 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3455 && multiple_p (bytepos * BITS_PER_UNIT,
3456 GET_MODE_ALIGNMENT (mode))
3457 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3458 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3460 else
3461 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3462 0, 0, mode, tmps[i], false, false);
3465 /* Copy from the pseudo into the (probable) hard reg. */
3466 if (orig_dst != dst)
3467 emit_move_insn (orig_dst, dst);
3470 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3471 of the value stored in X. */
3474 maybe_emit_group_store (rtx x, tree type)
3476 machine_mode mode = TYPE_MODE (type);
3477 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3478 if (GET_CODE (x) == PARALLEL)
3480 rtx result = gen_reg_rtx (mode);
3481 emit_group_store (result, x, type, int_size_in_bytes (type));
3482 return result;
3484 return x;
3487 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3489 This is used on targets that return BLKmode values in registers. */
3491 static void
3492 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3494 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3495 rtx src = NULL, dst = NULL;
3496 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3497 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3498 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3499 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3500 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3501 fixed_size_mode copy_mode;
3503 /* BLKmode registers created in the back-end shouldn't have survived. */
3504 gcc_assert (mode != BLKmode);
3506 /* If the structure doesn't take up a whole number of words, see whether
3507 SRCREG is padded on the left or on the right. If it's on the left,
3508 set PADDING_CORRECTION to the number of bits to skip.
3510 In most ABIs, the structure will be returned at the least end of
3511 the register, which translates to right padding on little-endian
3512 targets and left padding on big-endian targets. The opposite
3513 holds if the structure is returned at the most significant
3514 end of the register. */
3515 if (bytes % UNITS_PER_WORD != 0
3516 && (targetm.calls.return_in_msb (type)
3517 ? !BYTES_BIG_ENDIAN
3518 : BYTES_BIG_ENDIAN))
3519 padding_correction
3520 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3522 /* We can use a single move if we have an exact mode for the size. */
3523 else if (MEM_P (target)
3524 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3525 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3526 && bytes == GET_MODE_SIZE (mode))
3528 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3529 return;
3532 /* And if we additionally have the same mode for a register. */
3533 else if (REG_P (target)
3534 && GET_MODE (target) == mode
3535 && bytes == GET_MODE_SIZE (mode))
3537 emit_move_insn (target, srcreg);
3538 return;
3541 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3542 into a new pseudo which is a full word. */
3543 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3545 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3546 mode = word_mode;
3549 /* Copy the structure BITSIZE bits at a time. If the target lives in
3550 memory, take care of not reading/writing past its end by selecting
3551 a copy mode suited to BITSIZE. This should always be possible given
3552 how it is computed.
3554 If the target lives in register, make sure not to select a copy mode
3555 larger than the mode of the register.
3557 We could probably emit more efficient code for machines which do not use
3558 strict alignment, but it doesn't seem worth the effort at the current
3559 time. */
3561 copy_mode = word_mode;
3562 if (MEM_P (target))
3564 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3565 if (mem_mode.exists ())
3566 copy_mode = mem_mode.require ();
3568 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3569 copy_mode = tmode;
3571 for (bitpos = 0, xbitpos = padding_correction;
3572 bitpos < bytes * BITS_PER_UNIT;
3573 bitpos += bitsize, xbitpos += bitsize)
3575 /* We need a new source operand each time xbitpos is on a
3576 word boundary and when xbitpos == padding_correction
3577 (the first time through). */
3578 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3579 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3581 /* We need a new destination operand each time bitpos is on
3582 a word boundary. */
3583 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3584 dst = target;
3585 else if (bitpos % BITS_PER_WORD == 0)
3586 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3588 /* Use xbitpos for the source extraction (right justified) and
3589 bitpos for the destination store (left justified). */
3590 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3591 extract_bit_field (src, bitsize,
3592 xbitpos % BITS_PER_WORD, 1,
3593 NULL_RTX, copy_mode, copy_mode,
3594 false, NULL),
3595 false, false);
3599 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3600 register if it contains any data, otherwise return null.
3602 This is used on targets that return BLKmode values in registers. */
3605 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3607 int i, n_regs;
3608 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3609 unsigned int bitsize;
3610 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3611 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3612 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3613 fixed_size_mode dst_mode;
3614 scalar_int_mode min_mode;
3616 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3618 x = expand_normal (src);
3620 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3621 if (bytes == 0)
3622 return NULL_RTX;
3624 /* If the structure doesn't take up a whole number of words, see
3625 whether the register value should be padded on the left or on
3626 the right. Set PADDING_CORRECTION to the number of padding
3627 bits needed on the left side.
3629 In most ABIs, the structure will be returned at the least end of
3630 the register, which translates to right padding on little-endian
3631 targets and left padding on big-endian targets. The opposite
3632 holds if the structure is returned at the most significant
3633 end of the register. */
3634 if (bytes % UNITS_PER_WORD != 0
3635 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3636 ? !BYTES_BIG_ENDIAN
3637 : BYTES_BIG_ENDIAN))
3638 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3639 * BITS_PER_UNIT));
3641 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3642 dst_words = XALLOCAVEC (rtx, n_regs);
3643 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3644 min_mode = smallest_int_mode_for_size (bitsize);
3646 /* Copy the structure BITSIZE bits at a time. */
3647 for (bitpos = 0, xbitpos = padding_correction;
3648 bitpos < bytes * BITS_PER_UNIT;
3649 bitpos += bitsize, xbitpos += bitsize)
3651 /* We need a new destination pseudo each time xbitpos is
3652 on a word boundary and when xbitpos == padding_correction
3653 (the first time through). */
3654 if (xbitpos % BITS_PER_WORD == 0
3655 || xbitpos == padding_correction)
3657 /* Generate an appropriate register. */
3658 dst_word = gen_reg_rtx (word_mode);
3659 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3661 /* Clear the destination before we move anything into it. */
3662 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3665 /* Find the largest integer mode that can be used to copy all or as
3666 many bits as possible of the structure if the target supports larger
3667 copies. There are too many corner cases here w.r.t to alignments on
3668 the read/writes. So if there is any padding just use single byte
3669 operations. */
3670 opt_scalar_int_mode mode_iter;
3671 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3673 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3675 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3676 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3677 && msize <= BITS_PER_WORD)
3678 bitsize = msize;
3679 else
3680 break;
3684 /* We need a new source operand each time bitpos is on a word
3685 boundary. */
3686 if (bitpos % BITS_PER_WORD == 0)
3687 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3689 /* Use bitpos for the source extraction (left justified) and
3690 xbitpos for the destination store (right justified). */
3691 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3692 0, 0, word_mode,
3693 extract_bit_field (src_word, bitsize,
3694 bitpos % BITS_PER_WORD, 1,
3695 NULL_RTX, word_mode, word_mode,
3696 false, NULL),
3697 false, false);
3700 if (mode == BLKmode)
3702 /* Find the smallest integer mode large enough to hold the
3703 entire structure. */
3704 opt_scalar_int_mode mode_iter;
3705 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3706 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3707 break;
3709 /* A suitable mode should have been found. */
3710 mode = mode_iter.require ();
3713 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3714 dst_mode = word_mode;
3715 else
3716 dst_mode = mode;
3717 dst = gen_reg_rtx (dst_mode);
3719 for (i = 0; i < n_regs; i++)
3720 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3722 if (mode != dst_mode)
3723 dst = gen_lowpart (mode, dst);
3725 return dst;
3728 /* Add a USE expression for REG to the (possibly empty) list pointed
3729 to by CALL_FUSAGE. REG must denote a hard register. */
3731 void
3732 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3734 gcc_assert (REG_P (reg));
3736 if (!HARD_REGISTER_P (reg))
3737 return;
3739 *call_fusage
3740 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3743 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3744 to by CALL_FUSAGE. REG must denote a hard register. */
3746 void
3747 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3749 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3751 *call_fusage
3752 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3755 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3756 starting at REGNO. All of these registers must be hard registers. */
3758 void
3759 use_regs (rtx *call_fusage, int regno, int nregs)
3761 int i;
3763 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3765 for (i = 0; i < nregs; i++)
3766 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3769 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3770 PARALLEL REGS. This is for calls that pass values in multiple
3771 non-contiguous locations. The Irix 6 ABI has examples of this. */
3773 void
3774 use_group_regs (rtx *call_fusage, rtx regs)
3776 int i;
3778 for (i = 0; i < XVECLEN (regs, 0); i++)
3780 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3782 /* A NULL entry means the parameter goes both on the stack and in
3783 registers. This can also be a MEM for targets that pass values
3784 partially on the stack and partially in registers. */
3785 if (reg != 0 && REG_P (reg))
3786 use_reg (call_fusage, reg);
3790 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3791 assigment and the code of the expresion on the RHS is CODE. Return
3792 NULL otherwise. */
3794 static gimple *
3795 get_def_for_expr (tree name, enum tree_code code)
3797 gimple *def_stmt;
3799 if (TREE_CODE (name) != SSA_NAME)
3800 return NULL;
3802 def_stmt = get_gimple_for_ssa_name (name);
3803 if (!def_stmt
3804 || gimple_assign_rhs_code (def_stmt) != code)
3805 return NULL;
3807 return def_stmt;
3810 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3811 assigment and the class of the expresion on the RHS is CLASS. Return
3812 NULL otherwise. */
3814 static gimple *
3815 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3817 gimple *def_stmt;
3819 if (TREE_CODE (name) != SSA_NAME)
3820 return NULL;
3822 def_stmt = get_gimple_for_ssa_name (name);
3823 if (!def_stmt
3824 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3825 return NULL;
3827 return def_stmt;
3830 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3831 its length in bytes. */
3834 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3835 unsigned int expected_align, HOST_WIDE_INT expected_size,
3836 unsigned HOST_WIDE_INT min_size,
3837 unsigned HOST_WIDE_INT max_size,
3838 unsigned HOST_WIDE_INT probable_max_size,
3839 unsigned ctz_size)
3841 machine_mode mode = GET_MODE (object);
3842 unsigned int align;
3844 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3846 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3847 just move a zero. Otherwise, do this a piece at a time. */
3848 poly_int64 size_val;
3849 if (mode != BLKmode
3850 && poly_int_rtx_p (size, &size_val)
3851 && known_eq (size_val, GET_MODE_SIZE (mode)))
3853 rtx zero = CONST0_RTX (mode);
3854 if (zero != NULL)
3856 emit_move_insn (object, zero);
3857 return NULL;
3860 if (COMPLEX_MODE_P (mode))
3862 zero = CONST0_RTX (GET_MODE_INNER (mode));
3863 if (zero != NULL)
3865 write_complex_part (object, zero, 0, true);
3866 write_complex_part (object, zero, 1, false);
3867 return NULL;
3872 if (size == const0_rtx)
3873 return NULL;
3875 align = MEM_ALIGN (object);
3877 if (CONST_INT_P (size)
3878 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3879 CLEAR_BY_PIECES,
3880 optimize_insn_for_speed_p ()))
3881 clear_by_pieces (object, INTVAL (size), align);
3882 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3883 expected_align, expected_size,
3884 min_size, max_size, probable_max_size))
3886 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3887 min_size, max_size,
3888 NULL_RTX, 0, align))
3890 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3891 return set_storage_via_libcall (object, size, const0_rtx,
3892 method == BLOCK_OP_TAILCALL);
3893 else
3894 gcc_unreachable ();
3896 return NULL;
3900 clear_storage (rtx object, rtx size, enum block_op_methods method)
3902 unsigned HOST_WIDE_INT max, min = 0;
3903 if (GET_CODE (size) == CONST_INT)
3904 min = max = UINTVAL (size);
3905 else
3906 max = GET_MODE_MASK (GET_MODE (size));
3907 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3911 /* A subroutine of clear_storage. Expand a call to memset.
3912 Return the return value of memset, 0 otherwise. */
3915 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3917 tree call_expr, fn, object_tree, size_tree, val_tree;
3918 machine_mode size_mode;
3920 object = copy_addr_to_reg (XEXP (object, 0));
3921 object_tree = make_tree (ptr_type_node, object);
3923 if (!CONST_INT_P (val))
3924 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3925 val_tree = make_tree (integer_type_node, val);
3927 size_mode = TYPE_MODE (sizetype);
3928 size = convert_to_mode (size_mode, size, 1);
3929 size = copy_to_mode_reg (size_mode, size);
3930 size_tree = make_tree (sizetype, size);
3932 /* It is incorrect to use the libcall calling conventions for calls to
3933 memset because it can be provided by the user. */
3934 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3935 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3936 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3938 return expand_call (call_expr, NULL_RTX, false);
3941 /* Expand a setmem pattern; return true if successful. */
3943 bool
3944 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3945 unsigned int expected_align, HOST_WIDE_INT expected_size,
3946 unsigned HOST_WIDE_INT min_size,
3947 unsigned HOST_WIDE_INT max_size,
3948 unsigned HOST_WIDE_INT probable_max_size)
3950 /* Try the most limited insn first, because there's no point
3951 including more than one in the machine description unless
3952 the more limited one has some advantage. */
3954 if (expected_align < align)
3955 expected_align = align;
3956 if (expected_size != -1)
3958 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3959 expected_size = max_size;
3960 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3961 expected_size = min_size;
3964 opt_scalar_int_mode mode_iter;
3965 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3967 scalar_int_mode mode = mode_iter.require ();
3968 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3970 if (code != CODE_FOR_nothing
3971 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3972 here because if SIZE is less than the mode mask, as it is
3973 returned by the macro, it will definitely be less than the
3974 actual mode mask. Since SIZE is within the Pmode address
3975 space, we limit MODE to Pmode. */
3976 && ((CONST_INT_P (size)
3977 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3978 <= (GET_MODE_MASK (mode) >> 1)))
3979 || max_size <= (GET_MODE_MASK (mode) >> 1)
3980 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3982 class expand_operand ops[9];
3983 unsigned int nops;
3985 nops = insn_data[(int) code].n_generator_args;
3986 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3988 create_fixed_operand (&ops[0], object);
3989 /* The check above guarantees that this size conversion is valid. */
3990 create_convert_operand_to (&ops[1], size, mode, true);
3991 create_convert_operand_from (&ops[2], val, byte_mode, true);
3992 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3993 if (nops >= 6)
3995 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3996 create_integer_operand (&ops[5], expected_size);
3998 if (nops >= 8)
4000 create_integer_operand (&ops[6], min_size);
4001 /* If we cannot represent the maximal size,
4002 make parameter NULL. */
4003 if ((HOST_WIDE_INT) max_size != -1)
4004 create_integer_operand (&ops[7], max_size);
4005 else
4006 create_fixed_operand (&ops[7], NULL);
4008 if (nops == 9)
4010 /* If we cannot represent the maximal size,
4011 make parameter NULL. */
4012 if ((HOST_WIDE_INT) probable_max_size != -1)
4013 create_integer_operand (&ops[8], probable_max_size);
4014 else
4015 create_fixed_operand (&ops[8], NULL);
4017 if (maybe_expand_insn (code, nops, ops))
4018 return true;
4022 return false;
4026 /* Write to one of the components of the complex value CPLX. Write VAL to
4027 the real part if IMAG_P is false, and the imaginary part if its true.
4028 If UNDEFINED_P then the value in CPLX is currently undefined. */
4030 void
4031 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
4033 machine_mode cmode;
4034 scalar_mode imode;
4035 unsigned ibitsize;
4037 if (GET_CODE (cplx) == CONCAT)
4039 emit_move_insn (XEXP (cplx, imag_p), val);
4040 return;
4043 cmode = GET_MODE (cplx);
4044 imode = GET_MODE_INNER (cmode);
4045 ibitsize = GET_MODE_BITSIZE (imode);
4047 /* For MEMs simplify_gen_subreg may generate an invalid new address
4048 because, e.g., the original address is considered mode-dependent
4049 by the target, which restricts simplify_subreg from invoking
4050 adjust_address_nv. Instead of preparing fallback support for an
4051 invalid address, we call adjust_address_nv directly. */
4052 if (MEM_P (cplx))
4054 emit_move_insn (adjust_address_nv (cplx, imode,
4055 imag_p ? GET_MODE_SIZE (imode) : 0),
4056 val);
4057 return;
4060 /* If the sub-object is at least word sized, then we know that subregging
4061 will work. This special case is important, since store_bit_field
4062 wants to operate on integer modes, and there's rarely an OImode to
4063 correspond to TCmode. */
4064 if (ibitsize >= BITS_PER_WORD
4065 /* For hard regs we have exact predicates. Assume we can split
4066 the original object if it spans an even number of hard regs.
4067 This special case is important for SCmode on 64-bit platforms
4068 where the natural size of floating-point regs is 32-bit. */
4069 || (REG_P (cplx)
4070 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4071 && REG_NREGS (cplx) % 2 == 0))
4073 rtx part = simplify_gen_subreg (imode, cplx, cmode,
4074 imag_p ? GET_MODE_SIZE (imode) : 0);
4075 if (part)
4077 emit_move_insn (part, val);
4078 return;
4080 else
4081 /* simplify_gen_subreg may fail for sub-word MEMs. */
4082 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4085 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
4086 false, undefined_p);
4089 /* Extract one of the components of the complex value CPLX. Extract the
4090 real part if IMAG_P is false, and the imaginary part if it's true. */
4093 read_complex_part (rtx cplx, bool imag_p)
4095 machine_mode cmode;
4096 scalar_mode imode;
4097 unsigned ibitsize;
4099 if (GET_CODE (cplx) == CONCAT)
4100 return XEXP (cplx, imag_p);
4102 cmode = GET_MODE (cplx);
4103 imode = GET_MODE_INNER (cmode);
4104 ibitsize = GET_MODE_BITSIZE (imode);
4106 /* Special case reads from complex constants that got spilled to memory. */
4107 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
4109 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
4110 if (decl && TREE_CODE (decl) == COMPLEX_CST)
4112 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
4113 if (CONSTANT_CLASS_P (part))
4114 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
4118 /* For MEMs simplify_gen_subreg may generate an invalid new address
4119 because, e.g., the original address is considered mode-dependent
4120 by the target, which restricts simplify_subreg from invoking
4121 adjust_address_nv. Instead of preparing fallback support for an
4122 invalid address, we call adjust_address_nv directly. */
4123 if (MEM_P (cplx))
4124 return adjust_address_nv (cplx, imode,
4125 imag_p ? GET_MODE_SIZE (imode) : 0);
4127 /* If the sub-object is at least word sized, then we know that subregging
4128 will work. This special case is important, since extract_bit_field
4129 wants to operate on integer modes, and there's rarely an OImode to
4130 correspond to TCmode. */
4131 if (ibitsize >= BITS_PER_WORD
4132 /* For hard regs we have exact predicates. Assume we can split
4133 the original object if it spans an even number of hard regs.
4134 This special case is important for SCmode on 64-bit platforms
4135 where the natural size of floating-point regs is 32-bit. */
4136 || (REG_P (cplx)
4137 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4138 && REG_NREGS (cplx) % 2 == 0))
4140 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
4141 imag_p ? GET_MODE_SIZE (imode) : 0);
4142 if (ret)
4143 return ret;
4144 else
4145 /* simplify_gen_subreg may fail for sub-word MEMs. */
4146 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4149 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
4150 true, NULL_RTX, imode, imode, false, NULL);
4153 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
4154 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
4155 represented in NEW_MODE. If FORCE is true, this will never happen, as
4156 we'll force-create a SUBREG if needed. */
4158 static rtx
4159 emit_move_change_mode (machine_mode new_mode,
4160 machine_mode old_mode, rtx x, bool force)
4162 rtx ret;
4164 if (push_operand (x, GET_MODE (x)))
4166 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
4167 MEM_COPY_ATTRIBUTES (ret, x);
4169 else if (MEM_P (x))
4171 /* We don't have to worry about changing the address since the
4172 size in bytes is supposed to be the same. */
4173 if (reload_in_progress)
4175 /* Copy the MEM to change the mode and move any
4176 substitutions from the old MEM to the new one. */
4177 ret = adjust_address_nv (x, new_mode, 0);
4178 copy_replacements (x, ret);
4180 else
4181 ret = adjust_address (x, new_mode, 0);
4183 else
4185 /* Note that we do want simplify_subreg's behavior of validating
4186 that the new mode is ok for a hard register. If we were to use
4187 simplify_gen_subreg, we would create the subreg, but would
4188 probably run into the target not being able to implement it. */
4189 /* Except, of course, when FORCE is true, when this is exactly what
4190 we want. Which is needed for CCmodes on some targets. */
4191 if (force)
4192 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
4193 else
4194 ret = simplify_subreg (new_mode, x, old_mode, 0);
4197 return ret;
4200 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
4201 an integer mode of the same size as MODE. Returns the instruction
4202 emitted, or NULL if such a move could not be generated. */
4204 static rtx_insn *
4205 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
4207 scalar_int_mode imode;
4208 enum insn_code code;
4210 /* There must exist a mode of the exact size we require. */
4211 if (!int_mode_for_mode (mode).exists (&imode))
4212 return NULL;
4214 /* The target must support moves in this mode. */
4215 code = optab_handler (mov_optab, imode);
4216 if (code == CODE_FOR_nothing)
4217 return NULL;
4219 x = emit_move_change_mode (imode, mode, x, force);
4220 if (x == NULL_RTX)
4221 return NULL;
4222 y = emit_move_change_mode (imode, mode, y, force);
4223 if (y == NULL_RTX)
4224 return NULL;
4225 return emit_insn (GEN_FCN (code) (x, y));
4228 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
4229 Return an equivalent MEM that does not use an auto-increment. */
4232 emit_move_resolve_push (machine_mode mode, rtx x)
4234 enum rtx_code code = GET_CODE (XEXP (x, 0));
4235 rtx temp;
4237 poly_int64 adjust = GET_MODE_SIZE (mode);
4238 #ifdef PUSH_ROUNDING
4239 adjust = PUSH_ROUNDING (adjust);
4240 #endif
4241 if (code == PRE_DEC || code == POST_DEC)
4242 adjust = -adjust;
4243 else if (code == PRE_MODIFY || code == POST_MODIFY)
4245 rtx expr = XEXP (XEXP (x, 0), 1);
4247 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
4248 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
4249 if (GET_CODE (expr) == MINUS)
4250 val = -val;
4251 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
4252 adjust = val;
4255 /* Do not use anti_adjust_stack, since we don't want to update
4256 stack_pointer_delta. */
4257 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
4258 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
4259 0, OPTAB_LIB_WIDEN);
4260 if (temp != stack_pointer_rtx)
4261 emit_move_insn (stack_pointer_rtx, temp);
4263 switch (code)
4265 case PRE_INC:
4266 case PRE_DEC:
4267 case PRE_MODIFY:
4268 temp = stack_pointer_rtx;
4269 break;
4270 case POST_INC:
4271 case POST_DEC:
4272 case POST_MODIFY:
4273 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
4274 break;
4275 default:
4276 gcc_unreachable ();
4279 return replace_equiv_address (x, temp);
4282 /* A subroutine of emit_move_complex. Generate a move from Y into X.
4283 X is known to satisfy push_operand, and MODE is known to be complex.
4284 Returns the last instruction emitted. */
4286 rtx_insn *
4287 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
4289 scalar_mode submode = GET_MODE_INNER (mode);
4290 bool imag_first;
4292 #ifdef PUSH_ROUNDING
4293 poly_int64 submodesize = GET_MODE_SIZE (submode);
4295 /* In case we output to the stack, but the size is smaller than the
4296 machine can push exactly, we need to use move instructions. */
4297 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
4299 x = emit_move_resolve_push (mode, x);
4300 return emit_move_insn (x, y);
4302 #endif
4304 /* Note that the real part always precedes the imag part in memory
4305 regardless of machine's endianness. */
4306 switch (GET_CODE (XEXP (x, 0)))
4308 case PRE_DEC:
4309 case POST_DEC:
4310 imag_first = true;
4311 break;
4312 case PRE_INC:
4313 case POST_INC:
4314 imag_first = false;
4315 break;
4316 default:
4317 gcc_unreachable ();
4320 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4321 read_complex_part (y, imag_first));
4322 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4323 read_complex_part (y, !imag_first));
4326 /* A subroutine of emit_move_complex. Perform the move from Y to X
4327 via two moves of the parts. Returns the last instruction emitted. */
4329 rtx_insn *
4330 emit_move_complex_parts (rtx x, rtx y)
4332 /* Show the output dies here. This is necessary for SUBREGs
4333 of pseudos since we cannot track their lifetimes correctly;
4334 hard regs shouldn't appear here except as return values. */
4335 if (!reload_completed && !reload_in_progress
4336 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
4337 emit_clobber (x);
4339 write_complex_part (x, read_complex_part (y, false), false, true);
4340 write_complex_part (x, read_complex_part (y, true), true, false);
4342 return get_last_insn ();
4345 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4346 MODE is known to be complex. Returns the last instruction emitted. */
4348 static rtx_insn *
4349 emit_move_complex (machine_mode mode, rtx x, rtx y)
4351 bool try_int;
4353 /* Need to take special care for pushes, to maintain proper ordering
4354 of the data, and possibly extra padding. */
4355 if (push_operand (x, mode))
4356 return emit_move_complex_push (mode, x, y);
4358 /* See if we can coerce the target into moving both values at once, except
4359 for floating point where we favor moving as parts if this is easy. */
4360 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4361 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
4362 && !(REG_P (x)
4363 && HARD_REGISTER_P (x)
4364 && REG_NREGS (x) == 1)
4365 && !(REG_P (y)
4366 && HARD_REGISTER_P (y)
4367 && REG_NREGS (y) == 1))
4368 try_int = false;
4369 /* Not possible if the values are inherently not adjacent. */
4370 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
4371 try_int = false;
4372 /* Is possible if both are registers (or subregs of registers). */
4373 else if (register_operand (x, mode) && register_operand (y, mode))
4374 try_int = true;
4375 /* If one of the operands is a memory, and alignment constraints
4376 are friendly enough, we may be able to do combined memory operations.
4377 We do not attempt this if Y is a constant because that combination is
4378 usually better with the by-parts thing below. */
4379 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4380 && (!STRICT_ALIGNMENT
4381 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4382 try_int = true;
4383 else
4384 try_int = false;
4386 if (try_int)
4388 rtx_insn *ret;
4390 /* For memory to memory moves, optimal behavior can be had with the
4391 existing block move logic. But use normal expansion if optimizing
4392 for size. */
4393 if (MEM_P (x) && MEM_P (y))
4395 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4396 (optimize_insn_for_speed_p()
4397 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4398 return get_last_insn ();
4401 ret = emit_move_via_integer (mode, x, y, true);
4402 if (ret)
4403 return ret;
4406 return emit_move_complex_parts (x, y);
4409 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4410 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4412 static rtx_insn *
4413 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4415 rtx_insn *ret;
4417 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4418 if (mode != CCmode)
4420 enum insn_code code = optab_handler (mov_optab, CCmode);
4421 if (code != CODE_FOR_nothing)
4423 x = emit_move_change_mode (CCmode, mode, x, true);
4424 y = emit_move_change_mode (CCmode, mode, y, true);
4425 return emit_insn (GEN_FCN (code) (x, y));
4429 /* Otherwise, find the MODE_INT mode of the same width. */
4430 ret = emit_move_via_integer (mode, x, y, false);
4431 gcc_assert (ret != NULL);
4432 return ret;
4435 /* Return true if word I of OP lies entirely in the
4436 undefined bits of a paradoxical subreg. */
4438 static bool
4439 undefined_operand_subword_p (const_rtx op, int i)
4441 if (GET_CODE (op) != SUBREG)
4442 return false;
4443 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4444 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4445 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4446 || known_le (offset, -UNITS_PER_WORD));
4449 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4450 MODE is any multi-word or full-word mode that lacks a move_insn
4451 pattern. Note that you will get better code if you define such
4452 patterns, even if they must turn into multiple assembler instructions. */
4454 static rtx_insn *
4455 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4457 rtx_insn *last_insn = 0;
4458 rtx_insn *seq;
4459 rtx inner;
4460 bool need_clobber;
4461 int i, mode_size;
4463 /* This function can only handle cases where the number of words is
4464 known at compile time. */
4465 mode_size = GET_MODE_SIZE (mode).to_constant ();
4466 gcc_assert (mode_size >= UNITS_PER_WORD);
4468 /* If X is a push on the stack, do the push now and replace
4469 X with a reference to the stack pointer. */
4470 if (push_operand (x, mode))
4471 x = emit_move_resolve_push (mode, x);
4473 /* If we are in reload, see if either operand is a MEM whose address
4474 is scheduled for replacement. */
4475 if (reload_in_progress && MEM_P (x)
4476 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4477 x = replace_equiv_address_nv (x, inner);
4478 if (reload_in_progress && MEM_P (y)
4479 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4480 y = replace_equiv_address_nv (y, inner);
4482 start_sequence ();
4484 need_clobber = false;
4485 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4487 /* Do not generate code for a move if it would go entirely
4488 to the non-existing bits of a paradoxical subreg. */
4489 if (undefined_operand_subword_p (x, i))
4490 continue;
4492 rtx xpart = operand_subword (x, i, 1, mode);
4493 rtx ypart;
4495 /* Do not generate code for a move if it would come entirely
4496 from the undefined bits of a paradoxical subreg. */
4497 if (undefined_operand_subword_p (y, i))
4498 continue;
4500 ypart = operand_subword (y, i, 1, mode);
4502 /* If we can't get a part of Y, put Y into memory if it is a
4503 constant. Otherwise, force it into a register. Then we must
4504 be able to get a part of Y. */
4505 if (ypart == 0 && CONSTANT_P (y))
4507 y = use_anchored_address (force_const_mem (mode, y));
4508 ypart = operand_subword (y, i, 1, mode);
4510 else if (ypart == 0)
4511 ypart = operand_subword_force (y, i, mode);
4513 gcc_assert (xpart && ypart);
4515 need_clobber |= (GET_CODE (xpart) == SUBREG);
4517 last_insn = emit_move_insn (xpart, ypart);
4520 seq = get_insns ();
4521 end_sequence ();
4523 /* Show the output dies here. This is necessary for SUBREGs
4524 of pseudos since we cannot track their lifetimes correctly;
4525 hard regs shouldn't appear here except as return values.
4526 We never want to emit such a clobber after reload. */
4527 if (x != y
4528 && ! (reload_in_progress || reload_completed)
4529 && need_clobber != 0)
4530 emit_clobber (x);
4532 emit_insn (seq);
4534 return last_insn;
4537 /* Low level part of emit_move_insn.
4538 Called just like emit_move_insn, but assumes X and Y
4539 are basically valid. */
4541 rtx_insn *
4542 emit_move_insn_1 (rtx x, rtx y)
4544 machine_mode mode = GET_MODE (x);
4545 enum insn_code code;
4547 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4549 code = optab_handler (mov_optab, mode);
4550 if (code != CODE_FOR_nothing)
4551 return emit_insn (GEN_FCN (code) (x, y));
4553 /* Expand complex moves by moving real part and imag part. */
4554 if (COMPLEX_MODE_P (mode))
4555 return emit_move_complex (mode, x, y);
4557 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4558 || ALL_FIXED_POINT_MODE_P (mode))
4560 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4562 /* If we can't find an integer mode, use multi words. */
4563 if (result)
4564 return result;
4565 else
4566 return emit_move_multi_word (mode, x, y);
4569 if (GET_MODE_CLASS (mode) == MODE_CC)
4570 return emit_move_ccmode (mode, x, y);
4572 /* Try using a move pattern for the corresponding integer mode. This is
4573 only safe when simplify_subreg can convert MODE constants into integer
4574 constants. At present, it can only do this reliably if the value
4575 fits within a HOST_WIDE_INT. */
4576 if (!CONSTANT_P (y)
4577 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4579 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4581 if (ret)
4583 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4584 return ret;
4588 return emit_move_multi_word (mode, x, y);
4591 /* Generate code to copy Y into X.
4592 Both Y and X must have the same mode, except that
4593 Y can be a constant with VOIDmode.
4594 This mode cannot be BLKmode; use emit_block_move for that.
4596 Return the last instruction emitted. */
4598 rtx_insn *
4599 emit_move_insn (rtx x, rtx y)
4601 machine_mode mode = GET_MODE (x);
4602 rtx y_cst = NULL_RTX;
4603 rtx_insn *last_insn;
4604 rtx set;
4606 gcc_assert (mode != BLKmode
4607 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4609 /* If we have a copy that looks like one of the following patterns:
4610 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4611 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4612 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4613 (set (subreg:M1 (reg:M2 ...)) (constant C))
4614 where mode M1 is equal in size to M2, try to detect whether the
4615 mode change involves an implicit round trip through memory.
4616 If so, see if we can avoid that by removing the subregs and
4617 doing the move in mode M2 instead. */
4619 rtx x_inner = NULL_RTX;
4620 rtx y_inner = NULL_RTX;
4622 auto candidate_subreg_p = [&](rtx subreg) {
4623 return (REG_P (SUBREG_REG (subreg))
4624 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4625 GET_MODE_SIZE (GET_MODE (subreg)))
4626 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4627 != CODE_FOR_nothing);
4630 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4631 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4632 && !push_operand (mem, GET_MODE (mem))
4633 /* Not a candiate if innermode requires too much alignment. */
4634 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4635 || targetm.slow_unaligned_access (GET_MODE (mem),
4636 MEM_ALIGN (mem))
4637 || !targetm.slow_unaligned_access (innermode,
4638 MEM_ALIGN (mem))));
4641 if (SUBREG_P (x) && candidate_subreg_p (x))
4642 x_inner = SUBREG_REG (x);
4644 if (SUBREG_P (y) && candidate_subreg_p (y))
4645 y_inner = SUBREG_REG (y);
4647 if (x_inner != NULL_RTX
4648 && y_inner != NULL_RTX
4649 && GET_MODE (x_inner) == GET_MODE (y_inner)
4650 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4652 x = x_inner;
4653 y = y_inner;
4654 mode = GET_MODE (x_inner);
4656 else if (x_inner != NULL_RTX
4657 && MEM_P (y)
4658 && candidate_mem_p (GET_MODE (x_inner), y))
4660 x = x_inner;
4661 y = adjust_address (y, GET_MODE (x_inner), 0);
4662 mode = GET_MODE (x_inner);
4664 else if (y_inner != NULL_RTX
4665 && MEM_P (x)
4666 && candidate_mem_p (GET_MODE (y_inner), x))
4668 x = adjust_address (x, GET_MODE (y_inner), 0);
4669 y = y_inner;
4670 mode = GET_MODE (y_inner);
4672 else if (x_inner != NULL_RTX
4673 && CONSTANT_P (y)
4674 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4675 mode, ALL_REGS)
4676 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4678 x = x_inner;
4679 y = y_inner;
4680 mode = GET_MODE (x_inner);
4683 if (CONSTANT_P (y))
4685 if (optimize
4686 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4687 && (last_insn = compress_float_constant (x, y)))
4688 return last_insn;
4690 y_cst = y;
4692 if (!targetm.legitimate_constant_p (mode, y))
4694 y = force_const_mem (mode, y);
4696 /* If the target's cannot_force_const_mem prevented the spill,
4697 assume that the target's move expanders will also take care
4698 of the non-legitimate constant. */
4699 if (!y)
4700 y = y_cst;
4701 else
4702 y = use_anchored_address (y);
4706 /* If X or Y are memory references, verify that their addresses are valid
4707 for the machine. */
4708 if (MEM_P (x)
4709 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4710 MEM_ADDR_SPACE (x))
4711 && ! push_operand (x, GET_MODE (x))))
4712 x = validize_mem (x);
4714 if (MEM_P (y)
4715 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4716 MEM_ADDR_SPACE (y)))
4717 y = validize_mem (y);
4719 gcc_assert (mode != BLKmode);
4721 last_insn = emit_move_insn_1 (x, y);
4723 if (y_cst && REG_P (x)
4724 && (set = single_set (last_insn)) != NULL_RTX
4725 && SET_DEST (set) == x
4726 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4727 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4729 return last_insn;
4732 /* Generate the body of an instruction to copy Y into X.
4733 It may be a list of insns, if one insn isn't enough. */
4735 rtx_insn *
4736 gen_move_insn (rtx x, rtx y)
4738 rtx_insn *seq;
4740 start_sequence ();
4741 emit_move_insn_1 (x, y);
4742 seq = get_insns ();
4743 end_sequence ();
4744 return seq;
4747 /* If Y is representable exactly in a narrower mode, and the target can
4748 perform the extension directly from constant or memory, then emit the
4749 move as an extension. */
4751 static rtx_insn *
4752 compress_float_constant (rtx x, rtx y)
4754 machine_mode dstmode = GET_MODE (x);
4755 machine_mode orig_srcmode = GET_MODE (y);
4756 machine_mode srcmode;
4757 const REAL_VALUE_TYPE *r;
4758 int oldcost, newcost;
4759 bool speed = optimize_insn_for_speed_p ();
4761 r = CONST_DOUBLE_REAL_VALUE (y);
4763 if (targetm.legitimate_constant_p (dstmode, y))
4764 oldcost = set_src_cost (y, orig_srcmode, speed);
4765 else
4766 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4768 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4770 enum insn_code ic;
4771 rtx trunc_y;
4772 rtx_insn *last_insn;
4774 /* Skip if the target can't extend this way. */
4775 ic = can_extend_p (dstmode, srcmode, 0);
4776 if (ic == CODE_FOR_nothing)
4777 continue;
4779 /* Skip if the narrowed value isn't exact. */
4780 if (! exact_real_truncate (srcmode, r))
4781 continue;
4783 trunc_y = const_double_from_real_value (*r, srcmode);
4785 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4787 /* Skip if the target needs extra instructions to perform
4788 the extension. */
4789 if (!insn_operand_matches (ic, 1, trunc_y))
4790 continue;
4791 /* This is valid, but may not be cheaper than the original. */
4792 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4793 dstmode, speed);
4794 if (oldcost < newcost)
4795 continue;
4797 else if (float_extend_from_mem[dstmode][srcmode])
4799 trunc_y = force_const_mem (srcmode, trunc_y);
4800 /* This is valid, but may not be cheaper than the original. */
4801 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4802 dstmode, speed);
4803 if (oldcost < newcost)
4804 continue;
4805 trunc_y = validize_mem (trunc_y);
4807 else
4808 continue;
4810 /* For CSE's benefit, force the compressed constant pool entry
4811 into a new pseudo. This constant may be used in different modes,
4812 and if not, combine will put things back together for us. */
4813 trunc_y = force_reg (srcmode, trunc_y);
4815 /* If x is a hard register, perform the extension into a pseudo,
4816 so that e.g. stack realignment code is aware of it. */
4817 rtx target = x;
4818 if (REG_P (x) && HARD_REGISTER_P (x))
4819 target = gen_reg_rtx (dstmode);
4821 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4822 last_insn = get_last_insn ();
4824 if (REG_P (target))
4825 set_unique_reg_note (last_insn, REG_EQUAL, y);
4827 if (target != x)
4828 return emit_move_insn (x, target);
4829 return last_insn;
4832 return NULL;
4835 /* Pushing data onto the stack. */
4837 /* Push a block of length SIZE (perhaps variable)
4838 and return an rtx to address the beginning of the block.
4839 The value may be virtual_outgoing_args_rtx.
4841 EXTRA is the number of bytes of padding to push in addition to SIZE.
4842 BELOW nonzero means this padding comes at low addresses;
4843 otherwise, the padding comes at high addresses. */
4846 push_block (rtx size, poly_int64 extra, int below)
4848 rtx temp;
4850 size = convert_modes (Pmode, ptr_mode, size, 1);
4851 if (CONSTANT_P (size))
4852 anti_adjust_stack (plus_constant (Pmode, size, extra));
4853 else if (REG_P (size) && known_eq (extra, 0))
4854 anti_adjust_stack (size);
4855 else
4857 temp = copy_to_mode_reg (Pmode, size);
4858 if (maybe_ne (extra, 0))
4859 temp = expand_binop (Pmode, add_optab, temp,
4860 gen_int_mode (extra, Pmode),
4861 temp, 0, OPTAB_LIB_WIDEN);
4862 anti_adjust_stack (temp);
4865 if (STACK_GROWS_DOWNWARD)
4867 temp = virtual_outgoing_args_rtx;
4868 if (maybe_ne (extra, 0) && below)
4869 temp = plus_constant (Pmode, temp, extra);
4871 else
4873 poly_int64 csize;
4874 if (poly_int_rtx_p (size, &csize))
4875 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4876 -csize - (below ? 0 : extra));
4877 else if (maybe_ne (extra, 0) && !below)
4878 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4879 negate_rtx (Pmode, plus_constant (Pmode, size,
4880 extra)));
4881 else
4882 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4883 negate_rtx (Pmode, size));
4886 return memory_address (NARROWEST_INT_MODE, temp);
4889 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4891 static rtx
4892 mem_autoinc_base (rtx mem)
4894 if (MEM_P (mem))
4896 rtx addr = XEXP (mem, 0);
4897 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4898 return XEXP (addr, 0);
4900 return NULL;
4903 /* A utility routine used here, in reload, and in try_split. The insns
4904 after PREV up to and including LAST are known to adjust the stack,
4905 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4906 placing notes as appropriate. PREV may be NULL, indicating the
4907 entire insn sequence prior to LAST should be scanned.
4909 The set of allowed stack pointer modifications is small:
4910 (1) One or more auto-inc style memory references (aka pushes),
4911 (2) One or more addition/subtraction with the SP as destination,
4912 (3) A single move insn with the SP as destination,
4913 (4) A call_pop insn,
4914 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4916 Insns in the sequence that do not modify the SP are ignored,
4917 except for noreturn calls.
4919 The return value is the amount of adjustment that can be trivially
4920 verified, via immediate operand or auto-inc. If the adjustment
4921 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4923 poly_int64
4924 find_args_size_adjust (rtx_insn *insn)
4926 rtx dest, set, pat;
4927 int i;
4929 pat = PATTERN (insn);
4930 set = NULL;
4932 /* Look for a call_pop pattern. */
4933 if (CALL_P (insn))
4935 /* We have to allow non-call_pop patterns for the case
4936 of emit_single_push_insn of a TLS address. */
4937 if (GET_CODE (pat) != PARALLEL)
4938 return 0;
4940 /* All call_pop have a stack pointer adjust in the parallel.
4941 The call itself is always first, and the stack adjust is
4942 usually last, so search from the end. */
4943 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4945 set = XVECEXP (pat, 0, i);
4946 if (GET_CODE (set) != SET)
4947 continue;
4948 dest = SET_DEST (set);
4949 if (dest == stack_pointer_rtx)
4950 break;
4952 /* We'd better have found the stack pointer adjust. */
4953 if (i == 0)
4954 return 0;
4955 /* Fall through to process the extracted SET and DEST
4956 as if it was a standalone insn. */
4958 else if (GET_CODE (pat) == SET)
4959 set = pat;
4960 else if ((set = single_set (insn)) != NULL)
4962 else if (GET_CODE (pat) == PARALLEL)
4964 /* ??? Some older ports use a parallel with a stack adjust
4965 and a store for a PUSH_ROUNDING pattern, rather than a
4966 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4967 /* ??? See h8300 and m68k, pushqi1. */
4968 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4970 set = XVECEXP (pat, 0, i);
4971 if (GET_CODE (set) != SET)
4972 continue;
4973 dest = SET_DEST (set);
4974 if (dest == stack_pointer_rtx)
4975 break;
4977 /* We do not expect an auto-inc of the sp in the parallel. */
4978 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4979 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4980 != stack_pointer_rtx);
4982 if (i < 0)
4983 return 0;
4985 else
4986 return 0;
4988 dest = SET_DEST (set);
4990 /* Look for direct modifications of the stack pointer. */
4991 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4993 /* Look for a trivial adjustment, otherwise assume nothing. */
4994 /* Note that the SPU restore_stack_block pattern refers to
4995 the stack pointer in V4SImode. Consider that non-trivial. */
4996 poly_int64 offset;
4997 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4998 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4999 return offset;
5000 /* ??? Reload can generate no-op moves, which will be cleaned
5001 up later. Recognize it and continue searching. */
5002 else if (rtx_equal_p (dest, SET_SRC (set)))
5003 return 0;
5004 else
5005 return HOST_WIDE_INT_MIN;
5007 else
5009 rtx mem, addr;
5011 /* Otherwise only think about autoinc patterns. */
5012 if (mem_autoinc_base (dest) == stack_pointer_rtx)
5014 mem = dest;
5015 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
5016 != stack_pointer_rtx);
5018 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
5019 mem = SET_SRC (set);
5020 else
5021 return 0;
5023 addr = XEXP (mem, 0);
5024 switch (GET_CODE (addr))
5026 case PRE_INC:
5027 case POST_INC:
5028 return GET_MODE_SIZE (GET_MODE (mem));
5029 case PRE_DEC:
5030 case POST_DEC:
5031 return -GET_MODE_SIZE (GET_MODE (mem));
5032 case PRE_MODIFY:
5033 case POST_MODIFY:
5034 addr = XEXP (addr, 1);
5035 gcc_assert (GET_CODE (addr) == PLUS);
5036 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
5037 return rtx_to_poly_int64 (XEXP (addr, 1));
5038 default:
5039 gcc_unreachable ();
5044 poly_int64
5045 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
5046 poly_int64 end_args_size)
5048 poly_int64 args_size = end_args_size;
5049 bool saw_unknown = false;
5050 rtx_insn *insn;
5052 for (insn = last; insn != prev; insn = PREV_INSN (insn))
5054 if (!NONDEBUG_INSN_P (insn))
5055 continue;
5057 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
5058 a call argument containing a TLS address that itself requires
5059 a call to __tls_get_addr. The handling of stack_pointer_delta
5060 in emit_single_push_insn is supposed to ensure that any such
5061 notes are already correct. */
5062 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
5063 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
5065 poly_int64 this_delta = find_args_size_adjust (insn);
5066 if (known_eq (this_delta, 0))
5068 if (!CALL_P (insn)
5069 || ACCUMULATE_OUTGOING_ARGS
5070 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
5071 continue;
5074 gcc_assert (!saw_unknown);
5075 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
5076 saw_unknown = true;
5078 if (!note)
5079 add_args_size_note (insn, args_size);
5080 if (STACK_GROWS_DOWNWARD)
5081 this_delta = -poly_uint64 (this_delta);
5083 if (saw_unknown)
5084 args_size = HOST_WIDE_INT_MIN;
5085 else
5086 args_size -= this_delta;
5089 return args_size;
5092 #ifdef PUSH_ROUNDING
5093 /* Emit single push insn. */
5095 static void
5096 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
5098 rtx dest_addr;
5099 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
5100 rtx dest;
5101 enum insn_code icode;
5103 /* If there is push pattern, use it. Otherwise try old way of throwing
5104 MEM representing push operation to move expander. */
5105 icode = optab_handler (push_optab, mode);
5106 if (icode != CODE_FOR_nothing)
5108 class expand_operand ops[1];
5110 create_input_operand (&ops[0], x, mode);
5111 if (maybe_expand_insn (icode, 1, ops))
5112 return;
5114 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
5115 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
5116 /* If we are to pad downward, adjust the stack pointer first and
5117 then store X into the stack location using an offset. This is
5118 because emit_move_insn does not know how to pad; it does not have
5119 access to type. */
5120 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
5122 emit_move_insn (stack_pointer_rtx,
5123 expand_binop (Pmode,
5124 STACK_GROWS_DOWNWARD ? sub_optab
5125 : add_optab,
5126 stack_pointer_rtx,
5127 gen_int_mode (rounded_size, Pmode),
5128 NULL_RTX, 0, OPTAB_LIB_WIDEN));
5130 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
5131 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
5132 /* We have already decremented the stack pointer, so get the
5133 previous value. */
5134 offset += rounded_size;
5136 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
5137 /* We have already incremented the stack pointer, so get the
5138 previous value. */
5139 offset -= rounded_size;
5141 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
5143 else
5145 if (STACK_GROWS_DOWNWARD)
5146 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
5147 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
5148 else
5149 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
5150 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
5152 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
5155 dest = gen_rtx_MEM (mode, dest_addr);
5157 if (type != 0)
5159 set_mem_attributes (dest, type, 1);
5161 if (cfun->tail_call_marked)
5162 /* Function incoming arguments may overlap with sibling call
5163 outgoing arguments and we cannot allow reordering of reads
5164 from function arguments with stores to outgoing arguments
5165 of sibling calls. */
5166 set_mem_alias_set (dest, 0);
5168 emit_move_insn (dest, x);
5171 /* Emit and annotate a single push insn. */
5173 static void
5174 emit_single_push_insn (machine_mode mode, rtx x, tree type)
5176 poly_int64 delta, old_delta = stack_pointer_delta;
5177 rtx_insn *prev = get_last_insn ();
5178 rtx_insn *last;
5180 emit_single_push_insn_1 (mode, x, type);
5182 /* Adjust stack_pointer_delta to describe the situation after the push
5183 we just performed. Note that we must do this after the push rather
5184 than before the push in case calculating X needs pushes and pops of
5185 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
5186 for such pushes and pops must not include the effect of the future
5187 push of X. */
5188 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
5190 last = get_last_insn ();
5192 /* Notice the common case where we emitted exactly one insn. */
5193 if (PREV_INSN (last) == prev)
5195 add_args_size_note (last, stack_pointer_delta);
5196 return;
5199 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
5200 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
5201 || known_eq (delta, old_delta));
5203 #endif
5205 /* If reading SIZE bytes from X will end up reading from
5206 Y return the number of bytes that overlap. Return -1
5207 if there is no overlap or -2 if we can't determine
5208 (for example when X and Y have different base registers). */
5210 static int
5211 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
5213 rtx tmp = plus_constant (Pmode, x, size);
5214 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
5216 if (!CONST_INT_P (sub))
5217 return -2;
5219 HOST_WIDE_INT val = INTVAL (sub);
5221 return IN_RANGE (val, 1, size) ? val : -1;
5224 /* Generate code to push X onto the stack, assuming it has mode MODE and
5225 type TYPE.
5226 MODE is redundant except when X is a CONST_INT (since they don't
5227 carry mode info).
5228 SIZE is an rtx for the size of data to be copied (in bytes),
5229 needed only if X is BLKmode.
5230 Return true if successful. May return false if asked to push a
5231 partial argument during a sibcall optimization (as specified by
5232 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
5233 to not overlap.
5235 ALIGN (in bits) is maximum alignment we can assume.
5237 If PARTIAL and REG are both nonzero, then copy that many of the first
5238 bytes of X into registers starting with REG, and push the rest of X.
5239 The amount of space pushed is decreased by PARTIAL bytes.
5240 REG must be a hard register in this case.
5241 If REG is zero but PARTIAL is not, take any all others actions for an
5242 argument partially in registers, but do not actually load any
5243 registers.
5245 EXTRA is the amount in bytes of extra space to leave next to this arg.
5246 This is ignored if an argument block has already been allocated.
5248 On a machine that lacks real push insns, ARGS_ADDR is the address of
5249 the bottom of the argument block for this call. We use indexing off there
5250 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
5251 argument block has not been preallocated.
5253 ARGS_SO_FAR is the size of args previously pushed for this call.
5255 REG_PARM_STACK_SPACE is nonzero if functions require stack space
5256 for arguments passed in registers. If nonzero, it will be the number
5257 of bytes required. */
5259 bool
5260 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
5261 unsigned int align, int partial, rtx reg, poly_int64 extra,
5262 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
5263 rtx alignment_pad, bool sibcall_p)
5265 rtx xinner;
5266 pad_direction stack_direction
5267 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
5269 /* Decide where to pad the argument: PAD_DOWNWARD for below,
5270 PAD_UPWARD for above, or PAD_NONE for don't pad it.
5271 Default is below for small data on big-endian machines; else above. */
5272 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
5274 /* Invert direction if stack is post-decrement.
5275 FIXME: why? */
5276 if (STACK_PUSH_CODE == POST_DEC)
5277 if (where_pad != PAD_NONE)
5278 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
5280 xinner = x;
5282 int nregs = partial / UNITS_PER_WORD;
5283 rtx *tmp_regs = NULL;
5284 int overlapping = 0;
5286 if (mode == BLKmode
5287 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
5289 /* Copy a block into the stack, entirely or partially. */
5291 rtx temp;
5292 int used;
5293 int offset;
5294 int skip;
5296 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5297 used = partial - offset;
5299 if (mode != BLKmode)
5301 /* A value is to be stored in an insufficiently aligned
5302 stack slot; copy via a suitably aligned slot if
5303 necessary. */
5304 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
5305 if (!MEM_P (xinner))
5307 temp = assign_temp (type, 1, 1);
5308 emit_move_insn (temp, xinner);
5309 xinner = temp;
5313 gcc_assert (size);
5315 /* USED is now the # of bytes we need not copy to the stack
5316 because registers will take care of them. */
5318 if (partial != 0)
5319 xinner = adjust_address (xinner, BLKmode, used);
5321 /* If the partial register-part of the arg counts in its stack size,
5322 skip the part of stack space corresponding to the registers.
5323 Otherwise, start copying to the beginning of the stack space,
5324 by setting SKIP to 0. */
5325 skip = (reg_parm_stack_space == 0) ? 0 : used;
5327 #ifdef PUSH_ROUNDING
5328 /* NB: Let the backend known the number of bytes to push and
5329 decide if push insns should be generated. */
5330 unsigned int push_size;
5331 if (CONST_INT_P (size))
5332 push_size = INTVAL (size);
5333 else
5334 push_size = 0;
5336 /* Do it with several push insns if that doesn't take lots of insns
5337 and if there is no difficulty with push insns that skip bytes
5338 on the stack for alignment purposes. */
5339 if (args_addr == 0
5340 && targetm.calls.push_argument (push_size)
5341 && CONST_INT_P (size)
5342 && skip == 0
5343 && MEM_ALIGN (xinner) >= align
5344 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
5345 /* Here we avoid the case of a structure whose weak alignment
5346 forces many pushes of a small amount of data,
5347 and such small pushes do rounding that causes trouble. */
5348 && ((!targetm.slow_unaligned_access (word_mode, align))
5349 || align >= BIGGEST_ALIGNMENT
5350 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
5351 align / BITS_PER_UNIT))
5352 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
5354 /* Push padding now if padding above and stack grows down,
5355 or if padding below and stack grows up.
5356 But if space already allocated, this has already been done. */
5357 if (maybe_ne (extra, 0)
5358 && args_addr == 0
5359 && where_pad != PAD_NONE
5360 && where_pad != stack_direction)
5361 anti_adjust_stack (gen_int_mode (extra, Pmode));
5363 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
5364 RETURN_BEGIN);
5366 else
5367 #endif /* PUSH_ROUNDING */
5369 rtx target;
5371 /* Otherwise make space on the stack and copy the data
5372 to the address of that space. */
5374 /* Deduct words put into registers from the size we must copy. */
5375 if (partial != 0)
5377 if (CONST_INT_P (size))
5378 size = GEN_INT (INTVAL (size) - used);
5379 else
5380 size = expand_binop (GET_MODE (size), sub_optab, size,
5381 gen_int_mode (used, GET_MODE (size)),
5382 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5385 /* Get the address of the stack space.
5386 In this case, we do not deal with EXTRA separately.
5387 A single stack adjust will do. */
5388 poly_int64 const_args_so_far;
5389 if (! args_addr)
5391 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5392 extra = 0;
5394 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5395 temp = memory_address (BLKmode,
5396 plus_constant (Pmode, args_addr,
5397 skip + const_args_so_far));
5398 else
5399 temp = memory_address (BLKmode,
5400 plus_constant (Pmode,
5401 gen_rtx_PLUS (Pmode,
5402 args_addr,
5403 args_so_far),
5404 skip));
5406 if (!ACCUMULATE_OUTGOING_ARGS)
5408 /* If the source is referenced relative to the stack pointer,
5409 copy it to another register to stabilize it. We do not need
5410 to do this if we know that we won't be changing sp. */
5412 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5413 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5414 temp = copy_to_reg (temp);
5417 target = gen_rtx_MEM (BLKmode, temp);
5419 /* We do *not* set_mem_attributes here, because incoming arguments
5420 may overlap with sibling call outgoing arguments and we cannot
5421 allow reordering of reads from function arguments with stores
5422 to outgoing arguments of sibling calls. We do, however, want
5423 to record the alignment of the stack slot. */
5424 /* ALIGN may well be better aligned than TYPE, e.g. due to
5425 PARM_BOUNDARY. Assume the caller isn't lying. */
5426 set_mem_align (target, align);
5428 /* If part should go in registers and pushing to that part would
5429 overwrite some of the values that need to go into regs, load the
5430 overlapping values into temporary pseudos to be moved into the hard
5431 regs at the end after the stack pushing has completed.
5432 We cannot load them directly into the hard regs here because
5433 they can be clobbered by the block move expansions.
5434 See PR 65358. */
5436 if (partial > 0 && reg != 0 && mode == BLKmode
5437 && GET_CODE (reg) != PARALLEL)
5439 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5440 if (overlapping > 0)
5442 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5443 overlapping /= UNITS_PER_WORD;
5445 tmp_regs = XALLOCAVEC (rtx, overlapping);
5447 for (int i = 0; i < overlapping; i++)
5448 tmp_regs[i] = gen_reg_rtx (word_mode);
5450 for (int i = 0; i < overlapping; i++)
5451 emit_move_insn (tmp_regs[i],
5452 operand_subword_force (target, i, mode));
5454 else if (overlapping == -1)
5455 overlapping = 0;
5456 /* Could not determine whether there is overlap.
5457 Fail the sibcall. */
5458 else
5460 overlapping = 0;
5461 if (sibcall_p)
5462 return false;
5466 /* If source is a constant VAR_DECL with a simple constructor,
5467 store the constructor to the stack instead of moving it. */
5468 const_tree decl;
5469 if (partial == 0
5470 && MEM_P (xinner)
5471 && SYMBOL_REF_P (XEXP (xinner, 0))
5472 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5473 && VAR_P (decl)
5474 && TREE_READONLY (decl)
5475 && !TREE_SIDE_EFFECTS (decl)
5476 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5477 store_constructor (DECL_INITIAL (decl), target, 0,
5478 int_expr_size (DECL_INITIAL (decl)), false);
5479 else
5480 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5483 else if (partial > 0)
5485 /* Scalar partly in registers. This case is only supported
5486 for fixed-wdth modes. */
5487 int num_words = GET_MODE_SIZE (mode).to_constant ();
5488 num_words /= UNITS_PER_WORD;
5489 int i;
5490 int not_stack;
5491 /* # bytes of start of argument
5492 that we must make space for but need not store. */
5493 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5494 int args_offset = INTVAL (args_so_far);
5495 int skip;
5497 /* Push padding now if padding above and stack grows down,
5498 or if padding below and stack grows up.
5499 But if space already allocated, this has already been done. */
5500 if (maybe_ne (extra, 0)
5501 && args_addr == 0
5502 && where_pad != PAD_NONE
5503 && where_pad != stack_direction)
5504 anti_adjust_stack (gen_int_mode (extra, Pmode));
5506 /* If we make space by pushing it, we might as well push
5507 the real data. Otherwise, we can leave OFFSET nonzero
5508 and leave the space uninitialized. */
5509 if (args_addr == 0)
5510 offset = 0;
5512 /* Now NOT_STACK gets the number of words that we don't need to
5513 allocate on the stack. Convert OFFSET to words too. */
5514 not_stack = (partial - offset) / UNITS_PER_WORD;
5515 offset /= UNITS_PER_WORD;
5517 /* If the partial register-part of the arg counts in its stack size,
5518 skip the part of stack space corresponding to the registers.
5519 Otherwise, start copying to the beginning of the stack space,
5520 by setting SKIP to 0. */
5521 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5523 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5524 x = validize_mem (force_const_mem (mode, x));
5526 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5527 SUBREGs of such registers are not allowed. */
5528 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5529 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5530 x = copy_to_reg (x);
5532 /* Loop over all the words allocated on the stack for this arg. */
5533 /* We can do it by words, because any scalar bigger than a word
5534 has a size a multiple of a word. */
5535 tree word_mode_type = lang_hooks.types.type_for_mode (word_mode, 1);
5536 for (i = num_words - 1; i >= not_stack; i--)
5537 if (i >= not_stack + offset)
5538 if (!emit_push_insn (operand_subword_force (x, i, mode),
5539 word_mode, word_mode_type, NULL_RTX, align, 0,
5540 NULL_RTX, 0, args_addr,
5541 GEN_INT (args_offset + ((i - not_stack + skip)
5542 * UNITS_PER_WORD)),
5543 reg_parm_stack_space, alignment_pad, sibcall_p))
5544 return false;
5546 else
5548 rtx addr;
5549 rtx dest;
5551 /* Push padding now if padding above and stack grows down,
5552 or if padding below and stack grows up.
5553 But if space already allocated, this has already been done. */
5554 if (maybe_ne (extra, 0)
5555 && args_addr == 0
5556 && where_pad != PAD_NONE
5557 && where_pad != stack_direction)
5558 anti_adjust_stack (gen_int_mode (extra, Pmode));
5560 #ifdef PUSH_ROUNDING
5561 if (args_addr == 0 && targetm.calls.push_argument (0))
5562 emit_single_push_insn (mode, x, type);
5563 else
5564 #endif
5566 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5567 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5569 /* We do *not* set_mem_attributes here, because incoming arguments
5570 may overlap with sibling call outgoing arguments and we cannot
5571 allow reordering of reads from function arguments with stores
5572 to outgoing arguments of sibling calls. We do, however, want
5573 to record the alignment of the stack slot. */
5574 /* ALIGN may well be better aligned than TYPE, e.g. due to
5575 PARM_BOUNDARY. Assume the caller isn't lying. */
5576 set_mem_align (dest, align);
5578 emit_move_insn (dest, x);
5582 /* Move the partial arguments into the registers and any overlapping
5583 values that we moved into the pseudos in tmp_regs. */
5584 if (partial > 0 && reg != 0)
5586 /* Handle calls that pass values in multiple non-contiguous locations.
5587 The Irix 6 ABI has examples of this. */
5588 if (GET_CODE (reg) == PARALLEL)
5589 emit_group_load (reg, x, type, -1);
5590 else
5592 gcc_assert (partial % UNITS_PER_WORD == 0);
5593 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5595 for (int i = 0; i < overlapping; i++)
5596 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5597 + nregs - overlapping + i),
5598 tmp_regs[i]);
5603 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5604 anti_adjust_stack (gen_int_mode (extra, Pmode));
5606 if (alignment_pad && args_addr == 0)
5607 anti_adjust_stack (alignment_pad);
5609 return true;
5612 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5613 operations. */
5615 static rtx
5616 get_subtarget (rtx x)
5618 return (optimize
5619 || x == 0
5620 /* Only registers can be subtargets. */
5621 || !REG_P (x)
5622 /* Don't use hard regs to avoid extending their life. */
5623 || REGNO (x) < FIRST_PSEUDO_REGISTER
5624 ? 0 : x);
5627 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5628 FIELD is a bitfield. Returns true if the optimization was successful,
5629 and there's nothing else to do. */
5631 static bool
5632 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5633 poly_uint64 pbitpos,
5634 poly_uint64 pbitregion_start,
5635 poly_uint64 pbitregion_end,
5636 machine_mode mode1, rtx str_rtx,
5637 tree to, tree src, bool reverse)
5639 /* str_mode is not guaranteed to be a scalar type. */
5640 machine_mode str_mode = GET_MODE (str_rtx);
5641 unsigned int str_bitsize;
5642 tree op0, op1;
5643 rtx value, result;
5644 optab binop;
5645 gimple *srcstmt;
5646 enum tree_code code;
5648 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5649 if (mode1 != VOIDmode
5650 || !pbitsize.is_constant (&bitsize)
5651 || !pbitpos.is_constant (&bitpos)
5652 || !pbitregion_start.is_constant (&bitregion_start)
5653 || !pbitregion_end.is_constant (&bitregion_end)
5654 || bitsize >= BITS_PER_WORD
5655 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5656 || str_bitsize > BITS_PER_WORD
5657 || TREE_SIDE_EFFECTS (to)
5658 || TREE_THIS_VOLATILE (to))
5659 return false;
5661 STRIP_NOPS (src);
5662 if (TREE_CODE (src) != SSA_NAME)
5663 return false;
5664 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5665 return false;
5667 srcstmt = get_gimple_for_ssa_name (src);
5668 if (!srcstmt
5669 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5670 return false;
5672 code = gimple_assign_rhs_code (srcstmt);
5674 op0 = gimple_assign_rhs1 (srcstmt);
5676 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5677 to find its initialization. Hopefully the initialization will
5678 be from a bitfield load. */
5679 if (TREE_CODE (op0) == SSA_NAME)
5681 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5683 /* We want to eventually have OP0 be the same as TO, which
5684 should be a bitfield. */
5685 if (!op0stmt
5686 || !is_gimple_assign (op0stmt)
5687 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5688 return false;
5689 op0 = gimple_assign_rhs1 (op0stmt);
5692 op1 = gimple_assign_rhs2 (srcstmt);
5694 if (!operand_equal_p (to, op0, 0))
5695 return false;
5697 if (MEM_P (str_rtx))
5699 unsigned HOST_WIDE_INT offset1;
5701 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5702 str_bitsize = BITS_PER_WORD;
5704 scalar_int_mode best_mode;
5705 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5706 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5707 return false;
5708 str_mode = best_mode;
5709 str_bitsize = GET_MODE_BITSIZE (best_mode);
5711 offset1 = bitpos;
5712 bitpos %= str_bitsize;
5713 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5714 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5716 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5717 return false;
5719 /* If the bit field covers the whole REG/MEM, store_field
5720 will likely generate better code. */
5721 if (bitsize >= str_bitsize)
5722 return false;
5724 /* We can't handle fields split across multiple entities. */
5725 if (bitpos + bitsize > str_bitsize)
5726 return false;
5728 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5729 bitpos = str_bitsize - bitpos - bitsize;
5731 switch (code)
5733 case PLUS_EXPR:
5734 case MINUS_EXPR:
5735 /* For now, just optimize the case of the topmost bitfield
5736 where we don't need to do any masking and also
5737 1 bit bitfields where xor can be used.
5738 We might win by one instruction for the other bitfields
5739 too if insv/extv instructions aren't used, so that
5740 can be added later. */
5741 if ((reverse || bitpos + bitsize != str_bitsize)
5742 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5743 break;
5745 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5746 value = convert_modes (str_mode,
5747 TYPE_MODE (TREE_TYPE (op1)), value,
5748 TYPE_UNSIGNED (TREE_TYPE (op1)));
5750 /* We may be accessing data outside the field, which means
5751 we can alias adjacent data. */
5752 if (MEM_P (str_rtx))
5754 str_rtx = shallow_copy_rtx (str_rtx);
5755 set_mem_alias_set (str_rtx, 0);
5756 set_mem_expr (str_rtx, 0);
5759 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5761 value = expand_and (str_mode, value, const1_rtx, NULL);
5762 binop = xor_optab;
5764 else
5765 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5767 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5768 if (reverse)
5769 value = flip_storage_order (str_mode, value);
5770 result = expand_binop (str_mode, binop, str_rtx,
5771 value, str_rtx, 1, OPTAB_WIDEN);
5772 if (result != str_rtx)
5773 emit_move_insn (str_rtx, result);
5774 return true;
5776 case BIT_IOR_EXPR:
5777 case BIT_XOR_EXPR:
5778 if (TREE_CODE (op1) != INTEGER_CST)
5779 break;
5780 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5781 value = convert_modes (str_mode,
5782 TYPE_MODE (TREE_TYPE (op1)), value,
5783 TYPE_UNSIGNED (TREE_TYPE (op1)));
5785 /* We may be accessing data outside the field, which means
5786 we can alias adjacent data. */
5787 if (MEM_P (str_rtx))
5789 str_rtx = shallow_copy_rtx (str_rtx);
5790 set_mem_alias_set (str_rtx, 0);
5791 set_mem_expr (str_rtx, 0);
5794 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5795 if (bitpos + bitsize != str_bitsize)
5797 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5798 str_mode);
5799 value = expand_and (str_mode, value, mask, NULL_RTX);
5801 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5802 if (reverse)
5803 value = flip_storage_order (str_mode, value);
5804 result = expand_binop (str_mode, binop, str_rtx,
5805 value, str_rtx, 1, OPTAB_WIDEN);
5806 if (result != str_rtx)
5807 emit_move_insn (str_rtx, result);
5808 return true;
5810 default:
5811 break;
5814 return false;
5817 /* In the C++ memory model, consecutive bit fields in a structure are
5818 considered one memory location.
5820 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5821 returns the bit range of consecutive bits in which this COMPONENT_REF
5822 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5823 and *OFFSET may be adjusted in the process.
5825 If the access does not need to be restricted, 0 is returned in both
5826 *BITSTART and *BITEND. */
5828 void
5829 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp,
5830 poly_int64 *bitpos, tree *offset)
5832 poly_int64 bitoffset;
5833 tree field, repr;
5835 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5837 field = TREE_OPERAND (exp, 1);
5838 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5839 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5840 need to limit the range we can access. */
5841 if (!repr)
5843 *bitstart = *bitend = 0;
5844 return;
5847 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5848 part of a larger bit field, then the representative does not serve any
5849 useful purpose. This can occur in Ada. */
5850 if (handled_component_p (TREE_OPERAND (exp, 0)))
5852 machine_mode rmode;
5853 poly_int64 rbitsize, rbitpos;
5854 tree roffset;
5855 int unsignedp, reversep, volatilep = 0;
5856 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5857 &roffset, &rmode, &unsignedp, &reversep,
5858 &volatilep);
5859 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5861 *bitstart = *bitend = 0;
5862 return;
5866 /* Compute the adjustment to bitpos from the offset of the field
5867 relative to the representative. DECL_FIELD_OFFSET of field and
5868 repr are the same by construction if they are not constants,
5869 see finish_bitfield_layout. */
5870 poly_uint64 field_offset, repr_offset;
5871 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5872 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5873 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5874 else
5875 bitoffset = 0;
5876 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5877 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5879 /* If the adjustment is larger than bitpos, we would have a negative bit
5880 position for the lower bound and this may wreak havoc later. Adjust
5881 offset and bitpos to make the lower bound non-negative in that case. */
5882 if (maybe_gt (bitoffset, *bitpos))
5884 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5885 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5887 *bitpos += adjust_bits;
5888 if (*offset == NULL_TREE)
5889 *offset = size_int (-adjust_bytes);
5890 else
5891 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5892 *bitstart = 0;
5894 else
5895 *bitstart = *bitpos - bitoffset;
5897 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5900 /* Returns true if BASE is a DECL that does not reside in memory and
5901 has non-BLKmode. DECL_RTL must not be a MEM; if
5902 DECL_RTL was not set yet, return false. */
5904 bool
5905 non_mem_decl_p (tree base)
5907 if (!DECL_P (base)
5908 || TREE_ADDRESSABLE (base)
5909 || DECL_MODE (base) == BLKmode)
5910 return false;
5912 if (!DECL_RTL_SET_P (base))
5913 return false;
5915 return (!MEM_P (DECL_RTL (base)));
5918 /* Returns true if REF refers to an object that does not
5919 reside in memory and has non-BLKmode. */
5921 bool
5922 mem_ref_refers_to_non_mem_p (tree ref)
5924 tree base;
5926 if (TREE_CODE (ref) == MEM_REF
5927 || TREE_CODE (ref) == TARGET_MEM_REF)
5929 tree addr = TREE_OPERAND (ref, 0);
5931 if (TREE_CODE (addr) != ADDR_EXPR)
5932 return false;
5934 base = TREE_OPERAND (addr, 0);
5936 else
5937 base = ref;
5939 return non_mem_decl_p (base);
5942 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5943 is true, try generating a nontemporal store. */
5945 void
5946 expand_assignment (tree to, tree from, bool nontemporal)
5948 rtx to_rtx = 0;
5949 rtx result;
5950 machine_mode mode;
5951 unsigned int align;
5952 enum insn_code icode;
5954 /* Don't crash if the lhs of the assignment was erroneous. */
5955 if (TREE_CODE (to) == ERROR_MARK)
5957 expand_normal (from);
5958 return;
5961 /* Optimize away no-op moves without side-effects. */
5962 if (operand_equal_p (to, from, 0))
5963 return;
5965 /* Handle misaligned stores. */
5966 mode = TYPE_MODE (TREE_TYPE (to));
5967 if ((TREE_CODE (to) == MEM_REF
5968 || TREE_CODE (to) == TARGET_MEM_REF
5969 || DECL_P (to))
5970 && mode != BLKmode
5971 && !mem_ref_refers_to_non_mem_p (to)
5972 && ((align = get_object_alignment (to))
5973 < GET_MODE_ALIGNMENT (mode))
5974 && (((icode = optab_handler (movmisalign_optab, mode))
5975 != CODE_FOR_nothing)
5976 || targetm.slow_unaligned_access (mode, align)))
5978 rtx reg, mem;
5980 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5981 /* Handle PARALLEL. */
5982 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5983 reg = force_not_mem (reg);
5984 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5985 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5986 reg = flip_storage_order (mode, reg);
5988 if (icode != CODE_FOR_nothing)
5990 class expand_operand ops[2];
5992 create_fixed_operand (&ops[0], mem);
5993 create_input_operand (&ops[1], reg, mode);
5994 /* The movmisalign<mode> pattern cannot fail, else the assignment
5995 would silently be omitted. */
5996 expand_insn (icode, 2, ops);
5998 else
5999 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
6000 false, false);
6001 return;
6004 /* Assignment of a structure component needs special treatment
6005 if the structure component's rtx is not simply a MEM.
6006 Assignment of an array element at a constant index, and assignment of
6007 an array element in an unaligned packed structure field, has the same
6008 problem. Same for (partially) storing into a non-memory object. */
6009 if (handled_component_p (to)
6010 || (TREE_CODE (to) == MEM_REF
6011 && (REF_REVERSE_STORAGE_ORDER (to)
6012 || mem_ref_refers_to_non_mem_p (to)))
6013 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
6015 machine_mode mode1;
6016 poly_int64 bitsize, bitpos;
6017 poly_uint64 bitregion_start = 0;
6018 poly_uint64 bitregion_end = 0;
6019 tree offset;
6020 int unsignedp, reversep, volatilep = 0;
6021 tree tem;
6023 push_temp_slots ();
6024 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
6025 &unsignedp, &reversep, &volatilep);
6027 /* Make sure bitpos is not negative, it can wreak havoc later. */
6028 if (maybe_lt (bitpos, 0))
6030 gcc_assert (offset == NULL_TREE);
6031 offset = size_int (bits_to_bytes_round_down (bitpos));
6032 bitpos = num_trailing_bits (bitpos);
6035 if (TREE_CODE (to) == COMPONENT_REF
6036 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
6037 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
6038 /* The C++ memory model naturally applies to byte-aligned fields.
6039 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
6040 BITSIZE are not byte-aligned, there is no need to limit the range
6041 we can access. This can occur with packed structures in Ada. */
6042 else if (maybe_gt (bitsize, 0)
6043 && multiple_p (bitsize, BITS_PER_UNIT)
6044 && multiple_p (bitpos, BITS_PER_UNIT))
6046 bitregion_start = bitpos;
6047 bitregion_end = bitpos + bitsize - 1;
6050 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
6052 /* If the field has a mode, we want to access it in the
6053 field's mode, not the computed mode.
6054 If a MEM has VOIDmode (external with incomplete type),
6055 use BLKmode for it instead. */
6056 if (MEM_P (to_rtx))
6058 if (mode1 != VOIDmode)
6059 to_rtx = adjust_address (to_rtx, mode1, 0);
6060 else if (GET_MODE (to_rtx) == VOIDmode)
6061 to_rtx = adjust_address (to_rtx, BLKmode, 0);
6064 rtx stemp = NULL_RTX, old_to_rtx = NULL_RTX;
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 if (TREE_CODE (offset) == INTEGER_CST)
6076 expand_builtin_trap ();
6077 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
6079 /* Else spill for variable offset to the destination. We expect
6080 to run into this only for hard registers. */
6081 else
6083 gcc_assert (VAR_P (tem) && DECL_HARD_REGISTER (tem));
6084 stemp = assign_stack_temp (GET_MODE (to_rtx),
6085 GET_MODE_SIZE (GET_MODE (to_rtx)));
6086 emit_move_insn (stemp, to_rtx);
6087 old_to_rtx = to_rtx;
6088 to_rtx = stemp;
6092 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
6093 address_mode = get_address_mode (to_rtx);
6094 if (GET_MODE (offset_rtx) != address_mode)
6096 /* We cannot be sure that the RTL in offset_rtx is valid outside
6097 of a memory address context, so force it into a register
6098 before attempting to convert it to the desired mode. */
6099 offset_rtx = force_operand (offset_rtx, NULL_RTX);
6100 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6103 /* If we have an expression in OFFSET_RTX and a non-zero
6104 byte offset in BITPOS, adding the byte offset before the
6105 OFFSET_RTX results in better intermediate code, which makes
6106 later rtl optimization passes perform better.
6108 We prefer intermediate code like this:
6110 r124:DI=r123:DI+0x18
6111 [r124:DI]=r121:DI
6113 ... instead of ...
6115 r124:DI=r123:DI+0x10
6116 [r124:DI+0x8]=r121:DI
6118 This is only done for aligned data values, as these can
6119 be expected to result in single move instructions. */
6120 poly_int64 bytepos;
6121 if (mode1 != VOIDmode
6122 && maybe_ne (bitpos, 0)
6123 && maybe_gt (bitsize, 0)
6124 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6125 && multiple_p (bitpos, bitsize)
6126 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
6127 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
6129 to_rtx = adjust_address (to_rtx, mode1, bytepos);
6130 bitregion_start = 0;
6131 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
6132 bitregion_end -= bitpos;
6133 bitpos = 0;
6136 to_rtx = offset_address (to_rtx, offset_rtx,
6137 highest_pow2_factor_for_target (to,
6138 offset));
6141 /* No action is needed if the target is not a memory and the field
6142 lies completely outside that target. This can occur if the source
6143 code contains an out-of-bounds access to a small array. */
6144 if (!MEM_P (to_rtx)
6145 && GET_MODE (to_rtx) != BLKmode
6146 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
6148 expand_normal (from);
6149 result = NULL;
6151 /* Handle expand_expr of a complex value returning a CONCAT. */
6152 else if (GET_CODE (to_rtx) == CONCAT)
6154 machine_mode to_mode = GET_MODE (to_rtx);
6155 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
6156 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
6157 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
6158 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
6159 && known_eq (bitpos, 0)
6160 && known_eq (bitsize, mode_bitsize))
6161 result = store_expr (from, to_rtx, false, nontemporal, reversep);
6162 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
6163 && known_eq (bitsize, inner_bitsize)
6164 && (known_eq (bitpos, 0)
6165 || known_eq (bitpos, inner_bitsize)))
6166 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
6167 false, nontemporal, reversep);
6168 else if (known_le (bitpos + bitsize, inner_bitsize))
6169 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
6170 bitregion_start, bitregion_end,
6171 mode1, from, get_alias_set (to),
6172 nontemporal, reversep);
6173 else if (known_ge (bitpos, inner_bitsize))
6174 result = store_field (XEXP (to_rtx, 1), bitsize,
6175 bitpos - inner_bitsize,
6176 bitregion_start, bitregion_end,
6177 mode1, from, get_alias_set (to),
6178 nontemporal, reversep);
6179 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
6181 result = expand_normal (from);
6182 if (GET_CODE (result) == CONCAT)
6184 to_mode = GET_MODE_INNER (to_mode);
6185 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
6186 rtx from_real
6187 = simplify_gen_subreg (to_mode, XEXP (result, 0),
6188 from_mode, 0);
6189 rtx from_imag
6190 = simplify_gen_subreg (to_mode, XEXP (result, 1),
6191 from_mode, 0);
6192 if (!from_real || !from_imag)
6193 goto concat_store_slow;
6194 emit_move_insn (XEXP (to_rtx, 0), from_real);
6195 emit_move_insn (XEXP (to_rtx, 1), from_imag);
6197 else
6199 machine_mode from_mode
6200 = GET_MODE (result) == VOIDmode
6201 ? TYPE_MODE (TREE_TYPE (from))
6202 : GET_MODE (result);
6203 rtx from_rtx;
6204 if (MEM_P (result))
6205 from_rtx = change_address (result, to_mode, NULL_RTX);
6206 else
6207 from_rtx
6208 = simplify_gen_subreg (to_mode, result, from_mode, 0);
6209 if (from_rtx)
6211 emit_move_insn (XEXP (to_rtx, 0),
6212 read_complex_part (from_rtx, false));
6213 emit_move_insn (XEXP (to_rtx, 1),
6214 read_complex_part (from_rtx, true));
6216 else
6218 to_mode = GET_MODE_INNER (to_mode);
6219 rtx from_real
6220 = simplify_gen_subreg (to_mode, result, from_mode, 0);
6221 rtx from_imag
6222 = simplify_gen_subreg (to_mode, result, from_mode,
6223 GET_MODE_SIZE (to_mode));
6224 if (!from_real || !from_imag)
6225 goto concat_store_slow;
6226 emit_move_insn (XEXP (to_rtx, 0), from_real);
6227 emit_move_insn (XEXP (to_rtx, 1), from_imag);
6231 else
6233 concat_store_slow:;
6234 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6235 GET_MODE_SIZE (GET_MODE (to_rtx)));
6236 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
6237 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
6238 result = store_field (temp, bitsize, bitpos,
6239 bitregion_start, bitregion_end,
6240 mode1, from, get_alias_set (to),
6241 nontemporal, reversep);
6242 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
6243 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
6246 /* For calls to functions returning variable length structures, if TO_RTX
6247 is not a MEM, go through a MEM because we must not create temporaries
6248 of the VLA type. */
6249 else if (!MEM_P (to_rtx)
6250 && TREE_CODE (from) == CALL_EXPR
6251 && COMPLETE_TYPE_P (TREE_TYPE (from))
6252 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
6254 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6255 GET_MODE_SIZE (GET_MODE (to_rtx)));
6256 result = store_field (temp, bitsize, bitpos, bitregion_start,
6257 bitregion_end, mode1, from, get_alias_set (to),
6258 nontemporal, reversep);
6259 emit_move_insn (to_rtx, temp);
6261 else
6263 if (MEM_P (to_rtx))
6265 /* If the field is at offset zero, we could have been given the
6266 DECL_RTX of the parent struct. Don't munge it. */
6267 to_rtx = shallow_copy_rtx (to_rtx);
6268 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
6269 if (volatilep)
6270 MEM_VOLATILE_P (to_rtx) = 1;
6273 gcc_checking_assert (known_ge (bitpos, 0));
6274 if (optimize_bitfield_assignment_op (bitsize, bitpos,
6275 bitregion_start, bitregion_end,
6276 mode1, to_rtx, to, from,
6277 reversep))
6278 result = NULL;
6279 else if (SUBREG_P (to_rtx)
6280 && SUBREG_PROMOTED_VAR_P (to_rtx))
6282 /* If to_rtx is a promoted subreg, we need to zero or sign
6283 extend the value afterwards. */
6284 if (TREE_CODE (to) == MEM_REF
6285 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
6286 && !REF_REVERSE_STORAGE_ORDER (to)
6287 && known_eq (bitpos, 0)
6288 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
6289 result = store_expr (from, to_rtx, 0, nontemporal, false);
6290 /* Check if the field overlaps the MSB, requiring extension. */
6291 else if (maybe_eq (bitpos + bitsize,
6292 GET_MODE_BITSIZE (GET_MODE (to_rtx))))
6294 scalar_int_mode imode = subreg_unpromoted_mode (to_rtx);
6295 scalar_int_mode omode = subreg_promoted_mode (to_rtx);
6296 rtx to_rtx1 = lowpart_subreg (imode, SUBREG_REG (to_rtx),
6297 omode);
6298 result = store_field (to_rtx1, bitsize, bitpos,
6299 bitregion_start, bitregion_end,
6300 mode1, from, get_alias_set (to),
6301 nontemporal, reversep);
6302 /* If the target usually keeps IMODE appropriately
6303 extended in OMODE it's unsafe to refer to it using
6304 a SUBREG whilst this invariant doesn't hold. */
6305 if (targetm.mode_rep_extended (imode, omode) != UNKNOWN)
6306 to_rtx1 = simplify_gen_unary (TRUNCATE, imode,
6307 SUBREG_REG (to_rtx), omode);
6308 convert_move (SUBREG_REG (to_rtx), to_rtx1,
6309 SUBREG_PROMOTED_SIGN (to_rtx));
6311 else
6312 result = store_field (to_rtx, bitsize, bitpos,
6313 bitregion_start, bitregion_end,
6314 mode1, from, get_alias_set (to),
6315 nontemporal, reversep);
6317 else
6318 result = store_field (to_rtx, bitsize, bitpos,
6319 bitregion_start, bitregion_end,
6320 mode1, from, get_alias_set (to),
6321 nontemporal, reversep);
6322 /* Move the temporary storage back to the non-MEM_P. */
6323 if (stemp)
6324 emit_move_insn (old_to_rtx, stemp);
6327 if (result)
6328 preserve_temp_slots (result);
6329 pop_temp_slots ();
6330 return;
6333 /* If the rhs is a function call and its value is not an aggregate,
6334 call the function before we start to compute the lhs.
6335 This is needed for correct code for cases such as
6336 val = setjmp (buf) on machines where reference to val
6337 requires loading up part of an address in a separate insn.
6339 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
6340 since it might be a promoted variable where the zero- or sign- extension
6341 needs to be done. Handling this in the normal way is safe because no
6342 computation is done before the call. The same is true for SSA names. */
6343 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
6344 && COMPLETE_TYPE_P (TREE_TYPE (from))
6345 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
6346 && ! (((VAR_P (to)
6347 || TREE_CODE (to) == PARM_DECL
6348 || TREE_CODE (to) == RESULT_DECL)
6349 && REG_P (DECL_RTL (to)))
6350 || TREE_CODE (to) == SSA_NAME))
6352 rtx value;
6354 push_temp_slots ();
6355 value = expand_normal (from);
6357 if (to_rtx == 0)
6358 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6360 /* Handle calls that return values in multiple non-contiguous locations.
6361 The Irix 6 ABI has examples of this. */
6362 if (GET_CODE (to_rtx) == PARALLEL)
6364 if (GET_CODE (value) == PARALLEL)
6365 emit_group_move (to_rtx, value);
6366 else
6367 emit_group_load (to_rtx, value, TREE_TYPE (from),
6368 int_size_in_bytes (TREE_TYPE (from)));
6370 else if (GET_CODE (value) == PARALLEL)
6371 emit_group_store (to_rtx, value, TREE_TYPE (from),
6372 int_size_in_bytes (TREE_TYPE (from)));
6373 else if (GET_MODE (to_rtx) == BLKmode)
6375 /* Handle calls that return BLKmode values in registers. */
6376 if (REG_P (value))
6377 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
6378 else
6379 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
6381 else
6383 if (POINTER_TYPE_P (TREE_TYPE (to)))
6384 value = convert_memory_address_addr_space
6385 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
6386 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
6388 emit_move_insn (to_rtx, value);
6391 preserve_temp_slots (to_rtx);
6392 pop_temp_slots ();
6393 return;
6396 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
6397 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6399 /* Don't move directly into a return register. */
6400 if (TREE_CODE (to) == RESULT_DECL
6401 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
6403 rtx temp;
6405 push_temp_slots ();
6407 /* If the source is itself a return value, it still is in a pseudo at
6408 this point so we can move it back to the return register directly. */
6409 if (REG_P (to_rtx)
6410 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6411 && TREE_CODE (from) != CALL_EXPR)
6412 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6413 else
6414 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6416 /* Handle calls that return values in multiple non-contiguous locations.
6417 The Irix 6 ABI has examples of this. */
6418 if (GET_CODE (to_rtx) == PARALLEL)
6420 if (GET_CODE (temp) == PARALLEL)
6421 emit_group_move (to_rtx, temp);
6422 else
6423 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6424 int_size_in_bytes (TREE_TYPE (from)));
6426 else if (temp)
6427 emit_move_insn (to_rtx, temp);
6429 preserve_temp_slots (to_rtx);
6430 pop_temp_slots ();
6431 return;
6434 /* In case we are returning the contents of an object which overlaps
6435 the place the value is being stored, use a safe function when copying
6436 a value through a pointer into a structure value return block. */
6437 if (TREE_CODE (to) == RESULT_DECL
6438 && TREE_CODE (from) == INDIRECT_REF
6439 && ADDR_SPACE_GENERIC_P
6440 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6441 && refs_may_alias_p (to, from)
6442 && cfun->returns_struct
6443 && !cfun->returns_pcc_struct)
6445 rtx from_rtx, size;
6447 push_temp_slots ();
6448 size = expr_size (from);
6449 from_rtx = expand_normal (from);
6451 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6453 preserve_temp_slots (to_rtx);
6454 pop_temp_slots ();
6455 return;
6458 /* Compute FROM and store the value in the rtx we got. */
6460 push_temp_slots ();
6461 result = store_expr (from, to_rtx, 0, nontemporal, false);
6462 preserve_temp_slots (result);
6463 pop_temp_slots ();
6464 return;
6467 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6468 succeeded, false otherwise. */
6470 bool
6471 emit_storent_insn (rtx to, rtx from)
6473 class expand_operand ops[2];
6474 machine_mode mode = GET_MODE (to);
6475 enum insn_code code = optab_handler (storent_optab, mode);
6477 if (code == CODE_FOR_nothing)
6478 return false;
6480 create_fixed_operand (&ops[0], to);
6481 create_input_operand (&ops[1], from, mode);
6482 return maybe_expand_insn (code, 2, ops);
6485 /* Helper function for store_expr storing of STRING_CST. */
6487 static rtx
6488 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6489 fixed_size_mode mode)
6491 tree str = (tree) data;
6493 gcc_assert (offset >= 0);
6494 if (offset >= TREE_STRING_LENGTH (str))
6495 return const0_rtx;
6497 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6498 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6500 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6501 size_t l = TREE_STRING_LENGTH (str) - offset;
6502 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6503 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6504 return c_readstr (p, mode, false);
6507 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
6510 /* Generate code for computing expression EXP,
6511 and storing the value into TARGET.
6513 If the mode is BLKmode then we may return TARGET itself.
6514 It turns out that in BLKmode it doesn't cause a problem.
6515 because C has no operators that could combine two different
6516 assignments into the same BLKmode object with different values
6517 with no sequence point. Will other languages need this to
6518 be more thorough?
6520 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6521 stack, and block moves may need to be treated specially.
6523 If NONTEMPORAL is true, try using a nontemporal store instruction.
6525 If REVERSE is true, the store is to be done in reverse order. */
6528 store_expr (tree exp, rtx target, int call_param_p,
6529 bool nontemporal, bool reverse)
6531 rtx temp;
6532 rtx alt_rtl = NULL_RTX;
6533 location_t loc = curr_insn_location ();
6534 bool shortened_string_cst = false;
6536 if (VOID_TYPE_P (TREE_TYPE (exp)))
6538 /* C++ can generate ?: expressions with a throw expression in one
6539 branch and an rvalue in the other. Here, we resolve attempts to
6540 store the throw expression's nonexistent result. */
6541 gcc_assert (!call_param_p);
6542 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6543 return NULL_RTX;
6545 if (TREE_CODE (exp) == COMPOUND_EXPR)
6547 /* Perform first part of compound expression, then assign from second
6548 part. */
6549 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6550 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6551 return store_expr (TREE_OPERAND (exp, 1), target,
6552 call_param_p, nontemporal, reverse);
6554 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6556 /* For conditional expression, get safe form of the target. Then
6557 test the condition, doing the appropriate assignment on either
6558 side. This avoids the creation of unnecessary temporaries.
6559 For non-BLKmode, it is more efficient not to do this. */
6561 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6563 do_pending_stack_adjust ();
6564 NO_DEFER_POP;
6565 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6566 profile_probability::uninitialized ());
6567 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6568 nontemporal, reverse);
6569 emit_jump_insn (targetm.gen_jump (lab2));
6570 emit_barrier ();
6571 emit_label (lab1);
6572 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6573 nontemporal, reverse);
6574 emit_label (lab2);
6575 OK_DEFER_POP;
6577 return NULL_RTX;
6579 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6580 /* If this is a scalar in a register that is stored in a wider mode
6581 than the declared mode, compute the result into its declared mode
6582 and then convert to the wider mode. Our value is the computed
6583 expression. */
6585 rtx inner_target = 0;
6586 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6587 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6589 /* We can do the conversion inside EXP, which will often result
6590 in some optimizations. Do the conversion in two steps: first
6591 change the signedness, if needed, then the extend. But don't
6592 do this if the type of EXP is a subtype of something else
6593 since then the conversion might involve more than just
6594 converting modes. */
6595 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6596 && TREE_TYPE (TREE_TYPE (exp)) == 0
6597 && GET_MODE_PRECISION (outer_mode)
6598 == TYPE_PRECISION (TREE_TYPE (exp)))
6600 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6601 TYPE_UNSIGNED (TREE_TYPE (exp))))
6603 /* Some types, e.g. Fortran's logical*4, won't have a signed
6604 version, so use the mode instead. */
6605 tree ntype
6606 = (signed_or_unsigned_type_for
6607 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6608 if (ntype == NULL)
6609 ntype = lang_hooks.types.type_for_mode
6610 (TYPE_MODE (TREE_TYPE (exp)),
6611 SUBREG_PROMOTED_SIGN (target));
6613 exp = fold_convert_loc (loc, ntype, exp);
6616 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6617 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6618 exp);
6620 inner_target = SUBREG_REG (target);
6623 temp = expand_expr (exp, inner_target, VOIDmode,
6624 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6627 /* If TEMP is a VOIDmode constant, use convert_modes to make
6628 sure that we properly convert it. */
6629 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6631 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6632 temp, SUBREG_PROMOTED_SIGN (target));
6633 temp = convert_modes (inner_mode, outer_mode, temp,
6634 SUBREG_PROMOTED_SIGN (target));
6636 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6637 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6638 temp, SUBREG_PROMOTED_SIGN (target));
6640 convert_move (SUBREG_REG (target), temp,
6641 SUBREG_PROMOTED_SIGN (target));
6643 return NULL_RTX;
6645 else if ((TREE_CODE (exp) == STRING_CST
6646 || (TREE_CODE (exp) == MEM_REF
6647 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6648 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6649 == STRING_CST
6650 && integer_zerop (TREE_OPERAND (exp, 1))))
6651 && !nontemporal && !call_param_p
6652 && MEM_P (target))
6654 /* Optimize initialization of an array with a STRING_CST. */
6655 HOST_WIDE_INT exp_len, str_copy_len;
6656 rtx dest_mem;
6657 tree str = TREE_CODE (exp) == STRING_CST
6658 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6660 exp_len = int_expr_size (exp);
6661 if (exp_len <= 0)
6662 goto normal_expr;
6664 if (TREE_STRING_LENGTH (str) <= 0)
6665 goto normal_expr;
6667 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6668 MEM_ALIGN (target), false))
6670 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6671 MEM_ALIGN (target), false, RETURN_BEGIN);
6672 return NULL_RTX;
6675 str_copy_len = TREE_STRING_LENGTH (str);
6677 /* Trailing NUL bytes in EXP will be handled by the call to
6678 clear_storage, which is more efficient than copying them from
6679 the STRING_CST, so trim those from STR_COPY_LEN. */
6680 while (str_copy_len)
6682 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6683 break;
6684 str_copy_len--;
6687 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6689 str_copy_len += STORE_MAX_PIECES - 1;
6690 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6692 if (str_copy_len >= exp_len)
6693 goto normal_expr;
6695 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6696 (void *) str, MEM_ALIGN (target), false))
6697 goto normal_expr;
6699 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6700 (void *) str, MEM_ALIGN (target), false,
6701 RETURN_END);
6702 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6703 exp_len - str_copy_len),
6704 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6705 return NULL_RTX;
6707 else
6709 rtx tmp_target;
6711 normal_expr:
6712 /* If we want to use a nontemporal or a reverse order store, force the
6713 value into a register first. */
6714 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6715 tree rexp = exp;
6716 if (TREE_CODE (exp) == STRING_CST
6717 && tmp_target == target
6718 && GET_MODE (target) == BLKmode
6719 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6721 rtx size = expr_size (exp);
6722 if (CONST_INT_P (size)
6723 && size != const0_rtx
6724 && (UINTVAL (size)
6725 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6727 /* If the STRING_CST has much larger array type than
6728 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6729 it into the rodata section as the code later on will use
6730 memset zero for the remainder anyway. See PR95052. */
6731 tmp_target = NULL_RTX;
6732 rexp = copy_node (exp);
6733 tree index
6734 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6735 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6736 index);
6737 shortened_string_cst = true;
6740 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6741 (call_param_p
6742 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6743 &alt_rtl, false);
6744 if (shortened_string_cst)
6746 gcc_assert (MEM_P (temp));
6747 temp = change_address (temp, BLKmode, NULL_RTX);
6751 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6752 the same as that of TARGET, adjust the constant. This is needed, for
6753 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6754 only a word-sized value. */
6755 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6756 && TREE_CODE (exp) != ERROR_MARK
6757 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6759 gcc_assert (!shortened_string_cst);
6760 if (GET_MODE_CLASS (GET_MODE (target))
6761 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6762 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6763 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6765 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6766 TYPE_MODE (TREE_TYPE (exp)), 0);
6767 if (t)
6768 temp = t;
6770 if (GET_MODE (temp) == VOIDmode)
6771 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6772 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6775 /* If value was not generated in the target, store it there.
6776 Convert the value to TARGET's type first if necessary and emit the
6777 pending incrementations that have been queued when expanding EXP.
6778 Note that we cannot emit the whole queue blindly because this will
6779 effectively disable the POST_INC optimization later.
6781 If TEMP and TARGET compare equal according to rtx_equal_p, but
6782 one or both of them are volatile memory refs, we have to distinguish
6783 two cases:
6784 - expand_expr has used TARGET. In this case, we must not generate
6785 another copy. This can be detected by TARGET being equal according
6786 to == .
6787 - expand_expr has not used TARGET - that means that the source just
6788 happens to have the same RTX form. Since temp will have been created
6789 by expand_expr, it will compare unequal according to == .
6790 We must generate a copy in this case, to reach the correct number
6791 of volatile memory references. */
6793 if ((! rtx_equal_p (temp, target)
6794 || (temp != target && (side_effects_p (temp)
6795 || side_effects_p (target)
6796 || (MEM_P (temp)
6797 && !mems_same_for_tbaa_p (temp, target)))))
6798 && TREE_CODE (exp) != ERROR_MARK
6799 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6800 but TARGET is not valid memory reference, TEMP will differ
6801 from TARGET although it is really the same location. */
6802 && !(alt_rtl
6803 && rtx_equal_p (alt_rtl, target)
6804 && !side_effects_p (alt_rtl)
6805 && !side_effects_p (target))
6806 /* If there's nothing to copy, don't bother. Don't call
6807 expr_size unless necessary, because some front-ends (C++)
6808 expr_size-hook must not be given objects that are not
6809 supposed to be bit-copied or bit-initialized. */
6810 && expr_size (exp) != const0_rtx)
6812 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6814 gcc_assert (!shortened_string_cst);
6815 if (GET_MODE (target) == BLKmode)
6817 /* Handle calls that return BLKmode values in registers. */
6818 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6819 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6820 else
6821 store_bit_field (target,
6822 rtx_to_poly_int64 (expr_size (exp))
6823 * BITS_PER_UNIT,
6824 0, 0, 0, GET_MODE (temp), temp, reverse,
6825 false);
6827 else
6828 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6831 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6833 /* Handle copying a string constant into an array. The string
6834 constant may be shorter than the array. So copy just the string's
6835 actual length, and clear the rest. First get the size of the data
6836 type of the string, which is actually the size of the target. */
6837 rtx size = expr_size (exp);
6839 if (CONST_INT_P (size)
6840 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6841 emit_block_move (target, temp, size,
6842 (call_param_p
6843 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6844 else
6846 machine_mode pointer_mode
6847 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6848 machine_mode address_mode = get_address_mode (target);
6850 /* Compute the size of the data to copy from the string. */
6851 tree copy_size
6852 = size_binop_loc (loc, MIN_EXPR,
6853 make_tree (sizetype, size),
6854 size_int (TREE_STRING_LENGTH (exp)));
6855 rtx copy_size_rtx
6856 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6857 (call_param_p
6858 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6859 rtx_code_label *label = 0;
6861 /* Copy that much. */
6862 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6863 TYPE_UNSIGNED (sizetype));
6864 emit_block_move (target, temp, copy_size_rtx,
6865 (call_param_p
6866 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6868 /* Figure out how much is left in TARGET that we have to clear.
6869 Do all calculations in pointer_mode. */
6870 poly_int64 const_copy_size;
6871 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6873 size = plus_constant (address_mode, size, -const_copy_size);
6874 target = adjust_address (target, BLKmode, const_copy_size);
6876 else
6878 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6879 copy_size_rtx, NULL_RTX, 0,
6880 OPTAB_LIB_WIDEN);
6882 if (GET_MODE (copy_size_rtx) != address_mode)
6883 copy_size_rtx = convert_to_mode (address_mode,
6884 copy_size_rtx,
6885 TYPE_UNSIGNED (sizetype));
6887 target = offset_address (target, copy_size_rtx,
6888 highest_pow2_factor (copy_size));
6889 label = gen_label_rtx ();
6890 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6891 GET_MODE (size), 0, label);
6894 if (size != const0_rtx)
6895 clear_storage (target, size, BLOCK_OP_NORMAL);
6897 if (label)
6898 emit_label (label);
6901 else if (shortened_string_cst)
6902 gcc_unreachable ();
6903 /* Handle calls that return values in multiple non-contiguous locations.
6904 The Irix 6 ABI has examples of this. */
6905 else if (GET_CODE (target) == PARALLEL)
6907 if (GET_CODE (temp) == PARALLEL)
6908 emit_group_move (target, temp);
6909 else
6910 emit_group_load (target, temp, TREE_TYPE (exp),
6911 int_size_in_bytes (TREE_TYPE (exp)));
6913 else if (GET_CODE (temp) == PARALLEL)
6914 emit_group_store (target, temp, TREE_TYPE (exp),
6915 int_size_in_bytes (TREE_TYPE (exp)));
6916 else if (GET_MODE (temp) == BLKmode)
6917 emit_block_move (target, temp, expr_size (exp),
6918 (call_param_p
6919 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6920 /* If we emit a nontemporal store, there is nothing else to do. */
6921 else if (nontemporal && emit_storent_insn (target, temp))
6923 else
6925 if (reverse)
6926 temp = flip_storage_order (GET_MODE (target), temp);
6927 temp = force_operand (temp, target);
6928 if (temp != target)
6929 emit_move_insn (target, temp);
6932 else
6933 gcc_assert (!shortened_string_cst);
6935 return NULL_RTX;
6938 /* Return true if field F of structure TYPE is a flexible array. */
6940 static bool
6941 flexible_array_member_p (const_tree f, const_tree type)
6943 const_tree tf;
6945 tf = TREE_TYPE (f);
6946 return (DECL_CHAIN (f) == NULL
6947 && TREE_CODE (tf) == ARRAY_TYPE
6948 && TYPE_DOMAIN (tf)
6949 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6950 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6951 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6952 && int_size_in_bytes (type) >= 0);
6955 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6956 must have in order for it to completely initialize a value of type TYPE.
6957 Return -1 if the number isn't known.
6959 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6961 static HOST_WIDE_INT
6962 count_type_elements (const_tree type, bool for_ctor_p)
6964 switch (TREE_CODE (type))
6966 case ARRAY_TYPE:
6968 tree nelts;
6970 nelts = array_type_nelts (type);
6971 if (nelts && tree_fits_uhwi_p (nelts))
6973 unsigned HOST_WIDE_INT n;
6975 n = tree_to_uhwi (nelts) + 1;
6976 if (n == 0 || for_ctor_p)
6977 return n;
6978 else
6979 return n * count_type_elements (TREE_TYPE (type), false);
6981 return for_ctor_p ? -1 : 1;
6984 case RECORD_TYPE:
6986 unsigned HOST_WIDE_INT n;
6987 tree f;
6989 n = 0;
6990 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6991 if (TREE_CODE (f) == FIELD_DECL)
6993 if (!for_ctor_p)
6994 n += count_type_elements (TREE_TYPE (f), false);
6995 else if (!flexible_array_member_p (f, type))
6996 /* Don't count flexible arrays, which are not supposed
6997 to be initialized. */
6998 n += 1;
7001 return n;
7004 case UNION_TYPE:
7005 case QUAL_UNION_TYPE:
7007 tree f;
7008 HOST_WIDE_INT n, m;
7010 gcc_assert (!for_ctor_p);
7011 /* Estimate the number of scalars in each field and pick the
7012 maximum. Other estimates would do instead; the idea is simply
7013 to make sure that the estimate is not sensitive to the ordering
7014 of the fields. */
7015 n = 1;
7016 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7017 if (TREE_CODE (f) == FIELD_DECL)
7019 m = count_type_elements (TREE_TYPE (f), false);
7020 /* If the field doesn't span the whole union, add an extra
7021 scalar for the rest. */
7022 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
7023 TYPE_SIZE (type)) != 1)
7024 m++;
7025 if (n < m)
7026 n = m;
7028 return n;
7031 case COMPLEX_TYPE:
7032 return 2;
7034 case VECTOR_TYPE:
7036 unsigned HOST_WIDE_INT nelts;
7037 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
7038 return nelts;
7039 else
7040 return -1;
7043 case INTEGER_TYPE:
7044 case REAL_TYPE:
7045 case FIXED_POINT_TYPE:
7046 case ENUMERAL_TYPE:
7047 case BOOLEAN_TYPE:
7048 case POINTER_TYPE:
7049 case OFFSET_TYPE:
7050 case REFERENCE_TYPE:
7051 case NULLPTR_TYPE:
7052 case OPAQUE_TYPE:
7053 case BITINT_TYPE:
7054 return 1;
7056 case ERROR_MARK:
7057 return 0;
7059 case VOID_TYPE:
7060 case METHOD_TYPE:
7061 case FUNCTION_TYPE:
7062 case LANG_TYPE:
7063 default:
7064 gcc_unreachable ();
7068 /* Helper for categorize_ctor_elements. Identical interface. */
7070 static bool
7071 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7072 HOST_WIDE_INT *p_unique_nz_elts,
7073 HOST_WIDE_INT *p_init_elts, bool *p_complete)
7075 unsigned HOST_WIDE_INT idx;
7076 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
7077 tree value, purpose, elt_type;
7079 /* Whether CTOR is a valid constant initializer, in accordance with what
7080 initializer_constant_valid_p does. If inferred from the constructor
7081 elements, true until proven otherwise. */
7082 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
7083 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
7085 nz_elts = 0;
7086 unique_nz_elts = 0;
7087 init_elts = 0;
7088 num_fields = 0;
7089 elt_type = NULL_TREE;
7091 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
7093 HOST_WIDE_INT mult = 1;
7095 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
7097 tree lo_index = TREE_OPERAND (purpose, 0);
7098 tree hi_index = TREE_OPERAND (purpose, 1);
7100 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
7101 mult = (tree_to_uhwi (hi_index)
7102 - tree_to_uhwi (lo_index) + 1);
7104 num_fields += mult;
7105 elt_type = TREE_TYPE (value);
7107 switch (TREE_CODE (value))
7109 case CONSTRUCTOR:
7111 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
7113 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
7114 &ic, p_complete);
7116 nz_elts += mult * nz;
7117 unique_nz_elts += unz;
7118 init_elts += mult * ic;
7120 if (const_from_elts_p && const_p)
7121 const_p = const_elt_p;
7123 break;
7125 case INTEGER_CST:
7126 case REAL_CST:
7127 case FIXED_CST:
7128 if (!initializer_zerop (value))
7130 nz_elts += mult;
7131 unique_nz_elts++;
7133 init_elts += mult;
7134 break;
7136 case STRING_CST:
7137 nz_elts += mult * TREE_STRING_LENGTH (value);
7138 unique_nz_elts += TREE_STRING_LENGTH (value);
7139 init_elts += mult * TREE_STRING_LENGTH (value);
7140 break;
7142 case COMPLEX_CST:
7143 if (!initializer_zerop (TREE_REALPART (value)))
7145 nz_elts += mult;
7146 unique_nz_elts++;
7148 if (!initializer_zerop (TREE_IMAGPART (value)))
7150 nz_elts += mult;
7151 unique_nz_elts++;
7153 init_elts += 2 * mult;
7154 break;
7156 case VECTOR_CST:
7158 /* We can only construct constant-length vectors using
7159 CONSTRUCTOR. */
7160 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
7161 for (unsigned int i = 0; i < nunits; ++i)
7163 tree v = VECTOR_CST_ELT (value, i);
7164 if (!initializer_zerop (v))
7166 nz_elts += mult;
7167 unique_nz_elts++;
7169 init_elts += mult;
7172 break;
7174 default:
7176 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
7177 nz_elts += mult * tc;
7178 unique_nz_elts += tc;
7179 init_elts += mult * tc;
7181 if (const_from_elts_p && const_p)
7182 const_p
7183 = initializer_constant_valid_p (value,
7184 elt_type,
7185 TYPE_REVERSE_STORAGE_ORDER
7186 (TREE_TYPE (ctor)))
7187 != NULL_TREE;
7189 break;
7193 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
7194 num_fields, elt_type))
7195 *p_complete = false;
7197 *p_nz_elts += nz_elts;
7198 *p_unique_nz_elts += unique_nz_elts;
7199 *p_init_elts += init_elts;
7201 return const_p;
7204 /* Examine CTOR to discover:
7205 * how many scalar fields are set to nonzero values,
7206 and place it in *P_NZ_ELTS;
7207 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
7208 high - low + 1 (this can be useful for callers to determine ctors
7209 that could be cheaply initialized with - perhaps nested - loops
7210 compared to copied from huge read-only data),
7211 and place it in *P_UNIQUE_NZ_ELTS;
7212 * how many scalar fields in total are in CTOR,
7213 and place it in *P_ELT_COUNT.
7214 * whether the constructor is complete -- in the sense that every
7215 meaningful byte is explicitly given a value --
7216 and place it in *P_COMPLETE.
7218 Return whether or not CTOR is a valid static constant initializer, the same
7219 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
7221 bool
7222 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7223 HOST_WIDE_INT *p_unique_nz_elts,
7224 HOST_WIDE_INT *p_init_elts, bool *p_complete)
7226 *p_nz_elts = 0;
7227 *p_unique_nz_elts = 0;
7228 *p_init_elts = 0;
7229 *p_complete = true;
7231 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
7232 p_init_elts, p_complete);
7235 /* Return true if constructor CTOR is simple enough to be materialized
7236 in an integer mode register. Limit the size to WORDS words, which
7237 is 1 by default. */
7239 bool
7240 immediate_const_ctor_p (const_tree ctor, unsigned int words)
7242 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
7243 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
7244 return false;
7246 return TREE_CONSTANT (ctor)
7247 && !TREE_ADDRESSABLE (ctor)
7248 && CONSTRUCTOR_NELTS (ctor)
7249 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
7250 && int_expr_size (ctor) <= words * UNITS_PER_WORD
7251 && initializer_constant_valid_for_bitfield_p (ctor);
7254 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
7255 of which had type LAST_TYPE. Each element was itself a complete
7256 initializer, in the sense that every meaningful byte was explicitly
7257 given a value. Return true if the same is true for the constructor
7258 as a whole. */
7260 bool
7261 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
7262 const_tree last_type)
7264 if (TREE_CODE (type) == UNION_TYPE
7265 || TREE_CODE (type) == QUAL_UNION_TYPE)
7267 if (num_elts == 0)
7268 return false;
7270 gcc_assert (num_elts == 1 && last_type);
7272 /* ??? We could look at each element of the union, and find the
7273 largest element. Which would avoid comparing the size of the
7274 initialized element against any tail padding in the union.
7275 Doesn't seem worth the effort... */
7276 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
7279 return count_type_elements (type, true) == num_elts;
7282 /* Return true if EXP contains mostly (3/4) zeros. */
7284 static bool
7285 mostly_zeros_p (const_tree exp)
7287 if (TREE_CODE (exp) == CONSTRUCTOR)
7289 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7290 bool complete_p;
7292 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7293 &complete_p);
7294 return !complete_p || nz_elts < init_elts / 4;
7297 return initializer_zerop (exp);
7300 /* Return true if EXP contains all zeros. */
7302 static bool
7303 all_zeros_p (const_tree exp)
7305 if (TREE_CODE (exp) == CONSTRUCTOR)
7307 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7308 bool complete_p;
7310 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7311 &complete_p);
7312 return nz_elts == 0;
7315 return initializer_zerop (exp);
7318 /* Helper function for store_constructor.
7319 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
7320 CLEARED is as for store_constructor.
7321 ALIAS_SET is the alias set to use for any stores.
7322 If REVERSE is true, the store is to be done in reverse order.
7324 This provides a recursive shortcut back to store_constructor when it isn't
7325 necessary to go through store_field. This is so that we can pass through
7326 the cleared field to let store_constructor know that we may not have to
7327 clear a substructure if the outer structure has already been cleared. */
7329 static void
7330 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
7331 poly_uint64 bitregion_start,
7332 poly_uint64 bitregion_end,
7333 machine_mode mode,
7334 tree exp, int cleared,
7335 alias_set_type alias_set, bool reverse)
7337 poly_int64 bytepos;
7338 poly_uint64 bytesize;
7339 if (TREE_CODE (exp) == CONSTRUCTOR
7340 /* We can only call store_constructor recursively if the size and
7341 bit position are on a byte boundary. */
7342 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
7343 && maybe_ne (bitsize, 0U)
7344 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
7345 /* If we have a nonzero bitpos for a register target, then we just
7346 let store_field do the bitfield handling. This is unlikely to
7347 generate unnecessary clear instructions anyways. */
7348 && (known_eq (bitpos, 0) || MEM_P (target)))
7350 if (MEM_P (target))
7352 machine_mode target_mode = GET_MODE (target);
7353 if (target_mode != BLKmode
7354 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
7355 target_mode = BLKmode;
7356 target = adjust_address (target, target_mode, bytepos);
7360 /* Update the alias set, if required. */
7361 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
7362 && MEM_ALIAS_SET (target) != 0)
7364 target = copy_rtx (target);
7365 set_mem_alias_set (target, alias_set);
7368 store_constructor (exp, target, cleared, bytesize, reverse);
7370 else
7371 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
7372 exp, alias_set, false, reverse);
7376 /* Returns the number of FIELD_DECLs in TYPE. */
7378 static int
7379 fields_length (const_tree type)
7381 tree t = TYPE_FIELDS (type);
7382 int count = 0;
7384 for (; t; t = DECL_CHAIN (t))
7385 if (TREE_CODE (t) == FIELD_DECL)
7386 ++count;
7388 return count;
7392 /* Store the value of constructor EXP into the rtx TARGET.
7393 TARGET is either a REG or a MEM; we know it cannot conflict, since
7394 safe_from_p has been called.
7395 CLEARED is true if TARGET is known to have been zero'd.
7396 SIZE is the number of bytes of TARGET we are allowed to modify: this
7397 may not be the same as the size of EXP if we are assigning to a field
7398 which has been packed to exclude padding bits.
7399 If REVERSE is true, the store is to be done in reverse order. */
7401 void
7402 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
7403 bool reverse)
7405 tree type = TREE_TYPE (exp);
7406 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
7407 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
7409 switch (TREE_CODE (type))
7411 case RECORD_TYPE:
7412 case UNION_TYPE:
7413 case QUAL_UNION_TYPE:
7415 unsigned HOST_WIDE_INT idx;
7416 tree field, value;
7418 /* The storage order is specified for every aggregate type. */
7419 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7421 /* If size is zero or the target is already cleared, do nothing. */
7422 if (known_eq (size, 0) || cleared)
7423 cleared = 1;
7424 /* We either clear the aggregate or indicate the value is dead. */
7425 else if ((TREE_CODE (type) == UNION_TYPE
7426 || TREE_CODE (type) == QUAL_UNION_TYPE)
7427 && ! CONSTRUCTOR_ELTS (exp))
7428 /* If the constructor is empty, clear the union. */
7430 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7431 cleared = 1;
7434 /* If we are building a static constructor into a register,
7435 set the initial value as zero so we can fold the value into
7436 a constant. But if more than one register is involved,
7437 this probably loses. */
7438 else if (REG_P (target) && TREE_STATIC (exp)
7439 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7440 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7442 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7443 cleared = 1;
7446 /* If the constructor has fewer fields than the structure or
7447 if we are initializing the structure to mostly zeros, clear
7448 the whole structure first. Don't do this if TARGET is a
7449 register whose mode size isn't equal to SIZE since
7450 clear_storage can't handle this case. */
7451 else if (known_size_p (size)
7452 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7453 || mostly_zeros_p (exp))
7454 && (!REG_P (target)
7455 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7457 clear_storage (target, gen_int_mode (size, Pmode),
7458 BLOCK_OP_NORMAL);
7459 cleared = 1;
7462 if (REG_P (target) && !cleared)
7463 emit_clobber (target);
7465 /* Store each element of the constructor into the
7466 corresponding field of TARGET. */
7467 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7469 machine_mode mode;
7470 HOST_WIDE_INT bitsize;
7471 HOST_WIDE_INT bitpos = 0;
7472 tree offset;
7473 rtx to_rtx = target;
7475 /* Just ignore missing fields. We cleared the whole
7476 structure, above, if any fields are missing. */
7477 if (field == 0)
7478 continue;
7480 if (cleared && initializer_zerop (value))
7481 continue;
7483 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7484 bitsize = tree_to_uhwi (DECL_SIZE (field));
7485 else
7486 gcc_unreachable ();
7488 mode = DECL_MODE (field);
7489 if (DECL_BIT_FIELD (field))
7490 mode = VOIDmode;
7492 offset = DECL_FIELD_OFFSET (field);
7493 if (tree_fits_shwi_p (offset)
7494 && tree_fits_shwi_p (bit_position (field)))
7496 bitpos = int_bit_position (field);
7497 offset = NULL_TREE;
7499 else
7500 gcc_unreachable ();
7502 /* If this initializes a field that is smaller than a
7503 word, at the start of a word, try to widen it to a full
7504 word. This special case allows us to output C++ member
7505 function initializations in a form that the optimizers
7506 can understand. */
7507 if (WORD_REGISTER_OPERATIONS
7508 && REG_P (target)
7509 && bitsize < BITS_PER_WORD
7510 && bitpos % BITS_PER_WORD == 0
7511 && GET_MODE_CLASS (mode) == MODE_INT
7512 && TREE_CODE (value) == INTEGER_CST
7513 && exp_size >= 0
7514 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7516 type = TREE_TYPE (value);
7518 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7520 type = lang_hooks.types.type_for_mode
7521 (word_mode, TYPE_UNSIGNED (type));
7522 value = fold_convert (type, value);
7523 /* Make sure the bits beyond the original bitsize are zero
7524 so that we can correctly avoid extra zeroing stores in
7525 later constructor elements. */
7526 tree bitsize_mask
7527 = wide_int_to_tree (type, wi::mask (bitsize, false,
7528 BITS_PER_WORD));
7529 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7532 if (BYTES_BIG_ENDIAN)
7533 value
7534 = fold_build2 (LSHIFT_EXPR, type, value,
7535 build_int_cst (type,
7536 BITS_PER_WORD - bitsize));
7537 bitsize = BITS_PER_WORD;
7538 mode = word_mode;
7541 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7542 && DECL_NONADDRESSABLE_P (field))
7544 to_rtx = copy_rtx (to_rtx);
7545 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7548 store_constructor_field (to_rtx, bitsize, bitpos,
7549 0, bitregion_end, mode,
7550 value, cleared,
7551 get_alias_set (TREE_TYPE (field)),
7552 reverse);
7554 break;
7556 case ARRAY_TYPE:
7558 tree value, index;
7559 unsigned HOST_WIDE_INT i;
7560 bool need_to_clear;
7561 tree domain;
7562 tree elttype = TREE_TYPE (type);
7563 bool const_bounds_p;
7564 HOST_WIDE_INT minelt = 0;
7565 HOST_WIDE_INT maxelt = 0;
7567 /* The storage order is specified for every aggregate type. */
7568 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7570 domain = TYPE_DOMAIN (type);
7571 const_bounds_p = (TYPE_MIN_VALUE (domain)
7572 && TYPE_MAX_VALUE (domain)
7573 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7574 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7576 /* If we have constant bounds for the range of the type, get them. */
7577 if (const_bounds_p)
7579 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7580 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7583 /* If the constructor has fewer elements than the array, clear
7584 the whole array first. Similarly if this is static
7585 constructor of a non-BLKmode object. */
7586 if (cleared)
7587 need_to_clear = false;
7588 else if (REG_P (target) && TREE_STATIC (exp))
7589 need_to_clear = true;
7590 else
7592 unsigned HOST_WIDE_INT idx;
7593 HOST_WIDE_INT count = 0, zero_count = 0;
7594 need_to_clear = ! const_bounds_p;
7596 /* This loop is a more accurate version of the loop in
7597 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7598 is also needed to check for missing elements. */
7599 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7601 HOST_WIDE_INT this_node_count;
7603 if (need_to_clear)
7604 break;
7606 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7608 tree lo_index = TREE_OPERAND (index, 0);
7609 tree hi_index = TREE_OPERAND (index, 1);
7611 if (! tree_fits_uhwi_p (lo_index)
7612 || ! tree_fits_uhwi_p (hi_index))
7614 need_to_clear = true;
7615 break;
7618 this_node_count = (tree_to_uhwi (hi_index)
7619 - tree_to_uhwi (lo_index) + 1);
7621 else
7622 this_node_count = 1;
7624 count += this_node_count;
7625 if (mostly_zeros_p (value))
7626 zero_count += this_node_count;
7629 /* Clear the entire array first if there are any missing
7630 elements, or if the incidence of zero elements is >=
7631 75%. */
7632 if (! need_to_clear
7633 && (count < maxelt - minelt + 1
7634 || 4 * zero_count >= 3 * count))
7635 need_to_clear = true;
7638 if (need_to_clear && maybe_gt (size, 0))
7640 if (REG_P (target))
7641 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7642 else
7643 clear_storage (target, gen_int_mode (size, Pmode),
7644 BLOCK_OP_NORMAL);
7645 cleared = 1;
7648 if (!cleared && REG_P (target))
7649 /* Inform later passes that the old value is dead. */
7650 emit_clobber (target);
7652 /* Store each element of the constructor into the
7653 corresponding element of TARGET, determined by counting the
7654 elements. */
7655 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7657 machine_mode mode;
7658 poly_int64 bitsize;
7659 HOST_WIDE_INT bitpos;
7660 rtx xtarget = target;
7662 if (cleared && initializer_zerop (value))
7663 continue;
7665 mode = TYPE_MODE (elttype);
7666 if (mode != BLKmode)
7667 bitsize = GET_MODE_BITSIZE (mode);
7668 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7669 bitsize = -1;
7671 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7673 tree lo_index = TREE_OPERAND (index, 0);
7674 tree hi_index = TREE_OPERAND (index, 1);
7675 rtx index_r, pos_rtx;
7676 HOST_WIDE_INT lo, hi, count;
7677 tree position;
7679 /* If the range is constant and "small", unroll the loop. */
7680 if (const_bounds_p
7681 && tree_fits_shwi_p (lo_index)
7682 && tree_fits_shwi_p (hi_index)
7683 && (lo = tree_to_shwi (lo_index),
7684 hi = tree_to_shwi (hi_index),
7685 count = hi - lo + 1,
7686 (!MEM_P (target)
7687 || count <= 2
7688 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7689 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7690 <= 40 * 8)))))
7692 lo -= minelt; hi -= minelt;
7693 for (; lo <= hi; lo++)
7695 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7697 if (MEM_P (target)
7698 && !MEM_KEEP_ALIAS_SET_P (target)
7699 && TREE_CODE (type) == ARRAY_TYPE
7700 && TYPE_NONALIASED_COMPONENT (type))
7702 target = copy_rtx (target);
7703 MEM_KEEP_ALIAS_SET_P (target) = 1;
7706 store_constructor_field
7707 (target, bitsize, bitpos, 0, bitregion_end,
7708 mode, value, cleared,
7709 get_alias_set (elttype), reverse);
7712 else
7714 rtx_code_label *loop_start = gen_label_rtx ();
7715 rtx_code_label *loop_end = gen_label_rtx ();
7716 tree exit_cond;
7718 expand_normal (hi_index);
7720 index = build_decl (EXPR_LOCATION (exp),
7721 VAR_DECL, NULL_TREE, domain);
7722 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7723 SET_DECL_RTL (index, index_r);
7724 store_expr (lo_index, index_r, 0, false, reverse);
7726 /* Build the head of the loop. */
7727 do_pending_stack_adjust ();
7728 emit_label (loop_start);
7730 /* Assign value to element index. */
7731 position =
7732 fold_convert (ssizetype,
7733 fold_build2 (MINUS_EXPR,
7734 TREE_TYPE (index),
7735 index,
7736 TYPE_MIN_VALUE (domain)));
7738 position =
7739 size_binop (MULT_EXPR, position,
7740 fold_convert (ssizetype,
7741 TYPE_SIZE_UNIT (elttype)));
7743 pos_rtx = expand_normal (position);
7744 xtarget = offset_address (target, pos_rtx,
7745 highest_pow2_factor (position));
7746 xtarget = adjust_address (xtarget, mode, 0);
7747 if (TREE_CODE (value) == CONSTRUCTOR)
7748 store_constructor (value, xtarget, cleared,
7749 exact_div (bitsize, BITS_PER_UNIT),
7750 reverse);
7751 else
7752 store_expr (value, xtarget, 0, false, reverse);
7754 /* Generate a conditional jump to exit the loop. */
7755 exit_cond = build2 (LT_EXPR, integer_type_node,
7756 index, hi_index);
7757 jumpif (exit_cond, loop_end,
7758 profile_probability::uninitialized ());
7760 /* Update the loop counter, and jump to the head of
7761 the loop. */
7762 expand_assignment (index,
7763 build2 (PLUS_EXPR, TREE_TYPE (index),
7764 index, integer_one_node),
7765 false);
7767 emit_jump (loop_start);
7769 /* Build the end of the loop. */
7770 emit_label (loop_end);
7773 else if ((index != 0 && ! tree_fits_shwi_p (index))
7774 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7776 tree position;
7778 if (index == 0)
7779 index = ssize_int (1);
7781 if (minelt)
7782 index = fold_convert (ssizetype,
7783 fold_build2 (MINUS_EXPR,
7784 TREE_TYPE (index),
7785 index,
7786 TYPE_MIN_VALUE (domain)));
7788 position =
7789 size_binop (MULT_EXPR, index,
7790 fold_convert (ssizetype,
7791 TYPE_SIZE_UNIT (elttype)));
7792 xtarget = offset_address (target,
7793 expand_normal (position),
7794 highest_pow2_factor (position));
7795 xtarget = adjust_address (xtarget, mode, 0);
7796 store_expr (value, xtarget, 0, false, reverse);
7798 else
7800 if (index != 0)
7801 bitpos = ((tree_to_shwi (index) - minelt)
7802 * tree_to_uhwi (TYPE_SIZE (elttype)));
7803 else
7804 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7806 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7807 && TREE_CODE (type) == ARRAY_TYPE
7808 && TYPE_NONALIASED_COMPONENT (type))
7810 target = copy_rtx (target);
7811 MEM_KEEP_ALIAS_SET_P (target) = 1;
7813 store_constructor_field (target, bitsize, bitpos, 0,
7814 bitregion_end, mode, value,
7815 cleared, get_alias_set (elttype),
7816 reverse);
7819 break;
7822 case VECTOR_TYPE:
7824 unsigned HOST_WIDE_INT idx;
7825 constructor_elt *ce;
7826 int i;
7827 bool need_to_clear;
7828 insn_code icode = CODE_FOR_nothing;
7829 tree elt;
7830 tree elttype = TREE_TYPE (type);
7831 int elt_size = vector_element_bits (type);
7832 machine_mode eltmode = TYPE_MODE (elttype);
7833 HOST_WIDE_INT bitsize;
7834 HOST_WIDE_INT bitpos;
7835 rtvec vector = NULL;
7836 poly_uint64 n_elts;
7837 unsigned HOST_WIDE_INT const_n_elts;
7838 alias_set_type alias;
7839 bool vec_vec_init_p = false;
7840 machine_mode mode = GET_MODE (target);
7842 gcc_assert (eltmode != BLKmode);
7844 /* Try using vec_duplicate_optab for uniform vectors. */
7845 if (!TREE_SIDE_EFFECTS (exp)
7846 && VECTOR_MODE_P (mode)
7847 && eltmode == GET_MODE_INNER (mode)
7848 && ((icode = optab_handler (vec_duplicate_optab, mode))
7849 != CODE_FOR_nothing)
7850 && (elt = uniform_vector_p (exp))
7851 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7853 class expand_operand ops[2];
7854 create_output_operand (&ops[0], target, mode);
7855 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7856 expand_insn (icode, 2, ops);
7857 if (!rtx_equal_p (target, ops[0].value))
7858 emit_move_insn (target, ops[0].value);
7859 break;
7861 /* Use sign-extension for uniform boolean vectors with
7862 integer modes and single-bit mask entries.
7863 Effectively "vec_duplicate" for bitmasks. */
7864 if (elt_size == 1
7865 && !TREE_SIDE_EFFECTS (exp)
7866 && VECTOR_BOOLEAN_TYPE_P (type)
7867 && SCALAR_INT_MODE_P (TYPE_MODE (type))
7868 && (elt = uniform_vector_p (exp))
7869 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7871 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
7872 expand_normal (elt));
7873 rtx tmp = gen_reg_rtx (mode);
7874 convert_move (tmp, op0, 0);
7876 /* Ensure no excess bits are set.
7877 GCN needs this for nunits < 64.
7878 x86 needs this for nunits < 8. */
7879 auto nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
7880 if (maybe_ne (GET_MODE_PRECISION (mode), nunits))
7881 tmp = expand_binop (mode, and_optab, tmp,
7882 GEN_INT ((1 << nunits) - 1), target,
7883 true, OPTAB_WIDEN);
7884 if (tmp != target)
7885 emit_move_insn (target, tmp);
7886 break;
7889 n_elts = TYPE_VECTOR_SUBPARTS (type);
7890 if (REG_P (target)
7891 && VECTOR_MODE_P (mode)
7892 && n_elts.is_constant (&const_n_elts))
7894 machine_mode emode = eltmode;
7895 bool vector_typed_elts_p = false;
7897 if (CONSTRUCTOR_NELTS (exp)
7898 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7899 == VECTOR_TYPE))
7901 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7902 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7903 * TYPE_VECTOR_SUBPARTS (etype),
7904 n_elts));
7905 emode = TYPE_MODE (etype);
7906 vector_typed_elts_p = true;
7908 icode = convert_optab_handler (vec_init_optab, mode, emode);
7909 if (icode != CODE_FOR_nothing)
7911 unsigned int n = const_n_elts;
7913 if (vector_typed_elts_p)
7915 n = CONSTRUCTOR_NELTS (exp);
7916 vec_vec_init_p = true;
7918 vector = rtvec_alloc (n);
7919 for (unsigned int k = 0; k < n; k++)
7920 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7924 /* Compute the size of the elements in the CTOR. It differs
7925 from the size of the vector type elements only when the
7926 CTOR elements are vectors themselves. */
7927 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7928 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7929 : elttype);
7930 if (VECTOR_TYPE_P (val_type))
7931 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7932 else
7933 bitsize = elt_size;
7935 /* If the constructor has fewer elements than the vector,
7936 clear the whole array first. Similarly if this is static
7937 constructor of a non-BLKmode object. */
7938 if (cleared)
7939 need_to_clear = false;
7940 else if (REG_P (target) && TREE_STATIC (exp))
7941 need_to_clear = true;
7942 else
7944 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7945 tree value;
7947 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7949 int n_elts_here = bitsize / elt_size;
7950 count += n_elts_here;
7951 if (mostly_zeros_p (value))
7952 zero_count += n_elts_here;
7955 /* Clear the entire vector first if there are any missing elements,
7956 or if the incidence of zero elements is >= 75%. */
7957 need_to_clear = (maybe_lt (count, n_elts)
7958 || 4 * zero_count >= 3 * count);
7961 if (need_to_clear && maybe_gt (size, 0) && !vector)
7963 if (REG_P (target))
7964 emit_move_insn (target, CONST0_RTX (mode));
7965 else
7966 clear_storage (target, gen_int_mode (size, Pmode),
7967 BLOCK_OP_NORMAL);
7968 cleared = 1;
7971 /* Inform later passes that the old value is dead. */
7972 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
7974 emit_move_insn (target, CONST0_RTX (mode));
7975 cleared = 1;
7978 if (MEM_P (target))
7979 alias = MEM_ALIAS_SET (target);
7980 else
7981 alias = get_alias_set (elttype);
7983 /* Store each element of the constructor into the corresponding
7984 element of TARGET, determined by counting the elements. */
7985 for (idx = 0, i = 0;
7986 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7987 idx++, i += bitsize / elt_size)
7989 HOST_WIDE_INT eltpos;
7990 tree value = ce->value;
7992 if (cleared && initializer_zerop (value))
7993 continue;
7995 if (ce->index)
7996 eltpos = tree_to_uhwi (ce->index);
7997 else
7998 eltpos = i;
8000 if (vector)
8002 if (vec_vec_init_p)
8004 gcc_assert (ce->index == NULL_TREE);
8005 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
8006 eltpos = idx;
8008 else
8009 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
8010 RTVEC_ELT (vector, eltpos) = expand_normal (value);
8012 else
8014 machine_mode value_mode
8015 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
8016 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
8017 bitpos = eltpos * elt_size;
8018 store_constructor_field (target, bitsize, bitpos, 0,
8019 bitregion_end, value_mode,
8020 value, cleared, alias, reverse);
8024 if (vector)
8025 emit_insn (GEN_FCN (icode) (target,
8026 gen_rtx_PARALLEL (mode, vector)));
8027 break;
8030 default:
8031 gcc_unreachable ();
8035 /* Store the value of EXP (an expression tree)
8036 into a subfield of TARGET which has mode MODE and occupies
8037 BITSIZE bits, starting BITPOS bits from the start of TARGET.
8038 If MODE is VOIDmode, it means that we are storing into a bit-field.
8040 BITREGION_START is bitpos of the first bitfield in this region.
8041 BITREGION_END is the bitpos of the ending bitfield in this region.
8042 These two fields are 0, if the C++ memory model does not apply,
8043 or we are not interested in keeping track of bitfield regions.
8045 Always return const0_rtx unless we have something particular to
8046 return.
8048 ALIAS_SET is the alias set for the destination. This value will
8049 (in general) be different from that for TARGET, since TARGET is a
8050 reference to the containing structure.
8052 If NONTEMPORAL is true, try generating a nontemporal store.
8054 If REVERSE is true, the store is to be done in reverse order. */
8056 static rtx
8057 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
8058 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
8059 machine_mode mode, tree exp,
8060 alias_set_type alias_set, bool nontemporal, bool reverse)
8062 if (TREE_CODE (exp) == ERROR_MARK)
8063 return const0_rtx;
8065 /* If we have nothing to store, do nothing unless the expression has
8066 side-effects. Don't do that for zero sized addressable lhs of
8067 calls. */
8068 if (known_eq (bitsize, 0)
8069 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8070 || TREE_CODE (exp) != CALL_EXPR))
8071 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
8073 if (GET_CODE (target) == CONCAT)
8075 /* We're storing into a struct containing a single __complex. */
8077 gcc_assert (known_eq (bitpos, 0));
8078 return store_expr (exp, target, 0, nontemporal, reverse);
8081 /* If the structure is in a register or if the component
8082 is a bit field, we cannot use addressing to access it.
8083 Use bit-field techniques or SUBREG to store in it. */
8085 poly_int64 decl_bitsize;
8086 if (mode == VOIDmode
8087 || (mode != BLKmode && ! direct_store[(int) mode]
8088 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8089 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
8090 || REG_P (target)
8091 || GET_CODE (target) == SUBREG
8092 /* If the field isn't aligned enough to store as an ordinary memref,
8093 store it as a bit field. */
8094 || (mode != BLKmode
8095 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
8096 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
8097 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
8098 || !multiple_p (bitpos, BITS_PER_UNIT)))
8099 || (known_size_p (bitsize)
8100 && mode != BLKmode
8101 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
8102 /* If the RHS and field are a constant size and the size of the
8103 RHS isn't the same size as the bitfield, we must use bitfield
8104 operations. */
8105 || (known_size_p (bitsize)
8106 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
8107 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
8108 bitsize)
8109 /* Except for initialization of full bytes from a CONSTRUCTOR, which
8110 we will handle specially below. */
8111 && !(TREE_CODE (exp) == CONSTRUCTOR
8112 && multiple_p (bitsize, BITS_PER_UNIT))
8113 /* And except for bitwise copying of TREE_ADDRESSABLE types,
8114 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
8115 includes some extra padding. store_expr / expand_expr will in
8116 that case call get_inner_reference that will have the bitsize
8117 we check here and thus the block move will not clobber the
8118 padding that shouldn't be clobbered. In the future we could
8119 replace the TREE_ADDRESSABLE check with a check that
8120 get_base_address needs to live in memory. */
8121 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8122 || TREE_CODE (exp) != COMPONENT_REF
8123 || !multiple_p (bitsize, BITS_PER_UNIT)
8124 || !multiple_p (bitpos, BITS_PER_UNIT)
8125 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
8126 &decl_bitsize)
8127 || maybe_ne (decl_bitsize, bitsize))
8128 /* A call with an addressable return type and return-slot
8129 optimization must not need bitfield operations but we must
8130 pass down the original target. */
8131 && (TREE_CODE (exp) != CALL_EXPR
8132 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
8133 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
8134 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
8135 decl we must use bitfield operations. */
8136 || (known_size_p (bitsize)
8137 && TREE_CODE (exp) == MEM_REF
8138 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
8139 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8140 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8141 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
8143 rtx temp;
8144 gimple *nop_def;
8146 /* If EXP is a NOP_EXPR of precision less than its mode, then that
8147 implies a mask operation. If the precision is the same size as
8148 the field we're storing into, that mask is redundant. This is
8149 particularly common with bit field assignments generated by the
8150 C front end. */
8151 nop_def = get_def_for_expr (exp, NOP_EXPR);
8152 if (nop_def)
8154 tree type = TREE_TYPE (exp);
8155 if (INTEGRAL_TYPE_P (type)
8156 && maybe_ne (TYPE_PRECISION (type),
8157 GET_MODE_BITSIZE (TYPE_MODE (type)))
8158 && known_eq (bitsize, TYPE_PRECISION (type)))
8160 tree op = gimple_assign_rhs1 (nop_def);
8161 type = TREE_TYPE (op);
8162 if (INTEGRAL_TYPE_P (type)
8163 && known_ge (TYPE_PRECISION (type), bitsize))
8164 exp = op;
8168 temp = expand_normal (exp);
8170 /* We don't support variable-sized BLKmode bitfields, since our
8171 handling of BLKmode is bound up with the ability to break
8172 things into words. */
8173 gcc_assert (mode != BLKmode || bitsize.is_constant ());
8175 /* Handle calls that return values in multiple non-contiguous locations.
8176 The Irix 6 ABI has examples of this. */
8177 if (GET_CODE (temp) == PARALLEL)
8179 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
8180 machine_mode temp_mode = GET_MODE (temp);
8181 if (temp_mode == BLKmode || temp_mode == VOIDmode)
8182 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
8183 rtx temp_target = gen_reg_rtx (temp_mode);
8184 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
8185 temp = temp_target;
8188 /* Handle calls that return BLKmode values in registers. */
8189 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
8191 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
8192 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
8193 temp = temp_target;
8196 /* If the value has aggregate type and an integral mode then, if BITSIZE
8197 is narrower than this mode and this is for big-endian data, we first
8198 need to put the value into the low-order bits for store_bit_field,
8199 except when MODE is BLKmode and BITSIZE larger than the word size
8200 (see the handling of fields larger than a word in store_bit_field).
8201 Moreover, the field may be not aligned on a byte boundary; in this
8202 case, if it has reverse storage order, it needs to be accessed as a
8203 scalar field with reverse storage order and we must first put the
8204 value into target order. */
8205 scalar_int_mode temp_mode;
8206 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
8207 && is_int_mode (GET_MODE (temp), &temp_mode))
8209 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
8211 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
8213 if (reverse)
8214 temp = flip_storage_order (temp_mode, temp);
8216 gcc_checking_assert (known_le (bitsize, size));
8217 if (maybe_lt (bitsize, size)
8218 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
8219 /* Use of to_constant for BLKmode was checked above. */
8220 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
8221 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
8222 size - bitsize, NULL_RTX, 1);
8225 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
8226 if (mode != VOIDmode && mode != BLKmode
8227 && mode != TYPE_MODE (TREE_TYPE (exp)))
8228 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
8230 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
8231 and BITPOS must be aligned on a byte boundary. If so, we simply do
8232 a block copy. Likewise for a BLKmode-like TARGET. */
8233 if (GET_MODE (temp) == BLKmode
8234 && (GET_MODE (target) == BLKmode
8235 || (MEM_P (target)
8236 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
8237 && multiple_p (bitpos, BITS_PER_UNIT)
8238 && multiple_p (bitsize, BITS_PER_UNIT))))
8240 gcc_assert (MEM_P (target) && MEM_P (temp));
8241 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8242 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
8244 target = adjust_address (target, VOIDmode, bytepos);
8245 emit_block_move (target, temp,
8246 gen_int_mode (bytesize, Pmode),
8247 BLOCK_OP_NORMAL);
8249 return const0_rtx;
8252 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
8253 word size, we need to load the value (see again store_bit_field). */
8254 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
8256 temp_mode = smallest_int_mode_for_size (bitsize);
8257 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
8258 temp_mode, false, NULL);
8261 /* Store the value in the bitfield. */
8262 gcc_checking_assert (known_ge (bitpos, 0));
8263 store_bit_field (target, bitsize, bitpos,
8264 bitregion_start, bitregion_end,
8265 mode, temp, reverse, false);
8267 return const0_rtx;
8269 else
8271 /* Now build a reference to just the desired component. */
8272 rtx to_rtx = adjust_address (target, mode,
8273 exact_div (bitpos, BITS_PER_UNIT));
8275 if (to_rtx == target)
8276 to_rtx = copy_rtx (to_rtx);
8278 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
8279 set_mem_alias_set (to_rtx, alias_set);
8281 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
8282 into a target smaller than its type; handle that case now. */
8283 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
8285 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
8286 store_constructor (exp, to_rtx, 0, bytesize, reverse);
8287 return to_rtx;
8290 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
8294 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
8295 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
8296 codes and find the ultimate containing object, which we return.
8298 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
8299 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
8300 storage order of the field.
8301 If the position of the field is variable, we store a tree
8302 giving the variable offset (in units) in *POFFSET.
8303 This offset is in addition to the bit position.
8304 If the position is not variable, we store 0 in *POFFSET.
8306 If any of the extraction expressions is volatile,
8307 we store 1 in *PVOLATILEP. Otherwise we don't change that.
8309 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
8310 Otherwise, it is a mode that can be used to access the field.
8312 If the field describes a variable-sized object, *PMODE is set to
8313 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
8314 this case, but the address of the object can be found. */
8316 tree
8317 get_inner_reference (tree exp, poly_int64 *pbitsize,
8318 poly_int64 *pbitpos, tree *poffset,
8319 machine_mode *pmode, int *punsignedp,
8320 int *preversep, int *pvolatilep)
8322 tree size_tree = 0;
8323 machine_mode mode = VOIDmode;
8324 bool blkmode_bitfield = false;
8325 tree offset = size_zero_node;
8326 poly_offset_int bit_offset = 0;
8328 /* First get the mode, signedness, storage order and size. We do this from
8329 just the outermost expression. */
8330 *pbitsize = -1;
8331 if (TREE_CODE (exp) == COMPONENT_REF)
8333 tree field = TREE_OPERAND (exp, 1);
8334 size_tree = DECL_SIZE (field);
8335 if (flag_strict_volatile_bitfields > 0
8336 && TREE_THIS_VOLATILE (exp)
8337 && DECL_BIT_FIELD_TYPE (field)
8338 && DECL_MODE (field) != BLKmode)
8339 /* Volatile bitfields should be accessed in the mode of the
8340 field's type, not the mode computed based on the bit
8341 size. */
8342 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
8343 else if (!DECL_BIT_FIELD (field))
8345 mode = DECL_MODE (field);
8346 /* For vector fields re-check the target flags, as DECL_MODE
8347 could have been set with different target flags than
8348 the current function has. */
8349 if (VECTOR_TYPE_P (TREE_TYPE (field))
8350 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
8351 mode = TYPE_MODE (TREE_TYPE (field));
8353 else if (DECL_MODE (field) == BLKmode)
8354 blkmode_bitfield = true;
8356 *punsignedp = DECL_UNSIGNED (field);
8358 else if (TREE_CODE (exp) == BIT_FIELD_REF)
8360 size_tree = TREE_OPERAND (exp, 1);
8361 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
8362 || TYPE_UNSIGNED (TREE_TYPE (exp)));
8364 /* For vector element types with the correct size of access or for
8365 vector typed accesses use the mode of the access type. */
8366 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
8367 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
8368 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
8369 || VECTOR_TYPE_P (TREE_TYPE (exp)))
8370 mode = TYPE_MODE (TREE_TYPE (exp));
8372 else
8374 mode = TYPE_MODE (TREE_TYPE (exp));
8375 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
8377 if (mode == BLKmode)
8378 size_tree = TYPE_SIZE (TREE_TYPE (exp));
8379 else
8380 *pbitsize = GET_MODE_BITSIZE (mode);
8383 if (size_tree != 0)
8385 if (! tree_fits_uhwi_p (size_tree))
8386 mode = BLKmode, *pbitsize = -1;
8387 else
8388 *pbitsize = tree_to_uhwi (size_tree);
8391 *preversep = reverse_storage_order_for_component_p (exp);
8393 /* Compute cumulative bit-offset for nested component-refs and array-refs,
8394 and find the ultimate containing object. */
8395 while (1)
8397 switch (TREE_CODE (exp))
8399 case BIT_FIELD_REF:
8400 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
8401 break;
8403 case COMPONENT_REF:
8405 tree field = TREE_OPERAND (exp, 1);
8406 tree this_offset = component_ref_field_offset (exp);
8408 /* If this field hasn't been filled in yet, don't go past it.
8409 This should only happen when folding expressions made during
8410 type construction. */
8411 if (this_offset == 0)
8412 break;
8414 offset = size_binop (PLUS_EXPR, offset, this_offset);
8415 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
8417 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
8419 break;
8421 case ARRAY_REF:
8422 case ARRAY_RANGE_REF:
8424 tree index = TREE_OPERAND (exp, 1);
8425 tree low_bound = array_ref_low_bound (exp);
8426 tree unit_size = array_ref_element_size (exp);
8428 /* We assume all arrays have sizes that are a multiple of a byte.
8429 First subtract the lower bound, if any, in the type of the
8430 index, then convert to sizetype and multiply by the size of
8431 the array element. */
8432 if (! integer_zerop (low_bound))
8433 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8434 index, low_bound);
8436 offset = size_binop (PLUS_EXPR, offset,
8437 size_binop (MULT_EXPR,
8438 fold_convert (sizetype, index),
8439 unit_size));
8441 break;
8443 case REALPART_EXPR:
8444 break;
8446 case IMAGPART_EXPR:
8447 bit_offset += *pbitsize;
8448 break;
8450 case VIEW_CONVERT_EXPR:
8451 break;
8453 case MEM_REF:
8454 /* Hand back the decl for MEM[&decl, off]. */
8455 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8457 tree off = TREE_OPERAND (exp, 1);
8458 if (!integer_zerop (off))
8460 poly_offset_int boff = mem_ref_offset (exp);
8461 boff <<= LOG2_BITS_PER_UNIT;
8462 bit_offset += boff;
8464 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8466 goto done;
8468 default:
8469 goto done;
8472 /* If any reference in the chain is volatile, the effect is volatile. */
8473 if (TREE_THIS_VOLATILE (exp))
8474 *pvolatilep = 1;
8476 exp = TREE_OPERAND (exp, 0);
8478 done:
8480 /* If OFFSET is constant, see if we can return the whole thing as a
8481 constant bit position. Make sure to handle overflow during
8482 this conversion. */
8483 if (poly_int_tree_p (offset))
8485 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8486 TYPE_PRECISION (sizetype));
8487 tem <<= LOG2_BITS_PER_UNIT;
8488 tem += bit_offset;
8489 if (tem.to_shwi (pbitpos))
8490 *poffset = offset = NULL_TREE;
8493 /* Otherwise, split it up. */
8494 if (offset)
8496 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8497 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8499 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8500 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8501 offset = size_binop (PLUS_EXPR, offset,
8502 build_int_cst (sizetype, bytes.force_shwi ()));
8505 *poffset = offset;
8508 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8509 if (mode == VOIDmode
8510 && blkmode_bitfield
8511 && multiple_p (*pbitpos, BITS_PER_UNIT)
8512 && multiple_p (*pbitsize, BITS_PER_UNIT))
8513 *pmode = BLKmode;
8514 else
8515 *pmode = mode;
8517 return exp;
8520 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8522 static unsigned HOST_WIDE_INT
8523 target_align (const_tree target)
8525 /* We might have a chain of nested references with intermediate misaligning
8526 bitfields components, so need to recurse to find out. */
8528 unsigned HOST_WIDE_INT this_align, outer_align;
8530 switch (TREE_CODE (target))
8532 case BIT_FIELD_REF:
8533 return 1;
8535 case COMPONENT_REF:
8536 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8537 outer_align = target_align (TREE_OPERAND (target, 0));
8538 return MIN (this_align, outer_align);
8540 case ARRAY_REF:
8541 case ARRAY_RANGE_REF:
8542 this_align = TYPE_ALIGN (TREE_TYPE (target));
8543 outer_align = target_align (TREE_OPERAND (target, 0));
8544 return MIN (this_align, outer_align);
8546 CASE_CONVERT:
8547 case NON_LVALUE_EXPR:
8548 case VIEW_CONVERT_EXPR:
8549 this_align = TYPE_ALIGN (TREE_TYPE (target));
8550 outer_align = target_align (TREE_OPERAND (target, 0));
8551 return MAX (this_align, outer_align);
8553 default:
8554 return TYPE_ALIGN (TREE_TYPE (target));
8559 /* Given an rtx VALUE that may contain additions and multiplications, return
8560 an equivalent value that just refers to a register, memory, or constant.
8561 This is done by generating instructions to perform the arithmetic and
8562 returning a pseudo-register containing the value.
8564 The returned value may be a REG, SUBREG, MEM or constant. */
8567 force_operand (rtx value, rtx target)
8569 rtx op1, op2;
8570 /* Use subtarget as the target for operand 0 of a binary operation. */
8571 rtx subtarget = get_subtarget (target);
8572 enum rtx_code code = GET_CODE (value);
8574 /* Check for subreg applied to an expression produced by loop optimizer. */
8575 if (code == SUBREG
8576 && !REG_P (SUBREG_REG (value))
8577 && !MEM_P (SUBREG_REG (value)))
8579 value
8580 = simplify_gen_subreg (GET_MODE (value),
8581 force_reg (GET_MODE (SUBREG_REG (value)),
8582 force_operand (SUBREG_REG (value),
8583 NULL_RTX)),
8584 GET_MODE (SUBREG_REG (value)),
8585 SUBREG_BYTE (value));
8586 code = GET_CODE (value);
8589 /* Check for a PIC address load. */
8590 if ((code == PLUS || code == MINUS)
8591 && XEXP (value, 0) == pic_offset_table_rtx
8592 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8593 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8594 || GET_CODE (XEXP (value, 1)) == CONST))
8596 if (!subtarget)
8597 subtarget = gen_reg_rtx (GET_MODE (value));
8598 emit_move_insn (subtarget, value);
8599 return subtarget;
8602 if (ARITHMETIC_P (value))
8604 op2 = XEXP (value, 1);
8605 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8606 subtarget = 0;
8607 if (code == MINUS && CONST_INT_P (op2))
8609 code = PLUS;
8610 op2 = negate_rtx (GET_MODE (value), op2);
8613 /* Check for an addition with OP2 a constant integer and our first
8614 operand a PLUS of a virtual register and something else. In that
8615 case, we want to emit the sum of the virtual register and the
8616 constant first and then add the other value. This allows virtual
8617 register instantiation to simply modify the constant rather than
8618 creating another one around this addition. */
8619 if (code == PLUS && CONST_INT_P (op2)
8620 && GET_CODE (XEXP (value, 0)) == PLUS
8621 && REG_P (XEXP (XEXP (value, 0), 0))
8622 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8624 rtx temp = expand_simple_binop (GET_MODE (value), code,
8625 XEXP (XEXP (value, 0), 0), op2,
8626 subtarget, 0, OPTAB_LIB_WIDEN);
8627 return expand_simple_binop (GET_MODE (value), code, temp,
8628 force_operand (XEXP (XEXP (value,
8629 0), 1), 0),
8630 target, 0, OPTAB_LIB_WIDEN);
8633 op1 = force_operand (XEXP (value, 0), subtarget);
8634 op2 = force_operand (op2, NULL_RTX);
8635 switch (code)
8637 case MULT:
8638 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8639 case DIV:
8640 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8641 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8642 target, 1, OPTAB_LIB_WIDEN);
8643 else
8644 return expand_divmod (0,
8645 FLOAT_MODE_P (GET_MODE (value))
8646 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8647 GET_MODE (value), op1, op2, target, 0);
8648 case MOD:
8649 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8650 target, 0);
8651 case UDIV:
8652 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8653 target, 1);
8654 case UMOD:
8655 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8656 target, 1);
8657 case ASHIFTRT:
8658 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8659 target, 0, OPTAB_LIB_WIDEN);
8660 default:
8661 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8662 target, 1, OPTAB_LIB_WIDEN);
8665 if (UNARY_P (value))
8667 if (!target)
8668 target = gen_reg_rtx (GET_MODE (value));
8669 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8670 switch (code)
8672 case ZERO_EXTEND:
8673 case SIGN_EXTEND:
8674 case TRUNCATE:
8675 case FLOAT_EXTEND:
8676 case FLOAT_TRUNCATE:
8677 convert_move (target, op1, code == ZERO_EXTEND);
8678 return target;
8680 case FIX:
8681 case UNSIGNED_FIX:
8682 expand_fix (target, op1, code == UNSIGNED_FIX);
8683 return target;
8685 case FLOAT:
8686 case UNSIGNED_FLOAT:
8687 expand_float (target, op1, code == UNSIGNED_FLOAT);
8688 return target;
8690 default:
8691 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8695 #ifdef INSN_SCHEDULING
8696 /* On machines that have insn scheduling, we want all memory reference to be
8697 explicit, so we need to deal with such paradoxical SUBREGs. */
8698 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8699 value
8700 = simplify_gen_subreg (GET_MODE (value),
8701 force_reg (GET_MODE (SUBREG_REG (value)),
8702 force_operand (SUBREG_REG (value),
8703 NULL_RTX)),
8704 GET_MODE (SUBREG_REG (value)),
8705 SUBREG_BYTE (value));
8706 #endif
8708 return value;
8711 /* Subroutine of expand_expr: return true iff there is no way that
8712 EXP can reference X, which is being modified. TOP_P is nonzero if this
8713 call is going to be used to determine whether we need a temporary
8714 for EXP, as opposed to a recursive call to this function.
8716 It is always safe for this routine to return false since it merely
8717 searches for optimization opportunities. */
8719 bool
8720 safe_from_p (const_rtx x, tree exp, int top_p)
8722 rtx exp_rtl = 0;
8723 int i, nops;
8725 if (x == 0
8726 /* If EXP has varying size, we MUST use a target since we currently
8727 have no way of allocating temporaries of variable size
8728 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8729 So we assume here that something at a higher level has prevented a
8730 clash. This is somewhat bogus, but the best we can do. Only
8731 do this when X is BLKmode and when we are at the top level. */
8732 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8733 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8734 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8735 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8736 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8737 != INTEGER_CST)
8738 && GET_MODE (x) == BLKmode)
8739 /* If X is in the outgoing argument area, it is always safe. */
8740 || (MEM_P (x)
8741 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8742 || (GET_CODE (XEXP (x, 0)) == PLUS
8743 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8744 return true;
8746 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8747 find the underlying pseudo. */
8748 if (GET_CODE (x) == SUBREG)
8750 x = SUBREG_REG (x);
8751 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8752 return false;
8755 /* Now look at our tree code and possibly recurse. */
8756 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8758 case tcc_declaration:
8759 exp_rtl = DECL_RTL_IF_SET (exp);
8760 break;
8762 case tcc_constant:
8763 return true;
8765 case tcc_exceptional:
8766 if (TREE_CODE (exp) == TREE_LIST)
8768 while (1)
8770 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8771 return false;
8772 exp = TREE_CHAIN (exp);
8773 if (!exp)
8774 return true;
8775 if (TREE_CODE (exp) != TREE_LIST)
8776 return safe_from_p (x, exp, 0);
8779 else if (TREE_CODE (exp) == CONSTRUCTOR)
8781 constructor_elt *ce;
8782 unsigned HOST_WIDE_INT idx;
8784 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8785 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8786 || !safe_from_p (x, ce->value, 0))
8787 return false;
8788 return true;
8790 else if (TREE_CODE (exp) == ERROR_MARK)
8791 return true; /* An already-visited SAVE_EXPR? */
8792 else
8793 return false;
8795 case tcc_statement:
8796 /* The only case we look at here is the DECL_INITIAL inside a
8797 DECL_EXPR. */
8798 return (TREE_CODE (exp) != DECL_EXPR
8799 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8800 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8801 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8803 case tcc_binary:
8804 case tcc_comparison:
8805 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8806 return false;
8807 /* Fall through. */
8809 case tcc_unary:
8810 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8812 case tcc_expression:
8813 case tcc_reference:
8814 case tcc_vl_exp:
8815 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8816 the expression. If it is set, we conflict iff we are that rtx or
8817 both are in memory. Otherwise, we check all operands of the
8818 expression recursively. */
8820 switch (TREE_CODE (exp))
8822 case ADDR_EXPR:
8823 /* If the operand is static or we are static, we can't conflict.
8824 Likewise if we don't conflict with the operand at all. */
8825 if (staticp (TREE_OPERAND (exp, 0))
8826 || TREE_STATIC (exp)
8827 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8828 return true;
8830 /* Otherwise, the only way this can conflict is if we are taking
8831 the address of a DECL a that address if part of X, which is
8832 very rare. */
8833 exp = TREE_OPERAND (exp, 0);
8834 if (DECL_P (exp))
8836 if (!DECL_RTL_SET_P (exp)
8837 || !MEM_P (DECL_RTL (exp)))
8838 return false;
8839 else
8840 exp_rtl = XEXP (DECL_RTL (exp), 0);
8842 break;
8844 case MEM_REF:
8845 if (MEM_P (x)
8846 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8847 get_alias_set (exp)))
8848 return false;
8849 break;
8851 case CALL_EXPR:
8852 /* Assume that the call will clobber all hard registers and
8853 all of memory. */
8854 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8855 || MEM_P (x))
8856 return false;
8857 break;
8859 case WITH_CLEANUP_EXPR:
8860 case CLEANUP_POINT_EXPR:
8861 /* Lowered by gimplify.cc. */
8862 gcc_unreachable ();
8864 case SAVE_EXPR:
8865 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8867 default:
8868 break;
8871 /* If we have an rtx, we do not need to scan our operands. */
8872 if (exp_rtl)
8873 break;
8875 nops = TREE_OPERAND_LENGTH (exp);
8876 for (i = 0; i < nops; i++)
8877 if (TREE_OPERAND (exp, i) != 0
8878 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8879 return false;
8881 break;
8883 case tcc_type:
8884 /* Should never get a type here. */
8885 gcc_unreachable ();
8888 /* If we have an rtl, find any enclosed object. Then see if we conflict
8889 with it. */
8890 if (exp_rtl)
8892 if (GET_CODE (exp_rtl) == SUBREG)
8894 exp_rtl = SUBREG_REG (exp_rtl);
8895 if (REG_P (exp_rtl)
8896 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8897 return false;
8900 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8901 are memory and they conflict. */
8902 return ! (rtx_equal_p (x, exp_rtl)
8903 || (MEM_P (x) && MEM_P (exp_rtl)
8904 && true_dependence (exp_rtl, VOIDmode, x)));
8907 /* If we reach here, it is safe. */
8908 return true;
8912 /* Return the highest power of two that EXP is known to be a multiple of.
8913 This is used in updating alignment of MEMs in array references. */
8915 unsigned HOST_WIDE_INT
8916 highest_pow2_factor (const_tree exp)
8918 unsigned HOST_WIDE_INT ret;
8919 int trailing_zeros = tree_ctz (exp);
8920 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8921 return BIGGEST_ALIGNMENT;
8922 ret = HOST_WIDE_INT_1U << trailing_zeros;
8923 if (ret > BIGGEST_ALIGNMENT)
8924 return BIGGEST_ALIGNMENT;
8925 return ret;
8928 /* Similar, except that the alignment requirements of TARGET are
8929 taken into account. Assume it is at least as aligned as its
8930 type, unless it is a COMPONENT_REF in which case the layout of
8931 the structure gives the alignment. */
8933 static unsigned HOST_WIDE_INT
8934 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8936 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8937 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8939 return MAX (factor, talign);
8942 /* Convert the tree comparison code TCODE to the rtl one where the
8943 signedness is UNSIGNEDP. */
8945 static enum rtx_code
8946 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8948 enum rtx_code code;
8949 switch (tcode)
8951 case EQ_EXPR:
8952 code = EQ;
8953 break;
8954 case NE_EXPR:
8955 code = NE;
8956 break;
8957 case LT_EXPR:
8958 code = unsignedp ? LTU : LT;
8959 break;
8960 case LE_EXPR:
8961 code = unsignedp ? LEU : LE;
8962 break;
8963 case GT_EXPR:
8964 code = unsignedp ? GTU : GT;
8965 break;
8966 case GE_EXPR:
8967 code = unsignedp ? GEU : GE;
8968 break;
8969 case UNORDERED_EXPR:
8970 code = UNORDERED;
8971 break;
8972 case ORDERED_EXPR:
8973 code = ORDERED;
8974 break;
8975 case UNLT_EXPR:
8976 code = UNLT;
8977 break;
8978 case UNLE_EXPR:
8979 code = UNLE;
8980 break;
8981 case UNGT_EXPR:
8982 code = UNGT;
8983 break;
8984 case UNGE_EXPR:
8985 code = UNGE;
8986 break;
8987 case UNEQ_EXPR:
8988 code = UNEQ;
8989 break;
8990 case LTGT_EXPR:
8991 code = LTGT;
8992 break;
8994 default:
8995 gcc_unreachable ();
8997 return code;
9000 /* Subroutine of expand_expr. Expand the two operands of a binary
9001 expression EXP0 and EXP1 placing the results in OP0 and OP1.
9002 The value may be stored in TARGET if TARGET is nonzero. The
9003 MODIFIER argument is as documented by expand_expr. */
9005 void
9006 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
9007 enum expand_modifier modifier)
9009 if (! safe_from_p (target, exp1, 1))
9010 target = 0;
9011 if (operand_equal_p (exp0, exp1, 0))
9013 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
9014 *op1 = copy_rtx (*op0);
9016 else
9018 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
9019 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
9024 /* Return a MEM that contains constant EXP. DEFER is as for
9025 output_constant_def and MODIFIER is as for expand_expr. */
9027 static rtx
9028 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
9030 rtx mem;
9032 mem = output_constant_def (exp, defer);
9033 if (modifier != EXPAND_INITIALIZER)
9034 mem = use_anchored_address (mem);
9035 return mem;
9038 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
9039 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9041 static rtx
9042 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
9043 enum expand_modifier modifier, addr_space_t as)
9045 rtx result, subtarget;
9046 tree inner, offset;
9047 poly_int64 bitsize, bitpos;
9048 int unsignedp, reversep, volatilep = 0;
9049 machine_mode mode1;
9051 /* If we are taking the address of a constant and are at the top level,
9052 we have to use output_constant_def since we can't call force_const_mem
9053 at top level. */
9054 /* ??? This should be considered a front-end bug. We should not be
9055 generating ADDR_EXPR of something that isn't an LVALUE. The only
9056 exception here is STRING_CST. */
9057 if (CONSTANT_CLASS_P (exp))
9059 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
9060 if (modifier < EXPAND_SUM)
9061 result = force_operand (result, target);
9062 return result;
9065 /* Everything must be something allowed by is_gimple_addressable. */
9066 switch (TREE_CODE (exp))
9068 case INDIRECT_REF:
9069 /* This case will happen via recursion for &a->b. */
9070 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
9072 case MEM_REF:
9074 tree tem = TREE_OPERAND (exp, 0);
9075 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9076 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
9077 return expand_expr (tem, target, tmode, modifier);
9080 case TARGET_MEM_REF:
9081 return addr_for_mem_ref (exp, as, true);
9083 case CONST_DECL:
9084 /* Expand the initializer like constants above. */
9085 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
9086 0, modifier), 0);
9087 if (modifier < EXPAND_SUM)
9088 result = force_operand (result, target);
9089 return result;
9091 case REALPART_EXPR:
9092 /* The real part of the complex number is always first, therefore
9093 the address is the same as the address of the parent object. */
9094 offset = 0;
9095 bitpos = 0;
9096 inner = TREE_OPERAND (exp, 0);
9097 break;
9099 case IMAGPART_EXPR:
9100 /* The imaginary part of the complex number is always second.
9101 The expression is therefore always offset by the size of the
9102 scalar type. */
9103 offset = 0;
9104 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
9105 inner = TREE_OPERAND (exp, 0);
9106 break;
9108 case COMPOUND_LITERAL_EXPR:
9109 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
9110 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
9111 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
9112 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
9113 the initializers aren't gimplified. */
9114 if (COMPOUND_LITERAL_EXPR_DECL (exp)
9115 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
9116 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
9117 target, tmode, modifier, as);
9118 /* FALLTHRU */
9119 default:
9120 /* If the object is a DECL, then expand it for its rtl. Don't bypass
9121 expand_expr, as that can have various side effects; LABEL_DECLs for
9122 example, may not have their DECL_RTL set yet. Expand the rtl of
9123 CONSTRUCTORs too, which should yield a memory reference for the
9124 constructor's contents. Assume language specific tree nodes can
9125 be expanded in some interesting way. */
9126 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
9127 if (DECL_P (exp)
9128 || TREE_CODE (exp) == CONSTRUCTOR
9129 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
9131 result = expand_expr (exp, target, tmode,
9132 modifier == EXPAND_INITIALIZER
9133 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
9135 /* If the DECL isn't in memory, then the DECL wasn't properly
9136 marked TREE_ADDRESSABLE, which will be either a front-end
9137 or a tree optimizer bug. */
9139 gcc_assert (MEM_P (result));
9140 result = XEXP (result, 0);
9142 /* ??? Is this needed anymore? */
9143 if (DECL_P (exp))
9144 TREE_USED (exp) = 1;
9146 if (modifier != EXPAND_INITIALIZER
9147 && modifier != EXPAND_CONST_ADDRESS
9148 && modifier != EXPAND_SUM)
9149 result = force_operand (result, target);
9150 return result;
9153 /* Pass FALSE as the last argument to get_inner_reference although
9154 we are expanding to RTL. The rationale is that we know how to
9155 handle "aligning nodes" here: we can just bypass them because
9156 they won't change the final object whose address will be returned
9157 (they actually exist only for that purpose). */
9158 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
9159 &unsignedp, &reversep, &volatilep);
9160 break;
9163 /* We must have made progress. */
9164 gcc_assert (inner != exp);
9166 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
9167 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
9168 inner alignment, force the inner to be sufficiently aligned. */
9169 if (CONSTANT_CLASS_P (inner)
9170 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
9172 inner = copy_node (inner);
9173 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
9174 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
9175 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
9177 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
9179 if (offset)
9181 rtx tmp;
9183 if (modifier != EXPAND_NORMAL)
9184 result = force_operand (result, NULL);
9185 tmp = expand_expr (offset, NULL_RTX, tmode,
9186 modifier == EXPAND_INITIALIZER
9187 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
9189 /* expand_expr is allowed to return an object in a mode other
9190 than TMODE. If it did, we need to convert. */
9191 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
9192 tmp = convert_modes (tmode, GET_MODE (tmp),
9193 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
9194 result = convert_memory_address_addr_space (tmode, result, as);
9195 tmp = convert_memory_address_addr_space (tmode, tmp, as);
9197 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9198 result = simplify_gen_binary (PLUS, tmode, result, tmp);
9199 else
9201 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
9202 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
9203 1, OPTAB_LIB_WIDEN);
9207 if (maybe_ne (bitpos, 0))
9209 /* Someone beforehand should have rejected taking the address
9210 of an object that isn't byte-aligned. */
9211 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
9212 result = convert_memory_address_addr_space (tmode, result, as);
9213 result = plus_constant (tmode, result, bytepos);
9214 if (modifier < EXPAND_SUM)
9215 result = force_operand (result, target);
9218 return result;
9221 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
9222 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9224 static rtx
9225 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
9226 enum expand_modifier modifier)
9228 addr_space_t as = ADDR_SPACE_GENERIC;
9229 scalar_int_mode address_mode = Pmode;
9230 scalar_int_mode pointer_mode = ptr_mode;
9231 machine_mode rmode;
9232 rtx result;
9234 /* Target mode of VOIDmode says "whatever's natural". */
9235 if (tmode == VOIDmode)
9236 tmode = TYPE_MODE (TREE_TYPE (exp));
9238 if (POINTER_TYPE_P (TREE_TYPE (exp)))
9240 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
9241 address_mode = targetm.addr_space.address_mode (as);
9242 pointer_mode = targetm.addr_space.pointer_mode (as);
9245 /* We can get called with some Weird Things if the user does silliness
9246 like "(short) &a". In that case, convert_memory_address won't do
9247 the right thing, so ignore the given target mode. */
9248 scalar_int_mode new_tmode = (tmode == pointer_mode
9249 ? pointer_mode
9250 : address_mode);
9252 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
9253 new_tmode, modifier, as);
9255 /* Despite expand_expr claims concerning ignoring TMODE when not
9256 strictly convenient, stuff breaks if we don't honor it. Note
9257 that combined with the above, we only do this for pointer modes. */
9258 rmode = GET_MODE (result);
9259 if (rmode == VOIDmode)
9260 rmode = new_tmode;
9261 if (rmode != new_tmode)
9262 result = convert_memory_address_addr_space (new_tmode, result, as);
9264 return result;
9267 /* Generate code for computing CONSTRUCTOR EXP.
9268 An rtx for the computed value is returned. If AVOID_TEMP_MEM
9269 is TRUE, instead of creating a temporary variable in memory
9270 NULL is returned and the caller needs to handle it differently. */
9272 static rtx
9273 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
9274 bool avoid_temp_mem)
9276 tree type = TREE_TYPE (exp);
9277 machine_mode mode = TYPE_MODE (type);
9279 /* Try to avoid creating a temporary at all. This is possible
9280 if all of the initializer is zero.
9281 FIXME: try to handle all [0..255] initializers we can handle
9282 with memset. */
9283 if (TREE_STATIC (exp)
9284 && !TREE_ADDRESSABLE (exp)
9285 && target != 0 && mode == BLKmode
9286 && all_zeros_p (exp))
9288 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
9289 return target;
9292 /* All elts simple constants => refer to a constant in memory. But
9293 if this is a non-BLKmode mode, let it store a field at a time
9294 since that should make a CONST_INT, CONST_WIDE_INT or
9295 CONST_DOUBLE when we fold. Likewise, if we have a target we can
9296 use, it is best to store directly into the target unless the type
9297 is large enough that memcpy will be used. If we are making an
9298 initializer and all operands are constant, put it in memory as
9299 well.
9301 FIXME: Avoid trying to fill vector constructors piece-meal.
9302 Output them with output_constant_def below unless we're sure
9303 they're zeros. This should go away when vector initializers
9304 are treated like VECTOR_CST instead of arrays. */
9305 if ((TREE_STATIC (exp)
9306 && ((mode == BLKmode
9307 && ! (target != 0 && safe_from_p (target, exp, 1)))
9308 || TREE_ADDRESSABLE (exp)
9309 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9310 && (! can_move_by_pieces
9311 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9312 TYPE_ALIGN (type)))
9313 && ! mostly_zeros_p (exp))))
9314 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
9315 && TREE_CONSTANT (exp)))
9317 rtx constructor;
9319 if (avoid_temp_mem)
9320 return NULL_RTX;
9322 constructor = expand_expr_constant (exp, 1, modifier);
9324 if (modifier != EXPAND_CONST_ADDRESS
9325 && modifier != EXPAND_INITIALIZER
9326 && modifier != EXPAND_SUM)
9327 constructor = validize_mem (constructor);
9329 return constructor;
9332 /* If the CTOR is available in static storage and not mostly
9333 zeros and we can move it by pieces prefer to do so since
9334 that's usually more efficient than performing a series of
9335 stores from immediates. */
9336 if (avoid_temp_mem
9337 && TREE_STATIC (exp)
9338 && TREE_CONSTANT (exp)
9339 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9340 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9341 TYPE_ALIGN (type))
9342 && ! mostly_zeros_p (exp))
9343 return NULL_RTX;
9345 /* Handle calls that pass values in multiple non-contiguous
9346 locations. The Irix 6 ABI has examples of this. */
9347 if (target == 0 || ! safe_from_p (target, exp, 1)
9348 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
9349 /* Also make a temporary if the store is to volatile memory, to
9350 avoid individual accesses to aggregate members. */
9351 || (GET_CODE (target) == MEM
9352 && MEM_VOLATILE_P (target)
9353 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
9355 if (avoid_temp_mem)
9356 return NULL_RTX;
9358 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
9361 store_constructor (exp, target, 0, int_expr_size (exp), false);
9362 return target;
9366 /* expand_expr: generate code for computing expression EXP.
9367 An rtx for the computed value is returned. The value is never null.
9368 In the case of a void EXP, const0_rtx is returned.
9370 The value may be stored in TARGET if TARGET is nonzero.
9371 TARGET is just a suggestion; callers must assume that
9372 the rtx returned may not be the same as TARGET.
9374 If TARGET is CONST0_RTX, it means that the value will be ignored.
9376 If TMODE is not VOIDmode, it suggests generating the
9377 result in mode TMODE. But this is done only when convenient.
9378 Otherwise, TMODE is ignored and the value generated in its natural mode.
9379 TMODE is just a suggestion; callers must assume that
9380 the rtx returned may not have mode TMODE.
9382 Note that TARGET may have neither TMODE nor MODE. In that case, it
9383 probably will not be used.
9385 If MODIFIER is EXPAND_SUM then when EXP is an addition
9386 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
9387 or a nest of (PLUS ...) and (MINUS ...) where the terms are
9388 products as above, or REG or MEM, or constant.
9389 Ordinarily in such cases we would output mul or add instructions
9390 and then return a pseudo reg containing the sum.
9392 EXPAND_INITIALIZER is much like EXPAND_SUM except that
9393 it also marks a label as absolutely required (it can't be dead).
9394 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
9395 This is used for outputting expressions used in initializers.
9397 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
9398 with a constant address even if that address is not normally legitimate.
9399 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
9401 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
9402 a call parameter. Such targets require special care as we haven't yet
9403 marked TARGET so that it's safe from being trashed by libcalls. We
9404 don't want to use TARGET for anything but the final result;
9405 Intermediate values must go elsewhere. Additionally, calls to
9406 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
9408 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
9409 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
9410 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
9411 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
9412 recursively.
9413 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9414 then *ALT_RTL is set to TARGET (before legitimziation).
9416 If INNER_REFERENCE_P is true, we are expanding an inner reference.
9417 In this case, we don't adjust a returned MEM rtx that wouldn't be
9418 sufficiently aligned for its mode; instead, it's up to the caller
9419 to deal with it afterwards. This is used to make sure that unaligned
9420 base objects for which out-of-bounds accesses are supported, for
9421 example record types with trailing arrays, aren't realigned behind
9422 the back of the caller.
9423 The normal operating mode is to pass FALSE for this parameter. */
9426 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9427 enum expand_modifier modifier, rtx *alt_rtl,
9428 bool inner_reference_p)
9430 rtx ret;
9432 /* Handle ERROR_MARK before anybody tries to access its type. */
9433 if (TREE_CODE (exp) == ERROR_MARK
9434 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9436 ret = CONST0_RTX (tmode);
9437 return ret ? ret : const0_rtx;
9440 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9441 inner_reference_p);
9442 return ret;
9445 /* Try to expand the conditional expression which is represented by
9446 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9447 return the rtl reg which represents the result. Otherwise return
9448 NULL_RTX. */
9450 static rtx
9451 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9452 tree treeop1 ATTRIBUTE_UNUSED,
9453 tree treeop2 ATTRIBUTE_UNUSED)
9455 rtx insn;
9456 rtx op00, op01, op1, op2;
9457 enum rtx_code comparison_code;
9458 machine_mode comparison_mode;
9459 gimple *srcstmt;
9460 rtx temp;
9461 tree type = TREE_TYPE (treeop1);
9462 int unsignedp = TYPE_UNSIGNED (type);
9463 machine_mode mode = TYPE_MODE (type);
9464 machine_mode orig_mode = mode;
9465 static bool expanding_cond_expr_using_cmove = false;
9467 /* Conditional move expansion can end up TERing two operands which,
9468 when recursively hitting conditional expressions can result in
9469 exponential behavior if the cmove expansion ultimatively fails.
9470 It's hardly profitable to TER a cmove into a cmove so avoid doing
9471 that by failing early if we end up recursing. */
9472 if (expanding_cond_expr_using_cmove)
9473 return NULL_RTX;
9475 /* If we cannot do a conditional move on the mode, try doing it
9476 with the promoted mode. */
9477 if (!can_conditionally_move_p (mode))
9479 mode = promote_mode (type, mode, &unsignedp);
9480 if (!can_conditionally_move_p (mode))
9481 return NULL_RTX;
9482 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9484 else
9485 temp = assign_temp (type, 0, 1);
9487 expanding_cond_expr_using_cmove = true;
9488 start_sequence ();
9489 expand_operands (treeop1, treeop2,
9490 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9491 EXPAND_NORMAL);
9493 if (TREE_CODE (treeop0) == SSA_NAME
9494 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9496 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9497 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9498 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9499 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9500 comparison_mode = TYPE_MODE (type);
9501 unsignedp = TYPE_UNSIGNED (type);
9502 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9504 else if (COMPARISON_CLASS_P (treeop0))
9506 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9507 enum tree_code cmpcode = TREE_CODE (treeop0);
9508 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9509 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9510 unsignedp = TYPE_UNSIGNED (type);
9511 comparison_mode = TYPE_MODE (type);
9512 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9514 else
9516 op00 = expand_normal (treeop0);
9517 op01 = const0_rtx;
9518 comparison_code = NE;
9519 comparison_mode = GET_MODE (op00);
9520 if (comparison_mode == VOIDmode)
9521 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9523 expanding_cond_expr_using_cmove = false;
9525 if (GET_MODE (op1) != mode)
9526 op1 = gen_lowpart (mode, op1);
9528 if (GET_MODE (op2) != mode)
9529 op2 = gen_lowpart (mode, op2);
9531 /* Try to emit the conditional move. */
9532 insn = emit_conditional_move (temp,
9533 { comparison_code, op00, op01,
9534 comparison_mode },
9535 op1, op2, mode,
9536 unsignedp);
9538 /* If we could do the conditional move, emit the sequence,
9539 and return. */
9540 if (insn)
9542 rtx_insn *seq = get_insns ();
9543 end_sequence ();
9544 emit_insn (seq);
9545 return convert_modes (orig_mode, mode, temp, 0);
9548 /* Otherwise discard the sequence and fall back to code with
9549 branches. */
9550 end_sequence ();
9551 return NULL_RTX;
9554 /* A helper function for expand_expr_real_2 to be used with a
9555 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9556 is nonzero, with alignment ALIGN in bits.
9557 Store the value at TARGET if possible (if TARGET is nonzero).
9558 Regardless of TARGET, we return the rtx for where the value is placed.
9559 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9560 then *ALT_RTL is set to TARGET (before legitimziation). */
9562 static rtx
9563 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9564 unsigned int align, rtx target, rtx *alt_rtl)
9566 enum insn_code icode;
9568 if ((icode = optab_handler (movmisalign_optab, mode))
9569 != CODE_FOR_nothing)
9571 class expand_operand ops[2];
9573 /* We've already validated the memory, and we're creating a
9574 new pseudo destination. The predicates really can't fail,
9575 nor can the generator. */
9576 create_output_operand (&ops[0], NULL_RTX, mode);
9577 create_fixed_operand (&ops[1], temp);
9578 expand_insn (icode, 2, ops);
9579 temp = ops[0].value;
9581 else if (targetm.slow_unaligned_access (mode, align))
9582 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9583 0, unsignedp, target,
9584 mode, mode, false, alt_rtl);
9585 return temp;
9588 /* Helper function of expand_expr_2, expand a division or modulo.
9589 op0 and op1 should be already expanded treeop0 and treeop1, using
9590 expand_operands. */
9592 static rtx
9593 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9594 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9596 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9597 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9598 if (SCALAR_INT_MODE_P (mode)
9599 && optimize >= 2
9600 && get_range_pos_neg (treeop0) == 1
9601 && get_range_pos_neg (treeop1) == 1)
9603 /* If both arguments are known to be positive when interpreted
9604 as signed, we can expand it as both signed and unsigned
9605 division or modulo. Choose the cheaper sequence in that case. */
9606 bool speed_p = optimize_insn_for_speed_p ();
9607 do_pending_stack_adjust ();
9608 start_sequence ();
9609 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9610 rtx_insn *uns_insns = get_insns ();
9611 end_sequence ();
9612 start_sequence ();
9613 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9614 rtx_insn *sgn_insns = get_insns ();
9615 end_sequence ();
9616 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9617 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9619 /* If costs are the same then use as tie breaker the other other
9620 factor. */
9621 if (uns_cost == sgn_cost)
9623 uns_cost = seq_cost (uns_insns, !speed_p);
9624 sgn_cost = seq_cost (sgn_insns, !speed_p);
9627 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9629 emit_insn (uns_insns);
9630 return uns_ret;
9632 emit_insn (sgn_insns);
9633 return sgn_ret;
9635 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9639 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9640 enum expand_modifier modifier)
9642 rtx op0, op1, op2, temp;
9643 rtx_code_label *lab;
9644 tree type;
9645 int unsignedp;
9646 machine_mode mode;
9647 scalar_int_mode int_mode;
9648 enum tree_code code = ops->code;
9649 optab this_optab;
9650 rtx subtarget, original_target;
9651 int ignore;
9652 bool reduce_bit_field;
9653 location_t loc = ops->location;
9654 tree treeop0, treeop1, treeop2;
9655 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9656 ? reduce_to_bit_field_precision ((expr), \
9657 target, \
9658 type) \
9659 : (expr))
9661 type = ops->type;
9662 mode = TYPE_MODE (type);
9663 unsignedp = TYPE_UNSIGNED (type);
9665 treeop0 = ops->op0;
9666 treeop1 = ops->op1;
9667 treeop2 = ops->op2;
9669 /* We should be called only on simple (binary or unary) expressions,
9670 exactly those that are valid in gimple expressions that aren't
9671 GIMPLE_SINGLE_RHS (or invalid). */
9672 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9673 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9674 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9676 ignore = (target == const0_rtx
9677 || ((CONVERT_EXPR_CODE_P (code)
9678 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9679 && TREE_CODE (type) == VOID_TYPE));
9681 /* We should be called only if we need the result. */
9682 gcc_assert (!ignore);
9684 /* An operation in what may be a bit-field type needs the
9685 result to be reduced to the precision of the bit-field type,
9686 which is narrower than that of the type's mode. */
9687 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9688 && !type_has_mode_precision_p (type));
9690 if (reduce_bit_field
9691 && (modifier == EXPAND_STACK_PARM
9692 || (target && GET_MODE (target) != mode)))
9693 target = 0;
9695 /* Use subtarget as the target for operand 0 of a binary operation. */
9696 subtarget = get_subtarget (target);
9697 original_target = target;
9699 switch (code)
9701 case NON_LVALUE_EXPR:
9702 case PAREN_EXPR:
9703 CASE_CONVERT:
9704 if (treeop0 == error_mark_node)
9705 return const0_rtx;
9707 if (TREE_CODE (type) == UNION_TYPE)
9709 tree valtype = TREE_TYPE (treeop0);
9711 /* If both input and output are BLKmode, this conversion isn't doing
9712 anything except possibly changing memory attribute. */
9713 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9715 rtx result = expand_expr (treeop0, target, tmode,
9716 modifier);
9718 result = copy_rtx (result);
9719 set_mem_attributes (result, type, 0);
9720 return result;
9723 if (target == 0)
9725 if (TYPE_MODE (type) != BLKmode)
9726 target = gen_reg_rtx (TYPE_MODE (type));
9727 else
9728 target = assign_temp (type, 1, 1);
9731 if (MEM_P (target))
9732 /* Store data into beginning of memory target. */
9733 store_expr (treeop0,
9734 adjust_address (target, TYPE_MODE (valtype), 0),
9735 modifier == EXPAND_STACK_PARM,
9736 false, TYPE_REVERSE_STORAGE_ORDER (type));
9738 else
9740 gcc_assert (REG_P (target)
9741 && !TYPE_REVERSE_STORAGE_ORDER (type));
9743 /* Store this field into a union of the proper type. */
9744 poly_uint64 op0_size
9745 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9746 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9747 store_field (target,
9748 /* The conversion must be constructed so that
9749 we know at compile time how many bits
9750 to preserve. */
9751 ordered_min (op0_size, union_size),
9752 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9753 false, false);
9756 /* Return the entire union. */
9757 return target;
9760 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9762 op0 = expand_expr (treeop0, target, VOIDmode,
9763 modifier);
9765 return REDUCE_BIT_FIELD (op0);
9768 op0 = expand_expr (treeop0, NULL_RTX, mode,
9769 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9770 if (GET_MODE (op0) == mode)
9773 /* If OP0 is a constant, just convert it into the proper mode. */
9774 else if (CONSTANT_P (op0))
9776 tree inner_type = TREE_TYPE (treeop0);
9777 machine_mode inner_mode = GET_MODE (op0);
9779 if (inner_mode == VOIDmode)
9780 inner_mode = TYPE_MODE (inner_type);
9782 if (modifier == EXPAND_INITIALIZER)
9783 op0 = lowpart_subreg (mode, op0, inner_mode);
9784 else
9785 op0= convert_modes (mode, inner_mode, op0,
9786 TYPE_UNSIGNED (inner_type));
9789 else if (modifier == EXPAND_INITIALIZER)
9790 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9791 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9793 else if (target == 0)
9794 op0 = convert_to_mode (mode, op0,
9795 TYPE_UNSIGNED (TREE_TYPE
9796 (treeop0)));
9797 else
9799 convert_move (target, op0,
9800 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9801 op0 = target;
9804 return REDUCE_BIT_FIELD (op0);
9806 case ADDR_SPACE_CONVERT_EXPR:
9808 tree treeop0_type = TREE_TYPE (treeop0);
9810 gcc_assert (POINTER_TYPE_P (type));
9811 gcc_assert (POINTER_TYPE_P (treeop0_type));
9813 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9814 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9816 /* Conversions between pointers to the same address space should
9817 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9818 gcc_assert (as_to != as_from);
9820 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9822 /* Ask target code to handle conversion between pointers
9823 to overlapping address spaces. */
9824 if (targetm.addr_space.subset_p (as_to, as_from)
9825 || targetm.addr_space.subset_p (as_from, as_to))
9827 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9829 else
9831 /* For disjoint address spaces, converting anything but a null
9832 pointer invokes undefined behavior. We truncate or extend the
9833 value as if we'd converted via integers, which handles 0 as
9834 required, and all others as the programmer likely expects. */
9835 #ifndef POINTERS_EXTEND_UNSIGNED
9836 const int POINTERS_EXTEND_UNSIGNED = 1;
9837 #endif
9838 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9839 op0, POINTERS_EXTEND_UNSIGNED);
9841 gcc_assert (op0);
9842 return op0;
9845 case POINTER_PLUS_EXPR:
9846 /* Even though the sizetype mode and the pointer's mode can be different
9847 expand is able to handle this correctly and get the correct result out
9848 of the PLUS_EXPR code. */
9849 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9850 if sizetype precision is smaller than pointer precision. */
9851 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9852 treeop1 = fold_convert_loc (loc, type,
9853 fold_convert_loc (loc, ssizetype,
9854 treeop1));
9855 /* If sizetype precision is larger than pointer precision, truncate the
9856 offset to have matching modes. */
9857 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9858 treeop1 = fold_convert_loc (loc, type, treeop1);
9859 /* FALLTHRU */
9861 case PLUS_EXPR:
9862 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9863 something else, make sure we add the register to the constant and
9864 then to the other thing. This case can occur during strength
9865 reduction and doing it this way will produce better code if the
9866 frame pointer or argument pointer is eliminated.
9868 fold-const.cc will ensure that the constant is always in the inner
9869 PLUS_EXPR, so the only case we need to do anything about is if
9870 sp, ap, or fp is our second argument, in which case we must swap
9871 the innermost first argument and our second argument. */
9873 if (TREE_CODE (treeop0) == PLUS_EXPR
9874 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9875 && VAR_P (treeop1)
9876 && (DECL_RTL (treeop1) == frame_pointer_rtx
9877 || DECL_RTL (treeop1) == stack_pointer_rtx
9878 || DECL_RTL (treeop1) == arg_pointer_rtx))
9880 gcc_unreachable ();
9883 /* If the result is to be ptr_mode and we are adding an integer to
9884 something, we might be forming a constant. So try to use
9885 plus_constant. If it produces a sum and we can't accept it,
9886 use force_operand. This allows P = &ARR[const] to generate
9887 efficient code on machines where a SYMBOL_REF is not a valid
9888 address.
9890 If this is an EXPAND_SUM call, always return the sum. */
9891 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9892 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9894 if (modifier == EXPAND_STACK_PARM)
9895 target = 0;
9896 if (TREE_CODE (treeop0) == INTEGER_CST
9897 && HWI_COMPUTABLE_MODE_P (mode)
9898 && TREE_CONSTANT (treeop1))
9900 rtx constant_part;
9901 HOST_WIDE_INT wc;
9902 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9904 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9905 EXPAND_SUM);
9906 /* Use wi::shwi to ensure that the constant is
9907 truncated according to the mode of OP1, then sign extended
9908 to a HOST_WIDE_INT. Using the constant directly can result
9909 in non-canonical RTL in a 64x32 cross compile. */
9910 wc = TREE_INT_CST_LOW (treeop0);
9911 constant_part =
9912 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9913 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9914 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9915 op1 = force_operand (op1, target);
9916 return REDUCE_BIT_FIELD (op1);
9919 else if (TREE_CODE (treeop1) == INTEGER_CST
9920 && HWI_COMPUTABLE_MODE_P (mode)
9921 && TREE_CONSTANT (treeop0))
9923 rtx constant_part;
9924 HOST_WIDE_INT wc;
9925 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9927 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9928 (modifier == EXPAND_INITIALIZER
9929 ? EXPAND_INITIALIZER : EXPAND_SUM));
9930 if (! CONSTANT_P (op0))
9932 op1 = expand_expr (treeop1, NULL_RTX,
9933 VOIDmode, modifier);
9934 /* Return a PLUS if modifier says it's OK. */
9935 if (modifier == EXPAND_SUM
9936 || modifier == EXPAND_INITIALIZER)
9937 return simplify_gen_binary (PLUS, mode, op0, op1);
9938 goto binop2;
9940 /* Use wi::shwi to ensure that the constant is
9941 truncated according to the mode of OP1, then sign extended
9942 to a HOST_WIDE_INT. Using the constant directly can result
9943 in non-canonical RTL in a 64x32 cross compile. */
9944 wc = TREE_INT_CST_LOW (treeop1);
9945 constant_part
9946 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9947 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9948 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9949 op0 = force_operand (op0, target);
9950 return REDUCE_BIT_FIELD (op0);
9954 /* Use TER to expand pointer addition of a negated value
9955 as pointer subtraction. */
9956 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9957 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9958 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9959 && TREE_CODE (treeop1) == SSA_NAME
9960 && TYPE_MODE (TREE_TYPE (treeop0))
9961 == TYPE_MODE (TREE_TYPE (treeop1)))
9963 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9964 if (def)
9966 treeop1 = gimple_assign_rhs1 (def);
9967 code = MINUS_EXPR;
9968 goto do_minus;
9972 /* No sense saving up arithmetic to be done
9973 if it's all in the wrong mode to form part of an address.
9974 And force_operand won't know whether to sign-extend or
9975 zero-extend. */
9976 if (modifier != EXPAND_INITIALIZER
9977 && (modifier != EXPAND_SUM || mode != ptr_mode))
9979 expand_operands (treeop0, treeop1,
9980 subtarget, &op0, &op1, modifier);
9981 if (op0 == const0_rtx)
9982 return op1;
9983 if (op1 == const0_rtx)
9984 return op0;
9985 goto binop2;
9988 expand_operands (treeop0, treeop1,
9989 subtarget, &op0, &op1, modifier);
9990 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9992 case MINUS_EXPR:
9993 case POINTER_DIFF_EXPR:
9994 do_minus:
9995 /* For initializers, we are allowed to return a MINUS of two
9996 symbolic constants. Here we handle all cases when both operands
9997 are constant. */
9998 /* Handle difference of two symbolic constants,
9999 for the sake of an initializer. */
10000 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10001 && really_constant_p (treeop0)
10002 && really_constant_p (treeop1))
10004 expand_operands (treeop0, treeop1,
10005 NULL_RTX, &op0, &op1, modifier);
10006 return simplify_gen_binary (MINUS, mode, op0, op1);
10009 /* No sense saving up arithmetic to be done
10010 if it's all in the wrong mode to form part of an address.
10011 And force_operand won't know whether to sign-extend or
10012 zero-extend. */
10013 if (modifier != EXPAND_INITIALIZER
10014 && (modifier != EXPAND_SUM || mode != ptr_mode))
10015 goto binop;
10017 expand_operands (treeop0, treeop1,
10018 subtarget, &op0, &op1, modifier);
10020 /* Convert A - const to A + (-const). */
10021 if (CONST_INT_P (op1))
10023 op1 = negate_rtx (mode, op1);
10024 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
10027 goto binop2;
10029 case WIDEN_MULT_PLUS_EXPR:
10030 case WIDEN_MULT_MINUS_EXPR:
10031 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10032 op2 = expand_normal (treeop2);
10033 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10034 target, unsignedp);
10035 return target;
10037 case WIDEN_MULT_EXPR:
10038 /* If first operand is constant, swap them.
10039 Thus the following special case checks need only
10040 check the second operand. */
10041 if (TREE_CODE (treeop0) == INTEGER_CST)
10042 std::swap (treeop0, treeop1);
10044 /* First, check if we have a multiplication of one signed and one
10045 unsigned operand. */
10046 if (TREE_CODE (treeop1) != INTEGER_CST
10047 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
10048 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
10050 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
10051 this_optab = usmul_widen_optab;
10052 if (find_widening_optab_handler (this_optab, mode, innermode)
10053 != CODE_FOR_nothing)
10055 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10056 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10057 EXPAND_NORMAL);
10058 else
10059 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
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 op0 = convert_modes (mode, innermode, op0, true);
10066 op1 = convert_modes (mode, innermode, op1, false);
10067 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10068 target, unsignedp));
10070 goto binop3;
10073 /* Check for a multiplication with matching signedness. */
10074 else if ((TREE_CODE (treeop1) == INTEGER_CST
10075 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
10076 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
10077 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
10079 tree op0type = TREE_TYPE (treeop0);
10080 machine_mode innermode = TYPE_MODE (op0type);
10081 bool zextend_p = TYPE_UNSIGNED (op0type);
10082 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
10083 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
10085 if (TREE_CODE (treeop0) != INTEGER_CST)
10087 if (find_widening_optab_handler (this_optab, mode, innermode)
10088 != CODE_FOR_nothing)
10090 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10091 EXPAND_NORMAL);
10092 /* op0 and op1 might still be constant, despite the above
10093 != INTEGER_CST check. Handle it. */
10094 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10096 widen_mult_const:
10097 op0 = convert_modes (mode, innermode, op0, zextend_p);
10099 = convert_modes (mode, innermode, op1,
10100 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
10101 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10102 target,
10103 unsignedp));
10105 temp = expand_widening_mult (mode, op0, op1, target,
10106 unsignedp, this_optab);
10107 return REDUCE_BIT_FIELD (temp);
10109 if (find_widening_optab_handler (other_optab, mode, innermode)
10110 != CODE_FOR_nothing
10111 && innermode == word_mode)
10113 rtx htem, hipart;
10114 op0 = expand_normal (treeop0);
10115 op1 = expand_normal (treeop1);
10116 /* op0 and op1 might be constants, despite the above
10117 != INTEGER_CST check. Handle it. */
10118 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10119 goto widen_mult_const;
10120 temp = expand_binop (mode, other_optab, op0, op1, target,
10121 unsignedp, OPTAB_LIB_WIDEN);
10122 hipart = gen_highpart (word_mode, temp);
10123 htem = expand_mult_highpart_adjust (word_mode, hipart,
10124 op0, op1, hipart,
10125 zextend_p);
10126 if (htem != hipart)
10127 emit_move_insn (hipart, htem);
10128 return REDUCE_BIT_FIELD (temp);
10132 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
10133 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
10134 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10135 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10137 case MULT_EXPR:
10138 /* If this is a fixed-point operation, then we cannot use the code
10139 below because "expand_mult" doesn't support sat/no-sat fixed-point
10140 multiplications. */
10141 if (ALL_FIXED_POINT_MODE_P (mode))
10142 goto binop;
10144 /* If first operand is constant, swap them.
10145 Thus the following special case checks need only
10146 check the second operand. */
10147 if (TREE_CODE (treeop0) == INTEGER_CST)
10148 std::swap (treeop0, treeop1);
10150 /* Attempt to return something suitable for generating an
10151 indexed address, for machines that support that. */
10153 if (modifier == EXPAND_SUM && mode == ptr_mode
10154 && tree_fits_shwi_p (treeop1))
10156 tree exp1 = treeop1;
10158 op0 = expand_expr (treeop0, subtarget, VOIDmode,
10159 EXPAND_SUM);
10161 if (!REG_P (op0))
10162 op0 = force_operand (op0, NULL_RTX);
10163 if (!REG_P (op0))
10164 op0 = copy_to_mode_reg (mode, op0);
10166 op1 = gen_int_mode (tree_to_shwi (exp1),
10167 TYPE_MODE (TREE_TYPE (exp1)));
10168 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
10171 if (modifier == EXPAND_STACK_PARM)
10172 target = 0;
10174 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
10176 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
10177 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
10178 if (def_stmt0
10179 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
10180 def_stmt0 = NULL;
10181 if (def_stmt1
10182 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
10183 def_stmt1 = NULL;
10185 if (def_stmt0 || def_stmt1)
10187 /* X / Y * Y can be expanded as X - X % Y too.
10188 Choose the cheaper sequence of those two. */
10189 if (def_stmt0)
10190 treeop0 = gimple_assign_rhs1 (def_stmt0);
10191 else
10193 treeop1 = treeop0;
10194 treeop0 = gimple_assign_rhs1 (def_stmt1);
10196 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
10197 EXPAND_NORMAL);
10198 bool speed_p = optimize_insn_for_speed_p ();
10199 do_pending_stack_adjust ();
10200 start_sequence ();
10201 rtx divmul_ret
10202 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
10203 op0, op1, NULL_RTX, unsignedp);
10204 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
10205 unsignedp);
10206 rtx_insn *divmul_insns = get_insns ();
10207 end_sequence ();
10208 start_sequence ();
10209 rtx modsub_ret
10210 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
10211 op0, op1, NULL_RTX, unsignedp);
10212 this_optab = optab_for_tree_code (MINUS_EXPR, type,
10213 optab_default);
10214 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
10215 target, unsignedp, OPTAB_LIB_WIDEN);
10216 rtx_insn *modsub_insns = get_insns ();
10217 end_sequence ();
10218 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
10219 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
10220 /* If costs are the same then use as tie breaker the other other
10221 factor. */
10222 if (divmul_cost == modsub_cost)
10224 divmul_cost = seq_cost (divmul_insns, !speed_p);
10225 modsub_cost = seq_cost (modsub_insns, !speed_p);
10228 if (divmul_cost <= modsub_cost)
10230 emit_insn (divmul_insns);
10231 return REDUCE_BIT_FIELD (divmul_ret);
10233 emit_insn (modsub_insns);
10234 return REDUCE_BIT_FIELD (modsub_ret);
10238 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10240 /* Expand X*Y as X&-Y when Y must be zero or one. */
10241 if (SCALAR_INT_MODE_P (mode))
10243 bool gimple_zero_one_valued_p (tree, tree (*)(tree));
10244 bool bit0_p = gimple_zero_one_valued_p (treeop0, nullptr);
10245 bool bit1_p = gimple_zero_one_valued_p (treeop1, nullptr);
10247 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
10248 if (bit0_p && bit1_p)
10249 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
10251 if (bit0_p || bit1_p)
10253 bool speed = optimize_insn_for_speed_p ();
10254 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
10255 struct algorithm algorithm;
10256 enum mult_variant variant;
10257 if (CONST_INT_P (op1)
10258 ? !choose_mult_variant (mode, INTVAL (op1),
10259 &algorithm, &variant, cost)
10260 : cost < mul_cost (speed, mode))
10262 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
10263 op1, target)
10264 : expand_and (mode, op0,
10265 negate_rtx (mode, op1),
10266 target);
10267 return REDUCE_BIT_FIELD (target);
10272 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10274 case TRUNC_MOD_EXPR:
10275 case FLOOR_MOD_EXPR:
10276 case CEIL_MOD_EXPR:
10277 case ROUND_MOD_EXPR:
10279 case TRUNC_DIV_EXPR:
10280 case FLOOR_DIV_EXPR:
10281 case CEIL_DIV_EXPR:
10282 case ROUND_DIV_EXPR:
10283 case EXACT_DIV_EXPR:
10284 /* If this is a fixed-point operation, then we cannot use the code
10285 below because "expand_divmod" doesn't support sat/no-sat fixed-point
10286 divisions. */
10287 if (ALL_FIXED_POINT_MODE_P (mode))
10288 goto binop;
10290 if (modifier == EXPAND_STACK_PARM)
10291 target = 0;
10292 /* Possible optimization: compute the dividend with EXPAND_SUM
10293 then if the divisor is constant can optimize the case
10294 where some terms of the dividend have coeffs divisible by it. */
10295 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10296 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
10297 target, unsignedp);
10299 case RDIV_EXPR:
10300 goto binop;
10302 case MULT_HIGHPART_EXPR:
10303 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10304 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
10305 gcc_assert (temp);
10306 return temp;
10308 case FIXED_CONVERT_EXPR:
10309 op0 = expand_normal (treeop0);
10310 if (target == 0 || modifier == EXPAND_STACK_PARM)
10311 target = gen_reg_rtx (mode);
10313 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
10314 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10315 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
10316 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
10317 else
10318 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
10319 return target;
10321 case FIX_TRUNC_EXPR:
10322 op0 = expand_normal (treeop0);
10323 if (target == 0 || modifier == EXPAND_STACK_PARM)
10324 target = gen_reg_rtx (mode);
10325 expand_fix (target, op0, unsignedp);
10326 return target;
10328 case FLOAT_EXPR:
10329 op0 = expand_normal (treeop0);
10330 if (target == 0 || modifier == EXPAND_STACK_PARM)
10331 target = gen_reg_rtx (mode);
10332 /* expand_float can't figure out what to do if FROM has VOIDmode.
10333 So give it the correct mode. With -O, cse will optimize this. */
10334 if (GET_MODE (op0) == VOIDmode)
10335 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
10336 op0);
10337 expand_float (target, op0,
10338 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10339 return target;
10341 case NEGATE_EXPR:
10342 op0 = expand_expr (treeop0, subtarget,
10343 VOIDmode, EXPAND_NORMAL);
10344 if (modifier == EXPAND_STACK_PARM)
10345 target = 0;
10346 temp = expand_unop (mode,
10347 optab_for_tree_code (NEGATE_EXPR, type,
10348 optab_default),
10349 op0, target, 0);
10350 gcc_assert (temp);
10351 return REDUCE_BIT_FIELD (temp);
10353 case ABS_EXPR:
10354 case ABSU_EXPR:
10355 op0 = expand_expr (treeop0, subtarget,
10356 VOIDmode, EXPAND_NORMAL);
10357 if (modifier == EXPAND_STACK_PARM)
10358 target = 0;
10360 /* ABS_EXPR is not valid for complex arguments. */
10361 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10362 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
10364 /* Unsigned abs is simply the operand. Testing here means we don't
10365 risk generating incorrect code below. */
10366 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10367 return op0;
10369 return expand_abs (mode, op0, target, unsignedp,
10370 safe_from_p (target, treeop0, 1));
10372 case MAX_EXPR:
10373 case MIN_EXPR:
10374 target = original_target;
10375 if (target == 0
10376 || modifier == EXPAND_STACK_PARM
10377 || (MEM_P (target) && MEM_VOLATILE_P (target))
10378 || GET_MODE (target) != mode
10379 || (REG_P (target)
10380 && REGNO (target) < FIRST_PSEUDO_REGISTER))
10381 target = gen_reg_rtx (mode);
10382 expand_operands (treeop0, treeop1,
10383 target, &op0, &op1, EXPAND_NORMAL);
10385 /* First try to do it with a special MIN or MAX instruction.
10386 If that does not win, use a conditional jump to select the proper
10387 value. */
10388 this_optab = optab_for_tree_code (code, type, optab_default);
10389 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
10390 OPTAB_WIDEN);
10391 if (temp != 0)
10392 return temp;
10394 if (VECTOR_TYPE_P (type))
10395 gcc_unreachable ();
10397 /* At this point, a MEM target is no longer useful; we will get better
10398 code without it. */
10400 if (! REG_P (target))
10401 target = gen_reg_rtx (mode);
10403 /* If op1 was placed in target, swap op0 and op1. */
10404 if (target != op0 && target == op1)
10405 std::swap (op0, op1);
10407 /* We generate better code and avoid problems with op1 mentioning
10408 target by forcing op1 into a pseudo if it isn't a constant. */
10409 if (! CONSTANT_P (op1))
10410 op1 = force_reg (mode, op1);
10413 enum rtx_code comparison_code;
10414 rtx cmpop1 = op1;
10416 if (code == MAX_EXPR)
10417 comparison_code = unsignedp ? GEU : GE;
10418 else
10419 comparison_code = unsignedp ? LEU : LE;
10421 /* Canonicalize to comparisons against 0. */
10422 if (op1 == const1_rtx)
10424 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10425 or (a != 0 ? a : 1) for unsigned.
10426 For MIN we are safe converting (a <= 1 ? a : 1)
10427 into (a <= 0 ? a : 1) */
10428 cmpop1 = const0_rtx;
10429 if (code == MAX_EXPR)
10430 comparison_code = unsignedp ? NE : GT;
10432 if (op1 == constm1_rtx && !unsignedp)
10434 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10435 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10436 cmpop1 = const0_rtx;
10437 if (code == MIN_EXPR)
10438 comparison_code = LT;
10441 /* Use a conditional move if possible. */
10442 if (can_conditionally_move_p (mode))
10444 rtx insn;
10446 start_sequence ();
10448 /* Try to emit the conditional move. */
10449 insn = emit_conditional_move (target,
10450 { comparison_code,
10451 op0, cmpop1, mode },
10452 op0, op1, mode,
10453 unsignedp);
10455 /* If we could do the conditional move, emit the sequence,
10456 and return. */
10457 if (insn)
10459 rtx_insn *seq = get_insns ();
10460 end_sequence ();
10461 emit_insn (seq);
10462 return target;
10465 /* Otherwise discard the sequence and fall back to code with
10466 branches. */
10467 end_sequence ();
10470 if (target != op0)
10471 emit_move_insn (target, op0);
10473 lab = gen_label_rtx ();
10474 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10475 unsignedp, mode, NULL_RTX, NULL, lab,
10476 profile_probability::uninitialized ());
10478 emit_move_insn (target, op1);
10479 emit_label (lab);
10480 return target;
10482 case BIT_NOT_EXPR:
10483 op0 = expand_expr (treeop0, subtarget,
10484 VOIDmode, EXPAND_NORMAL);
10485 if (modifier == EXPAND_STACK_PARM)
10486 target = 0;
10487 /* In case we have to reduce the result to bitfield precision
10488 for unsigned bitfield expand this as XOR with a proper constant
10489 instead. */
10490 if (reduce_bit_field && TYPE_UNSIGNED (type))
10492 int_mode = SCALAR_INT_TYPE_MODE (type);
10493 wide_int mask = wi::mask (TYPE_PRECISION (type),
10494 false, GET_MODE_PRECISION (int_mode));
10496 temp = expand_binop (int_mode, xor_optab, op0,
10497 immed_wide_int_const (mask, int_mode),
10498 target, 1, OPTAB_LIB_WIDEN);
10500 else
10501 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10502 gcc_assert (temp);
10503 return temp;
10505 /* ??? Can optimize bitwise operations with one arg constant.
10506 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10507 and (a bitwise1 b) bitwise2 b (etc)
10508 but that is probably not worth while. */
10510 case BIT_AND_EXPR:
10511 case BIT_IOR_EXPR:
10512 case BIT_XOR_EXPR:
10513 goto binop;
10515 case LROTATE_EXPR:
10516 case RROTATE_EXPR:
10517 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10518 || type_has_mode_precision_p (type));
10519 /* fall through */
10521 case LSHIFT_EXPR:
10522 case RSHIFT_EXPR:
10524 /* If this is a fixed-point operation, then we cannot use the code
10525 below because "expand_shift" doesn't support sat/no-sat fixed-point
10526 shifts. */
10527 if (ALL_FIXED_POINT_MODE_P (mode))
10528 goto binop;
10530 if (! safe_from_p (subtarget, treeop1, 1))
10531 subtarget = 0;
10532 if (modifier == EXPAND_STACK_PARM)
10533 target = 0;
10534 op0 = expand_expr (treeop0, subtarget,
10535 VOIDmode, EXPAND_NORMAL);
10537 /* Left shift optimization when shifting across word_size boundary.
10539 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10540 there isn't native instruction to support this wide mode
10541 left shift. Given below scenario:
10543 Type A = (Type) B << C
10545 |< T >|
10546 | dest_high | dest_low |
10548 | word_size |
10550 If the shift amount C caused we shift B to across the word
10551 size boundary, i.e part of B shifted into high half of
10552 destination register, and part of B remains in the low
10553 half, then GCC will use the following left shift expand
10554 logic:
10556 1. Initialize dest_low to B.
10557 2. Initialize every bit of dest_high to the sign bit of B.
10558 3. Logic left shift dest_low by C bit to finalize dest_low.
10559 The value of dest_low before this shift is kept in a temp D.
10560 4. Logic left shift dest_high by C.
10561 5. Logic right shift D by (word_size - C).
10562 6. Or the result of 4 and 5 to finalize dest_high.
10564 While, by checking gimple statements, if operand B is
10565 coming from signed extension, then we can simplify above
10566 expand logic into:
10568 1. dest_high = src_low >> (word_size - C).
10569 2. dest_low = src_low << C.
10571 We can use one arithmetic right shift to finish all the
10572 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10573 needed from 6 into 2.
10575 The case is similar for zero extension, except that we
10576 initialize dest_high to zero rather than copies of the sign
10577 bit from B. Furthermore, we need to use a logical right shift
10578 in this case.
10580 The choice of sign-extension versus zero-extension is
10581 determined entirely by whether or not B is signed and is
10582 independent of the current setting of unsignedp. */
10584 temp = NULL_RTX;
10585 if (code == LSHIFT_EXPR
10586 && target
10587 && REG_P (target)
10588 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10589 && mode == int_mode
10590 && TREE_CONSTANT (treeop1)
10591 && TREE_CODE (treeop0) == SSA_NAME)
10593 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10594 if (is_gimple_assign (def)
10595 && gimple_assign_rhs_code (def) == NOP_EXPR)
10597 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10598 (TREE_TYPE (gimple_assign_rhs1 (def)));
10600 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10601 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10602 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10603 >= GET_MODE_BITSIZE (word_mode)))
10605 rtx_insn *seq, *seq_old;
10606 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10607 int_mode);
10608 bool extend_unsigned
10609 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10610 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10611 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10612 rtx dest_high = simplify_gen_subreg (word_mode, target,
10613 int_mode, high_off);
10614 HOST_WIDE_INT ramount = (BITS_PER_WORD
10615 - TREE_INT_CST_LOW (treeop1));
10616 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10618 start_sequence ();
10619 /* dest_high = src_low >> (word_size - C). */
10620 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10621 rshift, dest_high,
10622 extend_unsigned);
10623 if (temp != dest_high)
10624 emit_move_insn (dest_high, temp);
10626 /* dest_low = src_low << C. */
10627 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10628 treeop1, dest_low, unsignedp);
10629 if (temp != dest_low)
10630 emit_move_insn (dest_low, temp);
10632 seq = get_insns ();
10633 end_sequence ();
10634 temp = target ;
10636 if (have_insn_for (ASHIFT, int_mode))
10638 bool speed_p = optimize_insn_for_speed_p ();
10639 start_sequence ();
10640 rtx ret_old = expand_variable_shift (code, int_mode,
10641 op0, treeop1,
10642 target,
10643 unsignedp);
10645 seq_old = get_insns ();
10646 end_sequence ();
10647 if (seq_cost (seq, speed_p)
10648 >= seq_cost (seq_old, speed_p))
10650 seq = seq_old;
10651 temp = ret_old;
10654 emit_insn (seq);
10659 if (temp == NULL_RTX)
10660 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10661 unsignedp);
10662 if (code == LSHIFT_EXPR)
10663 temp = REDUCE_BIT_FIELD (temp);
10664 return temp;
10667 /* Could determine the answer when only additive constants differ. Also,
10668 the addition of one can be handled by changing the condition. */
10669 case LT_EXPR:
10670 case LE_EXPR:
10671 case GT_EXPR:
10672 case GE_EXPR:
10673 case EQ_EXPR:
10674 case NE_EXPR:
10675 case UNORDERED_EXPR:
10676 case ORDERED_EXPR:
10677 case UNLT_EXPR:
10678 case UNLE_EXPR:
10679 case UNGT_EXPR:
10680 case UNGE_EXPR:
10681 case UNEQ_EXPR:
10682 case LTGT_EXPR:
10684 temp = do_store_flag (ops,
10685 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10686 tmode != VOIDmode ? tmode : mode);
10687 if (temp)
10688 return temp;
10690 /* Use a compare and a jump for BLKmode comparisons, or for function
10691 type comparisons is have_canonicalize_funcptr_for_compare. */
10693 if ((target == 0
10694 || modifier == EXPAND_STACK_PARM
10695 || ! safe_from_p (target, treeop0, 1)
10696 || ! safe_from_p (target, treeop1, 1)
10697 /* Make sure we don't have a hard reg (such as function's return
10698 value) live across basic blocks, if not optimizing. */
10699 || (!optimize && REG_P (target)
10700 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10701 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10703 emit_move_insn (target, const0_rtx);
10705 rtx_code_label *lab1 = gen_label_rtx ();
10706 jumpifnot_1 (code, treeop0, treeop1, lab1,
10707 profile_probability::uninitialized ());
10709 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10710 emit_move_insn (target, constm1_rtx);
10711 else
10712 emit_move_insn (target, const1_rtx);
10714 emit_label (lab1);
10715 return target;
10717 case COMPLEX_EXPR:
10718 /* Get the rtx code of the operands. */
10719 op0 = expand_normal (treeop0);
10720 op1 = expand_normal (treeop1);
10722 if (!target)
10723 target = gen_reg_rtx (TYPE_MODE (type));
10724 else
10725 /* If target overlaps with op1, then either we need to force
10726 op1 into a pseudo (if target also overlaps with op0),
10727 or write the complex parts in reverse order. */
10728 switch (GET_CODE (target))
10730 case CONCAT:
10731 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10733 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10735 complex_expr_force_op1:
10736 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10737 emit_move_insn (temp, op1);
10738 op1 = temp;
10739 break;
10741 complex_expr_swap_order:
10742 /* Move the imaginary (op1) and real (op0) parts to their
10743 location. */
10744 write_complex_part (target, op1, true, true);
10745 write_complex_part (target, op0, false, false);
10747 return target;
10749 break;
10750 case MEM:
10751 temp = adjust_address_nv (target,
10752 GET_MODE_INNER (GET_MODE (target)), 0);
10753 if (reg_overlap_mentioned_p (temp, op1))
10755 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10756 temp = adjust_address_nv (target, imode,
10757 GET_MODE_SIZE (imode));
10758 if (reg_overlap_mentioned_p (temp, op0))
10759 goto complex_expr_force_op1;
10760 goto complex_expr_swap_order;
10762 break;
10763 default:
10764 if (reg_overlap_mentioned_p (target, op1))
10766 if (reg_overlap_mentioned_p (target, op0))
10767 goto complex_expr_force_op1;
10768 goto complex_expr_swap_order;
10770 break;
10773 /* Move the real (op0) and imaginary (op1) parts to their location. */
10774 write_complex_part (target, op0, false, true);
10775 write_complex_part (target, op1, true, false);
10777 return target;
10779 case WIDEN_SUM_EXPR:
10781 tree oprnd0 = treeop0;
10782 tree oprnd1 = treeop1;
10784 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10785 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10786 target, unsignedp);
10787 return target;
10790 case VEC_UNPACK_HI_EXPR:
10791 case VEC_UNPACK_LO_EXPR:
10792 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10793 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10795 op0 = expand_normal (treeop0);
10796 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10797 target, unsignedp);
10798 gcc_assert (temp);
10799 return temp;
10802 case VEC_UNPACK_FLOAT_HI_EXPR:
10803 case VEC_UNPACK_FLOAT_LO_EXPR:
10805 op0 = expand_normal (treeop0);
10806 /* The signedness is determined from input operand. */
10807 temp = expand_widen_pattern_expr
10808 (ops, op0, NULL_RTX, NULL_RTX,
10809 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10811 gcc_assert (temp);
10812 return temp;
10815 case VEC_WIDEN_MULT_HI_EXPR:
10816 case VEC_WIDEN_MULT_LO_EXPR:
10817 case VEC_WIDEN_MULT_EVEN_EXPR:
10818 case VEC_WIDEN_MULT_ODD_EXPR:
10819 case VEC_WIDEN_LSHIFT_HI_EXPR:
10820 case VEC_WIDEN_LSHIFT_LO_EXPR:
10821 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10822 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10823 target, unsignedp);
10824 gcc_assert (target);
10825 return target;
10827 case VEC_PACK_SAT_EXPR:
10828 case VEC_PACK_FIX_TRUNC_EXPR:
10829 mode = TYPE_MODE (TREE_TYPE (treeop0));
10830 subtarget = NULL_RTX;
10831 goto binop;
10833 case VEC_PACK_TRUNC_EXPR:
10834 if (VECTOR_BOOLEAN_TYPE_P (type)
10835 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10836 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10837 && SCALAR_INT_MODE_P (mode))
10839 class expand_operand eops[4];
10840 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10841 expand_operands (treeop0, treeop1,
10842 subtarget, &op0, &op1, EXPAND_NORMAL);
10843 this_optab = vec_pack_sbool_trunc_optab;
10844 enum insn_code icode = optab_handler (this_optab, imode);
10845 create_output_operand (&eops[0], target, mode);
10846 create_convert_operand_from (&eops[1], op0, imode, false);
10847 create_convert_operand_from (&eops[2], op1, imode, false);
10848 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10849 create_input_operand (&eops[3], temp, imode);
10850 expand_insn (icode, 4, eops);
10851 return eops[0].value;
10853 mode = TYPE_MODE (TREE_TYPE (treeop0));
10854 subtarget = NULL_RTX;
10855 goto binop;
10857 case VEC_PACK_FLOAT_EXPR:
10858 mode = TYPE_MODE (TREE_TYPE (treeop0));
10859 expand_operands (treeop0, treeop1,
10860 subtarget, &op0, &op1, EXPAND_NORMAL);
10861 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10862 optab_default);
10863 target = expand_binop (mode, this_optab, op0, op1, target,
10864 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10865 OPTAB_LIB_WIDEN);
10866 gcc_assert (target);
10867 return target;
10869 case VEC_PERM_EXPR:
10871 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10872 vec_perm_builder sel;
10873 if (TREE_CODE (treeop2) == VECTOR_CST
10874 && tree_to_vec_perm_builder (&sel, treeop2))
10876 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10877 temp = expand_vec_perm_const (mode, op0, op1, sel,
10878 sel_mode, target);
10880 else
10882 op2 = expand_normal (treeop2);
10883 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10885 gcc_assert (temp);
10886 return temp;
10889 case DOT_PROD_EXPR:
10891 tree oprnd0 = treeop0;
10892 tree oprnd1 = treeop1;
10893 tree oprnd2 = treeop2;
10895 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10896 op2 = expand_normal (oprnd2);
10897 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10898 target, unsignedp);
10899 return target;
10902 case SAD_EXPR:
10904 tree oprnd0 = treeop0;
10905 tree oprnd1 = treeop1;
10906 tree oprnd2 = treeop2;
10908 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10909 op2 = expand_normal (oprnd2);
10910 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10911 target, unsignedp);
10912 return target;
10915 case REALIGN_LOAD_EXPR:
10917 tree oprnd0 = treeop0;
10918 tree oprnd1 = treeop1;
10919 tree oprnd2 = treeop2;
10921 this_optab = optab_for_tree_code (code, type, optab_default);
10922 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10923 op2 = expand_normal (oprnd2);
10924 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10925 target, unsignedp);
10926 gcc_assert (temp);
10927 return temp;
10930 case COND_EXPR:
10932 /* A COND_EXPR with its type being VOID_TYPE represents a
10933 conditional jump and is handled in
10934 expand_gimple_cond_expr. */
10935 gcc_assert (!VOID_TYPE_P (type));
10937 /* Note that COND_EXPRs whose type is a structure or union
10938 are required to be constructed to contain assignments of
10939 a temporary variable, so that we can evaluate them here
10940 for side effect only. If type is void, we must do likewise. */
10942 gcc_assert (!TREE_ADDRESSABLE (type)
10943 && !ignore
10944 && TREE_TYPE (treeop1) != void_type_node
10945 && TREE_TYPE (treeop2) != void_type_node);
10947 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10948 if (temp)
10949 return temp;
10951 /* If we are not to produce a result, we have no target. Otherwise,
10952 if a target was specified use it; it will not be used as an
10953 intermediate target unless it is safe. If no target, use a
10954 temporary. */
10956 if (modifier != EXPAND_STACK_PARM
10957 && original_target
10958 && safe_from_p (original_target, treeop0, 1)
10959 && GET_MODE (original_target) == mode
10960 && !MEM_P (original_target))
10961 temp = original_target;
10962 else
10963 temp = assign_temp (type, 0, 1);
10965 do_pending_stack_adjust ();
10966 NO_DEFER_POP;
10967 rtx_code_label *lab0 = gen_label_rtx ();
10968 rtx_code_label *lab1 = gen_label_rtx ();
10969 jumpifnot (treeop0, lab0,
10970 profile_probability::uninitialized ());
10971 store_expr (treeop1, temp,
10972 modifier == EXPAND_STACK_PARM,
10973 false, false);
10975 emit_jump_insn (targetm.gen_jump (lab1));
10976 emit_barrier ();
10977 emit_label (lab0);
10978 store_expr (treeop2, temp,
10979 modifier == EXPAND_STACK_PARM,
10980 false, false);
10982 emit_label (lab1);
10983 OK_DEFER_POP;
10984 return temp;
10987 case VEC_DUPLICATE_EXPR:
10988 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10989 target = expand_vector_broadcast (mode, op0);
10990 gcc_assert (target);
10991 return target;
10993 case VEC_SERIES_EXPR:
10994 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10995 return expand_vec_series_expr (mode, op0, op1, target);
10997 case BIT_INSERT_EXPR:
10999 unsigned bitpos = tree_to_uhwi (treeop2);
11000 unsigned bitsize;
11001 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
11002 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
11003 else
11004 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
11005 op0 = expand_normal (treeop0);
11006 op1 = expand_normal (treeop1);
11007 rtx dst = gen_reg_rtx (mode);
11008 emit_move_insn (dst, op0);
11009 store_bit_field (dst, bitsize, bitpos, 0, 0,
11010 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
11011 return dst;
11014 default:
11015 gcc_unreachable ();
11018 /* Here to do an ordinary binary operator. */
11019 binop:
11020 expand_operands (treeop0, treeop1,
11021 subtarget, &op0, &op1, EXPAND_NORMAL);
11022 binop2:
11023 this_optab = optab_for_tree_code (code, type, optab_default);
11024 binop3:
11025 if (modifier == EXPAND_STACK_PARM)
11026 target = 0;
11027 temp = expand_binop (mode, this_optab, op0, op1, target,
11028 unsignedp, OPTAB_LIB_WIDEN);
11029 gcc_assert (temp);
11030 /* Bitwise operations do not need bitfield reduction as we expect their
11031 operands being properly truncated. */
11032 if (code == BIT_XOR_EXPR
11033 || code == BIT_AND_EXPR
11034 || code == BIT_IOR_EXPR)
11035 return temp;
11036 return REDUCE_BIT_FIELD (temp);
11038 #undef REDUCE_BIT_FIELD
11041 /* Return TRUE if expression STMT is suitable for replacement.
11042 Never consider memory loads as replaceable, because those don't ever lead
11043 into constant expressions. */
11045 static bool
11046 stmt_is_replaceable_p (gimple *stmt)
11048 if (ssa_is_replaceable_p (stmt))
11050 /* Don't move around loads. */
11051 if (!gimple_assign_single_p (stmt)
11052 || is_gimple_val (gimple_assign_rhs1 (stmt)))
11053 return true;
11055 return false;
11058 /* A subroutine of expand_expr_real_1. Expand gimple assignment G,
11059 which is known to set an SSA_NAME result. The other arguments are
11060 as for expand_expr_real_1. */
11063 expand_expr_real_gassign (gassign *g, rtx target, machine_mode tmode,
11064 enum expand_modifier modifier, rtx *alt_rtl,
11065 bool inner_reference_p)
11067 separate_ops ops;
11068 rtx r;
11069 location_t saved_loc = curr_insn_location ();
11070 auto loc = gimple_location (g);
11071 if (loc != UNKNOWN_LOCATION)
11072 set_curr_insn_location (loc);
11073 tree lhs = gimple_assign_lhs (g);
11074 ops.code = gimple_assign_rhs_code (g);
11075 ops.type = TREE_TYPE (lhs);
11076 switch (get_gimple_rhs_class (ops.code))
11078 case GIMPLE_TERNARY_RHS:
11079 ops.op2 = gimple_assign_rhs3 (g);
11080 /* Fallthru */
11081 case GIMPLE_BINARY_RHS:
11082 ops.op1 = gimple_assign_rhs2 (g);
11084 /* Try to expand conditonal compare. */
11085 if (targetm.gen_ccmp_first)
11087 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
11088 r = expand_ccmp_expr (g, TYPE_MODE (ops.type));
11089 if (r)
11090 break;
11092 /* Fallthru */
11093 case GIMPLE_UNARY_RHS:
11094 ops.op0 = gimple_assign_rhs1 (g);
11095 ops.location = loc;
11096 r = expand_expr_real_2 (&ops, target, tmode, modifier);
11097 break;
11098 case GIMPLE_SINGLE_RHS:
11100 r = expand_expr_real (gimple_assign_rhs1 (g), target,
11101 tmode, modifier, alt_rtl,
11102 inner_reference_p);
11103 break;
11105 default:
11106 gcc_unreachable ();
11108 set_curr_insn_location (saved_loc);
11109 if (REG_P (r) && !REG_EXPR (r))
11110 set_reg_attrs_for_decl_rtl (lhs, r);
11111 return r;
11115 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
11116 enum expand_modifier modifier, rtx *alt_rtl,
11117 bool inner_reference_p)
11119 rtx op0, op1, temp, decl_rtl;
11120 tree type;
11121 int unsignedp;
11122 machine_mode mode, dmode;
11123 enum tree_code code = TREE_CODE (exp);
11124 rtx subtarget, original_target;
11125 int ignore;
11126 bool reduce_bit_field;
11127 location_t loc = EXPR_LOCATION (exp);
11128 struct separate_ops ops;
11129 tree treeop0, treeop1, treeop2;
11130 tree ssa_name = NULL_TREE;
11131 gimple *g;
11133 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL
11134 expansion sign/zero extends integral types with less than mode precision
11135 when reading from bit-fields and after arithmetic operations (see
11136 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies
11137 on those extensions to have been already performed, but because of the
11138 above for _BitInt they need to be sign/zero extended when reading from
11139 locations that could be exposed to ABI boundaries (when loading from
11140 objects in memory, or function arguments, return value). Because we
11141 internally extend after arithmetic operations, we can avoid doing that
11142 when reading from SSA_NAMEs of vars. */
11143 #define EXTEND_BITINT(expr) \
11144 ((TREE_CODE (type) == BITINT_TYPE \
11145 && reduce_bit_field \
11146 && mode != BLKmode \
11147 && modifier != EXPAND_MEMORY \
11148 && modifier != EXPAND_WRITE \
11149 && modifier != EXPAND_INITIALIZER \
11150 && modifier != EXPAND_CONST_ADDRESS) \
11151 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr))
11153 type = TREE_TYPE (exp);
11154 mode = TYPE_MODE (type);
11155 unsignedp = TYPE_UNSIGNED (type);
11157 treeop0 = treeop1 = treeop2 = NULL_TREE;
11158 if (!VL_EXP_CLASS_P (exp))
11159 switch (TREE_CODE_LENGTH (code))
11161 default:
11162 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
11163 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
11164 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
11165 case 0: break;
11167 ops.code = code;
11168 ops.type = type;
11169 ops.op0 = treeop0;
11170 ops.op1 = treeop1;
11171 ops.op2 = treeop2;
11172 ops.location = loc;
11174 ignore = (target == const0_rtx
11175 || ((CONVERT_EXPR_CODE_P (code)
11176 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
11177 && TREE_CODE (type) == VOID_TYPE));
11179 /* An operation in what may be a bit-field type needs the
11180 result to be reduced to the precision of the bit-field type,
11181 which is narrower than that of the type's mode. */
11182 reduce_bit_field = (!ignore
11183 && INTEGRAL_TYPE_P (type)
11184 && !type_has_mode_precision_p (type));
11186 /* If we are going to ignore this result, we need only do something
11187 if there is a side-effect somewhere in the expression. If there
11188 is, short-circuit the most common cases here. Note that we must
11189 not call expand_expr with anything but const0_rtx in case this
11190 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
11192 if (ignore)
11194 if (! TREE_SIDE_EFFECTS (exp))
11195 return const0_rtx;
11197 /* Ensure we reference a volatile object even if value is ignored, but
11198 don't do this if all we are doing is taking its address. */
11199 if (TREE_THIS_VOLATILE (exp)
11200 && TREE_CODE (exp) != FUNCTION_DECL
11201 && mode != VOIDmode && mode != BLKmode
11202 && modifier != EXPAND_CONST_ADDRESS)
11204 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
11205 if (MEM_P (temp))
11206 copy_to_reg (temp);
11207 return const0_rtx;
11210 if (TREE_CODE_CLASS (code) == tcc_unary
11211 || code == BIT_FIELD_REF
11212 || code == COMPONENT_REF
11213 || code == INDIRECT_REF)
11214 return expand_expr (treeop0, const0_rtx, VOIDmode,
11215 modifier);
11217 else if (TREE_CODE_CLASS (code) == tcc_binary
11218 || TREE_CODE_CLASS (code) == tcc_comparison
11219 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
11221 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
11222 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
11223 return const0_rtx;
11226 target = 0;
11229 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
11230 target = 0;
11232 /* Use subtarget as the target for operand 0 of a binary operation. */
11233 subtarget = get_subtarget (target);
11234 original_target = target;
11236 switch (code)
11238 case LABEL_DECL:
11240 tree function = decl_function_context (exp);
11242 temp = label_rtx (exp);
11243 temp = gen_rtx_LABEL_REF (Pmode, temp);
11245 if (function != current_function_decl
11246 && function != 0)
11247 LABEL_REF_NONLOCAL_P (temp) = 1;
11249 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
11250 return temp;
11253 case SSA_NAME:
11254 /* ??? ivopts calls expander, without any preparation from
11255 out-of-ssa. So fake instructions as if this was an access to the
11256 base variable. This unnecessarily allocates a pseudo, see how we can
11257 reuse it, if partition base vars have it set already. */
11258 if (!currently_expanding_to_rtl)
11260 tree var = SSA_NAME_VAR (exp);
11261 if (var && DECL_RTL_SET_P (var))
11262 return DECL_RTL (var);
11263 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
11264 LAST_VIRTUAL_REGISTER + 1);
11267 g = get_gimple_for_ssa_name (exp);
11268 /* For EXPAND_INITIALIZER try harder to get something simpler. */
11269 if (g == NULL
11270 && modifier == EXPAND_INITIALIZER
11271 && !SSA_NAME_IS_DEFAULT_DEF (exp)
11272 && (optimize || !SSA_NAME_VAR (exp)
11273 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
11274 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
11275 g = SSA_NAME_DEF_STMT (exp);
11276 if (g)
11277 return expand_expr_real_gassign (as_a<gassign *> (g), target, tmode,
11278 modifier, alt_rtl, inner_reference_p);
11280 ssa_name = exp;
11281 decl_rtl = get_rtx_for_ssa_name (ssa_name);
11282 exp = SSA_NAME_VAR (ssa_name);
11283 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an
11284 SSA_NAME computed within the current function. In such case the
11285 value have been already extended before. While if it is a function
11286 parameter, result or some memory location, we need to be prepared
11287 for some other compiler leaving the bits uninitialized. */
11288 if (!exp || VAR_P (exp))
11289 reduce_bit_field = false;
11290 goto expand_decl_rtl;
11292 case VAR_DECL:
11293 /* Allow accel compiler to handle variables that require special
11294 treatment, e.g. if they have been modified in some way earlier in
11295 compilation by the adjust_private_decl OpenACC hook. */
11296 if (flag_openacc && targetm.goacc.expand_var_decl)
11298 temp = targetm.goacc.expand_var_decl (exp);
11299 if (temp)
11300 return temp;
11302 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
11303 have scalar integer modes to a reg via store_constructor. */
11304 if (TREE_READONLY (exp)
11305 && !TREE_SIDE_EFFECTS (exp)
11306 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
11307 && immediate_const_ctor_p (DECL_INITIAL (exp))
11308 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
11309 && crtl->emit.regno_pointer_align_length
11310 && !target)
11312 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
11313 store_constructor (DECL_INITIAL (exp), target, 0,
11314 int_expr_size (DECL_INITIAL (exp)), false);
11315 return target;
11317 /* ... fall through ... */
11319 case PARM_DECL:
11320 /* If a static var's type was incomplete when the decl was written,
11321 but the type is complete now, lay out the decl now. */
11322 if (DECL_SIZE (exp) == 0
11323 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
11324 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
11325 layout_decl (exp, 0);
11327 /* fall through */
11329 case FUNCTION_DECL:
11330 case RESULT_DECL:
11331 decl_rtl = DECL_RTL (exp);
11332 expand_decl_rtl:
11333 gcc_assert (decl_rtl);
11335 /* DECL_MODE might change when TYPE_MODE depends on attribute target
11336 settings for VECTOR_TYPE_P that might switch for the function. */
11337 if (currently_expanding_to_rtl
11338 && code == VAR_DECL && MEM_P (decl_rtl)
11339 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
11340 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
11341 else
11342 decl_rtl = copy_rtx (decl_rtl);
11344 /* Record writes to register variables. */
11345 if (modifier == EXPAND_WRITE
11346 && REG_P (decl_rtl)
11347 && HARD_REGISTER_P (decl_rtl))
11348 add_to_hard_reg_set (&crtl->asm_clobbers,
11349 GET_MODE (decl_rtl), REGNO (decl_rtl));
11351 /* Ensure variable marked as used even if it doesn't go through
11352 a parser. If it hasn't be used yet, write out an external
11353 definition. */
11354 if (exp)
11355 TREE_USED (exp) = 1;
11357 /* Show we haven't gotten RTL for this yet. */
11358 temp = 0;
11360 /* Variables inherited from containing functions should have
11361 been lowered by this point. */
11362 if (exp)
11364 tree context = decl_function_context (exp);
11365 gcc_assert (SCOPE_FILE_SCOPE_P (context)
11366 || context == current_function_decl
11367 || TREE_STATIC (exp)
11368 || DECL_EXTERNAL (exp)
11369 /* ??? C++ creates functions that are not
11370 TREE_STATIC. */
11371 || TREE_CODE (exp) == FUNCTION_DECL);
11374 /* This is the case of an array whose size is to be determined
11375 from its initializer, while the initializer is still being parsed.
11376 ??? We aren't parsing while expanding anymore. */
11378 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
11379 temp = validize_mem (decl_rtl);
11381 /* If DECL_RTL is memory, we are in the normal case and the
11382 address is not valid, get the address into a register. */
11384 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
11386 if (alt_rtl)
11387 *alt_rtl = decl_rtl;
11388 decl_rtl = use_anchored_address (decl_rtl);
11389 if (modifier != EXPAND_CONST_ADDRESS
11390 && modifier != EXPAND_SUM
11391 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
11392 : GET_MODE (decl_rtl),
11393 XEXP (decl_rtl, 0),
11394 MEM_ADDR_SPACE (decl_rtl)))
11395 temp = replace_equiv_address (decl_rtl,
11396 copy_rtx (XEXP (decl_rtl, 0)));
11399 /* If we got something, return it. But first, set the alignment
11400 if the address is a register. */
11401 if (temp != 0)
11403 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
11404 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
11406 else if (MEM_P (decl_rtl))
11407 temp = decl_rtl;
11409 if (temp != 0)
11411 if (MEM_P (temp)
11412 && modifier != EXPAND_WRITE
11413 && modifier != EXPAND_MEMORY
11414 && modifier != EXPAND_INITIALIZER
11415 && modifier != EXPAND_CONST_ADDRESS
11416 && modifier != EXPAND_SUM
11417 && !inner_reference_p
11418 && mode != BLKmode
11419 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
11420 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11421 MEM_ALIGN (temp), NULL_RTX, NULL);
11423 return EXTEND_BITINT (temp);
11426 if (exp)
11427 dmode = DECL_MODE (exp);
11428 else
11429 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
11431 /* If the mode of DECL_RTL does not match that of the decl,
11432 there are two cases: we are dealing with a BLKmode value
11433 that is returned in a register, or we are dealing with
11434 a promoted value. In the latter case, return a SUBREG
11435 of the wanted mode, but mark it so that we know that it
11436 was already extended. */
11437 if (REG_P (decl_rtl)
11438 && dmode != BLKmode
11439 && GET_MODE (decl_rtl) != dmode)
11441 machine_mode pmode;
11443 /* Get the signedness to be used for this variable. Ensure we get
11444 the same mode we got when the variable was declared. */
11445 if (code != SSA_NAME)
11446 pmode = promote_decl_mode (exp, &unsignedp);
11447 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
11448 && gimple_code (g) == GIMPLE_CALL
11449 && !gimple_call_internal_p (g))
11450 pmode = promote_function_mode (type, mode, &unsignedp,
11451 gimple_call_fntype (g),
11453 else
11454 pmode = promote_ssa_mode (ssa_name, &unsignedp);
11455 gcc_assert (GET_MODE (decl_rtl) == pmode);
11457 /* Some ABIs require scalar floating point modes to be passed
11458 in a wider scalar integer mode. We need to explicitly
11459 truncate to an integer mode of the correct precision before
11460 using a SUBREG to reinterpret as a floating point value. */
11461 if (SCALAR_FLOAT_MODE_P (mode)
11462 && SCALAR_INT_MODE_P (pmode)
11463 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11464 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11466 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11467 SUBREG_PROMOTED_VAR_P (temp) = 1;
11468 SUBREG_PROMOTED_SET (temp, unsignedp);
11469 return EXTEND_BITINT (temp);
11472 return EXTEND_BITINT (decl_rtl);
11474 case INTEGER_CST:
11476 if (TREE_CODE (type) == BITINT_TYPE)
11478 unsigned int prec = TYPE_PRECISION (type);
11479 struct bitint_info info;
11480 bool ok = targetm.c.bitint_type_info (prec, &info);
11481 gcc_assert (ok);
11482 scalar_int_mode limb_mode
11483 = as_a <scalar_int_mode> (info.limb_mode);
11484 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode);
11485 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE)
11487 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */
11488 exp = tree_output_constant_def (exp);
11489 return expand_expr (exp, target, VOIDmode, modifier);
11493 /* Given that TYPE_PRECISION (type) is not always equal to
11494 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11495 the former to the latter according to the signedness of the
11496 type. */
11497 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11498 temp = immed_wide_int_const
11499 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11500 return temp;
11503 case VECTOR_CST:
11505 tree tmp = NULL_TREE;
11506 if (VECTOR_MODE_P (mode))
11507 return const_vector_from_tree (exp);
11508 scalar_int_mode int_mode;
11509 if (is_int_mode (mode, &int_mode))
11511 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11512 if (type_for_mode)
11513 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11514 type_for_mode, exp);
11516 if (!tmp)
11518 vec<constructor_elt, va_gc> *v;
11519 /* Constructors need to be fixed-length. FIXME. */
11520 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11521 vec_alloc (v, nunits);
11522 for (unsigned int i = 0; i < nunits; ++i)
11523 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11524 tmp = build_constructor (type, v);
11526 return expand_expr (tmp, ignore ? const0_rtx : target,
11527 tmode, modifier);
11530 case CONST_DECL:
11531 if (modifier == EXPAND_WRITE)
11533 /* Writing into CONST_DECL is always invalid, but handle it
11534 gracefully. */
11535 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11536 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11537 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11538 EXPAND_NORMAL, as);
11539 op0 = memory_address_addr_space (mode, op0, as);
11540 temp = gen_rtx_MEM (mode, op0);
11541 set_mem_addr_space (temp, as);
11542 return temp;
11544 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11546 case REAL_CST:
11547 /* If optimized, generate immediate CONST_DOUBLE
11548 which will be turned into memory by reload if necessary.
11550 We used to force a register so that loop.c could see it. But
11551 this does not allow gen_* patterns to perform optimizations with
11552 the constants. It also produces two insns in cases like "x = 1.0;".
11553 On most machines, floating-point constants are not permitted in
11554 many insns, so we'd end up copying it to a register in any case.
11556 Now, we do the copying in expand_binop, if appropriate. */
11557 return const_double_from_real_value (TREE_REAL_CST (exp),
11558 TYPE_MODE (TREE_TYPE (exp)));
11560 case FIXED_CST:
11561 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11562 TYPE_MODE (TREE_TYPE (exp)));
11564 case COMPLEX_CST:
11565 /* Handle evaluating a complex constant in a CONCAT target. */
11566 if (original_target && GET_CODE (original_target) == CONCAT)
11568 rtx rtarg, itarg;
11570 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11571 rtarg = XEXP (original_target, 0);
11572 itarg = XEXP (original_target, 1);
11574 /* Move the real and imaginary parts separately. */
11575 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11576 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11578 if (op0 != rtarg)
11579 emit_move_insn (rtarg, op0);
11580 if (op1 != itarg)
11581 emit_move_insn (itarg, op1);
11583 return original_target;
11586 /* fall through */
11588 case STRING_CST:
11589 temp = expand_expr_constant (exp, 1, modifier);
11591 /* temp contains a constant address.
11592 On RISC machines where a constant address isn't valid,
11593 make some insns to get that address into a register. */
11594 if (modifier != EXPAND_CONST_ADDRESS
11595 && modifier != EXPAND_INITIALIZER
11596 && modifier != EXPAND_SUM
11597 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11598 MEM_ADDR_SPACE (temp)))
11599 return replace_equiv_address (temp,
11600 copy_rtx (XEXP (temp, 0)));
11601 return temp;
11603 case POLY_INT_CST:
11604 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11606 case SAVE_EXPR:
11608 tree val = treeop0;
11609 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11610 inner_reference_p);
11612 if (!SAVE_EXPR_RESOLVED_P (exp))
11614 /* We can indeed still hit this case, typically via builtin
11615 expanders calling save_expr immediately before expanding
11616 something. Assume this means that we only have to deal
11617 with non-BLKmode values. */
11618 gcc_assert (GET_MODE (ret) != BLKmode);
11620 val = build_decl (curr_insn_location (),
11621 VAR_DECL, NULL, TREE_TYPE (exp));
11622 DECL_ARTIFICIAL (val) = 1;
11623 DECL_IGNORED_P (val) = 1;
11624 treeop0 = val;
11625 TREE_OPERAND (exp, 0) = treeop0;
11626 SAVE_EXPR_RESOLVED_P (exp) = 1;
11628 if (!CONSTANT_P (ret))
11629 ret = copy_to_reg (ret);
11630 SET_DECL_RTL (val, ret);
11633 return ret;
11637 case CONSTRUCTOR:
11638 /* If we don't need the result, just ensure we evaluate any
11639 subexpressions. */
11640 if (ignore)
11642 unsigned HOST_WIDE_INT idx;
11643 tree value;
11645 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11646 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11648 return const0_rtx;
11651 return expand_constructor (exp, target, modifier, false);
11653 case TARGET_MEM_REF:
11655 addr_space_t as
11656 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11657 unsigned int align;
11659 op0 = addr_for_mem_ref (exp, as, true);
11660 op0 = memory_address_addr_space (mode, op0, as);
11661 temp = gen_rtx_MEM (mode, op0);
11662 set_mem_attributes (temp, exp, 0);
11663 set_mem_addr_space (temp, as);
11664 align = get_object_alignment (exp);
11665 if (modifier != EXPAND_WRITE
11666 && modifier != EXPAND_MEMORY
11667 && mode != BLKmode
11668 && align < GET_MODE_ALIGNMENT (mode))
11669 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11670 align, NULL_RTX, NULL);
11671 return EXTEND_BITINT (temp);
11674 case MEM_REF:
11676 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11677 addr_space_t as
11678 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11679 machine_mode address_mode;
11680 tree base = TREE_OPERAND (exp, 0);
11681 gimple *def_stmt;
11682 unsigned align;
11683 /* Handle expansion of non-aliased memory with non-BLKmode. That
11684 might end up in a register. */
11685 if (mem_ref_refers_to_non_mem_p (exp))
11687 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11688 base = TREE_OPERAND (base, 0);
11689 poly_uint64 type_size;
11690 if (known_eq (offset, 0)
11691 && !reverse
11692 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11693 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11694 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11695 target, tmode, modifier);
11696 if (TYPE_MODE (type) == BLKmode)
11698 temp = assign_stack_temp (DECL_MODE (base),
11699 GET_MODE_SIZE (DECL_MODE (base)));
11700 store_expr (base, temp, 0, false, false);
11701 temp = adjust_address (temp, BLKmode, offset);
11702 set_mem_size (temp, int_size_in_bytes (type));
11703 return temp;
11705 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11706 bitsize_int (offset * BITS_PER_UNIT));
11707 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11708 return expand_expr (exp, target, tmode, modifier);
11710 address_mode = targetm.addr_space.address_mode (as);
11711 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11713 tree mask = gimple_assign_rhs2 (def_stmt);
11714 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11715 gimple_assign_rhs1 (def_stmt), mask);
11716 TREE_OPERAND (exp, 0) = base;
11718 align = get_object_alignment (exp);
11719 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11720 op0 = memory_address_addr_space (mode, op0, as);
11721 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11723 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11724 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11725 op0 = memory_address_addr_space (mode, op0, as);
11727 temp = gen_rtx_MEM (mode, op0);
11728 set_mem_attributes (temp, exp, 0);
11729 set_mem_addr_space (temp, as);
11730 if (TREE_THIS_VOLATILE (exp))
11731 MEM_VOLATILE_P (temp) = 1;
11732 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY)
11733 return temp;
11734 if (!inner_reference_p
11735 && mode != BLKmode
11736 && align < GET_MODE_ALIGNMENT (mode))
11737 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11738 modifier == EXPAND_STACK_PARM
11739 ? NULL_RTX : target, alt_rtl);
11740 if (reverse)
11741 temp = flip_storage_order (mode, temp);
11742 return EXTEND_BITINT (temp);
11745 case ARRAY_REF:
11748 tree array = treeop0;
11749 tree index = treeop1;
11750 tree init;
11752 /* Fold an expression like: "foo"[2].
11753 This is not done in fold so it won't happen inside &.
11754 Don't fold if this is for wide characters since it's too
11755 difficult to do correctly and this is a very rare case. */
11757 if (modifier != EXPAND_CONST_ADDRESS
11758 && modifier != EXPAND_INITIALIZER
11759 && modifier != EXPAND_MEMORY)
11761 tree t = fold_read_from_constant_string (exp);
11763 if (t)
11764 return expand_expr (t, target, tmode, modifier);
11767 /* If this is a constant index into a constant array,
11768 just get the value from the array. Handle both the cases when
11769 we have an explicit constructor and when our operand is a variable
11770 that was declared const. */
11772 if (modifier != EXPAND_CONST_ADDRESS
11773 && modifier != EXPAND_INITIALIZER
11774 && modifier != EXPAND_MEMORY
11775 && TREE_CODE (array) == CONSTRUCTOR
11776 && ! TREE_SIDE_EFFECTS (array)
11777 && TREE_CODE (index) == INTEGER_CST)
11779 unsigned HOST_WIDE_INT ix;
11780 tree field, value;
11782 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11783 field, value)
11784 if (tree_int_cst_equal (field, index))
11786 if (!TREE_SIDE_EFFECTS (value))
11787 return expand_expr (fold (value), target, tmode, modifier);
11788 break;
11792 else if (optimize >= 1
11793 && modifier != EXPAND_CONST_ADDRESS
11794 && modifier != EXPAND_INITIALIZER
11795 && modifier != EXPAND_MEMORY
11796 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11797 && TREE_CODE (index) == INTEGER_CST
11798 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11799 && (init = ctor_for_folding (array)) != error_mark_node)
11801 if (init == NULL_TREE)
11803 tree value = build_zero_cst (type);
11804 if (TREE_CODE (value) == CONSTRUCTOR)
11806 /* If VALUE is a CONSTRUCTOR, this optimization is only
11807 useful if this doesn't store the CONSTRUCTOR into
11808 memory. If it does, it is more efficient to just
11809 load the data from the array directly. */
11810 rtx ret = expand_constructor (value, target,
11811 modifier, true);
11812 if (ret == NULL_RTX)
11813 value = NULL_TREE;
11816 if (value)
11817 return expand_expr (value, target, tmode, modifier);
11819 else if (TREE_CODE (init) == CONSTRUCTOR)
11821 unsigned HOST_WIDE_INT ix;
11822 tree field, value;
11824 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11825 field, value)
11826 if (tree_int_cst_equal (field, index))
11828 if (TREE_SIDE_EFFECTS (value))
11829 break;
11831 if (TREE_CODE (value) == CONSTRUCTOR)
11833 /* If VALUE is a CONSTRUCTOR, this
11834 optimization is only useful if
11835 this doesn't store the CONSTRUCTOR
11836 into memory. If it does, it is more
11837 efficient to just load the data from
11838 the array directly. */
11839 rtx ret = expand_constructor (value, target,
11840 modifier, true);
11841 if (ret == NULL_RTX)
11842 break;
11845 return
11846 expand_expr (fold (value), target, tmode, modifier);
11849 else if (TREE_CODE (init) == STRING_CST)
11851 tree low_bound = array_ref_low_bound (exp);
11852 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11854 /* Optimize the special case of a zero lower bound.
11856 We convert the lower bound to sizetype to avoid problems
11857 with constant folding. E.g. suppose the lower bound is
11858 1 and its mode is QI. Without the conversion
11859 (ARRAY + (INDEX - (unsigned char)1))
11860 becomes
11861 (ARRAY + (-(unsigned char)1) + INDEX)
11862 which becomes
11863 (ARRAY + 255 + INDEX). Oops! */
11864 if (!integer_zerop (low_bound))
11865 index1 = size_diffop_loc (loc, index1,
11866 fold_convert_loc (loc, sizetype,
11867 low_bound));
11869 if (tree_fits_uhwi_p (index1)
11870 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11872 tree char_type = TREE_TYPE (TREE_TYPE (init));
11873 scalar_int_mode char_mode;
11875 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11876 && GET_MODE_SIZE (char_mode) == 1)
11877 return gen_int_mode (TREE_STRING_POINTER (init)
11878 [TREE_INT_CST_LOW (index1)],
11879 char_mode);
11884 goto normal_inner_ref;
11886 case COMPONENT_REF:
11887 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11888 /* Fall through. */
11889 case BIT_FIELD_REF:
11890 case ARRAY_RANGE_REF:
11891 normal_inner_ref:
11893 machine_mode mode1, mode2;
11894 poly_int64 bitsize, bitpos, bytepos;
11895 tree offset;
11896 int reversep, volatilep = 0;
11897 tree tem
11898 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11899 &unsignedp, &reversep, &volatilep);
11900 rtx orig_op0, memloc;
11901 bool clear_mem_expr = false;
11902 bool must_force_mem;
11904 /* If we got back the original object, something is wrong. Perhaps
11905 we are evaluating an expression too early. In any event, don't
11906 infinitely recurse. */
11907 gcc_assert (tem != exp);
11909 /* Make sure bitpos is not negative, this can wreak havoc later. */
11910 if (maybe_lt (bitpos, 0))
11912 gcc_checking_assert (offset == NULL_TREE);
11913 offset = size_int (bits_to_bytes_round_down (bitpos));
11914 bitpos = num_trailing_bits (bitpos);
11917 /* If we have either an offset, a BLKmode result, or a reference
11918 outside the underlying object, we must force it to memory.
11919 Such a case can occur in Ada if we have unchecked conversion
11920 of an expression from a scalar type to an aggregate type or
11921 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11922 passed a partially uninitialized object or a view-conversion
11923 to a larger size. */
11924 must_force_mem = offset != NULL_TREE
11925 || mode1 == BLKmode
11926 || (mode == BLKmode
11927 && !int_mode_for_size (bitsize, 1).exists ());
11929 const enum expand_modifier tem_modifier
11930 = must_force_mem
11931 ? EXPAND_MEMORY
11932 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11934 /* If TEM's type is a union of variable size, pass TARGET to the inner
11935 computation, since it will need a temporary and TARGET is known
11936 to have to do. This occurs in unchecked conversion in Ada. */
11937 const rtx tem_target
11938 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11939 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11940 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11941 && modifier != EXPAND_STACK_PARM
11942 ? target
11943 : NULL_RTX;
11945 orig_op0 = op0
11946 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11947 true);
11949 /* If the field has a mode, we want to access it in the
11950 field's mode, not the computed mode.
11951 If a MEM has VOIDmode (external with incomplete type),
11952 use BLKmode for it instead. */
11953 if (MEM_P (op0))
11955 if (mode1 != VOIDmode)
11956 op0 = adjust_address (op0, mode1, 0);
11957 else if (GET_MODE (op0) == VOIDmode)
11958 op0 = adjust_address (op0, BLKmode, 0);
11961 mode2
11962 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11964 /* See above for the rationale. */
11965 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11966 must_force_mem = true;
11968 /* Handle CONCAT first. */
11969 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11971 if (known_eq (bitpos, 0)
11972 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11973 && COMPLEX_MODE_P (mode1)
11974 && COMPLEX_MODE_P (GET_MODE (op0))
11975 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11976 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11978 if (reversep)
11979 op0 = flip_storage_order (GET_MODE (op0), op0);
11980 if (mode1 != GET_MODE (op0))
11982 rtx parts[2];
11983 for (int i = 0; i < 2; i++)
11985 rtx op = read_complex_part (op0, i != 0);
11986 if (GET_CODE (op) == SUBREG)
11987 op = force_reg (GET_MODE (op), op);
11988 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11989 if (temp)
11990 op = temp;
11991 else
11993 if (!REG_P (op) && !MEM_P (op))
11994 op = force_reg (GET_MODE (op), op);
11995 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11997 parts[i] = op;
11999 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
12001 return op0;
12003 if (known_eq (bitpos, 0)
12004 && known_eq (bitsize,
12005 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
12006 && maybe_ne (bitsize, 0))
12008 op0 = XEXP (op0, 0);
12009 mode2 = GET_MODE (op0);
12011 else if (known_eq (bitpos,
12012 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
12013 && known_eq (bitsize,
12014 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
12015 && maybe_ne (bitpos, 0)
12016 && maybe_ne (bitsize, 0))
12018 op0 = XEXP (op0, 1);
12019 bitpos = 0;
12020 mode2 = GET_MODE (op0);
12022 else
12023 /* Otherwise force into memory. */
12024 must_force_mem = true;
12027 /* If this is a constant, put it in a register if it is a legitimate
12028 constant and we don't need a memory reference. */
12029 if (CONSTANT_P (op0)
12030 && mode2 != BLKmode
12031 && targetm.legitimate_constant_p (mode2, op0)
12032 && !must_force_mem)
12033 op0 = force_reg (mode2, op0);
12035 /* Otherwise, if this is a constant, try to force it to the constant
12036 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
12037 is a legitimate constant. */
12038 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
12039 op0 = validize_mem (memloc);
12041 /* Otherwise, if this is a constant or the object is not in memory
12042 and need be, put it there. */
12043 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
12045 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
12046 emit_move_insn (memloc, op0);
12047 op0 = memloc;
12048 clear_mem_expr = true;
12051 if (offset)
12053 machine_mode address_mode;
12054 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
12055 EXPAND_SUM);
12057 gcc_assert (MEM_P (op0));
12059 address_mode = get_address_mode (op0);
12060 if (GET_MODE (offset_rtx) != address_mode)
12062 /* We cannot be sure that the RTL in offset_rtx is valid outside
12063 of a memory address context, so force it into a register
12064 before attempting to convert it to the desired mode. */
12065 offset_rtx = force_operand (offset_rtx, NULL_RTX);
12066 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
12069 /* See the comment in expand_assignment for the rationale. */
12070 if (mode1 != VOIDmode
12071 && maybe_ne (bitpos, 0)
12072 && maybe_gt (bitsize, 0)
12073 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12074 && multiple_p (bitpos, bitsize)
12075 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
12076 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
12078 op0 = adjust_address (op0, mode1, bytepos);
12079 bitpos = 0;
12082 op0 = offset_address (op0, offset_rtx,
12083 highest_pow2_factor (offset));
12086 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
12087 record its alignment as BIGGEST_ALIGNMENT. */
12088 if (MEM_P (op0)
12089 && known_eq (bitpos, 0)
12090 && offset != 0
12091 && is_aligning_offset (offset, tem))
12092 set_mem_align (op0, BIGGEST_ALIGNMENT);
12094 /* Don't forget about volatility even if this is a bitfield. */
12095 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
12097 if (op0 == orig_op0)
12098 op0 = copy_rtx (op0);
12100 MEM_VOLATILE_P (op0) = 1;
12103 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
12105 if (op0 == orig_op0)
12106 op0 = copy_rtx (op0);
12108 set_mem_align (op0, BITS_PER_UNIT);
12111 /* In cases where an aligned union has an unaligned object
12112 as a field, we might be extracting a BLKmode value from
12113 an integer-mode (e.g., SImode) object. Handle this case
12114 by doing the extract into an object as wide as the field
12115 (which we know to be the width of a basic mode), then
12116 storing into memory, and changing the mode to BLKmode. */
12117 if (mode1 == VOIDmode
12118 || REG_P (op0) || GET_CODE (op0) == SUBREG
12119 || (mode1 != BLKmode && ! direct_load[(int) mode1]
12120 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
12121 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
12122 && modifier != EXPAND_CONST_ADDRESS
12123 && modifier != EXPAND_INITIALIZER
12124 && modifier != EXPAND_MEMORY)
12125 /* If the bitfield is volatile and the bitsize
12126 is narrower than the access size of the bitfield,
12127 we need to extract bitfields from the access. */
12128 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
12129 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
12130 && mode1 != BLKmode
12131 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
12132 /* If the field isn't aligned enough to fetch as a memref,
12133 fetch it as a bit field. */
12134 || (mode1 != BLKmode
12135 && (((MEM_P (op0)
12136 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
12137 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
12138 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
12139 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
12140 && modifier != EXPAND_MEMORY
12141 && ((modifier == EXPAND_CONST_ADDRESS
12142 || modifier == EXPAND_INITIALIZER)
12143 ? STRICT_ALIGNMENT
12144 : targetm.slow_unaligned_access (mode1,
12145 MEM_ALIGN (op0))))
12146 || !multiple_p (bitpos, BITS_PER_UNIT)))
12147 /* If the type and the field are a constant size and the
12148 size of the type isn't the same size as the bitfield,
12149 we must use bitfield operations. */
12150 || (known_size_p (bitsize)
12151 && TYPE_SIZE (TREE_TYPE (exp))
12152 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
12153 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
12154 bitsize)))
12156 machine_mode ext_mode = mode;
12158 if (ext_mode == BLKmode
12159 && ! (target != 0 && MEM_P (op0)
12160 && MEM_P (target)
12161 && multiple_p (bitpos, BITS_PER_UNIT)))
12162 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
12164 if (ext_mode == BLKmode)
12166 if (target == 0)
12167 target = assign_temp (type, 1, 1);
12169 /* ??? Unlike the similar test a few lines below, this one is
12170 very likely obsolete. */
12171 if (known_eq (bitsize, 0))
12172 return target;
12174 /* In this case, BITPOS must start at a byte boundary and
12175 TARGET, if specified, must be a MEM. */
12176 gcc_assert (MEM_P (op0)
12177 && (!target || MEM_P (target)));
12179 bytepos = exact_div (bitpos, BITS_PER_UNIT);
12180 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
12181 emit_block_move (target,
12182 adjust_address (op0, VOIDmode, bytepos),
12183 gen_int_mode (bytesize, Pmode),
12184 (modifier == EXPAND_STACK_PARM
12185 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
12187 return target;
12190 /* If we have nothing to extract, the result will be 0 for targets
12191 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
12192 return 0 for the sake of consistency, as reading a zero-sized
12193 bitfield is valid in Ada and the value is fully specified. */
12194 if (known_eq (bitsize, 0))
12195 return const0_rtx;
12197 op0 = validize_mem (op0);
12199 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
12200 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12202 /* If the result has aggregate type and the extraction is done in
12203 an integral mode, then the field may be not aligned on a byte
12204 boundary; in this case, if it has reverse storage order, it
12205 needs to be extracted as a scalar field with reverse storage
12206 order and put back into memory order afterwards. */
12207 if (AGGREGATE_TYPE_P (type)
12208 && GET_MODE_CLASS (ext_mode) == MODE_INT)
12209 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
12211 gcc_checking_assert (known_ge (bitpos, 0));
12212 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
12213 (modifier == EXPAND_STACK_PARM
12214 ? NULL_RTX : target),
12215 ext_mode, ext_mode, reversep, alt_rtl);
12217 /* If the result has aggregate type and the mode of OP0 is an
12218 integral mode then, if BITSIZE is narrower than this mode
12219 and this is for big-endian data, we must put the field
12220 into the high-order bits. And we must also put it back
12221 into memory order if it has been previously reversed. */
12222 scalar_int_mode op0_mode;
12223 if (AGGREGATE_TYPE_P (type)
12224 && is_int_mode (GET_MODE (op0), &op0_mode))
12226 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
12228 gcc_checking_assert (known_le (bitsize, size));
12229 if (maybe_lt (bitsize, size)
12230 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
12231 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
12232 size - bitsize, op0, 1);
12234 if (reversep)
12235 op0 = flip_storage_order (op0_mode, op0);
12238 /* If the result type is BLKmode, store the data into a temporary
12239 of the appropriate type, but with the mode corresponding to the
12240 mode for the data we have (op0's mode). */
12241 if (mode == BLKmode)
12243 rtx new_rtx
12244 = assign_stack_temp_for_type (ext_mode,
12245 GET_MODE_BITSIZE (ext_mode),
12246 type);
12247 emit_move_insn (new_rtx, op0);
12248 op0 = copy_rtx (new_rtx);
12249 PUT_MODE (op0, BLKmode);
12252 return op0;
12255 /* If the result is BLKmode, use that to access the object
12256 now as well. */
12257 if (mode == BLKmode)
12258 mode1 = BLKmode;
12260 /* Get a reference to just this component. */
12261 bytepos = bits_to_bytes_round_down (bitpos);
12262 if (modifier == EXPAND_CONST_ADDRESS
12263 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
12264 op0 = adjust_address_nv (op0, mode1, bytepos);
12265 else
12266 op0 = adjust_address (op0, mode1, bytepos);
12268 if (op0 == orig_op0)
12269 op0 = copy_rtx (op0);
12271 /* Don't set memory attributes if the base expression is
12272 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
12273 we should just honor its original memory attributes. */
12274 if (!(TREE_CODE (tem) == SSA_NAME
12275 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
12276 set_mem_attributes (op0, exp, 0);
12278 if (REG_P (XEXP (op0, 0)))
12279 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12281 /* If op0 is a temporary because the original expressions was forced
12282 to memory, clear MEM_EXPR so that the original expression cannot
12283 be marked as addressable through MEM_EXPR of the temporary. */
12284 if (clear_mem_expr)
12285 set_mem_expr (op0, NULL_TREE);
12287 MEM_VOLATILE_P (op0) |= volatilep;
12289 if (reversep
12290 && modifier != EXPAND_MEMORY
12291 && modifier != EXPAND_WRITE)
12292 op0 = flip_storage_order (mode1, op0);
12294 op0 = EXTEND_BITINT (op0);
12296 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
12297 || modifier == EXPAND_CONST_ADDRESS
12298 || modifier == EXPAND_INITIALIZER)
12299 return op0;
12301 if (target == 0)
12302 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
12304 convert_move (target, op0, unsignedp);
12305 return target;
12308 case OBJ_TYPE_REF:
12309 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
12311 case CALL_EXPR:
12312 /* All valid uses of __builtin_va_arg_pack () are removed during
12313 inlining. */
12314 if (CALL_EXPR_VA_ARG_PACK (exp))
12315 error ("invalid use of %<__builtin_va_arg_pack ()%>");
12317 tree fndecl = get_callee_fndecl (exp), attr;
12319 if (fndecl
12320 /* Don't diagnose the error attribute in thunks, those are
12321 artificially created. */
12322 && !CALL_FROM_THUNK_P (exp)
12323 && (attr = lookup_attribute ("error",
12324 DECL_ATTRIBUTES (fndecl))) != NULL)
12326 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12327 error ("call to %qs declared with attribute error: %s",
12328 identifier_to_locale (ident),
12329 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12331 if (fndecl
12332 /* Don't diagnose the warning attribute in thunks, those are
12333 artificially created. */
12334 && !CALL_FROM_THUNK_P (exp)
12335 && (attr = lookup_attribute ("warning",
12336 DECL_ATTRIBUTES (fndecl))) != NULL)
12338 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12339 warning_at (EXPR_LOCATION (exp),
12340 OPT_Wattribute_warning,
12341 "call to %qs declared with attribute warning: %s",
12342 identifier_to_locale (ident),
12343 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12346 /* Check for a built-in function. */
12347 if (fndecl && fndecl_built_in_p (fndecl))
12349 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
12350 return expand_builtin (exp, target, subtarget, tmode, ignore);
12353 return expand_call (exp, target, ignore);
12355 case VIEW_CONVERT_EXPR:
12356 op0 = NULL_RTX;
12358 /* If we are converting to BLKmode, try to avoid an intermediate
12359 temporary by fetching an inner memory reference. */
12360 if (mode == BLKmode
12361 && poly_int_tree_p (TYPE_SIZE (type))
12362 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
12363 && handled_component_p (treeop0))
12365 machine_mode mode1;
12366 poly_int64 bitsize, bitpos, bytepos;
12367 tree offset;
12368 int reversep, volatilep = 0;
12369 tree tem
12370 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
12371 &unsignedp, &reversep, &volatilep);
12373 /* ??? We should work harder and deal with non-zero offsets. */
12374 if (!offset
12375 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12376 && !reversep
12377 && known_size_p (bitsize)
12378 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
12380 /* See the normal_inner_ref case for the rationale. */
12381 rtx orig_op0
12382 = expand_expr_real (tem,
12383 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
12384 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
12385 != INTEGER_CST)
12386 && modifier != EXPAND_STACK_PARM
12387 ? target : NULL_RTX),
12388 VOIDmode,
12389 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
12390 NULL, true);
12392 if (MEM_P (orig_op0))
12394 op0 = orig_op0;
12396 /* Get a reference to just this component. */
12397 if (modifier == EXPAND_CONST_ADDRESS
12398 || modifier == EXPAND_SUM
12399 || modifier == EXPAND_INITIALIZER)
12400 op0 = adjust_address_nv (op0, mode, bytepos);
12401 else
12402 op0 = adjust_address (op0, mode, bytepos);
12404 if (op0 == orig_op0)
12405 op0 = copy_rtx (op0);
12407 set_mem_attributes (op0, treeop0, 0);
12408 if (REG_P (XEXP (op0, 0)))
12409 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12411 MEM_VOLATILE_P (op0) |= volatilep;
12416 if (!op0)
12417 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
12418 NULL, inner_reference_p);
12420 /* If the input and output modes are both the same, we are done. */
12421 if (mode == GET_MODE (op0))
12423 /* Similarly if the output mode is BLKmode and input is a MEM,
12424 adjust_address done below is all we need. */
12425 else if (mode == BLKmode && MEM_P (op0))
12427 /* If neither mode is BLKmode, and both modes are the same size
12428 then we can use gen_lowpart. */
12429 else if (mode != BLKmode
12430 && GET_MODE (op0) != BLKmode
12431 && known_eq (GET_MODE_PRECISION (mode),
12432 GET_MODE_PRECISION (GET_MODE (op0)))
12433 && !COMPLEX_MODE_P (GET_MODE (op0)))
12435 if (GET_CODE (op0) == SUBREG)
12436 op0 = force_reg (GET_MODE (op0), op0);
12437 temp = gen_lowpart_common (mode, op0);
12438 if (temp)
12439 op0 = temp;
12440 else
12442 if (!REG_P (op0) && !MEM_P (op0))
12443 op0 = force_reg (GET_MODE (op0), op0);
12444 op0 = gen_lowpart (mode, op0);
12447 /* If both types are integral, convert from one mode to the other. */
12448 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
12449 op0 = convert_modes (mode, GET_MODE (op0), op0,
12450 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
12451 /* If the output type is a bit-field type, do an extraction. */
12452 else if (reduce_bit_field)
12453 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12454 TYPE_UNSIGNED (type), NULL_RTX,
12455 mode, mode, false, NULL);
12456 /* As a last resort, spill op0 to memory, and reload it in a
12457 different mode. */
12458 else if (!MEM_P (op0))
12460 /* If the operand is not a MEM, force it into memory. Since we
12461 are going to be changing the mode of the MEM, don't call
12462 force_const_mem for constants because we don't allow pool
12463 constants to change mode. */
12464 tree inner_type = TREE_TYPE (treeop0);
12466 gcc_assert (!TREE_ADDRESSABLE (exp));
12468 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
12469 target
12470 = assign_stack_temp_for_type
12471 (TYPE_MODE (inner_type),
12472 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
12474 emit_move_insn (target, op0);
12475 op0 = target;
12478 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
12479 output type is such that the operand is known to be aligned, indicate
12480 that it is. Otherwise, we need only be concerned about alignment for
12481 non-BLKmode results. */
12482 if (MEM_P (op0))
12484 enum insn_code icode;
12486 if (modifier != EXPAND_WRITE
12487 && modifier != EXPAND_MEMORY
12488 && !inner_reference_p
12489 && mode != BLKmode
12490 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12492 /* If the target does have special handling for unaligned
12493 loads of mode then use them. */
12494 if ((icode = optab_handler (movmisalign_optab, mode))
12495 != CODE_FOR_nothing)
12497 rtx reg;
12499 op0 = adjust_address (op0, mode, 0);
12500 /* We've already validated the memory, and we're creating a
12501 new pseudo destination. The predicates really can't
12502 fail. */
12503 reg = gen_reg_rtx (mode);
12505 /* Nor can the insn generator. */
12506 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12507 emit_insn (insn);
12508 return reg;
12510 else if (STRICT_ALIGNMENT)
12512 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12513 poly_uint64 temp_size = mode_size;
12514 if (GET_MODE (op0) != BLKmode)
12515 temp_size = upper_bound (temp_size,
12516 GET_MODE_SIZE (GET_MODE (op0)));
12517 rtx new_rtx
12518 = assign_stack_temp_for_type (mode, temp_size, type);
12519 rtx new_with_op0_mode
12520 = adjust_address (new_rtx, GET_MODE (op0), 0);
12522 gcc_assert (!TREE_ADDRESSABLE (exp));
12524 if (GET_MODE (op0) == BLKmode)
12526 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12527 emit_block_move (new_with_op0_mode, op0, size_rtx,
12528 (modifier == EXPAND_STACK_PARM
12529 ? BLOCK_OP_CALL_PARM
12530 : BLOCK_OP_NORMAL));
12532 else
12533 emit_move_insn (new_with_op0_mode, op0);
12535 op0 = new_rtx;
12539 op0 = adjust_address (op0, mode, 0);
12542 return op0;
12544 case MODIFY_EXPR:
12546 tree lhs = treeop0;
12547 tree rhs = treeop1;
12548 gcc_assert (ignore);
12550 /* Check for |= or &= of a bitfield of size one into another bitfield
12551 of size 1. In this case, (unless we need the result of the
12552 assignment) we can do this more efficiently with a
12553 test followed by an assignment, if necessary.
12555 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12556 things change so we do, this code should be enhanced to
12557 support it. */
12558 if (TREE_CODE (lhs) == COMPONENT_REF
12559 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12560 || TREE_CODE (rhs) == BIT_AND_EXPR)
12561 && TREE_OPERAND (rhs, 0) == lhs
12562 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12563 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12564 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12566 rtx_code_label *label = gen_label_rtx ();
12567 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12568 profile_probability prob = profile_probability::uninitialized ();
12569 if (value)
12570 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12571 else
12572 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12573 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12574 false);
12575 do_pending_stack_adjust ();
12576 emit_label (label);
12577 return const0_rtx;
12580 expand_assignment (lhs, rhs, false);
12581 return const0_rtx;
12584 case ADDR_EXPR:
12585 return expand_expr_addr_expr (exp, target, tmode, modifier);
12587 case REALPART_EXPR:
12588 op0 = expand_normal (treeop0);
12589 return read_complex_part (op0, false);
12591 case IMAGPART_EXPR:
12592 op0 = expand_normal (treeop0);
12593 return read_complex_part (op0, true);
12595 case RETURN_EXPR:
12596 case LABEL_EXPR:
12597 case GOTO_EXPR:
12598 case SWITCH_EXPR:
12599 case ASM_EXPR:
12600 /* Expanded in cfgexpand.cc. */
12601 gcc_unreachable ();
12603 case TRY_CATCH_EXPR:
12604 case CATCH_EXPR:
12605 case EH_FILTER_EXPR:
12606 case TRY_FINALLY_EXPR:
12607 case EH_ELSE_EXPR:
12608 /* Lowered by tree-eh.cc. */
12609 gcc_unreachable ();
12611 case WITH_CLEANUP_EXPR:
12612 case CLEANUP_POINT_EXPR:
12613 case TARGET_EXPR:
12614 case CASE_LABEL_EXPR:
12615 case VA_ARG_EXPR:
12616 case BIND_EXPR:
12617 case INIT_EXPR:
12618 case CONJ_EXPR:
12619 case COMPOUND_EXPR:
12620 case PREINCREMENT_EXPR:
12621 case PREDECREMENT_EXPR:
12622 case POSTINCREMENT_EXPR:
12623 case POSTDECREMENT_EXPR:
12624 case LOOP_EXPR:
12625 case EXIT_EXPR:
12626 case COMPOUND_LITERAL_EXPR:
12627 /* Lowered by gimplify.cc. */
12628 gcc_unreachable ();
12630 case FDESC_EXPR:
12631 /* Function descriptors are not valid except for as
12632 initialization constants, and should not be expanded. */
12633 gcc_unreachable ();
12635 case WITH_SIZE_EXPR:
12636 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12637 have pulled out the size to use in whatever context it needed. */
12638 return expand_expr_real (treeop0, original_target, tmode,
12639 modifier, alt_rtl, inner_reference_p);
12641 default:
12642 return expand_expr_real_2 (&ops, target, tmode, modifier);
12645 #undef EXTEND_BITINT
12647 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12648 signedness of TYPE), possibly returning the result in TARGET.
12649 TYPE is known to be a partial integer type. */
12650 static rtx
12651 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12653 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12654 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12655 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12656 && (!target || GET_MODE (target) == mode));
12658 /* For constant values, reduce using wide_int_to_tree. */
12659 if (poly_int_rtx_p (exp))
12661 auto value = wi::to_poly_wide (exp, mode);
12662 tree t = wide_int_to_tree (type, value);
12663 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12665 else if (TYPE_UNSIGNED (type))
12667 rtx mask = immed_wide_int_const
12668 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12669 return expand_and (mode, exp, mask, target);
12671 else
12673 int count = GET_MODE_PRECISION (mode) - prec;
12674 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12675 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12679 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12680 when applied to the address of EXP produces an address known to be
12681 aligned more than BIGGEST_ALIGNMENT. */
12683 static bool
12684 is_aligning_offset (const_tree offset, const_tree exp)
12686 /* Strip off any conversions. */
12687 while (CONVERT_EXPR_P (offset))
12688 offset = TREE_OPERAND (offset, 0);
12690 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12691 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12692 if (TREE_CODE (offset) != BIT_AND_EXPR
12693 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12694 || compare_tree_int (TREE_OPERAND (offset, 1),
12695 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12696 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12697 return false;
12699 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12700 It must be NEGATE_EXPR. Then strip any more conversions. */
12701 offset = TREE_OPERAND (offset, 0);
12702 while (CONVERT_EXPR_P (offset))
12703 offset = TREE_OPERAND (offset, 0);
12705 if (TREE_CODE (offset) != NEGATE_EXPR)
12706 return false;
12708 offset = TREE_OPERAND (offset, 0);
12709 while (CONVERT_EXPR_P (offset))
12710 offset = TREE_OPERAND (offset, 0);
12712 /* This must now be the address of EXP. */
12713 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12716 /* Return a STRING_CST corresponding to ARG's constant initializer either
12717 if it's a string constant, or, when VALREP is set, any other constant,
12718 or null otherwise.
12719 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12720 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12721 to the size of the byte string. If nonnull, set *DECL to the constant
12722 declaration ARG refers to. */
12724 static tree
12725 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12726 bool valrep = false)
12728 tree dummy = NULL_TREE;
12729 if (!mem_size)
12730 mem_size = &dummy;
12732 /* Store the type of the original expression before conversions
12733 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12734 removed. */
12735 tree argtype = TREE_TYPE (arg);
12737 tree array;
12738 STRIP_NOPS (arg);
12740 /* Non-constant index into the character array in an ARRAY_REF
12741 expression or null. */
12742 tree varidx = NULL_TREE;
12744 poly_int64 base_off = 0;
12746 if (TREE_CODE (arg) == ADDR_EXPR)
12748 arg = TREE_OPERAND (arg, 0);
12749 tree ref = arg;
12750 if (TREE_CODE (arg) == ARRAY_REF)
12752 tree idx = TREE_OPERAND (arg, 1);
12753 if (TREE_CODE (idx) != INTEGER_CST)
12755 /* From a pointer (but not array) argument extract the variable
12756 index to prevent get_addr_base_and_unit_offset() from failing
12757 due to it. Use it later to compute the non-constant offset
12758 into the string and return it to the caller. */
12759 varidx = idx;
12760 ref = TREE_OPERAND (arg, 0);
12762 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12763 return NULL_TREE;
12765 if (!integer_zerop (array_ref_low_bound (arg)))
12766 return NULL_TREE;
12768 if (!integer_onep (array_ref_element_size (arg)))
12769 return NULL_TREE;
12772 array = get_addr_base_and_unit_offset (ref, &base_off);
12773 if (!array
12774 || (TREE_CODE (array) != VAR_DECL
12775 && TREE_CODE (array) != CONST_DECL
12776 && TREE_CODE (array) != STRING_CST))
12777 return NULL_TREE;
12779 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12781 tree arg0 = TREE_OPERAND (arg, 0);
12782 tree arg1 = TREE_OPERAND (arg, 1);
12784 tree offset;
12785 tree str = string_constant (arg0, &offset, mem_size, decl);
12786 if (!str)
12788 str = string_constant (arg1, &offset, mem_size, decl);
12789 arg1 = arg0;
12792 if (str)
12794 /* Avoid pointers to arrays (see bug 86622). */
12795 if (POINTER_TYPE_P (TREE_TYPE (arg))
12796 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12797 && !(decl && !*decl)
12798 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12799 && tree_fits_uhwi_p (*mem_size)
12800 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12801 return NULL_TREE;
12803 tree type = TREE_TYPE (offset);
12804 arg1 = fold_convert (type, arg1);
12805 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12806 return str;
12808 return NULL_TREE;
12810 else if (TREE_CODE (arg) == SSA_NAME)
12812 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12813 if (!is_gimple_assign (stmt))
12814 return NULL_TREE;
12816 tree rhs1 = gimple_assign_rhs1 (stmt);
12817 tree_code code = gimple_assign_rhs_code (stmt);
12818 if (code == ADDR_EXPR)
12819 return string_constant (rhs1, ptr_offset, mem_size, decl);
12820 else if (code != POINTER_PLUS_EXPR)
12821 return NULL_TREE;
12823 tree offset;
12824 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12826 /* Avoid pointers to arrays (see bug 86622). */
12827 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12828 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12829 && !(decl && !*decl)
12830 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12831 && tree_fits_uhwi_p (*mem_size)
12832 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12833 return NULL_TREE;
12835 tree rhs2 = gimple_assign_rhs2 (stmt);
12836 tree type = TREE_TYPE (offset);
12837 rhs2 = fold_convert (type, rhs2);
12838 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12839 return str;
12841 return NULL_TREE;
12843 else if (DECL_P (arg))
12844 array = arg;
12845 else
12846 return NULL_TREE;
12848 tree offset = wide_int_to_tree (sizetype, base_off);
12849 if (varidx)
12851 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12852 return NULL_TREE;
12854 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12855 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12856 if (TREE_CODE (chartype) != INTEGER_TYPE)
12857 return NULL;
12859 offset = fold_convert (sizetype, varidx);
12862 if (TREE_CODE (array) == STRING_CST)
12864 *ptr_offset = fold_convert (sizetype, offset);
12865 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12866 if (decl)
12867 *decl = NULL_TREE;
12868 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12869 >= TREE_STRING_LENGTH (array));
12870 return array;
12873 tree init = ctor_for_folding (array);
12874 if (!init || init == error_mark_node)
12875 return NULL_TREE;
12877 if (valrep)
12879 HOST_WIDE_INT cstoff;
12880 if (!base_off.is_constant (&cstoff))
12881 return NULL_TREE;
12883 /* Check that the host and target are sane. */
12884 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12885 return NULL_TREE;
12887 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12888 if (typesz <= 0 || (int) typesz != typesz)
12889 return NULL_TREE;
12891 HOST_WIDE_INT size = typesz;
12892 if (VAR_P (array)
12893 && DECL_SIZE_UNIT (array)
12894 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12896 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12897 gcc_checking_assert (size >= typesz);
12900 /* If value representation was requested convert the initializer
12901 for the whole array or object into a string of bytes forming
12902 its value representation and return it. */
12903 unsigned char *bytes = XNEWVEC (unsigned char, size);
12904 int r = native_encode_initializer (init, bytes, size);
12905 if (r < typesz)
12907 XDELETEVEC (bytes);
12908 return NULL_TREE;
12911 if (r < size)
12912 memset (bytes + r, '\0', size - r);
12914 const char *p = reinterpret_cast<const char *>(bytes);
12915 init = build_string_literal (size, p, char_type_node);
12916 init = TREE_OPERAND (init, 0);
12917 init = TREE_OPERAND (init, 0);
12918 XDELETE (bytes);
12920 *mem_size = size_int (TREE_STRING_LENGTH (init));
12921 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12923 if (decl)
12924 *decl = array;
12926 return init;
12929 if (TREE_CODE (init) == CONSTRUCTOR)
12931 /* Convert the 64-bit constant offset to a wider type to avoid
12932 overflow and use it to obtain the initializer for the subobject
12933 it points into. */
12934 offset_int wioff;
12935 if (!base_off.is_constant (&wioff))
12936 return NULL_TREE;
12938 wioff *= BITS_PER_UNIT;
12939 if (!wi::fits_uhwi_p (wioff))
12940 return NULL_TREE;
12942 base_off = wioff.to_uhwi ();
12943 unsigned HOST_WIDE_INT fieldoff = 0;
12944 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12945 &fieldoff);
12946 if (!init || init == error_mark_node)
12947 return NULL_TREE;
12949 HOST_WIDE_INT cstoff;
12950 if (!base_off.is_constant (&cstoff))
12951 return NULL_TREE;
12953 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12954 tree off = build_int_cst (sizetype, cstoff);
12955 if (varidx)
12956 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12957 else
12958 offset = off;
12961 *ptr_offset = offset;
12963 tree inittype = TREE_TYPE (init);
12965 if (TREE_CODE (init) == INTEGER_CST
12966 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12967 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12969 /* Check that the host and target are sane. */
12970 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12971 return NULL_TREE;
12973 /* For a reference to (address of) a single constant character,
12974 store the native representation of the character in CHARBUF.
12975 If the reference is to an element of an array or a member
12976 of a struct, only consider narrow characters until ctors
12977 for wide character arrays are transformed to STRING_CSTs
12978 like those for narrow arrays. */
12979 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12980 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12981 if (len > 0)
12983 /* Construct a string literal with elements of INITTYPE and
12984 the representation above. Then strip
12985 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12986 init = build_string_literal (len, (char *)charbuf, inittype);
12987 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12991 tree initsize = TYPE_SIZE_UNIT (inittype);
12993 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12995 /* Fold an empty/zero constructor for an implicitly initialized
12996 object or subobject into the empty string. */
12998 /* Determine the character type from that of the original
12999 expression. */
13000 tree chartype = argtype;
13001 if (POINTER_TYPE_P (chartype))
13002 chartype = TREE_TYPE (chartype);
13003 while (TREE_CODE (chartype) == ARRAY_TYPE)
13004 chartype = TREE_TYPE (chartype);
13006 if (INTEGRAL_TYPE_P (chartype)
13007 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
13009 /* Convert a char array to an empty STRING_CST having an array
13010 of the expected type and size. */
13011 if (!initsize)
13012 initsize = integer_zero_node;
13014 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
13015 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
13016 return NULL_TREE;
13018 init = build_string_literal (size, NULL, chartype, size);
13019 init = TREE_OPERAND (init, 0);
13020 init = TREE_OPERAND (init, 0);
13022 *ptr_offset = integer_zero_node;
13026 if (decl)
13027 *decl = array;
13029 if (TREE_CODE (init) != STRING_CST)
13030 return NULL_TREE;
13032 *mem_size = initsize;
13034 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
13036 return init;
13039 /* Return STRING_CST if an ARG corresponds to a string constant or zero
13040 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
13041 non-constant) offset in bytes within the string that ARG is accessing.
13042 If MEM_SIZE is non-zero the storage size of the memory is returned.
13043 If DECL is non-zero the constant declaration is returned if available. */
13045 tree
13046 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
13048 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
13051 /* Similar to string_constant, return a STRING_CST corresponding
13052 to the value representation of the first argument if it's
13053 a constant. */
13055 tree
13056 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
13058 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
13061 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
13062 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
13063 for C2 > 0 to x & C3 == C2
13064 for C2 < 0 to x & C3 == (C2 & C3). */
13065 enum tree_code
13066 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13068 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13069 tree treeop0 = gimple_assign_rhs1 (stmt);
13070 tree treeop1 = gimple_assign_rhs2 (stmt);
13071 tree type = TREE_TYPE (*arg0);
13072 scalar_int_mode mode;
13073 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13074 return code;
13075 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13076 || TYPE_PRECISION (type) <= 1
13077 || TYPE_UNSIGNED (type)
13078 /* Signed x % c == 0 should have been optimized into unsigned modulo
13079 earlier. */
13080 || integer_zerop (*arg1)
13081 /* If c is known to be non-negative, modulo will be expanded as unsigned
13082 modulo. */
13083 || get_range_pos_neg (treeop0) == 1)
13084 return code;
13086 /* x % c == d where d < 0 && d <= -c should be always false. */
13087 if (tree_int_cst_sgn (*arg1) == -1
13088 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
13089 return code;
13091 int prec = TYPE_PRECISION (type);
13092 wide_int w = wi::to_wide (treeop1) - 1;
13093 w |= wi::shifted_mask (0, prec - 1, true, prec);
13094 tree c3 = wide_int_to_tree (type, w);
13095 tree c4 = *arg1;
13096 if (tree_int_cst_sgn (*arg1) == -1)
13097 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
13099 rtx op0 = expand_normal (treeop0);
13100 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13102 bool speed_p = optimize_insn_for_speed_p ();
13104 do_pending_stack_adjust ();
13106 location_t loc = gimple_location (stmt);
13107 struct separate_ops ops;
13108 ops.code = TRUNC_MOD_EXPR;
13109 ops.location = loc;
13110 ops.type = TREE_TYPE (treeop0);
13111 ops.op0 = treeop0;
13112 ops.op1 = treeop1;
13113 ops.op2 = NULL_TREE;
13114 start_sequence ();
13115 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13116 EXPAND_NORMAL);
13117 rtx_insn *moinsns = get_insns ();
13118 end_sequence ();
13120 unsigned mocost = seq_cost (moinsns, speed_p);
13121 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13122 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13124 ops.code = BIT_AND_EXPR;
13125 ops.location = loc;
13126 ops.type = TREE_TYPE (treeop0);
13127 ops.op0 = treeop0;
13128 ops.op1 = c3;
13129 ops.op2 = NULL_TREE;
13130 start_sequence ();
13131 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13132 EXPAND_NORMAL);
13133 rtx_insn *muinsns = get_insns ();
13134 end_sequence ();
13136 unsigned mucost = seq_cost (muinsns, speed_p);
13137 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
13138 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
13140 if (mocost <= mucost)
13142 emit_insn (moinsns);
13143 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13144 return code;
13147 emit_insn (muinsns);
13148 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
13149 *arg1 = c4;
13150 return code;
13153 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
13154 If C1 is odd to:
13155 (X - C2) * C3 <= C4 (or >), where
13156 C3 is modular multiplicative inverse of C1 and 1<<prec and
13157 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
13158 if C2 > ((1<<prec) - 1) % C1).
13159 If C1 is even, S = ctz (C1) and C2 is 0, use
13160 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
13161 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
13163 For signed (X % C1) == 0 if C1 is odd to (all operations in it
13164 unsigned):
13165 (X * C3) + C4 <= 2 * C4, where
13166 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
13167 C4 is ((1<<(prec - 1) - 1) / C1).
13168 If C1 is even, S = ctz(C1), use
13169 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
13170 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
13171 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
13173 See the Hacker's Delight book, section 10-17. */
13174 enum tree_code
13175 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13177 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
13178 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
13180 if (optimize < 2)
13181 return code;
13183 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13184 if (stmt == NULL)
13185 return code;
13187 tree treeop0 = gimple_assign_rhs1 (stmt);
13188 tree treeop1 = gimple_assign_rhs2 (stmt);
13189 if (TREE_CODE (treeop0) != SSA_NAME
13190 || TREE_CODE (treeop1) != INTEGER_CST
13191 /* Don't optimize the undefined behavior case x % 0;
13192 x % 1 should have been optimized into zero, punt if
13193 it makes it here for whatever reason;
13194 x % -c should have been optimized into x % c. */
13195 || compare_tree_int (treeop1, 2) <= 0
13196 /* Likewise x % c == d where d >= c should be always false. */
13197 || tree_int_cst_le (treeop1, *arg1))
13198 return code;
13200 /* Unsigned x % pow2 is handled right already, for signed
13201 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
13202 if (integer_pow2p (treeop1))
13203 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
13205 tree type = TREE_TYPE (*arg0);
13206 scalar_int_mode mode;
13207 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13208 return code;
13209 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13210 || TYPE_PRECISION (type) <= 1)
13211 return code;
13213 signop sgn = UNSIGNED;
13214 /* If both operands are known to have the sign bit clear, handle
13215 even the signed modulo case as unsigned. treeop1 is always
13216 positive >= 2, checked above. */
13217 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
13218 sgn = SIGNED;
13220 if (!TYPE_UNSIGNED (type))
13222 if (tree_int_cst_sgn (*arg1) == -1)
13223 return code;
13224 type = unsigned_type_for (type);
13225 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
13226 return code;
13229 int prec = TYPE_PRECISION (type);
13230 wide_int w = wi::to_wide (treeop1);
13231 int shift = wi::ctz (w);
13232 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
13233 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
13234 If C1 is odd, we can handle all cases by subtracting
13235 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
13236 e.g. by testing for overflow on the subtraction, punt on that for now
13237 though. */
13238 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
13240 if (sgn == SIGNED)
13241 return code;
13242 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
13243 if (wi::gtu_p (wi::to_wide (*arg1), x))
13244 return code;
13247 imm_use_iterator imm_iter;
13248 use_operand_p use_p;
13249 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
13251 gimple *use_stmt = USE_STMT (use_p);
13252 /* Punt if treeop0 is used in the same bb in a division
13253 or another modulo with the same divisor. We should expect
13254 the division and modulo combined together. */
13255 if (use_stmt == stmt
13256 || gimple_bb (use_stmt) != gimple_bb (stmt))
13257 continue;
13258 if (!is_gimple_assign (use_stmt)
13259 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
13260 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
13261 continue;
13262 if (gimple_assign_rhs1 (use_stmt) != treeop0
13263 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
13264 continue;
13265 return code;
13268 w = wi::lrshift (w, shift);
13269 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
13270 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
13271 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
13272 tree c3 = wide_int_to_tree (type, m);
13273 tree c5 = NULL_TREE;
13274 wide_int d, e;
13275 if (sgn == UNSIGNED)
13277 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
13278 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
13279 otherwise use < or subtract one from C4. E.g. for
13280 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
13281 x % 3U == 1 already needs to be
13282 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
13283 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
13284 d -= 1;
13285 if (shift)
13286 d = wi::lrshift (d, shift);
13288 else
13290 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
13291 if (!shift)
13292 d = wi::lshift (e, 1);
13293 else
13295 e = wi::bit_and (e, wi::mask (shift, true, prec));
13296 d = wi::lrshift (e, shift - 1);
13298 c5 = wide_int_to_tree (type, e);
13300 tree c4 = wide_int_to_tree (type, d);
13302 rtx op0 = expand_normal (treeop0);
13303 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13305 bool speed_p = optimize_insn_for_speed_p ();
13307 do_pending_stack_adjust ();
13309 location_t loc = gimple_location (stmt);
13310 struct separate_ops ops;
13311 ops.code = TRUNC_MOD_EXPR;
13312 ops.location = loc;
13313 ops.type = TREE_TYPE (treeop0);
13314 ops.op0 = treeop0;
13315 ops.op1 = treeop1;
13316 ops.op2 = NULL_TREE;
13317 start_sequence ();
13318 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13319 EXPAND_NORMAL);
13320 rtx_insn *moinsns = get_insns ();
13321 end_sequence ();
13323 unsigned mocost = seq_cost (moinsns, speed_p);
13324 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13325 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13327 tree t = fold_convert_loc (loc, type, treeop0);
13328 if (!integer_zerop (*arg1))
13329 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
13330 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
13331 if (sgn == SIGNED)
13332 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
13333 if (shift)
13335 tree s = build_int_cst (NULL_TREE, shift);
13336 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
13339 start_sequence ();
13340 rtx mur = expand_normal (t);
13341 rtx_insn *muinsns = get_insns ();
13342 end_sequence ();
13344 unsigned mucost = seq_cost (muinsns, speed_p);
13345 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
13346 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
13348 if (mocost <= mucost)
13350 emit_insn (moinsns);
13351 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13352 return code;
13355 emit_insn (muinsns);
13356 *arg0 = make_tree (type, mur);
13357 *arg1 = c4;
13358 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
13361 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
13363 void
13364 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
13366 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
13367 || code == LT_EXPR || code == LE_EXPR);
13368 gcc_checking_assert (integer_zerop (*arg1));
13370 if (!optimize)
13371 return;
13373 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
13374 if (stmt == NULL)
13375 return;
13377 tree treeop0 = gimple_assign_rhs1 (stmt);
13378 tree treeop1 = gimple_assign_rhs2 (stmt);
13379 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
13380 return;
13382 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
13383 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
13384 "assuming signed overflow does not occur when "
13385 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
13386 op_symbol_code (code), op_symbol_code (code));
13388 *arg0 = treeop0;
13389 *arg1 = treeop1;
13393 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
13394 a single bit equality/inequality test, returns where the result is located. */
13396 static rtx
13397 expand_single_bit_test (location_t loc, enum tree_code code,
13398 tree inner, int bitnum,
13399 tree result_type, rtx target,
13400 machine_mode mode)
13402 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
13404 tree type = TREE_TYPE (inner);
13405 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
13406 int ops_unsigned;
13407 tree signed_type, unsigned_type, intermediate_type;
13408 gimple *inner_def;
13410 /* First, see if we can fold the single bit test into a sign-bit
13411 test. */
13412 if (bitnum == TYPE_PRECISION (type) - 1
13413 && type_has_mode_precision_p (type))
13415 tree stype = signed_type_for (type);
13416 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13417 result_type,
13418 fold_convert_loc (loc, stype, inner),
13419 build_int_cst (stype, 0));
13420 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
13423 /* Otherwise we have (A & C) != 0 where C is a single bit,
13424 convert that into ((A >> C2) & 1). Where C2 = log2(C).
13425 Similarly for (A & C) == 0. */
13427 /* If INNER is a right shift of a constant and it plus BITNUM does
13428 not overflow, adjust BITNUM and INNER. */
13429 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
13430 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
13431 && bitnum < TYPE_PRECISION (type)
13432 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
13433 TYPE_PRECISION (type) - bitnum))
13435 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
13436 inner = gimple_assign_rhs1 (inner_def);
13439 /* If we are going to be able to omit the AND below, we must do our
13440 operations as unsigned. If we must use the AND, we have a choice.
13441 Normally unsigned is faster, but for some machines signed is. */
13442 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
13443 && !flag_syntax_only) ? 0 : 1;
13445 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
13446 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
13447 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
13448 inner = fold_convert_loc (loc, intermediate_type, inner);
13450 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
13452 if (CONST_SCALAR_INT_P (inner0))
13454 wide_int t = rtx_mode_t (inner0, operand_mode);
13455 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
13456 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
13458 int bitpos = bitnum;
13460 if (BYTES_BIG_ENDIAN)
13461 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
13463 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
13464 operand_mode, mode, 0, NULL);
13466 if (code == EQ_EXPR)
13467 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
13468 NULL_RTX, 1, OPTAB_LIB_WIDEN);
13469 if (GET_MODE (inner0) != mode)
13471 rtx t = gen_reg_rtx (mode);
13472 convert_move (t, inner0, 0);
13473 return t;
13475 return inner0;
13478 /* Generate code to calculate OPS, and exploded expression
13479 using a store-flag instruction and return an rtx for the result.
13480 OPS reflects a comparison.
13482 If TARGET is nonzero, store the result there if convenient.
13484 Return zero if there is no suitable set-flag instruction
13485 available on this machine.
13487 Once expand_expr has been called on the arguments of the comparison,
13488 we are committed to doing the store flag, since it is not safe to
13489 re-evaluate the expression. We emit the store-flag insn by calling
13490 emit_store_flag, but only expand the arguments if we have a reason
13491 to believe that emit_store_flag will be successful. If we think that
13492 it will, but it isn't, we have to simulate the store-flag with a
13493 set/jump/set sequence. */
13495 static rtx
13496 do_store_flag (sepops ops, rtx target, machine_mode mode)
13498 enum rtx_code code;
13499 tree arg0, arg1, type;
13500 machine_mode operand_mode;
13501 int unsignedp;
13502 rtx op0, op1;
13503 rtx subtarget = target;
13504 location_t loc = ops->location;
13506 arg0 = ops->op0;
13507 arg1 = ops->op1;
13509 /* Don't crash if the comparison was erroneous. */
13510 if (arg0 == error_mark_node || arg1 == error_mark_node)
13511 return const0_rtx;
13513 type = TREE_TYPE (arg0);
13514 operand_mode = TYPE_MODE (type);
13515 unsignedp = TYPE_UNSIGNED (type);
13517 /* We won't bother with BLKmode store-flag operations because it would mean
13518 passing a lot of information to emit_store_flag. */
13519 if (operand_mode == BLKmode)
13520 return 0;
13522 /* We won't bother with store-flag operations involving function pointers
13523 when function pointers must be canonicalized before comparisons. */
13524 if (targetm.have_canonicalize_funcptr_for_compare ()
13525 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13526 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13527 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13528 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13529 return 0;
13531 STRIP_NOPS (arg0);
13532 STRIP_NOPS (arg1);
13534 /* For vector typed comparisons emit code to generate the desired
13535 all-ones or all-zeros mask. */
13536 if (VECTOR_TYPE_P (ops->type))
13538 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13539 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13540 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13541 return expand_vec_cmp_expr (ops->type, ifexp, target);
13542 else
13543 gcc_unreachable ();
13546 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13547 into (x - C2) * C3 < C4. */
13548 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13549 && TREE_CODE (arg0) == SSA_NAME
13550 && TREE_CODE (arg1) == INTEGER_CST)
13552 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13553 &arg0, &arg1);
13554 if (new_code != ops->code)
13556 struct separate_ops nops = *ops;
13557 nops.code = ops->code = new_code;
13558 nops.op0 = arg0;
13559 nops.op1 = arg1;
13560 nops.type = TREE_TYPE (arg0);
13561 return do_store_flag (&nops, target, mode);
13565 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13566 if (!unsignedp
13567 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13568 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13569 && integer_zerop (arg1)
13570 && TREE_CODE (arg0) == SSA_NAME)
13571 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13573 /* Get the rtx comparison code to use. We know that EXP is a comparison
13574 operation of some type. Some comparisons against 1 and -1 can be
13575 converted to comparisons with zero. Do so here so that the tests
13576 below will be aware that we have a comparison with zero. These
13577 tests will not catch constants in the first operand, but constants
13578 are rarely passed as the first operand. */
13580 switch (ops->code)
13582 case EQ_EXPR:
13583 code = EQ;
13584 break;
13585 case NE_EXPR:
13586 code = NE;
13587 break;
13588 case LT_EXPR:
13589 if (integer_onep (arg1))
13590 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13591 else
13592 code = unsignedp ? LTU : LT;
13593 break;
13594 case LE_EXPR:
13595 if (! unsignedp && integer_all_onesp (arg1))
13596 arg1 = integer_zero_node, code = LT;
13597 else
13598 code = unsignedp ? LEU : LE;
13599 break;
13600 case GT_EXPR:
13601 if (! unsignedp && integer_all_onesp (arg1))
13602 arg1 = integer_zero_node, code = GE;
13603 else
13604 code = unsignedp ? GTU : GT;
13605 break;
13606 case GE_EXPR:
13607 if (integer_onep (arg1))
13608 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13609 else
13610 code = unsignedp ? GEU : GE;
13611 break;
13613 case UNORDERED_EXPR:
13614 code = UNORDERED;
13615 break;
13616 case ORDERED_EXPR:
13617 code = ORDERED;
13618 break;
13619 case UNLT_EXPR:
13620 code = UNLT;
13621 break;
13622 case UNLE_EXPR:
13623 code = UNLE;
13624 break;
13625 case UNGT_EXPR:
13626 code = UNGT;
13627 break;
13628 case UNGE_EXPR:
13629 code = UNGE;
13630 break;
13631 case UNEQ_EXPR:
13632 code = UNEQ;
13633 break;
13634 case LTGT_EXPR:
13635 code = LTGT;
13636 break;
13638 default:
13639 gcc_unreachable ();
13642 /* Put a constant second. */
13643 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13644 || TREE_CODE (arg0) == FIXED_CST)
13646 std::swap (arg0, arg1);
13647 code = swap_condition (code);
13650 /* If this is an equality or inequality test of a single bit, we can
13651 do this by shifting the bit being tested to the low-order bit and
13652 masking the result with the constant 1. If the condition was EQ,
13653 we xor it with 1. This does not require an scc insn and is faster
13654 than an scc insn even if we have it. */
13656 if ((code == NE || code == EQ)
13657 && (integer_zerop (arg1)
13658 || integer_pow2p (arg1))
13659 /* vector types are not handled here. */
13660 && TREE_CODE (TREE_TYPE (arg1)) != VECTOR_TYPE
13661 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13663 tree narg0 = arg0;
13664 wide_int nz = tree_nonzero_bits (narg0);
13665 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR);
13666 /* If the defining statement was (x & POW2), then use that instead of
13667 the non-zero bits. */
13668 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13670 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13671 narg0 = gimple_assign_rhs1 (srcstmt);
13674 if (wi::popcount (nz) == 1
13675 && (integer_zerop (arg1)
13676 || wi::to_wide (arg1) == nz))
13678 int bitnum = wi::exact_log2 (nz);
13679 enum tree_code tcode = EQ_EXPR;
13680 if ((code == NE) ^ !integer_zerop (arg1))
13681 tcode = NE_EXPR;
13683 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13684 return expand_single_bit_test (loc, tcode,
13685 narg0,
13686 bitnum, type, target, mode);
13691 if (! get_subtarget (target)
13692 || GET_MODE (subtarget) != operand_mode)
13693 subtarget = 0;
13695 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13697 if (target == 0)
13698 target = gen_reg_rtx (mode);
13700 /* Try a cstore if possible. */
13701 return emit_store_flag_force (target, code, op0, op1,
13702 operand_mode, unsignedp,
13703 (TYPE_PRECISION (ops->type) == 1
13704 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13707 /* Attempt to generate a casesi instruction. Returns true if successful,
13708 false otherwise (i.e. if there is no casesi instruction).
13710 DEFAULT_PROBABILITY is the probability of jumping to the default
13711 label. */
13712 bool
13713 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13714 rtx table_label, rtx default_label, rtx fallback_label,
13715 profile_probability default_probability)
13717 class expand_operand ops[5];
13718 scalar_int_mode index_mode = SImode;
13719 rtx op1, op2, index;
13721 if (! targetm.have_casesi ())
13722 return false;
13724 /* The index must be some form of integer. Convert it to SImode. */
13725 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13726 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13728 rtx rangertx = expand_normal (range);
13730 /* We must handle the endpoints in the original mode. */
13731 index_expr = build2 (MINUS_EXPR, index_type,
13732 index_expr, minval);
13733 minval = integer_zero_node;
13734 index = expand_normal (index_expr);
13735 if (default_label)
13736 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13737 omode, 1, default_label,
13738 default_probability);
13739 /* Now we can safely truncate. */
13740 index = convert_to_mode (index_mode, index, 0);
13742 else
13744 if (omode != index_mode)
13746 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13747 index_expr = fold_convert (index_type, index_expr);
13750 index = expand_normal (index_expr);
13753 do_pending_stack_adjust ();
13755 op1 = expand_normal (minval);
13756 op2 = expand_normal (range);
13758 create_input_operand (&ops[0], index, index_mode);
13759 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13760 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13761 create_fixed_operand (&ops[3], table_label);
13762 create_fixed_operand (&ops[4], (default_label
13763 ? default_label
13764 : fallback_label));
13765 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13766 return true;
13769 /* Attempt to generate a tablejump instruction; same concept. */
13770 /* Subroutine of the next function.
13772 INDEX is the value being switched on, with the lowest value
13773 in the table already subtracted.
13774 MODE is its expected mode (needed if INDEX is constant).
13775 RANGE is the length of the jump table.
13776 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13778 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13779 index value is out of range.
13780 DEFAULT_PROBABILITY is the probability of jumping to
13781 the default label. */
13783 static void
13784 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13785 rtx default_label, profile_probability default_probability)
13787 rtx temp, vector;
13789 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13790 cfun->cfg->max_jumptable_ents = INTVAL (range);
13792 /* Do an unsigned comparison (in the proper mode) between the index
13793 expression and the value which represents the length of the range.
13794 Since we just finished subtracting the lower bound of the range
13795 from the index expression, this comparison allows us to simultaneously
13796 check that the original index expression value is both greater than
13797 or equal to the minimum value of the range and less than or equal to
13798 the maximum value of the range. */
13800 if (default_label)
13801 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13802 default_label, default_probability);
13804 /* If index is in range, it must fit in Pmode.
13805 Convert to Pmode so we can index with it. */
13806 if (mode != Pmode)
13808 unsigned int width;
13810 /* We know the value of INDEX is between 0 and RANGE. If we have a
13811 sign-extended subreg, and RANGE does not have the sign bit set, then
13812 we have a value that is valid for both sign and zero extension. In
13813 this case, we get better code if we sign extend. */
13814 if (GET_CODE (index) == SUBREG
13815 && SUBREG_PROMOTED_VAR_P (index)
13816 && SUBREG_PROMOTED_SIGNED_P (index)
13817 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13818 <= HOST_BITS_PER_WIDE_INT)
13819 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13820 index = convert_to_mode (Pmode, index, 0);
13821 else
13822 index = convert_to_mode (Pmode, index, 1);
13825 /* Don't let a MEM slip through, because then INDEX that comes
13826 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13827 and break_out_memory_refs will go to work on it and mess it up. */
13828 #ifdef PIC_CASE_VECTOR_ADDRESS
13829 if (flag_pic && !REG_P (index))
13830 index = copy_to_mode_reg (Pmode, index);
13831 #endif
13833 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13834 GET_MODE_SIZE, because this indicates how large insns are. The other
13835 uses should all be Pmode, because they are addresses. This code
13836 could fail if addresses and insns are not the same size. */
13837 index = simplify_gen_binary (MULT, Pmode, index,
13838 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13839 Pmode));
13840 index = simplify_gen_binary (PLUS, Pmode, index,
13841 gen_rtx_LABEL_REF (Pmode, table_label));
13843 #ifdef PIC_CASE_VECTOR_ADDRESS
13844 if (flag_pic)
13845 index = PIC_CASE_VECTOR_ADDRESS (index);
13846 else
13847 #endif
13848 index = memory_address (CASE_VECTOR_MODE, index);
13849 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13850 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13851 convert_move (temp, vector, 0);
13853 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13855 /* If we are generating PIC code or if the table is PC-relative, the
13856 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13857 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13858 emit_barrier ();
13861 bool
13862 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13863 rtx table_label, rtx default_label,
13864 profile_probability default_probability)
13866 rtx index;
13868 if (! targetm.have_tablejump ())
13869 return false;
13871 index_expr = fold_build2 (MINUS_EXPR, index_type,
13872 fold_convert (index_type, index_expr),
13873 fold_convert (index_type, minval));
13874 index = expand_normal (index_expr);
13875 do_pending_stack_adjust ();
13877 do_tablejump (index, TYPE_MODE (index_type),
13878 convert_modes (TYPE_MODE (index_type),
13879 TYPE_MODE (TREE_TYPE (range)),
13880 expand_normal (range),
13881 TYPE_UNSIGNED (TREE_TYPE (range))),
13882 table_label, default_label, default_probability);
13883 return true;
13886 /* Return a CONST_VECTOR rtx representing vector mask for
13887 a VECTOR_CST of booleans. */
13888 static rtx
13889 const_vector_mask_from_tree (tree exp)
13891 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13892 machine_mode inner = GET_MODE_INNER (mode);
13894 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13895 VECTOR_CST_NELTS_PER_PATTERN (exp));
13896 unsigned int count = builder.encoded_nelts ();
13897 for (unsigned int i = 0; i < count; ++i)
13899 tree elt = VECTOR_CST_ELT (exp, i);
13900 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13901 if (integer_zerop (elt))
13902 builder.quick_push (CONST0_RTX (inner));
13903 else if (integer_onep (elt)
13904 || integer_minus_onep (elt))
13905 builder.quick_push (CONSTM1_RTX (inner));
13906 else
13907 gcc_unreachable ();
13909 return builder.build ();
13912 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13913 static rtx
13914 const_vector_from_tree (tree exp)
13916 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13918 if (initializer_zerop (exp))
13919 return CONST0_RTX (mode);
13921 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13922 return const_vector_mask_from_tree (exp);
13924 machine_mode inner = GET_MODE_INNER (mode);
13926 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13927 VECTOR_CST_NELTS_PER_PATTERN (exp));
13928 unsigned int count = builder.encoded_nelts ();
13929 for (unsigned int i = 0; i < count; ++i)
13931 tree elt = VECTOR_CST_ELT (exp, i);
13932 if (TREE_CODE (elt) == REAL_CST)
13933 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13934 inner));
13935 else if (TREE_CODE (elt) == FIXED_CST)
13936 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13937 inner));
13938 else
13939 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13940 inner));
13942 return builder.build ();
13945 /* Build a decl for a personality function given a language prefix. */
13947 tree
13948 build_personality_function (const char *lang)
13950 const char *unwind_and_version;
13951 tree decl, type;
13952 char *name;
13954 switch (targetm_common.except_unwind_info (&global_options))
13956 case UI_NONE:
13957 return NULL;
13958 case UI_SJLJ:
13959 unwind_and_version = "_sj0";
13960 break;
13961 case UI_DWARF2:
13962 case UI_TARGET:
13963 unwind_and_version = "_v0";
13964 break;
13965 case UI_SEH:
13966 unwind_and_version = "_seh0";
13967 break;
13968 default:
13969 gcc_unreachable ();
13972 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13974 type = build_function_type_list (unsigned_type_node,
13975 integer_type_node, integer_type_node,
13976 long_long_unsigned_type_node,
13977 ptr_type_node, ptr_type_node, NULL_TREE);
13978 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13979 get_identifier (name), type);
13980 DECL_ARTIFICIAL (decl) = 1;
13981 DECL_EXTERNAL (decl) = 1;
13982 TREE_PUBLIC (decl) = 1;
13984 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13985 are the flags assigned by targetm.encode_section_info. */
13986 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13988 return decl;
13991 /* Extracts the personality function of DECL and returns the corresponding
13992 libfunc. */
13995 get_personality_function (tree decl)
13997 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13998 enum eh_personality_kind pk;
14000 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
14001 if (pk == eh_personality_none)
14002 return NULL;
14004 if (!personality
14005 && pk == eh_personality_any)
14006 personality = lang_hooks.eh_personality ();
14008 if (pk == eh_personality_lang)
14009 gcc_assert (personality != NULL_TREE);
14011 return XEXP (DECL_RTL (personality), 0);
14014 /* Returns a tree for the size of EXP in bytes. */
14016 static tree
14017 tree_expr_size (const_tree exp)
14019 if (DECL_P (exp)
14020 && DECL_SIZE_UNIT (exp) != 0)
14021 return DECL_SIZE_UNIT (exp);
14022 else
14023 return size_in_bytes (TREE_TYPE (exp));
14026 /* Return an rtx for the size in bytes of the value of EXP. */
14029 expr_size (tree exp)
14031 tree size;
14033 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
14034 size = TREE_OPERAND (exp, 1);
14035 else
14037 size = tree_expr_size (exp);
14038 gcc_assert (size);
14039 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
14042 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
14045 /* Return a wide integer for the size in bytes of the value of EXP, or -1
14046 if the size can vary or is larger than an integer. */
14048 HOST_WIDE_INT
14049 int_expr_size (const_tree exp)
14051 tree size;
14053 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
14054 size = TREE_OPERAND (exp, 1);
14055 else
14057 size = tree_expr_size (exp);
14058 gcc_assert (size);
14061 if (size == 0 || !tree_fits_shwi_p (size))
14062 return -1;
14064 return tree_to_shwi (size);