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