]> gcc.gnu.org Git - gcc.git/blob - gcc/expr.cc
docs: fix: WARNING: Parsing of expression failed. Using fallback parser.
[gcc.git] / gcc / expr.cc
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2022 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
69
70 /* If this is nonzero, we do not bother generating VOLATILE
71 around volatile memory references, and we are willing to
72 output indirect addresses. If cse is to follow, we reject
73 indirect addresses so a useful potential cse is generated;
74 if it is used only once, instruction combination will produce
75 the same indirect address eventually. */
76 int cse_not_expected;
77
78 static bool block_move_libcall_safe_for_call_parm (void);
79 static bool emit_block_move_via_pattern (rtx, rtx, rtx, unsigned, unsigned,
80 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
81 unsigned HOST_WIDE_INT,
82 unsigned HOST_WIDE_INT, bool);
83 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
84 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
85 static rtx_insn *compress_float_constant (rtx, rtx);
86 static rtx get_subtarget (rtx);
87 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
88 machine_mode, tree, alias_set_type, bool, bool);
89
90 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
91
92 static int is_aligning_offset (const_tree, const_tree);
93 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
94 static rtx do_store_flag (sepops, rtx, machine_mode);
95 #ifdef PUSH_ROUNDING
96 static void emit_single_push_insn (machine_mode, rtx, tree);
97 #endif
98 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
99 profile_probability);
100 static rtx const_vector_from_tree (tree);
101 static tree tree_expr_size (const_tree);
102 static void convert_mode_scalar (rtx, rtx, int);
103
104 \f
105 /* This is run to set up which modes can be used
106 directly in memory and to initialize the block move optab. It is run
107 at the beginning of compilation and when the target is reinitialized. */
108
109 void
110 init_expr_target (void)
111 {
112 rtx pat;
113 int num_clobbers;
114 rtx mem, mem1;
115 rtx reg;
116
117 /* Try indexing by frame ptr and try by stack ptr.
118 It is known that on the Convex the stack ptr isn't a valid index.
119 With luck, one or the other is valid on any machine. */
120 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
121 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
122
123 /* A scratch register we can modify in-place below to avoid
124 useless RTL allocations. */
125 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
126
127 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
128 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
129 PATTERN (insn) = pat;
130
131 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
132 mode = (machine_mode) ((int) mode + 1))
133 {
134 int regno;
135
136 direct_load[(int) mode] = direct_store[(int) mode] = 0;
137 PUT_MODE (mem, mode);
138 PUT_MODE (mem1, mode);
139
140 /* See if there is some register that can be used in this mode and
141 directly loaded or stored from memory. */
142
143 if (mode != VOIDmode && mode != BLKmode)
144 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
145 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
146 regno++)
147 {
148 if (!targetm.hard_regno_mode_ok (regno, mode))
149 continue;
150
151 set_mode_and_regno (reg, mode, regno);
152
153 SET_SRC (pat) = mem;
154 SET_DEST (pat) = reg;
155 if (recog (pat, insn, &num_clobbers) >= 0)
156 direct_load[(int) mode] = 1;
157
158 SET_SRC (pat) = mem1;
159 SET_DEST (pat) = reg;
160 if (recog (pat, insn, &num_clobbers) >= 0)
161 direct_load[(int) mode] = 1;
162
163 SET_SRC (pat) = reg;
164 SET_DEST (pat) = mem;
165 if (recog (pat, insn, &num_clobbers) >= 0)
166 direct_store[(int) mode] = 1;
167
168 SET_SRC (pat) = reg;
169 SET_DEST (pat) = mem1;
170 if (recog (pat, insn, &num_clobbers) >= 0)
171 direct_store[(int) mode] = 1;
172 }
173 }
174
175 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
176
177 opt_scalar_float_mode mode_iter;
178 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
179 {
180 scalar_float_mode mode = mode_iter.require ();
181 scalar_float_mode srcmode;
182 FOR_EACH_MODE_UNTIL (srcmode, mode)
183 {
184 enum insn_code ic;
185
186 ic = can_extend_p (mode, srcmode, 0);
187 if (ic == CODE_FOR_nothing)
188 continue;
189
190 PUT_MODE (mem, srcmode);
191
192 if (insn_operand_matches (ic, 1, mem))
193 float_extend_from_mem[mode][srcmode] = true;
194 }
195 }
196 }
197
198 /* This is run at the start of compiling a function. */
199
200 void
201 init_expr (void)
202 {
203 memset (&crtl->expr, 0, sizeof (crtl->expr));
204 }
205 \f
206 /* Copy data from FROM to TO, where the machine modes are not the same.
207 Both modes may be integer, or both may be floating, or both may be
208 fixed-point.
209 UNSIGNEDP should be nonzero if FROM is an unsigned type.
210 This causes zero-extension instead of sign-extension. */
211
212 void
213 convert_move (rtx to, rtx from, int unsignedp)
214 {
215 machine_mode to_mode = GET_MODE (to);
216 machine_mode from_mode = GET_MODE (from);
217
218 gcc_assert (to_mode != BLKmode);
219 gcc_assert (from_mode != BLKmode);
220
221 /* If the source and destination are already the same, then there's
222 nothing to do. */
223 if (to == from)
224 return;
225
226 /* If FROM is a SUBREG that indicates that we have already done at least
227 the required extension, strip it. We don't handle such SUBREGs as
228 TO here. */
229
230 scalar_int_mode to_int_mode;
231 if (GET_CODE (from) == SUBREG
232 && SUBREG_PROMOTED_VAR_P (from)
233 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
234 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
235 >= GET_MODE_PRECISION (to_int_mode))
236 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
237 {
238 scalar_int_mode int_orig_mode;
239 scalar_int_mode int_inner_mode;
240 machine_mode orig_mode = GET_MODE (from);
241
242 from = gen_lowpart (to_int_mode, SUBREG_REG (from));
243 from_mode = to_int_mode;
244
245 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
246 the original mode, but narrower than the inner mode. */
247 if (GET_CODE (from) == SUBREG
248 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
249 && GET_MODE_PRECISION (to_int_mode)
250 > GET_MODE_PRECISION (int_orig_mode)
251 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (from)),
252 &int_inner_mode)
253 && GET_MODE_PRECISION (int_inner_mode)
254 > GET_MODE_PRECISION (to_int_mode))
255 {
256 SUBREG_PROMOTED_VAR_P (from) = 1;
257 SUBREG_PROMOTED_SET (from, unsignedp);
258 }
259 }
260
261 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
262
263 if (to_mode == from_mode
264 || (from_mode == VOIDmode && CONSTANT_P (from)))
265 {
266 emit_move_insn (to, from);
267 return;
268 }
269
270 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
271 {
272 if (GET_MODE_UNIT_PRECISION (to_mode)
273 > GET_MODE_UNIT_PRECISION (from_mode))
274 {
275 optab op = unsignedp ? zext_optab : sext_optab;
276 insn_code icode = convert_optab_handler (op, to_mode, from_mode);
277 if (icode != CODE_FOR_nothing)
278 {
279 emit_unop_insn (icode, to, from,
280 unsignedp ? ZERO_EXTEND : SIGN_EXTEND);
281 return;
282 }
283 }
284
285 if (GET_MODE_UNIT_PRECISION (to_mode)
286 < GET_MODE_UNIT_PRECISION (from_mode))
287 {
288 insn_code icode = convert_optab_handler (trunc_optab,
289 to_mode, from_mode);
290 if (icode != CODE_FOR_nothing)
291 {
292 emit_unop_insn (icode, to, from, TRUNCATE);
293 return;
294 }
295 }
296
297 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
298 GET_MODE_BITSIZE (to_mode)));
299
300 if (VECTOR_MODE_P (to_mode))
301 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
302 else
303 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
304
305 emit_move_insn (to, from);
306 return;
307 }
308
309 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
310 {
311 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
312 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
313 return;
314 }
315
316 convert_mode_scalar (to, from, unsignedp);
317 }
318
319 /* Like convert_move, but deals only with scalar modes. */
320
321 static void
322 convert_mode_scalar (rtx to, rtx from, int unsignedp)
323 {
324 /* Both modes should be scalar types. */
325 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
326 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
327 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
328 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
329 enum insn_code code;
330 rtx libcall;
331
332 gcc_assert (to_real == from_real);
333
334 /* rtx code for making an equivalent value. */
335 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
336 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
337
338 if (to_real)
339 {
340 rtx value;
341 rtx_insn *insns;
342 convert_optab tab;
343
344 gcc_assert ((GET_MODE_PRECISION (from_mode)
345 != GET_MODE_PRECISION (to_mode))
346 || (DECIMAL_FLOAT_MODE_P (from_mode)
347 != DECIMAL_FLOAT_MODE_P (to_mode))
348 || (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
349 && REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
350 || (REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
351 && REAL_MODE_FORMAT (from_mode) == &ieee_half_format));
352
353 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
354 /* Conversion between decimal float and binary float, same size. */
355 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
356 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
357 tab = sext_optab;
358 else
359 tab = trunc_optab;
360
361 /* Try converting directly if the insn is supported. */
362
363 code = convert_optab_handler (tab, to_mode, from_mode);
364 if (code != CODE_FOR_nothing)
365 {
366 emit_unop_insn (code, to, from,
367 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
368 return;
369 }
370
371 #ifdef HAVE_SFmode
372 if (REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
373 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
374 {
375 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (SFmode))
376 {
377 /* To cut down on libgcc size, implement
378 BFmode -> {DF,XF,TF}mode conversions by
379 BFmode -> SFmode -> {DF,XF,TF}mode conversions. */
380 rtx temp = gen_reg_rtx (SFmode);
381 convert_mode_scalar (temp, from, unsignedp);
382 convert_mode_scalar (to, temp, unsignedp);
383 return;
384 }
385 if (REAL_MODE_FORMAT (to_mode) == &ieee_half_format)
386 {
387 /* Similarly, implement BFmode -> HFmode as
388 BFmode -> SFmode -> HFmode conversion where SFmode
389 has superset of BFmode values. We don't need
390 to handle sNaNs by raising exception and turning
391 into into qNaN though, as that can be done in the
392 SFmode -> HFmode conversion too. */
393 rtx temp = gen_reg_rtx (SFmode);
394 int save_flag_finite_math_only = flag_finite_math_only;
395 flag_finite_math_only = true;
396 convert_mode_scalar (temp, from, unsignedp);
397 flag_finite_math_only = save_flag_finite_math_only;
398 convert_mode_scalar (to, temp, unsignedp);
399 return;
400 }
401 if (to_mode == SFmode
402 && !HONOR_NANS (from_mode)
403 && !HONOR_NANS (to_mode)
404 && optimize_insn_for_speed_p ())
405 {
406 /* If we don't expect sNaNs, for BFmode -> SFmode we can just
407 shift the bits up. */
408 machine_mode fromi_mode, toi_mode;
409 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
410 0).exists (&fromi_mode)
411 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
412 0).exists (&toi_mode))
413 {
414 start_sequence ();
415 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
416 rtx tof = NULL_RTX;
417 if (fromi)
418 {
419 rtx toi;
420 if (GET_MODE (fromi) == VOIDmode)
421 toi = simplify_unary_operation (ZERO_EXTEND, toi_mode,
422 fromi, fromi_mode);
423 else
424 {
425 toi = gen_reg_rtx (toi_mode);
426 convert_mode_scalar (toi, fromi, 1);
427 }
428 toi
429 = maybe_expand_shift (LSHIFT_EXPR, toi_mode, toi,
430 GET_MODE_PRECISION (to_mode)
431 - GET_MODE_PRECISION (from_mode),
432 NULL_RTX, 1);
433 if (toi)
434 {
435 tof = lowpart_subreg (to_mode, toi, toi_mode);
436 if (tof)
437 emit_move_insn (to, tof);
438 }
439 }
440 insns = get_insns ();
441 end_sequence ();
442 if (tof)
443 {
444 emit_insn (insns);
445 return;
446 }
447 }
448 }
449 }
450 if (REAL_MODE_FORMAT (from_mode) == &ieee_single_format
451 && REAL_MODE_FORMAT (to_mode) == &arm_bfloat_half_format
452 && !HONOR_NANS (from_mode)
453 && !HONOR_NANS (to_mode)
454 && !flag_rounding_math
455 && optimize_insn_for_speed_p ())
456 {
457 /* If we don't expect qNaNs nor sNaNs and can assume rounding
458 to nearest, we can expand the conversion inline as
459 (fromi + 0x7fff + ((fromi >> 16) & 1)) >> 16. */
460 machine_mode fromi_mode, toi_mode;
461 if (int_mode_for_size (GET_MODE_BITSIZE (from_mode),
462 0).exists (&fromi_mode)
463 && int_mode_for_size (GET_MODE_BITSIZE (to_mode),
464 0).exists (&toi_mode))
465 {
466 start_sequence ();
467 rtx fromi = lowpart_subreg (fromi_mode, from, from_mode);
468 rtx tof = NULL_RTX;
469 do
470 {
471 if (!fromi)
472 break;
473 int shift = (GET_MODE_PRECISION (from_mode)
474 - GET_MODE_PRECISION (to_mode));
475 rtx temp1
476 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode, fromi,
477 shift, NULL_RTX, 1);
478 if (!temp1)
479 break;
480 rtx temp2
481 = expand_binop (fromi_mode, and_optab, temp1, const1_rtx,
482 NULL_RTX, 1, OPTAB_DIRECT);
483 if (!temp2)
484 break;
485 rtx temp3
486 = expand_binop (fromi_mode, add_optab, fromi,
487 gen_int_mode ((HOST_WIDE_INT_1U
488 << (shift - 1)) - 1,
489 fromi_mode), NULL_RTX,
490 1, OPTAB_DIRECT);
491 if (!temp3)
492 break;
493 rtx temp4
494 = expand_binop (fromi_mode, add_optab, temp3, temp2,
495 NULL_RTX, 1, OPTAB_DIRECT);
496 if (!temp4)
497 break;
498 rtx temp5 = maybe_expand_shift (RSHIFT_EXPR, fromi_mode,
499 temp4, shift, NULL_RTX, 1);
500 if (!temp5)
501 break;
502 rtx temp6 = lowpart_subreg (toi_mode, temp5, fromi_mode);
503 if (!temp6)
504 break;
505 tof = lowpart_subreg (to_mode, force_reg (toi_mode, temp6),
506 toi_mode);
507 if (tof)
508 emit_move_insn (to, tof);
509 }
510 while (0);
511 insns = get_insns ();
512 end_sequence ();
513 if (tof)
514 {
515 emit_insn (insns);
516 return;
517 }
518 }
519 }
520 #endif
521
522 /* Otherwise use a libcall. */
523 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
524
525 /* Is this conversion implemented yet? */
526 gcc_assert (libcall);
527
528 start_sequence ();
529 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
530 from, from_mode);
531 insns = get_insns ();
532 end_sequence ();
533 emit_libcall_block (insns, to, value,
534 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
535 from)
536 : gen_rtx_FLOAT_EXTEND (to_mode, from));
537 return;
538 }
539
540 /* Handle pointer conversion. */ /* SPEE 900220. */
541 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
542 {
543 convert_optab ctab;
544
545 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
546 ctab = trunc_optab;
547 else if (unsignedp)
548 ctab = zext_optab;
549 else
550 ctab = sext_optab;
551
552 if (convert_optab_handler (ctab, to_mode, from_mode)
553 != CODE_FOR_nothing)
554 {
555 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
556 to, from, UNKNOWN);
557 return;
558 }
559 }
560
561 /* Targets are expected to provide conversion insns between PxImode and
562 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
563 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
564 {
565 scalar_int_mode full_mode
566 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
567
568 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
569 != CODE_FOR_nothing);
570
571 if (full_mode != from_mode)
572 from = convert_to_mode (full_mode, from, unsignedp);
573 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
574 to, from, UNKNOWN);
575 return;
576 }
577 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
578 {
579 rtx new_from;
580 scalar_int_mode full_mode
581 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
582 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
583 enum insn_code icode;
584
585 icode = convert_optab_handler (ctab, full_mode, from_mode);
586 gcc_assert (icode != CODE_FOR_nothing);
587
588 if (to_mode == full_mode)
589 {
590 emit_unop_insn (icode, to, from, UNKNOWN);
591 return;
592 }
593
594 new_from = gen_reg_rtx (full_mode);
595 emit_unop_insn (icode, new_from, from, UNKNOWN);
596
597 /* else proceed to integer conversions below. */
598 from_mode = full_mode;
599 from = new_from;
600 }
601
602 /* Make sure both are fixed-point modes or both are not. */
603 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
604 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
605 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
606 {
607 /* If we widen from_mode to to_mode and they are in the same class,
608 we won't saturate the result.
609 Otherwise, always saturate the result to play safe. */
610 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
611 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
612 expand_fixed_convert (to, from, 0, 0);
613 else
614 expand_fixed_convert (to, from, 0, 1);
615 return;
616 }
617
618 /* Now both modes are integers. */
619
620 /* Handle expanding beyond a word. */
621 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
622 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
623 {
624 rtx_insn *insns;
625 rtx lowpart;
626 rtx fill_value;
627 rtx lowfrom;
628 int i;
629 scalar_mode lowpart_mode;
630 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
631
632 /* Try converting directly if the insn is supported. */
633 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
634 != CODE_FOR_nothing)
635 {
636 /* If FROM is a SUBREG, put it into a register. Do this
637 so that we always generate the same set of insns for
638 better cse'ing; if an intermediate assignment occurred,
639 we won't be doing the operation directly on the SUBREG. */
640 if (optimize > 0 && GET_CODE (from) == SUBREG)
641 from = force_reg (from_mode, from);
642 emit_unop_insn (code, to, from, equiv_code);
643 return;
644 }
645 /* Next, try converting via full word. */
646 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
647 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
648 != CODE_FOR_nothing))
649 {
650 rtx word_to = gen_reg_rtx (word_mode);
651 if (REG_P (to))
652 {
653 if (reg_overlap_mentioned_p (to, from))
654 from = force_reg (from_mode, from);
655 emit_clobber (to);
656 }
657 convert_move (word_to, from, unsignedp);
658 emit_unop_insn (code, to, word_to, equiv_code);
659 return;
660 }
661
662 /* No special multiword conversion insn; do it by hand. */
663 start_sequence ();
664
665 /* Since we will turn this into a no conflict block, we must ensure
666 the source does not overlap the target so force it into an isolated
667 register when maybe so. Likewise for any MEM input, since the
668 conversion sequence might require several references to it and we
669 must ensure we're getting the same value every time. */
670
671 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
672 from = force_reg (from_mode, from);
673
674 /* Get a copy of FROM widened to a word, if necessary. */
675 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
676 lowpart_mode = word_mode;
677 else
678 lowpart_mode = from_mode;
679
680 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
681
682 lowpart = gen_lowpart (lowpart_mode, to);
683 emit_move_insn (lowpart, lowfrom);
684
685 /* Compute the value to put in each remaining word. */
686 if (unsignedp)
687 fill_value = const0_rtx;
688 else
689 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
690 LT, lowfrom, const0_rtx,
691 lowpart_mode, 0, -1);
692
693 /* Fill the remaining words. */
694 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
695 {
696 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
697 rtx subword = operand_subword (to, index, 1, to_mode);
698
699 gcc_assert (subword);
700
701 if (fill_value != subword)
702 emit_move_insn (subword, fill_value);
703 }
704
705 insns = get_insns ();
706 end_sequence ();
707
708 emit_insn (insns);
709 return;
710 }
711
712 /* Truncating multi-word to a word or less. */
713 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
714 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
715 {
716 if (!((MEM_P (from)
717 && ! MEM_VOLATILE_P (from)
718 && direct_load[(int) to_mode]
719 && ! mode_dependent_address_p (XEXP (from, 0),
720 MEM_ADDR_SPACE (from)))
721 || REG_P (from)
722 || GET_CODE (from) == SUBREG))
723 from = force_reg (from_mode, from);
724 convert_move (to, gen_lowpart (word_mode, from), 0);
725 return;
726 }
727
728 /* Now follow all the conversions between integers
729 no more than a word long. */
730
731 /* For truncation, usually we can just refer to FROM in a narrower mode. */
732 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
733 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
734 {
735 if (!((MEM_P (from)
736 && ! MEM_VOLATILE_P (from)
737 && direct_load[(int) to_mode]
738 && ! mode_dependent_address_p (XEXP (from, 0),
739 MEM_ADDR_SPACE (from)))
740 || REG_P (from)
741 || GET_CODE (from) == SUBREG))
742 from = force_reg (from_mode, from);
743 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
744 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
745 from = copy_to_reg (from);
746 emit_move_insn (to, gen_lowpart (to_mode, from));
747 return;
748 }
749
750 /* Handle extension. */
751 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
752 {
753 /* Convert directly if that works. */
754 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
755 != CODE_FOR_nothing)
756 {
757 emit_unop_insn (code, to, from, equiv_code);
758 return;
759 }
760 else
761 {
762 rtx tmp;
763 int shift_amount;
764
765 /* Search for a mode to convert via. */
766 opt_scalar_mode intermediate_iter;
767 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
768 {
769 scalar_mode intermediate = intermediate_iter.require ();
770 if (((can_extend_p (to_mode, intermediate, unsignedp)
771 != CODE_FOR_nothing)
772 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
773 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
774 intermediate)))
775 && (can_extend_p (intermediate, from_mode, unsignedp)
776 != CODE_FOR_nothing))
777 {
778 convert_move (to, convert_to_mode (intermediate, from,
779 unsignedp), unsignedp);
780 return;
781 }
782 }
783
784 /* No suitable intermediate mode.
785 Generate what we need with shifts. */
786 shift_amount = (GET_MODE_PRECISION (to_mode)
787 - GET_MODE_PRECISION (from_mode));
788 from = gen_lowpart (to_mode, force_reg (from_mode, from));
789 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
790 to, unsignedp);
791 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
792 to, unsignedp);
793 if (tmp != to)
794 emit_move_insn (to, tmp);
795 return;
796 }
797 }
798
799 /* Support special truncate insns for certain modes. */
800 if (convert_optab_handler (trunc_optab, to_mode,
801 from_mode) != CODE_FOR_nothing)
802 {
803 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
804 to, from, UNKNOWN);
805 return;
806 }
807
808 /* Handle truncation of volatile memrefs, and so on;
809 the things that couldn't be truncated directly,
810 and for which there was no special instruction.
811
812 ??? Code above formerly short-circuited this, for most integer
813 mode pairs, with a force_reg in from_mode followed by a recursive
814 call to this routine. Appears always to have been wrong. */
815 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
816 {
817 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
818 emit_move_insn (to, temp);
819 return;
820 }
821
822 /* Mode combination is not recognized. */
823 gcc_unreachable ();
824 }
825
826 /* Return an rtx for a value that would result
827 from converting X to mode MODE.
828 Both X and MODE may be floating, or both integer.
829 UNSIGNEDP is nonzero if X is an unsigned value.
830 This can be done by referring to a part of X in place
831 or by copying to a new temporary with conversion. */
832
833 rtx
834 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
835 {
836 return convert_modes (mode, VOIDmode, x, unsignedp);
837 }
838
839 /* Return an rtx for a value that would result
840 from converting X from mode OLDMODE to mode MODE.
841 Both modes may be floating, or both integer.
842 UNSIGNEDP is nonzero if X is an unsigned value.
843
844 This can be done by referring to a part of X in place
845 or by copying to a new temporary with conversion.
846
847 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
848
849 rtx
850 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
851 {
852 rtx temp;
853 scalar_int_mode int_mode;
854
855 /* If FROM is a SUBREG that indicates that we have already done at least
856 the required extension, strip it. */
857
858 if (GET_CODE (x) == SUBREG
859 && SUBREG_PROMOTED_VAR_P (x)
860 && is_a <scalar_int_mode> (mode, &int_mode)
861 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
862 >= GET_MODE_PRECISION (int_mode))
863 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
864 {
865 scalar_int_mode int_orig_mode;
866 scalar_int_mode int_inner_mode;
867 machine_mode orig_mode = GET_MODE (x);
868 x = gen_lowpart (int_mode, SUBREG_REG (x));
869
870 /* Preserve SUBREG_PROMOTED_VAR_P if the new mode is wider than
871 the original mode, but narrower than the inner mode. */
872 if (GET_CODE (x) == SUBREG
873 && is_a <scalar_int_mode> (orig_mode, &int_orig_mode)
874 && GET_MODE_PRECISION (int_mode)
875 > GET_MODE_PRECISION (int_orig_mode)
876 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (x)),
877 &int_inner_mode)
878 && GET_MODE_PRECISION (int_inner_mode)
879 > GET_MODE_PRECISION (int_mode))
880 {
881 SUBREG_PROMOTED_VAR_P (x) = 1;
882 SUBREG_PROMOTED_SET (x, unsignedp);
883 }
884 }
885
886 if (GET_MODE (x) != VOIDmode)
887 oldmode = GET_MODE (x);
888
889 if (mode == oldmode)
890 return x;
891
892 if (CONST_SCALAR_INT_P (x)
893 && is_a <scalar_int_mode> (mode, &int_mode))
894 {
895 /* If the caller did not tell us the old mode, then there is not
896 much to do with respect to canonicalization. We have to
897 assume that all the bits are significant. */
898 if (!is_a <scalar_int_mode> (oldmode))
899 oldmode = MAX_MODE_INT;
900 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
901 GET_MODE_PRECISION (int_mode),
902 unsignedp ? UNSIGNED : SIGNED);
903 return immed_wide_int_const (w, int_mode);
904 }
905
906 /* We can do this with a gen_lowpart if both desired and current modes
907 are integer, and this is either a constant integer, a register, or a
908 non-volatile MEM. */
909 scalar_int_mode int_oldmode;
910 if (is_int_mode (mode, &int_mode)
911 && is_int_mode (oldmode, &int_oldmode)
912 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
913 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
914 || CONST_POLY_INT_P (x)
915 || (REG_P (x)
916 && (!HARD_REGISTER_P (x)
917 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
918 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
919 return gen_lowpart (int_mode, x);
920
921 /* Converting from integer constant into mode is always equivalent to an
922 subreg operation. */
923 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
924 {
925 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
926 GET_MODE_BITSIZE (oldmode)));
927 return simplify_gen_subreg (mode, x, oldmode, 0);
928 }
929
930 temp = gen_reg_rtx (mode);
931 convert_move (temp, x, unsignedp);
932 return temp;
933 }
934
935 /* Variant of convert_modes for ABI parameter passing/return.
936 Return an rtx for a value that would result from converting X from
937 a floating point mode FMODE to wider integer mode MODE. */
938
939 rtx
940 convert_float_to_wider_int (machine_mode mode, machine_mode fmode, rtx x)
941 {
942 gcc_assert (SCALAR_INT_MODE_P (mode) && SCALAR_FLOAT_MODE_P (fmode));
943 scalar_int_mode tmp_mode = int_mode_for_mode (fmode).require ();
944 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
945 return convert_modes (mode, tmp_mode, tmp, 1);
946 }
947
948 /* Variant of convert_modes for ABI parameter passing/return.
949 Return an rtx for a value that would result from converting X from
950 an integer mode IMODE to a narrower floating point mode MODE. */
951
952 rtx
953 convert_wider_int_to_float (machine_mode mode, machine_mode imode, rtx x)
954 {
955 gcc_assert (SCALAR_FLOAT_MODE_P (mode) && SCALAR_INT_MODE_P (imode));
956 scalar_int_mode tmp_mode = int_mode_for_mode (mode).require ();
957 rtx tmp = force_reg (tmp_mode, gen_lowpart (tmp_mode, x));
958 return gen_lowpart_SUBREG (mode, tmp);
959 }
960 \f
961 /* Return the largest alignment we can use for doing a move (or store)
962 of MAX_PIECES. ALIGN is the largest alignment we could use. */
963
964 static unsigned int
965 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
966 {
967 scalar_int_mode tmode
968 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 0).require ();
969
970 if (align >= GET_MODE_ALIGNMENT (tmode))
971 align = GET_MODE_ALIGNMENT (tmode);
972 else
973 {
974 scalar_int_mode xmode = NARROWEST_INT_MODE;
975 opt_scalar_int_mode mode_iter;
976 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
977 {
978 tmode = mode_iter.require ();
979 if (GET_MODE_SIZE (tmode) > max_pieces
980 || targetm.slow_unaligned_access (tmode, align))
981 break;
982 xmode = tmode;
983 }
984
985 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
986 }
987
988 return align;
989 }
990
991 /* Return the widest QI vector, if QI_MODE is true, or integer mode
992 that is narrower than SIZE bytes. */
993
994 static fixed_size_mode
995 widest_fixed_size_mode_for_size (unsigned int size, bool qi_vector)
996 {
997 fixed_size_mode result = NARROWEST_INT_MODE;
998
999 gcc_checking_assert (size > 1);
1000
1001 /* Use QI vector only if size is wider than a WORD. */
1002 if (qi_vector && size > UNITS_PER_WORD)
1003 {
1004 machine_mode mode;
1005 fixed_size_mode candidate;
1006 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1007 if (is_a<fixed_size_mode> (mode, &candidate)
1008 && GET_MODE_INNER (candidate) == QImode)
1009 {
1010 if (GET_MODE_SIZE (candidate) >= size)
1011 break;
1012 if (optab_handler (vec_duplicate_optab, candidate)
1013 != CODE_FOR_nothing)
1014 result = candidate;
1015 }
1016
1017 if (result != NARROWEST_INT_MODE)
1018 return result;
1019 }
1020
1021 opt_scalar_int_mode tmode;
1022 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
1023 if (GET_MODE_SIZE (tmode.require ()) < size)
1024 result = tmode.require ();
1025
1026 return result;
1027 }
1028
1029 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
1030 and should be performed piecewise. */
1031
1032 static bool
1033 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
1034 enum by_pieces_operation op)
1035 {
1036 return targetm.use_by_pieces_infrastructure_p (len, align, op,
1037 optimize_insn_for_speed_p ());
1038 }
1039
1040 /* Determine whether the LEN bytes can be moved by using several move
1041 instructions. Return nonzero if a call to move_by_pieces should
1042 succeed. */
1043
1044 bool
1045 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
1046 {
1047 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
1048 }
1049
1050 /* Return number of insns required to perform operation OP by pieces
1051 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
1052
1053 unsigned HOST_WIDE_INT
1054 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1055 unsigned int max_size, by_pieces_operation op)
1056 {
1057 unsigned HOST_WIDE_INT n_insns = 0;
1058 fixed_size_mode mode;
1059
1060 if (targetm.overlap_op_by_pieces_p () && op != COMPARE_BY_PIECES)
1061 {
1062 /* NB: Round up L and ALIGN to the widest integer mode for
1063 MAX_SIZE. */
1064 mode = widest_fixed_size_mode_for_size (max_size,
1065 op == SET_BY_PIECES);
1066 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
1067 {
1068 unsigned HOST_WIDE_INT up = ROUND_UP (l, GET_MODE_SIZE (mode));
1069 if (up > l)
1070 l = up;
1071 align = GET_MODE_ALIGNMENT (mode);
1072 }
1073 }
1074
1075 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1076
1077 while (max_size > 1 && l > 0)
1078 {
1079 mode = widest_fixed_size_mode_for_size (max_size,
1080 op == SET_BY_PIECES);
1081 enum insn_code icode;
1082
1083 unsigned int modesize = GET_MODE_SIZE (mode);
1084
1085 icode = optab_handler (mov_optab, mode);
1086 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1087 {
1088 unsigned HOST_WIDE_INT n_pieces = l / modesize;
1089 l %= modesize;
1090 switch (op)
1091 {
1092 default:
1093 n_insns += n_pieces;
1094 break;
1095
1096 case COMPARE_BY_PIECES:
1097 int batch = targetm.compare_by_pieces_branch_ratio (mode);
1098 int batch_ops = 4 * batch - 1;
1099 unsigned HOST_WIDE_INT full = n_pieces / batch;
1100 n_insns += full * batch_ops;
1101 if (n_pieces % batch != 0)
1102 n_insns++;
1103 break;
1104
1105 }
1106 }
1107 max_size = modesize;
1108 }
1109
1110 gcc_assert (!l);
1111 return n_insns;
1112 }
1113
1114 /* Used when performing piecewise block operations, holds information
1115 about one of the memory objects involved. The member functions
1116 can be used to generate code for loading from the object and
1117 updating the address when iterating. */
1118
1119 class pieces_addr
1120 {
1121 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
1122 stack pushes. */
1123 rtx m_obj;
1124 /* The address of the object. Can differ from that seen in the
1125 MEM rtx if we copied the address to a register. */
1126 rtx m_addr;
1127 /* Nonzero if the address on the object has an autoincrement already,
1128 signifies whether that was an increment or decrement. */
1129 signed char m_addr_inc;
1130 /* Nonzero if we intend to use autoinc without the address already
1131 having autoinc form. We will insert add insns around each memory
1132 reference, expecting later passes to form autoinc addressing modes.
1133 The only supported options are predecrement and postincrement. */
1134 signed char m_explicit_inc;
1135 /* True if we have either of the two possible cases of using
1136 autoincrement. */
1137 bool m_auto;
1138 /* True if this is an address to be used for load operations rather
1139 than stores. */
1140 bool m_is_load;
1141
1142 /* Optionally, a function to obtain constants for any given offset into
1143 the objects, and data associated with it. */
1144 by_pieces_constfn m_constfn;
1145 void *m_cfndata;
1146 public:
1147 pieces_addr (rtx, bool, by_pieces_constfn, void *);
1148 rtx adjust (fixed_size_mode, HOST_WIDE_INT, by_pieces_prev * = nullptr);
1149 void increment_address (HOST_WIDE_INT);
1150 void maybe_predec (HOST_WIDE_INT);
1151 void maybe_postinc (HOST_WIDE_INT);
1152 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
1153 int get_addr_inc ()
1154 {
1155 return m_addr_inc;
1156 }
1157 };
1158
1159 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
1160 true if the operation to be performed on this object is a load
1161 rather than a store. For stores, OBJ can be NULL, in which case we
1162 assume the operation is a stack push. For loads, the optional
1163 CONSTFN and its associated CFNDATA can be used in place of the
1164 memory load. */
1165
1166 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
1167 void *cfndata)
1168 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
1169 {
1170 m_addr_inc = 0;
1171 m_auto = false;
1172 if (obj)
1173 {
1174 rtx addr = XEXP (obj, 0);
1175 rtx_code code = GET_CODE (addr);
1176 m_addr = addr;
1177 bool dec = code == PRE_DEC || code == POST_DEC;
1178 bool inc = code == PRE_INC || code == POST_INC;
1179 m_auto = inc || dec;
1180 if (m_auto)
1181 m_addr_inc = dec ? -1 : 1;
1182
1183 /* While we have always looked for these codes here, the code
1184 implementing the memory operation has never handled them.
1185 Support could be added later if necessary or beneficial. */
1186 gcc_assert (code != PRE_INC && code != POST_DEC);
1187 }
1188 else
1189 {
1190 m_addr = NULL_RTX;
1191 if (!is_load)
1192 {
1193 m_auto = true;
1194 if (STACK_GROWS_DOWNWARD)
1195 m_addr_inc = -1;
1196 else
1197 m_addr_inc = 1;
1198 }
1199 else
1200 gcc_assert (constfn != NULL);
1201 }
1202 m_explicit_inc = 0;
1203 if (constfn)
1204 gcc_assert (is_load);
1205 }
1206
1207 /* Decide whether to use autoinc for an address involved in a memory op.
1208 MODE is the mode of the accesses, REVERSE is true if we've decided to
1209 perform the operation starting from the end, and LEN is the length of
1210 the operation. Don't override an earlier decision to set m_auto. */
1211
1212 void
1213 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
1214 HOST_WIDE_INT len)
1215 {
1216 if (m_auto || m_obj == NULL_RTX)
1217 return;
1218
1219 bool use_predec = (m_is_load
1220 ? USE_LOAD_PRE_DECREMENT (mode)
1221 : USE_STORE_PRE_DECREMENT (mode));
1222 bool use_postinc = (m_is_load
1223 ? USE_LOAD_POST_INCREMENT (mode)
1224 : USE_STORE_POST_INCREMENT (mode));
1225 machine_mode addr_mode = get_address_mode (m_obj);
1226
1227 if (use_predec && reverse)
1228 {
1229 m_addr = copy_to_mode_reg (addr_mode,
1230 plus_constant (addr_mode,
1231 m_addr, len));
1232 m_auto = true;
1233 m_explicit_inc = -1;
1234 }
1235 else if (use_postinc && !reverse)
1236 {
1237 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1238 m_auto = true;
1239 m_explicit_inc = 1;
1240 }
1241 else if (CONSTANT_P (m_addr))
1242 m_addr = copy_to_mode_reg (addr_mode, m_addr);
1243 }
1244
1245 /* Adjust the address to refer to the data at OFFSET in MODE. If we
1246 are using autoincrement for this address, we don't add the offset,
1247 but we still modify the MEM's properties. */
1248
1249 rtx
1250 pieces_addr::adjust (fixed_size_mode mode, HOST_WIDE_INT offset,
1251 by_pieces_prev *prev)
1252 {
1253 if (m_constfn)
1254 /* Pass the previous data to m_constfn. */
1255 return m_constfn (m_cfndata, prev, offset, mode);
1256 if (m_obj == NULL_RTX)
1257 return NULL_RTX;
1258 if (m_auto)
1259 return adjust_automodify_address (m_obj, mode, m_addr, offset);
1260 else
1261 return adjust_address (m_obj, mode, offset);
1262 }
1263
1264 /* Emit an add instruction to increment the address by SIZE. */
1265
1266 void
1267 pieces_addr::increment_address (HOST_WIDE_INT size)
1268 {
1269 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
1270 emit_insn (gen_add2_insn (m_addr, amount));
1271 }
1272
1273 /* If we are supposed to decrement the address after each access, emit code
1274 to do so now. Increment by SIZE (which has should have the correct sign
1275 already). */
1276
1277 void
1278 pieces_addr::maybe_predec (HOST_WIDE_INT size)
1279 {
1280 if (m_explicit_inc >= 0)
1281 return;
1282 gcc_assert (HAVE_PRE_DECREMENT);
1283 increment_address (size);
1284 }
1285
1286 /* If we are supposed to decrement the address after each access, emit code
1287 to do so now. Increment by SIZE. */
1288
1289 void
1290 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1291 {
1292 if (m_explicit_inc <= 0)
1293 return;
1294 gcc_assert (HAVE_POST_INCREMENT);
1295 increment_address (size);
1296 }
1297
1298 /* This structure is used by do_op_by_pieces to describe the operation
1299 to be performed. */
1300
1301 class op_by_pieces_d
1302 {
1303 private:
1304 fixed_size_mode get_usable_mode (fixed_size_mode, unsigned int);
1305 fixed_size_mode smallest_fixed_size_mode_for_size (unsigned int);
1306
1307 protected:
1308 pieces_addr m_to, m_from;
1309 /* Make m_len read-only so that smallest_fixed_size_mode_for_size can
1310 use it to check the valid mode size. */
1311 const unsigned HOST_WIDE_INT m_len;
1312 HOST_WIDE_INT m_offset;
1313 unsigned int m_align;
1314 unsigned int m_max_size;
1315 bool m_reverse;
1316 /* True if this is a stack push. */
1317 bool m_push;
1318 /* True if targetm.overlap_op_by_pieces_p () returns true. */
1319 bool m_overlap_op_by_pieces;
1320 /* True if QI vector mode can be used. */
1321 bool m_qi_vector_mode;
1322
1323 /* Virtual functions, overriden by derived classes for the specific
1324 operation. */
1325 virtual void generate (rtx, rtx, machine_mode) = 0;
1326 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1327 virtual void finish_mode (machine_mode)
1328 {
1329 }
1330
1331 public:
1332 op_by_pieces_d (unsigned int, rtx, bool, rtx, bool, by_pieces_constfn,
1333 void *, unsigned HOST_WIDE_INT, unsigned int, bool,
1334 bool = false);
1335 void run ();
1336 };
1337
1338 /* The constructor for an op_by_pieces_d structure. We require two
1339 objects named TO and FROM, which are identified as loads or stores
1340 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1341 and its associated FROM_CFN_DATA can be used to replace loads with
1342 constant values. MAX_PIECES describes the maximum number of bytes
1343 at a time which can be moved efficiently. LEN describes the length
1344 of the operation. */
1345
1346 op_by_pieces_d::op_by_pieces_d (unsigned int max_pieces, rtx to,
1347 bool to_load, rtx from, bool from_load,
1348 by_pieces_constfn from_cfn,
1349 void *from_cfn_data,
1350 unsigned HOST_WIDE_INT len,
1351 unsigned int align, bool push,
1352 bool qi_vector_mode)
1353 : m_to (to, to_load, NULL, NULL),
1354 m_from (from, from_load, from_cfn, from_cfn_data),
1355 m_len (len), m_max_size (max_pieces + 1),
1356 m_push (push), m_qi_vector_mode (qi_vector_mode)
1357 {
1358 int toi = m_to.get_addr_inc ();
1359 int fromi = m_from.get_addr_inc ();
1360 if (toi >= 0 && fromi >= 0)
1361 m_reverse = false;
1362 else if (toi <= 0 && fromi <= 0)
1363 m_reverse = true;
1364 else
1365 gcc_unreachable ();
1366
1367 m_offset = m_reverse ? len : 0;
1368 align = MIN (to ? MEM_ALIGN (to) : align,
1369 from ? MEM_ALIGN (from) : align);
1370
1371 /* If copying requires more than two move insns,
1372 copy addresses to registers (to make displacements shorter)
1373 and use post-increment if available. */
1374 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1375 {
1376 /* Find the mode of the largest comparison. */
1377 fixed_size_mode mode
1378 = widest_fixed_size_mode_for_size (m_max_size,
1379 m_qi_vector_mode);
1380
1381 m_from.decide_autoinc (mode, m_reverse, len);
1382 m_to.decide_autoinc (mode, m_reverse, len);
1383 }
1384
1385 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1386 m_align = align;
1387
1388 m_overlap_op_by_pieces = targetm.overlap_op_by_pieces_p ();
1389 }
1390
1391 /* This function returns the largest usable integer mode for LEN bytes
1392 whose size is no bigger than size of MODE. */
1393
1394 fixed_size_mode
1395 op_by_pieces_d::get_usable_mode (fixed_size_mode mode, unsigned int len)
1396 {
1397 unsigned int size;
1398 do
1399 {
1400 size = GET_MODE_SIZE (mode);
1401 if (len >= size && prepare_mode (mode, m_align))
1402 break;
1403 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1404 mode = widest_fixed_size_mode_for_size (size, m_qi_vector_mode);
1405 }
1406 while (1);
1407 return mode;
1408 }
1409
1410 /* Return the smallest integer or QI vector mode that is not narrower
1411 than SIZE bytes. */
1412
1413 fixed_size_mode
1414 op_by_pieces_d::smallest_fixed_size_mode_for_size (unsigned int size)
1415 {
1416 /* Use QI vector only for > size of WORD. */
1417 if (m_qi_vector_mode && size > UNITS_PER_WORD)
1418 {
1419 machine_mode mode;
1420 fixed_size_mode candidate;
1421 FOR_EACH_MODE_IN_CLASS (mode, MODE_VECTOR_INT)
1422 if (is_a<fixed_size_mode> (mode, &candidate)
1423 && GET_MODE_INNER (candidate) == QImode)
1424 {
1425 /* Don't return a mode wider than M_LEN. */
1426 if (GET_MODE_SIZE (candidate) > m_len)
1427 break;
1428
1429 if (GET_MODE_SIZE (candidate) >= size
1430 && (optab_handler (vec_duplicate_optab, candidate)
1431 != CODE_FOR_nothing))
1432 return candidate;
1433 }
1434 }
1435
1436 return smallest_int_mode_for_size (size * BITS_PER_UNIT);
1437 }
1438
1439 /* This function contains the main loop used for expanding a block
1440 operation. First move what we can in the largest integer mode,
1441 then go to successively smaller modes. For every access, call
1442 GENFUN with the two operands and the EXTRA_DATA. */
1443
1444 void
1445 op_by_pieces_d::run ()
1446 {
1447 if (m_len == 0)
1448 return;
1449
1450 unsigned HOST_WIDE_INT length = m_len;
1451
1452 /* widest_fixed_size_mode_for_size checks M_MAX_SIZE > 1. */
1453 fixed_size_mode mode
1454 = widest_fixed_size_mode_for_size (m_max_size, m_qi_vector_mode);
1455 mode = get_usable_mode (mode, length);
1456
1457 by_pieces_prev to_prev = { nullptr, mode };
1458 by_pieces_prev from_prev = { nullptr, mode };
1459
1460 do
1461 {
1462 unsigned int size = GET_MODE_SIZE (mode);
1463 rtx to1 = NULL_RTX, from1;
1464
1465 while (length >= size)
1466 {
1467 if (m_reverse)
1468 m_offset -= size;
1469
1470 to1 = m_to.adjust (mode, m_offset, &to_prev);
1471 to_prev.data = to1;
1472 to_prev.mode = mode;
1473 from1 = m_from.adjust (mode, m_offset, &from_prev);
1474 from_prev.data = from1;
1475 from_prev.mode = mode;
1476
1477 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1478 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1479
1480 generate (to1, from1, mode);
1481
1482 m_to.maybe_postinc (size);
1483 m_from.maybe_postinc (size);
1484
1485 if (!m_reverse)
1486 m_offset += size;
1487
1488 length -= size;
1489 }
1490
1491 finish_mode (mode);
1492
1493 if (length == 0)
1494 return;
1495
1496 if (!m_push && m_overlap_op_by_pieces)
1497 {
1498 /* NB: Generate overlapping operations if it is not a stack
1499 push since stack push must not overlap. Get the smallest
1500 fixed size mode for M_LEN bytes. */
1501 mode = smallest_fixed_size_mode_for_size (length);
1502 mode = get_usable_mode (mode, GET_MODE_SIZE (mode));
1503 int gap = GET_MODE_SIZE (mode) - length;
1504 if (gap > 0)
1505 {
1506 /* If size of MODE > M_LEN, generate the last operation
1507 in MODE for the remaining bytes with ovelapping memory
1508 from the previois operation. */
1509 if (m_reverse)
1510 m_offset += gap;
1511 else
1512 m_offset -= gap;
1513 length += gap;
1514 }
1515 }
1516 else
1517 {
1518 /* widest_fixed_size_mode_for_size checks SIZE > 1. */
1519 mode = widest_fixed_size_mode_for_size (size,
1520 m_qi_vector_mode);
1521 mode = get_usable_mode (mode, length);
1522 }
1523 }
1524 while (1);
1525 }
1526
1527 /* Derived class from op_by_pieces_d, providing support for block move
1528 operations. */
1529
1530 #ifdef PUSH_ROUNDING
1531 #define PUSHG_P(to) ((to) == nullptr)
1532 #else
1533 #define PUSHG_P(to) false
1534 #endif
1535
1536 class move_by_pieces_d : public op_by_pieces_d
1537 {
1538 insn_gen_fn m_gen_fun;
1539 void generate (rtx, rtx, machine_mode) final override;
1540 bool prepare_mode (machine_mode, unsigned int) final override;
1541
1542 public:
1543 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1544 unsigned int align)
1545 : op_by_pieces_d (MOVE_MAX_PIECES, to, false, from, true, NULL,
1546 NULL, len, align, PUSHG_P (to))
1547 {
1548 }
1549 rtx finish_retmode (memop_ret);
1550 };
1551
1552 /* Return true if MODE can be used for a set of copies, given an
1553 alignment ALIGN. Prepare whatever data is necessary for later
1554 calls to generate. */
1555
1556 bool
1557 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1558 {
1559 insn_code icode = optab_handler (mov_optab, mode);
1560 m_gen_fun = GEN_FCN (icode);
1561 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1562 }
1563
1564 /* A callback used when iterating for a compare_by_pieces_operation.
1565 OP0 and OP1 are the values that have been loaded and should be
1566 compared in MODE. If OP0 is NULL, this means we should generate a
1567 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1568 gen function that should be used to generate the mode. */
1569
1570 void
1571 move_by_pieces_d::generate (rtx op0, rtx op1,
1572 machine_mode mode ATTRIBUTE_UNUSED)
1573 {
1574 #ifdef PUSH_ROUNDING
1575 if (op0 == NULL_RTX)
1576 {
1577 emit_single_push_insn (mode, op1, NULL);
1578 return;
1579 }
1580 #endif
1581 emit_insn (m_gen_fun (op0, op1));
1582 }
1583
1584 /* Perform the final adjustment at the end of a string to obtain the
1585 correct return value for the block operation.
1586 Return value is based on RETMODE argument. */
1587
1588 rtx
1589 move_by_pieces_d::finish_retmode (memop_ret retmode)
1590 {
1591 gcc_assert (!m_reverse);
1592 if (retmode == RETURN_END_MINUS_ONE)
1593 {
1594 m_to.maybe_postinc (-1);
1595 --m_offset;
1596 }
1597 return m_to.adjust (QImode, m_offset);
1598 }
1599
1600 /* Generate several move instructions to copy LEN bytes from block FROM to
1601 block TO. (These are MEM rtx's with BLKmode).
1602
1603 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1604 used to push FROM to the stack.
1605
1606 ALIGN is maximum stack alignment we can assume.
1607
1608 Return value is based on RETMODE argument. */
1609
1610 rtx
1611 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1612 unsigned int align, memop_ret retmode)
1613 {
1614 #ifndef PUSH_ROUNDING
1615 if (to == NULL)
1616 gcc_unreachable ();
1617 #endif
1618
1619 move_by_pieces_d data (to, from, len, align);
1620
1621 data.run ();
1622
1623 if (retmode != RETURN_BEGIN)
1624 return data.finish_retmode (retmode);
1625 else
1626 return to;
1627 }
1628
1629 /* Derived class from op_by_pieces_d, providing support for block move
1630 operations. */
1631
1632 class store_by_pieces_d : public op_by_pieces_d
1633 {
1634 insn_gen_fn m_gen_fun;
1635 void generate (rtx, rtx, machine_mode) final override;
1636 bool prepare_mode (machine_mode, unsigned int) final override;
1637
1638 public:
1639 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1640 unsigned HOST_WIDE_INT len, unsigned int align,
1641 bool qi_vector_mode)
1642 : op_by_pieces_d (STORE_MAX_PIECES, to, false, NULL_RTX, true, cfn,
1643 cfn_data, len, align, false, qi_vector_mode)
1644 {
1645 }
1646 rtx finish_retmode (memop_ret);
1647 };
1648
1649 /* Return true if MODE can be used for a set of stores, given an
1650 alignment ALIGN. Prepare whatever data is necessary for later
1651 calls to generate. */
1652
1653 bool
1654 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1655 {
1656 insn_code icode = optab_handler (mov_optab, mode);
1657 m_gen_fun = GEN_FCN (icode);
1658 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1659 }
1660
1661 /* A callback used when iterating for a store_by_pieces_operation.
1662 OP0 and OP1 are the values that have been loaded and should be
1663 compared in MODE. If OP0 is NULL, this means we should generate a
1664 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1665 gen function that should be used to generate the mode. */
1666
1667 void
1668 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1669 {
1670 emit_insn (m_gen_fun (op0, op1));
1671 }
1672
1673 /* Perform the final adjustment at the end of a string to obtain the
1674 correct return value for the block operation.
1675 Return value is based on RETMODE argument. */
1676
1677 rtx
1678 store_by_pieces_d::finish_retmode (memop_ret retmode)
1679 {
1680 gcc_assert (!m_reverse);
1681 if (retmode == RETURN_END_MINUS_ONE)
1682 {
1683 m_to.maybe_postinc (-1);
1684 --m_offset;
1685 }
1686 return m_to.adjust (QImode, m_offset);
1687 }
1688
1689 /* Determine whether the LEN bytes generated by CONSTFUN can be
1690 stored to memory using several move instructions. CONSTFUNDATA is
1691 a pointer which will be passed as argument in every CONSTFUN call.
1692 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1693 a memset operation and false if it's a copy of a constant string.
1694 Return nonzero if a call to store_by_pieces should succeed. */
1695
1696 int
1697 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1698 by_pieces_constfn constfun,
1699 void *constfundata, unsigned int align, bool memsetp)
1700 {
1701 unsigned HOST_WIDE_INT l;
1702 unsigned int max_size;
1703 HOST_WIDE_INT offset = 0;
1704 enum insn_code icode;
1705 int reverse;
1706 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1707 rtx cst ATTRIBUTE_UNUSED;
1708
1709 if (len == 0)
1710 return 1;
1711
1712 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1713 memsetp
1714 ? SET_BY_PIECES
1715 : STORE_BY_PIECES,
1716 optimize_insn_for_speed_p ()))
1717 return 0;
1718
1719 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1720
1721 /* We would first store what we can in the largest integer mode, then go to
1722 successively smaller modes. */
1723
1724 for (reverse = 0;
1725 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1726 reverse++)
1727 {
1728 l = len;
1729 max_size = STORE_MAX_PIECES + 1;
1730 while (max_size > 1 && l > 0)
1731 {
1732 fixed_size_mode mode
1733 = widest_fixed_size_mode_for_size (max_size, memsetp);
1734
1735 icode = optab_handler (mov_optab, mode);
1736 if (icode != CODE_FOR_nothing
1737 && align >= GET_MODE_ALIGNMENT (mode))
1738 {
1739 unsigned int size = GET_MODE_SIZE (mode);
1740
1741 while (l >= size)
1742 {
1743 if (reverse)
1744 offset -= size;
1745
1746 cst = (*constfun) (constfundata, nullptr, offset, mode);
1747 /* All CONST_VECTORs can be loaded for memset since
1748 vec_duplicate_optab is a precondition to pick a
1749 vector mode for the memset expander. */
1750 if (!((memsetp && VECTOR_MODE_P (mode))
1751 || targetm.legitimate_constant_p (mode, cst)))
1752 return 0;
1753
1754 if (!reverse)
1755 offset += size;
1756
1757 l -= size;
1758 }
1759 }
1760
1761 max_size = GET_MODE_SIZE (mode);
1762 }
1763
1764 /* The code above should have handled everything. */
1765 gcc_assert (!l);
1766 }
1767
1768 return 1;
1769 }
1770
1771 /* Generate several move instructions to store LEN bytes generated by
1772 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1773 pointer which will be passed as argument in every CONSTFUN call.
1774 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1775 a memset operation and false if it's a copy of a constant string.
1776 Return value is based on RETMODE argument. */
1777
1778 rtx
1779 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1780 by_pieces_constfn constfun,
1781 void *constfundata, unsigned int align, bool memsetp,
1782 memop_ret retmode)
1783 {
1784 if (len == 0)
1785 {
1786 gcc_assert (retmode != RETURN_END_MINUS_ONE);
1787 return to;
1788 }
1789
1790 gcc_assert (targetm.use_by_pieces_infrastructure_p
1791 (len, align,
1792 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1793 optimize_insn_for_speed_p ()));
1794
1795 store_by_pieces_d data (to, constfun, constfundata, len, align,
1796 memsetp);
1797 data.run ();
1798
1799 if (retmode != RETURN_BEGIN)
1800 return data.finish_retmode (retmode);
1801 else
1802 return to;
1803 }
1804
1805 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1806 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1807
1808 static void
1809 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1810 {
1811 if (len == 0)
1812 return;
1813
1814 /* Use builtin_memset_read_str to support vector mode broadcast. */
1815 char c = 0;
1816 store_by_pieces_d data (to, builtin_memset_read_str, &c, len, align,
1817 true);
1818 data.run ();
1819 }
1820
1821 /* Context used by compare_by_pieces_genfn. It stores the fail label
1822 to jump to in case of miscomparison, and for branch ratios greater than 1,
1823 it stores an accumulator and the current and maximum counts before
1824 emitting another branch. */
1825
1826 class compare_by_pieces_d : public op_by_pieces_d
1827 {
1828 rtx_code_label *m_fail_label;
1829 rtx m_accumulator;
1830 int m_count, m_batch;
1831
1832 void generate (rtx, rtx, machine_mode) final override;
1833 bool prepare_mode (machine_mode, unsigned int) final override;
1834 void finish_mode (machine_mode) final override;
1835 public:
1836 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1837 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1838 rtx_code_label *fail_label)
1839 : op_by_pieces_d (COMPARE_MAX_PIECES, op0, true, op1, true, op1_cfn,
1840 op1_cfn_data, len, align, false)
1841 {
1842 m_fail_label = fail_label;
1843 }
1844 };
1845
1846 /* A callback used when iterating for a compare_by_pieces_operation.
1847 OP0 and OP1 are the values that have been loaded and should be
1848 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1849 context structure. */
1850
1851 void
1852 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1853 {
1854 if (m_batch > 1)
1855 {
1856 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1857 true, OPTAB_LIB_WIDEN);
1858 if (m_count != 0)
1859 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1860 true, OPTAB_LIB_WIDEN);
1861 m_accumulator = temp;
1862
1863 if (++m_count < m_batch)
1864 return;
1865
1866 m_count = 0;
1867 op0 = m_accumulator;
1868 op1 = const0_rtx;
1869 m_accumulator = NULL_RTX;
1870 }
1871 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1872 m_fail_label, profile_probability::uninitialized ());
1873 }
1874
1875 /* Return true if MODE can be used for a set of moves and comparisons,
1876 given an alignment ALIGN. Prepare whatever data is necessary for
1877 later calls to generate. */
1878
1879 bool
1880 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1881 {
1882 insn_code icode = optab_handler (mov_optab, mode);
1883 if (icode == CODE_FOR_nothing
1884 || align < GET_MODE_ALIGNMENT (mode)
1885 || !can_compare_p (EQ, mode, ccp_jump))
1886 return false;
1887 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1888 if (m_batch < 0)
1889 return false;
1890 m_accumulator = NULL_RTX;
1891 m_count = 0;
1892 return true;
1893 }
1894
1895 /* Called after expanding a series of comparisons in MODE. If we have
1896 accumulated results for which we haven't emitted a branch yet, do
1897 so now. */
1898
1899 void
1900 compare_by_pieces_d::finish_mode (machine_mode mode)
1901 {
1902 if (m_accumulator != NULL_RTX)
1903 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1904 NULL_RTX, NULL, m_fail_label,
1905 profile_probability::uninitialized ());
1906 }
1907
1908 /* Generate several move instructions to compare LEN bytes from blocks
1909 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1910
1911 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1912 used to push FROM to the stack.
1913
1914 ALIGN is maximum stack alignment we can assume.
1915
1916 Optionally, the caller can pass a constfn and associated data in A1_CFN
1917 and A1_CFN_DATA. describing that the second operand being compared is a
1918 known constant and how to obtain its data. */
1919
1920 static rtx
1921 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1922 rtx target, unsigned int align,
1923 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1924 {
1925 rtx_code_label *fail_label = gen_label_rtx ();
1926 rtx_code_label *end_label = gen_label_rtx ();
1927
1928 if (target == NULL_RTX
1929 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1930 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1931
1932 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1933 fail_label);
1934
1935 data.run ();
1936
1937 emit_move_insn (target, const0_rtx);
1938 emit_jump (end_label);
1939 emit_barrier ();
1940 emit_label (fail_label);
1941 emit_move_insn (target, const1_rtx);
1942 emit_label (end_label);
1943
1944 return target;
1945 }
1946 \f
1947 /* Emit code to move a block Y to a block X. This may be done with
1948 string-move instructions, with multiple scalar move instructions,
1949 or with a library call.
1950
1951 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1952 SIZE is an rtx that says how long they are.
1953 ALIGN is the maximum alignment we can assume they have.
1954 METHOD describes what kind of copy this is, and what mechanisms may be used.
1955 MIN_SIZE is the minimal size of block to move
1956 MAX_SIZE is the maximal size of block to move, if it cannot be represented
1957 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1958
1959 Return the address of the new block, if memcpy is called and returns it,
1960 0 otherwise. */
1961
1962 rtx
1963 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1964 unsigned int expected_align, HOST_WIDE_INT expected_size,
1965 unsigned HOST_WIDE_INT min_size,
1966 unsigned HOST_WIDE_INT max_size,
1967 unsigned HOST_WIDE_INT probable_max_size,
1968 bool bail_out_libcall, bool *is_move_done,
1969 bool might_overlap)
1970 {
1971 int may_use_call;
1972 rtx retval = 0;
1973 unsigned int align;
1974
1975 if (is_move_done)
1976 *is_move_done = true;
1977
1978 gcc_assert (size);
1979 if (CONST_INT_P (size) && INTVAL (size) == 0)
1980 return 0;
1981
1982 switch (method)
1983 {
1984 case BLOCK_OP_NORMAL:
1985 case BLOCK_OP_TAILCALL:
1986 may_use_call = 1;
1987 break;
1988
1989 case BLOCK_OP_CALL_PARM:
1990 may_use_call = block_move_libcall_safe_for_call_parm ();
1991
1992 /* Make inhibit_defer_pop nonzero around the library call
1993 to force it to pop the arguments right away. */
1994 NO_DEFER_POP;
1995 break;
1996
1997 case BLOCK_OP_NO_LIBCALL:
1998 may_use_call = 0;
1999 break;
2000
2001 case BLOCK_OP_NO_LIBCALL_RET:
2002 may_use_call = -1;
2003 break;
2004
2005 default:
2006 gcc_unreachable ();
2007 }
2008
2009 gcc_assert (MEM_P (x) && MEM_P (y));
2010 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2011 gcc_assert (align >= BITS_PER_UNIT);
2012
2013 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
2014 block copy is more efficient for other large modes, e.g. DCmode. */
2015 x = adjust_address (x, BLKmode, 0);
2016 y = adjust_address (y, BLKmode, 0);
2017
2018 /* If source and destination are the same, no need to copy anything. */
2019 if (rtx_equal_p (x, y)
2020 && !MEM_VOLATILE_P (x)
2021 && !MEM_VOLATILE_P (y))
2022 return 0;
2023
2024 /* Set MEM_SIZE as appropriate for this block copy. The main place this
2025 can be incorrect is coming from __builtin_memcpy. */
2026 poly_int64 const_size;
2027 if (poly_int_rtx_p (size, &const_size))
2028 {
2029 x = shallow_copy_rtx (x);
2030 y = shallow_copy_rtx (y);
2031 set_mem_size (x, const_size);
2032 set_mem_size (y, const_size);
2033 }
2034
2035 bool pieces_ok = CONST_INT_P (size)
2036 && can_move_by_pieces (INTVAL (size), align);
2037 bool pattern_ok = false;
2038
2039 if (!pieces_ok || might_overlap)
2040 {
2041 pattern_ok
2042 = emit_block_move_via_pattern (x, y, size, align,
2043 expected_align, expected_size,
2044 min_size, max_size, probable_max_size,
2045 might_overlap);
2046 if (!pattern_ok && might_overlap)
2047 {
2048 /* Do not try any of the other methods below as they are not safe
2049 for overlapping moves. */
2050 *is_move_done = false;
2051 return retval;
2052 }
2053 }
2054
2055 if (pattern_ok)
2056 ;
2057 else if (pieces_ok)
2058 move_by_pieces (x, y, INTVAL (size), align, RETURN_BEGIN);
2059 else if (may_use_call && !might_overlap
2060 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
2061 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
2062 {
2063 if (bail_out_libcall)
2064 {
2065 if (is_move_done)
2066 *is_move_done = false;
2067 return retval;
2068 }
2069
2070 if (may_use_call < 0)
2071 return pc_rtx;
2072
2073 retval = emit_block_copy_via_libcall (x, y, size,
2074 method == BLOCK_OP_TAILCALL);
2075 }
2076 else if (might_overlap)
2077 *is_move_done = false;
2078 else
2079 emit_block_move_via_loop (x, y, size, align);
2080
2081 if (method == BLOCK_OP_CALL_PARM)
2082 OK_DEFER_POP;
2083
2084 return retval;
2085 }
2086
2087 rtx
2088 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
2089 {
2090 unsigned HOST_WIDE_INT max, min = 0;
2091 if (GET_CODE (size) == CONST_INT)
2092 min = max = UINTVAL (size);
2093 else
2094 max = GET_MODE_MASK (GET_MODE (size));
2095 return emit_block_move_hints (x, y, size, method, 0, -1,
2096 min, max, max);
2097 }
2098
2099 /* A subroutine of emit_block_move. Returns true if calling the
2100 block move libcall will not clobber any parameters which may have
2101 already been placed on the stack. */
2102
2103 static bool
2104 block_move_libcall_safe_for_call_parm (void)
2105 {
2106 tree fn;
2107
2108 /* If arguments are pushed on the stack, then they're safe. */
2109 if (targetm.calls.push_argument (0))
2110 return true;
2111
2112 /* If registers go on the stack anyway, any argument is sure to clobber
2113 an outgoing argument. */
2114 #if defined (REG_PARM_STACK_SPACE)
2115 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2116 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
2117 depend on its argument. */
2118 (void) fn;
2119 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
2120 && REG_PARM_STACK_SPACE (fn) != 0)
2121 return false;
2122 #endif
2123
2124 /* If any argument goes in memory, then it might clobber an outgoing
2125 argument. */
2126 {
2127 CUMULATIVE_ARGS args_so_far_v;
2128 cumulative_args_t args_so_far;
2129 tree arg;
2130
2131 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2132 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2133 args_so_far = pack_cumulative_args (&args_so_far_v);
2134
2135 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2136 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
2137 {
2138 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2139 function_arg_info arg_info (mode, /*named=*/true);
2140 rtx tmp = targetm.calls.function_arg (args_so_far, arg_info);
2141 if (!tmp || !REG_P (tmp))
2142 return false;
2143 if (targetm.calls.arg_partial_bytes (args_so_far, arg_info))
2144 return false;
2145 targetm.calls.function_arg_advance (args_so_far, arg_info);
2146 }
2147 }
2148 return true;
2149 }
2150
2151 /* A subroutine of emit_block_move. Expand a cpymem or movmem pattern;
2152 return true if successful.
2153
2154 X is the destination of the copy or move.
2155 Y is the source of the copy or move.
2156 SIZE is the size of the block to be moved.
2157
2158 MIGHT_OVERLAP indicates this originated with expansion of a
2159 builtin_memmove() and the source and destination blocks may
2160 overlap.
2161 */
2162
2163 static bool
2164 emit_block_move_via_pattern (rtx x, rtx y, rtx size, unsigned int align,
2165 unsigned int expected_align,
2166 HOST_WIDE_INT expected_size,
2167 unsigned HOST_WIDE_INT min_size,
2168 unsigned HOST_WIDE_INT max_size,
2169 unsigned HOST_WIDE_INT probable_max_size,
2170 bool might_overlap)
2171 {
2172 if (expected_align < align)
2173 expected_align = align;
2174 if (expected_size != -1)
2175 {
2176 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
2177 expected_size = probable_max_size;
2178 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2179 expected_size = min_size;
2180 }
2181
2182 /* Since this is a move insn, we don't care about volatility. */
2183 temporary_volatile_ok v (true);
2184
2185 /* Try the most limited insn first, because there's no point
2186 including more than one in the machine description unless
2187 the more limited one has some advantage. */
2188
2189 opt_scalar_int_mode mode_iter;
2190 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2191 {
2192 scalar_int_mode mode = mode_iter.require ();
2193 enum insn_code code;
2194 if (might_overlap)
2195 code = direct_optab_handler (movmem_optab, mode);
2196 else
2197 code = direct_optab_handler (cpymem_optab, mode);
2198
2199 if (code != CODE_FOR_nothing
2200 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2201 here because if SIZE is less than the mode mask, as it is
2202 returned by the macro, it will definitely be less than the
2203 actual mode mask. Since SIZE is within the Pmode address
2204 space, we limit MODE to Pmode. */
2205 && ((CONST_INT_P (size)
2206 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2207 <= (GET_MODE_MASK (mode) >> 1)))
2208 || max_size <= (GET_MODE_MASK (mode) >> 1)
2209 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2210 {
2211 class expand_operand ops[9];
2212 unsigned int nops;
2213
2214 /* ??? When called via emit_block_move_for_call, it'd be
2215 nice if there were some way to inform the backend, so
2216 that it doesn't fail the expansion because it thinks
2217 emitting the libcall would be more efficient. */
2218 nops = insn_data[(int) code].n_generator_args;
2219 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2220
2221 create_fixed_operand (&ops[0], x);
2222 create_fixed_operand (&ops[1], y);
2223 /* The check above guarantees that this size conversion is valid. */
2224 create_convert_operand_to (&ops[2], size, mode, true);
2225 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2226 if (nops >= 6)
2227 {
2228 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2229 create_integer_operand (&ops[5], expected_size);
2230 }
2231 if (nops >= 8)
2232 {
2233 create_integer_operand (&ops[6], min_size);
2234 /* If we cannot represent the maximal size,
2235 make parameter NULL. */
2236 if ((HOST_WIDE_INT) max_size != -1)
2237 create_integer_operand (&ops[7], max_size);
2238 else
2239 create_fixed_operand (&ops[7], NULL);
2240 }
2241 if (nops == 9)
2242 {
2243 /* If we cannot represent the maximal size,
2244 make parameter NULL. */
2245 if ((HOST_WIDE_INT) probable_max_size != -1)
2246 create_integer_operand (&ops[8], probable_max_size);
2247 else
2248 create_fixed_operand (&ops[8], NULL);
2249 }
2250 if (maybe_expand_insn (code, nops, ops))
2251 return true;
2252 }
2253 }
2254
2255 return false;
2256 }
2257
2258 /* A subroutine of emit_block_move. Copy the data via an explicit
2259 loop. This is used only when libcalls are forbidden. */
2260 /* ??? It'd be nice to copy in hunks larger than QImode. */
2261
2262 static void
2263 emit_block_move_via_loop (rtx x, rtx y, rtx size,
2264 unsigned int align ATTRIBUTE_UNUSED)
2265 {
2266 rtx_code_label *cmp_label, *top_label;
2267 rtx iter, x_addr, y_addr, tmp;
2268 machine_mode x_addr_mode = get_address_mode (x);
2269 machine_mode y_addr_mode = get_address_mode (y);
2270 machine_mode iter_mode;
2271
2272 iter_mode = GET_MODE (size);
2273 if (iter_mode == VOIDmode)
2274 iter_mode = word_mode;
2275
2276 top_label = gen_label_rtx ();
2277 cmp_label = gen_label_rtx ();
2278 iter = gen_reg_rtx (iter_mode);
2279
2280 emit_move_insn (iter, const0_rtx);
2281
2282 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
2283 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
2284 do_pending_stack_adjust ();
2285
2286 emit_jump (cmp_label);
2287 emit_label (top_label);
2288
2289 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
2290 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
2291
2292 if (x_addr_mode != y_addr_mode)
2293 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
2294 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
2295
2296 x = change_address (x, QImode, x_addr);
2297 y = change_address (y, QImode, y_addr);
2298
2299 emit_move_insn (x, y);
2300
2301 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
2302 true, OPTAB_LIB_WIDEN);
2303 if (tmp != iter)
2304 emit_move_insn (iter, tmp);
2305
2306 emit_label (cmp_label);
2307
2308 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
2309 true, top_label,
2310 profile_probability::guessed_always ()
2311 .apply_scale (9, 10));
2312 }
2313 \f
2314 /* Expand a call to memcpy or memmove or memcmp, and return the result.
2315 TAILCALL is true if this is a tail call. */
2316
2317 rtx
2318 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
2319 rtx size, bool tailcall)
2320 {
2321 rtx dst_addr, src_addr;
2322 tree call_expr, dst_tree, src_tree, size_tree;
2323 machine_mode size_mode;
2324
2325 /* Since dst and src are passed to a libcall, mark the corresponding
2326 tree EXPR as addressable. */
2327 tree dst_expr = MEM_EXPR (dst);
2328 tree src_expr = MEM_EXPR (src);
2329 if (dst_expr)
2330 mark_addressable (dst_expr);
2331 if (src_expr)
2332 mark_addressable (src_expr);
2333
2334 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
2335 dst_addr = convert_memory_address (ptr_mode, dst_addr);
2336 dst_tree = make_tree (ptr_type_node, dst_addr);
2337
2338 src_addr = copy_addr_to_reg (XEXP (src, 0));
2339 src_addr = convert_memory_address (ptr_mode, src_addr);
2340 src_tree = make_tree (ptr_type_node, src_addr);
2341
2342 size_mode = TYPE_MODE (sizetype);
2343 size = convert_to_mode (size_mode, size, 1);
2344 size = copy_to_mode_reg (size_mode, size);
2345 size_tree = make_tree (sizetype, size);
2346
2347 /* It is incorrect to use the libcall calling conventions for calls to
2348 memcpy/memmove/memcmp because they can be provided by the user. */
2349 tree fn = builtin_decl_implicit (fncode);
2350 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
2351 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2352
2353 return expand_call (call_expr, NULL_RTX, false);
2354 }
2355
2356 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
2357 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
2358 otherwise return null. */
2359
2360 rtx
2361 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
2362 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
2363 HOST_WIDE_INT align)
2364 {
2365 machine_mode insn_mode = insn_data[icode].operand[0].mode;
2366
2367 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
2368 target = NULL_RTX;
2369
2370 class expand_operand ops[5];
2371 create_output_operand (&ops[0], target, insn_mode);
2372 create_fixed_operand (&ops[1], arg1_rtx);
2373 create_fixed_operand (&ops[2], arg2_rtx);
2374 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
2375 TYPE_UNSIGNED (arg3_type));
2376 create_integer_operand (&ops[4], align);
2377 if (maybe_expand_insn (icode, 5, ops))
2378 return ops[0].value;
2379 return NULL_RTX;
2380 }
2381
2382 /* Expand a block compare between X and Y with length LEN using the
2383 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
2384 of the expression that was used to calculate the length. ALIGN
2385 gives the known minimum common alignment. */
2386
2387 static rtx
2388 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
2389 unsigned align)
2390 {
2391 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
2392 implementing memcmp because it will stop if it encounters two
2393 zero bytes. */
2394 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
2395
2396 if (icode == CODE_FOR_nothing)
2397 return NULL_RTX;
2398
2399 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
2400 }
2401
2402 /* Emit code to compare a block Y to a block X. This may be done with
2403 string-compare instructions, with multiple scalar instructions,
2404 or with a library call.
2405
2406 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
2407 they are. LEN_TYPE is the type of the expression that was used to
2408 calculate it.
2409
2410 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
2411 value of a normal memcmp call, instead we can just compare for equality.
2412 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
2413 returning NULL_RTX.
2414
2415 Optionally, the caller can pass a constfn and associated data in Y_CFN
2416 and Y_CFN_DATA. describing that the second operand being compared is a
2417 known constant and how to obtain its data.
2418 Return the result of the comparison, or NULL_RTX if we failed to
2419 perform the operation. */
2420
2421 rtx
2422 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
2423 bool equality_only, by_pieces_constfn y_cfn,
2424 void *y_cfndata)
2425 {
2426 rtx result = 0;
2427
2428 if (CONST_INT_P (len) && INTVAL (len) == 0)
2429 return const0_rtx;
2430
2431 gcc_assert (MEM_P (x) && MEM_P (y));
2432 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
2433 gcc_assert (align >= BITS_PER_UNIT);
2434
2435 x = adjust_address (x, BLKmode, 0);
2436 y = adjust_address (y, BLKmode, 0);
2437
2438 if (equality_only
2439 && CONST_INT_P (len)
2440 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
2441 result = compare_by_pieces (x, y, INTVAL (len), target, align,
2442 y_cfn, y_cfndata);
2443 else
2444 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
2445
2446 return result;
2447 }
2448 \f
2449 /* Copy all or part of a value X into registers starting at REGNO.
2450 The number of registers to be filled is NREGS. */
2451
2452 void
2453 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2454 {
2455 if (nregs == 0)
2456 return;
2457
2458 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2459 x = validize_mem (force_const_mem (mode, x));
2460
2461 /* See if the machine can do this with a load multiple insn. */
2462 if (targetm.have_load_multiple ())
2463 {
2464 rtx_insn *last = get_last_insn ();
2465 rtx first = gen_rtx_REG (word_mode, regno);
2466 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2467 GEN_INT (nregs)))
2468 {
2469 emit_insn (pat);
2470 return;
2471 }
2472 else
2473 delete_insns_since (last);
2474 }
2475
2476 for (int i = 0; i < nregs; i++)
2477 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2478 operand_subword_force (x, i, mode));
2479 }
2480
2481 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2482 The number of registers to be filled is NREGS. */
2483
2484 void
2485 move_block_from_reg (int regno, rtx x, int nregs)
2486 {
2487 if (nregs == 0)
2488 return;
2489
2490 /* See if the machine can do this with a store multiple insn. */
2491 if (targetm.have_store_multiple ())
2492 {
2493 rtx_insn *last = get_last_insn ();
2494 rtx first = gen_rtx_REG (word_mode, regno);
2495 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2496 GEN_INT (nregs)))
2497 {
2498 emit_insn (pat);
2499 return;
2500 }
2501 else
2502 delete_insns_since (last);
2503 }
2504
2505 for (int i = 0; i < nregs; i++)
2506 {
2507 rtx tem = operand_subword (x, i, 1, BLKmode);
2508
2509 gcc_assert (tem);
2510
2511 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2512 }
2513 }
2514
2515 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2516 ORIG, where ORIG is a non-consecutive group of registers represented by
2517 a PARALLEL. The clone is identical to the original except in that the
2518 original set of registers is replaced by a new set of pseudo registers.
2519 The new set has the same modes as the original set. */
2520
2521 rtx
2522 gen_group_rtx (rtx orig)
2523 {
2524 int i, length;
2525 rtx *tmps;
2526
2527 gcc_assert (GET_CODE (orig) == PARALLEL);
2528
2529 length = XVECLEN (orig, 0);
2530 tmps = XALLOCAVEC (rtx, length);
2531
2532 /* Skip a NULL entry in first slot. */
2533 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2534
2535 if (i)
2536 tmps[0] = 0;
2537
2538 for (; i < length; i++)
2539 {
2540 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2541 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2542
2543 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2544 }
2545
2546 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2547 }
2548
2549 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2550 except that values are placed in TMPS[i], and must later be moved
2551 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2552
2553 static void
2554 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2555 poly_int64 ssize)
2556 {
2557 rtx src;
2558 int start, i;
2559 machine_mode m = GET_MODE (orig_src);
2560
2561 gcc_assert (GET_CODE (dst) == PARALLEL);
2562
2563 if (m != VOIDmode
2564 && !SCALAR_INT_MODE_P (m)
2565 && !MEM_P (orig_src)
2566 && GET_CODE (orig_src) != CONCAT)
2567 {
2568 scalar_int_mode imode;
2569 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2570 {
2571 src = gen_reg_rtx (imode);
2572 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2573 }
2574 else
2575 {
2576 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2577 emit_move_insn (src, orig_src);
2578 }
2579 emit_group_load_1 (tmps, dst, src, type, ssize);
2580 return;
2581 }
2582
2583 /* Check for a NULL entry, used to indicate that the parameter goes
2584 both on the stack and in registers. */
2585 if (XEXP (XVECEXP (dst, 0, 0), 0))
2586 start = 0;
2587 else
2588 start = 1;
2589
2590 /* Process the pieces. */
2591 for (i = start; i < XVECLEN (dst, 0); i++)
2592 {
2593 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2594 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (dst, 0, i), 1));
2595 poly_int64 bytelen = GET_MODE_SIZE (mode);
2596 poly_int64 shift = 0;
2597
2598 /* Handle trailing fragments that run over the size of the struct.
2599 It's the target's responsibility to make sure that the fragment
2600 cannot be strictly smaller in some cases and strictly larger
2601 in others. */
2602 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2603 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2604 {
2605 /* Arrange to shift the fragment to where it belongs.
2606 extract_bit_field loads to the lsb of the reg. */
2607 if (
2608 #ifdef BLOCK_REG_PADDING
2609 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2610 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2611 #else
2612 BYTES_BIG_ENDIAN
2613 #endif
2614 )
2615 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2616 bytelen = ssize - bytepos;
2617 gcc_assert (maybe_gt (bytelen, 0));
2618 }
2619
2620 /* If we won't be loading directly from memory, protect the real source
2621 from strange tricks we might play; but make sure that the source can
2622 be loaded directly into the destination. */
2623 src = orig_src;
2624 if (!MEM_P (orig_src)
2625 && (!CONSTANT_P (orig_src)
2626 || (GET_MODE (orig_src) != mode
2627 && GET_MODE (orig_src) != VOIDmode)))
2628 {
2629 if (GET_MODE (orig_src) == VOIDmode)
2630 src = gen_reg_rtx (mode);
2631 else
2632 src = gen_reg_rtx (GET_MODE (orig_src));
2633
2634 emit_move_insn (src, orig_src);
2635 }
2636
2637 /* Optimize the access just a bit. */
2638 if (MEM_P (src)
2639 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2640 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2641 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2642 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2643 {
2644 tmps[i] = gen_reg_rtx (mode);
2645 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2646 }
2647 else if (COMPLEX_MODE_P (mode)
2648 && GET_MODE (src) == mode
2649 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2650 /* Let emit_move_complex do the bulk of the work. */
2651 tmps[i] = src;
2652 else if (GET_CODE (src) == CONCAT)
2653 {
2654 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2655 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2656 unsigned int elt;
2657 poly_int64 subpos;
2658
2659 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2660 && known_le (subpos + bytelen, slen0))
2661 {
2662 /* The following assumes that the concatenated objects all
2663 have the same size. In this case, a simple calculation
2664 can be used to determine the object and the bit field
2665 to be extracted. */
2666 tmps[i] = XEXP (src, elt);
2667 if (maybe_ne (subpos, 0)
2668 || maybe_ne (subpos + bytelen, slen0)
2669 || (!CONSTANT_P (tmps[i])
2670 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2671 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2672 subpos * BITS_PER_UNIT,
2673 1, NULL_RTX, mode, mode, false,
2674 NULL);
2675 }
2676 else
2677 {
2678 rtx mem;
2679
2680 gcc_assert (known_eq (bytepos, 0));
2681 mem = assign_stack_temp (GET_MODE (src), slen);
2682 emit_move_insn (mem, src);
2683 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2684 0, 1, NULL_RTX, mode, mode, false,
2685 NULL);
2686 }
2687 }
2688 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2689 && XVECLEN (dst, 0) > 1)
2690 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2691 else if (CONSTANT_P (src))
2692 {
2693 if (known_eq (bytelen, ssize))
2694 tmps[i] = src;
2695 else
2696 {
2697 rtx first, second;
2698
2699 /* TODO: const_wide_int can have sizes other than this... */
2700 gcc_assert (known_eq (2 * bytelen, ssize));
2701 split_double (src, &first, &second);
2702 if (i)
2703 tmps[i] = second;
2704 else
2705 tmps[i] = first;
2706 }
2707 }
2708 else if (REG_P (src) && GET_MODE (src) == mode)
2709 tmps[i] = src;
2710 else
2711 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2712 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2713 mode, mode, false, NULL);
2714
2715 if (maybe_ne (shift, 0))
2716 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2717 shift, tmps[i], 0);
2718 }
2719 }
2720
2721 /* Emit code to move a block SRC of type TYPE to a block DST,
2722 where DST is non-consecutive registers represented by a PARALLEL.
2723 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2724 if not known. */
2725
2726 void
2727 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2728 {
2729 rtx *tmps;
2730 int i;
2731
2732 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2733 emit_group_load_1 (tmps, dst, src, type, ssize);
2734
2735 /* Copy the extracted pieces into the proper (probable) hard regs. */
2736 for (i = 0; i < XVECLEN (dst, 0); i++)
2737 {
2738 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2739 if (d == NULL)
2740 continue;
2741 emit_move_insn (d, tmps[i]);
2742 }
2743 }
2744
2745 /* Similar, but load SRC into new pseudos in a format that looks like
2746 PARALLEL. This can later be fed to emit_group_move to get things
2747 in the right place. */
2748
2749 rtx
2750 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2751 {
2752 rtvec vec;
2753 int i;
2754
2755 vec = rtvec_alloc (XVECLEN (parallel, 0));
2756 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2757
2758 /* Convert the vector to look just like the original PARALLEL, except
2759 with the computed values. */
2760 for (i = 0; i < XVECLEN (parallel, 0); i++)
2761 {
2762 rtx e = XVECEXP (parallel, 0, i);
2763 rtx d = XEXP (e, 0);
2764
2765 if (d)
2766 {
2767 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2768 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2769 }
2770 RTVEC_ELT (vec, i) = e;
2771 }
2772
2773 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2774 }
2775
2776 /* Emit code to move a block SRC to block DST, where SRC and DST are
2777 non-consecutive groups of registers, each represented by a PARALLEL. */
2778
2779 void
2780 emit_group_move (rtx dst, rtx src)
2781 {
2782 int i;
2783
2784 gcc_assert (GET_CODE (src) == PARALLEL
2785 && GET_CODE (dst) == PARALLEL
2786 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2787
2788 /* Skip first entry if NULL. */
2789 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2790 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2791 XEXP (XVECEXP (src, 0, i), 0));
2792 }
2793
2794 /* Move a group of registers represented by a PARALLEL into pseudos. */
2795
2796 rtx
2797 emit_group_move_into_temps (rtx src)
2798 {
2799 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2800 int i;
2801
2802 for (i = 0; i < XVECLEN (src, 0); i++)
2803 {
2804 rtx e = XVECEXP (src, 0, i);
2805 rtx d = XEXP (e, 0);
2806
2807 if (d)
2808 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2809 RTVEC_ELT (vec, i) = e;
2810 }
2811
2812 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2813 }
2814
2815 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2816 where SRC is non-consecutive registers represented by a PARALLEL.
2817 SSIZE represents the total size of block ORIG_DST, or -1 if not
2818 known. */
2819
2820 void
2821 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2822 poly_int64 ssize)
2823 {
2824 rtx *tmps, dst;
2825 int start, finish, i;
2826 machine_mode m = GET_MODE (orig_dst);
2827
2828 gcc_assert (GET_CODE (src) == PARALLEL);
2829
2830 if (!SCALAR_INT_MODE_P (m)
2831 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2832 {
2833 scalar_int_mode imode;
2834 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2835 {
2836 dst = gen_reg_rtx (imode);
2837 emit_group_store (dst, src, type, ssize);
2838 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2839 }
2840 else
2841 {
2842 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2843 emit_group_store (dst, src, type, ssize);
2844 }
2845 emit_move_insn (orig_dst, dst);
2846 return;
2847 }
2848
2849 /* Check for a NULL entry, used to indicate that the parameter goes
2850 both on the stack and in registers. */
2851 if (XEXP (XVECEXP (src, 0, 0), 0))
2852 start = 0;
2853 else
2854 start = 1;
2855 finish = XVECLEN (src, 0);
2856
2857 tmps = XALLOCAVEC (rtx, finish);
2858
2859 /* Copy the (probable) hard regs into pseudos. */
2860 for (i = start; i < finish; i++)
2861 {
2862 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2863 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2864 {
2865 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2866 emit_move_insn (tmps[i], reg);
2867 }
2868 else
2869 tmps[i] = reg;
2870 }
2871
2872 /* If we won't be storing directly into memory, protect the real destination
2873 from strange tricks we might play. */
2874 dst = orig_dst;
2875 if (GET_CODE (dst) == PARALLEL)
2876 {
2877 rtx temp;
2878
2879 /* We can get a PARALLEL dst if there is a conditional expression in
2880 a return statement. In that case, the dst and src are the same,
2881 so no action is necessary. */
2882 if (rtx_equal_p (dst, src))
2883 return;
2884
2885 /* It is unclear if we can ever reach here, but we may as well handle
2886 it. Allocate a temporary, and split this into a store/load to/from
2887 the temporary. */
2888 temp = assign_stack_temp (GET_MODE (dst), ssize);
2889 emit_group_store (temp, src, type, ssize);
2890 emit_group_load (dst, temp, type, ssize);
2891 return;
2892 }
2893 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2894 {
2895 machine_mode outer = GET_MODE (dst);
2896 machine_mode inner;
2897 poly_int64 bytepos;
2898 bool done = false;
2899 rtx temp;
2900
2901 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2902 dst = gen_reg_rtx (outer);
2903
2904 /* Make life a bit easier for combine: if the first element of the
2905 vector is the word (or larger) low part of the destination mode,
2906 use a paradoxical subreg to initialize the destination. */
2907 if (start < finish)
2908 {
2909 inner = GET_MODE (tmps[start]);
2910 bytepos = subreg_lowpart_offset (inner, outer);
2911 if (known_ge (GET_MODE_BITSIZE (inner), BITS_PER_WORD)
2912 && known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2913 start), 1)),
2914 bytepos))
2915 {
2916 temp = simplify_gen_subreg (outer, tmps[start], inner, 0);
2917 if (temp)
2918 {
2919 emit_move_insn (dst, temp);
2920 done = true;
2921 start++;
2922 }
2923 }
2924 }
2925
2926 /* If the first element wasn't the low part, try the last. */
2927 if (!done
2928 && start < finish - 1)
2929 {
2930 inner = GET_MODE (tmps[finish - 1]);
2931 bytepos = subreg_lowpart_offset (inner, outer);
2932 if (known_ge (GET_MODE_BITSIZE (inner), BITS_PER_WORD)
2933 && known_eq (rtx_to_poly_int64 (XEXP (XVECEXP (src, 0,
2934 finish - 1), 1)),
2935 bytepos))
2936 {
2937 temp = simplify_gen_subreg (outer, tmps[finish - 1], inner, 0);
2938 if (temp)
2939 {
2940 emit_move_insn (dst, temp);
2941 done = true;
2942 finish--;
2943 }
2944 }
2945 }
2946
2947 /* Otherwise, simply initialize the result to zero. */
2948 if (!done)
2949 emit_move_insn (dst, CONST0_RTX (outer));
2950 }
2951
2952 /* Process the pieces. */
2953 for (i = start; i < finish; i++)
2954 {
2955 poly_int64 bytepos = rtx_to_poly_int64 (XEXP (XVECEXP (src, 0, i), 1));
2956 machine_mode mode = GET_MODE (tmps[i]);
2957 poly_int64 bytelen = GET_MODE_SIZE (mode);
2958 poly_uint64 adj_bytelen;
2959 rtx dest = dst;
2960
2961 /* Handle trailing fragments that run over the size of the struct.
2962 It's the target's responsibility to make sure that the fragment
2963 cannot be strictly smaller in some cases and strictly larger
2964 in others. */
2965 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2966 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2967 adj_bytelen = ssize - bytepos;
2968 else
2969 adj_bytelen = bytelen;
2970
2971 /* Deal with destination CONCATs by either storing into one of the parts
2972 or doing a copy after storing into a register or stack temporary. */
2973 if (GET_CODE (dst) == CONCAT)
2974 {
2975 if (known_le (bytepos + adj_bytelen,
2976 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2977 dest = XEXP (dst, 0);
2978
2979 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2980 {
2981 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2982 dest = XEXP (dst, 1);
2983 }
2984
2985 else
2986 {
2987 machine_mode dest_mode = GET_MODE (dest);
2988 machine_mode tmp_mode = GET_MODE (tmps[i]);
2989 scalar_int_mode dest_imode;
2990
2991 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2992
2993 /* If the source is a single scalar integer register, and the
2994 destination has a complex mode for which a same-sized integer
2995 mode exists, then we can take the left-justified part of the
2996 source in the complex mode. */
2997 if (finish == start + 1
2998 && REG_P (tmps[i])
2999 && SCALAR_INT_MODE_P (tmp_mode)
3000 && COMPLEX_MODE_P (dest_mode)
3001 && int_mode_for_mode (dest_mode).exists (&dest_imode))
3002 {
3003 const scalar_int_mode tmp_imode
3004 = as_a <scalar_int_mode> (tmp_mode);
3005
3006 if (GET_MODE_BITSIZE (dest_imode)
3007 < GET_MODE_BITSIZE (tmp_imode))
3008 {
3009 dest = gen_reg_rtx (dest_imode);
3010 if (BYTES_BIG_ENDIAN)
3011 tmps[i] = expand_shift (RSHIFT_EXPR, tmp_mode, tmps[i],
3012 GET_MODE_BITSIZE (tmp_imode)
3013 - GET_MODE_BITSIZE (dest_imode),
3014 NULL_RTX, 1);
3015 emit_move_insn (dest, gen_lowpart (dest_imode, tmps[i]));
3016 dst = gen_lowpart (dest_mode, dest);
3017 }
3018 else
3019 dst = gen_lowpart (dest_mode, tmps[i]);
3020 }
3021
3022 /* Otherwise spill the source onto the stack using the more
3023 aligned of the two modes. */
3024 else if (GET_MODE_ALIGNMENT (dest_mode)
3025 >= GET_MODE_ALIGNMENT (tmp_mode))
3026 {
3027 dest = assign_stack_temp (dest_mode,
3028 GET_MODE_SIZE (dest_mode));
3029 emit_move_insn (adjust_address (dest, tmp_mode, bytepos),
3030 tmps[i]);
3031 dst = dest;
3032 }
3033
3034 else
3035 {
3036 dest = assign_stack_temp (tmp_mode,
3037 GET_MODE_SIZE (tmp_mode));
3038 emit_move_insn (dest, tmps[i]);
3039 dst = adjust_address (dest, dest_mode, bytepos);
3040 }
3041
3042 break;
3043 }
3044 }
3045
3046 /* Handle trailing fragments that run over the size of the struct. */
3047 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
3048 {
3049 /* store_bit_field always takes its value from the lsb.
3050 Move the fragment to the lsb if it's not already there. */
3051 if (
3052 #ifdef BLOCK_REG_PADDING
3053 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
3054 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
3055 #else
3056 BYTES_BIG_ENDIAN
3057 #endif
3058 )
3059 {
3060 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
3061 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
3062 shift, tmps[i], 0);
3063 }
3064
3065 /* Make sure not to write past the end of the struct. */
3066 store_bit_field (dest,
3067 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3068 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
3069 VOIDmode, tmps[i], false, false);
3070 }
3071
3072 /* Optimize the access just a bit. */
3073 else if (MEM_P (dest)
3074 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
3075 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
3076 && multiple_p (bytepos * BITS_PER_UNIT,
3077 GET_MODE_ALIGNMENT (mode))
3078 && known_eq (bytelen, GET_MODE_SIZE (mode)))
3079 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
3080
3081 else
3082 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
3083 0, 0, mode, tmps[i], false, false);
3084 }
3085
3086 /* Copy from the pseudo into the (probable) hard reg. */
3087 if (orig_dst != dst)
3088 emit_move_insn (orig_dst, dst);
3089 }
3090
3091 /* Return a form of X that does not use a PARALLEL. TYPE is the type
3092 of the value stored in X. */
3093
3094 rtx
3095 maybe_emit_group_store (rtx x, tree type)
3096 {
3097 machine_mode mode = TYPE_MODE (type);
3098 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
3099 if (GET_CODE (x) == PARALLEL)
3100 {
3101 rtx result = gen_reg_rtx (mode);
3102 emit_group_store (result, x, type, int_size_in_bytes (type));
3103 return result;
3104 }
3105 return x;
3106 }
3107
3108 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
3109
3110 This is used on targets that return BLKmode values in registers. */
3111
3112 static void
3113 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
3114 {
3115 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
3116 rtx src = NULL, dst = NULL;
3117 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
3118 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
3119 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3120 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
3121 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
3122 fixed_size_mode copy_mode;
3123
3124 /* BLKmode registers created in the back-end shouldn't have survived. */
3125 gcc_assert (mode != BLKmode);
3126
3127 /* If the structure doesn't take up a whole number of words, see whether
3128 SRCREG is padded on the left or on the right. If it's on the left,
3129 set PADDING_CORRECTION to the number of bits to skip.
3130
3131 In most ABIs, the structure will be returned at the least end of
3132 the register, which translates to right padding on little-endian
3133 targets and left padding on big-endian targets. The opposite
3134 holds if the structure is returned at the most significant
3135 end of the register. */
3136 if (bytes % UNITS_PER_WORD != 0
3137 && (targetm.calls.return_in_msb (type)
3138 ? !BYTES_BIG_ENDIAN
3139 : BYTES_BIG_ENDIAN))
3140 padding_correction
3141 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
3142
3143 /* We can use a single move if we have an exact mode for the size. */
3144 else if (MEM_P (target)
3145 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
3146 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
3147 && bytes == GET_MODE_SIZE (mode))
3148 {
3149 emit_move_insn (adjust_address (target, mode, 0), srcreg);
3150 return;
3151 }
3152
3153 /* And if we additionally have the same mode for a register. */
3154 else if (REG_P (target)
3155 && GET_MODE (target) == mode
3156 && bytes == GET_MODE_SIZE (mode))
3157 {
3158 emit_move_insn (target, srcreg);
3159 return;
3160 }
3161
3162 /* This code assumes srcreg is at least a full word. If it isn't, copy it
3163 into a new pseudo which is a full word. */
3164 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3165 {
3166 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
3167 mode = word_mode;
3168 }
3169
3170 /* Copy the structure BITSIZE bits at a time. If the target lives in
3171 memory, take care of not reading/writing past its end by selecting
3172 a copy mode suited to BITSIZE. This should always be possible given
3173 how it is computed.
3174
3175 If the target lives in register, make sure not to select a copy mode
3176 larger than the mode of the register.
3177
3178 We could probably emit more efficient code for machines which do not use
3179 strict alignment, but it doesn't seem worth the effort at the current
3180 time. */
3181
3182 copy_mode = word_mode;
3183 if (MEM_P (target))
3184 {
3185 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
3186 if (mem_mode.exists ())
3187 copy_mode = mem_mode.require ();
3188 }
3189 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3190 copy_mode = tmode;
3191
3192 for (bitpos = 0, xbitpos = padding_correction;
3193 bitpos < bytes * BITS_PER_UNIT;
3194 bitpos += bitsize, xbitpos += bitsize)
3195 {
3196 /* We need a new source operand each time xbitpos is on a
3197 word boundary and when xbitpos == padding_correction
3198 (the first time through). */
3199 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
3200 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
3201
3202 /* We need a new destination operand each time bitpos is on
3203 a word boundary. */
3204 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
3205 dst = target;
3206 else if (bitpos % BITS_PER_WORD == 0)
3207 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
3208
3209 /* Use xbitpos for the source extraction (right justified) and
3210 bitpos for the destination store (left justified). */
3211 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
3212 extract_bit_field (src, bitsize,
3213 xbitpos % BITS_PER_WORD, 1,
3214 NULL_RTX, copy_mode, copy_mode,
3215 false, NULL),
3216 false, false);
3217 }
3218 }
3219
3220 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
3221 register if it contains any data, otherwise return null.
3222
3223 This is used on targets that return BLKmode values in registers. */
3224
3225 rtx
3226 copy_blkmode_to_reg (machine_mode mode_in, tree src)
3227 {
3228 int i, n_regs;
3229 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
3230 unsigned int bitsize;
3231 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
3232 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
3233 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
3234 fixed_size_mode dst_mode;
3235 scalar_int_mode min_mode;
3236
3237 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
3238
3239 x = expand_normal (src);
3240
3241 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
3242 if (bytes == 0)
3243 return NULL_RTX;
3244
3245 /* If the structure doesn't take up a whole number of words, see
3246 whether the register value should be padded on the left or on
3247 the right. Set PADDING_CORRECTION to the number of padding
3248 bits needed on the left side.
3249
3250 In most ABIs, the structure will be returned at the least end of
3251 the register, which translates to right padding on little-endian
3252 targets and left padding on big-endian targets. The opposite
3253 holds if the structure is returned at the most significant
3254 end of the register. */
3255 if (bytes % UNITS_PER_WORD != 0
3256 && (targetm.calls.return_in_msb (TREE_TYPE (src))
3257 ? !BYTES_BIG_ENDIAN
3258 : BYTES_BIG_ENDIAN))
3259 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3260 * BITS_PER_UNIT));
3261
3262 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3263 dst_words = XALLOCAVEC (rtx, n_regs);
3264 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
3265 min_mode = smallest_int_mode_for_size (bitsize);
3266
3267 /* Copy the structure BITSIZE bits at a time. */
3268 for (bitpos = 0, xbitpos = padding_correction;
3269 bitpos < bytes * BITS_PER_UNIT;
3270 bitpos += bitsize, xbitpos += bitsize)
3271 {
3272 /* We need a new destination pseudo each time xbitpos is
3273 on a word boundary and when xbitpos == padding_correction
3274 (the first time through). */
3275 if (xbitpos % BITS_PER_WORD == 0
3276 || xbitpos == padding_correction)
3277 {
3278 /* Generate an appropriate register. */
3279 dst_word = gen_reg_rtx (word_mode);
3280 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
3281
3282 /* Clear the destination before we move anything into it. */
3283 emit_move_insn (dst_word, CONST0_RTX (word_mode));
3284 }
3285
3286 /* Find the largest integer mode that can be used to copy all or as
3287 many bits as possible of the structure if the target supports larger
3288 copies. There are too many corner cases here w.r.t to alignments on
3289 the read/writes. So if there is any padding just use single byte
3290 operations. */
3291 opt_scalar_int_mode mode_iter;
3292 if (padding_correction == 0 && !STRICT_ALIGNMENT)
3293 {
3294 FOR_EACH_MODE_FROM (mode_iter, min_mode)
3295 {
3296 unsigned int msize = GET_MODE_BITSIZE (mode_iter.require ());
3297 if (msize <= ((bytes * BITS_PER_UNIT) - bitpos)
3298 && msize <= BITS_PER_WORD)
3299 bitsize = msize;
3300 else
3301 break;
3302 }
3303 }
3304
3305 /* We need a new source operand each time bitpos is on a word
3306 boundary. */
3307 if (bitpos % BITS_PER_WORD == 0)
3308 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
3309
3310 /* Use bitpos for the source extraction (left justified) and
3311 xbitpos for the destination store (right justified). */
3312 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
3313 0, 0, word_mode,
3314 extract_bit_field (src_word, bitsize,
3315 bitpos % BITS_PER_WORD, 1,
3316 NULL_RTX, word_mode, word_mode,
3317 false, NULL),
3318 false, false);
3319 }
3320
3321 if (mode == BLKmode)
3322 {
3323 /* Find the smallest integer mode large enough to hold the
3324 entire structure. */
3325 opt_scalar_int_mode mode_iter;
3326 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3327 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
3328 break;
3329
3330 /* A suitable mode should have been found. */
3331 mode = mode_iter.require ();
3332 }
3333
3334 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3335 dst_mode = word_mode;
3336 else
3337 dst_mode = mode;
3338 dst = gen_reg_rtx (dst_mode);
3339
3340 for (i = 0; i < n_regs; i++)
3341 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
3342
3343 if (mode != dst_mode)
3344 dst = gen_lowpart (mode, dst);
3345
3346 return dst;
3347 }
3348
3349 /* Add a USE expression for REG to the (possibly empty) list pointed
3350 to by CALL_FUSAGE. REG must denote a hard register. */
3351
3352 void
3353 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3354 {
3355 gcc_assert (REG_P (reg));
3356
3357 if (!HARD_REGISTER_P (reg))
3358 return;
3359
3360 *call_fusage
3361 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
3362 }
3363
3364 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
3365 to by CALL_FUSAGE. REG must denote a hard register. */
3366
3367 void
3368 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
3369 {
3370 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
3371
3372 *call_fusage
3373 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
3374 }
3375
3376 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
3377 starting at REGNO. All of these registers must be hard registers. */
3378
3379 void
3380 use_regs (rtx *call_fusage, int regno, int nregs)
3381 {
3382 int i;
3383
3384 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
3385
3386 for (i = 0; i < nregs; i++)
3387 use_reg (call_fusage, regno_reg_rtx[regno + i]);
3388 }
3389
3390 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
3391 PARALLEL REGS. This is for calls that pass values in multiple
3392 non-contiguous locations. The Irix 6 ABI has examples of this. */
3393
3394 void
3395 use_group_regs (rtx *call_fusage, rtx regs)
3396 {
3397 int i;
3398
3399 for (i = 0; i < XVECLEN (regs, 0); i++)
3400 {
3401 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
3402
3403 /* A NULL entry means the parameter goes both on the stack and in
3404 registers. This can also be a MEM for targets that pass values
3405 partially on the stack and partially in registers. */
3406 if (reg != 0 && REG_P (reg))
3407 use_reg (call_fusage, reg);
3408 }
3409 }
3410
3411 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3412 assigment and the code of the expresion on the RHS is CODE. Return
3413 NULL otherwise. */
3414
3415 static gimple *
3416 get_def_for_expr (tree name, enum tree_code code)
3417 {
3418 gimple *def_stmt;
3419
3420 if (TREE_CODE (name) != SSA_NAME)
3421 return NULL;
3422
3423 def_stmt = get_gimple_for_ssa_name (name);
3424 if (!def_stmt
3425 || gimple_assign_rhs_code (def_stmt) != code)
3426 return NULL;
3427
3428 return def_stmt;
3429 }
3430
3431 /* Return the defining gimple statement for SSA_NAME NAME if it is an
3432 assigment and the class of the expresion on the RHS is CLASS. Return
3433 NULL otherwise. */
3434
3435 static gimple *
3436 get_def_for_expr_class (tree name, enum tree_code_class tclass)
3437 {
3438 gimple *def_stmt;
3439
3440 if (TREE_CODE (name) != SSA_NAME)
3441 return NULL;
3442
3443 def_stmt = get_gimple_for_ssa_name (name);
3444 if (!def_stmt
3445 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
3446 return NULL;
3447
3448 return def_stmt;
3449 }
3450 \f
3451 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
3452 its length in bytes. */
3453
3454 rtx
3455 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
3456 unsigned int expected_align, HOST_WIDE_INT expected_size,
3457 unsigned HOST_WIDE_INT min_size,
3458 unsigned HOST_WIDE_INT max_size,
3459 unsigned HOST_WIDE_INT probable_max_size,
3460 unsigned ctz_size)
3461 {
3462 machine_mode mode = GET_MODE (object);
3463 unsigned int align;
3464
3465 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
3466
3467 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
3468 just move a zero. Otherwise, do this a piece at a time. */
3469 poly_int64 size_val;
3470 if (mode != BLKmode
3471 && poly_int_rtx_p (size, &size_val)
3472 && known_eq (size_val, GET_MODE_SIZE (mode)))
3473 {
3474 rtx zero = CONST0_RTX (mode);
3475 if (zero != NULL)
3476 {
3477 emit_move_insn (object, zero);
3478 return NULL;
3479 }
3480
3481 if (COMPLEX_MODE_P (mode))
3482 {
3483 zero = CONST0_RTX (GET_MODE_INNER (mode));
3484 if (zero != NULL)
3485 {
3486 write_complex_part (object, zero, 0, true);
3487 write_complex_part (object, zero, 1, false);
3488 return NULL;
3489 }
3490 }
3491 }
3492
3493 if (size == const0_rtx)
3494 return NULL;
3495
3496 align = MEM_ALIGN (object);
3497
3498 if (CONST_INT_P (size)
3499 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3500 CLEAR_BY_PIECES,
3501 optimize_insn_for_speed_p ()))
3502 clear_by_pieces (object, INTVAL (size), align);
3503 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3504 expected_align, expected_size,
3505 min_size, max_size, probable_max_size))
3506 ;
3507 else if (try_store_by_multiple_pieces (object, size, ctz_size,
3508 min_size, max_size,
3509 NULL_RTX, 0, align))
3510 ;
3511 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3512 return set_storage_via_libcall (object, size, const0_rtx,
3513 method == BLOCK_OP_TAILCALL);
3514 else
3515 gcc_unreachable ();
3516
3517 return NULL;
3518 }
3519
3520 rtx
3521 clear_storage (rtx object, rtx size, enum block_op_methods method)
3522 {
3523 unsigned HOST_WIDE_INT max, min = 0;
3524 if (GET_CODE (size) == CONST_INT)
3525 min = max = UINTVAL (size);
3526 else
3527 max = GET_MODE_MASK (GET_MODE (size));
3528 return clear_storage_hints (object, size, method, 0, -1, min, max, max, 0);
3529 }
3530
3531
3532 /* A subroutine of clear_storage. Expand a call to memset.
3533 Return the return value of memset, 0 otherwise. */
3534
3535 rtx
3536 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3537 {
3538 tree call_expr, fn, object_tree, size_tree, val_tree;
3539 machine_mode size_mode;
3540
3541 object = copy_addr_to_reg (XEXP (object, 0));
3542 object_tree = make_tree (ptr_type_node, object);
3543
3544 if (!CONST_INT_P (val))
3545 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3546 val_tree = make_tree (integer_type_node, val);
3547
3548 size_mode = TYPE_MODE (sizetype);
3549 size = convert_to_mode (size_mode, size, 1);
3550 size = copy_to_mode_reg (size_mode, size);
3551 size_tree = make_tree (sizetype, size);
3552
3553 /* It is incorrect to use the libcall calling conventions for calls to
3554 memset because it can be provided by the user. */
3555 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3556 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3557 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3558
3559 return expand_call (call_expr, NULL_RTX, false);
3560 }
3561 \f
3562 /* Expand a setmem pattern; return true if successful. */
3563
3564 bool
3565 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3566 unsigned int expected_align, HOST_WIDE_INT expected_size,
3567 unsigned HOST_WIDE_INT min_size,
3568 unsigned HOST_WIDE_INT max_size,
3569 unsigned HOST_WIDE_INT probable_max_size)
3570 {
3571 /* Try the most limited insn first, because there's no point
3572 including more than one in the machine description unless
3573 the more limited one has some advantage. */
3574
3575 if (expected_align < align)
3576 expected_align = align;
3577 if (expected_size != -1)
3578 {
3579 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3580 expected_size = max_size;
3581 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3582 expected_size = min_size;
3583 }
3584
3585 opt_scalar_int_mode mode_iter;
3586 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3587 {
3588 scalar_int_mode mode = mode_iter.require ();
3589 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3590
3591 if (code != CODE_FOR_nothing
3592 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3593 here because if SIZE is less than the mode mask, as it is
3594 returned by the macro, it will definitely be less than the
3595 actual mode mask. Since SIZE is within the Pmode address
3596 space, we limit MODE to Pmode. */
3597 && ((CONST_INT_P (size)
3598 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3599 <= (GET_MODE_MASK (mode) >> 1)))
3600 || max_size <= (GET_MODE_MASK (mode) >> 1)
3601 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3602 {
3603 class expand_operand ops[9];
3604 unsigned int nops;
3605
3606 nops = insn_data[(int) code].n_generator_args;
3607 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3608
3609 create_fixed_operand (&ops[0], object);
3610 /* The check above guarantees that this size conversion is valid. */
3611 create_convert_operand_to (&ops[1], size, mode, true);
3612 create_convert_operand_from (&ops[2], val, byte_mode, true);
3613 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3614 if (nops >= 6)
3615 {
3616 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3617 create_integer_operand (&ops[5], expected_size);
3618 }
3619 if (nops >= 8)
3620 {
3621 create_integer_operand (&ops[6], min_size);
3622 /* If we cannot represent the maximal size,
3623 make parameter NULL. */
3624 if ((HOST_WIDE_INT) max_size != -1)
3625 create_integer_operand (&ops[7], max_size);
3626 else
3627 create_fixed_operand (&ops[7], NULL);
3628 }
3629 if (nops == 9)
3630 {
3631 /* If we cannot represent the maximal size,
3632 make parameter NULL. */
3633 if ((HOST_WIDE_INT) probable_max_size != -1)
3634 create_integer_operand (&ops[8], probable_max_size);
3635 else
3636 create_fixed_operand (&ops[8], NULL);
3637 }
3638 if (maybe_expand_insn (code, nops, ops))
3639 return true;
3640 }
3641 }
3642
3643 return false;
3644 }
3645
3646 \f
3647 /* Write to one of the components of the complex value CPLX. Write VAL to
3648 the real part if IMAG_P is false, and the imaginary part if its true.
3649 If UNDEFINED_P then the value in CPLX is currently undefined. */
3650
3651 void
3652 write_complex_part (rtx cplx, rtx val, bool imag_p, bool undefined_p)
3653 {
3654 machine_mode cmode;
3655 scalar_mode imode;
3656 unsigned ibitsize;
3657
3658 if (GET_CODE (cplx) == CONCAT)
3659 {
3660 emit_move_insn (XEXP (cplx, imag_p), val);
3661 return;
3662 }
3663
3664 cmode = GET_MODE (cplx);
3665 imode = GET_MODE_INNER (cmode);
3666 ibitsize = GET_MODE_BITSIZE (imode);
3667
3668 /* For MEMs simplify_gen_subreg may generate an invalid new address
3669 because, e.g., the original address is considered mode-dependent
3670 by the target, which restricts simplify_subreg from invoking
3671 adjust_address_nv. Instead of preparing fallback support for an
3672 invalid address, we call adjust_address_nv directly. */
3673 if (MEM_P (cplx))
3674 {
3675 emit_move_insn (adjust_address_nv (cplx, imode,
3676 imag_p ? GET_MODE_SIZE (imode) : 0),
3677 val);
3678 return;
3679 }
3680
3681 /* If the sub-object is at least word sized, then we know that subregging
3682 will work. This special case is important, since store_bit_field
3683 wants to operate on integer modes, and there's rarely an OImode to
3684 correspond to TCmode. */
3685 if (ibitsize >= BITS_PER_WORD
3686 /* For hard regs we have exact predicates. Assume we can split
3687 the original object if it spans an even number of hard regs.
3688 This special case is important for SCmode on 64-bit platforms
3689 where the natural size of floating-point regs is 32-bit. */
3690 || (REG_P (cplx)
3691 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3692 && REG_NREGS (cplx) % 2 == 0))
3693 {
3694 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3695 imag_p ? GET_MODE_SIZE (imode) : 0);
3696 if (part)
3697 {
3698 emit_move_insn (part, val);
3699 return;
3700 }
3701 else
3702 /* simplify_gen_subreg may fail for sub-word MEMs. */
3703 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3704 }
3705
3706 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3707 false, undefined_p);
3708 }
3709
3710 /* Extract one of the components of the complex value CPLX. Extract the
3711 real part if IMAG_P is false, and the imaginary part if it's true. */
3712
3713 rtx
3714 read_complex_part (rtx cplx, bool imag_p)
3715 {
3716 machine_mode cmode;
3717 scalar_mode imode;
3718 unsigned ibitsize;
3719
3720 if (GET_CODE (cplx) == CONCAT)
3721 return XEXP (cplx, imag_p);
3722
3723 cmode = GET_MODE (cplx);
3724 imode = GET_MODE_INNER (cmode);
3725 ibitsize = GET_MODE_BITSIZE (imode);
3726
3727 /* Special case reads from complex constants that got spilled to memory. */
3728 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3729 {
3730 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3731 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3732 {
3733 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3734 if (CONSTANT_CLASS_P (part))
3735 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3736 }
3737 }
3738
3739 /* For MEMs simplify_gen_subreg may generate an invalid new address
3740 because, e.g., the original address is considered mode-dependent
3741 by the target, which restricts simplify_subreg from invoking
3742 adjust_address_nv. Instead of preparing fallback support for an
3743 invalid address, we call adjust_address_nv directly. */
3744 if (MEM_P (cplx))
3745 return adjust_address_nv (cplx, imode,
3746 imag_p ? GET_MODE_SIZE (imode) : 0);
3747
3748 /* If the sub-object is at least word sized, then we know that subregging
3749 will work. This special case is important, since extract_bit_field
3750 wants to operate on integer modes, and there's rarely an OImode to
3751 correspond to TCmode. */
3752 if (ibitsize >= BITS_PER_WORD
3753 /* For hard regs we have exact predicates. Assume we can split
3754 the original object if it spans an even number of hard regs.
3755 This special case is important for SCmode on 64-bit platforms
3756 where the natural size of floating-point regs is 32-bit. */
3757 || (REG_P (cplx)
3758 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3759 && REG_NREGS (cplx) % 2 == 0))
3760 {
3761 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3762 imag_p ? GET_MODE_SIZE (imode) : 0);
3763 if (ret)
3764 return ret;
3765 else
3766 /* simplify_gen_subreg may fail for sub-word MEMs. */
3767 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3768 }
3769
3770 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3771 true, NULL_RTX, imode, imode, false, NULL);
3772 }
3773 \f
3774 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3775 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3776 represented in NEW_MODE. If FORCE is true, this will never happen, as
3777 we'll force-create a SUBREG if needed. */
3778
3779 static rtx
3780 emit_move_change_mode (machine_mode new_mode,
3781 machine_mode old_mode, rtx x, bool force)
3782 {
3783 rtx ret;
3784
3785 if (push_operand (x, GET_MODE (x)))
3786 {
3787 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3788 MEM_COPY_ATTRIBUTES (ret, x);
3789 }
3790 else if (MEM_P (x))
3791 {
3792 /* We don't have to worry about changing the address since the
3793 size in bytes is supposed to be the same. */
3794 if (reload_in_progress)
3795 {
3796 /* Copy the MEM to change the mode and move any
3797 substitutions from the old MEM to the new one. */
3798 ret = adjust_address_nv (x, new_mode, 0);
3799 copy_replacements (x, ret);
3800 }
3801 else
3802 ret = adjust_address (x, new_mode, 0);
3803 }
3804 else
3805 {
3806 /* Note that we do want simplify_subreg's behavior of validating
3807 that the new mode is ok for a hard register. If we were to use
3808 simplify_gen_subreg, we would create the subreg, but would
3809 probably run into the target not being able to implement it. */
3810 /* Except, of course, when FORCE is true, when this is exactly what
3811 we want. Which is needed for CCmodes on some targets. */
3812 if (force)
3813 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3814 else
3815 ret = simplify_subreg (new_mode, x, old_mode, 0);
3816 }
3817
3818 return ret;
3819 }
3820
3821 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3822 an integer mode of the same size as MODE. Returns the instruction
3823 emitted, or NULL if such a move could not be generated. */
3824
3825 static rtx_insn *
3826 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3827 {
3828 scalar_int_mode imode;
3829 enum insn_code code;
3830
3831 /* There must exist a mode of the exact size we require. */
3832 if (!int_mode_for_mode (mode).exists (&imode))
3833 return NULL;
3834
3835 /* The target must support moves in this mode. */
3836 code = optab_handler (mov_optab, imode);
3837 if (code == CODE_FOR_nothing)
3838 return NULL;
3839
3840 x = emit_move_change_mode (imode, mode, x, force);
3841 if (x == NULL_RTX)
3842 return NULL;
3843 y = emit_move_change_mode (imode, mode, y, force);
3844 if (y == NULL_RTX)
3845 return NULL;
3846 return emit_insn (GEN_FCN (code) (x, y));
3847 }
3848
3849 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3850 Return an equivalent MEM that does not use an auto-increment. */
3851
3852 rtx
3853 emit_move_resolve_push (machine_mode mode, rtx x)
3854 {
3855 enum rtx_code code = GET_CODE (XEXP (x, 0));
3856 rtx temp;
3857
3858 poly_int64 adjust = GET_MODE_SIZE (mode);
3859 #ifdef PUSH_ROUNDING
3860 adjust = PUSH_ROUNDING (adjust);
3861 #endif
3862 if (code == PRE_DEC || code == POST_DEC)
3863 adjust = -adjust;
3864 else if (code == PRE_MODIFY || code == POST_MODIFY)
3865 {
3866 rtx expr = XEXP (XEXP (x, 0), 1);
3867
3868 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3869 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3870 if (GET_CODE (expr) == MINUS)
3871 val = -val;
3872 gcc_assert (known_eq (adjust, val) || known_eq (adjust, -val));
3873 adjust = val;
3874 }
3875
3876 /* Do not use anti_adjust_stack, since we don't want to update
3877 stack_pointer_delta. */
3878 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3879 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3880 0, OPTAB_LIB_WIDEN);
3881 if (temp != stack_pointer_rtx)
3882 emit_move_insn (stack_pointer_rtx, temp);
3883
3884 switch (code)
3885 {
3886 case PRE_INC:
3887 case PRE_DEC:
3888 case PRE_MODIFY:
3889 temp = stack_pointer_rtx;
3890 break;
3891 case POST_INC:
3892 case POST_DEC:
3893 case POST_MODIFY:
3894 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3895 break;
3896 default:
3897 gcc_unreachable ();
3898 }
3899
3900 return replace_equiv_address (x, temp);
3901 }
3902
3903 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3904 X is known to satisfy push_operand, and MODE is known to be complex.
3905 Returns the last instruction emitted. */
3906
3907 rtx_insn *
3908 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3909 {
3910 scalar_mode submode = GET_MODE_INNER (mode);
3911 bool imag_first;
3912
3913 #ifdef PUSH_ROUNDING
3914 poly_int64 submodesize = GET_MODE_SIZE (submode);
3915
3916 /* In case we output to the stack, but the size is smaller than the
3917 machine can push exactly, we need to use move instructions. */
3918 if (maybe_ne (PUSH_ROUNDING (submodesize), submodesize))
3919 {
3920 x = emit_move_resolve_push (mode, x);
3921 return emit_move_insn (x, y);
3922 }
3923 #endif
3924
3925 /* Note that the real part always precedes the imag part in memory
3926 regardless of machine's endianness. */
3927 switch (GET_CODE (XEXP (x, 0)))
3928 {
3929 case PRE_DEC:
3930 case POST_DEC:
3931 imag_first = true;
3932 break;
3933 case PRE_INC:
3934 case POST_INC:
3935 imag_first = false;
3936 break;
3937 default:
3938 gcc_unreachable ();
3939 }
3940
3941 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3942 read_complex_part (y, imag_first));
3943 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3944 read_complex_part (y, !imag_first));
3945 }
3946
3947 /* A subroutine of emit_move_complex. Perform the move from Y to X
3948 via two moves of the parts. Returns the last instruction emitted. */
3949
3950 rtx_insn *
3951 emit_move_complex_parts (rtx x, rtx y)
3952 {
3953 /* Show the output dies here. This is necessary for SUBREGs
3954 of pseudos since we cannot track their lifetimes correctly;
3955 hard regs shouldn't appear here except as return values. */
3956 if (!reload_completed && !reload_in_progress
3957 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3958 emit_clobber (x);
3959
3960 write_complex_part (x, read_complex_part (y, false), false, true);
3961 write_complex_part (x, read_complex_part (y, true), true, false);
3962
3963 return get_last_insn ();
3964 }
3965
3966 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3967 MODE is known to be complex. Returns the last instruction emitted. */
3968
3969 static rtx_insn *
3970 emit_move_complex (machine_mode mode, rtx x, rtx y)
3971 {
3972 bool try_int;
3973
3974 /* Need to take special care for pushes, to maintain proper ordering
3975 of the data, and possibly extra padding. */
3976 if (push_operand (x, mode))
3977 return emit_move_complex_push (mode, x, y);
3978
3979 /* See if we can coerce the target into moving both values at once, except
3980 for floating point where we favor moving as parts if this is easy. */
3981 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3982 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3983 && !(REG_P (x)
3984 && HARD_REGISTER_P (x)
3985 && REG_NREGS (x) == 1)
3986 && !(REG_P (y)
3987 && HARD_REGISTER_P (y)
3988 && REG_NREGS (y) == 1))
3989 try_int = false;
3990 /* Not possible if the values are inherently not adjacent. */
3991 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3992 try_int = false;
3993 /* Is possible if both are registers (or subregs of registers). */
3994 else if (register_operand (x, mode) && register_operand (y, mode))
3995 try_int = true;
3996 /* If one of the operands is a memory, and alignment constraints
3997 are friendly enough, we may be able to do combined memory operations.
3998 We do not attempt this if Y is a constant because that combination is
3999 usually better with the by-parts thing below. */
4000 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
4001 && (!STRICT_ALIGNMENT
4002 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
4003 try_int = true;
4004 else
4005 try_int = false;
4006
4007 if (try_int)
4008 {
4009 rtx_insn *ret;
4010
4011 /* For memory to memory moves, optimal behavior can be had with the
4012 existing block move logic. But use normal expansion if optimizing
4013 for size. */
4014 if (MEM_P (x) && MEM_P (y))
4015 {
4016 emit_block_move (x, y, gen_int_mode (GET_MODE_SIZE (mode), Pmode),
4017 (optimize_insn_for_speed_p()
4018 ? BLOCK_OP_NO_LIBCALL : BLOCK_OP_NORMAL));
4019 return get_last_insn ();
4020 }
4021
4022 ret = emit_move_via_integer (mode, x, y, true);
4023 if (ret)
4024 return ret;
4025 }
4026
4027 return emit_move_complex_parts (x, y);
4028 }
4029
4030 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4031 MODE is known to be MODE_CC. Returns the last instruction emitted. */
4032
4033 static rtx_insn *
4034 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
4035 {
4036 rtx_insn *ret;
4037
4038 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
4039 if (mode != CCmode)
4040 {
4041 enum insn_code code = optab_handler (mov_optab, CCmode);
4042 if (code != CODE_FOR_nothing)
4043 {
4044 x = emit_move_change_mode (CCmode, mode, x, true);
4045 y = emit_move_change_mode (CCmode, mode, y, true);
4046 return emit_insn (GEN_FCN (code) (x, y));
4047 }
4048 }
4049
4050 /* Otherwise, find the MODE_INT mode of the same width. */
4051 ret = emit_move_via_integer (mode, x, y, false);
4052 gcc_assert (ret != NULL);
4053 return ret;
4054 }
4055
4056 /* Return true if word I of OP lies entirely in the
4057 undefined bits of a paradoxical subreg. */
4058
4059 static bool
4060 undefined_operand_subword_p (const_rtx op, int i)
4061 {
4062 if (GET_CODE (op) != SUBREG)
4063 return false;
4064 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
4065 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
4066 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
4067 || known_le (offset, -UNITS_PER_WORD));
4068 }
4069
4070 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
4071 MODE is any multi-word or full-word mode that lacks a move_insn
4072 pattern. Note that you will get better code if you define such
4073 patterns, even if they must turn into multiple assembler instructions. */
4074
4075 static rtx_insn *
4076 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
4077 {
4078 rtx_insn *last_insn = 0;
4079 rtx_insn *seq;
4080 rtx inner;
4081 bool need_clobber;
4082 int i, mode_size;
4083
4084 /* This function can only handle cases where the number of words is
4085 known at compile time. */
4086 mode_size = GET_MODE_SIZE (mode).to_constant ();
4087 gcc_assert (mode_size >= UNITS_PER_WORD);
4088
4089 /* If X is a push on the stack, do the push now and replace
4090 X with a reference to the stack pointer. */
4091 if (push_operand (x, mode))
4092 x = emit_move_resolve_push (mode, x);
4093
4094 /* If we are in reload, see if either operand is a MEM whose address
4095 is scheduled for replacement. */
4096 if (reload_in_progress && MEM_P (x)
4097 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
4098 x = replace_equiv_address_nv (x, inner);
4099 if (reload_in_progress && MEM_P (y)
4100 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
4101 y = replace_equiv_address_nv (y, inner);
4102
4103 start_sequence ();
4104
4105 need_clobber = false;
4106 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
4107 {
4108 /* Do not generate code for a move if it would go entirely
4109 to the non-existing bits of a paradoxical subreg. */
4110 if (undefined_operand_subword_p (x, i))
4111 continue;
4112
4113 rtx xpart = operand_subword (x, i, 1, mode);
4114 rtx ypart;
4115
4116 /* Do not generate code for a move if it would come entirely
4117 from the undefined bits of a paradoxical subreg. */
4118 if (undefined_operand_subword_p (y, i))
4119 continue;
4120
4121 ypart = operand_subword (y, i, 1, mode);
4122
4123 /* If we can't get a part of Y, put Y into memory if it is a
4124 constant. Otherwise, force it into a register. Then we must
4125 be able to get a part of Y. */
4126 if (ypart == 0 && CONSTANT_P (y))
4127 {
4128 y = use_anchored_address (force_const_mem (mode, y));
4129 ypart = operand_subword (y, i, 1, mode);
4130 }
4131 else if (ypart == 0)
4132 ypart = operand_subword_force (y, i, mode);
4133
4134 gcc_assert (xpart && ypart);
4135
4136 need_clobber |= (GET_CODE (xpart) == SUBREG);
4137
4138 last_insn = emit_move_insn (xpart, ypart);
4139 }
4140
4141 seq = get_insns ();
4142 end_sequence ();
4143
4144 /* Show the output dies here. This is necessary for SUBREGs
4145 of pseudos since we cannot track their lifetimes correctly;
4146 hard regs shouldn't appear here except as return values.
4147 We never want to emit such a clobber after reload. */
4148 if (x != y
4149 && ! (reload_in_progress || reload_completed)
4150 && need_clobber != 0)
4151 emit_clobber (x);
4152
4153 emit_insn (seq);
4154
4155 return last_insn;
4156 }
4157
4158 /* Low level part of emit_move_insn.
4159 Called just like emit_move_insn, but assumes X and Y
4160 are basically valid. */
4161
4162 rtx_insn *
4163 emit_move_insn_1 (rtx x, rtx y)
4164 {
4165 machine_mode mode = GET_MODE (x);
4166 enum insn_code code;
4167
4168 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
4169
4170 code = optab_handler (mov_optab, mode);
4171 if (code != CODE_FOR_nothing)
4172 return emit_insn (GEN_FCN (code) (x, y));
4173
4174 /* Expand complex moves by moving real part and imag part. */
4175 if (COMPLEX_MODE_P (mode))
4176 return emit_move_complex (mode, x, y);
4177
4178 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
4179 || ALL_FIXED_POINT_MODE_P (mode))
4180 {
4181 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
4182
4183 /* If we can't find an integer mode, use multi words. */
4184 if (result)
4185 return result;
4186 else
4187 return emit_move_multi_word (mode, x, y);
4188 }
4189
4190 if (GET_MODE_CLASS (mode) == MODE_CC)
4191 return emit_move_ccmode (mode, x, y);
4192
4193 /* Try using a move pattern for the corresponding integer mode. This is
4194 only safe when simplify_subreg can convert MODE constants into integer
4195 constants. At present, it can only do this reliably if the value
4196 fits within a HOST_WIDE_INT. */
4197 if (!CONSTANT_P (y)
4198 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
4199 {
4200 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
4201
4202 if (ret)
4203 {
4204 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
4205 return ret;
4206 }
4207 }
4208
4209 return emit_move_multi_word (mode, x, y);
4210 }
4211
4212 /* Generate code to copy Y into X.
4213 Both Y and X must have the same mode, except that
4214 Y can be a constant with VOIDmode.
4215 This mode cannot be BLKmode; use emit_block_move for that.
4216
4217 Return the last instruction emitted. */
4218
4219 rtx_insn *
4220 emit_move_insn (rtx x, rtx y)
4221 {
4222 machine_mode mode = GET_MODE (x);
4223 rtx y_cst = NULL_RTX;
4224 rtx_insn *last_insn;
4225 rtx set;
4226
4227 gcc_assert (mode != BLKmode
4228 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
4229
4230 /* If we have a copy that looks like one of the following patterns:
4231 (set (subreg:M1 (reg:M2 ...)) (subreg:M1 (reg:M2 ...)))
4232 (set (subreg:M1 (reg:M2 ...)) (mem:M1 ADDR))
4233 (set (mem:M1 ADDR) (subreg:M1 (reg:M2 ...)))
4234 (set (subreg:M1 (reg:M2 ...)) (constant C))
4235 where mode M1 is equal in size to M2, try to detect whether the
4236 mode change involves an implicit round trip through memory.
4237 If so, see if we can avoid that by removing the subregs and
4238 doing the move in mode M2 instead. */
4239
4240 rtx x_inner = NULL_RTX;
4241 rtx y_inner = NULL_RTX;
4242
4243 auto candidate_subreg_p = [&](rtx subreg) {
4244 return (REG_P (SUBREG_REG (subreg))
4245 && known_eq (GET_MODE_SIZE (GET_MODE (SUBREG_REG (subreg))),
4246 GET_MODE_SIZE (GET_MODE (subreg)))
4247 && optab_handler (mov_optab, GET_MODE (SUBREG_REG (subreg)))
4248 != CODE_FOR_nothing);
4249 };
4250
4251 auto candidate_mem_p = [&](machine_mode innermode, rtx mem) {
4252 return (!targetm.can_change_mode_class (innermode, GET_MODE (mem), ALL_REGS)
4253 && !push_operand (mem, GET_MODE (mem))
4254 /* Not a candiate if innermode requires too much alignment. */
4255 && (MEM_ALIGN (mem) >= GET_MODE_ALIGNMENT (innermode)
4256 || targetm.slow_unaligned_access (GET_MODE (mem),
4257 MEM_ALIGN (mem))
4258 || !targetm.slow_unaligned_access (innermode,
4259 MEM_ALIGN (mem))));
4260 };
4261
4262 if (SUBREG_P (x) && candidate_subreg_p (x))
4263 x_inner = SUBREG_REG (x);
4264
4265 if (SUBREG_P (y) && candidate_subreg_p (y))
4266 y_inner = SUBREG_REG (y);
4267
4268 if (x_inner != NULL_RTX
4269 && y_inner != NULL_RTX
4270 && GET_MODE (x_inner) == GET_MODE (y_inner)
4271 && !targetm.can_change_mode_class (GET_MODE (x_inner), mode, ALL_REGS))
4272 {
4273 x = x_inner;
4274 y = y_inner;
4275 mode = GET_MODE (x_inner);
4276 }
4277 else if (x_inner != NULL_RTX
4278 && MEM_P (y)
4279 && candidate_mem_p (GET_MODE (x_inner), y))
4280 {
4281 x = x_inner;
4282 y = adjust_address (y, GET_MODE (x_inner), 0);
4283 mode = GET_MODE (x_inner);
4284 }
4285 else if (y_inner != NULL_RTX
4286 && MEM_P (x)
4287 && candidate_mem_p (GET_MODE (y_inner), x))
4288 {
4289 x = adjust_address (x, GET_MODE (y_inner), 0);
4290 y = y_inner;
4291 mode = GET_MODE (y_inner);
4292 }
4293 else if (x_inner != NULL_RTX
4294 && CONSTANT_P (y)
4295 && !targetm.can_change_mode_class (GET_MODE (x_inner),
4296 mode, ALL_REGS)
4297 && (y_inner = simplify_subreg (GET_MODE (x_inner), y, mode, 0)))
4298 {
4299 x = x_inner;
4300 y = y_inner;
4301 mode = GET_MODE (x_inner);
4302 }
4303
4304 if (CONSTANT_P (y))
4305 {
4306 if (optimize
4307 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
4308 && (last_insn = compress_float_constant (x, y)))
4309 return last_insn;
4310
4311 y_cst = y;
4312
4313 if (!targetm.legitimate_constant_p (mode, y))
4314 {
4315 y = force_const_mem (mode, y);
4316
4317 /* If the target's cannot_force_const_mem prevented the spill,
4318 assume that the target's move expanders will also take care
4319 of the non-legitimate constant. */
4320 if (!y)
4321 y = y_cst;
4322 else
4323 y = use_anchored_address (y);
4324 }
4325 }
4326
4327 /* If X or Y are memory references, verify that their addresses are valid
4328 for the machine. */
4329 if (MEM_P (x)
4330 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4331 MEM_ADDR_SPACE (x))
4332 && ! push_operand (x, GET_MODE (x))))
4333 x = validize_mem (x);
4334
4335 if (MEM_P (y)
4336 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
4337 MEM_ADDR_SPACE (y)))
4338 y = validize_mem (y);
4339
4340 gcc_assert (mode != BLKmode);
4341
4342 last_insn = emit_move_insn_1 (x, y);
4343
4344 if (y_cst && REG_P (x)
4345 && (set = single_set (last_insn)) != NULL_RTX
4346 && SET_DEST (set) == x
4347 && ! rtx_equal_p (y_cst, SET_SRC (set)))
4348 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
4349
4350 return last_insn;
4351 }
4352
4353 /* Generate the body of an instruction to copy Y into X.
4354 It may be a list of insns, if one insn isn't enough. */
4355
4356 rtx_insn *
4357 gen_move_insn (rtx x, rtx y)
4358 {
4359 rtx_insn *seq;
4360
4361 start_sequence ();
4362 emit_move_insn_1 (x, y);
4363 seq = get_insns ();
4364 end_sequence ();
4365 return seq;
4366 }
4367
4368 /* If Y is representable exactly in a narrower mode, and the target can
4369 perform the extension directly from constant or memory, then emit the
4370 move as an extension. */
4371
4372 static rtx_insn *
4373 compress_float_constant (rtx x, rtx y)
4374 {
4375 machine_mode dstmode = GET_MODE (x);
4376 machine_mode orig_srcmode = GET_MODE (y);
4377 machine_mode srcmode;
4378 const REAL_VALUE_TYPE *r;
4379 int oldcost, newcost;
4380 bool speed = optimize_insn_for_speed_p ();
4381
4382 r = CONST_DOUBLE_REAL_VALUE (y);
4383
4384 if (targetm.legitimate_constant_p (dstmode, y))
4385 oldcost = set_src_cost (y, orig_srcmode, speed);
4386 else
4387 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
4388
4389 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
4390 {
4391 enum insn_code ic;
4392 rtx trunc_y;
4393 rtx_insn *last_insn;
4394
4395 /* Skip if the target can't extend this way. */
4396 ic = can_extend_p (dstmode, srcmode, 0);
4397 if (ic == CODE_FOR_nothing)
4398 continue;
4399
4400 /* Skip if the narrowed value isn't exact. */
4401 if (! exact_real_truncate (srcmode, r))
4402 continue;
4403
4404 trunc_y = const_double_from_real_value (*r, srcmode);
4405
4406 if (targetm.legitimate_constant_p (srcmode, trunc_y))
4407 {
4408 /* Skip if the target needs extra instructions to perform
4409 the extension. */
4410 if (!insn_operand_matches (ic, 1, trunc_y))
4411 continue;
4412 /* This is valid, but may not be cheaper than the original. */
4413 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4414 dstmode, speed);
4415 if (oldcost < newcost)
4416 continue;
4417 }
4418 else if (float_extend_from_mem[dstmode][srcmode])
4419 {
4420 trunc_y = force_const_mem (srcmode, trunc_y);
4421 /* This is valid, but may not be cheaper than the original. */
4422 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
4423 dstmode, speed);
4424 if (oldcost < newcost)
4425 continue;
4426 trunc_y = validize_mem (trunc_y);
4427 }
4428 else
4429 continue;
4430
4431 /* For CSE's benefit, force the compressed constant pool entry
4432 into a new pseudo. This constant may be used in different modes,
4433 and if not, combine will put things back together for us. */
4434 trunc_y = force_reg (srcmode, trunc_y);
4435
4436 /* If x is a hard register, perform the extension into a pseudo,
4437 so that e.g. stack realignment code is aware of it. */
4438 rtx target = x;
4439 if (REG_P (x) && HARD_REGISTER_P (x))
4440 target = gen_reg_rtx (dstmode);
4441
4442 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
4443 last_insn = get_last_insn ();
4444
4445 if (REG_P (target))
4446 set_unique_reg_note (last_insn, REG_EQUAL, y);
4447
4448 if (target != x)
4449 return emit_move_insn (x, target);
4450 return last_insn;
4451 }
4452
4453 return NULL;
4454 }
4455 \f
4456 /* Pushing data onto the stack. */
4457
4458 /* Push a block of length SIZE (perhaps variable)
4459 and return an rtx to address the beginning of the block.
4460 The value may be virtual_outgoing_args_rtx.
4461
4462 EXTRA is the number of bytes of padding to push in addition to SIZE.
4463 BELOW nonzero means this padding comes at low addresses;
4464 otherwise, the padding comes at high addresses. */
4465
4466 rtx
4467 push_block (rtx size, poly_int64 extra, int below)
4468 {
4469 rtx temp;
4470
4471 size = convert_modes (Pmode, ptr_mode, size, 1);
4472 if (CONSTANT_P (size))
4473 anti_adjust_stack (plus_constant (Pmode, size, extra));
4474 else if (REG_P (size) && known_eq (extra, 0))
4475 anti_adjust_stack (size);
4476 else
4477 {
4478 temp = copy_to_mode_reg (Pmode, size);
4479 if (maybe_ne (extra, 0))
4480 temp = expand_binop (Pmode, add_optab, temp,
4481 gen_int_mode (extra, Pmode),
4482 temp, 0, OPTAB_LIB_WIDEN);
4483 anti_adjust_stack (temp);
4484 }
4485
4486 if (STACK_GROWS_DOWNWARD)
4487 {
4488 temp = virtual_outgoing_args_rtx;
4489 if (maybe_ne (extra, 0) && below)
4490 temp = plus_constant (Pmode, temp, extra);
4491 }
4492 else
4493 {
4494 poly_int64 csize;
4495 if (poly_int_rtx_p (size, &csize))
4496 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
4497 -csize - (below ? 0 : extra));
4498 else if (maybe_ne (extra, 0) && !below)
4499 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4500 negate_rtx (Pmode, plus_constant (Pmode, size,
4501 extra)));
4502 else
4503 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
4504 negate_rtx (Pmode, size));
4505 }
4506
4507 return memory_address (NARROWEST_INT_MODE, temp);
4508 }
4509
4510 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
4511
4512 static rtx
4513 mem_autoinc_base (rtx mem)
4514 {
4515 if (MEM_P (mem))
4516 {
4517 rtx addr = XEXP (mem, 0);
4518 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4519 return XEXP (addr, 0);
4520 }
4521 return NULL;
4522 }
4523
4524 /* A utility routine used here, in reload, and in try_split. The insns
4525 after PREV up to and including LAST are known to adjust the stack,
4526 with a final value of END_ARGS_SIZE. Iterate backward from LAST
4527 placing notes as appropriate. PREV may be NULL, indicating the
4528 entire insn sequence prior to LAST should be scanned.
4529
4530 The set of allowed stack pointer modifications is small:
4531 (1) One or more auto-inc style memory references (aka pushes),
4532 (2) One or more addition/subtraction with the SP as destination,
4533 (3) A single move insn with the SP as destination,
4534 (4) A call_pop insn,
4535 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
4536
4537 Insns in the sequence that do not modify the SP are ignored,
4538 except for noreturn calls.
4539
4540 The return value is the amount of adjustment that can be trivially
4541 verified, via immediate operand or auto-inc. If the adjustment
4542 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
4543
4544 poly_int64
4545 find_args_size_adjust (rtx_insn *insn)
4546 {
4547 rtx dest, set, pat;
4548 int i;
4549
4550 pat = PATTERN (insn);
4551 set = NULL;
4552
4553 /* Look for a call_pop pattern. */
4554 if (CALL_P (insn))
4555 {
4556 /* We have to allow non-call_pop patterns for the case
4557 of emit_single_push_insn of a TLS address. */
4558 if (GET_CODE (pat) != PARALLEL)
4559 return 0;
4560
4561 /* All call_pop have a stack pointer adjust in the parallel.
4562 The call itself is always first, and the stack adjust is
4563 usually last, so search from the end. */
4564 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
4565 {
4566 set = XVECEXP (pat, 0, i);
4567 if (GET_CODE (set) != SET)
4568 continue;
4569 dest = SET_DEST (set);
4570 if (dest == stack_pointer_rtx)
4571 break;
4572 }
4573 /* We'd better have found the stack pointer adjust. */
4574 if (i == 0)
4575 return 0;
4576 /* Fall through to process the extracted SET and DEST
4577 as if it was a standalone insn. */
4578 }
4579 else if (GET_CODE (pat) == SET)
4580 set = pat;
4581 else if ((set = single_set (insn)) != NULL)
4582 ;
4583 else if (GET_CODE (pat) == PARALLEL)
4584 {
4585 /* ??? Some older ports use a parallel with a stack adjust
4586 and a store for a PUSH_ROUNDING pattern, rather than a
4587 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4588 /* ??? See h8300 and m68k, pushqi1. */
4589 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4590 {
4591 set = XVECEXP (pat, 0, i);
4592 if (GET_CODE (set) != SET)
4593 continue;
4594 dest = SET_DEST (set);
4595 if (dest == stack_pointer_rtx)
4596 break;
4597
4598 /* We do not expect an auto-inc of the sp in the parallel. */
4599 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4600 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4601 != stack_pointer_rtx);
4602 }
4603 if (i < 0)
4604 return 0;
4605 }
4606 else
4607 return 0;
4608
4609 dest = SET_DEST (set);
4610
4611 /* Look for direct modifications of the stack pointer. */
4612 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4613 {
4614 /* Look for a trivial adjustment, otherwise assume nothing. */
4615 /* Note that the SPU restore_stack_block pattern refers to
4616 the stack pointer in V4SImode. Consider that non-trivial. */
4617 poly_int64 offset;
4618 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4619 && strip_offset (SET_SRC (set), &offset) == stack_pointer_rtx)
4620 return offset;
4621 /* ??? Reload can generate no-op moves, which will be cleaned
4622 up later. Recognize it and continue searching. */
4623 else if (rtx_equal_p (dest, SET_SRC (set)))
4624 return 0;
4625 else
4626 return HOST_WIDE_INT_MIN;
4627 }
4628 else
4629 {
4630 rtx mem, addr;
4631
4632 /* Otherwise only think about autoinc patterns. */
4633 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4634 {
4635 mem = dest;
4636 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4637 != stack_pointer_rtx);
4638 }
4639 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4640 mem = SET_SRC (set);
4641 else
4642 return 0;
4643
4644 addr = XEXP (mem, 0);
4645 switch (GET_CODE (addr))
4646 {
4647 case PRE_INC:
4648 case POST_INC:
4649 return GET_MODE_SIZE (GET_MODE (mem));
4650 case PRE_DEC:
4651 case POST_DEC:
4652 return -GET_MODE_SIZE (GET_MODE (mem));
4653 case PRE_MODIFY:
4654 case POST_MODIFY:
4655 addr = XEXP (addr, 1);
4656 gcc_assert (GET_CODE (addr) == PLUS);
4657 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4658 return rtx_to_poly_int64 (XEXP (addr, 1));
4659 default:
4660 gcc_unreachable ();
4661 }
4662 }
4663 }
4664
4665 poly_int64
4666 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4667 poly_int64 end_args_size)
4668 {
4669 poly_int64 args_size = end_args_size;
4670 bool saw_unknown = false;
4671 rtx_insn *insn;
4672
4673 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4674 {
4675 if (!NONDEBUG_INSN_P (insn))
4676 continue;
4677
4678 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4679 a call argument containing a TLS address that itself requires
4680 a call to __tls_get_addr. The handling of stack_pointer_delta
4681 in emit_single_push_insn is supposed to ensure that any such
4682 notes are already correct. */
4683 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4684 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4685
4686 poly_int64 this_delta = find_args_size_adjust (insn);
4687 if (known_eq (this_delta, 0))
4688 {
4689 if (!CALL_P (insn)
4690 || ACCUMULATE_OUTGOING_ARGS
4691 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4692 continue;
4693 }
4694
4695 gcc_assert (!saw_unknown);
4696 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4697 saw_unknown = true;
4698
4699 if (!note)
4700 add_args_size_note (insn, args_size);
4701 if (STACK_GROWS_DOWNWARD)
4702 this_delta = -poly_uint64 (this_delta);
4703
4704 if (saw_unknown)
4705 args_size = HOST_WIDE_INT_MIN;
4706 else
4707 args_size -= this_delta;
4708 }
4709
4710 return args_size;
4711 }
4712
4713 #ifdef PUSH_ROUNDING
4714 /* Emit single push insn. */
4715
4716 static void
4717 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4718 {
4719 rtx dest_addr;
4720 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4721 rtx dest;
4722 enum insn_code icode;
4723
4724 /* If there is push pattern, use it. Otherwise try old way of throwing
4725 MEM representing push operation to move expander. */
4726 icode = optab_handler (push_optab, mode);
4727 if (icode != CODE_FOR_nothing)
4728 {
4729 class expand_operand ops[1];
4730
4731 create_input_operand (&ops[0], x, mode);
4732 if (maybe_expand_insn (icode, 1, ops))
4733 return;
4734 }
4735 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4736 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4737 /* If we are to pad downward, adjust the stack pointer first and
4738 then store X into the stack location using an offset. This is
4739 because emit_move_insn does not know how to pad; it does not have
4740 access to type. */
4741 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4742 {
4743 emit_move_insn (stack_pointer_rtx,
4744 expand_binop (Pmode,
4745 STACK_GROWS_DOWNWARD ? sub_optab
4746 : add_optab,
4747 stack_pointer_rtx,
4748 gen_int_mode (rounded_size, Pmode),
4749 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4750
4751 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4752 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4753 /* We have already decremented the stack pointer, so get the
4754 previous value. */
4755 offset += rounded_size;
4756
4757 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4758 /* We have already incremented the stack pointer, so get the
4759 previous value. */
4760 offset -= rounded_size;
4761
4762 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4763 }
4764 else
4765 {
4766 if (STACK_GROWS_DOWNWARD)
4767 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4768 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4769 else
4770 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4771 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4772
4773 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4774 }
4775
4776 dest = gen_rtx_MEM (mode, dest_addr);
4777
4778 if (type != 0)
4779 {
4780 set_mem_attributes (dest, type, 1);
4781
4782 if (cfun->tail_call_marked)
4783 /* Function incoming arguments may overlap with sibling call
4784 outgoing arguments and we cannot allow reordering of reads
4785 from function arguments with stores to outgoing arguments
4786 of sibling calls. */
4787 set_mem_alias_set (dest, 0);
4788 }
4789 emit_move_insn (dest, x);
4790 }
4791
4792 /* Emit and annotate a single push insn. */
4793
4794 static void
4795 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4796 {
4797 poly_int64 delta, old_delta = stack_pointer_delta;
4798 rtx_insn *prev = get_last_insn ();
4799 rtx_insn *last;
4800
4801 emit_single_push_insn_1 (mode, x, type);
4802
4803 /* Adjust stack_pointer_delta to describe the situation after the push
4804 we just performed. Note that we must do this after the push rather
4805 than before the push in case calculating X needs pushes and pops of
4806 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4807 for such pushes and pops must not include the effect of the future
4808 push of X. */
4809 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4810
4811 last = get_last_insn ();
4812
4813 /* Notice the common case where we emitted exactly one insn. */
4814 if (PREV_INSN (last) == prev)
4815 {
4816 add_args_size_note (last, stack_pointer_delta);
4817 return;
4818 }
4819
4820 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4821 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4822 || known_eq (delta, old_delta));
4823 }
4824 #endif
4825
4826 /* If reading SIZE bytes from X will end up reading from
4827 Y return the number of bytes that overlap. Return -1
4828 if there is no overlap or -2 if we can't determine
4829 (for example when X and Y have different base registers). */
4830
4831 static int
4832 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4833 {
4834 rtx tmp = plus_constant (Pmode, x, size);
4835 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4836
4837 if (!CONST_INT_P (sub))
4838 return -2;
4839
4840 HOST_WIDE_INT val = INTVAL (sub);
4841
4842 return IN_RANGE (val, 1, size) ? val : -1;
4843 }
4844
4845 /* Generate code to push X onto the stack, assuming it has mode MODE and
4846 type TYPE.
4847 MODE is redundant except when X is a CONST_INT (since they don't
4848 carry mode info).
4849 SIZE is an rtx for the size of data to be copied (in bytes),
4850 needed only if X is BLKmode.
4851 Return true if successful. May return false if asked to push a
4852 partial argument during a sibcall optimization (as specified by
4853 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4854 to not overlap.
4855
4856 ALIGN (in bits) is maximum alignment we can assume.
4857
4858 If PARTIAL and REG are both nonzero, then copy that many of the first
4859 bytes of X into registers starting with REG, and push the rest of X.
4860 The amount of space pushed is decreased by PARTIAL bytes.
4861 REG must be a hard register in this case.
4862 If REG is zero but PARTIAL is not, take any all others actions for an
4863 argument partially in registers, but do not actually load any
4864 registers.
4865
4866 EXTRA is the amount in bytes of extra space to leave next to this arg.
4867 This is ignored if an argument block has already been allocated.
4868
4869 On a machine that lacks real push insns, ARGS_ADDR is the address of
4870 the bottom of the argument block for this call. We use indexing off there
4871 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4872 argument block has not been preallocated.
4873
4874 ARGS_SO_FAR is the size of args previously pushed for this call.
4875
4876 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4877 for arguments passed in registers. If nonzero, it will be the number
4878 of bytes required. */
4879
4880 bool
4881 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4882 unsigned int align, int partial, rtx reg, poly_int64 extra,
4883 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4884 rtx alignment_pad, bool sibcall_p)
4885 {
4886 rtx xinner;
4887 pad_direction stack_direction
4888 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4889
4890 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4891 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4892 Default is below for small data on big-endian machines; else above. */
4893 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4894
4895 /* Invert direction if stack is post-decrement.
4896 FIXME: why? */
4897 if (STACK_PUSH_CODE == POST_DEC)
4898 if (where_pad != PAD_NONE)
4899 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4900
4901 xinner = x;
4902
4903 int nregs = partial / UNITS_PER_WORD;
4904 rtx *tmp_regs = NULL;
4905 int overlapping = 0;
4906
4907 if (mode == BLKmode
4908 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4909 {
4910 /* Copy a block into the stack, entirely or partially. */
4911
4912 rtx temp;
4913 int used;
4914 int offset;
4915 int skip;
4916
4917 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4918 used = partial - offset;
4919
4920 if (mode != BLKmode)
4921 {
4922 /* A value is to be stored in an insufficiently aligned
4923 stack slot; copy via a suitably aligned slot if
4924 necessary. */
4925 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4926 if (!MEM_P (xinner))
4927 {
4928 temp = assign_temp (type, 1, 1);
4929 emit_move_insn (temp, xinner);
4930 xinner = temp;
4931 }
4932 }
4933
4934 gcc_assert (size);
4935
4936 /* USED is now the # of bytes we need not copy to the stack
4937 because registers will take care of them. */
4938
4939 if (partial != 0)
4940 xinner = adjust_address (xinner, BLKmode, used);
4941
4942 /* If the partial register-part of the arg counts in its stack size,
4943 skip the part of stack space corresponding to the registers.
4944 Otherwise, start copying to the beginning of the stack space,
4945 by setting SKIP to 0. */
4946 skip = (reg_parm_stack_space == 0) ? 0 : used;
4947
4948 #ifdef PUSH_ROUNDING
4949 /* NB: Let the backend known the number of bytes to push and
4950 decide if push insns should be generated. */
4951 unsigned int push_size;
4952 if (CONST_INT_P (size))
4953 push_size = INTVAL (size);
4954 else
4955 push_size = 0;
4956
4957 /* Do it with several push insns if that doesn't take lots of insns
4958 and if there is no difficulty with push insns that skip bytes
4959 on the stack for alignment purposes. */
4960 if (args_addr == 0
4961 && targetm.calls.push_argument (push_size)
4962 && CONST_INT_P (size)
4963 && skip == 0
4964 && MEM_ALIGN (xinner) >= align
4965 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4966 /* Here we avoid the case of a structure whose weak alignment
4967 forces many pushes of a small amount of data,
4968 and such small pushes do rounding that causes trouble. */
4969 && ((!targetm.slow_unaligned_access (word_mode, align))
4970 || align >= BIGGEST_ALIGNMENT
4971 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4972 align / BITS_PER_UNIT))
4973 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4974 {
4975 /* Push padding now if padding above and stack grows down,
4976 or if padding below and stack grows up.
4977 But if space already allocated, this has already been done. */
4978 if (maybe_ne (extra, 0)
4979 && args_addr == 0
4980 && where_pad != PAD_NONE
4981 && where_pad != stack_direction)
4982 anti_adjust_stack (gen_int_mode (extra, Pmode));
4983
4984 move_by_pieces (NULL, xinner, INTVAL (size) - used, align,
4985 RETURN_BEGIN);
4986 }
4987 else
4988 #endif /* PUSH_ROUNDING */
4989 {
4990 rtx target;
4991
4992 /* Otherwise make space on the stack and copy the data
4993 to the address of that space. */
4994
4995 /* Deduct words put into registers from the size we must copy. */
4996 if (partial != 0)
4997 {
4998 if (CONST_INT_P (size))
4999 size = GEN_INT (INTVAL (size) - used);
5000 else
5001 size = expand_binop (GET_MODE (size), sub_optab, size,
5002 gen_int_mode (used, GET_MODE (size)),
5003 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5004 }
5005
5006 /* Get the address of the stack space.
5007 In this case, we do not deal with EXTRA separately.
5008 A single stack adjust will do. */
5009 poly_int64 const_args_so_far;
5010 if (! args_addr)
5011 {
5012 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
5013 extra = 0;
5014 }
5015 else if (poly_int_rtx_p (args_so_far, &const_args_so_far))
5016 temp = memory_address (BLKmode,
5017 plus_constant (Pmode, args_addr,
5018 skip + const_args_so_far));
5019 else
5020 temp = memory_address (BLKmode,
5021 plus_constant (Pmode,
5022 gen_rtx_PLUS (Pmode,
5023 args_addr,
5024 args_so_far),
5025 skip));
5026
5027 if (!ACCUMULATE_OUTGOING_ARGS)
5028 {
5029 /* If the source is referenced relative to the stack pointer,
5030 copy it to another register to stabilize it. We do not need
5031 to do this if we know that we won't be changing sp. */
5032
5033 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
5034 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
5035 temp = copy_to_reg (temp);
5036 }
5037
5038 target = gen_rtx_MEM (BLKmode, temp);
5039
5040 /* We do *not* set_mem_attributes here, because incoming arguments
5041 may overlap with sibling call outgoing arguments and we cannot
5042 allow reordering of reads from function arguments with stores
5043 to outgoing arguments of sibling calls. We do, however, want
5044 to record the alignment of the stack slot. */
5045 /* ALIGN may well be better aligned than TYPE, e.g. due to
5046 PARM_BOUNDARY. Assume the caller isn't lying. */
5047 set_mem_align (target, align);
5048
5049 /* If part should go in registers and pushing to that part would
5050 overwrite some of the values that need to go into regs, load the
5051 overlapping values into temporary pseudos to be moved into the hard
5052 regs at the end after the stack pushing has completed.
5053 We cannot load them directly into the hard regs here because
5054 they can be clobbered by the block move expansions.
5055 See PR 65358. */
5056
5057 if (partial > 0 && reg != 0 && mode == BLKmode
5058 && GET_CODE (reg) != PARALLEL)
5059 {
5060 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
5061 if (overlapping > 0)
5062 {
5063 gcc_assert (overlapping % UNITS_PER_WORD == 0);
5064 overlapping /= UNITS_PER_WORD;
5065
5066 tmp_regs = XALLOCAVEC (rtx, overlapping);
5067
5068 for (int i = 0; i < overlapping; i++)
5069 tmp_regs[i] = gen_reg_rtx (word_mode);
5070
5071 for (int i = 0; i < overlapping; i++)
5072 emit_move_insn (tmp_regs[i],
5073 operand_subword_force (target, i, mode));
5074 }
5075 else if (overlapping == -1)
5076 overlapping = 0;
5077 /* Could not determine whether there is overlap.
5078 Fail the sibcall. */
5079 else
5080 {
5081 overlapping = 0;
5082 if (sibcall_p)
5083 return false;
5084 }
5085 }
5086
5087 /* If source is a constant VAR_DECL with a simple constructor,
5088 store the constructor to the stack instead of moving it. */
5089 const_tree decl;
5090 if (partial == 0
5091 && MEM_P (xinner)
5092 && SYMBOL_REF_P (XEXP (xinner, 0))
5093 && (decl = SYMBOL_REF_DECL (XEXP (xinner, 0))) != NULL_TREE
5094 && VAR_P (decl)
5095 && TREE_READONLY (decl)
5096 && !TREE_SIDE_EFFECTS (decl)
5097 && immediate_const_ctor_p (DECL_INITIAL (decl), 2))
5098 store_constructor (DECL_INITIAL (decl), target, 0,
5099 int_expr_size (DECL_INITIAL (decl)), false);
5100 else
5101 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
5102 }
5103 }
5104 else if (partial > 0)
5105 {
5106 /* Scalar partly in registers. This case is only supported
5107 for fixed-wdth modes. */
5108 int num_words = GET_MODE_SIZE (mode).to_constant ();
5109 num_words /= UNITS_PER_WORD;
5110 int i;
5111 int not_stack;
5112 /* # bytes of start of argument
5113 that we must make space for but need not store. */
5114 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
5115 int args_offset = INTVAL (args_so_far);
5116 int skip;
5117
5118 /* Push padding now if padding above and stack grows down,
5119 or if padding below and stack grows up.
5120 But if space already allocated, this has already been done. */
5121 if (maybe_ne (extra, 0)
5122 && args_addr == 0
5123 && where_pad != PAD_NONE
5124 && where_pad != stack_direction)
5125 anti_adjust_stack (gen_int_mode (extra, Pmode));
5126
5127 /* If we make space by pushing it, we might as well push
5128 the real data. Otherwise, we can leave OFFSET nonzero
5129 and leave the space uninitialized. */
5130 if (args_addr == 0)
5131 offset = 0;
5132
5133 /* Now NOT_STACK gets the number of words that we don't need to
5134 allocate on the stack. Convert OFFSET to words too. */
5135 not_stack = (partial - offset) / UNITS_PER_WORD;
5136 offset /= UNITS_PER_WORD;
5137
5138 /* If the partial register-part of the arg counts in its stack size,
5139 skip the part of stack space corresponding to the registers.
5140 Otherwise, start copying to the beginning of the stack space,
5141 by setting SKIP to 0. */
5142 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
5143
5144 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
5145 x = validize_mem (force_const_mem (mode, x));
5146
5147 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
5148 SUBREGs of such registers are not allowed. */
5149 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
5150 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
5151 x = copy_to_reg (x);
5152
5153 /* Loop over all the words allocated on the stack for this arg. */
5154 /* We can do it by words, because any scalar bigger than a word
5155 has a size a multiple of a word. */
5156 for (i = num_words - 1; i >= not_stack; i--)
5157 if (i >= not_stack + offset)
5158 if (!emit_push_insn (operand_subword_force (x, i, mode),
5159 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
5160 0, args_addr,
5161 GEN_INT (args_offset + ((i - not_stack + skip)
5162 * UNITS_PER_WORD)),
5163 reg_parm_stack_space, alignment_pad, sibcall_p))
5164 return false;
5165 }
5166 else
5167 {
5168 rtx addr;
5169 rtx dest;
5170
5171 /* Push padding now if padding above and stack grows down,
5172 or if padding below and stack grows up.
5173 But if space already allocated, this has already been done. */
5174 if (maybe_ne (extra, 0)
5175 && args_addr == 0
5176 && where_pad != PAD_NONE
5177 && where_pad != stack_direction)
5178 anti_adjust_stack (gen_int_mode (extra, Pmode));
5179
5180 #ifdef PUSH_ROUNDING
5181 if (args_addr == 0 && targetm.calls.push_argument (0))
5182 emit_single_push_insn (mode, x, type);
5183 else
5184 #endif
5185 {
5186 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
5187 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
5188
5189 /* We do *not* set_mem_attributes here, because incoming arguments
5190 may overlap with sibling call outgoing arguments and we cannot
5191 allow reordering of reads from function arguments with stores
5192 to outgoing arguments of sibling calls. We do, however, want
5193 to record the alignment of the stack slot. */
5194 /* ALIGN may well be better aligned than TYPE, e.g. due to
5195 PARM_BOUNDARY. Assume the caller isn't lying. */
5196 set_mem_align (dest, align);
5197
5198 emit_move_insn (dest, x);
5199 }
5200 }
5201
5202 /* Move the partial arguments into the registers and any overlapping
5203 values that we moved into the pseudos in tmp_regs. */
5204 if (partial > 0 && reg != 0)
5205 {
5206 /* Handle calls that pass values in multiple non-contiguous locations.
5207 The Irix 6 ABI has examples of this. */
5208 if (GET_CODE (reg) == PARALLEL)
5209 emit_group_load (reg, x, type, -1);
5210 else
5211 {
5212 gcc_assert (partial % UNITS_PER_WORD == 0);
5213 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
5214
5215 for (int i = 0; i < overlapping; i++)
5216 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
5217 + nregs - overlapping + i),
5218 tmp_regs[i]);
5219
5220 }
5221 }
5222
5223 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
5224 anti_adjust_stack (gen_int_mode (extra, Pmode));
5225
5226 if (alignment_pad && args_addr == 0)
5227 anti_adjust_stack (alignment_pad);
5228
5229 return true;
5230 }
5231 \f
5232 /* Return X if X can be used as a subtarget in a sequence of arithmetic
5233 operations. */
5234
5235 static rtx
5236 get_subtarget (rtx x)
5237 {
5238 return (optimize
5239 || x == 0
5240 /* Only registers can be subtargets. */
5241 || !REG_P (x)
5242 /* Don't use hard regs to avoid extending their life. */
5243 || REGNO (x) < FIRST_PSEUDO_REGISTER
5244 ? 0 : x);
5245 }
5246
5247 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
5248 FIELD is a bitfield. Returns true if the optimization was successful,
5249 and there's nothing else to do. */
5250
5251 static bool
5252 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
5253 poly_uint64 pbitpos,
5254 poly_uint64 pbitregion_start,
5255 poly_uint64 pbitregion_end,
5256 machine_mode mode1, rtx str_rtx,
5257 tree to, tree src, bool reverse)
5258 {
5259 /* str_mode is not guaranteed to be a scalar type. */
5260 machine_mode str_mode = GET_MODE (str_rtx);
5261 unsigned int str_bitsize;
5262 tree op0, op1;
5263 rtx value, result;
5264 optab binop;
5265 gimple *srcstmt;
5266 enum tree_code code;
5267
5268 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
5269 if (mode1 != VOIDmode
5270 || !pbitsize.is_constant (&bitsize)
5271 || !pbitpos.is_constant (&bitpos)
5272 || !pbitregion_start.is_constant (&bitregion_start)
5273 || !pbitregion_end.is_constant (&bitregion_end)
5274 || bitsize >= BITS_PER_WORD
5275 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
5276 || str_bitsize > BITS_PER_WORD
5277 || TREE_SIDE_EFFECTS (to)
5278 || TREE_THIS_VOLATILE (to))
5279 return false;
5280
5281 STRIP_NOPS (src);
5282 if (TREE_CODE (src) != SSA_NAME)
5283 return false;
5284 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
5285 return false;
5286
5287 srcstmt = get_gimple_for_ssa_name (src);
5288 if (!srcstmt
5289 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
5290 return false;
5291
5292 code = gimple_assign_rhs_code (srcstmt);
5293
5294 op0 = gimple_assign_rhs1 (srcstmt);
5295
5296 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
5297 to find its initialization. Hopefully the initialization will
5298 be from a bitfield load. */
5299 if (TREE_CODE (op0) == SSA_NAME)
5300 {
5301 gimple *op0stmt = get_gimple_for_ssa_name (op0);
5302
5303 /* We want to eventually have OP0 be the same as TO, which
5304 should be a bitfield. */
5305 if (!op0stmt
5306 || !is_gimple_assign (op0stmt)
5307 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
5308 return false;
5309 op0 = gimple_assign_rhs1 (op0stmt);
5310 }
5311
5312 op1 = gimple_assign_rhs2 (srcstmt);
5313
5314 if (!operand_equal_p (to, op0, 0))
5315 return false;
5316
5317 if (MEM_P (str_rtx))
5318 {
5319 unsigned HOST_WIDE_INT offset1;
5320
5321 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
5322 str_bitsize = BITS_PER_WORD;
5323
5324 scalar_int_mode best_mode;
5325 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
5326 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
5327 return false;
5328 str_mode = best_mode;
5329 str_bitsize = GET_MODE_BITSIZE (best_mode);
5330
5331 offset1 = bitpos;
5332 bitpos %= str_bitsize;
5333 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
5334 str_rtx = adjust_address (str_rtx, str_mode, offset1);
5335 }
5336 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
5337 return false;
5338
5339 /* If the bit field covers the whole REG/MEM, store_field
5340 will likely generate better code. */
5341 if (bitsize >= str_bitsize)
5342 return false;
5343
5344 /* We can't handle fields split across multiple entities. */
5345 if (bitpos + bitsize > str_bitsize)
5346 return false;
5347
5348 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5349 bitpos = str_bitsize - bitpos - bitsize;
5350
5351 switch (code)
5352 {
5353 case PLUS_EXPR:
5354 case MINUS_EXPR:
5355 /* For now, just optimize the case of the topmost bitfield
5356 where we don't need to do any masking and also
5357 1 bit bitfields where xor can be used.
5358 We might win by one instruction for the other bitfields
5359 too if insv/extv instructions aren't used, so that
5360 can be added later. */
5361 if ((reverse || bitpos + bitsize != str_bitsize)
5362 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
5363 break;
5364
5365 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5366 value = convert_modes (str_mode,
5367 TYPE_MODE (TREE_TYPE (op1)), value,
5368 TYPE_UNSIGNED (TREE_TYPE (op1)));
5369
5370 /* We may be accessing data outside the field, which means
5371 we can alias adjacent data. */
5372 if (MEM_P (str_rtx))
5373 {
5374 str_rtx = shallow_copy_rtx (str_rtx);
5375 set_mem_alias_set (str_rtx, 0);
5376 set_mem_expr (str_rtx, 0);
5377 }
5378
5379 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
5380 {
5381 value = expand_and (str_mode, value, const1_rtx, NULL);
5382 binop = xor_optab;
5383 }
5384 else
5385 binop = code == PLUS_EXPR ? add_optab : sub_optab;
5386
5387 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5388 if (reverse)
5389 value = flip_storage_order (str_mode, value);
5390 result = expand_binop (str_mode, binop, str_rtx,
5391 value, str_rtx, 1, OPTAB_WIDEN);
5392 if (result != str_rtx)
5393 emit_move_insn (str_rtx, result);
5394 return true;
5395
5396 case BIT_IOR_EXPR:
5397 case BIT_XOR_EXPR:
5398 if (TREE_CODE (op1) != INTEGER_CST)
5399 break;
5400 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
5401 value = convert_modes (str_mode,
5402 TYPE_MODE (TREE_TYPE (op1)), value,
5403 TYPE_UNSIGNED (TREE_TYPE (op1)));
5404
5405 /* We may be accessing data outside the field, which means
5406 we can alias adjacent data. */
5407 if (MEM_P (str_rtx))
5408 {
5409 str_rtx = shallow_copy_rtx (str_rtx);
5410 set_mem_alias_set (str_rtx, 0);
5411 set_mem_expr (str_rtx, 0);
5412 }
5413
5414 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
5415 if (bitpos + bitsize != str_bitsize)
5416 {
5417 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
5418 str_mode);
5419 value = expand_and (str_mode, value, mask, NULL_RTX);
5420 }
5421 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
5422 if (reverse)
5423 value = flip_storage_order (str_mode, value);
5424 result = expand_binop (str_mode, binop, str_rtx,
5425 value, str_rtx, 1, OPTAB_WIDEN);
5426 if (result != str_rtx)
5427 emit_move_insn (str_rtx, result);
5428 return true;
5429
5430 default:
5431 break;
5432 }
5433
5434 return false;
5435 }
5436
5437 /* In the C++ memory model, consecutive bit fields in a structure are
5438 considered one memory location.
5439
5440 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
5441 returns the bit range of consecutive bits in which this COMPONENT_REF
5442 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
5443 and *OFFSET may be adjusted in the process.
5444
5445 If the access does not need to be restricted, 0 is returned in both
5446 *BITSTART and *BITEND. */
5447
5448 void
5449 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
5450 poly_int64_pod *bitpos, tree *offset)
5451 {
5452 poly_int64 bitoffset;
5453 tree field, repr;
5454
5455 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
5456
5457 field = TREE_OPERAND (exp, 1);
5458 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
5459 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
5460 need to limit the range we can access. */
5461 if (!repr)
5462 {
5463 *bitstart = *bitend = 0;
5464 return;
5465 }
5466
5467 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
5468 part of a larger bit field, then the representative does not serve any
5469 useful purpose. This can occur in Ada. */
5470 if (handled_component_p (TREE_OPERAND (exp, 0)))
5471 {
5472 machine_mode rmode;
5473 poly_int64 rbitsize, rbitpos;
5474 tree roffset;
5475 int unsignedp, reversep, volatilep = 0;
5476 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
5477 &roffset, &rmode, &unsignedp, &reversep,
5478 &volatilep);
5479 if (!multiple_p (rbitpos, BITS_PER_UNIT))
5480 {
5481 *bitstart = *bitend = 0;
5482 return;
5483 }
5484 }
5485
5486 /* Compute the adjustment to bitpos from the offset of the field
5487 relative to the representative. DECL_FIELD_OFFSET of field and
5488 repr are the same by construction if they are not constants,
5489 see finish_bitfield_layout. */
5490 poly_uint64 field_offset, repr_offset;
5491 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
5492 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
5493 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
5494 else
5495 bitoffset = 0;
5496 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
5497 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
5498
5499 /* If the adjustment is larger than bitpos, we would have a negative bit
5500 position for the lower bound and this may wreak havoc later. Adjust
5501 offset and bitpos to make the lower bound non-negative in that case. */
5502 if (maybe_gt (bitoffset, *bitpos))
5503 {
5504 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
5505 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
5506
5507 *bitpos += adjust_bits;
5508 if (*offset == NULL_TREE)
5509 *offset = size_int (-adjust_bytes);
5510 else
5511 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
5512 *bitstart = 0;
5513 }
5514 else
5515 *bitstart = *bitpos - bitoffset;
5516
5517 *bitend = *bitstart + tree_to_poly_uint64 (DECL_SIZE (repr)) - 1;
5518 }
5519
5520 /* Returns true if BASE is a DECL that does not reside in memory and
5521 has non-BLKmode. DECL_RTL must not be a MEM; if
5522 DECL_RTL was not set yet, return false. */
5523
5524 bool
5525 non_mem_decl_p (tree base)
5526 {
5527 if (!DECL_P (base)
5528 || TREE_ADDRESSABLE (base)
5529 || DECL_MODE (base) == BLKmode)
5530 return false;
5531
5532 if (!DECL_RTL_SET_P (base))
5533 return false;
5534
5535 return (!MEM_P (DECL_RTL (base)));
5536 }
5537
5538 /* Returns true if REF refers to an object that does not
5539 reside in memory and has non-BLKmode. */
5540
5541 bool
5542 mem_ref_refers_to_non_mem_p (tree ref)
5543 {
5544 tree base;
5545
5546 if (TREE_CODE (ref) == MEM_REF
5547 || TREE_CODE (ref) == TARGET_MEM_REF)
5548 {
5549 tree addr = TREE_OPERAND (ref, 0);
5550
5551 if (TREE_CODE (addr) != ADDR_EXPR)
5552 return false;
5553
5554 base = TREE_OPERAND (addr, 0);
5555 }
5556 else
5557 base = ref;
5558
5559 return non_mem_decl_p (base);
5560 }
5561
5562 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
5563 is true, try generating a nontemporal store. */
5564
5565 void
5566 expand_assignment (tree to, tree from, bool nontemporal)
5567 {
5568 rtx to_rtx = 0;
5569 rtx result;
5570 machine_mode mode;
5571 unsigned int align;
5572 enum insn_code icode;
5573
5574 /* Don't crash if the lhs of the assignment was erroneous. */
5575 if (TREE_CODE (to) == ERROR_MARK)
5576 {
5577 expand_normal (from);
5578 return;
5579 }
5580
5581 /* Optimize away no-op moves without side-effects. */
5582 if (operand_equal_p (to, from, 0))
5583 return;
5584
5585 /* Handle misaligned stores. */
5586 mode = TYPE_MODE (TREE_TYPE (to));
5587 if ((TREE_CODE (to) == MEM_REF
5588 || TREE_CODE (to) == TARGET_MEM_REF
5589 || DECL_P (to))
5590 && mode != BLKmode
5591 && !mem_ref_refers_to_non_mem_p (to)
5592 && ((align = get_object_alignment (to))
5593 < GET_MODE_ALIGNMENT (mode))
5594 && (((icode = optab_handler (movmisalign_optab, mode))
5595 != CODE_FOR_nothing)
5596 || targetm.slow_unaligned_access (mode, align)))
5597 {
5598 rtx reg, mem;
5599
5600 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
5601 /* Handle PARALLEL. */
5602 reg = maybe_emit_group_store (reg, TREE_TYPE (from));
5603 reg = force_not_mem (reg);
5604 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5605 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
5606 reg = flip_storage_order (mode, reg);
5607
5608 if (icode != CODE_FOR_nothing)
5609 {
5610 class expand_operand ops[2];
5611
5612 create_fixed_operand (&ops[0], mem);
5613 create_input_operand (&ops[1], reg, mode);
5614 /* The movmisalign<mode> pattern cannot fail, else the assignment
5615 would silently be omitted. */
5616 expand_insn (icode, 2, ops);
5617 }
5618 else
5619 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
5620 false, false);
5621 return;
5622 }
5623
5624 /* Assignment of a structure component needs special treatment
5625 if the structure component's rtx is not simply a MEM.
5626 Assignment of an array element at a constant index, and assignment of
5627 an array element in an unaligned packed structure field, has the same
5628 problem. Same for (partially) storing into a non-memory object. */
5629 if (handled_component_p (to)
5630 || (TREE_CODE (to) == MEM_REF
5631 && (REF_REVERSE_STORAGE_ORDER (to)
5632 || mem_ref_refers_to_non_mem_p (to)))
5633 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5634 {
5635 machine_mode mode1;
5636 poly_int64 bitsize, bitpos;
5637 poly_uint64 bitregion_start = 0;
5638 poly_uint64 bitregion_end = 0;
5639 tree offset;
5640 int unsignedp, reversep, volatilep = 0;
5641 tree tem;
5642
5643 push_temp_slots ();
5644 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5645 &unsignedp, &reversep, &volatilep);
5646
5647 /* Make sure bitpos is not negative, it can wreak havoc later. */
5648 if (maybe_lt (bitpos, 0))
5649 {
5650 gcc_assert (offset == NULL_TREE);
5651 offset = size_int (bits_to_bytes_round_down (bitpos));
5652 bitpos = num_trailing_bits (bitpos);
5653 }
5654
5655 if (TREE_CODE (to) == COMPONENT_REF
5656 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5657 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5658 /* The C++ memory model naturally applies to byte-aligned fields.
5659 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5660 BITSIZE are not byte-aligned, there is no need to limit the range
5661 we can access. This can occur with packed structures in Ada. */
5662 else if (maybe_gt (bitsize, 0)
5663 && multiple_p (bitsize, BITS_PER_UNIT)
5664 && multiple_p (bitpos, BITS_PER_UNIT))
5665 {
5666 bitregion_start = bitpos;
5667 bitregion_end = bitpos + bitsize - 1;
5668 }
5669
5670 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5671
5672 /* If the field has a mode, we want to access it in the
5673 field's mode, not the computed mode.
5674 If a MEM has VOIDmode (external with incomplete type),
5675 use BLKmode for it instead. */
5676 if (MEM_P (to_rtx))
5677 {
5678 if (mode1 != VOIDmode)
5679 to_rtx = adjust_address (to_rtx, mode1, 0);
5680 else if (GET_MODE (to_rtx) == VOIDmode)
5681 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5682 }
5683
5684 if (offset != 0)
5685 {
5686 machine_mode address_mode;
5687 rtx offset_rtx;
5688
5689 if (!MEM_P (to_rtx))
5690 {
5691 /* We can get constant negative offsets into arrays with broken
5692 user code. Translate this to a trap instead of ICEing. */
5693 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5694 expand_builtin_trap ();
5695 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5696 }
5697
5698 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5699 address_mode = get_address_mode (to_rtx);
5700 if (GET_MODE (offset_rtx) != address_mode)
5701 {
5702 /* We cannot be sure that the RTL in offset_rtx is valid outside
5703 of a memory address context, so force it into a register
5704 before attempting to convert it to the desired mode. */
5705 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5706 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5707 }
5708
5709 /* If we have an expression in OFFSET_RTX and a non-zero
5710 byte offset in BITPOS, adding the byte offset before the
5711 OFFSET_RTX results in better intermediate code, which makes
5712 later rtl optimization passes perform better.
5713
5714 We prefer intermediate code like this:
5715
5716 r124:DI=r123:DI+0x18
5717 [r124:DI]=r121:DI
5718
5719 ... instead of ...
5720
5721 r124:DI=r123:DI+0x10
5722 [r124:DI+0x8]=r121:DI
5723
5724 This is only done for aligned data values, as these can
5725 be expected to result in single move instructions. */
5726 poly_int64 bytepos;
5727 if (mode1 != VOIDmode
5728 && maybe_ne (bitpos, 0)
5729 && maybe_gt (bitsize, 0)
5730 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5731 && multiple_p (bitpos, bitsize)
5732 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5733 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5734 {
5735 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5736 bitregion_start = 0;
5737 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5738 bitregion_end -= bitpos;
5739 bitpos = 0;
5740 }
5741
5742 to_rtx = offset_address (to_rtx, offset_rtx,
5743 highest_pow2_factor_for_target (to,
5744 offset));
5745 }
5746
5747 /* No action is needed if the target is not a memory and the field
5748 lies completely outside that target. This can occur if the source
5749 code contains an out-of-bounds access to a small array. */
5750 if (!MEM_P (to_rtx)
5751 && GET_MODE (to_rtx) != BLKmode
5752 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5753 {
5754 expand_normal (from);
5755 result = NULL;
5756 }
5757 /* Handle expand_expr of a complex value returning a CONCAT. */
5758 else if (GET_CODE (to_rtx) == CONCAT)
5759 {
5760 machine_mode to_mode = GET_MODE (to_rtx);
5761 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5762 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5763 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5764 if (TYPE_MODE (TREE_TYPE (from)) == to_mode
5765 && known_eq (bitpos, 0)
5766 && known_eq (bitsize, mode_bitsize))
5767 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5768 else if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE_INNER (to_mode)
5769 && known_eq (bitsize, inner_bitsize)
5770 && (known_eq (bitpos, 0)
5771 || known_eq (bitpos, inner_bitsize)))
5772 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5773 false, nontemporal, reversep);
5774 else if (known_le (bitpos + bitsize, inner_bitsize))
5775 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5776 bitregion_start, bitregion_end,
5777 mode1, from, get_alias_set (to),
5778 nontemporal, reversep);
5779 else if (known_ge (bitpos, inner_bitsize))
5780 result = store_field (XEXP (to_rtx, 1), bitsize,
5781 bitpos - inner_bitsize,
5782 bitregion_start, bitregion_end,
5783 mode1, from, get_alias_set (to),
5784 nontemporal, reversep);
5785 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5786 {
5787 result = expand_normal (from);
5788 if (GET_CODE (result) == CONCAT)
5789 {
5790 to_mode = GET_MODE_INNER (to_mode);
5791 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5792 rtx from_real
5793 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5794 from_mode, 0);
5795 rtx from_imag
5796 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5797 from_mode, 0);
5798 if (!from_real || !from_imag)
5799 goto concat_store_slow;
5800 emit_move_insn (XEXP (to_rtx, 0), from_real);
5801 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5802 }
5803 else
5804 {
5805 machine_mode from_mode
5806 = GET_MODE (result) == VOIDmode
5807 ? TYPE_MODE (TREE_TYPE (from))
5808 : GET_MODE (result);
5809 rtx from_rtx;
5810 if (MEM_P (result))
5811 from_rtx = change_address (result, to_mode, NULL_RTX);
5812 else
5813 from_rtx
5814 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5815 if (from_rtx)
5816 {
5817 emit_move_insn (XEXP (to_rtx, 0),
5818 read_complex_part (from_rtx, false));
5819 emit_move_insn (XEXP (to_rtx, 1),
5820 read_complex_part (from_rtx, true));
5821 }
5822 else
5823 {
5824 to_mode = GET_MODE_INNER (to_mode);
5825 rtx from_real
5826 = simplify_gen_subreg (to_mode, result, from_mode, 0);
5827 rtx from_imag
5828 = simplify_gen_subreg (to_mode, result, from_mode,
5829 GET_MODE_SIZE (to_mode));
5830 if (!from_real || !from_imag)
5831 goto concat_store_slow;
5832 emit_move_insn (XEXP (to_rtx, 0), from_real);
5833 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5834 }
5835 }
5836 }
5837 else
5838 {
5839 concat_store_slow:;
5840 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5841 GET_MODE_SIZE (GET_MODE (to_rtx)));
5842 write_complex_part (temp, XEXP (to_rtx, 0), false, true);
5843 write_complex_part (temp, XEXP (to_rtx, 1), true, false);
5844 result = store_field (temp, bitsize, bitpos,
5845 bitregion_start, bitregion_end,
5846 mode1, from, get_alias_set (to),
5847 nontemporal, reversep);
5848 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5849 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5850 }
5851 }
5852 /* For calls to functions returning variable length structures, if TO_RTX
5853 is not a MEM, go through a MEM because we must not create temporaries
5854 of the VLA type. */
5855 else if (!MEM_P (to_rtx)
5856 && TREE_CODE (from) == CALL_EXPR
5857 && COMPLETE_TYPE_P (TREE_TYPE (from))
5858 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) != INTEGER_CST)
5859 {
5860 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5861 GET_MODE_SIZE (GET_MODE (to_rtx)));
5862 result = store_field (temp, bitsize, bitpos, bitregion_start,
5863 bitregion_end, mode1, from, get_alias_set (to),
5864 nontemporal, reversep);
5865 emit_move_insn (to_rtx, temp);
5866 }
5867 else
5868 {
5869 if (MEM_P (to_rtx))
5870 {
5871 /* If the field is at offset zero, we could have been given the
5872 DECL_RTX of the parent struct. Don't munge it. */
5873 to_rtx = shallow_copy_rtx (to_rtx);
5874 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5875 if (volatilep)
5876 MEM_VOLATILE_P (to_rtx) = 1;
5877 }
5878
5879 gcc_checking_assert (known_ge (bitpos, 0));
5880 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5881 bitregion_start, bitregion_end,
5882 mode1, to_rtx, to, from,
5883 reversep))
5884 result = NULL;
5885 else if (SUBREG_P (to_rtx)
5886 && SUBREG_PROMOTED_VAR_P (to_rtx))
5887 {
5888 /* If to_rtx is a promoted subreg, we need to zero or sign
5889 extend the value afterwards. */
5890 if (TREE_CODE (to) == MEM_REF
5891 && TYPE_MODE (TREE_TYPE (from)) != BLKmode
5892 && !REF_REVERSE_STORAGE_ORDER (to)
5893 && known_eq (bitpos, 0)
5894 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (to_rtx))))
5895 result = store_expr (from, to_rtx, 0, nontemporal, false);
5896 else
5897 {
5898 rtx to_rtx1
5899 = lowpart_subreg (subreg_unpromoted_mode (to_rtx),
5900 SUBREG_REG (to_rtx),
5901 subreg_promoted_mode (to_rtx));
5902 result = store_field (to_rtx1, bitsize, bitpos,
5903 bitregion_start, bitregion_end,
5904 mode1, from, get_alias_set (to),
5905 nontemporal, reversep);
5906 convert_move (SUBREG_REG (to_rtx), to_rtx1,
5907 SUBREG_PROMOTED_SIGN (to_rtx));
5908 }
5909 }
5910 else
5911 result = store_field (to_rtx, bitsize, bitpos,
5912 bitregion_start, bitregion_end,
5913 mode1, from, get_alias_set (to),
5914 nontemporal, reversep);
5915 }
5916
5917 if (result)
5918 preserve_temp_slots (result);
5919 pop_temp_slots ();
5920 return;
5921 }
5922
5923 /* If the rhs is a function call and its value is not an aggregate,
5924 call the function before we start to compute the lhs.
5925 This is needed for correct code for cases such as
5926 val = setjmp (buf) on machines where reference to val
5927 requires loading up part of an address in a separate insn.
5928
5929 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5930 since it might be a promoted variable where the zero- or sign- extension
5931 needs to be done. Handling this in the normal way is safe because no
5932 computation is done before the call. The same is true for SSA names. */
5933 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5934 && COMPLETE_TYPE_P (TREE_TYPE (from))
5935 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5936 && ! (((VAR_P (to)
5937 || TREE_CODE (to) == PARM_DECL
5938 || TREE_CODE (to) == RESULT_DECL)
5939 && REG_P (DECL_RTL (to)))
5940 || TREE_CODE (to) == SSA_NAME))
5941 {
5942 rtx value;
5943
5944 push_temp_slots ();
5945 value = expand_normal (from);
5946
5947 if (to_rtx == 0)
5948 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5949
5950 /* Handle calls that return values in multiple non-contiguous locations.
5951 The Irix 6 ABI has examples of this. */
5952 if (GET_CODE (to_rtx) == PARALLEL)
5953 {
5954 if (GET_CODE (value) == PARALLEL)
5955 emit_group_move (to_rtx, value);
5956 else
5957 emit_group_load (to_rtx, value, TREE_TYPE (from),
5958 int_size_in_bytes (TREE_TYPE (from)));
5959 }
5960 else if (GET_CODE (value) == PARALLEL)
5961 emit_group_store (to_rtx, value, TREE_TYPE (from),
5962 int_size_in_bytes (TREE_TYPE (from)));
5963 else if (GET_MODE (to_rtx) == BLKmode)
5964 {
5965 /* Handle calls that return BLKmode values in registers. */
5966 if (REG_P (value))
5967 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5968 else
5969 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5970 }
5971 else
5972 {
5973 if (POINTER_TYPE_P (TREE_TYPE (to)))
5974 value = convert_memory_address_addr_space
5975 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5976 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5977
5978 emit_move_insn (to_rtx, value);
5979 }
5980
5981 preserve_temp_slots (to_rtx);
5982 pop_temp_slots ();
5983 return;
5984 }
5985
5986 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5987 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5988
5989 /* Don't move directly into a return register. */
5990 if (TREE_CODE (to) == RESULT_DECL
5991 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5992 {
5993 rtx temp;
5994
5995 push_temp_slots ();
5996
5997 /* If the source is itself a return value, it still is in a pseudo at
5998 this point so we can move it back to the return register directly. */
5999 if (REG_P (to_rtx)
6000 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
6001 && TREE_CODE (from) != CALL_EXPR)
6002 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
6003 else
6004 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
6005
6006 /* Handle calls that return values in multiple non-contiguous locations.
6007 The Irix 6 ABI has examples of this. */
6008 if (GET_CODE (to_rtx) == PARALLEL)
6009 {
6010 if (GET_CODE (temp) == PARALLEL)
6011 emit_group_move (to_rtx, temp);
6012 else
6013 emit_group_load (to_rtx, temp, TREE_TYPE (from),
6014 int_size_in_bytes (TREE_TYPE (from)));
6015 }
6016 else if (temp)
6017 emit_move_insn (to_rtx, temp);
6018
6019 preserve_temp_slots (to_rtx);
6020 pop_temp_slots ();
6021 return;
6022 }
6023
6024 /* In case we are returning the contents of an object which overlaps
6025 the place the value is being stored, use a safe function when copying
6026 a value through a pointer into a structure value return block. */
6027 if (TREE_CODE (to) == RESULT_DECL
6028 && TREE_CODE (from) == INDIRECT_REF
6029 && ADDR_SPACE_GENERIC_P
6030 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
6031 && refs_may_alias_p (to, from)
6032 && cfun->returns_struct
6033 && !cfun->returns_pcc_struct)
6034 {
6035 rtx from_rtx, size;
6036
6037 push_temp_slots ();
6038 size = expr_size (from);
6039 from_rtx = expand_normal (from);
6040
6041 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
6042
6043 preserve_temp_slots (to_rtx);
6044 pop_temp_slots ();
6045 return;
6046 }
6047
6048 /* Compute FROM and store the value in the rtx we got. */
6049
6050 push_temp_slots ();
6051 result = store_expr (from, to_rtx, 0, nontemporal, false);
6052 preserve_temp_slots (result);
6053 pop_temp_slots ();
6054 return;
6055 }
6056
6057 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
6058 succeeded, false otherwise. */
6059
6060 bool
6061 emit_storent_insn (rtx to, rtx from)
6062 {
6063 class expand_operand ops[2];
6064 machine_mode mode = GET_MODE (to);
6065 enum insn_code code = optab_handler (storent_optab, mode);
6066
6067 if (code == CODE_FOR_nothing)
6068 return false;
6069
6070 create_fixed_operand (&ops[0], to);
6071 create_input_operand (&ops[1], from, mode);
6072 return maybe_expand_insn (code, 2, ops);
6073 }
6074
6075 /* Helper function for store_expr storing of STRING_CST. */
6076
6077 static rtx
6078 string_cst_read_str (void *data, void *, HOST_WIDE_INT offset,
6079 fixed_size_mode mode)
6080 {
6081 tree str = (tree) data;
6082
6083 gcc_assert (offset >= 0);
6084 if (offset >= TREE_STRING_LENGTH (str))
6085 return const0_rtx;
6086
6087 if ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
6088 > (unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (str))
6089 {
6090 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
6091 size_t l = TREE_STRING_LENGTH (str) - offset;
6092 memcpy (p, TREE_STRING_POINTER (str) + offset, l);
6093 memset (p + l, '\0', GET_MODE_SIZE (mode) - l);
6094 return c_readstr (p, as_a <scalar_int_mode> (mode), false);
6095 }
6096
6097 /* The by-pieces infrastructure does not try to pick a vector mode
6098 for storing STRING_CST. */
6099 return c_readstr (TREE_STRING_POINTER (str) + offset,
6100 as_a <scalar_int_mode> (mode), false);
6101 }
6102
6103 /* Generate code for computing expression EXP,
6104 and storing the value into TARGET.
6105
6106 If the mode is BLKmode then we may return TARGET itself.
6107 It turns out that in BLKmode it doesn't cause a problem.
6108 because C has no operators that could combine two different
6109 assignments into the same BLKmode object with different values
6110 with no sequence point. Will other languages need this to
6111 be more thorough?
6112
6113 If CALL_PARAM_P is nonzero, this is a store into a call param on the
6114 stack, and block moves may need to be treated specially.
6115
6116 If NONTEMPORAL is true, try using a nontemporal store instruction.
6117
6118 If REVERSE is true, the store is to be done in reverse order. */
6119
6120 rtx
6121 store_expr (tree exp, rtx target, int call_param_p,
6122 bool nontemporal, bool reverse)
6123 {
6124 rtx temp;
6125 rtx alt_rtl = NULL_RTX;
6126 location_t loc = curr_insn_location ();
6127 bool shortened_string_cst = false;
6128
6129 if (VOID_TYPE_P (TREE_TYPE (exp)))
6130 {
6131 /* C++ can generate ?: expressions with a throw expression in one
6132 branch and an rvalue in the other. Here, we resolve attempts to
6133 store the throw expression's nonexistent result. */
6134 gcc_assert (!call_param_p);
6135 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6136 return NULL_RTX;
6137 }
6138 if (TREE_CODE (exp) == COMPOUND_EXPR)
6139 {
6140 /* Perform first part of compound expression, then assign from second
6141 part. */
6142 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
6143 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6144 return store_expr (TREE_OPERAND (exp, 1), target,
6145 call_param_p, nontemporal, reverse);
6146 }
6147 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
6148 {
6149 /* For conditional expression, get safe form of the target. Then
6150 test the condition, doing the appropriate assignment on either
6151 side. This avoids the creation of unnecessary temporaries.
6152 For non-BLKmode, it is more efficient not to do this. */
6153
6154 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
6155
6156 do_pending_stack_adjust ();
6157 NO_DEFER_POP;
6158 jumpifnot (TREE_OPERAND (exp, 0), lab1,
6159 profile_probability::uninitialized ());
6160 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
6161 nontemporal, reverse);
6162 emit_jump_insn (targetm.gen_jump (lab2));
6163 emit_barrier ();
6164 emit_label (lab1);
6165 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
6166 nontemporal, reverse);
6167 emit_label (lab2);
6168 OK_DEFER_POP;
6169
6170 return NULL_RTX;
6171 }
6172 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
6173 /* If this is a scalar in a register that is stored in a wider mode
6174 than the declared mode, compute the result into its declared mode
6175 and then convert to the wider mode. Our value is the computed
6176 expression. */
6177 {
6178 rtx inner_target = 0;
6179 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
6180 scalar_int_mode inner_mode = subreg_promoted_mode (target);
6181
6182 /* We can do the conversion inside EXP, which will often result
6183 in some optimizations. Do the conversion in two steps: first
6184 change the signedness, if needed, then the extend. But don't
6185 do this if the type of EXP is a subtype of something else
6186 since then the conversion might involve more than just
6187 converting modes. */
6188 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
6189 && TREE_TYPE (TREE_TYPE (exp)) == 0
6190 && GET_MODE_PRECISION (outer_mode)
6191 == TYPE_PRECISION (TREE_TYPE (exp)))
6192 {
6193 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
6194 TYPE_UNSIGNED (TREE_TYPE (exp))))
6195 {
6196 /* Some types, e.g. Fortran's logical*4, won't have a signed
6197 version, so use the mode instead. */
6198 tree ntype
6199 = (signed_or_unsigned_type_for
6200 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
6201 if (ntype == NULL)
6202 ntype = lang_hooks.types.type_for_mode
6203 (TYPE_MODE (TREE_TYPE (exp)),
6204 SUBREG_PROMOTED_SIGN (target));
6205
6206 exp = fold_convert_loc (loc, ntype, exp);
6207 }
6208
6209 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
6210 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
6211 exp);
6212
6213 inner_target = SUBREG_REG (target);
6214 }
6215
6216 temp = expand_expr (exp, inner_target, VOIDmode,
6217 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
6218
6219
6220 /* If TEMP is a VOIDmode constant, use convert_modes to make
6221 sure that we properly convert it. */
6222 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
6223 {
6224 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
6225 temp, SUBREG_PROMOTED_SIGN (target));
6226 temp = convert_modes (inner_mode, outer_mode, temp,
6227 SUBREG_PROMOTED_SIGN (target));
6228 }
6229
6230 convert_move (SUBREG_REG (target), temp,
6231 SUBREG_PROMOTED_SIGN (target));
6232
6233 return NULL_RTX;
6234 }
6235 else if ((TREE_CODE (exp) == STRING_CST
6236 || (TREE_CODE (exp) == MEM_REF
6237 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6238 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6239 == STRING_CST
6240 && integer_zerop (TREE_OPERAND (exp, 1))))
6241 && !nontemporal && !call_param_p
6242 && MEM_P (target))
6243 {
6244 /* Optimize initialization of an array with a STRING_CST. */
6245 HOST_WIDE_INT exp_len, str_copy_len;
6246 rtx dest_mem;
6247 tree str = TREE_CODE (exp) == STRING_CST
6248 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6249
6250 exp_len = int_expr_size (exp);
6251 if (exp_len <= 0)
6252 goto normal_expr;
6253
6254 if (TREE_STRING_LENGTH (str) <= 0)
6255 goto normal_expr;
6256
6257 if (can_store_by_pieces (exp_len, string_cst_read_str, (void *) str,
6258 MEM_ALIGN (target), false))
6259 {
6260 store_by_pieces (target, exp_len, string_cst_read_str, (void *) str,
6261 MEM_ALIGN (target), false, RETURN_BEGIN);
6262 return NULL_RTX;
6263 }
6264
6265 str_copy_len = TREE_STRING_LENGTH (str);
6266
6267 /* Trailing NUL bytes in EXP will be handled by the call to
6268 clear_storage, which is more efficient than copying them from
6269 the STRING_CST, so trim those from STR_COPY_LEN. */
6270 while (str_copy_len)
6271 {
6272 if (TREE_STRING_POINTER (str)[str_copy_len - 1])
6273 break;
6274 str_copy_len--;
6275 }
6276
6277 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0)
6278 {
6279 str_copy_len += STORE_MAX_PIECES - 1;
6280 str_copy_len &= ~(STORE_MAX_PIECES - 1);
6281 }
6282 if (str_copy_len >= exp_len)
6283 goto normal_expr;
6284
6285 if (!can_store_by_pieces (str_copy_len, string_cst_read_str,
6286 (void *) str, MEM_ALIGN (target), false))
6287 goto normal_expr;
6288
6289 dest_mem = store_by_pieces (target, str_copy_len, string_cst_read_str,
6290 (void *) str, MEM_ALIGN (target), false,
6291 RETURN_END);
6292 clear_storage (adjust_address_1 (dest_mem, BLKmode, 0, 1, 1, 0,
6293 exp_len - str_copy_len),
6294 GEN_INT (exp_len - str_copy_len), BLOCK_OP_NORMAL);
6295 return NULL_RTX;
6296 }
6297 else
6298 {
6299 rtx tmp_target;
6300
6301 normal_expr:
6302 /* If we want to use a nontemporal or a reverse order store, force the
6303 value into a register first. */
6304 tmp_target = nontemporal || reverse ? NULL_RTX : target;
6305 tree rexp = exp;
6306 if (TREE_CODE (exp) == STRING_CST
6307 && tmp_target == target
6308 && GET_MODE (target) == BLKmode
6309 && TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
6310 {
6311 rtx size = expr_size (exp);
6312 if (CONST_INT_P (size)
6313 && size != const0_rtx
6314 && (UINTVAL (size)
6315 > ((unsigned HOST_WIDE_INT) TREE_STRING_LENGTH (exp) + 32)))
6316 {
6317 /* If the STRING_CST has much larger array type than
6318 TREE_STRING_LENGTH, only emit the TREE_STRING_LENGTH part of
6319 it into the rodata section as the code later on will use
6320 memset zero for the remainder anyway. See PR95052. */
6321 tmp_target = NULL_RTX;
6322 rexp = copy_node (exp);
6323 tree index
6324 = build_index_type (size_int (TREE_STRING_LENGTH (exp) - 1));
6325 TREE_TYPE (rexp) = build_array_type (TREE_TYPE (TREE_TYPE (exp)),
6326 index);
6327 shortened_string_cst = true;
6328 }
6329 }
6330 temp = expand_expr_real (rexp, tmp_target, GET_MODE (target),
6331 (call_param_p
6332 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
6333 &alt_rtl, false);
6334 if (shortened_string_cst)
6335 {
6336 gcc_assert (MEM_P (temp));
6337 temp = change_address (temp, BLKmode, NULL_RTX);
6338 }
6339 }
6340
6341 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
6342 the same as that of TARGET, adjust the constant. This is needed, for
6343 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
6344 only a word-sized value. */
6345 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
6346 && TREE_CODE (exp) != ERROR_MARK
6347 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
6348 {
6349 gcc_assert (!shortened_string_cst);
6350 if (GET_MODE_CLASS (GET_MODE (target))
6351 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
6352 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
6353 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
6354 {
6355 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
6356 TYPE_MODE (TREE_TYPE (exp)), 0);
6357 if (t)
6358 temp = t;
6359 }
6360 if (GET_MODE (temp) == VOIDmode)
6361 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
6362 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6363 }
6364
6365 /* If value was not generated in the target, store it there.
6366 Convert the value to TARGET's type first if necessary and emit the
6367 pending incrementations that have been queued when expanding EXP.
6368 Note that we cannot emit the whole queue blindly because this will
6369 effectively disable the POST_INC optimization later.
6370
6371 If TEMP and TARGET compare equal according to rtx_equal_p, but
6372 one or both of them are volatile memory refs, we have to distinguish
6373 two cases:
6374 - expand_expr has used TARGET. In this case, we must not generate
6375 another copy. This can be detected by TARGET being equal according
6376 to == .
6377 - expand_expr has not used TARGET - that means that the source just
6378 happens to have the same RTX form. Since temp will have been created
6379 by expand_expr, it will compare unequal according to == .
6380 We must generate a copy in this case, to reach the correct number
6381 of volatile memory references. */
6382
6383 if ((! rtx_equal_p (temp, target)
6384 || (temp != target && (side_effects_p (temp)
6385 || side_effects_p (target)
6386 || (MEM_P (temp)
6387 && !mems_same_for_tbaa_p (temp, target)))))
6388 && TREE_CODE (exp) != ERROR_MARK
6389 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
6390 but TARGET is not valid memory reference, TEMP will differ
6391 from TARGET although it is really the same location. */
6392 && !(alt_rtl
6393 && rtx_equal_p (alt_rtl, target)
6394 && !side_effects_p (alt_rtl)
6395 && !side_effects_p (target))
6396 /* If there's nothing to copy, don't bother. Don't call
6397 expr_size unless necessary, because some front-ends (C++)
6398 expr_size-hook must not be given objects that are not
6399 supposed to be bit-copied or bit-initialized. */
6400 && expr_size (exp) != const0_rtx)
6401 {
6402 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
6403 {
6404 gcc_assert (!shortened_string_cst);
6405 if (GET_MODE (target) == BLKmode)
6406 {
6407 /* Handle calls that return BLKmode values in registers. */
6408 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6409 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
6410 else
6411 store_bit_field (target,
6412 rtx_to_poly_int64 (expr_size (exp))
6413 * BITS_PER_UNIT,
6414 0, 0, 0, GET_MODE (temp), temp, reverse,
6415 false);
6416 }
6417 else
6418 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
6419 }
6420
6421 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
6422 {
6423 /* Handle copying a string constant into an array. The string
6424 constant may be shorter than the array. So copy just the string's
6425 actual length, and clear the rest. First get the size of the data
6426 type of the string, which is actually the size of the target. */
6427 rtx size = expr_size (exp);
6428
6429 if (CONST_INT_P (size)
6430 && INTVAL (size) < TREE_STRING_LENGTH (exp))
6431 emit_block_move (target, temp, size,
6432 (call_param_p
6433 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6434 else
6435 {
6436 machine_mode pointer_mode
6437 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
6438 machine_mode address_mode = get_address_mode (target);
6439
6440 /* Compute the size of the data to copy from the string. */
6441 tree copy_size
6442 = size_binop_loc (loc, MIN_EXPR,
6443 make_tree (sizetype, size),
6444 size_int (TREE_STRING_LENGTH (exp)));
6445 rtx copy_size_rtx
6446 = expand_expr (copy_size, NULL_RTX, VOIDmode,
6447 (call_param_p
6448 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
6449 rtx_code_label *label = 0;
6450
6451 /* Copy that much. */
6452 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
6453 TYPE_UNSIGNED (sizetype));
6454 emit_block_move (target, temp, copy_size_rtx,
6455 (call_param_p
6456 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6457
6458 /* Figure out how much is left in TARGET that we have to clear.
6459 Do all calculations in pointer_mode. */
6460 poly_int64 const_copy_size;
6461 if (poly_int_rtx_p (copy_size_rtx, &const_copy_size))
6462 {
6463 size = plus_constant (address_mode, size, -const_copy_size);
6464 target = adjust_address (target, BLKmode, const_copy_size);
6465 }
6466 else
6467 {
6468 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
6469 copy_size_rtx, NULL_RTX, 0,
6470 OPTAB_LIB_WIDEN);
6471
6472 if (GET_MODE (copy_size_rtx) != address_mode)
6473 copy_size_rtx = convert_to_mode (address_mode,
6474 copy_size_rtx,
6475 TYPE_UNSIGNED (sizetype));
6476
6477 target = offset_address (target, copy_size_rtx,
6478 highest_pow2_factor (copy_size));
6479 label = gen_label_rtx ();
6480 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
6481 GET_MODE (size), 0, label);
6482 }
6483
6484 if (size != const0_rtx)
6485 clear_storage (target, size, BLOCK_OP_NORMAL);
6486
6487 if (label)
6488 emit_label (label);
6489 }
6490 }
6491 else if (shortened_string_cst)
6492 gcc_unreachable ();
6493 /* Handle calls that return values in multiple non-contiguous locations.
6494 The Irix 6 ABI has examples of this. */
6495 else if (GET_CODE (target) == PARALLEL)
6496 {
6497 if (GET_CODE (temp) == PARALLEL)
6498 emit_group_move (target, temp);
6499 else
6500 emit_group_load (target, temp, TREE_TYPE (exp),
6501 int_size_in_bytes (TREE_TYPE (exp)));
6502 }
6503 else if (GET_CODE (temp) == PARALLEL)
6504 emit_group_store (target, temp, TREE_TYPE (exp),
6505 int_size_in_bytes (TREE_TYPE (exp)));
6506 else if (GET_MODE (temp) == BLKmode)
6507 emit_block_move (target, temp, expr_size (exp),
6508 (call_param_p
6509 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
6510 /* If we emit a nontemporal store, there is nothing else to do. */
6511 else if (nontemporal && emit_storent_insn (target, temp))
6512 ;
6513 else
6514 {
6515 if (reverse)
6516 temp = flip_storage_order (GET_MODE (target), temp);
6517 temp = force_operand (temp, target);
6518 if (temp != target)
6519 emit_move_insn (target, temp);
6520 }
6521 }
6522 else
6523 gcc_assert (!shortened_string_cst);
6524
6525 return NULL_RTX;
6526 }
6527 \f
6528 /* Return true if field F of structure TYPE is a flexible array. */
6529
6530 static bool
6531 flexible_array_member_p (const_tree f, const_tree type)
6532 {
6533 const_tree tf;
6534
6535 tf = TREE_TYPE (f);
6536 return (DECL_CHAIN (f) == NULL
6537 && TREE_CODE (tf) == ARRAY_TYPE
6538 && TYPE_DOMAIN (tf)
6539 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
6540 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
6541 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
6542 && int_size_in_bytes (type) >= 0);
6543 }
6544
6545 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
6546 must have in order for it to completely initialize a value of type TYPE.
6547 Return -1 if the number isn't known.
6548
6549 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
6550
6551 static HOST_WIDE_INT
6552 count_type_elements (const_tree type, bool for_ctor_p)
6553 {
6554 switch (TREE_CODE (type))
6555 {
6556 case ARRAY_TYPE:
6557 {
6558 tree nelts;
6559
6560 nelts = array_type_nelts (type);
6561 if (nelts && tree_fits_uhwi_p (nelts))
6562 {
6563 unsigned HOST_WIDE_INT n;
6564
6565 n = tree_to_uhwi (nelts) + 1;
6566 if (n == 0 || for_ctor_p)
6567 return n;
6568 else
6569 return n * count_type_elements (TREE_TYPE (type), false);
6570 }
6571 return for_ctor_p ? -1 : 1;
6572 }
6573
6574 case RECORD_TYPE:
6575 {
6576 unsigned HOST_WIDE_INT n;
6577 tree f;
6578
6579 n = 0;
6580 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6581 if (TREE_CODE (f) == FIELD_DECL)
6582 {
6583 if (!for_ctor_p)
6584 n += count_type_elements (TREE_TYPE (f), false);
6585 else if (!flexible_array_member_p (f, type))
6586 /* Don't count flexible arrays, which are not supposed
6587 to be initialized. */
6588 n += 1;
6589 }
6590
6591 return n;
6592 }
6593
6594 case UNION_TYPE:
6595 case QUAL_UNION_TYPE:
6596 {
6597 tree f;
6598 HOST_WIDE_INT n, m;
6599
6600 gcc_assert (!for_ctor_p);
6601 /* Estimate the number of scalars in each field and pick the
6602 maximum. Other estimates would do instead; the idea is simply
6603 to make sure that the estimate is not sensitive to the ordering
6604 of the fields. */
6605 n = 1;
6606 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
6607 if (TREE_CODE (f) == FIELD_DECL)
6608 {
6609 m = count_type_elements (TREE_TYPE (f), false);
6610 /* If the field doesn't span the whole union, add an extra
6611 scalar for the rest. */
6612 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
6613 TYPE_SIZE (type)) != 1)
6614 m++;
6615 if (n < m)
6616 n = m;
6617 }
6618 return n;
6619 }
6620
6621 case COMPLEX_TYPE:
6622 return 2;
6623
6624 case VECTOR_TYPE:
6625 {
6626 unsigned HOST_WIDE_INT nelts;
6627 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
6628 return nelts;
6629 else
6630 return -1;
6631 }
6632
6633 case INTEGER_TYPE:
6634 case REAL_TYPE:
6635 case FIXED_POINT_TYPE:
6636 case ENUMERAL_TYPE:
6637 case BOOLEAN_TYPE:
6638 case POINTER_TYPE:
6639 case OFFSET_TYPE:
6640 case REFERENCE_TYPE:
6641 case NULLPTR_TYPE:
6642 case OPAQUE_TYPE:
6643 return 1;
6644
6645 case ERROR_MARK:
6646 return 0;
6647
6648 case VOID_TYPE:
6649 case METHOD_TYPE:
6650 case FUNCTION_TYPE:
6651 case LANG_TYPE:
6652 default:
6653 gcc_unreachable ();
6654 }
6655 }
6656
6657 /* Helper for categorize_ctor_elements. Identical interface. */
6658
6659 static bool
6660 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6661 HOST_WIDE_INT *p_unique_nz_elts,
6662 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6663 {
6664 unsigned HOST_WIDE_INT idx;
6665 HOST_WIDE_INT nz_elts, unique_nz_elts, init_elts, num_fields;
6666 tree value, purpose, elt_type;
6667
6668 /* Whether CTOR is a valid constant initializer, in accordance with what
6669 initializer_constant_valid_p does. If inferred from the constructor
6670 elements, true until proven otherwise. */
6671 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
6672 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
6673
6674 nz_elts = 0;
6675 unique_nz_elts = 0;
6676 init_elts = 0;
6677 num_fields = 0;
6678 elt_type = NULL_TREE;
6679
6680 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
6681 {
6682 HOST_WIDE_INT mult = 1;
6683
6684 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
6685 {
6686 tree lo_index = TREE_OPERAND (purpose, 0);
6687 tree hi_index = TREE_OPERAND (purpose, 1);
6688
6689 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
6690 mult = (tree_to_uhwi (hi_index)
6691 - tree_to_uhwi (lo_index) + 1);
6692 }
6693 num_fields += mult;
6694 elt_type = TREE_TYPE (value);
6695
6696 switch (TREE_CODE (value))
6697 {
6698 case CONSTRUCTOR:
6699 {
6700 HOST_WIDE_INT nz = 0, unz = 0, ic = 0;
6701
6702 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &unz,
6703 &ic, p_complete);
6704
6705 nz_elts += mult * nz;
6706 unique_nz_elts += unz;
6707 init_elts += mult * ic;
6708
6709 if (const_from_elts_p && const_p)
6710 const_p = const_elt_p;
6711 }
6712 break;
6713
6714 case INTEGER_CST:
6715 case REAL_CST:
6716 case FIXED_CST:
6717 if (!initializer_zerop (value))
6718 {
6719 nz_elts += mult;
6720 unique_nz_elts++;
6721 }
6722 init_elts += mult;
6723 break;
6724
6725 case STRING_CST:
6726 nz_elts += mult * TREE_STRING_LENGTH (value);
6727 unique_nz_elts += TREE_STRING_LENGTH (value);
6728 init_elts += mult * TREE_STRING_LENGTH (value);
6729 break;
6730
6731 case COMPLEX_CST:
6732 if (!initializer_zerop (TREE_REALPART (value)))
6733 {
6734 nz_elts += mult;
6735 unique_nz_elts++;
6736 }
6737 if (!initializer_zerop (TREE_IMAGPART (value)))
6738 {
6739 nz_elts += mult;
6740 unique_nz_elts++;
6741 }
6742 init_elts += 2 * mult;
6743 break;
6744
6745 case VECTOR_CST:
6746 {
6747 /* We can only construct constant-length vectors using
6748 CONSTRUCTOR. */
6749 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6750 for (unsigned int i = 0; i < nunits; ++i)
6751 {
6752 tree v = VECTOR_CST_ELT (value, i);
6753 if (!initializer_zerop (v))
6754 {
6755 nz_elts += mult;
6756 unique_nz_elts++;
6757 }
6758 init_elts += mult;
6759 }
6760 }
6761 break;
6762
6763 default:
6764 {
6765 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6766 nz_elts += mult * tc;
6767 unique_nz_elts += tc;
6768 init_elts += mult * tc;
6769
6770 if (const_from_elts_p && const_p)
6771 const_p
6772 = initializer_constant_valid_p (value,
6773 elt_type,
6774 TYPE_REVERSE_STORAGE_ORDER
6775 (TREE_TYPE (ctor)))
6776 != NULL_TREE;
6777 }
6778 break;
6779 }
6780 }
6781
6782 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6783 num_fields, elt_type))
6784 *p_complete = false;
6785
6786 *p_nz_elts += nz_elts;
6787 *p_unique_nz_elts += unique_nz_elts;
6788 *p_init_elts += init_elts;
6789
6790 return const_p;
6791 }
6792
6793 /* Examine CTOR to discover:
6794 * how many scalar fields are set to nonzero values,
6795 and place it in *P_NZ_ELTS;
6796 * the same, but counting RANGE_EXPRs as multiplier of 1 instead of
6797 high - low + 1 (this can be useful for callers to determine ctors
6798 that could be cheaply initialized with - perhaps nested - loops
6799 compared to copied from huge read-only data),
6800 and place it in *P_UNIQUE_NZ_ELTS;
6801 * how many scalar fields in total are in CTOR,
6802 and place it in *P_ELT_COUNT.
6803 * whether the constructor is complete -- in the sense that every
6804 meaningful byte is explicitly given a value --
6805 and place it in *P_COMPLETE.
6806
6807 Return whether or not CTOR is a valid static constant initializer, the same
6808 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6809
6810 bool
6811 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6812 HOST_WIDE_INT *p_unique_nz_elts,
6813 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6814 {
6815 *p_nz_elts = 0;
6816 *p_unique_nz_elts = 0;
6817 *p_init_elts = 0;
6818 *p_complete = true;
6819
6820 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_unique_nz_elts,
6821 p_init_elts, p_complete);
6822 }
6823
6824 /* Return true if constructor CTOR is simple enough to be materialized
6825 in an integer mode register. Limit the size to WORDS words, which
6826 is 1 by default. */
6827
6828 bool
6829 immediate_const_ctor_p (const_tree ctor, unsigned int words)
6830 {
6831 /* Allow function to be called with a VAR_DECL's DECL_INITIAL. */
6832 if (!ctor || TREE_CODE (ctor) != CONSTRUCTOR)
6833 return false;
6834
6835 return TREE_CONSTANT (ctor)
6836 && !TREE_ADDRESSABLE (ctor)
6837 && CONSTRUCTOR_NELTS (ctor)
6838 && TREE_CODE (TREE_TYPE (ctor)) != ARRAY_TYPE
6839 && int_expr_size (ctor) <= words * UNITS_PER_WORD
6840 && initializer_constant_valid_for_bitfield_p (ctor);
6841 }
6842
6843 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6844 of which had type LAST_TYPE. Each element was itself a complete
6845 initializer, in the sense that every meaningful byte was explicitly
6846 given a value. Return true if the same is true for the constructor
6847 as a whole. */
6848
6849 bool
6850 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6851 const_tree last_type)
6852 {
6853 if (TREE_CODE (type) == UNION_TYPE
6854 || TREE_CODE (type) == QUAL_UNION_TYPE)
6855 {
6856 if (num_elts == 0)
6857 return false;
6858
6859 gcc_assert (num_elts == 1 && last_type);
6860
6861 /* ??? We could look at each element of the union, and find the
6862 largest element. Which would avoid comparing the size of the
6863 initialized element against any tail padding in the union.
6864 Doesn't seem worth the effort... */
6865 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6866 }
6867
6868 return count_type_elements (type, true) == num_elts;
6869 }
6870
6871 /* Return 1 if EXP contains mostly (3/4) zeros. */
6872
6873 static int
6874 mostly_zeros_p (const_tree exp)
6875 {
6876 if (TREE_CODE (exp) == CONSTRUCTOR)
6877 {
6878 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6879 bool complete_p;
6880
6881 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6882 &complete_p);
6883 return !complete_p || nz_elts < init_elts / 4;
6884 }
6885
6886 return initializer_zerop (exp);
6887 }
6888
6889 /* Return 1 if EXP contains all zeros. */
6890
6891 static int
6892 all_zeros_p (const_tree exp)
6893 {
6894 if (TREE_CODE (exp) == CONSTRUCTOR)
6895 {
6896 HOST_WIDE_INT nz_elts, unz_elts, init_elts;
6897 bool complete_p;
6898
6899 categorize_ctor_elements (exp, &nz_elts, &unz_elts, &init_elts,
6900 &complete_p);
6901 return nz_elts == 0;
6902 }
6903
6904 return initializer_zerop (exp);
6905 }
6906 \f
6907 /* Helper function for store_constructor.
6908 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6909 CLEARED is as for store_constructor.
6910 ALIAS_SET is the alias set to use for any stores.
6911 If REVERSE is true, the store is to be done in reverse order.
6912
6913 This provides a recursive shortcut back to store_constructor when it isn't
6914 necessary to go through store_field. This is so that we can pass through
6915 the cleared field to let store_constructor know that we may not have to
6916 clear a substructure if the outer structure has already been cleared. */
6917
6918 static void
6919 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6920 poly_uint64 bitregion_start,
6921 poly_uint64 bitregion_end,
6922 machine_mode mode,
6923 tree exp, int cleared,
6924 alias_set_type alias_set, bool reverse)
6925 {
6926 poly_int64 bytepos;
6927 poly_uint64 bytesize;
6928 if (TREE_CODE (exp) == CONSTRUCTOR
6929 /* We can only call store_constructor recursively if the size and
6930 bit position are on a byte boundary. */
6931 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6932 && maybe_ne (bitsize, 0U)
6933 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6934 /* If we have a nonzero bitpos for a register target, then we just
6935 let store_field do the bitfield handling. This is unlikely to
6936 generate unnecessary clear instructions anyways. */
6937 && (known_eq (bitpos, 0) || MEM_P (target)))
6938 {
6939 if (MEM_P (target))
6940 {
6941 machine_mode target_mode = GET_MODE (target);
6942 if (target_mode != BLKmode
6943 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6944 target_mode = BLKmode;
6945 target = adjust_address (target, target_mode, bytepos);
6946 }
6947
6948
6949 /* Update the alias set, if required. */
6950 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6951 && MEM_ALIAS_SET (target) != 0)
6952 {
6953 target = copy_rtx (target);
6954 set_mem_alias_set (target, alias_set);
6955 }
6956
6957 store_constructor (exp, target, cleared, bytesize, reverse);
6958 }
6959 else
6960 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6961 exp, alias_set, false, reverse);
6962 }
6963
6964
6965 /* Returns the number of FIELD_DECLs in TYPE. */
6966
6967 static int
6968 fields_length (const_tree type)
6969 {
6970 tree t = TYPE_FIELDS (type);
6971 int count = 0;
6972
6973 for (; t; t = DECL_CHAIN (t))
6974 if (TREE_CODE (t) == FIELD_DECL)
6975 ++count;
6976
6977 return count;
6978 }
6979
6980
6981 /* Store the value of constructor EXP into the rtx TARGET.
6982 TARGET is either a REG or a MEM; we know it cannot conflict, since
6983 safe_from_p has been called.
6984 CLEARED is true if TARGET is known to have been zero'd.
6985 SIZE is the number of bytes of TARGET we are allowed to modify: this
6986 may not be the same as the size of EXP if we are assigning to a field
6987 which has been packed to exclude padding bits.
6988 If REVERSE is true, the store is to be done in reverse order. */
6989
6990 void
6991 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6992 bool reverse)
6993 {
6994 tree type = TREE_TYPE (exp);
6995 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6996 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6997
6998 switch (TREE_CODE (type))
6999 {
7000 case RECORD_TYPE:
7001 case UNION_TYPE:
7002 case QUAL_UNION_TYPE:
7003 {
7004 unsigned HOST_WIDE_INT idx;
7005 tree field, value;
7006
7007 /* The storage order is specified for every aggregate type. */
7008 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7009
7010 /* If size is zero or the target is already cleared, do nothing. */
7011 if (known_eq (size, 0) || cleared)
7012 cleared = 1;
7013 /* We either clear the aggregate or indicate the value is dead. */
7014 else if ((TREE_CODE (type) == UNION_TYPE
7015 || TREE_CODE (type) == QUAL_UNION_TYPE)
7016 && ! CONSTRUCTOR_ELTS (exp))
7017 /* If the constructor is empty, clear the union. */
7018 {
7019 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7020 cleared = 1;
7021 }
7022
7023 /* If we are building a static constructor into a register,
7024 set the initial value as zero so we can fold the value into
7025 a constant. But if more than one register is involved,
7026 this probably loses. */
7027 else if (REG_P (target) && TREE_STATIC (exp)
7028 && known_le (GET_MODE_SIZE (GET_MODE (target)),
7029 REGMODE_NATURAL_SIZE (GET_MODE (target))))
7030 {
7031 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7032 cleared = 1;
7033 }
7034
7035 /* If the constructor has fewer fields than the structure or
7036 if we are initializing the structure to mostly zeros, clear
7037 the whole structure first. Don't do this if TARGET is a
7038 register whose mode size isn't equal to SIZE since
7039 clear_storage can't handle this case. */
7040 else if (known_size_p (size)
7041 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
7042 || mostly_zeros_p (exp))
7043 && (!REG_P (target)
7044 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
7045 {
7046 clear_storage (target, gen_int_mode (size, Pmode),
7047 BLOCK_OP_NORMAL);
7048 cleared = 1;
7049 }
7050
7051 if (REG_P (target) && !cleared)
7052 emit_clobber (target);
7053
7054 /* Store each element of the constructor into the
7055 corresponding field of TARGET. */
7056 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
7057 {
7058 machine_mode mode;
7059 HOST_WIDE_INT bitsize;
7060 HOST_WIDE_INT bitpos = 0;
7061 tree offset;
7062 rtx to_rtx = target;
7063
7064 /* Just ignore missing fields. We cleared the whole
7065 structure, above, if any fields are missing. */
7066 if (field == 0)
7067 continue;
7068
7069 if (cleared && initializer_zerop (value))
7070 continue;
7071
7072 if (tree_fits_uhwi_p (DECL_SIZE (field)))
7073 bitsize = tree_to_uhwi (DECL_SIZE (field));
7074 else
7075 gcc_unreachable ();
7076
7077 mode = DECL_MODE (field);
7078 if (DECL_BIT_FIELD (field))
7079 mode = VOIDmode;
7080
7081 offset = DECL_FIELD_OFFSET (field);
7082 if (tree_fits_shwi_p (offset)
7083 && tree_fits_shwi_p (bit_position (field)))
7084 {
7085 bitpos = int_bit_position (field);
7086 offset = NULL_TREE;
7087 }
7088 else
7089 gcc_unreachable ();
7090
7091 /* If this initializes a field that is smaller than a
7092 word, at the start of a word, try to widen it to a full
7093 word. This special case allows us to output C++ member
7094 function initializations in a form that the optimizers
7095 can understand. */
7096 if (WORD_REGISTER_OPERATIONS
7097 && REG_P (target)
7098 && bitsize < BITS_PER_WORD
7099 && bitpos % BITS_PER_WORD == 0
7100 && GET_MODE_CLASS (mode) == MODE_INT
7101 && TREE_CODE (value) == INTEGER_CST
7102 && exp_size >= 0
7103 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
7104 {
7105 type = TREE_TYPE (value);
7106
7107 if (TYPE_PRECISION (type) < BITS_PER_WORD)
7108 {
7109 type = lang_hooks.types.type_for_mode
7110 (word_mode, TYPE_UNSIGNED (type));
7111 value = fold_convert (type, value);
7112 /* Make sure the bits beyond the original bitsize are zero
7113 so that we can correctly avoid extra zeroing stores in
7114 later constructor elements. */
7115 tree bitsize_mask
7116 = wide_int_to_tree (type, wi::mask (bitsize, false,
7117 BITS_PER_WORD));
7118 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
7119 }
7120
7121 if (BYTES_BIG_ENDIAN)
7122 value
7123 = fold_build2 (LSHIFT_EXPR, type, value,
7124 build_int_cst (type,
7125 BITS_PER_WORD - bitsize));
7126 bitsize = BITS_PER_WORD;
7127 mode = word_mode;
7128 }
7129
7130 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
7131 && DECL_NONADDRESSABLE_P (field))
7132 {
7133 to_rtx = copy_rtx (to_rtx);
7134 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
7135 }
7136
7137 store_constructor_field (to_rtx, bitsize, bitpos,
7138 0, bitregion_end, mode,
7139 value, cleared,
7140 get_alias_set (TREE_TYPE (field)),
7141 reverse);
7142 }
7143 break;
7144 }
7145 case ARRAY_TYPE:
7146 {
7147 tree value, index;
7148 unsigned HOST_WIDE_INT i;
7149 int need_to_clear;
7150 tree domain;
7151 tree elttype = TREE_TYPE (type);
7152 int const_bounds_p;
7153 HOST_WIDE_INT minelt = 0;
7154 HOST_WIDE_INT maxelt = 0;
7155
7156 /* The storage order is specified for every aggregate type. */
7157 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
7158
7159 domain = TYPE_DOMAIN (type);
7160 const_bounds_p = (TYPE_MIN_VALUE (domain)
7161 && TYPE_MAX_VALUE (domain)
7162 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
7163 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
7164
7165 /* If we have constant bounds for the range of the type, get them. */
7166 if (const_bounds_p)
7167 {
7168 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
7169 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
7170 }
7171
7172 /* If the constructor has fewer elements than the array, clear
7173 the whole array first. Similarly if this is static
7174 constructor of a non-BLKmode object. */
7175 if (cleared)
7176 need_to_clear = 0;
7177 else if (REG_P (target) && TREE_STATIC (exp))
7178 need_to_clear = 1;
7179 else
7180 {
7181 unsigned HOST_WIDE_INT idx;
7182 HOST_WIDE_INT count = 0, zero_count = 0;
7183 need_to_clear = ! const_bounds_p;
7184
7185 /* This loop is a more accurate version of the loop in
7186 mostly_zeros_p (it handles RANGE_EXPR in an index). It
7187 is also needed to check for missing elements. */
7188 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
7189 {
7190 HOST_WIDE_INT this_node_count;
7191
7192 if (need_to_clear)
7193 break;
7194
7195 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7196 {
7197 tree lo_index = TREE_OPERAND (index, 0);
7198 tree hi_index = TREE_OPERAND (index, 1);
7199
7200 if (! tree_fits_uhwi_p (lo_index)
7201 || ! tree_fits_uhwi_p (hi_index))
7202 {
7203 need_to_clear = 1;
7204 break;
7205 }
7206
7207 this_node_count = (tree_to_uhwi (hi_index)
7208 - tree_to_uhwi (lo_index) + 1);
7209 }
7210 else
7211 this_node_count = 1;
7212
7213 count += this_node_count;
7214 if (mostly_zeros_p (value))
7215 zero_count += this_node_count;
7216 }
7217
7218 /* Clear the entire array first if there are any missing
7219 elements, or if the incidence of zero elements is >=
7220 75%. */
7221 if (! need_to_clear
7222 && (count < maxelt - minelt + 1
7223 || 4 * zero_count >= 3 * count))
7224 need_to_clear = 1;
7225 }
7226
7227 if (need_to_clear && maybe_gt (size, 0))
7228 {
7229 if (REG_P (target))
7230 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
7231 else
7232 clear_storage (target, gen_int_mode (size, Pmode),
7233 BLOCK_OP_NORMAL);
7234 cleared = 1;
7235 }
7236
7237 if (!cleared && REG_P (target))
7238 /* Inform later passes that the old value is dead. */
7239 emit_clobber (target);
7240
7241 /* Store each element of the constructor into the
7242 corresponding element of TARGET, determined by counting the
7243 elements. */
7244 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
7245 {
7246 machine_mode mode;
7247 poly_int64 bitsize;
7248 HOST_WIDE_INT bitpos;
7249 rtx xtarget = target;
7250
7251 if (cleared && initializer_zerop (value))
7252 continue;
7253
7254 mode = TYPE_MODE (elttype);
7255 if (mode != BLKmode)
7256 bitsize = GET_MODE_BITSIZE (mode);
7257 else if (!poly_int_tree_p (TYPE_SIZE (elttype), &bitsize))
7258 bitsize = -1;
7259
7260 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
7261 {
7262 tree lo_index = TREE_OPERAND (index, 0);
7263 tree hi_index = TREE_OPERAND (index, 1);
7264 rtx index_r, pos_rtx;
7265 HOST_WIDE_INT lo, hi, count;
7266 tree position;
7267
7268 /* If the range is constant and "small", unroll the loop. */
7269 if (const_bounds_p
7270 && tree_fits_shwi_p (lo_index)
7271 && tree_fits_shwi_p (hi_index)
7272 && (lo = tree_to_shwi (lo_index),
7273 hi = tree_to_shwi (hi_index),
7274 count = hi - lo + 1,
7275 (!MEM_P (target)
7276 || count <= 2
7277 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
7278 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
7279 <= 40 * 8)))))
7280 {
7281 lo -= minelt; hi -= minelt;
7282 for (; lo <= hi; lo++)
7283 {
7284 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
7285
7286 if (MEM_P (target)
7287 && !MEM_KEEP_ALIAS_SET_P (target)
7288 && TREE_CODE (type) == ARRAY_TYPE
7289 && TYPE_NONALIASED_COMPONENT (type))
7290 {
7291 target = copy_rtx (target);
7292 MEM_KEEP_ALIAS_SET_P (target) = 1;
7293 }
7294
7295 store_constructor_field
7296 (target, bitsize, bitpos, 0, bitregion_end,
7297 mode, value, cleared,
7298 get_alias_set (elttype), reverse);
7299 }
7300 }
7301 else
7302 {
7303 rtx_code_label *loop_start = gen_label_rtx ();
7304 rtx_code_label *loop_end = gen_label_rtx ();
7305 tree exit_cond;
7306
7307 expand_normal (hi_index);
7308
7309 index = build_decl (EXPR_LOCATION (exp),
7310 VAR_DECL, NULL_TREE, domain);
7311 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
7312 SET_DECL_RTL (index, index_r);
7313 store_expr (lo_index, index_r, 0, false, reverse);
7314
7315 /* Build the head of the loop. */
7316 do_pending_stack_adjust ();
7317 emit_label (loop_start);
7318
7319 /* Assign value to element index. */
7320 position =
7321 fold_convert (ssizetype,
7322 fold_build2 (MINUS_EXPR,
7323 TREE_TYPE (index),
7324 index,
7325 TYPE_MIN_VALUE (domain)));
7326
7327 position =
7328 size_binop (MULT_EXPR, position,
7329 fold_convert (ssizetype,
7330 TYPE_SIZE_UNIT (elttype)));
7331
7332 pos_rtx = expand_normal (position);
7333 xtarget = offset_address (target, pos_rtx,
7334 highest_pow2_factor (position));
7335 xtarget = adjust_address (xtarget, mode, 0);
7336 if (TREE_CODE (value) == CONSTRUCTOR)
7337 store_constructor (value, xtarget, cleared,
7338 exact_div (bitsize, BITS_PER_UNIT),
7339 reverse);
7340 else
7341 store_expr (value, xtarget, 0, false, reverse);
7342
7343 /* Generate a conditional jump to exit the loop. */
7344 exit_cond = build2 (LT_EXPR, integer_type_node,
7345 index, hi_index);
7346 jumpif (exit_cond, loop_end,
7347 profile_probability::uninitialized ());
7348
7349 /* Update the loop counter, and jump to the head of
7350 the loop. */
7351 expand_assignment (index,
7352 build2 (PLUS_EXPR, TREE_TYPE (index),
7353 index, integer_one_node),
7354 false);
7355
7356 emit_jump (loop_start);
7357
7358 /* Build the end of the loop. */
7359 emit_label (loop_end);
7360 }
7361 }
7362 else if ((index != 0 && ! tree_fits_shwi_p (index))
7363 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
7364 {
7365 tree position;
7366
7367 if (index == 0)
7368 index = ssize_int (1);
7369
7370 if (minelt)
7371 index = fold_convert (ssizetype,
7372 fold_build2 (MINUS_EXPR,
7373 TREE_TYPE (index),
7374 index,
7375 TYPE_MIN_VALUE (domain)));
7376
7377 position =
7378 size_binop (MULT_EXPR, index,
7379 fold_convert (ssizetype,
7380 TYPE_SIZE_UNIT (elttype)));
7381 xtarget = offset_address (target,
7382 expand_normal (position),
7383 highest_pow2_factor (position));
7384 xtarget = adjust_address (xtarget, mode, 0);
7385 store_expr (value, xtarget, 0, false, reverse);
7386 }
7387 else
7388 {
7389 if (index != 0)
7390 bitpos = ((tree_to_shwi (index) - minelt)
7391 * tree_to_uhwi (TYPE_SIZE (elttype)));
7392 else
7393 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
7394
7395 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
7396 && TREE_CODE (type) == ARRAY_TYPE
7397 && TYPE_NONALIASED_COMPONENT (type))
7398 {
7399 target = copy_rtx (target);
7400 MEM_KEEP_ALIAS_SET_P (target) = 1;
7401 }
7402 store_constructor_field (target, bitsize, bitpos, 0,
7403 bitregion_end, mode, value,
7404 cleared, get_alias_set (elttype),
7405 reverse);
7406 }
7407 }
7408 break;
7409 }
7410
7411 case VECTOR_TYPE:
7412 {
7413 unsigned HOST_WIDE_INT idx;
7414 constructor_elt *ce;
7415 int i;
7416 int need_to_clear;
7417 insn_code icode = CODE_FOR_nothing;
7418 tree elt;
7419 tree elttype = TREE_TYPE (type);
7420 int elt_size = vector_element_bits (type);
7421 machine_mode eltmode = TYPE_MODE (elttype);
7422 HOST_WIDE_INT bitsize;
7423 HOST_WIDE_INT bitpos;
7424 rtvec vector = NULL;
7425 poly_uint64 n_elts;
7426 unsigned HOST_WIDE_INT const_n_elts;
7427 alias_set_type alias;
7428 bool vec_vec_init_p = false;
7429 machine_mode mode = GET_MODE (target);
7430
7431 gcc_assert (eltmode != BLKmode);
7432
7433 /* Try using vec_duplicate_optab for uniform vectors. */
7434 if (!TREE_SIDE_EFFECTS (exp)
7435 && VECTOR_MODE_P (mode)
7436 && eltmode == GET_MODE_INNER (mode)
7437 && ((icode = optab_handler (vec_duplicate_optab, mode))
7438 != CODE_FOR_nothing)
7439 && (elt = uniform_vector_p (exp))
7440 && !VECTOR_TYPE_P (TREE_TYPE (elt)))
7441 {
7442 class expand_operand ops[2];
7443 create_output_operand (&ops[0], target, mode);
7444 create_input_operand (&ops[1], expand_normal (elt), eltmode);
7445 expand_insn (icode, 2, ops);
7446 if (!rtx_equal_p (target, ops[0].value))
7447 emit_move_insn (target, ops[0].value);
7448 break;
7449 }
7450
7451 n_elts = TYPE_VECTOR_SUBPARTS (type);
7452 if (REG_P (target)
7453 && VECTOR_MODE_P (mode)
7454 && n_elts.is_constant (&const_n_elts))
7455 {
7456 machine_mode emode = eltmode;
7457 bool vector_typed_elts_p = false;
7458
7459 if (CONSTRUCTOR_NELTS (exp)
7460 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
7461 == VECTOR_TYPE))
7462 {
7463 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
7464 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
7465 * TYPE_VECTOR_SUBPARTS (etype),
7466 n_elts));
7467 emode = TYPE_MODE (etype);
7468 vector_typed_elts_p = true;
7469 }
7470 icode = convert_optab_handler (vec_init_optab, mode, emode);
7471 if (icode != CODE_FOR_nothing)
7472 {
7473 unsigned int n = const_n_elts;
7474
7475 if (vector_typed_elts_p)
7476 {
7477 n = CONSTRUCTOR_NELTS (exp);
7478 vec_vec_init_p = true;
7479 }
7480 vector = rtvec_alloc (n);
7481 for (unsigned int k = 0; k < n; k++)
7482 RTVEC_ELT (vector, k) = CONST0_RTX (emode);
7483 }
7484 }
7485
7486 /* Compute the size of the elements in the CTOR. It differs
7487 from the size of the vector type elements only when the
7488 CTOR elements are vectors themselves. */
7489 tree val_type = (CONSTRUCTOR_NELTS (exp) != 0
7490 ? TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value)
7491 : elttype);
7492 if (VECTOR_TYPE_P (val_type))
7493 bitsize = tree_to_uhwi (TYPE_SIZE (val_type));
7494 else
7495 bitsize = elt_size;
7496
7497 /* If the constructor has fewer elements than the vector,
7498 clear the whole array first. Similarly if this is static
7499 constructor of a non-BLKmode object. */
7500 if (cleared)
7501 need_to_clear = 0;
7502 else if (REG_P (target) && TREE_STATIC (exp))
7503 need_to_clear = 1;
7504 else
7505 {
7506 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
7507 tree value;
7508
7509 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
7510 {
7511 int n_elts_here = bitsize / elt_size;
7512 count += n_elts_here;
7513 if (mostly_zeros_p (value))
7514 zero_count += n_elts_here;
7515 }
7516
7517 /* Clear the entire vector first if there are any missing elements,
7518 or if the incidence of zero elements is >= 75%. */
7519 need_to_clear = (maybe_lt (count, n_elts)
7520 || 4 * zero_count >= 3 * count);
7521 }
7522
7523 if (need_to_clear && maybe_gt (size, 0) && !vector)
7524 {
7525 if (REG_P (target))
7526 emit_move_insn (target, CONST0_RTX (mode));
7527 else
7528 clear_storage (target, gen_int_mode (size, Pmode),
7529 BLOCK_OP_NORMAL);
7530 cleared = 1;
7531 }
7532
7533 /* Inform later passes that the old value is dead. */
7534 if (!cleared && !vector && REG_P (target))
7535 emit_move_insn (target, CONST0_RTX (mode));
7536
7537 if (MEM_P (target))
7538 alias = MEM_ALIAS_SET (target);
7539 else
7540 alias = get_alias_set (elttype);
7541
7542 /* Store each element of the constructor into the corresponding
7543 element of TARGET, determined by counting the elements. */
7544 for (idx = 0, i = 0;
7545 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
7546 idx++, i += bitsize / elt_size)
7547 {
7548 HOST_WIDE_INT eltpos;
7549 tree value = ce->value;
7550
7551 if (cleared && initializer_zerop (value))
7552 continue;
7553
7554 if (ce->index)
7555 eltpos = tree_to_uhwi (ce->index);
7556 else
7557 eltpos = i;
7558
7559 if (vector)
7560 {
7561 if (vec_vec_init_p)
7562 {
7563 gcc_assert (ce->index == NULL_TREE);
7564 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
7565 eltpos = idx;
7566 }
7567 else
7568 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
7569 RTVEC_ELT (vector, eltpos) = expand_normal (value);
7570 }
7571 else
7572 {
7573 machine_mode value_mode
7574 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
7575 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
7576 bitpos = eltpos * elt_size;
7577 store_constructor_field (target, bitsize, bitpos, 0,
7578 bitregion_end, value_mode,
7579 value, cleared, alias, reverse);
7580 }
7581 }
7582
7583 if (vector)
7584 emit_insn (GEN_FCN (icode) (target,
7585 gen_rtx_PARALLEL (mode, vector)));
7586 break;
7587 }
7588
7589 default:
7590 gcc_unreachable ();
7591 }
7592 }
7593
7594 /* Store the value of EXP (an expression tree)
7595 into a subfield of TARGET which has mode MODE and occupies
7596 BITSIZE bits, starting BITPOS bits from the start of TARGET.
7597 If MODE is VOIDmode, it means that we are storing into a bit-field.
7598
7599 BITREGION_START is bitpos of the first bitfield in this region.
7600 BITREGION_END is the bitpos of the ending bitfield in this region.
7601 These two fields are 0, if the C++ memory model does not apply,
7602 or we are not interested in keeping track of bitfield regions.
7603
7604 Always return const0_rtx unless we have something particular to
7605 return.
7606
7607 ALIAS_SET is the alias set for the destination. This value will
7608 (in general) be different from that for TARGET, since TARGET is a
7609 reference to the containing structure.
7610
7611 If NONTEMPORAL is true, try generating a nontemporal store.
7612
7613 If REVERSE is true, the store is to be done in reverse order. */
7614
7615 static rtx
7616 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
7617 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
7618 machine_mode mode, tree exp,
7619 alias_set_type alias_set, bool nontemporal, bool reverse)
7620 {
7621 if (TREE_CODE (exp) == ERROR_MARK)
7622 return const0_rtx;
7623
7624 /* If we have nothing to store, do nothing unless the expression has
7625 side-effects. Don't do that for zero sized addressable lhs of
7626 calls. */
7627 if (known_eq (bitsize, 0)
7628 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7629 || TREE_CODE (exp) != CALL_EXPR))
7630 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
7631
7632 if (GET_CODE (target) == CONCAT)
7633 {
7634 /* We're storing into a struct containing a single __complex. */
7635
7636 gcc_assert (known_eq (bitpos, 0));
7637 return store_expr (exp, target, 0, nontemporal, reverse);
7638 }
7639
7640 /* If the structure is in a register or if the component
7641 is a bit field, we cannot use addressing to access it.
7642 Use bit-field techniques or SUBREG to store in it. */
7643
7644 poly_int64 decl_bitsize;
7645 if (mode == VOIDmode
7646 || (mode != BLKmode && ! direct_store[(int) mode]
7647 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
7648 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
7649 || REG_P (target)
7650 || GET_CODE (target) == SUBREG
7651 /* If the field isn't aligned enough to store as an ordinary memref,
7652 store it as a bit field. */
7653 || (mode != BLKmode
7654 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
7655 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
7656 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
7657 || !multiple_p (bitpos, BITS_PER_UNIT)))
7658 || (known_size_p (bitsize)
7659 && mode != BLKmode
7660 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
7661 /* If the RHS and field are a constant size and the size of the
7662 RHS isn't the same size as the bitfield, we must use bitfield
7663 operations. */
7664 || (known_size_p (bitsize)
7665 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
7666 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
7667 bitsize)
7668 /* Except for initialization of full bytes from a CONSTRUCTOR, which
7669 we will handle specially below. */
7670 && !(TREE_CODE (exp) == CONSTRUCTOR
7671 && multiple_p (bitsize, BITS_PER_UNIT))
7672 /* And except for bitwise copying of TREE_ADDRESSABLE types,
7673 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
7674 includes some extra padding. store_expr / expand_expr will in
7675 that case call get_inner_reference that will have the bitsize
7676 we check here and thus the block move will not clobber the
7677 padding that shouldn't be clobbered. In the future we could
7678 replace the TREE_ADDRESSABLE check with a check that
7679 get_base_address needs to live in memory. */
7680 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
7681 || TREE_CODE (exp) != COMPONENT_REF
7682 || !multiple_p (bitsize, BITS_PER_UNIT)
7683 || !multiple_p (bitpos, BITS_PER_UNIT)
7684 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
7685 &decl_bitsize)
7686 || maybe_ne (decl_bitsize, bitsize))
7687 /* A call with an addressable return type and return-slot
7688 optimization must not need bitfield operations but we must
7689 pass down the original target. */
7690 && (TREE_CODE (exp) != CALL_EXPR
7691 || !TREE_ADDRESSABLE (TREE_TYPE (exp))
7692 || !CALL_EXPR_RETURN_SLOT_OPT (exp)))
7693 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
7694 decl we must use bitfield operations. */
7695 || (known_size_p (bitsize)
7696 && TREE_CODE (exp) == MEM_REF
7697 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
7698 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7699 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
7700 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
7701 {
7702 rtx temp;
7703 gimple *nop_def;
7704
7705 /* If EXP is a NOP_EXPR of precision less than its mode, then that
7706 implies a mask operation. If the precision is the same size as
7707 the field we're storing into, that mask is redundant. This is
7708 particularly common with bit field assignments generated by the
7709 C front end. */
7710 nop_def = get_def_for_expr (exp, NOP_EXPR);
7711 if (nop_def)
7712 {
7713 tree type = TREE_TYPE (exp);
7714 if (INTEGRAL_TYPE_P (type)
7715 && maybe_ne (TYPE_PRECISION (type),
7716 GET_MODE_BITSIZE (TYPE_MODE (type)))
7717 && known_eq (bitsize, TYPE_PRECISION (type)))
7718 {
7719 tree op = gimple_assign_rhs1 (nop_def);
7720 type = TREE_TYPE (op);
7721 if (INTEGRAL_TYPE_P (type)
7722 && known_ge (TYPE_PRECISION (type), bitsize))
7723 exp = op;
7724 }
7725 }
7726
7727 temp = expand_normal (exp);
7728
7729 /* We don't support variable-sized BLKmode bitfields, since our
7730 handling of BLKmode is bound up with the ability to break
7731 things into words. */
7732 gcc_assert (mode != BLKmode || bitsize.is_constant ());
7733
7734 /* Handle calls that return values in multiple non-contiguous locations.
7735 The Irix 6 ABI has examples of this. */
7736 if (GET_CODE (temp) == PARALLEL)
7737 {
7738 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
7739 machine_mode temp_mode = GET_MODE (temp);
7740 if (temp_mode == BLKmode || temp_mode == VOIDmode)
7741 temp_mode = smallest_int_mode_for_size (size * BITS_PER_UNIT);
7742 rtx temp_target = gen_reg_rtx (temp_mode);
7743 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
7744 temp = temp_target;
7745 }
7746
7747 /* Handle calls that return BLKmode values in registers. */
7748 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
7749 {
7750 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
7751 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
7752 temp = temp_target;
7753 }
7754
7755 /* If the value has aggregate type and an integral mode then, if BITSIZE
7756 is narrower than this mode and this is for big-endian data, we first
7757 need to put the value into the low-order bits for store_bit_field,
7758 except when MODE is BLKmode and BITSIZE larger than the word size
7759 (see the handling of fields larger than a word in store_bit_field).
7760 Moreover, the field may be not aligned on a byte boundary; in this
7761 case, if it has reverse storage order, it needs to be accessed as a
7762 scalar field with reverse storage order and we must first put the
7763 value into target order. */
7764 scalar_int_mode temp_mode;
7765 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7766 && is_int_mode (GET_MODE (temp), &temp_mode))
7767 {
7768 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7769
7770 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7771
7772 if (reverse)
7773 temp = flip_storage_order (temp_mode, temp);
7774
7775 gcc_checking_assert (known_le (bitsize, size));
7776 if (maybe_lt (bitsize, size)
7777 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7778 /* Use of to_constant for BLKmode was checked above. */
7779 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7780 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7781 size - bitsize, NULL_RTX, 1);
7782 }
7783
7784 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7785 if (mode != VOIDmode && mode != BLKmode
7786 && mode != TYPE_MODE (TREE_TYPE (exp)))
7787 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7788
7789 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7790 and BITPOS must be aligned on a byte boundary. If so, we simply do
7791 a block copy. Likewise for a BLKmode-like TARGET. */
7792 if (GET_MODE (temp) == BLKmode
7793 && (GET_MODE (target) == BLKmode
7794 || (MEM_P (target)
7795 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7796 && multiple_p (bitpos, BITS_PER_UNIT)
7797 && multiple_p (bitsize, BITS_PER_UNIT))))
7798 {
7799 gcc_assert (MEM_P (target) && MEM_P (temp));
7800 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7801 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7802
7803 target = adjust_address (target, VOIDmode, bytepos);
7804 emit_block_move (target, temp,
7805 gen_int_mode (bytesize, Pmode),
7806 BLOCK_OP_NORMAL);
7807
7808 return const0_rtx;
7809 }
7810
7811 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7812 word size, we need to load the value (see again store_bit_field). */
7813 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7814 {
7815 temp_mode = smallest_int_mode_for_size (bitsize);
7816 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7817 temp_mode, false, NULL);
7818 }
7819
7820 /* Store the value in the bitfield. */
7821 gcc_checking_assert (known_ge (bitpos, 0));
7822 store_bit_field (target, bitsize, bitpos,
7823 bitregion_start, bitregion_end,
7824 mode, temp, reverse, false);
7825
7826 return const0_rtx;
7827 }
7828 else
7829 {
7830 /* Now build a reference to just the desired component. */
7831 rtx to_rtx = adjust_address (target, mode,
7832 exact_div (bitpos, BITS_PER_UNIT));
7833
7834 if (to_rtx == target)
7835 to_rtx = copy_rtx (to_rtx);
7836
7837 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7838 set_mem_alias_set (to_rtx, alias_set);
7839
7840 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7841 into a target smaller than its type; handle that case now. */
7842 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7843 {
7844 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7845 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7846 return to_rtx;
7847 }
7848
7849 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7850 }
7851 }
7852 \f
7853 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7854 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7855 codes and find the ultimate containing object, which we return.
7856
7857 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7858 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7859 storage order of the field.
7860 If the position of the field is variable, we store a tree
7861 giving the variable offset (in units) in *POFFSET.
7862 This offset is in addition to the bit position.
7863 If the position is not variable, we store 0 in *POFFSET.
7864
7865 If any of the extraction expressions is volatile,
7866 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7867
7868 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7869 Otherwise, it is a mode that can be used to access the field.
7870
7871 If the field describes a variable-sized object, *PMODE is set to
7872 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7873 this case, but the address of the object can be found. */
7874
7875 tree
7876 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7877 poly_int64_pod *pbitpos, tree *poffset,
7878 machine_mode *pmode, int *punsignedp,
7879 int *preversep, int *pvolatilep)
7880 {
7881 tree size_tree = 0;
7882 machine_mode mode = VOIDmode;
7883 bool blkmode_bitfield = false;
7884 tree offset = size_zero_node;
7885 poly_offset_int bit_offset = 0;
7886
7887 /* First get the mode, signedness, storage order and size. We do this from
7888 just the outermost expression. */
7889 *pbitsize = -1;
7890 if (TREE_CODE (exp) == COMPONENT_REF)
7891 {
7892 tree field = TREE_OPERAND (exp, 1);
7893 size_tree = DECL_SIZE (field);
7894 if (flag_strict_volatile_bitfields > 0
7895 && TREE_THIS_VOLATILE (exp)
7896 && DECL_BIT_FIELD_TYPE (field)
7897 && DECL_MODE (field) != BLKmode)
7898 /* Volatile bitfields should be accessed in the mode of the
7899 field's type, not the mode computed based on the bit
7900 size. */
7901 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7902 else if (!DECL_BIT_FIELD (field))
7903 {
7904 mode = DECL_MODE (field);
7905 /* For vector fields re-check the target flags, as DECL_MODE
7906 could have been set with different target flags than
7907 the current function has. */
7908 if (VECTOR_TYPE_P (TREE_TYPE (field))
7909 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7910 mode = TYPE_MODE (TREE_TYPE (field));
7911 }
7912 else if (DECL_MODE (field) == BLKmode)
7913 blkmode_bitfield = true;
7914
7915 *punsignedp = DECL_UNSIGNED (field);
7916 }
7917 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7918 {
7919 size_tree = TREE_OPERAND (exp, 1);
7920 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7921 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7922
7923 /* For vector element types with the correct size of access or for
7924 vector typed accesses use the mode of the access type. */
7925 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7926 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7927 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7928 || VECTOR_TYPE_P (TREE_TYPE (exp)))
7929 mode = TYPE_MODE (TREE_TYPE (exp));
7930 }
7931 else
7932 {
7933 mode = TYPE_MODE (TREE_TYPE (exp));
7934 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7935
7936 if (mode == BLKmode)
7937 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7938 else
7939 *pbitsize = GET_MODE_BITSIZE (mode);
7940 }
7941
7942 if (size_tree != 0)
7943 {
7944 if (! tree_fits_uhwi_p (size_tree))
7945 mode = BLKmode, *pbitsize = -1;
7946 else
7947 *pbitsize = tree_to_uhwi (size_tree);
7948 }
7949
7950 *preversep = reverse_storage_order_for_component_p (exp);
7951
7952 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7953 and find the ultimate containing object. */
7954 while (1)
7955 {
7956 switch (TREE_CODE (exp))
7957 {
7958 case BIT_FIELD_REF:
7959 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7960 break;
7961
7962 case COMPONENT_REF:
7963 {
7964 tree field = TREE_OPERAND (exp, 1);
7965 tree this_offset = component_ref_field_offset (exp);
7966
7967 /* If this field hasn't been filled in yet, don't go past it.
7968 This should only happen when folding expressions made during
7969 type construction. */
7970 if (this_offset == 0)
7971 break;
7972
7973 offset = size_binop (PLUS_EXPR, offset, this_offset);
7974 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7975
7976 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7977 }
7978 break;
7979
7980 case ARRAY_REF:
7981 case ARRAY_RANGE_REF:
7982 {
7983 tree index = TREE_OPERAND (exp, 1);
7984 tree low_bound = array_ref_low_bound (exp);
7985 tree unit_size = array_ref_element_size (exp);
7986
7987 /* We assume all arrays have sizes that are a multiple of a byte.
7988 First subtract the lower bound, if any, in the type of the
7989 index, then convert to sizetype and multiply by the size of
7990 the array element. */
7991 if (! integer_zerop (low_bound))
7992 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7993 index, low_bound);
7994
7995 offset = size_binop (PLUS_EXPR, offset,
7996 size_binop (MULT_EXPR,
7997 fold_convert (sizetype, index),
7998 unit_size));
7999 }
8000 break;
8001
8002 case REALPART_EXPR:
8003 break;
8004
8005 case IMAGPART_EXPR:
8006 bit_offset += *pbitsize;
8007 break;
8008
8009 case VIEW_CONVERT_EXPR:
8010 break;
8011
8012 case MEM_REF:
8013 /* Hand back the decl for MEM[&decl, off]. */
8014 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
8015 {
8016 tree off = TREE_OPERAND (exp, 1);
8017 if (!integer_zerop (off))
8018 {
8019 poly_offset_int boff = mem_ref_offset (exp);
8020 boff <<= LOG2_BITS_PER_UNIT;
8021 bit_offset += boff;
8022 }
8023 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
8024 }
8025 goto done;
8026
8027 default:
8028 goto done;
8029 }
8030
8031 /* If any reference in the chain is volatile, the effect is volatile. */
8032 if (TREE_THIS_VOLATILE (exp))
8033 *pvolatilep = 1;
8034
8035 exp = TREE_OPERAND (exp, 0);
8036 }
8037 done:
8038
8039 /* If OFFSET is constant, see if we can return the whole thing as a
8040 constant bit position. Make sure to handle overflow during
8041 this conversion. */
8042 if (poly_int_tree_p (offset))
8043 {
8044 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
8045 TYPE_PRECISION (sizetype));
8046 tem <<= LOG2_BITS_PER_UNIT;
8047 tem += bit_offset;
8048 if (tem.to_shwi (pbitpos))
8049 *poffset = offset = NULL_TREE;
8050 }
8051
8052 /* Otherwise, split it up. */
8053 if (offset)
8054 {
8055 /* Avoid returning a negative bitpos as this may wreak havoc later. */
8056 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
8057 {
8058 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
8059 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
8060 offset = size_binop (PLUS_EXPR, offset,
8061 build_int_cst (sizetype, bytes.force_shwi ()));
8062 }
8063
8064 *poffset = offset;
8065 }
8066
8067 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
8068 if (mode == VOIDmode
8069 && blkmode_bitfield
8070 && multiple_p (*pbitpos, BITS_PER_UNIT)
8071 && multiple_p (*pbitsize, BITS_PER_UNIT))
8072 *pmode = BLKmode;
8073 else
8074 *pmode = mode;
8075
8076 return exp;
8077 }
8078
8079 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
8080
8081 static unsigned HOST_WIDE_INT
8082 target_align (const_tree target)
8083 {
8084 /* We might have a chain of nested references with intermediate misaligning
8085 bitfields components, so need to recurse to find out. */
8086
8087 unsigned HOST_WIDE_INT this_align, outer_align;
8088
8089 switch (TREE_CODE (target))
8090 {
8091 case BIT_FIELD_REF:
8092 return 1;
8093
8094 case COMPONENT_REF:
8095 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
8096 outer_align = target_align (TREE_OPERAND (target, 0));
8097 return MIN (this_align, outer_align);
8098
8099 case ARRAY_REF:
8100 case ARRAY_RANGE_REF:
8101 this_align = TYPE_ALIGN (TREE_TYPE (target));
8102 outer_align = target_align (TREE_OPERAND (target, 0));
8103 return MIN (this_align, outer_align);
8104
8105 CASE_CONVERT:
8106 case NON_LVALUE_EXPR:
8107 case VIEW_CONVERT_EXPR:
8108 this_align = TYPE_ALIGN (TREE_TYPE (target));
8109 outer_align = target_align (TREE_OPERAND (target, 0));
8110 return MAX (this_align, outer_align);
8111
8112 default:
8113 return TYPE_ALIGN (TREE_TYPE (target));
8114 }
8115 }
8116
8117 \f
8118 /* Given an rtx VALUE that may contain additions and multiplications, return
8119 an equivalent value that just refers to a register, memory, or constant.
8120 This is done by generating instructions to perform the arithmetic and
8121 returning a pseudo-register containing the value.
8122
8123 The returned value may be a REG, SUBREG, MEM or constant. */
8124
8125 rtx
8126 force_operand (rtx value, rtx target)
8127 {
8128 rtx op1, op2;
8129 /* Use subtarget as the target for operand 0 of a binary operation. */
8130 rtx subtarget = get_subtarget (target);
8131 enum rtx_code code = GET_CODE (value);
8132
8133 /* Check for subreg applied to an expression produced by loop optimizer. */
8134 if (code == SUBREG
8135 && !REG_P (SUBREG_REG (value))
8136 && !MEM_P (SUBREG_REG (value)))
8137 {
8138 value
8139 = simplify_gen_subreg (GET_MODE (value),
8140 force_reg (GET_MODE (SUBREG_REG (value)),
8141 force_operand (SUBREG_REG (value),
8142 NULL_RTX)),
8143 GET_MODE (SUBREG_REG (value)),
8144 SUBREG_BYTE (value));
8145 code = GET_CODE (value);
8146 }
8147
8148 /* Check for a PIC address load. */
8149 if ((code == PLUS || code == MINUS)
8150 && XEXP (value, 0) == pic_offset_table_rtx
8151 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
8152 || GET_CODE (XEXP (value, 1)) == LABEL_REF
8153 || GET_CODE (XEXP (value, 1)) == CONST))
8154 {
8155 if (!subtarget)
8156 subtarget = gen_reg_rtx (GET_MODE (value));
8157 emit_move_insn (subtarget, value);
8158 return subtarget;
8159 }
8160
8161 if (ARITHMETIC_P (value))
8162 {
8163 op2 = XEXP (value, 1);
8164 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
8165 subtarget = 0;
8166 if (code == MINUS && CONST_INT_P (op2))
8167 {
8168 code = PLUS;
8169 op2 = negate_rtx (GET_MODE (value), op2);
8170 }
8171
8172 /* Check for an addition with OP2 a constant integer and our first
8173 operand a PLUS of a virtual register and something else. In that
8174 case, we want to emit the sum of the virtual register and the
8175 constant first and then add the other value. This allows virtual
8176 register instantiation to simply modify the constant rather than
8177 creating another one around this addition. */
8178 if (code == PLUS && CONST_INT_P (op2)
8179 && GET_CODE (XEXP (value, 0)) == PLUS
8180 && REG_P (XEXP (XEXP (value, 0), 0))
8181 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
8182 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
8183 {
8184 rtx temp = expand_simple_binop (GET_MODE (value), code,
8185 XEXP (XEXP (value, 0), 0), op2,
8186 subtarget, 0, OPTAB_LIB_WIDEN);
8187 return expand_simple_binop (GET_MODE (value), code, temp,
8188 force_operand (XEXP (XEXP (value,
8189 0), 1), 0),
8190 target, 0, OPTAB_LIB_WIDEN);
8191 }
8192
8193 op1 = force_operand (XEXP (value, 0), subtarget);
8194 op2 = force_operand (op2, NULL_RTX);
8195 switch (code)
8196 {
8197 case MULT:
8198 return expand_mult (GET_MODE (value), op1, op2, target, 1);
8199 case DIV:
8200 if (!INTEGRAL_MODE_P (GET_MODE (value)))
8201 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8202 target, 1, OPTAB_LIB_WIDEN);
8203 else
8204 return expand_divmod (0,
8205 FLOAT_MODE_P (GET_MODE (value))
8206 ? RDIV_EXPR : TRUNC_DIV_EXPR,
8207 GET_MODE (value), op1, op2, target, 0);
8208 case MOD:
8209 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8210 target, 0);
8211 case UDIV:
8212 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
8213 target, 1);
8214 case UMOD:
8215 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
8216 target, 1);
8217 case ASHIFTRT:
8218 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8219 target, 0, OPTAB_LIB_WIDEN);
8220 default:
8221 return expand_simple_binop (GET_MODE (value), code, op1, op2,
8222 target, 1, OPTAB_LIB_WIDEN);
8223 }
8224 }
8225 if (UNARY_P (value))
8226 {
8227 if (!target)
8228 target = gen_reg_rtx (GET_MODE (value));
8229 op1 = force_operand (XEXP (value, 0), NULL_RTX);
8230 switch (code)
8231 {
8232 case ZERO_EXTEND:
8233 case SIGN_EXTEND:
8234 case TRUNCATE:
8235 case FLOAT_EXTEND:
8236 case FLOAT_TRUNCATE:
8237 convert_move (target, op1, code == ZERO_EXTEND);
8238 return target;
8239
8240 case FIX:
8241 case UNSIGNED_FIX:
8242 expand_fix (target, op1, code == UNSIGNED_FIX);
8243 return target;
8244
8245 case FLOAT:
8246 case UNSIGNED_FLOAT:
8247 expand_float (target, op1, code == UNSIGNED_FLOAT);
8248 return target;
8249
8250 default:
8251 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
8252 }
8253 }
8254
8255 #ifdef INSN_SCHEDULING
8256 /* On machines that have insn scheduling, we want all memory reference to be
8257 explicit, so we need to deal with such paradoxical SUBREGs. */
8258 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
8259 value
8260 = simplify_gen_subreg (GET_MODE (value),
8261 force_reg (GET_MODE (SUBREG_REG (value)),
8262 force_operand (SUBREG_REG (value),
8263 NULL_RTX)),
8264 GET_MODE (SUBREG_REG (value)),
8265 SUBREG_BYTE (value));
8266 #endif
8267
8268 return value;
8269 }
8270 \f
8271 /* Subroutine of expand_expr: return nonzero iff there is no way that
8272 EXP can reference X, which is being modified. TOP_P is nonzero if this
8273 call is going to be used to determine whether we need a temporary
8274 for EXP, as opposed to a recursive call to this function.
8275
8276 It is always safe for this routine to return zero since it merely
8277 searches for optimization opportunities. */
8278
8279 int
8280 safe_from_p (const_rtx x, tree exp, int top_p)
8281 {
8282 rtx exp_rtl = 0;
8283 int i, nops;
8284
8285 if (x == 0
8286 /* If EXP has varying size, we MUST use a target since we currently
8287 have no way of allocating temporaries of variable size
8288 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
8289 So we assume here that something at a higher level has prevented a
8290 clash. This is somewhat bogus, but the best we can do. Only
8291 do this when X is BLKmode and when we are at the top level. */
8292 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
8293 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
8294 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
8295 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
8296 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
8297 != INTEGER_CST)
8298 && GET_MODE (x) == BLKmode)
8299 /* If X is in the outgoing argument area, it is always safe. */
8300 || (MEM_P (x)
8301 && (XEXP (x, 0) == virtual_outgoing_args_rtx
8302 || (GET_CODE (XEXP (x, 0)) == PLUS
8303 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
8304 return 1;
8305
8306 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
8307 find the underlying pseudo. */
8308 if (GET_CODE (x) == SUBREG)
8309 {
8310 x = SUBREG_REG (x);
8311 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8312 return 0;
8313 }
8314
8315 /* Now look at our tree code and possibly recurse. */
8316 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
8317 {
8318 case tcc_declaration:
8319 exp_rtl = DECL_RTL_IF_SET (exp);
8320 break;
8321
8322 case tcc_constant:
8323 return 1;
8324
8325 case tcc_exceptional:
8326 if (TREE_CODE (exp) == TREE_LIST)
8327 {
8328 while (1)
8329 {
8330 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
8331 return 0;
8332 exp = TREE_CHAIN (exp);
8333 if (!exp)
8334 return 1;
8335 if (TREE_CODE (exp) != TREE_LIST)
8336 return safe_from_p (x, exp, 0);
8337 }
8338 }
8339 else if (TREE_CODE (exp) == CONSTRUCTOR)
8340 {
8341 constructor_elt *ce;
8342 unsigned HOST_WIDE_INT idx;
8343
8344 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
8345 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
8346 || !safe_from_p (x, ce->value, 0))
8347 return 0;
8348 return 1;
8349 }
8350 else if (TREE_CODE (exp) == ERROR_MARK)
8351 return 1; /* An already-visited SAVE_EXPR? */
8352 else
8353 return 0;
8354
8355 case tcc_statement:
8356 /* The only case we look at here is the DECL_INITIAL inside a
8357 DECL_EXPR. */
8358 return (TREE_CODE (exp) != DECL_EXPR
8359 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
8360 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
8361 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
8362
8363 case tcc_binary:
8364 case tcc_comparison:
8365 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
8366 return 0;
8367 /* Fall through. */
8368
8369 case tcc_unary:
8370 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8371
8372 case tcc_expression:
8373 case tcc_reference:
8374 case tcc_vl_exp:
8375 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
8376 the expression. If it is set, we conflict iff we are that rtx or
8377 both are in memory. Otherwise, we check all operands of the
8378 expression recursively. */
8379
8380 switch (TREE_CODE (exp))
8381 {
8382 case ADDR_EXPR:
8383 /* If the operand is static or we are static, we can't conflict.
8384 Likewise if we don't conflict with the operand at all. */
8385 if (staticp (TREE_OPERAND (exp, 0))
8386 || TREE_STATIC (exp)
8387 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
8388 return 1;
8389
8390 /* Otherwise, the only way this can conflict is if we are taking
8391 the address of a DECL a that address if part of X, which is
8392 very rare. */
8393 exp = TREE_OPERAND (exp, 0);
8394 if (DECL_P (exp))
8395 {
8396 if (!DECL_RTL_SET_P (exp)
8397 || !MEM_P (DECL_RTL (exp)))
8398 return 0;
8399 else
8400 exp_rtl = XEXP (DECL_RTL (exp), 0);
8401 }
8402 break;
8403
8404 case MEM_REF:
8405 if (MEM_P (x)
8406 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
8407 get_alias_set (exp)))
8408 return 0;
8409 break;
8410
8411 case CALL_EXPR:
8412 /* Assume that the call will clobber all hard registers and
8413 all of memory. */
8414 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
8415 || MEM_P (x))
8416 return 0;
8417 break;
8418
8419 case WITH_CLEANUP_EXPR:
8420 case CLEANUP_POINT_EXPR:
8421 /* Lowered by gimplify.cc. */
8422 gcc_unreachable ();
8423
8424 case SAVE_EXPR:
8425 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
8426
8427 default:
8428 break;
8429 }
8430
8431 /* If we have an rtx, we do not need to scan our operands. */
8432 if (exp_rtl)
8433 break;
8434
8435 nops = TREE_OPERAND_LENGTH (exp);
8436 for (i = 0; i < nops; i++)
8437 if (TREE_OPERAND (exp, i) != 0
8438 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
8439 return 0;
8440
8441 break;
8442
8443 case tcc_type:
8444 /* Should never get a type here. */
8445 gcc_unreachable ();
8446 }
8447
8448 /* If we have an rtl, find any enclosed object. Then see if we conflict
8449 with it. */
8450 if (exp_rtl)
8451 {
8452 if (GET_CODE (exp_rtl) == SUBREG)
8453 {
8454 exp_rtl = SUBREG_REG (exp_rtl);
8455 if (REG_P (exp_rtl)
8456 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
8457 return 0;
8458 }
8459
8460 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
8461 are memory and they conflict. */
8462 return ! (rtx_equal_p (x, exp_rtl)
8463 || (MEM_P (x) && MEM_P (exp_rtl)
8464 && true_dependence (exp_rtl, VOIDmode, x)));
8465 }
8466
8467 /* If we reach here, it is safe. */
8468 return 1;
8469 }
8470
8471 \f
8472 /* Return the highest power of two that EXP is known to be a multiple of.
8473 This is used in updating alignment of MEMs in array references. */
8474
8475 unsigned HOST_WIDE_INT
8476 highest_pow2_factor (const_tree exp)
8477 {
8478 unsigned HOST_WIDE_INT ret;
8479 int trailing_zeros = tree_ctz (exp);
8480 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
8481 return BIGGEST_ALIGNMENT;
8482 ret = HOST_WIDE_INT_1U << trailing_zeros;
8483 if (ret > BIGGEST_ALIGNMENT)
8484 return BIGGEST_ALIGNMENT;
8485 return ret;
8486 }
8487
8488 /* Similar, except that the alignment requirements of TARGET are
8489 taken into account. Assume it is at least as aligned as its
8490 type, unless it is a COMPONENT_REF in which case the layout of
8491 the structure gives the alignment. */
8492
8493 static unsigned HOST_WIDE_INT
8494 highest_pow2_factor_for_target (const_tree target, const_tree exp)
8495 {
8496 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
8497 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
8498
8499 return MAX (factor, talign);
8500 }
8501 \f
8502 /* Convert the tree comparison code TCODE to the rtl one where the
8503 signedness is UNSIGNEDP. */
8504
8505 static enum rtx_code
8506 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
8507 {
8508 enum rtx_code code;
8509 switch (tcode)
8510 {
8511 case EQ_EXPR:
8512 code = EQ;
8513 break;
8514 case NE_EXPR:
8515 code = NE;
8516 break;
8517 case LT_EXPR:
8518 code = unsignedp ? LTU : LT;
8519 break;
8520 case LE_EXPR:
8521 code = unsignedp ? LEU : LE;
8522 break;
8523 case GT_EXPR:
8524 code = unsignedp ? GTU : GT;
8525 break;
8526 case GE_EXPR:
8527 code = unsignedp ? GEU : GE;
8528 break;
8529 case UNORDERED_EXPR:
8530 code = UNORDERED;
8531 break;
8532 case ORDERED_EXPR:
8533 code = ORDERED;
8534 break;
8535 case UNLT_EXPR:
8536 code = UNLT;
8537 break;
8538 case UNLE_EXPR:
8539 code = UNLE;
8540 break;
8541 case UNGT_EXPR:
8542 code = UNGT;
8543 break;
8544 case UNGE_EXPR:
8545 code = UNGE;
8546 break;
8547 case UNEQ_EXPR:
8548 code = UNEQ;
8549 break;
8550 case LTGT_EXPR:
8551 code = LTGT;
8552 break;
8553
8554 default:
8555 gcc_unreachable ();
8556 }
8557 return code;
8558 }
8559
8560 /* Subroutine of expand_expr. Expand the two operands of a binary
8561 expression EXP0 and EXP1 placing the results in OP0 and OP1.
8562 The value may be stored in TARGET if TARGET is nonzero. The
8563 MODIFIER argument is as documented by expand_expr. */
8564
8565 void
8566 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
8567 enum expand_modifier modifier)
8568 {
8569 if (! safe_from_p (target, exp1, 1))
8570 target = 0;
8571 if (operand_equal_p (exp0, exp1, 0))
8572 {
8573 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8574 *op1 = copy_rtx (*op0);
8575 }
8576 else
8577 {
8578 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
8579 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
8580 }
8581 }
8582
8583 \f
8584 /* Return a MEM that contains constant EXP. DEFER is as for
8585 output_constant_def and MODIFIER is as for expand_expr. */
8586
8587 static rtx
8588 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
8589 {
8590 rtx mem;
8591
8592 mem = output_constant_def (exp, defer);
8593 if (modifier != EXPAND_INITIALIZER)
8594 mem = use_anchored_address (mem);
8595 return mem;
8596 }
8597
8598 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
8599 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8600
8601 static rtx
8602 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
8603 enum expand_modifier modifier, addr_space_t as)
8604 {
8605 rtx result, subtarget;
8606 tree inner, offset;
8607 poly_int64 bitsize, bitpos;
8608 int unsignedp, reversep, volatilep = 0;
8609 machine_mode mode1;
8610
8611 /* If we are taking the address of a constant and are at the top level,
8612 we have to use output_constant_def since we can't call force_const_mem
8613 at top level. */
8614 /* ??? This should be considered a front-end bug. We should not be
8615 generating ADDR_EXPR of something that isn't an LVALUE. The only
8616 exception here is STRING_CST. */
8617 if (CONSTANT_CLASS_P (exp))
8618 {
8619 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
8620 if (modifier < EXPAND_SUM)
8621 result = force_operand (result, target);
8622 return result;
8623 }
8624
8625 /* Everything must be something allowed by is_gimple_addressable. */
8626 switch (TREE_CODE (exp))
8627 {
8628 case INDIRECT_REF:
8629 /* This case will happen via recursion for &a->b. */
8630 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
8631
8632 case MEM_REF:
8633 {
8634 tree tem = TREE_OPERAND (exp, 0);
8635 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8636 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
8637 return expand_expr (tem, target, tmode, modifier);
8638 }
8639
8640 case TARGET_MEM_REF:
8641 return addr_for_mem_ref (exp, as, true);
8642
8643 case CONST_DECL:
8644 /* Expand the initializer like constants above. */
8645 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
8646 0, modifier), 0);
8647 if (modifier < EXPAND_SUM)
8648 result = force_operand (result, target);
8649 return result;
8650
8651 case REALPART_EXPR:
8652 /* The real part of the complex number is always first, therefore
8653 the address is the same as the address of the parent object. */
8654 offset = 0;
8655 bitpos = 0;
8656 inner = TREE_OPERAND (exp, 0);
8657 break;
8658
8659 case IMAGPART_EXPR:
8660 /* The imaginary part of the complex number is always second.
8661 The expression is therefore always offset by the size of the
8662 scalar type. */
8663 offset = 0;
8664 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
8665 inner = TREE_OPERAND (exp, 0);
8666 break;
8667
8668 case COMPOUND_LITERAL_EXPR:
8669 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
8670 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
8671 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
8672 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
8673 the initializers aren't gimplified. */
8674 if (COMPOUND_LITERAL_EXPR_DECL (exp)
8675 && is_global_var (COMPOUND_LITERAL_EXPR_DECL (exp)))
8676 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
8677 target, tmode, modifier, as);
8678 /* FALLTHRU */
8679 default:
8680 /* If the object is a DECL, then expand it for its rtl. Don't bypass
8681 expand_expr, as that can have various side effects; LABEL_DECLs for
8682 example, may not have their DECL_RTL set yet. Expand the rtl of
8683 CONSTRUCTORs too, which should yield a memory reference for the
8684 constructor's contents. Assume language specific tree nodes can
8685 be expanded in some interesting way. */
8686 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
8687 if (DECL_P (exp)
8688 || TREE_CODE (exp) == CONSTRUCTOR
8689 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
8690 {
8691 result = expand_expr (exp, target, tmode,
8692 modifier == EXPAND_INITIALIZER
8693 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
8694
8695 /* If the DECL isn't in memory, then the DECL wasn't properly
8696 marked TREE_ADDRESSABLE, which will be either a front-end
8697 or a tree optimizer bug. */
8698
8699 gcc_assert (MEM_P (result));
8700 result = XEXP (result, 0);
8701
8702 /* ??? Is this needed anymore? */
8703 if (DECL_P (exp))
8704 TREE_USED (exp) = 1;
8705
8706 if (modifier != EXPAND_INITIALIZER
8707 && modifier != EXPAND_CONST_ADDRESS
8708 && modifier != EXPAND_SUM)
8709 result = force_operand (result, target);
8710 return result;
8711 }
8712
8713 /* Pass FALSE as the last argument to get_inner_reference although
8714 we are expanding to RTL. The rationale is that we know how to
8715 handle "aligning nodes" here: we can just bypass them because
8716 they won't change the final object whose address will be returned
8717 (they actually exist only for that purpose). */
8718 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
8719 &unsignedp, &reversep, &volatilep);
8720 break;
8721 }
8722
8723 /* We must have made progress. */
8724 gcc_assert (inner != exp);
8725
8726 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
8727 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
8728 inner alignment, force the inner to be sufficiently aligned. */
8729 if (CONSTANT_CLASS_P (inner)
8730 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
8731 {
8732 inner = copy_node (inner);
8733 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
8734 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
8735 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
8736 }
8737 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
8738
8739 if (offset)
8740 {
8741 rtx tmp;
8742
8743 if (modifier != EXPAND_NORMAL)
8744 result = force_operand (result, NULL);
8745 tmp = expand_expr (offset, NULL_RTX, tmode,
8746 modifier == EXPAND_INITIALIZER
8747 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
8748
8749 /* expand_expr is allowed to return an object in a mode other
8750 than TMODE. If it did, we need to convert. */
8751 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
8752 tmp = convert_modes (tmode, GET_MODE (tmp),
8753 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8754 result = convert_memory_address_addr_space (tmode, result, as);
8755 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8756
8757 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8758 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8759 else
8760 {
8761 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8762 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8763 1, OPTAB_LIB_WIDEN);
8764 }
8765 }
8766
8767 if (maybe_ne (bitpos, 0))
8768 {
8769 /* Someone beforehand should have rejected taking the address
8770 of an object that isn't byte-aligned. */
8771 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8772 result = convert_memory_address_addr_space (tmode, result, as);
8773 result = plus_constant (tmode, result, bytepos);
8774 if (modifier < EXPAND_SUM)
8775 result = force_operand (result, target);
8776 }
8777
8778 return result;
8779 }
8780
8781 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8782 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8783
8784 static rtx
8785 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8786 enum expand_modifier modifier)
8787 {
8788 addr_space_t as = ADDR_SPACE_GENERIC;
8789 scalar_int_mode address_mode = Pmode;
8790 scalar_int_mode pointer_mode = ptr_mode;
8791 machine_mode rmode;
8792 rtx result;
8793
8794 /* Target mode of VOIDmode says "whatever's natural". */
8795 if (tmode == VOIDmode)
8796 tmode = TYPE_MODE (TREE_TYPE (exp));
8797
8798 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8799 {
8800 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8801 address_mode = targetm.addr_space.address_mode (as);
8802 pointer_mode = targetm.addr_space.pointer_mode (as);
8803 }
8804
8805 /* We can get called with some Weird Things if the user does silliness
8806 like "(short) &a". In that case, convert_memory_address won't do
8807 the right thing, so ignore the given target mode. */
8808 scalar_int_mode new_tmode = (tmode == pointer_mode
8809 ? pointer_mode
8810 : address_mode);
8811
8812 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8813 new_tmode, modifier, as);
8814
8815 /* Despite expand_expr claims concerning ignoring TMODE when not
8816 strictly convenient, stuff breaks if we don't honor it. Note
8817 that combined with the above, we only do this for pointer modes. */
8818 rmode = GET_MODE (result);
8819 if (rmode == VOIDmode)
8820 rmode = new_tmode;
8821 if (rmode != new_tmode)
8822 result = convert_memory_address_addr_space (new_tmode, result, as);
8823
8824 return result;
8825 }
8826
8827 /* Generate code for computing CONSTRUCTOR EXP.
8828 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8829 is TRUE, instead of creating a temporary variable in memory
8830 NULL is returned and the caller needs to handle it differently. */
8831
8832 static rtx
8833 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8834 bool avoid_temp_mem)
8835 {
8836 tree type = TREE_TYPE (exp);
8837 machine_mode mode = TYPE_MODE (type);
8838
8839 /* Try to avoid creating a temporary at all. This is possible
8840 if all of the initializer is zero.
8841 FIXME: try to handle all [0..255] initializers we can handle
8842 with memset. */
8843 if (TREE_STATIC (exp)
8844 && !TREE_ADDRESSABLE (exp)
8845 && target != 0 && mode == BLKmode
8846 && all_zeros_p (exp))
8847 {
8848 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8849 return target;
8850 }
8851
8852 /* All elts simple constants => refer to a constant in memory. But
8853 if this is a non-BLKmode mode, let it store a field at a time
8854 since that should make a CONST_INT, CONST_WIDE_INT or
8855 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8856 use, it is best to store directly into the target unless the type
8857 is large enough that memcpy will be used. If we are making an
8858 initializer and all operands are constant, put it in memory as
8859 well.
8860
8861 FIXME: Avoid trying to fill vector constructors piece-meal.
8862 Output them with output_constant_def below unless we're sure
8863 they're zeros. This should go away when vector initializers
8864 are treated like VECTOR_CST instead of arrays. */
8865 if ((TREE_STATIC (exp)
8866 && ((mode == BLKmode
8867 && ! (target != 0 && safe_from_p (target, exp, 1)))
8868 || TREE_ADDRESSABLE (exp)
8869 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8870 && (! can_move_by_pieces
8871 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8872 TYPE_ALIGN (type)))
8873 && ! mostly_zeros_p (exp))))
8874 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8875 && TREE_CONSTANT (exp)))
8876 {
8877 rtx constructor;
8878
8879 if (avoid_temp_mem)
8880 return NULL_RTX;
8881
8882 constructor = expand_expr_constant (exp, 1, modifier);
8883
8884 if (modifier != EXPAND_CONST_ADDRESS
8885 && modifier != EXPAND_INITIALIZER
8886 && modifier != EXPAND_SUM)
8887 constructor = validize_mem (constructor);
8888
8889 return constructor;
8890 }
8891
8892 /* If the CTOR is available in static storage and not mostly
8893 zeros and we can move it by pieces prefer to do so since
8894 that's usually more efficient than performing a series of
8895 stores from immediates. */
8896 if (avoid_temp_mem
8897 && TREE_STATIC (exp)
8898 && TREE_CONSTANT (exp)
8899 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8900 && can_move_by_pieces (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8901 TYPE_ALIGN (type))
8902 && ! mostly_zeros_p (exp))
8903 return NULL_RTX;
8904
8905 /* Handle calls that pass values in multiple non-contiguous
8906 locations. The Irix 6 ABI has examples of this. */
8907 if (target == 0 || ! safe_from_p (target, exp, 1)
8908 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM
8909 /* Also make a temporary if the store is to volatile memory, to
8910 avoid individual accesses to aggregate members. */
8911 || (GET_CODE (target) == MEM
8912 && MEM_VOLATILE_P (target)
8913 && !TREE_ADDRESSABLE (TREE_TYPE (exp))))
8914 {
8915 if (avoid_temp_mem)
8916 return NULL_RTX;
8917
8918 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8919 }
8920
8921 store_constructor (exp, target, 0, int_expr_size (exp), false);
8922 return target;
8923 }
8924
8925
8926 /* expand_expr: generate code for computing expression EXP.
8927 An rtx for the computed value is returned. The value is never null.
8928 In the case of a void EXP, const0_rtx is returned.
8929
8930 The value may be stored in TARGET if TARGET is nonzero.
8931 TARGET is just a suggestion; callers must assume that
8932 the rtx returned may not be the same as TARGET.
8933
8934 If TARGET is CONST0_RTX, it means that the value will be ignored.
8935
8936 If TMODE is not VOIDmode, it suggests generating the
8937 result in mode TMODE. But this is done only when convenient.
8938 Otherwise, TMODE is ignored and the value generated in its natural mode.
8939 TMODE is just a suggestion; callers must assume that
8940 the rtx returned may not have mode TMODE.
8941
8942 Note that TARGET may have neither TMODE nor MODE. In that case, it
8943 probably will not be used.
8944
8945 If MODIFIER is EXPAND_SUM then when EXP is an addition
8946 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8947 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8948 products as above, or REG or MEM, or constant.
8949 Ordinarily in such cases we would output mul or add instructions
8950 and then return a pseudo reg containing the sum.
8951
8952 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8953 it also marks a label as absolutely required (it can't be dead).
8954 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8955 This is used for outputting expressions used in initializers.
8956
8957 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8958 with a constant address even if that address is not normally legitimate.
8959 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8960
8961 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8962 a call parameter. Such targets require special care as we haven't yet
8963 marked TARGET so that it's safe from being trashed by libcalls. We
8964 don't want to use TARGET for anything but the final result;
8965 Intermediate values must go elsewhere. Additionally, calls to
8966 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8967
8968 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8969 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8970 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8971 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8972 recursively.
8973 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
8974 then *ALT_RTL is set to TARGET (before legitimziation).
8975
8976 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8977 In this case, we don't adjust a returned MEM rtx that wouldn't be
8978 sufficiently aligned for its mode; instead, it's up to the caller
8979 to deal with it afterwards. This is used to make sure that unaligned
8980 base objects for which out-of-bounds accesses are supported, for
8981 example record types with trailing arrays, aren't realigned behind
8982 the back of the caller.
8983 The normal operating mode is to pass FALSE for this parameter. */
8984
8985 rtx
8986 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8987 enum expand_modifier modifier, rtx *alt_rtl,
8988 bool inner_reference_p)
8989 {
8990 rtx ret;
8991
8992 /* Handle ERROR_MARK before anybody tries to access its type. */
8993 if (TREE_CODE (exp) == ERROR_MARK
8994 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8995 {
8996 ret = CONST0_RTX (tmode);
8997 return ret ? ret : const0_rtx;
8998 }
8999
9000 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
9001 inner_reference_p);
9002 return ret;
9003 }
9004
9005 /* Try to expand the conditional expression which is represented by
9006 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
9007 return the rtl reg which represents the result. Otherwise return
9008 NULL_RTX. */
9009
9010 static rtx
9011 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
9012 tree treeop1 ATTRIBUTE_UNUSED,
9013 tree treeop2 ATTRIBUTE_UNUSED)
9014 {
9015 rtx insn;
9016 rtx op00, op01, op1, op2;
9017 enum rtx_code comparison_code;
9018 machine_mode comparison_mode;
9019 gimple *srcstmt;
9020 rtx temp;
9021 tree type = TREE_TYPE (treeop1);
9022 int unsignedp = TYPE_UNSIGNED (type);
9023 machine_mode mode = TYPE_MODE (type);
9024 machine_mode orig_mode = mode;
9025 static bool expanding_cond_expr_using_cmove = false;
9026
9027 /* Conditional move expansion can end up TERing two operands which,
9028 when recursively hitting conditional expressions can result in
9029 exponential behavior if the cmove expansion ultimatively fails.
9030 It's hardly profitable to TER a cmove into a cmove so avoid doing
9031 that by failing early if we end up recursing. */
9032 if (expanding_cond_expr_using_cmove)
9033 return NULL_RTX;
9034
9035 /* If we cannot do a conditional move on the mode, try doing it
9036 with the promoted mode. */
9037 if (!can_conditionally_move_p (mode))
9038 {
9039 mode = promote_mode (type, mode, &unsignedp);
9040 if (!can_conditionally_move_p (mode))
9041 return NULL_RTX;
9042 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
9043 }
9044 else
9045 temp = assign_temp (type, 0, 1);
9046
9047 expanding_cond_expr_using_cmove = true;
9048 start_sequence ();
9049 expand_operands (treeop1, treeop2,
9050 mode == orig_mode ? temp : NULL_RTX, &op1, &op2,
9051 EXPAND_NORMAL);
9052
9053 if (TREE_CODE (treeop0) == SSA_NAME
9054 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
9055 {
9056 type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
9057 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
9058 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
9059 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
9060 comparison_mode = TYPE_MODE (type);
9061 unsignedp = TYPE_UNSIGNED (type);
9062 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9063 }
9064 else if (COMPARISON_CLASS_P (treeop0))
9065 {
9066 type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
9067 enum tree_code cmpcode = TREE_CODE (treeop0);
9068 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
9069 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
9070 unsignedp = TYPE_UNSIGNED (type);
9071 comparison_mode = TYPE_MODE (type);
9072 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
9073 }
9074 else
9075 {
9076 op00 = expand_normal (treeop0);
9077 op01 = const0_rtx;
9078 comparison_code = NE;
9079 comparison_mode = GET_MODE (op00);
9080 if (comparison_mode == VOIDmode)
9081 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
9082 }
9083 expanding_cond_expr_using_cmove = false;
9084
9085 if (GET_MODE (op1) != mode)
9086 op1 = gen_lowpart (mode, op1);
9087
9088 if (GET_MODE (op2) != mode)
9089 op2 = gen_lowpart (mode, op2);
9090
9091 /* Try to emit the conditional move. */
9092 insn = emit_conditional_move (temp,
9093 { comparison_code, op00, op01,
9094 comparison_mode },
9095 op1, op2, mode,
9096 unsignedp);
9097
9098 /* If we could do the conditional move, emit the sequence,
9099 and return. */
9100 if (insn)
9101 {
9102 rtx_insn *seq = get_insns ();
9103 end_sequence ();
9104 emit_insn (seq);
9105 return convert_modes (orig_mode, mode, temp, 0);
9106 }
9107
9108 /* Otherwise discard the sequence and fall back to code with
9109 branches. */
9110 end_sequence ();
9111 return NULL_RTX;
9112 }
9113
9114 /* A helper function for expand_expr_real_2 to be used with a
9115 misaligned mem_ref TEMP. Assume an unsigned type if UNSIGNEDP
9116 is nonzero, with alignment ALIGN in bits.
9117 Store the value at TARGET if possible (if TARGET is nonzero).
9118 Regardless of TARGET, we return the rtx for where the value is placed.
9119 If the result can be stored at TARGET, and ALT_RTL is non-NULL,
9120 then *ALT_RTL is set to TARGET (before legitimziation). */
9121
9122 static rtx
9123 expand_misaligned_mem_ref (rtx temp, machine_mode mode, int unsignedp,
9124 unsigned int align, rtx target, rtx *alt_rtl)
9125 {
9126 enum insn_code icode;
9127
9128 if ((icode = optab_handler (movmisalign_optab, mode))
9129 != CODE_FOR_nothing)
9130 {
9131 class expand_operand ops[2];
9132
9133 /* We've already validated the memory, and we're creating a
9134 new pseudo destination. The predicates really can't fail,
9135 nor can the generator. */
9136 create_output_operand (&ops[0], NULL_RTX, mode);
9137 create_fixed_operand (&ops[1], temp);
9138 expand_insn (icode, 2, ops);
9139 temp = ops[0].value;
9140 }
9141 else if (targetm.slow_unaligned_access (mode, align))
9142 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9143 0, unsignedp, target,
9144 mode, mode, false, alt_rtl);
9145 return temp;
9146 }
9147
9148 /* Helper function of expand_expr_2, expand a division or modulo.
9149 op0 and op1 should be already expanded treeop0 and treeop1, using
9150 expand_operands. */
9151
9152 static rtx
9153 expand_expr_divmod (tree_code code, machine_mode mode, tree treeop0,
9154 tree treeop1, rtx op0, rtx op1, rtx target, int unsignedp)
9155 {
9156 bool mod_p = (code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
9157 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR);
9158 if (SCALAR_INT_MODE_P (mode)
9159 && optimize >= 2
9160 && get_range_pos_neg (treeop0) == 1
9161 && get_range_pos_neg (treeop1) == 1)
9162 {
9163 /* If both arguments are known to be positive when interpreted
9164 as signed, we can expand it as both signed and unsigned
9165 division or modulo. Choose the cheaper sequence in that case. */
9166 bool speed_p = optimize_insn_for_speed_p ();
9167 do_pending_stack_adjust ();
9168 start_sequence ();
9169 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
9170 rtx_insn *uns_insns = get_insns ();
9171 end_sequence ();
9172 start_sequence ();
9173 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
9174 rtx_insn *sgn_insns = get_insns ();
9175 end_sequence ();
9176 unsigned uns_cost = seq_cost (uns_insns, speed_p);
9177 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
9178
9179 /* If costs are the same then use as tie breaker the other other
9180 factor. */
9181 if (uns_cost == sgn_cost)
9182 {
9183 uns_cost = seq_cost (uns_insns, !speed_p);
9184 sgn_cost = seq_cost (sgn_insns, !speed_p);
9185 }
9186
9187 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9188 {
9189 emit_insn (uns_insns);
9190 return uns_ret;
9191 }
9192 emit_insn (sgn_insns);
9193 return sgn_ret;
9194 }
9195 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9196 }
9197
9198 rtx
9199 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
9200 enum expand_modifier modifier)
9201 {
9202 rtx op0, op1, op2, temp;
9203 rtx_code_label *lab;
9204 tree type;
9205 int unsignedp;
9206 machine_mode mode;
9207 scalar_int_mode int_mode;
9208 enum tree_code code = ops->code;
9209 optab this_optab;
9210 rtx subtarget, original_target;
9211 int ignore;
9212 bool reduce_bit_field;
9213 location_t loc = ops->location;
9214 tree treeop0, treeop1, treeop2;
9215 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
9216 ? reduce_to_bit_field_precision ((expr), \
9217 target, \
9218 type) \
9219 : (expr))
9220
9221 type = ops->type;
9222 mode = TYPE_MODE (type);
9223 unsignedp = TYPE_UNSIGNED (type);
9224
9225 treeop0 = ops->op0;
9226 treeop1 = ops->op1;
9227 treeop2 = ops->op2;
9228
9229 /* We should be called only on simple (binary or unary) expressions,
9230 exactly those that are valid in gimple expressions that aren't
9231 GIMPLE_SINGLE_RHS (or invalid). */
9232 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
9233 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
9234 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
9235
9236 ignore = (target == const0_rtx
9237 || ((CONVERT_EXPR_CODE_P (code)
9238 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9239 && TREE_CODE (type) == VOID_TYPE));
9240
9241 /* We should be called only if we need the result. */
9242 gcc_assert (!ignore);
9243
9244 /* An operation in what may be a bit-field type needs the
9245 result to be reduced to the precision of the bit-field type,
9246 which is narrower than that of the type's mode. */
9247 reduce_bit_field = (INTEGRAL_TYPE_P (type)
9248 && !type_has_mode_precision_p (type));
9249
9250 if (reduce_bit_field
9251 && (modifier == EXPAND_STACK_PARM
9252 || (target && GET_MODE (target) != mode)))
9253 target = 0;
9254
9255 /* Use subtarget as the target for operand 0 of a binary operation. */
9256 subtarget = get_subtarget (target);
9257 original_target = target;
9258
9259 switch (code)
9260 {
9261 case NON_LVALUE_EXPR:
9262 case PAREN_EXPR:
9263 CASE_CONVERT:
9264 if (treeop0 == error_mark_node)
9265 return const0_rtx;
9266
9267 if (TREE_CODE (type) == UNION_TYPE)
9268 {
9269 tree valtype = TREE_TYPE (treeop0);
9270
9271 /* If both input and output are BLKmode, this conversion isn't doing
9272 anything except possibly changing memory attribute. */
9273 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
9274 {
9275 rtx result = expand_expr (treeop0, target, tmode,
9276 modifier);
9277
9278 result = copy_rtx (result);
9279 set_mem_attributes (result, type, 0);
9280 return result;
9281 }
9282
9283 if (target == 0)
9284 {
9285 if (TYPE_MODE (type) != BLKmode)
9286 target = gen_reg_rtx (TYPE_MODE (type));
9287 else
9288 target = assign_temp (type, 1, 1);
9289 }
9290
9291 if (MEM_P (target))
9292 /* Store data into beginning of memory target. */
9293 store_expr (treeop0,
9294 adjust_address (target, TYPE_MODE (valtype), 0),
9295 modifier == EXPAND_STACK_PARM,
9296 false, TYPE_REVERSE_STORAGE_ORDER (type));
9297
9298 else
9299 {
9300 gcc_assert (REG_P (target)
9301 && !TYPE_REVERSE_STORAGE_ORDER (type));
9302
9303 /* Store this field into a union of the proper type. */
9304 poly_uint64 op0_size
9305 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
9306 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
9307 store_field (target,
9308 /* The conversion must be constructed so that
9309 we know at compile time how many bits
9310 to preserve. */
9311 ordered_min (op0_size, union_size),
9312 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
9313 false, false);
9314 }
9315
9316 /* Return the entire union. */
9317 return target;
9318 }
9319
9320 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
9321 {
9322 op0 = expand_expr (treeop0, target, VOIDmode,
9323 modifier);
9324
9325 /* If the signedness of the conversion differs and OP0 is
9326 a promoted SUBREG, clear that indication since we now
9327 have to do the proper extension. */
9328 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
9329 && GET_CODE (op0) == SUBREG)
9330 SUBREG_PROMOTED_VAR_P (op0) = 0;
9331
9332 return REDUCE_BIT_FIELD (op0);
9333 }
9334
9335 op0 = expand_expr (treeop0, NULL_RTX, mode,
9336 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
9337 if (GET_MODE (op0) == mode)
9338 ;
9339
9340 /* If OP0 is a constant, just convert it into the proper mode. */
9341 else if (CONSTANT_P (op0))
9342 {
9343 tree inner_type = TREE_TYPE (treeop0);
9344 machine_mode inner_mode = GET_MODE (op0);
9345
9346 if (inner_mode == VOIDmode)
9347 inner_mode = TYPE_MODE (inner_type);
9348
9349 if (modifier == EXPAND_INITIALIZER)
9350 op0 = lowpart_subreg (mode, op0, inner_mode);
9351 else
9352 op0= convert_modes (mode, inner_mode, op0,
9353 TYPE_UNSIGNED (inner_type));
9354 }
9355
9356 else if (modifier == EXPAND_INITIALIZER)
9357 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9358 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
9359
9360 else if (target == 0)
9361 op0 = convert_to_mode (mode, op0,
9362 TYPE_UNSIGNED (TREE_TYPE
9363 (treeop0)));
9364 else
9365 {
9366 convert_move (target, op0,
9367 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9368 op0 = target;
9369 }
9370
9371 return REDUCE_BIT_FIELD (op0);
9372
9373 case ADDR_SPACE_CONVERT_EXPR:
9374 {
9375 tree treeop0_type = TREE_TYPE (treeop0);
9376
9377 gcc_assert (POINTER_TYPE_P (type));
9378 gcc_assert (POINTER_TYPE_P (treeop0_type));
9379
9380 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
9381 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
9382
9383 /* Conversions between pointers to the same address space should
9384 have been implemented via CONVERT_EXPR / NOP_EXPR. */
9385 gcc_assert (as_to != as_from);
9386
9387 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9388
9389 /* Ask target code to handle conversion between pointers
9390 to overlapping address spaces. */
9391 if (targetm.addr_space.subset_p (as_to, as_from)
9392 || targetm.addr_space.subset_p (as_from, as_to))
9393 {
9394 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
9395 }
9396 else
9397 {
9398 /* For disjoint address spaces, converting anything but a null
9399 pointer invokes undefined behavior. We truncate or extend the
9400 value as if we'd converted via integers, which handles 0 as
9401 required, and all others as the programmer likely expects. */
9402 #ifndef POINTERS_EXTEND_UNSIGNED
9403 const int POINTERS_EXTEND_UNSIGNED = 1;
9404 #endif
9405 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
9406 op0, POINTERS_EXTEND_UNSIGNED);
9407 }
9408 gcc_assert (op0);
9409 return op0;
9410 }
9411
9412 case POINTER_PLUS_EXPR:
9413 /* Even though the sizetype mode and the pointer's mode can be different
9414 expand is able to handle this correctly and get the correct result out
9415 of the PLUS_EXPR code. */
9416 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
9417 if sizetype precision is smaller than pointer precision. */
9418 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
9419 treeop1 = fold_convert_loc (loc, type,
9420 fold_convert_loc (loc, ssizetype,
9421 treeop1));
9422 /* If sizetype precision is larger than pointer precision, truncate the
9423 offset to have matching modes. */
9424 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
9425 treeop1 = fold_convert_loc (loc, type, treeop1);
9426 /* FALLTHRU */
9427
9428 case PLUS_EXPR:
9429 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
9430 something else, make sure we add the register to the constant and
9431 then to the other thing. This case can occur during strength
9432 reduction and doing it this way will produce better code if the
9433 frame pointer or argument pointer is eliminated.
9434
9435 fold-const.cc will ensure that the constant is always in the inner
9436 PLUS_EXPR, so the only case we need to do anything about is if
9437 sp, ap, or fp is our second argument, in which case we must swap
9438 the innermost first argument and our second argument. */
9439
9440 if (TREE_CODE (treeop0) == PLUS_EXPR
9441 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
9442 && VAR_P (treeop1)
9443 && (DECL_RTL (treeop1) == frame_pointer_rtx
9444 || DECL_RTL (treeop1) == stack_pointer_rtx
9445 || DECL_RTL (treeop1) == arg_pointer_rtx))
9446 {
9447 gcc_unreachable ();
9448 }
9449
9450 /* If the result is to be ptr_mode and we are adding an integer to
9451 something, we might be forming a constant. So try to use
9452 plus_constant. If it produces a sum and we can't accept it,
9453 use force_operand. This allows P = &ARR[const] to generate
9454 efficient code on machines where a SYMBOL_REF is not a valid
9455 address.
9456
9457 If this is an EXPAND_SUM call, always return the sum. */
9458 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
9459 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
9460 {
9461 if (modifier == EXPAND_STACK_PARM)
9462 target = 0;
9463 if (TREE_CODE (treeop0) == INTEGER_CST
9464 && HWI_COMPUTABLE_MODE_P (mode)
9465 && TREE_CONSTANT (treeop1))
9466 {
9467 rtx constant_part;
9468 HOST_WIDE_INT wc;
9469 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
9470
9471 op1 = expand_expr (treeop1, subtarget, VOIDmode,
9472 EXPAND_SUM);
9473 /* Use wi::shwi to ensure that the constant is
9474 truncated according to the mode of OP1, then sign extended
9475 to a HOST_WIDE_INT. Using the constant directly can result
9476 in non-canonical RTL in a 64x32 cross compile. */
9477 wc = TREE_INT_CST_LOW (treeop0);
9478 constant_part =
9479 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9480 op1 = plus_constant (mode, op1, INTVAL (constant_part));
9481 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9482 op1 = force_operand (op1, target);
9483 return REDUCE_BIT_FIELD (op1);
9484 }
9485
9486 else if (TREE_CODE (treeop1) == INTEGER_CST
9487 && HWI_COMPUTABLE_MODE_P (mode)
9488 && TREE_CONSTANT (treeop0))
9489 {
9490 rtx constant_part;
9491 HOST_WIDE_INT wc;
9492 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
9493
9494 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9495 (modifier == EXPAND_INITIALIZER
9496 ? EXPAND_INITIALIZER : EXPAND_SUM));
9497 if (! CONSTANT_P (op0))
9498 {
9499 op1 = expand_expr (treeop1, NULL_RTX,
9500 VOIDmode, modifier);
9501 /* Return a PLUS if modifier says it's OK. */
9502 if (modifier == EXPAND_SUM
9503 || modifier == EXPAND_INITIALIZER)
9504 return simplify_gen_binary (PLUS, mode, op0, op1);
9505 goto binop2;
9506 }
9507 /* Use wi::shwi to ensure that the constant is
9508 truncated according to the mode of OP1, then sign extended
9509 to a HOST_WIDE_INT. Using the constant directly can result
9510 in non-canonical RTL in a 64x32 cross compile. */
9511 wc = TREE_INT_CST_LOW (treeop1);
9512 constant_part
9513 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
9514 op0 = plus_constant (mode, op0, INTVAL (constant_part));
9515 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
9516 op0 = force_operand (op0, target);
9517 return REDUCE_BIT_FIELD (op0);
9518 }
9519 }
9520
9521 /* Use TER to expand pointer addition of a negated value
9522 as pointer subtraction. */
9523 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
9524 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
9525 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
9526 && TREE_CODE (treeop1) == SSA_NAME
9527 && TYPE_MODE (TREE_TYPE (treeop0))
9528 == TYPE_MODE (TREE_TYPE (treeop1)))
9529 {
9530 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
9531 if (def)
9532 {
9533 treeop1 = gimple_assign_rhs1 (def);
9534 code = MINUS_EXPR;
9535 goto do_minus;
9536 }
9537 }
9538
9539 /* No sense saving up arithmetic to be done
9540 if it's all in the wrong mode to form part of an address.
9541 And force_operand won't know whether to sign-extend or
9542 zero-extend. */
9543 if (modifier != EXPAND_INITIALIZER
9544 && (modifier != EXPAND_SUM || mode != ptr_mode))
9545 {
9546 expand_operands (treeop0, treeop1,
9547 subtarget, &op0, &op1, modifier);
9548 if (op0 == const0_rtx)
9549 return op1;
9550 if (op1 == const0_rtx)
9551 return op0;
9552 goto binop2;
9553 }
9554
9555 expand_operands (treeop0, treeop1,
9556 subtarget, &op0, &op1, modifier);
9557 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9558
9559 case MINUS_EXPR:
9560 case POINTER_DIFF_EXPR:
9561 do_minus:
9562 /* For initializers, we are allowed to return a MINUS of two
9563 symbolic constants. Here we handle all cases when both operands
9564 are constant. */
9565 /* Handle difference of two symbolic constants,
9566 for the sake of an initializer. */
9567 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9568 && really_constant_p (treeop0)
9569 && really_constant_p (treeop1))
9570 {
9571 expand_operands (treeop0, treeop1,
9572 NULL_RTX, &op0, &op1, modifier);
9573 return simplify_gen_binary (MINUS, mode, op0, op1);
9574 }
9575
9576 /* No sense saving up arithmetic to be done
9577 if it's all in the wrong mode to form part of an address.
9578 And force_operand won't know whether to sign-extend or
9579 zero-extend. */
9580 if (modifier != EXPAND_INITIALIZER
9581 && (modifier != EXPAND_SUM || mode != ptr_mode))
9582 goto binop;
9583
9584 expand_operands (treeop0, treeop1,
9585 subtarget, &op0, &op1, modifier);
9586
9587 /* Convert A - const to A + (-const). */
9588 if (CONST_INT_P (op1))
9589 {
9590 op1 = negate_rtx (mode, op1);
9591 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
9592 }
9593
9594 goto binop2;
9595
9596 case WIDEN_MULT_PLUS_EXPR:
9597 case WIDEN_MULT_MINUS_EXPR:
9598 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9599 op2 = expand_normal (treeop2);
9600 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9601 target, unsignedp);
9602 return target;
9603
9604 case WIDEN_PLUS_EXPR:
9605 case WIDEN_MINUS_EXPR:
9606 case WIDEN_MULT_EXPR:
9607 /* If first operand is constant, swap them.
9608 Thus the following special case checks need only
9609 check the second operand. */
9610 if (TREE_CODE (treeop0) == INTEGER_CST)
9611 std::swap (treeop0, treeop1);
9612
9613 /* First, check if we have a multiplication of one signed and one
9614 unsigned operand. */
9615 if (TREE_CODE (treeop1) != INTEGER_CST
9616 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
9617 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
9618 {
9619 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
9620 this_optab = usmul_widen_optab;
9621 if (find_widening_optab_handler (this_optab, mode, innermode)
9622 != CODE_FOR_nothing)
9623 {
9624 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9625 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9626 EXPAND_NORMAL);
9627 else
9628 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
9629 EXPAND_NORMAL);
9630 /* op0 and op1 might still be constant, despite the above
9631 != INTEGER_CST check. Handle it. */
9632 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9633 {
9634 op0 = convert_modes (mode, innermode, op0, true);
9635 op1 = convert_modes (mode, innermode, op1, false);
9636 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9637 target, unsignedp));
9638 }
9639 goto binop3;
9640 }
9641 }
9642 /* Check for a multiplication with matching signedness. */
9643 else if ((TREE_CODE (treeop1) == INTEGER_CST
9644 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
9645 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
9646 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
9647 {
9648 tree op0type = TREE_TYPE (treeop0);
9649 machine_mode innermode = TYPE_MODE (op0type);
9650 bool zextend_p = TYPE_UNSIGNED (op0type);
9651 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
9652 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
9653
9654 if (TREE_CODE (treeop0) != INTEGER_CST)
9655 {
9656 if (find_widening_optab_handler (this_optab, mode, innermode)
9657 != CODE_FOR_nothing)
9658 {
9659 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
9660 EXPAND_NORMAL);
9661 /* op0 and op1 might still be constant, despite the above
9662 != INTEGER_CST check. Handle it. */
9663 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9664 {
9665 widen_mult_const:
9666 op0 = convert_modes (mode, innermode, op0, zextend_p);
9667 op1
9668 = convert_modes (mode, innermode, op1,
9669 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
9670 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
9671 target,
9672 unsignedp));
9673 }
9674 temp = expand_widening_mult (mode, op0, op1, target,
9675 unsignedp, this_optab);
9676 return REDUCE_BIT_FIELD (temp);
9677 }
9678 if (find_widening_optab_handler (other_optab, mode, innermode)
9679 != CODE_FOR_nothing
9680 && innermode == word_mode)
9681 {
9682 rtx htem, hipart;
9683 op0 = expand_normal (treeop0);
9684 op1 = expand_normal (treeop1);
9685 /* op0 and op1 might be constants, despite the above
9686 != INTEGER_CST check. Handle it. */
9687 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
9688 goto widen_mult_const;
9689 temp = expand_binop (mode, other_optab, op0, op1, target,
9690 unsignedp, OPTAB_LIB_WIDEN);
9691 hipart = gen_highpart (word_mode, temp);
9692 htem = expand_mult_highpart_adjust (word_mode, hipart,
9693 op0, op1, hipart,
9694 zextend_p);
9695 if (htem != hipart)
9696 emit_move_insn (hipart, htem);
9697 return REDUCE_BIT_FIELD (temp);
9698 }
9699 }
9700 }
9701 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
9702 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
9703 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9704 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9705
9706 case MULT_EXPR:
9707 /* If this is a fixed-point operation, then we cannot use the code
9708 below because "expand_mult" doesn't support sat/no-sat fixed-point
9709 multiplications. */
9710 if (ALL_FIXED_POINT_MODE_P (mode))
9711 goto binop;
9712
9713 /* If first operand is constant, swap them.
9714 Thus the following special case checks need only
9715 check the second operand. */
9716 if (TREE_CODE (treeop0) == INTEGER_CST)
9717 std::swap (treeop0, treeop1);
9718
9719 /* Attempt to return something suitable for generating an
9720 indexed address, for machines that support that. */
9721
9722 if (modifier == EXPAND_SUM && mode == ptr_mode
9723 && tree_fits_shwi_p (treeop1))
9724 {
9725 tree exp1 = treeop1;
9726
9727 op0 = expand_expr (treeop0, subtarget, VOIDmode,
9728 EXPAND_SUM);
9729
9730 if (!REG_P (op0))
9731 op0 = force_operand (op0, NULL_RTX);
9732 if (!REG_P (op0))
9733 op0 = copy_to_mode_reg (mode, op0);
9734
9735 op1 = gen_int_mode (tree_to_shwi (exp1),
9736 TYPE_MODE (TREE_TYPE (exp1)));
9737 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0, op1));
9738 }
9739
9740 if (modifier == EXPAND_STACK_PARM)
9741 target = 0;
9742
9743 if (SCALAR_INT_MODE_P (mode) && optimize >= 2)
9744 {
9745 gimple *def_stmt0 = get_def_for_expr (treeop0, TRUNC_DIV_EXPR);
9746 gimple *def_stmt1 = get_def_for_expr (treeop1, TRUNC_DIV_EXPR);
9747 if (def_stmt0
9748 && !operand_equal_p (treeop1, gimple_assign_rhs2 (def_stmt0), 0))
9749 def_stmt0 = NULL;
9750 if (def_stmt1
9751 && !operand_equal_p (treeop0, gimple_assign_rhs2 (def_stmt1), 0))
9752 def_stmt1 = NULL;
9753
9754 if (def_stmt0 || def_stmt1)
9755 {
9756 /* X / Y * Y can be expanded as X - X % Y too.
9757 Choose the cheaper sequence of those two. */
9758 if (def_stmt0)
9759 treeop0 = gimple_assign_rhs1 (def_stmt0);
9760 else
9761 {
9762 treeop1 = treeop0;
9763 treeop0 = gimple_assign_rhs1 (def_stmt1);
9764 }
9765 expand_operands (treeop0, treeop1, subtarget, &op0, &op1,
9766 EXPAND_NORMAL);
9767 bool speed_p = optimize_insn_for_speed_p ();
9768 do_pending_stack_adjust ();
9769 start_sequence ();
9770 rtx divmul_ret
9771 = expand_expr_divmod (TRUNC_DIV_EXPR, mode, treeop0, treeop1,
9772 op0, op1, NULL_RTX, unsignedp);
9773 divmul_ret = expand_mult (mode, divmul_ret, op1, target,
9774 unsignedp);
9775 rtx_insn *divmul_insns = get_insns ();
9776 end_sequence ();
9777 start_sequence ();
9778 rtx modsub_ret
9779 = expand_expr_divmod (TRUNC_MOD_EXPR, mode, treeop0, treeop1,
9780 op0, op1, NULL_RTX, unsignedp);
9781 this_optab = optab_for_tree_code (MINUS_EXPR, type,
9782 optab_default);
9783 modsub_ret = expand_binop (mode, this_optab, op0, modsub_ret,
9784 target, unsignedp, OPTAB_LIB_WIDEN);
9785 rtx_insn *modsub_insns = get_insns ();
9786 end_sequence ();
9787 unsigned divmul_cost = seq_cost (divmul_insns, speed_p);
9788 unsigned modsub_cost = seq_cost (modsub_insns, speed_p);
9789 /* If costs are the same then use as tie breaker the other other
9790 factor. */
9791 if (divmul_cost == modsub_cost)
9792 {
9793 divmul_cost = seq_cost (divmul_insns, !speed_p);
9794 modsub_cost = seq_cost (modsub_insns, !speed_p);
9795 }
9796
9797 if (divmul_cost <= modsub_cost)
9798 {
9799 emit_insn (divmul_insns);
9800 return REDUCE_BIT_FIELD (divmul_ret);
9801 }
9802 emit_insn (modsub_insns);
9803 return REDUCE_BIT_FIELD (modsub_ret);
9804 }
9805 }
9806
9807 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9808
9809 /* Expand X*Y as X&-Y when Y must be zero or one. */
9810 if (SCALAR_INT_MODE_P (mode))
9811 {
9812 bool bit0_p = tree_nonzero_bits (treeop0) == 1;
9813 bool bit1_p = tree_nonzero_bits (treeop1) == 1;
9814
9815 /* Expand X*Y as X&Y when both X and Y must be zero or one. */
9816 if (bit0_p && bit1_p)
9817 return REDUCE_BIT_FIELD (expand_and (mode, op0, op1, target));
9818
9819 if (bit0_p || bit1_p)
9820 {
9821 bool speed = optimize_insn_for_speed_p ();
9822 int cost = add_cost (speed, mode) + neg_cost (speed, mode);
9823 struct algorithm algorithm;
9824 enum mult_variant variant;
9825 if (CONST_INT_P (op1)
9826 ? !choose_mult_variant (mode, INTVAL (op1),
9827 &algorithm, &variant, cost)
9828 : cost < mul_cost (speed, mode))
9829 {
9830 target = bit0_p ? expand_and (mode, negate_rtx (mode, op0),
9831 op1, target)
9832 : expand_and (mode, op0,
9833 negate_rtx (mode, op1),
9834 target);
9835 return REDUCE_BIT_FIELD (target);
9836 }
9837 }
9838 }
9839
9840 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
9841
9842 case TRUNC_MOD_EXPR:
9843 case FLOOR_MOD_EXPR:
9844 case CEIL_MOD_EXPR:
9845 case ROUND_MOD_EXPR:
9846
9847 case TRUNC_DIV_EXPR:
9848 case FLOOR_DIV_EXPR:
9849 case CEIL_DIV_EXPR:
9850 case ROUND_DIV_EXPR:
9851 case EXACT_DIV_EXPR:
9852 /* If this is a fixed-point operation, then we cannot use the code
9853 below because "expand_divmod" doesn't support sat/no-sat fixed-point
9854 divisions. */
9855 if (ALL_FIXED_POINT_MODE_P (mode))
9856 goto binop;
9857
9858 if (modifier == EXPAND_STACK_PARM)
9859 target = 0;
9860 /* Possible optimization: compute the dividend with EXPAND_SUM
9861 then if the divisor is constant can optimize the case
9862 where some terms of the dividend have coeffs divisible by it. */
9863 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9864 return expand_expr_divmod (code, mode, treeop0, treeop1, op0, op1,
9865 target, unsignedp);
9866
9867 case RDIV_EXPR:
9868 goto binop;
9869
9870 case MULT_HIGHPART_EXPR:
9871 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9872 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9873 gcc_assert (temp);
9874 return temp;
9875
9876 case FIXED_CONVERT_EXPR:
9877 op0 = expand_normal (treeop0);
9878 if (target == 0 || modifier == EXPAND_STACK_PARM)
9879 target = gen_reg_rtx (mode);
9880
9881 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9882 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9883 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9884 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9885 else
9886 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9887 return target;
9888
9889 case FIX_TRUNC_EXPR:
9890 op0 = expand_normal (treeop0);
9891 if (target == 0 || modifier == EXPAND_STACK_PARM)
9892 target = gen_reg_rtx (mode);
9893 expand_fix (target, op0, unsignedp);
9894 return target;
9895
9896 case FLOAT_EXPR:
9897 op0 = expand_normal (treeop0);
9898 if (target == 0 || modifier == EXPAND_STACK_PARM)
9899 target = gen_reg_rtx (mode);
9900 /* expand_float can't figure out what to do if FROM has VOIDmode.
9901 So give it the correct mode. With -O, cse will optimize this. */
9902 if (GET_MODE (op0) == VOIDmode)
9903 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9904 op0);
9905 expand_float (target, op0,
9906 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9907 return target;
9908
9909 case NEGATE_EXPR:
9910 op0 = expand_expr (treeop0, subtarget,
9911 VOIDmode, EXPAND_NORMAL);
9912 if (modifier == EXPAND_STACK_PARM)
9913 target = 0;
9914 temp = expand_unop (mode,
9915 optab_for_tree_code (NEGATE_EXPR, type,
9916 optab_default),
9917 op0, target, 0);
9918 gcc_assert (temp);
9919 return REDUCE_BIT_FIELD (temp);
9920
9921 case ABS_EXPR:
9922 case ABSU_EXPR:
9923 op0 = expand_expr (treeop0, subtarget,
9924 VOIDmode, EXPAND_NORMAL);
9925 if (modifier == EXPAND_STACK_PARM)
9926 target = 0;
9927
9928 /* ABS_EXPR is not valid for complex arguments. */
9929 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9930 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9931
9932 /* Unsigned abs is simply the operand. Testing here means we don't
9933 risk generating incorrect code below. */
9934 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9935 return op0;
9936
9937 return expand_abs (mode, op0, target, unsignedp,
9938 safe_from_p (target, treeop0, 1));
9939
9940 case MAX_EXPR:
9941 case MIN_EXPR:
9942 target = original_target;
9943 if (target == 0
9944 || modifier == EXPAND_STACK_PARM
9945 || (MEM_P (target) && MEM_VOLATILE_P (target))
9946 || GET_MODE (target) != mode
9947 || (REG_P (target)
9948 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9949 target = gen_reg_rtx (mode);
9950 expand_operands (treeop0, treeop1,
9951 target, &op0, &op1, EXPAND_NORMAL);
9952
9953 /* First try to do it with a special MIN or MAX instruction.
9954 If that does not win, use a conditional jump to select the proper
9955 value. */
9956 this_optab = optab_for_tree_code (code, type, optab_default);
9957 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9958 OPTAB_WIDEN);
9959 if (temp != 0)
9960 return temp;
9961
9962 if (VECTOR_TYPE_P (type))
9963 gcc_unreachable ();
9964
9965 /* At this point, a MEM target is no longer useful; we will get better
9966 code without it. */
9967
9968 if (! REG_P (target))
9969 target = gen_reg_rtx (mode);
9970
9971 /* If op1 was placed in target, swap op0 and op1. */
9972 if (target != op0 && target == op1)
9973 std::swap (op0, op1);
9974
9975 /* We generate better code and avoid problems with op1 mentioning
9976 target by forcing op1 into a pseudo if it isn't a constant. */
9977 if (! CONSTANT_P (op1))
9978 op1 = force_reg (mode, op1);
9979
9980 {
9981 enum rtx_code comparison_code;
9982 rtx cmpop1 = op1;
9983
9984 if (code == MAX_EXPR)
9985 comparison_code = unsignedp ? GEU : GE;
9986 else
9987 comparison_code = unsignedp ? LEU : LE;
9988
9989 /* Canonicalize to comparisons against 0. */
9990 if (op1 == const1_rtx)
9991 {
9992 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9993 or (a != 0 ? a : 1) for unsigned.
9994 For MIN we are safe converting (a <= 1 ? a : 1)
9995 into (a <= 0 ? a : 1) */
9996 cmpop1 = const0_rtx;
9997 if (code == MAX_EXPR)
9998 comparison_code = unsignedp ? NE : GT;
9999 }
10000 if (op1 == constm1_rtx && !unsignedp)
10001 {
10002 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
10003 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
10004 cmpop1 = const0_rtx;
10005 if (code == MIN_EXPR)
10006 comparison_code = LT;
10007 }
10008
10009 /* Use a conditional move if possible. */
10010 if (can_conditionally_move_p (mode))
10011 {
10012 rtx insn;
10013
10014 start_sequence ();
10015
10016 /* Try to emit the conditional move. */
10017 insn = emit_conditional_move (target,
10018 { comparison_code,
10019 op0, cmpop1, mode },
10020 op0, op1, mode,
10021 unsignedp);
10022
10023 /* If we could do the conditional move, emit the sequence,
10024 and return. */
10025 if (insn)
10026 {
10027 rtx_insn *seq = get_insns ();
10028 end_sequence ();
10029 emit_insn (seq);
10030 return target;
10031 }
10032
10033 /* Otherwise discard the sequence and fall back to code with
10034 branches. */
10035 end_sequence ();
10036 }
10037
10038 if (target != op0)
10039 emit_move_insn (target, op0);
10040
10041 lab = gen_label_rtx ();
10042 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
10043 unsignedp, mode, NULL_RTX, NULL, lab,
10044 profile_probability::uninitialized ());
10045 }
10046 emit_move_insn (target, op1);
10047 emit_label (lab);
10048 return target;
10049
10050 case BIT_NOT_EXPR:
10051 op0 = expand_expr (treeop0, subtarget,
10052 VOIDmode, EXPAND_NORMAL);
10053 if (modifier == EXPAND_STACK_PARM)
10054 target = 0;
10055 /* In case we have to reduce the result to bitfield precision
10056 for unsigned bitfield expand this as XOR with a proper constant
10057 instead. */
10058 if (reduce_bit_field && TYPE_UNSIGNED (type))
10059 {
10060 int_mode = SCALAR_INT_TYPE_MODE (type);
10061 wide_int mask = wi::mask (TYPE_PRECISION (type),
10062 false, GET_MODE_PRECISION (int_mode));
10063
10064 temp = expand_binop (int_mode, xor_optab, op0,
10065 immed_wide_int_const (mask, int_mode),
10066 target, 1, OPTAB_LIB_WIDEN);
10067 }
10068 else
10069 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
10070 gcc_assert (temp);
10071 return temp;
10072
10073 /* ??? Can optimize bitwise operations with one arg constant.
10074 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
10075 and (a bitwise1 b) bitwise2 b (etc)
10076 but that is probably not worth while. */
10077
10078 case BIT_AND_EXPR:
10079 case BIT_IOR_EXPR:
10080 case BIT_XOR_EXPR:
10081 goto binop;
10082
10083 case LROTATE_EXPR:
10084 case RROTATE_EXPR:
10085 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
10086 || type_has_mode_precision_p (type));
10087 /* fall through */
10088
10089 case LSHIFT_EXPR:
10090 case RSHIFT_EXPR:
10091 {
10092 /* If this is a fixed-point operation, then we cannot use the code
10093 below because "expand_shift" doesn't support sat/no-sat fixed-point
10094 shifts. */
10095 if (ALL_FIXED_POINT_MODE_P (mode))
10096 goto binop;
10097
10098 if (! safe_from_p (subtarget, treeop1, 1))
10099 subtarget = 0;
10100 if (modifier == EXPAND_STACK_PARM)
10101 target = 0;
10102 op0 = expand_expr (treeop0, subtarget,
10103 VOIDmode, EXPAND_NORMAL);
10104
10105 /* Left shift optimization when shifting across word_size boundary.
10106
10107 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
10108 there isn't native instruction to support this wide mode
10109 left shift. Given below scenario:
10110
10111 Type A = (Type) B << C
10112
10113 |< T >|
10114 | dest_high | dest_low |
10115
10116 | word_size |
10117
10118 If the shift amount C caused we shift B to across the word
10119 size boundary, i.e part of B shifted into high half of
10120 destination register, and part of B remains in the low
10121 half, then GCC will use the following left shift expand
10122 logic:
10123
10124 1. Initialize dest_low to B.
10125 2. Initialize every bit of dest_high to the sign bit of B.
10126 3. Logic left shift dest_low by C bit to finalize dest_low.
10127 The value of dest_low before this shift is kept in a temp D.
10128 4. Logic left shift dest_high by C.
10129 5. Logic right shift D by (word_size - C).
10130 6. Or the result of 4 and 5 to finalize dest_high.
10131
10132 While, by checking gimple statements, if operand B is
10133 coming from signed extension, then we can simplify above
10134 expand logic into:
10135
10136 1. dest_high = src_low >> (word_size - C).
10137 2. dest_low = src_low << C.
10138
10139 We can use one arithmetic right shift to finish all the
10140 purpose of steps 2, 4, 5, 6, thus we reduce the steps
10141 needed from 6 into 2.
10142
10143 The case is similar for zero extension, except that we
10144 initialize dest_high to zero rather than copies of the sign
10145 bit from B. Furthermore, we need to use a logical right shift
10146 in this case.
10147
10148 The choice of sign-extension versus zero-extension is
10149 determined entirely by whether or not B is signed and is
10150 independent of the current setting of unsignedp. */
10151
10152 temp = NULL_RTX;
10153 if (code == LSHIFT_EXPR
10154 && target
10155 && REG_P (target)
10156 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
10157 && mode == int_mode
10158 && TREE_CONSTANT (treeop1)
10159 && TREE_CODE (treeop0) == SSA_NAME)
10160 {
10161 gimple *def = SSA_NAME_DEF_STMT (treeop0);
10162 if (is_gimple_assign (def)
10163 && gimple_assign_rhs_code (def) == NOP_EXPR)
10164 {
10165 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
10166 (TREE_TYPE (gimple_assign_rhs1 (def)));
10167
10168 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
10169 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
10170 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
10171 >= GET_MODE_BITSIZE (word_mode)))
10172 {
10173 rtx_insn *seq, *seq_old;
10174 poly_uint64 high_off = subreg_highpart_offset (word_mode,
10175 int_mode);
10176 bool extend_unsigned
10177 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
10178 rtx low = lowpart_subreg (word_mode, op0, int_mode);
10179 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
10180 rtx dest_high = simplify_gen_subreg (word_mode, target,
10181 int_mode, high_off);
10182 HOST_WIDE_INT ramount = (BITS_PER_WORD
10183 - TREE_INT_CST_LOW (treeop1));
10184 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
10185
10186 start_sequence ();
10187 /* dest_high = src_low >> (word_size - C). */
10188 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
10189 rshift, dest_high,
10190 extend_unsigned);
10191 if (temp != dest_high)
10192 emit_move_insn (dest_high, temp);
10193
10194 /* dest_low = src_low << C. */
10195 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
10196 treeop1, dest_low, unsignedp);
10197 if (temp != dest_low)
10198 emit_move_insn (dest_low, temp);
10199
10200 seq = get_insns ();
10201 end_sequence ();
10202 temp = target ;
10203
10204 if (have_insn_for (ASHIFT, int_mode))
10205 {
10206 bool speed_p = optimize_insn_for_speed_p ();
10207 start_sequence ();
10208 rtx ret_old = expand_variable_shift (code, int_mode,
10209 op0, treeop1,
10210 target,
10211 unsignedp);
10212
10213 seq_old = get_insns ();
10214 end_sequence ();
10215 if (seq_cost (seq, speed_p)
10216 >= seq_cost (seq_old, speed_p))
10217 {
10218 seq = seq_old;
10219 temp = ret_old;
10220 }
10221 }
10222 emit_insn (seq);
10223 }
10224 }
10225 }
10226
10227 if (temp == NULL_RTX)
10228 temp = expand_variable_shift (code, mode, op0, treeop1, target,
10229 unsignedp);
10230 if (code == LSHIFT_EXPR)
10231 temp = REDUCE_BIT_FIELD (temp);
10232 return temp;
10233 }
10234
10235 /* Could determine the answer when only additive constants differ. Also,
10236 the addition of one can be handled by changing the condition. */
10237 case LT_EXPR:
10238 case LE_EXPR:
10239 case GT_EXPR:
10240 case GE_EXPR:
10241 case EQ_EXPR:
10242 case NE_EXPR:
10243 case UNORDERED_EXPR:
10244 case ORDERED_EXPR:
10245 case UNLT_EXPR:
10246 case UNLE_EXPR:
10247 case UNGT_EXPR:
10248 case UNGE_EXPR:
10249 case UNEQ_EXPR:
10250 case LTGT_EXPR:
10251 {
10252 temp = do_store_flag (ops,
10253 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
10254 tmode != VOIDmode ? tmode : mode);
10255 if (temp)
10256 return temp;
10257
10258 /* Use a compare and a jump for BLKmode comparisons, or for function
10259 type comparisons is have_canonicalize_funcptr_for_compare. */
10260
10261 if ((target == 0
10262 || modifier == EXPAND_STACK_PARM
10263 || ! safe_from_p (target, treeop0, 1)
10264 || ! safe_from_p (target, treeop1, 1)
10265 /* Make sure we don't have a hard reg (such as function's return
10266 value) live across basic blocks, if not optimizing. */
10267 || (!optimize && REG_P (target)
10268 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
10269 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10270
10271 emit_move_insn (target, const0_rtx);
10272
10273 rtx_code_label *lab1 = gen_label_rtx ();
10274 jumpifnot_1 (code, treeop0, treeop1, lab1,
10275 profile_probability::uninitialized ());
10276
10277 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
10278 emit_move_insn (target, constm1_rtx);
10279 else
10280 emit_move_insn (target, const1_rtx);
10281
10282 emit_label (lab1);
10283 return target;
10284 }
10285 case COMPLEX_EXPR:
10286 /* Get the rtx code of the operands. */
10287 op0 = expand_normal (treeop0);
10288 op1 = expand_normal (treeop1);
10289
10290 if (!target)
10291 target = gen_reg_rtx (TYPE_MODE (type));
10292 else
10293 /* If target overlaps with op1, then either we need to force
10294 op1 into a pseudo (if target also overlaps with op0),
10295 or write the complex parts in reverse order. */
10296 switch (GET_CODE (target))
10297 {
10298 case CONCAT:
10299 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
10300 {
10301 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
10302 {
10303 complex_expr_force_op1:
10304 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
10305 emit_move_insn (temp, op1);
10306 op1 = temp;
10307 break;
10308 }
10309 complex_expr_swap_order:
10310 /* Move the imaginary (op1) and real (op0) parts to their
10311 location. */
10312 write_complex_part (target, op1, true, true);
10313 write_complex_part (target, op0, false, false);
10314
10315 return target;
10316 }
10317 break;
10318 case MEM:
10319 temp = adjust_address_nv (target,
10320 GET_MODE_INNER (GET_MODE (target)), 0);
10321 if (reg_overlap_mentioned_p (temp, op1))
10322 {
10323 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
10324 temp = adjust_address_nv (target, imode,
10325 GET_MODE_SIZE (imode));
10326 if (reg_overlap_mentioned_p (temp, op0))
10327 goto complex_expr_force_op1;
10328 goto complex_expr_swap_order;
10329 }
10330 break;
10331 default:
10332 if (reg_overlap_mentioned_p (target, op1))
10333 {
10334 if (reg_overlap_mentioned_p (target, op0))
10335 goto complex_expr_force_op1;
10336 goto complex_expr_swap_order;
10337 }
10338 break;
10339 }
10340
10341 /* Move the real (op0) and imaginary (op1) parts to their location. */
10342 write_complex_part (target, op0, false, true);
10343 write_complex_part (target, op1, true, false);
10344
10345 return target;
10346
10347 case WIDEN_SUM_EXPR:
10348 {
10349 tree oprnd0 = treeop0;
10350 tree oprnd1 = treeop1;
10351
10352 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10353 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
10354 target, unsignedp);
10355 return target;
10356 }
10357
10358 case VEC_UNPACK_HI_EXPR:
10359 case VEC_UNPACK_LO_EXPR:
10360 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
10361 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
10362 {
10363 op0 = expand_normal (treeop0);
10364 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
10365 target, unsignedp);
10366 gcc_assert (temp);
10367 return temp;
10368 }
10369
10370 case VEC_UNPACK_FLOAT_HI_EXPR:
10371 case VEC_UNPACK_FLOAT_LO_EXPR:
10372 {
10373 op0 = expand_normal (treeop0);
10374 /* The signedness is determined from input operand. */
10375 temp = expand_widen_pattern_expr
10376 (ops, op0, NULL_RTX, NULL_RTX,
10377 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10378
10379 gcc_assert (temp);
10380 return temp;
10381 }
10382
10383 case VEC_WIDEN_PLUS_HI_EXPR:
10384 case VEC_WIDEN_PLUS_LO_EXPR:
10385 case VEC_WIDEN_MINUS_HI_EXPR:
10386 case VEC_WIDEN_MINUS_LO_EXPR:
10387 case VEC_WIDEN_MULT_HI_EXPR:
10388 case VEC_WIDEN_MULT_LO_EXPR:
10389 case VEC_WIDEN_MULT_EVEN_EXPR:
10390 case VEC_WIDEN_MULT_ODD_EXPR:
10391 case VEC_WIDEN_LSHIFT_HI_EXPR:
10392 case VEC_WIDEN_LSHIFT_LO_EXPR:
10393 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10394 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
10395 target, unsignedp);
10396 gcc_assert (target);
10397 return target;
10398
10399 case VEC_PACK_SAT_EXPR:
10400 case VEC_PACK_FIX_TRUNC_EXPR:
10401 mode = TYPE_MODE (TREE_TYPE (treeop0));
10402 subtarget = NULL_RTX;
10403 goto binop;
10404
10405 case VEC_PACK_TRUNC_EXPR:
10406 if (VECTOR_BOOLEAN_TYPE_P (type)
10407 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (treeop0))
10408 && mode == TYPE_MODE (TREE_TYPE (treeop0))
10409 && SCALAR_INT_MODE_P (mode))
10410 {
10411 class expand_operand eops[4];
10412 machine_mode imode = TYPE_MODE (TREE_TYPE (treeop0));
10413 expand_operands (treeop0, treeop1,
10414 subtarget, &op0, &op1, EXPAND_NORMAL);
10415 this_optab = vec_pack_sbool_trunc_optab;
10416 enum insn_code icode = optab_handler (this_optab, imode);
10417 create_output_operand (&eops[0], target, mode);
10418 create_convert_operand_from (&eops[1], op0, imode, false);
10419 create_convert_operand_from (&eops[2], op1, imode, false);
10420 temp = GEN_INT (TYPE_VECTOR_SUBPARTS (type).to_constant ());
10421 create_input_operand (&eops[3], temp, imode);
10422 expand_insn (icode, 4, eops);
10423 return eops[0].value;
10424 }
10425 mode = TYPE_MODE (TREE_TYPE (treeop0));
10426 subtarget = NULL_RTX;
10427 goto binop;
10428
10429 case VEC_PACK_FLOAT_EXPR:
10430 mode = TYPE_MODE (TREE_TYPE (treeop0));
10431 expand_operands (treeop0, treeop1,
10432 subtarget, &op0, &op1, EXPAND_NORMAL);
10433 this_optab = optab_for_tree_code (code, TREE_TYPE (treeop0),
10434 optab_default);
10435 target = expand_binop (mode, this_optab, op0, op1, target,
10436 TYPE_UNSIGNED (TREE_TYPE (treeop0)),
10437 OPTAB_LIB_WIDEN);
10438 gcc_assert (target);
10439 return target;
10440
10441 case VEC_PERM_EXPR:
10442 {
10443 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
10444 vec_perm_builder sel;
10445 if (TREE_CODE (treeop2) == VECTOR_CST
10446 && tree_to_vec_perm_builder (&sel, treeop2))
10447 {
10448 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
10449 temp = expand_vec_perm_const (mode, op0, op1, sel,
10450 sel_mode, target);
10451 }
10452 else
10453 {
10454 op2 = expand_normal (treeop2);
10455 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
10456 }
10457 gcc_assert (temp);
10458 return temp;
10459 }
10460
10461 case DOT_PROD_EXPR:
10462 {
10463 tree oprnd0 = treeop0;
10464 tree oprnd1 = treeop1;
10465 tree oprnd2 = treeop2;
10466
10467 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10468 op2 = expand_normal (oprnd2);
10469 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10470 target, unsignedp);
10471 return target;
10472 }
10473
10474 case SAD_EXPR:
10475 {
10476 tree oprnd0 = treeop0;
10477 tree oprnd1 = treeop1;
10478 tree oprnd2 = treeop2;
10479
10480 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10481 op2 = expand_normal (oprnd2);
10482 target = expand_widen_pattern_expr (ops, op0, op1, op2,
10483 target, unsignedp);
10484 return target;
10485 }
10486
10487 case REALIGN_LOAD_EXPR:
10488 {
10489 tree oprnd0 = treeop0;
10490 tree oprnd1 = treeop1;
10491 tree oprnd2 = treeop2;
10492
10493 this_optab = optab_for_tree_code (code, type, optab_default);
10494 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
10495 op2 = expand_normal (oprnd2);
10496 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
10497 target, unsignedp);
10498 gcc_assert (temp);
10499 return temp;
10500 }
10501
10502 case COND_EXPR:
10503 {
10504 /* A COND_EXPR with its type being VOID_TYPE represents a
10505 conditional jump and is handled in
10506 expand_gimple_cond_expr. */
10507 gcc_assert (!VOID_TYPE_P (type));
10508
10509 /* Note that COND_EXPRs whose type is a structure or union
10510 are required to be constructed to contain assignments of
10511 a temporary variable, so that we can evaluate them here
10512 for side effect only. If type is void, we must do likewise. */
10513
10514 gcc_assert (!TREE_ADDRESSABLE (type)
10515 && !ignore
10516 && TREE_TYPE (treeop1) != void_type_node
10517 && TREE_TYPE (treeop2) != void_type_node);
10518
10519 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
10520 if (temp)
10521 return temp;
10522
10523 /* If we are not to produce a result, we have no target. Otherwise,
10524 if a target was specified use it; it will not be used as an
10525 intermediate target unless it is safe. If no target, use a
10526 temporary. */
10527
10528 if (modifier != EXPAND_STACK_PARM
10529 && original_target
10530 && safe_from_p (original_target, treeop0, 1)
10531 && GET_MODE (original_target) == mode
10532 && !MEM_P (original_target))
10533 temp = original_target;
10534 else
10535 temp = assign_temp (type, 0, 1);
10536
10537 do_pending_stack_adjust ();
10538 NO_DEFER_POP;
10539 rtx_code_label *lab0 = gen_label_rtx ();
10540 rtx_code_label *lab1 = gen_label_rtx ();
10541 jumpifnot (treeop0, lab0,
10542 profile_probability::uninitialized ());
10543 store_expr (treeop1, temp,
10544 modifier == EXPAND_STACK_PARM,
10545 false, false);
10546
10547 emit_jump_insn (targetm.gen_jump (lab1));
10548 emit_barrier ();
10549 emit_label (lab0);
10550 store_expr (treeop2, temp,
10551 modifier == EXPAND_STACK_PARM,
10552 false, false);
10553
10554 emit_label (lab1);
10555 OK_DEFER_POP;
10556 return temp;
10557 }
10558
10559 case VEC_DUPLICATE_EXPR:
10560 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
10561 target = expand_vector_broadcast (mode, op0);
10562 gcc_assert (target);
10563 return target;
10564
10565 case VEC_SERIES_EXPR:
10566 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
10567 return expand_vec_series_expr (mode, op0, op1, target);
10568
10569 case BIT_INSERT_EXPR:
10570 {
10571 unsigned bitpos = tree_to_uhwi (treeop2);
10572 unsigned bitsize;
10573 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
10574 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
10575 else
10576 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
10577 op0 = expand_normal (treeop0);
10578 op1 = expand_normal (treeop1);
10579 rtx dst = gen_reg_rtx (mode);
10580 emit_move_insn (dst, op0);
10581 store_bit_field (dst, bitsize, bitpos, 0, 0,
10582 TYPE_MODE (TREE_TYPE (treeop1)), op1, false, false);
10583 return dst;
10584 }
10585
10586 default:
10587 gcc_unreachable ();
10588 }
10589
10590 /* Here to do an ordinary binary operator. */
10591 binop:
10592 expand_operands (treeop0, treeop1,
10593 subtarget, &op0, &op1, EXPAND_NORMAL);
10594 binop2:
10595 this_optab = optab_for_tree_code (code, type, optab_default);
10596 binop3:
10597 if (modifier == EXPAND_STACK_PARM)
10598 target = 0;
10599 temp = expand_binop (mode, this_optab, op0, op1, target,
10600 unsignedp, OPTAB_LIB_WIDEN);
10601 gcc_assert (temp);
10602 /* Bitwise operations do not need bitfield reduction as we expect their
10603 operands being properly truncated. */
10604 if (code == BIT_XOR_EXPR
10605 || code == BIT_AND_EXPR
10606 || code == BIT_IOR_EXPR)
10607 return temp;
10608 return REDUCE_BIT_FIELD (temp);
10609 }
10610 #undef REDUCE_BIT_FIELD
10611
10612
10613 /* Return TRUE if expression STMT is suitable for replacement.
10614 Never consider memory loads as replaceable, because those don't ever lead
10615 into constant expressions. */
10616
10617 static bool
10618 stmt_is_replaceable_p (gimple *stmt)
10619 {
10620 if (ssa_is_replaceable_p (stmt))
10621 {
10622 /* Don't move around loads. */
10623 if (!gimple_assign_single_p (stmt)
10624 || is_gimple_val (gimple_assign_rhs1 (stmt)))
10625 return true;
10626 }
10627 return false;
10628 }
10629
10630 rtx
10631 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
10632 enum expand_modifier modifier, rtx *alt_rtl,
10633 bool inner_reference_p)
10634 {
10635 rtx op0, op1, temp, decl_rtl;
10636 tree type;
10637 int unsignedp;
10638 machine_mode mode, dmode;
10639 enum tree_code code = TREE_CODE (exp);
10640 rtx subtarget, original_target;
10641 int ignore;
10642 bool reduce_bit_field;
10643 location_t loc = EXPR_LOCATION (exp);
10644 struct separate_ops ops;
10645 tree treeop0, treeop1, treeop2;
10646 tree ssa_name = NULL_TREE;
10647 gimple *g;
10648
10649 type = TREE_TYPE (exp);
10650 mode = TYPE_MODE (type);
10651 unsignedp = TYPE_UNSIGNED (type);
10652
10653 treeop0 = treeop1 = treeop2 = NULL_TREE;
10654 if (!VL_EXP_CLASS_P (exp))
10655 switch (TREE_CODE_LENGTH (code))
10656 {
10657 default:
10658 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
10659 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
10660 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
10661 case 0: break;
10662 }
10663 ops.code = code;
10664 ops.type = type;
10665 ops.op0 = treeop0;
10666 ops.op1 = treeop1;
10667 ops.op2 = treeop2;
10668 ops.location = loc;
10669
10670 ignore = (target == const0_rtx
10671 || ((CONVERT_EXPR_CODE_P (code)
10672 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
10673 && TREE_CODE (type) == VOID_TYPE));
10674
10675 /* An operation in what may be a bit-field type needs the
10676 result to be reduced to the precision of the bit-field type,
10677 which is narrower than that of the type's mode. */
10678 reduce_bit_field = (!ignore
10679 && INTEGRAL_TYPE_P (type)
10680 && !type_has_mode_precision_p (type));
10681
10682 /* If we are going to ignore this result, we need only do something
10683 if there is a side-effect somewhere in the expression. If there
10684 is, short-circuit the most common cases here. Note that we must
10685 not call expand_expr with anything but const0_rtx in case this
10686 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
10687
10688 if (ignore)
10689 {
10690 if (! TREE_SIDE_EFFECTS (exp))
10691 return const0_rtx;
10692
10693 /* Ensure we reference a volatile object even if value is ignored, but
10694 don't do this if all we are doing is taking its address. */
10695 if (TREE_THIS_VOLATILE (exp)
10696 && TREE_CODE (exp) != FUNCTION_DECL
10697 && mode != VOIDmode && mode != BLKmode
10698 && modifier != EXPAND_CONST_ADDRESS)
10699 {
10700 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
10701 if (MEM_P (temp))
10702 copy_to_reg (temp);
10703 return const0_rtx;
10704 }
10705
10706 if (TREE_CODE_CLASS (code) == tcc_unary
10707 || code == BIT_FIELD_REF
10708 || code == COMPONENT_REF
10709 || code == INDIRECT_REF)
10710 return expand_expr (treeop0, const0_rtx, VOIDmode,
10711 modifier);
10712
10713 else if (TREE_CODE_CLASS (code) == tcc_binary
10714 || TREE_CODE_CLASS (code) == tcc_comparison
10715 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
10716 {
10717 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
10718 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
10719 return const0_rtx;
10720 }
10721
10722 target = 0;
10723 }
10724
10725 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
10726 target = 0;
10727
10728 /* Use subtarget as the target for operand 0 of a binary operation. */
10729 subtarget = get_subtarget (target);
10730 original_target = target;
10731
10732 switch (code)
10733 {
10734 case LABEL_DECL:
10735 {
10736 tree function = decl_function_context (exp);
10737
10738 temp = label_rtx (exp);
10739 temp = gen_rtx_LABEL_REF (Pmode, temp);
10740
10741 if (function != current_function_decl
10742 && function != 0)
10743 LABEL_REF_NONLOCAL_P (temp) = 1;
10744
10745 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
10746 return temp;
10747 }
10748
10749 case SSA_NAME:
10750 /* ??? ivopts calls expander, without any preparation from
10751 out-of-ssa. So fake instructions as if this was an access to the
10752 base variable. This unnecessarily allocates a pseudo, see how we can
10753 reuse it, if partition base vars have it set already. */
10754 if (!currently_expanding_to_rtl)
10755 {
10756 tree var = SSA_NAME_VAR (exp);
10757 if (var && DECL_RTL_SET_P (var))
10758 return DECL_RTL (var);
10759 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
10760 LAST_VIRTUAL_REGISTER + 1);
10761 }
10762
10763 g = get_gimple_for_ssa_name (exp);
10764 /* For EXPAND_INITIALIZER try harder to get something simpler. */
10765 if (g == NULL
10766 && modifier == EXPAND_INITIALIZER
10767 && !SSA_NAME_IS_DEFAULT_DEF (exp)
10768 && (optimize || !SSA_NAME_VAR (exp)
10769 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
10770 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
10771 g = SSA_NAME_DEF_STMT (exp);
10772 if (g)
10773 {
10774 rtx r;
10775 location_t saved_loc = curr_insn_location ();
10776 loc = gimple_location (g);
10777 if (loc != UNKNOWN_LOCATION)
10778 set_curr_insn_location (loc);
10779 ops.code = gimple_assign_rhs_code (g);
10780 switch (get_gimple_rhs_class (ops.code))
10781 {
10782 case GIMPLE_TERNARY_RHS:
10783 ops.op2 = gimple_assign_rhs3 (g);
10784 /* Fallthru */
10785 case GIMPLE_BINARY_RHS:
10786 ops.op1 = gimple_assign_rhs2 (g);
10787
10788 /* Try to expand conditonal compare. */
10789 if (targetm.gen_ccmp_first)
10790 {
10791 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
10792 r = expand_ccmp_expr (g, mode);
10793 if (r)
10794 break;
10795 }
10796 /* Fallthru */
10797 case GIMPLE_UNARY_RHS:
10798 ops.op0 = gimple_assign_rhs1 (g);
10799 ops.type = TREE_TYPE (gimple_assign_lhs (g));
10800 ops.location = loc;
10801 r = expand_expr_real_2 (&ops, target, tmode, modifier);
10802 break;
10803 case GIMPLE_SINGLE_RHS:
10804 {
10805 r = expand_expr_real (gimple_assign_rhs1 (g), target,
10806 tmode, modifier, alt_rtl,
10807 inner_reference_p);
10808 break;
10809 }
10810 default:
10811 gcc_unreachable ();
10812 }
10813 set_curr_insn_location (saved_loc);
10814 if (REG_P (r) && !REG_EXPR (r))
10815 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
10816 return r;
10817 }
10818
10819 ssa_name = exp;
10820 decl_rtl = get_rtx_for_ssa_name (ssa_name);
10821 exp = SSA_NAME_VAR (ssa_name);
10822 goto expand_decl_rtl;
10823
10824 case VAR_DECL:
10825 /* Allow accel compiler to handle variables that require special
10826 treatment, e.g. if they have been modified in some way earlier in
10827 compilation by the adjust_private_decl OpenACC hook. */
10828 if (flag_openacc && targetm.goacc.expand_var_decl)
10829 {
10830 temp = targetm.goacc.expand_var_decl (exp);
10831 if (temp)
10832 return temp;
10833 }
10834 /* Expand const VAR_DECLs with CONSTRUCTOR initializers that
10835 have scalar integer modes to a reg via store_constructor. */
10836 if (TREE_READONLY (exp)
10837 && !TREE_SIDE_EFFECTS (exp)
10838 && (modifier == EXPAND_NORMAL || modifier == EXPAND_STACK_PARM)
10839 && immediate_const_ctor_p (DECL_INITIAL (exp))
10840 && SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (exp)))
10841 && crtl->emit.regno_pointer_align_length
10842 && !target)
10843 {
10844 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
10845 store_constructor (DECL_INITIAL (exp), target, 0,
10846 int_expr_size (DECL_INITIAL (exp)), false);
10847 return target;
10848 }
10849 /* ... fall through ... */
10850
10851 case PARM_DECL:
10852 /* If a static var's type was incomplete when the decl was written,
10853 but the type is complete now, lay out the decl now. */
10854 if (DECL_SIZE (exp) == 0
10855 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
10856 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
10857 layout_decl (exp, 0);
10858
10859 /* fall through */
10860
10861 case FUNCTION_DECL:
10862 case RESULT_DECL:
10863 decl_rtl = DECL_RTL (exp);
10864 expand_decl_rtl:
10865 gcc_assert (decl_rtl);
10866
10867 /* DECL_MODE might change when TYPE_MODE depends on attribute target
10868 settings for VECTOR_TYPE_P that might switch for the function. */
10869 if (currently_expanding_to_rtl
10870 && code == VAR_DECL && MEM_P (decl_rtl)
10871 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
10872 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
10873 else
10874 decl_rtl = copy_rtx (decl_rtl);
10875
10876 /* Record writes to register variables. */
10877 if (modifier == EXPAND_WRITE
10878 && REG_P (decl_rtl)
10879 && HARD_REGISTER_P (decl_rtl))
10880 add_to_hard_reg_set (&crtl->asm_clobbers,
10881 GET_MODE (decl_rtl), REGNO (decl_rtl));
10882
10883 /* Ensure variable marked as used even if it doesn't go through
10884 a parser. If it hasn't be used yet, write out an external
10885 definition. */
10886 if (exp)
10887 TREE_USED (exp) = 1;
10888
10889 /* Show we haven't gotten RTL for this yet. */
10890 temp = 0;
10891
10892 /* Variables inherited from containing functions should have
10893 been lowered by this point. */
10894 if (exp)
10895 {
10896 tree context = decl_function_context (exp);
10897 gcc_assert (SCOPE_FILE_SCOPE_P (context)
10898 || context == current_function_decl
10899 || TREE_STATIC (exp)
10900 || DECL_EXTERNAL (exp)
10901 /* ??? C++ creates functions that are not
10902 TREE_STATIC. */
10903 || TREE_CODE (exp) == FUNCTION_DECL);
10904 }
10905
10906 /* This is the case of an array whose size is to be determined
10907 from its initializer, while the initializer is still being parsed.
10908 ??? We aren't parsing while expanding anymore. */
10909
10910 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10911 temp = validize_mem (decl_rtl);
10912
10913 /* If DECL_RTL is memory, we are in the normal case and the
10914 address is not valid, get the address into a register. */
10915
10916 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10917 {
10918 if (alt_rtl)
10919 *alt_rtl = decl_rtl;
10920 decl_rtl = use_anchored_address (decl_rtl);
10921 if (modifier != EXPAND_CONST_ADDRESS
10922 && modifier != EXPAND_SUM
10923 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10924 : GET_MODE (decl_rtl),
10925 XEXP (decl_rtl, 0),
10926 MEM_ADDR_SPACE (decl_rtl)))
10927 temp = replace_equiv_address (decl_rtl,
10928 copy_rtx (XEXP (decl_rtl, 0)));
10929 }
10930
10931 /* If we got something, return it. But first, set the alignment
10932 if the address is a register. */
10933 if (temp != 0)
10934 {
10935 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10936 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10937 }
10938 else if (MEM_P (decl_rtl))
10939 temp = decl_rtl;
10940
10941 if (temp != 0)
10942 {
10943 if (MEM_P (temp)
10944 && modifier != EXPAND_WRITE
10945 && modifier != EXPAND_MEMORY
10946 && modifier != EXPAND_INITIALIZER
10947 && modifier != EXPAND_CONST_ADDRESS
10948 && modifier != EXPAND_SUM
10949 && !inner_reference_p
10950 && mode != BLKmode
10951 && MEM_ALIGN (temp) < GET_MODE_ALIGNMENT (mode))
10952 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
10953 MEM_ALIGN (temp), NULL_RTX, NULL);
10954
10955 return temp;
10956 }
10957
10958 if (exp)
10959 dmode = DECL_MODE (exp);
10960 else
10961 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10962
10963 /* If the mode of DECL_RTL does not match that of the decl,
10964 there are two cases: we are dealing with a BLKmode value
10965 that is returned in a register, or we are dealing with
10966 a promoted value. In the latter case, return a SUBREG
10967 of the wanted mode, but mark it so that we know that it
10968 was already extended. */
10969 if (REG_P (decl_rtl)
10970 && dmode != BLKmode
10971 && GET_MODE (decl_rtl) != dmode)
10972 {
10973 machine_mode pmode;
10974
10975 /* Get the signedness to be used for this variable. Ensure we get
10976 the same mode we got when the variable was declared. */
10977 if (code != SSA_NAME)
10978 pmode = promote_decl_mode (exp, &unsignedp);
10979 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10980 && gimple_code (g) == GIMPLE_CALL
10981 && !gimple_call_internal_p (g))
10982 pmode = promote_function_mode (type, mode, &unsignedp,
10983 gimple_call_fntype (g),
10984 2);
10985 else
10986 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10987 gcc_assert (GET_MODE (decl_rtl) == pmode);
10988
10989 /* Some ABIs require scalar floating point modes to be passed
10990 in a wider scalar integer mode. We need to explicitly
10991 truncate to an integer mode of the correct precision before
10992 using a SUBREG to reinterpret as a floating point value. */
10993 if (SCALAR_FLOAT_MODE_P (mode)
10994 && SCALAR_INT_MODE_P (pmode)
10995 && known_lt (GET_MODE_SIZE (mode), GET_MODE_SIZE (pmode)))
10996 return convert_wider_int_to_float (mode, pmode, decl_rtl);
10997
10998 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10999 SUBREG_PROMOTED_VAR_P (temp) = 1;
11000 SUBREG_PROMOTED_SET (temp, unsignedp);
11001 return temp;
11002 }
11003
11004 return decl_rtl;
11005
11006 case INTEGER_CST:
11007 {
11008 /* Given that TYPE_PRECISION (type) is not always equal to
11009 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
11010 the former to the latter according to the signedness of the
11011 type. */
11012 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (type);
11013 temp = immed_wide_int_const
11014 (wi::to_wide (exp, GET_MODE_PRECISION (int_mode)), int_mode);
11015 return temp;
11016 }
11017
11018 case VECTOR_CST:
11019 {
11020 tree tmp = NULL_TREE;
11021 if (VECTOR_MODE_P (mode))
11022 return const_vector_from_tree (exp);
11023 scalar_int_mode int_mode;
11024 if (is_int_mode (mode, &int_mode))
11025 {
11026 tree type_for_mode = lang_hooks.types.type_for_mode (int_mode, 1);
11027 if (type_for_mode)
11028 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
11029 type_for_mode, exp);
11030 }
11031 if (!tmp)
11032 {
11033 vec<constructor_elt, va_gc> *v;
11034 /* Constructors need to be fixed-length. FIXME. */
11035 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11036 vec_alloc (v, nunits);
11037 for (unsigned int i = 0; i < nunits; ++i)
11038 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
11039 tmp = build_constructor (type, v);
11040 }
11041 return expand_expr (tmp, ignore ? const0_rtx : target,
11042 tmode, modifier);
11043 }
11044
11045 case CONST_DECL:
11046 if (modifier == EXPAND_WRITE)
11047 {
11048 /* Writing into CONST_DECL is always invalid, but handle it
11049 gracefully. */
11050 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
11051 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
11052 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
11053 EXPAND_NORMAL, as);
11054 op0 = memory_address_addr_space (mode, op0, as);
11055 temp = gen_rtx_MEM (mode, op0);
11056 set_mem_addr_space (temp, as);
11057 return temp;
11058 }
11059 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
11060
11061 case REAL_CST:
11062 /* If optimized, generate immediate CONST_DOUBLE
11063 which will be turned into memory by reload if necessary.
11064
11065 We used to force a register so that loop.c could see it. But
11066 this does not allow gen_* patterns to perform optimizations with
11067 the constants. It also produces two insns in cases like "x = 1.0;".
11068 On most machines, floating-point constants are not permitted in
11069 many insns, so we'd end up copying it to a register in any case.
11070
11071 Now, we do the copying in expand_binop, if appropriate. */
11072 return const_double_from_real_value (TREE_REAL_CST (exp),
11073 TYPE_MODE (TREE_TYPE (exp)));
11074
11075 case FIXED_CST:
11076 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
11077 TYPE_MODE (TREE_TYPE (exp)));
11078
11079 case COMPLEX_CST:
11080 /* Handle evaluating a complex constant in a CONCAT target. */
11081 if (original_target && GET_CODE (original_target) == CONCAT)
11082 {
11083 rtx rtarg, itarg;
11084
11085 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
11086 rtarg = XEXP (original_target, 0);
11087 itarg = XEXP (original_target, 1);
11088
11089 /* Move the real and imaginary parts separately. */
11090 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
11091 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
11092
11093 if (op0 != rtarg)
11094 emit_move_insn (rtarg, op0);
11095 if (op1 != itarg)
11096 emit_move_insn (itarg, op1);
11097
11098 return original_target;
11099 }
11100
11101 /* fall through */
11102
11103 case STRING_CST:
11104 temp = expand_expr_constant (exp, 1, modifier);
11105
11106 /* temp contains a constant address.
11107 On RISC machines where a constant address isn't valid,
11108 make some insns to get that address into a register. */
11109 if (modifier != EXPAND_CONST_ADDRESS
11110 && modifier != EXPAND_INITIALIZER
11111 && modifier != EXPAND_SUM
11112 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
11113 MEM_ADDR_SPACE (temp)))
11114 return replace_equiv_address (temp,
11115 copy_rtx (XEXP (temp, 0)));
11116 return temp;
11117
11118 case POLY_INT_CST:
11119 return immed_wide_int_const (poly_int_cst_value (exp), mode);
11120
11121 case SAVE_EXPR:
11122 {
11123 tree val = treeop0;
11124 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
11125 inner_reference_p);
11126
11127 if (!SAVE_EXPR_RESOLVED_P (exp))
11128 {
11129 /* We can indeed still hit this case, typically via builtin
11130 expanders calling save_expr immediately before expanding
11131 something. Assume this means that we only have to deal
11132 with non-BLKmode values. */
11133 gcc_assert (GET_MODE (ret) != BLKmode);
11134
11135 val = build_decl (curr_insn_location (),
11136 VAR_DECL, NULL, TREE_TYPE (exp));
11137 DECL_ARTIFICIAL (val) = 1;
11138 DECL_IGNORED_P (val) = 1;
11139 treeop0 = val;
11140 TREE_OPERAND (exp, 0) = treeop0;
11141 SAVE_EXPR_RESOLVED_P (exp) = 1;
11142
11143 if (!CONSTANT_P (ret))
11144 ret = copy_to_reg (ret);
11145 SET_DECL_RTL (val, ret);
11146 }
11147
11148 return ret;
11149 }
11150
11151
11152 case CONSTRUCTOR:
11153 /* If we don't need the result, just ensure we evaluate any
11154 subexpressions. */
11155 if (ignore)
11156 {
11157 unsigned HOST_WIDE_INT idx;
11158 tree value;
11159
11160 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
11161 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
11162
11163 return const0_rtx;
11164 }
11165
11166 return expand_constructor (exp, target, modifier, false);
11167
11168 case TARGET_MEM_REF:
11169 {
11170 addr_space_t as
11171 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11172 unsigned int align;
11173
11174 op0 = addr_for_mem_ref (exp, as, true);
11175 op0 = memory_address_addr_space (mode, op0, as);
11176 temp = gen_rtx_MEM (mode, op0);
11177 set_mem_attributes (temp, exp, 0);
11178 set_mem_addr_space (temp, as);
11179 align = get_object_alignment (exp);
11180 if (modifier != EXPAND_WRITE
11181 && modifier != EXPAND_MEMORY
11182 && mode != BLKmode
11183 && align < GET_MODE_ALIGNMENT (mode))
11184 temp = expand_misaligned_mem_ref (temp, mode, unsignedp,
11185 align, NULL_RTX, NULL);
11186 return temp;
11187 }
11188
11189 case MEM_REF:
11190 {
11191 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
11192 addr_space_t as
11193 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
11194 machine_mode address_mode;
11195 tree base = TREE_OPERAND (exp, 0);
11196 gimple *def_stmt;
11197 unsigned align;
11198 /* Handle expansion of non-aliased memory with non-BLKmode. That
11199 might end up in a register. */
11200 if (mem_ref_refers_to_non_mem_p (exp))
11201 {
11202 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
11203 base = TREE_OPERAND (base, 0);
11204 poly_uint64 type_size;
11205 if (known_eq (offset, 0)
11206 && !reverse
11207 && poly_int_tree_p (TYPE_SIZE (type), &type_size)
11208 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)), type_size))
11209 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
11210 target, tmode, modifier);
11211 if (TYPE_MODE (type) == BLKmode)
11212 {
11213 temp = assign_stack_temp (DECL_MODE (base),
11214 GET_MODE_SIZE (DECL_MODE (base)));
11215 store_expr (base, temp, 0, false, false);
11216 temp = adjust_address (temp, BLKmode, offset);
11217 set_mem_size (temp, int_size_in_bytes (type));
11218 return temp;
11219 }
11220 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
11221 bitsize_int (offset * BITS_PER_UNIT));
11222 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
11223 return expand_expr (exp, target, tmode, modifier);
11224 }
11225 address_mode = targetm.addr_space.address_mode (as);
11226 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
11227 {
11228 tree mask = gimple_assign_rhs2 (def_stmt);
11229 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
11230 gimple_assign_rhs1 (def_stmt), mask);
11231 TREE_OPERAND (exp, 0) = base;
11232 }
11233 align = get_object_alignment (exp);
11234 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
11235 op0 = memory_address_addr_space (mode, op0, as);
11236 if (!integer_zerop (TREE_OPERAND (exp, 1)))
11237 {
11238 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
11239 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
11240 op0 = memory_address_addr_space (mode, op0, as);
11241 }
11242 temp = gen_rtx_MEM (mode, op0);
11243 set_mem_attributes (temp, exp, 0);
11244 set_mem_addr_space (temp, as);
11245 if (TREE_THIS_VOLATILE (exp))
11246 MEM_VOLATILE_P (temp) = 1;
11247 if (modifier != EXPAND_WRITE
11248 && modifier != EXPAND_MEMORY
11249 && !inner_reference_p
11250 && mode != BLKmode
11251 && align < GET_MODE_ALIGNMENT (mode))
11252 temp = expand_misaligned_mem_ref (temp, mode, unsignedp, align,
11253 modifier == EXPAND_STACK_PARM
11254 ? NULL_RTX : target, alt_rtl);
11255 if (reverse
11256 && modifier != EXPAND_MEMORY
11257 && modifier != EXPAND_WRITE)
11258 temp = flip_storage_order (mode, temp);
11259 return temp;
11260 }
11261
11262 case ARRAY_REF:
11263
11264 {
11265 tree array = treeop0;
11266 tree index = treeop1;
11267 tree init;
11268
11269 /* Fold an expression like: "foo"[2].
11270 This is not done in fold so it won't happen inside &.
11271 Don't fold if this is for wide characters since it's too
11272 difficult to do correctly and this is a very rare case. */
11273
11274 if (modifier != EXPAND_CONST_ADDRESS
11275 && modifier != EXPAND_INITIALIZER
11276 && modifier != EXPAND_MEMORY)
11277 {
11278 tree t = fold_read_from_constant_string (exp);
11279
11280 if (t)
11281 return expand_expr (t, target, tmode, modifier);
11282 }
11283
11284 /* If this is a constant index into a constant array,
11285 just get the value from the array. Handle both the cases when
11286 we have an explicit constructor and when our operand is a variable
11287 that was declared const. */
11288
11289 if (modifier != EXPAND_CONST_ADDRESS
11290 && modifier != EXPAND_INITIALIZER
11291 && modifier != EXPAND_MEMORY
11292 && TREE_CODE (array) == CONSTRUCTOR
11293 && ! TREE_SIDE_EFFECTS (array)
11294 && TREE_CODE (index) == INTEGER_CST)
11295 {
11296 unsigned HOST_WIDE_INT ix;
11297 tree field, value;
11298
11299 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
11300 field, value)
11301 if (tree_int_cst_equal (field, index))
11302 {
11303 if (!TREE_SIDE_EFFECTS (value))
11304 return expand_expr (fold (value), target, tmode, modifier);
11305 break;
11306 }
11307 }
11308
11309 else if (optimize >= 1
11310 && modifier != EXPAND_CONST_ADDRESS
11311 && modifier != EXPAND_INITIALIZER
11312 && modifier != EXPAND_MEMORY
11313 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
11314 && TREE_CODE (index) == INTEGER_CST
11315 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11316 && (init = ctor_for_folding (array)) != error_mark_node)
11317 {
11318 if (init == NULL_TREE)
11319 {
11320 tree value = build_zero_cst (type);
11321 if (TREE_CODE (value) == CONSTRUCTOR)
11322 {
11323 /* If VALUE is a CONSTRUCTOR, this optimization is only
11324 useful if this doesn't store the CONSTRUCTOR into
11325 memory. If it does, it is more efficient to just
11326 load the data from the array directly. */
11327 rtx ret = expand_constructor (value, target,
11328 modifier, true);
11329 if (ret == NULL_RTX)
11330 value = NULL_TREE;
11331 }
11332
11333 if (value)
11334 return expand_expr (value, target, tmode, modifier);
11335 }
11336 else if (TREE_CODE (init) == CONSTRUCTOR)
11337 {
11338 unsigned HOST_WIDE_INT ix;
11339 tree field, value;
11340
11341 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
11342 field, value)
11343 if (tree_int_cst_equal (field, index))
11344 {
11345 if (TREE_SIDE_EFFECTS (value))
11346 break;
11347
11348 if (TREE_CODE (value) == CONSTRUCTOR)
11349 {
11350 /* If VALUE is a CONSTRUCTOR, this
11351 optimization is only useful if
11352 this doesn't store the CONSTRUCTOR
11353 into memory. If it does, it is more
11354 efficient to just load the data from
11355 the array directly. */
11356 rtx ret = expand_constructor (value, target,
11357 modifier, true);
11358 if (ret == NULL_RTX)
11359 break;
11360 }
11361
11362 return
11363 expand_expr (fold (value), target, tmode, modifier);
11364 }
11365 }
11366 else if (TREE_CODE (init) == STRING_CST)
11367 {
11368 tree low_bound = array_ref_low_bound (exp);
11369 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
11370
11371 /* Optimize the special case of a zero lower bound.
11372
11373 We convert the lower bound to sizetype to avoid problems
11374 with constant folding. E.g. suppose the lower bound is
11375 1 and its mode is QI. Without the conversion
11376 (ARRAY + (INDEX - (unsigned char)1))
11377 becomes
11378 (ARRAY + (-(unsigned char)1) + INDEX)
11379 which becomes
11380 (ARRAY + 255 + INDEX). Oops! */
11381 if (!integer_zerop (low_bound))
11382 index1 = size_diffop_loc (loc, index1,
11383 fold_convert_loc (loc, sizetype,
11384 low_bound));
11385
11386 if (tree_fits_uhwi_p (index1)
11387 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
11388 {
11389 tree char_type = TREE_TYPE (TREE_TYPE (init));
11390 scalar_int_mode char_mode;
11391
11392 if (is_int_mode (TYPE_MODE (char_type), &char_mode)
11393 && GET_MODE_SIZE (char_mode) == 1)
11394 return gen_int_mode (TREE_STRING_POINTER (init)
11395 [TREE_INT_CST_LOW (index1)],
11396 char_mode);
11397 }
11398 }
11399 }
11400 }
11401 goto normal_inner_ref;
11402
11403 case COMPONENT_REF:
11404 gcc_assert (TREE_CODE (treeop0) != CONSTRUCTOR);
11405 /* Fall through. */
11406 case BIT_FIELD_REF:
11407 case ARRAY_RANGE_REF:
11408 normal_inner_ref:
11409 {
11410 machine_mode mode1, mode2;
11411 poly_int64 bitsize, bitpos, bytepos;
11412 tree offset;
11413 int reversep, volatilep = 0;
11414 tree tem
11415 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
11416 &unsignedp, &reversep, &volatilep);
11417 rtx orig_op0, memloc;
11418 bool clear_mem_expr = false;
11419 bool must_force_mem;
11420
11421 /* If we got back the original object, something is wrong. Perhaps
11422 we are evaluating an expression too early. In any event, don't
11423 infinitely recurse. */
11424 gcc_assert (tem != exp);
11425
11426 /* Make sure bitpos is not negative, this can wreak havoc later. */
11427 if (maybe_lt (bitpos, 0))
11428 {
11429 gcc_checking_assert (offset == NULL_TREE);
11430 offset = size_int (bits_to_bytes_round_down (bitpos));
11431 bitpos = num_trailing_bits (bitpos);
11432 }
11433
11434 /* If we have either an offset, a BLKmode result, or a reference
11435 outside the underlying object, we must force it to memory.
11436 Such a case can occur in Ada if we have unchecked conversion
11437 of an expression from a scalar type to an aggregate type or
11438 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
11439 passed a partially uninitialized object or a view-conversion
11440 to a larger size. */
11441 must_force_mem = offset != NULL_TREE
11442 || mode1 == BLKmode
11443 || (mode == BLKmode
11444 && !int_mode_for_size (bitsize, 1).exists ());
11445
11446 const enum expand_modifier tem_modifier
11447 = must_force_mem
11448 ? EXPAND_MEMORY
11449 : modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier;
11450
11451 /* If TEM's type is a union of variable size, pass TARGET to the inner
11452 computation, since it will need a temporary and TARGET is known
11453 to have to do. This occurs in unchecked conversion in Ada. */
11454 const rtx tem_target
11455 = TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11456 && COMPLETE_TYPE_P (TREE_TYPE (tem))
11457 && TREE_CODE (TYPE_SIZE (TREE_TYPE (tem))) != INTEGER_CST
11458 && modifier != EXPAND_STACK_PARM
11459 ? target
11460 : NULL_RTX;
11461
11462 orig_op0 = op0
11463 = expand_expr_real (tem, tem_target, VOIDmode, tem_modifier, NULL,
11464 true);
11465
11466 /* If the field has a mode, we want to access it in the
11467 field's mode, not the computed mode.
11468 If a MEM has VOIDmode (external with incomplete type),
11469 use BLKmode for it instead. */
11470 if (MEM_P (op0))
11471 {
11472 if (mode1 != VOIDmode)
11473 op0 = adjust_address (op0, mode1, 0);
11474 else if (GET_MODE (op0) == VOIDmode)
11475 op0 = adjust_address (op0, BLKmode, 0);
11476 }
11477
11478 mode2
11479 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
11480
11481 /* See above for the rationale. */
11482 if (maybe_gt (bitpos + bitsize, GET_MODE_BITSIZE (mode2)))
11483 must_force_mem = true;
11484
11485 /* Handle CONCAT first. */
11486 if (GET_CODE (op0) == CONCAT && !must_force_mem)
11487 {
11488 if (known_eq (bitpos, 0)
11489 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
11490 && COMPLEX_MODE_P (mode1)
11491 && COMPLEX_MODE_P (GET_MODE (op0))
11492 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
11493 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
11494 {
11495 if (reversep)
11496 op0 = flip_storage_order (GET_MODE (op0), op0);
11497 if (mode1 != GET_MODE (op0))
11498 {
11499 rtx parts[2];
11500 for (int i = 0; i < 2; i++)
11501 {
11502 rtx op = read_complex_part (op0, i != 0);
11503 if (GET_CODE (op) == SUBREG)
11504 op = force_reg (GET_MODE (op), op);
11505 temp = gen_lowpart_common (GET_MODE_INNER (mode1), op);
11506 if (temp)
11507 op = temp;
11508 else
11509 {
11510 if (!REG_P (op) && !MEM_P (op))
11511 op = force_reg (GET_MODE (op), op);
11512 op = gen_lowpart (GET_MODE_INNER (mode1), op);
11513 }
11514 parts[i] = op;
11515 }
11516 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
11517 }
11518 return op0;
11519 }
11520 if (known_eq (bitpos, 0)
11521 && known_eq (bitsize,
11522 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11523 && maybe_ne (bitsize, 0))
11524 {
11525 op0 = XEXP (op0, 0);
11526 mode2 = GET_MODE (op0);
11527 }
11528 else if (known_eq (bitpos,
11529 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
11530 && known_eq (bitsize,
11531 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
11532 && maybe_ne (bitpos, 0)
11533 && maybe_ne (bitsize, 0))
11534 {
11535 op0 = XEXP (op0, 1);
11536 bitpos = 0;
11537 mode2 = GET_MODE (op0);
11538 }
11539 else
11540 /* Otherwise force into memory. */
11541 must_force_mem = true;
11542 }
11543
11544 /* If this is a constant, put it in a register if it is a legitimate
11545 constant and we don't need a memory reference. */
11546 if (CONSTANT_P (op0)
11547 && mode2 != BLKmode
11548 && targetm.legitimate_constant_p (mode2, op0)
11549 && !must_force_mem)
11550 op0 = force_reg (mode2, op0);
11551
11552 /* Otherwise, if this is a constant, try to force it to the constant
11553 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
11554 is a legitimate constant. */
11555 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
11556 op0 = validize_mem (memloc);
11557
11558 /* Otherwise, if this is a constant or the object is not in memory
11559 and need be, put it there. */
11560 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
11561 {
11562 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
11563 emit_move_insn (memloc, op0);
11564 op0 = memloc;
11565 clear_mem_expr = true;
11566 }
11567
11568 if (offset)
11569 {
11570 machine_mode address_mode;
11571 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
11572 EXPAND_SUM);
11573
11574 gcc_assert (MEM_P (op0));
11575
11576 address_mode = get_address_mode (op0);
11577 if (GET_MODE (offset_rtx) != address_mode)
11578 {
11579 /* We cannot be sure that the RTL in offset_rtx is valid outside
11580 of a memory address context, so force it into a register
11581 before attempting to convert it to the desired mode. */
11582 offset_rtx = force_operand (offset_rtx, NULL_RTX);
11583 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
11584 }
11585
11586 /* See the comment in expand_assignment for the rationale. */
11587 if (mode1 != VOIDmode
11588 && maybe_ne (bitpos, 0)
11589 && maybe_gt (bitsize, 0)
11590 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11591 && multiple_p (bitpos, bitsize)
11592 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
11593 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
11594 {
11595 op0 = adjust_address (op0, mode1, bytepos);
11596 bitpos = 0;
11597 }
11598
11599 op0 = offset_address (op0, offset_rtx,
11600 highest_pow2_factor (offset));
11601 }
11602
11603 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
11604 record its alignment as BIGGEST_ALIGNMENT. */
11605 if (MEM_P (op0)
11606 && known_eq (bitpos, 0)
11607 && offset != 0
11608 && is_aligning_offset (offset, tem))
11609 set_mem_align (op0, BIGGEST_ALIGNMENT);
11610
11611 /* Don't forget about volatility even if this is a bitfield. */
11612 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
11613 {
11614 if (op0 == orig_op0)
11615 op0 = copy_rtx (op0);
11616
11617 MEM_VOLATILE_P (op0) = 1;
11618 }
11619
11620 if (MEM_P (op0) && TREE_CODE (tem) == FUNCTION_DECL)
11621 {
11622 if (op0 == orig_op0)
11623 op0 = copy_rtx (op0);
11624
11625 set_mem_align (op0, BITS_PER_UNIT);
11626 }
11627
11628 /* In cases where an aligned union has an unaligned object
11629 as a field, we might be extracting a BLKmode value from
11630 an integer-mode (e.g., SImode) object. Handle this case
11631 by doing the extract into an object as wide as the field
11632 (which we know to be the width of a basic mode), then
11633 storing into memory, and changing the mode to BLKmode. */
11634 if (mode1 == VOIDmode
11635 || REG_P (op0) || GET_CODE (op0) == SUBREG
11636 || (mode1 != BLKmode && ! direct_load[(int) mode1]
11637 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
11638 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
11639 && modifier != EXPAND_CONST_ADDRESS
11640 && modifier != EXPAND_INITIALIZER
11641 && modifier != EXPAND_MEMORY)
11642 /* If the bitfield is volatile and the bitsize
11643 is narrower than the access size of the bitfield,
11644 we need to extract bitfields from the access. */
11645 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
11646 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
11647 && mode1 != BLKmode
11648 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
11649 /* If the field isn't aligned enough to fetch as a memref,
11650 fetch it as a bit field. */
11651 || (mode1 != BLKmode
11652 && (((MEM_P (op0)
11653 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
11654 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
11655 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
11656 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
11657 && modifier != EXPAND_MEMORY
11658 && ((modifier == EXPAND_CONST_ADDRESS
11659 || modifier == EXPAND_INITIALIZER)
11660 ? STRICT_ALIGNMENT
11661 : targetm.slow_unaligned_access (mode1,
11662 MEM_ALIGN (op0))))
11663 || !multiple_p (bitpos, BITS_PER_UNIT)))
11664 /* If the type and the field are a constant size and the
11665 size of the type isn't the same size as the bitfield,
11666 we must use bitfield operations. */
11667 || (known_size_p (bitsize)
11668 && TYPE_SIZE (TREE_TYPE (exp))
11669 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
11670 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
11671 bitsize)))
11672 {
11673 machine_mode ext_mode = mode;
11674
11675 if (ext_mode == BLKmode
11676 && ! (target != 0 && MEM_P (op0)
11677 && MEM_P (target)
11678 && multiple_p (bitpos, BITS_PER_UNIT)))
11679 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
11680
11681 if (ext_mode == BLKmode)
11682 {
11683 if (target == 0)
11684 target = assign_temp (type, 1, 1);
11685
11686 /* ??? Unlike the similar test a few lines below, this one is
11687 very likely obsolete. */
11688 if (known_eq (bitsize, 0))
11689 return target;
11690
11691 /* In this case, BITPOS must start at a byte boundary and
11692 TARGET, if specified, must be a MEM. */
11693 gcc_assert (MEM_P (op0)
11694 && (!target || MEM_P (target)));
11695
11696 bytepos = exact_div (bitpos, BITS_PER_UNIT);
11697 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
11698 emit_block_move (target,
11699 adjust_address (op0, VOIDmode, bytepos),
11700 gen_int_mode (bytesize, Pmode),
11701 (modifier == EXPAND_STACK_PARM
11702 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11703
11704 return target;
11705 }
11706
11707 /* If we have nothing to extract, the result will be 0 for targets
11708 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
11709 return 0 for the sake of consistency, as reading a zero-sized
11710 bitfield is valid in Ada and the value is fully specified. */
11711 if (known_eq (bitsize, 0))
11712 return const0_rtx;
11713
11714 op0 = validize_mem (op0);
11715
11716 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
11717 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11718
11719 /* If the result has aggregate type and the extraction is done in
11720 an integral mode, then the field may be not aligned on a byte
11721 boundary; in this case, if it has reverse storage order, it
11722 needs to be extracted as a scalar field with reverse storage
11723 order and put back into memory order afterwards. */
11724 if (AGGREGATE_TYPE_P (type)
11725 && GET_MODE_CLASS (ext_mode) == MODE_INT)
11726 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
11727
11728 gcc_checking_assert (known_ge (bitpos, 0));
11729 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
11730 (modifier == EXPAND_STACK_PARM
11731 ? NULL_RTX : target),
11732 ext_mode, ext_mode, reversep, alt_rtl);
11733
11734 /* If the result has aggregate type and the mode of OP0 is an
11735 integral mode then, if BITSIZE is narrower than this mode
11736 and this is for big-endian data, we must put the field
11737 into the high-order bits. And we must also put it back
11738 into memory order if it has been previously reversed. */
11739 scalar_int_mode op0_mode;
11740 if (AGGREGATE_TYPE_P (type)
11741 && is_int_mode (GET_MODE (op0), &op0_mode))
11742 {
11743 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
11744
11745 gcc_checking_assert (known_le (bitsize, size));
11746 if (maybe_lt (bitsize, size)
11747 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
11748 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
11749 size - bitsize, op0, 1);
11750
11751 if (reversep)
11752 op0 = flip_storage_order (op0_mode, op0);
11753 }
11754
11755 /* If the result type is BLKmode, store the data into a temporary
11756 of the appropriate type, but with the mode corresponding to the
11757 mode for the data we have (op0's mode). */
11758 if (mode == BLKmode)
11759 {
11760 rtx new_rtx
11761 = assign_stack_temp_for_type (ext_mode,
11762 GET_MODE_BITSIZE (ext_mode),
11763 type);
11764 emit_move_insn (new_rtx, op0);
11765 op0 = copy_rtx (new_rtx);
11766 PUT_MODE (op0, BLKmode);
11767 }
11768
11769 return op0;
11770 }
11771
11772 /* If the result is BLKmode, use that to access the object
11773 now as well. */
11774 if (mode == BLKmode)
11775 mode1 = BLKmode;
11776
11777 /* Get a reference to just this component. */
11778 bytepos = bits_to_bytes_round_down (bitpos);
11779 if (modifier == EXPAND_CONST_ADDRESS
11780 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
11781 op0 = adjust_address_nv (op0, mode1, bytepos);
11782 else
11783 op0 = adjust_address (op0, mode1, bytepos);
11784
11785 if (op0 == orig_op0)
11786 op0 = copy_rtx (op0);
11787
11788 /* Don't set memory attributes if the base expression is
11789 SSA_NAME that got expanded as a MEM or a CONSTANT. In that case,
11790 we should just honor its original memory attributes. */
11791 if (!(TREE_CODE (tem) == SSA_NAME
11792 && (MEM_P (orig_op0) || CONSTANT_P (orig_op0))))
11793 set_mem_attributes (op0, exp, 0);
11794
11795 if (REG_P (XEXP (op0, 0)))
11796 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11797
11798 /* If op0 is a temporary because the original expressions was forced
11799 to memory, clear MEM_EXPR so that the original expression cannot
11800 be marked as addressable through MEM_EXPR of the temporary. */
11801 if (clear_mem_expr)
11802 set_mem_expr (op0, NULL_TREE);
11803
11804 MEM_VOLATILE_P (op0) |= volatilep;
11805
11806 if (reversep
11807 && modifier != EXPAND_MEMORY
11808 && modifier != EXPAND_WRITE)
11809 op0 = flip_storage_order (mode1, op0);
11810
11811 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
11812 || modifier == EXPAND_CONST_ADDRESS
11813 || modifier == EXPAND_INITIALIZER)
11814 return op0;
11815
11816 if (target == 0)
11817 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
11818
11819 convert_move (target, op0, unsignedp);
11820 return target;
11821 }
11822
11823 case OBJ_TYPE_REF:
11824 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
11825
11826 case CALL_EXPR:
11827 /* All valid uses of __builtin_va_arg_pack () are removed during
11828 inlining. */
11829 if (CALL_EXPR_VA_ARG_PACK (exp))
11830 error ("invalid use of %<__builtin_va_arg_pack ()%>");
11831 {
11832 tree fndecl = get_callee_fndecl (exp), attr;
11833
11834 if (fndecl
11835 /* Don't diagnose the error attribute in thunks, those are
11836 artificially created. */
11837 && !CALL_FROM_THUNK_P (exp)
11838 && (attr = lookup_attribute ("error",
11839 DECL_ATTRIBUTES (fndecl))) != NULL)
11840 {
11841 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11842 error ("call to %qs declared with attribute error: %s",
11843 identifier_to_locale (ident),
11844 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11845 }
11846 if (fndecl
11847 /* Don't diagnose the warning attribute in thunks, those are
11848 artificially created. */
11849 && !CALL_FROM_THUNK_P (exp)
11850 && (attr = lookup_attribute ("warning",
11851 DECL_ATTRIBUTES (fndecl))) != NULL)
11852 {
11853 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
11854 warning_at (EXPR_LOCATION (exp),
11855 OPT_Wattribute_warning,
11856 "call to %qs declared with attribute warning: %s",
11857 identifier_to_locale (ident),
11858 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
11859 }
11860
11861 /* Check for a built-in function. */
11862 if (fndecl && fndecl_built_in_p (fndecl))
11863 {
11864 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
11865 return expand_builtin (exp, target, subtarget, tmode, ignore);
11866 }
11867 }
11868 return expand_call (exp, target, ignore);
11869
11870 case VIEW_CONVERT_EXPR:
11871 op0 = NULL_RTX;
11872
11873 /* If we are converting to BLKmode, try to avoid an intermediate
11874 temporary by fetching an inner memory reference. */
11875 if (mode == BLKmode
11876 && poly_int_tree_p (TYPE_SIZE (type))
11877 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11878 && handled_component_p (treeop0))
11879 {
11880 machine_mode mode1;
11881 poly_int64 bitsize, bitpos, bytepos;
11882 tree offset;
11883 int reversep, volatilep = 0;
11884 tree tem
11885 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11886 &unsignedp, &reversep, &volatilep);
11887
11888 /* ??? We should work harder and deal with non-zero offsets. */
11889 if (!offset
11890 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11891 && !reversep
11892 && known_size_p (bitsize)
11893 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11894 {
11895 /* See the normal_inner_ref case for the rationale. */
11896 rtx orig_op0
11897 = expand_expr_real (tem,
11898 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11899 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11900 != INTEGER_CST)
11901 && modifier != EXPAND_STACK_PARM
11902 ? target : NULL_RTX),
11903 VOIDmode,
11904 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11905 NULL, true);
11906
11907 if (MEM_P (orig_op0))
11908 {
11909 op0 = orig_op0;
11910
11911 /* Get a reference to just this component. */
11912 if (modifier == EXPAND_CONST_ADDRESS
11913 || modifier == EXPAND_SUM
11914 || modifier == EXPAND_INITIALIZER)
11915 op0 = adjust_address_nv (op0, mode, bytepos);
11916 else
11917 op0 = adjust_address (op0, mode, bytepos);
11918
11919 if (op0 == orig_op0)
11920 op0 = copy_rtx (op0);
11921
11922 set_mem_attributes (op0, treeop0, 0);
11923 if (REG_P (XEXP (op0, 0)))
11924 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11925
11926 MEM_VOLATILE_P (op0) |= volatilep;
11927 }
11928 }
11929 }
11930
11931 if (!op0)
11932 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11933 NULL, inner_reference_p);
11934
11935 /* If the input and output modes are both the same, we are done. */
11936 if (mode == GET_MODE (op0))
11937 ;
11938 /* If neither mode is BLKmode, and both modes are the same size
11939 then we can use gen_lowpart. */
11940 else if (mode != BLKmode
11941 && GET_MODE (op0) != BLKmode
11942 && known_eq (GET_MODE_PRECISION (mode),
11943 GET_MODE_PRECISION (GET_MODE (op0)))
11944 && !COMPLEX_MODE_P (GET_MODE (op0)))
11945 {
11946 if (GET_CODE (op0) == SUBREG)
11947 op0 = force_reg (GET_MODE (op0), op0);
11948 temp = gen_lowpart_common (mode, op0);
11949 if (temp)
11950 op0 = temp;
11951 else
11952 {
11953 if (!REG_P (op0) && !MEM_P (op0))
11954 op0 = force_reg (GET_MODE (op0), op0);
11955 op0 = gen_lowpart (mode, op0);
11956 }
11957 }
11958 /* If both types are integral, convert from one mode to the other. */
11959 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11960 op0 = convert_modes (mode, GET_MODE (op0), op0,
11961 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11962 /* If the output type is a bit-field type, do an extraction. */
11963 else if (reduce_bit_field)
11964 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11965 TYPE_UNSIGNED (type), NULL_RTX,
11966 mode, mode, false, NULL);
11967 /* As a last resort, spill op0 to memory, and reload it in a
11968 different mode. */
11969 else if (!MEM_P (op0))
11970 {
11971 /* If the operand is not a MEM, force it into memory. Since we
11972 are going to be changing the mode of the MEM, don't call
11973 force_const_mem for constants because we don't allow pool
11974 constants to change mode. */
11975 tree inner_type = TREE_TYPE (treeop0);
11976
11977 gcc_assert (!TREE_ADDRESSABLE (exp));
11978
11979 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11980 target
11981 = assign_stack_temp_for_type
11982 (TYPE_MODE (inner_type),
11983 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11984
11985 emit_move_insn (target, op0);
11986 op0 = target;
11987 }
11988
11989 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11990 output type is such that the operand is known to be aligned, indicate
11991 that it is. Otherwise, we need only be concerned about alignment for
11992 non-BLKmode results. */
11993 if (MEM_P (op0))
11994 {
11995 enum insn_code icode;
11996
11997 if (modifier != EXPAND_WRITE
11998 && modifier != EXPAND_MEMORY
11999 && !inner_reference_p
12000 && mode != BLKmode
12001 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
12002 {
12003 /* If the target does have special handling for unaligned
12004 loads of mode then use them. */
12005 if ((icode = optab_handler (movmisalign_optab, mode))
12006 != CODE_FOR_nothing)
12007 {
12008 rtx reg;
12009
12010 op0 = adjust_address (op0, mode, 0);
12011 /* We've already validated the memory, and we're creating a
12012 new pseudo destination. The predicates really can't
12013 fail. */
12014 reg = gen_reg_rtx (mode);
12015
12016 /* Nor can the insn generator. */
12017 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
12018 emit_insn (insn);
12019 return reg;
12020 }
12021 else if (STRICT_ALIGNMENT)
12022 {
12023 poly_uint64 mode_size = GET_MODE_SIZE (mode);
12024 poly_uint64 temp_size = mode_size;
12025 if (GET_MODE (op0) != BLKmode)
12026 temp_size = upper_bound (temp_size,
12027 GET_MODE_SIZE (GET_MODE (op0)));
12028 rtx new_rtx
12029 = assign_stack_temp_for_type (mode, temp_size, type);
12030 rtx new_with_op0_mode
12031 = adjust_address (new_rtx, GET_MODE (op0), 0);
12032
12033 gcc_assert (!TREE_ADDRESSABLE (exp));
12034
12035 if (GET_MODE (op0) == BLKmode)
12036 {
12037 rtx size_rtx = gen_int_mode (mode_size, Pmode);
12038 emit_block_move (new_with_op0_mode, op0, size_rtx,
12039 (modifier == EXPAND_STACK_PARM
12040 ? BLOCK_OP_CALL_PARM
12041 : BLOCK_OP_NORMAL));
12042 }
12043 else
12044 emit_move_insn (new_with_op0_mode, op0);
12045
12046 op0 = new_rtx;
12047 }
12048 }
12049
12050 op0 = adjust_address (op0, mode, 0);
12051 }
12052
12053 return op0;
12054
12055 case MODIFY_EXPR:
12056 {
12057 tree lhs = treeop0;
12058 tree rhs = treeop1;
12059 gcc_assert (ignore);
12060
12061 /* Check for |= or &= of a bitfield of size one into another bitfield
12062 of size 1. In this case, (unless we need the result of the
12063 assignment) we can do this more efficiently with a
12064 test followed by an assignment, if necessary.
12065
12066 ??? At this point, we can't get a BIT_FIELD_REF here. But if
12067 things change so we do, this code should be enhanced to
12068 support it. */
12069 if (TREE_CODE (lhs) == COMPONENT_REF
12070 && (TREE_CODE (rhs) == BIT_IOR_EXPR
12071 || TREE_CODE (rhs) == BIT_AND_EXPR)
12072 && TREE_OPERAND (rhs, 0) == lhs
12073 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
12074 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
12075 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
12076 {
12077 rtx_code_label *label = gen_label_rtx ();
12078 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
12079 profile_probability prob = profile_probability::uninitialized ();
12080 if (value)
12081 jumpifnot (TREE_OPERAND (rhs, 1), label, prob);
12082 else
12083 jumpif (TREE_OPERAND (rhs, 1), label, prob);
12084 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
12085 false);
12086 do_pending_stack_adjust ();
12087 emit_label (label);
12088 return const0_rtx;
12089 }
12090
12091 expand_assignment (lhs, rhs, false);
12092 return const0_rtx;
12093 }
12094
12095 case ADDR_EXPR:
12096 return expand_expr_addr_expr (exp, target, tmode, modifier);
12097
12098 case REALPART_EXPR:
12099 op0 = expand_normal (treeop0);
12100 return read_complex_part (op0, false);
12101
12102 case IMAGPART_EXPR:
12103 op0 = expand_normal (treeop0);
12104 return read_complex_part (op0, true);
12105
12106 case RETURN_EXPR:
12107 case LABEL_EXPR:
12108 case GOTO_EXPR:
12109 case SWITCH_EXPR:
12110 case ASM_EXPR:
12111 /* Expanded in cfgexpand.cc. */
12112 gcc_unreachable ();
12113
12114 case TRY_CATCH_EXPR:
12115 case CATCH_EXPR:
12116 case EH_FILTER_EXPR:
12117 case TRY_FINALLY_EXPR:
12118 case EH_ELSE_EXPR:
12119 /* Lowered by tree-eh.cc. */
12120 gcc_unreachable ();
12121
12122 case WITH_CLEANUP_EXPR:
12123 case CLEANUP_POINT_EXPR:
12124 case TARGET_EXPR:
12125 case CASE_LABEL_EXPR:
12126 case VA_ARG_EXPR:
12127 case BIND_EXPR:
12128 case INIT_EXPR:
12129 case CONJ_EXPR:
12130 case COMPOUND_EXPR:
12131 case PREINCREMENT_EXPR:
12132 case PREDECREMENT_EXPR:
12133 case POSTINCREMENT_EXPR:
12134 case POSTDECREMENT_EXPR:
12135 case LOOP_EXPR:
12136 case EXIT_EXPR:
12137 case COMPOUND_LITERAL_EXPR:
12138 /* Lowered by gimplify.cc. */
12139 gcc_unreachable ();
12140
12141 case FDESC_EXPR:
12142 /* Function descriptors are not valid except for as
12143 initialization constants, and should not be expanded. */
12144 gcc_unreachable ();
12145
12146 case WITH_SIZE_EXPR:
12147 /* WITH_SIZE_EXPR expands to its first argument. The caller should
12148 have pulled out the size to use in whatever context it needed. */
12149 return expand_expr_real (treeop0, original_target, tmode,
12150 modifier, alt_rtl, inner_reference_p);
12151
12152 default:
12153 return expand_expr_real_2 (&ops, target, tmode, modifier);
12154 }
12155 }
12156 \f
12157 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
12158 signedness of TYPE), possibly returning the result in TARGET.
12159 TYPE is known to be a partial integer type. */
12160 static rtx
12161 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
12162 {
12163 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
12164 HOST_WIDE_INT prec = TYPE_PRECISION (type);
12165 gcc_assert ((GET_MODE (exp) == VOIDmode || GET_MODE (exp) == mode)
12166 && (!target || GET_MODE (target) == mode));
12167
12168 /* For constant values, reduce using wide_int_to_tree. */
12169 if (poly_int_rtx_p (exp))
12170 {
12171 auto value = wi::to_poly_wide (exp, mode);
12172 tree t = wide_int_to_tree (type, value);
12173 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
12174 }
12175 else if (TYPE_UNSIGNED (type))
12176 {
12177 rtx mask = immed_wide_int_const
12178 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
12179 return expand_and (mode, exp, mask, target);
12180 }
12181 else
12182 {
12183 int count = GET_MODE_PRECISION (mode) - prec;
12184 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
12185 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
12186 }
12187 }
12188 \f
12189 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
12190 when applied to the address of EXP produces an address known to be
12191 aligned more than BIGGEST_ALIGNMENT. */
12192
12193 static int
12194 is_aligning_offset (const_tree offset, const_tree exp)
12195 {
12196 /* Strip off any conversions. */
12197 while (CONVERT_EXPR_P (offset))
12198 offset = TREE_OPERAND (offset, 0);
12199
12200 /* We must now have a BIT_AND_EXPR with a constant that is one less than
12201 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
12202 if (TREE_CODE (offset) != BIT_AND_EXPR
12203 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
12204 || compare_tree_int (TREE_OPERAND (offset, 1),
12205 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
12206 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
12207 return 0;
12208
12209 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
12210 It must be NEGATE_EXPR. Then strip any more conversions. */
12211 offset = TREE_OPERAND (offset, 0);
12212 while (CONVERT_EXPR_P (offset))
12213 offset = TREE_OPERAND (offset, 0);
12214
12215 if (TREE_CODE (offset) != NEGATE_EXPR)
12216 return 0;
12217
12218 offset = TREE_OPERAND (offset, 0);
12219 while (CONVERT_EXPR_P (offset))
12220 offset = TREE_OPERAND (offset, 0);
12221
12222 /* This must now be the address of EXP. */
12223 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
12224 }
12225
12226 /* Return a STRING_CST corresponding to ARG's constant initializer either
12227 if it's a string constant, or, when VALREP is set, any other constant,
12228 or null otherwise.
12229 On success, set *PTR_OFFSET to the (possibly non-constant) byte offset
12230 within the byte string that ARG is references. If nonnull set *MEM_SIZE
12231 to the size of the byte string. If nonnull, set *DECL to the constant
12232 declaration ARG refers to. */
12233
12234 static tree
12235 constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl,
12236 bool valrep = false)
12237 {
12238 tree dummy = NULL_TREE;
12239 if (!mem_size)
12240 mem_size = &dummy;
12241
12242 /* Store the type of the original expression before conversions
12243 via NOP_EXPR or POINTER_PLUS_EXPR to other types have been
12244 removed. */
12245 tree argtype = TREE_TYPE (arg);
12246
12247 tree array;
12248 STRIP_NOPS (arg);
12249
12250 /* Non-constant index into the character array in an ARRAY_REF
12251 expression or null. */
12252 tree varidx = NULL_TREE;
12253
12254 poly_int64 base_off = 0;
12255
12256 if (TREE_CODE (arg) == ADDR_EXPR)
12257 {
12258 arg = TREE_OPERAND (arg, 0);
12259 tree ref = arg;
12260 if (TREE_CODE (arg) == ARRAY_REF)
12261 {
12262 tree idx = TREE_OPERAND (arg, 1);
12263 if (TREE_CODE (idx) != INTEGER_CST)
12264 {
12265 /* From a pointer (but not array) argument extract the variable
12266 index to prevent get_addr_base_and_unit_offset() from failing
12267 due to it. Use it later to compute the non-constant offset
12268 into the string and return it to the caller. */
12269 varidx = idx;
12270 ref = TREE_OPERAND (arg, 0);
12271
12272 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12273 return NULL_TREE;
12274
12275 if (!integer_zerop (array_ref_low_bound (arg)))
12276 return NULL_TREE;
12277
12278 if (!integer_onep (array_ref_element_size (arg)))
12279 return NULL_TREE;
12280 }
12281 }
12282 array = get_addr_base_and_unit_offset (ref, &base_off);
12283 if (!array
12284 || (TREE_CODE (array) != VAR_DECL
12285 && TREE_CODE (array) != CONST_DECL
12286 && TREE_CODE (array) != STRING_CST))
12287 return NULL_TREE;
12288 }
12289 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
12290 {
12291 tree arg0 = TREE_OPERAND (arg, 0);
12292 tree arg1 = TREE_OPERAND (arg, 1);
12293
12294 tree offset;
12295 tree str = string_constant (arg0, &offset, mem_size, decl);
12296 if (!str)
12297 {
12298 str = string_constant (arg1, &offset, mem_size, decl);
12299 arg1 = arg0;
12300 }
12301
12302 if (str)
12303 {
12304 /* Avoid pointers to arrays (see bug 86622). */
12305 if (POINTER_TYPE_P (TREE_TYPE (arg))
12306 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE
12307 && !(decl && !*decl)
12308 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12309 && tree_fits_uhwi_p (*mem_size)
12310 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12311 return NULL_TREE;
12312
12313 tree type = TREE_TYPE (offset);
12314 arg1 = fold_convert (type, arg1);
12315 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, arg1);
12316 return str;
12317 }
12318 return NULL_TREE;
12319 }
12320 else if (TREE_CODE (arg) == SSA_NAME)
12321 {
12322 gimple *stmt = SSA_NAME_DEF_STMT (arg);
12323 if (!is_gimple_assign (stmt))
12324 return NULL_TREE;
12325
12326 tree rhs1 = gimple_assign_rhs1 (stmt);
12327 tree_code code = gimple_assign_rhs_code (stmt);
12328 if (code == ADDR_EXPR)
12329 return string_constant (rhs1, ptr_offset, mem_size, decl);
12330 else if (code != POINTER_PLUS_EXPR)
12331 return NULL_TREE;
12332
12333 tree offset;
12334 if (tree str = string_constant (rhs1, &offset, mem_size, decl))
12335 {
12336 /* Avoid pointers to arrays (see bug 86622). */
12337 if (POINTER_TYPE_P (TREE_TYPE (rhs1))
12338 && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE
12339 && !(decl && !*decl)
12340 && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl))
12341 && tree_fits_uhwi_p (*mem_size)
12342 && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl))))
12343 return NULL_TREE;
12344
12345 tree rhs2 = gimple_assign_rhs2 (stmt);
12346 tree type = TREE_TYPE (offset);
12347 rhs2 = fold_convert (type, rhs2);
12348 *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2);
12349 return str;
12350 }
12351 return NULL_TREE;
12352 }
12353 else if (DECL_P (arg))
12354 array = arg;
12355 else
12356 return NULL_TREE;
12357
12358 tree offset = wide_int_to_tree (sizetype, base_off);
12359 if (varidx)
12360 {
12361 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE)
12362 return NULL_TREE;
12363
12364 gcc_assert (TREE_CODE (arg) == ARRAY_REF);
12365 tree chartype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg, 0)));
12366 if (TREE_CODE (chartype) != INTEGER_TYPE)
12367 return NULL;
12368
12369 offset = fold_convert (sizetype, varidx);
12370 }
12371
12372 if (TREE_CODE (array) == STRING_CST)
12373 {
12374 *ptr_offset = fold_convert (sizetype, offset);
12375 *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array));
12376 if (decl)
12377 *decl = NULL_TREE;
12378 gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array)))
12379 >= TREE_STRING_LENGTH (array));
12380 return array;
12381 }
12382
12383 tree init = ctor_for_folding (array);
12384 if (!init || init == error_mark_node)
12385 return NULL_TREE;
12386
12387 if (valrep)
12388 {
12389 HOST_WIDE_INT cstoff;
12390 if (!base_off.is_constant (&cstoff))
12391 return NULL_TREE;
12392
12393 /* Check that the host and target are sane. */
12394 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12395 return NULL_TREE;
12396
12397 HOST_WIDE_INT typesz = int_size_in_bytes (TREE_TYPE (init));
12398 if (typesz <= 0 || (int) typesz != typesz)
12399 return NULL_TREE;
12400
12401 HOST_WIDE_INT size = typesz;
12402 if (VAR_P (array)
12403 && DECL_SIZE_UNIT (array)
12404 && tree_fits_shwi_p (DECL_SIZE_UNIT (array)))
12405 {
12406 size = tree_to_shwi (DECL_SIZE_UNIT (array));
12407 gcc_checking_assert (size >= typesz);
12408 }
12409
12410 /* If value representation was requested convert the initializer
12411 for the whole array or object into a string of bytes forming
12412 its value representation and return it. */
12413 unsigned char *bytes = XNEWVEC (unsigned char, size);
12414 int r = native_encode_initializer (init, bytes, size);
12415 if (r < typesz)
12416 {
12417 XDELETEVEC (bytes);
12418 return NULL_TREE;
12419 }
12420
12421 if (r < size)
12422 memset (bytes + r, '\0', size - r);
12423
12424 const char *p = reinterpret_cast<const char *>(bytes);
12425 init = build_string_literal (size, p, char_type_node);
12426 init = TREE_OPERAND (init, 0);
12427 init = TREE_OPERAND (init, 0);
12428 XDELETE (bytes);
12429
12430 *mem_size = size_int (TREE_STRING_LENGTH (init));
12431 *ptr_offset = wide_int_to_tree (ssizetype, base_off);
12432
12433 if (decl)
12434 *decl = array;
12435
12436 return init;
12437 }
12438
12439 if (TREE_CODE (init) == CONSTRUCTOR)
12440 {
12441 /* Convert the 64-bit constant offset to a wider type to avoid
12442 overflow and use it to obtain the initializer for the subobject
12443 it points into. */
12444 offset_int wioff;
12445 if (!base_off.is_constant (&wioff))
12446 return NULL_TREE;
12447
12448 wioff *= BITS_PER_UNIT;
12449 if (!wi::fits_uhwi_p (wioff))
12450 return NULL_TREE;
12451
12452 base_off = wioff.to_uhwi ();
12453 unsigned HOST_WIDE_INT fieldoff = 0;
12454 init = fold_ctor_reference (TREE_TYPE (arg), init, base_off, 0, array,
12455 &fieldoff);
12456 if (!init || init == error_mark_node)
12457 return NULL_TREE;
12458
12459 HOST_WIDE_INT cstoff;
12460 if (!base_off.is_constant (&cstoff))
12461 return NULL_TREE;
12462
12463 cstoff = (cstoff - fieldoff) / BITS_PER_UNIT;
12464 tree off = build_int_cst (sizetype, cstoff);
12465 if (varidx)
12466 offset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset, off);
12467 else
12468 offset = off;
12469 }
12470
12471 *ptr_offset = offset;
12472
12473 tree inittype = TREE_TYPE (init);
12474
12475 if (TREE_CODE (init) == INTEGER_CST
12476 && (TREE_CODE (TREE_TYPE (array)) == INTEGER_TYPE
12477 || TYPE_MAIN_VARIANT (inittype) == char_type_node))
12478 {
12479 /* Check that the host and target are sane. */
12480 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
12481 return NULL_TREE;
12482
12483 /* For a reference to (address of) a single constant character,
12484 store the native representation of the character in CHARBUF.
12485 If the reference is to an element of an array or a member
12486 of a struct, only consider narrow characters until ctors
12487 for wide character arrays are transformed to STRING_CSTs
12488 like those for narrow arrays. */
12489 unsigned char charbuf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12490 int len = native_encode_expr (init, charbuf, sizeof charbuf, 0);
12491 if (len > 0)
12492 {
12493 /* Construct a string literal with elements of INITTYPE and
12494 the representation above. Then strip
12495 the ADDR_EXPR (ARRAY_REF (...)) around the STRING_CST. */
12496 init = build_string_literal (len, (char *)charbuf, inittype);
12497 init = TREE_OPERAND (TREE_OPERAND (init, 0), 0);
12498 }
12499 }
12500
12501 tree initsize = TYPE_SIZE_UNIT (inittype);
12502
12503 if (TREE_CODE (init) == CONSTRUCTOR && initializer_zerop (init))
12504 {
12505 /* Fold an empty/zero constructor for an implicitly initialized
12506 object or subobject into the empty string. */
12507
12508 /* Determine the character type from that of the original
12509 expression. */
12510 tree chartype = argtype;
12511 if (POINTER_TYPE_P (chartype))
12512 chartype = TREE_TYPE (chartype);
12513 while (TREE_CODE (chartype) == ARRAY_TYPE)
12514 chartype = TREE_TYPE (chartype);
12515
12516 if (INTEGRAL_TYPE_P (chartype)
12517 && TYPE_PRECISION (chartype) == TYPE_PRECISION (char_type_node))
12518 {
12519 /* Convert a char array to an empty STRING_CST having an array
12520 of the expected type and size. */
12521 if (!initsize)
12522 initsize = integer_zero_node;
12523
12524 unsigned HOST_WIDE_INT size = tree_to_uhwi (initsize);
12525 if (size > (unsigned HOST_WIDE_INT) INT_MAX)
12526 return NULL_TREE;
12527
12528 init = build_string_literal (size, NULL, chartype, size);
12529 init = TREE_OPERAND (init, 0);
12530 init = TREE_OPERAND (init, 0);
12531
12532 *ptr_offset = integer_zero_node;
12533 }
12534 }
12535
12536 if (decl)
12537 *decl = array;
12538
12539 if (TREE_CODE (init) != STRING_CST)
12540 return NULL_TREE;
12541
12542 *mem_size = initsize;
12543
12544 gcc_checking_assert (tree_to_shwi (initsize) >= TREE_STRING_LENGTH (init));
12545
12546 return init;
12547 }
12548
12549 /* Return STRING_CST if an ARG corresponds to a string constant or zero
12550 if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly
12551 non-constant) offset in bytes within the string that ARG is accessing.
12552 If MEM_SIZE is non-zero the storage size of the memory is returned.
12553 If DECL is non-zero the constant declaration is returned if available. */
12554
12555 tree
12556 string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12557 {
12558 return constant_byte_string (arg, ptr_offset, mem_size, decl, false);
12559 }
12560
12561 /* Similar to string_constant, return a STRING_CST corresponding
12562 to the value representation of the first argument if it's
12563 a constant. */
12564
12565 tree
12566 byte_representation (tree arg, tree *ptr_offset, tree *mem_size, tree *decl)
12567 {
12568 return constant_byte_string (arg, ptr_offset, mem_size, decl, true);
12569 }
12570
12571 /* Optimize x % C1 == C2 for signed modulo if C1 is a power of two and C2
12572 is non-zero and C3 ((1<<(prec-1)) | (C1 - 1)):
12573 for C2 > 0 to x & C3 == C2
12574 for C2 < 0 to x & C3 == (C2 & C3). */
12575 enum tree_code
12576 maybe_optimize_pow2p_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12577 {
12578 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12579 tree treeop0 = gimple_assign_rhs1 (stmt);
12580 tree treeop1 = gimple_assign_rhs2 (stmt);
12581 tree type = TREE_TYPE (*arg0);
12582 scalar_int_mode mode;
12583 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12584 return code;
12585 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12586 || TYPE_PRECISION (type) <= 1
12587 || TYPE_UNSIGNED (type)
12588 /* Signed x % c == 0 should have been optimized into unsigned modulo
12589 earlier. */
12590 || integer_zerop (*arg1)
12591 /* If c is known to be non-negative, modulo will be expanded as unsigned
12592 modulo. */
12593 || get_range_pos_neg (treeop0) == 1)
12594 return code;
12595
12596 /* x % c == d where d < 0 && d <= -c should be always false. */
12597 if (tree_int_cst_sgn (*arg1) == -1
12598 && -wi::to_widest (treeop1) >= wi::to_widest (*arg1))
12599 return code;
12600
12601 int prec = TYPE_PRECISION (type);
12602 wide_int w = wi::to_wide (treeop1) - 1;
12603 w |= wi::shifted_mask (0, prec - 1, true, prec);
12604 tree c3 = wide_int_to_tree (type, w);
12605 tree c4 = *arg1;
12606 if (tree_int_cst_sgn (*arg1) == -1)
12607 c4 = wide_int_to_tree (type, w & wi::to_wide (*arg1));
12608
12609 rtx op0 = expand_normal (treeop0);
12610 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12611
12612 bool speed_p = optimize_insn_for_speed_p ();
12613
12614 do_pending_stack_adjust ();
12615
12616 location_t loc = gimple_location (stmt);
12617 struct separate_ops ops;
12618 ops.code = TRUNC_MOD_EXPR;
12619 ops.location = loc;
12620 ops.type = TREE_TYPE (treeop0);
12621 ops.op0 = treeop0;
12622 ops.op1 = treeop1;
12623 ops.op2 = NULL_TREE;
12624 start_sequence ();
12625 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12626 EXPAND_NORMAL);
12627 rtx_insn *moinsns = get_insns ();
12628 end_sequence ();
12629
12630 unsigned mocost = seq_cost (moinsns, speed_p);
12631 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12632 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12633
12634 ops.code = BIT_AND_EXPR;
12635 ops.location = loc;
12636 ops.type = TREE_TYPE (treeop0);
12637 ops.op0 = treeop0;
12638 ops.op1 = c3;
12639 ops.op2 = NULL_TREE;
12640 start_sequence ();
12641 rtx mur = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12642 EXPAND_NORMAL);
12643 rtx_insn *muinsns = get_insns ();
12644 end_sequence ();
12645
12646 unsigned mucost = seq_cost (muinsns, speed_p);
12647 mucost += rtx_cost (mur, mode, EQ, 0, speed_p);
12648 mucost += rtx_cost (expand_normal (c4), mode, EQ, 1, speed_p);
12649
12650 if (mocost <= mucost)
12651 {
12652 emit_insn (moinsns);
12653 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12654 return code;
12655 }
12656
12657 emit_insn (muinsns);
12658 *arg0 = make_tree (TREE_TYPE (*arg0), mur);
12659 *arg1 = c4;
12660 return code;
12661 }
12662
12663 /* Attempt to optimize unsigned (X % C1) == C2 (or (X % C1) != C2).
12664 If C1 is odd to:
12665 (X - C2) * C3 <= C4 (or >), where
12666 C3 is modular multiplicative inverse of C1 and 1<<prec and
12667 C4 is ((1<<prec) - 1) / C1 or ((1<<prec) - 1) / C1 - 1 (the latter
12668 if C2 > ((1<<prec) - 1) % C1).
12669 If C1 is even, S = ctz (C1) and C2 is 0, use
12670 ((X * C3) r>> S) <= C4, where C3 is modular multiplicative
12671 inverse of C1>>S and 1<<prec and C4 is (((1<<prec) - 1) / (C1>>S)) >> S.
12672
12673 For signed (X % C1) == 0 if C1 is odd to (all operations in it
12674 unsigned):
12675 (X * C3) + C4 <= 2 * C4, where
12676 C3 is modular multiplicative inverse of (unsigned) C1 and 1<<prec and
12677 C4 is ((1<<(prec - 1) - 1) / C1).
12678 If C1 is even, S = ctz(C1), use
12679 ((X * C3) + C4) r>> S <= (C4 >> (S - 1))
12680 where C3 is modular multiplicative inverse of (unsigned)(C1>>S) and 1<<prec
12681 and C4 is ((1<<(prec - 1) - 1) / (C1>>S)) & (-1<<S).
12682
12683 See the Hacker's Delight book, section 10-17. */
12684 enum tree_code
12685 maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1)
12686 {
12687 gcc_checking_assert (code == EQ_EXPR || code == NE_EXPR);
12688 gcc_checking_assert (TREE_CODE (*arg1) == INTEGER_CST);
12689
12690 if (optimize < 2)
12691 return code;
12692
12693 gimple *stmt = get_def_for_expr (*arg0, TRUNC_MOD_EXPR);
12694 if (stmt == NULL)
12695 return code;
12696
12697 tree treeop0 = gimple_assign_rhs1 (stmt);
12698 tree treeop1 = gimple_assign_rhs2 (stmt);
12699 if (TREE_CODE (treeop0) != SSA_NAME
12700 || TREE_CODE (treeop1) != INTEGER_CST
12701 /* Don't optimize the undefined behavior case x % 0;
12702 x % 1 should have been optimized into zero, punt if
12703 it makes it here for whatever reason;
12704 x % -c should have been optimized into x % c. */
12705 || compare_tree_int (treeop1, 2) <= 0
12706 /* Likewise x % c == d where d >= c should be always false. */
12707 || tree_int_cst_le (treeop1, *arg1))
12708 return code;
12709
12710 /* Unsigned x % pow2 is handled right already, for signed
12711 modulo handle it in maybe_optimize_pow2p_mod_cmp. */
12712 if (integer_pow2p (treeop1))
12713 return maybe_optimize_pow2p_mod_cmp (code, arg0, arg1);
12714
12715 tree type = TREE_TYPE (*arg0);
12716 scalar_int_mode mode;
12717 if (!is_a <scalar_int_mode> (TYPE_MODE (type), &mode))
12718 return code;
12719 if (GET_MODE_BITSIZE (mode) != TYPE_PRECISION (type)
12720 || TYPE_PRECISION (type) <= 1)
12721 return code;
12722
12723 signop sgn = UNSIGNED;
12724 /* If both operands are known to have the sign bit clear, handle
12725 even the signed modulo case as unsigned. treeop1 is always
12726 positive >= 2, checked above. */
12727 if (!TYPE_UNSIGNED (type) && get_range_pos_neg (treeop0) != 1)
12728 sgn = SIGNED;
12729
12730 if (!TYPE_UNSIGNED (type))
12731 {
12732 if (tree_int_cst_sgn (*arg1) == -1)
12733 return code;
12734 type = unsigned_type_for (type);
12735 if (!type || TYPE_MODE (type) != TYPE_MODE (TREE_TYPE (*arg0)))
12736 return code;
12737 }
12738
12739 int prec = TYPE_PRECISION (type);
12740 wide_int w = wi::to_wide (treeop1);
12741 int shift = wi::ctz (w);
12742 /* Unsigned (X % C1) == C2 is equivalent to (X - C2) % C1 == 0 if
12743 C2 <= -1U % C1, because for any Z >= 0U - C2 in that case (Z % C1) != 0.
12744 If C1 is odd, we can handle all cases by subtracting
12745 C4 below. We could handle even the even C1 and C2 > -1U % C1 cases
12746 e.g. by testing for overflow on the subtraction, punt on that for now
12747 though. */
12748 if ((sgn == SIGNED || shift) && !integer_zerop (*arg1))
12749 {
12750 if (sgn == SIGNED)
12751 return code;
12752 wide_int x = wi::umod_trunc (wi::mask (prec, false, prec), w);
12753 if (wi::gtu_p (wi::to_wide (*arg1), x))
12754 return code;
12755 }
12756
12757 imm_use_iterator imm_iter;
12758 use_operand_p use_p;
12759 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, treeop0)
12760 {
12761 gimple *use_stmt = USE_STMT (use_p);
12762 /* Punt if treeop0 is used in the same bb in a division
12763 or another modulo with the same divisor. We should expect
12764 the division and modulo combined together. */
12765 if (use_stmt == stmt
12766 || gimple_bb (use_stmt) != gimple_bb (stmt))
12767 continue;
12768 if (!is_gimple_assign (use_stmt)
12769 || (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR
12770 && gimple_assign_rhs_code (use_stmt) != TRUNC_MOD_EXPR))
12771 continue;
12772 if (gimple_assign_rhs1 (use_stmt) != treeop0
12773 || !operand_equal_p (gimple_assign_rhs2 (use_stmt), treeop1, 0))
12774 continue;
12775 return code;
12776 }
12777
12778 w = wi::lrshift (w, shift);
12779 wide_int a = wide_int::from (w, prec + 1, UNSIGNED);
12780 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
12781 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
12782 tree c3 = wide_int_to_tree (type, m);
12783 tree c5 = NULL_TREE;
12784 wide_int d, e;
12785 if (sgn == UNSIGNED)
12786 {
12787 d = wi::divmod_trunc (wi::mask (prec, false, prec), w, UNSIGNED, &e);
12788 /* Use <= floor ((1<<prec) - 1) / C1 only if C2 <= ((1<<prec) - 1) % C1,
12789 otherwise use < or subtract one from C4. E.g. for
12790 x % 3U == 0 we transform this into x * 0xaaaaaaab <= 0x55555555, but
12791 x % 3U == 1 already needs to be
12792 (x - 1) * 0xaaaaaaabU <= 0x55555554. */
12793 if (!shift && wi::gtu_p (wi::to_wide (*arg1), e))
12794 d -= 1;
12795 if (shift)
12796 d = wi::lrshift (d, shift);
12797 }
12798 else
12799 {
12800 e = wi::udiv_trunc (wi::mask (prec - 1, false, prec), w);
12801 if (!shift)
12802 d = wi::lshift (e, 1);
12803 else
12804 {
12805 e = wi::bit_and (e, wi::mask (shift, true, prec));
12806 d = wi::lrshift (e, shift - 1);
12807 }
12808 c5 = wide_int_to_tree (type, e);
12809 }
12810 tree c4 = wide_int_to_tree (type, d);
12811
12812 rtx op0 = expand_normal (treeop0);
12813 treeop0 = make_tree (TREE_TYPE (treeop0), op0);
12814
12815 bool speed_p = optimize_insn_for_speed_p ();
12816
12817 do_pending_stack_adjust ();
12818
12819 location_t loc = gimple_location (stmt);
12820 struct separate_ops ops;
12821 ops.code = TRUNC_MOD_EXPR;
12822 ops.location = loc;
12823 ops.type = TREE_TYPE (treeop0);
12824 ops.op0 = treeop0;
12825 ops.op1 = treeop1;
12826 ops.op2 = NULL_TREE;
12827 start_sequence ();
12828 rtx mor = expand_expr_real_2 (&ops, NULL_RTX, TYPE_MODE (ops.type),
12829 EXPAND_NORMAL);
12830 rtx_insn *moinsns = get_insns ();
12831 end_sequence ();
12832
12833 unsigned mocost = seq_cost (moinsns, speed_p);
12834 mocost += rtx_cost (mor, mode, EQ, 0, speed_p);
12835 mocost += rtx_cost (expand_normal (*arg1), mode, EQ, 1, speed_p);
12836
12837 tree t = fold_convert_loc (loc, type, treeop0);
12838 if (!integer_zerop (*arg1))
12839 t = fold_build2_loc (loc, MINUS_EXPR, type, t, fold_convert (type, *arg1));
12840 t = fold_build2_loc (loc, MULT_EXPR, type, t, c3);
12841 if (sgn == SIGNED)
12842 t = fold_build2_loc (loc, PLUS_EXPR, type, t, c5);
12843 if (shift)
12844 {
12845 tree s = build_int_cst (NULL_TREE, shift);
12846 t = fold_build2_loc (loc, RROTATE_EXPR, type, t, s);
12847 }
12848
12849 start_sequence ();
12850 rtx mur = expand_normal (t);
12851 rtx_insn *muinsns = get_insns ();
12852 end_sequence ();
12853
12854 unsigned mucost = seq_cost (muinsns, speed_p);
12855 mucost += rtx_cost (mur, mode, LE, 0, speed_p);
12856 mucost += rtx_cost (expand_normal (c4), mode, LE, 1, speed_p);
12857
12858 if (mocost <= mucost)
12859 {
12860 emit_insn (moinsns);
12861 *arg0 = make_tree (TREE_TYPE (*arg0), mor);
12862 return code;
12863 }
12864
12865 emit_insn (muinsns);
12866 *arg0 = make_tree (type, mur);
12867 *arg1 = c4;
12868 return code == EQ_EXPR ? LE_EXPR : GT_EXPR;
12869 }
12870
12871 /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */
12872
12873 void
12874 maybe_optimize_sub_cmp_0 (enum tree_code code, tree *arg0, tree *arg1)
12875 {
12876 gcc_checking_assert (code == GT_EXPR || code == GE_EXPR
12877 || code == LT_EXPR || code == LE_EXPR);
12878 gcc_checking_assert (integer_zerop (*arg1));
12879
12880 if (!optimize)
12881 return;
12882
12883 gimple *stmt = get_def_for_expr (*arg0, MINUS_EXPR);
12884 if (stmt == NULL)
12885 return;
12886
12887 tree treeop0 = gimple_assign_rhs1 (stmt);
12888 tree treeop1 = gimple_assign_rhs2 (stmt);
12889 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (treeop0)))
12890 return;
12891
12892 if (issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_COMPARISON))
12893 warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
12894 "assuming signed overflow does not occur when "
12895 "simplifying %<X - Y %s 0%> to %<X %s Y%>",
12896 op_symbol_code (code), op_symbol_code (code));
12897
12898 *arg0 = treeop0;
12899 *arg1 = treeop1;
12900 }
12901 \f
12902 /* Generate code to calculate OPS, and exploded expression
12903 using a store-flag instruction and return an rtx for the result.
12904 OPS reflects a comparison.
12905
12906 If TARGET is nonzero, store the result there if convenient.
12907
12908 Return zero if there is no suitable set-flag instruction
12909 available on this machine.
12910
12911 Once expand_expr has been called on the arguments of the comparison,
12912 we are committed to doing the store flag, since it is not safe to
12913 re-evaluate the expression. We emit the store-flag insn by calling
12914 emit_store_flag, but only expand the arguments if we have a reason
12915 to believe that emit_store_flag will be successful. If we think that
12916 it will, but it isn't, we have to simulate the store-flag with a
12917 set/jump/set sequence. */
12918
12919 static rtx
12920 do_store_flag (sepops ops, rtx target, machine_mode mode)
12921 {
12922 enum rtx_code code;
12923 tree arg0, arg1, type;
12924 machine_mode operand_mode;
12925 int unsignedp;
12926 rtx op0, op1;
12927 rtx subtarget = target;
12928 location_t loc = ops->location;
12929
12930 arg0 = ops->op0;
12931 arg1 = ops->op1;
12932
12933 /* Don't crash if the comparison was erroneous. */
12934 if (arg0 == error_mark_node || arg1 == error_mark_node)
12935 return const0_rtx;
12936
12937 type = TREE_TYPE (arg0);
12938 operand_mode = TYPE_MODE (type);
12939 unsignedp = TYPE_UNSIGNED (type);
12940
12941 /* We won't bother with BLKmode store-flag operations because it would mean
12942 passing a lot of information to emit_store_flag. */
12943 if (operand_mode == BLKmode)
12944 return 0;
12945
12946 /* We won't bother with store-flag operations involving function pointers
12947 when function pointers must be canonicalized before comparisons. */
12948 if (targetm.have_canonicalize_funcptr_for_compare ()
12949 && ((POINTER_TYPE_P (TREE_TYPE (arg0))
12950 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg0))))
12951 || (POINTER_TYPE_P (TREE_TYPE (arg1))
12952 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (arg1))))))
12953 return 0;
12954
12955 STRIP_NOPS (arg0);
12956 STRIP_NOPS (arg1);
12957
12958 /* For vector typed comparisons emit code to generate the desired
12959 all-ones or all-zeros mask. */
12960 if (TREE_CODE (ops->type) == VECTOR_TYPE)
12961 {
12962 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
12963 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
12964 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
12965 return expand_vec_cmp_expr (ops->type, ifexp, target);
12966 else
12967 gcc_unreachable ();
12968 }
12969
12970 /* Optimize (x % C1) == C2 or (x % C1) != C2 if it is beneficial
12971 into (x - C2) * C3 < C4. */
12972 if ((ops->code == EQ_EXPR || ops->code == NE_EXPR)
12973 && TREE_CODE (arg0) == SSA_NAME
12974 && TREE_CODE (arg1) == INTEGER_CST)
12975 {
12976 enum tree_code new_code = maybe_optimize_mod_cmp (ops->code,
12977 &arg0, &arg1);
12978 if (new_code != ops->code)
12979 {
12980 struct separate_ops nops = *ops;
12981 nops.code = ops->code = new_code;
12982 nops.op0 = arg0;
12983 nops.op1 = arg1;
12984 nops.type = TREE_TYPE (arg0);
12985 return do_store_flag (&nops, target, mode);
12986 }
12987 }
12988
12989 /* Optimize (x - y) < 0 into x < y if x - y has undefined overflow. */
12990 if (!unsignedp
12991 && (ops->code == LT_EXPR || ops->code == LE_EXPR
12992 || ops->code == GT_EXPR || ops->code == GE_EXPR)
12993 && integer_zerop (arg1)
12994 && TREE_CODE (arg0) == SSA_NAME)
12995 maybe_optimize_sub_cmp_0 (ops->code, &arg0, &arg1);
12996
12997 /* Get the rtx comparison code to use. We know that EXP is a comparison
12998 operation of some type. Some comparisons against 1 and -1 can be
12999 converted to comparisons with zero. Do so here so that the tests
13000 below will be aware that we have a comparison with zero. These
13001 tests will not catch constants in the first operand, but constants
13002 are rarely passed as the first operand. */
13003
13004 switch (ops->code)
13005 {
13006 case EQ_EXPR:
13007 code = EQ;
13008 break;
13009 case NE_EXPR:
13010 code = NE;
13011 break;
13012 case LT_EXPR:
13013 if (integer_onep (arg1))
13014 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
13015 else
13016 code = unsignedp ? LTU : LT;
13017 break;
13018 case LE_EXPR:
13019 if (! unsignedp && integer_all_onesp (arg1))
13020 arg1 = integer_zero_node, code = LT;
13021 else
13022 code = unsignedp ? LEU : LE;
13023 break;
13024 case GT_EXPR:
13025 if (! unsignedp && integer_all_onesp (arg1))
13026 arg1 = integer_zero_node, code = GE;
13027 else
13028 code = unsignedp ? GTU : GT;
13029 break;
13030 case GE_EXPR:
13031 if (integer_onep (arg1))
13032 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
13033 else
13034 code = unsignedp ? GEU : GE;
13035 break;
13036
13037 case UNORDERED_EXPR:
13038 code = UNORDERED;
13039 break;
13040 case ORDERED_EXPR:
13041 code = ORDERED;
13042 break;
13043 case UNLT_EXPR:
13044 code = UNLT;
13045 break;
13046 case UNLE_EXPR:
13047 code = UNLE;
13048 break;
13049 case UNGT_EXPR:
13050 code = UNGT;
13051 break;
13052 case UNGE_EXPR:
13053 code = UNGE;
13054 break;
13055 case UNEQ_EXPR:
13056 code = UNEQ;
13057 break;
13058 case LTGT_EXPR:
13059 code = LTGT;
13060 break;
13061
13062 default:
13063 gcc_unreachable ();
13064 }
13065
13066 /* Put a constant second. */
13067 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
13068 || TREE_CODE (arg0) == FIXED_CST)
13069 {
13070 std::swap (arg0, arg1);
13071 code = swap_condition (code);
13072 }
13073
13074 /* If this is an equality or inequality test of a single bit, we can
13075 do this by shifting the bit being tested to the low-order bit and
13076 masking the result with the constant 1. If the condition was EQ,
13077 we xor it with 1. This does not require an scc insn and is faster
13078 than an scc insn even if we have it.
13079
13080 The code to make this transformation was moved into fold_single_bit_test,
13081 so we just call into the folder and expand its result. */
13082
13083 if ((code == NE || code == EQ)
13084 && integer_zerop (arg1)
13085 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
13086 {
13087 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
13088 if (srcstmt
13089 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
13090 {
13091 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
13092 type = lang_hooks.types.type_for_mode (mode, unsignedp);
13093 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
13094 gimple_assign_rhs1 (srcstmt),
13095 gimple_assign_rhs2 (srcstmt));
13096 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
13097 if (temp)
13098 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
13099 }
13100 }
13101
13102 if (! get_subtarget (target)
13103 || GET_MODE (subtarget) != operand_mode)
13104 subtarget = 0;
13105
13106 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
13107
13108 if (target == 0)
13109 target = gen_reg_rtx (mode);
13110
13111 /* Try a cstore if possible. */
13112 return emit_store_flag_force (target, code, op0, op1,
13113 operand_mode, unsignedp,
13114 (TYPE_PRECISION (ops->type) == 1
13115 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
13116 }
13117 \f
13118 /* Attempt to generate a casesi instruction. Returns 1 if successful,
13119 0 otherwise (i.e. if there is no casesi instruction).
13120
13121 DEFAULT_PROBABILITY is the probability of jumping to the default
13122 label. */
13123 int
13124 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
13125 rtx table_label, rtx default_label, rtx fallback_label,
13126 profile_probability default_probability)
13127 {
13128 class expand_operand ops[5];
13129 scalar_int_mode index_mode = SImode;
13130 rtx op1, op2, index;
13131
13132 if (! targetm.have_casesi ())
13133 return 0;
13134
13135 /* The index must be some form of integer. Convert it to SImode. */
13136 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
13137 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
13138 {
13139 rtx rangertx = expand_normal (range);
13140
13141 /* We must handle the endpoints in the original mode. */
13142 index_expr = build2 (MINUS_EXPR, index_type,
13143 index_expr, minval);
13144 minval = integer_zero_node;
13145 index = expand_normal (index_expr);
13146 if (default_label)
13147 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
13148 omode, 1, default_label,
13149 default_probability);
13150 /* Now we can safely truncate. */
13151 index = convert_to_mode (index_mode, index, 0);
13152 }
13153 else
13154 {
13155 if (omode != index_mode)
13156 {
13157 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
13158 index_expr = fold_convert (index_type, index_expr);
13159 }
13160
13161 index = expand_normal (index_expr);
13162 }
13163
13164 do_pending_stack_adjust ();
13165
13166 op1 = expand_normal (minval);
13167 op2 = expand_normal (range);
13168
13169 create_input_operand (&ops[0], index, index_mode);
13170 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
13171 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
13172 create_fixed_operand (&ops[3], table_label);
13173 create_fixed_operand (&ops[4], (default_label
13174 ? default_label
13175 : fallback_label));
13176 expand_jump_insn (targetm.code_for_casesi, 5, ops);
13177 return 1;
13178 }
13179
13180 /* Attempt to generate a tablejump instruction; same concept. */
13181 /* Subroutine of the next function.
13182
13183 INDEX is the value being switched on, with the lowest value
13184 in the table already subtracted.
13185 MODE is its expected mode (needed if INDEX is constant).
13186 RANGE is the length of the jump table.
13187 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
13188
13189 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
13190 index value is out of range.
13191 DEFAULT_PROBABILITY is the probability of jumping to
13192 the default label. */
13193
13194 static void
13195 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
13196 rtx default_label, profile_probability default_probability)
13197 {
13198 rtx temp, vector;
13199
13200 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
13201 cfun->cfg->max_jumptable_ents = INTVAL (range);
13202
13203 /* Do an unsigned comparison (in the proper mode) between the index
13204 expression and the value which represents the length of the range.
13205 Since we just finished subtracting the lower bound of the range
13206 from the index expression, this comparison allows us to simultaneously
13207 check that the original index expression value is both greater than
13208 or equal to the minimum value of the range and less than or equal to
13209 the maximum value of the range. */
13210
13211 if (default_label)
13212 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
13213 default_label, default_probability);
13214
13215 /* If index is in range, it must fit in Pmode.
13216 Convert to Pmode so we can index with it. */
13217 if (mode != Pmode)
13218 {
13219 unsigned int width;
13220
13221 /* We know the value of INDEX is between 0 and RANGE. If we have a
13222 sign-extended subreg, and RANGE does not have the sign bit set, then
13223 we have a value that is valid for both sign and zero extension. In
13224 this case, we get better code if we sign extend. */
13225 if (GET_CODE (index) == SUBREG
13226 && SUBREG_PROMOTED_VAR_P (index)
13227 && SUBREG_PROMOTED_SIGNED_P (index)
13228 && ((width = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode)))
13229 <= HOST_BITS_PER_WIDE_INT)
13230 && ! (UINTVAL (range) & (HOST_WIDE_INT_1U << (width - 1))))
13231 index = convert_to_mode (Pmode, index, 0);
13232 else
13233 index = convert_to_mode (Pmode, index, 1);
13234 }
13235
13236 /* Don't let a MEM slip through, because then INDEX that comes
13237 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
13238 and break_out_memory_refs will go to work on it and mess it up. */
13239 #ifdef PIC_CASE_VECTOR_ADDRESS
13240 if (flag_pic && !REG_P (index))
13241 index = copy_to_mode_reg (Pmode, index);
13242 #endif
13243
13244 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
13245 GET_MODE_SIZE, because this indicates how large insns are. The other
13246 uses should all be Pmode, because they are addresses. This code
13247 could fail if addresses and insns are not the same size. */
13248 index = simplify_gen_binary (MULT, Pmode, index,
13249 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
13250 Pmode));
13251 index = simplify_gen_binary (PLUS, Pmode, index,
13252 gen_rtx_LABEL_REF (Pmode, table_label));
13253
13254 #ifdef PIC_CASE_VECTOR_ADDRESS
13255 if (flag_pic)
13256 index = PIC_CASE_VECTOR_ADDRESS (index);
13257 else
13258 #endif
13259 index = memory_address (CASE_VECTOR_MODE, index);
13260 temp = gen_reg_rtx (CASE_VECTOR_MODE);
13261 vector = gen_const_mem (CASE_VECTOR_MODE, index);
13262 convert_move (temp, vector, 0);
13263
13264 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
13265
13266 /* If we are generating PIC code or if the table is PC-relative, the
13267 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
13268 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
13269 emit_barrier ();
13270 }
13271
13272 int
13273 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
13274 rtx table_label, rtx default_label,
13275 profile_probability default_probability)
13276 {
13277 rtx index;
13278
13279 if (! targetm.have_tablejump ())
13280 return 0;
13281
13282 index_expr = fold_build2 (MINUS_EXPR, index_type,
13283 fold_convert (index_type, index_expr),
13284 fold_convert (index_type, minval));
13285 index = expand_normal (index_expr);
13286 do_pending_stack_adjust ();
13287
13288 do_tablejump (index, TYPE_MODE (index_type),
13289 convert_modes (TYPE_MODE (index_type),
13290 TYPE_MODE (TREE_TYPE (range)),
13291 expand_normal (range),
13292 TYPE_UNSIGNED (TREE_TYPE (range))),
13293 table_label, default_label, default_probability);
13294 return 1;
13295 }
13296
13297 /* Return a CONST_VECTOR rtx representing vector mask for
13298 a VECTOR_CST of booleans. */
13299 static rtx
13300 const_vector_mask_from_tree (tree exp)
13301 {
13302 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13303 machine_mode inner = GET_MODE_INNER (mode);
13304
13305 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13306 VECTOR_CST_NELTS_PER_PATTERN (exp));
13307 unsigned int count = builder.encoded_nelts ();
13308 for (unsigned int i = 0; i < count; ++i)
13309 {
13310 tree elt = VECTOR_CST_ELT (exp, i);
13311 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
13312 if (integer_zerop (elt))
13313 builder.quick_push (CONST0_RTX (inner));
13314 else if (integer_onep (elt)
13315 || integer_minus_onep (elt))
13316 builder.quick_push (CONSTM1_RTX (inner));
13317 else
13318 gcc_unreachable ();
13319 }
13320 return builder.build ();
13321 }
13322
13323 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
13324 static rtx
13325 const_vector_from_tree (tree exp)
13326 {
13327 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
13328
13329 if (initializer_zerop (exp))
13330 return CONST0_RTX (mode);
13331
13332 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
13333 return const_vector_mask_from_tree (exp);
13334
13335 machine_mode inner = GET_MODE_INNER (mode);
13336
13337 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
13338 VECTOR_CST_NELTS_PER_PATTERN (exp));
13339 unsigned int count = builder.encoded_nelts ();
13340 for (unsigned int i = 0; i < count; ++i)
13341 {
13342 tree elt = VECTOR_CST_ELT (exp, i);
13343 if (TREE_CODE (elt) == REAL_CST)
13344 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
13345 inner));
13346 else if (TREE_CODE (elt) == FIXED_CST)
13347 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
13348 inner));
13349 else
13350 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
13351 inner));
13352 }
13353 return builder.build ();
13354 }
13355
13356 /* Build a decl for a personality function given a language prefix. */
13357
13358 tree
13359 build_personality_function (const char *lang)
13360 {
13361 const char *unwind_and_version;
13362 tree decl, type;
13363 char *name;
13364
13365 switch (targetm_common.except_unwind_info (&global_options))
13366 {
13367 case UI_NONE:
13368 return NULL;
13369 case UI_SJLJ:
13370 unwind_and_version = "_sj0";
13371 break;
13372 case UI_DWARF2:
13373 case UI_TARGET:
13374 unwind_and_version = "_v0";
13375 break;
13376 case UI_SEH:
13377 unwind_and_version = "_seh0";
13378 break;
13379 default:
13380 gcc_unreachable ();
13381 }
13382
13383 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
13384
13385 type = build_function_type_list (unsigned_type_node,
13386 integer_type_node, integer_type_node,
13387 long_long_unsigned_type_node,
13388 ptr_type_node, ptr_type_node, NULL_TREE);
13389 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
13390 get_identifier (name), type);
13391 DECL_ARTIFICIAL (decl) = 1;
13392 DECL_EXTERNAL (decl) = 1;
13393 TREE_PUBLIC (decl) = 1;
13394
13395 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
13396 are the flags assigned by targetm.encode_section_info. */
13397 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
13398
13399 return decl;
13400 }
13401
13402 /* Extracts the personality function of DECL and returns the corresponding
13403 libfunc. */
13404
13405 rtx
13406 get_personality_function (tree decl)
13407 {
13408 tree personality = DECL_FUNCTION_PERSONALITY (decl);
13409 enum eh_personality_kind pk;
13410
13411 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
13412 if (pk == eh_personality_none)
13413 return NULL;
13414
13415 if (!personality
13416 && pk == eh_personality_any)
13417 personality = lang_hooks.eh_personality ();
13418
13419 if (pk == eh_personality_lang)
13420 gcc_assert (personality != NULL_TREE);
13421
13422 return XEXP (DECL_RTL (personality), 0);
13423 }
13424
13425 /* Returns a tree for the size of EXP in bytes. */
13426
13427 static tree
13428 tree_expr_size (const_tree exp)
13429 {
13430 if (DECL_P (exp)
13431 && DECL_SIZE_UNIT (exp) != 0)
13432 return DECL_SIZE_UNIT (exp);
13433 else
13434 return size_in_bytes (TREE_TYPE (exp));
13435 }
13436
13437 /* Return an rtx for the size in bytes of the value of EXP. */
13438
13439 rtx
13440 expr_size (tree exp)
13441 {
13442 tree size;
13443
13444 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13445 size = TREE_OPERAND (exp, 1);
13446 else
13447 {
13448 size = tree_expr_size (exp);
13449 gcc_assert (size);
13450 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
13451 }
13452
13453 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
13454 }
13455
13456 /* Return a wide integer for the size in bytes of the value of EXP, or -1
13457 if the size can vary or is larger than an integer. */
13458
13459 HOST_WIDE_INT
13460 int_expr_size (const_tree exp)
13461 {
13462 tree size;
13463
13464 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
13465 size = TREE_OPERAND (exp, 1);
13466 else
13467 {
13468 size = tree_expr_size (exp);
13469 gcc_assert (size);
13470 }
13471
13472 if (size == 0 || !tree_fits_shwi_p (size))
13473 return -1;
13474
13475 return tree_to_shwi (size);
13476 }
This page took 0.609709 seconds and 5 git commands to generate.