Debian Codegraph @

gcc/expr.cc

raw
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "ssa.h"
32 #include "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"
68 #include "internal-fn.h"
69
70
71 /* If this is nonzero, we do not bother generating VOLATILE
72 around volatile memory references, and we are willing to
73 output indirect addresses. If cse is to follow, we reject
74 indirect addresses so a useful potential cse is generated;
75 if it is used only once, instruction combination will produce
76 the same indirect address eventually. */
77 int cse_not_expected;
78
79 static bool block_move_libcall_safe_for_call_parm (void);
80 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
81 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
82 unsigned HOST_WIDE_INT,
83 unsigned HOST_WIDE_INT, bool);
84 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned, int);
85 static void emit_block_move_via_sized_loop (rtx, rtx, rtx, unsigned, unsigned);
86 static void emit_block_move_via_oriented_loop (rtx, rtx, rtx, unsigned, unsigned);
87 static rtx emit_block_cmp_via_loop (rtx, rtx, rtx, tree, rtx, bool,
88 unsigned, unsigned);
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);
93
94 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
95
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 (const_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);
107
108
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. */
112
113 void
114 init_expr_target (void)
115 {
116 rtx pat;
117 int num_clobbers;
118 rtx mem, mem1;
119 rtx reg;
120
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);
126
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);
130
131 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
132 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
133 PATTERN (insn) = pat;
134
135 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
136 mode = (machine_mode) ((int) mode + 1))
137 {
138 int regno;
139
140 direct_load[(int) mode] = direct_store[(int) mode] = 0;
141 PUT_MODE (mem, mode);
142 PUT_MODE (mem1, mode);
143
144 /* See if there is some register that can be used in this mode and
145 directly loaded or stored from memory. */
146
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++)
151 {
152 if (!targetm.hard_regno_mode_ok (regno, mode))
153 continue;
154
155 set_mode_and_regno (reg, mode, regno);
156
157 SET_SRC (pat) = mem;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
161
162 SET_SRC (pat) = mem1;
163 SET_DEST (pat) = reg;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_load[(int) mode] = 1;
166
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
171
172 SET_SRC (pat) = reg;
173 SET_DEST (pat) = mem1;
174 if (recog (pat, insn, &num_clobbers) >= 0)
175 direct_store[(int) mode] = 1;
176 }
177 }
178
179 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
180
181 opt_scalar_float_mode mode_iter;
182 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
183 {
184 scalar_float_mode mode = mode_iter.require ();
185 scalar_float_mode srcmode;
186 FOR_EACH_MODE_UNTIL (srcmode, mode)
187 {
188 enum insn_code ic;
189
190 ic = can_extend_p (mode, srcmode, 0);
191 if (ic == CODE_FOR_nothing)
192 continue;
193
194 PUT_MODE (mem, srcmode);
195
196 if (insn_operand_matches (ic, 1, mem))
197 float_extend_from_mem[mode][srcmode] = true;
198 }
199 }
200 }
201
202 /* This is run at the start of compiling a function. */
203
204 void
205 init_expr (void)
206 {
207 memset (&crtl->expr, 0, sizeof (crtl->expr));
208 }
209
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. */
215
216 void
217 convert_move (rtx to, rtx from, int unsignedp)
218 {
219 machine_mode to_mode = GET_MODE (to);
220 machine_mode from_mode = GET_MODE (from);
221
222 gcc_assert (to_mode != BLKmode);
223 gcc_assert (from_mode != BLKmode);
224
225 /* If the source and destination are already the same, then there's
226 nothing to do. */
227 if (to == from)
228 return;
229
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. */
233
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))
241 {
242 scalar_int_mode int_orig_mode;
243 scalar_int_mode int_inner_mode;
244 machine_mode orig_mode = GET_MODE (from);
245
246 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
247 from_mode = to_int_mode;
248
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))
259 {
260 SUBREG_PROMOTED_VAR_P (from) = 1;
261 SUBREG_PROMOTED_SET (from, unsignedp);
262 }
263 }
264
265 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
266
267 if (to_mode == from_mode
268 || (from_mode == VOIDmode && CONSTANT_P (from)))
269 {
270 emit_move_insn (to, from);
271 return;
272 }
273
274 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
275 {
276 if (GET_MODE_UNIT_PRECISION (to_mode)
277 > GET_MODE_UNIT_PRECISION (from_mode))
278 {
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)
282 {
283 emit_unop_insn (icode, to, from,
284 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
285 return;
286 }
287 }
288
289 if (GET_MODE_UNIT_PRECISION (to_mode)
290 < GET_MODE_UNIT_PRECISION (from_mode))
291 {
292 insn_code icode = convert_optab_handler (trunc_optab,
293 to_mode, from_mode);
294 if (icode != CODE_FOR_nothing)
295 {
296 emit_unop_insn (icode, to, from, TRUNCATE);
297 return;
298 }
299 }
300
301 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
302 GET_MODE_BITSIZE (to_mode)));
303
304 if (VECTOR_MODE_P (to_mode))
305 from = force_subreg (to_mode, from, GET_MODE (from), 0);
306 else
307 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
308
309 emit_move_insn (to, from);
310 return;
311 }
312
313 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
314 {
315 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
316 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
317 return;
318 }
319
320 convert_mode_scalar (to, from, unsignedp);
321 }
322
323 /* Like convert_move, but deals only with scalar modes. */
324
325 static void
326 convert_mode_scalar (rtx to, rtx from, int unsignedp)
327 {
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;
335
336 gcc_assert (to_real == from_real);
337
338 /* rtx code for making an equivalent value. */
339 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
340 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
341
342 auto acceptable_same_precision_modes
343 = [] (scalar_mode from_mode, scalar_mode to_mode) -> bool
344 {
345 if (DECIMAL_FLOAT_MODE_P (from_mode) != DECIMAL_FLOAT_MODE_P (to_mode))
346 return true;
347
348 /* arm_bfloat_half_format <-> ieee_half_format */
349 if ((REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
350 && REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
351 || (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
352 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format))
353 return true;
354
355 /* ibm_extended_format <-> ieee_quad_format */
356 if ((REAL_MODE_FORMAT (from_mode) == &ibm_extended_format
357 && REAL_MODE_FORMAT (to_mode) == &ieee_quad_format)
358 || (REAL_MODE_FORMAT (from_mode) == &ieee_quad_format
359 && REAL_MODE_FORMAT (to_mode) == &ibm_extended_format))
360 return true;
361
362 return false;
363 };
364
365 if (to_real)
366 {
367 rtx value;
368 rtx_insn *insns;
369 convert_optab tab;
370
371 gcc_assert ((GET_MODE_PRECISION (from_mode)
372 != GET_MODE_PRECISION (to_mode))
373 || acceptable_same_precision_modes (from_mode, to_mode));
374
375 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
376 {
377 if ((REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
378 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format)
379 || (REAL_MODE_FORMAT (to_mode) == &ieee_quad_format
380 && REAL_MODE_FORMAT (from_mode) == &ibm_extended_format))
381 /* libgcc implements just __trunchfbf2, not __extendhfbf2;
382 and __trunctfkf2, not __extendtfkf2. */
383 tab = trunc_optab;
384 else
385 /* Conversion between decimal float and binary float, same
386 size. */
387 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
388 }
389 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
390 tab = sext_optab;
391 else
392 tab = trunc_optab;
393
394 /* Try converting directly if the insn is supported. */
395
396 code = convert_optab_handler (tab, to_mode, from_mode);
397 if (code != CODE_FOR_nothing)
398 {
399 emit_unop_insn (code, to, from,
400 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
401 return;
402 }
403
404 #ifdef HAVE_SFmode
405 if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
406 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
407 {
408 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode))
409 {
410 /* To cut down on libgcc size, implement
411 BFmode -> {DF,XF,TF}mode conversions by
412 BFmode -> SFmode -> {DF,XF,TF}mode conversions. */
413 rtx temp = gen_reg_rtx (SFmode);
414 convert_mode_scalar (temp, from, unsignedp);
415 convert_mode_scalar (to, temp, unsignedp);
416 return;
417 }
418 if (REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
419 {
420 /* Similarly, implement BFmode -> HFmode as
421 BFmode -> SFmode -> HFmode conversion where SFmode
422 has superset of BFmode values. We don't need
423 to handle sNaNs by raising exception and turning
424 it into qNaN though, as that can be done in the
425 SFmode -> HFmode conversion too. */
426 rtx temp = gen_reg_rtx (SFmode);
427 int save_flag_finite_math_only = flag_finite_math_only;
428 flag_finite_math_only = true;
429 convert_mode_scalar (temp, from, unsignedp);
430 flag_finite_math_only = save_flag_finite_math_only;
431 convert_mode_scalar (to, temp, unsignedp);
432 return;
433 }
434 if (to_mode == SFmode
435 && !HONOR_NANS (from_mode)
436 && !HONOR_NANS (to_mode)
437 && optimize_insn_for_speed_p ())
438 {
439 /* If we don't expect sNaNs, for BFmode -> SFmode we can just
440 shift the bits up. */
441 machine_mode fromi_mode, toi_mode;
442 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
443 0).exists (&fromi_mode)
444 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
445 0).exists (&toi_mode))
446 {
447 start_sequence ();
448 rtx fromi = force_lowpart_subreg (fromi_mode, from,
449 from_mode);
450 rtx tof = NULL_RTX;
451 if (fromi)
452 {
453 rtx toi;
454 if (GET_MODE (fromi) == VOIDmode)
455 toi = simplify_unary_operation (ZERO_EXTEND, toi_mode,
456 fromi, fromi_mode);
457 else
458 {
459 toi = gen_reg_rtx (toi_mode);
460 convert_mode_scalar (toi, fromi, 1);
461 }
462 toi
463 = maybe_expand_shift (LSHIFT_EXPR, toi_mode, toi,
464 GET_MODE_PRECISION (to_mode)
465 - GET_MODE_PRECISION (from_mode),
466 NULL_RTX, 1);
467 if (toi)
468 {
469 tof = force_lowpart_subreg (to_mode, toi, toi_mode);
470 if (tof)
471 emit_move_insn (to, tof);
472 }
473 }
474 insns = get_insns ();
475 end_sequence ();
476 if (tof)
477 {
478 emit_insn (insns);
479 return;
480 }
481 }
482 }
483 }
484 if (REAL_MODE_FORMAT (from_mode) == &ieee_single_format
485 && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
486 && !HONOR_NANS (from_mode)
487 && !HONOR_NANS (to_mode)
488 && !flag_rounding_math
489 && optimize_insn_for_speed_p ())
490 {
491 /* If we don't expect qNaNs nor sNaNs and can assume rounding
492 to nearest, we can expand the conversion inline as
493 (fromi + 0x7fff + ((fromi >> 16) & 1)) >> 16. */
494 machine_mode fromi_mode, toi_mode;
495 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
496 0).exists (&fromi_mode)
497 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
498 0).exists (&toi_mode))
499 {
500 start_sequence ();
501 rtx fromi = force_lowpart_subreg (fromi_mode, from, from_mode);
502 rtx tof = NULL_RTX;
503 do
504 {
505 if (!fromi)
506 break;
507 int shift = (GET_MODE_PRECISION (from_mode)
508 - GET_MODE_PRECISION (to_mode));
509 rtx temp1
510 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, fromi,
511 shift, NULL_RTX, 1);
512 if (!temp1)
513 break;
514 rtx temp2
515 = expand_binop (fromi_mode, and_optab, temp1, const1_rtx,
516 NULL_RTX, 1, OPTAB_DIRECT);
517 if (!temp2)
518 break;
519 rtx temp3
520 = expand_binop (fromi_mode, add_optab, fromi,
521 gen_int_mode ((HOST_WIDE_INT_1U
522 << (shift - 1)) - 1,
523 fromi_mode), NULL_RTX,
524 1, OPTAB_DIRECT);
525 if (!temp3)
526 break;
527 rtx temp4
528 = expand_binop (fromi_mode, add_optab, temp3, temp2,
529 NULL_RTX, 1, OPTAB_DIRECT);
530 if (!temp4)
531 break;
532 rtx temp5 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode,
533 temp4, shift, NULL_RTX, 1);
534 if (!temp5)
535 break;
536 rtx temp6 = force_lowpart_subreg (toi_mode, temp5,
537 fromi_mode);
538 if (!temp6)
539 break;
540 tof = force_lowpart_subreg (to_mode, temp6, toi_mode);
541 if (tof)
542 emit_move_insn (to, tof);
543 }
544 while (0);
545 insns = get_insns ();
546 end_sequence ();
547 if (tof)
548 {
549 emit_insn (insns);
550 return;
551 }
552 }
553 }
554 #endif
555
556 /* Otherwise use a libcall. */
557 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
558
559 /* Is this conversion implemented yet? */
560 gcc_assert (libcall);
561
562 start_sequence ();
563 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
564 from, from_mode);
565 insns = get_insns ();
566 end_sequence ();
567 emit_libcall_block (insns, to, value,
568 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
569 from)
570 : gen_rtx_FLOAT_EXTEND (to_mode, from));
571 return;
572 }
573
574 /* Handle pointer conversion. */ /* SPEE 900220. */
575 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
576 {
577 convert_optab ctab;
578
579 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
580 ctab = trunc_optab;
581 else if (unsignedp)
582 ctab = zext_optab;
583 else
584 ctab = sext_optab;
585
586 if (convert_optab_handler (ctab, to_mode, from_mode)
587 != CODE_FOR_nothing)
588 {
589 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
590 to, from, UNKNOWN);
591 return;
592 }
593 }
594
595 /* Targets are expected to provide conversion insns between PxImode and
596 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
597 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
598 {
599 scalar_int_mode full_mode
600 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode)).require ();
601
602 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
603 != CODE_FOR_nothing);
604
605 if (full_mode != from_mode)
606 from = convert_to_mode (full_mode, from, unsignedp);
607 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
608 to, from, UNKNOWN);
609 return;
610 }
611 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
612 {
613 rtx new_from;
614 scalar_int_mode full_mode
615 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode)).require ();
616 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
617 enum insn_code icode;
618
619 icode = convert_optab_handler (ctab, full_mode, from_mode);
620 gcc_assert (icode != CODE_FOR_nothing);
621
622 if (to_mode == full_mode)
623 {
624 emit_unop_insn (icode, to, from, UNKNOWN);
625 return;
626 }
627
628 new_from = gen_reg_rtx (full_mode);
629 emit_unop_insn (icode, new_from, from, UNKNOWN);
630
631 /* else proceed to integer conversions below. */
632 from_mode = full_mode;
633 from = new_from;
634 }
635
636 /* Make sure both are fixed-point modes or both are not. */
637 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
638 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
639 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
640 {
641 /* If we widen from_mode to to_mode and they are in the same class,
642 we won't saturate the result.
643 Otherwise, always saturate the result to play safe. */
644 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
645 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
646 expand_fixed_convert (to, from, 0, 0);
647 else
648 expand_fixed_convert (to, from, 0, 1);
649 return;
650 }
651
652 /* Now both modes are integers. */
653
654 /* Handle expanding beyond a word. */
655 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
656 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
657 {
658 rtx_insn *insns;
659 rtx lowpart;
660 rtx fill_value;
661 rtx lowfrom;
662 int i;
663 scalar_mode lowpart_mode;
664 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
665
666 /* Try converting directly if the insn is supported. */
667 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
668 != CODE_FOR_nothing)
669 {
670 /* If FROM is a SUBREG, put it into a register. Do this
671 so that we always generate the same set of insns for
672 better cse'ing; if an intermediate assignment occurred,
673 we won't be doing the operation directly on the SUBREG. */
674 if (optimize > 0 && GET_CODE (from) == SUBREG)
675 from = force_reg (from_mode, from);
676 emit_unop_insn (code, to, from, equiv_code);
677 return;
678 }
679 /* Next, try converting via full word. */
680 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
681 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
682 != CODE_FOR_nothing))
683 {
684 rtx word_to = gen_reg_rtx (word_mode);
685 if (REG_P (to))
686 {
687 if (reg_overlap_mentioned_p (to, from))
688 from = force_reg (from_mode, from);
689 emit_clobber (to);
690 }
691 convert_move (word_to, from, unsignedp);
692 emit_unop_insn (code, to, word_to, equiv_code);
693 return;
694 }
695
696 /* No special multiword conversion insn; do it by hand. */
697 start_sequence ();
698
699 /* Since we will turn this into a no conflict block, we must ensure
700 the source does not overlap the target so force it into an isolated
701 register when maybe so. Likewise for any MEM input, since the
702 conversion sequence might require several references to it and we
703 must ensure we're getting the same value every time. */
704
705 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
706 from = force_reg (from_mode, from);
707
708 /* Get a copy of FROM widened to a word, if necessary. */
709 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
710 lowpart_mode = word_mode;
711 else
712 lowpart_mode = from_mode;
713
714 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
715
716 lowpart = gen_lowpart (lowpart_mode, to);
717 emit_move_insn (lowpart, lowfrom);
718
719 /* Compute the value to put in each remaining word. */
720 if (unsignedp)
721 fill_value = const0_rtx;
722 else
723 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
724 LT, lowfrom, const0_rtx,
725 lowpart_mode, 0, -1);
726
727 /* Fill the remaining words. */
728 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
729 {
730 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
731 rtx subword = operand_subword (to, index, 1, to_mode);
732
733 gcc_assert (subword);
734
735 if (fill_value != subword)
736 emit_move_insn (subword, fill_value);
737 }
738
739 insns = get_insns ();
740 end_sequence ();
741
742 emit_insn (insns);
743 return;
744 }
745
746 /* Truncating multi-word to a word or less. */
747 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
748 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
749 {
750 if (!((MEM_P (from)
751 && ! MEM_VOLATILE_P (from)
752 && direct_load[(int) to_mode]
753 && ! mode_dependent_address_p (XEXP (from, 0),
754 MEM_ADDR_SPACE (from)))
755 || REG_P (from)
756 || GET_CODE (from) == SUBREG))
757 from = force_reg (from_mode, from);
758 convert_move (to, gen_lowpart (word_mode, from), 0);
759 return;
760 }
761
762 /* Now follow all the conversions between integers
763 no more than a word long. */
764
765 /* For truncation, usually we can just refer to FROM in a narrower mode. */
766 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
767 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
768 {
769 if (!((MEM_P (from)
770 && ! MEM_VOLATILE_P (from)
771 && direct_load[(int) to_mode]
772 && ! mode_dependent_address_p (XEXP (from, 0),
773 MEM_ADDR_SPACE (from)))
774 || REG_P (from)
775 || GET_CODE (from) == SUBREG))
776 from = force_reg (from_mode, from);
777 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
778 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
779 from = copy_to_reg (from);
780 emit_move_insn (to, gen_lowpart (to_mode, from));
781 return;
782 }
783
784 /* Handle extension. */
785 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
786 {
787 /* Convert directly if that works. */
788 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
789 != CODE_FOR_nothing)
790 {
791 emit_unop_insn (code, to, from, equiv_code);
792 return;
793 }
794 else
795 {
796 rtx tmp;
797 int shift_amount;
798
799 /* Search for a mode to convert via. */
800 opt_scalar_mode intermediate_iter;
801 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
802 {
803 scalar_mode intermediate = intermediate_iter.require ();
804 if (((can_extend_p (to_mode, intermediate, unsignedp)
805 != CODE_FOR_nothing)
806 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
807 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
808 intermediate)))
809 && (can_extend_p (intermediate, from_mode, unsignedp)
810 != CODE_FOR_nothing))
811 {
812 convert_move (to, convert_to_mode (intermediate, from,
813 unsignedp), unsignedp);
814 return;
815 }
816 }
817
818 /* No suitable intermediate mode.
819 Generate what we need with shifts. */
820 shift_amount = (GET_MODE_PRECISION (to_mode)
821 - GET_MODE_PRECISION (from_mode));
822 from = gen_lowpart (to_mode, force_reg (from_mode, from));
823 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
824 to, unsignedp);
825 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
826 to, unsignedp);
827 if (tmp != to)
828 emit_move_insn (to, tmp);
829 return;
830 }
831 }
832
833 /* Support special truncate insns for certain modes. */
834 if (convert_optab_handler (trunc_optab, to_mode,
835 from_mode) != CODE_FOR_nothing)
836 {
837 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
838 to, from, UNKNOWN);
839 return;
840 }
841
842 /* Handle truncation of volatile memrefs, and so on;
843 the things that couldn't be truncated directly,
844 and for which there was no special instruction.
845
846 ??? Code above formerly short-circuited this, for most integer
847 mode pairs, with a force_reg in from_mode followed by a recursive
848 call to this routine. Appears always to have been wrong. */
849 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
850 {
851 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
852 emit_move_insn (to, temp);
853 return;
854 }
855
856 /* Mode combination is not recognized. */
857 gcc_unreachable ();
858 }
859
860 /* Return an rtx for a value that would result
861 from converting X to mode MODE.
862 Both X and MODE may be floating, or both integer.
863 UNSIGNEDP is nonzero if X is an unsigned value.
864 This can be done by referring to a part of X in place
865 or by copying to a new temporary with conversion. */
866
867 rtx
868 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
869 {
870 return convert_modes (mode, VOIDmode, x, unsignedp);
871 }
872
873 /* Return an rtx for a value that would result
874 from converting X from mode OLDMODE to mode MODE.
875 Both modes may be floating, or both integer.
876 UNSIGNEDP is nonzero if X is an unsigned value.
877
878 This can be done by referring to a part of X in place
879 or by copying to a new temporary with conversion.
880
881 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
882
883 rtx
884 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
885 {
886 rtx temp;
887 scalar_int_mode int_mode;
888
889 /* If FROM is a SUBREG that indicates that we have already done at least
890 the required extension, strip it. */
891
892 if (GET_CODE (x) == SUBREG
893 && SUBREG_PROMOTED_VAR_P (x)
894 && is_a <scalar_int_mode> (mode, &int_mode)
895 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
896 >= GET_MODE_PRECISION (int_mode))
897 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
898 {
899 scalar_int_mode int_orig_mode;
900 scalar_int_mode int_inner_mode;
901 machine_mode orig_mode = GET_MODE (x);
902 x = gen_lowpart (int_mode, SUBREG_REG (x));
903
904 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
905 the original mode, but narrower than the inner mode. */
906 if (GET_CODE (x) == SUBREG
907 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
908 && GET_MODE_PRECISION (int_mode)
909 > GET_MODE_PRECISION (int_orig_mode)
910 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
911 &int_inner_mode)
912 && GET_MODE_PRECISION (int_inner_mode)
913 > GET_MODE_PRECISION (int_mode))
914 {
915 SUBREG_PROMOTED_VAR_P (x) = 1;
916 SUBREG_PROMOTED_SET (x, unsignedp);
917 }
918 }
919
920 if (GET_MODE (x) != VOIDmode)
921 oldmode = GET_MODE (x);
922
923 if (mode == oldmode)
924 return x;
925
926 if (CONST_SCALAR_INT_P (x)
927 && is_a <scalar_int_mode> (mode, &int_mode))
928 {
929 /* If the caller did not tell us the old mode, then there is not
930 much to do with respect to canonicalization. We have to
931 assume that all the bits are significant. */
932 if (!is_a <scalar_int_mode> (oldmode))
933 oldmode = MAX_MODE_INT;
934 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
935 GET_MODE_PRECISION (int_mode),
936 unsignedp ? UNSIGNED : SIGNED);
937 return immed_wide_int_const (w, int_mode);
938 }
939
940 /* We can do this with a gen_lowpart if both desired and current modes
941 are integer, and this is either a constant integer, a register, or a
942 non-volatile MEM. */
943 scalar_int_mode int_oldmode;
944 if (is_int_mode (mode, &int_mode)
945 && is_int_mode (oldmode, &int_oldmode)
946 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
947 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
948 || CONST_POLY_INT_P (x)
949 || (REG_P (x)
950 && (!HARD_REGISTER_P (x)
951 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
952 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
953 return gen_lowpart (int_mode, x);
954
955 /* Converting from integer constant into mode is always equivalent to an
956 subreg operation. */
957 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
958 {
959 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
960 GET_MODE_BITSIZE (oldmode)));
961 return force_subreg (mode, x, oldmode, 0);
962 }
963
964 temp = gen_reg_rtx (mode);
965 convert_move (temp, x, unsignedp);
966 return temp;
967 }
968
969 /* Variant of convert_modes for ABI parameter passing/return.
970 Return an rtx for a value that would result from converting X from
971 a floating point mode FMODE to wider integer mode MODE. */
972
973 rtx
974 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x)
975 {
976 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode));
977 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require ();
978 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
979 return convert_modes (mode, tmp_mode, tmp, 1);
980 }
981
982 /* Variant of convert_modes for ABI parameter passing/return.
983 Return an rtx for a value that would result from converting X from
984 an integer mode IMODE to a narrower floating point mode MODE. */
985
986 rtx
987 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x)
988 {
989 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode));
990 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require ();
991 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
992 return gen_lowpart_SUBREG (mode, tmp);
993 }
994
995 /* Return the largest alignment we can use for doing a move (or store)
996 of MAX_PIECES. ALIGN is the largest alignment we could use. */
997
998 static unsigned int
999 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
1000 {
1001 scalar_int_mode tmode
1002 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
1003
1004 if (align >= GET_MODE_ALIGNMENT (tmode))
1005 align = GET_MODE_ALIGNMENT (tmode);
1006 else
1007 {
1008 scalar_int_mode xmode = NARROWEST_INT_MODE;
1009 opt_scalar_int_mode mode_iter;
1010 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1011 {
1012 tmode = mode_iter.require ();
1013 if (GET_MODE_SIZE (tmode) > max_pieces
1014 || targetm.slow_unaligned_access (tmode, align))
1015 break;
1016 xmode = tmode;
1017 }
1018
1019 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
1020 }
1021
1022 return align;
1023 }
1024
1025 /* Return true if we know how to implement OP using vectors of bytes. */
1026 static bool
1027 can_use_qi_vectors (by_pieces_operation op)
1028 {
1029 return (op == COMPARE_BY_PIECES
1030 || op == SET_BY_PIECES
1031 || op == CLEAR_BY_PIECES);
1032 }
1033
1034 /* Return true if optabs exists for the mode and certain by pieces
1035 operations. */
1036 static bool
1037 by_pieces_mode_supported_p (fixed_size_mode mode, by_pieces_operation op)
1038 {
1039 if (optab_handler (mov_optab, mode) == CODE_FOR_nothing)
1040 return false;
1041
1042 if ((op == SET_BY_PIECES || op == CLEAR_BY_PIECES)
1043 && VECTOR_MODE_P (mode)
1044 && optab_handler (vec_duplicate_optab, mode) == CODE_FOR_nothing)
1045 return false;
1046
1047 if (op == COMPARE_BY_PIECES
1048 && !can_compare_p (EQ, mode, ccp_jump))
1049 return false;
1050
1051 return true;
1052 }
1053
1054 /* Return the widest mode that can be used to perform part of an
1055 operation OP on SIZE bytes. Try to use QI vector modes where
1056 possible. */
1057 static fixed_size_mode
1058 widest_fixed_size_mode_for_size (unsigned int size, by_pieces_operation op)
1059 {
1060 fixed_size_mode result = NARROWEST_INT_MODE;
1061
1062 gcc_checking_assert (size > 1);
1063
1064 /* Use QI vector only if size is wider than a WORD. */
1065 if (can_use_qi_vectors (op))
1066 {
1067 machine_mode mode;
1068 fixed_size_mode candidate;
1069 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1070 if (is_a<fixed_size_mode> (mode, &candidate)
1071 && GET_MODE_SIZE (candidate) > UNITS_PER_WORD
1072 && GET_MODE_INNER (candidate) == QImode)
1073 {
1074 if (GET_MODE_SIZE (candidate) >= size)
1075 break;
1076 if (by_pieces_mode_supported_p (candidate, op))
1077 result = candidate;
1078 }
1079
1080 if (result != NARROWEST_INT_MODE)
1081 return result;
1082 }
1083
1084 opt_scalar_int_mode tmode;
1085 scalar_int_mode mode;
1086 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
1087 {
1088 mode = tmode.require ();
1089 if (GET_MODE_SIZE (mode) < size
1090 && by_pieces_mode_supported_p (mode, op))
1091 result = mode;
1092 }
1093
1094 return result;
1095 }
1096
1097 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
1098 and should be performed piecewise. */
1099
1100 static bool
1101 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
1102 enum by_pieces_operation op)
1103 {
1104 return targetm.use_by_pieces_infrastructure_p (len, align, op,
1105 optimize_insn_for_speed_p ());
1106 }
1107
1108 /* Determine whether the LEN bytes can be moved by using several move
1109 instructions. Return nonzero if a call to move_by_pieces should
1110 succeed. */
1111
1112 bool
1113 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
1114 {
1115 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
1116 }
1117
1118 /* Return number of insns required to perform operation OP by pieces
1119 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
1120
1121 unsigned HOST_WIDE_INT
1122 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1123 unsigned int max_size, by_pieces_operation op)
1124 {
1125 unsigned HOST_WIDE_INT n_insns = 0;
1126 fixed_size_mode mode;
1127
1128 if (targetm.overlap_op_by_pieces_p ())
1129 {
1130 /* NB: Round up L and ALIGN to the widest integer mode for
1131 MAX_SIZE. */
1132 mode = widest_fixed_size_mode_for_size (max_size, op);
1133 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1134 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1135 if (up > l)
1136 l = up;
1137 align = GET_MODE_ALIGNMENT (mode);
1138 }
1139
1140 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1141
1142 while (max_size > 1 && l > 0)
1143 {
1144 mode = widest_fixed_size_mode_for_size (max_size, op);
1145 gcc_assert (optab_handler (mov_optab, mode) != CODE_FOR_nothing);
1146
1147 unsigned int modesize = GET_MODE_SIZE (mode);
1148
1149 if (align >= GET_MODE_ALIGNMENT (mode))
1150 {
1151 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1152 l %= modesize;
1153 switch (op)
1154 {
1155 default:
1156 n_insns += n_pieces;
1157 break;
1158
1159 case COMPARE_BY_PIECES:
1160 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1161 int batch_ops = 4 * batch - 1;
1162 unsigned HOST_WIDE_INT full = n_pieces / batch;
1163 n_insns += full * batch_ops;
1164 if (n_pieces % batch != 0)
1165 n_insns++;
1166 break;
1167
1168 }
1169 }
1170 max_size = modesize;
1171 }
1172
1173 gcc_assert (!l);
1174 return n_insns;
1175 }
1176
1177 /* Used when performing piecewise block operations, holds information
1178 about one of the memory objects involved. The member functions
1179 can be used to generate code for loading from the object and
1180 updating the address when iterating. */
1181
1182 class pieces_addr
1183 {
1184 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1185 stack pushes. */
1186 rtx m_obj;
1187 /* The address of the object. Can differ from that seen in the
1188 MEM rtx if we copied the address to a register. */
1189 rtx m_addr;
1190 /* Nonzero if the address on the object has an autoincrement already,
1191 signifies whether that was an increment or decrement. */
1192 signed char m_addr_inc;
1193 /* Nonzero if we intend to use autoinc without the address already
1194 having autoinc form. We will insert add insns around each memory
1195 reference, expecting later passes to form autoinc addressing modes.
1196 The only supported options are predecrement and postincrement. */
1197 signed char m_explicit_inc;
1198 /* True if we have either of the two possible cases of using
1199 autoincrement. */
1200 bool m_auto;
1201 /* True if this is an address to be used for load operations rather
1202 than stores. */
1203 bool m_is_load;
1204
1205 /* Optionally, a function to obtain constants for any given offset into
1206 the objects, and data associated with it. */
1207 by_pieces_constfn m_constfn;
1208 void *m_cfndata;
1209 public:
1210 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1211 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1212 void increment_address (HOST_WIDE_INT);
1213 void maybe_predec (HOST_WIDE_INT);
1214 void maybe_postinc (HOST_WIDE_INT);
1215 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1216 int get_addr_inc ()
1217 {
1218 return m_addr_inc;
1219 }
1220 };
1221
1222 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1223 true if the operation to be performed on this object is a load
1224 rather than a store. For stores, OBJ can be NULL, in which case we
1225 assume the operation is a stack push. For loads, the optional
1226 CONSTFN and its associated CFNDATA can be used in place of the
1227 memory load. */
1228
1229 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1230 void *cfndata)
1231 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1232 {
1233 m_addr_inc = 0;
1234 m_auto = false;
1235 if (obj)
1236 {
1237 rtx addr = XEXP (obj, 0);
1238 rtx_code code = GET_CODE (addr);
1239 m_addr = addr;
1240 bool dec = code == PRE_DEC || code == POST_DEC;
1241 bool inc = code == PRE_INC || code == POST_INC;
1242 m_auto = inc || dec;
1243 if (m_auto)
1244 m_addr_inc = dec ? -1 : 1;
1245
1246 /* While we have always looked for these codes here, the code
1247 implementing the memory operation has never handled them.
1248 Support could be added later if necessary or beneficial. */
1249 gcc_assert (code != PRE_INC && code != POST_DEC);
1250 }
1251 else
1252 {
1253 m_addr = NULL_RTX;
1254 if (!is_load)
1255 {
1256 m_auto = true;
1257 if (STACK_GROWS_DOWNWARD)
1258 m_addr_inc = -1;
1259 else
1260 m_addr_inc = 1;
1261 }
1262 else
1263 gcc_assert (constfn != NULL);
1264 }
1265 m_explicit_inc = 0;
1266 if (constfn)
1267 gcc_assert (is_load);
1268 }
1269
1270 /* Decide whether to use autoinc for an address involved in a memory op.
1271 MODE is the mode of the accesses, REVERSE is true if we've decided to
1272 perform the operation starting from the end, and LEN is the length of
1273 the operation. Don't override an earlier decision to set m_auto. */
1274
1275 void
1276 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1277 HOST_WIDE_INT len)
1278 {
1279 if (m_auto || m_obj == NULL_RTX)
1280 return;
1281
1282 bool use_predec = (m_is_load
1283 ? USE_LOAD_PRE_DECREMENT (mode)
1284 : USE_STORE_PRE_DECREMENT (mode));
1285 bool use_postinc = (m_is_load
1286 ? USE_LOAD_POST_INCREMENT (mode)
1287 : USE_STORE_POST_INCREMENT (mode));
1288 machine_mode addr_mode = get_address_mode (m_obj);
1289
1290 if (use_predec && reverse)
1291 {
1292 m_addr = copy_to_mode_reg (addr_mode,
1293 plus_constant (addr_mode,
1294 m_addr, len));
1295 m_auto = true;
1296 m_explicit_inc = -1;
1297 }
1298 else if (use_postinc && !reverse)
1299 {
1300 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1301 m_auto = true;
1302 m_explicit_inc = 1;
1303 }
1304 else if (CONSTANT_P (m_addr))
1305 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1306 }
1307
1308 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1309 are using autoincrement for this address, we don't add the offset,
1310 but we still modify the MEM's properties. */
1311
1312 rtx
1313 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1314 by_pieces_prev *prev)
1315 {
1316 if (m_constfn)
1317 /* Pass the previous data to m_constfn. */
1318 return m_constfn (m_cfndata, prev, offset, mode);
1319 if (m_obj == NULL_RTX)
1320 return NULL_RTX;
1321 if (m_auto)
1322 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1323 else
1324 return adjust_address (m_obj, mode, offset);
1325 }
1326
1327 /* Emit an add instruction to increment the address by SIZE. */
1328
1329 void
1330 pieces_addr::increment_address (HOST_WIDE_INT size)
1331 {
1332 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1333 emit_insn (gen_add2_insn (m_addr, amount));
1334 }
1335
1336 /* If we are supposed to decrement the address after each access, emit code
1337 to do so now. Increment by SIZE (which has should have the correct sign
1338 already). */
1339
1340 void
1341 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1342 {
1343 if (m_explicit_inc >= 0)
1344 return;
1345 gcc_assert (HAVE_PRE_DECREMENT);
1346 increment_address (size);
1347 }
1348
1349 /* If we are supposed to decrement the address after each access, emit code
1350 to do so now. Increment by SIZE. */
1351
1352 void
1353 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1354 {
1355 if (m_explicit_inc <= 0)
1356 return;
1357 gcc_assert (HAVE_POST_INCREMENT);
1358 increment_address (size);
1359 }
1360
1361 /* This structure is used by do_op_by_pieces to describe the operation
1362 to be performed. */
1363
1364 class op_by_pieces_d
1365 {
1366 private:
1367 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1368 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1369
1370 protected:
1371 pieces_addr m_to, m_from;
1372 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1373 use it to check the valid mode size. */
1374 const unsigned HOST_WIDE_INT m_len;
1375 HOST_WIDE_INT m_offset;
1376 unsigned int m_align;
1377 unsigned int m_max_size;
1378 bool m_reverse;
1379 /* True if this is a stack push. */
1380 bool m_push;
1381 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1382 bool m_overlap_op_by_pieces;
1383 /* The type of operation that we're performing. */
1384 by_pieces_operation m_op;
1385
1386 /* Virtual functions, overriden by derived classes for the specific
1387 operation. */
1388 virtual void generate (rtx, rtx, machine_mode) = 0;
1389 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1390 virtual void finish_mode (machine_mode)
1391 {
1392 }
1393
1394 public:
1395 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1396 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1397 by_pieces_operation);
1398 void run ();
1399 };
1400
1401 /* The constructor for an op_by_pieces_d structure. We require two
1402 objects named TO and FROM, which are identified as loads or stores
1403 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1404 and its associated FROM_CFN_DATA can be used to replace loads with
1405 constant values. MAX_PIECES describes the maximum number of bytes
1406 at a time which can be moved efficiently. LEN describes the length
1407 of the operation. */
1408
1409 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1410 bool to_load, rtx from, bool from_load,
1411 by_pieces_constfn from_cfn,
1412 void *from_cfn_data,
1413 unsigned HOST_WIDE_INT len,
1414 unsigned int align, bool push,
1415 by_pieces_operation op)
1416 : m_to (to, to_load, NULL, NULL),
1417 m_from (from, from_load, from_cfn, from_cfn_data),
1418 m_len (len), m_max_size (max_pieces + 1),
1419 m_push (push), m_op (op)
1420 {
1421 int toi = m_to.get_addr_inc ();
1422 int fromi = m_from.get_addr_inc ();
1423 if (toi >= 0 && fromi >= 0)
1424 m_reverse = false;
1425 else if (toi <= 0 && fromi <= 0)
1426 m_reverse = true;
1427 else
1428 gcc_unreachable ();
1429
1430 m_offset = m_reverse ? len : 0;
1431 align = MIN (to ? MEM_ALIGN (to) : align,
1432 from ? MEM_ALIGN (from) : align);
1433
1434 /* If copying requires more than two move insns,
1435 copy addresses to registers (to make displacements shorter)
1436 and use post-increment if available. */
1437 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1438 {
1439 /* Find the mode of the largest comparison. */
1440 fixed_size_mode mode
1441 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1442
1443 m_from.decide_autoinc (mode, m_reverse, len);
1444 m_to.decide_autoinc (mode, m_reverse, len);
1445 }
1446
1447 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1448 m_align = align;
1449
1450 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1451 }
1452
1453 /* This function returns the largest usable integer mode for LEN bytes
1454 whose size is no bigger than size of MODE. */
1455
1456 fixed_size_mode
1457 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1458 {
1459 unsigned int size;
1460 do
1461 {
1462 size = GET_MODE_SIZE (mode);
1463 if (len >= size && prepare_mode (mode, m_align))
1464 break;
1465 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1466 mode = widest_fixed_size_mode_for_size (size, m_op);
1467 }
1468 while (1);
1469 return mode;
1470 }
1471
1472 /* Return the smallest integer or QI vector mode that is not narrower
1473 than SIZE bytes. */
1474
1475 fixed_size_mode
1476 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1477 {
1478 /* Use QI vector only for > size of WORD. */
1479 if (can_use_qi_vectors (m_op) && size > UNITS_PER_WORD)
1480 {
1481 machine_mode mode;
1482 fixed_size_mode candidate;
1483 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1484 if (is_a<fixed_size_mode> (mode, &candidate)
1485 && GET_MODE_INNER (candidate) == QImode)
1486 {
1487 /* Don't return a mode wider than M_LEN. */
1488 if (GET_MODE_SIZE (candidate) > m_len)
1489 break;
1490
1491 if (GET_MODE_SIZE (candidate) >= size
1492 && by_pieces_mode_supported_p (candidate, m_op))
1493 return candidate;
1494 }
1495 }
1496
1497 return smallest_int_mode_for_size (size * BITS_PER_UNIT).require ();
1498 }
1499
1500 /* This function contains the main loop used for expanding a block
1501 operation. First move what we can in the largest integer mode,
1502 then go to successively smaller modes. For every access, call
1503 GENFUN with the two operands and the EXTRA_DATA. */
1504
1505 void
1506 op_by_pieces_d::run ()
1507 {
1508 if (m_len == 0)
1509 return;
1510
1511 unsigned HOST_WIDE_INT length = m_len;
1512
1513 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1514 fixed_size_mode mode
1515 = widest_fixed_size_mode_for_size (m_max_size, m_op);
1516 mode = get_usable_mode (mode, length);
1517
1518 by_pieces_prev to_prev = { nullptr, mode };
1519 by_pieces_prev from_prev = { nullptr, mode };
1520
1521 do
1522 {
1523 unsigned int size = GET_MODE_SIZE (mode);
1524 rtx to1 = NULL_RTX, from1;
1525
1526 while (length >= size)
1527 {
1528 if (m_reverse)
1529 m_offset -= size;
1530
1531 to1 = m_to.adjust (mode, m_offset, &to_prev);
1532 to_prev.data = to1;
1533 to_prev.mode = mode;
1534 from1 = m_from.adjust (mode, m_offset, &from_prev);
1535 from_prev.data = from1;
1536 from_prev.mode = mode;
1537
1538 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1539 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1540
1541 generate (to1, from1, mode);
1542
1543 m_to.maybe_postinc (size);
1544 m_from.maybe_postinc (size);
1545
1546 if (!m_reverse)
1547 m_offset += size;
1548
1549 length -= size;
1550 }
1551
1552 finish_mode (mode);
1553
1554 if (length == 0)
1555 return;
1556
1557 if (!m_push && m_overlap_op_by_pieces)
1558 {
1559 /* NB: Generate overlapping operations if it is not a stack
1560 push since stack push must not overlap. Get the smallest
1561 fixed size mode for M_LEN bytes. */
1562 mode = smallest_fixed_size_mode_for_size (length);
1563 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1564 int gap = GET_MODE_SIZE (mode) - length;
1565 if (gap > 0)
1566 {
1567 /* If size of MODE > M_LEN, generate the last operation
1568 in MODE for the remaining bytes with ovelapping memory
1569 from the previois operation. */
1570 if (m_reverse)
1571 m_offset += gap;
1572 else
1573 m_offset -= gap;
1574 length += gap;
1575 }
1576 }
1577 else
1578 {
1579 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1580 mode = widest_fixed_size_mode_for_size (size, m_op);
1581 mode = get_usable_mode (mode, length);
1582 }
1583 }
1584 while (1);
1585 }
1586
1587 /* Derived class from op_by_pieces_d, providing support for block move
1588 operations. */
1589
1590 #ifdef PUSH_ROUNDING
1591 #define PUSHG_P(to) ((to) == nullptr)
1592 #else
1593 #define PUSHG_P(to) false
1594 #endif
1595
1596 class move_by_pieces_d : public op_by_pieces_d
1597 {
1598 insn_gen_fn m_gen_fun;
1599 void generate (rtx, rtx, machine_mode) final override;
1600 bool prepare_mode (machine_mode, unsigned int) final override;
1601
1602 public:
1603 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1604 unsigned int align)
1605 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1606 NULL, len, align, PUSHG_P (to), MOVE_BY_PIECES)
1607 {
1608 }
1609 rtx finish_retmode (memop_ret);
1610 };
1611
1612 /* Return true if MODE can be used for a set of copies, given an
1613 alignment ALIGN. Prepare whatever data is necessary for later
1614 calls to generate. */
1615
1616 bool
1617 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1618 {
1619 insn_code icode = optab_handler (mov_optab, mode);
1620 m_gen_fun = GEN_FCN (icode);
1621 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1622 }
1623
1624 /* A callback used when iterating for a compare_by_pieces_operation.
1625 OP0 and OP1 are the values that have been loaded and should be
1626 compared in MODE. If OP0 is NULL, this means we should generate a
1627 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1628 gen function that should be used to generate the mode. */
1629
1630 void
1631 move_by_pieces_d::generate (rtx op0, rtx op1,
1632 machine_mode mode ATTRIBUTE_UNUSED)
1633 {
1634 #ifdef PUSH_ROUNDING
1635 if (op0 == NULL_RTX)
1636 {
1637 emit_single_push_insn (mode, op1, NULL);
1638 return;
1639 }
1640 #endif
1641 emit_insn (m_gen_fun (op0, op1));
1642 }
1643
1644 /* Perform the final adjustment at the end of a string to obtain the
1645 correct return value for the block operation.
1646 Return value is based on RETMODE argument. */
1647
1648 rtx
1649 move_by_pieces_d::finish_retmode (memop_ret retmode)
1650 {
1651 gcc_assert (!m_reverse);
1652 if (retmode == RETURN_END_MINUS_ONE)
1653 {
1654 m_to.maybe_postinc (-1);
1655 --m_offset;
1656 }
1657 return m_to.adjust (QImode, m_offset);
1658 }
1659
1660 /* Generate several move instructions to copy LEN bytes from block FROM to
1661 block TO. (These are MEM rtx's with BLKmode).
1662
1663 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1664 used to push FROM to the stack.
1665
1666 ALIGN is maximum stack alignment we can assume.
1667
1668 Return value is based on RETMODE argument. */
1669
1670 rtx
1671 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1672 unsigned int align, memop_ret retmode)
1673 {
1674 #ifndef PUSH_ROUNDING
1675 if (to == NULL)
1676 gcc_unreachable ();
1677 #endif
1678
1679 move_by_pieces_d data (to, from, len, align);
1680
1681 data.run ();
1682
1683 if (retmode != RETURN_BEGIN)
1684 return data.finish_retmode (retmode);
1685 else
1686 return to;
1687 }
1688
1689 /* Derived class from op_by_pieces_d, providing support for block move
1690 operations. */
1691
1692 class store_by_pieces_d : public op_by_pieces_d
1693 {
1694 insn_gen_fn m_gen_fun;
1695
1696 void generate (rtx, rtx, machine_mode) final override;
1697 bool prepare_mode (machine_mode, unsigned int) final override;
1698
1699 public:
1700 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1701 unsigned HOST_WIDE_INT len, unsigned int align,
1702 by_pieces_operation op)
1703 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1704 cfn_data, len, align, false, op)
1705 {
1706 }
1707 rtx finish_retmode (memop_ret);
1708 };
1709
1710 /* Return true if MODE can be used for a set of stores, given an
1711 alignment ALIGN. Prepare whatever data is necessary for later
1712 calls to generate. */
1713
1714 bool
1715 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1716 {
1717 insn_code icode = optab_handler (mov_optab, mode);
1718 m_gen_fun = GEN_FCN (icode);
1719 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1720 }
1721
1722 /* A callback used when iterating for a store_by_pieces_operation.
1723 OP0 and OP1 are the values that have been loaded and should be
1724 compared in MODE. If OP0 is NULL, this means we should generate a
1725 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1726 gen function that should be used to generate the mode. */
1727
1728 void
1729 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1730 {
1731 emit_insn (m_gen_fun (op0, op1));
1732 }
1733
1734 /* Perform the final adjustment at the end of a string to obtain the
1735 correct return value for the block operation.
1736 Return value is based on RETMODE argument. */
1737
1738 rtx
1739 store_by_pieces_d::finish_retmode (memop_ret retmode)
1740 {
1741 gcc_assert (!m_reverse);
1742 if (retmode == RETURN_END_MINUS_ONE)
1743 {
1744 m_to.maybe_postinc (-1);
1745 --m_offset;
1746 }
1747 return m_to.adjust (QImode, m_offset);
1748 }
1749
1750 /* Determine whether the LEN bytes generated by CONSTFUN can be
1751 stored to memory using several move instructions. CONSTFUNDATA is
1752 a pointer which will be passed as argument in every CONSTFUN call.
1753 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1754 a memset operation and false if it's a copy of a constant string.
1755 Return true if a call to store_by_pieces should succeed. */
1756
1757 bool
1758 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1759 by_pieces_constfn constfun,
1760 void *constfundata, unsigned int align, bool memsetp)
1761 {
1762 unsigned HOST_WIDE_INT l;
1763 unsigned int max_size;
1764 HOST_WIDE_INT offset = 0;
1765 enum insn_code icode;
1766 int reverse;
1767 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1768 rtx cst ATTRIBUTE_UNUSED;
1769
1770 if (len == 0)
1771 return true;
1772
1773 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1774 memsetp
1775 ? SET_BY_PIECES
1776 : STORE_BY_PIECES,
1777 optimize_insn_for_speed_p ()))
1778 return false;
1779
1780 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1781
1782 /* We would first store what we can in the largest integer mode, then go to
1783 successively smaller modes. */
1784
1785 for (reverse = 0;
1786 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1787 reverse++)
1788 {
1789 l = len;
1790 max_size = STORE_MAX_PIECES + 1;
1791 while (max_size > 1 && l > 0)
1792 {
1793 auto op = memsetp ? SET_BY_PIECES : STORE_BY_PIECES;
1794 auto mode = widest_fixed_size_mode_for_size (max_size, op);
1795
1796 icode = optab_handler (mov_optab, mode);
1797 if (icode != CODE_FOR_nothing
1798 && align >= GET_MODE_ALIGNMENT (mode))
1799 {
1800 unsigned int size = GET_MODE_SIZE (mode);
1801
1802 while (l >= size)
1803 {
1804 if (reverse)
1805 offset -= size;
1806
1807 cst = (*constfun) (constfundata, nullptr, offset, mode);
1808 /* All CONST_VECTORs can be loaded for memset since
1809 vec_duplicate_optab is a precondition to pick a
1810 vector mode for the memset expander. */
1811 if (!((memsetp && VECTOR_MODE_P (mode))
1812 || targetm.legitimate_constant_p (mode, cst)))
1813 return false;
1814
1815 if (!reverse)
1816 offset += size;
1817
1818 l -= size;
1819 }
1820 }
1821
1822 max_size = GET_MODE_SIZE (mode);
1823 }
1824
1825 /* The code above should have handled everything. */
1826 gcc_assert (!l);
1827 }
1828
1829 return true;
1830 }
1831
1832 /* Generate several move instructions to store LEN bytes generated by
1833 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1834 pointer which will be passed as argument in every CONSTFUN call.
1835 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1836 a memset operation and false if it's a copy of a constant string.
1837 Return value is based on RETMODE argument. */
1838
1839 rtx
1840 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1841 by_pieces_constfn constfun,
1842 void *constfundata, unsigned int align, bool memsetp,
1843 memop_ret retmode)
1844 {
1845 if (len == 0)
1846 {
1847 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1848 return to;
1849 }
1850
1851 gcc_assert (targetm.use_by_pieces_infrastructure_p
1852 (len, align,
1853 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1854 optimize_insn_for_speed_p ()));
1855
1856 store_by_pieces_d data (to, constfun, constfundata, len, align,
1857 memsetp ? SET_BY_PIECES : STORE_BY_PIECES);
1858 data.run ();
1859
1860 if (retmode != RETURN_BEGIN)
1861 return data.finish_retmode (retmode);
1862 else
1863 return to;
1864 }
1865
1866 void
1867 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1868 {
1869 if (len == 0)
1870 return;
1871
1872 /* Use builtin_memset_read_str to support vector mode broadcast. */
1873 char c = 0;
1874 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1875 CLEAR_BY_PIECES);
1876 data.run ();
1877 }
1878
1879 /* Context used by compare_by_pieces_genfn. It stores the fail label
1880 to jump to in case of miscomparison, and for branch ratios greater than 1,
1881 it stores an accumulator and the current and maximum counts before
1882 emitting another branch. */
1883
1884 class compare_by_pieces_d : public op_by_pieces_d
1885 {
1886 rtx_code_label *m_fail_label;
1887 rtx m_accumulator;
1888 int m_count, m_batch;
1889
1890 void generate (rtx, rtx, machine_mode) final override;
1891 bool prepare_mode (machine_mode, unsigned int) final override;
1892 void finish_mode (machine_mode) final override;
1893
1894 public:
1895 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1896 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1897 rtx_code_label *fail_label)
1898 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1899 op1_cfn_data, len, align, false, COMPARE_BY_PIECES)
1900 {
1901 m_fail_label = fail_label;
1902 }
1903 };
1904
1905 /* A callback used when iterating for a compare_by_pieces_operation.
1906 OP0 and OP1 are the values that have been loaded and should be
1907 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1908 context structure. */
1909
1910 void
1911 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1912 {
1913 if (m_batch > 1)
1914 {
1915 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1916 true, OPTAB_LIB_WIDEN);
1917 if (m_count != 0)
1918 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1919 true, OPTAB_LIB_WIDEN);
1920 m_accumulator = temp;
1921
1922 if (++m_count < m_batch)
1923 return;
1924
1925 m_count = 0;
1926 op0 = m_accumulator;
1927 op1 = const0_rtx;
1928 m_accumulator = NULL_RTX;
1929 }
1930 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1931 m_fail_label, profile_probability::uninitialized ());
1932 }
1933
1934 /* Return true if MODE can be used for a set of moves and comparisons,
1935 given an alignment ALIGN. Prepare whatever data is necessary for
1936 later calls to generate. */
1937
1938 bool
1939 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1940 {
1941 insn_code icode = optab_handler (mov_optab, mode);
1942 if (icode == CODE_FOR_nothing
1943 || align < GET_MODE_ALIGNMENT (mode)
1944 || !can_compare_p (EQ, mode, ccp_jump))
1945 return false;
1946 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1947 if (m_batch < 0)
1948 return false;
1949 m_accumulator = NULL_RTX;
1950 m_count = 0;
1951 return true;
1952 }
1953
1954 /* Called after expanding a series of comparisons in MODE. If we have
1955 accumulated results for which we haven't emitted a branch yet, do
1956 so now. */
1957
1958 void
1959 compare_by_pieces_d::finish_mode (machine_mode mode)
1960 {
1961 if (m_accumulator != NULL_RTX)
1962 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1963 NULL_RTX, NULL, m_fail_label,
1964 profile_probability::uninitialized ());
1965 }
1966
1967 /* Generate several move instructions to compare LEN bytes from blocks
1968 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1969
1970 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1971 used to push FROM to the stack.
1972
1973 ALIGN is maximum stack alignment we can assume.
1974
1975 Optionally, the caller can pass a constfn and associated data in A1_CFN
1976 and A1_CFN_DATA. describing that the second operand being compared is a
1977 known constant and how to obtain its data. */
1978
1979 static rtx
1980 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1981 rtx target, unsigned int align,
1982 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1983 {
1984 rtx_code_label *fail_label = gen_label_rtx ();
1985 rtx_code_label *end_label = gen_label_rtx ();
1986
1987 if (target == NULL_RTX
1988 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1989 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1990
1991 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1992 fail_label);
1993
1994 data.run ();
1995
1996 emit_move_insn (target, const0_rtx);
1997 emit_jump (end_label);
1998 emit_barrier ();
1999 emit_label (fail_label);
2000 emit_move_insn (target, const1_rtx);
2001 emit_label (end_label);
2002
2003 return target;
2004 }
2005
2006 /* Emit code to move a block Y to a block X. This may be done with
2007 string-move instructions, with multiple scalar move instructions,
2008 or with a library call.
2009
2010 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
2011 SIZE is an rtx that says how long they are.
2012 ALIGN is the maximum alignment we can assume they have.
2013 METHOD describes what kind of copy this is, and what mechanisms may be used.
2014 MIN_SIZE is the minimal size of block to move
2015 MAX_SIZE is the maximal size of block to move, if it cannot be represented
2016 in unsigned HOST_WIDE_INT, than it is mask of all ones.
2017 CTZ_SIZE is the trailing-zeros count of SIZE; even a nonconstant SIZE is
2018 known to be a multiple of 1<<CTZ_SIZE.
2019
2020 Return the address of the new block, if memcpy is called and returns it,
2021 0 otherwise. */
2022
2023 rtx
2024 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
2025 unsigned int expected_align, HOST_WIDE_INT expected_size,
2026 unsigned HOST_WIDE_INT min_size,
2027 unsigned HOST_WIDE_INT max_size,
2028 unsigned HOST_WIDE_INT probable_max_size,
2029 bool bail_out_libcall, bool *is_move_done,
2030 bool might_overlap, unsigned ctz_size)
2031 {
2032 int may_use_call;
2033 rtx retval = 0;
2034 unsigned int align;
2035
2036 if (is_move_done)
2037 *is_move_done = true;
2038
2039 gcc_assert (size);
2040 if (CONST_INT_P (size) && INTVAL (size) == 0)
2041 return 0;
2042
2043 switch (method)
2044 {
2045 case BLOCK_OP_NORMAL:
2046 case BLOCK_OP_TAILCALL:
2047 may_use_call = 1;
2048 break;
2049
2050 case BLOCK_OP_CALL_PARM:
2051 may_use_call = block_move_libcall_safe_for_call_parm ();
2052
2053 /* Make inhibit_defer_pop nonzero around the library call
2054 to force it to pop the arguments right away. */
2055 NO_DEFER_POP;
2056 break;
2057
2058 case BLOCK_OP_NO_LIBCALL:
2059 may_use_call = 0;
2060 break;
2061
2062 case BLOCK_OP_NO_LIBCALL_RET:
2063 may_use_call = -1;
2064 break;
2065
2066 default:
2067 gcc_unreachable ();
2068 }
2069
2070 gcc_assert (MEM_P (x) && MEM_P (y));
2071 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2072 gcc_assert (align >= BITS_PER_UNIT);
2073
2074 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2075 block copy is more efficient for other large modes, e.g. DCmode. */
2076 x = adjust_address (x, BLKmode, 0);
2077 y = adjust_address (y, BLKmode, 0);
2078
2079 /* If source and destination are the same, no need to copy anything. */
2080 if (rtx_equal_p (x, y)
2081 && !MEM_VOLATILE_P (x)
2082 && !MEM_VOLATILE_P (y))
2083 return 0;
2084
2085 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2086 can be incorrect is coming from __builtin_memcpy. */
2087 poly_int64 const_size;
2088 if (poly_int_rtx_p (size, &const_size))
2089 {
2090 x = shallow_copy_rtx (x);
2091 y = shallow_copy_rtx (y);
2092 set_mem_size (x, const_size);
2093 set_mem_size (y, const_size);
2094 }
2095
2096 bool pieces_ok = CONST_INT_P (size)
2097 && can_move_by_pieces (INTVAL (size), align);
2098 bool pattern_ok = false;
2099
2100 if (!pieces_ok || might_overlap)
2101 {
2102 pattern_ok
2103 = emit_block_move_via_pattern (x, y, size, align,
2104 expected_align, expected_size,
2105 min_size, max_size, probable_max_size,
2106 might_overlap);
2107 if (!pattern_ok && might_overlap)
2108 {
2109 /* Do not try any of the other methods below as they are not safe
2110 for overlapping moves. */
2111 *is_move_done = false;
2112 return retval;
2113 }
2114 }
2115
2116 bool dynamic_direction = false;
2117 if (!pattern_ok && !pieces_ok && may_use_call
2118 && (flag_inline_stringops & (might_overlap ? ILSOP_MEMMOVE : ILSOP_MEMCPY)))
2119 {
2120 may_use_call = 0;
2121 dynamic_direction = might_overlap;
2122 }
2123
2124 if (pattern_ok)
2125 ;
2126 else if (pieces_ok)
2127 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2128 else if (may_use_call && !might_overlap
2129 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2130 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2131 {
2132 if (bail_out_libcall)
2133 {
2134 if (is_move_done)
2135 *is_move_done = false;
2136 return retval;
2137 }
2138
2139 if (may_use_call < 0)
2140 return pc_rtx;
2141
2142 retval = emit_block_copy_via_libcall (x, y, size,
2143 method == BLOCK_OP_TAILCALL);
2144 }
2145 else if (dynamic_direction)
2146 emit_block_move_via_oriented_loop (x, y, size, align, ctz_size);
2147 else if (might_overlap)
2148 *is_move_done = false;
2149 else
2150 emit_block_move_via_sized_loop (x, y, size, align, ctz_size);
2151
2152 if (method == BLOCK_OP_CALL_PARM)
2153 OK_DEFER_POP;
2154
2155 return retval;
2156 }
2157
2158 rtx
2159 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method,
2160 unsigned int ctz_size)
2161 {
2162 unsigned HOST_WIDE_INT max, min = 0;
2163 if (GET_CODE (size) == CONST_INT)
2164 min = max = UINTVAL (size);
2165 else
2166 max = GET_MODE_MASK (GET_MODE (size));
2167 return emit_block_move_hints (x, y, size, method, 0, -1,
2168 min, max, max,
2169 false, NULL, false, ctz_size);
2170 }
2171
2172 /* A subroutine of emit_block_move. Returns true if calling the
2173 block move libcall will not clobber any parameters which may have
2174 already been placed on the stack. */
2175
2176 static bool
2177 block_move_libcall_safe_for_call_parm (void)
2178 {
2179 tree fn;
2180
2181 /* If arguments are pushed on the stack, then they're safe. */
2182 if (targetm.calls.push_argument (0))
2183 return true;
2184
2185 /* If registers go on the stack anyway, any argument is sure to clobber
2186 an outgoing argument. */
2187 #if defined (REG_PARM_STACK_SPACE)
2188 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2189 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2190 depend on its argument. */
2191 (void) fn;
2192 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2193 && REG_PARM_STACK_SPACE (fn) != 0)
2194 return false;
2195 #endif
2196
2197 /* If any argument goes in memory, then it might clobber an outgoing
2198 argument. */
2199 {
2200 CUMULATIVE_ARGS args_so_far_v;
2201 cumulative_args_t args_so_far;
2202 tree arg;
2203
2204 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2205 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2206 args_so_far = pack_cumulative_args (&args_so_far_v);
2207
2208 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2209 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2210 {
2211 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2212 function_arg_info arg_info (mode, /*named=*/true);
2213 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2214 if (!tmp || !REG_P (tmp))
2215 return false;
2216 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2217 return false;
2218 targetm.calls.function_arg_advance (args_so_far, arg_info);
2219 }
2220 }
2221 return true;
2222 }
2223
2224 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2225 return true if successful.
2226
2227 X is the destination of the copy or move.
2228 Y is the source of the copy or move.
2229 SIZE is the size of the block to be moved.
2230
2231 MIGHT_OVERLAP indicates this originated with expansion of a
2232 builtin_memmove() and the source and destination blocks may
2233 overlap.
2234 */
2235
2236 static bool
2237 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2238 unsigned int expected_align,
2239 HOST_WIDE_INT expected_size,
2240 unsigned HOST_WIDE_INT min_size,
2241 unsigned HOST_WIDE_INT max_size,
2242 unsigned HOST_WIDE_INT probable_max_size,
2243 bool might_overlap)
2244 {
2245 if (expected_align < align)
2246 expected_align = align;
2247 if (expected_size != -1)
2248 {
2249 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2250 expected_size = probable_max_size;
2251 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2252 expected_size = min_size;
2253 }
2254
2255 /* Since this is a move insn, we don't care about volatility. */
2256 temporary_volatile_ok v (true);
2257
2258 /* Try the most limited insn first, because there's no point
2259 including more than one in the machine description unless
2260 the more limited one has some advantage. */
2261
2262 opt_scalar_int_mode mode_iter;
2263 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2264 {
2265 scalar_int_mode mode = mode_iter.require ();
2266 enum insn_code code;
2267 if (might_overlap)
2268 code = direct_optab_handler (movmem_optab, mode);
2269 else
2270 code = direct_optab_handler (cpymem_optab, mode);
2271
2272 if (code != CODE_FOR_nothing
2273 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2274 here because if SIZE is less than the mode mask, as it is
2275 returned by the macro, it will definitely be less than the
2276 actual mode mask. Since SIZE is within the Pmode address
2277 space, we limit MODE to Pmode. */
2278 && ((CONST_INT_P (size)
2279 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2280 <= (GET_MODE_MASK (mode) >> 1)))
2281 || max_size <= (GET_MODE_MASK (mode) >> 1)
2282 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2283 {
2284 class expand_operand ops[9];
2285 unsigned int nops;
2286
2287 /* ??? When called via emit_block_move_for_call, it'd be
2288 nice if there were some way to inform the backend, so
2289 that it doesn't fail the expansion because it thinks
2290 emitting the libcall would be more efficient. */
2291 nops = insn_data[(int) code].n_generator_args;
2292 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2293
2294 create_fixed_operand (&ops[0], x);
2295 create_fixed_operand (&ops[1], y);
2296 /* The check above guarantees that this size conversion is valid. */
2297 create_convert_operand_to (&ops[2], size, mode, true);
2298 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2299 if (nops >= 6)
2300 {
2301 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2302 create_integer_operand (&ops[5], expected_size);
2303 }
2304 if (nops >= 8)
2305 {
2306 create_integer_operand (&ops[6], min_size);
2307 /* If we cannot represent the maximal size,
2308 make parameter NULL. */
2309 if ((HOST_WIDE_INT) max_size != -1)
2310 create_integer_operand (&ops[7], max_size);
2311 else
2312 create_fixed_operand (&ops[7], NULL);
2313 }
2314 if (nops == 9)
2315 {
2316 /* If we cannot represent the maximal size,
2317 make parameter NULL. */
2318 if ((HOST_WIDE_INT) probable_max_size != -1)
2319 create_integer_operand (&ops[8], probable_max_size);
2320 else
2321 create_fixed_operand (&ops[8], NULL);
2322 }
2323 if (maybe_expand_insn (code, nops, ops))
2324 return true;
2325 }
2326 }
2327
2328 return false;
2329 }
2330
2331 /* Like emit_block_move_via_loop, but choose a suitable INCR based on
2332 ALIGN and CTZ_SIZE. */
2333
2334 static void
2335 emit_block_move_via_sized_loop (rtx x, rtx y, rtx size,
2336 unsigned int align,
2337 unsigned int ctz_size)
2338 {
2339 int incr = align / BITS_PER_UNIT;
2340
2341 if (CONST_INT_P (size))
2342 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size)));
2343
2344 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr)
2345 incr = HOST_WIDE_INT_1U << ctz_size;
2346
2347 while (incr > 1 && !can_move_by_pieces (incr, align))
2348 incr >>= 1;
2349
2350 gcc_checking_assert (incr);
2351
2352 return emit_block_move_via_loop (x, y, size, align, incr);
2353 }
2354
2355 /* Like emit_block_move_via_sized_loop, but besides choosing INCR so
2356 as to ensure safe moves even in case of overlap, output dynamic
2357 tests to choose between two loops, one moving downwards, another
2358 moving upwards. */
2359
2360 static void
2361 emit_block_move_via_oriented_loop (rtx x, rtx y, rtx size,
2362 unsigned int align,
2363 unsigned int ctz_size)
2364 {
2365 int incr = align / BITS_PER_UNIT;
2366
2367 if (CONST_INT_P (size))
2368 ctz_size = MAX (ctz_size, (unsigned) wi::ctz (UINTVAL (size)));
2369
2370 if (HOST_WIDE_INT_1U << ctz_size < (unsigned HOST_WIDE_INT) incr)
2371 incr = HOST_WIDE_INT_1U << ctz_size;
2372
2373 while (incr > 1 && !int_mode_for_size (incr, 0).exists ())
2374 incr >>= 1;
2375
2376 gcc_checking_assert (incr);
2377
2378 rtx_code_label *upw_label, *end_label;
2379 upw_label = gen_label_rtx ();
2380 end_label = gen_label_rtx ();
2381
2382 rtx x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2383 rtx y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2384 do_pending_stack_adjust ();
2385
2386 machine_mode mode = GET_MODE (x_addr);
2387 if (mode != GET_MODE (y_addr))
2388 {
2389 scalar_int_mode xmode
2390 = smallest_int_mode_for_size (GET_MODE_BITSIZE (mode)).require ();
2391 scalar_int_mode ymode
2392 = smallest_int_mode_for_size (GET_MODE_BITSIZE
2393 (GET_MODE (y_addr))).require ();
2394 if (GET_MODE_BITSIZE (xmode) < GET_MODE_BITSIZE (ymode))
2395 mode = ymode;
2396 else
2397 mode = xmode;
2398
2399 #ifndef POINTERS_EXTEND_UNSIGNED
2400 const int POINTERS_EXTEND_UNSIGNED = 1;
2401 #endif
2402 x_addr = convert_modes (mode, GET_MODE (x_addr), x_addr,
2403 POINTERS_EXTEND_UNSIGNED);
2404 y_addr = convert_modes (mode, GET_MODE (y_addr), y_addr,
2405 POINTERS_EXTEND_UNSIGNED);
2406 }
2407
2408 /* Test for overlap: if (x >= y || x + size <= y) goto upw_label. */
2409 emit_cmp_and_jump_insns (x_addr, y_addr, GEU, NULL_RTX, mode,
2410 true, upw_label,
2411 profile_probability::guessed_always ()
2412 .apply_scale (5, 10));
2413 rtx tmp = convert_modes (GET_MODE (x_addr), GET_MODE (size), size, true);
2414 tmp = simplify_gen_binary (PLUS, GET_MODE (x_addr), x_addr, tmp);
2415
2416 emit_cmp_and_jump_insns (tmp, y_addr, LEU, NULL_RTX, mode,
2417 true, upw_label,
2418 profile_probability::guessed_always ()
2419 .apply_scale (8, 10));
2420
2421 emit_block_move_via_loop (x, y, size, align, -incr);
2422
2423 emit_jump (end_label);
2424 emit_label (upw_label);
2425
2426 emit_block_move_via_loop (x, y, size, align, incr);
2427
2428 emit_label (end_label);
2429 }
2430
2431 /* A subroutine of emit_block_move. Copy the data via an explicit
2432 loop. This is used only when libcalls are forbidden, or when
2433 inlining is required. INCR is the block size to be copied in each
2434 loop iteration. If it is negative, the absolute value is used, and
2435 the block is copied backwards. INCR must be a power of two, an
2436 exact divisor for SIZE and ALIGN, and imply a mode that can be
2437 safely copied per iteration assuming no overlap. */
2438
2439 static void
2440 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2441 unsigned int align, int incr)
2442 {
2443 rtx_code_label *cmp_label, *top_label;
2444 rtx iter, x_addr, y_addr, tmp;
2445 machine_mode x_addr_mode = get_address_mode (x);
2446 machine_mode y_addr_mode = get_address_mode (y);
2447 machine_mode iter_mode;
2448
2449 iter_mode = GET_MODE (size);
2450 if (iter_mode == VOIDmode)
2451 iter_mode = word_mode;
2452
2453 top_label = gen_label_rtx ();
2454 cmp_label = gen_label_rtx ();
2455 iter = gen_reg_rtx (iter_mode);
2456
2457 bool downwards = incr < 0;
2458 rtx iter_init;
2459 rtx_code iter_cond;
2460 rtx iter_limit;
2461 rtx iter_incr;
2462 machine_mode move_mode;
2463 if (downwards)
2464 {
2465 incr = -incr;
2466 iter_init = size;
2467 iter_cond = GEU;
2468 iter_limit = const0_rtx;
2469 iter_incr = GEN_INT (incr);
2470 }
2471 else
2472 {
2473 iter_init = const0_rtx;
2474 iter_cond = LTU;
2475 iter_limit = size;
2476 iter_incr = GEN_INT (incr);
2477 }
2478 emit_move_insn (iter, iter_init);
2479
2480 opt_scalar_int_mode int_move_mode
2481 = int_mode_for_size (incr * BITS_PER_UNIT, 1);
2482 if (!int_move_mode.exists (&move_mode)
2483 || GET_MODE_BITSIZE (int_move_mode.require ()) != incr * BITS_PER_UNIT)
2484 {
2485 move_mode = BLKmode;
2486 gcc_checking_assert (can_move_by_pieces (incr, align));
2487 }
2488
2489 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2490 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2491 do_pending_stack_adjust ();
2492
2493 emit_jump (cmp_label);
2494 emit_label (top_label);
2495
2496 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2497 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2498
2499 if (x_addr_mode != y_addr_mode)
2500 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2501 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2502
2503 x = change_address (x, move_mode, x_addr);
2504 y = change_address (y, move_mode, y_addr);
2505
2506 if (move_mode == BLKmode)
2507 {
2508 bool done;
2509 emit_block_move_hints (x, y, iter_incr, BLOCK_OP_NO_LIBCALL,
2510 align, incr, incr, incr, incr,
2511 false, &done, false);
2512 gcc_checking_assert (done);
2513 }
2514 else
2515 emit_move_insn (x, y);
2516
2517 if (downwards)
2518 emit_label (cmp_label);
2519
2520 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2521 true, OPTAB_LIB_WIDEN);
2522 if (tmp != iter)
2523 emit_move_insn (iter, tmp);
2524
2525 if (!downwards)
2526 emit_label (cmp_label);
2527
2528 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2529 true, top_label,
2530 profile_probability::guessed_always ()
2531 .apply_scale (9, 10));
2532 }
2533
2534 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2535 TAILCALL is true if this is a tail call. */
2536
2537 rtx
2538 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2539 rtx size, bool tailcall)
2540 {
2541 rtx dst_addr, src_addr;
2542 tree call_expr, dst_tree, src_tree, size_tree;
2543 machine_mode size_mode;
2544
2545 /* Since dst and src are passed to a libcall, mark the corresponding
2546 tree EXPR as addressable. */
2547 tree dst_expr = MEM_EXPR (dst);
2548 tree src_expr = MEM_EXPR (src);
2549 if (dst_expr)
2550 mark_addressable (dst_expr);
2551 if (src_expr)
2552 mark_addressable (src_expr);
2553
2554 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2555 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2556 dst_tree = make_tree (ptr_type_node, dst_addr);
2557
2558 src_addr = copy_addr_to_reg (XEXP (src, 0));
2559 src_addr = convert_memory_address (ptr_mode, src_addr);
2560 src_tree = make_tree (ptr_type_node, src_addr);
2561
2562 size_mode = TYPE_MODE (sizetype);
2563 size = convert_to_mode (size_mode, size, 1);
2564 size = copy_to_mode_reg (size_mode, size);
2565 size_tree = make_tree (sizetype, size);
2566
2567 /* It is incorrect to use the libcall calling conventions for calls to
2568 memcpy/memmove/memcmp because they can be provided by the user. */
2569 tree fn = builtin_decl_implicit (fncode);
2570 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2571 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2572
2573 return expand_call (call_expr, NULL_RTX, false);
2574 }
2575
2576 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2577 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2578 otherwise return null. */
2579
2580 rtx
2581 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2582 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2583 HOST_WIDE_INT align)
2584 {
2585 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2586
2587 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2588 target = NULL_RTX;
2589
2590 class expand_operand ops[5];
2591 create_output_operand (&ops[0], target, insn_mode);
2592 create_fixed_operand (&ops[1], arg1_rtx);
2593 create_fixed_operand (&ops[2], arg2_rtx);
2594 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2595 TYPE_UNSIGNED (arg3_type));
2596 create_integer_operand (&ops[4], align);
2597 if (maybe_expand_insn (icode, 5, ops))
2598 return ops[0].value;
2599 return NULL_RTX;
2600 }
2601
2602 /* Expand a block compare between X and Y with length LEN using the
2603 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2604 of the expression that was used to calculate the length. ALIGN
2605 gives the known minimum common alignment. */
2606
2607 static rtx
2608 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2609 unsigned align)
2610 {
2611 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2612 implementing memcmp because it will stop if it encounters two
2613 zero bytes. */
2614 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2615
2616 if (icode == CODE_FOR_nothing)
2617 return NULL_RTX;
2618
2619 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2620 }
2621
2622 /* Emit code to compare a block Y to a block X. This may be done with
2623 string-compare instructions, with multiple scalar instructions,
2624 or with a library call.
2625
2626 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2627 they are. LEN_TYPE is the type of the expression that was used to
2628 calculate it, and CTZ_LEN is the known trailing-zeros count of LEN,
2629 so LEN must be a multiple of 1<<CTZ_LEN even if it's not constant.
2630
2631 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2632 value of a normal memcmp call, instead we can just compare for equality.
2633 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2634 returning NULL_RTX.
2635
2636 Optionally, the caller can pass a constfn and associated data in Y_CFN
2637 and Y_CFN_DATA. describing that the second operand being compared is a
2638 known constant and how to obtain its data.
2639 Return the result of the comparison, or NULL_RTX if we failed to
2640 perform the operation. */
2641
2642 rtx
2643 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2644 bool equality_only, by_pieces_constfn y_cfn,
2645 void *y_cfndata, unsigned ctz_len)
2646 {
2647 rtx result = 0;
2648
2649 if (CONST_INT_P (len) && INTVAL (len) == 0)
2650 return const0_rtx;
2651
2652 gcc_assert (MEM_P (x) && MEM_P (y));
2653 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2654 gcc_assert (align >= BITS_PER_UNIT);
2655
2656 x = adjust_address (x, BLKmode, 0);
2657 y = adjust_address (y, BLKmode, 0);
2658
2659 if (equality_only
2660 && CONST_INT_P (len)
2661 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2662 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2663 y_cfn, y_cfndata);
2664 else
2665 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2666
2667 if (!result && (flag_inline_stringops & ILSOP_MEMCMP))
2668 result = emit_block_cmp_via_loop (x, y, len, len_type,
2669 target, equality_only,
2670 align, ctz_len);
2671
2672 return result;
2673 }
2674
2675 /* Like emit_block_cmp_hints, but with known alignment and no support
2676 for constats. Always expand to a loop with iterations that compare
2677 blocks of the largest compare-by-pieces size that divides both len
2678 and align, and then, if !EQUALITY_ONLY, identify the word and then
2679 the unit that first differs to return the result. */
2680
2681 rtx
2682 emit_block_cmp_via_loop (rtx x, rtx y, rtx len, tree len_type, rtx target,
2683 bool equality_only, unsigned align, unsigned ctz_len)
2684 {
2685 unsigned incr = align / BITS_PER_UNIT;
2686
2687 if (CONST_INT_P (len))
2688 ctz_len = MAX (ctz_len, (unsigned) wi::ctz (UINTVAL (len)));
2689
2690 if (HOST_WIDE_INT_1U << ctz_len < (unsigned HOST_WIDE_INT) incr)
2691 incr = HOST_WIDE_INT_1U << ctz_len;
2692
2693 while (incr > 1
2694 && !can_do_by_pieces (incr, align, COMPARE_BY_PIECES))
2695 incr >>= 1;
2696
2697 rtx_code_label *cmp_label, *top_label, *ne_label, *res_label;
2698 rtx iter, x_addr, y_addr, tmp;
2699 machine_mode x_addr_mode = get_address_mode (x);
2700 machine_mode y_addr_mode = get_address_mode (y);
2701 machine_mode iter_mode;
2702
2703 iter_mode = GET_MODE (len);
2704 if (iter_mode == VOIDmode)
2705 iter_mode = word_mode;
2706
2707 rtx iter_init = const0_rtx;
2708 rtx_code iter_cond = LTU;
2709 rtx_code entry_cond = GEU;
2710 rtx iter_limit = len;
2711 rtx iter_incr = GEN_INT (incr);
2712 machine_mode cmp_mode;
2713
2714 /* We can drop the loop back edge if we know there's exactly one
2715 iteration. */
2716 top_label = (!rtx_equal_p (len, iter_incr)
2717 ? gen_label_rtx ()
2718 : NULL);
2719 /* We need not test before entering the loop if len is known
2720 nonzero. ??? This could be even stricter, testing whether a
2721 nonconstant LEN could possibly be zero. */
2722 cmp_label = (!CONSTANT_P (len) || rtx_equal_p (len, iter_init)
2723 ? gen_label_rtx ()
2724 : NULL);
2725 ne_label = gen_label_rtx ();
2726 res_label = gen_label_rtx ();
2727
2728 iter = gen_reg_rtx (iter_mode);
2729 emit_move_insn (iter, iter_init);
2730
2731 opt_scalar_int_mode int_cmp_mode
2732 = int_mode_for_size (incr * BITS_PER_UNIT, 1);
2733 if (!int_cmp_mode.exists (&cmp_mode)
2734 || GET_MODE_BITSIZE (int_cmp_mode.require ()) != incr * BITS_PER_UNIT
2735 || !can_compare_p (NE, cmp_mode, ccp_jump))
2736 {
2737 cmp_mode = BLKmode;
2738 gcc_checking_assert (incr != 1);
2739 }
2740
2741 /* Save the base addresses. */
2742 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2743 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2744 do_pending_stack_adjust ();
2745
2746 if (cmp_label)
2747 {
2748 if (top_label)
2749 emit_jump (cmp_label);
2750 else
2751 emit_cmp_and_jump_insns (iter, iter_limit, entry_cond,
2752 NULL_RTX, iter_mode,
2753 true, cmp_label,
2754 profile_probability::guessed_always ()
2755 .apply_scale (1, 10));
2756 }
2757 if (top_label)
2758 emit_label (top_label);
2759
2760 /* Offset the base addresses by ITER. */
2761 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2762 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2763
2764 if (x_addr_mode != y_addr_mode)
2765 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2766 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2767
2768 x = change_address (x, cmp_mode, x_addr);
2769 y = change_address (y, cmp_mode, y_addr);
2770
2771 /* Compare one block. */
2772 rtx part_res;
2773 if (cmp_mode == BLKmode)
2774 part_res = compare_by_pieces (x, y, incr, target, align, 0, 0);
2775 else
2776 part_res = expand_binop (cmp_mode, sub_optab, x, y, NULL_RTX,
2777 true, OPTAB_LIB_WIDEN);
2778
2779 /* Stop if we found a difference. */
2780 emit_cmp_and_jump_insns (part_res, GEN_INT (0), NE, NULL_RTX,
2781 GET_MODE (part_res), true, ne_label,
2782 profile_probability::guessed_always ()
2783 .apply_scale (1, 10));
2784
2785 /* Increment ITER. */
2786 tmp = expand_simple_binop (iter_mode, PLUS, iter, iter_incr, iter,
2787 true, OPTAB_LIB_WIDEN);
2788 if (tmp != iter)
2789 emit_move_insn (iter, tmp);
2790
2791 if (cmp_label)
2792 emit_label (cmp_label);
2793 /* Loop until we reach the limit. */
2794
2795 if (top_label)
2796 emit_cmp_and_jump_insns (iter, iter_limit, iter_cond, NULL_RTX, iter_mode,
2797 true, top_label,
2798 profile_probability::guessed_always ()
2799 .apply_scale (9, 10));
2800
2801 /* We got to the end without differences, so the result is zero. */
2802 if (target == NULL_RTX
2803 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
2804 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
2805
2806 emit_move_insn (target, const0_rtx);
2807 emit_jump (res_label);
2808
2809 emit_label (ne_label);
2810
2811 /* Return nonzero, or pinpoint the difference to return the expected
2812 result for non-equality tests. */
2813 if (equality_only)
2814 emit_move_insn (target, const1_rtx);
2815 else
2816 {
2817 if (incr > UNITS_PER_WORD)
2818 /* ??? Re-compare the block found to be different one word at a
2819 time. */
2820 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2821 target, equality_only,
2822 BITS_PER_WORD, 0);
2823 else if (incr > 1)
2824 /* ??? Re-compare the block found to be different one byte at a
2825 time. We could do better using part_res, and being careful
2826 about endianness. */
2827 part_res = emit_block_cmp_via_loop (x, y, GEN_INT (incr), len_type,
2828 target, equality_only,
2829 BITS_PER_UNIT, 0);
2830 else if (known_gt (GET_MODE_BITSIZE (GET_MODE (target)),
2831 GET_MODE_BITSIZE (cmp_mode)))
2832 part_res = expand_binop (GET_MODE (target), sub_optab, x, y, target,
2833 true, OPTAB_LIB_WIDEN);
2834 else
2835 {
2836 /* In the odd chance target is QImode, we can't count on
2837 widening subtract to capture the result of the unsigned
2838 compares. */
2839 rtx_code_label *ltu_label;
2840 ltu_label = gen_label_rtx ();
2841 emit_cmp_and_jump_insns (x, y, LTU, NULL_RTX,
2842 cmp_mode, true, ltu_label,
2843 profile_probability::guessed_always ()
2844 .apply_scale (5, 10));
2845
2846 emit_move_insn (target, const1_rtx);
2847 emit_jump (res_label);
2848
2849 emit_label (ltu_label);
2850 emit_move_insn (target, constm1_rtx);
2851 part_res = target;
2852 }
2853
2854 if (target != part_res)
2855 convert_move (target, part_res, false);
2856 }
2857
2858 emit_label (res_label);
2859
2860 return target;
2861 }
2862
2863
2864 /* Copy all or part of a value X into registers starting at REGNO.
2865 The number of registers to be filled is NREGS. */
2866
2867 void
2868 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2869 {
2870 if (nregs == 0)
2871 return;
2872
2873 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2874 x = validize_mem (force_const_mem (mode, x));
2875
2876 /* See if the machine can do this with a load multiple insn. */
2877 if (targetm.have_load_multiple ())
2878 {
2879 rtx_insn *last = get_last_insn ();
2880 rtx first = gen_rtx_REG (word_mode, regno);
2881 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2882 GEN_INT (nregs)))
2883 {
2884 emit_insn (pat);
2885 return;
2886 }
2887 else
2888 delete_insns_since (last);
2889 }
2890
2891 for (int i = 0; i < nregs; i++)
2892 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2893 operand_subword_force (x, i, mode));
2894 }
2895
2896 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2897 The number of registers to be filled is NREGS. */
2898
2899 void
2900 move_block_from_reg (int regno, rtx x, int nregs)
2901 {
2902 if (nregs == 0)
2903 return;
2904
2905 /* See if the machine can do this with a store multiple insn. */
2906 if (targetm.have_store_multiple ())
2907 {
2908 rtx_insn *last = get_last_insn ();
2909 rtx first = gen_rtx_REG (word_mode, regno);
2910 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2911 GEN_INT (nregs)))
2912 {
2913 emit_insn (pat);
2914 return;
2915 }
2916 else
2917 delete_insns_since (last);
2918 }
2919
2920 for (int i = 0; i < nregs; i++)
2921 {
2922 rtx tem = operand_subword (x, i, 1, BLKmode);
2923
2924 gcc_assert (tem);
2925
2926 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2927 }
2928 }
2929
2930 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2931 ORIG, where ORIG is a non-consecutive group of registers represented by
2932 a PARALLEL. The clone is identical to the original except in that the
2933 original set of registers is replaced by a new set of pseudo registers.
2934 The new set has the same modes as the original set. */
2935
2936 rtx
2937 gen_group_rtx (rtx orig)
2938 {
2939 int i, length;
2940 rtx *tmps;
2941
2942 gcc_assert (GET_CODE (orig) == PARALLEL);
2943
2944 length = XVECLEN (orig, 0);
2945 tmps = XALLOCAVEC (rtx, length);
2946
2947 /* Skip a NULL entry in first slot. */
2948 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2949
2950 if (i)
2951 tmps[0] = 0;
2952
2953 for (; i < length; i++)
2954 {
2955 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2956 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2957
2958 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2959 }
2960
2961 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2962 }
2963
2964 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2965 except that values are placed in TMPS[i], and must later be moved
2966 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2967
2968 static void
2969 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2970 poly_int64 ssize)
2971 {
2972 rtx src;
2973 int start, i;
2974 machine_mode m = GET_MODE (orig_src);
2975
2976 gcc_assert (GET_CODE (dst) == PARALLEL);
2977
2978 if (m != VOIDmode
2979 && !SCALAR_INT_MODE_P (m)
2980 && !MEM_P (orig_src)
2981 && GET_CODE (orig_src) != CONCAT)
2982 {
2983 scalar_int_mode imode;
2984 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2985 {
2986 src = gen_reg_rtx (imode);
2987 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2988 }
2989 else
2990 {
2991 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2992 emit_move_insn (src, orig_src);
2993 }
2994 emit_group_load_1 (tmps, dst, src, type, ssize);
2995 return;
2996 }
2997
2998 /* Check for a NULL entry, used to indicate that the parameter goes
2999 both on the stack and in registers. */
3000 if (XEXP (XVECEXP (dst, 0, 0), 0))
3001 start = 0;
3002 else
3003 start = 1;
3004
3005 /* Process the pieces. */
3006 for (i = start; i < XVECLEN (dst, 0); i++)
3007 {
3008 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
3009 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
3010 poly_int64 bytelen = GET_MODE_SIZE (mode);
3011 poly_int64 shift = 0;
3012
3013 /* Handle trailing fragments that run over the size of the struct.
3014 It's the target's responsibility to make sure that the fragment
3015 cannot be strictly smaller in some cases and strictly larger
3016 in others. */
3017 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
3018 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3019 {
3020 /* Arrange to shift the fragment to where it belongs.
3021 extract_bit_field loads to the lsb of the reg. */
3022 if (
3023 #ifdef BLOCK_REG_PADDING
3024 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
3025 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3026 #else
3027 BYTES_BIG_ENDIAN
3028 #endif
3029 )
3030 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3031 bytelen = ssize - bytepos;
3032 gcc_assert (maybe_gt (bytelen, 0));
3033 }
3034
3035 /* If we won't be loading directly from memory, protect the real source
3036 from strange tricks we might play; but make sure that the source can
3037 be loaded directly into the destination. */
3038 src = orig_src;
3039 if (!MEM_P (orig_src)
3040 && (!REG_P (orig_src) || HARD_REGISTER_P (orig_src))
3041 && !CONSTANT_P (orig_src))
3042 {
3043 gcc_assert (GET_MODE (orig_src) != VOIDmode);
3044 src = force_reg (GET_MODE (orig_src), orig_src);
3045 }
3046
3047 /* Optimize the access just a bit. */
3048 if (MEM_P (src)
3049 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
3050 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
3051 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
3052 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3053 {
3054 tmps[i] = gen_reg_rtx (mode);
3055 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
3056 }
3057 else if (COMPLEX_MODE_P (mode)
3058 && GET_MODE (src) == mode
3059 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3060 /* Let emit_move_complex do the bulk of the work. */
3061 tmps[i] = src;
3062 else if (GET_CODE (src) == CONCAT)
3063 {
3064 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
3065 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
3066 unsigned int elt;
3067 poly_int64 subpos;
3068
3069 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
3070 && known_le (subpos + bytelen, slen0))
3071 {
3072 /* The following assumes that the concatenated objects all
3073 have the same size. In this case, a simple calculation
3074 can be used to determine the object and the bit field
3075 to be extracted. */
3076 tmps[i] = XEXP (src, elt);
3077 if (maybe_ne (subpos, 0)
3078 || maybe_ne (subpos + bytelen, slen0)
3079 || (!CONSTANT_P (tmps[i])
3080 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
3081 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
3082 subpos * BITS_PER_UNIT,
3083 1, NULL_RTX, mode, mode, false,
3084 NULL);
3085 }
3086 else
3087 {
3088 rtx mem;
3089
3090 gcc_assert (known_eq (bytepos, 0));
3091 mem = assign_stack_temp (GET_MODE (src), slen);
3092 emit_move_insn (mem, src);
3093 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
3094 0, 1, NULL_RTX, mode, mode, false,
3095 NULL);
3096 }
3097 }
3098 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
3099 && XVECLEN (dst, 0) > 1)
3100 tmps[i] = force_subreg (mode, src, GET_MODE (dst), bytepos);
3101 else if (CONSTANT_P (src))
3102 {
3103 if (known_eq (bytelen, ssize))
3104 tmps[i] = src;
3105 else
3106 {
3107 rtx first, second;
3108
3109 /* TODO: const_wide_int can have sizes other than this... */
3110 gcc_assert (known_eq (2 * bytelen, ssize));
3111 split_double (src, &first, &second);
3112 if (i)
3113 tmps[i] = second;
3114 else
3115 tmps[i] = first;
3116 }
3117 }
3118 else if (REG_P (src) && GET_MODE (src) == mode)
3119 tmps[i] = src;
3120 else
3121 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
3122 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
3123 mode, mode, false, NULL);
3124
3125 if (maybe_ne (shift, 0))
3126 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
3127 shift, tmps[i], 0);
3128 }
3129 }
3130
3131 /* Emit code to move a block SRC of type TYPE to a block DST,
3132 where DST is non-consecutive registers represented by a PARALLEL.
3133 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
3134 if not known. */
3135
3136 void
3137 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
3138 {
3139 rtx *tmps;
3140 int i;
3141
3142 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
3143 emit_group_load_1 (tmps, dst, src, type, ssize);
3144
3145 /* Copy the extracted pieces into the proper (probable) hard regs. */
3146 for (i = 0; i < XVECLEN (dst, 0); i++)
3147 {
3148 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
3149 if (d == NULL)
3150 continue;
3151 emit_move_insn (d, tmps[i]);
3152 }
3153 }
3154
3155 /* Similar, but load SRC into new pseudos in a format that looks like
3156 PARALLEL. This can later be fed to emit_group_move to get things
3157 in the right place. */
3158
3159 rtx
3160 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
3161 {
3162 rtvec vec;
3163 int i;
3164
3165 vec = rtvec_alloc (XVECLEN (parallel, 0));
3166 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
3167
3168 /* Convert the vector to look just like the original PARALLEL, except
3169 with the computed values. */
3170 for (i = 0; i < XVECLEN (parallel, 0); i++)
3171 {
3172 rtx e = XVECEXP (parallel, 0, i);
3173 rtx d = XEXP (e, 0);
3174
3175 if (d)
3176 {
3177 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
3178 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
3179 }
3180 RTVEC_ELT (vec, i) = e;
3181 }
3182
3183 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
3184 }
3185
3186 /* Emit code to move a block SRC to block DST, where SRC and DST are
3187 non-consecutive groups of registers, each represented by a PARALLEL. */
3188
3189 void
3190 emit_group_move (rtx dst, rtx src)
3191 {
3192 int i;
3193
3194 gcc_assert (GET_CODE (src) == PARALLEL
3195 && GET_CODE (dst) == PARALLEL
3196 && XVECLEN (src, 0) == XVECLEN (dst, 0));
3197
3198 /* Skip first entry if NULL. */
3199 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
3200 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
3201 XEXP (XVECEXP (src, 0, i), 0));
3202 }
3203
3204 /* Move a group of registers represented by a PARALLEL into pseudos. */
3205
3206 rtx
3207 emit_group_move_into_temps (rtx src)
3208 {
3209 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
3210 int i;
3211
3212 for (i = 0; i < XVECLEN (src, 0); i++)
3213 {
3214 rtx e = XVECEXP (src, 0, i);
3215 rtx d = XEXP (e, 0);
3216
3217 if (d)
3218 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
3219 RTVEC_ELT (vec, i) = e;
3220 }
3221
3222 return gen_rtx_PARALLEL (GET_MODE (src), vec);
3223 }
3224
3225 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
3226 where SRC is non-consecutive registers represented by a PARALLEL.
3227 SSIZE represents the total size of block ORIG_DST, or -1 if not
3228 known. */
3229
3230 void
3231 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
3232 poly_int64 ssize)
3233 {
3234 rtx *tmps, dst;
3235 int start, finish, i;
3236 machine_mode m = GET_MODE (orig_dst);
3237
3238 gcc_assert (GET_CODE (src) == PARALLEL);
3239
3240 if (!SCALAR_INT_MODE_P (m)
3241 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
3242 {
3243 scalar_int_mode imode;
3244 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
3245 {
3246 dst = gen_reg_rtx (imode);
3247 emit_group_store (dst, src, type, ssize);
3248 dst = gen_lowpart (GET_MODE (orig_dst), dst);
3249 }
3250 else
3251 {
3252 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
3253 emit_group_store (dst, src, type, ssize);
3254 }
3255 emit_move_insn (orig_dst, dst);
3256 return;
3257 }
3258
3259 /* Check for a NULL entry, used to indicate that the parameter goes
3260 both on the stack and in registers. */
3261 if (XEXP (XVECEXP (src, 0, 0), 0))
3262 start = 0;
3263 else
3264 start = 1;
3265 finish = XVECLEN (src, 0);
3266
3267 tmps = XALLOCAVEC (rtx, finish);
3268
3269 /* Copy the (probable) hard regs into pseudos. */
3270 for (i = start; i < finish; i++)
3271 {
3272 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
3273 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
3274 {
3275 tmps[i] = gen_reg_rtx (GET_MODE (reg));
3276 emit_move_insn (tmps[i], reg);
3277 }
3278 else
3279 tmps[i] = reg;
3280 }
3281
3282 /* If we won't be storing directly into memory, protect the real destination
3283 from strange tricks we might play. */
3284 dst = orig_dst;
3285 if (GET_CODE (dst) == PARALLEL)
3286 {
3287 rtx temp;
3288
3289 /* We can get a PARALLEL dst if there is a conditional expression in
3290 a return statement. In that case, the dst and src are the same,
3291 so no action is necessary. */
3292 if (rtx_equal_p (dst, src))
3293 return;
3294
3295 /* It is unclear if we can ever reach here, but we may as well handle
3296 it. Allocate a temporary, and split this into a store/load to/from
3297 the temporary. */
3298 temp = assign_stack_temp (GET_MODE (dst), ssize);
3299 emit_group_store (temp, src, type, ssize);
3300 emit_group_load (dst, temp, type, ssize);
3301 return;
3302 }
3303 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
3304 {
3305 machine_mode outer = GET_MODE (dst);
3306 machine_mode inner;
3307 poly_int64 bytepos;
3308 bool done = false;
3309 rtx temp;
3310
3311 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
3312 dst = gen_reg_rtx (outer);
3313
3314 /* Make life a bit easier for combine: if the first element of the
3315 vector is the low part of the destination mode, use a paradoxical
3316 subreg to initialize the destination. */
3317 if (start < finish)
3318 {
3319 inner = GET_MODE (tmps[start]);
3320 bytepos = subreg_lowpart_offset (inner, outer);
3321 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, start), 1)),
3322 bytepos))
3323 {
3324 temp = force_subreg (outer, tmps[start], inner, 0);
3325 if (temp)
3326 {
3327 emit_move_insn (dst, temp);
3328 done = true;
3329 start++;
3330 }
3331 }
3332 }
3333
3334 /* If the first element wasn't the low part, try the last. */
3335 if (!done
3336 && start < finish - 1)
3337 {
3338 inner = GET_MODE (tmps[finish - 1]);
3339 bytepos = subreg_lowpart_offset (inner, outer);
3340 if (known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
3341 finish - 1), 1)),
3342 bytepos))
3343 {
3344 temp = force_subreg (outer, tmps[finish - 1], inner, 0);
3345 if (temp)
3346 {
3347 emit_move_insn (dst, temp);
3348 done = true;
3349 finish--;
3350 }
3351 }
3352 }
3353
3354 /* Otherwise, simply initialize the result to zero. */
3355 if (!done)
3356 emit_move_insn (dst, CONST0_RTX (outer));
3357 }
3358
3359 /* Process the pieces. */
3360 for (i = start; i < finish; i++)
3361 {
3362 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
3363 machine_mode mode = GET_MODE (tmps[i]);
3364 poly_int64 bytelen = GET_MODE_SIZE (mode);
3365 poly_uint64 adj_bytelen;
3366 rtx dest = dst;
3367
3368 /* Handle trailing fragments that run over the size of the struct.
3369 It's the target's responsibility to make sure that the fragment
3370 cannot be strictly smaller in some cases and strictly larger
3371 in others. */
3372 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
3373 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3374 adj_bytelen = ssize - bytepos;
3375 else
3376 adj_bytelen = bytelen;
3377
3378 /* Deal with destination CONCATs by either storing into one of the parts
3379 or doing a copy after storing into a register or stack temporary. */
3380 if (GET_CODE (dst) == CONCAT)
3381 {
3382 if (known_le (bytepos + adj_bytelen,
3383 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3384 dest = XEXP (dst, 0);
3385
3386 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
3387 {
3388 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
3389 dest = XEXP (dst, 1);
3390 }
3391
3392 else
3393 {
3394 machine_mode dest_mode = GET_MODE (dest);
3395 machine_mode tmp_mode = GET_MODE (tmps[i]);
3396 scalar_int_mode dest_imode;
3397
3398 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
3399
3400 /* If the source is a single scalar integer register, and the
3401 destination has a complex mode for which a same-sized integer
3402 mode exists, then we can take the left-justified part of the
3403 source in the complex mode. */
3404 if (finish == start + 1
3405 && REG_P (tmps[i])
3406 && SCALAR_INT_MODE_P (tmp_mode)
3407 && COMPLEX_MODE_P (dest_mode)
3408 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3409 {
3410 const scalar_int_mode tmp_imode
3411 = as_a <scalar_int_mode> (tmp_mode);
3412
3413 if (GET_MODE_BITSIZE (dest_imode)
3414 < GET_MODE_BITSIZE (tmp_imode))
3415 {
3416 dest = gen_reg_rtx (dest_imode);
3417 if (BYTES_BIG_ENDIAN)
3418 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3419 GET_MODE_BITSIZE (tmp_imode)
3420 - GET_MODE_BITSIZE (dest_imode),
3421 NULL_RTX, 1);
3422 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3423 dst = gen_lowpart (dest_mode, dest);
3424 }
3425 else
3426 dst = gen_lowpart (dest_mode, tmps[i]);
3427 }
3428
3429 /* Otherwise spill the source onto the stack using the more
3430 aligned of the two modes. */
3431 else if (GET_MODE_ALIGNMENT (dest_mode)
3432 >= GET_MODE_ALIGNMENT (tmp_mode))
3433 {
3434 dest = assign_stack_temp (dest_mode,
3435 GET_MODE_SIZE (dest_mode));
3436 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3437 tmps[i]);
3438 dst = dest;
3439 }
3440
3441 else
3442 {
3443 dest = assign_stack_temp (tmp_mode,
3444 GET_MODE_SIZE (tmp_mode));
3445 emit_move_insn (dest, tmps[i]);
3446 dst = adjust_address (dest, dest_mode, bytepos);
3447 }
3448
3449 break;
3450 }
3451 }
3452
3453 /* Handle trailing fragments that run over the size of the struct. */
3454 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3455 {
3456 /* store_bit_field always takes its value from the lsb.
3457 Move the fragment to the lsb if it's not already there. */
3458 if (
3459 #ifdef BLOCK_REG_PADDING
3460 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3461 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3462 #else
3463 BYTES_BIG_ENDIAN
3464 #endif
3465 )
3466 {
3467 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3468 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3469 shift, tmps[i], 0);
3470 }
3471
3472 /* Make sure not to write past the end of the struct. */
3473 store_bit_field (dest,
3474 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3475 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3476 VOIDmode, tmps[i], false, false);
3477 }
3478
3479 /* Optimize the access just a bit. */
3480 else if (MEM_P (dest)
3481 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3482 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3483 && multiple_p (bytepos * BITS_PER_UNIT,
3484 GET_MODE_ALIGNMENT (mode))
3485 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3486 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3487
3488 else
3489 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3490 0, 0, mode, tmps[i], false, false);
3491 }
3492
3493 /* Copy from the pseudo into the (probable) hard reg. */
3494 if (orig_dst != dst)
3495 emit_move_insn (orig_dst, dst);
3496 }
3497
3498 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3499 of the value stored in X. */
3500
3501 rtx
3502 maybe_emit_group_store (rtx x, tree type)
3503 {
3504 machine_mode mode = TYPE_MODE (type);
3505 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3506 if (GET_CODE (x) == PARALLEL)
3507 {
3508 rtx result = gen_reg_rtx (mode);
3509 emit_group_store (result, x, type, int_size_in_bytes (type));
3510 return result;
3511 }
3512 return x;
3513 }
3514
3515 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3516
3517 This is used on targets that return BLKmode values in registers. */
3518
3519 static void
3520 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3521 {
3522 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3523 rtx src = NULL, dst = NULL;
3524 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3525 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3526 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3527 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3528 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3529 fixed_size_mode copy_mode;
3530
3531 /* BLKmode registers created in the back-end shouldn't have survived. */
3532 gcc_assert (mode != BLKmode);
3533
3534 /* If the structure doesn't take up a whole number of words, see whether
3535 SRCREG is padded on the left or on the right. If it's on the left,
3536 set PADDING_CORRECTION to the number of bits to skip.
3537
3538 In most ABIs, the structure will be returned at the least end of
3539 the register, which translates to right padding on little-endian
3540 targets and left padding on big-endian targets. The opposite
3541 holds if the structure is returned at the most significant
3542 end of the register. */
3543 if (bytes % UNITS_PER_WORD != 0
3544 && (targetm.calls.return_in_msb (type)
3545 ? !BYTES_BIG_ENDIAN
3546 : BYTES_BIG_ENDIAN))
3547 padding_correction
3548 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3549
3550 /* We can use a single move if we have an exact mode for the size. */
3551 else if (MEM_P (target)
3552 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3553 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3554 && bytes == GET_MODE_SIZE (mode))
3555 {
3556 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3557 return;
3558 }
3559
3560 /* And if we additionally have the same mode for a register. */
3561 else if (REG_P (target)
3562 && GET_MODE (target) == mode
3563 && bytes == GET_MODE_SIZE (mode))
3564 {
3565 emit_move_insn (target, srcreg);
3566 return;
3567 }
3568
3569 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3570 into a new pseudo which is a full word. */
3571 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3572 {
3573 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3574 mode = word_mode;
3575 }
3576
3577 /* Copy the structure BITSIZE bits at a time. If the target lives in
3578 memory, take care of not reading/writing past its end by selecting
3579 a copy mode suited to BITSIZE. This should always be possible given
3580 how it is computed.
3581
3582 If the target lives in register, make sure not to select a copy mode
3583 larger than the mode of the register.
3584
3585 We could probably emit more efficient code for machines which do not use
3586 strict alignment, but it doesn't seem worth the effort at the current
3587 time. */
3588
3589 copy_mode = word_mode;
3590 if (MEM_P (target))
3591 {
3592 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3593 if (mem_mode.exists ())
3594 copy_mode = mem_mode.require ();
3595 }
3596 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3597 copy_mode = tmode;
3598
3599 for (bitpos = 0, xbitpos = padding_correction;
3600 bitpos < bytes * BITS_PER_UNIT;
3601 bitpos += bitsize, xbitpos += bitsize)
3602 {
3603 /* We need a new source operand each time xbitpos is on a
3604 word boundary and when xbitpos == padding_correction
3605 (the first time through). */
3606 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3607 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3608
3609 /* We need a new destination operand each time bitpos is on
3610 a word boundary. */
3611 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3612 dst = target;
3613 else if (bitpos % BITS_PER_WORD == 0)
3614 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3615
3616 /* Use xbitpos for the source extraction (right justified) and
3617 bitpos for the destination store (left justified). */
3618 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3619 extract_bit_field (src, bitsize,
3620 xbitpos % BITS_PER_WORD, 1,
3621 NULL_RTX, copy_mode, copy_mode,
3622 false, NULL),
3623 false, false);
3624 }
3625 }
3626
3627 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3628 register if it contains any data, otherwise return null.
3629
3630 This is used on targets that return BLKmode values in registers. */
3631
3632 rtx
3633 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3634 {
3635 int i, n_regs;
3636 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3637 unsigned int bitsize;
3638 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3639 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3640 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3641 fixed_size_mode dst_mode;
3642 scalar_int_mode min_mode;
3643
3644 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3645
3646 x = expand_normal (src);
3647
3648 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3649 if (bytes == 0)
3650 return NULL_RTX;
3651
3652 /* If the structure doesn't take up a whole number of words, see
3653 whether the register value should be padded on the left or on
3654 the right. Set PADDING_CORRECTION to the number of padding
3655 bits needed on the left side.
3656
3657 In most ABIs, the structure will be returned at the least end of
3658 the register, which translates to right padding on little-endian
3659 targets and left padding on big-endian targets. The opposite
3660 holds if the structure is returned at the most significant
3661 end of the register. */
3662 if (bytes % UNITS_PER_WORD != 0
3663 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3664 ? !BYTES_BIG_ENDIAN
3665 : BYTES_BIG_ENDIAN))
3666 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3667 * BITS_PER_UNIT));
3668
3669 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3670 dst_words = XALLOCAVEC (rtx, n_regs);
3671 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3672 min_mode = smallest_int_mode_for_size (bitsize).require ();
3673
3674 /* Copy the structure BITSIZE bits at a time. */
3675 for (bitpos = 0, xbitpos = padding_correction;
3676 bitpos < bytes * BITS_PER_UNIT;
3677 bitpos += bitsize, xbitpos += bitsize)
3678 {
3679 /* We need a new destination pseudo each time xbitpos is
3680 on a word boundary and when xbitpos == padding_correction
3681 (the first time through). */
3682 if (xbitpos % BITS_PER_WORD == 0
3683 || xbitpos == padding_correction)
3684 {
3685 /* Generate an appropriate register. */
3686 dst_word = gen_reg_rtx (word_mode);
3687 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3688
3689 /* Clear the destination before we move anything into it. */
3690 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3691 }
3692
3693 /* Find the largest integer mode that can be used to copy all or as
3694 many bits as possible of the structure if the target supports larger
3695 copies. There are too many corner cases here w.r.t to alignments on
3696 the read/writes. So if there is any padding just use single byte
3697 operations. */
3698 opt_scalar_int_mode mode_iter;
3699 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3700 {
3701 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3702 {
3703 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3704 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3705 && msize <= BITS_PER_WORD)
3706 bitsize = msize;
3707 else
3708 break;
3709 }
3710 }
3711
3712 /* We need a new source operand each time bitpos is on a word
3713 boundary. */
3714 if (bitpos % BITS_PER_WORD == 0)
3715 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3716
3717 /* Use bitpos for the source extraction (left justified) and
3718 xbitpos for the destination store (right justified). */
3719 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3720 0, 0, word_mode,
3721 extract_bit_field (src_word, bitsize,
3722 bitpos % BITS_PER_WORD, 1,
3723 NULL_RTX, word_mode, word_mode,
3724 false, NULL),
3725 false, false);
3726 }
3727
3728 if (mode == BLKmode)
3729 {
3730 /* Find the smallest integer mode large enough to hold the
3731 entire structure. */
3732 opt_scalar_int_mode mode_iter;
3733 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3734 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3735 break;
3736
3737 /* A suitable mode should have been found. */
3738 mode = mode_iter.require ();
3739 }
3740
3741 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3742 dst_mode = word_mode;
3743 else
3744 dst_mode = mode;
3745 dst = gen_reg_rtx (dst_mode);
3746
3747 for (i = 0; i < n_regs; i++)
3748 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3749
3750 if (mode != dst_mode)
3751 dst = gen_lowpart (mode, dst);
3752
3753 return dst;
3754 }
3755
3756 /* Add a USE expression for REG to the (possibly empty) list pointed
3757 to by CALL_FUSAGE. REG must denote a hard register. */
3758
3759 void
3760 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3761 {
3762 gcc_assert (REG_P (reg));
3763
3764 if (!HARD_REGISTER_P (reg))
3765 return;
3766
3767 *call_fusage
3768 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3769 }
3770
3771 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3772 to by CALL_FUSAGE. REG must denote a hard register. */
3773
3774 void
3775 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3776 {
3777 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3778
3779 *call_fusage
3780 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3781 }
3782
3783 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3784 starting at REGNO. All of these registers must be hard registers. */
3785
3786 void
3787 use_regs (rtx *call_fusage, int regno, int nregs)
3788 {
3789 int i;
3790
3791 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3792
3793 for (i = 0; i < nregs; i++)
3794 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3795 }
3796
3797 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3798 PARALLEL REGS. This is for calls that pass values in multiple
3799 non-contiguous locations. The Irix 6 ABI has examples of this. */
3800
3801 void
3802 use_group_regs (rtx *call_fusage, rtx regs)
3803 {
3804 int i;
3805
3806 for (i = 0; i < XVECLEN (regs, 0); i++)
3807 {
3808 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3809
3810 /* A NULL entry means the parameter goes both on the stack and in
3811 registers. This can also be a MEM for targets that pass values
3812 partially on the stack and partially in registers. */
3813 if (reg != 0 && REG_P (reg))
3814 use_reg (call_fusage, reg);
3815 }
3816 }
3817
3818 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3819 assigment and the code of the expresion on the RHS is CODE. Return
3820 NULL otherwise. */
3821
3822 static gimple *
3823 get_def_for_expr (tree name, enum tree_code code)
3824 {
3825 gimple *def_stmt;
3826
3827 if (TREE_CODE (name) != SSA_NAME)
3828 return NULL;
3829
3830 def_stmt = get_gimple_for_ssa_name (name);
3831 if (!def_stmt
3832 || !is_gimple_assign (def_stmt)
3833 || gimple_assign_rhs_code (def_stmt) != code)
3834 return NULL;
3835
3836 return def_stmt;
3837 }
3838
3839 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3840 assigment and the class of the expresion on the RHS is CLASS. Return
3841 NULL otherwise. */
3842
3843 static gimple *
3844 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3845 {
3846 gimple *def_stmt;
3847
3848 if (TREE_CODE (name) != SSA_NAME)
3849 return NULL;
3850
3851 def_stmt = get_gimple_for_ssa_name (name);
3852 if (!def_stmt
3853 || !is_gimple_assign (def_stmt)
3854 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3855 return NULL;
3856
3857 return def_stmt;
3858 }
3859
3860 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3861 its length in bytes. */
3862
3863 rtx
3864 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3865 unsigned int expected_align, HOST_WIDE_INT expected_size,
3866 unsigned HOST_WIDE_INT min_size,
3867 unsigned HOST_WIDE_INT max_size,
3868 unsigned HOST_WIDE_INT probable_max_size,
3869 unsigned ctz_size)
3870 {
3871 machine_mode mode = GET_MODE (object);
3872 unsigned int align;
3873
3874 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3875
3876 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3877 just move a zero. Otherwise, do this a piece at a time. */
3878 poly_int64 size_val;
3879 if (mode != BLKmode
3880 && poly_int_rtx_p (size, &size_val)
3881 && known_eq (size_val, GET_MODE_SIZE (mode)))
3882 {
3883 rtx zero = CONST0_RTX (mode);
3884 if (zero != NULL)
3885 {
3886 emit_move_insn (object, zero);
3887 return NULL;
3888 }
3889
3890 if (COMPLEX_MODE_P (mode))
3891 {
3892 zero = CONST0_RTX (GET_MODE_INNER (mode));
3893 if (zero != NULL)
3894 {
3895 write_complex_part (object, zero, 0, true);
3896 write_complex_part (object, zero, 1, false);
3897 return NULL;
3898 }
3899 }
3900 }
3901
3902 if (size == const0_rtx)
3903 return NULL;
3904
3905 align = MEM_ALIGN (object);
3906
3907 if (CONST_INT_P (size)
3908 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3909 CLEAR_BY_PIECES,
3910 optimize_insn_for_speed_p ()))
3911 clear_by_pieces (object, INTVAL (size), align);
3912 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3913 expected_align, expected_size,
3914 min_size, max_size, probable_max_size))
3915 ;
3916 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3917 min_size, max_size,
3918 NULL_RTX, 0, align))
3919 ;
3920 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3921 return set_storage_via_libcall (object, size, const0_rtx,
3922 method == BLOCK_OP_TAILCALL);
3923 else
3924 gcc_unreachable ();
3925
3926 return NULL;
3927 }
3928
3929 rtx
3930 clear_storage (rtx object, rtx size, enum block_op_methods method)
3931 {
3932 unsigned HOST_WIDE_INT max, min = 0;
3933 if (GET_CODE (size) == CONST_INT)
3934 min = max = UINTVAL (size);
3935 else
3936 max = GET_MODE_MASK (GET_MODE (size));
3937 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3938 }
3939
3940
3941 /* A subroutine of clear_storage. Expand a call to memset.
3942 Return the return value of memset, 0 otherwise. */
3943
3944 rtx
3945 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3946 {
3947 tree call_expr, fn, object_tree, size_tree, val_tree;
3948 machine_mode size_mode;
3949
3950 object = copy_addr_to_reg (XEXP (object, 0));
3951 object_tree = make_tree (ptr_type_node, object);
3952
3953 if (!CONST_INT_P (val))
3954 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3955 val_tree = make_tree (integer_type_node, val);
3956
3957 size_mode = TYPE_MODE (sizetype);
3958 size = convert_to_mode (size_mode, size, 1);
3959 size = copy_to_mode_reg (size_mode, size);
3960 size_tree = make_tree (sizetype, size);
3961
3962 /* It is incorrect to use the libcall calling conventions for calls to
3963 memset because it can be provided by the user. */
3964 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3965 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3966 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3967
3968 return expand_call (call_expr, NULL_RTX, false);
3969 }
3970
3971 /* Expand a setmem pattern; return true if successful. */
3972
3973 bool
3974 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3975 unsigned int expected_align, HOST_WIDE_INT expected_size,
3976 unsigned HOST_WIDE_INT min_size,
3977 unsigned HOST_WIDE_INT max_size,
3978 unsigned HOST_WIDE_INT probable_max_size)
3979 {
3980 /* Try the most limited insn first, because there's no point
3981 including more than one in the machine description unless
3982 the more limited one has some advantage. */
3983
3984 if (expected_align < align)
3985 expected_align = align;
3986 if (expected_size != -1)
3987 {
3988 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3989 expected_size = max_size;
3990 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3991 expected_size = min_size;
3992 }
3993
3994 opt_scalar_int_mode mode_iter;
3995 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3996 {
3997 scalar_int_mode mode = mode_iter.require ();
3998 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3999
4000 if (code != CODE_FOR_nothing
4001 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
4002 here because if SIZE is less than the mode mask, as it is
4003 returned by the macro, it will definitely be less than the
4004 actual mode mask. Since SIZE is within the Pmode address
4005 space, we limit MODE to Pmode. */
4006 && ((CONST_INT_P (size)
4007 && ((unsigned HOST_WIDE_INT) INTVAL (size)
4008 <= (GET_MODE_MASK (mode) >> 1)))
4009 || max_size <= (GET_MODE_MASK (mode) >> 1)
4010 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
4011 {
4012 class expand_operand ops[9];
4013 unsigned int nops;
4014
4015 nops = insn_data[(int) code].n_generator_args;
4016 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
4017
4018 create_fixed_operand (&ops[0], object);
4019 /* The check above guarantees that this size conversion is valid. */
4020 create_convert_operand_to (&ops[1], size, mode, true);
4021 create_convert_operand_from (&ops[2], val, byte_mode, true);
4022 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
4023 if (nops >= 6)
4024 {
4025 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
4026 create_integer_operand (&ops[5], expected_size);
4027 }
4028 if (nops >= 8)
4029 {
4030 create_integer_operand (&ops[6], min_size);
4031 /* If we cannot represent the maximal size,
4032 make parameter NULL. */
4033 if ((HOST_WIDE_INT) max_size != -1)
4034 create_integer_operand (&ops[7], max_size);
4035 else
4036 create_fixed_operand (&ops[7], NULL);
4037 }
4038 if (nops == 9)
4039 {
4040 /* If we cannot represent the maximal size,
4041 make parameter NULL. */
4042 if ((HOST_WIDE_INT) probable_max_size != -1)
4043 create_integer_operand (&ops[8], probable_max_size);
4044 else
4045 create_fixed_operand (&ops[8], NULL);
4046 }
4047 if (maybe_expand_insn (code, nops, ops))
4048 return true;
4049 }
4050 }
4051
4052 return false;
4053 }
4054
4055
4056 /* Write to one of the components of the complex value CPLX. Write VAL to
4057 the real part if IMAG_P is false, and the imaginary part if its true.
4058 If UNDEFINED_P then the value in CPLX is currently undefined. */
4059
4060 void
4061 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
4062 {
4063 machine_mode cmode;
4064 scalar_mode imode;
4065 unsigned ibitsize;
4066
4067 if (GET_CODE (cplx) == CONCAT)
4068 {
4069 emit_move_insn (XEXP (cplx, imag_p), val);
4070 return;
4071 }
4072
4073 cmode = GET_MODE (cplx);
4074 imode = GET_MODE_INNER (cmode);
4075 ibitsize = GET_MODE_BITSIZE (imode);
4076
4077 /* For MEMs simplify_gen_subreg may generate an invalid new address
4078 because, e.g., the original address is considered mode-dependent
4079 by the target, which restricts simplify_subreg from invoking
4080 adjust_address_nv. Instead of preparing fallback support for an
4081 invalid address, we call adjust_address_nv directly. */
4082 if (MEM_P (cplx))
4083 {
4084 emit_move_insn (adjust_address_nv (cplx, imode,
4085 imag_p ? GET_MODE_SIZE (imode) : 0),
4086 val);
4087 return;
4088 }
4089
4090 /* If the sub-object is at least word sized, then we know that subregging
4091 will work. This special case is important, since store_bit_field
4092 wants to operate on integer modes, and there's rarely an OImode to
4093 correspond to TCmode. */
4094 if (ibitsize >= BITS_PER_WORD
4095 /* For hard regs we have exact predicates. Assume we can split
4096 the original object if it spans an even number of hard regs.
4097 This special case is important for SCmode on 64-bit platforms
4098 where the natural size of floating-point regs is 32-bit. */
4099 || (REG_P (cplx)
4100 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4101 && REG_NREGS (cplx) % 2 == 0))
4102 {
4103 rtx part = simplify_gen_subreg (imode, cplx, cmode,
4104 imag_p ? GET_MODE_SIZE (imode) : 0);
4105 if (part)
4106 {
4107 emit_move_insn (part, val);
4108 return;
4109 }
4110 else
4111 /* simplify_gen_subreg may fail for sub-word MEMs. */
4112 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4113 }
4114
4115 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
4116 false, undefined_p);
4117 }
4118
4119 /* Extract one of the components of the complex value CPLX. Extract the
4120 real part if IMAG_P is false, and the imaginary part if it's true. */
4121
4122 rtx
4123 read_complex_part (rtx cplx, bool imag_p)
4124 {
4125 machine_mode cmode;
4126 scalar_mode imode;
4127 unsigned ibitsize;
4128
4129 if (GET_CODE (cplx) == CONCAT)
4130 return XEXP (cplx, imag_p);
4131
4132 cmode = GET_MODE (cplx);
4133 imode = GET_MODE_INNER (cmode);
4134 ibitsize = GET_MODE_BITSIZE (imode);
4135
4136 /* Special case reads from complex constants that got spilled to memory. */
4137 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
4138 {
4139 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
4140 if (decl && TREE_CODE (decl) == COMPLEX_CST)
4141 {
4142 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
4143 if (CONSTANT_CLASS_P (part))
4144 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
4145 }
4146 }
4147
4148 /* For MEMs simplify_gen_subreg may generate an invalid new address
4149 because, e.g., the original address is considered mode-dependent
4150 by the target, which restricts simplify_subreg from invoking
4151 adjust_address_nv. Instead of preparing fallback support for an
4152 invalid address, we call adjust_address_nv directly. */
4153 if (MEM_P (cplx))
4154 return adjust_address_nv (cplx, imode,
4155 imag_p ? GET_MODE_SIZE (imode) : 0);
4156
4157 /* If the sub-object is at least word sized, then we know that subregging
4158 will work. This special case is important, since extract_bit_field
4159 wants to operate on integer modes, and there's rarely an OImode to
4160 correspond to TCmode. */
4161 if (ibitsize >= BITS_PER_WORD
4162 /* For hard regs we have exact predicates. Assume we can split
4163 the original object if it spans an even number of hard regs.
4164 This special case is important for SCmode on 64-bit platforms
4165 where the natural size of floating-point regs is 32-bit. */
4166 || (REG_P (cplx)
4167 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
4168 && REG_NREGS (cplx) % 2 == 0))
4169 {
4170 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
4171 imag_p ? GET_MODE_SIZE (imode) : 0);
4172 if (ret)
4173 return ret;
4174 else
4175 /* simplify_gen_subreg may fail for sub-word MEMs. */
4176 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
4177 }
4178
4179 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
4180 true, NULL_RTX, imode, imode, false, NULL);
4181 }
4182
4183 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
4184 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
4185 represented in NEW_MODE. If FORCE is true, this will never happen, as
4186 we'll force-create a SUBREG if needed. */
4187
4188 static rtx
4189 emit_move_change_mode (machine_mode new_mode,
4190 machine_mode old_mode, rtx x, bool force)
4191 {
4192 rtx ret;
4193
4194 if (push_operand (x, GET_MODE (x)))
4195 {
4196 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
4197 MEM_COPY_ATTRIBUTES (ret, x);
4198 }
4199 else if (MEM_P (x))
4200 {
4201 /* We don't have to worry about changing the address since the
4202 size in bytes is supposed to be the same. */
4203 if (reload_in_progress)
4204 {
4205 /* Copy the MEM to change the mode and move any
4206 substitutions from the old MEM to the new one. */
4207 ret = adjust_address_nv (x, new_mode, 0);
4208 copy_replacements (x, ret);
4209 }
4210 else
4211 ret = adjust_address (x, new_mode, 0);
4212 }
4213 else
4214 {
4215 /* Note that we do want simplify_subreg's behavior of validating
4216 that the new mode is ok for a hard register. If we were to use
4217 simplify_gen_subreg, we would create the subreg, but would
4218 probably run into the target not being able to implement it. */
4219 /* Except, of course, when FORCE is true, when this is exactly what
4220 we want. Which is needed for CCmodes on some targets. */
4221 if (force)
4222 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
4223 else
4224 ret = simplify_subreg (new_mode, x, old_mode, 0);
4225 }
4226
4227 return ret;
4228 }
4229
4230 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
4231 an integer mode of the same size as MODE. Returns the instruction
4232 emitted, or NULL if such a move could not be generated. */
4233
4234 static rtx_insn *
4235 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
4236 {
4237 scalar_int_mode imode;
4238 enum insn_code code;
4239
4240 /* There must exist a mode of the exact size we require. */
4241 if (!int_mode_for_mode (mode).exists (&imode))
4242 return NULL;
4243
4244 /* The target must support moves in this mode. */
4245 code = optab_handler (mov_optab, imode);
4246 if (code == CODE_FOR_nothing)
4247 return NULL;
4248
4249 x = emit_move_change_mode (imode, mode, x, force);
4250 if (x == NULL_RTX)
4251 return NULL;
4252 y = emit_move_change_mode (imode, mode, y, force);
4253 if (y == NULL_RTX)
4254 return NULL;
4255 return emit_insn (GEN_FCN (code) (x, y));
4256 }
4257
4258 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
4259 Return an equivalent MEM that does not use an auto-increment. */
4260
4261 rtx
4262 emit_move_resolve_push (machine_mode mode, rtx x)
4263 {
4264 enum rtx_code code = GET_CODE (XEXP (x, 0));
4265 rtx temp;
4266
4267 poly_int64 adjust = GET_MODE_SIZE (mode);
4268 #ifdef PUSH_ROUNDING
4269 adjust = PUSH_ROUNDING (adjust);
4270 #endif
4271 if (code == PRE_DEC || code == POST_DEC)
4272 adjust = -adjust;
4273 else if (code == PRE_MODIFY || code == POST_MODIFY)
4274 {
4275 rtx expr = XEXP (XEXP (x, 0), 1);
4276
4277 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
4278 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
4279 if (GET_CODE (expr) == MINUS)
4280 val = -val;
4281 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
4282 adjust = val;
4283 }
4284
4285 /* Do not use anti_adjust_stack, since we don't want to update
4286 stack_pointer_delta. */
4287 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
4288 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
4289 0, OPTAB_LIB_WIDEN);
4290 if (temp != stack_pointer_rtx)
4291 emit_move_insn (stack_pointer_rtx, temp);
4292
4293 switch (code)
4294 {
4295 case PRE_INC:
4296 case PRE_DEC:
4297 case PRE_MODIFY:
4298 temp = stack_pointer_rtx;
4299 break;
4300 case POST_INC:
4301 case POST_DEC:
4302 case POST_MODIFY:
4303 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
4304 break;
4305 default:
4306 gcc_unreachable ();
4307 }
4308
4309 return replace_equiv_address (x, temp);
4310 }
4311
4312 /* A subroutine of emit_move_complex. Generate a move from Y into X.
4313 X is known to satisfy push_operand, and MODE is known to be complex.
4314 Returns the last instruction emitted. */
4315
4316 rtx_insn *
4317 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
4318 {
4319 scalar_mode submode = GET_MODE_INNER (mode);
4320 bool imag_first;
4321
4322 #ifdef PUSH_ROUNDING
4323 poly_int64 submodesize = GET_MODE_SIZE (submode);
4324
4325 /* In case we output to the stack, but the size is smaller than the
4326 machine can push exactly, we need to use move instructions. */
4327 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
4328 {
4329 x = emit_move_resolve_push (mode, x);
4330 return emit_move_insn (x, y);
4331 }
4332 #endif
4333
4334 /* Note that the real part always precedes the imag part in memory
4335 regardless of machine's endianness. */
4336 switch (GET_CODE (XEXP (x, 0)))
4337 {
4338 case PRE_DEC:
4339 case POST_DEC:
4340 imag_first = true;
4341 break;
4342 case PRE_INC:
4343 case POST_INC:
4344 imag_first = false;
4345 break;
4346 default:
4347 gcc_unreachable ();
4348 }
4349
4350 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4351 read_complex_part (y, imag_first));
4352 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
4353 read_complex_part (y, !imag_first));
4354 }
4355
4356 /* A subroutine of emit_move_complex. Perform the move from Y to X
4357 via two moves of the parts. Returns the last instruction emitted. */
4358
4359 rtx_insn *
4360 emit_move_complex_parts (rtx x, rtx y)
4361 {
4362 /* Show the output dies here. This is necessary for SUBREGs
4363 of pseudos since we cannot track their lifetimes correctly;
4364 hard regs shouldn't appear here except as return values. */
4365 if (!reload_completed && !reload_in_progress
4366 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
4367 emit_clobber (x);
4368
4369 write_complex_part (x, read_complex_part (y, false), false, true);
4370 write_complex_part (x, read_complex_part (y, true), true, false);
4371
4372 return get_last_insn ();
4373 }
4374
4375 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4376 MODE is known to be complex. Returns the last instruction emitted. */
4377
4378 static rtx_insn *
4379 emit_move_complex (machine_mode mode, rtx x, rtx y)
4380 {
4381 bool try_int;
4382
4383 /* Need to take special care for pushes, to maintain proper ordering
4384 of the data, and possibly extra padding. */
4385 if (push_operand (x, mode))
4386 return emit_move_complex_push (mode, x, y);
4387
4388 /* See if we can coerce the target into moving both values at once, except
4389 for floating point where we favor moving as parts if this is easy. */
4390 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4391 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
4392 && !(REG_P (x)
4393 && HARD_REGISTER_P (x)
4394 && REG_NREGS (x) == 1)
4395 && !(REG_P (y)
4396 && HARD_REGISTER_P (y)
4397 && REG_NREGS (y) == 1))
4398 try_int = false;
4399 /* Not possible if the values are inherently not adjacent. */
4400 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
4401 try_int = false;
4402 /* Is possible if both are registers (or subregs of registers). */
4403 else if (register_operand (x, mode) && register_operand (y, mode))
4404 try_int = true;
4405 /* If one of the operands is a memory, and alignment constraints
4406 are friendly enough, we may be able to do combined memory operations.
4407 We do not attempt this if Y is a constant because that combination is
4408 usually better with the by-parts thing below. */
4409 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4410 && (!STRICT_ALIGNMENT
4411 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4412 try_int = true;
4413 else
4414 try_int = false;
4415
4416 if (try_int)
4417 {
4418 rtx_insn *ret;
4419
4420 /* For memory to memory moves, optimal behavior can be had with the
4421 existing block move logic. But use normal expansion if optimizing
4422 for size. */
4423 if (MEM_P (x) && MEM_P (y))
4424 {
4425 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4426 (optimize_insn_for_speed_p()
4427 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4428 return get_last_insn ();
4429 }
4430
4431 ret = emit_move_via_integer (mode, x, y, true);
4432 if (ret)
4433 return ret;
4434 }
4435
4436 return emit_move_complex_parts (x, y);
4437 }
4438
4439 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4440 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4441
4442 static rtx_insn *
4443 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4444 {
4445 rtx_insn *ret;
4446
4447 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4448 if (mode != CCmode)
4449 {
4450 enum insn_code code = optab_handler (mov_optab, CCmode);
4451 if (code != CODE_FOR_nothing)
4452 {
4453 x = emit_move_change_mode (CCmode, mode, x, true);
4454 y = emit_move_change_mode (CCmode, mode, y, true);
4455 return emit_insn (GEN_FCN (code) (x, y));
4456 }
4457 }
4458
4459 /* Otherwise, find the MODE_INT mode of the same width. */
4460 ret = emit_move_via_integer (mode, x, y, false);
4461 gcc_assert (ret != NULL);
4462 return ret;
4463 }
4464
4465 /* Return true if word I of OP lies entirely in the
4466 undefined bits of a paradoxical subreg. */
4467
4468 static bool
4469 undefined_operand_subword_p (const_rtx op, int i)
4470 {
4471 if (GET_CODE (op) != SUBREG)
4472 return false;
4473 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4474 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4475 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4476 || known_le (offset, -UNITS_PER_WORD));
4477 }
4478
4479 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4480 MODE is any multi-word or full-word mode that lacks a move_insn
4481 pattern. Note that you will get better code if you define such
4482 patterns, even if they must turn into multiple assembler instructions. */
4483
4484 static rtx_insn *
4485 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4486 {
4487 rtx_insn *last_insn = 0;
4488 rtx_insn *seq;
4489 rtx inner;
4490 bool need_clobber;
4491 int i, mode_size;
4492
4493 /* This function can only handle cases where the number of words is
4494 known at compile time. */
4495 mode_size = GET_MODE_SIZE (mode).to_constant ();
4496 gcc_assert (mode_size >= UNITS_PER_WORD);
4497
4498 /* If X is a push on the stack, do the push now and replace
4499 X with a reference to the stack pointer. */
4500 if (push_operand (x, mode))
4501 x = emit_move_resolve_push (mode, x);
4502
4503 /* If we are in reload, see if either operand is a MEM whose address
4504 is scheduled for replacement. */
4505 if (reload_in_progress && MEM_P (x)
4506 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4507 x = replace_equiv_address_nv (x, inner);
4508 if (reload_in_progress && MEM_P (y)
4509 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4510 y = replace_equiv_address_nv (y, inner);
4511
4512 start_sequence ();
4513
4514 need_clobber = false;
4515 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4516 {
4517 /* Do not generate code for a move if it would go entirely
4518 to the non-existing bits of a paradoxical subreg. */
4519 if (undefined_operand_subword_p (x, i))
4520 continue;
4521
4522 rtx xpart = operand_subword (x, i, 1, mode);
4523 rtx ypart;
4524
4525 /* Do not generate code for a move if it would come entirely
4526 from the undefined bits of a paradoxical subreg. */
4527 if (undefined_operand_subword_p (y, i))
4528 continue;
4529
4530 ypart = operand_subword (y, i, 1, mode);
4531
4532 /* If we can't get a part of Y, put Y into memory if it is a
4533 constant. Otherwise, force it into a register. Then we must
4534 be able to get a part of Y. */
4535 if (ypart == 0 && CONSTANT_P (y))
4536 {
4537 y = use_anchored_address (force_const_mem (mode, y));
4538 ypart = operand_subword (y, i, 1, mode);
4539 }
4540 else if (ypart == 0)
4541 ypart = operand_subword_force (y, i, mode);
4542
4543 gcc_assert (xpart && ypart);
4544
4545 need_clobber |= (GET_CODE (xpart) == SUBREG);
4546
4547 last_insn = emit_move_insn (xpart, ypart);
4548 }
4549
4550 seq = get_insns ();
4551 end_sequence ();
4552
4553 /* Show the output dies here. This is necessary for SUBREGs
4554 of pseudos since we cannot track their lifetimes correctly;
4555 hard regs shouldn't appear here except as return values.
4556 We never want to emit such a clobber after reload. */
4557 if (x != y
4558 && ! (reload_in_progress || reload_completed)
4559 && need_clobber != 0)
4560 emit_clobber (x);
4561
4562 emit_insn (seq);
4563
4564 return last_insn;
4565 }
4566
4567 /* Low level part of emit_move_insn.
4568 Called just like emit_move_insn, but assumes X and Y
4569 are basically valid. */
4570
4571 rtx_insn *
4572 emit_move_insn_1 (rtx x, rtx y)
4573 {
4574 machine_mode mode = GET_MODE (x);
4575 enum insn_code code;
4576
4577 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4578
4579 code = optab_handler (mov_optab, mode);
4580 if (code != CODE_FOR_nothing)
4581 return emit_insn (GEN_FCN (code) (x, y));
4582
4583 /* Expand complex moves by moving real part and imag part. */
4584 if (COMPLEX_MODE_P (mode))
4585 return emit_move_complex (mode, x, y);
4586
4587 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4588 || ALL_FIXED_POINT_MODE_P (mode))
4589 {
4590 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4591
4592 /* If we can't find an integer mode, use multi words. */
4593 if (result)
4594 return result;
4595 else
4596 return emit_move_multi_word (mode, x, y);
4597 }
4598
4599 if (GET_MODE_CLASS (mode) == MODE_CC)
4600 return emit_move_ccmode (mode, x, y);
4601
4602 /* Try using a move pattern for the corresponding integer mode. This is
4603 only safe when simplify_subreg can convert MODE constants into integer
4604 constants. At present, it can only do this reliably if the value
4605 fits within a HOST_WIDE_INT. */
4606 if (!CONSTANT_P (y)
4607 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4608 {
4609 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4610
4611 if (ret)
4612 {
4613 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4614 return ret;
4615 }
4616 }
4617
4618 return emit_move_multi_word (mode, x, y);
4619 }
4620
4621 /* Generate code to copy Y into X.
4622 Both Y and X must have the same mode, except that
4623 Y can be a constant with VOIDmode.
4624 This mode cannot be BLKmode; use emit_block_move for that.
4625
4626 Return the last instruction emitted. */
4627
4628 rtx_insn *
4629 emit_move_insn (rtx x, rtx y)
4630 {
4631 machine_mode mode = GET_MODE (x);
4632 rtx y_cst = NULL_RTX;
4633 rtx_insn *last_insn;
4634 rtx set;
4635
4636 gcc_assert (mode != BLKmode
4637 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4638
4639 /* If we have a copy that looks like one of the following patterns:
4640 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4641 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4642 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4643 (set (subreg:M1 (reg:M2 ...)) (constant C))
4644 where mode M1 is equal in size to M2, try to detect whether the
4645 mode change involves an implicit round trip through memory.
4646 If so, see if we can avoid that by removing the subregs and
4647 doing the move in mode M2 instead. */
4648
4649 rtx x_inner = NULL_RTX;
4650 rtx y_inner = NULL_RTX;
4651
4652 auto candidate_subreg_p = [&](rtx subreg) {
4653 return (REG_P (SUBREG_REG (subreg))
4654 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4655 GET_MODE_SIZE (GET_MODE (subreg)))
4656 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4657 != CODE_FOR_nothing);
4658 };
4659
4660 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4661 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4662 && !push_operand (mem, GET_MODE (mem))
4663 /* Not a candiate if innermode requires too much alignment. */
4664 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4665 || targetm.slow_unaligned_access (GET_MODE (mem),
4666 MEM_ALIGN (mem))
4667 || !targetm.slow_unaligned_access (innermode,
4668 MEM_ALIGN (mem))));
4669 };
4670
4671 if (SUBREG_P (x) && candidate_subreg_p (x))
4672 x_inner = SUBREG_REG (x);
4673
4674 if (SUBREG_P (y) && candidate_subreg_p (y))
4675 y_inner = SUBREG_REG (y);
4676
4677 if (x_inner != NULL_RTX
4678 && y_inner != NULL_RTX
4679 && GET_MODE (x_inner) == GET_MODE (y_inner)
4680 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4681 {
4682 x = x_inner;
4683 y = y_inner;
4684 mode = GET_MODE (x_inner);
4685 }
4686 else if (x_inner != NULL_RTX
4687 && MEM_P (y)
4688 && candidate_mem_p (GET_MODE (x_inner), y))
4689 {
4690 x = x_inner;
4691 y = adjust_address (y, GET_MODE (x_inner), 0);
4692 mode = GET_MODE (x_inner);
4693 }
4694 else if (y_inner != NULL_RTX
4695 && MEM_P (x)
4696 && candidate_mem_p (GET_MODE (y_inner), x))
4697 {
4698 x = adjust_address (x, GET_MODE (y_inner), 0);
4699 y = y_inner;
4700 mode = GET_MODE (y_inner);
4701 }
4702 else if (x_inner != NULL_RTX
4703 && CONSTANT_P (y)
4704 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4705 mode, ALL_REGS)
4706 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4707 {
4708 x = x_inner;
4709 y = y_inner;
4710 mode = GET_MODE (x_inner);
4711 }
4712
4713 if (CONSTANT_P (y))
4714 {
4715 if (optimize
4716 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4717 && (last_insn = compress_float_constant (x, y)))
4718 return last_insn;
4719
4720 y_cst = y;
4721
4722 if (!targetm.legitimate_constant_p (mode, y))
4723 {
4724 y = force_const_mem (mode, y);
4725
4726 /* If the target's cannot_force_const_mem prevented the spill,
4727 assume that the target's move expanders will also take care
4728 of the non-legitimate constant. */
4729 if (!y)
4730 y = y_cst;
4731 else
4732 y = use_anchored_address (y);
4733 }
4734 }
4735
4736 /* If X or Y are memory references, verify that their addresses are valid
4737 for the machine. */
4738 if (MEM_P (x)
4739 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4740 MEM_ADDR_SPACE (x))
4741 && ! push_operand (x, GET_MODE (x))))
4742 x = validize_mem (x);
4743
4744 if (MEM_P (y)
4745 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4746 MEM_ADDR_SPACE (y)))
4747 y = validize_mem (y);
4748
4749 gcc_assert (mode != BLKmode);
4750
4751 last_insn = emit_move_insn_1 (x, y);
4752
4753 if (y_cst && REG_P (x)
4754 && (set = single_set (last_insn)) != NULL_RTX
4755 && SET_DEST (set) == x
4756 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4757 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4758
4759 return last_insn;
4760 }
4761
4762 /* Generate the body of an instruction to copy Y into X.
4763 It may be a list of insns, if one insn isn't enough. */
4764
4765 rtx_insn *
4766 gen_move_insn (rtx x, rtx y)
4767 {
4768 rtx_insn *seq;
4769
4770 start_sequence ();
4771 emit_move_insn_1 (x, y);
4772 seq = get_insns ();
4773 end_sequence ();
4774 return seq;
4775 }
4776
4777 /* If Y is representable exactly in a narrower mode, and the target can
4778 perform the extension directly from constant or memory, then emit the
4779 move as an extension. */
4780
4781 static rtx_insn *
4782 compress_float_constant (rtx x, rtx y)
4783 {
4784 machine_mode dstmode = GET_MODE (x);
4785 machine_mode orig_srcmode = GET_MODE (y);
4786 machine_mode srcmode;
4787 const REAL_VALUE_TYPE *r;
4788 int oldcost, newcost;
4789 bool speed = optimize_insn_for_speed_p ();
4790
4791 r = CONST_DOUBLE_REAL_VALUE (y);
4792
4793 if (targetm.legitimate_constant_p (dstmode, y))
4794 oldcost = set_src_cost (y, orig_srcmode, speed);
4795 else
4796 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4797
4798 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4799 {
4800 enum insn_code ic;
4801 rtx trunc_y;
4802 rtx_insn *last_insn;
4803
4804 /* Skip if the target can't extend this way. */
4805 ic = can_extend_p (dstmode, srcmode, 0);
4806 if (ic == CODE_FOR_nothing)
4807 continue;
4808
4809 /* Skip if the narrowed value isn't exact. */
4810 if (! exact_real_truncate (srcmode, r))
4811 continue;
4812
4813 trunc_y = const_double_from_real_value (*r, srcmode);
4814
4815 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4816 {
4817 /* Skip if the target needs extra instructions to perform
4818 the extension. */
4819 if (!insn_operand_matches (ic, 1, trunc_y))
4820 continue;
4821 /* This is valid, but may not be cheaper than the original. */
4822 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4823 dstmode, speed);
4824 if (oldcost < newcost)
4825 continue;
4826 }
4827 else if (float_extend_from_mem[dstmode][srcmode])
4828 {
4829 trunc_y = force_const_mem (srcmode, trunc_y);
4830 /* This is valid, but may not be cheaper than the original. */
4831 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4832 dstmode, speed);
4833 if (oldcost < newcost)
4834 continue;
4835 trunc_y = validize_mem (trunc_y);
4836 }
4837 else
4838 continue;
4839
4840 /* For CSE's benefit, force the compressed constant pool entry
4841 into a new pseudo. This constant may be used in different modes,
4842 and if not, combine will put things back together for us. */
4843 trunc_y = force_reg (srcmode, trunc_y);
4844
4845 /* If x is a hard register, perform the extension into a pseudo,
4846 so that e.g. stack realignment code is aware of it. */
4847 rtx target = x;
4848 if (REG_P (x) && HARD_REGISTER_P (x))
4849 target = gen_reg_rtx (dstmode);
4850
4851 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4852 last_insn = get_last_insn ();
4853
4854 if (REG_P (target))
4855 set_unique_reg_note (last_insn, REG_EQUAL, y);
4856
4857 if (target != x)
4858 return emit_move_insn (x, target);
4859 return last_insn;
4860 }
4861
4862 return NULL;
4863 }
4864
4865 /* Pushing data onto the stack. */
4866
4867 /* Push a block of length SIZE (perhaps variable)
4868 and return an rtx to address the beginning of the block.
4869 The value may be virtual_outgoing_args_rtx.
4870
4871 EXTRA is the number of bytes of padding to push in addition to SIZE.
4872 BELOW nonzero means this padding comes at low addresses;
4873 otherwise, the padding comes at high addresses. */
4874
4875 rtx
4876 push_block (rtx size, poly_int64 extra, int below)
4877 {
4878 rtx temp;
4879
4880 size = convert_modes (Pmode, ptr_mode, size, 1);
4881 if (CONSTANT_P (size))
4882 anti_adjust_stack (plus_constant (Pmode, size, extra));
4883 else if (REG_P (size) && known_eq (extra, 0))
4884 anti_adjust_stack (size);
4885 else
4886 {
4887 temp = copy_to_mode_reg (Pmode, size);
4888 if (maybe_ne (extra, 0))
4889 temp = expand_binop (Pmode, add_optab, temp,
4890 gen_int_mode (extra, Pmode),
4891 temp, 0, OPTAB_LIB_WIDEN);
4892 anti_adjust_stack (temp);
4893 }
4894
4895 if (STACK_GROWS_DOWNWARD)
4896 {
4897 temp = virtual_outgoing_args_rtx;
4898 if (maybe_ne (extra, 0) && below)
4899 temp = plus_constant (Pmode, temp, extra);
4900 }
4901 else
4902 {
4903 poly_int64 csize;
4904 if (poly_int_rtx_p (size, &csize))
4905 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4906 -csize - (below ? 0 : extra));
4907 else if (maybe_ne (extra, 0) && !below)
4908 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4909 negate_rtx (Pmode, plus_constant (Pmode, size,
4910 extra)));
4911 else
4912 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4913 negate_rtx (Pmode, size));
4914 }
4915
4916 return memory_address (NARROWEST_INT_MODE, temp);
4917 }
4918
4919 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4920
4921 static rtx
4922 mem_autoinc_base (rtx mem)
4923 {
4924 if (MEM_P (mem))
4925 {
4926 rtx addr = XEXP (mem, 0);
4927 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4928 return XEXP (addr, 0);
4929 }
4930 return NULL;
4931 }
4932
4933 /* A utility routine used here, in reload, and in try_split. The insns
4934 after PREV up to and including LAST are known to adjust the stack,
4935 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4936 placing notes as appropriate. PREV may be NULL, indicating the
4937 entire insn sequence prior to LAST should be scanned.
4938
4939 The set of allowed stack pointer modifications is small:
4940 (1) One or more auto-inc style memory references (aka pushes),
4941 (2) One or more addition/subtraction with the SP as destination,
4942 (3) A single move insn with the SP as destination,
4943 (4) A call_pop insn,
4944 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4945
4946 Insns in the sequence that do not modify the SP are ignored,
4947 except for noreturn calls.
4948
4949 The return value is the amount of adjustment that can be trivially
4950 verified, via immediate operand or auto-inc. If the adjustment
4951 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4952
4953 poly_int64
4954 find_args_size_adjust (rtx_insn *insn)
4955 {
4956 rtx dest, set, pat;
4957 int i;
4958
4959 pat = PATTERN (insn);
4960 set = NULL;
4961
4962 /* Look for a call_pop pattern. */
4963 if (CALL_P (insn))
4964 {
4965 /* We have to allow non-call_pop patterns for the case
4966 of emit_single_push_insn of a TLS address. */
4967 if (GET_CODE (pat) != PARALLEL)
4968 return 0;
4969
4970 /* All call_pop have a stack pointer adjust in the parallel.
4971 The call itself is always first, and the stack adjust is
4972 usually last, so search from the end. */
4973 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4974 {
4975 set = XVECEXP (pat, 0, i);
4976 if (GET_CODE (set) != SET)
4977 continue;
4978 dest = SET_DEST (set);
4979 if (dest == stack_pointer_rtx)
4980 break;
4981 }
4982 /* We'd better have found the stack pointer adjust. */
4983 if (i == 0)
4984 return 0;
4985 /* Fall through to process the extracted SET and DEST
4986 as if it was a standalone insn. */
4987 }
4988 else if (GET_CODE (pat) == SET)
4989 set = pat;
4990 else if ((set = single_set (insn)) != NULL)
4991 ;
4992 else if (GET_CODE (pat) == PARALLEL)
4993 {
4994 /* ??? Some older ports use a parallel with a stack adjust
4995 and a store for a PUSH_ROUNDING pattern, rather than a
4996 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4997 /* ??? See h8300 and m68k, pushqi1. */
4998 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4999 {
5000 set = XVECEXP (pat, 0, i);
5001 if (GET_CODE (set) != SET)
5002 continue;
5003 dest = SET_DEST (set);
5004 if (dest == stack_pointer_rtx)
5005 break;
5006
5007 /* We do not expect an auto-inc of the sp in the parallel. */
5008 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
5009 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
5010 != stack_pointer_rtx);
5011 }
5012 if (i < 0)
5013 return 0;
5014 }
5015 else
5016 return 0;
5017
5018 dest = SET_DEST (set);
5019
5020 /* Look for direct modifications of the stack pointer. */
5021 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
5022 {
5023 /* Look for a trivial adjustment, otherwise assume nothing. */
5024 /* Note that the SPU restore_stack_block pattern refers to
5025 the stack pointer in V4SImode. Consider that non-trivial. */
5026 poly_int64 offset;
5027 if (SCALAR_INT_MODE_P (GET_MODE (dest))
5028 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
5029 return offset;
5030 /* ??? Reload can generate no-op moves, which will be cleaned
5031 up later. Recognize it and continue searching. */
5032 else if (rtx_equal_p (dest, SET_SRC (set)))
5033 return 0;
5034 else
5035 return HOST_WIDE_INT_MIN;
5036 }
5037 else
5038 {
5039 rtx mem, addr;
5040
5041 /* Otherwise only think about autoinc patterns. */
5042 if (mem_autoinc_base (dest) == stack_pointer_rtx)
5043 {
5044 mem = dest;
5045 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
5046 != stack_pointer_rtx);
5047 }
5048 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
5049 mem = SET_SRC (set);
5050 else
5051 return 0;
5052
5053 addr = XEXP (mem, 0);
5054 switch (GET_CODE (addr))
5055 {
5056 case PRE_INC:
5057 case POST_INC:
5058 return GET_MODE_SIZE (GET_MODE (mem));
5059 case PRE_DEC:
5060 case POST_DEC:
5061 return -GET_MODE_SIZE (GET_MODE (mem));
5062 case PRE_MODIFY:
5063 case POST_MODIFY:
5064 addr = XEXP (addr, 1);
5065 gcc_assert (GET_CODE (addr) == PLUS);
5066 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
5067 return rtx_to_poly_int64 (XEXP (addr, 1));
5068 default:
5069 gcc_unreachable ();
5070 }
5071 }
5072 }
5073
5074 poly_int64
5075 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
5076 poly_int64 end_args_size)
5077 {
5078 poly_int64 args_size = end_args_size;
5079 bool saw_unknown = false;
5080 rtx_insn *insn;
5081
5082 for (insn = last; insn != prev; insn = PREV_INSN (insn))
5083 {
5084 if (!NONDEBUG_INSN_P (insn))
5085 continue;
5086
5087 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
5088 a call argument containing a TLS address that itself requires
5089 a call to __tls_get_addr. The handling of stack_pointer_delta
5090 in emit_single_push_insn is supposed to ensure that any such
5091 notes are already correct. */
5092 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
5093 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
5094
5095 poly_int64 this_delta = find_args_size_adjust (insn);
5096 if (known_eq (this_delta, 0))
5097 {
5098 if (!CALL_P (insn)
5099 || ACCUMULATE_OUTGOING_ARGS
5100 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
5101 continue;
5102 }
5103
5104 gcc_assert (!saw_unknown);
5105 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
5106 saw_unknown = true;
5107
5108 if (!note)
5109 add_args_size_note (insn, args_size);
5110 if (STACK_GROWS_DOWNWARD)
5111 this_delta = -poly_uint64 (this_delta);
5112
5113 if (saw_unknown)
5114 args_size = HOST_WIDE_INT_MIN;
5115 else
5116 args_size -= this_delta;
5117 }
5118
5119 return args_size;
5120 }
5121
5122 #ifdef PUSH_ROUNDING
5123 /* Emit single push insn. */
5124
5125 static void
5126 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
5127 {
5128 rtx dest_addr;
5129 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
5130 rtx dest;
5131 enum insn_code icode;
5132
5133 /* If there is push pattern, use it. Otherwise try old way of throwing
5134 MEM representing push operation to move expander. */
5135 icode = optab_handler (push_optab, mode);
5136 if (icode != CODE_FOR_nothing)
5137 {
5138 class expand_operand ops[1];
5139
5140 create_input_operand (&ops[0], x, mode);
5141 if (maybe_expand_insn (icode, 1, ops))
5142 return;
5143 }
5144 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
5145 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
5146 /* If we are to pad downward, adjust the stack pointer first and
5147 then store X into the stack location using an offset. This is
5148 because emit_move_insn does not know how to pad; it does not have
5149 access to type. */
5150 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
5151 {
5152 emit_move_insn (stack_pointer_rtx,
5153 expand_binop (Pmode,
5154 STACK_GROWS_DOWNWARD ? sub_optab
5155 : add_optab,
5156 stack_pointer_rtx,
5157 gen_int_mode (rounded_size, Pmode),
5158 NULL_RTX, 0, OPTAB_LIB_WIDEN));
5159
5160 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
5161 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
5162 /* We have already decremented the stack pointer, so get the
5163 previous value. */
5164 offset += rounded_size;
5165
5166 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
5167 /* We have already incremented the stack pointer, so get the
5168 previous value. */
5169 offset -= rounded_size;
5170
5171 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
5172 }
5173 else
5174 {
5175 if (STACK_GROWS_DOWNWARD)
5176 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
5177 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
5178 else
5179 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
5180 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
5181
5182 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
5183 }
5184
5185 dest = gen_rtx_MEM (mode, dest_addr);
5186
5187 if (type != 0)
5188 {
5189 set_mem_attributes (dest, type, 1);
5190
5191 if (cfun->tail_call_marked)
5192 /* Function incoming arguments may overlap with sibling call
5193 outgoing arguments and we cannot allow reordering of reads
5194 from function arguments with stores to outgoing arguments
5195 of sibling calls. */
5196 set_mem_alias_set (dest, 0);
5197 }
5198 emit_move_insn (dest, x);
5199 }
5200
5201 /* Emit and annotate a single push insn. */
5202
5203 static void
5204 emit_single_push_insn (machine_mode mode, rtx x, tree type)
5205 {
5206 poly_int64 delta, old_delta = stack_pointer_delta;
5207 rtx_insn *prev = get_last_insn ();
5208 rtx_insn *last;
5209
5210 emit_single_push_insn_1 (mode, x, type);
5211
5212 /* Adjust stack_pointer_delta to describe the situation after the push
5213 we just performed. Note that we must do this after the push rather
5214 than before the push in case calculating X needs pushes and pops of
5215 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
5216 for such pushes and pops must not include the effect of the future
5217 push of X. */
5218 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
5219
5220 last = get_last_insn ();
5221
5222 /* Notice the common case where we emitted exactly one insn. */
5223 if (PREV_INSN (last) == prev)
5224 {
5225 add_args_size_note (last, stack_pointer_delta);
5226 return;
5227 }
5228
5229 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
5230 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
5231 || known_eq (delta, old_delta));
5232 }
5233 #endif
5234
5235 /* If reading SIZE bytes from X will end up reading from
5236 Y return the number of bytes that overlap. Return -1
5237 if there is no overlap or -2 if we can't determine
5238 (for example when X and Y have different base registers). */
5239
5240 static int
5241 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
5242 {
5243 rtx tmp = plus_constant (Pmode, x, size);
5244 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
5245
5246 if (!CONST_INT_P (sub))
5247 return -2;
5248
5249 HOST_WIDE_INT val = INTVAL (sub);
5250
5251 return IN_RANGE (val, 1, size) ? val : -1;
5252 }
5253
5254 /* Generate code to push X onto the stack, assuming it has mode MODE and
5255 type TYPE.
5256 MODE is redundant except when X is a CONST_INT (since they don't
5257 carry mode info).
5258 SIZE is an rtx for the size of data to be copied (in bytes),
5259 needed only if X is BLKmode.
5260 Return true if successful. May return false if asked to push a
5261 partial argument during a sibcall optimization (as specified by
5262 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
5263 to not overlap.
5264
5265 ALIGN (in bits) is maximum alignment we can assume.
5266
5267 If PARTIAL and REG are both nonzero, then copy that many of the first
5268 bytes of X into registers starting with REG, and push the rest of X.
5269 The amount of space pushed is decreased by PARTIAL bytes.
5270 REG must be a hard register in this case.
5271 If REG is zero but PARTIAL is not, take any all others actions for an
5272 argument partially in registers, but do not actually load any
5273 registers.
5274
5275 EXTRA is the amount in bytes of extra space to leave next to this arg.
5276 This is ignored if an argument block has already been allocated.
5277
5278 On a machine that lacks real push insns, ARGS_ADDR is the address of
5279 the bottom of the argument block for this call. We use indexing off there
5280 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
5281 argument block has not been preallocated.
5282
5283 ARGS_SO_FAR is the size of args previously pushed for this call.
5284
5285 REG_PARM_STACK_SPACE is nonzero if functions require stack space
5286 for arguments passed in registers. If nonzero, it will be the number
5287 of bytes required. */
5288
5289 bool
5290 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
5291 unsigned int align, int partial, rtx reg, poly_int64 extra,
5292 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
5293 rtx alignment_pad, bool sibcall_p)
5294 {
5295 rtx xinner;
5296 pad_direction stack_direction
5297 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
5298
5299 /* Decide where to pad the argument: PAD_DOWNWARD for below,
5300 PAD_UPWARD for above, or PAD_NONE for don't pad it.
5301 Default is below for small data on big-endian machines; else above. */
5302 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
5303
5304 /* Invert direction if stack is post-decrement.
5305 FIXME: why? */
5306 if (STACK_PUSH_CODE == POST_DEC)
5307 if (where_pad != PAD_NONE)
5308 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
5309
5310 xinner = x;
5311
5312 int nregs = partial / UNITS_PER_WORD;
5313 rtx *tmp_regs = NULL;
5314 int overlapping = 0;
5315
5316 if (mode == BLKmode
5317 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
5318 {
5319 /* Copy a block into the stack, entirely or partially. */
5320
5321 rtx temp;
5322 int used;
5323 int offset;
5324 int skip;
5325
5326 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5327 used = partial - offset;
5328
5329 if (mode != BLKmode)
5330 {
5331 /* A value is to be stored in an insufficiently aligned
5332 stack slot; copy via a suitably aligned slot if
5333 necessary. */
5334 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
5335 if (!MEM_P (xinner))
5336 {
5337 temp = assign_temp (type, 1, 1);
5338 emit_move_insn (temp, xinner);
5339 xinner = temp;
5340 }
5341 }
5342
5343 gcc_assert (size);
5344
5345 /* USED is now the # of bytes we need not copy to the stack
5346 because registers will take care of them. */
5347
5348 if (partial != 0)
5349 xinner = adjust_address (xinner, BLKmode, used);
5350
5351 /* If the partial register-part of the arg counts in its stack size,
5352 skip the part of stack space corresponding to the registers.
5353 Otherwise, start copying to the beginning of the stack space,
5354 by setting SKIP to 0. */
5355 skip = (reg_parm_stack_space == 0) ? 0 : used;
5356
5357 #ifdef PUSH_ROUNDING
5358 /* NB: Let the backend known the number of bytes to push and
5359 decide if push insns should be generated. */
5360 unsigned int push_size;
5361 if (CONST_INT_P (size))
5362 push_size = INTVAL (size);
5363 else
5364 push_size = 0;
5365
5366 /* Do it with several push insns if that doesn't take lots of insns
5367 and if there is no difficulty with push insns that skip bytes
5368 on the stack for alignment purposes. */
5369 if (args_addr == 0
5370 && targetm.calls.push_argument (push_size)
5371 && CONST_INT_P (size)
5372 && skip == 0
5373 && MEM_ALIGN (xinner) >= align
5374 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
5375 /* Here we avoid the case of a structure whose weak alignment
5376 forces many pushes of a small amount of data,
5377 and such small pushes do rounding that causes trouble. */
5378 && ((!targetm.slow_unaligned_access (word_mode, align))
5379 || align >= BIGGEST_ALIGNMENT
5380 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
5381 align / BITS_PER_UNIT))
5382 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
5383 {
5384 /* Push padding now if padding above and stack grows down,
5385 or if padding below and stack grows up.
5386 But if space already allocated, this has already been done. */
5387 if (maybe_ne (extra, 0)
5388 && args_addr == 0
5389 && where_pad != PAD_NONE
5390 && where_pad != stack_direction)
5391 anti_adjust_stack (gen_int_mode (extra, Pmode));
5392
5393 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
5394 RETURN_BEGIN);
5395 }
5396 else
5397 #endif /* PUSH_ROUNDING */
5398 {
5399 rtx target;
5400
5401 /* Otherwise make space on the stack and copy the data
5402 to the address of that space. */
5403
5404 /* Deduct words put into registers from the size we must copy. */
5405 if (partial != 0)
5406 {
5407 if (CONST_INT_P (size))
5408 size = GEN_INT (INTVAL (size) - used);
5409 else
5410 size = expand_binop (GET_MODE (size), sub_optab, size,
5411 gen_int_mode (used, GET_MODE (size)),
5412 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5413 }
5414
5415 /* Get the address of the stack space.
5416 In this case, we do not deal with EXTRA separately.
5417 A single stack adjust will do. */
5418 poly_int64 const_args_so_far;
5419 if (! args_addr)
5420 {
5421 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5422 extra = 0;
5423 }
5424 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5425 temp = memory_address (BLKmode,
5426 plus_constant (Pmode, args_addr,
5427 skip + const_args_so_far));
5428 else
5429 temp = memory_address (BLKmode,
5430 plus_constant (Pmode,
5431 gen_rtx_PLUS (Pmode,
5432 args_addr,
5433 args_so_far),
5434 skip));
5435
5436 if (!ACCUMULATE_OUTGOING_ARGS)
5437 {
5438 /* If the source is referenced relative to the stack pointer,
5439 copy it to another register to stabilize it. We do not need
5440 to do this if we know that we won't be changing sp. */
5441
5442 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5443 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5444 temp = copy_to_reg (temp);
5445 }
5446
5447 target = gen_rtx_MEM (BLKmode, temp);
5448
5449 /* We do *not* set_mem_attributes here, because incoming arguments
5450 may overlap with sibling call outgoing arguments and we cannot
5451 allow reordering of reads from function arguments with stores
5452 to outgoing arguments of sibling calls. We do, however, want
5453 to record the alignment of the stack slot. */
5454 /* ALIGN may well be better aligned than TYPE, e.g. due to
5455 PARM_BOUNDARY. Assume the caller isn't lying. */
5456 set_mem_align (target, align);
5457
5458 /* If part should go in registers and pushing to that part would
5459 overwrite some of the values that need to go into regs, load the
5460 overlapping values into temporary pseudos to be moved into the hard
5461 regs at the end after the stack pushing has completed.
5462 We cannot load them directly into the hard regs here because
5463 they can be clobbered by the block move expansions.
5464 See PR 65358. */
5465
5466 if (partial > 0 && reg != 0 && mode == BLKmode
5467 && GET_CODE (reg) != PARALLEL)
5468 {
5469 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5470 if (overlapping > 0)
5471 {
5472 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5473 overlapping /= UNITS_PER_WORD;
5474
5475 tmp_regs = XALLOCAVEC (rtx, overlapping);
5476
5477 for (int i = 0; i < overlapping; i++)
5478 tmp_regs[i] = gen_reg_rtx (word_mode);
5479
5480 for (int i = 0; i < overlapping; i++)
5481 emit_move_insn (tmp_regs[i],
5482 operand_subword_force (target, i, mode));
5483 }
5484 else if (overlapping == -1)
5485 overlapping = 0;
5486 /* Could not determine whether there is overlap.
5487 Fail the sibcall. */
5488 else
5489 {
5490 overlapping = 0;
5491 if (sibcall_p)
5492 return false;
5493 }
5494 }
5495
5496 /* If source is a constant VAR_DECL with a simple constructor,
5497 store the constructor to the stack instead of moving it. */
5498 const_tree decl;
5499 HOST_WIDE_INT sz;
5500 if (partial == 0
5501 && MEM_P (xinner)
5502 && SYMBOL_REF_P (XEXP (xinner, 0))
5503 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5504 && VAR_P (decl)
5505 && TREE_READONLY (decl)
5506 && !TREE_SIDE_EFFECTS (decl)
5507 && immediate_const_ctor_p (DECL_INITIAL (decl), 2)
5508 && (sz = int_expr_size (DECL_INITIAL (decl))) > 0
5509 && CONST_INT_P (size)
5510 && INTVAL (size) == sz)
5511 store_constructor (DECL_INITIAL (decl), target, 0, sz, false);
5512 else
5513 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5514 }
5515 }
5516 else if (partial > 0)
5517 {
5518 /* Scalar partly in registers. This case is only supported
5519 for fixed-wdth modes. */
5520 int num_words = GET_MODE_SIZE (mode).to_constant ();
5521 num_words /= UNITS_PER_WORD;
5522 int i;
5523 int not_stack;
5524 /* # bytes of start of argument
5525 that we must make space for but need not store. */
5526 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5527 int args_offset = INTVAL (args_so_far);
5528 int skip;
5529
5530 /* Push padding now if padding above and stack grows down,
5531 or if padding below and stack grows up.
5532 But if space already allocated, this has already been done. */
5533 if (maybe_ne (extra, 0)
5534 && args_addr == 0
5535 && where_pad != PAD_NONE
5536 && where_pad != stack_direction)
5537 anti_adjust_stack (gen_int_mode (extra, Pmode));
5538
5539 /* If we make space by pushing it, we might as well push
5540 the real data. Otherwise, we can leave OFFSET nonzero
5541 and leave the space uninitialized. */
5542 if (args_addr == 0)
5543 offset = 0;
5544
5545 /* Now NOT_STACK gets the number of words that we don't need to
5546 allocate on the stack. Convert OFFSET to words too. */
5547 not_stack = (partial - offset) / UNITS_PER_WORD;
5548 offset /= UNITS_PER_WORD;
5549
5550 /* If the partial register-part of the arg counts in its stack size,
5551 skip the part of stack space corresponding to the registers.
5552 Otherwise, start copying to the beginning of the stack space,
5553 by setting SKIP to 0. */
5554 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5555
5556 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5557 x = validize_mem (force_const_mem (mode, x));
5558
5559 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5560 SUBREGs of such registers are not allowed. */
5561 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5562 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5563 x = copy_to_reg (x);
5564
5565 /* Loop over all the words allocated on the stack for this arg. */
5566 /* We can do it by words, because any scalar bigger than a word
5567 has a size a multiple of a word. */
5568 tree word_mode_type = lang_hooks.types.type_for_mode (word_mode, 1);
5569 for (i = num_words - 1; i >= not_stack; i--)
5570 if (i >= not_stack + offset)
5571 if (!emit_push_insn (operand_subword_force (x, i, mode),
5572 word_mode, word_mode_type, NULL_RTX, align, 0,
5573 NULL_RTX, 0, args_addr,
5574 GEN_INT (args_offset + ((i - not_stack + skip)
5575 * UNITS_PER_WORD)),
5576 reg_parm_stack_space, alignment_pad, sibcall_p))
5577 return false;
5578 }
5579 else
5580 {
5581 rtx addr;
5582 rtx dest;
5583
5584 /* Push padding now if padding above and stack grows down,
5585 or if padding below and stack grows up.
5586 But if space already allocated, this has already been done. */
5587 if (maybe_ne (extra, 0)
5588 && args_addr == 0
5589 && where_pad != PAD_NONE
5590 && where_pad != stack_direction)
5591 anti_adjust_stack (gen_int_mode (extra, Pmode));
5592
5593 #ifdef PUSH_ROUNDING
5594 if (args_addr == 0 && targetm.calls.push_argument (0))
5595 emit_single_push_insn (mode, x, type);
5596 else
5597 #endif
5598 {
5599 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5600 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5601
5602 /* We do *not* set_mem_attributes here, because incoming arguments
5603 may overlap with sibling call outgoing arguments and we cannot
5604 allow reordering of reads from function arguments with stores
5605 to outgoing arguments of sibling calls. We do, however, want
5606 to record the alignment of the stack slot. */
5607 /* ALIGN may well be better aligned than TYPE, e.g. due to
5608 PARM_BOUNDARY. Assume the caller isn't lying. */
5609 set_mem_align (dest, align);
5610
5611 emit_move_insn (dest, x);
5612 }
5613 }
5614
5615 /* Move the partial arguments into the registers and any overlapping
5616 values that we moved into the pseudos in tmp_regs. */
5617 if (partial > 0 && reg != 0)
5618 {
5619 /* Handle calls that pass values in multiple non-contiguous locations.
5620 The Irix 6 ABI has examples of this. */
5621 if (GET_CODE (reg) == PARALLEL)
5622 emit_group_load (reg, x, type, -1);
5623 else
5624 {
5625 gcc_assert (partial % UNITS_PER_WORD == 0);
5626 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5627
5628 for (int i = 0; i < overlapping; i++)
5629 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5630 + nregs - overlapping + i),
5631 tmp_regs[i]);
5632
5633 }
5634 }
5635
5636 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5637 anti_adjust_stack (gen_int_mode (extra, Pmode));
5638
5639 if (alignment_pad && args_addr == 0)
5640 anti_adjust_stack (alignment_pad);
5641
5642 return true;
5643 }
5644
5645 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5646 operations. */
5647
5648 static rtx
5649 get_subtarget (rtx x)
5650 {
5651 return (optimize
5652 || x == 0
5653 /* Only registers can be subtargets. */
5654 || !REG_P (x)
5655 /* Don't use hard regs to avoid extending their life. */
5656 || REGNO (x) < FIRST_PSEUDO_REGISTER
5657 ? 0 : x);
5658 }
5659
5660 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5661 FIELD is a bitfield. Returns true if the optimization was successful,
5662 and there's nothing else to do. */
5663
5664 static bool
5665 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5666 poly_uint64 pbitpos,
5667 poly_uint64 pbitregion_start,
5668 poly_uint64 pbitregion_end,
5669 machine_mode mode1, rtx str_rtx,
5670 tree to, tree src, bool reverse)
5671 {
5672 /* str_mode is not guaranteed to be a scalar type. */
5673 machine_mode str_mode = GET_MODE (str_rtx);
5674 unsigned int str_bitsize;
5675 tree op0, op1;
5676 rtx value, result;
5677 optab binop;
5678 gimple *srcstmt;
5679 enum tree_code code;
5680
5681 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5682 if (mode1 != VOIDmode
5683 || !pbitsize.is_constant (&bitsize)
5684 || !pbitpos.is_constant (&bitpos)
5685 || !pbitregion_start.is_constant (&bitregion_start)
5686 || !pbitregion_end.is_constant (&bitregion_end)
5687 || bitsize >= BITS_PER_WORD
5688 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5689 || str_bitsize > BITS_PER_WORD
5690 || TREE_SIDE_EFFECTS (to)
5691 || TREE_THIS_VOLATILE (to))
5692 return false;
5693
5694 STRIP_NOPS (src);
5695 if (TREE_CODE (src) != SSA_NAME)
5696 return false;
5697 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5698 return false;
5699
5700 srcstmt = get_gimple_for_ssa_name (src);
5701 if (!srcstmt
5702 || !is_gimple_assign (srcstmt)
5703 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5704 return false;
5705
5706 code = gimple_assign_rhs_code (srcstmt);
5707
5708 op0 = gimple_assign_rhs1 (srcstmt);
5709
5710 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5711 to find its initialization. Hopefully the initialization will
5712 be from a bitfield load. */
5713 if (TREE_CODE (op0) == SSA_NAME)
5714 {
5715 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5716
5717 /* We want to eventually have OP0 be the same as TO, which
5718 should be a bitfield. */
5719 if (!op0stmt
5720 || !is_gimple_assign (op0stmt)
5721 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5722 return false;
5723 op0 = gimple_assign_rhs1 (op0stmt);
5724 }
5725
5726 op1 = gimple_assign_rhs2 (srcstmt);
5727
5728 if (!operand_equal_p (to, op0, 0))
5729 return false;
5730
5731 if (MEM_P (str_rtx))
5732 {
5733 unsigned HOST_WIDE_INT offset1;
5734
5735 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5736 str_bitsize = BITS_PER_WORD;
5737
5738 scalar_int_mode best_mode;
5739 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5740 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5741 return false;
5742 str_mode = best_mode;
5743 str_bitsize = GET_MODE_BITSIZE (best_mode);
5744
5745 offset1 = bitpos;
5746 bitpos %= str_bitsize;
5747 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5748 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5749 }
5750 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5751 return false;
5752
5753 /* If the bit field covers the whole REG/MEM, store_field
5754 will likely generate better code. */
5755 if (bitsize >= str_bitsize)
5756 return false;
5757
5758 /* We can't handle fields split across multiple entities. */
5759 if (bitpos + bitsize > str_bitsize)
5760 return false;
5761
5762 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5763 bitpos = str_bitsize - bitpos - bitsize;
5764
5765 switch (code)
5766 {
5767 case PLUS_EXPR:
5768 case MINUS_EXPR:
5769 /* For now, just optimize the case of the topmost bitfield
5770 where we don't need to do any masking and also
5771 1 bit bitfields where xor can be used.
5772 We might win by one instruction for the other bitfields
5773 too if insv/extv instructions aren't used, so that
5774 can be added later. */
5775 if ((reverse || bitpos + bitsize != str_bitsize)
5776 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5777 break;
5778
5779 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5780 value = convert_modes (str_mode,
5781 TYPE_MODE (TREE_TYPE (op1)), value,
5782 TYPE_UNSIGNED (TREE_TYPE (op1)));
5783
5784 /* We may be accessing data outside the field, which means
5785 we can alias adjacent data. */
5786 if (MEM_P (str_rtx))
5787 {
5788 str_rtx = shallow_copy_rtx (str_rtx);
5789 set_mem_alias_set (str_rtx, 0);
5790 set_mem_expr (str_rtx, 0);
5791 }
5792
5793 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5794 {
5795 value = expand_and (str_mode, value, const1_rtx, NULL);
5796 binop = xor_optab;
5797 }
5798 else
5799 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5800
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;
5809
5810 case BIT_IOR_EXPR:
5811 case BIT_XOR_EXPR:
5812 if (TREE_CODE (op1) != INTEGER_CST)
5813 break;
5814 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5815 value = convert_modes (str_mode,
5816 TYPE_MODE (TREE_TYPE (op1)), value,
5817 TYPE_UNSIGNED (TREE_TYPE (op1)));
5818
5819 /* We may be accessing data outside the field, which means
5820 we can alias adjacent data. */
5821 if (MEM_P (str_rtx))
5822 {
5823 str_rtx = shallow_copy_rtx (str_rtx);
5824 set_mem_alias_set (str_rtx, 0);
5825 set_mem_expr (str_rtx, 0);
5826 }
5827
5828 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5829 if (bitpos + bitsize != str_bitsize)
5830 {
5831 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5832 str_mode);
5833 value = expand_and (str_mode, value, mask, NULL_RTX);
5834 }
5835 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5836 if (reverse)
5837 value = flip_storage_order (str_mode, value);
5838 result = expand_binop (str_mode, binop, str_rtx,
5839 value, str_rtx, 1, OPTAB_WIDEN);
5840 if (result != str_rtx)
5841 emit_move_insn (str_rtx, result);
5842 return true;
5843
5844 default:
5845 break;
5846 }
5847
5848 return false;
5849 }
5850
5851 /* In the C++ memory model, consecutive bit fields in a structure are
5852 considered one memory location.
5853
5854 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5855 returns the bit range of consecutive bits in which this COMPONENT_REF
5856 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5857 and *OFFSET may be adjusted in the process.
5858
5859 If the access does not need to be restricted, 0 is returned in both
5860 *BITSTART and *BITEND. */
5861
5862 void
5863 get_bit_range (poly_uint64 *bitstart, poly_uint64 *bitend, tree exp,
5864 poly_int64 *bitpos, tree *offset)
5865 {
5866 poly_int64 bitoffset;
5867 tree field, repr;
5868
5869 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5870
5871 field = TREE_OPERAND (exp, 1);
5872 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5873 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5874 need to limit the range we can access. */
5875 if (!repr)
5876 {
5877 *bitstart = *bitend = 0;
5878 return;
5879 }
5880
5881 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5882 part of a larger bit field, then the representative does not serve any
5883 useful purpose. This can occur in Ada. */
5884 if (handled_component_p (TREE_OPERAND (exp, 0)))
5885 {
5886 machine_mode rmode;
5887 poly_int64 rbitsize, rbitpos;
5888 tree roffset;
5889 int unsignedp, reversep, volatilep = 0;
5890 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5891 &roffset, &rmode, &unsignedp, &reversep,
5892 &volatilep);
5893 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5894 {
5895 *bitstart = *bitend = 0;
5896 return;
5897 }
5898 }
5899
5900 /* Compute the adjustment to bitpos from the offset of the field
5901 relative to the representative. DECL_FIELD_OFFSET of field and
5902 repr are the same by construction if they are not constants,
5903 see finish_bitfield_layout. */
5904 poly_uint64 field_offset, repr_offset;
5905 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5906 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5907 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5908 else
5909 bitoffset = 0;
5910 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5911 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5912
5913 /* If the adjustment is larger than bitpos, we would have a negative bit
5914 position for the lower bound and this may wreak havoc later. Adjust
5915 offset and bitpos to make the lower bound non-negative in that case. */
5916 if (maybe_gt (bitoffset, *bitpos))
5917 {
5918 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5919 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5920
5921 *bitpos += adjust_bits;
5922 if (*offset == NULL_TREE)
5923 *offset = size_int (-adjust_bytes);
5924 else
5925 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5926 *bitstart = 0;
5927 }
5928 else
5929 *bitstart = *bitpos - bitoffset;
5930
5931 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5932 }
5933
5934 /* Returns true if BASE is a DECL that does not reside in memory and
5935 has non-BLKmode. DECL_RTL must not be a MEM; if
5936 DECL_RTL was not set yet, return false. */
5937
5938 bool
5939 non_mem_decl_p (tree base)
5940 {
5941 if (!DECL_P (base)
5942 || TREE_ADDRESSABLE (base)
5943 || DECL_MODE (base) == BLKmode)
5944 return false;
5945
5946 if (!DECL_RTL_SET_P (base))
5947 return false;
5948
5949 return (!MEM_P (DECL_RTL (base)));
5950 }
5951
5952 /* Returns true if REF refers to an object that does not
5953 reside in memory and has non-BLKmode. */
5954
5955 bool
5956 mem_ref_refers_to_non_mem_p (tree ref)
5957 {
5958 tree base;
5959
5960 if (TREE_CODE (ref) == MEM_REF
5961 || TREE_CODE (ref) == TARGET_MEM_REF)
5962 {
5963 tree addr = TREE_OPERAND (ref, 0);
5964
5965 if (TREE_CODE (addr) != ADDR_EXPR)
5966 return false;
5967
5968 base = TREE_OPERAND (addr, 0);
5969 }
5970 else
5971 base = ref;
5972
5973 return non_mem_decl_p (base);
5974 }
5975
5976 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5977 is true, try generating a nontemporal store. */
5978
5979 void
5980 expand_assignment (tree to, tree from, bool nontemporal)
5981 {
5982 rtx to_rtx = 0;
5983 rtx result;
5984 machine_mode mode;
5985 unsigned int align;
5986 enum insn_code icode;
5987
5988 /* Don't crash if the lhs of the assignment was erroneous. */
5989 if (TREE_CODE (to) == ERROR_MARK)
5990 {
5991 expand_normal (from);
5992 return;
5993 }
5994
5995 /* Optimize away no-op moves without side-effects. */
5996 if (operand_equal_p (to, from, 0))
5997 return;
5998
5999 /* Handle misaligned stores. */
6000 mode = TYPE_MODE (TREE_TYPE (to));
6001 if ((TREE_CODE (to) == MEM_REF
6002 || TREE_CODE (to) == TARGET_MEM_REF
6003 || DECL_P (to))
6004 && mode != BLKmode
6005 && !mem_ref_refers_to_non_mem_p (to)
6006 && ((align = get_object_alignment (to))
6007 < GET_MODE_ALIGNMENT (mode))
6008 && (((icode = optab_handler (movmisalign_optab, mode))
6009 != CODE_FOR_nothing)
6010 || targetm.slow_unaligned_access (mode, align)))
6011 {
6012 rtx reg, mem;
6013
6014 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6015 /* Handle PARALLEL. */
6016 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
6017 reg = force_not_mem (reg);
6018 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6019 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
6020 reg = flip_storage_order (mode, reg);
6021
6022 if (icode != CODE_FOR_nothing)
6023 {
6024 class expand_operand ops[2];
6025
6026 create_fixed_operand (&ops[0], mem);
6027 create_input_operand (&ops[1], reg, mode);
6028 /* The movmisalign<mode> pattern cannot fail, else the assignment
6029 would silently be omitted. */
6030 expand_insn (icode, 2, ops);
6031 }
6032 else
6033 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
6034 false, false);
6035 return;
6036 }
6037
6038 /* Assignment of a structure component needs special treatment
6039 if the structure component's rtx is not simply a MEM.
6040 Assignment of an array element at a constant index, and assignment of
6041 an array element in an unaligned packed structure field, has the same
6042 problem. Same for (partially) storing into a non-memory object. */
6043 if (handled_component_p (to)
6044 || (TREE_CODE (to) == MEM_REF
6045 && (REF_REVERSE_STORAGE_ORDER (to)
6046 || mem_ref_refers_to_non_mem_p (to)))
6047 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
6048 {
6049 machine_mode mode1;
6050 poly_int64 bitsize, bitpos;
6051 poly_uint64 bitregion_start = 0;
6052 poly_uint64 bitregion_end = 0;
6053 tree offset;
6054 int unsignedp, reversep, volatilep = 0;
6055 tree tem;
6056
6057 push_temp_slots ();
6058 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
6059 &unsignedp, &reversep, &volatilep);
6060
6061 /* Make sure bitpos is not negative, it can wreak havoc later. */
6062 if (maybe_lt (bitpos, 0))
6063 {
6064 gcc_assert (offset == NULL_TREE);
6065 offset = size_int (bits_to_bytes_round_down (bitpos));
6066 bitpos = num_trailing_bits (bitpos);
6067 }
6068
6069 if (TREE_CODE (to) == COMPONENT_REF
6070 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
6071 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
6072 /* The C++ memory model naturally applies to byte-aligned fields.
6073 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
6074 BITSIZE are not byte-aligned, there is no need to limit the range
6075 we can access. This can occur with packed structures in Ada. */
6076 else if (maybe_gt (bitsize, 0)
6077 && multiple_p (bitsize, BITS_PER_UNIT)
6078 && multiple_p (bitpos, BITS_PER_UNIT))
6079 {
6080 bitregion_start = bitpos;
6081 bitregion_end = bitpos + bitsize - 1;
6082 }
6083
6084 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
6085
6086 /* If the field has a mode, we want to access it in the
6087 field's mode, not the computed mode.
6088 If a MEM has VOIDmode (external with incomplete type),
6089 use BLKmode for it instead. */
6090 if (MEM_P (to_rtx))
6091 {
6092 if (mode1 != VOIDmode)
6093 to_rtx = adjust_address (to_rtx, mode1, 0);
6094 else if (GET_MODE (to_rtx) == VOIDmode)
6095 to_rtx = adjust_address (to_rtx, BLKmode, 0);
6096 }
6097
6098 rtx stemp = NULL_RTX, old_to_rtx = NULL_RTX;
6099 if (offset != 0)
6100 {
6101 machine_mode address_mode;
6102 rtx offset_rtx;
6103
6104 if (!MEM_P (to_rtx))
6105 {
6106 /* We can get constant negative offsets into arrays with broken
6107 user code. Translate this to a trap instead of ICEing. */
6108 if (TREE_CODE (offset) == INTEGER_CST)
6109 {
6110 expand_builtin_trap ();
6111 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
6112 }
6113 /* Else spill for variable offset to the destination. We expect
6114 to run into this only for hard registers. */
6115 else
6116 {
6117 gcc_assert (VAR_P (tem) && DECL_HARD_REGISTER (tem));
6118 stemp = assign_stack_temp (GET_MODE (to_rtx),
6119 GET_MODE_SIZE (GET_MODE (to_rtx)));
6120 emit_move_insn (stemp, to_rtx);
6121 old_to_rtx = to_rtx;
6122 to_rtx = stemp;
6123 }
6124 }
6125
6126 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
6127 address_mode = get_address_mode (to_rtx);
6128 if (GET_MODE (offset_rtx) != address_mode)
6129 {
6130 /* We cannot be sure that the RTL in offset_rtx is valid outside
6131 of a memory address context, so force it into a register
6132 before attempting to convert it to the desired mode. */
6133 offset_rtx = force_operand (offset_rtx, NULL_RTX);
6134 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6135 }
6136
6137 /* If we have an expression in OFFSET_RTX and a non-zero
6138 byte offset in BITPOS, adding the byte offset before the
6139 OFFSET_RTX results in better intermediate code, which makes
6140 later rtl optimization passes perform better.
6141
6142 We prefer intermediate code like this:
6143
6144 r124:DI=r123:DI+0x18
6145 [r124:DI]=r121:DI
6146
6147 ... instead of ...
6148
6149 r124:DI=r123:DI+0x10
6150 [r124:DI+0x8]=r121:DI
6151
6152 This is only done for aligned data values, as these can
6153 be expected to result in single move instructions. */
6154 poly_int64 bytepos;
6155 if (mode1 != VOIDmode
6156 && maybe_ne (bitpos, 0)
6157 && maybe_gt (bitsize, 0)
6158 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6159 && multiple_p (bitpos, bitsize)
6160 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
6161 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
6162 {
6163 to_rtx = adjust_address (to_rtx, mode1, bytepos);
6164 bitregion_start = 0;
6165 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
6166 bitregion_end -= bitpos;
6167 bitpos = 0;
6168 }
6169
6170 to_rtx = offset_address (to_rtx, offset_rtx,
6171 highest_pow2_factor_for_target (to,
6172 offset));
6173 }
6174
6175 /* No action is needed if the target is not a memory and the field
6176 lies completely outside that target. This can occur if the source
6177 code contains an out-of-bounds access to a small array. */
6178 if (!MEM_P (to_rtx)
6179 && GET_MODE (to_rtx) != BLKmode
6180 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
6181 {
6182 expand_normal (from);
6183 result = NULL;
6184 }
6185 /* Handle expand_expr of a complex value returning a CONCAT. */
6186 else if (GET_CODE (to_rtx) == CONCAT)
6187 {
6188 machine_mode to_mode = GET_MODE (to_rtx);
6189 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
6190 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
6191 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
6192 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
6193 && known_eq (bitpos, 0)
6194 && known_eq (bitsize, mode_bitsize))
6195 result = store_expr (from, to_rtx, false, nontemporal, reversep);
6196 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
6197 && known_eq (bitsize, inner_bitsize)
6198 && (known_eq (bitpos, 0)
6199 || known_eq (bitpos, inner_bitsize)))
6200 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
6201 false, nontemporal, reversep);
6202 else if (known_le (bitpos + bitsize, inner_bitsize))
6203 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
6204 bitregion_start, bitregion_end,
6205 mode1, from, get_alias_set (to),
6206 nontemporal, reversep);
6207 else if (known_ge (bitpos, inner_bitsize))
6208 result = store_field (XEXP (to_rtx, 1), bitsize,
6209 bitpos - inner_bitsize,
6210 bitregion_start, bitregion_end,
6211 mode1, from, get_alias_set (to),
6212 nontemporal, reversep);
6213 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
6214 {
6215 result = expand_normal (from);
6216 if (GET_CODE (result) == CONCAT)
6217 {
6218 to_mode = GET_MODE_INNER (to_mode);
6219 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
6220 rtx from_real
6221 = force_subreg (to_mode, XEXP (result, 0), from_mode, 0);
6222 rtx from_imag
6223 = force_subreg (to_mode, XEXP (result, 1), from_mode, 0);
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);
6228 }
6229 else
6230 {
6231 machine_mode from_mode
6232 = GET_MODE (result) == VOIDmode
6233 ? TYPE_MODE (TREE_TYPE (from))
6234 : GET_MODE (result);
6235 rtx from_rtx;
6236 if (MEM_P (result))
6237 from_rtx = change_address (result, to_mode, NULL_RTX);
6238 else
6239 from_rtx = force_subreg (to_mode, result, from_mode, 0);
6240 if (from_rtx)
6241 {
6242 emit_move_insn (XEXP (to_rtx, 0),
6243 read_complex_part (from_rtx, false));
6244 emit_move_insn (XEXP (to_rtx, 1),
6245 read_complex_part (from_rtx, true));
6246 }
6247 else
6248 {
6249 to_mode = GET_MODE_INNER (to_mode);
6250 rtx from_real
6251 = force_subreg (to_mode, result, from_mode, 0);
6252 rtx from_imag
6253 = force_subreg (to_mode, result, from_mode,
6254 GET_MODE_SIZE (to_mode));
6255 if (!from_real || !from_imag)
6256 goto concat_store_slow;
6257 emit_move_insn (XEXP (to_rtx, 0), from_real);
6258 emit_move_insn (XEXP (to_rtx, 1), from_imag);
6259 }
6260 }
6261 }
6262 else
6263 {
6264 concat_store_slow:;
6265 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6266 GET_MODE_SIZE (GET_MODE (to_rtx)));
6267 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
6268 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
6269 result = store_field (temp, bitsize, bitpos,
6270 bitregion_start, bitregion_end,
6271 mode1, from, get_alias_set (to),
6272 nontemporal, reversep);
6273 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
6274 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
6275 }
6276 }
6277 /* For calls to functions returning variable length structures, if TO_RTX
6278 is not a MEM, go through a MEM because we must not create temporaries
6279 of the VLA type. */
6280 else if (!MEM_P (to_rtx)
6281 && TREE_CODE (from) == CALL_EXPR
6282 && COMPLETE_TYPE_P (TREE_TYPE (from))
6283 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
6284 {
6285 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
6286 GET_MODE_SIZE (GET_MODE (to_rtx)));
6287 result = store_field (temp, bitsize, bitpos, bitregion_start,
6288 bitregion_end, mode1, from, get_alias_set (to),
6289 nontemporal, reversep);
6290 emit_move_insn (to_rtx, temp);
6291 }
6292 else
6293 {
6294 if (MEM_P (to_rtx))
6295 {
6296 /* If the field is at offset zero, we could have been given the
6297 DECL_RTX of the parent struct. Don't munge it. */
6298 to_rtx = shallow_copy_rtx (to_rtx);
6299 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
6300 if (volatilep)
6301 MEM_VOLATILE_P (to_rtx) = 1;
6302 }
6303
6304 gcc_checking_assert (known_ge (bitpos, 0));
6305 if (optimize_bitfield_assignment_op (bitsize, bitpos,
6306 bitregion_start, bitregion_end,
6307 mode1, to_rtx, to, from,
6308 reversep))
6309 result = NULL;
6310 else if (SUBREG_P (to_rtx)
6311 && SUBREG_PROMOTED_VAR_P (to_rtx))
6312 {
6313 /* If to_rtx is a promoted subreg, we need to zero or sign
6314 extend the value afterwards. */
6315 if (TREE_CODE (to) == MEM_REF
6316 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
6317 && !REF_REVERSE_STORAGE_ORDER (to)
6318 && known_eq (bitpos, 0)
6319 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
6320 result = store_expr (from, to_rtx, 0, nontemporal, false);
6321 /* Check if the field overlaps the MSB, requiring extension. */
6322 else if (maybe_eq (bitpos + bitsize,
6323 GET_MODE_BITSIZE (GET_MODE (to_rtx))))
6324 {
6325 scalar_int_mode imode = subreg_unpromoted_mode (to_rtx);
6326 scalar_int_mode omode = subreg_promoted_mode (to_rtx);
6327 rtx to_rtx1 = lowpart_subreg (imode, SUBREG_REG (to_rtx),
6328 omode);
6329 result = store_field (to_rtx1, bitsize, bitpos,
6330 bitregion_start, bitregion_end,
6331 mode1, from, get_alias_set (to),
6332 nontemporal, reversep);
6333 /* If the target usually keeps IMODE appropriately
6334 extended in OMODE it's unsafe to refer to it using
6335 a SUBREG whilst this invariant doesn't hold. */
6336 if (targetm.mode_rep_extended (imode, omode) != UNKNOWN)
6337 to_rtx1 = simplify_gen_unary (TRUNCATE, imode,
6338 SUBREG_REG (to_rtx), omode);
6339 convert_move (SUBREG_REG (to_rtx), to_rtx1,
6340 SUBREG_PROMOTED_SIGN (to_rtx));
6341 }
6342 else
6343 result = store_field (to_rtx, bitsize, bitpos,
6344 bitregion_start, bitregion_end,
6345 mode1, from, get_alias_set (to),
6346 nontemporal, reversep);
6347 }
6348 else
6349 result = store_field (to_rtx, bitsize, bitpos,
6350 bitregion_start, bitregion_end,
6351 mode1, from, get_alias_set (to),
6352 nontemporal, reversep);
6353 /* Move the temporary storage back to the non-MEM_P. */
6354 if (stemp)
6355 emit_move_insn (old_to_rtx, stemp);
6356 }
6357
6358 if (result)
6359 preserve_temp_slots (result);
6360 pop_temp_slots ();
6361 return;
6362 }
6363
6364 /* If the rhs is a function call and its value is not an aggregate,
6365 call the function before we start to compute the lhs.
6366 This is needed for correct code for cases such as
6367 val = setjmp (buf) on machines where reference to val
6368 requires loading up part of an address in a separate insn.
6369
6370 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
6371 since it might be a promoted variable where the zero- or sign- extension
6372 needs to be done. Handling this in the normal way is safe because no
6373 computation is done before the call. The same is true for SSA names. */
6374 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
6375 && COMPLETE_TYPE_P (TREE_TYPE (from))
6376 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
6377 && ! (((VAR_P (to)
6378 || TREE_CODE (to) == PARM_DECL
6379 || TREE_CODE (to) == RESULT_DECL)
6380 && REG_P (DECL_RTL (to)))
6381 || TREE_CODE (to) == SSA_NAME))
6382 {
6383 rtx value;
6384
6385 push_temp_slots ();
6386 value = expand_normal (from);
6387
6388 if (to_rtx == 0)
6389 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6390
6391 /* Handle calls that return values in multiple non-contiguous locations.
6392 The Irix 6 ABI has examples of this. */
6393 if (GET_CODE (to_rtx) == PARALLEL)
6394 {
6395 if (GET_CODE (value) == PARALLEL)
6396 emit_group_move (to_rtx, value);
6397 else
6398 emit_group_load (to_rtx, value, TREE_TYPE (from),
6399 int_size_in_bytes (TREE_TYPE (from)));
6400 }
6401 else if (GET_CODE (value) == PARALLEL)
6402 emit_group_store (to_rtx, value, TREE_TYPE (from),
6403 int_size_in_bytes (TREE_TYPE (from)));
6404 else if (GET_MODE (to_rtx) == BLKmode)
6405 {
6406 /* Handle calls that return BLKmode values in registers. */
6407 if (REG_P (value))
6408 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
6409 else
6410 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
6411 }
6412 else
6413 {
6414 if (POINTER_TYPE_P (TREE_TYPE (to)))
6415 value = convert_memory_address_addr_space
6416 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
6417 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
6418
6419 emit_move_insn (to_rtx, value);
6420 }
6421
6422 preserve_temp_slots (to_rtx);
6423 pop_temp_slots ();
6424 return;
6425 }
6426
6427 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
6428 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
6429
6430 /* Don't move directly into a return register. */
6431 if (TREE_CODE (to) == RESULT_DECL
6432 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
6433 {
6434 rtx temp;
6435
6436 push_temp_slots ();
6437
6438 /* If the source is itself a return value, it still is in a pseudo at
6439 this point so we can move it back to the return register directly. */
6440 if (REG_P (to_rtx)
6441 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6442 && TREE_CODE (from) != CALL_EXPR)
6443 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6444 else
6445 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6446
6447 /* Handle calls that return values in multiple non-contiguous locations.
6448 The Irix 6 ABI has examples of this. */
6449 if (GET_CODE (to_rtx) == PARALLEL)
6450 {
6451 if (GET_CODE (temp) == PARALLEL)
6452 emit_group_move (to_rtx, temp);
6453 else
6454 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6455 int_size_in_bytes (TREE_TYPE (from)));
6456 }
6457 else if (temp)
6458 emit_move_insn (to_rtx, temp);
6459
6460 preserve_temp_slots (to_rtx);
6461 pop_temp_slots ();
6462 return;
6463 }
6464
6465 /* In case we are returning the contents of an object which overlaps
6466 the place the value is being stored, use a safe function when copying
6467 a value through a pointer into a structure value return block. */
6468 if (TREE_CODE (to) == RESULT_DECL
6469 && TREE_CODE (from) == INDIRECT_REF
6470 && ADDR_SPACE_GENERIC_P
6471 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6472 && refs_may_alias_p (to, from)
6473 && cfun->returns_struct
6474 && !cfun->returns_pcc_struct)
6475 {
6476 rtx from_rtx, size;
6477
6478 push_temp_slots ();
6479 size = expr_size (from);
6480 from_rtx = expand_normal (from);
6481
6482 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6483
6484 preserve_temp_slots (to_rtx);
6485 pop_temp_slots ();
6486 return;
6487 }
6488
6489 /* Compute FROM and store the value in the rtx we got. */
6490
6491 push_temp_slots ();
6492 result = store_expr (from, to_rtx, 0, nontemporal, false);
6493 preserve_temp_slots (result);
6494 pop_temp_slots ();
6495 return;
6496 }
6497
6498 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6499 succeeded, false otherwise. */
6500
6501 bool
6502 emit_storent_insn (rtx to, rtx from)
6503 {
6504 class expand_operand ops[2];
6505 machine_mode mode = GET_MODE (to);
6506 enum insn_code code = optab_handler (storent_optab, mode);
6507
6508 if (code == CODE_FOR_nothing)
6509 return false;
6510
6511 create_fixed_operand (&ops[0], to);
6512 create_input_operand (&ops[1], from, mode);
6513 return maybe_expand_insn (code, 2, ops);
6514 }
6515
6516 /* Helper function for store_expr storing of STRING_CST. */
6517
6518 static rtx
6519 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6520 fixed_size_mode mode)
6521 {
6522 tree str = (tree) data;
6523
6524 gcc_assert (offset >= 0);
6525 if (offset >= TREE_STRING_LENGTH (str))
6526 return const0_rtx;
6527
6528 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6529 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6530 {
6531 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6532 size_t l = TREE_STRING_LENGTH (str) - offset;
6533 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6534 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6535 return c_readstr (p, mode, false);
6536 }
6537
6538 return c_readstr (TREE_STRING_POINTER (str) + offset, mode, false);
6539 }
6540
6541 /* Helper function for store_expr storing of RAW_DATA_CST. */
6542
6543 static rtx
6544 raw_data_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6545 fixed_size_mode mode)
6546 {
6547 tree cst = (tree) data;
6548
6549 gcc_assert (offset >= 0);
6550 if (offset >= RAW_DATA_LENGTH (cst))
6551 return const0_rtx;
6552
6553 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6554 > (unsigned HOST_WIDE_INT) RAW_DATA_LENGTH (cst))
6555 {
6556 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6557 size_t l = RAW_DATA_LENGTH (cst) - offset;
6558 memcpy (p, RAW_DATA_POINTER (cst) + offset, l);
6559 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6560 return c_readstr (p, mode, false);
6561 }
6562
6563 return c_readstr (RAW_DATA_POINTER (cst) + offset, mode, false);
6564 }
6565
6566 /* Generate code for computing expression EXP,
6567 and storing the value into TARGET.
6568
6569 If the mode is BLKmode then we may return TARGET itself.
6570 It turns out that in BLKmode it doesn't cause a problem.
6571 because C has no operators that could combine two different
6572 assignments into the same BLKmode object with different values
6573 with no sequence point. Will other languages need this to
6574 be more thorough?
6575
6576 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6577 stack, and block moves may need to be treated specially.
6578
6579 If NONTEMPORAL is true, try using a nontemporal store instruction.
6580
6581 If REVERSE is true, the store is to be done in reverse order. */
6582
6583 rtx
6584 store_expr (tree exp, rtx target, int call_param_p,
6585 bool nontemporal, bool reverse)
6586 {
6587 rtx temp;
6588 rtx alt_rtl = NULL_RTX;
6589 location_t loc = curr_insn_location ();
6590 bool shortened_string_cst = false;
6591
6592 if (VOID_TYPE_P (TREE_TYPE (exp)))
6593 {
6594 /* C++ can generate ?: expressions with a throw expression in one
6595 branch and an rvalue in the other. Here, we resolve attempts to
6596 store the throw expression's nonexistent result. */
6597 gcc_assert (!call_param_p);
6598 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6599 return NULL_RTX;
6600 }
6601 if (TREE_CODE (exp) == COMPOUND_EXPR)
6602 {
6603 /* Perform first part of compound expression, then assign from second
6604 part. */
6605 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6606 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6607 return store_expr (TREE_OPERAND (exp, 1), target,
6608 call_param_p, nontemporal, reverse);
6609 }
6610 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6611 {
6612 /* For conditional expression, get safe form of the target. Then
6613 test the condition, doing the appropriate assignment on either
6614 side. This avoids the creation of unnecessary temporaries.
6615 For non-BLKmode, it is more efficient not to do this. */
6616
6617 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6618
6619 do_pending_stack_adjust ();
6620 NO_DEFER_POP;
6621 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6622 profile_probability::uninitialized ());
6623 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6624 nontemporal, reverse);
6625 emit_jump_insn (targetm.gen_jump (lab2));
6626 emit_barrier ();
6627 emit_label (lab1);
6628 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6629 nontemporal, reverse);
6630 emit_label (lab2);
6631 OK_DEFER_POP;
6632
6633 return NULL_RTX;
6634 }
6635 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6636 /* If this is a scalar in a register that is stored in a wider mode
6637 than the declared mode, compute the result into its declared mode
6638 and then convert to the wider mode. Our value is the computed
6639 expression. */
6640 {
6641 rtx inner_target = 0;
6642 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6643 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6644
6645 /* We can do the conversion inside EXP, which will often result
6646 in some optimizations. Do the conversion in two steps: first
6647 change the signedness, if needed, then the extend. But don't
6648 do this if the type of EXP is a subtype of something else
6649 since then the conversion might involve more than just
6650 converting modes. */
6651 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6652 && TREE_TYPE (TREE_TYPE (exp)) == 0
6653 && GET_MODE_PRECISION (outer_mode)
6654 == TYPE_PRECISION (TREE_TYPE (exp)))
6655 {
6656 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6657 TYPE_UNSIGNED (TREE_TYPE (exp))))
6658 {
6659 /* Some types, e.g. Fortran's logical*4, won't have a signed
6660 version, so use the mode instead. */
6661 tree ntype
6662 = (signed_or_unsigned_type_for
6663 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6664 if (ntype == NULL)
6665 ntype = lang_hooks.types.type_for_mode
6666 (TYPE_MODE (TREE_TYPE (exp)),
6667 SUBREG_PROMOTED_SIGN (target));
6668
6669 exp = fold_convert_loc (loc, ntype, exp);
6670 }
6671
6672 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6673 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6674 exp);
6675
6676 inner_target = SUBREG_REG (target);
6677 }
6678
6679 temp = expand_expr (exp, inner_target, VOIDmode,
6680 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6681
6682
6683 /* If TEMP is a VOIDmode constant, use convert_modes to make
6684 sure that we properly convert it. */
6685 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6686 {
6687 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6688 temp, SUBREG_PROMOTED_SIGN (target));
6689 temp = convert_modes (inner_mode, outer_mode, temp,
6690 SUBREG_PROMOTED_SIGN (target));
6691 }
6692 else if (!SCALAR_INT_MODE_P (GET_MODE (temp)))
6693 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6694 temp, SUBREG_PROMOTED_SIGN (target));
6695
6696 convert_move (SUBREG_REG (target), temp,
6697 SUBREG_PROMOTED_SIGN (target));
6698
6699 return NULL_RTX;
6700 }
6701 else if ((TREE_CODE (exp) == STRING_CST
6702 || (TREE_CODE (exp) == MEM_REF
6703 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6704 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6705 == STRING_CST
6706 && integer_zerop (TREE_OPERAND (exp, 1))))
6707 && !nontemporal && !call_param_p
6708 && MEM_P (target))
6709 {
6710 /* Optimize initialization of an array with a STRING_CST. */
6711 HOST_WIDE_INT exp_len, str_copy_len;
6712 rtx dest_mem;
6713 tree str = TREE_CODE (exp) == STRING_CST
6714 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6715
6716 exp_len = int_expr_size (exp);
6717 if (exp_len <= 0)
6718 goto normal_expr;
6719
6720 if (TREE_STRING_LENGTH (str) <= 0)
6721 goto normal_expr;
6722
6723 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6724 MEM_ALIGN (target), false))
6725 {
6726 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6727 MEM_ALIGN (target), false, RETURN_BEGIN);
6728 return NULL_RTX;
6729 }
6730
6731 str_copy_len = TREE_STRING_LENGTH (str);
6732
6733 /* Trailing NUL bytes in EXP will be handled by the call to
6734 clear_storage, which is more efficient than copying them from
6735 the STRING_CST, so trim those from STR_COPY_LEN. */
6736 while (str_copy_len)
6737 {
6738 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6739 break;
6740 str_copy_len--;
6741 }
6742
6743 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6744 {
6745 str_copy_len += STORE_MAX_PIECES - 1;
6746 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6747 }
6748 if (str_copy_len >= exp_len)
6749 goto normal_expr;
6750
6751 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6752 (void *) str, MEM_ALIGN (target), false))
6753 goto normal_expr;
6754
6755 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6756 (void *) str, MEM_ALIGN (target), false,
6757 RETURN_END);
6758 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6759 exp_len - str_copy_len),
6760 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6761 return NULL_RTX;
6762 }
6763 else
6764 {
6765 rtx tmp_target;
6766
6767 normal_expr:
6768 /* If we want to use a nontemporal or a reverse order store, force the
6769 value into a register first. */
6770 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6771 tree rexp = exp;
6772 if (TREE_CODE (exp) == STRING_CST
6773 && tmp_target == target
6774 && GET_MODE (target) == BLKmode
6775 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6776 {
6777 rtx size = expr_size (exp);
6778 if (CONST_INT_P (size)
6779 && size != const0_rtx
6780 && (UINTVAL (size)
6781 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6782 {
6783 /* If the STRING_CST has much larger array type than
6784 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6785 it into the rodata section as the code later on will use
6786 memset zero for the remainder anyway. See PR95052. */
6787 tmp_target = NULL_RTX;
6788 rexp = copy_node (exp);
6789 tree index
6790 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6791 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6792 index);
6793 shortened_string_cst = true;
6794 }
6795 }
6796 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6797 (call_param_p
6798 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6799 &alt_rtl, false);
6800 if (shortened_string_cst)
6801 {
6802 gcc_assert (MEM_P (temp));
6803 temp = change_address (temp, BLKmode, NULL_RTX);
6804 }
6805 }
6806
6807 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6808 the same as that of TARGET, adjust the constant. This is needed, for
6809 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6810 only a word-sized value. */
6811 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6812 && TREE_CODE (exp) != ERROR_MARK
6813 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6814 {
6815 gcc_assert (!shortened_string_cst);
6816 if (GET_MODE_CLASS (GET_MODE (target))
6817 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6818 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6819 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6820 {
6821 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6822 TYPE_MODE (TREE_TYPE (exp)), 0);
6823 if (t)
6824 temp = t;
6825 }
6826 if (GET_MODE (temp) == VOIDmode)
6827 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6828 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6829 }
6830
6831 /* If value was not generated in the target, store it there.
6832 Convert the value to TARGET's type first if necessary and emit the
6833 pending incrementations that have been queued when expanding EXP.
6834 Note that we cannot emit the whole queue blindly because this will
6835 effectively disable the POST_INC optimization later.
6836
6837 If TEMP and TARGET compare equal according to rtx_equal_p, but
6838 one or both of them are volatile memory refs, we have to distinguish
6839 two cases:
6840 - expand_expr has used TARGET. In this case, we must not generate
6841 another copy. This can be detected by TARGET being equal according
6842 to == .
6843 - expand_expr has not used TARGET - that means that the source just
6844 happens to have the same RTX form. Since temp will have been created
6845 by expand_expr, it will compare unequal according to == .
6846 We must generate a copy in this case, to reach the correct number
6847 of volatile memory references. */
6848
6849 if ((! rtx_equal_p (temp, target)
6850 || (temp != target && (side_effects_p (temp)
6851 || side_effects_p (target)
6852 || (MEM_P (temp)
6853 && !mems_same_for_tbaa_p (temp, target)))))
6854 && TREE_CODE (exp) != ERROR_MARK
6855 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6856 but TARGET is not valid memory reference, TEMP will differ
6857 from TARGET although it is really the same location. */
6858 && !(alt_rtl
6859 && rtx_equal_p (alt_rtl, target)
6860 && !side_effects_p (alt_rtl)
6861 && !side_effects_p (target))
6862 /* If there's nothing to copy, don't bother. Don't call
6863 expr_size unless necessary, because some front-ends (C++)
6864 expr_size-hook must not be given objects that are not
6865 supposed to be bit-copied or bit-initialized. */
6866 && expr_size (exp) != const0_rtx)
6867 {
6868 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6869 {
6870 gcc_assert (!shortened_string_cst);
6871 if (GET_MODE (target) == BLKmode)
6872 {
6873 /* Handle calls that return BLKmode values in registers. */
6874 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6875 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6876 else
6877 store_bit_field (target,
6878 rtx_to_poly_int64 (expr_size (exp))
6879 * BITS_PER_UNIT,
6880 0, 0, 0, GET_MODE (temp), temp, reverse,
6881 false);
6882 }
6883 else
6884 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6885 }
6886
6887 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6888 {
6889 /* Handle copying a string constant into an array. The string
6890 constant may be shorter than the array. So copy just the string's
6891 actual length, and clear the rest. First get the size of the data
6892 type of the string, which is actually the size of the target. */
6893 rtx size = expr_size (exp);
6894
6895 if (CONST_INT_P (size)
6896 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6897 emit_block_move (target, temp, size,
6898 (call_param_p
6899 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6900 else
6901 {
6902 machine_mode pointer_mode
6903 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6904 machine_mode address_mode = get_address_mode (target);
6905
6906 /* Compute the size of the data to copy from the string. */
6907 tree copy_size
6908 = size_binop_loc (loc, MIN_EXPR,
6909 make_tree (sizetype, size),
6910 size_int (TREE_STRING_LENGTH (exp)));
6911 rtx copy_size_rtx
6912 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6913 (call_param_p
6914 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6915 rtx_code_label *label = 0;
6916
6917 /* Copy that much. */
6918 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6919 TYPE_UNSIGNED (sizetype));
6920 emit_block_move (target, temp, copy_size_rtx,
6921 (call_param_p
6922 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6923
6924 /* Figure out how much is left in TARGET that we have to clear.
6925 Do all calculations in pointer_mode. */
6926 poly_int64 const_copy_size;
6927 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6928 {
6929 size = plus_constant (address_mode, size, -const_copy_size);
6930 target = adjust_address (target, BLKmode, const_copy_size);
6931 }
6932 else
6933 {
6934 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6935 copy_size_rtx, NULL_RTX, 0,
6936 OPTAB_LIB_WIDEN);
6937
6938 if (GET_MODE (copy_size_rtx) != address_mode)
6939 copy_size_rtx = convert_to_mode (address_mode,
6940 copy_size_rtx,
6941 TYPE_UNSIGNED (sizetype));
6942
6943 target = offset_address (target, copy_size_rtx,
6944 highest_pow2_factor (copy_size));
6945 label = gen_label_rtx ();
6946 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6947 GET_MODE (size), 0, label);
6948 }
6949
6950 if (size != const0_rtx)
6951 clear_storage (target, size, BLOCK_OP_NORMAL);
6952
6953 if (label)
6954 emit_label (label);
6955 }
6956 }
6957 else if (shortened_string_cst)
6958 gcc_unreachable ();
6959 /* Handle calls that return values in multiple non-contiguous locations.
6960 The Irix 6 ABI has examples of this. */
6961 else if (GET_CODE (target) == PARALLEL)
6962 {
6963 if (GET_CODE (temp) == PARALLEL)
6964 emit_group_move (target, temp);
6965 else
6966 emit_group_load (target, temp, TREE_TYPE (exp),
6967 int_size_in_bytes (TREE_TYPE (exp)));
6968 }
6969 else if (GET_CODE (temp) == PARALLEL)
6970 emit_group_store (target, temp, TREE_TYPE (exp),
6971 int_size_in_bytes (TREE_TYPE (exp)));
6972 else if (GET_MODE (temp) == BLKmode)
6973 emit_block_move (target, temp, expr_size (exp),
6974 (call_param_p
6975 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6976 /* If we emit a nontemporal store, there is nothing else to do. */
6977 else if (nontemporal && emit_storent_insn (target, temp))
6978 ;
6979 else
6980 {
6981 if (reverse)
6982 temp = flip_storage_order (GET_MODE (target), temp);
6983 temp = force_operand (temp, target);
6984 if (temp != target)
6985 emit_move_insn (target, temp);
6986 }
6987 }
6988 else
6989 gcc_assert (!shortened_string_cst);
6990
6991 return NULL_RTX;
6992 }
6993
6994 /* Return true if field F of structure TYPE is a flexible array. */
6995
6996 static bool
6997 flexible_array_member_p (const_tree f, const_tree type)
6998 {
6999 const_tree tf;
7000
7001 tf = TREE_TYPE (f);
7002 return (DECL_CHAIN (f) == NULL
7003 && TREE_CODE (tf) == ARRAY_TYPE
7004 && TYPE_DOMAIN (tf)
7005 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
7006 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
7007 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
7008 && int_size_in_bytes (type) >= 0);
7009 }
7010
7011 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
7012 must have in order for it to completely initialize a value of type TYPE.
7013 Return -1 if the number isn't known.
7014
7015 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
7016
7017 static HOST_WIDE_INT
7018 count_type_elements (const_tree type, bool for_ctor_p)
7019 {
7020 switch (TREE_CODE (type))
7021 {
7022 case ARRAY_TYPE:
7023 {
7024 tree nelts_minus_one;
7025
7026 nelts_minus_one = array_type_nelts_minus_one (type);
7027 if (nelts_minus_one && tree_fits_uhwi_p (nelts_minus_one))
7028 {
7029 unsigned HOST_WIDE_INT n;
7030
7031 n = tree_to_uhwi (nelts_minus_one) + 1;
7032 if (n == 0 || for_ctor_p)
7033 return n;
7034 else
7035 return n * count_type_elements (TREE_TYPE (type), false);
7036 }
7037 return for_ctor_p ? -1 : 1;
7038 }
7039
7040 case RECORD_TYPE:
7041 {
7042 unsigned HOST_WIDE_INT n;
7043 tree f;
7044
7045 n = 0;
7046 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7047 if (TREE_CODE (f) == FIELD_DECL)
7048 {
7049 if (!for_ctor_p)
7050 n += count_type_elements (TREE_TYPE (f), false);
7051 else if (!flexible_array_member_p (f, type))
7052 /* Don't count flexible arrays, which are not supposed
7053 to be initialized. */
7054 n += 1;
7055 }
7056
7057 return n;
7058 }
7059
7060 case UNION_TYPE:
7061 case QUAL_UNION_TYPE:
7062 {
7063 tree f;
7064 HOST_WIDE_INT n, m;
7065
7066 gcc_assert (!for_ctor_p);
7067 /* Estimate the number of scalars in each field and pick the
7068 maximum. Other estimates would do instead; the idea is simply
7069 to make sure that the estimate is not sensitive to the ordering
7070 of the fields. */
7071 n = 1;
7072 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7073 if (TREE_CODE (f) == FIELD_DECL)
7074 {
7075 m = count_type_elements (TREE_TYPE (f), false);
7076 /* If the field doesn't span the whole union, add an extra
7077 scalar for the rest. */
7078 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
7079 TYPE_SIZE (type)) != 1)
7080 m++;
7081 if (n < m)
7082 n = m;
7083 }
7084 return n;
7085 }
7086
7087 case COMPLEX_TYPE:
7088 return 2;
7089
7090 case VECTOR_TYPE:
7091 {
7092 unsigned HOST_WIDE_INT nelts;
7093 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
7094 return nelts;
7095 else
7096 return -1;
7097 }
7098
7099 case INTEGER_TYPE:
7100 case REAL_TYPE:
7101 case FIXED_POINT_TYPE:
7102 case ENUMERAL_TYPE:
7103 case BOOLEAN_TYPE:
7104 case POINTER_TYPE:
7105 case OFFSET_TYPE:
7106 case REFERENCE_TYPE:
7107 case NULLPTR_TYPE:
7108 case OPAQUE_TYPE:
7109 case BITINT_TYPE:
7110 return 1;
7111
7112 case ERROR_MARK:
7113 return 0;
7114
7115 case VOID_TYPE:
7116 case METHOD_TYPE:
7117 case FUNCTION_TYPE:
7118 case LANG_TYPE:
7119 default:
7120 gcc_unreachable ();
7121 }
7122 }
7123
7124 /* Helper for categorize_ctor_elements. Identical interface. */
7125
7126 static bool
7127 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7128 HOST_WIDE_INT *p_unique_nz_elts,
7129 HOST_WIDE_INT *p_init_elts, int *p_complete)
7130 {
7131 unsigned HOST_WIDE_INT idx;
7132 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
7133 tree value, purpose, elt_type;
7134
7135 /* Whether CTOR is a valid constant initializer, in accordance with what
7136 initializer_constant_valid_p does. If inferred from the constructor
7137 elements, true until proven otherwise. */
7138 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
7139 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
7140
7141 nz_elts = 0;
7142 unique_nz_elts = 0;
7143 init_elts = 0;
7144 num_fields = 0;
7145 elt_type = NULL_TREE;
7146
7147 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
7148 {
7149 HOST_WIDE_INT mult = 1;
7150
7151 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
7152 {
7153 tree lo_index = TREE_OPERAND (purpose, 0);
7154 tree hi_index = TREE_OPERAND (purpose, 1);
7155
7156 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
7157 mult = (tree_to_uhwi (hi_index)
7158 - tree_to_uhwi (lo_index) + 1);
7159 }
7160 num_fields += mult;
7161 elt_type = TREE_TYPE (value);
7162
7163 switch (TREE_CODE (value))
7164 {
7165 case CONSTRUCTOR:
7166 {
7167 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
7168
7169 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
7170 &ic, p_complete);
7171
7172 nz_elts += mult * nz;
7173 unique_nz_elts += unz;
7174 init_elts += mult * ic;
7175
7176 if (const_from_elts_p && const_p)
7177 const_p = const_elt_p;
7178 }
7179 break;
7180
7181 case INTEGER_CST:
7182 case REAL_CST:
7183 case FIXED_CST:
7184 if (!initializer_zerop (value))
7185 {
7186 nz_elts += mult;
7187 unique_nz_elts++;
7188 }
7189 init_elts += mult;
7190 break;
7191
7192 case STRING_CST:
7193 nz_elts += mult * TREE_STRING_LENGTH (value);
7194 unique_nz_elts += TREE_STRING_LENGTH (value);
7195 init_elts += mult * TREE_STRING_LENGTH (value);
7196 break;
7197
7198 case RAW_DATA_CST:
7199 nz_elts += mult * RAW_DATA_LENGTH (value);
7200 unique_nz_elts += RAW_DATA_LENGTH (value);
7201 init_elts += mult * RAW_DATA_LENGTH (value);
7202 num_fields += mult * (RAW_DATA_LENGTH (value) - 1);
7203 break;
7204
7205 case COMPLEX_CST:
7206 if (!initializer_zerop (TREE_REALPART (value)))
7207 {
7208 nz_elts += mult;
7209 unique_nz_elts++;
7210 }
7211 if (!initializer_zerop (TREE_IMAGPART (value)))
7212 {
7213 nz_elts += mult;
7214 unique_nz_elts++;
7215 }
7216 init_elts += 2 * mult;
7217 break;
7218
7219 case VECTOR_CST:
7220 {
7221 unsigned int nunits
7222 = constant_lower_bound
7223 (TYPE_VECTOR_SUBPARTS (TREE_TYPE (value)));
7224 for (unsigned int i = 0; i < nunits; ++i)
7225 {
7226 tree v = VECTOR_CST_ELT (value, i);
7227 if (!initializer_zerop (v))
7228 {
7229 nz_elts += mult;
7230 unique_nz_elts++;
7231 }
7232 init_elts += mult;
7233 }
7234 }
7235 break;
7236
7237 default:
7238 {
7239 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
7240 nz_elts += mult * tc;
7241 unique_nz_elts += tc;
7242 init_elts += mult * tc;
7243
7244 if (const_from_elts_p && const_p)
7245 const_p
7246 = initializer_constant_valid_p (value,
7247 elt_type,
7248 TYPE_REVERSE_STORAGE_ORDER
7249 (TREE_TYPE (ctor)))
7250 != NULL_TREE;
7251 }
7252 break;
7253 }
7254 }
7255
7256 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
7257 num_fields, elt_type))
7258 *p_complete = 0;
7259 else if (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
7260 || TREE_CODE (TREE_TYPE (ctor)) == QUAL_UNION_TYPE)
7261 {
7262 if (*p_complete
7263 && CONSTRUCTOR_ZERO_PADDING_BITS (ctor)
7264 && (num_fields
7265 ? simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
7266 TYPE_SIZE (elt_type)) != 1
7267 : type_has_padding_at_level_p (TREE_TYPE (ctor))))
7268 *p_complete = 0;
7269 else if (*p_complete > 0
7270 && (num_fields
7271 ? simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
7272 TYPE_SIZE (elt_type)) != 1
7273 : type_has_padding_at_level_p (TREE_TYPE (ctor))))
7274 *p_complete = -1;
7275 }
7276 else if (*p_complete
7277 && (CONSTRUCTOR_ZERO_PADDING_BITS (ctor)
7278 || flag_zero_init_padding_bits == ZERO_INIT_PADDING_BITS_ALL)
7279 && type_has_padding_at_level_p (TREE_TYPE (ctor)))
7280 *p_complete = 0;
7281 else if (*p_complete > 0
7282 && type_has_padding_at_level_p (TREE_TYPE (ctor)))
7283 *p_complete = -1;
7284
7285 *p_nz_elts += nz_elts;
7286 *p_unique_nz_elts += unique_nz_elts;
7287 *p_init_elts += init_elts;
7288
7289 return const_p;
7290 }
7291
7292 /* Examine CTOR to discover:
7293 * how many scalar fields are set to nonzero values,
7294 and place it in *P_NZ_ELTS;
7295 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
7296 high - low + 1 (this can be useful for callers to determine ctors
7297 that could be cheaply initialized with - perhaps nested - loops
7298 compared to copied from huge read-only data),
7299 and place it in *P_UNIQUE_NZ_ELTS;
7300 * how many scalar fields in total are in CTOR,
7301 and place it in *P_ELT_COUNT.
7302 * whether the constructor is complete -- in the sense that every
7303 meaningful byte is explicitly given a value --
7304 and place it in *P_COMPLETE:
7305 - 0 if any field is missing
7306 - 1 if all fields are initialized, and there's no padding
7307 - -1 if all fields are initialized, but there's padding
7308
7309 Return whether or not CTOR is a valid static constant initializer, the same
7310 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
7311
7312 bool
7313 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
7314 HOST_WIDE_INT *p_unique_nz_elts,
7315 HOST_WIDE_INT *p_init_elts, int *p_complete)
7316 {
7317 *p_nz_elts = 0;
7318 *p_unique_nz_elts = 0;
7319 *p_init_elts = 0;
7320 *p_complete = 1;
7321
7322 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
7323 p_init_elts, p_complete);
7324 }
7325
7326 /* Return true if constructor CTOR is simple enough to be materialized
7327 in an integer mode register. Limit the size to WORDS words, which
7328 is 1 by default. */
7329
7330 bool
7331 immediate_const_ctor_p (const_tree ctor, unsigned int words)
7332 {
7333 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
7334 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
7335 return false;
7336
7337 return TREE_CONSTANT (ctor)
7338 && !TREE_ADDRESSABLE (ctor)
7339 && CONSTRUCTOR_NELTS (ctor)
7340 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
7341 && int_expr_size (ctor) <= words * UNITS_PER_WORD
7342 && initializer_constant_valid_for_bitfield_p (ctor);
7343 }
7344
7345 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
7346 of which had type LAST_TYPE. Each element was itself a complete
7347 initializer, in the sense that every meaningful byte was explicitly
7348 given a value. Return true if the same is true for the constructor
7349 as a whole. */
7350
7351 bool
7352 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
7353 const_tree last_type)
7354 {
7355 if (TREE_CODE (type) == UNION_TYPE || TREE_CODE (type) == QUAL_UNION_TYPE)
7356 {
7357 if (num_elts == 0)
7358 {
7359 if (flag_zero_init_padding_bits >= ZERO_INIT_PADDING_BITS_UNIONS)
7360 return false;
7361
7362 /* If the CONSTRUCTOR doesn't have any elts, it is
7363 incomplete if the union has at least one field. */
7364 for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
7365 if (TREE_CODE (f) == FIELD_DECL)
7366 return false;
7367
7368 return true;
7369 }
7370
7371 gcc_assert (num_elts == 1 && last_type);
7372
7373 if (flag_zero_init_padding_bits >= ZERO_INIT_PADDING_BITS_UNIONS)
7374 /* ??? We could look at each element of the union, and find the
7375 largest element. Which would avoid comparing the size of the
7376 initialized element against any tail padding in the union.
7377 Doesn't seem worth the effort... */
7378 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
7379
7380 return true;
7381 }
7382
7383 return count_type_elements (type, true) == num_elts;
7384 }
7385
7386 /* Return true if EXP contains mostly (3/4) zeros. */
7387
7388 static bool
7389 mostly_zeros_p (const_tree exp)
7390 {
7391 if (TREE_CODE (exp) == CONSTRUCTOR)
7392 {
7393 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7394 int complete_p;
7395
7396 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7397 &complete_p);
7398 return !complete_p || nz_elts < init_elts / 4;
7399 }
7400
7401 return initializer_zerop (exp);
7402 }
7403
7404 /* Return true if EXP contains all zeros. */
7405
7406 static bool
7407 all_zeros_p (const_tree exp)
7408 {
7409 if (TREE_CODE (exp) == CONSTRUCTOR)
7410 {
7411 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
7412 int complete_p;
7413
7414 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
7415 &complete_p);
7416 return nz_elts == 0;
7417 }
7418
7419 return initializer_zerop (exp);
7420 }
7421
7422 /* Helper function for store_constructor.
7423 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
7424 CLEARED is as for store_constructor.
7425 ALIAS_SET is the alias set to use for any stores.
7426 If REVERSE is true, the store is to be done in reverse order.
7427
7428 This provides a recursive shortcut back to store_constructor when it isn't
7429 necessary to go through store_field. This is so that we can pass through
7430 the cleared field to let store_constructor know that we may not have to
7431 clear a substructure if the outer structure has already been cleared. */
7432
7433 static void
7434 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
7435 poly_uint64 bitregion_start,
7436 poly_uint64 bitregion_end,
7437 machine_mode mode,
7438 tree exp, int cleared,
7439 alias_set_type alias_set, bool reverse)
7440 {
7441 poly_int64 bytepos;
7442 poly_uint64 bytesize;
7443 if (TREE_CODE (exp) == CONSTRUCTOR
7444 /* We can only call store_constructor recursively if the size and
7445 bit position are on a byte boundary. */
7446 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
7447 && maybe_ne (bitsize, 0U)
7448 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
7449 /* If we have a nonzero bitpos for a register target, then we just
7450 let store_field do the bitfield handling. This is unlikely to
7451 generate unnecessary clear instructions anyways. */
7452 && (known_eq (bitpos, 0) || MEM_P (target)))
7453 {
7454 if (MEM_P (target))
7455 {
7456 machine_mode target_mode = GET_MODE (target);
7457 if (target_mode != BLKmode
7458 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
7459 target_mode = BLKmode;
7460 target = adjust_address (target, target_mode, bytepos);
7461 }
7462
7463
7464 /* Update the alias set, if required. */
7465 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
7466 && MEM_ALIAS_SET (target) != 0)
7467 {
7468 target = copy_rtx (target);
7469 set_mem_alias_set (target, alias_set);
7470 }
7471
7472 store_constructor (exp, target, cleared, bytesize, reverse);
7473 }
7474 else
7475 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
7476 exp, alias_set, false, reverse);
7477 }
7478
7479
7480 /* Returns the number of FIELD_DECLs in TYPE. */
7481
7482 static int
7483 fields_length (const_tree type)
7484 {
7485 tree t = TYPE_FIELDS (type);
7486 int count = 0;
7487
7488 for (; t; t = DECL_CHAIN (t))
7489 if (TREE_CODE (t) == FIELD_DECL)
7490 ++count;
7491
7492 return count;
7493 }
7494
7495
7496 /* Store the value of constructor EXP into the rtx TARGET.
7497 TARGET is either a REG or a MEM; we know it cannot conflict, since
7498 safe_from_p has been called.
7499 CLEARED is true if TARGET is known to have been zero'd.
7500 SIZE is the number of bytes of TARGET we are allowed to modify: this
7501 may not be the same as the size of EXP if we are assigning to a field
7502 which has been packed to exclude padding bits.
7503 If REVERSE is true, the store is to be done in reverse order. */
7504
7505 void
7506 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
7507 bool reverse)
7508 {
7509 tree type = TREE_TYPE (exp);
7510 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
7511 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
7512
7513 switch (TREE_CODE (type))
7514 {
7515 case RECORD_TYPE:
7516 case UNION_TYPE:
7517 case QUAL_UNION_TYPE:
7518 {
7519 unsigned HOST_WIDE_INT idx;
7520 tree field, value;
7521
7522 /* The storage order is specified for every aggregate type. */
7523 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7524
7525 /* If size is zero or the target is already cleared, do nothing. */
7526 if (known_eq (size, 0) || cleared)
7527 cleared = 1;
7528 /* We either clear the aggregate or indicate the value is dead. */
7529 else if ((TREE_CODE (type) == UNION_TYPE
7530 || TREE_CODE (type) == QUAL_UNION_TYPE)
7531 && ! CONSTRUCTOR_ELTS (exp))
7532 /* If the constructor is empty, clear the union. */
7533 {
7534 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7535 cleared = 1;
7536 }
7537
7538 /* If we are building a static constructor into a register,
7539 set the initial value as zero so we can fold the value into
7540 a constant. But if more than one register is involved,
7541 this probably loses. */
7542 else if (REG_P (target) && TREE_STATIC (exp)
7543 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7544 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7545 {
7546 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7547 cleared = 1;
7548 }
7549
7550 /* If the constructor has fewer fields than the structure or
7551 if we are initializing the structure to mostly zeros, clear
7552 the whole structure first. Don't do this if TARGET is a
7553 register whose mode size isn't equal to SIZE since
7554 clear_storage can't handle this case. */
7555 else if (known_size_p (size)
7556 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7557 || mostly_zeros_p (exp))
7558 && (!REG_P (target)
7559 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7560 {
7561 clear_storage (target, gen_int_mode (size, Pmode),
7562 BLOCK_OP_NORMAL);
7563 cleared = 1;
7564 }
7565
7566 if (REG_P (target) && !cleared)
7567 emit_clobber (target);
7568
7569 /* Store each element of the constructor into the
7570 corresponding field of TARGET. */
7571 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7572 {
7573 machine_mode mode;
7574 HOST_WIDE_INT bitsize;
7575 HOST_WIDE_INT bitpos = 0;
7576 tree offset;
7577 rtx to_rtx = target;
7578
7579 /* Just ignore missing fields. We cleared the whole
7580 structure, above, if any fields are missing. */
7581 if (field == 0)
7582 continue;
7583
7584 if (cleared && initializer_zerop (value))
7585 continue;
7586
7587 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7588 bitsize = tree_to_uhwi (DECL_SIZE (field));
7589 else
7590 gcc_unreachable ();
7591
7592 mode = DECL_MODE (field);
7593 if (DECL_BIT_FIELD (field))
7594 mode = VOIDmode;
7595
7596 offset = DECL_FIELD_OFFSET (field);
7597 if (tree_fits_shwi_p (offset)
7598 && tree_fits_shwi_p (bit_position (field)))
7599 {
7600 bitpos = int_bit_position (field);
7601 offset = NULL_TREE;
7602 }
7603 else
7604 gcc_unreachable ();
7605
7606 /* If this initializes a field that is smaller than a
7607 word, at the start of a word, try to widen it to a full
7608 word. This special case allows us to output C++ member
7609 function initializations in a form that the optimizers
7610 can understand. */
7611 if (WORD_REGISTER_OPERATIONS
7612 && REG_P (target)
7613 && bitsize < BITS_PER_WORD
7614 && bitpos % BITS_PER_WORD == 0
7615 && GET_MODE_CLASS (mode) == MODE_INT
7616 && TREE_CODE (value) == INTEGER_CST
7617 && exp_size >= 0
7618 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7619 {
7620 type = TREE_TYPE (value);
7621
7622 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7623 {
7624 type = lang_hooks.types.type_for_mode
7625 (word_mode, TYPE_UNSIGNED (type));
7626 value = fold_convert (type, value);
7627 /* Make sure the bits beyond the original bitsize are zero
7628 so that we can correctly avoid extra zeroing stores in
7629 later constructor elements. */
7630 tree bitsize_mask
7631 = wide_int_to_tree (type, wi::mask (bitsize, false,
7632 BITS_PER_WORD));
7633 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7634 }
7635
7636 if (BYTES_BIG_ENDIAN)
7637 value
7638 = fold_build2 (LSHIFT_EXPR, type, value,
7639 build_int_cst (type,
7640 BITS_PER_WORD - bitsize));
7641 bitsize = BITS_PER_WORD;
7642 mode = word_mode;
7643 }
7644
7645 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7646 && DECL_NONADDRESSABLE_P (field))
7647 {
7648 to_rtx = copy_rtx (to_rtx);
7649 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7650 }
7651
7652 store_constructor_field (to_rtx, bitsize, bitpos,
7653 0, bitregion_end, mode,
7654 value, cleared,
7655 get_alias_set (TREE_TYPE (field)),
7656 reverse);
7657 }
7658 break;
7659 }
7660 case ARRAY_TYPE:
7661 {
7662 tree value, index;
7663 unsigned HOST_WIDE_INT i, j = 0;
7664 bool need_to_clear;
7665 tree domain;
7666 tree elttype = TREE_TYPE (type);
7667 bool const_bounds_p;
7668 HOST_WIDE_INT minelt = 0;
7669 HOST_WIDE_INT maxelt = 0;
7670
7671 /* The storage order is specified for every aggregate type. */
7672 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7673
7674 domain = TYPE_DOMAIN (type);
7675 const_bounds_p = (TYPE_MIN_VALUE (domain)
7676 && TYPE_MAX_VALUE (domain)
7677 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7678 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7679
7680 /* If we have constant bounds for the range of the type, get them. */
7681 if (const_bounds_p)
7682 {
7683 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7684 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7685 }
7686
7687 /* If the constructor has fewer elements than the array, clear
7688 the whole array first. Similarly if this is static
7689 constructor of a non-BLKmode object. */
7690 if (cleared)
7691 need_to_clear = false;
7692 else if (REG_P (target) && TREE_STATIC (exp))
7693 need_to_clear = true;
7694 else
7695 {
7696 unsigned HOST_WIDE_INT idx;
7697 HOST_WIDE_INT count = 0, zero_count = 0;
7698 need_to_clear = ! const_bounds_p;
7699
7700 /* This loop is a more accurate version of the loop in
7701 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7702 is also needed to check for missing elements. */
7703 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7704 {
7705 HOST_WIDE_INT this_node_count;
7706
7707 if (need_to_clear)
7708 break;
7709
7710 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7711 {
7712 tree lo_index = TREE_OPERAND (index, 0);
7713 tree hi_index = TREE_OPERAND (index, 1);
7714
7715 if (! tree_fits_uhwi_p (lo_index)
7716 || ! tree_fits_uhwi_p (hi_index))
7717 {
7718 need_to_clear = true;
7719 break;
7720 }
7721
7722 this_node_count = (tree_to_uhwi (hi_index)
7723 - tree_to_uhwi (lo_index) + 1);
7724 }
7725 else if (TREE_CODE (value) == RAW_DATA_CST)
7726 this_node_count = RAW_DATA_LENGTH (value);
7727 else
7728 this_node_count = 1;
7729
7730 count += this_node_count;
7731 if (mostly_zeros_p (value))
7732 zero_count += this_node_count;
7733 }
7734
7735 /* Clear the entire array first if there are any missing
7736 elements, or if the incidence of zero elements is >=
7737 75%. */
7738 if (! need_to_clear
7739 && (count < maxelt - minelt + 1
7740 || 4 * zero_count >= 3 * count))
7741 need_to_clear = true;
7742 }
7743
7744 if (need_to_clear && maybe_gt (size, 0))
7745 {
7746 if (REG_P (target))
7747 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7748 else
7749 clear_storage (target, gen_int_mode (size, Pmode),
7750 BLOCK_OP_NORMAL);
7751 cleared = 1;
7752 }
7753
7754 if (!cleared && REG_P (target))
7755 /* Inform later passes that the old value is dead. */
7756 emit_clobber (target);
7757
7758 /* Store each element of the constructor into the
7759 corresponding element of TARGET, determined by counting the
7760 elements. */
7761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7762 {
7763 machine_mode mode;
7764 poly_int64 bitsize;
7765 HOST_WIDE_INT bitpos;
7766 rtx xtarget = target;
7767
7768 if (cleared && initializer_zerop (value))
7769 {
7770 if (TREE_CODE (value) == RAW_DATA_CST)
7771 j += RAW_DATA_LENGTH (value) - 1;
7772 continue;
7773 }
7774
7775 mode = TYPE_MODE (elttype);
7776 if (mode != BLKmode)
7777 bitsize = GET_MODE_BITSIZE (mode);
7778 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7779 bitsize = -1;
7780
7781 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7782 {
7783 tree lo_index = TREE_OPERAND (index, 0);
7784 tree hi_index = TREE_OPERAND (index, 1);
7785 rtx index_r, pos_rtx;
7786 HOST_WIDE_INT lo, hi, count;
7787 tree position;
7788
7789 gcc_assert (TREE_CODE (value) != RAW_DATA_CST);
7790
7791 /* If the range is constant and "small", unroll the loop. */
7792 if (const_bounds_p
7793 && tree_fits_shwi_p (lo_index)
7794 && tree_fits_shwi_p (hi_index)
7795 && (lo = tree_to_shwi (lo_index),
7796 hi = tree_to_shwi (hi_index),
7797 count = hi - lo + 1,
7798 (!MEM_P (target)
7799 || count <= 2
7800 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7801 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7802 <= 40 * 8)))))
7803 {
7804 lo -= minelt; hi -= minelt;
7805 for (; lo <= hi; lo++)
7806 {
7807 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7808
7809 if (MEM_P (target)
7810 && !MEM_KEEP_ALIAS_SET_P (target)
7811 && TREE_CODE (type) == ARRAY_TYPE
7812 && TYPE_NONALIASED_COMPONENT (type))
7813 {
7814 target = copy_rtx (target);
7815 MEM_KEEP_ALIAS_SET_P (target) = 1;
7816 }
7817
7818 store_constructor_field
7819 (target, bitsize, bitpos, 0, bitregion_end,
7820 mode, value, cleared,
7821 get_alias_set (elttype), reverse);
7822 }
7823 }
7824 else
7825 {
7826 rtx_code_label *loop_start = gen_label_rtx ();
7827 rtx_code_label *loop_end = gen_label_rtx ();
7828 tree exit_cond;
7829
7830 expand_normal (hi_index);
7831
7832 index = build_decl (EXPR_LOCATION (exp),
7833 VAR_DECL, NULL_TREE, domain);
7834 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7835 SET_DECL_RTL (index, index_r);
7836 store_expr (lo_index, index_r, 0, false, reverse);
7837
7838 /* Build the head of the loop. */
7839 do_pending_stack_adjust ();
7840 emit_label (loop_start);
7841
7842 /* Assign value to element index. */
7843 position =
7844 fold_convert (ssizetype,
7845 fold_build2 (MINUS_EXPR,
7846 TREE_TYPE (index),
7847 index,
7848 TYPE_MIN_VALUE (domain)));
7849
7850 position =
7851 size_binop (MULT_EXPR, position,
7852 fold_convert (ssizetype,
7853 TYPE_SIZE_UNIT (elttype)));
7854
7855 pos_rtx = expand_normal (position);
7856 xtarget = offset_address (target, pos_rtx,
7857 highest_pow2_factor (position));
7858 xtarget = adjust_address (xtarget, mode, 0);
7859 if (TREE_CODE (value) == CONSTRUCTOR)
7860 store_constructor (value, xtarget, cleared,
7861 exact_div (bitsize, BITS_PER_UNIT),
7862 reverse);
7863 else
7864 store_expr (value, xtarget, 0, false, reverse);
7865
7866 /* Generate a conditional jump to exit the loop. */
7867 exit_cond = build2 (LT_EXPR, integer_type_node,
7868 index, hi_index);
7869 jumpif (exit_cond, loop_end,
7870 profile_probability::uninitialized ());
7871
7872 /* Update the loop counter, and jump to the head of
7873 the loop. */
7874 expand_assignment (index,
7875 build2 (PLUS_EXPR, TREE_TYPE (index),
7876 index, integer_one_node),
7877 false);
7878
7879 emit_jump (loop_start);
7880
7881 /* Build the end of the loop. */
7882 emit_label (loop_end);
7883 }
7884 }
7885 else if ((index != 0 && ! tree_fits_shwi_p (index))
7886 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7887 {
7888 tree position;
7889
7890 gcc_assert (TREE_CODE (value) != RAW_DATA_CST);
7891 if (index == 0)
7892 index = ssize_int (1);
7893
7894 if (minelt)
7895 index = fold_convert (ssizetype,
7896 fold_build2 (MINUS_EXPR,
7897 TREE_TYPE (index),
7898 index,
7899 TYPE_MIN_VALUE (domain)));
7900
7901 position =
7902 size_binop (MULT_EXPR, index,
7903 fold_convert (ssizetype,
7904 TYPE_SIZE_UNIT (elttype)));
7905 xtarget = offset_address (target,
7906 expand_normal (position),
7907 highest_pow2_factor (position));
7908 xtarget = adjust_address (xtarget, mode, 0);
7909 store_expr (value, xtarget, 0, false, reverse);
7910 }
7911 else
7912 {
7913 if (index != 0)
7914 bitpos = ((tree_to_shwi (index) - minelt)
7915 * tree_to_uhwi (TYPE_SIZE (elttype)));
7916 else
7917 bitpos = ((i + j) * tree_to_uhwi (TYPE_SIZE (elttype)));
7918
7919 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7920 && TREE_CODE (type) == ARRAY_TYPE
7921 && TYPE_NONALIASED_COMPONENT (type))
7922 {
7923 target = copy_rtx (target);
7924 MEM_KEEP_ALIAS_SET_P (target) = 1;
7925 }
7926 if (TREE_CODE (value) != RAW_DATA_CST)
7927 store_constructor_field (target, bitsize, bitpos, 0,
7928 bitregion_end, mode, value,
7929 cleared, get_alias_set (elttype),
7930 reverse);
7931 else
7932 {
7933 j += RAW_DATA_LENGTH (value) - 1;
7934 gcc_assert (known_eq (bitsize, BITS_PER_UNIT));
7935 rtx to_rtx = adjust_address (target, mode,
7936 bitpos / BITS_PER_UNIT);
7937
7938 if (to_rtx == target)
7939 to_rtx = copy_rtx (to_rtx);
7940
7941 if (!MEM_KEEP_ALIAS_SET_P (to_rtx)
7942 && MEM_ALIAS_SET (to_rtx) != 0)
7943 set_mem_alias_set (to_rtx, get_alias_set (elttype));
7944
7945 if (can_store_by_pieces (RAW_DATA_LENGTH (value),
7946 raw_data_cst_read_str,
7947 (void *) value,
7948 MEM_ALIGN (target), false))
7949 {
7950 store_by_pieces (target, RAW_DATA_LENGTH (value),
7951 raw_data_cst_read_str, (void *) value,
7952 MEM_ALIGN (target), false,
7953 RETURN_BEGIN);
7954 continue;
7955 }
7956
7957 elttype
7958 = build_array_type_nelts (TREE_TYPE (value),
7959 RAW_DATA_LENGTH (value));
7960 tree ctor = build_constructor_single (elttype, NULL_TREE,
7961 value);
7962 ctor = tree_output_constant_def (ctor);
7963 mode = TYPE_MODE (type);
7964 store_constructor_field (target,
7965 bitsize * RAW_DATA_LENGTH (value),
7966 bitpos, 0, bitregion_end, mode,
7967 ctor, cleared,
7968 get_alias_set (elttype), reverse);
7969 }
7970 }
7971 }
7972 break;
7973 }
7974
7975 case VECTOR_TYPE:
7976 {
7977 unsigned HOST_WIDE_INT idx;
7978 constructor_elt *ce;
7979 bool need_to_clear;
7980 insn_code icode = CODE_FOR_nothing;
7981 tree elt;
7982 tree elttype = TREE_TYPE (type);
7983 int elt_size = vector_element_bits (type);
7984 machine_mode eltmode = TYPE_MODE (elttype);
7985 poly_int64 bitsize;
7986 poly_int64 bitpos;
7987 rtvec vector = NULL;
7988 poly_uint64 n_elts;
7989 unsigned HOST_WIDE_INT const_n_elts;
7990 alias_set_type alias;
7991 bool vec_vec_init_p = false;
7992 machine_mode mode = GET_MODE (target);
7993
7994 gcc_assert (eltmode != BLKmode);
7995
7996 /* Try using vec_duplicate_optab for uniform vectors. */
7997 icode = optab_handler (vec_duplicate_optab, mode);
7998 if (!TREE_SIDE_EFFECTS (exp)
7999 && VECTOR_MODE_P (mode)
8000 && icode != CODE_FOR_nothing
8001 /* If the vec_duplicate target pattern does not specify an element
8002 mode check that eltmode is the normal inner mode of the
8003 requested vector mode. But if the target allows eltmode
8004 explicitly go ahead and use it. */
8005 && (eltmode == GET_MODE_INNER (mode)
8006 || insn_data[icode].operand[1].mode == eltmode)
8007 && (elt = uniform_vector_p (exp))
8008 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
8009 {
8010 class expand_operand ops[2];
8011 create_output_operand (&ops[0], target, mode);
8012 create_input_operand (&ops[1], expand_normal (elt), eltmode);
8013 expand_insn (icode, 2, ops);
8014 if (!rtx_equal_p (target, ops[0].value))
8015 emit_move_insn (target, ops[0].value);
8016 break;
8017 }
8018 /* Use sign-extension for uniform boolean vectors with
8019 integer modes and single-bit mask entries.
8020 Effectively "vec_duplicate" for bitmasks. */
8021 if (elt_size == 1
8022 && !TREE_SIDE_EFFECTS (exp)
8023 && VECTOR_BOOLEAN_TYPE_P (type)
8024 && SCALAR_INT_MODE_P (TYPE_MODE (type))
8025 && (elt = uniform_vector_p (exp))
8026 && !VECTOR_TYPE_P (TREE_TYPE (elt))
8027 && !BYTES_BIG_ENDIAN)
8028 {
8029 rtx op0 = force_reg (TYPE_MODE (TREE_TYPE (elt)),
8030 expand_normal (elt));
8031 rtx tmp = gen_reg_rtx (mode);
8032 convert_move (tmp, op0, 0);
8033
8034 /* Ensure no excess bits are set.
8035 GCN needs this for nunits < 64.
8036 x86 needs this for nunits < 8. */
8037 unsigned int nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8038 if (maybe_ne (GET_MODE_PRECISION (mode), nunits))
8039 tmp = expand_binop (mode, and_optab, tmp,
8040 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
8041 target, true, OPTAB_WIDEN);
8042 if (tmp != target)
8043 emit_move_insn (target, tmp);
8044 break;
8045 }
8046
8047 n_elts = TYPE_VECTOR_SUBPARTS (type);
8048 if (REG_P (target)
8049 && VECTOR_MODE_P (mode))
8050 {
8051 const_n_elts = 0;
8052 if (CONSTRUCTOR_NELTS (exp)
8053 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
8054 == VECTOR_TYPE))
8055 {
8056 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
8057 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
8058 * TYPE_VECTOR_SUBPARTS (etype),
8059 n_elts));
8060
8061 icode = convert_optab_handler (vec_init_optab, mode,
8062 TYPE_MODE (etype));
8063 const_n_elts = CONSTRUCTOR_NELTS (exp);
8064 vec_vec_init_p = icode != CODE_FOR_nothing;
8065 }
8066 else if (exact_div (n_elts, GET_MODE_NUNITS (eltmode))
8067 .is_constant (&const_n_elts))
8068 {
8069 /* For a non-const type vector, we check it is made up of
8070 similarly non-const type vectors. */
8071 icode = convert_optab_handler (vec_init_optab, mode, eltmode);
8072 }
8073
8074 if (const_n_elts && icode != CODE_FOR_nothing)
8075 {
8076 vector = rtvec_alloc (const_n_elts);
8077 for (unsigned int k = 0; k < const_n_elts; k++)
8078 RTVEC_ELT (vector, k) = CONST0_RTX (eltmode);
8079 }
8080 }
8081
8082 /* Compute the size of the elements in the CTOR. It differs
8083 from the size of the vector type elements only when the
8084 CTOR elements are vectors themselves. */
8085 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
8086 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
8087 : elttype);
8088 if (VECTOR_TYPE_P (val_type))
8089 bitsize = tree_to_poly_uint64 (TYPE_SIZE (val_type));
8090 else
8091 bitsize = elt_size;
8092
8093 /* If the constructor has fewer elements than the vector,
8094 clear the whole array first. Similarly if this is static
8095 constructor of a non-BLKmode object. */
8096 if (cleared)
8097 need_to_clear = false;
8098 else if (REG_P (target) && TREE_STATIC (exp))
8099 need_to_clear = true;
8100 else
8101 {
8102 poly_uint64 count = 0, zero_count = 0;
8103 tree value;
8104
8105 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
8106 {
8107 poly_int64 n_elts_here = exact_div (bitsize, elt_size);
8108 count += n_elts_here;
8109 if (mostly_zeros_p (value))
8110 zero_count += n_elts_here;
8111 }
8112
8113 /* Clear the entire vector first if there are any missing elements,
8114 or if the incidence of zero elements is >= 75%. */
8115 need_to_clear = (maybe_lt (count, n_elts)
8116 || maybe_gt (4 * zero_count, 3 * count));
8117 }
8118
8119 if (need_to_clear && maybe_gt (size, 0) && !vector)
8120 {
8121 if (REG_P (target))
8122 emit_move_insn (target, CONST0_RTX (mode));
8123 else
8124 clear_storage (target, gen_int_mode (size, Pmode),
8125 BLOCK_OP_NORMAL);
8126 cleared = 1;
8127 }
8128
8129 /* Inform later passes that the old value is dead. */
8130 if (!cleared && !vector && REG_P (target) && maybe_gt (n_elts, 1u))
8131 {
8132 emit_move_insn (target, CONST0_RTX (mode));
8133 cleared = 1;
8134 }
8135
8136 if (MEM_P (target))
8137 alias = MEM_ALIAS_SET (target);
8138 else
8139 alias = get_alias_set (elttype);
8140
8141 /* Store each element of the constructor into the corresponding
8142 element of TARGET, determined by counting the elements. */
8143 HOST_WIDE_INT chunk_size = 0;
8144 bool chunk_multiple_p = constant_multiple_p (bitsize, elt_size,
8145 &chunk_size);
8146 gcc_assert (chunk_multiple_p || vec_vec_init_p);
8147
8148 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
8149 idx++)
8150 {
8151 HOST_WIDE_INT eltpos;
8152 tree value = ce->value;
8153
8154 if (cleared && initializer_zerop (value))
8155 continue;
8156
8157 if (ce->index)
8158 eltpos = tree_to_uhwi (ce->index);
8159 else
8160 eltpos = idx * chunk_size;
8161
8162 if (vector)
8163 {
8164 if (vec_vec_init_p)
8165 {
8166 gcc_assert (ce->index == NULL_TREE);
8167 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
8168 eltpos = idx;
8169 }
8170 else
8171 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
8172 RTVEC_ELT (vector, eltpos) = expand_normal (value);
8173 }
8174 else
8175 {
8176 machine_mode value_mode
8177 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
8178 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
8179 bitpos = eltpos * elt_size;
8180 store_constructor_field (target, bitsize, bitpos, 0,
8181 bitregion_end, value_mode,
8182 value, cleared, alias, reverse);
8183 }
8184 }
8185
8186 if (vector)
8187 emit_insn (GEN_FCN (icode) (target,
8188 gen_rtx_PARALLEL (mode, vector)));
8189 break;
8190 }
8191
8192 default:
8193 gcc_unreachable ();
8194 }
8195 }
8196
8197 /* Store the value of EXP (an expression tree)
8198 into a subfield of TARGET which has mode MODE and occupies
8199 BITSIZE bits, starting BITPOS bits from the start of TARGET.
8200 If MODE is VOIDmode, it means that we are storing into a bit-field.
8201
8202 BITREGION_START is bitpos of the first bitfield in this region.
8203 BITREGION_END is the bitpos of the ending bitfield in this region.
8204 These two fields are 0, if the C++ memory model does not apply,
8205 or we are not interested in keeping track of bitfield regions.
8206
8207 Always return const0_rtx unless we have something particular to
8208 return.
8209
8210 ALIAS_SET is the alias set for the destination. This value will
8211 (in general) be different from that for TARGET, since TARGET is a
8212 reference to the containing structure.
8213
8214 If NONTEMPORAL is true, try generating a nontemporal store.
8215
8216 If REVERSE is true, the store is to be done in reverse order. */
8217
8218 static rtx
8219 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
8220 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
8221 machine_mode mode, tree exp,
8222 alias_set_type alias_set, bool nontemporal, bool reverse)
8223 {
8224 if (TREE_CODE (exp) == ERROR_MARK)
8225 return const0_rtx;
8226
8227 /* If we have nothing to store, do nothing unless the expression has
8228 side-effects. Don't do that for zero sized addressable lhs of
8229 calls. */
8230 if (known_eq (bitsize, 0)
8231 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8232 || TREE_CODE (exp) != CALL_EXPR))
8233 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
8234
8235 if (GET_CODE (target) == CONCAT)
8236 {
8237 /* We're storing into a struct containing a single __complex. */
8238
8239 gcc_assert (known_eq (bitpos, 0));
8240 return store_expr (exp, target, 0, nontemporal, reverse);
8241 }
8242
8243 /* If the structure is in a register or if the component
8244 is a bit field, we cannot use addressing to access it.
8245 Use bit-field techniques or SUBREG to store in it. */
8246
8247 poly_int64 decl_bitsize;
8248 if (mode == VOIDmode
8249 || (mode != BLKmode && ! direct_store[(int) mode]
8250 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8251 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
8252 || REG_P (target)
8253 || GET_CODE (target) == SUBREG
8254 /* If the field isn't aligned enough to store as an ordinary memref,
8255 store it as a bit field. */
8256 || (mode != BLKmode
8257 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
8258 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
8259 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
8260 || !multiple_p (bitpos, BITS_PER_UNIT)))
8261 || (known_size_p (bitsize)
8262 && mode != BLKmode
8263 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
8264 /* If the RHS and field are a constant size and the size of the
8265 RHS isn't the same size as the bitfield, we must use bitfield
8266 operations. */
8267 || (known_size_p (bitsize)
8268 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
8269 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
8270 bitsize)
8271 /* Except for initialization of full bytes from a CONSTRUCTOR, which
8272 we will handle specially below. */
8273 && !(TREE_CODE (exp) == CONSTRUCTOR
8274 && multiple_p (bitsize, BITS_PER_UNIT))
8275 /* And except for bitwise copying of TREE_ADDRESSABLE types,
8276 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
8277 includes some extra padding. store_expr / expand_expr will in
8278 that case call get_inner_reference that will have the bitsize
8279 we check here and thus the block move will not clobber the
8280 padding that shouldn't be clobbered. In the future we could
8281 replace the TREE_ADDRESSABLE check with a check that
8282 get_base_address needs to live in memory. */
8283 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
8284 || TREE_CODE (exp) != COMPONENT_REF
8285 || !multiple_p (bitsize, BITS_PER_UNIT)
8286 || !multiple_p (bitpos, BITS_PER_UNIT)
8287 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
8288 &decl_bitsize)
8289 || maybe_ne (decl_bitsize, bitsize))
8290 /* A call with an addressable return type and return-slot
8291 optimization must not need bitfield operations but we must
8292 pass down the original target. */
8293 && (TREE_CODE (exp) != CALL_EXPR
8294 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
8295 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
8296 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
8297 decl we must use bitfield operations. */
8298 || (known_size_p (bitsize)
8299 && TREE_CODE (exp) == MEM_REF
8300 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
8301 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8302 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
8303 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
8304 {
8305 rtx temp;
8306 gimple *nop_def;
8307
8308 /* If EXP is a NOP_EXPR of precision less than its mode, then that
8309 implies a mask operation. If the precision is the same size as
8310 the field we're storing into, that mask is redundant. This is
8311 particularly common with bit field assignments generated by the
8312 C front end. */
8313 nop_def = get_def_for_expr (exp, NOP_EXPR);
8314 if (nop_def)
8315 {
8316 tree type = TREE_TYPE (exp);
8317 if (INTEGRAL_TYPE_P (type)
8318 && maybe_ne (TYPE_PRECISION (type),
8319 GET_MODE_BITSIZE (TYPE_MODE (type)))
8320 && known_eq (bitsize, TYPE_PRECISION (type)))
8321 {
8322 tree op = gimple_assign_rhs1 (nop_def);
8323 type = TREE_TYPE (op);
8324 if (INTEGRAL_TYPE_P (type)
8325 && known_ge (TYPE_PRECISION (type), bitsize))
8326 exp = op;
8327 }
8328 }
8329
8330 temp = expand_normal (exp);
8331
8332 /* We don't support variable-sized BLKmode bitfields, since our
8333 handling of BLKmode is bound up with the ability to break
8334 things into words. */
8335 gcc_assert (mode != BLKmode || bitsize.is_constant ());
8336
8337 /* Handle calls that return values in multiple non-contiguous locations.
8338 The Irix 6 ABI has examples of this. */
8339 if (GET_CODE (temp) == PARALLEL)
8340 {
8341 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
8342 machine_mode temp_mode = GET_MODE (temp);
8343 if (temp_mode == BLKmode || temp_mode == VOIDmode)
8344 temp_mode
8345 = smallest_int_mode_for_size (size * BITS_PER_UNIT).require ();
8346 rtx temp_target = gen_reg_rtx (temp_mode);
8347 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
8348 temp = temp_target;
8349 }
8350
8351 /* Handle calls that return BLKmode values in registers. */
8352 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
8353 {
8354 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
8355 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
8356 temp = temp_target;
8357 }
8358
8359 /* If the value has aggregate type and an integral mode then, if BITSIZE
8360 is narrower than this mode and this is for big-endian data, we first
8361 need to put the value into the low-order bits for store_bit_field,
8362 except when MODE is BLKmode and BITSIZE larger than the word size
8363 (see the handling of fields larger than a word in store_bit_field).
8364 Moreover, the field may be not aligned on a byte boundary; in this
8365 case, if it has reverse storage order, it needs to be accessed as a
8366 scalar field with reverse storage order and we must first put the
8367 value into target order. */
8368 scalar_int_mode temp_mode;
8369 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
8370 && is_int_mode (GET_MODE (temp), &temp_mode))
8371 {
8372 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
8373
8374 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
8375
8376 if (reverse)
8377 temp = flip_storage_order (temp_mode, temp);
8378
8379 gcc_checking_assert (known_le (bitsize, size));
8380 if (maybe_lt (bitsize, size)
8381 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
8382 /* Use of to_constant for BLKmode was checked above. */
8383 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
8384 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
8385 size - bitsize, NULL_RTX, 1);
8386 }
8387
8388 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
8389 if (mode != VOIDmode && mode != BLKmode
8390 && mode != TYPE_MODE (TREE_TYPE (exp)))
8391 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
8392
8393 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
8394 and BITPOS must be aligned on a byte boundary. If so, we simply do
8395 a block copy. Likewise for a BLKmode-like TARGET. */
8396 if (GET_MODE (temp) == BLKmode
8397 && (GET_MODE (target) == BLKmode
8398 || (MEM_P (target)
8399 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
8400 && multiple_p (bitpos, BITS_PER_UNIT)
8401 && multiple_p (bitsize, BITS_PER_UNIT))))
8402 {
8403 gcc_assert (MEM_P (target) && MEM_P (temp));
8404 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8405 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
8406
8407 target = adjust_address (target, VOIDmode, bytepos);
8408 emit_block_move (target, temp,
8409 gen_int_mode (bytesize, Pmode),
8410 BLOCK_OP_NORMAL);
8411
8412 return const0_rtx;
8413 }
8414
8415 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
8416 word size, we need to load the value (see again store_bit_field). */
8417 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
8418 {
8419 temp_mode = smallest_int_mode_for_size (bitsize).require ();
8420 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
8421 temp_mode, false, NULL);
8422 }
8423
8424 /* Store the value in the bitfield. */
8425 gcc_checking_assert (known_ge (bitpos, 0));
8426 store_bit_field (target, bitsize, bitpos,
8427 bitregion_start, bitregion_end,
8428 mode, temp, reverse, false);
8429
8430 return const0_rtx;
8431 }
8432 else
8433 {
8434 /* Now build a reference to just the desired component. */
8435 rtx to_rtx = adjust_address (target, mode,
8436 exact_div (bitpos, BITS_PER_UNIT));
8437
8438 if (to_rtx == target)
8439 to_rtx = copy_rtx (to_rtx);
8440
8441 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
8442 set_mem_alias_set (to_rtx, alias_set);
8443
8444 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
8445 into a target smaller than its type; handle that case now. */
8446 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
8447 {
8448 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
8449 store_constructor (exp, to_rtx, 0, bytesize, reverse);
8450 return to_rtx;
8451 }
8452
8453 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
8454 }
8455 }
8456
8457 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
8458 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
8459 codes and find the ultimate containing object, which we return.
8460
8461 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
8462 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
8463 storage order of the field.
8464 If the position of the field is variable, we store a tree
8465 giving the variable offset (in units) in *POFFSET.
8466 This offset is in addition to the bit position.
8467 If the position is not variable, we store 0 in *POFFSET.
8468
8469 If any of the extraction expressions is volatile,
8470 we store 1 in *PVOLATILEP. Otherwise we don't change that.
8471
8472 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
8473 Otherwise, it is a mode that can be used to access the field.
8474
8475 If the field describes a variable-sized object, *PMODE is set to
8476 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
8477 this case, but the address of the object can be found. */
8478
8479 tree
8480 get_inner_reference (tree exp, poly_int64 *pbitsize,
8481 poly_int64 *pbitpos, tree *poffset,
8482 machine_mode *pmode, int *punsignedp,
8483 int *preversep, int *pvolatilep)
8484 {
8485 tree size_tree = 0;
8486 machine_mode mode = VOIDmode;
8487 bool blkmode_bitfield = false;
8488 tree offset = size_zero_node;
8489 poly_offset_int bit_offset = 0;
8490
8491 /* First get the mode, signedness, storage order and size. We do this from
8492 just the outermost expression. */
8493 *pbitsize = -1;
8494 if (TREE_CODE (exp) == COMPONENT_REF)
8495 {
8496 tree field = TREE_OPERAND (exp, 1);
8497 size_tree = DECL_SIZE (field);
8498 if (flag_strict_volatile_bitfields > 0
8499 && TREE_THIS_VOLATILE (exp)
8500 && DECL_BIT_FIELD_TYPE (field)
8501 && DECL_MODE (field) != BLKmode)
8502 /* Volatile bitfields should be accessed in the mode of the
8503 field's type, not the mode computed based on the bit
8504 size. */
8505 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
8506 else if (!DECL_BIT_FIELD (field))
8507 {
8508 mode = DECL_MODE (field);
8509 /* For vector fields re-check the target flags, as DECL_MODE
8510 could have been set with different target flags than
8511 the current function has. */
8512 if (VECTOR_TYPE_P (TREE_TYPE (field))
8513 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
8514 mode = TYPE_MODE (TREE_TYPE (field));
8515 }
8516 else if (DECL_MODE (field) == BLKmode)
8517 blkmode_bitfield = true;
8518
8519 *punsignedp = DECL_UNSIGNED (field);
8520 }
8521 else if (TREE_CODE (exp) == BIT_FIELD_REF)
8522 {
8523 size_tree = TREE_OPERAND (exp, 1);
8524 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
8525 || TYPE_UNSIGNED (TREE_TYPE (exp)));
8526
8527 /* For vector element types with the correct size of access or for
8528 vector typed accesses use the mode of the access type. */
8529 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
8530 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
8531 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
8532 || VECTOR_TYPE_P (TREE_TYPE (exp)))
8533 mode = TYPE_MODE (TREE_TYPE (exp));
8534 }
8535 else
8536 {
8537 mode = TYPE_MODE (TREE_TYPE (exp));
8538 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
8539
8540 if (mode == BLKmode)
8541 size_tree = TYPE_SIZE (TREE_TYPE (exp));
8542 else
8543 *pbitsize = GET_MODE_BITSIZE (mode);
8544 }
8545
8546 if (size_tree != 0)
8547 {
8548 if (!poly_int_tree_p (size_tree, pbitsize))
8549 mode = BLKmode, *pbitsize = -1;
8550 }
8551
8552 *preversep = reverse_storage_order_for_component_p (exp);
8553
8554 /* Compute cumulative bit-offset for nested component-refs and array-refs,
8555 and find the ultimate containing object. */
8556 while (1)
8557 {
8558 switch (TREE_CODE (exp))
8559 {
8560 case BIT_FIELD_REF:
8561 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
8562 break;
8563
8564 case COMPONENT_REF:
8565 {
8566 tree field = TREE_OPERAND (exp, 1);
8567 tree this_offset = component_ref_field_offset (exp);
8568
8569 /* If this field hasn't been filled in yet, don't go past it.
8570 This should only happen when folding expressions made during
8571 type construction. */
8572 if (this_offset == 0)
8573 break;
8574
8575 offset = size_binop (PLUS_EXPR, offset, this_offset);
8576 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
8577
8578 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
8579 }
8580 break;
8581
8582 case ARRAY_REF:
8583 case ARRAY_RANGE_REF:
8584 {
8585 tree index = TREE_OPERAND (exp, 1);
8586 tree low_bound = array_ref_low_bound (exp);
8587 tree unit_size = array_ref_element_size (exp);
8588
8589 /* We assume all arrays have sizes that are a multiple of a byte.
8590 First subtract the lower bound, if any, in the type of the
8591 index, then convert to sizetype and multiply by the size of
8592 the array element. */
8593 if (! integer_zerop (low_bound))
8594 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
8595 index, low_bound);
8596
8597 offset = size_binop (PLUS_EXPR, offset,
8598 size_binop (MULT_EXPR,
8599 fold_convert (sizetype, index),
8600 unit_size));
8601 }
8602 break;
8603
8604 case REALPART_EXPR:
8605 break;
8606
8607 case IMAGPART_EXPR:
8608 bit_offset += *pbitsize;
8609 break;
8610
8611 case VIEW_CONVERT_EXPR:
8612 break;
8613
8614 case MEM_REF:
8615 /* Hand back the decl for MEM[&decl, off]. */
8616 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8617 {
8618 tree off = TREE_OPERAND (exp, 1);
8619 if (!integer_zerop (off))
8620 {
8621 poly_offset_int boff = mem_ref_offset (exp);
8622 boff <<= LOG2_BITS_PER_UNIT;
8623 bit_offset += boff;
8624 }
8625 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8626 }
8627 goto done;
8628
8629 default:
8630 goto done;
8631 }
8632
8633 /* If any reference in the chain is volatile, the effect is volatile. */
8634 if (TREE_THIS_VOLATILE (exp))
8635 *pvolatilep = 1;
8636
8637 exp = TREE_OPERAND (exp, 0);
8638 }
8639 done:
8640
8641 /* If OFFSET is constant, see if we can return the whole thing as a
8642 constant bit position. Make sure to handle overflow during
8643 this conversion. */
8644 if (poly_int_tree_p (offset))
8645 {
8646 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8647 TYPE_PRECISION (sizetype));
8648 tem <<= LOG2_BITS_PER_UNIT;
8649 tem += bit_offset;
8650 if (tem.to_shwi (pbitpos))
8651 *poffset = offset = NULL_TREE;
8652 }
8653
8654 /* Otherwise, split it up. */
8655 if (offset)
8656 {
8657 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8658 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8659 {
8660 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8661 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8662 offset = size_binop (PLUS_EXPR, offset,
8663 build_int_cst (sizetype, bytes.force_shwi ()));
8664 }
8665
8666 *poffset = offset;
8667 }
8668
8669 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8670 if (mode == VOIDmode
8671 && blkmode_bitfield
8672 && multiple_p (*pbitpos, BITS_PER_UNIT)
8673 && multiple_p (*pbitsize, BITS_PER_UNIT))
8674 *pmode = BLKmode;
8675 else
8676 *pmode = mode;
8677
8678 return exp;
8679 }
8680
8681 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8682
8683 static unsigned HOST_WIDE_INT
8684 target_align (const_tree target)
8685 {
8686 /* We might have a chain of nested references with intermediate misaligning
8687 bitfields components, so need to recurse to find out. */
8688
8689 unsigned HOST_WIDE_INT this_align, outer_align;
8690
8691 switch (TREE_CODE (target))
8692 {
8693 case BIT_FIELD_REF:
8694 return 1;
8695
8696 case COMPONENT_REF:
8697 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8698 outer_align = target_align (TREE_OPERAND (target, 0));
8699 return MIN (this_align, outer_align);
8700
8701 case ARRAY_REF:
8702 case ARRAY_RANGE_REF:
8703 this_align = TYPE_ALIGN (TREE_TYPE (target));
8704 outer_align = target_align (TREE_OPERAND (target, 0));
8705 return MIN (this_align, outer_align);
8706
8707 CASE_CONVERT:
8708 case NON_LVALUE_EXPR:
8709 case VIEW_CONVERT_EXPR:
8710 this_align = TYPE_ALIGN (TREE_TYPE (target));
8711 outer_align = target_align (TREE_OPERAND (target, 0));
8712 return MAX (this_align, outer_align);
8713
8714 default:
8715 return TYPE_ALIGN (TREE_TYPE (target));
8716 }
8717 }
8718
8719
8720 /* Given an rtx VALUE that may contain additions and multiplications, return
8721 an equivalent value that just refers to a register, memory, or constant.
8722 This is done by generating instructions to perform the arithmetic and
8723 returning a pseudo-register containing the value.
8724
8725 The returned value may be a REG, SUBREG, MEM or constant. */
8726
8727 rtx
8728 force_operand (rtx value, rtx target)
8729 {
8730 rtx op1, op2;
8731 /* Use subtarget as the target for operand 0 of a binary operation. */
8732 rtx subtarget = get_subtarget (target);
8733 enum rtx_code code = GET_CODE (value);
8734
8735 /* Check for subreg applied to an expression produced by loop optimizer. */
8736 if (code == SUBREG
8737 && !REG_P (SUBREG_REG (value))
8738 && !MEM_P (SUBREG_REG (value)))
8739 {
8740 value
8741 = simplify_gen_subreg (GET_MODE (value),
8742 force_reg (GET_MODE (SUBREG_REG (value)),
8743 force_operand (SUBREG_REG (value),
8744 NULL_RTX)),
8745 GET_MODE (SUBREG_REG (value)),
8746 SUBREG_BYTE (value));
8747 code = GET_CODE (value);
8748 }
8749
8750 /* Check for a PIC address load. */
8751 if ((code == PLUS || code == MINUS)
8752 && XEXP (value, 0) == pic_offset_table_rtx
8753 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8754 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8755 || GET_CODE (XEXP (value, 1)) == CONST))
8756 {
8757 if (!subtarget)
8758 subtarget = gen_reg_rtx (GET_MODE (value));
8759 emit_move_insn (subtarget, value);
8760 return subtarget;
8761 }
8762
8763 if (ARITHMETIC_P (value))
8764 {
8765 op2 = XEXP (value, 1);
8766 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8767 subtarget = 0;
8768 if (code == MINUS && CONST_INT_P (op2))
8769 {
8770 code = PLUS;
8771 op2 = negate_rtx (GET_MODE (value), op2);
8772 }
8773
8774 /* Check for an addition with OP2 a constant integer and our first
8775 operand a PLUS of a virtual register and something else. In that
8776 case, we want to emit the sum of the virtual register and the
8777 constant first and then add the other value. This allows virtual
8778 register instantiation to simply modify the constant rather than
8779 creating another one around this addition. */
8780 if (code == PLUS && CONST_INT_P (op2)
8781 && GET_CODE (XEXP (value, 0)) == PLUS
8782 && REG_P (XEXP (XEXP (value, 0), 0))
8783 && VIRTUAL_REGISTER_P (XEXP (XEXP (value, 0), 0)))
8784 {
8785 rtx temp = expand_simple_binop (GET_MODE (value), code,
8786 XEXP (XEXP (value, 0), 0), op2,
8787 subtarget, 0, OPTAB_LIB_WIDEN);
8788 return expand_simple_binop (GET_MODE (value), code, temp,
8789 force_operand (XEXP (XEXP (value,
8790 0), 1), 0),
8791 target, 0, OPTAB_LIB_WIDEN);
8792 }
8793
8794 op1 = force_operand (XEXP (value, 0), subtarget);
8795 op2 = force_operand (op2, NULL_RTX);
8796 switch (code)
8797 {
8798 case MULT:
8799 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8800 case DIV:
8801 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8802 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8803 target, 1, OPTAB_LIB_WIDEN);
8804 else
8805 return expand_divmod (0,
8806 FLOAT_MODE_P (GET_MODE (value))
8807 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8808 GET_MODE (value), op1, op2, target, 0);
8809 case MOD:
8810 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8811 target, 0);
8812 case UDIV:
8813 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8814 target, 1);
8815 case UMOD:
8816 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8817 target, 1);
8818 case ASHIFTRT:
8819 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8820 target, 0, OPTAB_LIB_WIDEN);
8821 default:
8822 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8823 target, 1, OPTAB_LIB_WIDEN);
8824 }
8825 }
8826 if (UNARY_P (value))
8827 {
8828 if (!target)
8829 target = gen_reg_rtx (GET_MODE (value));
8830 /* FIX or UNSIGNED_FIX with integral mode has unspecified rounding,
8831 while FIX with floating point mode rounds toward zero. So, some
8832 targets use expressions like (fix:SI (fix:DF (reg:DF ...)))
8833 to express rounding toward zero during the conversion to int.
8834 expand_fix isn't able to handle that, it can only handle
8835 FIX/UNSIGNED_FIX from floating point mode to integral one. */
8836 if ((code == FIX || code == UNSIGNED_FIX)
8837 && GET_CODE (XEXP (value, 0)) == FIX
8838 && (GET_MODE (XEXP (value, 0))
8839 == GET_MODE (XEXP (XEXP (value, 0), 0))))
8840 op1 = force_operand (XEXP (XEXP (value, 0), 0), NULL_RTX);
8841 else
8842 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8843 switch (code)
8844 {
8845 case ZERO_EXTEND:
8846 case SIGN_EXTEND:
8847 case TRUNCATE:
8848 case FLOAT_EXTEND:
8849 case FLOAT_TRUNCATE:
8850 convert_move (target, op1, code == ZERO_EXTEND);
8851 return target;
8852
8853 case FIX:
8854 case UNSIGNED_FIX:
8855 expand_fix (target, op1, code == UNSIGNED_FIX);
8856 return target;
8857
8858 case FLOAT:
8859 case UNSIGNED_FLOAT:
8860 expand_float (target, op1, code == UNSIGNED_FLOAT);
8861 return target;
8862
8863 default:
8864 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8865 }
8866 }
8867
8868 #ifdef INSN_SCHEDULING
8869 /* On machines that have insn scheduling, we want all memory reference to be
8870 explicit, so we need to deal with such paradoxical SUBREGs. */
8871 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8872 value
8873 = simplify_gen_subreg (GET_MODE (value),
8874 force_reg (GET_MODE (SUBREG_REG (value)),
8875 force_operand (SUBREG_REG (value),
8876 NULL_RTX)),
8877 GET_MODE (SUBREG_REG (value)),
8878 SUBREG_BYTE (value));
8879 #endif
8880
8881 return value;
8882 }
8883
8884 /* Subroutine of expand_expr: return true iff there is no way that
8885 EXP can reference X, which is being modified. TOP_P is nonzero if this
8886 call is going to be used to determine whether we need a temporary
8887 for EXP, as opposed to a recursive call to this function.
8888
8889 It is always safe for this routine to return false since it merely
8890 searches for optimization opportunities. */
8891
8892 bool
8893 safe_from_p (const_rtx x, tree exp, int top_p)
8894 {
8895 rtx exp_rtl = 0;
8896 int i, nops;
8897
8898 if (x == 0
8899 /* If EXP has varying size, we MUST use a target since we currently
8900 have no way of allocating temporaries of variable size
8901 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8902 So we assume here that something at a higher level has prevented a
8903 clash. This is somewhat bogus, but the best we can do. Only
8904 do this when X is BLKmode and when we are at the top level. */
8905 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8906 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8907 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8908 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8909 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8910 != INTEGER_CST)
8911 && GET_MODE (x) == BLKmode)
8912 /* If X is in the outgoing argument area, it is always safe. */
8913 || (MEM_P (x)
8914 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8915 || (GET_CODE (XEXP (x, 0)) == PLUS
8916 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8917 return true;
8918
8919 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8920 find the underlying pseudo. */
8921 if (GET_CODE (x) == SUBREG)
8922 {
8923 x = SUBREG_REG (x);
8924 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8925 return false;
8926 }
8927
8928 /* Now look at our tree code and possibly recurse. */
8929 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8930 {
8931 case tcc_declaration:
8932 exp_rtl = DECL_RTL_IF_SET (exp);
8933 break;
8934
8935 case tcc_constant:
8936 return true;
8937
8938 case tcc_exceptional:
8939 if (TREE_CODE (exp) == TREE_LIST)
8940 {
8941 while (1)
8942 {
8943 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8944 return false;
8945 exp = TREE_CHAIN (exp);
8946 if (!exp)
8947 return true;
8948 if (TREE_CODE (exp) != TREE_LIST)
8949 return safe_from_p (x, exp, 0);
8950 }
8951 }
8952 else if (TREE_CODE (exp) == CONSTRUCTOR)
8953 {
8954 constructor_elt *ce;
8955 unsigned HOST_WIDE_INT idx;
8956
8957 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8958 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8959 || !safe_from_p (x, ce->value, 0))
8960 return false;
8961 return true;
8962 }
8963 else if (TREE_CODE (exp) == ERROR_MARK)
8964 return true; /* An already-visited SAVE_EXPR? */
8965 else
8966 return false;
8967
8968 case tcc_statement:
8969 /* The only case we look at here is the DECL_INITIAL inside a
8970 DECL_EXPR. */
8971 return (TREE_CODE (exp) != DECL_EXPR
8972 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8973 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8974 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8975
8976 case tcc_binary:
8977 case tcc_comparison:
8978 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8979 return false;
8980 /* Fall through. */
8981
8982 case tcc_unary:
8983 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8984
8985 case tcc_expression:
8986 case tcc_reference:
8987 case tcc_vl_exp:
8988 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8989 the expression. If it is set, we conflict iff we are that rtx or
8990 both are in memory. Otherwise, we check all operands of the
8991 expression recursively. */
8992
8993 switch (TREE_CODE (exp))
8994 {
8995 case ADDR_EXPR:
8996 /* If the operand is static or we are static, we can't conflict.
8997 Likewise if we don't conflict with the operand at all. */
8998 if (staticp (TREE_OPERAND (exp, 0))
8999 || TREE_STATIC (exp)
9000 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
9001 return true;
9002
9003 /* Otherwise, the only way this can conflict is if we are taking
9004 the address of a DECL a that address if part of X, which is
9005 very rare. */
9006 exp = TREE_OPERAND (exp, 0);
9007 if (DECL_P (exp))
9008 {
9009 if (!DECL_RTL_SET_P (exp)
9010 || !MEM_P (DECL_RTL (exp)))
9011 return false;
9012 else
9013 exp_rtl = XEXP (DECL_RTL (exp), 0);
9014 }
9015 break;
9016
9017 case MEM_REF:
9018 if (MEM_P (x)
9019 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
9020 get_alias_set (exp)))
9021 return false;
9022 break;
9023
9024 case CALL_EXPR:
9025 /* Assume that the call will clobber all hard registers and
9026 all of memory. */
9027 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
9028 || MEM_P (x))
9029 return false;
9030 break;
9031
9032 case WITH_CLEANUP_EXPR:
9033 case CLEANUP_POINT_EXPR:
9034 /* Lowered by gimplify.cc. */
9035 gcc_unreachable ();
9036
9037 case SAVE_EXPR:
9038 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
9039
9040 default:
9041 break;
9042 }
9043
9044 /* If we have an rtx, we do not need to scan our operands. */
9045 if (exp_rtl)
9046 break;
9047
9048 nops = TREE_OPERAND_LENGTH (exp);
9049 for (i = 0; i < nops; i++)
9050 if (TREE_OPERAND (exp, i) != 0
9051 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
9052 return false;
9053
9054 break;
9055
9056 case tcc_type:
9057 /* Should never get a type here. */
9058 gcc_unreachable ();
9059 }
9060
9061 /* If we have an rtl, find any enclosed object. Then see if we conflict
9062 with it. */
9063 if (exp_rtl)
9064 {
9065 if (GET_CODE (exp_rtl) == SUBREG)
9066 {
9067 exp_rtl = SUBREG_REG (exp_rtl);
9068 if (REG_P (exp_rtl)
9069 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
9070 return false;
9071 }
9072
9073 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
9074 are memory and they conflict. */
9075 return ! (rtx_equal_p (x, exp_rtl)
9076 || (MEM_P (x) && MEM_P (exp_rtl)
9077 && true_dependence (exp_rtl, VOIDmode, x)));
9078 }
9079
9080 /* If we reach here, it is safe. */
9081 return true;
9082 }
9083
9084
9085 /* Return the highest power of two that EXP is known to be a multiple of.
9086 This is used in updating alignment of MEMs in array references. */
9087
9088 unsigned HOST_WIDE_INT
9089 highest_pow2_factor (const_tree exp)
9090 {
9091 unsigned HOST_WIDE_INT ret;
9092 int trailing_zeros = tree_ctz (exp);
9093 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
9094 return BIGGEST_ALIGNMENT;
9095 ret = HOST_WIDE_INT_1U << trailing_zeros;
9096 if (ret > BIGGEST_ALIGNMENT)
9097 return BIGGEST_ALIGNMENT;
9098 return ret;
9099 }
9100
9101 /* Similar, except that the alignment requirements of TARGET are
9102 taken into account. Assume it is at least as aligned as its
9103 type, unless it is a COMPONENT_REF in which case the layout of
9104 the structure gives the alignment. */
9105
9106 static unsigned HOST_WIDE_INT
9107 highest_pow2_factor_for_target (const_tree target, const_tree exp)
9108 {
9109 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
9110 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
9111
9112 return MAX (factor, talign);
9113 }
9114
9115 /* Convert the tree comparison code TCODE to the rtl one where the
9116 signedness is UNSIGNEDP. */
9117
9118 static enum rtx_code
9119 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
9120 {
9121 enum rtx_code code;
9122 switch (tcode)
9123 {
9124 case EQ_EXPR:
9125 code = EQ;
9126 break;
9127 case NE_EXPR:
9128 code = NE;
9129 break;
9130 case LT_EXPR:
9131 code = unsignedp ? LTU : LT;
9132 break;
9133 case LE_EXPR:
9134 code = unsignedp ? LEU : LE;
9135 break;
9136 case GT_EXPR:
9137 code = unsignedp ? GTU : GT;
9138 break;
9139 case GE_EXPR:
9140 code = unsignedp ? GEU : GE;
9141 break;
9142 case UNORDERED_EXPR:
9143 code = UNORDERED;
9144 break;
9145 case ORDERED_EXPR:
9146 code = ORDERED;
9147 break;
9148 case UNLT_EXPR:
9149 code = UNLT;
9150 break;
9151 case UNLE_EXPR:
9152 code = UNLE;
9153 break;
9154 case UNGT_EXPR:
9155 code = UNGT;
9156 break;
9157 case UNGE_EXPR:
9158 code = UNGE;
9159 break;
9160 case UNEQ_EXPR:
9161 code = UNEQ;
9162 break;
9163 case LTGT_EXPR:
9164 code = LTGT;
9165 break;
9166
9167 default:
9168 gcc_unreachable ();
9169 }
9170 return code;
9171 }
9172
9173 /* Subroutine of expand_expr. Expand the two operands of a binary
9174 expression EXP0 and EXP1 placing the results in OP0 and OP1.
9175 The value may be stored in TARGET if TARGET is nonzero. The
9176 MODIFIER argument is as documented by expand_expr. */
9177
9178 void
9179 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
9180 enum expand_modifier modifier)
9181 {
9182 if (! safe_from_p (target, exp1, 1))
9183 target = 0;
9184 if (operand_equal_p (exp0, exp1, 0))
9185 {
9186 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
9187 *op1 = copy_rtx (*op0);
9188 }
9189 else
9190 {
9191 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
9192 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
9193 }
9194 }
9195
9196
9197 /* Return a MEM that contains constant EXP. DEFER is as for
9198 output_constant_def and MODIFIER is as for expand_expr. */
9199
9200 static rtx
9201 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
9202 {
9203 rtx mem;
9204
9205 mem = output_constant_def (exp, defer);
9206 if (modifier != EXPAND_INITIALIZER)
9207 mem = use_anchored_address (mem);
9208 return mem;
9209 }
9210
9211 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
9212 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9213
9214 static rtx
9215 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
9216 enum expand_modifier modifier, addr_space_t as)
9217 {
9218 rtx result, subtarget;
9219 tree inner, offset;
9220 poly_int64 bitsize, bitpos;
9221 int unsignedp, reversep, volatilep = 0;
9222 machine_mode mode1;
9223
9224 /* If we are taking the address of a constant and are at the top level,
9225 we have to use output_constant_def since we can't call force_const_mem
9226 at top level. */
9227 /* ??? This should be considered a front-end bug. We should not be
9228 generating ADDR_EXPR of something that isn't an LVALUE. The only
9229 exception here is STRING_CST. */
9230 if (CONSTANT_CLASS_P (exp))
9231 {
9232 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
9233 if (modifier < EXPAND_SUM)
9234 result = force_operand (result, target);
9235 return result;
9236 }
9237
9238 /* Everything must be something allowed by is_gimple_addressable. */
9239 switch (TREE_CODE (exp))
9240 {
9241 case INDIRECT_REF:
9242 /* This case will happen via recursion for &a->b. */
9243 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
9244
9245 case MEM_REF:
9246 {
9247 tree tem = TREE_OPERAND (exp, 0);
9248 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9249 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
9250 return expand_expr (tem, target, tmode, modifier);
9251 }
9252
9253 case TARGET_MEM_REF:
9254 return addr_for_mem_ref (exp, as, true);
9255
9256 case CONST_DECL:
9257 /* Expand the initializer like constants above. */
9258 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
9259 0, modifier), 0);
9260 if (modifier < EXPAND_SUM)
9261 result = force_operand (result, target);
9262 return result;
9263
9264 case REALPART_EXPR:
9265 /* The real part of the complex number is always first, therefore
9266 the address is the same as the address of the parent object. */
9267 offset = 0;
9268 bitpos = 0;
9269 inner = TREE_OPERAND (exp, 0);
9270 break;
9271
9272 case IMAGPART_EXPR:
9273 /* The imaginary part of the complex number is always second.
9274 The expression is therefore always offset by the size of the
9275 scalar type. */
9276 offset = 0;
9277 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
9278 inner = TREE_OPERAND (exp, 0);
9279 break;
9280
9281 case COMPOUND_LITERAL_EXPR:
9282 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
9283 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
9284 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
9285 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
9286 the initializers aren't gimplified. */
9287 if (COMPOUND_LITERAL_EXPR_DECL (exp)
9288 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
9289 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
9290 target, tmode, modifier, as);
9291 /* FALLTHRU */
9292 default:
9293 /* If the object is a DECL, then expand it for its rtl. Don't bypass
9294 expand_expr, as that can have various side effects; LABEL_DECLs for
9295 example, may not have their DECL_RTL set yet. Expand the rtl of
9296 CONSTRUCTORs too, which should yield a memory reference for the
9297 constructor's contents. Assume language specific tree nodes can
9298 be expanded in some interesting way. */
9299 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
9300 if (DECL_P (exp)
9301 || TREE_CODE (exp) == CONSTRUCTOR
9302 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
9303 {
9304 result = expand_expr (exp, target, tmode,
9305 modifier == EXPAND_INITIALIZER
9306 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
9307
9308 /* If the DECL isn't in memory, then the DECL wasn't properly
9309 marked TREE_ADDRESSABLE, which will be either a front-end
9310 or a tree optimizer bug. */
9311
9312 gcc_assert (MEM_P (result));
9313 result = XEXP (result, 0);
9314
9315 /* ??? Is this needed anymore? */
9316 if (DECL_P (exp))
9317 TREE_USED (exp) = 1;
9318
9319 if (modifier != EXPAND_INITIALIZER
9320 && modifier != EXPAND_CONST_ADDRESS
9321 && modifier != EXPAND_SUM)
9322 result = force_operand (result, target);
9323 return result;
9324 }
9325
9326 /* Pass FALSE as the last argument to get_inner_reference although
9327 we are expanding to RTL. The rationale is that we know how to
9328 handle "aligning nodes" here: we can just bypass them because
9329 they won't change the final object whose address will be returned
9330 (they actually exist only for that purpose). */
9331 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
9332 &unsignedp, &reversep, &volatilep);
9333 break;
9334 }
9335
9336 /* We must have made progress. */
9337 gcc_assert (inner != exp);
9338
9339 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
9340 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
9341 inner alignment, force the inner to be sufficiently aligned. */
9342 if (CONSTANT_CLASS_P (inner)
9343 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
9344 {
9345 inner = copy_node (inner);
9346 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
9347 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
9348 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
9349 }
9350 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
9351
9352 if (offset)
9353 {
9354 rtx tmp;
9355
9356 if (modifier != EXPAND_NORMAL)
9357 result = force_operand (result, NULL);
9358 tmp = expand_expr (offset, NULL_RTX, tmode,
9359 modifier == EXPAND_INITIALIZER
9360 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
9361
9362 /* expand_expr is allowed to return an object in a mode other
9363 than TMODE. If it did, we need to convert. */
9364 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
9365 tmp = convert_modes (tmode, GET_MODE (tmp),
9366 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
9367 result = convert_memory_address_addr_space (tmode, result, as);
9368 tmp = convert_memory_address_addr_space (tmode, tmp, as);
9369
9370 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9371 result = simplify_gen_binary (PLUS, tmode, result, tmp);
9372 else
9373 {
9374 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
9375 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
9376 1, OPTAB_LIB_WIDEN);
9377 }
9378 }
9379
9380 if (maybe_ne (bitpos, 0))
9381 {
9382 /* Someone beforehand should have rejected taking the address
9383 of an object that isn't byte-aligned. */
9384 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
9385 result = convert_memory_address_addr_space (tmode, result, as);
9386 result = plus_constant (tmode, result, bytepos);
9387 if (modifier < EXPAND_SUM)
9388 result = force_operand (result, target);
9389 }
9390
9391 return result;
9392 }
9393
9394 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
9395 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
9396
9397 static rtx
9398 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
9399 enum expand_modifier modifier)
9400 {
9401 addr_space_t as = ADDR_SPACE_GENERIC;
9402 scalar_int_mode address_mode = Pmode;
9403 scalar_int_mode pointer_mode = ptr_mode;
9404 machine_mode rmode;
9405 rtx result;
9406
9407 /* Target mode of VOIDmode says "whatever's natural". */
9408 if (tmode == VOIDmode)
9409 tmode = TYPE_MODE (TREE_TYPE (exp));
9410
9411 if (POINTER_TYPE_P (TREE_TYPE (exp)))
9412 {
9413 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
9414 address_mode = targetm.addr_space.address_mode (as);
9415 pointer_mode = targetm.addr_space.pointer_mode (as);
9416 }
9417
9418 /* We can get called with some Weird Things if the user does silliness
9419 like "(short) &a". In that case, convert_memory_address won't do
9420 the right thing, so ignore the given target mode. */
9421 scalar_int_mode new_tmode = (tmode == pointer_mode
9422 ? pointer_mode
9423 : address_mode);
9424
9425 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
9426 new_tmode, modifier, as);
9427
9428 /* Despite expand_expr claims concerning ignoring TMODE when not
9429 strictly convenient, stuff breaks if we don't honor it. Note
9430 that combined with the above, we only do this for pointer modes. */
9431 rmode = GET_MODE (result);
9432 if (rmode == VOIDmode)
9433 rmode = new_tmode;
9434 if (rmode != new_tmode)
9435 result = convert_memory_address_addr_space (new_tmode, result, as);
9436
9437 return result;
9438 }
9439
9440 /* Generate code for computing CONSTRUCTOR EXP.
9441 An rtx for the computed value is returned. If AVOID_TEMP_MEM
9442 is TRUE, instead of creating a temporary variable in memory
9443 NULL is returned and the caller needs to handle it differently. */
9444
9445 static rtx
9446 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
9447 bool avoid_temp_mem)
9448 {
9449 tree type = TREE_TYPE (exp);
9450 machine_mode mode = TYPE_MODE (type);
9451
9452 /* Try to avoid creating a temporary at all. This is possible
9453 if all of the initializer is zero.
9454 FIXME: try to handle all [0..255] initializers we can handle
9455 with memset. */
9456 if (TREE_STATIC (exp)
9457 && !TREE_ADDRESSABLE (exp)
9458 && target != 0 && mode == BLKmode
9459 && all_zeros_p (exp))
9460 {
9461 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
9462 return target;
9463 }
9464
9465 /* All elts simple constants => refer to a constant in memory. But
9466 if this is a non-BLKmode mode, let it store a field at a time
9467 since that should make a CONST_INT, CONST_WIDE_INT or
9468 CONST_DOUBLE when we fold. Likewise, if we have a target we can
9469 use, it is best to store directly into the target unless the type
9470 is large enough that memcpy will be used. If we are making an
9471 initializer and all operands are constant, put it in memory as
9472 well.
9473
9474 FIXME: Avoid trying to fill vector constructors piece-meal.
9475 Output them with output_constant_def below unless we're sure
9476 they're zeros. This should go away when vector initializers
9477 are treated like VECTOR_CST instead of arrays. */
9478 if ((TREE_STATIC (exp)
9479 && ((mode == BLKmode
9480 && ! (target != 0 && safe_from_p (target, exp, 1)))
9481 || TREE_ADDRESSABLE (exp)
9482 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9483 && (! can_move_by_pieces
9484 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9485 TYPE_ALIGN (type)))
9486 && ! mostly_zeros_p (exp))))
9487 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
9488 && TREE_CONSTANT (exp)))
9489 {
9490 rtx constructor;
9491
9492 if (avoid_temp_mem)
9493 return NULL_RTX;
9494
9495 constructor = expand_expr_constant (exp, 1, modifier);
9496
9497 if (modifier != EXPAND_CONST_ADDRESS
9498 && modifier != EXPAND_INITIALIZER
9499 && modifier != EXPAND_SUM)
9500 constructor = validize_mem (constructor);
9501
9502 return constructor;
9503 }
9504
9505 /* If the CTOR is available in static storage and not mostly
9506 zeros and we can move it by pieces prefer to do so since
9507 that's usually more efficient than performing a series of
9508 stores from immediates. */
9509 if (avoid_temp_mem
9510 && TREE_STATIC (exp)
9511 && TREE_CONSTANT (exp)
9512 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9513 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
9514 TYPE_ALIGN (type))
9515 && ! mostly_zeros_p (exp))
9516 return NULL_RTX;
9517
9518 /* Handle calls that pass values in multiple non-contiguous
9519 locations. The Irix 6 ABI has examples of this. */
9520 if (target == 0 || ! safe_from_p (target, exp, 1)
9521 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
9522 /* Also make a temporary if the store is to volatile memory, to
9523 avoid individual accesses to aggregate members. */
9524 || (GET_CODE (target) == MEM
9525 && MEM_VOLATILE_P (target)
9526 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
9527 {
9528 if (avoid_temp_mem)
9529 return NULL_RTX;
9530
9531 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
9532 }
9533
9534 store_constructor (exp, target, 0, int_expr_size (exp), false);
9535 return target;
9536 }
9537
9538
9539 /* expand_expr: generate code for computing expression EXP.
9540 An rtx for the computed value is returned. The value is never null.
9541 In the case of a void EXP, const0_rtx is returned.
9542
9543 The value may be stored in TARGET if TARGET is nonzero.
9544 TARGET is just a suggestion; callers must assume that
9545 the rtx returned may not be the same as TARGET.
9546
9547 If TARGET is CONST0_RTX, it means that the value will be ignored.
9548
9549 If TMODE is not VOIDmode, it suggests generating the
9550 result in mode TMODE. But this is done only when convenient.
9551 Otherwise, TMODE is ignored and the value generated in its natural mode.
9552 TMODE is just a suggestion; callers must assume that
9553 the rtx returned may not have mode TMODE.
9554
9555 Note that TARGET may have neither TMODE nor MODE. In that case, it
9556 probably will not be used.
9557
9558 If MODIFIER is EXPAND_SUM then when EXP is an addition
9559 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
9560 or a nest of (PLUS ...) and (MINUS ...) where the terms are
9561 products as above, or REG or MEM, or constant.
9562 Ordinarily in such cases we would output mul or add instructions
9563 and then return a pseudo reg containing the sum.
9564
9565 EXPAND_INITIALIZER is much like EXPAND_SUM except that
9566 it also marks a label as absolutely required (it can't be dead).
9567 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
9568 This is used for outputting expressions used in initializers.
9569
9570 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
9571 with a constant address even if that address is not normally legitimate.
9572 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
9573
9574 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
9575 a call parameter. Such targets require special care as we haven't yet
9576 marked TARGET so that it's safe from being trashed by libcalls. We
9577 don't want to use TARGET for anything but the final result;
9578 Intermediate values must go elsewhere. Additionally, calls to
9579 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
9580
9581 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
9582 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
9583 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
9584 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
9585 recursively.
9586 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9587 then *ALT_RTL is set to TARGET (before legitimziation).
9588
9589 If INNER_REFERENCE_P is true, we are expanding an inner reference.
9590 In this case, we don't adjust a returned MEM rtx that wouldn't be
9591 sufficiently aligned for its mode; instead, it's up to the caller
9592 to deal with it afterwards. This is used to make sure that unaligned
9593 base objects for which out-of-bounds accesses are supported, for
9594 example record types with trailing arrays, aren't realigned behind
9595 the back of the caller.
9596 The normal operating mode is to pass FALSE for this parameter. */
9597
9598 rtx
9599 expand_expr_real (tree exp, rtx target, machine_mode tmode,
9600 enum expand_modifier modifier, rtx *alt_rtl,
9601 bool inner_reference_p)
9602 {
9603 rtx ret;
9604
9605 /* Handle ERROR_MARK before anybody tries to access its type. */
9606 if (TREE_CODE (exp) == ERROR_MARK
9607 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
9608 {
9609 ret = CONST0_RTX (tmode);
9610 return ret ? ret : const0_rtx;
9611 }
9612
9613 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9614 inner_reference_p);
9615 return ret;
9616 }
9617
9618 /* Try to expand the conditional expression which is represented by
9619 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9620 return the rtl reg which represents the result. Otherwise return
9621 NULL_RTX. */
9622
9623 static rtx
9624 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9625 tree treeop1 ATTRIBUTE_UNUSED,
9626 tree treeop2 ATTRIBUTE_UNUSED)
9627 {
9628 rtx insn;
9629 rtx op00, op01, op1, op2;
9630 enum rtx_code comparison_code;
9631 machine_mode comparison_mode;
9632 gimple *srcstmt;
9633 rtx temp;
9634 tree type = TREE_TYPE (treeop1);
9635 int unsignedp = TYPE_UNSIGNED (type);
9636 machine_mode mode = TYPE_MODE (type);
9637 machine_mode orig_mode = mode;
9638 static bool expanding_cond_expr_using_cmove = false;
9639
9640 /* Conditional move expansion can end up TERing two operands which,
9641 when recursively hitting conditional expressions can result in
9642 exponential behavior if the cmove expansion ultimatively fails.
9643 It's hardly profitable to TER a cmove into a cmove so avoid doing
9644 that by failing early if we end up recursing. */
9645 if (expanding_cond_expr_using_cmove)
9646 return NULL_RTX;
9647
9648 /* If we cannot do a conditional move on the mode, try doing it
9649 with the promoted mode. */
9650 if (!can_conditionally_move_p (mode))
9651 {
9652 mode = promote_mode (type, mode, &unsignedp);
9653 if (!can_conditionally_move_p (mode))
9654 return NULL_RTX;
9655 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9656 }
9657 else
9658 temp = assign_temp (type, 0, 1);
9659
9660 expanding_cond_expr_using_cmove = true;
9661 start_sequence ();
9662 expand_operands (treeop1, treeop2,
9663 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9664 EXPAND_NORMAL);
9665
9666 if (TREE_CODE (treeop0) == SSA_NAME
9667 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison))
9668 && !VECTOR_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (srcstmt))))
9669 {
9670 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9671 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9672 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9673 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9674 comparison_mode = TYPE_MODE (type);
9675 unsignedp = TYPE_UNSIGNED (type);
9676 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9677 }
9678 else if (COMPARISON_CLASS_P (treeop0)
9679 && !VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (treeop0, 0))))
9680 {
9681 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9682 enum tree_code cmpcode = TREE_CODE (treeop0);
9683 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9684 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9685 unsignedp = TYPE_UNSIGNED (type);
9686 comparison_mode = TYPE_MODE (type);
9687 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9688 }
9689 else
9690 {
9691 op00 = expand_normal (treeop0);
9692 op01 = const0_rtx;
9693 comparison_code = NE;
9694 comparison_mode = GET_MODE (op00);
9695 if (comparison_mode == VOIDmode)
9696 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9697 }
9698 expanding_cond_expr_using_cmove = false;
9699
9700 if (GET_MODE (op1) != mode)
9701 op1 = gen_lowpart (mode, op1);
9702
9703 if (GET_MODE (op2) != mode)
9704 op2 = gen_lowpart (mode, op2);
9705
9706 /* Try to emit the conditional move. */
9707 insn = emit_conditional_move (temp,
9708 { comparison_code, op00, op01,
9709 comparison_mode },
9710 op1, op2, mode,
9711 unsignedp);
9712
9713 /* If we could do the conditional move, emit the sequence,
9714 and return. */
9715 if (insn)
9716 {
9717 rtx_insn *seq = get_insns ();
9718 end_sequence ();
9719 emit_insn (seq);
9720 return convert_modes (orig_mode, mode, temp, 0);
9721 }
9722
9723 /* Otherwise discard the sequence and fall back to code with
9724 branches. */
9725 end_sequence ();
9726 return NULL_RTX;
9727 }
9728
9729 /* A helper function for expand_expr_real_2 to be used with a
9730 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9731 is nonzero, with alignment ALIGN in bits.
9732 Store the value at TARGET if possible (if TARGET is nonzero).
9733 Regardless of TARGET, we return the rtx for where the value is placed.
9734 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9735 then *ALT_RTL is set to TARGET (before legitimziation). */
9736
9737 static rtx
9738 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9739 unsigned int align, rtx target, rtx *alt_rtl)
9740 {
9741 enum insn_code icode;
9742
9743 if ((icode = optab_handler (movmisalign_optab, mode))
9744 != CODE_FOR_nothing)
9745 {
9746 class expand_operand ops[2];
9747
9748 /* We've already validated the memory, and we're creating a
9749 new pseudo destination. The predicates really can't fail,
9750 nor can the generator. */
9751 create_output_operand (&ops[0], NULL_RTX, mode);
9752 create_fixed_operand (&ops[1], temp);
9753 expand_insn (icode, 2, ops);
9754 temp = ops[0].value;
9755 }
9756 else if (targetm.slow_unaligned_access (mode, align))
9757 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9758 0, unsignedp, target,
9759 mode, mode, false, alt_rtl);
9760 return temp;
9761 }
9762
9763 /* Helper function of expand_expr_2, expand a division or modulo.
9764 op0 and op1 should be already expanded treeop0 and treeop1, using
9765 expand_operands. */
9766
9767 static rtx
9768 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9769 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9770 {
9771 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9772 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9773 if (SCALAR_INT_MODE_P (mode)
9774 && optimize >= 2
9775 && get_range_pos_neg (treeop0) == 1
9776 && get_range_pos_neg (treeop1) == 1)
9777 {
9778 /* If both arguments are known to be positive when interpreted
9779 as signed, we can expand it as both signed and unsigned
9780 division or modulo. Choose the cheaper sequence in that case. */
9781 bool speed_p = optimize_insn_for_speed_p ();
9782 do_pending_stack_adjust ();
9783 start_sequence ();
9784 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9785 rtx_insn *uns_insns = get_insns ();
9786 end_sequence ();
9787 start_sequence ();
9788 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9789 rtx_insn *sgn_insns = get_insns ();
9790 end_sequence ();
9791 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9792 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9793 bool was_tie = false;
9794
9795 /* If costs are the same then use as tie breaker the other other
9796 factor. */
9797 if (uns_cost == sgn_cost)
9798 {
9799 uns_cost = seq_cost (uns_insns, !speed_p);
9800 sgn_cost = seq_cost (sgn_insns, !speed_p);
9801 was_tie = true;
9802 }
9803
9804 if (dump_file && (dump_flags & TDF_DETAILS))
9805 fprintf (dump_file, ";; positive division:%s unsigned cost: %u; "
9806 "signed cost: %u\n",
9807 was_tie ? " (needed tie breaker)" : "", uns_cost, sgn_cost);
9808
9809 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9810 {
9811 emit_insn (uns_insns);
9812 return uns_ret;
9813 }
9814 emit_insn (sgn_insns);
9815 return sgn_ret;
9816 }
9817 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9818 }
9819
9820 rtx
9821 expand_expr_real_2 (const_sepops ops, rtx target, machine_mode tmode,
9822 enum expand_modifier modifier)
9823 {
9824 rtx op0, op1, op2, temp;
9825 rtx_code_label *lab;
9826 tree type;
9827 int unsignedp;
9828 machine_mode mode;
9829 scalar_int_mode int_mode;
9830 enum tree_code code = ops->code;
9831 optab this_optab;
9832 rtx subtarget, original_target;
9833 int ignore;
9834 bool reduce_bit_field;
9835 location_t loc = ops->location;
9836 tree treeop0, treeop1, treeop2;
9837 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9838 ? reduce_to_bit_field_precision ((expr), \
9839 target, \
9840 type) \
9841 : (expr))
9842
9843 type = ops->type;
9844 mode = TYPE_MODE (type);
9845 unsignedp = TYPE_UNSIGNED (type);
9846
9847 treeop0 = ops->op0;
9848 treeop1 = ops->op1;
9849 treeop2 = ops->op2;
9850
9851 /* We should be called only on simple (binary or unary) expressions,
9852 exactly those that are valid in gimple expressions that aren't
9853 GIMPLE_SINGLE_RHS (or invalid). */
9854 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9855 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9856 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9857
9858 ignore = (target == const0_rtx
9859 || ((CONVERT_EXPR_CODE_P (code)
9860 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9861 && TREE_CODE (type) == VOID_TYPE));
9862
9863 /* We should be called only if we need the result. */
9864 gcc_assert (!ignore);
9865
9866 /* An operation in what may be a bit-field type needs the
9867 result to be reduced to the precision of the bit-field type,
9868 which is narrower than that of the type's mode. */
9869 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9870 && !type_has_mode_precision_p (type));
9871
9872 if (reduce_bit_field
9873 && (modifier == EXPAND_STACK_PARM
9874 || (target && GET_MODE (target) != mode)))
9875 target = 0;
9876
9877 /* Use subtarget as the target for operand 0 of a binary operation. */
9878 subtarget = get_subtarget (target);
9879 original_target = target;
9880
9881 switch (code)
9882 {
9883 case NON_LVALUE_EXPR:
9884 case PAREN_EXPR:
9885 CASE_CONVERT:
9886 if (treeop0 == error_mark_node)
9887 return const0_rtx;
9888
9889 if (TREE_CODE (type) == UNION_TYPE)
9890 {
9891 tree valtype = TREE_TYPE (treeop0);
9892
9893 /* If both input and output are BLKmode, this conversion isn't doing
9894 anything except possibly changing memory attribute. */
9895 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9896 {
9897 rtx result = expand_expr (treeop0, target, tmode,
9898 modifier);
9899
9900 result = copy_rtx (result);
9901 set_mem_attributes (result, type, 0);
9902 return result;
9903 }
9904
9905 if (target == 0)
9906 {
9907 if (TYPE_MODE (type) != BLKmode)
9908 target = gen_reg_rtx (TYPE_MODE (type));
9909 else
9910 target = assign_temp (type, 1, 1);
9911 }
9912
9913 if (MEM_P (target))
9914 /* Store data into beginning of memory target. */
9915 store_expr (treeop0,
9916 adjust_address (target, TYPE_MODE (valtype), 0),
9917 modifier == EXPAND_STACK_PARM,
9918 false, TYPE_REVERSE_STORAGE_ORDER (type));
9919
9920 else
9921 {
9922 gcc_assert (REG_P (target)
9923 && !TYPE_REVERSE_STORAGE_ORDER (type));
9924
9925 /* Store this field into a union of the proper type. */
9926 poly_uint64 op0_size
9927 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9928 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9929 store_field (target,
9930 /* The conversion must be constructed so that
9931 we know at compile time how many bits
9932 to preserve. */
9933 ordered_min (op0_size, union_size),
9934 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9935 false, false);
9936 }
9937
9938 /* Return the entire union. */
9939 return target;
9940 }
9941
9942 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9943 {
9944 op0 = expand_expr (treeop0, target, VOIDmode,
9945 modifier);
9946
9947 return REDUCE_BIT_FIELD (op0);
9948 }
9949
9950 op0 = expand_expr (treeop0, NULL_RTX, mode,
9951 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9952 if (GET_MODE (op0) == mode)
9953 ;
9954
9955 /* If OP0 is a constant, just convert it into the proper mode. */
9956 else if (CONSTANT_P (op0))
9957 {
9958 tree inner_type = TREE_TYPE (treeop0);
9959 machine_mode inner_mode = GET_MODE (op0);
9960
9961 if (inner_mode == VOIDmode)
9962 inner_mode = TYPE_MODE (inner_type);
9963
9964 if (modifier == EXPAND_INITIALIZER)
9965 op0 = force_lowpart_subreg (mode, op0, inner_mode);
9966 else
9967 op0 = convert_modes (mode, inner_mode, op0,
9968 TYPE_UNSIGNED (inner_type));
9969 }
9970
9971 else if (modifier == EXPAND_INITIALIZER)
9972 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9973 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9974
9975 else if (target == 0)
9976 op0 = convert_to_mode (mode, op0,
9977 TYPE_UNSIGNED (TREE_TYPE
9978 (treeop0)));
9979 else
9980 {
9981 convert_move (target, op0,
9982 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9983 op0 = target;
9984 }
9985
9986 return REDUCE_BIT_FIELD (op0);
9987
9988 case ADDR_SPACE_CONVERT_EXPR:
9989 {
9990 tree treeop0_type = TREE_TYPE (treeop0);
9991
9992 gcc_assert (POINTER_TYPE_P (type));
9993 gcc_assert (POINTER_TYPE_P (treeop0_type));
9994
9995 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9996 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9997
9998 /* Conversions between pointers to the same address space should
9999 have been implemented via CONVERT_EXPR / NOP_EXPR. */
10000 gcc_assert (as_to != as_from);
10001
10002 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10003
10004 /* Ask target code to handle conversion between pointers
10005 to overlapping address spaces. */
10006 if (targetm.addr_space.subset_p (as_to, as_from)
10007 || targetm.addr_space.subset_p (as_from, as_to))
10008 {
10009 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
10010 }
10011 else
10012 {
10013 /* For disjoint address spaces, converting anything but a null
10014 pointer invokes undefined behavior. We truncate or extend the
10015 value as if we'd converted via integers, which handles 0 as
10016 required, and all others as the programmer likely expects. */
10017 #ifndef POINTERS_EXTEND_UNSIGNED
10018 const int POINTERS_EXTEND_UNSIGNED = 1;
10019 #endif
10020 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
10021 op0, POINTERS_EXTEND_UNSIGNED);
10022 }
10023 gcc_assert (op0);
10024 return op0;
10025 }
10026
10027 case POINTER_PLUS_EXPR:
10028 /* Even though the sizetype mode and the pointer's mode can be different
10029 expand is able to handle this correctly and get the correct result out
10030 of the PLUS_EXPR code. */
10031 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
10032 if sizetype precision is smaller than pointer precision. */
10033 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
10034 treeop1 = fold_convert_loc (loc, type,
10035 fold_convert_loc (loc, ssizetype,
10036 treeop1));
10037 /* If sizetype precision is larger than pointer precision, truncate the
10038 offset to have matching modes. */
10039 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
10040 treeop1 = fold_convert_loc (loc, type, treeop1);
10041 /* FALLTHRU */
10042
10043 case PLUS_EXPR:
10044 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
10045 something else, make sure we add the register to the constant and
10046 then to the other thing. This case can occur during strength
10047 reduction and doing it this way will produce better code if the
10048 frame pointer or argument pointer is eliminated.
10049
10050 fold-const.cc will ensure that the constant is always in the inner
10051 PLUS_EXPR, so the only case we need to do anything about is if
10052 sp, ap, or fp is our second argument, in which case we must swap
10053 the innermost first argument and our second argument. */
10054
10055 if (TREE_CODE (treeop0) == PLUS_EXPR
10056 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
10057 && VAR_P (treeop1)
10058 && (DECL_RTL (treeop1) == frame_pointer_rtx
10059 || DECL_RTL (treeop1) == stack_pointer_rtx
10060 || DECL_RTL (treeop1) == arg_pointer_rtx))
10061 {
10062 gcc_unreachable ();
10063 }
10064
10065 /* If the result is to be ptr_mode and we are adding an integer to
10066 something, we might be forming a constant. So try to use
10067 plus_constant. If it produces a sum and we can't accept it,
10068 use force_operand. This allows P = &ARR[const] to generate
10069 efficient code on machines where a SYMBOL_REF is not a valid
10070 address.
10071
10072 If this is an EXPAND_SUM call, always return the sum. */
10073 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
10074 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
10075 {
10076 if (modifier == EXPAND_STACK_PARM)
10077 target = 0;
10078 if (TREE_CODE (treeop0) == INTEGER_CST
10079 && HWI_COMPUTABLE_MODE_P (mode)
10080 && TREE_CONSTANT (treeop1))
10081 {
10082 rtx constant_part;
10083 HOST_WIDE_INT wc;
10084 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
10085
10086 op1 = expand_expr (treeop1, subtarget, VOIDmode,
10087 EXPAND_SUM);
10088 /* Use wi::shwi to ensure that the constant is
10089 truncated according to the mode of OP1, then sign extended
10090 to a HOST_WIDE_INT. Using the constant directly can result
10091 in non-canonical RTL in a 64x32 cross compile. */
10092 wc = TREE_INT_CST_LOW (treeop0);
10093 constant_part =
10094 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
10095 op1 = plus_constant (mode, op1, INTVAL (constant_part));
10096 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
10097 op1 = force_operand (op1, target);
10098 return REDUCE_BIT_FIELD (op1);
10099 }
10100
10101 else if (TREE_CODE (treeop1) == INTEGER_CST
10102 && HWI_COMPUTABLE_MODE_P (mode)
10103 && TREE_CONSTANT (treeop0))
10104 {
10105 rtx constant_part;
10106 HOST_WIDE_INT wc;
10107 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
10108
10109 op0 = expand_expr (treeop0, subtarget, VOIDmode,
10110 (modifier == EXPAND_INITIALIZER
10111 ? EXPAND_INITIALIZER : EXPAND_SUM));
10112 if (! CONSTANT_P (op0))
10113 {
10114 op1 = expand_expr (treeop1, NULL_RTX,
10115 VOIDmode, modifier);
10116 /* Return a PLUS if modifier says it's OK. */
10117 if (modifier == EXPAND_SUM
10118 || modifier == EXPAND_INITIALIZER)
10119 return simplify_gen_binary (PLUS, mode, op0, op1);
10120 goto binop2;
10121 }
10122 /* Use wi::shwi to ensure that the constant is
10123 truncated according to the mode of OP1, then sign extended
10124 to a HOST_WIDE_INT. Using the constant directly can result
10125 in non-canonical RTL in a 64x32 cross compile. */
10126 wc = TREE_INT_CST_LOW (treeop1);
10127 constant_part
10128 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
10129 op0 = plus_constant (mode, op0, INTVAL (constant_part));
10130 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
10131 op0 = force_operand (op0, target);
10132 return REDUCE_BIT_FIELD (op0);
10133 }
10134 }
10135
10136 /* Use TER to expand pointer addition of a negated value
10137 as pointer subtraction. */
10138 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
10139 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
10140 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
10141 && TREE_CODE (treeop1) == SSA_NAME
10142 && TYPE_MODE (TREE_TYPE (treeop0))
10143 == TYPE_MODE (TREE_TYPE (treeop1)))
10144 {
10145 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
10146 if (def)
10147 {
10148 treeop1 = gimple_assign_rhs1 (def);
10149 code = MINUS_EXPR;
10150 goto do_minus;
10151 }
10152 }
10153
10154 /* No sense saving up arithmetic to be done
10155 if it's all in the wrong mode to form part of an address.
10156 And force_operand won't know whether to sign-extend or
10157 zero-extend. */
10158 if (modifier != EXPAND_INITIALIZER
10159 && (modifier != EXPAND_SUM || mode != ptr_mode))
10160 {
10161 expand_operands (treeop0, treeop1,
10162 subtarget, &op0, &op1, modifier);
10163 if (op0 == const0_rtx)
10164 return op1;
10165 if (op1 == const0_rtx)
10166 return op0;
10167 goto binop2;
10168 }
10169
10170 expand_operands (treeop0, treeop1,
10171 subtarget, &op0, &op1, modifier);
10172 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
10173
10174 case MINUS_EXPR:
10175 case POINTER_DIFF_EXPR:
10176 do_minus:
10177 /* For initializers, we are allowed to return a MINUS of two
10178 symbolic constants. Here we handle all cases when both operands
10179 are constant. */
10180 /* Handle difference of two symbolic constants,
10181 for the sake of an initializer. */
10182 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10183 && really_constant_p (treeop0)
10184 && really_constant_p (treeop1))
10185 {
10186 expand_operands (treeop0, treeop1,
10187 NULL_RTX, &op0, &op1, modifier);
10188 return simplify_gen_binary (MINUS, mode, op0, op1);
10189 }
10190
10191 /* No sense saving up arithmetic to be done
10192 if it's all in the wrong mode to form part of an address.
10193 And force_operand won't know whether to sign-extend or
10194 zero-extend. */
10195 if (modifier != EXPAND_INITIALIZER
10196 && (modifier != EXPAND_SUM || mode != ptr_mode))
10197 goto binop;
10198
10199 expand_operands (treeop0, treeop1,
10200 subtarget, &op0, &op1, modifier);
10201
10202 /* Convert A - const to A + (-const). */
10203 if (CONST_INT_P (op1))
10204 {
10205 op1 = negate_rtx (mode, op1);
10206 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
10207 }
10208
10209 goto binop2;
10210
10211 case WIDEN_MULT_PLUS_EXPR:
10212 case WIDEN_MULT_MINUS_EXPR:
10213 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10214 op2 = expand_normal (treeop2);
10215 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10216 target, unsignedp);
10217 return target;
10218
10219 case WIDEN_MULT_EXPR:
10220 /* If first operand is constant, swap them.
10221 Thus the following special case checks need only
10222 check the second operand. */
10223 if (TREE_CODE (treeop0) == INTEGER_CST)
10224 std::swap (treeop0, treeop1);
10225
10226 /* First, check if we have a multiplication of one signed and one
10227 unsigned operand. */
10228 if (TREE_CODE (treeop1) != INTEGER_CST
10229 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
10230 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
10231 {
10232 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
10233 this_optab = usmul_widen_optab;
10234 if (find_widening_optab_handler (this_optab, mode, innermode)
10235 != CODE_FOR_nothing)
10236 {
10237 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10238 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10239 EXPAND_NORMAL);
10240 else
10241 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
10242 EXPAND_NORMAL);
10243 /* op0 and op1 might still be constant, despite the above
10244 != INTEGER_CST check. Handle it. */
10245 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10246 {
10247 op0 = convert_modes (mode, innermode, op0, true);
10248 op1 = convert_modes (mode, innermode, op1, false);
10249 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10250 target, unsignedp));
10251 }
10252 goto binop3;
10253 }
10254 }
10255 /* Check for a multiplication with matching signedness. */
10256 else if ((TREE_CODE (treeop1) == INTEGER_CST
10257 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
10258 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
10259 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
10260 {
10261 tree op0type = TREE_TYPE (treeop0);
10262 machine_mode innermode = TYPE_MODE (op0type);
10263 bool zextend_p = TYPE_UNSIGNED (op0type);
10264 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
10265 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
10266
10267 if (TREE_CODE (treeop0) != INTEGER_CST)
10268 {
10269 if (find_widening_optab_handler (this_optab, mode, innermode)
10270 != CODE_FOR_nothing)
10271 {
10272 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
10273 EXPAND_NORMAL);
10274 /* op0 and op1 might still be constant, despite the above
10275 != INTEGER_CST check. Handle it. */
10276 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10277 {
10278 widen_mult_const:
10279 op0 = convert_modes (mode, innermode, op0, zextend_p);
10280 op1
10281 = convert_modes (mode, innermode, op1,
10282 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
10283 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
10284 target,
10285 unsignedp));
10286 }
10287 temp = expand_widening_mult (mode, op0, op1, target,
10288 unsignedp, this_optab);
10289 return REDUCE_BIT_FIELD (temp);
10290 }
10291 if (find_widening_optab_handler (other_optab, mode, innermode)
10292 != CODE_FOR_nothing
10293 && innermode == word_mode)
10294 {
10295 rtx htem, hipart;
10296 op0 = expand_normal (treeop0);
10297 op1 = expand_normal (treeop1);
10298 /* op0 and op1 might be constants, despite the above
10299 != INTEGER_CST check. Handle it. */
10300 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
10301 goto widen_mult_const;
10302 temp = expand_binop (mode, other_optab, op0, op1, target,
10303 unsignedp, OPTAB_LIB_WIDEN);
10304 hipart = gen_highpart (word_mode, temp);
10305 htem = expand_mult_highpart_adjust (word_mode, hipart,
10306 op0, op1, hipart,
10307 zextend_p);
10308 if (htem != hipart)
10309 emit_move_insn (hipart, htem);
10310 return REDUCE_BIT_FIELD (temp);
10311 }
10312 }
10313 }
10314 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
10315 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
10316 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10317 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10318
10319 case MULT_EXPR:
10320 /* If this is a fixed-point operation, then we cannot use the code
10321 below because "expand_mult" doesn't support sat/no-sat fixed-point
10322 multiplications. */
10323 if (ALL_FIXED_POINT_MODE_P (mode))
10324 goto binop;
10325
10326 /* If first operand is constant, swap them.
10327 Thus the following special case checks need only
10328 check the second operand. */
10329 if (TREE_CODE (treeop0) == INTEGER_CST)
10330 std::swap (treeop0, treeop1);
10331
10332 /* Attempt to return something suitable for generating an
10333 indexed address, for machines that support that. */
10334
10335 if (modifier == EXPAND_SUM && mode == ptr_mode
10336 && tree_fits_shwi_p (treeop1))
10337 {
10338 tree exp1 = treeop1;
10339
10340 op0 = expand_expr (treeop0, subtarget, VOIDmode,
10341 EXPAND_SUM);
10342
10343 if (!REG_P (op0))
10344 op0 = force_operand (op0, NULL_RTX);
10345 if (!REG_P (op0))
10346 op0 = copy_to_mode_reg (mode, op0);
10347
10348 op1 = gen_int_mode (tree_to_shwi (exp1),
10349 TYPE_MODE (TREE_TYPE (exp1)));
10350 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
10351 }
10352
10353 if (modifier == EXPAND_STACK_PARM)
10354 target = 0;
10355
10356 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
10357 {
10358 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
10359 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
10360 if (def_stmt0
10361 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
10362 def_stmt0 = NULL;
10363 if (def_stmt1
10364 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
10365 def_stmt1 = NULL;
10366
10367 if (def_stmt0 || def_stmt1)
10368 {
10369 /* X / Y * Y can be expanded as X - X % Y too.
10370 Choose the cheaper sequence of those two. */
10371 if (def_stmt0)
10372 treeop0 = gimple_assign_rhs1 (def_stmt0);
10373 else
10374 {
10375 treeop1 = treeop0;
10376 treeop0 = gimple_assign_rhs1 (def_stmt1);
10377 }
10378 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
10379 EXPAND_NORMAL);
10380 bool speed_p = optimize_insn_for_speed_p ();
10381 do_pending_stack_adjust ();
10382 start_sequence ();
10383 rtx divmul_ret
10384 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
10385 op0, op1, NULL_RTX, unsignedp);
10386 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
10387 unsignedp);
10388 rtx_insn *divmul_insns = get_insns ();
10389 end_sequence ();
10390 start_sequence ();
10391 rtx modsub_ret
10392 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
10393 op0, op1, NULL_RTX, unsignedp);
10394 this_optab = optab_for_tree_code (MINUS_EXPR, type,
10395 optab_default);
10396 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
10397 target, unsignedp, OPTAB_LIB_WIDEN);
10398 rtx_insn *modsub_insns = get_insns ();
10399 end_sequence ();
10400 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
10401 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
10402 /* If costs are the same then use as tie breaker the other other
10403 factor. */
10404 if (divmul_cost == modsub_cost)
10405 {
10406 divmul_cost = seq_cost (divmul_insns, !speed_p);
10407 modsub_cost = seq_cost (modsub_insns, !speed_p);
10408 }
10409
10410 if (divmul_cost <= modsub_cost)
10411 {
10412 emit_insn (divmul_insns);
10413 return REDUCE_BIT_FIELD (divmul_ret);
10414 }
10415 emit_insn (modsub_insns);
10416 return REDUCE_BIT_FIELD (modsub_ret);
10417 }
10418 }
10419
10420 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10421
10422 /* Expand X*Y as X&-Y when Y must be zero or one. */
10423 if (SCALAR_INT_MODE_P (mode))
10424 {
10425 bool gimple_zero_one_valued_p (tree, tree (*)(tree));
10426 bool bit0_p = gimple_zero_one_valued_p (treeop0, nullptr);
10427 bool bit1_p = gimple_zero_one_valued_p (treeop1, nullptr);
10428
10429 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
10430 if (bit0_p && bit1_p)
10431 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
10432
10433 if (bit0_p || bit1_p)
10434 {
10435 bool speed = optimize_insn_for_speed_p ();
10436 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
10437 struct algorithm algorithm;
10438 enum mult_variant variant;
10439 if (CONST_INT_P (op1)
10440 ? !choose_mult_variant (mode, INTVAL (op1),
10441 &algorithm, &variant, cost)
10442 : cost < mul_cost (speed, mode))
10443 {
10444 temp = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
10445 op1, target)
10446 : expand_and (mode, op0,
10447 negate_rtx (mode, op1),
10448 target);
10449 return REDUCE_BIT_FIELD (temp);
10450 }
10451 }
10452 }
10453
10454 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
10455
10456 case TRUNC_MOD_EXPR:
10457 case FLOOR_MOD_EXPR:
10458 case CEIL_MOD_EXPR:
10459 case ROUND_MOD_EXPR:
10460
10461 case TRUNC_DIV_EXPR:
10462 case FLOOR_DIV_EXPR:
10463 case CEIL_DIV_EXPR:
10464 case ROUND_DIV_EXPR:
10465 case EXACT_DIV_EXPR:
10466 /* If this is a fixed-point operation, then we cannot use the code
10467 below because "expand_divmod" doesn't support sat/no-sat fixed-point
10468 divisions. */
10469 if (ALL_FIXED_POINT_MODE_P (mode))
10470 goto binop;
10471
10472 if (modifier == EXPAND_STACK_PARM)
10473 target = 0;
10474 /* Possible optimization: compute the dividend with EXPAND_SUM
10475 then if the divisor is constant can optimize the case
10476 where some terms of the dividend have coeffs divisible by it. */
10477 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10478 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
10479 target, unsignedp);
10480
10481 case RDIV_EXPR:
10482 goto binop;
10483
10484 case MULT_HIGHPART_EXPR:
10485 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
10486 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
10487 gcc_assert (temp);
10488 return temp;
10489
10490 case FIXED_CONVERT_EXPR:
10491 op0 = expand_normal (treeop0);
10492 if (target == 0 || modifier == EXPAND_STACK_PARM)
10493 target = gen_reg_rtx (mode);
10494
10495 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
10496 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10497 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
10498 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
10499 else
10500 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
10501 return target;
10502
10503 case FIX_TRUNC_EXPR:
10504 op0 = expand_normal (treeop0);
10505 if (target == 0 || modifier == EXPAND_STACK_PARM)
10506 target = gen_reg_rtx (mode);
10507 expand_fix (target, op0, unsignedp);
10508 return target;
10509
10510 case FLOAT_EXPR:
10511 op0 = expand_normal (treeop0);
10512 if (target == 0 || modifier == EXPAND_STACK_PARM)
10513 target = gen_reg_rtx (mode);
10514 /* expand_float can't figure out what to do if FROM has VOIDmode.
10515 So give it the correct mode. With -O, cse will optimize this. */
10516 if (GET_MODE (op0) == VOIDmode)
10517 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
10518 op0);
10519 expand_float (target, op0,
10520 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10521 return target;
10522
10523 case NEGATE_EXPR:
10524 op0 = expand_expr (treeop0, subtarget,
10525 VOIDmode, EXPAND_NORMAL);
10526 if (modifier == EXPAND_STACK_PARM)
10527 target = 0;
10528 temp = expand_unop (mode,
10529 optab_for_tree_code (NEGATE_EXPR, type,
10530 optab_default),
10531 op0, target, 0);
10532 gcc_assert (temp);
10533 return REDUCE_BIT_FIELD (temp);
10534
10535 case ABS_EXPR:
10536 case ABSU_EXPR:
10537 op0 = expand_expr (treeop0, subtarget,
10538 VOIDmode, EXPAND_NORMAL);
10539 if (modifier == EXPAND_STACK_PARM)
10540 target = 0;
10541
10542 /* ABS_EXPR is not valid for complex arguments. */
10543 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10544 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
10545
10546 /* Unsigned abs is simply the operand. Testing here means we don't
10547 risk generating incorrect code below. */
10548 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
10549 return op0;
10550
10551 return expand_abs (mode, op0, target, unsignedp,
10552 safe_from_p (target, treeop0, 1));
10553
10554 case MAX_EXPR:
10555 case MIN_EXPR:
10556 target = original_target;
10557 if (target == 0
10558 || modifier == EXPAND_STACK_PARM
10559 || (MEM_P (target) && MEM_VOLATILE_P (target))
10560 || GET_MODE (target) != mode
10561 || (REG_P (target)
10562 && REGNO (target) < FIRST_PSEUDO_REGISTER))
10563 target = gen_reg_rtx (mode);
10564 expand_operands (treeop0, treeop1,
10565 target, &op0, &op1, EXPAND_NORMAL);
10566
10567 /* First try to do it with a special MIN or MAX instruction.
10568 If that does not win, use a conditional jump to select the proper
10569 value. */
10570 this_optab = optab_for_tree_code (code, type, optab_default);
10571 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
10572 OPTAB_WIDEN);
10573 if (temp != 0)
10574 return temp;
10575
10576 if (VECTOR_TYPE_P (type))
10577 gcc_unreachable ();
10578
10579 /* At this point, a MEM target is no longer useful; we will get better
10580 code without it. */
10581
10582 if (! REG_P (target))
10583 target = gen_reg_rtx (mode);
10584
10585 /* If op1 was placed in target, swap op0 and op1. */
10586 if (target != op0 && target == op1)
10587 std::swap (op0, op1);
10588
10589 /* We generate better code and avoid problems with op1 mentioning
10590 target by forcing op1 into a pseudo if it isn't a constant. */
10591 if (! CONSTANT_P (op1))
10592 op1 = force_reg (mode, op1);
10593
10594 {
10595 enum rtx_code comparison_code;
10596 rtx cmpop1 = op1;
10597
10598 if (code == MAX_EXPR)
10599 comparison_code = unsignedp ? GEU : GE;
10600 else
10601 comparison_code = unsignedp ? LEU : LE;
10602
10603 /* Canonicalize to comparisons against 0. */
10604 if (op1 == const1_rtx)
10605 {
10606 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
10607 or (a != 0 ? a : 1) for unsigned.
10608 For MIN we are safe converting (a <= 1 ? a : 1)
10609 into (a <= 0 ? a : 1) */
10610 cmpop1 = const0_rtx;
10611 if (code == MAX_EXPR)
10612 comparison_code = unsignedp ? NE : GT;
10613 }
10614 if (op1 == constm1_rtx && !unsignedp)
10615 {
10616 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10617 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10618 cmpop1 = const0_rtx;
10619 if (code == MIN_EXPR)
10620 comparison_code = LT;
10621 }
10622
10623 /* Use a conditional move if possible. */
10624 if (can_conditionally_move_p (mode))
10625 {
10626 rtx insn;
10627
10628 start_sequence ();
10629
10630 /* Try to emit the conditional move. */
10631 insn = emit_conditional_move (target,
10632 { comparison_code,
10633 op0, cmpop1, mode },
10634 op0, op1, mode,
10635 unsignedp);
10636
10637 /* If we could do the conditional move, emit the sequence,
10638 and return. */
10639 if (insn)
10640 {
10641 rtx_insn *seq = get_insns ();
10642 end_sequence ();
10643 emit_insn (seq);
10644 return target;
10645 }
10646
10647 /* Otherwise discard the sequence and fall back to code with
10648 branches. */
10649 end_sequence ();
10650 }
10651
10652 if (target != op0)
10653 emit_move_insn (target, op0);
10654
10655 lab = gen_label_rtx ();
10656 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10657 unsignedp, mode, NULL_RTX, NULL, lab,
10658 profile_probability::uninitialized ());
10659 }
10660 emit_move_insn (target, op1);
10661 emit_label (lab);
10662 return target;
10663
10664 case BIT_NOT_EXPR:
10665 op0 = expand_expr (treeop0, subtarget,
10666 VOIDmode, EXPAND_NORMAL);
10667 if (modifier == EXPAND_STACK_PARM)
10668 target = 0;
10669 /* In case we have to reduce the result to bitfield precision
10670 for unsigned bitfield expand this as XOR with a proper constant
10671 instead. */
10672 if (reduce_bit_field && TYPE_UNSIGNED (type))
10673 {
10674 int_mode = SCALAR_INT_TYPE_MODE (type);
10675 wide_int mask = wi::mask (TYPE_PRECISION (type),
10676 false, GET_MODE_PRECISION (int_mode));
10677
10678 temp = expand_binop (int_mode, xor_optab, op0,
10679 immed_wide_int_const (mask, int_mode),
10680 target, 1, OPTAB_LIB_WIDEN);
10681 }
10682 else
10683 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10684 gcc_assert (temp);
10685 return temp;
10686
10687 /* ??? Can optimize bitwise operations with one arg constant.
10688 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10689 and (a bitwise1 b) bitwise2 b (etc)
10690 but that is probably not worth while. */
10691
10692 case BIT_AND_EXPR:
10693 case BIT_IOR_EXPR:
10694 case BIT_XOR_EXPR:
10695 goto binop;
10696
10697 case LROTATE_EXPR:
10698 case RROTATE_EXPR:
10699 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10700 || type_has_mode_precision_p (type));
10701 /* fall through */
10702
10703 case LSHIFT_EXPR:
10704 case RSHIFT_EXPR:
10705 {
10706 /* If this is a fixed-point operation, then we cannot use the code
10707 below because "expand_shift" doesn't support sat/no-sat fixed-point
10708 shifts. */
10709 if (ALL_FIXED_POINT_MODE_P (mode))
10710 goto binop;
10711
10712 if (! safe_from_p (subtarget, treeop1, 1))
10713 subtarget = 0;
10714 if (modifier == EXPAND_STACK_PARM)
10715 target = 0;
10716 op0 = expand_expr (treeop0, subtarget,
10717 VOIDmode, EXPAND_NORMAL);
10718
10719 /* Left shift optimization when shifting across word_size boundary.
10720
10721 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10722 there isn't native instruction to support this wide mode
10723 left shift. Given below scenario:
10724
10725 Type A = (Type) B << C
10726
10727 |< T >|
10728 | dest_high | dest_low |
10729
10730 | word_size |
10731
10732 If the shift amount C caused we shift B to across the word
10733 size boundary, i.e part of B shifted into high half of
10734 destination register, and part of B remains in the low
10735 half, then GCC will use the following left shift expand
10736 logic:
10737
10738 1. Initialize dest_low to B.
10739 2. Initialize every bit of dest_high to the sign bit of B.
10740 3. Logic left shift dest_low by C bit to finalize dest_low.
10741 The value of dest_low before this shift is kept in a temp D.
10742 4. Logic left shift dest_high by C.
10743 5. Logic right shift D by (word_size - C).
10744 6. Or the result of 4 and 5 to finalize dest_high.
10745
10746 While, by checking gimple statements, if operand B is
10747 coming from signed extension, then we can simplify above
10748 expand logic into:
10749
10750 1. dest_high = src_low >> (word_size - C).
10751 2. dest_low = src_low << C.
10752
10753 We can use one arithmetic right shift to finish all the
10754 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10755 needed from 6 into 2.
10756
10757 The case is similar for zero extension, except that we
10758 initialize dest_high to zero rather than copies of the sign
10759 bit from B. Furthermore, we need to use a logical right shift
10760 in this case.
10761
10762 The choice of sign-extension versus zero-extension is
10763 determined entirely by whether or not B is signed and is
10764 independent of the current setting of unsignedp. */
10765
10766 temp = NULL_RTX;
10767 if (code == LSHIFT_EXPR
10768 && target
10769 && REG_P (target)
10770 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10771 && mode == int_mode
10772 && TREE_CONSTANT (treeop1)
10773 && TREE_CODE (treeop0) == SSA_NAME)
10774 {
10775 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10776 if (is_gimple_assign (def)
10777 && gimple_assign_rhs_code (def) == NOP_EXPR)
10778 {
10779 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10780 (TREE_TYPE (gimple_assign_rhs1 (def)));
10781
10782 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10783 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10784 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10785 >= GET_MODE_BITSIZE (word_mode)))
10786 {
10787 rtx_insn *seq, *seq_old;
10788 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10789 int_mode);
10790 bool extend_unsigned
10791 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10792 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10793 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10794 rtx dest_high = simplify_gen_subreg (word_mode, target,
10795 int_mode, high_off);
10796 HOST_WIDE_INT ramount = (BITS_PER_WORD
10797 - TREE_INT_CST_LOW (treeop1));
10798 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10799
10800 start_sequence ();
10801 /* dest_high = src_low >> (word_size - C). */
10802 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10803 rshift, dest_high,
10804 extend_unsigned);
10805 if (temp != dest_high)
10806 emit_move_insn (dest_high, temp);
10807
10808 /* dest_low = src_low << C. */
10809 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10810 treeop1, dest_low, unsignedp);
10811 if (temp != dest_low)
10812 emit_move_insn (dest_low, temp);
10813
10814 seq = get_insns ();
10815 end_sequence ();
10816 temp = target ;
10817
10818 if (have_insn_for (ASHIFT, int_mode))
10819 {
10820 bool speed_p = optimize_insn_for_speed_p ();
10821 start_sequence ();
10822 rtx ret_old = expand_variable_shift (code, int_mode,
10823 op0, treeop1,
10824 target,
10825 unsignedp);
10826
10827 seq_old = get_insns ();
10828 end_sequence ();
10829 if (seq_cost (seq, speed_p)
10830 >= seq_cost (seq_old, speed_p))
10831 {
10832 seq = seq_old;
10833 temp = ret_old;
10834 }
10835 }
10836 emit_insn (seq);
10837 }
10838 }
10839 }
10840
10841 if (temp == NULL_RTX)
10842 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10843 unsignedp);
10844 if (code == LSHIFT_EXPR)
10845 temp = REDUCE_BIT_FIELD (temp);
10846 return temp;
10847 }
10848
10849 /* Could determine the answer when only additive constants differ. Also,
10850 the addition of one can be handled by changing the condition. */
10851 case LT_EXPR:
10852 case LE_EXPR:
10853 case GT_EXPR:
10854 case GE_EXPR:
10855 case EQ_EXPR:
10856 case NE_EXPR:
10857 case UNORDERED_EXPR:
10858 case ORDERED_EXPR:
10859 case UNLT_EXPR:
10860 case UNLE_EXPR:
10861 case UNGT_EXPR:
10862 case UNGE_EXPR:
10863 case UNEQ_EXPR:
10864 case LTGT_EXPR:
10865 {
10866 temp = do_store_flag (ops,
10867 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10868 tmode != VOIDmode ? tmode : mode);
10869 if (temp)
10870 return temp;
10871
10872 /* Use a compare and a jump for BLKmode comparisons, or for function
10873 type comparisons is have_canonicalize_funcptr_for_compare. */
10874
10875 if ((target == 0
10876 || modifier == EXPAND_STACK_PARM
10877 || ! safe_from_p (target, treeop0, 1)
10878 || ! safe_from_p (target, treeop1, 1)
10879 /* Make sure we don't have a hard reg (such as function's return
10880 value) live across basic blocks, if not optimizing. */
10881 || (!optimize && REG_P (target)
10882 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10883 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10884
10885 emit_move_insn (target, const0_rtx);
10886
10887 rtx_code_label *lab1 = gen_label_rtx ();
10888 jumpifnot_1 (code, treeop0, treeop1, lab1,
10889 profile_probability::uninitialized ());
10890
10891 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10892 emit_move_insn (target, constm1_rtx);
10893 else
10894 emit_move_insn (target, const1_rtx);
10895
10896 emit_label (lab1);
10897 return target;
10898 }
10899 case COMPLEX_EXPR:
10900 /* Get the rtx code of the operands. */
10901 op0 = expand_normal (treeop0);
10902 op1 = expand_normal (treeop1);
10903
10904 if (!target)
10905 target = gen_reg_rtx (TYPE_MODE (type));
10906 else
10907 /* If target overlaps with op1, then either we need to force
10908 op1 into a pseudo (if target also overlaps with op0),
10909 or write the complex parts in reverse order. */
10910 switch (GET_CODE (target))
10911 {
10912 case CONCAT:
10913 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10914 {
10915 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10916 {
10917 complex_expr_force_op1:
10918 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10919 emit_move_insn (temp, op1);
10920 op1 = temp;
10921 break;
10922 }
10923 complex_expr_swap_order:
10924 /* Move the imaginary (op1) and real (op0) parts to their
10925 location. */
10926 write_complex_part (target, op1, true, true);
10927 write_complex_part (target, op0, false, false);
10928
10929 return target;
10930 }
10931 break;
10932 case MEM:
10933 temp = adjust_address_nv (target,
10934 GET_MODE_INNER (GET_MODE (target)), 0);
10935 if (reg_overlap_mentioned_p (temp, op1))
10936 {
10937 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10938 temp = adjust_address_nv (target, imode,
10939 GET_MODE_SIZE (imode));
10940 if (reg_overlap_mentioned_p (temp, op0))
10941 goto complex_expr_force_op1;
10942 goto complex_expr_swap_order;
10943 }
10944 break;
10945 default:
10946 if (reg_overlap_mentioned_p (target, op1))
10947 {
10948 if (reg_overlap_mentioned_p (target, op0))
10949 goto complex_expr_force_op1;
10950 goto complex_expr_swap_order;
10951 }
10952 break;
10953 }
10954
10955 /* Move the real (op0) and imaginary (op1) parts to their location. */
10956 write_complex_part (target, op0, false, true);
10957 write_complex_part (target, op1, true, false);
10958
10959 return target;
10960
10961 case WIDEN_SUM_EXPR:
10962 {
10963 tree oprnd0 = treeop0;
10964 tree oprnd1 = treeop1;
10965
10966 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10967 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10968 target, unsignedp);
10969 return target;
10970 }
10971
10972 case VEC_UNPACK_HI_EXPR:
10973 case VEC_UNPACK_LO_EXPR:
10974 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10975 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10976 {
10977 op0 = expand_normal (treeop0);
10978 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10979 target, unsignedp);
10980 gcc_assert (temp);
10981 return temp;
10982 }
10983
10984 case VEC_UNPACK_FLOAT_HI_EXPR:
10985 case VEC_UNPACK_FLOAT_LO_EXPR:
10986 {
10987 op0 = expand_normal (treeop0);
10988 /* The signedness is determined from input operand. */
10989 temp = expand_widen_pattern_expr
10990 (ops, op0, NULL_RTX, NULL_RTX,
10991 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10992
10993 gcc_assert (temp);
10994 return temp;
10995 }
10996
10997 case VEC_WIDEN_MULT_HI_EXPR:
10998 case VEC_WIDEN_MULT_LO_EXPR:
10999 case VEC_WIDEN_MULT_EVEN_EXPR:
11000 case VEC_WIDEN_MULT_ODD_EXPR:
11001 case VEC_WIDEN_LSHIFT_HI_EXPR:
11002 case VEC_WIDEN_LSHIFT_LO_EXPR:
11003 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
11004 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
11005 target, unsignedp);
11006 gcc_assert (target);
11007 return target;
11008
11009 case VEC_PACK_SAT_EXPR:
11010 case VEC_PACK_FIX_TRUNC_EXPR:
11011 mode = TYPE_MODE (TREE_TYPE (treeop0));
11012 subtarget = NULL_RTX;
11013 goto binop;
11014
11015 case VEC_PACK_TRUNC_EXPR:
11016 if (VECTOR_BOOLEAN_TYPE_P (type)
11017 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
11018 && mode == TYPE_MODE (TREE_TYPE (treeop0))
11019 && SCALAR_INT_MODE_P (mode))
11020 {
11021 class expand_operand eops[4];
11022 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
11023 expand_operands (treeop0, treeop1,
11024 subtarget, &op0, &op1, EXPAND_NORMAL);
11025 this_optab = vec_pack_sbool_trunc_optab;
11026 enum insn_code icode = optab_handler (this_optab, imode);
11027 create_output_operand (&eops[0], target, mode);
11028 create_convert_operand_from (&eops[1], op0, imode, false);
11029 create_convert_operand_from (&eops[2], op1, imode, false);
11030 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
11031 create_input_operand (&eops[3], temp, imode);
11032 expand_insn (icode, 4, eops);
11033 return eops[0].value;
11034 }
11035 mode = TYPE_MODE (TREE_TYPE (treeop0));
11036 subtarget = NULL_RTX;
11037 goto binop;
11038
11039 case VEC_PACK_FLOAT_EXPR:
11040 mode = TYPE_MODE (TREE_TYPE (treeop0));
11041 expand_operands (treeop0, treeop1,
11042 subtarget, &op0, &op1, EXPAND_NORMAL);
11043 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
11044 optab_default);
11045 target = expand_binop (mode, this_optab, op0, op1, target,
11046 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
11047 OPTAB_LIB_WIDEN);
11048 gcc_assert (target);
11049 return target;
11050
11051 case VEC_PERM_EXPR:
11052 {
11053 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
11054 vec_perm_builder sel;
11055 if (TREE_CODE (treeop2) == VECTOR_CST
11056 && tree_to_vec_perm_builder (&sel, treeop2))
11057 {
11058 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
11059 temp = expand_vec_perm_const (mode, op0, op1, sel,
11060 sel_mode, target);
11061 }
11062 else
11063 {
11064 op2 = expand_normal (treeop2);
11065 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
11066 }
11067 gcc_assert (temp);
11068 return temp;
11069 }
11070
11071 case DOT_PROD_EXPR:
11072 {
11073 tree oprnd0 = treeop0;
11074 tree oprnd1 = treeop1;
11075 tree oprnd2 = treeop2;
11076
11077 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
11078 op2 = expand_normal (oprnd2);
11079 target = expand_widen_pattern_expr (ops, op0, op1, op2,
11080 target, unsignedp);
11081 return target;
11082 }
11083
11084 case SAD_EXPR:
11085 {
11086 tree oprnd0 = treeop0;
11087 tree oprnd1 = treeop1;
11088 tree oprnd2 = treeop2;
11089
11090 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
11091 op2 = expand_normal (oprnd2);
11092 target = expand_widen_pattern_expr (ops, op0, op1, op2,
11093 target, unsignedp);
11094 return target;
11095 }
11096
11097 case REALIGN_LOAD_EXPR:
11098 {
11099 tree oprnd0 = treeop0;
11100 tree oprnd1 = treeop1;
11101 tree oprnd2 = treeop2;
11102
11103 this_optab = optab_for_tree_code (code, type, optab_default);
11104 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
11105 op2 = expand_normal (oprnd2);
11106 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
11107 target, unsignedp);
11108 gcc_assert (temp);
11109 return temp;
11110 }
11111
11112 case COND_EXPR:
11113 {
11114 /* A COND_EXPR with its type being VOID_TYPE represents a
11115 conditional jump and is handled in
11116 expand_gimple_cond_expr. */
11117 gcc_assert (!VOID_TYPE_P (type));
11118
11119 /* Note that COND_EXPRs whose type is a structure or union
11120 are required to be constructed to contain assignments of
11121 a temporary variable, so that we can evaluate them here
11122 for side effect only. If type is void, we must do likewise. */
11123
11124 gcc_assert (!TREE_ADDRESSABLE (type)
11125 && !ignore
11126 && TREE_TYPE (treeop1) != void_type_node
11127 && TREE_TYPE (treeop2) != void_type_node);
11128
11129 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
11130 if (temp)
11131 return temp;
11132
11133 /* If we are not to produce a result, we have no target. Otherwise,
11134 if a target was specified use it; it will not be used as an
11135 intermediate target unless it is safe. If no target, use a
11136 temporary. */
11137
11138 if (modifier != EXPAND_STACK_PARM
11139 && original_target
11140 && safe_from_p (original_target, treeop0, 1)
11141 && GET_MODE (original_target) == mode
11142 && !MEM_P (original_target))
11143 temp = original_target;
11144 else
11145 temp = assign_temp (type, 0, 1);
11146
11147 do_pending_stack_adjust ();
11148 NO_DEFER_POP;
11149 rtx_code_label *lab0 = gen_label_rtx ();
11150 rtx_code_label *lab1 = gen_label_rtx ();
11151 jumpifnot (treeop0, lab0,
11152 profile_probability::uninitialized ());
11153 store_expr (treeop1, temp,
11154 modifier == EXPAND_STACK_PARM,
11155 false, false);
11156
11157 emit_jump_insn (targetm.gen_jump (lab1));
11158 emit_barrier ();
11159 emit_label (lab0);
11160 store_expr (treeop2, temp,
11161 modifier == EXPAND_STACK_PARM,
11162 false, false);
11163
11164 emit_label (lab1);
11165 OK_DEFER_POP;
11166 return temp;
11167 }
11168
11169 case VEC_DUPLICATE_EXPR:
11170 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
11171 target = expand_vector_broadcast (mode, op0);
11172 gcc_assert (target);
11173 return target;
11174
11175 case VEC_SERIES_EXPR:
11176 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
11177 return expand_vec_series_expr (mode, op0, op1, target);
11178
11179 case BIT_INSERT_EXPR:
11180 {
11181 unsigned bitpos = tree_to_uhwi (treeop2);
11182 unsigned bitsize;
11183 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
11184 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
11185 else
11186 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
11187 op0 = expand_normal (treeop0);
11188 op1 = expand_normal (treeop1);
11189 rtx dst = gen_reg_rtx (mode);
11190 emit_move_insn (dst, op0);
11191 store_bit_field (dst, bitsize, bitpos, 0, 0,
11192 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
11193 return dst;
11194 }
11195
11196 default:
11197 gcc_unreachable ();
11198 }
11199
11200 /* Here to do an ordinary binary operator. */
11201 binop:
11202 expand_operands (treeop0, treeop1,
11203 subtarget, &op0, &op1, EXPAND_NORMAL);
11204 binop2:
11205 this_optab = optab_for_tree_code (code, type, optab_default);
11206 binop3:
11207 if (modifier == EXPAND_STACK_PARM)
11208 target = 0;
11209 temp = expand_binop (mode, this_optab, op0, op1, target,
11210 unsignedp, OPTAB_LIB_WIDEN);
11211 gcc_assert (temp);
11212 /* Bitwise operations do not need bitfield reduction as we expect their
11213 operands being properly truncated. */
11214 if (code == BIT_XOR_EXPR
11215 || code == BIT_AND_EXPR
11216 || code == BIT_IOR_EXPR)
11217 return temp;
11218 return REDUCE_BIT_FIELD (temp);
11219 }
11220 #undef REDUCE_BIT_FIELD
11221
11222
11223 /* Return TRUE if expression STMT is suitable for replacement.
11224 Never consider memory loads as replaceable, because those don't ever lead
11225 into constant expressions. */
11226
11227 static bool
11228 stmt_is_replaceable_p (gimple *stmt)
11229 {
11230 if (ssa_is_replaceable_p (stmt))
11231 {
11232 /* Don't move around loads. */
11233 if (!gimple_assign_single_p (stmt)
11234 || is_gimple_val (gimple_assign_rhs1 (stmt)))
11235 return true;
11236 }
11237 return false;
11238 }
11239
11240 /* A subroutine of expand_expr_real_1. Expand gimple assignment G,
11241 which is known to set an SSA_NAME result. The other arguments are
11242 as for expand_expr_real_1. */
11243
11244 rtx
11245 expand_expr_real_gassign (gassign *g, rtx target, machine_mode tmode,
11246 enum expand_modifier modifier, rtx *alt_rtl,
11247 bool inner_reference_p)
11248 {
11249 separate_ops ops;
11250 rtx r;
11251 location_t saved_loc = curr_insn_location ();
11252 auto loc = gimple_location (g);
11253 if (loc != UNKNOWN_LOCATION)
11254 set_curr_insn_location (loc);
11255 tree lhs = gimple_assign_lhs (g);
11256 ops.code = gimple_assign_rhs_code (g);
11257 ops.type = TREE_TYPE (lhs);
11258 switch (get_gimple_rhs_class (ops.code))
11259 {
11260 case GIMPLE_TERNARY_RHS:
11261 ops.op2 = gimple_assign_rhs3 (g);
11262 /* Fallthru */
11263 case GIMPLE_BINARY_RHS:
11264 ops.op1 = gimple_assign_rhs2 (g);
11265
11266 /* Try to expand conditonal compare. */
11267 if (targetm.have_ccmp ())
11268 {
11269 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
11270 r = expand_ccmp_expr (g, TYPE_MODE (ops.type));
11271 if (r)
11272 break;
11273 }
11274 /* Fallthru */
11275 case GIMPLE_UNARY_RHS:
11276 ops.op0 = gimple_assign_rhs1 (g);
11277 ops.location = loc;
11278 r = expand_expr_real_2 (&ops, target, tmode, modifier);
11279 break;
11280 case GIMPLE_SINGLE_RHS:
11281 {
11282 r = expand_expr_real (gimple_assign_rhs1 (g), target,
11283 tmode, modifier, alt_rtl,
11284 inner_reference_p);
11285 break;
11286 }
11287 default:
11288 gcc_unreachable ();
11289 }
11290 set_curr_insn_location (saved_loc);
11291 if (REG_P (r) && !REG_EXPR (r))
11292 set_reg_attrs_for_decl_rtl (lhs, r);
11293 return r;
11294 }
11295
11296 rtx
11297 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
11298 enum expand_modifier modifier, rtx *alt_rtl,
11299 bool inner_reference_p)
11300 {
11301 rtx op0, op1, temp, decl_rtl;
11302 tree type;
11303 int unsignedp;
11304 machine_mode mode, dmode;
11305 enum tree_code code = TREE_CODE (exp);
11306 rtx subtarget, original_target;
11307 int ignore;
11308 bool reduce_bit_field;
11309 location_t loc = EXPR_LOCATION (exp);
11310 struct separate_ops ops;
11311 tree treeop0, treeop1, treeop2;
11312 tree ssa_name = NULL_TREE;
11313 gimple *g;
11314
11315 /* Some ABIs define padding bits in _BitInt uninitialized. Normally, RTL
11316 expansion sign/zero extends integral types with less than mode precision
11317 when reading from bit-fields and after arithmetic operations (see
11318 REDUCE_BIT_FIELD in expand_expr_real_2) and on subsequent loads relies
11319 on those extensions to have been already performed, but because of the
11320 above for _BitInt they need to be sign/zero extended when reading from
11321 locations that could be exposed to ABI boundaries (when loading from
11322 objects in memory, or function arguments, return value). Because we
11323 internally extend after arithmetic operations, we can avoid doing that
11324 when reading from SSA_NAMEs of vars. */
11325 #define EXTEND_BITINT(expr) \
11326 ((TREE_CODE (type) == BITINT_TYPE \
11327 && reduce_bit_field \
11328 && mode != BLKmode \
11329 && modifier != EXPAND_MEMORY \
11330 && modifier != EXPAND_WRITE \
11331 && modifier != EXPAND_INITIALIZER \
11332 && modifier != EXPAND_CONST_ADDRESS) \
11333 ? reduce_to_bit_field_precision ((expr), NULL_RTX, type) : (expr))
11334
11335 type = TREE_TYPE (exp);
11336 mode = TYPE_MODE (type);
11337 unsignedp = TYPE_UNSIGNED (type);
11338
11339 treeop0 = treeop1 = treeop2 = NULL_TREE;
11340 if (!VL_EXP_CLASS_P (exp))
11341 switch (TREE_CODE_LENGTH (code))
11342 {
11343 default:
11344 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
11345 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
11346 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
11347 case 0: break;
11348 }
11349 ops.code = code;
11350 ops.type = type;
11351 ops.op0 = treeop0;
11352 ops.op1 = treeop1;
11353 ops.op2 = treeop2;
11354 ops.location = loc;
11355
11356 ignore = (target == const0_rtx
11357 || ((CONVERT_EXPR_CODE_P (code)
11358 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
11359 && TREE_CODE (type) == VOID_TYPE));
11360
11361 /* An operation in what may be a bit-field type needs the
11362 result to be reduced to the precision of the bit-field type,
11363 which is narrower than that of the type's mode. */
11364 reduce_bit_field = (!ignore
11365 && INTEGRAL_TYPE_P (type)
11366 && !type_has_mode_precision_p (type));
11367
11368 /* If we are going to ignore this result, we need only do something
11369 if there is a side-effect somewhere in the expression. If there
11370 is, short-circuit the most common cases here. Note that we must
11371 not call expand_expr with anything but const0_rtx in case this
11372 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
11373
11374 if (ignore)
11375 {
11376 if (! TREE_SIDE_EFFECTS (exp))
11377 return const0_rtx;
11378
11379 /* Ensure we reference a volatile object even if value is ignored, but
11380 don't do this if all we are doing is taking its address. */
11381 if (TREE_THIS_VOLATILE (exp)
11382 && TREE_CODE (exp) != FUNCTION_DECL
11383 && mode != VOIDmode && mode != BLKmode
11384 && modifier != EXPAND_CONST_ADDRESS)
11385 {
11386 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
11387 if (MEM_P (temp))
11388 copy_to_reg (temp);
11389 return const0_rtx;
11390 }
11391
11392 if (TREE_CODE_CLASS (code) == tcc_unary
11393 || code == BIT_FIELD_REF
11394 || code == COMPONENT_REF
11395 || code == INDIRECT_REF)
11396 return expand_expr (treeop0, const0_rtx, VOIDmode,
11397 modifier);
11398
11399 else if (TREE_CODE_CLASS (code) == tcc_binary
11400 || TREE_CODE_CLASS (code) == tcc_comparison
11401 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
11402 {
11403 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
11404 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
11405 return const0_rtx;
11406 }
11407
11408 target = 0;
11409 }
11410
11411 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
11412 target = 0;
11413
11414 /* Use subtarget as the target for operand 0 of a binary operation. */
11415 subtarget = get_subtarget (target);
11416 original_target = target;
11417
11418 switch (code)
11419 {
11420 case LABEL_DECL:
11421 {
11422 tree function = decl_function_context (exp);
11423
11424 temp = label_rtx (exp);
11425 temp = gen_rtx_LABEL_REF (Pmode, temp);
11426
11427 if (function != current_function_decl
11428 && function != 0)
11429 LABEL_REF_NONLOCAL_P (temp) = 1;
11430
11431 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
11432 return temp;
11433 }
11434
11435 case SSA_NAME:
11436 /* ??? ivopts calls expander, without any preparation from
11437 out-of-ssa. So fake instructions as if this was an access to the
11438 base variable. This unnecessarily allocates a pseudo, see how we can
11439 reuse it, if partition base vars have it set already. */
11440 if (!currently_expanding_to_rtl)
11441 {
11442 tree var = SSA_NAME_VAR (exp);
11443 if (var && DECL_RTL_SET_P (var))
11444 return DECL_RTL (var);
11445 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
11446 LAST_VIRTUAL_REGISTER + 1);
11447 }
11448
11449 g = get_gimple_for_ssa_name (exp);
11450 /* For EXPAND_INITIALIZER try harder to get something simpler. */
11451 if (g == NULL
11452 && modifier == EXPAND_INITIALIZER
11453 && !SSA_NAME_IS_DEFAULT_DEF (exp)
11454 && (optimize || !SSA_NAME_VAR (exp)
11455 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
11456 && is_gimple_assign (SSA_NAME_DEF_STMT (exp))
11457 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
11458 g = SSA_NAME_DEF_STMT (exp);
11459 if (safe_is_a <gassign *> (g))
11460 return expand_expr_real_gassign (as_a<gassign *> (g), target, tmode,
11461 modifier, alt_rtl, inner_reference_p);
11462 else if (safe_is_a <gcall *> (g))
11463 {
11464 /* ??? internal call expansion doesn't follow the usual API
11465 of returning the destination RTX and being passed a desired
11466 target. */
11467 rtx dest = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
11468 tree tmplhs = make_tree (TREE_TYPE (exp), dest);
11469 gimple_call_set_lhs (g, tmplhs);
11470 expand_internal_call (as_a <gcall *> (g));
11471 gimple_call_set_lhs (g, exp);
11472 return dest;
11473 }
11474
11475 ssa_name = exp;
11476 decl_rtl = get_rtx_for_ssa_name (ssa_name);
11477 exp = SSA_NAME_VAR (ssa_name);
11478 /* Optimize and avoid to EXTEND_BITINIT doing anything if it is an
11479 SSA_NAME computed within the current function. In such case the
11480 value have been already extended before. While if it is a function
11481 parameter, result or some memory location, we need to be prepared
11482 for some other compiler leaving the bits uninitialized. */
11483 if (!exp || VAR_P (exp))
11484 reduce_bit_field = false;
11485 goto expand_decl_rtl;
11486
11487 case VAR_DECL:
11488 /* Allow accel compiler to handle variables that require special
11489 treatment, e.g. if they have been modified in some way earlier in
11490 compilation by the adjust_private_decl OpenACC hook. */
11491 if (flag_openacc && targetm.goacc.expand_var_decl)
11492 {
11493 temp = targetm.goacc.expand_var_decl (exp);
11494 if (temp)
11495 return temp;
11496 }
11497 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
11498 have scalar integer modes to a reg via store_constructor. */
11499 if (TREE_READONLY (exp)
11500 && !TREE_SIDE_EFFECTS (exp)
11501 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
11502 && immediate_const_ctor_p (DECL_INITIAL (exp))
11503 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
11504 && crtl->emit.regno_pointer_align_length
11505 && !target)
11506 {
11507 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
11508 store_constructor (DECL_INITIAL (exp), target, 0,
11509 int_expr_size (DECL_INITIAL (exp)), false);
11510 return target;
11511 }
11512 /* ... fall through ... */
11513
11514 case PARM_DECL:
11515 /* If a static var's type was incomplete when the decl was written,
11516 but the type is complete now, lay out the decl now. */
11517 if (DECL_SIZE (exp) == 0
11518 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
11519 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
11520 layout_decl (exp, 0);
11521
11522 /* fall through */
11523
11524 case FUNCTION_DECL:
11525 case RESULT_DECL:
11526 decl_rtl = DECL_RTL (exp);
11527 expand_decl_rtl:
11528 gcc_assert (decl_rtl);
11529
11530 /* DECL_MODE might change when TYPE_MODE depends on attribute target
11531 settings for VECTOR_TYPE_P that might switch for the function. */
11532 if (currently_expanding_to_rtl
11533 && code == VAR_DECL && MEM_P (decl_rtl)
11534 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
11535 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
11536 else
11537 decl_rtl = copy_rtx (decl_rtl);
11538
11539 /* Record writes to register variables. */
11540 if (modifier == EXPAND_WRITE
11541 && REG_P (decl_rtl)
11542 && HARD_REGISTER_P (decl_rtl))
11543 add_to_hard_reg_set (&crtl->asm_clobbers,
11544 GET_MODE (decl_rtl), REGNO (decl_rtl));
11545
11546 /* Ensure variable marked as used even if it doesn't go through
11547 a parser. If it hasn't be used yet, write out an external
11548 definition. */
11549 if (exp)
11550 TREE_USED (exp) = 1;
11551
11552 /* Show we haven't gotten RTL for this yet. */
11553 temp = 0;
11554
11555 /* Variables inherited from containing functions should have
11556 been lowered by this point. */
11557 if (exp)
11558 {
11559 tree context = decl_function_context (exp);
11560 gcc_assert (SCOPE_FILE_SCOPE_P (context)
11561 || context == current_function_decl
11562 || TREE_STATIC (exp)
11563 || DECL_EXTERNAL (exp)
11564 /* ??? C++ creates functions that are not
11565 TREE_STATIC. */
11566 || TREE_CODE (exp) == FUNCTION_DECL);
11567 }
11568
11569 /* This is the case of an array whose size is to be determined
11570 from its initializer, while the initializer is still being parsed.
11571 ??? We aren't parsing while expanding anymore. */
11572
11573 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
11574 temp = validize_mem (decl_rtl);
11575
11576 /* If DECL_RTL is memory, we are in the normal case and the
11577 address is not valid, get the address into a register. */
11578
11579 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
11580 {
11581 if (alt_rtl)
11582 *alt_rtl = decl_rtl;
11583 decl_rtl = use_anchored_address (decl_rtl);
11584 if (modifier != EXPAND_CONST_ADDRESS
11585 && modifier != EXPAND_SUM
11586 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
11587 : GET_MODE (decl_rtl),
11588 XEXP (decl_rtl, 0),
11589 MEM_ADDR_SPACE (decl_rtl)))
11590 temp = replace_equiv_address (decl_rtl,
11591 copy_rtx (XEXP (decl_rtl, 0)));
11592 }
11593
11594 /* If we got something, return it. But first, set the alignment
11595 if the address is a register. */
11596 if (temp != 0)
11597 {
11598 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
11599 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
11600 }
11601 else if (MEM_P (decl_rtl))
11602 temp = decl_rtl;
11603
11604 if (temp != 0)
11605 {
11606 if (MEM_P (temp)
11607 && modifier != EXPAND_WRITE
11608 && modifier != EXPAND_MEMORY
11609 && modifier != EXPAND_INITIALIZER
11610 && modifier != EXPAND_CONST_ADDRESS
11611 && modifier != EXPAND_SUM
11612 && !inner_reference_p
11613 && mode != BLKmode
11614 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
11615 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11616 MEM_ALIGN (temp), NULL_RTX, NULL);
11617
11618 return EXTEND_BITINT (temp);
11619 }
11620
11621 if (exp)
11622 dmode = DECL_MODE (exp);
11623 else
11624 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
11625
11626 /* If the mode of DECL_RTL does not match that of the decl,
11627 there are two cases: we are dealing with a BLKmode value
11628 that is returned in a register, or we are dealing with
11629 a promoted value. In the latter case, return a SUBREG
11630 of the wanted mode, but mark it so that we know that it
11631 was already extended. */
11632 if (REG_P (decl_rtl)
11633 && dmode != BLKmode
11634 && GET_MODE (decl_rtl) != dmode)
11635 {
11636 machine_mode pmode;
11637
11638 /* Get the signedness to be used for this variable. Ensure we get
11639 the same mode we got when the variable was declared. */
11640 if (code != SSA_NAME)
11641 pmode = promote_decl_mode (exp, &unsignedp);
11642 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
11643 && gimple_code (g) == GIMPLE_CALL
11644 && !gimple_call_internal_p (g))
11645 pmode = promote_function_mode (type, mode, &unsignedp,
11646 gimple_call_fntype (g),
11647 2);
11648 else
11649 pmode = promote_ssa_mode (ssa_name, &unsignedp);
11650 gcc_assert (GET_MODE (decl_rtl) == pmode);
11651
11652 /* Some ABIs require scalar floating point modes to be passed
11653 in a wider scalar integer mode. We need to explicitly
11654 truncate to an integer mode of the correct precision before
11655 using a SUBREG to reinterpret as a floating point value. */
11656 if (SCALAR_FLOAT_MODE_P (mode)
11657 && SCALAR_INT_MODE_P (pmode)
11658 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
11659 return convert_wider_int_to_float (mode, pmode, decl_rtl);
11660
11661 temp = gen_lowpart_SUBREG (mode, decl_rtl);
11662 SUBREG_PROMOTED_VAR_P (temp) = 1;
11663 SUBREG_PROMOTED_SET (temp, unsignedp);
11664 return EXTEND_BITINT (temp);
11665 }
11666
11667 return EXTEND_BITINT (decl_rtl);
11668
11669 case INTEGER_CST:
11670 {
11671 if (TREE_CODE (type) == BITINT_TYPE)
11672 {
11673 unsigned int prec = TYPE_PRECISION (type);
11674 struct bitint_info info;
11675 bool ok = targetm.c.bitint_type_info (prec, &info);
11676 gcc_assert (ok);
11677 scalar_int_mode limb_mode
11678 = as_a <scalar_int_mode> (info.limb_mode);
11679 unsigned int limb_prec = GET_MODE_PRECISION (limb_mode);
11680 if (prec > limb_prec && prec > MAX_FIXED_MODE_SIZE)
11681 {
11682 /* Emit large/huge _BitInt INTEGER_CSTs into memory. */
11683 exp = tree_output_constant_def (exp);
11684 return expand_expr (exp, target, VOIDmode, modifier);
11685 }
11686 }
11687
11688 /* Given that TYPE_PRECISION (type) is not always equal to
11689 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11690 the former to the latter according to the signedness of the
11691 type. */
11692 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11693 temp = immed_wide_int_const
11694 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11695 return temp;
11696 }
11697
11698 case VECTOR_CST:
11699 {
11700 tree tmp = NULL_TREE;
11701 if (VECTOR_MODE_P (mode))
11702 return const_vector_from_tree (exp);
11703 scalar_int_mode int_mode;
11704 if (is_int_mode (mode, &int_mode))
11705 {
11706 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11707 if (type_for_mode)
11708 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11709 type_for_mode, exp);
11710 }
11711 if (!tmp)
11712 {
11713 vec<constructor_elt, va_gc> *v;
11714 /* Constructors need to be fixed-length. FIXME. */
11715 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11716 vec_alloc (v, nunits);
11717 for (unsigned int i = 0; i < nunits; ++i)
11718 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11719 tmp = build_constructor (type, v);
11720 }
11721 return expand_expr (tmp, ignore ? const0_rtx : target,
11722 tmode, modifier);
11723 }
11724
11725 case CONST_DECL:
11726 if (modifier == EXPAND_WRITE)
11727 {
11728 /* Writing into CONST_DECL is always invalid, but handle it
11729 gracefully. */
11730 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11731 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11732 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11733 EXPAND_NORMAL, as);
11734 op0 = memory_address_addr_space (mode, op0, as);
11735 temp = gen_rtx_MEM (mode, op0);
11736 set_mem_addr_space (temp, as);
11737 return temp;
11738 }
11739 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11740
11741 case REAL_CST:
11742 /* If optimized, generate immediate CONST_DOUBLE
11743 which will be turned into memory by reload if necessary.
11744
11745 We used to force a register so that loop.c could see it. But
11746 this does not allow gen_* patterns to perform optimizations with
11747 the constants. It also produces two insns in cases like "x = 1.0;".
11748 On most machines, floating-point constants are not permitted in
11749 many insns, so we'd end up copying it to a register in any case.
11750
11751 Now, we do the copying in expand_binop, if appropriate. */
11752 return const_double_from_real_value (TREE_REAL_CST (exp),
11753 TYPE_MODE (TREE_TYPE (exp)));
11754
11755 case FIXED_CST:
11756 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11757 TYPE_MODE (TREE_TYPE (exp)));
11758
11759 case COMPLEX_CST:
11760 /* Handle evaluating a complex constant in a CONCAT target. */
11761 if (original_target && GET_CODE (original_target) == CONCAT)
11762 {
11763 rtx rtarg, itarg;
11764
11765 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11766 rtarg = XEXP (original_target, 0);
11767 itarg = XEXP (original_target, 1);
11768
11769 /* Move the real and imaginary parts separately. */
11770 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11771 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11772
11773 if (op0 != rtarg)
11774 emit_move_insn (rtarg, op0);
11775 if (op1 != itarg)
11776 emit_move_insn (itarg, op1);
11777
11778 return original_target;
11779 }
11780
11781 /* fall through */
11782
11783 case STRING_CST:
11784 temp = expand_expr_constant (exp, 1, modifier);
11785
11786 /* temp contains a constant address.
11787 On RISC machines where a constant address isn't valid,
11788 make some insns to get that address into a register. */
11789 if (modifier != EXPAND_CONST_ADDRESS
11790 && modifier != EXPAND_INITIALIZER
11791 && modifier != EXPAND_SUM
11792 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11793 MEM_ADDR_SPACE (temp)))
11794 return replace_equiv_address (temp,
11795 copy_rtx (XEXP (temp, 0)));
11796 return temp;
11797
11798 case POLY_INT_CST:
11799 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11800
11801 case SAVE_EXPR:
11802 {
11803 tree val = treeop0;
11804 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11805 inner_reference_p);
11806
11807 if (!SAVE_EXPR_RESOLVED_P (exp))
11808 {
11809 /* We can indeed still hit this case, typically via builtin
11810 expanders calling save_expr immediately before expanding
11811 something. Assume this means that we only have to deal
11812 with non-BLKmode values. */
11813 gcc_assert (GET_MODE (ret) != BLKmode);
11814
11815 val = build_decl (curr_insn_location (),
11816 VAR_DECL, NULL, TREE_TYPE (exp));
11817 DECL_ARTIFICIAL (val) = 1;
11818 DECL_IGNORED_P (val) = 1;
11819 treeop0 = val;
11820 TREE_OPERAND (exp, 0) = treeop0;
11821 SAVE_EXPR_RESOLVED_P (exp) = 1;
11822
11823 if (!CONSTANT_P (ret))
11824 ret = copy_to_reg (ret);
11825 SET_DECL_RTL (val, ret);
11826 }
11827
11828 return ret;
11829 }
11830
11831
11832 case CONSTRUCTOR:
11833 /* If we don't need the result, just ensure we evaluate any
11834 subexpressions. */
11835 if (ignore)
11836 {
11837 unsigned HOST_WIDE_INT idx;
11838 tree value;
11839
11840 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11841 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11842
11843 return const0_rtx;
11844 }
11845
11846 return expand_constructor (exp, target, modifier, false);
11847
11848 case TARGET_MEM_REF:
11849 {
11850 addr_space_t as
11851 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11852 unsigned int align;
11853
11854 op0 = addr_for_mem_ref (exp, as, true);
11855 op0 = memory_address_addr_space (mode, op0, as);
11856 temp = gen_rtx_MEM (mode, op0);
11857 set_mem_attributes (temp, exp, 0);
11858 set_mem_addr_space (temp, as);
11859 align = get_object_alignment (exp);
11860 if (modifier != EXPAND_WRITE
11861 && modifier != EXPAND_MEMORY
11862 && mode != BLKmode
11863 && align < GET_MODE_ALIGNMENT (mode))
11864 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11865 align, NULL_RTX, NULL);
11866 return EXTEND_BITINT (temp);
11867 }
11868
11869 case MEM_REF:
11870 {
11871 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11872 addr_space_t as
11873 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11874 machine_mode address_mode;
11875 tree base = TREE_OPERAND (exp, 0);
11876 gimple *def_stmt;
11877 unsigned align;
11878 /* Handle expansion of non-aliased memory with non-BLKmode. That
11879 might end up in a register. */
11880 if (mem_ref_refers_to_non_mem_p (exp))
11881 {
11882 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11883 base = TREE_OPERAND (base, 0);
11884 poly_uint64 type_size;
11885 if (known_eq (offset, 0)
11886 && !reverse
11887 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11888 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11889 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11890 target, tmode, modifier);
11891 unsigned align;
11892 if (TYPE_MODE (type) == BLKmode || maybe_lt (offset, 0))
11893 {
11894 temp = assign_stack_temp (DECL_MODE (base),
11895 GET_MODE_SIZE (DECL_MODE (base)));
11896 store_expr (base, temp, 0, false, false);
11897 temp = adjust_address (temp, TYPE_MODE (type), offset);
11898 if (TYPE_MODE (type) == BLKmode)
11899 set_mem_size (temp, int_size_in_bytes (type));
11900 /* When the original ref was misaligned so will be the
11901 access to the stack temporary. Not all targets handle
11902 this correctly, some will ICE in sanity checking.
11903 Handle this by doing bitfield extraction when necessary. */
11904 else if ((align = get_object_alignment (exp))
11905 < GET_MODE_ALIGNMENT (TYPE_MODE (type)))
11906 temp
11907 = expand_misaligned_mem_ref (temp, TYPE_MODE (type),
11908 unsignedp, align,
11909 modifier == EXPAND_STACK_PARM
11910 ? NULL_RTX : target, NULL);
11911 return temp;
11912 }
11913 /* When the access is fully outside of the underlying object
11914 expand the offset as zero. This avoids out-of-bound
11915 BIT_FIELD_REFs and generates smaller code for these cases
11916 with UB. */
11917 type_size = tree_to_poly_uint64 (TYPE_SIZE_UNIT (type));
11918 if (!ranges_maybe_overlap_p (offset, type_size, 0,
11919 GET_MODE_SIZE (DECL_MODE (base))))
11920 offset = 0;
11921 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11922 bitsize_int (offset * BITS_PER_UNIT));
11923 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11924 return expand_expr (exp, target, tmode, modifier);
11925 }
11926 address_mode = targetm.addr_space.address_mode (as);
11927 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11928 {
11929 tree mask = gimple_assign_rhs2 (def_stmt);
11930 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11931 gimple_assign_rhs1 (def_stmt), mask);
11932 TREE_OPERAND (exp, 0) = base;
11933 }
11934 align = get_object_alignment (exp);
11935 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11936 op0 = memory_address_addr_space (mode, op0, as);
11937 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11938 {
11939 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11940 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11941 op0 = memory_address_addr_space (mode, op0, as);
11942 }
11943 temp = gen_rtx_MEM (mode, op0);
11944 set_mem_attributes (temp, exp, 0);
11945 set_mem_addr_space (temp, as);
11946 if (TREE_THIS_VOLATILE (exp))
11947 MEM_VOLATILE_P (temp) = 1;
11948 if (modifier == EXPAND_WRITE || modifier == EXPAND_MEMORY)
11949 return temp;
11950 if (!inner_reference_p
11951 && mode != BLKmode
11952 && align < GET_MODE_ALIGNMENT (mode))
11953 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11954 modifier == EXPAND_STACK_PARM
11955 ? NULL_RTX : target, alt_rtl);
11956 if (reverse)
11957 temp = flip_storage_order (mode, temp);
11958 return EXTEND_BITINT (temp);
11959 }
11960
11961 case ARRAY_REF:
11962
11963 {
11964 tree array = treeop0;
11965 tree index = treeop1;
11966 tree init;
11967
11968 /* Fold an expression like: "foo"[2].
11969 This is not done in fold so it won't happen inside &.
11970 Don't fold if this is for wide characters since it's too
11971 difficult to do correctly and this is a very rare case. */
11972
11973 if (modifier != EXPAND_CONST_ADDRESS
11974 && modifier != EXPAND_INITIALIZER
11975 && modifier != EXPAND_MEMORY)
11976 {
11977 tree t = fold_read_from_constant_string (exp);
11978
11979 if (t)
11980 return expand_expr (t, target, tmode, modifier);
11981 }
11982
11983 /* If this is a constant index into a constant array,
11984 just get the value from the array. Handle both the cases when
11985 we have an explicit constructor and when our operand is a variable
11986 that was declared const. */
11987
11988 if (modifier != EXPAND_CONST_ADDRESS
11989 && modifier != EXPAND_INITIALIZER
11990 && modifier != EXPAND_MEMORY
11991 && TREE_CODE (array) == CONSTRUCTOR
11992 && ! TREE_SIDE_EFFECTS (array)
11993 && TREE_CODE (index) == INTEGER_CST)
11994 {
11995 unsigned HOST_WIDE_INT ix;
11996 tree field, value;
11997
11998 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11999 field, value)
12000 if (tree_int_cst_equal (field, index))
12001 {
12002 if (!TREE_SIDE_EFFECTS (value)
12003 && TREE_CODE (value) != RAW_DATA_CST)
12004 return expand_expr (fold (value), target, tmode, modifier);
12005 break;
12006 }
12007 }
12008
12009 else if (optimize >= 1
12010 && modifier != EXPAND_CONST_ADDRESS
12011 && modifier != EXPAND_INITIALIZER
12012 && modifier != EXPAND_MEMORY
12013 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
12014 && TREE_CODE (index) == INTEGER_CST
12015 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
12016 && (init = ctor_for_folding (array)) != error_mark_node)
12017 {
12018 if (init == NULL_TREE)
12019 {
12020 tree value = build_zero_cst (type);
12021 if (TREE_CODE (value) == CONSTRUCTOR)
12022 {
12023 /* If VALUE is a CONSTRUCTOR, this optimization is only
12024 useful if this doesn't store the CONSTRUCTOR into
12025 memory. If it does, it is more efficient to just
12026 load the data from the array directly. */
12027 rtx ret = expand_constructor (value, target,
12028 modifier, true);
12029 if (ret == NULL_RTX)
12030 value = NULL_TREE;
12031 }
12032
12033 if (value)
12034 return expand_expr (value, target, tmode, modifier);
12035 }
12036 else if (TREE_CODE (init) == CONSTRUCTOR)
12037 {
12038 unsigned HOST_WIDE_INT ix;
12039 tree field, value;
12040
12041 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
12042 field, value)
12043 if (tree_int_cst_equal (field, index))
12044 {
12045 if (TREE_SIDE_EFFECTS (value)
12046 || TREE_CODE (value) == RAW_DATA_CST)
12047 break;
12048
12049 if (TREE_CODE (value) == CONSTRUCTOR)
12050 {
12051 /* If VALUE is a CONSTRUCTOR, this
12052 optimization is only useful if
12053 this doesn't store the CONSTRUCTOR
12054 into memory. If it does, it is more
12055 efficient to just load the data from
12056 the array directly. */
12057 rtx ret = expand_constructor (value, target,
12058 modifier, true);
12059 if (ret == NULL_RTX)
12060 break;
12061 }
12062
12063 return expand_expr (fold (value), target, tmode,
12064 modifier);
12065 }
12066 }
12067 else if (TREE_CODE (init) == STRING_CST)
12068 {
12069 tree low_bound = array_ref_low_bound (exp);
12070 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
12071
12072 /* Optimize the special case of a zero lower bound.
12073
12074 We convert the lower bound to sizetype to avoid problems
12075 with constant folding. E.g. suppose the lower bound is
12076 1 and its mode is QI. Without the conversion
12077 (ARRAY + (INDEX - (unsigned char)1))
12078 becomes
12079 (ARRAY + (-(unsigned char)1) + INDEX)
12080 which becomes
12081 (ARRAY + 255 + INDEX). Oops! */
12082 if (!integer_zerop (low_bound))
12083 index1 = size_diffop_loc (loc, index1,
12084 fold_convert_loc (loc, sizetype,
12085 low_bound));
12086
12087 if (tree_fits_uhwi_p (index1)
12088 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
12089 {
12090 tree char_type = TREE_TYPE (TREE_TYPE (init));
12091 scalar_int_mode char_mode;
12092
12093 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
12094 && GET_MODE_SIZE (char_mode) == 1)
12095 return gen_int_mode (TREE_STRING_POINTER (init)
12096 [TREE_INT_CST_LOW (index1)],
12097 char_mode);
12098 }
12099 }
12100 }
12101 }
12102 goto normal_inner_ref;
12103
12104 case COMPONENT_REF:
12105 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
12106 /* Fall through. */
12107 case BIT_FIELD_REF:
12108 case ARRAY_RANGE_REF:
12109 normal_inner_ref:
12110 {
12111 machine_mode mode1, mode2;
12112 poly_int64 bitsize, bitpos, bytepos;
12113 tree offset;
12114 int reversep, volatilep = 0;
12115 tree tem
12116 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
12117 &unsignedp, &reversep, &volatilep);
12118 rtx orig_op0, memloc;
12119 bool clear_mem_expr = false;
12120 bool must_force_mem;
12121
12122 /* If we got back the original object, something is wrong. Perhaps
12123 we are evaluating an expression too early. In any event, don't
12124 infinitely recurse. */
12125 gcc_assert (tem != exp);
12126
12127 /* Make sure bitpos is not negative, this can wreak havoc later. */
12128 if (maybe_lt (bitpos, 0))
12129 {
12130 gcc_checking_assert (offset == NULL_TREE);
12131 offset = size_int (bits_to_bytes_round_down (bitpos));
12132 bitpos = num_trailing_bits (bitpos);
12133 }
12134
12135 /* If we have either an offset, a BLKmode result, or a reference
12136 outside the underlying object, we must force it to memory.
12137 Such a case can occur in Ada if we have unchecked conversion
12138 of an expression from a scalar type to an aggregate type or
12139 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
12140 passed a partially uninitialized object or a view-conversion
12141 to a larger size. */
12142 must_force_mem = offset != NULL_TREE
12143 || mode1 == BLKmode
12144 || (mode == BLKmode
12145 && !int_mode_for_size (bitsize, 1).exists ());
12146
12147 const enum expand_modifier tem_modifier
12148 = must_force_mem
12149 ? EXPAND_MEMORY
12150 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
12151
12152 /* If TEM's type is a union of variable size, pass TARGET to the inner
12153 computation, since it will need a temporary and TARGET is known
12154 to have to do. This occurs in unchecked conversion in Ada. */
12155 const rtx tem_target
12156 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
12157 && COMPLETE_TYPE_P (TREE_TYPE (tem))
12158 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
12159 && modifier != EXPAND_STACK_PARM
12160 ? target
12161 : NULL_RTX;
12162
12163 orig_op0 = op0
12164 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
12165 true);
12166
12167 /* If the field has a mode, we want to access it in the
12168 field's mode, not the computed mode.
12169 If a MEM has VOIDmode (external with incomplete type),
12170 use BLKmode for it instead. */
12171 if (MEM_P (op0))
12172 {
12173 if (mode1 != VOIDmode)
12174 op0 = adjust_address (op0, mode1, 0);
12175 else if (GET_MODE (op0) == VOIDmode)
12176 op0 = adjust_address (op0, BLKmode, 0);
12177 }
12178
12179 mode2
12180 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
12181
12182 /* See above for the rationale. */
12183 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
12184 must_force_mem = true;
12185
12186 /* Handle CONCAT first. */
12187 if (GET_CODE (op0) == CONCAT && !must_force_mem)
12188 {
12189 if (known_eq (bitpos, 0)
12190 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
12191 && COMPLEX_MODE_P (mode1)
12192 && COMPLEX_MODE_P (GET_MODE (op0))
12193 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
12194 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
12195 {
12196 if (reversep)
12197 op0 = flip_storage_order (GET_MODE (op0), op0);
12198 if (mode1 != GET_MODE (op0))
12199 {
12200 rtx parts[2];
12201 for (int i = 0; i < 2; i++)
12202 {
12203 rtx op = read_complex_part (op0, i != 0);
12204 if (GET_CODE (op) == SUBREG)
12205 op = force_reg (GET_MODE (op), op);
12206 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
12207 if (temp)
12208 op = temp;
12209 else
12210 {
12211 if (!REG_P (op) && !MEM_P (op))
12212 op = force_reg (GET_MODE (op), op);
12213 op = gen_lowpart (GET_MODE_INNER (mode1), op);
12214 }
12215 parts[i] = op;
12216 }
12217 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
12218 }
12219 return op0;
12220 }
12221 if (known_eq (bitpos, 0)
12222 && known_eq (bitsize,
12223 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
12224 && maybe_ne (bitsize, 0))
12225 {
12226 op0 = XEXP (op0, 0);
12227 mode2 = GET_MODE (op0);
12228 }
12229 else if (known_eq (bitpos,
12230 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
12231 && known_eq (bitsize,
12232 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
12233 && maybe_ne (bitpos, 0)
12234 && maybe_ne (bitsize, 0))
12235 {
12236 op0 = XEXP (op0, 1);
12237 bitpos = 0;
12238 mode2 = GET_MODE (op0);
12239 }
12240 else
12241 /* Otherwise force into memory. */
12242 must_force_mem = true;
12243 }
12244
12245 /* If this is a constant, put it in a register if it is a legitimate
12246 constant and we don't need a memory reference. */
12247 if (CONSTANT_P (op0)
12248 && mode2 != BLKmode
12249 && targetm.legitimate_constant_p (mode2, op0)
12250 && !must_force_mem)
12251 op0 = force_reg (mode2, op0);
12252
12253 /* Otherwise, if this is a constant, try to force it to the constant
12254 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
12255 is a legitimate constant. */
12256 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
12257 op0 = validize_mem (memloc);
12258
12259 /* Otherwise, if this is a constant or the object is not in memory
12260 and need be, put it there. */
12261 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
12262 {
12263 machine_mode tem_mode = TYPE_MODE (TREE_TYPE (tem));
12264 poly_int64 size;
12265 if (!poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (tem)), &size))
12266 size = max_int_size_in_bytes (TREE_TYPE (tem));
12267 unsigned int align = TREE_CODE (tem) == SSA_NAME
12268 ? TYPE_ALIGN (TREE_TYPE (tem))
12269 : get_object_alignment (tem);
12270 if (STRICT_ALIGNMENT)
12271 {
12272 /* For STRICT_ALIGNMENT targets, when we force the operand to
12273 memory, we may need to increase the alignment to meet the
12274 expectation in later RTL lowering passes. The increased
12275 alignment is capped by MAX_SUPPORTED_STACK_ALIGNMENT. */
12276 if (tem_mode != BLKmode)
12277 align = MAX (align, GET_MODE_ALIGNMENT (tem_mode));
12278 else
12279 align = MAX (align, TYPE_ALIGN (TREE_TYPE (tem)));
12280 align = MIN (align, (unsigned) MAX_SUPPORTED_STACK_ALIGNMENT);
12281 }
12282 memloc = assign_stack_local (tem_mode, size, align);
12283 emit_move_insn (memloc, op0);
12284 op0 = memloc;
12285 clear_mem_expr = true;
12286 }
12287
12288 if (offset)
12289 {
12290 machine_mode address_mode;
12291 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
12292 EXPAND_SUM);
12293
12294 gcc_assert (MEM_P (op0));
12295
12296 address_mode = get_address_mode (op0);
12297 if (GET_MODE (offset_rtx) != address_mode)
12298 {
12299 /* We cannot be sure that the RTL in offset_rtx is valid outside
12300 of a memory address context, so force it into a register
12301 before attempting to convert it to the desired mode. */
12302 offset_rtx = force_operand (offset_rtx, NULL_RTX);
12303 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
12304 }
12305
12306 /* See the comment in expand_assignment for the rationale. */
12307 if (mode1 != VOIDmode
12308 && maybe_ne (bitpos, 0)
12309 && maybe_gt (bitsize, 0)
12310 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12311 && multiple_p (bitpos, bitsize)
12312 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
12313 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
12314 {
12315 op0 = adjust_address (op0, mode1, bytepos);
12316 bitpos = 0;
12317 }
12318
12319 op0 = offset_address (op0, offset_rtx,
12320 highest_pow2_factor (offset));
12321 }
12322
12323 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
12324 record its alignment as BIGGEST_ALIGNMENT. */
12325 if (MEM_P (op0)
12326 && known_eq (bitpos, 0)
12327 && offset != 0
12328 && is_aligning_offset (offset, tem))
12329 set_mem_align (op0, BIGGEST_ALIGNMENT);
12330
12331 /* Don't forget about volatility even if this is a bitfield. */
12332 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
12333 {
12334 if (op0 == orig_op0)
12335 op0 = copy_rtx (op0);
12336
12337 MEM_VOLATILE_P (op0) = 1;
12338 }
12339
12340 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
12341 {
12342 if (op0 == orig_op0)
12343 op0 = copy_rtx (op0);
12344
12345 set_mem_align (op0, BITS_PER_UNIT);
12346 }
12347
12348 /* In cases where an aligned union has an unaligned object
12349 as a field, we might be extracting a BLKmode value from
12350 an integer-mode (e.g., SImode) object. Handle this case
12351 by doing the extract into an object as wide as the field
12352 (which we know to be the width of a basic mode), then
12353 storing into memory, and changing the mode to BLKmode. */
12354 if (mode1 == VOIDmode
12355 || REG_P (op0) || GET_CODE (op0) == SUBREG
12356 || (mode1 != BLKmode && ! direct_load[(int) mode1]
12357 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
12358 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
12359 && modifier != EXPAND_CONST_ADDRESS
12360 && modifier != EXPAND_INITIALIZER
12361 && modifier != EXPAND_MEMORY)
12362 /* If the bitfield is volatile and the bitsize
12363 is narrower than the access size of the bitfield,
12364 we need to extract bitfields from the access. */
12365 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
12366 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
12367 && mode1 != BLKmode
12368 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
12369 /* If the field isn't aligned enough to fetch as a memref,
12370 fetch it as a bit field. */
12371 || (mode1 != BLKmode
12372 && (((MEM_P (op0)
12373 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
12374 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
12375 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
12376 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
12377 && modifier != EXPAND_MEMORY
12378 && ((modifier == EXPAND_CONST_ADDRESS
12379 || modifier == EXPAND_INITIALIZER)
12380 ? STRICT_ALIGNMENT
12381 : targetm.slow_unaligned_access (mode1,
12382 MEM_ALIGN (op0))))
12383 || !multiple_p (bitpos, BITS_PER_UNIT)))
12384 /* If the type and the field are a constant size and the
12385 size of the type isn't the same size as the bitfield,
12386 we must use bitfield operations. */
12387 || (known_size_p (bitsize)
12388 && TYPE_SIZE (TREE_TYPE (exp))
12389 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
12390 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
12391 bitsize)))
12392 {
12393 machine_mode ext_mode = mode;
12394
12395 if (ext_mode == BLKmode
12396 && ! (target != 0 && MEM_P (op0)
12397 && MEM_P (target)
12398 && multiple_p (bitpos, BITS_PER_UNIT)))
12399 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
12400
12401 if (ext_mode == BLKmode)
12402 {
12403 if (target == 0)
12404 target = assign_temp (type, 1, 1);
12405
12406 /* ??? Unlike the similar test a few lines below, this one is
12407 very likely obsolete. */
12408 if (known_eq (bitsize, 0))
12409 return target;
12410
12411 /* In this case, BITPOS must start at a byte boundary and
12412 TARGET, if specified, must be a MEM. */
12413 gcc_assert (MEM_P (op0)
12414 && (!target || MEM_P (target)));
12415
12416 bytepos = exact_div (bitpos, BITS_PER_UNIT);
12417 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
12418 emit_block_move (target,
12419 adjust_address (op0, VOIDmode, bytepos),
12420 gen_int_mode (bytesize, Pmode),
12421 (modifier == EXPAND_STACK_PARM
12422 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
12423
12424 return target;
12425 }
12426
12427 /* If we have nothing to extract, the result will be 0 for targets
12428 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
12429 return 0 for the sake of consistency, as reading a zero-sized
12430 bitfield is valid in Ada and the value is fully specified. */
12431 if (known_eq (bitsize, 0))
12432 return const0_rtx;
12433
12434 op0 = validize_mem (op0);
12435
12436 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
12437 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12438
12439 /* If the result has aggregate type and the extraction is done in
12440 an integral mode, then the field may be not aligned on a byte
12441 boundary; in this case, if it has reverse storage order, it
12442 needs to be extracted as a scalar field with reverse storage
12443 order and put back into memory order afterwards. */
12444 if (AGGREGATE_TYPE_P (type)
12445 && GET_MODE_CLASS (ext_mode) == MODE_INT)
12446 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
12447
12448 gcc_checking_assert (known_ge (bitpos, 0));
12449 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
12450 (modifier == EXPAND_STACK_PARM
12451 ? NULL_RTX : target),
12452 ext_mode, ext_mode, reversep, alt_rtl);
12453
12454 /* If the result has aggregate type and the mode of OP0 is an
12455 integral mode then, if BITSIZE is narrower than this mode
12456 and this is for big-endian data, we must put the field
12457 into the high-order bits. And we must also put it back
12458 into memory order if it has been previously reversed. */
12459 scalar_int_mode op0_mode;
12460 if (AGGREGATE_TYPE_P (type)
12461 && is_int_mode (GET_MODE (op0), &op0_mode))
12462 {
12463 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
12464
12465 gcc_checking_assert (known_le (bitsize, size));
12466 if (maybe_lt (bitsize, size)
12467 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
12468 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
12469 size - bitsize, op0, 1);
12470
12471 if (reversep)
12472 op0 = flip_storage_order (op0_mode, op0);
12473 }
12474
12475 /* If the result type is BLKmode, store the data into a temporary
12476 of the appropriate type, but with the mode corresponding to the
12477 mode for the data we have (op0's mode). */
12478 if (mode == BLKmode)
12479 {
12480 rtx new_rtx
12481 = assign_stack_temp_for_type (ext_mode,
12482 GET_MODE_BITSIZE (ext_mode),
12483 type);
12484 emit_move_insn (new_rtx, op0);
12485 op0 = copy_rtx (new_rtx);
12486 PUT_MODE (op0, BLKmode);
12487 }
12488
12489 return op0;
12490 }
12491
12492 /* If the result is BLKmode, use that to access the object
12493 now as well. */
12494 if (mode == BLKmode)
12495 mode1 = BLKmode;
12496
12497 /* Get a reference to just this component. */
12498 bytepos = bits_to_bytes_round_down (bitpos);
12499 if (modifier == EXPAND_CONST_ADDRESS
12500 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
12501 op0 = adjust_address_nv (op0, mode1, bytepos);
12502 else
12503 op0 = adjust_address (op0, mode1, bytepos);
12504
12505 if (op0 == orig_op0)
12506 op0 = copy_rtx (op0);
12507
12508 /* Don't set memory attributes if the base expression is
12509 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
12510 we should just honor its original memory attributes. */
12511 if (!(TREE_CODE (tem) == SSA_NAME
12512 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
12513 set_mem_attributes (op0, exp, 0);
12514
12515 if (REG_P (XEXP (op0, 0)))
12516 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12517
12518 /* If op0 is a temporary because the original expressions was forced
12519 to memory, clear MEM_EXPR so that the original expression cannot
12520 be marked as addressable through MEM_EXPR of the temporary. */
12521 if (clear_mem_expr)
12522 set_mem_expr (op0, NULL_TREE);
12523
12524 MEM_VOLATILE_P (op0) |= volatilep;
12525
12526 if (reversep
12527 && modifier != EXPAND_MEMORY
12528 && modifier != EXPAND_WRITE)
12529 op0 = flip_storage_order (mode1, op0);
12530
12531 op0 = EXTEND_BITINT (op0);
12532
12533 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
12534 || modifier == EXPAND_CONST_ADDRESS
12535 || modifier == EXPAND_INITIALIZER)
12536 return op0;
12537
12538 if (target == 0)
12539 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
12540
12541 convert_move (target, op0, unsignedp);
12542 return target;
12543 }
12544
12545 case OBJ_TYPE_REF:
12546 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
12547
12548 case CALL_EXPR:
12549 /* All valid uses of __builtin_va_arg_pack () are removed during
12550 inlining. */
12551 if (CALL_EXPR_VA_ARG_PACK (exp))
12552 error ("invalid use of %<__builtin_va_arg_pack ()%>");
12553 {
12554 tree fndecl = get_callee_fndecl (exp), attr;
12555
12556 if (fndecl
12557 /* Don't diagnose the error attribute in thunks, those are
12558 artificially created. */
12559 && !CALL_FROM_THUNK_P (exp)
12560 && (attr = lookup_attribute ("error",
12561 DECL_ATTRIBUTES (fndecl))) != NULL)
12562 {
12563 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12564 error ("call to %qs declared with attribute error: %s",
12565 identifier_to_locale (ident),
12566 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12567 }
12568 if (fndecl
12569 /* Don't diagnose the warning attribute in thunks, those are
12570 artificially created. */
12571 && !CALL_FROM_THUNK_P (exp)
12572 && (attr = lookup_attribute ("warning",
12573 DECL_ATTRIBUTES (fndecl))) != NULL)
12574 {
12575 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
12576 warning_at (EXPR_LOCATION (exp),
12577 OPT_Wattribute_warning,
12578 "call to %qs declared with attribute warning: %s",
12579 identifier_to_locale (ident),
12580 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
12581 }
12582
12583 /* Check for a built-in function. */
12584 if (fndecl && fndecl_built_in_p (fndecl))
12585 {
12586 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
12587 return expand_builtin (exp, target, subtarget, tmode, ignore);
12588 }
12589 }
12590 temp = expand_call (exp, target, ignore);
12591 return EXTEND_BITINT (temp);
12592
12593 case VIEW_CONVERT_EXPR:
12594 op0 = NULL_RTX;
12595
12596 /* If we are converting to BLKmode, try to avoid an intermediate
12597 temporary by fetching an inner memory reference. */
12598 if (mode == BLKmode
12599 && poly_int_tree_p (TYPE_SIZE (type))
12600 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
12601 && handled_component_p (treeop0))
12602 {
12603 machine_mode mode1;
12604 poly_int64 bitsize, bitpos, bytepos;
12605 tree offset;
12606 int reversep, volatilep = 0;
12607 tree tem
12608 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
12609 &unsignedp, &reversep, &volatilep);
12610
12611 /* ??? We should work harder and deal with non-zero offsets. */
12612 if (!offset
12613 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
12614 && !reversep
12615 && known_size_p (bitsize)
12616 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
12617 {
12618 /* See the normal_inner_ref case for the rationale. */
12619 rtx orig_op0
12620 = expand_expr_real (tem,
12621 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
12622 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
12623 != INTEGER_CST)
12624 && modifier != EXPAND_STACK_PARM
12625 ? target : NULL_RTX),
12626 VOIDmode,
12627 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
12628 NULL, true);
12629
12630 if (MEM_P (orig_op0))
12631 {
12632 op0 = orig_op0;
12633
12634 /* Get a reference to just this component. */
12635 if (modifier == EXPAND_CONST_ADDRESS
12636 || modifier == EXPAND_SUM
12637 || modifier == EXPAND_INITIALIZER)
12638 op0 = adjust_address_nv (op0, mode, bytepos);
12639 else
12640 op0 = adjust_address (op0, mode, bytepos);
12641
12642 if (op0 == orig_op0)
12643 op0 = copy_rtx (op0);
12644
12645 set_mem_attributes (op0, treeop0, 0);
12646 if (REG_P (XEXP (op0, 0)))
12647 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
12648
12649 MEM_VOLATILE_P (op0) |= volatilep;
12650 }
12651 }
12652 }
12653
12654 if (!op0)
12655 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
12656 NULL, inner_reference_p || mode == BLKmode);
12657
12658 /* If the input and output modes are both the same, we are done. */
12659 if (mode == GET_MODE (op0))
12660 ;
12661 /* Similarly if the output mode is BLKmode and input is a MEM,
12662 adjust_address done below is all we need. */
12663 else if (mode == BLKmode && MEM_P (op0))
12664 ;
12665 /* If neither mode is BLKmode, and both modes are the same size
12666 then we can use gen_lowpart. */
12667 else if (mode != BLKmode
12668 && GET_MODE (op0) != BLKmode
12669 && known_eq (GET_MODE_PRECISION (mode),
12670 GET_MODE_PRECISION (GET_MODE (op0)))
12671 && !COMPLEX_MODE_P (GET_MODE (op0)))
12672 {
12673 if (GET_CODE (op0) == SUBREG)
12674 op0 = force_reg (GET_MODE (op0), op0);
12675 temp = gen_lowpart_common (mode, op0);
12676 if (temp)
12677 op0 = temp;
12678 else
12679 {
12680 if (!REG_P (op0) && !MEM_P (op0))
12681 op0 = force_reg (GET_MODE (op0), op0);
12682 op0 = gen_lowpart (mode, op0);
12683 }
12684 }
12685 /* If both types are integral, convert from one mode to the other. */
12686 else if (INTEGRAL_TYPE_P (type)
12687 && INTEGRAL_TYPE_P (TREE_TYPE (treeop0))
12688 && mode != BLKmode
12689 && GET_MODE (op0) != BLKmode)
12690 op0 = convert_modes (mode, GET_MODE (op0), op0,
12691 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
12692 /* If the output type is a bit-field type, do an extraction. */
12693 else if (reduce_bit_field
12694 && mode != BLKmode
12695 && (MEM_P (op0) || !COMPLEX_MODE_P (GET_MODE (op0))))
12696 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12697 TYPE_UNSIGNED (type), NULL_RTX,
12698 mode, mode, false, NULL);
12699 /* As a last resort, spill op0 to memory, and reload it in a
12700 different mode. */
12701 else if (!MEM_P (op0))
12702 {
12703 /* If the operand is not a MEM, force it into memory. Since we
12704 are going to be changing the mode of the MEM, don't call
12705 force_const_mem for constants because we don't allow pool
12706 constants to change mode. */
12707 tree inner_type = TREE_TYPE (treeop0);
12708
12709 gcc_assert (!TREE_ADDRESSABLE (exp));
12710
12711 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
12712 target
12713 = assign_stack_temp_for_type
12714 (TYPE_MODE (inner_type),
12715 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
12716
12717 emit_move_insn (target, op0);
12718 op0 = target;
12719
12720 if (reduce_bit_field && mode != BLKmode)
12721 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
12722 TYPE_UNSIGNED (type), NULL_RTX,
12723 mode, mode, false, NULL);
12724 }
12725
12726 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
12727 output type is such that the operand is known to be aligned, indicate
12728 that it is. Otherwise, we need only be concerned about alignment for
12729 non-BLKmode results. */
12730 if (MEM_P (op0))
12731 {
12732 enum insn_code icode;
12733
12734 if (modifier != EXPAND_WRITE
12735 && modifier != EXPAND_MEMORY
12736 && !inner_reference_p
12737 && mode != BLKmode
12738 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12739 {
12740 /* If the target does have special handling for unaligned
12741 loads of mode then use them. */
12742 if ((icode = optab_handler (movmisalign_optab, mode))
12743 != CODE_FOR_nothing)
12744 {
12745 rtx reg;
12746
12747 op0 = adjust_address (op0, mode, 0);
12748 /* We've already validated the memory, and we're creating a
12749 new pseudo destination. The predicates really can't
12750 fail. */
12751 reg = gen_reg_rtx (mode);
12752
12753 /* Nor can the insn generator. */
12754 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12755 emit_insn (insn);
12756 return reg;
12757 }
12758 else if (STRICT_ALIGNMENT)
12759 {
12760 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12761 poly_uint64 temp_size = mode_size;
12762 if (GET_MODE (op0) != BLKmode)
12763 temp_size = upper_bound (temp_size,
12764 GET_MODE_SIZE (GET_MODE (op0)));
12765 rtx new_rtx
12766 = assign_stack_temp_for_type (mode, temp_size, type);
12767 rtx new_with_op0_mode
12768 = adjust_address (new_rtx, GET_MODE (op0), 0);
12769
12770 gcc_assert (!TREE_ADDRESSABLE (exp));
12771
12772 if (GET_MODE (op0) == BLKmode)
12773 {
12774 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12775 emit_block_move (new_with_op0_mode, op0, size_rtx,
12776 (modifier == EXPAND_STACK_PARM
12777 ? BLOCK_OP_CALL_PARM
12778 : BLOCK_OP_NORMAL));
12779 }
12780 else
12781 emit_move_insn (new_with_op0_mode, op0);
12782
12783 op0 = new_rtx;
12784 }
12785 }
12786
12787 op0 = adjust_address (op0, mode, 0);
12788 }
12789
12790 return op0;
12791
12792 case MODIFY_EXPR:
12793 {
12794 tree lhs = treeop0;
12795 tree rhs = treeop1;
12796 gcc_assert (ignore);
12797
12798 /* Check for |= or &= of a bitfield of size one into another bitfield
12799 of size 1. In this case, (unless we need the result of the
12800 assignment) we can do this more efficiently with a
12801 test followed by an assignment, if necessary.
12802
12803 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12804 things change so we do, this code should be enhanced to
12805 support it. */
12806 if (TREE_CODE (lhs) == COMPONENT_REF
12807 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12808 || TREE_CODE (rhs) == BIT_AND_EXPR)
12809 && TREE_OPERAND (rhs, 0) == lhs
12810 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12811 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12812 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12813 {
12814 rtx_code_label *label = gen_label_rtx ();
12815 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12816 profile_probability prob = profile_probability::uninitialized ();
12817 if (value)
12818 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12819 else
12820 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12821 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12822 false);
12823 do_pending_stack_adjust ();
12824 emit_label (label);
12825 return const0_rtx;
12826 }
12827
12828 expand_assignment (lhs, rhs, false);
12829 return const0_rtx;
12830 }
12831
12832 case ADDR_EXPR:
12833 return expand_expr_addr_expr (exp, target, tmode, modifier);
12834
12835 case REALPART_EXPR:
12836 op0 = expand_normal (treeop0);
12837 return read_complex_part (op0, false);
12838
12839 case IMAGPART_EXPR:
12840 op0 = expand_normal (treeop0);
12841 return read_complex_part (op0, true);
12842
12843 case RETURN_EXPR:
12844 case LABEL_EXPR:
12845 case GOTO_EXPR:
12846 case SWITCH_EXPR:
12847 case ASM_EXPR:
12848 /* Expanded in cfgexpand.cc. */
12849 gcc_unreachable ();
12850
12851 case TRY_CATCH_EXPR:
12852 case CATCH_EXPR:
12853 case EH_FILTER_EXPR:
12854 case TRY_FINALLY_EXPR:
12855 case EH_ELSE_EXPR:
12856 /* Lowered by tree-eh.cc. */
12857 gcc_unreachable ();
12858
12859 case WITH_CLEANUP_EXPR:
12860 case CLEANUP_POINT_EXPR:
12861 case TARGET_EXPR:
12862 case CASE_LABEL_EXPR:
12863 case VA_ARG_EXPR:
12864 case BIND_EXPR:
12865 case INIT_EXPR:
12866 case CONJ_EXPR:
12867 case COMPOUND_EXPR:
12868 case PREINCREMENT_EXPR:
12869 case PREDECREMENT_EXPR:
12870 case POSTINCREMENT_EXPR:
12871 case POSTDECREMENT_EXPR:
12872 case LOOP_EXPR:
12873 case EXIT_EXPR:
12874 case COMPOUND_LITERAL_EXPR:
12875 /* Lowered by gimplify.cc. */
12876 gcc_unreachable ();
12877
12878 case FDESC_EXPR:
12879 /* Function descriptors are not valid except for as
12880 initialization constants, and should not be expanded. */
12881 gcc_unreachable ();
12882
12883 case WITH_SIZE_EXPR:
12884 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12885 have pulled out the size to use in whatever context it needed. */
12886 return expand_expr_real (treeop0, original_target, tmode,
12887 modifier, alt_rtl, inner_reference_p);
12888
12889 default:
12890 return expand_expr_real_2 (&ops, target, tmode, modifier);
12891 }
12892 }
12893 #undef EXTEND_BITINT
12894
12895 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12896 signedness of TYPE), possibly returning the result in TARGET.
12897 TYPE is known to be a partial integer type. */
12898 static rtx
12899 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12900 {
12901 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12902 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12903 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12904 && (!target || GET_MODE (target) == mode));
12905
12906 /* For constant values, reduce using wide_int_to_tree. */
12907 if (poly_int_rtx_p (exp))
12908 {
12909 auto value = wi::to_poly_wide (exp, mode);
12910 tree t = wide_int_to_tree (type, value);
12911 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12912 }
12913 else if (TYPE_UNSIGNED (type))
12914 {
12915 rtx mask = immed_wide_int_const
12916 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12917 return expand_and (mode, exp, mask, target);
12918 }
12919 else
12920 {
12921 int count = GET_MODE_PRECISION (mode) - prec;
12922 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12923 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12924 }
12925 }
12926
12927 /* Subroutine of above: returns true if OFFSET corresponds to an offset that
12928 when applied to the address of EXP produces an address known to be
12929 aligned more than BIGGEST_ALIGNMENT. */
12930
12931 static bool
12932 is_aligning_offset (const_tree offset, const_tree exp)
12933 {
12934 /* Strip off any conversions. */
12935 while (CONVERT_EXPR_P (offset))
12936 offset = TREE_OPERAND (offset, 0);
12937
12938 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12939 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12940 if (TREE_CODE (offset) != BIT_AND_EXPR
12941 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12942 || compare_tree_int (TREE_OPERAND (offset, 1),
12943 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12944 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12945 return false;
12946
12947 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12948 It must be NEGATE_EXPR. Then strip any more conversions. */
12949 offset = TREE_OPERAND (offset, 0);
12950 while (CONVERT_EXPR_P (offset))
12951 offset = TREE_OPERAND (offset, 0);
12952
12953 if (TREE_CODE (offset) != NEGATE_EXPR)
12954 return false;
12955
12956 offset = TREE_OPERAND (offset, 0);
12957 while (CONVERT_EXPR_P (offset))
12958 offset = TREE_OPERAND (offset, 0);
12959
12960 /* This must now be the address of EXP. */
12961 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12962 }
12963
12964 /* Return a STRING_CST corresponding to ARG's constant initializer either
12965 if it's a string constant, or, when VALREP is set, any other constant,
12966 or null otherwise.
12967 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12968 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12969 to the size of the byte string. If nonnull, set *DECL to the constant
12970 declaration ARG refers to. */
12971
12972 static tree
12973 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12974 bool valrep = false)
12975 {
12976 tree dummy = NULL_TREE;
12977 if (!mem_size)
12978 mem_size = &dummy;
12979
12980 /* Store the type of the original expression before conversions
12981 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12982 removed. */
12983 tree argtype = TREE_TYPE (arg);
12984
12985 tree array;
12986 STRIP_NOPS (arg);
12987
12988 /* Non-constant index into the character array in an ARRAY_REF
12989 expression or null. */
12990 tree varidx = NULL_TREE;
12991
12992 poly_int64 base_off = 0;
12993
12994 if (TREE_CODE (arg) == ADDR_EXPR)
12995 {
12996 arg = TREE_OPERAND (arg, 0);
12997 tree ref = arg;
12998 if (TREE_CODE (arg) == ARRAY_REF)
12999 {
13000 tree idx = TREE_OPERAND (arg, 1);
13001 if (TREE_CODE (idx) != INTEGER_CST)
13002 {
13003 /* From a pointer (but not array) argument extract the variable
13004 index to prevent get_addr_base_and_unit_offset() from failing
13005 due to it. Use it later to compute the non-constant offset
13006 into the string and return it to the caller. */
13007 varidx = idx;
13008 ref = TREE_OPERAND (arg, 0);
13009
13010 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
13011 return NULL_TREE;
13012
13013 if (!integer_zerop (array_ref_low_bound (arg)))
13014 return NULL_TREE;
13015
13016 if (!integer_onep (array_ref_element_size (arg)))
13017 return NULL_TREE;
13018 }
13019 }
13020 array = get_addr_base_and_unit_offset (ref, &base_off);
13021 if (!array
13022 || (TREE_CODE (array) != VAR_DECL
13023 && TREE_CODE (array) != CONST_DECL
13024 && TREE_CODE (array) != STRING_CST))
13025 return NULL_TREE;
13026 }
13027 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
13028 {
13029 tree arg0 = TREE_OPERAND (arg, 0);
13030 tree arg1 = TREE_OPERAND (arg, 1);
13031
13032 tree offset;
13033 tree str = string_constant (arg0, &offset, mem_size, decl);
13034 if (!str)
13035 {
13036 str = string_constant (arg1, &offset, mem_size, decl);
13037 arg1 = arg0;
13038 }
13039
13040 if (str)
13041 {
13042 /* Avoid pointers to arrays (see bug 86622). */
13043 if (POINTER_TYPE_P (TREE_TYPE (arg))
13044 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
13045 && !(decl && !*decl)
13046 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
13047 && tree_fits_uhwi_p (*mem_size)
13048 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
13049 return NULL_TREE;
13050
13051 tree type = TREE_TYPE (offset);
13052 arg1 = fold_convert (type, arg1);
13053 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
13054 return str;
13055 }
13056 return NULL_TREE;
13057 }
13058 else if (TREE_CODE (arg) == SSA_NAME)
13059 {
13060 gimple *stmt = SSA_NAME_DEF_STMT (arg);
13061 if (!is_gimple_assign (stmt))
13062 return NULL_TREE;
13063
13064 tree rhs1 = gimple_assign_rhs1 (stmt);
13065 tree_code code = gimple_assign_rhs_code (stmt);
13066 if (code == ADDR_EXPR)
13067 return string_constant (rhs1, ptr_offset, mem_size, decl);
13068 else if (code != POINTER_PLUS_EXPR)
13069 return NULL_TREE;
13070
13071 tree offset;
13072 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
13073 {
13074 /* Avoid pointers to arrays (see bug 86622). */
13075 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
13076 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
13077 && !(decl && !*decl)
13078 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
13079 && tree_fits_uhwi_p (*mem_size)
13080 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
13081 return NULL_TREE;
13082
13083 tree rhs2 = gimple_assign_rhs2 (stmt);
13084 tree type = TREE_TYPE (offset);
13085 rhs2 = fold_convert (type, rhs2);
13086 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
13087 return str;
13088 }
13089 return NULL_TREE;
13090 }
13091 else if (DECL_P (arg))
13092 array = arg;
13093 else
13094 return NULL_TREE;
13095
13096 tree offset = wide_int_to_tree (sizetype, base_off);
13097 if (varidx)
13098 {
13099 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
13100 return NULL_TREE;
13101
13102 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
13103 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
13104 if (TREE_CODE (chartype) != INTEGER_TYPE)
13105 return NULL;
13106
13107 offset = fold_convert (sizetype, varidx);
13108 }
13109
13110 if (TREE_CODE (array) == STRING_CST)
13111 {
13112 *ptr_offset = fold_convert (sizetype, offset);
13113 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
13114 if (decl)
13115 *decl = NULL_TREE;
13116 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
13117 >= TREE_STRING_LENGTH (array));
13118 return array;
13119 }
13120
13121 tree init = ctor_for_folding (array);
13122 if (!init || init == error_mark_node)
13123 return NULL_TREE;
13124
13125 if (valrep)
13126 {
13127 HOST_WIDE_INT cstoff;
13128 if (!base_off.is_constant (&cstoff))
13129 return NULL_TREE;
13130
13131 /* Check that the host and target are sane. */
13132 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
13133 return NULL_TREE;
13134
13135 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
13136 if (typesz <= 0 || (int) typesz != typesz)
13137 return NULL_TREE;
13138
13139 HOST_WIDE_INT size = typesz;
13140 if (VAR_P (array)
13141 && DECL_SIZE_UNIT (array)
13142 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
13143 {
13144 size = tree_to_shwi (DECL_SIZE_UNIT (array));
13145 gcc_checking_assert (size >= typesz);
13146 }
13147
13148 /* If value representation was requested convert the initializer
13149 for the whole array or object into a string of bytes forming
13150 its value representation and return it. */
13151 unsigned char *bytes = XNEWVEC (unsigned char, size);
13152 int r = native_encode_initializer (init, bytes, size);
13153 if (r < typesz)
13154 {
13155 XDELETEVEC (bytes);
13156 return NULL_TREE;
13157 }
13158
13159 if (r < size)
13160 memset (bytes + r, '\0', size - r);
13161
13162 const char *p = reinterpret_cast<const char *>(bytes);
13163 init = build_string_literal (size, p, char_type_node);
13164 init = TREE_OPERAND (init, 0);
13165 init = TREE_OPERAND (init, 0);
13166 XDELETE (bytes);
13167
13168 *mem_size = size_int (TREE_STRING_LENGTH (init));
13169 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
13170
13171 if (decl)
13172 *decl = array;
13173
13174 return init;
13175 }
13176
13177 if (TREE_CODE (init) == CONSTRUCTOR)
13178 {
13179 /* Convert the 64-bit constant offset to a wider type to avoid
13180 overflow and use it to obtain the initializer for the subobject
13181 it points into. */
13182 offset_int wioff;
13183 if (!base_off.is_constant (&wioff))
13184 return NULL_TREE;
13185
13186 wioff *= BITS_PER_UNIT;
13187 if (!wi::fits_uhwi_p (wioff))
13188 return NULL_TREE;
13189
13190 base_off = wioff.to_uhwi ();
13191 unsigned HOST_WIDE_INT fieldoff = 0;
13192 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
13193 &fieldoff);
13194 if (!init || init == error_mark_node)
13195 return NULL_TREE;
13196
13197 HOST_WIDE_INT cstoff;
13198 if (!base_off.is_constant (&cstoff))
13199 return NULL_TREE;
13200
13201 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
13202 tree off = build_int_cst (sizetype, cstoff);
13203 if (varidx)
13204 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
13205 else
13206 offset = off;
13207 }
13208
13209 *ptr_offset = offset;
13210
13211 tree inittype = TREE_TYPE (init);
13212
13213 if (TREE_CODE (init) == INTEGER_CST
13214 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
13215 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
13216 {
13217 /* Check that the host and target are sane. */
13218 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
13219 return NULL_TREE;
13220
13221 /* For a reference to (address of) a single constant character,
13222 store the native representation of the character in CHARBUF.
13223 If the reference is to an element of an array or a member
13224 of a struct, only consider narrow characters until ctors
13225 for wide character arrays are transformed to STRING_CSTs
13226 like those for narrow arrays. */
13227 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13228 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
13229 if (len > 0)
13230 {
13231 /* Construct a string literal with elements of INITTYPE and
13232 the representation above. Then strip
13233 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
13234 init = build_string_literal (len, (char *)charbuf, inittype);
13235 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
13236 }
13237 }
13238
13239 tree initsize = TYPE_SIZE_UNIT (inittype);
13240
13241 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
13242 {
13243 /* Fold an empty/zero constructor for an implicitly initialized
13244 object or subobject into the empty string. */
13245
13246 /* Determine the character type from that of the original
13247 expression. */
13248 tree chartype = argtype;
13249 if (POINTER_TYPE_P (chartype))
13250 chartype = TREE_TYPE (chartype);
13251 while (TREE_CODE (chartype) == ARRAY_TYPE)
13252 chartype = TREE_TYPE (chartype);
13253
13254 if (INTEGRAL_TYPE_P (chartype)
13255 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
13256 {
13257 /* Convert a char array to an empty STRING_CST having an array
13258 of the expected type and size. */
13259 if (!initsize)
13260 initsize = integer_zero_node;
13261
13262 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
13263 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
13264 return NULL_TREE;
13265
13266 init = build_string_literal (size, NULL, chartype, size);
13267 init = TREE_OPERAND (init, 0);
13268 init = TREE_OPERAND (init, 0);
13269
13270 *ptr_offset = integer_zero_node;
13271 }
13272 }
13273
13274 if (decl)
13275 *decl = array;
13276
13277 if (TREE_CODE (init) != STRING_CST)
13278 return NULL_TREE;
13279
13280 *mem_size = initsize;
13281
13282 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
13283
13284 return init;
13285 }
13286
13287 /* Return STRING_CST if an ARG corresponds to a string constant or zero
13288 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
13289 non-constant) offset in bytes within the string that ARG is accessing.
13290 If MEM_SIZE is non-zero the storage size of the memory is returned.
13291 If DECL is non-zero the constant declaration is returned if available. */
13292
13293 tree
13294 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
13295 {
13296 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
13297 }
13298
13299 /* Similar to string_constant, return a STRING_CST corresponding
13300 to the value representation of the first argument if it's
13301 a constant. */
13302
13303 tree
13304 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
13305 {
13306 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
13307 }
13308
13309 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
13310 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
13311 for C2 > 0 to x & C3 == C2
13312 for C2 < 0 to x & C3 == (C2 & C3). */
13313 enum tree_code
13314 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13315 {
13316 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13317 tree treeop0 = gimple_assign_rhs1 (stmt);
13318 tree treeop1 = gimple_assign_rhs2 (stmt);
13319 tree type = TREE_TYPE (*arg0);
13320 scalar_int_mode mode;
13321 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13322 return code;
13323 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13324 || TYPE_PRECISION (type) <= 1
13325 || TYPE_UNSIGNED (type)
13326 /* Signed x % c == 0 should have been optimized into unsigned modulo
13327 earlier. */
13328 || integer_zerop (*arg1)
13329 /* If c is known to be non-negative, modulo will be expanded as unsigned
13330 modulo. */
13331 || get_range_pos_neg (treeop0) == 1)
13332 return code;
13333
13334 /* x % c == d where d < 0 && d <= -c should be always false. */
13335 if (tree_int_cst_sgn (*arg1) == -1
13336 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
13337 return code;
13338
13339 int prec = TYPE_PRECISION (type);
13340 wide_int w = wi::to_wide (treeop1) - 1;
13341 w |= wi::shifted_mask (0, prec - 1, true, prec);
13342 tree c3 = wide_int_to_tree (type, w);
13343 tree c4 = *arg1;
13344 if (tree_int_cst_sgn (*arg1) == -1)
13345 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
13346
13347 rtx op0 = expand_normal (treeop0);
13348 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13349
13350 bool speed_p = optimize_insn_for_speed_p ();
13351
13352 do_pending_stack_adjust ();
13353
13354 location_t loc = gimple_location (stmt);
13355 struct separate_ops ops;
13356 ops.code = TRUNC_MOD_EXPR;
13357 ops.location = loc;
13358 ops.type = TREE_TYPE (treeop0);
13359 ops.op0 = treeop0;
13360 ops.op1 = treeop1;
13361 ops.op2 = NULL_TREE;
13362 start_sequence ();
13363 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13364 EXPAND_NORMAL);
13365 rtx_insn *moinsns = get_insns ();
13366 end_sequence ();
13367
13368 unsigned mocost = seq_cost (moinsns, speed_p);
13369 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13370 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13371
13372 ops.code = BIT_AND_EXPR;
13373 ops.location = loc;
13374 ops.type = TREE_TYPE (treeop0);
13375 ops.op0 = treeop0;
13376 ops.op1 = c3;
13377 ops.op2 = NULL_TREE;
13378 start_sequence ();
13379 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13380 EXPAND_NORMAL);
13381 rtx_insn *muinsns = get_insns ();
13382 end_sequence ();
13383
13384 unsigned mucost = seq_cost (muinsns, speed_p);
13385 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
13386 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
13387
13388 if (mocost <= mucost)
13389 {
13390 emit_insn (moinsns);
13391 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13392 return code;
13393 }
13394
13395 emit_insn (muinsns);
13396 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
13397 *arg1 = c4;
13398 return code;
13399 }
13400
13401 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
13402 If C1 is odd to:
13403 (X - C2) * C3 <= C4 (or >), where
13404 C3 is modular multiplicative inverse of C1 and 1<<prec and
13405 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
13406 if C2 > ((1<<prec) - 1) % C1).
13407 If C1 is even, S = ctz (C1) and C2 is 0, use
13408 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
13409 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
13410
13411 For signed (X % C1) == 0 if C1 is odd to (all operations in it
13412 unsigned):
13413 (X * C3) + C4 <= 2 * C4, where
13414 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
13415 C4 is ((1<<(prec - 1) - 1) / C1).
13416 If C1 is even, S = ctz(C1), use
13417 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
13418 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
13419 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
13420
13421 See the Hacker's Delight book, section 10-17. */
13422 enum tree_code
13423 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
13424 {
13425 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
13426 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
13427
13428 if (optimize < 2)
13429 return code;
13430
13431 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
13432 if (stmt == NULL)
13433 return code;
13434
13435 tree treeop0 = gimple_assign_rhs1 (stmt);
13436 tree treeop1 = gimple_assign_rhs2 (stmt);
13437 if (TREE_CODE (treeop0) != SSA_NAME
13438 || TREE_CODE (treeop1) != INTEGER_CST
13439 /* Don't optimize the undefined behavior case x % 0;
13440 x % 1 should have been optimized into zero, punt if
13441 it makes it here for whatever reason;
13442 x % -c should have been optimized into x % c. */
13443 || compare_tree_int (treeop1, 2) <= 0
13444 /* Likewise x % c == d where d >= c should be always false. */
13445 || tree_int_cst_le (treeop1, *arg1))
13446 return code;
13447
13448 /* Unsigned x % pow2 is handled right already, for signed
13449 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
13450 if (integer_pow2p (treeop1))
13451 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
13452
13453 tree type = TREE_TYPE (*arg0);
13454 scalar_int_mode mode;
13455 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
13456 return code;
13457 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
13458 || TYPE_PRECISION (type) <= 1)
13459 return code;
13460
13461 signop sgn = UNSIGNED;
13462 /* If both operands are known to have the sign bit clear, handle
13463 even the signed modulo case as unsigned. treeop1 is always
13464 positive >= 2, checked above. */
13465 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
13466 sgn = SIGNED;
13467
13468 if (!TYPE_UNSIGNED (type))
13469 {
13470 if (tree_int_cst_sgn (*arg1) == -1)
13471 return code;
13472 type = unsigned_type_for (type);
13473 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
13474 return code;
13475 }
13476
13477 int prec = TYPE_PRECISION (type);
13478 wide_int w = wi::to_wide (treeop1);
13479 int shift = wi::ctz (w);
13480 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
13481 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
13482 If C1 is odd, we can handle all cases by subtracting
13483 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
13484 e.g. by testing for overflow on the subtraction, punt on that for now
13485 though. */
13486 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
13487 {
13488 if (sgn == SIGNED)
13489 return code;
13490 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
13491 if (wi::gtu_p (wi::to_wide (*arg1), x))
13492 return code;
13493 }
13494
13495 imm_use_iterator imm_iter;
13496 use_operand_p use_p;
13497 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
13498 {
13499 gimple *use_stmt = USE_STMT (use_p);
13500 /* Punt if treeop0 is used in the same bb in a division
13501 or another modulo with the same divisor. We should expect
13502 the division and modulo combined together. */
13503 if (use_stmt == stmt
13504 || gimple_bb (use_stmt) != gimple_bb (stmt))
13505 continue;
13506 if (!is_gimple_assign (use_stmt)
13507 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
13508 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
13509 continue;
13510 if (gimple_assign_rhs1 (use_stmt) != treeop0
13511 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
13512 continue;
13513 return code;
13514 }
13515
13516 w = wi::lrshift (w, shift);
13517 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
13518 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
13519 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
13520 tree c3 = wide_int_to_tree (type, m);
13521 tree c5 = NULL_TREE;
13522 wide_int d, e;
13523 if (sgn == UNSIGNED)
13524 {
13525 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
13526 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
13527 otherwise use < or subtract one from C4. E.g. for
13528 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
13529 x % 3U == 1 already needs to be
13530 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
13531 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
13532 d -= 1;
13533 if (shift)
13534 d = wi::lrshift (d, shift);
13535 }
13536 else
13537 {
13538 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
13539 if (!shift)
13540 d = wi::lshift (e, 1);
13541 else
13542 {
13543 e = wi::bit_and (e, wi::mask (shift, true, prec));
13544 d = wi::lrshift (e, shift - 1);
13545 }
13546 c5 = wide_int_to_tree (type, e);
13547 }
13548 tree c4 = wide_int_to_tree (type, d);
13549
13550 rtx op0 = expand_normal (treeop0);
13551 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
13552
13553 bool speed_p = optimize_insn_for_speed_p ();
13554
13555 do_pending_stack_adjust ();
13556
13557 location_t loc = gimple_location (stmt);
13558 struct separate_ops ops;
13559 ops.code = TRUNC_MOD_EXPR;
13560 ops.location = loc;
13561 ops.type = TREE_TYPE (treeop0);
13562 ops.op0 = treeop0;
13563 ops.op1 = treeop1;
13564 ops.op2 = NULL_TREE;
13565 start_sequence ();
13566 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
13567 EXPAND_NORMAL);
13568 rtx_insn *moinsns = get_insns ();
13569 end_sequence ();
13570
13571 unsigned mocost = seq_cost (moinsns, speed_p);
13572 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
13573 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
13574
13575 tree t = fold_convert_loc (loc, type, treeop0);
13576 if (!integer_zerop (*arg1))
13577 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
13578 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
13579 if (sgn == SIGNED)
13580 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
13581 if (shift)
13582 {
13583 tree s = build_int_cst (NULL_TREE, shift);
13584 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
13585 }
13586
13587 start_sequence ();
13588 rtx mur = expand_normal (t);
13589 rtx_insn *muinsns = get_insns ();
13590 end_sequence ();
13591
13592 unsigned mucost = seq_cost (muinsns, speed_p);
13593 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
13594 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
13595
13596 if (mocost <= mucost)
13597 {
13598 emit_insn (moinsns);
13599 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
13600 return code;
13601 }
13602
13603 emit_insn (muinsns);
13604 *arg0 = make_tree (type, mur);
13605 *arg1 = c4;
13606 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
13607 }
13608
13609 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
13610
13611 void
13612 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
13613 {
13614 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
13615 || code == LT_EXPR || code == LE_EXPR);
13616 gcc_checking_assert (integer_zerop (*arg1));
13617
13618 if (!optimize)
13619 return;
13620
13621 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
13622 if (stmt == NULL)
13623 return;
13624
13625 tree treeop0 = gimple_assign_rhs1 (stmt);
13626 tree treeop1 = gimple_assign_rhs2 (stmt);
13627 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
13628 return;
13629
13630 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
13631 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
13632 "assuming signed overflow does not occur when "
13633 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
13634 op_symbol_code (code), op_symbol_code (code));
13635
13636 *arg0 = treeop0;
13637 *arg1 = treeop1;
13638 }
13639
13640
13641 /* Expand CODE with arguments INNER & (1<<BITNUM) and 0 that represents
13642 a single bit equality/inequality test, returns where the result is located. */
13643
13644 static rtx
13645 expand_single_bit_test (location_t loc, enum tree_code code,
13646 tree inner, int bitnum,
13647 tree result_type, rtx target,
13648 machine_mode mode)
13649 {
13650 gcc_assert (code == NE_EXPR || code == EQ_EXPR);
13651
13652 tree type = TREE_TYPE (inner);
13653 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
13654 int ops_unsigned;
13655 tree signed_type, unsigned_type, intermediate_type;
13656 gimple *inner_def;
13657
13658 /* First, see if we can fold the single bit test into a sign-bit
13659 test. */
13660 if (bitnum == TYPE_PRECISION (type) - 1
13661 && type_has_mode_precision_p (type))
13662 {
13663 tree stype = signed_type_for (type);
13664 tree tmp = fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13665 result_type,
13666 fold_convert_loc (loc, stype, inner),
13667 build_int_cst (stype, 0));
13668 return expand_expr (tmp, target, VOIDmode, EXPAND_NORMAL);
13669 }
13670
13671 /* Otherwise we have (A & C) != 0 where C is a single bit,
13672 convert that into ((A >> C2) & 1). Where C2 = log2(C).
13673 Similarly for (A & C) == 0. */
13674
13675 /* If INNER is a right shift of a constant and it plus BITNUM does
13676 not overflow, adjust BITNUM and INNER. */
13677 if ((inner_def = get_def_for_expr (inner, RSHIFT_EXPR))
13678 && TREE_CODE (gimple_assign_rhs2 (inner_def)) == INTEGER_CST
13679 && bitnum < TYPE_PRECISION (type)
13680 && wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (inner_def)),
13681 TYPE_PRECISION (type) - bitnum))
13682 {
13683 bitnum += tree_to_uhwi (gimple_assign_rhs2 (inner_def));
13684 inner = gimple_assign_rhs1 (inner_def);
13685 }
13686
13687 /* If we are going to be able to omit the AND below, we must do our
13688 operations as unsigned. If we must use the AND, we have a choice.
13689 Normally unsigned is faster, but for some machines signed is. */
13690 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
13691 && !flag_syntax_only) ? 0 : 1;
13692
13693 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
13694 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
13695 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
13696 inner = fold_convert_loc (loc, intermediate_type, inner);
13697
13698 rtx inner0 = expand_expr (inner, NULL_RTX, VOIDmode, EXPAND_NORMAL);
13699
13700 if (CONST_SCALAR_INT_P (inner0))
13701 {
13702 wide_int t = rtx_mode_t (inner0, operand_mode);
13703 bool setp = (wi::lrshift (t, bitnum) & 1) != 0;
13704 return (setp ^ (code == EQ_EXPR)) ? const1_rtx : const0_rtx;
13705 }
13706 int bitpos = bitnum;
13707
13708 if (BYTES_BIG_ENDIAN)
13709 bitpos = GET_MODE_BITSIZE (operand_mode) - 1 - bitpos;
13710
13711 inner0 = extract_bit_field (inner0, 1, bitpos, 1, target,
13712 operand_mode, mode, 0, NULL);
13713
13714 if (code == EQ_EXPR)
13715 inner0 = expand_binop (GET_MODE (inner0), xor_optab, inner0, const1_rtx,
13716 NULL_RTX, 1, OPTAB_LIB_WIDEN);
13717 if (GET_MODE (inner0) != mode)
13718 {
13719 rtx t = gen_reg_rtx (mode);
13720 convert_move (t, inner0, 0);
13721 return t;
13722 }
13723 return inner0;
13724 }
13725
13726 /* Generate code to calculate OPS, and exploded expression
13727 using a store-flag instruction and return an rtx for the result.
13728 OPS reflects a comparison.
13729
13730 If TARGET is nonzero, store the result there if convenient.
13731
13732 Return zero if there is no suitable set-flag instruction
13733 available on this machine.
13734
13735 Once expand_expr has been called on the arguments of the comparison,
13736 we are committed to doing the store flag, since it is not safe to
13737 re-evaluate the expression. We emit the store-flag insn by calling
13738 emit_store_flag, but only expand the arguments if we have a reason
13739 to believe that emit_store_flag will be successful. If we think that
13740 it will, but it isn't, we have to simulate the store-flag with a
13741 set/jump/set sequence. */
13742
13743 static rtx
13744 do_store_flag (const_sepops ops, rtx target, machine_mode mode)
13745 {
13746 enum rtx_code code;
13747 tree arg0, arg1, type;
13748 machine_mode operand_mode;
13749 int unsignedp;
13750 rtx op0, op1;
13751 rtx subtarget = target;
13752 location_t loc = ops->location;
13753 unsigned HOST_WIDE_INT nunits;
13754
13755 arg0 = ops->op0;
13756 arg1 = ops->op1;
13757
13758 /* Don't crash if the comparison was erroneous. */
13759 if (arg0 == error_mark_node || arg1 == error_mark_node)
13760 return const0_rtx;
13761
13762 type = TREE_TYPE (arg0);
13763 operand_mode = TYPE_MODE (type);
13764 unsignedp = TYPE_UNSIGNED (type);
13765
13766 /* We won't bother with BLKmode store-flag operations because it would mean
13767 passing a lot of information to emit_store_flag. */
13768 if (operand_mode == BLKmode)
13769 return 0;
13770
13771 /* We won't bother with store-flag operations involving function pointers
13772 when function pointers must be canonicalized before comparisons. */
13773 if (targetm.have_canonicalize_funcptr_for_compare ()
13774 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
13775 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
13776 || (POINTER_TYPE_P (TREE_TYPE (arg1))
13777 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
13778 return 0;
13779
13780 STRIP_NOPS (arg0);
13781 STRIP_NOPS (arg1);
13782
13783 /* For vector typed comparisons emit code to generate the desired
13784 all-ones or all-zeros mask. */
13785 if (VECTOR_TYPE_P (ops->type))
13786 {
13787 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
13788 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
13789 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
13790 return expand_vec_cmp_expr (ops->type, ifexp, target);
13791 else
13792 gcc_unreachable ();
13793 }
13794
13795 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
13796 into (x - C2) * C3 < C4. */
13797 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
13798 && TREE_CODE (arg0) == SSA_NAME
13799 && TREE_CODE (arg1) == INTEGER_CST)
13800 {
13801 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
13802 &arg0, &arg1);
13803 if (new_code != ops->code)
13804 {
13805 struct separate_ops nops = *ops;
13806 nops.code = new_code;
13807 nops.op0 = arg0;
13808 nops.op1 = arg1;
13809 nops.type = TREE_TYPE (arg0);
13810 return do_store_flag (&nops, target, mode);
13811 }
13812 }
13813
13814 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
13815 if (!unsignedp
13816 && (ops->code == LT_EXPR || ops->code == LE_EXPR
13817 || ops->code == GT_EXPR || ops->code == GE_EXPR)
13818 && integer_zerop (arg1)
13819 && TREE_CODE (arg0) == SSA_NAME)
13820 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
13821
13822 /* Get the rtx comparison code to use. We know that EXP is a comparison
13823 operation of some type. Some comparisons against 1 and -1 can be
13824 converted to comparisons with zero. Do so here so that the tests
13825 below will be aware that we have a comparison with zero. These
13826 tests will not catch constants in the first operand, but constants
13827 are rarely passed as the first operand. */
13828
13829 switch (ops->code)
13830 {
13831 case EQ_EXPR:
13832 code = EQ;
13833 break;
13834 case NE_EXPR:
13835 code = NE;
13836 break;
13837 case LT_EXPR:
13838 if (integer_onep (arg1))
13839 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13840 else
13841 code = unsignedp ? LTU : LT;
13842 break;
13843 case LE_EXPR:
13844 if (! unsignedp && integer_all_onesp (arg1))
13845 arg1 = integer_zero_node, code = LT;
13846 else
13847 code = unsignedp ? LEU : LE;
13848 break;
13849 case GT_EXPR:
13850 if (! unsignedp && integer_all_onesp (arg1))
13851 arg1 = integer_zero_node, code = GE;
13852 else
13853 code = unsignedp ? GTU : GT;
13854 break;
13855 case GE_EXPR:
13856 if (integer_onep (arg1))
13857 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13858 else
13859 code = unsignedp ? GEU : GE;
13860 break;
13861
13862 case UNORDERED_EXPR:
13863 code = UNORDERED;
13864 break;
13865 case ORDERED_EXPR:
13866 code = ORDERED;
13867 break;
13868 case UNLT_EXPR:
13869 code = UNLT;
13870 break;
13871 case UNLE_EXPR:
13872 code = UNLE;
13873 break;
13874 case UNGT_EXPR:
13875 code = UNGT;
13876 break;
13877 case UNGE_EXPR:
13878 code = UNGE;
13879 break;
13880 case UNEQ_EXPR:
13881 code = UNEQ;
13882 break;
13883 case LTGT_EXPR:
13884 code = LTGT;
13885 break;
13886
13887 default:
13888 gcc_unreachable ();
13889 }
13890
13891 /* Put a constant second. */
13892 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13893 || TREE_CODE (arg0) == FIXED_CST)
13894 {
13895 std::swap (arg0, arg1);
13896 code = swap_condition (code);
13897 }
13898
13899 /* If this is an equality or inequality test of a single bit, we can
13900 do this by shifting the bit being tested to the low-order bit and
13901 masking the result with the constant 1. If the condition was EQ,
13902 we xor it with 1. This does not require an scc insn and is faster
13903 than an scc insn even if we have it. */
13904
13905 if ((code == NE || code == EQ)
13906 && (integer_zerop (arg1)
13907 || integer_pow2p (arg1))
13908 /* vector types are not handled here. */
13909 && TREE_CODE (TREE_TYPE (arg1)) != VECTOR_TYPE
13910 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13911 {
13912 tree narg0 = arg0;
13913 wide_int nz = tree_nonzero_bits (narg0);
13914 gimple *srcstmt = get_def_for_expr (narg0, BIT_AND_EXPR);
13915 /* If the defining statement was (x & POW2), then use that instead of
13916 the non-zero bits. */
13917 if (srcstmt && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13918 {
13919 nz = wi::to_wide (gimple_assign_rhs2 (srcstmt));
13920 narg0 = gimple_assign_rhs1 (srcstmt);
13921 }
13922
13923 if (wi::popcount (nz) == 1
13924 && (integer_zerop (arg1)
13925 || wi::to_wide (arg1) == nz))
13926 {
13927 int bitnum = wi::exact_log2 (nz);
13928 enum tree_code tcode = EQ_EXPR;
13929 if ((code == NE) ^ !integer_zerop (arg1))
13930 tcode = NE_EXPR;
13931
13932 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13933 return expand_single_bit_test (loc, tcode,
13934 narg0,
13935 bitnum, type, target, mode);
13936 }
13937 }
13938
13939
13940 if (! get_subtarget (target)
13941 || GET_MODE (subtarget) != operand_mode)
13942 subtarget = 0;
13943
13944 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13945
13946 /* For boolean vectors with less than mode precision
13947 make sure to fill padding with consistent values. */
13948 if (VECTOR_BOOLEAN_TYPE_P (type)
13949 && SCALAR_INT_MODE_P (operand_mode)
13950 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nunits)
13951 && maybe_ne (GET_MODE_PRECISION (operand_mode), nunits))
13952 {
13953 gcc_assert (code == EQ || code == NE);
13954 op0 = expand_binop (mode, and_optab, op0,
13955 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
13956 NULL_RTX, true, OPTAB_WIDEN);
13957 op1 = expand_binop (mode, and_optab, op1,
13958 GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
13959 NULL_RTX, true, OPTAB_WIDEN);
13960 }
13961
13962 if (target == 0)
13963 target = gen_reg_rtx (mode);
13964
13965 /* Try a cstore if possible. */
13966 return emit_store_flag_force (target, code, op0, op1,
13967 operand_mode, unsignedp,
13968 (TYPE_PRECISION (ops->type) == 1
13969 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13970 }
13971
13972 /* Attempt to generate a casesi instruction. Returns true if successful,
13973 false otherwise (i.e. if there is no casesi instruction).
13974
13975 DEFAULT_PROBABILITY is the probability of jumping to the default
13976 label. */
13977 bool
13978 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13979 rtx table_label, rtx default_label, rtx fallback_label,
13980 profile_probability default_probability)
13981 {
13982 class expand_operand ops[5];
13983 scalar_int_mode index_mode = SImode;
13984 rtx op1, op2, index;
13985
13986 if (! targetm.have_casesi ())
13987 return false;
13988
13989 /* The index must be some form of integer. Convert it to SImode. */
13990 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13991 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13992 {
13993 rtx rangertx = expand_normal (range);
13994
13995 /* We must handle the endpoints in the original mode. */
13996 index_expr = build2 (MINUS_EXPR, index_type,
13997 index_expr, minval);
13998 minval = integer_zero_node;
13999 index = expand_normal (index_expr);
14000 if (default_label)
14001 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
14002 omode, 1, default_label,
14003 default_probability);
14004 /* Now we can safely truncate. */
14005 index = convert_to_mode (index_mode, index, 0);
14006 }
14007 else
14008 {
14009 if (omode != index_mode)
14010 {
14011 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
14012 index_expr = fold_convert (index_type, index_expr);
14013 }
14014
14015 index = expand_normal (index_expr);
14016 }
14017
14018 do_pending_stack_adjust ();
14019
14020 op1 = expand_normal (minval);
14021 op2 = expand_normal (range);
14022
14023 create_input_operand (&ops[0], index, index_mode);
14024 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
14025 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
14026 create_fixed_operand (&ops[3], table_label);
14027 create_fixed_operand (&ops[4], (default_label
14028 ? default_label
14029 : fallback_label));
14030 expand_jump_insn (targetm.code_for_casesi, 5, ops);
14031 return true;
14032 }
14033
14034 /* Attempt to generate a tablejump instruction; same concept. */
14035 /* Subroutine of the next function.
14036
14037 INDEX is the value being switched on, with the lowest value
14038 in the table already subtracted.
14039 MODE is its expected mode (needed if INDEX is constant).
14040 RANGE is the length of the jump table.
14041 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
14042
14043 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
14044 index value is out of range.
14045 DEFAULT_PROBABILITY is the probability of jumping to
14046 the default label. */
14047
14048 static void
14049 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
14050 rtx default_label, profile_probability default_probability)
14051 {
14052 rtx temp, vector;
14053
14054 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
14055 cfun->cfg->max_jumptable_ents = INTVAL (range);
14056
14057 /* Do an unsigned comparison (in the proper mode) between the index
14058 expression and the value which represents the length of the range.
14059 Since we just finished subtracting the lower bound of the range
14060 from the index expression, this comparison allows us to simultaneously
14061 check that the original index expression value is both greater than
14062 or equal to the minimum value of the range and less than or equal to
14063 the maximum value of the range. */
14064
14065 if (default_label)
14066 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
14067 default_label, default_probability);
14068
14069 /* If index is in range, it must fit in Pmode.
14070 Convert to Pmode so we can index with it. */
14071 if (mode != Pmode)
14072 {
14073 unsigned int width;
14074
14075 /* We know the value of INDEX is between 0 and RANGE. If we have a
14076 sign-extended subreg, and RANGE does not have the sign bit set, then
14077 we have a value that is valid for both sign and zero extension. In
14078 this case, we get better code if we sign extend. */
14079 if (GET_CODE (index) == SUBREG
14080 && SUBREG_PROMOTED_VAR_P (index)
14081 && SUBREG_PROMOTED_SIGNED_P (index)
14082 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
14083 <= HOST_BITS_PER_WIDE_INT)
14084 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
14085 index = convert_to_mode (Pmode, index, 0);
14086 else
14087 index = convert_to_mode (Pmode, index, 1);
14088 }
14089
14090 /* Don't let a MEM slip through, because then INDEX that comes
14091 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
14092 and break_out_memory_refs will go to work on it and mess it up. */
14093 #ifdef PIC_CASE_VECTOR_ADDRESS
14094 if (flag_pic && !REG_P (index))
14095 index = copy_to_mode_reg (Pmode, index);
14096 #endif
14097
14098 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
14099 GET_MODE_SIZE, because this indicates how large insns are. The other
14100 uses should all be Pmode, because they are addresses. This code
14101 could fail if addresses and insns are not the same size. */
14102 index = simplify_gen_binary (MULT, Pmode, index,
14103 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
14104 Pmode));
14105 index = simplify_gen_binary (PLUS, Pmode, index,
14106 gen_rtx_LABEL_REF (Pmode, table_label));
14107
14108 #ifdef PIC_CASE_VECTOR_ADDRESS
14109 if (flag_pic)
14110 index = PIC_CASE_VECTOR_ADDRESS (index);
14111 else
14112 #endif
14113 index = memory_address (CASE_VECTOR_MODE, index);
14114 temp = gen_reg_rtx (CASE_VECTOR_MODE);
14115 vector = gen_const_mem (CASE_VECTOR_MODE, index);
14116 convert_move (temp, vector, 0);
14117
14118 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
14119
14120 /* If we are generating PIC code or if the table is PC-relative, the
14121 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
14122 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
14123 emit_barrier ();
14124 }
14125
14126 bool
14127 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
14128 rtx table_label, rtx default_label,
14129 profile_probability default_probability)
14130 {
14131 rtx index;
14132
14133 if (! targetm.have_tablejump ())
14134 return false;
14135
14136 index_expr = fold_build2 (MINUS_EXPR, index_type,
14137 fold_convert (index_type, index_expr),
14138 fold_convert (index_type, minval));
14139 index = expand_normal (index_expr);
14140 do_pending_stack_adjust ();
14141
14142 do_tablejump (index, TYPE_MODE (index_type),
14143 convert_modes (TYPE_MODE (index_type),
14144 TYPE_MODE (TREE_TYPE (range)),
14145 expand_normal (range),
14146 TYPE_UNSIGNED (TREE_TYPE (range))),
14147 table_label, default_label, default_probability);
14148 return true;
14149 }
14150
14151 /* Return a CONST_VECTOR rtx representing vector mask for
14152 a VECTOR_CST of booleans. */
14153 static rtx
14154 const_vector_mask_from_tree (tree exp)
14155 {
14156 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
14157 machine_mode inner = GET_MODE_INNER (mode);
14158
14159 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
14160 VECTOR_CST_NELTS_PER_PATTERN (exp));
14161 unsigned int count = builder.encoded_nelts ();
14162 for (unsigned int i = 0; i < count; ++i)
14163 {
14164 tree elt = VECTOR_CST_ELT (exp, i);
14165 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
14166 if (integer_zerop (elt))
14167 builder.quick_push (CONST0_RTX (inner));
14168 else if (integer_onep (elt)
14169 || integer_minus_onep (elt))
14170 builder.quick_push (CONSTM1_RTX (inner));
14171 else
14172 gcc_unreachable ();
14173 }
14174 return builder.build ();
14175 }
14176
14177 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
14178 static rtx
14179 const_vector_from_tree (tree exp)
14180 {
14181 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
14182
14183 if (initializer_zerop (exp))
14184 return CONST0_RTX (mode);
14185
14186 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
14187 return const_vector_mask_from_tree (exp);
14188
14189 machine_mode inner = GET_MODE_INNER (mode);
14190
14191 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
14192 VECTOR_CST_NELTS_PER_PATTERN (exp));
14193 unsigned int count = builder.encoded_nelts ();
14194 for (unsigned int i = 0; i < count; ++i)
14195 {
14196 tree elt = VECTOR_CST_ELT (exp, i);
14197 if (TREE_CODE (elt) == REAL_CST)
14198 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
14199 inner));
14200 else if (TREE_CODE (elt) == FIXED_CST)
14201 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
14202 inner));
14203 else
14204 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
14205 inner));
14206 }
14207 return builder.build ();
14208 }
14209
14210 /* Build a decl for a personality function given a language prefix. */
14211
14212 tree
14213 build_personality_function (const char *lang)
14214 {
14215 const char *unwind_and_version;
14216 tree decl, type;
14217 char *name;
14218
14219 switch (targetm_common.except_unwind_info (&global_options))
14220 {
14221 case UI_NONE:
14222 return NULL;
14223 case UI_SJLJ:
14224 unwind_and_version = "_sj0";
14225 break;
14226 case UI_DWARF2:
14227 case UI_TARGET:
14228 unwind_and_version = "_v0";
14229 break;
14230 case UI_SEH:
14231 unwind_and_version = "_seh0";
14232 break;
14233 default:
14234 gcc_unreachable ();
14235 }
14236
14237 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
14238
14239 type = build_function_type_list (unsigned_type_node,
14240 integer_type_node, integer_type_node,
14241 long_long_unsigned_type_node,
14242 ptr_type_node, ptr_type_node, NULL_TREE);
14243 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
14244 get_identifier (name), type);
14245 DECL_ARTIFICIAL (decl) = 1;
14246 DECL_EXTERNAL (decl) = 1;
14247 TREE_PUBLIC (decl) = 1;
14248
14249 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
14250 are the flags assigned by targetm.encode_section_info. */
14251 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
14252
14253 return decl;
14254 }
14255
14256 /* Extracts the personality function of DECL and returns the corresponding
14257 libfunc. */
14258
14259 rtx
14260 get_personality_function (tree decl)
14261 {
14262 tree personality = DECL_FUNCTION_PERSONALITY (decl);
14263 enum eh_personality_kind pk;
14264
14265 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
14266 if (pk == eh_personality_none)
14267 return NULL;
14268
14269 if (!personality
14270 && pk == eh_personality_any)
14271 personality = lang_hooks.eh_personality ();
14272
14273 if (pk == eh_personality_lang)
14274 gcc_assert (personality != NULL_TREE);
14275
14276 return XEXP (DECL_RTL (personality), 0);
14277 }
14278
14279 /* Returns a tree for the size of EXP in bytes. */
14280
14281 static tree
14282 tree_expr_size (const_tree exp)
14283 {
14284 if (DECL_P (exp)
14285 && DECL_SIZE_UNIT (exp) != 0)
14286 return DECL_SIZE_UNIT (exp);
14287 else
14288 return size_in_bytes (TREE_TYPE (exp));
14289 }
14290
14291 /* Return an rtx for the size in bytes of the value of EXP. */
14292
14293 rtx
14294 expr_size (tree exp)
14295 {
14296 tree size;
14297
14298 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
14299 size = TREE_OPERAND (exp, 1);
14300 else
14301 {
14302 size = tree_expr_size (exp);
14303 gcc_assert (size);
14304 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
14305 }
14306
14307 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
14308 }
14309
14310 /* Return a wide integer for the size in bytes of the value of EXP, or -1
14311 if the size can vary or is larger than an integer. */
14312
14313 HOST_WIDE_INT
14314 int_expr_size (const_tree exp)
14315 {
14316 tree size;
14317
14318 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
14319 size = TREE_OPERAND (exp, 1);
14320 else
14321 {
14322 size = tree_expr_size (exp);
14323 gcc_assert (size);
14324 }
14325
14326 if (size == 0 || !tree_fits_shwi_p (size))
14327 return -1;
14328
14329 return tree_to_shwi (size);
14330 }
14331
14332 /* Return the quotient of polynomial long division of x^2N by POLYNOMIAL
14333 in GF (2^N).
14334 Author: Richard Sandiford <[email protected]> */
14335
14336 unsigned HOST_WIDE_INT
14337 gf2n_poly_long_div_quotient (unsigned HOST_WIDE_INT polynomial,
14338 unsigned short n)
14339 {
14340 /* The result has degree N, so needs N + 1 bits. */
14341 gcc_assert (n < 64);
14342
14343 /* Perform a division step for the x^2N coefficient. At this point the
14344 quotient and remainder have N implicit trailing zeros. */
14345 unsigned HOST_WIDE_INT quotient = 1;
14346 unsigned HOST_WIDE_INT remainder = polynomial;
14347
14348 /* Process the coefficients for x^(2N-1) down to x^N, with each step
14349 reducing the number of implicit trailing zeros by one. */
14350 for (unsigned int i = 0; i < n; ++i)
14351 {
14352 bool coeff = remainder & (HOST_WIDE_INT_1U << (n - 1));
14353 quotient = (quotient << 1) | coeff;
14354 remainder = (remainder << 1) ^ (coeff ? polynomial : 0);
14355 }
14356 return quotient;
14357 }
14358
14359 /* Calculate CRC for the initial CRC and given POLYNOMIAL.
14360 CRC_BITS is CRC size. */
14361
14362 static unsigned HOST_WIDE_INT
14363 calculate_crc (unsigned HOST_WIDE_INT crc,
14364 unsigned HOST_WIDE_INT polynomial,
14365 unsigned short crc_bits)
14366 {
14367 unsigned HOST_WIDE_INT msb = HOST_WIDE_INT_1U << (crc_bits - 1);
14368 crc = crc << (crc_bits - 8);
14369 for (short i = 8; i > 0; --i)
14370 {
14371 if (crc & msb)
14372 crc = (crc << 1) ^ polynomial;
14373 else
14374 crc <<= 1;
14375 }
14376 /* Zero out bits in crc beyond the specified number of crc_bits. */
14377 if (crc_bits < sizeof (crc) * CHAR_BIT)
14378 crc &= (HOST_WIDE_INT_1U << crc_bits) - 1;
14379 return crc;
14380 }
14381
14382 /* Assemble CRC table with 256 elements for the given POLYNOM and CRC_BITS.
14383 POLYNOM is the polynomial used to calculate the CRC table's elements.
14384 CRC_BITS is the size of CRC, may be 8, 16, ... . */
14385
14386 static rtx
14387 assemble_crc_table (unsigned HOST_WIDE_INT polynom, unsigned short crc_bits)
14388 {
14389 unsigned table_el_n = 0x100;
14390 tree ar = build_array_type (make_unsigned_type (crc_bits),
14391 build_index_type (size_int (table_el_n - 1)));
14392
14393 /* Initialize the table. */
14394 vec<tree, va_gc> *initial_values;
14395 vec_alloc (initial_values, table_el_n);
14396 for (size_t i = 0; i < table_el_n; ++i)
14397 {
14398 unsigned HOST_WIDE_INT crc = calculate_crc (i, polynom, crc_bits);
14399 tree element = build_int_cstu (make_unsigned_type (crc_bits), crc);
14400 vec_safe_push (initial_values, element);
14401 }
14402 tree ctor = build_constructor_from_vec (ar, initial_values);
14403 rtx mem = output_constant_def (ctor, 1);
14404 gcc_assert (MEM_P (mem));
14405 if (dump_file && (dump_flags & TDF_DETAILS))
14406 {
14407 fprintf (dump_file,
14408 ";; emitting crc table crc_%u_polynomial_"
14409 HOST_WIDE_INT_PRINT_HEX " ",
14410 crc_bits, polynom);
14411 print_rtl_single (dump_file, XEXP (mem, 0));
14412 fprintf (dump_file, "\n");
14413 }
14414
14415 return XEXP (mem, 0);
14416 }
14417
14418 /* Generate CRC lookup table by calculating CRC for all possible
14419 8-bit data values. The table is stored with a specific name in the read-only
14420 static data section.
14421 POLYNOM is the polynomial used to calculate the CRC table's elements.
14422 CRC_BITS is the size of CRC, may be 8, 16, ... . */
14423
14424 static rtx
14425 generate_crc_table (unsigned HOST_WIDE_INT polynom, unsigned short crc_bits)
14426 {
14427 gcc_assert (crc_bits <= 64);
14428
14429 return assemble_crc_table (polynom, crc_bits);
14430 }
14431
14432 /* Generate table-based CRC code for the given CRC, INPUT_DATA and the
14433 POLYNOMIAL (without leading 1).
14434
14435 First, using POLYNOMIAL's value generates CRC table of 256 elements,
14436 then generates the assembly for the following code,
14437 where crc_bit_size and data_bit_size may be 8, 16, 32, 64, depending on CRC:
14438
14439 for (int i = 0; i < data_bit_size / 8; i++)
14440 crc = (crc << 8) ^ crc_table[(crc >> (crc_bit_size - 8))
14441 ^ (data >> (data_bit_size - (i + 1) * 8)
14442 & 0xFF))];
14443
14444 So to take values from the table, we need 8-bit data.
14445 If input data size is not 8, then first we extract upper 8 bits,
14446 then the other 8 bits, and so on. */
14447
14448 static void
14449 calculate_table_based_CRC (rtx *crc, const rtx &input_data,
14450 const rtx &polynomial,
14451 machine_mode data_mode)
14452 {
14453 machine_mode mode = GET_MODE (*crc);
14454 unsigned short crc_bit_size = GET_MODE_BITSIZE (mode).to_constant ();
14455 unsigned short data_size = GET_MODE_SIZE (data_mode).to_constant ();
14456 rtx tab = generate_crc_table (UINTVAL (polynomial), crc_bit_size);
14457
14458 for (unsigned short i = 0; i < data_size; i++)
14459 {
14460 /* crc >> (crc_bit_size - 8). */
14461 *crc = force_reg (mode, *crc);
14462 rtx op1 = expand_shift (RSHIFT_EXPR, mode, *crc, crc_bit_size - 8,
14463 NULL_RTX, 1);
14464
14465 /* data >> (8 * (GET_MODE_SIZE (data_mode).to_constant () - i - 1)). */
14466 unsigned range_8 = 8 * (data_size - i - 1);
14467 /* CRC's mode is always at least as wide as INPUT_DATA. Convert
14468 INPUT_DATA into CRC's mode. */
14469 rtx data = gen_reg_rtx (mode);
14470 convert_move (data, input_data, 1);
14471 data = expand_shift (RSHIFT_EXPR, mode, data, range_8, NULL_RTX, 1);
14472
14473 /* data >> (8 * (GET_MODE_SIZE (mode)
14474 .to_constant () - i - 1)) & 0xFF. */
14475 rtx data_final = expand_and (mode, data,
14476 gen_int_mode (255, mode), NULL_RTX);
14477
14478 /* (crc >> (crc_bit_size - 8)) ^ data_8bit. */
14479 rtx in = expand_binop (mode, xor_optab, op1, data_final,
14480 NULL_RTX, 1, OPTAB_WIDEN);
14481
14482 /* ((crc >> (crc_bit_size - 8)) ^ data_8bit) & 0xFF. */
14483 rtx index = expand_and (mode, in, gen_int_mode (255, mode),
14484 NULL_RTX);
14485 int log_crc_size = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
14486 index = expand_shift (LSHIFT_EXPR, mode, index,
14487 log_crc_size, NULL_RTX, 0);
14488
14489 rtx addr = gen_reg_rtx (Pmode);
14490 convert_move (addr, index, 1);
14491 addr = expand_binop (Pmode, add_optab, addr, tab, NULL_RTX,
14492 0, OPTAB_DIRECT);
14493
14494 /* crc_table[(crc >> (crc_bit_size - 8)) ^ data_8bit] */
14495 rtx tab_el = validize_mem (gen_rtx_MEM (mode, addr));
14496
14497 /* (crc << 8) if CRC is larger than 8, otherwise crc = 0. */
14498 rtx high = NULL_RTX;
14499 if (crc_bit_size != 8)
14500 high = expand_shift (LSHIFT_EXPR, mode, *crc, 8, NULL_RTX, 0);
14501 else
14502 high = gen_int_mode (0, mode);
14503
14504 /* crc = (crc << 8)
14505 ^ crc_table[(crc >> (crc_bit_size - 8)) ^ data_8bit]; */
14506 *crc = expand_binop (mode, xor_optab, tab_el, high, NULL_RTX, 1,
14507 OPTAB_WIDEN);
14508 }
14509 }
14510
14511 /* Generate table-based CRC code for the given CRC, INPUT_DATA and the
14512 POLYNOMIAL (without leading 1).
14513
14514 CRC is OP1, data is OP2 and the polynomial is OP3.
14515 This must generate a CRC table and an assembly for the following code,
14516 where crc_bit_size and data_bit_size may be 8, 16, 32, 64:
14517 uint_crc_bit_size_t
14518 crc_crc_bit_size (uint_crc_bit_size_t crc_init,
14519 uint_data_bit_size_t data, size_t size)
14520 {
14521 uint_crc_bit_size_t crc = crc_init;
14522 for (int i = 0; i < data_bit_size / 8; i++)
14523 crc = (crc << 8) ^ crc_table[(crc >> (crc_bit_size - 8))
14524 ^ (data >> (data_bit_size - (i + 1) * 8)
14525 & 0xFF))];
14526 return crc;
14527 } */
14528
14529 void
14530 expand_crc_table_based (rtx op0, rtx op1, rtx op2, rtx op3,
14531 machine_mode data_mode)
14532 {
14533 gcc_assert (!CONST_INT_P (op0));
14534 gcc_assert (CONST_INT_P (op3));
14535 machine_mode crc_mode = GET_MODE (op0);
14536 rtx crc = gen_reg_rtx (crc_mode);
14537 convert_move (crc, op1, 0);
14538 calculate_table_based_CRC (&crc, op2, op3, data_mode);
14539 convert_move (op0, crc, 0);
14540 }
14541
14542 /* Generate the common operation for reflecting values:
14543 *OP = (*OP & AND1_VALUE) << SHIFT_VAL | (*OP & AND2_VALUE) >> SHIFT_VAL; */
14544
14545 void
14546 gen_common_operation_to_reflect (rtx *op,
14547 unsigned HOST_WIDE_INT and1_value,
14548 unsigned HOST_WIDE_INT and2_value,
14549 unsigned shift_val)
14550 {
14551 rtx op1 = expand_and (GET_MODE (*op), *op,
14552 gen_int_mode (and1_value, GET_MODE (*op)), NULL_RTX);
14553 op1 = expand_shift (LSHIFT_EXPR, GET_MODE (*op), op1, shift_val, op1, 0);
14554 rtx op2 = expand_and (GET_MODE (*op), *op,
14555 gen_int_mode (and2_value, GET_MODE (*op)), NULL_RTX);
14556 op2 = expand_shift (RSHIFT_EXPR, GET_MODE (*op), op2, shift_val, op2, 1);
14557 *op = expand_binop (GET_MODE (*op), ior_optab, op1,
14558 op2, *op, 0, OPTAB_LIB_WIDEN);
14559 }
14560
14561 /* Reflect 64-bit value for the 64-bit target. */
14562
14563 void
14564 reflect_64_bit_value (rtx *op)
14565 {
14566 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x00000000FFFFFFFF),
14567 HOST_WIDE_INT_C (0xFFFFFFFF00000000), 32);
14568 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0000FFFF0000FFFF),
14569 HOST_WIDE_INT_C (0xFFFF0000FFFF0000), 16);
14570 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x00FF00FF00FF00FF),
14571 HOST_WIDE_INT_C (0xFF00FF00FF00FF00), 8);
14572 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0F0F0F0F0F0F0F0F),
14573 HOST_WIDE_INT_C (0xF0F0F0F0F0F0F0F0), 4);
14574 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x3333333333333333),
14575 HOST_WIDE_INT_C (0xCCCCCCCCCCCCCCCC), 2);
14576 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x5555555555555555),
14577 HOST_WIDE_INT_C (0xAAAAAAAAAAAAAAAA), 1);
14578 }
14579
14580 /* Reflect 32-bit value for the 32-bit target. */
14581
14582 void
14583 reflect_32_bit_value (rtx *op)
14584 {
14585 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0000FFFF),
14586 HOST_WIDE_INT_C (0xFFFF0000), 16);
14587 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x00FF00FF),
14588 HOST_WIDE_INT_C (0xFF00FF00), 8);
14589 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0F0F0F0F),
14590 HOST_WIDE_INT_C (0xF0F0F0F0), 4);
14591 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x33333333),
14592 HOST_WIDE_INT_C (0xCCCCCCCC), 2);
14593 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x55555555),
14594 HOST_WIDE_INT_C (0xAAAAAAAA), 1);
14595 }
14596
14597 /* Reflect 16-bit value for the 16-bit target. */
14598
14599 void
14600 reflect_16_bit_value (rtx *op)
14601 {
14602 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x00FF),
14603 HOST_WIDE_INT_C (0xFF00), 8);
14604 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0F0F),
14605 HOST_WIDE_INT_C (0xF0F0), 4);
14606 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x3333),
14607 HOST_WIDE_INT_C (0xCCCC), 2);
14608 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x5555),
14609 HOST_WIDE_INT_C (0xAAAA), 1);
14610 }
14611
14612 /* Reflect 8-bit value for the 8-bit target. */
14613
14614 void
14615 reflect_8_bit_value (rtx *op)
14616 {
14617 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x0F),
14618 HOST_WIDE_INT_C (0xF0), 4);
14619 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x33),
14620 HOST_WIDE_INT_C (0xCC), 2);
14621 gen_common_operation_to_reflect (op, HOST_WIDE_INT_C (0x55),
14622 HOST_WIDE_INT_C (0xAA), 1);
14623 }
14624
14625 /* Generate instruction sequence which reflects the value of the OP
14626 using shift, and, or operations. OP's mode may be less than word_mode. */
14627
14628 void
14629 generate_reflecting_code_standard (rtx *op)
14630 {
14631 gcc_assert (GET_MODE_BITSIZE (GET_MODE (*op)).to_constant () >= 8
14632 && GET_MODE_BITSIZE (GET_MODE (*op)).to_constant () <= 64);
14633
14634 if (GET_MODE_BITSIZE (GET_MODE (*op)).to_constant () == 64)
14635 reflect_64_bit_value (op);
14636 else if (GET_MODE_BITSIZE (GET_MODE (*op)).to_constant () == 32)
14637 reflect_32_bit_value (op);
14638 else if (GET_MODE_BITSIZE (GET_MODE (*op)).to_constant () == 16)
14639 reflect_16_bit_value (op);
14640 else
14641 reflect_8_bit_value (op);
14642 }
14643
14644 /* Generate table-based reversed CRC code for the given CRC, INPUT_DATA and
14645 the POLYNOMIAL (without leading 1).
14646
14647 CRC is OP1, data is OP2 and the polynomial is OP3.
14648 This must generate CRC table and assembly for the following code,
14649 where crc_bit_size and data_bit_size may be 8, 16, 32, 64:
14650 uint_crc_bit_size_t
14651 crc_crc_bit_size (uint_crc_bit_size_t crc_init,
14652 uint_data_bit_size_t data, size_t size)
14653 {
14654 reflect (crc_init)
14655 uint_crc_bit_size_t crc = crc_init;
14656 reflect (data);
14657 for (int i = 0; i < data_bit_size / 8; i++)
14658 crc = (crc << 8) ^ crc_table[(crc >> (crc_bit_size - 8))
14659 ^ (data >> (data_bit_size - (i + 1) * 8) & 0xFF))];
14660 reflect (crc);
14661 return crc;
14662 } */
14663
14664 void
14665 expand_reversed_crc_table_based (rtx op0, rtx op1, rtx op2, rtx op3,
14666 machine_mode data_mode,
14667 void (*gen_reflecting_code) (rtx *op))
14668 {
14669 gcc_assert (!CONST_INT_P (op0));
14670 gcc_assert (CONST_INT_P (op3));
14671 machine_mode crc_mode = GET_MODE (op0);
14672
14673 rtx crc = gen_reg_rtx (crc_mode);
14674 convert_move (crc, op1, 0);
14675 gen_reflecting_code (&crc);
14676
14677 rtx data = gen_reg_rtx (data_mode);
14678 convert_move (data, op2, 0);
14679 gen_reflecting_code (&data);
14680
14681 calculate_table_based_CRC (&crc, data, op3, data_mode);
14682
14683 gen_reflecting_code (&crc);
14684 convert_move (op0, crc, 0);
14685 }